ql4_os.c 45 KB

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