ql4_os.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2006 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <scsi/scsi_tcq.h>
  9. #include <scsi/scsicam.h>
  10. #include "ql4_def.h"
  11. #include "ql4_version.h"
  12. #include "ql4_glbl.h"
  13. #include "ql4_dbg.h"
  14. #include "ql4_inline.h"
  15. /*
  16. * Driver version
  17. */
  18. static char qla4xxx_version_str[40];
  19. /*
  20. * SRB allocation cache
  21. */
  22. static struct kmem_cache *srb_cachep;
  23. /*
  24. * Module parameter information and variables
  25. */
  26. int ql4xdiscoverywait = 60;
  27. module_param(ql4xdiscoverywait, int, S_IRUGO | S_IRUSR);
  28. MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
  29. int ql4xdontresethba = 0;
  30. module_param(ql4xdontresethba, int, S_IRUGO | S_IRUSR);
  31. MODULE_PARM_DESC(ql4xdontresethba,
  32. "Dont reset the HBA when the driver gets 0x8002 AEN "
  33. " default it will reset hba :0"
  34. " set to 1 to avoid resetting HBA");
  35. int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
  36. module_param(ql4xextended_error_logging, int, S_IRUGO | S_IRUSR);
  37. MODULE_PARM_DESC(ql4xextended_error_logging,
  38. "Option to enable extended error logging, "
  39. "Default is 0 - no logging, 1 - debug logging");
  40. int ql4_mod_unload = 0;
  41. /*
  42. * SCSI host template entry points
  43. */
  44. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
  45. /*
  46. * iSCSI template entry points
  47. */
  48. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  49. enum iscsi_tgt_dscvr type, uint32_t enable,
  50. struct sockaddr *dst_addr);
  51. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  52. enum iscsi_param param, char *buf);
  53. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  54. enum iscsi_param param, char *buf);
  55. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  56. enum iscsi_host_param param, char *buf);
  57. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
  58. /*
  59. * SCSI host template entry points
  60. */
  61. static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
  62. void (*done) (struct scsi_cmnd *));
  63. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  64. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  65. static int qla4xxx_slave_alloc(struct scsi_device *device);
  66. static int qla4xxx_slave_configure(struct scsi_device *device);
  67. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  68. static struct scsi_host_template qla4xxx_driver_template = {
  69. .module = THIS_MODULE,
  70. .name = DRIVER_NAME,
  71. .proc_name = DRIVER_NAME,
  72. .queuecommand = qla4xxx_queuecommand,
  73. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  74. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  75. .slave_configure = qla4xxx_slave_configure,
  76. .slave_alloc = qla4xxx_slave_alloc,
  77. .slave_destroy = qla4xxx_slave_destroy,
  78. .scan_finished = iscsi_scan_finished,
  79. .this_id = -1,
  80. .cmd_per_lun = 3,
  81. .use_clustering = ENABLE_CLUSTERING,
  82. .sg_tablesize = SG_ALL,
  83. .max_sectors = 0xFFFF,
  84. };
  85. static struct iscsi_transport qla4xxx_iscsi_transport = {
  86. .owner = THIS_MODULE,
  87. .name = DRIVER_NAME,
  88. .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
  89. CAP_DATA_PATH_OFFLOAD,
  90. .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
  91. ISCSI_TARGET_NAME | ISCSI_TPGT,
  92. .host_param_mask = ISCSI_HOST_HWADDRESS |
  93. ISCSI_HOST_IPADDRESS |
  94. ISCSI_HOST_INITIATOR_NAME,
  95. .sessiondata_size = sizeof(struct ddb_entry),
  96. .host_template = &qla4xxx_driver_template,
  97. .tgt_dscvr = qla4xxx_tgt_dscvr,
  98. .get_conn_param = qla4xxx_conn_get_param,
  99. .get_session_param = qla4xxx_sess_get_param,
  100. .get_host_param = qla4xxx_host_get_param,
  101. .session_recovery_timedout = qla4xxx_recovery_timedout,
  102. };
  103. static struct scsi_transport_template *qla4xxx_scsi_transport;
  104. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
  105. {
  106. struct ddb_entry *ddb_entry = session->dd_data;
  107. struct scsi_qla_host *ha = ddb_entry->ha;
  108. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  109. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  110. DEBUG2(printk("scsi%ld: %s: index [%d] port down retry count "
  111. "of (%d) secs exhausted, marking device DEAD.\n",
  112. ha->host_no, __func__, ddb_entry->fw_ddb_index,
  113. ha->port_down_retry_count));
  114. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine - dpc "
  115. "flags = 0x%lx\n",
  116. ha->host_no, __func__, ha->dpc_flags));
  117. queue_work(ha->dpc_thread, &ha->dpc_work);
  118. }
  119. }
  120. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  121. enum iscsi_host_param param, char *buf)
  122. {
  123. struct scsi_qla_host *ha = to_qla_host(shost);
  124. int len;
  125. switch (param) {
  126. case ISCSI_HOST_PARAM_HWADDRESS:
  127. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  128. break;
  129. case ISCSI_HOST_PARAM_IPADDRESS:
  130. len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
  131. ha->ip_address[1], ha->ip_address[2],
  132. ha->ip_address[3]);
  133. break;
  134. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  135. len = sprintf(buf, "%s\n", ha->name_string);
  136. break;
  137. default:
  138. return -ENOSYS;
  139. }
  140. return len;
  141. }
  142. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  143. enum iscsi_param param, char *buf)
  144. {
  145. struct ddb_entry *ddb_entry = sess->dd_data;
  146. int len;
  147. switch (param) {
  148. case ISCSI_PARAM_TARGET_NAME:
  149. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  150. ddb_entry->iscsi_name);
  151. break;
  152. case ISCSI_PARAM_TPGT:
  153. len = sprintf(buf, "%u\n", ddb_entry->tpgt);
  154. break;
  155. default:
  156. return -ENOSYS;
  157. }
  158. return len;
  159. }
  160. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  161. enum iscsi_param param, char *buf)
  162. {
  163. struct iscsi_cls_session *session;
  164. struct ddb_entry *ddb_entry;
  165. int len;
  166. session = iscsi_dev_to_session(conn->dev.parent);
  167. ddb_entry = session->dd_data;
  168. switch (param) {
  169. case ISCSI_PARAM_CONN_PORT:
  170. len = sprintf(buf, "%hu\n", ddb_entry->port);
  171. break;
  172. case ISCSI_PARAM_CONN_ADDRESS:
  173. /* TODO: what are the ipv6 bits */
  174. len = sprintf(buf, "%u.%u.%u.%u\n",
  175. NIPQUAD(ddb_entry->ip_addr));
  176. break;
  177. default:
  178. return -ENOSYS;
  179. }
  180. return len;
  181. }
  182. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  183. enum iscsi_tgt_dscvr type, uint32_t enable,
  184. struct sockaddr *dst_addr)
  185. {
  186. struct scsi_qla_host *ha;
  187. struct sockaddr_in *addr;
  188. struct sockaddr_in6 *addr6;
  189. int ret = 0;
  190. ha = (struct scsi_qla_host *) shost->hostdata;
  191. switch (type) {
  192. case ISCSI_TGT_DSCVR_SEND_TARGETS:
  193. if (dst_addr->sa_family == AF_INET) {
  194. addr = (struct sockaddr_in *)dst_addr;
  195. if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
  196. addr->sin_port) != QLA_SUCCESS)
  197. ret = -EIO;
  198. } else if (dst_addr->sa_family == AF_INET6) {
  199. /*
  200. * TODO: fix qla4xxx_send_tgts
  201. */
  202. addr6 = (struct sockaddr_in6 *)dst_addr;
  203. if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
  204. addr6->sin6_port) != QLA_SUCCESS)
  205. ret = -EIO;
  206. } else
  207. ret = -ENOSYS;
  208. break;
  209. default:
  210. ret = -ENOSYS;
  211. }
  212. return ret;
  213. }
  214. void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
  215. {
  216. if (!ddb_entry->sess)
  217. return;
  218. if (ddb_entry->conn) {
  219. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  220. iscsi_remove_session(ddb_entry->sess);
  221. }
  222. iscsi_free_session(ddb_entry->sess);
  223. }
  224. int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
  225. {
  226. int err;
  227. ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
  228. err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
  229. if (err) {
  230. DEBUG2(printk(KERN_ERR "Could not add session.\n"));
  231. return err;
  232. }
  233. ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0);
  234. if (!ddb_entry->conn) {
  235. iscsi_remove_session(ddb_entry->sess);
  236. DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
  237. return -ENOMEM;
  238. }
  239. /* finally ready to go */
  240. iscsi_unblock_session(ddb_entry->sess);
  241. return 0;
  242. }
  243. struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
  244. {
  245. struct ddb_entry *ddb_entry;
  246. struct iscsi_cls_session *sess;
  247. sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport);
  248. if (!sess)
  249. return NULL;
  250. ddb_entry = sess->dd_data;
  251. memset(ddb_entry, 0, sizeof(*ddb_entry));
  252. ddb_entry->ha = ha;
  253. ddb_entry->sess = sess;
  254. return ddb_entry;
  255. }
  256. /*
  257. * Timer routines
  258. */
  259. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  260. unsigned long interval)
  261. {
  262. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  263. __func__, ha->host->host_no));
  264. init_timer(&ha->timer);
  265. ha->timer.expires = jiffies + interval * HZ;
  266. ha->timer.data = (unsigned long)ha;
  267. ha->timer.function = (void (*)(unsigned long))func;
  268. add_timer(&ha->timer);
  269. ha->timer_active = 1;
  270. }
  271. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  272. {
  273. del_timer_sync(&ha->timer);
  274. ha->timer_active = 0;
  275. }
  276. /***
  277. * qla4xxx_mark_device_missing - mark a device as missing.
  278. * @ha: Pointer to host adapter structure.
  279. * @ddb_entry: Pointer to device database entry
  280. *
  281. * This routine marks a device missing and resets the relogin retry count.
  282. **/
  283. void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
  284. struct ddb_entry *ddb_entry)
  285. {
  286. atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
  287. DEBUG3(printk("scsi%d:%d:%d: index [%d] marked MISSING\n",
  288. ha->host_no, ddb_entry->bus, ddb_entry->target,
  289. ddb_entry->fw_ddb_index));
  290. iscsi_block_session(ddb_entry->sess);
  291. iscsi_conn_error(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
  292. }
  293. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  294. struct ddb_entry *ddb_entry,
  295. struct scsi_cmnd *cmd,
  296. void (*done)(struct scsi_cmnd *))
  297. {
  298. struct srb *srb;
  299. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  300. if (!srb)
  301. return srb;
  302. atomic_set(&srb->ref_count, 1);
  303. srb->ha = ha;
  304. srb->ddb = ddb_entry;
  305. srb->cmd = cmd;
  306. srb->flags = 0;
  307. cmd->SCp.ptr = (void *)srb;
  308. cmd->scsi_done = done;
  309. return srb;
  310. }
  311. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  312. {
  313. struct scsi_cmnd *cmd = srb->cmd;
  314. if (srb->flags & SRB_DMA_VALID) {
  315. scsi_dma_unmap(cmd);
  316. srb->flags &= ~SRB_DMA_VALID;
  317. }
  318. cmd->SCp.ptr = NULL;
  319. }
  320. void qla4xxx_srb_compl(struct scsi_qla_host *ha, struct srb *srb)
  321. {
  322. struct scsi_cmnd *cmd = srb->cmd;
  323. qla4xxx_srb_free_dma(ha, srb);
  324. mempool_free(srb, ha->srb_mempool);
  325. cmd->scsi_done(cmd);
  326. }
  327. /**
  328. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  329. * @cmd: Pointer to Linux's SCSI command structure
  330. * @done_fn: Function that the driver calls to notify the SCSI mid-layer
  331. * that the command has been processed.
  332. *
  333. * Remarks:
  334. * This routine is invoked by Linux to send a SCSI command to the driver.
  335. * The mid-level driver tries to ensure that queuecommand never gets
  336. * invoked concurrently with itself or the interrupt handler (although
  337. * the interrupt handler may call this routine as part of request-
  338. * completion handling). Unfortunely, it sometimes calls the scheduler
  339. * in interrupt context which is a big NO! NO!.
  340. **/
  341. static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
  342. void (*done)(struct scsi_cmnd *))
  343. {
  344. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  345. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  346. struct iscsi_cls_session *sess = ddb_entry->sess;
  347. struct srb *srb;
  348. int rval;
  349. if (!sess) {
  350. cmd->result = DID_IMM_RETRY << 16;
  351. goto qc_fail_command;
  352. }
  353. rval = iscsi_session_chkready(sess);
  354. if (rval) {
  355. cmd->result = rval;
  356. goto qc_fail_command;
  357. }
  358. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  359. if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
  360. cmd->result = DID_NO_CONNECT << 16;
  361. goto qc_fail_command;
  362. }
  363. goto qc_host_busy;
  364. }
  365. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
  366. goto qc_host_busy;
  367. spin_unlock_irq(ha->host->host_lock);
  368. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
  369. if (!srb)
  370. goto qc_host_busy_lock;
  371. rval = qla4xxx_send_command_to_isp(ha, srb);
  372. if (rval != QLA_SUCCESS)
  373. goto qc_host_busy_free_sp;
  374. spin_lock_irq(ha->host->host_lock);
  375. return 0;
  376. qc_host_busy_free_sp:
  377. qla4xxx_srb_free_dma(ha, srb);
  378. mempool_free(srb, ha->srb_mempool);
  379. qc_host_busy_lock:
  380. spin_lock_irq(ha->host->host_lock);
  381. qc_host_busy:
  382. return SCSI_MLQUEUE_HOST_BUSY;
  383. qc_fail_command:
  384. done(cmd);
  385. return 0;
  386. }
  387. /**
  388. * qla4xxx_mem_free - frees memory allocated to adapter
  389. * @ha: Pointer to host adapter structure.
  390. *
  391. * Frees memory previously allocated by qla4xxx_mem_alloc
  392. **/
  393. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  394. {
  395. if (ha->queues)
  396. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  397. ha->queues_dma);
  398. ha->queues_len = 0;
  399. ha->queues = NULL;
  400. ha->queues_dma = 0;
  401. ha->request_ring = NULL;
  402. ha->request_dma = 0;
  403. ha->response_ring = NULL;
  404. ha->response_dma = 0;
  405. ha->shadow_regs = NULL;
  406. ha->shadow_regs_dma = 0;
  407. /* Free srb pool. */
  408. if (ha->srb_mempool)
  409. mempool_destroy(ha->srb_mempool);
  410. ha->srb_mempool = NULL;
  411. /* release io space registers */
  412. if (ha->reg)
  413. iounmap(ha->reg);
  414. pci_release_regions(ha->pdev);
  415. }
  416. /**
  417. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  418. * @ha: Pointer to host adapter structure
  419. *
  420. * Allocates DMA memory for request and response queues. Also allocates memory
  421. * for srbs.
  422. **/
  423. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  424. {
  425. unsigned long align;
  426. /* Allocate contiguous block of DMA memory for queues. */
  427. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  428. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  429. sizeof(struct shadow_regs) +
  430. MEM_ALIGN_VALUE +
  431. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  432. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  433. &ha->queues_dma, GFP_KERNEL);
  434. if (ha->queues == NULL) {
  435. dev_warn(&ha->pdev->dev,
  436. "Memory Allocation failed - queues.\n");
  437. goto mem_alloc_error_exit;
  438. }
  439. memset(ha->queues, 0, ha->queues_len);
  440. /*
  441. * As per RISC alignment requirements -- the bus-address must be a
  442. * multiple of the request-ring size (in bytes).
  443. */
  444. align = 0;
  445. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  446. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  447. (MEM_ALIGN_VALUE - 1));
  448. /* Update request and response queue pointers. */
  449. ha->request_dma = ha->queues_dma + align;
  450. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  451. ha->response_dma = ha->queues_dma + align +
  452. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  453. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  454. (REQUEST_QUEUE_DEPTH *
  455. QUEUE_SIZE));
  456. ha->shadow_regs_dma = ha->queues_dma + align +
  457. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  458. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  459. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  460. (REQUEST_QUEUE_DEPTH *
  461. QUEUE_SIZE) +
  462. (RESPONSE_QUEUE_DEPTH *
  463. QUEUE_SIZE));
  464. /* Allocate memory for srb pool. */
  465. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  466. mempool_free_slab, srb_cachep);
  467. if (ha->srb_mempool == NULL) {
  468. dev_warn(&ha->pdev->dev,
  469. "Memory Allocation failed - SRB Pool.\n");
  470. goto mem_alloc_error_exit;
  471. }
  472. return QLA_SUCCESS;
  473. mem_alloc_error_exit:
  474. qla4xxx_mem_free(ha);
  475. return QLA_ERROR;
  476. }
  477. /**
  478. * qla4xxx_timer - checks every second for work to do.
  479. * @ha: Pointer to host adapter structure.
  480. **/
  481. static void qla4xxx_timer(struct scsi_qla_host *ha)
  482. {
  483. struct ddb_entry *ddb_entry, *dtemp;
  484. int start_dpc = 0;
  485. /* Search for relogin's to time-out and port down retry. */
  486. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  487. /* Count down time between sending relogins */
  488. if (adapter_up(ha) &&
  489. !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  490. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  491. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  492. INVALID_ENTRY) {
  493. if (atomic_read(&ddb_entry->retry_relogin_timer)
  494. == 0) {
  495. atomic_set(&ddb_entry->
  496. retry_relogin_timer,
  497. INVALID_ENTRY);
  498. set_bit(DPC_RELOGIN_DEVICE,
  499. &ha->dpc_flags);
  500. set_bit(DF_RELOGIN, &ddb_entry->flags);
  501. DEBUG2(printk("scsi%ld: %s: index [%d]"
  502. " login device\n",
  503. ha->host_no, __func__,
  504. ddb_entry->fw_ddb_index));
  505. } else
  506. atomic_dec(&ddb_entry->
  507. retry_relogin_timer);
  508. }
  509. }
  510. /* Wait for relogin to timeout */
  511. if (atomic_read(&ddb_entry->relogin_timer) &&
  512. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  513. /*
  514. * If the relogin times out and the device is
  515. * still NOT ONLINE then try and relogin again.
  516. */
  517. if (atomic_read(&ddb_entry->state) !=
  518. DDB_STATE_ONLINE &&
  519. ddb_entry->fw_ddb_device_state ==
  520. DDB_DS_SESSION_FAILED) {
  521. /* Reset retry relogin timer */
  522. atomic_inc(&ddb_entry->relogin_retry_count);
  523. DEBUG2(printk("scsi%ld: index[%d] relogin"
  524. " timed out-retrying"
  525. " relogin (%d)\n",
  526. ha->host_no,
  527. ddb_entry->fw_ddb_index,
  528. atomic_read(&ddb_entry->
  529. relogin_retry_count))
  530. );
  531. start_dpc++;
  532. DEBUG(printk("scsi%ld:%d:%d: index [%d] "
  533. "initate relogin after"
  534. " %d seconds\n",
  535. ha->host_no, ddb_entry->bus,
  536. ddb_entry->target,
  537. ddb_entry->fw_ddb_index,
  538. ddb_entry->default_time2wait + 4)
  539. );
  540. atomic_set(&ddb_entry->retry_relogin_timer,
  541. ddb_entry->default_time2wait + 4);
  542. }
  543. }
  544. }
  545. /* Check for heartbeat interval. */
  546. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  547. ha->heartbeat_interval != 0) {
  548. ha->seconds_since_last_heartbeat++;
  549. if (ha->seconds_since_last_heartbeat >
  550. ha->heartbeat_interval + 2)
  551. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  552. }
  553. /* Wakeup the dpc routine for this adapter, if needed. */
  554. if ((start_dpc ||
  555. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  556. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  557. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  558. test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
  559. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  560. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  561. test_bit(DPC_AEN, &ha->dpc_flags)) &&
  562. ha->dpc_thread) {
  563. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  564. " - dpc flags = 0x%lx\n",
  565. ha->host_no, __func__, ha->dpc_flags));
  566. queue_work(ha->dpc_thread, &ha->dpc_work);
  567. }
  568. /* Reschedule timer thread to call us back in one second */
  569. mod_timer(&ha->timer, jiffies + HZ);
  570. DEBUG2(ha->seconds_since_last_intr++);
  571. }
  572. /**
  573. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  574. * @ha: Pointer to host adapter structure.
  575. *
  576. * This routine stalls the driver until all outstanding commands are returned.
  577. * Caller must release the Hardware Lock prior to calling this routine.
  578. **/
  579. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  580. {
  581. uint32_t index = 0;
  582. int stat = QLA_SUCCESS;
  583. unsigned long flags;
  584. struct scsi_cmnd *cmd;
  585. int wait_cnt = WAIT_CMD_TOV; /*
  586. * Initialized for 30 seconds as we
  587. * expect all commands to retuned
  588. * ASAP.
  589. */
  590. while (wait_cnt) {
  591. spin_lock_irqsave(&ha->hardware_lock, flags);
  592. /* Find a command that hasn't completed. */
  593. for (index = 0; index < ha->host->can_queue; index++) {
  594. cmd = scsi_host_find_tag(ha->host, index);
  595. if (cmd != NULL)
  596. break;
  597. }
  598. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  599. /* If No Commands are pending, wait is complete */
  600. if (index == ha->host->can_queue) {
  601. break;
  602. }
  603. /* If we timed out on waiting for commands to come back
  604. * return ERROR.
  605. */
  606. wait_cnt--;
  607. if (wait_cnt == 0)
  608. stat = QLA_ERROR;
  609. else {
  610. msleep(1000);
  611. }
  612. } /* End of While (wait_cnt) */
  613. return stat;
  614. }
  615. void qla4xxx_hw_reset(struct scsi_qla_host *ha)
  616. {
  617. uint32_t ctrl_status;
  618. unsigned long flags = 0;
  619. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  620. spin_lock_irqsave(&ha->hardware_lock, flags);
  621. /*
  622. * If the SCSI Reset Interrupt bit is set, clear it.
  623. * Otherwise, the Soft Reset won't work.
  624. */
  625. ctrl_status = readw(&ha->reg->ctrl_status);
  626. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  627. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  628. /* Issue Soft Reset */
  629. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  630. readl(&ha->reg->ctrl_status);
  631. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  632. }
  633. /**
  634. * qla4xxx_soft_reset - performs soft reset.
  635. * @ha: Pointer to host adapter structure.
  636. **/
  637. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  638. {
  639. uint32_t max_wait_time;
  640. unsigned long flags = 0;
  641. int status = QLA_ERROR;
  642. uint32_t ctrl_status;
  643. qla4xxx_hw_reset(ha);
  644. /* Wait until the Network Reset Intr bit is cleared */
  645. max_wait_time = RESET_INTR_TOV;
  646. do {
  647. spin_lock_irqsave(&ha->hardware_lock, flags);
  648. ctrl_status = readw(&ha->reg->ctrl_status);
  649. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  650. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  651. break;
  652. msleep(1000);
  653. } while ((--max_wait_time));
  654. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  655. DEBUG2(printk(KERN_WARNING
  656. "scsi%ld: Network Reset Intr not cleared by "
  657. "Network function, clearing it now!\n",
  658. ha->host_no));
  659. spin_lock_irqsave(&ha->hardware_lock, flags);
  660. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  661. readl(&ha->reg->ctrl_status);
  662. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  663. }
  664. /* Wait until the firmware tells us the Soft Reset is done */
  665. max_wait_time = SOFT_RESET_TOV;
  666. do {
  667. spin_lock_irqsave(&ha->hardware_lock, flags);
  668. ctrl_status = readw(&ha->reg->ctrl_status);
  669. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  670. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  671. status = QLA_SUCCESS;
  672. break;
  673. }
  674. msleep(1000);
  675. } while ((--max_wait_time));
  676. /*
  677. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  678. * after the soft reset has taken place.
  679. */
  680. spin_lock_irqsave(&ha->hardware_lock, flags);
  681. ctrl_status = readw(&ha->reg->ctrl_status);
  682. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  683. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  684. readl(&ha->reg->ctrl_status);
  685. }
  686. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  687. /* If soft reset fails then most probably the bios on other
  688. * function is also enabled.
  689. * Since the initialization is sequential the other fn
  690. * wont be able to acknowledge the soft reset.
  691. * Issue a force soft reset to workaround this scenario.
  692. */
  693. if (max_wait_time == 0) {
  694. /* Issue Force Soft Reset */
  695. spin_lock_irqsave(&ha->hardware_lock, flags);
  696. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  697. readl(&ha->reg->ctrl_status);
  698. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  699. /* Wait until the firmware tells us the Soft Reset is done */
  700. max_wait_time = SOFT_RESET_TOV;
  701. do {
  702. spin_lock_irqsave(&ha->hardware_lock, flags);
  703. ctrl_status = readw(&ha->reg->ctrl_status);
  704. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  705. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  706. status = QLA_SUCCESS;
  707. break;
  708. }
  709. msleep(1000);
  710. } while ((--max_wait_time));
  711. }
  712. return status;
  713. }
  714. /**
  715. * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
  716. * @ha: Pointer to host adapter structure.
  717. *
  718. * This routine is called just prior to a HARD RESET to return all
  719. * outstanding commands back to the Operating System.
  720. * Caller should make sure that the following locks are released
  721. * before this calling routine: Hardware lock, and io_request_lock.
  722. **/
  723. static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
  724. {
  725. struct srb *srb;
  726. int i;
  727. unsigned long flags;
  728. spin_lock_irqsave(&ha->hardware_lock, flags);
  729. for (i = 0; i < ha->host->can_queue; i++) {
  730. srb = qla4xxx_del_from_active_array(ha, i);
  731. if (srb != NULL) {
  732. srb->cmd->result = DID_RESET << 16;
  733. qla4xxx_srb_compl(ha, srb);
  734. }
  735. }
  736. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  737. }
  738. /**
  739. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  740. * @ha: Pointer to host adapter structure.
  741. * @renew_ddb_list: Indicates what to do with the adapter's ddb list
  742. *
  743. * renew_ddb_list value can be 0=preserve ddb list, 1=destroy and rebuild
  744. * ddb list.
  745. **/
  746. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
  747. uint8_t renew_ddb_list)
  748. {
  749. int status;
  750. /* Stall incoming I/O until we are done */
  751. clear_bit(AF_ONLINE, &ha->flags);
  752. DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
  753. __func__));
  754. /* Wait for outstanding commands to complete.
  755. * Stalls the driver for max 30 secs
  756. */
  757. status = qla4xxx_cmd_wait(ha);
  758. qla4xxx_disable_intrs(ha);
  759. /* Flush any pending ddb changed AENs */
  760. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  761. /* Reset the firmware. If successful, function
  762. * returns with ISP interrupts enabled.
  763. */
  764. if (status == QLA_SUCCESS) {
  765. DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
  766. ha->host_no, __func__));
  767. qla4xxx_flush_active_srbs(ha);
  768. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
  769. status = qla4xxx_soft_reset(ha);
  770. else
  771. status = QLA_ERROR;
  772. }
  773. /* Flush any pending ddb changed AENs */
  774. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  775. /* Re-initialize firmware. If successful, function returns
  776. * with ISP interrupts enabled */
  777. if (status == QLA_SUCCESS) {
  778. DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
  779. ha->host_no, __func__));
  780. /* If successful, AF_ONLINE flag set in
  781. * qla4xxx_initialize_adapter */
  782. status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
  783. }
  784. /* Failed adapter initialization?
  785. * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
  786. if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
  787. (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
  788. /* Adapter initialization failed, see if we can retry
  789. * resetting the ha */
  790. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  791. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  792. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  793. "(%d) more times\n", ha->host_no,
  794. ha->retry_reset_ha_cnt));
  795. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  796. status = QLA_ERROR;
  797. } else {
  798. if (ha->retry_reset_ha_cnt > 0) {
  799. /* Schedule another Reset HA--DPC will retry */
  800. ha->retry_reset_ha_cnt--;
  801. DEBUG2(printk("scsi%ld: recover adapter - "
  802. "retry remaining %d\n",
  803. ha->host_no,
  804. ha->retry_reset_ha_cnt));
  805. status = QLA_ERROR;
  806. }
  807. if (ha->retry_reset_ha_cnt == 0) {
  808. /* Recover adapter retries have been exhausted.
  809. * Adapter DEAD */
  810. DEBUG2(printk("scsi%ld: recover adapter "
  811. "failed - board disabled\n",
  812. ha->host_no));
  813. qla4xxx_flush_active_srbs(ha);
  814. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  815. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  816. clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
  817. &ha->dpc_flags);
  818. status = QLA_ERROR;
  819. }
  820. }
  821. } else {
  822. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  823. clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
  824. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  825. }
  826. ha->adapter_error_count++;
  827. if (status == QLA_SUCCESS)
  828. qla4xxx_enable_intrs(ha);
  829. DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
  830. return status;
  831. }
  832. /**
  833. * qla4xxx_do_dpc - dpc routine
  834. * @data: in our case pointer to adapter structure
  835. *
  836. * This routine is a task that is schedule by the interrupt handler
  837. * to perform the background processing for interrupts. We put it
  838. * on a task queue that is consumed whenever the scheduler runs; that's
  839. * so you can do anything (i.e. put the process to sleep etc). In fact,
  840. * the mid-level tries to sleep when it reaches the driver threshold
  841. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  842. **/
  843. static void qla4xxx_do_dpc(struct work_struct *work)
  844. {
  845. struct scsi_qla_host *ha =
  846. container_of(work, struct scsi_qla_host, dpc_work);
  847. struct ddb_entry *ddb_entry, *dtemp;
  848. int status = QLA_ERROR;
  849. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  850. "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
  851. ha->host_no, __func__, ha->flags, ha->dpc_flags,
  852. readw(&ha->reg->ctrl_status)));
  853. /* Initialization not yet finished. Don't do anything yet. */
  854. if (!test_bit(AF_INIT_DONE, &ha->flags))
  855. return;
  856. if (adapter_up(ha) ||
  857. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  858. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  859. test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
  860. if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
  861. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  862. qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
  863. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  864. uint8_t wait_time = RESET_INTR_TOV;
  865. while ((readw(&ha->reg->ctrl_status) &
  866. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  867. if (--wait_time == 0)
  868. break;
  869. msleep(1000);
  870. }
  871. if (wait_time == 0)
  872. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  873. "bit not cleared-- resetting\n",
  874. ha->host_no, __func__));
  875. qla4xxx_flush_active_srbs(ha);
  876. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  877. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  878. status = qla4xxx_initialize_adapter(ha,
  879. PRESERVE_DDB_LIST);
  880. }
  881. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  882. if (status == QLA_SUCCESS)
  883. qla4xxx_enable_intrs(ha);
  884. }
  885. }
  886. /* ---- process AEN? --- */
  887. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  888. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  889. /* ---- Get DHCP IP Address? --- */
  890. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  891. qla4xxx_get_dhcp_ip_address(ha);
  892. /* ---- relogin device? --- */
  893. if (adapter_up(ha) &&
  894. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  895. list_for_each_entry_safe(ddb_entry, dtemp,
  896. &ha->ddb_list, list) {
  897. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  898. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  899. qla4xxx_relogin_device(ha, ddb_entry);
  900. /*
  901. * If mbx cmd times out there is no point
  902. * in continuing further.
  903. * With large no of targets this can hang
  904. * the system.
  905. */
  906. if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  907. printk(KERN_WARNING "scsi%ld: %s: "
  908. "need to reset hba\n",
  909. ha->host_no, __func__);
  910. break;
  911. }
  912. }
  913. }
  914. }
  915. /**
  916. * qla4xxx_free_adapter - release the adapter
  917. * @ha: pointer to adapter structure
  918. **/
  919. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  920. {
  921. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  922. /* Turn-off interrupts on the card. */
  923. qla4xxx_disable_intrs(ha);
  924. }
  925. /* Kill the kernel thread for this host */
  926. if (ha->dpc_thread)
  927. destroy_workqueue(ha->dpc_thread);
  928. /* Issue Soft Reset to put firmware in unknown state */
  929. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
  930. qla4xxx_hw_reset(ha);
  931. /* Remove timer thread, if present */
  932. if (ha->timer_active)
  933. qla4xxx_stop_timer(ha);
  934. /* Detach interrupts */
  935. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  936. free_irq(ha->pdev->irq, ha);
  937. /* free extra memory */
  938. qla4xxx_mem_free(ha);
  939. pci_disable_device(ha->pdev);
  940. }
  941. /***
  942. * qla4xxx_iospace_config - maps registers
  943. * @ha: pointer to adapter structure
  944. *
  945. * This routines maps HBA's registers from the pci address space
  946. * into the kernel virtual address space for memory mapped i/o.
  947. **/
  948. static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  949. {
  950. unsigned long pio, pio_len, pio_flags;
  951. unsigned long mmio, mmio_len, mmio_flags;
  952. pio = pci_resource_start(ha->pdev, 0);
  953. pio_len = pci_resource_len(ha->pdev, 0);
  954. pio_flags = pci_resource_flags(ha->pdev, 0);
  955. if (pio_flags & IORESOURCE_IO) {
  956. if (pio_len < MIN_IOBASE_LEN) {
  957. dev_warn(&ha->pdev->dev,
  958. "Invalid PCI I/O region size\n");
  959. pio = 0;
  960. }
  961. } else {
  962. dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
  963. pio = 0;
  964. }
  965. /* Use MMIO operations for all accesses. */
  966. mmio = pci_resource_start(ha->pdev, 1);
  967. mmio_len = pci_resource_len(ha->pdev, 1);
  968. mmio_flags = pci_resource_flags(ha->pdev, 1);
  969. if (!(mmio_flags & IORESOURCE_MEM)) {
  970. dev_err(&ha->pdev->dev,
  971. "region #0 not an MMIO resource, aborting\n");
  972. goto iospace_error_exit;
  973. }
  974. if (mmio_len < MIN_IOBASE_LEN) {
  975. dev_err(&ha->pdev->dev,
  976. "Invalid PCI mem region size, aborting\n");
  977. goto iospace_error_exit;
  978. }
  979. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  980. dev_warn(&ha->pdev->dev,
  981. "Failed to reserve PIO/MMIO regions\n");
  982. goto iospace_error_exit;
  983. }
  984. ha->pio_address = pio;
  985. ha->pio_length = pio_len;
  986. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  987. if (!ha->reg) {
  988. dev_err(&ha->pdev->dev,
  989. "cannot remap MMIO, aborting\n");
  990. goto iospace_error_exit;
  991. }
  992. return 0;
  993. iospace_error_exit:
  994. return -ENOMEM;
  995. }
  996. /**
  997. * qla4xxx_probe_adapter - callback function to probe HBA
  998. * @pdev: pointer to pci_dev structure
  999. * @pci_device_id: pointer to pci_device entry
  1000. *
  1001. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  1002. * It returns zero if successful. It also initializes all data necessary for
  1003. * the driver.
  1004. **/
  1005. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  1006. const struct pci_device_id *ent)
  1007. {
  1008. int ret = -ENODEV, status;
  1009. struct Scsi_Host *host;
  1010. struct scsi_qla_host *ha;
  1011. struct ddb_entry *ddb_entry, *ddbtemp;
  1012. uint8_t init_retry_count = 0;
  1013. char buf[34];
  1014. if (pci_enable_device(pdev))
  1015. return -1;
  1016. host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
  1017. if (host == NULL) {
  1018. printk(KERN_WARNING
  1019. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  1020. goto probe_disable_device;
  1021. }
  1022. /* Clear our data area */
  1023. ha = (struct scsi_qla_host *) host->hostdata;
  1024. memset(ha, 0, sizeof(*ha));
  1025. /* Save the information from PCI BIOS. */
  1026. ha->pdev = pdev;
  1027. ha->host = host;
  1028. ha->host_no = host->host_no;
  1029. /* Configure PCI I/O space. */
  1030. ret = qla4xxx_iospace_config(ha);
  1031. if (ret)
  1032. goto probe_failed;
  1033. dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  1034. pdev->device, pdev->irq, ha->reg);
  1035. qla4xxx_config_dma_addressing(ha);
  1036. /* Initialize lists and spinlocks. */
  1037. INIT_LIST_HEAD(&ha->ddb_list);
  1038. INIT_LIST_HEAD(&ha->free_srb_q);
  1039. mutex_init(&ha->mbox_sem);
  1040. spin_lock_init(&ha->hardware_lock);
  1041. /* Allocate dma buffers */
  1042. if (qla4xxx_mem_alloc(ha)) {
  1043. dev_warn(&ha->pdev->dev,
  1044. "[ERROR] Failed to allocate memory for adapter\n");
  1045. ret = -ENOMEM;
  1046. goto probe_failed;
  1047. }
  1048. /*
  1049. * Initialize the Host adapter request/response queues and
  1050. * firmware
  1051. * NOTE: interrupts enabled upon successful completion
  1052. */
  1053. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1054. while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
  1055. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  1056. "(%d)\n", __func__, init_retry_count));
  1057. qla4xxx_soft_reset(ha);
  1058. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1059. }
  1060. if (status == QLA_ERROR) {
  1061. dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
  1062. ret = -ENODEV;
  1063. goto probe_failed;
  1064. }
  1065. host->cmd_per_lun = 3;
  1066. host->max_channel = 0;
  1067. host->max_lun = MAX_LUNS - 1;
  1068. host->max_id = MAX_TARGETS;
  1069. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  1070. host->can_queue = MAX_SRBS ;
  1071. host->transportt = qla4xxx_scsi_transport;
  1072. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  1073. if (ret) {
  1074. dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed\n");
  1075. goto probe_failed;
  1076. }
  1077. /* Startup the kernel thread for this host adapter. */
  1078. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  1079. "qla4xxx_dpc\n", __func__));
  1080. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  1081. ha->dpc_thread = create_singlethread_workqueue(buf);
  1082. if (!ha->dpc_thread) {
  1083. dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
  1084. ret = -ENODEV;
  1085. goto probe_failed;
  1086. }
  1087. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  1088. ret = request_irq(pdev->irq, qla4xxx_intr_handler,
  1089. IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
  1090. if (ret) {
  1091. dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
  1092. " already in use.\n", pdev->irq);
  1093. goto probe_failed;
  1094. }
  1095. set_bit(AF_IRQ_ATTACHED, &ha->flags);
  1096. host->irq = pdev->irq;
  1097. DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
  1098. qla4xxx_enable_intrs(ha);
  1099. /* Start timer thread. */
  1100. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  1101. set_bit(AF_INIT_DONE, &ha->flags);
  1102. pci_set_drvdata(pdev, ha);
  1103. ret = scsi_add_host(host, &pdev->dev);
  1104. if (ret)
  1105. goto probe_failed;
  1106. /* Update transport device information for all devices. */
  1107. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  1108. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
  1109. if (qla4xxx_add_sess(ddb_entry))
  1110. goto remove_host;
  1111. }
  1112. printk(KERN_INFO
  1113. " QLogic iSCSI HBA Driver version: %s\n"
  1114. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  1115. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  1116. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  1117. ha->patch_number, ha->build_number);
  1118. scsi_scan_host(host);
  1119. return 0;
  1120. remove_host:
  1121. qla4xxx_free_ddb_list(ha);
  1122. scsi_remove_host(host);
  1123. probe_failed:
  1124. qla4xxx_free_adapter(ha);
  1125. scsi_host_put(ha->host);
  1126. probe_disable_device:
  1127. pci_disable_device(pdev);
  1128. return ret;
  1129. }
  1130. /**
  1131. * qla4xxx_remove_adapter - calback function to remove adapter.
  1132. * @pci_dev: PCI device pointer
  1133. **/
  1134. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  1135. {
  1136. struct scsi_qla_host *ha;
  1137. ha = pci_get_drvdata(pdev);
  1138. qla4xxx_disable_intrs(ha);
  1139. while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
  1140. ssleep(1);
  1141. /* remove devs from iscsi_sessions to scsi_devices */
  1142. qla4xxx_free_ddb_list(ha);
  1143. scsi_remove_host(ha->host);
  1144. qla4xxx_free_adapter(ha);
  1145. scsi_host_put(ha->host);
  1146. pci_set_drvdata(pdev, NULL);
  1147. }
  1148. /**
  1149. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  1150. * @ha: HA context
  1151. *
  1152. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  1153. * supported addressing method.
  1154. */
  1155. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  1156. {
  1157. int retval;
  1158. /* Update our PCI device dma_mask for full 64 bit mask */
  1159. if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
  1160. if (pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
  1161. dev_dbg(&ha->pdev->dev,
  1162. "Failed to set 64 bit PCI consistent mask; "
  1163. "using 32 bit.\n");
  1164. retval = pci_set_consistent_dma_mask(ha->pdev,
  1165. DMA_32BIT_MASK);
  1166. }
  1167. } else
  1168. retval = pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
  1169. }
  1170. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  1171. {
  1172. struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
  1173. struct ddb_entry *ddb = sess->dd_data;
  1174. sdev->hostdata = ddb;
  1175. sdev->tagged_supported = 1;
  1176. scsi_activate_tcq(sdev, sdev->host->can_queue);
  1177. return 0;
  1178. }
  1179. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  1180. {
  1181. sdev->tagged_supported = 1;
  1182. return 0;
  1183. }
  1184. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  1185. {
  1186. scsi_deactivate_tcq(sdev, 1);
  1187. }
  1188. /**
  1189. * qla4xxx_del_from_active_array - returns an active srb
  1190. * @ha: Pointer to host adapter structure.
  1191. * @index: index into to the active_array
  1192. *
  1193. * This routine removes and returns the srb at the specified index
  1194. **/
  1195. struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
  1196. {
  1197. struct srb *srb = NULL;
  1198. struct scsi_cmnd *cmd;
  1199. if (!(cmd = scsi_host_find_tag(ha->host, index)))
  1200. return srb;
  1201. if (!(srb = (struct srb *)cmd->host_scribble))
  1202. return srb;
  1203. /* update counters */
  1204. if (srb->flags & SRB_DMA_VALID) {
  1205. ha->req_q_count += srb->iocb_cnt;
  1206. ha->iocb_cnt -= srb->iocb_cnt;
  1207. if (srb->cmd)
  1208. srb->cmd->host_scribble = NULL;
  1209. }
  1210. return srb;
  1211. }
  1212. /**
  1213. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  1214. * @ha: actual ha whose done queue will contain the comd returned by firmware.
  1215. * @cmd: Scsi Command to wait on.
  1216. *
  1217. * This routine waits for the command to be returned by the Firmware
  1218. * for some max time.
  1219. **/
  1220. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  1221. struct scsi_cmnd *cmd)
  1222. {
  1223. int done = 0;
  1224. struct srb *rp;
  1225. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  1226. do {
  1227. /* Checking to see if its returned to OS */
  1228. rp = (struct srb *) cmd->SCp.ptr;
  1229. if (rp == NULL) {
  1230. done++;
  1231. break;
  1232. }
  1233. msleep(2000);
  1234. } while (max_wait_time--);
  1235. return done;
  1236. }
  1237. /**
  1238. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  1239. * @ha: Pointer to host adapter structure
  1240. **/
  1241. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  1242. {
  1243. unsigned long wait_online;
  1244. wait_online = jiffies + (30 * HZ);
  1245. while (time_before(jiffies, wait_online)) {
  1246. if (adapter_up(ha))
  1247. return QLA_SUCCESS;
  1248. else if (ha->retry_reset_ha_cnt == 0)
  1249. return QLA_ERROR;
  1250. msleep(2000);
  1251. }
  1252. return QLA_ERROR;
  1253. }
  1254. /**
  1255. * qla4xxx_eh_wait_for_active_target_commands - wait for active cmds to finish.
  1256. * @ha: pointer to to HBA
  1257. * @t: target id
  1258. * @l: lun id
  1259. *
  1260. * This function waits for all outstanding commands to a lun to complete. It
  1261. * returns 0 if all pending commands are returned and 1 otherwise.
  1262. **/
  1263. static int qla4xxx_eh_wait_for_active_target_commands(struct scsi_qla_host *ha,
  1264. int t, int l)
  1265. {
  1266. int cnt;
  1267. int status = 0;
  1268. struct scsi_cmnd *cmd;
  1269. /*
  1270. * Waiting for all commands for the designated target in the active
  1271. * array
  1272. */
  1273. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  1274. cmd = scsi_host_find_tag(ha->host, cnt);
  1275. if (cmd && cmd->device->id == t && cmd->device->lun == l) {
  1276. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1277. status++;
  1278. break;
  1279. }
  1280. }
  1281. }
  1282. return status;
  1283. }
  1284. /**
  1285. * qla4xxx_eh_device_reset - callback for target reset.
  1286. * @cmd: Pointer to Linux's SCSI command structure
  1287. *
  1288. * This routine is called by the Linux OS to reset all luns on the
  1289. * specified target.
  1290. **/
  1291. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  1292. {
  1293. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1294. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1295. struct srb *sp;
  1296. int ret = FAILED, stat;
  1297. sp = (struct srb *) cmd->SCp.ptr;
  1298. if (!sp || !ddb_entry)
  1299. return ret;
  1300. dev_info(&ha->pdev->dev,
  1301. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  1302. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1303. DEBUG2(printk(KERN_INFO
  1304. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  1305. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  1306. cmd, jiffies, cmd->timeout_per_command / HZ,
  1307. ha->dpc_flags, cmd->result, cmd->allowed));
  1308. /* FIXME: wait for hba to go online */
  1309. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  1310. if (stat != QLA_SUCCESS) {
  1311. dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
  1312. goto eh_dev_reset_done;
  1313. }
  1314. /* Send marker. */
  1315. ha->marker_needed = 1;
  1316. /*
  1317. * If we are coming down the EH path, wait for all commands to complete
  1318. * for the device.
  1319. */
  1320. if (cmd->device->host->shost_state == SHOST_RECOVERY) {
  1321. if (qla4xxx_eh_wait_for_active_target_commands(ha,
  1322. cmd->device->id,
  1323. cmd->device->lun)){
  1324. dev_info(&ha->pdev->dev,
  1325. "DEVICE RESET FAILED - waiting for "
  1326. "commands.\n");
  1327. goto eh_dev_reset_done;
  1328. }
  1329. }
  1330. dev_info(&ha->pdev->dev,
  1331. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  1332. ha->host_no, cmd->device->channel, cmd->device->id,
  1333. cmd->device->lun);
  1334. ret = SUCCESS;
  1335. eh_dev_reset_done:
  1336. return ret;
  1337. }
  1338. /**
  1339. * qla4xxx_eh_host_reset - kernel callback
  1340. * @cmd: Pointer to Linux's SCSI command structure
  1341. *
  1342. * This routine is invoked by the Linux kernel to perform fatal error
  1343. * recovery on the specified adapter.
  1344. **/
  1345. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  1346. {
  1347. int return_status = FAILED;
  1348. struct scsi_qla_host *ha;
  1349. ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
  1350. dev_info(&ha->pdev->dev,
  1351. "scsi(%ld:%d:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no,
  1352. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1353. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  1354. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  1355. "DEAD.\n", ha->host_no, cmd->device->channel,
  1356. __func__));
  1357. return FAILED;
  1358. }
  1359. /* make sure the dpc thread is stopped while we reset the hba */
  1360. clear_bit(AF_ONLINE, &ha->flags);
  1361. flush_workqueue(ha->dpc_thread);
  1362. if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS)
  1363. return_status = SUCCESS;
  1364. dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
  1365. return_status == FAILED ? "FAILED" : "SUCCEDED");
  1366. return return_status;
  1367. }
  1368. static struct pci_device_id qla4xxx_pci_tbl[] = {
  1369. {
  1370. .vendor = PCI_VENDOR_ID_QLOGIC,
  1371. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  1372. .subvendor = PCI_ANY_ID,
  1373. .subdevice = PCI_ANY_ID,
  1374. },
  1375. {
  1376. .vendor = PCI_VENDOR_ID_QLOGIC,
  1377. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  1378. .subvendor = PCI_ANY_ID,
  1379. .subdevice = PCI_ANY_ID,
  1380. },
  1381. {
  1382. .vendor = PCI_VENDOR_ID_QLOGIC,
  1383. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  1384. .subvendor = PCI_ANY_ID,
  1385. .subdevice = PCI_ANY_ID,
  1386. },
  1387. {0, 0},
  1388. };
  1389. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  1390. static struct pci_driver qla4xxx_pci_driver = {
  1391. .name = DRIVER_NAME,
  1392. .id_table = qla4xxx_pci_tbl,
  1393. .probe = qla4xxx_probe_adapter,
  1394. .remove = qla4xxx_remove_adapter,
  1395. };
  1396. static int __init qla4xxx_module_init(void)
  1397. {
  1398. int ret;
  1399. /* Allocate cache for SRBs. */
  1400. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  1401. SLAB_HWCACHE_ALIGN, NULL);
  1402. if (srb_cachep == NULL) {
  1403. printk(KERN_ERR
  1404. "%s: Unable to allocate SRB cache..."
  1405. "Failing load!\n", DRIVER_NAME);
  1406. ret = -ENOMEM;
  1407. goto no_srp_cache;
  1408. }
  1409. /* Derive version string. */
  1410. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  1411. if (ql4xextended_error_logging)
  1412. strcat(qla4xxx_version_str, "-debug");
  1413. qla4xxx_scsi_transport =
  1414. iscsi_register_transport(&qla4xxx_iscsi_transport);
  1415. if (!qla4xxx_scsi_transport){
  1416. ret = -ENODEV;
  1417. goto release_srb_cache;
  1418. }
  1419. ret = pci_register_driver(&qla4xxx_pci_driver);
  1420. if (ret)
  1421. goto unregister_transport;
  1422. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  1423. return 0;
  1424. unregister_transport:
  1425. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  1426. release_srb_cache:
  1427. kmem_cache_destroy(srb_cachep);
  1428. no_srp_cache:
  1429. return ret;
  1430. }
  1431. static void __exit qla4xxx_module_exit(void)
  1432. {
  1433. ql4_mod_unload = 1;
  1434. pci_unregister_driver(&qla4xxx_pci_driver);
  1435. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  1436. kmem_cache_destroy(srb_cachep);
  1437. }
  1438. module_init(qla4xxx_module_init);
  1439. module_exit(qla4xxx_module_exit);
  1440. MODULE_AUTHOR("QLogic Corporation");
  1441. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  1442. MODULE_LICENSE("GPL");
  1443. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);