ql4_os.c 45 KB

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