ql4_os.c 45 KB

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