ql4_os.c 45 KB

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