ql4_os.c 45 KB

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