ql4_os.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/slab.h>
  9. #include <scsi/scsi_tcq.h>
  10. #include <scsi/scsicam.h>
  11. #include "ql4_def.h"
  12. #include "ql4_version.h"
  13. #include "ql4_glbl.h"
  14. #include "ql4_dbg.h"
  15. #include "ql4_inline.h"
  16. /*
  17. * Driver version
  18. */
  19. static char qla4xxx_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static struct kmem_cache *srb_cachep;
  24. /*
  25. * Module parameter information and variables
  26. */
  27. int ql4xdontresethba = 0;
  28. module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(ql4xdontresethba,
  30. "Don't reset the HBA for driver recovery \n"
  31. " 0 - It will reset HBA (Default)\n"
  32. " 1 - It will NOT reset HBA");
  33. int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
  34. module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
  35. MODULE_PARM_DESC(ql4xextended_error_logging,
  36. "Option to enable extended error logging, "
  37. "Default is 0 - no logging, 1 - debug logging");
  38. int ql4xenablemsix = 1;
  39. module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  40. MODULE_PARM_DESC(ql4xenablemsix,
  41. "Set to enable MSI or MSI-X interrupt mechanism.\n"
  42. " 0 = enable INTx interrupt mechanism.\n"
  43. " 1 = enable MSI-X interrupt mechanism (Default).\n"
  44. " 2 = enable MSI interrupt mechanism.");
  45. #define QL4_DEF_QDEPTH 32
  46. static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
  47. module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(ql4xmaxqdepth,
  49. "Maximum queue depth to report for target devices.\n"
  50. " Default: 32.");
  51. static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
  52. module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
  53. MODULE_PARM_DESC(ql4xsess_recovery_tmo,
  54. "Target Session Recovery Timeout.\n"
  55. " Default: 30 sec.");
  56. /*
  57. * SCSI host template entry points
  58. */
  59. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
  60. /*
  61. * iSCSI template entry points
  62. */
  63. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  64. enum iscsi_tgt_dscvr type, uint32_t enable,
  65. struct sockaddr *dst_addr);
  66. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  67. enum iscsi_param param, char *buf);
  68. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  69. enum iscsi_param param, char *buf);
  70. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  71. enum iscsi_host_param param, char *buf);
  72. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
  73. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
  74. /*
  75. * SCSI host template entry points
  76. */
  77. static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  78. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
  79. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  80. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
  81. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  82. static int qla4xxx_slave_alloc(struct scsi_device *device);
  83. static int qla4xxx_slave_configure(struct scsi_device *device);
  84. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  85. static void qla4xxx_scan_start(struct Scsi_Host *shost);
  86. static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
  87. QLA82XX_LEGACY_INTR_CONFIG;
  88. static struct scsi_host_template qla4xxx_driver_template = {
  89. .module = THIS_MODULE,
  90. .name = DRIVER_NAME,
  91. .proc_name = DRIVER_NAME,
  92. .queuecommand = qla4xxx_queuecommand,
  93. .eh_abort_handler = qla4xxx_eh_abort,
  94. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  95. .eh_target_reset_handler = qla4xxx_eh_target_reset,
  96. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  97. .eh_timed_out = qla4xxx_eh_cmd_timed_out,
  98. .slave_configure = qla4xxx_slave_configure,
  99. .slave_alloc = qla4xxx_slave_alloc,
  100. .slave_destroy = qla4xxx_slave_destroy,
  101. .scan_finished = iscsi_scan_finished,
  102. .scan_start = qla4xxx_scan_start,
  103. .this_id = -1,
  104. .cmd_per_lun = 3,
  105. .use_clustering = ENABLE_CLUSTERING,
  106. .sg_tablesize = SG_ALL,
  107. .max_sectors = 0xFFFF,
  108. };
  109. static struct iscsi_transport qla4xxx_iscsi_transport = {
  110. .owner = THIS_MODULE,
  111. .name = DRIVER_NAME,
  112. .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
  113. CAP_DATA_PATH_OFFLOAD,
  114. .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
  115. ISCSI_TARGET_NAME | ISCSI_TPGT |
  116. ISCSI_TARGET_ALIAS,
  117. .host_param_mask = ISCSI_HOST_HWADDRESS |
  118. ISCSI_HOST_IPADDRESS |
  119. ISCSI_HOST_INITIATOR_NAME,
  120. .tgt_dscvr = qla4xxx_tgt_dscvr,
  121. .get_conn_param = qla4xxx_conn_get_param,
  122. .get_session_param = qla4xxx_sess_get_param,
  123. .get_host_param = qla4xxx_host_get_param,
  124. .session_recovery_timedout = qla4xxx_recovery_timedout,
  125. };
  126. static struct scsi_transport_template *qla4xxx_scsi_transport;
  127. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  128. {
  129. struct iscsi_cls_session *session;
  130. struct ddb_entry *ddb_entry;
  131. session = starget_to_session(scsi_target(sc->device));
  132. ddb_entry = session->dd_data;
  133. /* if we are not logged in then the LLD is going to clean up the cmd */
  134. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  135. return BLK_EH_RESET_TIMER;
  136. else
  137. return BLK_EH_NOT_HANDLED;
  138. }
  139. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
  140. {
  141. struct ddb_entry *ddb_entry = session->dd_data;
  142. struct scsi_qla_host *ha = ddb_entry->ha;
  143. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  144. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  145. DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
  146. "of (%d) secs exhausted, marking device DEAD.\n",
  147. ha->host_no, __func__, ddb_entry->fw_ddb_index,
  148. ddb_entry->sess->recovery_tmo));
  149. }
  150. }
  151. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  152. enum iscsi_host_param param, char *buf)
  153. {
  154. struct scsi_qla_host *ha = to_qla_host(shost);
  155. int len;
  156. switch (param) {
  157. case ISCSI_HOST_PARAM_HWADDRESS:
  158. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  159. break;
  160. case ISCSI_HOST_PARAM_IPADDRESS:
  161. len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
  162. ha->ip_address[1], ha->ip_address[2],
  163. ha->ip_address[3]);
  164. break;
  165. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  166. len = sprintf(buf, "%s\n", ha->name_string);
  167. break;
  168. default:
  169. return -ENOSYS;
  170. }
  171. return len;
  172. }
  173. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  174. enum iscsi_param param, char *buf)
  175. {
  176. struct ddb_entry *ddb_entry = sess->dd_data;
  177. int len;
  178. switch (param) {
  179. case ISCSI_PARAM_TARGET_NAME:
  180. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  181. ddb_entry->iscsi_name);
  182. break;
  183. case ISCSI_PARAM_TPGT:
  184. len = sprintf(buf, "%u\n", ddb_entry->tpgt);
  185. break;
  186. case ISCSI_PARAM_TARGET_ALIAS:
  187. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  188. ddb_entry->iscsi_alias);
  189. break;
  190. default:
  191. return -ENOSYS;
  192. }
  193. return len;
  194. }
  195. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  196. enum iscsi_param param, char *buf)
  197. {
  198. struct iscsi_cls_session *session;
  199. struct ddb_entry *ddb_entry;
  200. int len;
  201. session = iscsi_dev_to_session(conn->dev.parent);
  202. ddb_entry = session->dd_data;
  203. switch (param) {
  204. case ISCSI_PARAM_CONN_PORT:
  205. len = sprintf(buf, "%hu\n", ddb_entry->port);
  206. break;
  207. case ISCSI_PARAM_CONN_ADDRESS:
  208. /* TODO: what are the ipv6 bits */
  209. len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
  210. break;
  211. default:
  212. return -ENOSYS;
  213. }
  214. return len;
  215. }
  216. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  217. enum iscsi_tgt_dscvr type, uint32_t enable,
  218. struct sockaddr *dst_addr)
  219. {
  220. struct scsi_qla_host *ha;
  221. struct sockaddr_in *addr;
  222. struct sockaddr_in6 *addr6;
  223. int ret = 0;
  224. ha = (struct scsi_qla_host *) shost->hostdata;
  225. switch (type) {
  226. case ISCSI_TGT_DSCVR_SEND_TARGETS:
  227. if (dst_addr->sa_family == AF_INET) {
  228. addr = (struct sockaddr_in *)dst_addr;
  229. if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
  230. addr->sin_port) != QLA_SUCCESS)
  231. ret = -EIO;
  232. } else if (dst_addr->sa_family == AF_INET6) {
  233. /*
  234. * TODO: fix qla4xxx_send_tgts
  235. */
  236. addr6 = (struct sockaddr_in6 *)dst_addr;
  237. if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
  238. addr6->sin6_port) != QLA_SUCCESS)
  239. ret = -EIO;
  240. } else
  241. ret = -ENOSYS;
  242. break;
  243. default:
  244. ret = -ENOSYS;
  245. }
  246. return ret;
  247. }
  248. void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
  249. {
  250. if (!ddb_entry->sess)
  251. return;
  252. if (ddb_entry->conn) {
  253. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  254. iscsi_remove_session(ddb_entry->sess);
  255. }
  256. iscsi_free_session(ddb_entry->sess);
  257. }
  258. int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
  259. {
  260. int err;
  261. ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
  262. err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
  263. if (err) {
  264. DEBUG2(printk(KERN_ERR "Could not add session.\n"));
  265. return err;
  266. }
  267. ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
  268. if (!ddb_entry->conn) {
  269. iscsi_remove_session(ddb_entry->sess);
  270. DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
  271. return -ENOMEM;
  272. }
  273. /* finally ready to go */
  274. iscsi_unblock_session(ddb_entry->sess);
  275. return 0;
  276. }
  277. struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
  278. {
  279. struct ddb_entry *ddb_entry;
  280. struct iscsi_cls_session *sess;
  281. sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
  282. sizeof(struct ddb_entry));
  283. if (!sess)
  284. return NULL;
  285. ddb_entry = sess->dd_data;
  286. memset(ddb_entry, 0, sizeof(*ddb_entry));
  287. ddb_entry->ha = ha;
  288. ddb_entry->sess = sess;
  289. return ddb_entry;
  290. }
  291. static void qla4xxx_scan_start(struct Scsi_Host *shost)
  292. {
  293. struct scsi_qla_host *ha = shost_priv(shost);
  294. struct ddb_entry *ddb_entry, *ddbtemp;
  295. /* finish setup of sessions that were already setup in firmware */
  296. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  297. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
  298. qla4xxx_add_sess(ddb_entry);
  299. }
  300. }
  301. /*
  302. * Timer routines
  303. */
  304. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  305. unsigned long interval)
  306. {
  307. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  308. __func__, ha->host->host_no));
  309. init_timer(&ha->timer);
  310. ha->timer.expires = jiffies + interval * HZ;
  311. ha->timer.data = (unsigned long)ha;
  312. ha->timer.function = (void (*)(unsigned long))func;
  313. add_timer(&ha->timer);
  314. ha->timer_active = 1;
  315. }
  316. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  317. {
  318. del_timer_sync(&ha->timer);
  319. ha->timer_active = 0;
  320. }
  321. /***
  322. * qla4xxx_mark_device_missing - mark a device as missing.
  323. * @ha: Pointer to host adapter structure.
  324. * @ddb_entry: Pointer to device database entry
  325. *
  326. * This routine marks a device missing and close connection.
  327. **/
  328. void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
  329. struct ddb_entry *ddb_entry)
  330. {
  331. if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
  332. atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
  333. DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
  334. ha->host_no, ddb_entry->fw_ddb_index));
  335. } else
  336. DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
  337. ddb_entry->fw_ddb_index))
  338. iscsi_block_session(ddb_entry->sess);
  339. iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
  340. }
  341. /**
  342. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  343. * @ha: Pointer to host adapter structure.
  344. *
  345. * This routine marks a device missing and resets the relogin retry count.
  346. **/
  347. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  348. {
  349. struct ddb_entry *ddb_entry, *ddbtemp;
  350. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  351. qla4xxx_mark_device_missing(ha, ddb_entry);
  352. }
  353. }
  354. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  355. struct ddb_entry *ddb_entry,
  356. struct scsi_cmnd *cmd,
  357. void (*done)(struct scsi_cmnd *))
  358. {
  359. struct srb *srb;
  360. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  361. if (!srb)
  362. return srb;
  363. kref_init(&srb->srb_ref);
  364. srb->ha = ha;
  365. srb->ddb = ddb_entry;
  366. srb->cmd = cmd;
  367. srb->flags = 0;
  368. CMD_SP(cmd) = (void *)srb;
  369. cmd->scsi_done = done;
  370. return srb;
  371. }
  372. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  373. {
  374. struct scsi_cmnd *cmd = srb->cmd;
  375. if (srb->flags & SRB_DMA_VALID) {
  376. scsi_dma_unmap(cmd);
  377. srb->flags &= ~SRB_DMA_VALID;
  378. }
  379. CMD_SP(cmd) = NULL;
  380. }
  381. void qla4xxx_srb_compl(struct kref *ref)
  382. {
  383. struct srb *srb = container_of(ref, struct srb, srb_ref);
  384. struct scsi_cmnd *cmd = srb->cmd;
  385. struct scsi_qla_host *ha = srb->ha;
  386. qla4xxx_srb_free_dma(ha, srb);
  387. mempool_free(srb, ha->srb_mempool);
  388. cmd->scsi_done(cmd);
  389. }
  390. /**
  391. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  392. * @cmd: Pointer to Linux's SCSI command structure
  393. * @done_fn: Function that the driver calls to notify the SCSI mid-layer
  394. * that the command has been processed.
  395. *
  396. * Remarks:
  397. * This routine is invoked by Linux to send a SCSI command to the driver.
  398. * The mid-level driver tries to ensure that queuecommand never gets
  399. * invoked concurrently with itself or the interrupt handler (although
  400. * the interrupt handler may call this routine as part of request-
  401. * completion handling). Unfortunely, it sometimes calls the scheduler
  402. * in interrupt context which is a big NO! NO!.
  403. **/
  404. static int qla4xxx_queuecommand_lck(struct scsi_cmnd *cmd,
  405. void (*done)(struct scsi_cmnd *))
  406. {
  407. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  408. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  409. struct iscsi_cls_session *sess = ddb_entry->sess;
  410. struct srb *srb;
  411. int rval;
  412. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  413. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  414. cmd->result = DID_NO_CONNECT << 16;
  415. else
  416. cmd->result = DID_REQUEUE << 16;
  417. goto qc_fail_command;
  418. }
  419. if (!sess) {
  420. cmd->result = DID_IMM_RETRY << 16;
  421. goto qc_fail_command;
  422. }
  423. rval = iscsi_session_chkready(sess);
  424. if (rval) {
  425. cmd->result = rval;
  426. goto qc_fail_command;
  427. }
  428. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  429. if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
  430. cmd->result = DID_NO_CONNECT << 16;
  431. goto qc_fail_command;
  432. }
  433. return SCSI_MLQUEUE_TARGET_BUSY;
  434. }
  435. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  436. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  437. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  438. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  439. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  440. !test_bit(AF_ONLINE, &ha->flags) ||
  441. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  442. goto qc_host_busy;
  443. spin_unlock_irq(ha->host->host_lock);
  444. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
  445. if (!srb)
  446. goto qc_host_busy_lock;
  447. rval = qla4xxx_send_command_to_isp(ha, srb);
  448. if (rval != QLA_SUCCESS)
  449. goto qc_host_busy_free_sp;
  450. spin_lock_irq(ha->host->host_lock);
  451. return 0;
  452. qc_host_busy_free_sp:
  453. qla4xxx_srb_free_dma(ha, srb);
  454. mempool_free(srb, ha->srb_mempool);
  455. qc_host_busy_lock:
  456. spin_lock_irq(ha->host->host_lock);
  457. qc_host_busy:
  458. return SCSI_MLQUEUE_HOST_BUSY;
  459. qc_fail_command:
  460. done(cmd);
  461. return 0;
  462. }
  463. static DEF_SCSI_QCMD(qla4xxx_queuecommand)
  464. /**
  465. * qla4xxx_mem_free - frees memory allocated to adapter
  466. * @ha: Pointer to host adapter structure.
  467. *
  468. * Frees memory previously allocated by qla4xxx_mem_alloc
  469. **/
  470. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  471. {
  472. if (ha->queues)
  473. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  474. ha->queues_dma);
  475. ha->queues_len = 0;
  476. ha->queues = NULL;
  477. ha->queues_dma = 0;
  478. ha->request_ring = NULL;
  479. ha->request_dma = 0;
  480. ha->response_ring = NULL;
  481. ha->response_dma = 0;
  482. ha->shadow_regs = NULL;
  483. ha->shadow_regs_dma = 0;
  484. /* Free srb pool. */
  485. if (ha->srb_mempool)
  486. mempool_destroy(ha->srb_mempool);
  487. ha->srb_mempool = NULL;
  488. /* release io space registers */
  489. if (is_qla8022(ha)) {
  490. if (ha->nx_pcibase)
  491. iounmap(
  492. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  493. } else if (ha->reg)
  494. iounmap(ha->reg);
  495. pci_release_regions(ha->pdev);
  496. }
  497. /**
  498. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  499. * @ha: Pointer to host adapter structure
  500. *
  501. * Allocates DMA memory for request and response queues. Also allocates memory
  502. * for srbs.
  503. **/
  504. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  505. {
  506. unsigned long align;
  507. /* Allocate contiguous block of DMA memory for queues. */
  508. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  509. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  510. sizeof(struct shadow_regs) +
  511. MEM_ALIGN_VALUE +
  512. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  513. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  514. &ha->queues_dma, GFP_KERNEL);
  515. if (ha->queues == NULL) {
  516. ql4_printk(KERN_WARNING, ha,
  517. "Memory Allocation failed - queues.\n");
  518. goto mem_alloc_error_exit;
  519. }
  520. memset(ha->queues, 0, ha->queues_len);
  521. /*
  522. * As per RISC alignment requirements -- the bus-address must be a
  523. * multiple of the request-ring size (in bytes).
  524. */
  525. align = 0;
  526. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  527. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  528. (MEM_ALIGN_VALUE - 1));
  529. /* Update request and response queue pointers. */
  530. ha->request_dma = ha->queues_dma + align;
  531. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  532. ha->response_dma = ha->queues_dma + align +
  533. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  534. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  535. (REQUEST_QUEUE_DEPTH *
  536. QUEUE_SIZE));
  537. ha->shadow_regs_dma = ha->queues_dma + align +
  538. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  539. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  540. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  541. (REQUEST_QUEUE_DEPTH *
  542. QUEUE_SIZE) +
  543. (RESPONSE_QUEUE_DEPTH *
  544. QUEUE_SIZE));
  545. /* Allocate memory for srb pool. */
  546. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  547. mempool_free_slab, srb_cachep);
  548. if (ha->srb_mempool == NULL) {
  549. ql4_printk(KERN_WARNING, ha,
  550. "Memory Allocation failed - SRB Pool.\n");
  551. goto mem_alloc_error_exit;
  552. }
  553. return QLA_SUCCESS;
  554. mem_alloc_error_exit:
  555. qla4xxx_mem_free(ha);
  556. return QLA_ERROR;
  557. }
  558. /**
  559. * qla4_8xxx_check_fw_alive - Check firmware health
  560. * @ha: Pointer to host adapter structure.
  561. *
  562. * Context: Interrupt
  563. **/
  564. static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  565. {
  566. uint32_t fw_heartbeat_counter, halt_status;
  567. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  568. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  569. if (fw_heartbeat_counter == 0xffffffff) {
  570. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  571. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  572. ha->host_no, __func__));
  573. return;
  574. }
  575. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  576. ha->seconds_since_last_heartbeat++;
  577. /* FW not alive after 2 seconds */
  578. if (ha->seconds_since_last_heartbeat == 2) {
  579. ha->seconds_since_last_heartbeat = 0;
  580. halt_status = qla4_8xxx_rd_32(ha,
  581. QLA82XX_PEG_HALT_STATUS1);
  582. ql4_printk(KERN_INFO, ha,
  583. "scsi(%ld): %s, Dumping hw/fw registers:\n "
  584. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
  585. " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
  586. " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
  587. " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
  588. ha->host_no, __func__, halt_status,
  589. qla4_8xxx_rd_32(ha,
  590. QLA82XX_PEG_HALT_STATUS2),
  591. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
  592. 0x3c),
  593. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
  594. 0x3c),
  595. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
  596. 0x3c),
  597. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
  598. 0x3c),
  599. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
  600. 0x3c));
  601. /* Since we cannot change dev_state in interrupt
  602. * context, set appropriate DPC flag then wakeup
  603. * DPC */
  604. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  605. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  606. else {
  607. printk("scsi%ld: %s: detect abort needed!\n",
  608. ha->host_no, __func__);
  609. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  610. }
  611. qla4xxx_wake_dpc(ha);
  612. qla4xxx_mailbox_premature_completion(ha);
  613. }
  614. } else
  615. ha->seconds_since_last_heartbeat = 0;
  616. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  617. }
  618. /**
  619. * qla4_8xxx_watchdog - Poll dev state
  620. * @ha: Pointer to host adapter structure.
  621. *
  622. * Context: Interrupt
  623. **/
  624. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  625. {
  626. uint32_t dev_state;
  627. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  628. /* don't poll if reset is going on */
  629. if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  630. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  631. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
  632. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  633. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  634. if (!ql4xdontresethba) {
  635. ql4_printk(KERN_INFO, ha, "%s: HW State: "
  636. "NEED RESET!\n", __func__);
  637. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  638. qla4xxx_wake_dpc(ha);
  639. qla4xxx_mailbox_premature_completion(ha);
  640. }
  641. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  642. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  643. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
  644. __func__);
  645. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  646. qla4xxx_wake_dpc(ha);
  647. } else {
  648. /* Check firmware health */
  649. qla4_8xxx_check_fw_alive(ha);
  650. }
  651. }
  652. }
  653. /**
  654. * qla4xxx_timer - checks every second for work to do.
  655. * @ha: Pointer to host adapter structure.
  656. **/
  657. static void qla4xxx_timer(struct scsi_qla_host *ha)
  658. {
  659. struct ddb_entry *ddb_entry, *dtemp;
  660. int start_dpc = 0;
  661. uint16_t w;
  662. /* If we are in the middle of AER/EEH processing
  663. * skip any processing and reschedule the timer
  664. */
  665. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  666. mod_timer(&ha->timer, jiffies + HZ);
  667. return;
  668. }
  669. /* Hardware read to trigger an EEH error during mailbox waits. */
  670. if (!pci_channel_offline(ha->pdev))
  671. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  672. if (is_qla8022(ha)) {
  673. qla4_8xxx_watchdog(ha);
  674. }
  675. /* Search for relogin's to time-out and port down retry. */
  676. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  677. /* Count down time between sending relogins */
  678. if (adapter_up(ha) &&
  679. !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  680. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  681. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  682. INVALID_ENTRY) {
  683. if (atomic_read(&ddb_entry->retry_relogin_timer)
  684. == 0) {
  685. atomic_set(&ddb_entry->
  686. retry_relogin_timer,
  687. INVALID_ENTRY);
  688. set_bit(DPC_RELOGIN_DEVICE,
  689. &ha->dpc_flags);
  690. set_bit(DF_RELOGIN, &ddb_entry->flags);
  691. DEBUG2(printk("scsi%ld: %s: ddb [%d]"
  692. " login device\n",
  693. ha->host_no, __func__,
  694. ddb_entry->fw_ddb_index));
  695. } else
  696. atomic_dec(&ddb_entry->
  697. retry_relogin_timer);
  698. }
  699. }
  700. /* Wait for relogin to timeout */
  701. if (atomic_read(&ddb_entry->relogin_timer) &&
  702. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  703. /*
  704. * If the relogin times out and the device is
  705. * still NOT ONLINE then try and relogin again.
  706. */
  707. if (atomic_read(&ddb_entry->state) !=
  708. DDB_STATE_ONLINE &&
  709. ddb_entry->fw_ddb_device_state ==
  710. DDB_DS_SESSION_FAILED) {
  711. /* Reset retry relogin timer */
  712. atomic_inc(&ddb_entry->relogin_retry_count);
  713. DEBUG2(printk("scsi%ld: ddb [%d] relogin"
  714. " timed out-retrying"
  715. " relogin (%d)\n",
  716. ha->host_no,
  717. ddb_entry->fw_ddb_index,
  718. atomic_read(&ddb_entry->
  719. relogin_retry_count))
  720. );
  721. start_dpc++;
  722. DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
  723. "initiate relogin after"
  724. " %d seconds\n",
  725. ha->host_no, ddb_entry->bus,
  726. ddb_entry->target,
  727. ddb_entry->fw_ddb_index,
  728. ddb_entry->default_time2wait + 4)
  729. );
  730. atomic_set(&ddb_entry->retry_relogin_timer,
  731. ddb_entry->default_time2wait + 4);
  732. }
  733. }
  734. }
  735. if (!is_qla8022(ha)) {
  736. /* Check for heartbeat interval. */
  737. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  738. ha->heartbeat_interval != 0) {
  739. ha->seconds_since_last_heartbeat++;
  740. if (ha->seconds_since_last_heartbeat >
  741. ha->heartbeat_interval + 2)
  742. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  743. }
  744. }
  745. /* Wakeup the dpc routine for this adapter, if needed. */
  746. if (start_dpc ||
  747. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  748. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  749. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  750. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  751. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  752. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  753. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  754. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  755. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  756. test_bit(DPC_AEN, &ha->dpc_flags)) {
  757. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  758. " - dpc flags = 0x%lx\n",
  759. ha->host_no, __func__, ha->dpc_flags));
  760. qla4xxx_wake_dpc(ha);
  761. }
  762. /* Reschedule timer thread to call us back in one second */
  763. mod_timer(&ha->timer, jiffies + HZ);
  764. DEBUG2(ha->seconds_since_last_intr++);
  765. }
  766. /**
  767. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  768. * @ha: Pointer to host adapter structure.
  769. *
  770. * This routine stalls the driver until all outstanding commands are returned.
  771. * Caller must release the Hardware Lock prior to calling this routine.
  772. **/
  773. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  774. {
  775. uint32_t index = 0;
  776. unsigned long flags;
  777. struct scsi_cmnd *cmd;
  778. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  779. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  780. "complete\n", WAIT_CMD_TOV));
  781. while (!time_after_eq(jiffies, wtime)) {
  782. spin_lock_irqsave(&ha->hardware_lock, flags);
  783. /* Find a command that hasn't completed. */
  784. for (index = 0; index < ha->host->can_queue; index++) {
  785. cmd = scsi_host_find_tag(ha->host, index);
  786. /*
  787. * We cannot just check if the index is valid,
  788. * becase if we are run from the scsi eh, then
  789. * the scsi/block layer is going to prevent
  790. * the tag from being released.
  791. */
  792. if (cmd != NULL && CMD_SP(cmd))
  793. break;
  794. }
  795. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  796. /* If No Commands are pending, wait is complete */
  797. if (index == ha->host->can_queue)
  798. return QLA_SUCCESS;
  799. msleep(1000);
  800. }
  801. /* If we timed out on waiting for commands to come back
  802. * return ERROR. */
  803. return QLA_ERROR;
  804. }
  805. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  806. {
  807. uint32_t ctrl_status;
  808. unsigned long flags = 0;
  809. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  810. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  811. return QLA_ERROR;
  812. spin_lock_irqsave(&ha->hardware_lock, flags);
  813. /*
  814. * If the SCSI Reset Interrupt bit is set, clear it.
  815. * Otherwise, the Soft Reset won't work.
  816. */
  817. ctrl_status = readw(&ha->reg->ctrl_status);
  818. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  819. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  820. /* Issue Soft Reset */
  821. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  822. readl(&ha->reg->ctrl_status);
  823. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  824. return QLA_SUCCESS;
  825. }
  826. /**
  827. * qla4xxx_soft_reset - performs soft reset.
  828. * @ha: Pointer to host adapter structure.
  829. **/
  830. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  831. {
  832. uint32_t max_wait_time;
  833. unsigned long flags = 0;
  834. int status;
  835. uint32_t ctrl_status;
  836. status = qla4xxx_hw_reset(ha);
  837. if (status != QLA_SUCCESS)
  838. return status;
  839. status = QLA_ERROR;
  840. /* Wait until the Network Reset Intr bit is cleared */
  841. max_wait_time = RESET_INTR_TOV;
  842. do {
  843. spin_lock_irqsave(&ha->hardware_lock, flags);
  844. ctrl_status = readw(&ha->reg->ctrl_status);
  845. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  846. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  847. break;
  848. msleep(1000);
  849. } while ((--max_wait_time));
  850. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  851. DEBUG2(printk(KERN_WARNING
  852. "scsi%ld: Network Reset Intr not cleared by "
  853. "Network function, clearing it now!\n",
  854. ha->host_no));
  855. spin_lock_irqsave(&ha->hardware_lock, flags);
  856. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  857. readl(&ha->reg->ctrl_status);
  858. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  859. }
  860. /* Wait until the firmware tells us the Soft Reset is done */
  861. max_wait_time = SOFT_RESET_TOV;
  862. do {
  863. spin_lock_irqsave(&ha->hardware_lock, flags);
  864. ctrl_status = readw(&ha->reg->ctrl_status);
  865. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  866. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  867. status = QLA_SUCCESS;
  868. break;
  869. }
  870. msleep(1000);
  871. } while ((--max_wait_time));
  872. /*
  873. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  874. * after the soft reset has taken place.
  875. */
  876. spin_lock_irqsave(&ha->hardware_lock, flags);
  877. ctrl_status = readw(&ha->reg->ctrl_status);
  878. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  879. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  880. readl(&ha->reg->ctrl_status);
  881. }
  882. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  883. /* If soft reset fails then most probably the bios on other
  884. * function is also enabled.
  885. * Since the initialization is sequential the other fn
  886. * wont be able to acknowledge the soft reset.
  887. * Issue a force soft reset to workaround this scenario.
  888. */
  889. if (max_wait_time == 0) {
  890. /* Issue Force Soft Reset */
  891. spin_lock_irqsave(&ha->hardware_lock, flags);
  892. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  893. readl(&ha->reg->ctrl_status);
  894. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  895. /* Wait until the firmware tells us the Soft Reset is done */
  896. max_wait_time = SOFT_RESET_TOV;
  897. do {
  898. spin_lock_irqsave(&ha->hardware_lock, flags);
  899. ctrl_status = readw(&ha->reg->ctrl_status);
  900. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  901. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  902. status = QLA_SUCCESS;
  903. break;
  904. }
  905. msleep(1000);
  906. } while ((--max_wait_time));
  907. }
  908. return status;
  909. }
  910. /**
  911. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  912. * @ha: Pointer to host adapter structure.
  913. * @res: returned scsi status
  914. *
  915. * This routine is called just prior to a HARD RESET to return all
  916. * outstanding commands back to the Operating System.
  917. * Caller should make sure that the following locks are released
  918. * before this calling routine: Hardware lock, and io_request_lock.
  919. **/
  920. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  921. {
  922. struct srb *srb;
  923. int i;
  924. unsigned long flags;
  925. spin_lock_irqsave(&ha->hardware_lock, flags);
  926. for (i = 0; i < ha->host->can_queue; i++) {
  927. srb = qla4xxx_del_from_active_array(ha, i);
  928. if (srb != NULL) {
  929. srb->cmd->result = res;
  930. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  931. }
  932. }
  933. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  934. }
  935. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  936. {
  937. clear_bit(AF_ONLINE, &ha->flags);
  938. /* Disable the board */
  939. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  940. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  941. qla4xxx_mark_all_devices_missing(ha);
  942. clear_bit(AF_INIT_DONE, &ha->flags);
  943. }
  944. /**
  945. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  946. * @ha: Pointer to host adapter structure.
  947. **/
  948. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  949. {
  950. int status = QLA_ERROR;
  951. uint8_t reset_chip = 0;
  952. /* Stall incoming I/O until we are done */
  953. scsi_block_requests(ha->host);
  954. clear_bit(AF_ONLINE, &ha->flags);
  955. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  956. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  957. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  958. reset_chip = 1;
  959. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  960. * do not reset adapter, jump to initialize_adapter */
  961. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  962. status = QLA_SUCCESS;
  963. goto recover_ha_init_adapter;
  964. }
  965. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  966. * from eh_host_reset or ioctl module */
  967. if (is_qla8022(ha) && !reset_chip &&
  968. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  969. DEBUG2(ql4_printk(KERN_INFO, ha,
  970. "scsi%ld: %s - Performing stop_firmware...\n",
  971. ha->host_no, __func__));
  972. status = ha->isp_ops->reset_firmware(ha);
  973. if (status == QLA_SUCCESS) {
  974. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  975. qla4xxx_cmd_wait(ha);
  976. ha->isp_ops->disable_intrs(ha);
  977. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  978. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  979. } else {
  980. /* If the stop_firmware fails then
  981. * reset the entire chip */
  982. reset_chip = 1;
  983. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  984. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  985. }
  986. }
  987. /* Issue full chip reset if recovering from a catastrophic error,
  988. * or if stop_firmware fails for ISP-82xx.
  989. * This is the default case for ISP-4xxx */
  990. if (!is_qla8022(ha) || reset_chip) {
  991. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  992. qla4xxx_cmd_wait(ha);
  993. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  994. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  995. DEBUG2(ql4_printk(KERN_INFO, ha,
  996. "scsi%ld: %s - Performing chip reset..\n",
  997. ha->host_no, __func__));
  998. status = ha->isp_ops->reset_chip(ha);
  999. }
  1000. /* Flush any pending ddb changed AENs */
  1001. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1002. recover_ha_init_adapter:
  1003. /* Upon successful firmware/chip reset, re-initialize the adapter */
  1004. if (status == QLA_SUCCESS) {
  1005. /* For ISP-4xxx, force function 1 to always initialize
  1006. * before function 3 to prevent both funcions from
  1007. * stepping on top of the other */
  1008. if (!is_qla8022(ha) && (ha->mac_index == 3))
  1009. ssleep(6);
  1010. /* NOTE: AF_ONLINE flag set upon successful completion of
  1011. * qla4xxx_initialize_adapter */
  1012. status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  1013. }
  1014. /* Retry failed adapter initialization, if necessary
  1015. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  1016. * case to prevent ping-pong resets between functions */
  1017. if (!test_bit(AF_ONLINE, &ha->flags) &&
  1018. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1019. /* Adapter initialization failed, see if we can retry
  1020. * resetting the ha.
  1021. * Since we don't want to block the DPC for too long
  1022. * with multiple resets in the same thread,
  1023. * utilize DPC to retry */
  1024. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  1025. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  1026. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  1027. "(%d) more times\n", ha->host_no,
  1028. ha->retry_reset_ha_cnt));
  1029. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1030. status = QLA_ERROR;
  1031. } else {
  1032. if (ha->retry_reset_ha_cnt > 0) {
  1033. /* Schedule another Reset HA--DPC will retry */
  1034. ha->retry_reset_ha_cnt--;
  1035. DEBUG2(printk("scsi%ld: recover adapter - "
  1036. "retry remaining %d\n",
  1037. ha->host_no,
  1038. ha->retry_reset_ha_cnt));
  1039. status = QLA_ERROR;
  1040. }
  1041. if (ha->retry_reset_ha_cnt == 0) {
  1042. /* Recover adapter retries have been exhausted.
  1043. * Adapter DEAD */
  1044. DEBUG2(printk("scsi%ld: recover adapter "
  1045. "failed - board disabled\n",
  1046. ha->host_no));
  1047. qla4xxx_dead_adapter_cleanup(ha);
  1048. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1049. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1050. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  1051. &ha->dpc_flags);
  1052. status = QLA_ERROR;
  1053. }
  1054. }
  1055. } else {
  1056. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1057. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1058. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1059. }
  1060. ha->adapter_error_count++;
  1061. if (test_bit(AF_ONLINE, &ha->flags))
  1062. ha->isp_ops->enable_intrs(ha);
  1063. scsi_unblock_requests(ha->host);
  1064. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1065. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  1066. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  1067. return status;
  1068. }
  1069. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  1070. {
  1071. struct ddb_entry *ddb_entry, *dtemp;
  1072. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  1073. if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
  1074. (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
  1075. if (ddb_entry->fw_ddb_device_state ==
  1076. DDB_DS_SESSION_ACTIVE) {
  1077. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  1078. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  1079. " marked ONLINE\n", ha->host_no, __func__,
  1080. ddb_entry->fw_ddb_index);
  1081. iscsi_unblock_session(ddb_entry->sess);
  1082. } else
  1083. qla4xxx_relogin_device(ha, ddb_entry);
  1084. }
  1085. }
  1086. }
  1087. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  1088. {
  1089. if (ha->dpc_thread)
  1090. queue_work(ha->dpc_thread, &ha->dpc_work);
  1091. }
  1092. /**
  1093. * qla4xxx_do_dpc - dpc routine
  1094. * @data: in our case pointer to adapter structure
  1095. *
  1096. * This routine is a task that is schedule by the interrupt handler
  1097. * to perform the background processing for interrupts. We put it
  1098. * on a task queue that is consumed whenever the scheduler runs; that's
  1099. * so you can do anything (i.e. put the process to sleep etc). In fact,
  1100. * the mid-level tries to sleep when it reaches the driver threshold
  1101. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  1102. **/
  1103. static void qla4xxx_do_dpc(struct work_struct *work)
  1104. {
  1105. struct scsi_qla_host *ha =
  1106. container_of(work, struct scsi_qla_host, dpc_work);
  1107. struct ddb_entry *ddb_entry, *dtemp;
  1108. int status = QLA_ERROR;
  1109. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  1110. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  1111. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  1112. /* Initialization not yet finished. Don't do anything yet. */
  1113. if (!test_bit(AF_INIT_DONE, &ha->flags))
  1114. return;
  1115. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1116. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  1117. ha->host_no, __func__, ha->flags));
  1118. return;
  1119. }
  1120. if (is_qla8022(ha)) {
  1121. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  1122. qla4_8xxx_idc_lock(ha);
  1123. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1124. QLA82XX_DEV_FAILED);
  1125. qla4_8xxx_idc_unlock(ha);
  1126. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  1127. qla4_8xxx_device_state_handler(ha);
  1128. }
  1129. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1130. qla4_8xxx_need_qsnt_handler(ha);
  1131. }
  1132. }
  1133. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  1134. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1135. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1136. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  1137. if (ql4xdontresethba) {
  1138. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1139. ha->host_no, __func__));
  1140. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1141. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1142. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1143. goto dpc_post_reset_ha;
  1144. }
  1145. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1146. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1147. qla4xxx_recover_adapter(ha);
  1148. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1149. uint8_t wait_time = RESET_INTR_TOV;
  1150. while ((readw(&ha->reg->ctrl_status) &
  1151. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  1152. if (--wait_time == 0)
  1153. break;
  1154. msleep(1000);
  1155. }
  1156. if (wait_time == 0)
  1157. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  1158. "bit not cleared-- resetting\n",
  1159. ha->host_no, __func__));
  1160. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1161. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  1162. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1163. status = qla4xxx_recover_adapter(ha);
  1164. }
  1165. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1166. if (status == QLA_SUCCESS)
  1167. ha->isp_ops->enable_intrs(ha);
  1168. }
  1169. }
  1170. dpc_post_reset_ha:
  1171. /* ---- process AEN? --- */
  1172. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  1173. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  1174. /* ---- Get DHCP IP Address? --- */
  1175. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  1176. qla4xxx_get_dhcp_ip_address(ha);
  1177. /* ---- link change? --- */
  1178. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  1179. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  1180. /* ---- link down? --- */
  1181. qla4xxx_mark_all_devices_missing(ha);
  1182. } else {
  1183. /* ---- link up? --- *
  1184. * F/W will auto login to all devices ONLY ONCE after
  1185. * link up during driver initialization and runtime
  1186. * fatal error recovery. Therefore, the driver must
  1187. * manually relogin to devices when recovering from
  1188. * connection failures, logouts, expired KATO, etc. */
  1189. qla4xxx_relogin_all_devices(ha);
  1190. }
  1191. }
  1192. /* ---- relogin device? --- */
  1193. if (adapter_up(ha) &&
  1194. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  1195. list_for_each_entry_safe(ddb_entry, dtemp,
  1196. &ha->ddb_list, list) {
  1197. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1198. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  1199. qla4xxx_relogin_device(ha, ddb_entry);
  1200. /*
  1201. * If mbx cmd times out there is no point
  1202. * in continuing further.
  1203. * With large no of targets this can hang
  1204. * the system.
  1205. */
  1206. if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1207. printk(KERN_WARNING "scsi%ld: %s: "
  1208. "need to reset hba\n",
  1209. ha->host_no, __func__);
  1210. break;
  1211. }
  1212. }
  1213. }
  1214. }
  1215. /**
  1216. * qla4xxx_free_adapter - release the adapter
  1217. * @ha: pointer to adapter structure
  1218. **/
  1219. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  1220. {
  1221. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  1222. /* Turn-off interrupts on the card. */
  1223. ha->isp_ops->disable_intrs(ha);
  1224. }
  1225. /* Remove timer thread, if present */
  1226. if (ha->timer_active)
  1227. qla4xxx_stop_timer(ha);
  1228. /* Kill the kernel thread for this host */
  1229. if (ha->dpc_thread)
  1230. destroy_workqueue(ha->dpc_thread);
  1231. /* Put firmware in known state */
  1232. ha->isp_ops->reset_firmware(ha);
  1233. if (is_qla8022(ha)) {
  1234. qla4_8xxx_idc_lock(ha);
  1235. qla4_8xxx_clear_drv_active(ha);
  1236. qla4_8xxx_idc_unlock(ha);
  1237. }
  1238. /* Detach interrupts */
  1239. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  1240. qla4xxx_free_irqs(ha);
  1241. /* free extra memory */
  1242. qla4xxx_mem_free(ha);
  1243. }
  1244. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  1245. {
  1246. int status = 0;
  1247. uint8_t revision_id;
  1248. unsigned long mem_base, mem_len, db_base, db_len;
  1249. struct pci_dev *pdev = ha->pdev;
  1250. status = pci_request_regions(pdev, DRIVER_NAME);
  1251. if (status) {
  1252. printk(KERN_WARNING
  1253. "scsi(%ld) Failed to reserve PIO regions (%s) "
  1254. "status=%d\n", ha->host_no, pci_name(pdev), status);
  1255. goto iospace_error_exit;
  1256. }
  1257. pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
  1258. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  1259. __func__, revision_id));
  1260. ha->revision_id = revision_id;
  1261. /* remap phys address */
  1262. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  1263. mem_len = pci_resource_len(pdev, 0);
  1264. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  1265. __func__, mem_base, mem_len));
  1266. /* mapping of pcibase pointer */
  1267. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  1268. if (!ha->nx_pcibase) {
  1269. printk(KERN_ERR
  1270. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  1271. pci_release_regions(ha->pdev);
  1272. goto iospace_error_exit;
  1273. }
  1274. /* Mapping of IO base pointer, door bell read and write pointer */
  1275. /* mapping of IO base pointer */
  1276. ha->qla4_8xxx_reg =
  1277. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  1278. 0xbc000 + (ha->pdev->devfn << 11));
  1279. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  1280. db_len = pci_resource_len(pdev, 4);
  1281. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  1282. QLA82XX_CAM_RAM_DB2);
  1283. return 0;
  1284. iospace_error_exit:
  1285. return -ENOMEM;
  1286. }
  1287. /***
  1288. * qla4xxx_iospace_config - maps registers
  1289. * @ha: pointer to adapter structure
  1290. *
  1291. * This routines maps HBA's registers from the pci address space
  1292. * into the kernel virtual address space for memory mapped i/o.
  1293. **/
  1294. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  1295. {
  1296. unsigned long pio, pio_len, pio_flags;
  1297. unsigned long mmio, mmio_len, mmio_flags;
  1298. pio = pci_resource_start(ha->pdev, 0);
  1299. pio_len = pci_resource_len(ha->pdev, 0);
  1300. pio_flags = pci_resource_flags(ha->pdev, 0);
  1301. if (pio_flags & IORESOURCE_IO) {
  1302. if (pio_len < MIN_IOBASE_LEN) {
  1303. ql4_printk(KERN_WARNING, ha,
  1304. "Invalid PCI I/O region size\n");
  1305. pio = 0;
  1306. }
  1307. } else {
  1308. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  1309. pio = 0;
  1310. }
  1311. /* Use MMIO operations for all accesses. */
  1312. mmio = pci_resource_start(ha->pdev, 1);
  1313. mmio_len = pci_resource_len(ha->pdev, 1);
  1314. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1315. if (!(mmio_flags & IORESOURCE_MEM)) {
  1316. ql4_printk(KERN_ERR, ha,
  1317. "region #0 not an MMIO resource, aborting\n");
  1318. goto iospace_error_exit;
  1319. }
  1320. if (mmio_len < MIN_IOBASE_LEN) {
  1321. ql4_printk(KERN_ERR, ha,
  1322. "Invalid PCI mem region size, aborting\n");
  1323. goto iospace_error_exit;
  1324. }
  1325. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  1326. ql4_printk(KERN_WARNING, ha,
  1327. "Failed to reserve PIO/MMIO regions\n");
  1328. goto iospace_error_exit;
  1329. }
  1330. ha->pio_address = pio;
  1331. ha->pio_length = pio_len;
  1332. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  1333. if (!ha->reg) {
  1334. ql4_printk(KERN_ERR, ha,
  1335. "cannot remap MMIO, aborting\n");
  1336. goto iospace_error_exit;
  1337. }
  1338. return 0;
  1339. iospace_error_exit:
  1340. return -ENOMEM;
  1341. }
  1342. static struct isp_operations qla4xxx_isp_ops = {
  1343. .iospace_config = qla4xxx_iospace_config,
  1344. .pci_config = qla4xxx_pci_config,
  1345. .disable_intrs = qla4xxx_disable_intrs,
  1346. .enable_intrs = qla4xxx_enable_intrs,
  1347. .start_firmware = qla4xxx_start_firmware,
  1348. .intr_handler = qla4xxx_intr_handler,
  1349. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  1350. .reset_chip = qla4xxx_soft_reset,
  1351. .reset_firmware = qla4xxx_hw_reset,
  1352. .queue_iocb = qla4xxx_queue_iocb,
  1353. .complete_iocb = qla4xxx_complete_iocb,
  1354. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  1355. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  1356. .get_sys_info = qla4xxx_get_sys_info,
  1357. };
  1358. static struct isp_operations qla4_8xxx_isp_ops = {
  1359. .iospace_config = qla4_8xxx_iospace_config,
  1360. .pci_config = qla4_8xxx_pci_config,
  1361. .disable_intrs = qla4_8xxx_disable_intrs,
  1362. .enable_intrs = qla4_8xxx_enable_intrs,
  1363. .start_firmware = qla4_8xxx_load_risc,
  1364. .intr_handler = qla4_8xxx_intr_handler,
  1365. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  1366. .reset_chip = qla4_8xxx_isp_reset,
  1367. .reset_firmware = qla4_8xxx_stop_firmware,
  1368. .queue_iocb = qla4_8xxx_queue_iocb,
  1369. .complete_iocb = qla4_8xxx_complete_iocb,
  1370. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  1371. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  1372. .get_sys_info = qla4_8xxx_get_sys_info,
  1373. };
  1374. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1375. {
  1376. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  1377. }
  1378. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1379. {
  1380. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  1381. }
  1382. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1383. {
  1384. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  1385. }
  1386. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1387. {
  1388. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  1389. }
  1390. /**
  1391. * qla4xxx_probe_adapter - callback function to probe HBA
  1392. * @pdev: pointer to pci_dev structure
  1393. * @pci_device_id: pointer to pci_device entry
  1394. *
  1395. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  1396. * It returns zero if successful. It also initializes all data necessary for
  1397. * the driver.
  1398. **/
  1399. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  1400. const struct pci_device_id *ent)
  1401. {
  1402. int ret = -ENODEV, status;
  1403. struct Scsi_Host *host;
  1404. struct scsi_qla_host *ha;
  1405. uint8_t init_retry_count = 0;
  1406. char buf[34];
  1407. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  1408. uint32_t dev_state;
  1409. if (pci_enable_device(pdev))
  1410. return -1;
  1411. host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
  1412. if (host == NULL) {
  1413. printk(KERN_WARNING
  1414. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  1415. goto probe_disable_device;
  1416. }
  1417. /* Clear our data area */
  1418. ha = (struct scsi_qla_host *) host->hostdata;
  1419. memset(ha, 0, sizeof(*ha));
  1420. /* Save the information from PCI BIOS. */
  1421. ha->pdev = pdev;
  1422. ha->host = host;
  1423. ha->host_no = host->host_no;
  1424. pci_enable_pcie_error_reporting(pdev);
  1425. /* Setup Runtime configurable options */
  1426. if (is_qla8022(ha)) {
  1427. ha->isp_ops = &qla4_8xxx_isp_ops;
  1428. rwlock_init(&ha->hw_lock);
  1429. ha->qdr_sn_window = -1;
  1430. ha->ddr_mn_window = -1;
  1431. ha->curr_window = 255;
  1432. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  1433. nx_legacy_intr = &legacy_intr[ha->func_num];
  1434. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  1435. ha->nx_legacy_intr.tgt_status_reg =
  1436. nx_legacy_intr->tgt_status_reg;
  1437. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  1438. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  1439. } else {
  1440. ha->isp_ops = &qla4xxx_isp_ops;
  1441. }
  1442. /* Set EEH reset type to fundamental if required by hba */
  1443. if (is_qla8022(ha))
  1444. pdev->needs_freset = 1;
  1445. /* Configure PCI I/O space. */
  1446. ret = ha->isp_ops->iospace_config(ha);
  1447. if (ret)
  1448. goto probe_failed_ioconfig;
  1449. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  1450. pdev->device, pdev->irq, ha->reg);
  1451. qla4xxx_config_dma_addressing(ha);
  1452. /* Initialize lists and spinlocks. */
  1453. INIT_LIST_HEAD(&ha->ddb_list);
  1454. INIT_LIST_HEAD(&ha->free_srb_q);
  1455. mutex_init(&ha->mbox_sem);
  1456. init_completion(&ha->mbx_intr_comp);
  1457. spin_lock_init(&ha->hardware_lock);
  1458. /* Allocate dma buffers */
  1459. if (qla4xxx_mem_alloc(ha)) {
  1460. ql4_printk(KERN_WARNING, ha,
  1461. "[ERROR] Failed to allocate memory for adapter\n");
  1462. ret = -ENOMEM;
  1463. goto probe_failed;
  1464. }
  1465. if (is_qla8022(ha))
  1466. (void) qla4_8xxx_get_flash_info(ha);
  1467. /*
  1468. * Initialize the Host adapter request/response queues and
  1469. * firmware
  1470. * NOTE: interrupts enabled upon successful completion
  1471. */
  1472. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1473. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  1474. init_retry_count++ < MAX_INIT_RETRIES) {
  1475. if (is_qla8022(ha)) {
  1476. qla4_8xxx_idc_lock(ha);
  1477. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  1478. qla4_8xxx_idc_unlock(ha);
  1479. if (dev_state == QLA82XX_DEV_FAILED) {
  1480. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  1481. "initialize adapter. H/W is in failed state\n",
  1482. __func__);
  1483. break;
  1484. }
  1485. }
  1486. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  1487. "(%d)\n", __func__, init_retry_count));
  1488. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  1489. continue;
  1490. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1491. }
  1492. if (!test_bit(AF_ONLINE, &ha->flags)) {
  1493. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  1494. if (is_qla8022(ha) && ql4xdontresethba) {
  1495. /* Put the device in failed state. */
  1496. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  1497. qla4_8xxx_idc_lock(ha);
  1498. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1499. QLA82XX_DEV_FAILED);
  1500. qla4_8xxx_idc_unlock(ha);
  1501. }
  1502. ret = -ENODEV;
  1503. goto probe_failed;
  1504. }
  1505. host->cmd_per_lun = 3;
  1506. host->max_channel = 0;
  1507. host->max_lun = MAX_LUNS - 1;
  1508. host->max_id = MAX_TARGETS;
  1509. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  1510. host->can_queue = MAX_SRBS ;
  1511. host->transportt = qla4xxx_scsi_transport;
  1512. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  1513. if (ret) {
  1514. ql4_printk(KERN_WARNING, ha,
  1515. "scsi_init_shared_tag_map failed\n");
  1516. goto probe_failed;
  1517. }
  1518. /* Startup the kernel thread for this host adapter. */
  1519. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  1520. "qla4xxx_dpc\n", __func__));
  1521. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  1522. ha->dpc_thread = create_singlethread_workqueue(buf);
  1523. if (!ha->dpc_thread) {
  1524. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  1525. ret = -ENODEV;
  1526. goto probe_failed;
  1527. }
  1528. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  1529. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  1530. * (which is called indirectly by qla4xxx_initialize_adapter),
  1531. * so that irqs will be registered after crbinit but before
  1532. * mbx_intr_enable.
  1533. */
  1534. if (!is_qla8022(ha)) {
  1535. ret = qla4xxx_request_irqs(ha);
  1536. if (ret) {
  1537. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  1538. "interrupt %d already in use.\n", pdev->irq);
  1539. goto probe_failed;
  1540. }
  1541. }
  1542. pci_save_state(ha->pdev);
  1543. ha->isp_ops->enable_intrs(ha);
  1544. /* Start timer thread. */
  1545. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  1546. set_bit(AF_INIT_DONE, &ha->flags);
  1547. pci_set_drvdata(pdev, ha);
  1548. ret = scsi_add_host(host, &pdev->dev);
  1549. if (ret)
  1550. goto probe_failed;
  1551. printk(KERN_INFO
  1552. " QLogic iSCSI HBA Driver version: %s\n"
  1553. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  1554. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  1555. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  1556. ha->patch_number, ha->build_number);
  1557. scsi_scan_host(host);
  1558. return 0;
  1559. probe_failed:
  1560. qla4xxx_free_adapter(ha);
  1561. probe_failed_ioconfig:
  1562. pci_disable_pcie_error_reporting(pdev);
  1563. scsi_host_put(ha->host);
  1564. probe_disable_device:
  1565. pci_disable_device(pdev);
  1566. return ret;
  1567. }
  1568. /**
  1569. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  1570. * @ha: pointer to adapter structure
  1571. *
  1572. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  1573. * so that the other port will not re-initialize while in the process of
  1574. * removing the ha due to driver unload or hba hotplug.
  1575. **/
  1576. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  1577. {
  1578. struct scsi_qla_host *other_ha = NULL;
  1579. struct pci_dev *other_pdev = NULL;
  1580. int fn = ISP4XXX_PCI_FN_2;
  1581. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  1582. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  1583. fn = ISP4XXX_PCI_FN_1;
  1584. other_pdev =
  1585. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  1586. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  1587. fn));
  1588. /* Get other_ha if other_pdev is valid and state is enable*/
  1589. if (other_pdev) {
  1590. if (atomic_read(&other_pdev->enable_cnt)) {
  1591. other_ha = pci_get_drvdata(other_pdev);
  1592. if (other_ha) {
  1593. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  1594. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  1595. "Prevent %s reinit\n", __func__,
  1596. dev_name(&other_ha->pdev->dev)));
  1597. }
  1598. }
  1599. pci_dev_put(other_pdev);
  1600. }
  1601. }
  1602. /**
  1603. * qla4xxx_remove_adapter - calback function to remove adapter.
  1604. * @pci_dev: PCI device pointer
  1605. **/
  1606. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  1607. {
  1608. struct scsi_qla_host *ha;
  1609. ha = pci_get_drvdata(pdev);
  1610. if (!is_qla8022(ha))
  1611. qla4xxx_prevent_other_port_reinit(ha);
  1612. /* remove devs from iscsi_sessions to scsi_devices */
  1613. qla4xxx_free_ddb_list(ha);
  1614. scsi_remove_host(ha->host);
  1615. qla4xxx_free_adapter(ha);
  1616. scsi_host_put(ha->host);
  1617. pci_disable_pcie_error_reporting(pdev);
  1618. pci_disable_device(pdev);
  1619. pci_set_drvdata(pdev, NULL);
  1620. }
  1621. /**
  1622. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  1623. * @ha: HA context
  1624. *
  1625. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  1626. * supported addressing method.
  1627. */
  1628. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  1629. {
  1630. int retval;
  1631. /* Update our PCI device dma_mask for full 64 bit mask */
  1632. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  1633. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  1634. dev_dbg(&ha->pdev->dev,
  1635. "Failed to set 64 bit PCI consistent mask; "
  1636. "using 32 bit.\n");
  1637. retval = pci_set_consistent_dma_mask(ha->pdev,
  1638. DMA_BIT_MASK(32));
  1639. }
  1640. } else
  1641. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  1642. }
  1643. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  1644. {
  1645. struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
  1646. struct ddb_entry *ddb = sess->dd_data;
  1647. int queue_depth = QL4_DEF_QDEPTH;
  1648. sdev->hostdata = ddb;
  1649. sdev->tagged_supported = 1;
  1650. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  1651. queue_depth = ql4xmaxqdepth;
  1652. scsi_activate_tcq(sdev, queue_depth);
  1653. return 0;
  1654. }
  1655. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  1656. {
  1657. sdev->tagged_supported = 1;
  1658. return 0;
  1659. }
  1660. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  1661. {
  1662. scsi_deactivate_tcq(sdev, 1);
  1663. }
  1664. /**
  1665. * qla4xxx_del_from_active_array - returns an active srb
  1666. * @ha: Pointer to host adapter structure.
  1667. * @index: index into the active_array
  1668. *
  1669. * This routine removes and returns the srb at the specified index
  1670. **/
  1671. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  1672. uint32_t index)
  1673. {
  1674. struct srb *srb = NULL;
  1675. struct scsi_cmnd *cmd = NULL;
  1676. cmd = scsi_host_find_tag(ha->host, index);
  1677. if (!cmd)
  1678. return srb;
  1679. srb = (struct srb *)CMD_SP(cmd);
  1680. if (!srb)
  1681. return srb;
  1682. /* update counters */
  1683. if (srb->flags & SRB_DMA_VALID) {
  1684. ha->req_q_count += srb->iocb_cnt;
  1685. ha->iocb_cnt -= srb->iocb_cnt;
  1686. if (srb->cmd)
  1687. srb->cmd->host_scribble =
  1688. (unsigned char *)(unsigned long) MAX_SRBS;
  1689. }
  1690. return srb;
  1691. }
  1692. /**
  1693. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  1694. * @ha: Pointer to host adapter structure.
  1695. * @cmd: Scsi Command to wait on.
  1696. *
  1697. * This routine waits for the command to be returned by the Firmware
  1698. * for some max time.
  1699. **/
  1700. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  1701. struct scsi_cmnd *cmd)
  1702. {
  1703. int done = 0;
  1704. struct srb *rp;
  1705. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  1706. int ret = SUCCESS;
  1707. /* Dont wait on command if PCI error is being handled
  1708. * by PCI AER driver
  1709. */
  1710. if (unlikely(pci_channel_offline(ha->pdev)) ||
  1711. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  1712. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  1713. ha->host_no, __func__);
  1714. return ret;
  1715. }
  1716. do {
  1717. /* Checking to see if its returned to OS */
  1718. rp = (struct srb *) CMD_SP(cmd);
  1719. if (rp == NULL) {
  1720. done++;
  1721. break;
  1722. }
  1723. msleep(2000);
  1724. } while (max_wait_time--);
  1725. return done;
  1726. }
  1727. /**
  1728. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  1729. * @ha: Pointer to host adapter structure
  1730. **/
  1731. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  1732. {
  1733. unsigned long wait_online;
  1734. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  1735. while (time_before(jiffies, wait_online)) {
  1736. if (adapter_up(ha))
  1737. return QLA_SUCCESS;
  1738. msleep(2000);
  1739. }
  1740. return QLA_ERROR;
  1741. }
  1742. /**
  1743. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  1744. * @ha: pointer to HBA
  1745. * @t: target id
  1746. * @l: lun id
  1747. *
  1748. * This function waits for all outstanding commands to a lun to complete. It
  1749. * returns 0 if all pending commands are returned and 1 otherwise.
  1750. **/
  1751. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  1752. struct scsi_target *stgt,
  1753. struct scsi_device *sdev)
  1754. {
  1755. int cnt;
  1756. int status = 0;
  1757. struct scsi_cmnd *cmd;
  1758. /*
  1759. * Waiting for all commands for the designated target or dev
  1760. * in the active array
  1761. */
  1762. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  1763. cmd = scsi_host_find_tag(ha->host, cnt);
  1764. if (cmd && stgt == scsi_target(cmd->device) &&
  1765. (!sdev || sdev == cmd->device)) {
  1766. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1767. status++;
  1768. break;
  1769. }
  1770. }
  1771. }
  1772. return status;
  1773. }
  1774. /**
  1775. * qla4xxx_eh_abort - callback for abort task.
  1776. * @cmd: Pointer to Linux's SCSI command structure
  1777. *
  1778. * This routine is called by the Linux OS to abort the specified
  1779. * command.
  1780. **/
  1781. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  1782. {
  1783. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1784. unsigned int id = cmd->device->id;
  1785. unsigned int lun = cmd->device->lun;
  1786. unsigned long flags;
  1787. struct srb *srb = NULL;
  1788. int ret = SUCCESS;
  1789. int wait = 0;
  1790. ql4_printk(KERN_INFO, ha,
  1791. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  1792. ha->host_no, id, lun, cmd);
  1793. spin_lock_irqsave(&ha->hardware_lock, flags);
  1794. srb = (struct srb *) CMD_SP(cmd);
  1795. if (!srb) {
  1796. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1797. return SUCCESS;
  1798. }
  1799. kref_get(&srb->srb_ref);
  1800. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1801. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  1802. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  1803. ha->host_no, id, lun));
  1804. ret = FAILED;
  1805. } else {
  1806. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  1807. ha->host_no, id, lun));
  1808. wait = 1;
  1809. }
  1810. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  1811. /* Wait for command to complete */
  1812. if (wait) {
  1813. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1814. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  1815. ha->host_no, id, lun));
  1816. ret = FAILED;
  1817. }
  1818. }
  1819. ql4_printk(KERN_INFO, ha,
  1820. "scsi%ld:%d:%d: Abort command - %s\n",
  1821. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  1822. return ret;
  1823. }
  1824. /**
  1825. * qla4xxx_eh_device_reset - callback for target reset.
  1826. * @cmd: Pointer to Linux's SCSI command structure
  1827. *
  1828. * This routine is called by the Linux OS to reset all luns on the
  1829. * specified target.
  1830. **/
  1831. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  1832. {
  1833. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1834. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1835. int ret = FAILED, stat;
  1836. if (!ddb_entry)
  1837. return ret;
  1838. ret = iscsi_block_scsi_eh(cmd);
  1839. if (ret)
  1840. return ret;
  1841. ret = FAILED;
  1842. ql4_printk(KERN_INFO, ha,
  1843. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  1844. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1845. DEBUG2(printk(KERN_INFO
  1846. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  1847. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  1848. cmd, jiffies, cmd->request->timeout / HZ,
  1849. ha->dpc_flags, cmd->result, cmd->allowed));
  1850. /* FIXME: wait for hba to go online */
  1851. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  1852. if (stat != QLA_SUCCESS) {
  1853. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  1854. goto eh_dev_reset_done;
  1855. }
  1856. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  1857. cmd->device)) {
  1858. ql4_printk(KERN_INFO, ha,
  1859. "DEVICE RESET FAILED - waiting for "
  1860. "commands.\n");
  1861. goto eh_dev_reset_done;
  1862. }
  1863. /* Send marker. */
  1864. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  1865. MM_LUN_RESET) != QLA_SUCCESS)
  1866. goto eh_dev_reset_done;
  1867. ql4_printk(KERN_INFO, ha,
  1868. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  1869. ha->host_no, cmd->device->channel, cmd->device->id,
  1870. cmd->device->lun);
  1871. ret = SUCCESS;
  1872. eh_dev_reset_done:
  1873. return ret;
  1874. }
  1875. /**
  1876. * qla4xxx_eh_target_reset - callback for target reset.
  1877. * @cmd: Pointer to Linux's SCSI command structure
  1878. *
  1879. * This routine is called by the Linux OS to reset the target.
  1880. **/
  1881. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  1882. {
  1883. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1884. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1885. int stat, ret;
  1886. if (!ddb_entry)
  1887. return FAILED;
  1888. ret = iscsi_block_scsi_eh(cmd);
  1889. if (ret)
  1890. return ret;
  1891. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1892. "WARM TARGET RESET ISSUED.\n");
  1893. DEBUG2(printk(KERN_INFO
  1894. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  1895. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  1896. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  1897. ha->dpc_flags, cmd->result, cmd->allowed));
  1898. stat = qla4xxx_reset_target(ha, ddb_entry);
  1899. if (stat != QLA_SUCCESS) {
  1900. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1901. "WARM TARGET RESET FAILED.\n");
  1902. return FAILED;
  1903. }
  1904. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  1905. NULL)) {
  1906. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1907. "WARM TARGET DEVICE RESET FAILED - "
  1908. "waiting for commands.\n");
  1909. return FAILED;
  1910. }
  1911. /* Send marker. */
  1912. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  1913. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  1914. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1915. "WARM TARGET DEVICE RESET FAILED - "
  1916. "marker iocb failed.\n");
  1917. return FAILED;
  1918. }
  1919. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1920. "WARM TARGET RESET SUCCEEDED.\n");
  1921. return SUCCESS;
  1922. }
  1923. /**
  1924. * qla4xxx_eh_host_reset - kernel callback
  1925. * @cmd: Pointer to Linux's SCSI command structure
  1926. *
  1927. * This routine is invoked by the Linux kernel to perform fatal error
  1928. * recovery on the specified adapter.
  1929. **/
  1930. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  1931. {
  1932. int return_status = FAILED;
  1933. struct scsi_qla_host *ha;
  1934. ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
  1935. if (ql4xdontresethba) {
  1936. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1937. ha->host_no, __func__));
  1938. return FAILED;
  1939. }
  1940. ql4_printk(KERN_INFO, ha,
  1941. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  1942. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1943. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  1944. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  1945. "DEAD.\n", ha->host_no, cmd->device->channel,
  1946. __func__));
  1947. return FAILED;
  1948. }
  1949. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1950. if (is_qla8022(ha))
  1951. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1952. else
  1953. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1954. }
  1955. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  1956. return_status = SUCCESS;
  1957. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  1958. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  1959. return return_status;
  1960. }
  1961. /* PCI AER driver recovers from all correctable errors w/o
  1962. * driver intervention. For uncorrectable errors PCI AER
  1963. * driver calls the following device driver's callbacks
  1964. *
  1965. * - Fatal Errors - link_reset
  1966. * - Non-Fatal Errors - driver's pci_error_detected() which
  1967. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  1968. *
  1969. * PCI AER driver calls
  1970. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  1971. * returns RECOVERED or NEED_RESET if fw_hung
  1972. * NEED_RESET - driver's slot_reset()
  1973. * DISCONNECT - device is dead & cannot recover
  1974. * RECOVERED - driver's pci_resume()
  1975. */
  1976. static pci_ers_result_t
  1977. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  1978. {
  1979. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  1980. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  1981. ha->host_no, __func__, state);
  1982. if (!is_aer_supported(ha))
  1983. return PCI_ERS_RESULT_NONE;
  1984. switch (state) {
  1985. case pci_channel_io_normal:
  1986. clear_bit(AF_EEH_BUSY, &ha->flags);
  1987. return PCI_ERS_RESULT_CAN_RECOVER;
  1988. case pci_channel_io_frozen:
  1989. set_bit(AF_EEH_BUSY, &ha->flags);
  1990. qla4xxx_mailbox_premature_completion(ha);
  1991. qla4xxx_free_irqs(ha);
  1992. pci_disable_device(pdev);
  1993. /* Return back all IOs */
  1994. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1995. return PCI_ERS_RESULT_NEED_RESET;
  1996. case pci_channel_io_perm_failure:
  1997. set_bit(AF_EEH_BUSY, &ha->flags);
  1998. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  1999. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  2000. return PCI_ERS_RESULT_DISCONNECT;
  2001. }
  2002. return PCI_ERS_RESULT_NEED_RESET;
  2003. }
  2004. /**
  2005. * qla4xxx_pci_mmio_enabled() gets called if
  2006. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  2007. * and read/write to the device still works.
  2008. **/
  2009. static pci_ers_result_t
  2010. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  2011. {
  2012. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2013. if (!is_aer_supported(ha))
  2014. return PCI_ERS_RESULT_NONE;
  2015. return PCI_ERS_RESULT_RECOVERED;
  2016. }
  2017. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  2018. {
  2019. uint32_t rval = QLA_ERROR;
  2020. uint32_t ret = 0;
  2021. int fn;
  2022. struct pci_dev *other_pdev = NULL;
  2023. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  2024. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2025. if (test_bit(AF_ONLINE, &ha->flags)) {
  2026. clear_bit(AF_ONLINE, &ha->flags);
  2027. qla4xxx_mark_all_devices_missing(ha);
  2028. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2029. }
  2030. fn = PCI_FUNC(ha->pdev->devfn);
  2031. while (fn > 0) {
  2032. fn--;
  2033. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  2034. "func %x\n", ha->host_no, __func__, fn);
  2035. /* Get the pci device given the domain, bus,
  2036. * slot/function number */
  2037. other_pdev =
  2038. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  2039. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  2040. fn));
  2041. if (!other_pdev)
  2042. continue;
  2043. if (atomic_read(&other_pdev->enable_cnt)) {
  2044. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  2045. "func in enabled state%x\n", ha->host_no,
  2046. __func__, fn);
  2047. pci_dev_put(other_pdev);
  2048. break;
  2049. }
  2050. pci_dev_put(other_pdev);
  2051. }
  2052. /* The first function on the card, the reset owner will
  2053. * start & initialize the firmware. The other functions
  2054. * on the card will reset the firmware context
  2055. */
  2056. if (!fn) {
  2057. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  2058. "0x%x is the owner\n", ha->host_no, __func__,
  2059. ha->pdev->devfn);
  2060. qla4_8xxx_idc_lock(ha);
  2061. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2062. QLA82XX_DEV_COLD);
  2063. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  2064. QLA82XX_IDC_VERSION);
  2065. qla4_8xxx_idc_unlock(ha);
  2066. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2067. rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  2068. qla4_8xxx_idc_lock(ha);
  2069. if (rval != QLA_SUCCESS) {
  2070. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2071. "FAILED\n", ha->host_no, __func__);
  2072. qla4_8xxx_clear_drv_active(ha);
  2073. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2074. QLA82XX_DEV_FAILED);
  2075. } else {
  2076. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2077. "READY\n", ha->host_no, __func__);
  2078. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2079. QLA82XX_DEV_READY);
  2080. /* Clear driver state register */
  2081. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  2082. qla4_8xxx_set_drv_active(ha);
  2083. ret = qla4xxx_request_irqs(ha);
  2084. if (ret) {
  2085. ql4_printk(KERN_WARNING, ha, "Failed to "
  2086. "reserve interrupt %d already in use.\n",
  2087. ha->pdev->irq);
  2088. rval = QLA_ERROR;
  2089. } else {
  2090. ha->isp_ops->enable_intrs(ha);
  2091. rval = QLA_SUCCESS;
  2092. }
  2093. }
  2094. qla4_8xxx_idc_unlock(ha);
  2095. } else {
  2096. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  2097. "the reset owner\n", ha->host_no, __func__,
  2098. ha->pdev->devfn);
  2099. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  2100. QLA82XX_DEV_READY)) {
  2101. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2102. rval = qla4xxx_initialize_adapter(ha,
  2103. PRESERVE_DDB_LIST);
  2104. if (rval == QLA_SUCCESS) {
  2105. ret = qla4xxx_request_irqs(ha);
  2106. if (ret) {
  2107. ql4_printk(KERN_WARNING, ha, "Failed to"
  2108. " reserve interrupt %d already in"
  2109. " use.\n", ha->pdev->irq);
  2110. rval = QLA_ERROR;
  2111. } else {
  2112. ha->isp_ops->enable_intrs(ha);
  2113. rval = QLA_SUCCESS;
  2114. }
  2115. }
  2116. qla4_8xxx_idc_lock(ha);
  2117. qla4_8xxx_set_drv_active(ha);
  2118. qla4_8xxx_idc_unlock(ha);
  2119. }
  2120. }
  2121. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2122. return rval;
  2123. }
  2124. static pci_ers_result_t
  2125. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  2126. {
  2127. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  2128. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2129. int rc;
  2130. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  2131. ha->host_no, __func__);
  2132. if (!is_aer_supported(ha))
  2133. return PCI_ERS_RESULT_NONE;
  2134. /* Restore the saved state of PCIe device -
  2135. * BAR registers, PCI Config space, PCIX, MSI,
  2136. * IOV states
  2137. */
  2138. pci_restore_state(pdev);
  2139. /* pci_restore_state() clears the saved_state flag of the device
  2140. * save restored state which resets saved_state flag
  2141. */
  2142. pci_save_state(pdev);
  2143. /* Initialize device or resume if in suspended state */
  2144. rc = pci_enable_device(pdev);
  2145. if (rc) {
  2146. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  2147. "device after reset\n", ha->host_no, __func__);
  2148. goto exit_slot_reset;
  2149. }
  2150. ha->isp_ops->disable_intrs(ha);
  2151. if (is_qla8022(ha)) {
  2152. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  2153. ret = PCI_ERS_RESULT_RECOVERED;
  2154. goto exit_slot_reset;
  2155. } else
  2156. goto exit_slot_reset;
  2157. }
  2158. exit_slot_reset:
  2159. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  2160. "device after reset\n", ha->host_no, __func__, ret);
  2161. return ret;
  2162. }
  2163. static void
  2164. qla4xxx_pci_resume(struct pci_dev *pdev)
  2165. {
  2166. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2167. int ret;
  2168. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  2169. ha->host_no, __func__);
  2170. ret = qla4xxx_wait_for_hba_online(ha);
  2171. if (ret != QLA_SUCCESS) {
  2172. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  2173. "resume I/O from slot/link_reset\n", ha->host_no,
  2174. __func__);
  2175. }
  2176. pci_cleanup_aer_uncorrect_error_status(pdev);
  2177. clear_bit(AF_EEH_BUSY, &ha->flags);
  2178. }
  2179. static struct pci_error_handlers qla4xxx_err_handler = {
  2180. .error_detected = qla4xxx_pci_error_detected,
  2181. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  2182. .slot_reset = qla4xxx_pci_slot_reset,
  2183. .resume = qla4xxx_pci_resume,
  2184. };
  2185. static struct pci_device_id qla4xxx_pci_tbl[] = {
  2186. {
  2187. .vendor = PCI_VENDOR_ID_QLOGIC,
  2188. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  2189. .subvendor = PCI_ANY_ID,
  2190. .subdevice = PCI_ANY_ID,
  2191. },
  2192. {
  2193. .vendor = PCI_VENDOR_ID_QLOGIC,
  2194. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  2195. .subvendor = PCI_ANY_ID,
  2196. .subdevice = PCI_ANY_ID,
  2197. },
  2198. {
  2199. .vendor = PCI_VENDOR_ID_QLOGIC,
  2200. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  2201. .subvendor = PCI_ANY_ID,
  2202. .subdevice = PCI_ANY_ID,
  2203. },
  2204. {
  2205. .vendor = PCI_VENDOR_ID_QLOGIC,
  2206. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  2207. .subvendor = PCI_ANY_ID,
  2208. .subdevice = PCI_ANY_ID,
  2209. },
  2210. {0, 0},
  2211. };
  2212. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  2213. static struct pci_driver qla4xxx_pci_driver = {
  2214. .name = DRIVER_NAME,
  2215. .id_table = qla4xxx_pci_tbl,
  2216. .probe = qla4xxx_probe_adapter,
  2217. .remove = qla4xxx_remove_adapter,
  2218. .err_handler = &qla4xxx_err_handler,
  2219. };
  2220. static int __init qla4xxx_module_init(void)
  2221. {
  2222. int ret;
  2223. /* Allocate cache for SRBs. */
  2224. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  2225. SLAB_HWCACHE_ALIGN, NULL);
  2226. if (srb_cachep == NULL) {
  2227. printk(KERN_ERR
  2228. "%s: Unable to allocate SRB cache..."
  2229. "Failing load!\n", DRIVER_NAME);
  2230. ret = -ENOMEM;
  2231. goto no_srp_cache;
  2232. }
  2233. /* Derive version string. */
  2234. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  2235. if (ql4xextended_error_logging)
  2236. strcat(qla4xxx_version_str, "-debug");
  2237. qla4xxx_scsi_transport =
  2238. iscsi_register_transport(&qla4xxx_iscsi_transport);
  2239. if (!qla4xxx_scsi_transport){
  2240. ret = -ENODEV;
  2241. goto release_srb_cache;
  2242. }
  2243. ret = pci_register_driver(&qla4xxx_pci_driver);
  2244. if (ret)
  2245. goto unregister_transport;
  2246. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  2247. return 0;
  2248. unregister_transport:
  2249. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2250. release_srb_cache:
  2251. kmem_cache_destroy(srb_cachep);
  2252. no_srp_cache:
  2253. return ret;
  2254. }
  2255. static void __exit qla4xxx_module_exit(void)
  2256. {
  2257. pci_unregister_driver(&qla4xxx_pci_driver);
  2258. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2259. kmem_cache_destroy(srb_cachep);
  2260. }
  2261. module_init(qla4xxx_module_init);
  2262. module_exit(qla4xxx_module_exit);
  2263. MODULE_AUTHOR("QLogic Corporation");
  2264. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  2265. MODULE_LICENSE("GPL");
  2266. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);