ql4_init.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <scsi/iscsi_if.h>
  8. #include "ql4_def.h"
  9. #include "ql4_glbl.h"
  10. #include "ql4_dbg.h"
  11. #include "ql4_inline.h"
  12. static struct ddb_entry *qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
  13. uint32_t fw_ddb_index);
  14. static void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
  15. {
  16. uint32_t value;
  17. uint8_t func_number;
  18. unsigned long flags;
  19. /* Get the function number */
  20. spin_lock_irqsave(&ha->hardware_lock, flags);
  21. value = readw(&ha->reg->ctrl_status);
  22. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  23. func_number = (uint8_t) ((value >> 4) & 0x30);
  24. switch (value & ISP_CONTROL_FN_MASK) {
  25. case ISP_CONTROL_FN0_SCSI:
  26. ha->mac_index = 1;
  27. break;
  28. case ISP_CONTROL_FN1_SCSI:
  29. ha->mac_index = 3;
  30. break;
  31. default:
  32. DEBUG2(printk("scsi%ld: %s: Invalid function number, "
  33. "ispControlStatus = 0x%x\n", ha->host_no,
  34. __func__, value));
  35. break;
  36. }
  37. DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
  38. ha->mac_index));
  39. }
  40. /**
  41. * qla4xxx_free_ddb - deallocate ddb
  42. * @ha: pointer to host adapter structure.
  43. * @ddb_entry: pointer to device database entry
  44. *
  45. * This routine deallocates and unlinks the specified ddb_entry from the
  46. * adapter's
  47. **/
  48. void qla4xxx_free_ddb(struct scsi_qla_host *ha,
  49. struct ddb_entry *ddb_entry)
  50. {
  51. /* Remove device entry from list */
  52. list_del_init(&ddb_entry->list);
  53. /* Remove device pointer from index mapping arrays */
  54. ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
  55. (struct ddb_entry *) INVALID_ENTRY;
  56. ha->tot_ddbs--;
  57. /* Free memory and scsi-ml struct for device entry */
  58. qla4xxx_destroy_sess(ddb_entry);
  59. }
  60. /**
  61. * qla4xxx_free_ddb_list - deallocate all ddbs
  62. * @ha: pointer to host adapter structure.
  63. *
  64. * This routine deallocates and removes all devices on the sppecified adapter.
  65. **/
  66. void qla4xxx_free_ddb_list(struct scsi_qla_host *ha)
  67. {
  68. struct list_head *ptr;
  69. struct ddb_entry *ddb_entry;
  70. while (!list_empty(&ha->ddb_list)) {
  71. ptr = ha->ddb_list.next;
  72. /* Free memory for device entry and remove */
  73. ddb_entry = list_entry(ptr, struct ddb_entry, list);
  74. qla4xxx_free_ddb(ha, ddb_entry);
  75. }
  76. }
  77. /**
  78. * qla4xxx_init_response_q_entries() - Initializes response queue entries.
  79. * @ha: HA context
  80. *
  81. * Beginning of request ring has initialization control block already built
  82. * by nvram config routine.
  83. **/
  84. static void qla4xxx_init_response_q_entries(struct scsi_qla_host *ha)
  85. {
  86. uint16_t cnt;
  87. struct response *pkt;
  88. pkt = (struct response *)ha->response_ptr;
  89. for (cnt = 0; cnt < RESPONSE_QUEUE_DEPTH; cnt++) {
  90. pkt->signature = RESPONSE_PROCESSED;
  91. pkt++;
  92. }
  93. }
  94. /**
  95. * qla4xxx_init_rings - initialize hw queues
  96. * @ha: pointer to host adapter structure.
  97. *
  98. * This routine initializes the internal queues for the specified adapter.
  99. * The QLA4010 requires us to restart the queues at index 0.
  100. * The QLA4000 doesn't care, so just default to QLA4010's requirement.
  101. **/
  102. int qla4xxx_init_rings(struct scsi_qla_host *ha)
  103. {
  104. unsigned long flags = 0;
  105. /* Initialize request queue. */
  106. spin_lock_irqsave(&ha->hardware_lock, flags);
  107. ha->request_out = 0;
  108. ha->request_in = 0;
  109. ha->request_ptr = &ha->request_ring[ha->request_in];
  110. ha->req_q_count = REQUEST_QUEUE_DEPTH;
  111. /* Initialize response queue. */
  112. ha->response_in = 0;
  113. ha->response_out = 0;
  114. ha->response_ptr = &ha->response_ring[ha->response_out];
  115. if (is_qla8022(ha)) {
  116. writel(0,
  117. (unsigned long __iomem *)&ha->qla4_8xxx_reg->req_q_out);
  118. writel(0,
  119. (unsigned long __iomem *)&ha->qla4_8xxx_reg->rsp_q_in);
  120. writel(0,
  121. (unsigned long __iomem *)&ha->qla4_8xxx_reg->rsp_q_out);
  122. } else {
  123. /*
  124. * Initialize DMA Shadow registers. The firmware is really
  125. * supposed to take care of this, but on some uniprocessor
  126. * systems, the shadow registers aren't cleared-- causing
  127. * the interrupt_handler to think there are responses to be
  128. * processed when there aren't.
  129. */
  130. ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
  131. ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
  132. wmb();
  133. writel(0, &ha->reg->req_q_in);
  134. writel(0, &ha->reg->rsp_q_out);
  135. readl(&ha->reg->rsp_q_out);
  136. }
  137. qla4xxx_init_response_q_entries(ha);
  138. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  139. return QLA_SUCCESS;
  140. }
  141. /**
  142. * qla4xxx_get_sys_info - validate adapter MAC address(es)
  143. * @ha: pointer to host adapter structure.
  144. *
  145. **/
  146. int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
  147. {
  148. struct flash_sys_info *sys_info;
  149. dma_addr_t sys_info_dma;
  150. int status = QLA_ERROR;
  151. sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
  152. &sys_info_dma, GFP_KERNEL);
  153. if (sys_info == NULL) {
  154. DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
  155. ha->host_no, __func__));
  156. goto exit_get_sys_info_no_free;
  157. }
  158. memset(sys_info, 0, sizeof(*sys_info));
  159. /* Get flash sys info */
  160. if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
  161. sizeof(*sys_info)) != QLA_SUCCESS) {
  162. DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
  163. "failed\n", ha->host_no, __func__));
  164. goto exit_get_sys_info;
  165. }
  166. /* Save M.A.C. address & serial_number */
  167. memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
  168. min(sizeof(ha->my_mac),
  169. sizeof(sys_info->physAddr[0].address)));
  170. memcpy(ha->serial_number, &sys_info->acSerialNumber,
  171. min(sizeof(ha->serial_number),
  172. sizeof(sys_info->acSerialNumber)));
  173. status = QLA_SUCCESS;
  174. exit_get_sys_info:
  175. dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
  176. sys_info_dma);
  177. exit_get_sys_info_no_free:
  178. return status;
  179. }
  180. /**
  181. * qla4xxx_init_local_data - initialize adapter specific local data
  182. * @ha: pointer to host adapter structure.
  183. *
  184. **/
  185. static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
  186. {
  187. /* Initialize aen queue */
  188. ha->aen_q_count = MAX_AEN_ENTRIES;
  189. return qla4xxx_get_firmware_status(ha);
  190. }
  191. static uint8_t
  192. qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
  193. {
  194. uint8_t ipv4_wait = 0;
  195. uint8_t ipv6_wait = 0;
  196. int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
  197. /* If both IPv4 & IPv6 are enabled, possibly only one
  198. * IP address may be acquired, so check to see if we
  199. * need to wait for another */
  200. if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
  201. if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
  202. ((ha->addl_fw_state &
  203. FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
  204. ipv4_wait = 1;
  205. }
  206. if (((ha->ipv6_addl_options &
  207. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
  208. ((ha->ipv6_link_local_state == IP_ADDRSTATE_ACQUIRING) ||
  209. (ha->ipv6_addr0_state == IP_ADDRSTATE_ACQUIRING) ||
  210. (ha->ipv6_addr1_state == IP_ADDRSTATE_ACQUIRING))) {
  211. ipv6_wait = 1;
  212. if ((ha->ipv6_link_local_state ==
  213. IP_ADDRSTATE_PREFERRED) ||
  214. (ha->ipv6_addr0_state == IP_ADDRSTATE_PREFERRED) ||
  215. (ha->ipv6_addr1_state == IP_ADDRSTATE_PREFERRED)) {
  216. DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
  217. "Preferred IP configured."
  218. " Don't wait!\n", ha->host_no,
  219. __func__));
  220. ipv6_wait = 0;
  221. }
  222. if (memcmp(&ha->ipv6_default_router_addr, ip_address,
  223. IPv6_ADDR_LEN) == 0) {
  224. DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
  225. "No Router configured. "
  226. "Don't wait!\n", ha->host_no,
  227. __func__));
  228. ipv6_wait = 0;
  229. }
  230. if ((ha->ipv6_default_router_state ==
  231. IPV6_RTRSTATE_MANUAL) &&
  232. (ha->ipv6_link_local_state ==
  233. IP_ADDRSTATE_TENTATIVE) &&
  234. (memcmp(&ha->ipv6_link_local_addr,
  235. &ha->ipv6_default_router_addr, 4) == 0)) {
  236. DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
  237. "IP configured. Don't wait!\n",
  238. ha->host_no, __func__));
  239. ipv6_wait = 0;
  240. }
  241. }
  242. if (ipv4_wait || ipv6_wait) {
  243. DEBUG2(printk("scsi%ld: %s: Wait for additional "
  244. "IP(s) \"", ha->host_no, __func__));
  245. if (ipv4_wait)
  246. DEBUG2(printk("IPv4 "));
  247. if (ha->ipv6_link_local_state == IP_ADDRSTATE_ACQUIRING)
  248. DEBUG2(printk("IPv6LinkLocal "));
  249. if (ha->ipv6_addr0_state == IP_ADDRSTATE_ACQUIRING)
  250. DEBUG2(printk("IPv6Addr0 "));
  251. if (ha->ipv6_addr1_state == IP_ADDRSTATE_ACQUIRING)
  252. DEBUG2(printk("IPv6Addr1 "));
  253. DEBUG2(printk("\"\n"));
  254. }
  255. }
  256. return ipv4_wait|ipv6_wait;
  257. }
  258. static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
  259. {
  260. uint32_t timeout_count;
  261. int ready = 0;
  262. DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
  263. for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
  264. timeout_count--) {
  265. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  266. qla4xxx_get_dhcp_ip_address(ha);
  267. /* Get firmware state. */
  268. if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
  269. DEBUG2(printk("scsi%ld: %s: unable to get firmware "
  270. "state\n", ha->host_no, __func__));
  271. break;
  272. }
  273. if (ha->firmware_state & FW_STATE_ERROR) {
  274. DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
  275. " occurred\n", ha->host_no, __func__));
  276. break;
  277. }
  278. if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
  279. /*
  280. * The firmware has not yet been issued an Initialize
  281. * Firmware command, so issue it now.
  282. */
  283. if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
  284. break;
  285. /* Go back and test for ready state - no wait. */
  286. continue;
  287. }
  288. if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
  289. DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
  290. "AUTOCONNECT in progress\n",
  291. ha->host_no, __func__));
  292. }
  293. if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
  294. DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
  295. " CONFIGURING IP\n",
  296. ha->host_no, __func__));
  297. /*
  298. * Check for link state after 15 secs and if link is
  299. * still DOWN then, cable is unplugged. Ignore "DHCP
  300. * in Progress/CONFIGURING IP" bit to check if firmware
  301. * is in ready state or not after 15 secs.
  302. * This is applicable for both 2.x & 3.x firmware
  303. */
  304. if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
  305. if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
  306. DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
  307. " LINK UP (Cable plugged)\n",
  308. ha->host_no, __func__));
  309. } else if (ha->firmware_state &
  310. (FW_STATE_CONFIGURING_IP |
  311. FW_STATE_READY)) {
  312. DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
  313. "LINK DOWN (Cable unplugged)\n",
  314. ha->host_no, __func__));
  315. ha->firmware_state = FW_STATE_READY;
  316. }
  317. }
  318. }
  319. if (ha->firmware_state == FW_STATE_READY) {
  320. /* If DHCP IP Addr is available, retrieve it now. */
  321. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
  322. &ha->dpc_flags))
  323. qla4xxx_get_dhcp_ip_address(ha);
  324. if (!qla4xxx_wait_for_ip_config(ha) ||
  325. timeout_count == 1) {
  326. DEBUG2(ql4_printk(KERN_INFO, ha,
  327. "Firmware Ready..\n"));
  328. /* The firmware is ready to process SCSI
  329. commands. */
  330. DEBUG2(ql4_printk(KERN_INFO, ha,
  331. "scsi%ld: %s: MEDIA TYPE"
  332. " - %s\n", ha->host_no,
  333. __func__, (ha->addl_fw_state &
  334. FW_ADDSTATE_OPTICAL_MEDIA)
  335. != 0 ? "OPTICAL" : "COPPER"));
  336. DEBUG2(ql4_printk(KERN_INFO, ha,
  337. "scsi%ld: %s: DHCPv4 STATE"
  338. " Enabled %s\n", ha->host_no,
  339. __func__, (ha->addl_fw_state &
  340. FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
  341. "YES" : "NO"));
  342. DEBUG2(ql4_printk(KERN_INFO, ha,
  343. "scsi%ld: %s: LINK %s\n",
  344. ha->host_no, __func__,
  345. (ha->addl_fw_state &
  346. FW_ADDSTATE_LINK_UP) != 0 ?
  347. "UP" : "DOWN"));
  348. DEBUG2(ql4_printk(KERN_INFO, ha,
  349. "scsi%ld: %s: iSNS Service "
  350. "Started %s\n",
  351. ha->host_no, __func__,
  352. (ha->addl_fw_state &
  353. FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
  354. "YES" : "NO"));
  355. ready = 1;
  356. break;
  357. }
  358. }
  359. DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
  360. "seconds expired= %d\n", ha->host_no, __func__,
  361. ha->firmware_state, ha->addl_fw_state,
  362. timeout_count));
  363. if (is_qla4032(ha) &&
  364. !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
  365. (timeout_count < ADAPTER_INIT_TOV - 5)) {
  366. break;
  367. }
  368. msleep(1000);
  369. } /* end of for */
  370. if (timeout_count <= 0)
  371. DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
  372. ha->host_no, __func__));
  373. if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
  374. DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
  375. "it's waiting to configure an IP address\n",
  376. ha->host_no, __func__));
  377. ready = 1;
  378. } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
  379. DEBUG2(printk("scsi%ld: %s: FW initialized, but "
  380. "auto-discovery still in process\n",
  381. ha->host_no, __func__));
  382. ready = 1;
  383. }
  384. return ready;
  385. }
  386. /**
  387. * qla4xxx_init_firmware - initializes the firmware.
  388. * @ha: pointer to host adapter structure.
  389. *
  390. **/
  391. static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
  392. {
  393. int status = QLA_ERROR;
  394. if (is_aer_supported(ha) &&
  395. test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  396. return status;
  397. /* For 82xx, stop firmware before initializing because if BIOS
  398. * has previously initialized firmware, then driver's initialize
  399. * firmware will fail. */
  400. if (is_qla8022(ha))
  401. qla4_8xxx_stop_firmware(ha);
  402. ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
  403. if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
  404. DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
  405. "control block\n", ha->host_no, __func__));
  406. return status;
  407. }
  408. if (!qla4xxx_fw_ready(ha))
  409. return status;
  410. return qla4xxx_get_firmware_status(ha);
  411. }
  412. static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
  413. uint32_t fw_ddb_index,
  414. uint32_t *new_tgt)
  415. {
  416. struct dev_db_entry *fw_ddb_entry = NULL;
  417. dma_addr_t fw_ddb_entry_dma;
  418. struct ddb_entry *ddb_entry = NULL;
  419. int found = 0;
  420. uint32_t device_state;
  421. *new_tgt = 0;
  422. /* Make sure the dma buffer is valid */
  423. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
  424. sizeof(*fw_ddb_entry),
  425. &fw_ddb_entry_dma, GFP_KERNEL);
  426. if (fw_ddb_entry == NULL) {
  427. DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
  428. ha->host_no, __func__));
  429. goto exit_get_ddb_entry_no_free;
  430. }
  431. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
  432. fw_ddb_entry_dma, NULL, NULL,
  433. &device_state, NULL, NULL, NULL) ==
  434. QLA_ERROR) {
  435. DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
  436. "fw_ddb_index %d\n", ha->host_no, __func__,
  437. fw_ddb_index));
  438. goto exit_get_ddb_entry;
  439. }
  440. /* Allocate DDB if not already allocated. */
  441. DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
  442. __func__, fw_ddb_index));
  443. list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
  444. if ((memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
  445. ISCSI_NAME_SIZE) == 0) &&
  446. (ddb_entry->tpgt ==
  447. le32_to_cpu(fw_ddb_entry->tgt_portal_grp)) &&
  448. (memcmp(ddb_entry->isid, fw_ddb_entry->isid,
  449. sizeof(ddb_entry->isid)) == 0)) {
  450. found++;
  451. break;
  452. }
  453. }
  454. /* if not found allocate new ddb */
  455. if (!found) {
  456. DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
  457. "new ddb\n", ha->host_no, __func__,
  458. fw_ddb_index));
  459. *new_tgt = 1;
  460. ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
  461. }
  462. exit_get_ddb_entry:
  463. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
  464. fw_ddb_entry_dma);
  465. exit_get_ddb_entry_no_free:
  466. return ddb_entry;
  467. }
  468. /**
  469. * qla4xxx_update_ddb_entry - update driver's internal ddb
  470. * @ha: pointer to host adapter structure.
  471. * @ddb_entry: pointer to device database structure to be filled
  472. * @fw_ddb_index: index of the ddb entry in fw ddb table
  473. *
  474. * This routine updates the driver's internal device database entry
  475. * with information retrieved from the firmware's device database
  476. * entry for the specified device. The ddb_entry->fw_ddb_index field
  477. * must be initialized prior to calling this routine
  478. *
  479. **/
  480. static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
  481. struct ddb_entry *ddb_entry,
  482. uint32_t fw_ddb_index)
  483. {
  484. struct dev_db_entry *fw_ddb_entry = NULL;
  485. dma_addr_t fw_ddb_entry_dma;
  486. int status = QLA_ERROR;
  487. uint32_t conn_err;
  488. if (ddb_entry == NULL) {
  489. DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
  490. __func__));
  491. goto exit_update_ddb_no_free;
  492. }
  493. /* Make sure the dma buffer is valid */
  494. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
  495. sizeof(*fw_ddb_entry),
  496. &fw_ddb_entry_dma, GFP_KERNEL);
  497. if (fw_ddb_entry == NULL) {
  498. DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
  499. ha->host_no, __func__));
  500. goto exit_update_ddb_no_free;
  501. }
  502. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
  503. fw_ddb_entry_dma, NULL, NULL,
  504. &ddb_entry->fw_ddb_device_state, &conn_err,
  505. &ddb_entry->tcp_source_port_num,
  506. &ddb_entry->connection_id) ==
  507. QLA_ERROR) {
  508. DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
  509. "fw_ddb_index %d\n", ha->host_no, __func__,
  510. fw_ddb_index));
  511. goto exit_update_ddb;
  512. }
  513. status = QLA_SUCCESS;
  514. ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
  515. ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
  516. ddb_entry->task_mgmt_timeout =
  517. le16_to_cpu(fw_ddb_entry->def_timeout);
  518. ddb_entry->CmdSn = 0;
  519. ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
  520. ddb_entry->default_relogin_timeout =
  521. le16_to_cpu(fw_ddb_entry->def_timeout);
  522. ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  523. /* Update index in case it changed */
  524. ddb_entry->fw_ddb_index = fw_ddb_index;
  525. ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
  526. ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
  527. ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  528. memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
  529. memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
  530. min(sizeof(ddb_entry->iscsi_name),
  531. sizeof(fw_ddb_entry->iscsi_name)));
  532. memcpy(&ddb_entry->iscsi_alias[0], &fw_ddb_entry->iscsi_alias[0],
  533. min(sizeof(ddb_entry->iscsi_alias),
  534. sizeof(fw_ddb_entry->iscsi_alias)));
  535. memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
  536. min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
  537. ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
  538. ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
  539. ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
  540. ddb_entry->iscsi_max_rcv_data_seg_len =
  541. fw_ddb_entry->iscsi_max_rcv_data_seg_len;
  542. ddb_entry->iscsi_max_snd_data_seg_len =
  543. fw_ddb_entry->iscsi_max_snd_data_seg_len;
  544. if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
  545. memcpy(&ddb_entry->remote_ipv6_addr,
  546. fw_ddb_entry->ip_addr,
  547. min(sizeof(ddb_entry->remote_ipv6_addr),
  548. sizeof(fw_ddb_entry->ip_addr)));
  549. memcpy(&ddb_entry->link_local_ipv6_addr,
  550. fw_ddb_entry->link_local_ipv6_addr,
  551. min(sizeof(ddb_entry->link_local_ipv6_addr),
  552. sizeof(fw_ddb_entry->link_local_ipv6_addr)));
  553. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
  554. " ConnErr %08x IP %pI6 "
  555. ":%04d \"%s\"\n",
  556. __func__, fw_ddb_index,
  557. ddb_entry->fw_ddb_device_state,
  558. conn_err, fw_ddb_entry->ip_addr,
  559. le16_to_cpu(fw_ddb_entry->port),
  560. fw_ddb_entry->iscsi_name));
  561. } else
  562. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
  563. " ConnErr %08x IP %pI4 "
  564. ":%04d \"%s\"\n",
  565. __func__, fw_ddb_index,
  566. ddb_entry->fw_ddb_device_state,
  567. conn_err, fw_ddb_entry->ip_addr,
  568. le16_to_cpu(fw_ddb_entry->port),
  569. fw_ddb_entry->iscsi_name));
  570. exit_update_ddb:
  571. if (fw_ddb_entry)
  572. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  573. fw_ddb_entry, fw_ddb_entry_dma);
  574. exit_update_ddb_no_free:
  575. return status;
  576. }
  577. /**
  578. * qla4xxx_alloc_ddb - allocate device database entry
  579. * @ha: Pointer to host adapter structure.
  580. * @fw_ddb_index: Firmware's device database index
  581. *
  582. * This routine allocates a ddb_entry, ititializes some values, and
  583. * inserts it into the ddb list.
  584. **/
  585. static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
  586. uint32_t fw_ddb_index)
  587. {
  588. struct ddb_entry *ddb_entry;
  589. DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
  590. __func__, fw_ddb_index));
  591. ddb_entry = qla4xxx_alloc_sess(ha);
  592. if (ddb_entry == NULL) {
  593. DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
  594. "to add fw_ddb_index [%d]\n",
  595. ha->host_no, __func__, fw_ddb_index));
  596. return ddb_entry;
  597. }
  598. ddb_entry->fw_ddb_index = fw_ddb_index;
  599. atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
  600. atomic_set(&ddb_entry->relogin_timer, 0);
  601. atomic_set(&ddb_entry->relogin_retry_count, 0);
  602. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  603. list_add_tail(&ddb_entry->list, &ha->ddb_list);
  604. ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
  605. ha->tot_ddbs++;
  606. return ddb_entry;
  607. }
  608. /**
  609. * qla4_is_relogin_allowed - Are we allowed to login?
  610. * @ha: Pointer to host adapter structure.
  611. * @conn_err: Last connection error associated with the ddb
  612. *
  613. * This routine tests the given connection error to determine if
  614. * we are allowed to login.
  615. **/
  616. int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
  617. {
  618. uint32_t err_code, login_rsp_sts_class;
  619. int relogin = 1;
  620. err_code = ((conn_err & 0x00ff0000) >> 16);
  621. login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
  622. if (err_code == 0x1c || err_code == 0x06) {
  623. DEBUG2(ql4_printk(KERN_INFO, ha,
  624. ": conn_err=0x%08x, send target completed"
  625. " or access denied failure\n", conn_err));
  626. relogin = 0;
  627. }
  628. if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
  629. /* Login Response PDU returned an error.
  630. Login Response Status in Error Code Detail
  631. indicates login should not be retried.*/
  632. DEBUG2(ql4_printk(KERN_INFO, ha,
  633. ": conn_err=0x%08x, do not retry relogin\n",
  634. conn_err));
  635. relogin = 0;
  636. }
  637. return relogin;
  638. }
  639. /**
  640. * qla4xxx_configure_ddbs - builds driver ddb list
  641. * @ha: Pointer to host adapter structure.
  642. *
  643. * This routine searches for all valid firmware ddb entries and builds
  644. * an internal ddb list. Ddbs that are considered valid are those with
  645. * a device state of SESSION_ACTIVE.
  646. **/
  647. static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
  648. {
  649. int status = QLA_ERROR;
  650. uint32_t fw_ddb_index = 0;
  651. uint32_t next_fw_ddb_index = 0;
  652. uint32_t ddb_state;
  653. uint32_t conn_err;
  654. struct ddb_entry *ddb_entry;
  655. struct dev_db_entry *fw_ddb_entry = NULL;
  656. dma_addr_t fw_ddb_entry_dma;
  657. uint32_t ipv6_device;
  658. uint32_t new_tgt;
  659. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  660. &fw_ddb_entry_dma, GFP_KERNEL);
  661. if (fw_ddb_entry == NULL) {
  662. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DMA alloc failed\n",
  663. __func__));
  664. goto exit_build_ddb_list_no_free;
  665. }
  666. ql4_printk(KERN_INFO, ha, "Initializing DDBs ...\n");
  667. for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
  668. fw_ddb_index = next_fw_ddb_index) {
  669. /* First, let's see if a device exists here */
  670. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
  671. 0, NULL, &next_fw_ddb_index,
  672. &ddb_state, &conn_err,
  673. NULL, NULL) ==
  674. QLA_ERROR) {
  675. DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
  676. "fw_ddb_index %d failed", ha->host_no,
  677. __func__, fw_ddb_index));
  678. goto exit_build_ddb_list;
  679. }
  680. DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
  681. "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
  682. fw_ddb_index, ddb_state, next_fw_ddb_index));
  683. /* Issue relogin, if necessary. */
  684. if (ddb_state == DDB_DS_SESSION_FAILED ||
  685. ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
  686. /* Try and login to device */
  687. DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
  688. ha->host_no, __func__, fw_ddb_index));
  689. ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
  690. DDB_OPT_IPV6_DEVICE;
  691. if (qla4_is_relogin_allowed(ha, conn_err) &&
  692. ((!ipv6_device &&
  693. *((uint32_t *)fw_ddb_entry->ip_addr))
  694. || ipv6_device)) {
  695. qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
  696. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
  697. NULL, 0, NULL,
  698. &next_fw_ddb_index,
  699. &ddb_state, &conn_err,
  700. NULL, NULL)
  701. == QLA_ERROR) {
  702. DEBUG2(printk("scsi%ld: %s:"
  703. "get_ddb_entry %d failed\n",
  704. ha->host_no,
  705. __func__, fw_ddb_index));
  706. goto exit_build_ddb_list;
  707. }
  708. }
  709. }
  710. if (ddb_state != DDB_DS_SESSION_ACTIVE)
  711. goto next_one;
  712. /*
  713. * if fw_ddb with session active state found,
  714. * add to ddb_list
  715. */
  716. DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
  717. ha->host_no, __func__, fw_ddb_index));
  718. /* Add DDB to internal our ddb list. */
  719. ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
  720. if (ddb_entry == NULL) {
  721. DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
  722. "for device at fw_ddb_index %d\n",
  723. ha->host_no, __func__, fw_ddb_index));
  724. goto exit_build_ddb_list;
  725. }
  726. /* Fill in the device structure */
  727. if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
  728. QLA_ERROR) {
  729. ha->fw_ddb_index_map[fw_ddb_index] =
  730. (struct ddb_entry *)INVALID_ENTRY;
  731. DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
  732. "for fw_ddb_index %d.\n",
  733. ha->host_no, __func__, fw_ddb_index));
  734. goto exit_build_ddb_list;
  735. }
  736. next_one:
  737. /* We know we've reached the last device when
  738. * next_fw_ddb_index is 0 */
  739. if (next_fw_ddb_index == 0)
  740. break;
  741. }
  742. status = QLA_SUCCESS;
  743. ql4_printk(KERN_INFO, ha, "DDB list done..\n");
  744. exit_build_ddb_list:
  745. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
  746. fw_ddb_entry_dma);
  747. exit_build_ddb_list_no_free:
  748. return status;
  749. }
  750. struct qla4_relog_scan {
  751. int halt_wait;
  752. uint32_t conn_err;
  753. uint32_t fw_ddb_index;
  754. uint32_t next_fw_ddb_index;
  755. uint32_t fw_ddb_device_state;
  756. };
  757. static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
  758. {
  759. struct ddb_entry *ddb_entry;
  760. if (qla4_is_relogin_allowed(ha, rs->conn_err)) {
  761. /* We either have a device that is in
  762. * the process of relogging in or a
  763. * device that is waiting to be
  764. * relogged in */
  765. rs->halt_wait = 0;
  766. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
  767. rs->fw_ddb_index);
  768. if (ddb_entry == NULL)
  769. return QLA_ERROR;
  770. if (ddb_entry->dev_scan_wait_to_start_relogin != 0
  771. && time_after_eq(jiffies,
  772. ddb_entry->
  773. dev_scan_wait_to_start_relogin))
  774. {
  775. ddb_entry->dev_scan_wait_to_start_relogin = 0;
  776. qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
  777. }
  778. }
  779. return QLA_SUCCESS;
  780. }
  781. static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
  782. struct qla4_relog_scan *rs)
  783. {
  784. int error;
  785. /* scan for relogins
  786. * ----------------- */
  787. for (rs->fw_ddb_index = 0; rs->fw_ddb_index < MAX_DDB_ENTRIES;
  788. rs->fw_ddb_index = rs->next_fw_ddb_index) {
  789. if (qla4xxx_get_fwddb_entry(ha, rs->fw_ddb_index, NULL, 0,
  790. NULL, &rs->next_fw_ddb_index,
  791. &rs->fw_ddb_device_state,
  792. &rs->conn_err, NULL, NULL)
  793. == QLA_ERROR)
  794. return QLA_ERROR;
  795. if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
  796. rs->halt_wait = 0;
  797. if (rs->fw_ddb_device_state == DDB_DS_SESSION_FAILED ||
  798. rs->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE) {
  799. error = qla4_test_rdy(ha, rs);
  800. if (error)
  801. return error;
  802. }
  803. /* We know we've reached the last device when
  804. * next_fw_ddb_index is 0 */
  805. if (rs->next_fw_ddb_index == 0)
  806. break;
  807. }
  808. return QLA_SUCCESS;
  809. }
  810. /**
  811. * qla4xxx_devices_ready - wait for target devices to be logged in
  812. * @ha: pointer to adapter structure
  813. *
  814. * This routine waits up to ql4xdiscoverywait seconds
  815. * F/W database during driver load time.
  816. **/
  817. static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
  818. {
  819. int error;
  820. unsigned long discovery_wtime;
  821. struct qla4_relog_scan rs;
  822. discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
  823. DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
  824. do {
  825. /* poll for AEN. */
  826. qla4xxx_get_firmware_state(ha);
  827. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags)) {
  828. /* Set time-between-relogin timer */
  829. qla4xxx_process_aen(ha, RELOGIN_DDB_CHANGED_AENS);
  830. }
  831. /* if no relogins active or needed, halt discvery wait */
  832. rs.halt_wait = 1;
  833. error = qla4_scan_for_relogin(ha, &rs);
  834. if (rs.halt_wait) {
  835. DEBUG2(printk("scsi%ld: %s: Delay halted. Devices "
  836. "Ready.\n", ha->host_no, __func__));
  837. return QLA_SUCCESS;
  838. }
  839. msleep(2000);
  840. } while (!time_after_eq(jiffies, discovery_wtime));
  841. DEBUG3(qla4xxx_get_conn_event_log(ha));
  842. return QLA_SUCCESS;
  843. }
  844. static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
  845. {
  846. unsigned long wtime;
  847. /* Flush the 0x8014 AEN from the firmware as a result of
  848. * Auto connect. We are basically doing get_firmware_ddb()
  849. * to determine whether we need to log back in or not.
  850. * Trying to do a set ddb before we have processed 0x8014
  851. * will result in another set_ddb() for the same ddb. In other
  852. * words there will be stale entries in the aen_q.
  853. */
  854. wtime = jiffies + (2 * HZ);
  855. do {
  856. if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
  857. if (ha->firmware_state & (BIT_2 | BIT_0))
  858. return;
  859. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  860. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  861. msleep(1000);
  862. } while (!time_after_eq(jiffies, wtime));
  863. }
  864. static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
  865. {
  866. uint16_t fw_ddb_index;
  867. int status = QLA_SUCCESS;
  868. /* free the ddb list if is not empty */
  869. if (!list_empty(&ha->ddb_list))
  870. qla4xxx_free_ddb_list(ha);
  871. for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
  872. ha->fw_ddb_index_map[fw_ddb_index] =
  873. (struct ddb_entry *)INVALID_ENTRY;
  874. ha->tot_ddbs = 0;
  875. qla4xxx_flush_AENS(ha);
  876. /* Wait for an AEN */
  877. qla4xxx_devices_ready(ha);
  878. /*
  879. * First perform device discovery for active
  880. * fw ddb indexes and build
  881. * ddb list.
  882. */
  883. if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
  884. return status;
  885. /*
  886. * Targets can come online after the inital discovery, so processing
  887. * the aens here will catch them.
  888. */
  889. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  890. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  891. return status;
  892. }
  893. /**
  894. * qla4xxx_reinitialize_ddb_list - update the driver ddb list
  895. * @ha: pointer to host adapter structure.
  896. *
  897. * This routine obtains device information from the F/W database after
  898. * firmware or adapter resets. The device table is preserved.
  899. **/
  900. int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
  901. {
  902. int status = QLA_SUCCESS;
  903. struct ddb_entry *ddb_entry, *detemp;
  904. /* Update the device information for all devices. */
  905. list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
  906. qla4xxx_update_ddb_entry(ha, ddb_entry,
  907. ddb_entry->fw_ddb_index);
  908. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  909. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  910. DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
  911. "ONLINE\n", ha->host_no, __func__,
  912. ddb_entry->fw_ddb_index));
  913. iscsi_unblock_session(ddb_entry->sess);
  914. } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
  915. qla4xxx_mark_device_missing(ha, ddb_entry);
  916. }
  917. return status;
  918. }
  919. /**
  920. * qla4xxx_relogin_device - re-establish session
  921. * @ha: Pointer to host adapter structure.
  922. * @ddb_entry: Pointer to device database entry
  923. *
  924. * This routine does a session relogin with the specified device.
  925. * The ddb entry must be assigned prior to making this call.
  926. **/
  927. int qla4xxx_relogin_device(struct scsi_qla_host *ha,
  928. struct ddb_entry * ddb_entry)
  929. {
  930. uint16_t relogin_timer;
  931. relogin_timer = max(ddb_entry->default_relogin_timeout,
  932. (uint16_t)RELOGIN_TOV);
  933. atomic_set(&ddb_entry->relogin_timer, relogin_timer);
  934. DEBUG2(printk("scsi%ld: Relogin ddb [%d]. TOV=%d\n", ha->host_no,
  935. ddb_entry->fw_ddb_index, relogin_timer));
  936. qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
  937. return QLA_SUCCESS;
  938. }
  939. static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
  940. {
  941. unsigned long flags;
  942. union external_hw_config_reg extHwConfig;
  943. DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
  944. __func__));
  945. if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
  946. return QLA_ERROR;
  947. if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
  948. ql4xxx_unlock_flash(ha);
  949. return QLA_ERROR;
  950. }
  951. /* Get EEPRom Parameters from NVRAM and validate */
  952. ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
  953. if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
  954. spin_lock_irqsave(&ha->hardware_lock, flags);
  955. extHwConfig.Asuint32_t =
  956. rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
  957. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  958. } else {
  959. ql4_printk(KERN_WARNING, ha,
  960. "scsi%ld: %s: EEProm checksum invalid. "
  961. "Please update your EEPROM\n", ha->host_no,
  962. __func__);
  963. /* Attempt to set defaults */
  964. if (is_qla4010(ha))
  965. extHwConfig.Asuint32_t = 0x1912;
  966. else if (is_qla4022(ha) | is_qla4032(ha))
  967. extHwConfig.Asuint32_t = 0x0023;
  968. else
  969. return QLA_ERROR;
  970. }
  971. DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
  972. ha->host_no, __func__, extHwConfig.Asuint32_t));
  973. spin_lock_irqsave(&ha->hardware_lock, flags);
  974. writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
  975. readl(isp_ext_hw_conf(ha));
  976. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  977. ql4xxx_unlock_nvram(ha);
  978. ql4xxx_unlock_flash(ha);
  979. return QLA_SUCCESS;
  980. }
  981. /**
  982. * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
  983. * @ha: HA context
  984. */
  985. void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
  986. {
  987. pci_set_master(ha->pdev);
  988. }
  989. void qla4xxx_pci_config(struct scsi_qla_host *ha)
  990. {
  991. uint16_t w;
  992. int status;
  993. ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  994. pci_set_master(ha->pdev);
  995. status = pci_set_mwi(ha->pdev);
  996. /*
  997. * We want to respect framework's setting of PCI configuration space
  998. * command register and also want to make sure that all bits of
  999. * interest to us are properly set in command register.
  1000. */
  1001. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  1002. w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  1003. w &= ~PCI_COMMAND_INTX_DISABLE;
  1004. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  1005. }
  1006. static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
  1007. {
  1008. int status = QLA_ERROR;
  1009. unsigned long max_wait_time;
  1010. unsigned long flags;
  1011. uint32_t mbox_status;
  1012. ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
  1013. /*
  1014. * Start firmware from flash ROM
  1015. *
  1016. * WORKAROUND: Stuff a non-constant value that the firmware can
  1017. * use as a seed for a random number generator in MB7 prior to
  1018. * setting BOOT_ENABLE. Fixes problem where the TCP
  1019. * connections use the same TCP ports after each reboot,
  1020. * causing some connections to not get re-established.
  1021. */
  1022. DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
  1023. ha->host_no, __func__));
  1024. spin_lock_irqsave(&ha->hardware_lock, flags);
  1025. writel(jiffies, &ha->reg->mailbox[7]);
  1026. if (is_qla4022(ha) | is_qla4032(ha))
  1027. writel(set_rmask(NVR_WRITE_ENABLE),
  1028. &ha->reg->u1.isp4022.nvram);
  1029. writel(2, &ha->reg->mailbox[6]);
  1030. readl(&ha->reg->mailbox[6]);
  1031. writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
  1032. readl(&ha->reg->ctrl_status);
  1033. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1034. /* Wait for firmware to come UP. */
  1035. DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
  1036. "boot firmware to complete...\n",
  1037. ha->host_no, __func__, FIRMWARE_UP_TOV));
  1038. max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
  1039. do {
  1040. uint32_t ctrl_status;
  1041. spin_lock_irqsave(&ha->hardware_lock, flags);
  1042. ctrl_status = readw(&ha->reg->ctrl_status);
  1043. mbox_status = readw(&ha->reg->mailbox[0]);
  1044. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1045. if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
  1046. break;
  1047. if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
  1048. break;
  1049. DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
  1050. "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
  1051. ha->host_no, __func__, ctrl_status, max_wait_time));
  1052. msleep_interruptible(250);
  1053. } while (!time_after_eq(jiffies, max_wait_time));
  1054. if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
  1055. DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
  1056. ha->host_no, __func__));
  1057. spin_lock_irqsave(&ha->hardware_lock, flags);
  1058. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  1059. &ha->reg->ctrl_status);
  1060. readl(&ha->reg->ctrl_status);
  1061. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1062. status = QLA_SUCCESS;
  1063. } else {
  1064. printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
  1065. "- mbox status 0x%x\n", ha->host_no, __func__,
  1066. mbox_status);
  1067. status = QLA_ERROR;
  1068. }
  1069. return status;
  1070. }
  1071. int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
  1072. {
  1073. #define QL4_LOCK_DRVR_WAIT 60
  1074. #define QL4_LOCK_DRVR_SLEEP 1
  1075. int drvr_wait = QL4_LOCK_DRVR_WAIT;
  1076. while (drvr_wait) {
  1077. if (ql4xxx_lock_drvr(a) == 0) {
  1078. ssleep(QL4_LOCK_DRVR_SLEEP);
  1079. if (drvr_wait) {
  1080. DEBUG2(printk("scsi%ld: %s: Waiting for "
  1081. "Global Init Semaphore(%d)...\n",
  1082. a->host_no,
  1083. __func__, drvr_wait));
  1084. }
  1085. drvr_wait -= QL4_LOCK_DRVR_SLEEP;
  1086. } else {
  1087. DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
  1088. "acquired\n", a->host_no, __func__));
  1089. return QLA_SUCCESS;
  1090. }
  1091. }
  1092. return QLA_ERROR;
  1093. }
  1094. /**
  1095. * qla4xxx_start_firmware - starts qla4xxx firmware
  1096. * @ha: Pointer to host adapter structure.
  1097. *
  1098. * This routine performs the necessary steps to start the firmware for
  1099. * the QLA4010 adapter.
  1100. **/
  1101. int qla4xxx_start_firmware(struct scsi_qla_host *ha)
  1102. {
  1103. unsigned long flags = 0;
  1104. uint32_t mbox_status;
  1105. int status = QLA_ERROR;
  1106. int soft_reset = 1;
  1107. int config_chip = 0;
  1108. if (is_qla4022(ha) | is_qla4032(ha))
  1109. ql4xxx_set_mac_number(ha);
  1110. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  1111. return QLA_ERROR;
  1112. spin_lock_irqsave(&ha->hardware_lock, flags);
  1113. DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
  1114. __func__, readw(isp_port_ctrl(ha))));
  1115. DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
  1116. __func__, readw(isp_port_status(ha))));
  1117. /* Is Hardware already initialized? */
  1118. if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
  1119. DEBUG(printk("scsi%ld: %s: Hardware has already been "
  1120. "initialized\n", ha->host_no, __func__));
  1121. /* Receive firmware boot acknowledgement */
  1122. mbox_status = readw(&ha->reg->mailbox[0]);
  1123. DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
  1124. "0x%x\n", ha->host_no, __func__, mbox_status));
  1125. /* Is firmware already booted? */
  1126. if (mbox_status == 0) {
  1127. /* F/W not running, must be config by net driver */
  1128. config_chip = 1;
  1129. soft_reset = 0;
  1130. } else {
  1131. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  1132. &ha->reg->ctrl_status);
  1133. readl(&ha->reg->ctrl_status);
  1134. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1135. if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
  1136. DEBUG2(printk("scsi%ld: %s: Get firmware "
  1137. "state -- state = 0x%x\n",
  1138. ha->host_no,
  1139. __func__, ha->firmware_state));
  1140. /* F/W is running */
  1141. if (ha->firmware_state &
  1142. FW_STATE_CONFIG_WAIT) {
  1143. DEBUG2(printk("scsi%ld: %s: Firmware "
  1144. "in known state -- "
  1145. "config and "
  1146. "boot, state = 0x%x\n",
  1147. ha->host_no, __func__,
  1148. ha->firmware_state));
  1149. config_chip = 1;
  1150. soft_reset = 0;
  1151. }
  1152. } else {
  1153. DEBUG2(printk("scsi%ld: %s: Firmware in "
  1154. "unknown state -- resetting,"
  1155. " state = "
  1156. "0x%x\n", ha->host_no, __func__,
  1157. ha->firmware_state));
  1158. }
  1159. spin_lock_irqsave(&ha->hardware_lock, flags);
  1160. }
  1161. } else {
  1162. DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
  1163. "started - resetting\n", ha->host_no, __func__));
  1164. }
  1165. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1166. DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
  1167. ha->host_no, __func__, soft_reset, config_chip));
  1168. if (soft_reset) {
  1169. DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
  1170. __func__));
  1171. status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
  1172. * lock again, but ok */
  1173. if (status == QLA_ERROR) {
  1174. DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
  1175. ha->host_no, __func__));
  1176. ql4xxx_unlock_drvr(ha);
  1177. return QLA_ERROR;
  1178. }
  1179. config_chip = 1;
  1180. /* Reset clears the semaphore, so acquire again */
  1181. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  1182. return QLA_ERROR;
  1183. }
  1184. if (config_chip) {
  1185. if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
  1186. status = qla4xxx_start_firmware_from_flash(ha);
  1187. }
  1188. ql4xxx_unlock_drvr(ha);
  1189. if (status == QLA_SUCCESS) {
  1190. if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
  1191. qla4xxx_get_crash_record(ha);
  1192. } else {
  1193. DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
  1194. ha->host_no, __func__));
  1195. }
  1196. return status;
  1197. }
  1198. /**
  1199. * qla4xxx_initialize_adapter - initiailizes hba
  1200. * @ha: Pointer to host adapter structure.
  1201. * @renew_ddb_list: Indicates what to do with the adapter's ddb list
  1202. * after adapter recovery has completed.
  1203. * 0=preserve ddb list, 1=destroy and rebuild ddb list
  1204. *
  1205. * This routine parforms all of the steps necessary to initialize the adapter.
  1206. *
  1207. **/
  1208. int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
  1209. uint8_t renew_ddb_list)
  1210. {
  1211. int status = QLA_ERROR;
  1212. int8_t ip_address[IP_ADDR_LEN] = {0} ;
  1213. ha->eeprom_cmd_data = 0;
  1214. ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  1215. ha->isp_ops->pci_config(ha);
  1216. ha->isp_ops->disable_intrs(ha);
  1217. /* Initialize the Host adapter request/response queues and firmware */
  1218. if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
  1219. goto exit_init_hba;
  1220. if (qla4xxx_get_fw_version(ha) == QLA_ERROR)
  1221. goto exit_init_hba;
  1222. if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
  1223. goto exit_init_hba;
  1224. if (qla4xxx_init_local_data(ha) == QLA_ERROR)
  1225. goto exit_init_hba;
  1226. status = qla4xxx_init_firmware(ha);
  1227. if (status == QLA_ERROR)
  1228. goto exit_init_hba;
  1229. /*
  1230. * FW is waiting to get an IP address from DHCP server: Skip building
  1231. * the ddb_list and wait for DHCP lease acquired aen to come in
  1232. * followed by 0x8014 aen" to trigger the tgt discovery process.
  1233. */
  1234. if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
  1235. goto exit_init_online;
  1236. /* Skip device discovery if ip and subnet is zero */
  1237. if (memcmp(ha->ip_address, ip_address, IP_ADDR_LEN) == 0 ||
  1238. memcmp(ha->subnet_mask, ip_address, IP_ADDR_LEN) == 0)
  1239. goto exit_init_online;
  1240. if (renew_ddb_list == PRESERVE_DDB_LIST) {
  1241. /*
  1242. * We want to preserve lun states (i.e. suspended, etc.)
  1243. * for recovery initiated by the driver. So just update
  1244. * the device states for the existing ddb_list.
  1245. */
  1246. qla4xxx_reinitialize_ddb_list(ha);
  1247. } else if (renew_ddb_list == REBUILD_DDB_LIST) {
  1248. /*
  1249. * We want to build the ddb_list from scratch during
  1250. * driver initialization and recovery initiated by the
  1251. * INT_HBA_RESET IOCTL.
  1252. */
  1253. status = qla4xxx_initialize_ddb_list(ha);
  1254. if (status == QLA_ERROR) {
  1255. DEBUG2(printk("%s(%ld) Error occurred during build"
  1256. "ddb list\n", __func__, ha->host_no));
  1257. goto exit_init_hba;
  1258. }
  1259. }
  1260. if (!ha->tot_ddbs) {
  1261. DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
  1262. "present in Firmware device database\n",
  1263. ha->host_no));
  1264. }
  1265. exit_init_online:
  1266. set_bit(AF_ONLINE, &ha->flags);
  1267. exit_init_hba:
  1268. if (is_qla8022(ha) && (status == QLA_ERROR)) {
  1269. /* Since interrupts are registered in start_firmware for
  1270. * 82xx, release them here if initialize_adapter fails */
  1271. qla4xxx_free_irqs(ha);
  1272. }
  1273. DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
  1274. status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
  1275. return status;
  1276. }
  1277. /**
  1278. * qla4xxx_add_device_dynamically - ddb addition due to an AEN
  1279. * @ha: Pointer to host adapter structure.
  1280. * @fw_ddb_index: Firmware's device database index
  1281. *
  1282. * This routine processes adds a device as a result of an 8014h AEN.
  1283. **/
  1284. static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
  1285. uint32_t fw_ddb_index)
  1286. {
  1287. struct ddb_entry * ddb_entry;
  1288. uint32_t new_tgt;
  1289. /* First allocate a device structure */
  1290. ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
  1291. if (ddb_entry == NULL) {
  1292. DEBUG2(printk(KERN_WARNING
  1293. "scsi%ld: Unable to allocate memory to add "
  1294. "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
  1295. return;
  1296. }
  1297. if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
  1298. /* Target has been bound to a new fw_ddb_index */
  1299. qla4xxx_free_ddb(ha, ddb_entry);
  1300. ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
  1301. if (ddb_entry == NULL) {
  1302. DEBUG2(printk(KERN_WARNING
  1303. "scsi%ld: Unable to allocate memory"
  1304. " to add fw_ddb_index %d\n",
  1305. ha->host_no, fw_ddb_index));
  1306. return;
  1307. }
  1308. }
  1309. if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
  1310. QLA_ERROR) {
  1311. ha->fw_ddb_index_map[fw_ddb_index] =
  1312. (struct ddb_entry *)INVALID_ENTRY;
  1313. DEBUG2(printk(KERN_WARNING
  1314. "scsi%ld: failed to add new device at index "
  1315. "[%d]\n Unable to retrieve fw ddb entry\n",
  1316. ha->host_no, fw_ddb_index));
  1317. qla4xxx_free_ddb(ha, ddb_entry);
  1318. return;
  1319. }
  1320. if (qla4xxx_add_sess(ddb_entry)) {
  1321. DEBUG2(printk(KERN_WARNING
  1322. "scsi%ld: failed to add new device at index "
  1323. "[%d]\n Unable to add connection and session\n",
  1324. ha->host_no, fw_ddb_index));
  1325. qla4xxx_free_ddb(ha, ddb_entry);
  1326. }
  1327. }
  1328. /**
  1329. * qla4xxx_process_ddb_changed - process ddb state change
  1330. * @ha - Pointer to host adapter structure.
  1331. * @fw_ddb_index - Firmware's device database index
  1332. * @state - Device state
  1333. *
  1334. * This routine processes a Decive Database Changed AEN Event.
  1335. **/
  1336. int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
  1337. uint32_t state, uint32_t conn_err)
  1338. {
  1339. struct ddb_entry * ddb_entry;
  1340. uint32_t old_fw_ddb_device_state;
  1341. /* check for out of range index */
  1342. if (fw_ddb_index >= MAX_DDB_ENTRIES)
  1343. return QLA_ERROR;
  1344. /* Get the corresponging ddb entry */
  1345. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
  1346. /* Device does not currently exist in our database. */
  1347. if (ddb_entry == NULL) {
  1348. if (state == DDB_DS_SESSION_ACTIVE)
  1349. qla4xxx_add_device_dynamically(ha, fw_ddb_index);
  1350. return QLA_SUCCESS;
  1351. }
  1352. /* Device already exists in our database. */
  1353. old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
  1354. DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
  1355. "index [%d]\n", ha->host_no, __func__,
  1356. ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
  1357. if (old_fw_ddb_device_state == state &&
  1358. state == DDB_DS_SESSION_ACTIVE) {
  1359. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  1360. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  1361. iscsi_unblock_session(ddb_entry->sess);
  1362. }
  1363. return QLA_SUCCESS;
  1364. }
  1365. ddb_entry->fw_ddb_device_state = state;
  1366. /* Device is back online. */
  1367. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  1368. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  1369. atomic_set(&ddb_entry->relogin_retry_count, 0);
  1370. atomic_set(&ddb_entry->relogin_timer, 0);
  1371. clear_bit(DF_RELOGIN, &ddb_entry->flags);
  1372. clear_bit(DF_NO_RELOGIN, &ddb_entry->flags);
  1373. iscsi_unblock_session(ddb_entry->sess);
  1374. iscsi_session_event(ddb_entry->sess,
  1375. ISCSI_KEVENT_CREATE_SESSION);
  1376. /*
  1377. * Change the lun state to READY in case the lun TIMEOUT before
  1378. * the device came back.
  1379. */
  1380. } else {
  1381. /* Device went away, mark device missing */
  1382. if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE) {
  1383. DEBUG2(ql4_printk(KERN_INFO, ha, "%s mark missing "
  1384. "ddb_entry 0x%p sess 0x%p conn 0x%p\n",
  1385. __func__, ddb_entry,
  1386. ddb_entry->sess, ddb_entry->conn));
  1387. qla4xxx_mark_device_missing(ha, ddb_entry);
  1388. }
  1389. /*
  1390. * Relogin if device state changed to a not active state.
  1391. * However, do not relogin if a RELOGIN is in process, or
  1392. * we are not allowed to relogin to this DDB.
  1393. */
  1394. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
  1395. !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1396. !test_bit(DF_NO_RELOGIN, &ddb_entry->flags) &&
  1397. qla4_is_relogin_allowed(ha, conn_err)) {
  1398. /*
  1399. * This triggers a relogin. After the relogin_timer
  1400. * expires, the relogin gets scheduled. We must wait a
  1401. * minimum amount of time since receiving an 0x8014 AEN
  1402. * with failed device_state or a logout response before
  1403. * we can issue another relogin.
  1404. */
  1405. /* Firmware pads this timeout: (time2wait +1).
  1406. * Driver retry to login should be longer than F/W.
  1407. * Otherwise F/W will fail
  1408. * set_ddb() mbx cmd with 0x4005 since it still
  1409. * counting down its time2wait.
  1410. */
  1411. atomic_set(&ddb_entry->relogin_timer, 0);
  1412. atomic_set(&ddb_entry->retry_relogin_timer,
  1413. ddb_entry->default_time2wait + 4);
  1414. DEBUG(printk("scsi%ld: %s: ddb[%d] "
  1415. "initiate relogin after %d seconds\n",
  1416. ha->host_no, __func__,
  1417. ddb_entry->fw_ddb_index,
  1418. ddb_entry->default_time2wait + 4));
  1419. } else {
  1420. DEBUG(printk("scsi%ld: %s: ddb[%d] "
  1421. "relogin not initiated, state = %d, "
  1422. "ddb_entry->flags = 0x%lx\n",
  1423. ha->host_no, __func__,
  1424. ddb_entry->fw_ddb_index,
  1425. ddb_entry->fw_ddb_device_state,
  1426. ddb_entry->flags));
  1427. }
  1428. }
  1429. return QLA_SUCCESS;
  1430. }