ql4_os.c 45 KB

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