ql4_init.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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 <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. ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
  395. if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
  396. DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
  397. "control block\n", ha->host_no, __func__));
  398. return status;
  399. }
  400. if (!qla4xxx_fw_ready(ha))
  401. return status;
  402. return qla4xxx_get_firmware_status(ha);
  403. }
  404. static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
  405. uint32_t fw_ddb_index,
  406. uint32_t *new_tgt)
  407. {
  408. struct dev_db_entry *fw_ddb_entry = NULL;
  409. dma_addr_t fw_ddb_entry_dma;
  410. struct ddb_entry *ddb_entry = NULL;
  411. int found = 0;
  412. uint32_t device_state;
  413. *new_tgt = 0;
  414. /* Make sure the dma buffer is valid */
  415. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
  416. sizeof(*fw_ddb_entry),
  417. &fw_ddb_entry_dma, GFP_KERNEL);
  418. if (fw_ddb_entry == NULL) {
  419. DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
  420. ha->host_no, __func__));
  421. goto exit_get_ddb_entry_no_free;
  422. }
  423. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
  424. fw_ddb_entry_dma, NULL, NULL,
  425. &device_state, NULL, NULL, NULL) ==
  426. QLA_ERROR) {
  427. DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
  428. "fw_ddb_index %d\n", ha->host_no, __func__,
  429. fw_ddb_index));
  430. goto exit_get_ddb_entry;
  431. }
  432. /* Allocate DDB if not already allocated. */
  433. DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
  434. __func__, fw_ddb_index));
  435. list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
  436. if ((memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
  437. ISCSI_NAME_SIZE) == 0) &&
  438. (ddb_entry->tpgt ==
  439. le32_to_cpu(fw_ddb_entry->tgt_portal_grp)) &&
  440. (memcmp(ddb_entry->isid, fw_ddb_entry->isid,
  441. sizeof(ddb_entry->isid)) == 0)) {
  442. found++;
  443. break;
  444. }
  445. }
  446. /* if not found allocate new ddb */
  447. if (!found) {
  448. DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
  449. "new ddb\n", ha->host_no, __func__,
  450. fw_ddb_index));
  451. *new_tgt = 1;
  452. ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
  453. }
  454. exit_get_ddb_entry:
  455. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
  456. fw_ddb_entry_dma);
  457. exit_get_ddb_entry_no_free:
  458. return ddb_entry;
  459. }
  460. /**
  461. * qla4xxx_update_ddb_entry - update driver's internal ddb
  462. * @ha: pointer to host adapter structure.
  463. * @ddb_entry: pointer to device database structure to be filled
  464. * @fw_ddb_index: index of the ddb entry in fw ddb table
  465. *
  466. * This routine updates the driver's internal device database entry
  467. * with information retrieved from the firmware's device database
  468. * entry for the specified device. The ddb_entry->fw_ddb_index field
  469. * must be initialized prior to calling this routine
  470. *
  471. **/
  472. static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
  473. struct ddb_entry *ddb_entry,
  474. uint32_t fw_ddb_index)
  475. {
  476. struct dev_db_entry *fw_ddb_entry = NULL;
  477. dma_addr_t fw_ddb_entry_dma;
  478. int status = QLA_ERROR;
  479. uint32_t conn_err;
  480. if (ddb_entry == NULL) {
  481. DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
  482. __func__));
  483. goto exit_update_ddb_no_free;
  484. }
  485. /* Make sure the dma buffer is valid */
  486. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
  487. sizeof(*fw_ddb_entry),
  488. &fw_ddb_entry_dma, GFP_KERNEL);
  489. if (fw_ddb_entry == NULL) {
  490. DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
  491. ha->host_no, __func__));
  492. goto exit_update_ddb_no_free;
  493. }
  494. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
  495. fw_ddb_entry_dma, NULL, NULL,
  496. &ddb_entry->fw_ddb_device_state, &conn_err,
  497. &ddb_entry->tcp_source_port_num,
  498. &ddb_entry->connection_id) ==
  499. QLA_ERROR) {
  500. DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
  501. "fw_ddb_index %d\n", ha->host_no, __func__,
  502. fw_ddb_index));
  503. goto exit_update_ddb;
  504. }
  505. status = QLA_SUCCESS;
  506. ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
  507. ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
  508. ddb_entry->task_mgmt_timeout =
  509. le16_to_cpu(fw_ddb_entry->def_timeout);
  510. ddb_entry->CmdSn = 0;
  511. ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
  512. ddb_entry->default_relogin_timeout =
  513. le16_to_cpu(fw_ddb_entry->def_timeout);
  514. ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  515. /* Update index in case it changed */
  516. ddb_entry->fw_ddb_index = fw_ddb_index;
  517. ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
  518. ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
  519. ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  520. memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
  521. memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
  522. min(sizeof(ddb_entry->iscsi_name),
  523. sizeof(fw_ddb_entry->iscsi_name)));
  524. memcpy(&ddb_entry->iscsi_alias[0], &fw_ddb_entry->iscsi_alias[0],
  525. min(sizeof(ddb_entry->iscsi_alias),
  526. sizeof(fw_ddb_entry->iscsi_alias)));
  527. memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
  528. min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
  529. ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
  530. ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
  531. ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
  532. ddb_entry->iscsi_max_rcv_data_seg_len =
  533. fw_ddb_entry->iscsi_max_rcv_data_seg_len;
  534. ddb_entry->iscsi_max_snd_data_seg_len =
  535. fw_ddb_entry->iscsi_max_snd_data_seg_len;
  536. if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
  537. memcpy(&ddb_entry->remote_ipv6_addr,
  538. fw_ddb_entry->ip_addr,
  539. min(sizeof(ddb_entry->remote_ipv6_addr),
  540. sizeof(fw_ddb_entry->ip_addr)));
  541. memcpy(&ddb_entry->link_local_ipv6_addr,
  542. fw_ddb_entry->link_local_ipv6_addr,
  543. min(sizeof(ddb_entry->link_local_ipv6_addr),
  544. sizeof(fw_ddb_entry->link_local_ipv6_addr)));
  545. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
  546. " ConnErr %08x IP %pI6 "
  547. ":%04d \"%s\"\n",
  548. __func__, fw_ddb_index,
  549. ddb_entry->fw_ddb_device_state,
  550. conn_err, fw_ddb_entry->ip_addr,
  551. le16_to_cpu(fw_ddb_entry->port),
  552. fw_ddb_entry->iscsi_name));
  553. } else
  554. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DDB[%d] State %04x"
  555. " ConnErr %08x IP %pI4 "
  556. ":%04d \"%s\"\n",
  557. __func__, fw_ddb_index,
  558. ddb_entry->fw_ddb_device_state,
  559. conn_err, fw_ddb_entry->ip_addr,
  560. le16_to_cpu(fw_ddb_entry->port),
  561. fw_ddb_entry->iscsi_name));
  562. exit_update_ddb:
  563. if (fw_ddb_entry)
  564. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  565. fw_ddb_entry, fw_ddb_entry_dma);
  566. exit_update_ddb_no_free:
  567. return status;
  568. }
  569. /**
  570. * qla4xxx_alloc_ddb - allocate device database entry
  571. * @ha: Pointer to host adapter structure.
  572. * @fw_ddb_index: Firmware's device database index
  573. *
  574. * This routine allocates a ddb_entry, ititializes some values, and
  575. * inserts it into the ddb list.
  576. **/
  577. static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
  578. uint32_t fw_ddb_index)
  579. {
  580. struct ddb_entry *ddb_entry;
  581. DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
  582. __func__, fw_ddb_index));
  583. ddb_entry = qla4xxx_alloc_sess(ha);
  584. if (ddb_entry == NULL) {
  585. DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
  586. "to add fw_ddb_index [%d]\n",
  587. ha->host_no, __func__, fw_ddb_index));
  588. return ddb_entry;
  589. }
  590. ddb_entry->fw_ddb_index = fw_ddb_index;
  591. atomic_set(&ddb_entry->port_down_timer, ha->port_down_retry_count);
  592. atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
  593. atomic_set(&ddb_entry->relogin_timer, 0);
  594. atomic_set(&ddb_entry->relogin_retry_count, 0);
  595. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  596. list_add_tail(&ddb_entry->list, &ha->ddb_list);
  597. ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
  598. ha->tot_ddbs++;
  599. return ddb_entry;
  600. }
  601. /**
  602. * qla4_is_relogin_allowed - Are we allowed to login?
  603. * @ha: Pointer to host adapter structure.
  604. * @conn_err: Last connection error associated with the ddb
  605. *
  606. * This routine tests the given connection error to determine if
  607. * we are allowed to login.
  608. **/
  609. int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
  610. {
  611. uint32_t err_code, login_rsp_sts_class;
  612. int relogin = 1;
  613. err_code = ((conn_err & 0x00ff0000) >> 16);
  614. login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
  615. if (err_code == 0x1c || err_code == 0x06) {
  616. DEBUG2(ql4_printk(KERN_INFO, ha,
  617. ": conn_err=0x%08x, send target completed"
  618. " or access denied failure\n", conn_err));
  619. relogin = 0;
  620. }
  621. if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
  622. /* Login Response PDU returned an error.
  623. Login Response Status in Error Code Detail
  624. indicates login should not be retried.*/
  625. DEBUG2(ql4_printk(KERN_INFO, ha,
  626. ": conn_err=0x%08x, do not retry relogin\n",
  627. conn_err));
  628. relogin = 0;
  629. }
  630. return relogin;
  631. }
  632. /**
  633. * qla4xxx_configure_ddbs - builds driver ddb list
  634. * @ha: Pointer to host adapter structure.
  635. *
  636. * This routine searches for all valid firmware ddb entries and builds
  637. * an internal ddb list. Ddbs that are considered valid are those with
  638. * a device state of SESSION_ACTIVE.
  639. **/
  640. static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
  641. {
  642. int status = QLA_ERROR;
  643. uint32_t fw_ddb_index = 0;
  644. uint32_t next_fw_ddb_index = 0;
  645. uint32_t ddb_state;
  646. uint32_t conn_err;
  647. struct ddb_entry *ddb_entry;
  648. struct dev_db_entry *fw_ddb_entry = NULL;
  649. dma_addr_t fw_ddb_entry_dma;
  650. uint32_t ipv6_device;
  651. uint32_t new_tgt;
  652. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  653. &fw_ddb_entry_dma, GFP_KERNEL);
  654. if (fw_ddb_entry == NULL) {
  655. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: DMA alloc failed\n",
  656. __func__));
  657. goto exit_build_ddb_list_no_free;
  658. }
  659. ql4_printk(KERN_INFO, ha, "Initializing DDBs ...\n");
  660. for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
  661. fw_ddb_index = next_fw_ddb_index) {
  662. /* First, let's see if a device exists here */
  663. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
  664. 0, NULL, &next_fw_ddb_index,
  665. &ddb_state, &conn_err,
  666. NULL, NULL) ==
  667. QLA_ERROR) {
  668. DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
  669. "fw_ddb_index %d failed", ha->host_no,
  670. __func__, fw_ddb_index));
  671. goto exit_build_ddb_list;
  672. }
  673. DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
  674. "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
  675. fw_ddb_index, ddb_state, next_fw_ddb_index));
  676. /* Issue relogin, if necessary. */
  677. if (ddb_state == DDB_DS_SESSION_FAILED ||
  678. ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
  679. /* Try and login to device */
  680. DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
  681. ha->host_no, __func__, fw_ddb_index));
  682. ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
  683. DDB_OPT_IPV6_DEVICE;
  684. if (qla4_is_relogin_allowed(ha, conn_err) &&
  685. ((!ipv6_device &&
  686. *((uint32_t *)fw_ddb_entry->ip_addr))
  687. || ipv6_device)) {
  688. qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
  689. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
  690. NULL, 0, NULL,
  691. &next_fw_ddb_index,
  692. &ddb_state, &conn_err,
  693. NULL, NULL)
  694. == QLA_ERROR) {
  695. DEBUG2(printk("scsi%ld: %s:"
  696. "get_ddb_entry %d failed\n",
  697. ha->host_no,
  698. __func__, fw_ddb_index));
  699. goto exit_build_ddb_list;
  700. }
  701. }
  702. }
  703. if (ddb_state != DDB_DS_SESSION_ACTIVE)
  704. goto next_one;
  705. /*
  706. * if fw_ddb with session active state found,
  707. * add to ddb_list
  708. */
  709. DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
  710. ha->host_no, __func__, fw_ddb_index));
  711. /* Add DDB to internal our ddb list. */
  712. ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
  713. if (ddb_entry == NULL) {
  714. DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
  715. "for device at fw_ddb_index %d\n",
  716. ha->host_no, __func__, fw_ddb_index));
  717. goto exit_build_ddb_list;
  718. }
  719. /* Fill in the device structure */
  720. if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
  721. QLA_ERROR) {
  722. ha->fw_ddb_index_map[fw_ddb_index] =
  723. (struct ddb_entry *)INVALID_ENTRY;
  724. DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
  725. "for fw_ddb_index %d.\n",
  726. ha->host_no, __func__, fw_ddb_index));
  727. goto exit_build_ddb_list;
  728. }
  729. next_one:
  730. /* We know we've reached the last device when
  731. * next_fw_ddb_index is 0 */
  732. if (next_fw_ddb_index == 0)
  733. break;
  734. }
  735. status = QLA_SUCCESS;
  736. ql4_printk(KERN_INFO, ha, "DDB list done..\n");
  737. exit_build_ddb_list:
  738. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
  739. fw_ddb_entry_dma);
  740. exit_build_ddb_list_no_free:
  741. return status;
  742. }
  743. struct qla4_relog_scan {
  744. int halt_wait;
  745. uint32_t conn_err;
  746. uint32_t fw_ddb_index;
  747. uint32_t next_fw_ddb_index;
  748. uint32_t fw_ddb_device_state;
  749. };
  750. static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
  751. {
  752. struct ddb_entry *ddb_entry;
  753. if (qla4_is_relogin_allowed(ha, rs->conn_err)) {
  754. /* We either have a device that is in
  755. * the process of relogging in or a
  756. * device that is waiting to be
  757. * relogged in */
  758. rs->halt_wait = 0;
  759. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
  760. rs->fw_ddb_index);
  761. if (ddb_entry == NULL)
  762. return QLA_ERROR;
  763. if (ddb_entry->dev_scan_wait_to_start_relogin != 0
  764. && time_after_eq(jiffies,
  765. ddb_entry->
  766. dev_scan_wait_to_start_relogin))
  767. {
  768. ddb_entry->dev_scan_wait_to_start_relogin = 0;
  769. qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
  770. }
  771. }
  772. return QLA_SUCCESS;
  773. }
  774. static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
  775. struct qla4_relog_scan *rs)
  776. {
  777. int error;
  778. /* scan for relogins
  779. * ----------------- */
  780. for (rs->fw_ddb_index = 0; rs->fw_ddb_index < MAX_DDB_ENTRIES;
  781. rs->fw_ddb_index = rs->next_fw_ddb_index) {
  782. if (qla4xxx_get_fwddb_entry(ha, rs->fw_ddb_index, NULL, 0,
  783. NULL, &rs->next_fw_ddb_index,
  784. &rs->fw_ddb_device_state,
  785. &rs->conn_err, NULL, NULL)
  786. == QLA_ERROR)
  787. return QLA_ERROR;
  788. if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
  789. rs->halt_wait = 0;
  790. if (rs->fw_ddb_device_state == DDB_DS_SESSION_FAILED ||
  791. rs->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE) {
  792. error = qla4_test_rdy(ha, rs);
  793. if (error)
  794. return error;
  795. }
  796. /* We know we've reached the last device when
  797. * next_fw_ddb_index is 0 */
  798. if (rs->next_fw_ddb_index == 0)
  799. break;
  800. }
  801. return QLA_SUCCESS;
  802. }
  803. /**
  804. * qla4xxx_devices_ready - wait for target devices to be logged in
  805. * @ha: pointer to adapter structure
  806. *
  807. * This routine waits up to ql4xdiscoverywait seconds
  808. * F/W database during driver load time.
  809. **/
  810. static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
  811. {
  812. int error;
  813. unsigned long discovery_wtime;
  814. struct qla4_relog_scan rs;
  815. discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
  816. DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
  817. do {
  818. /* poll for AEN. */
  819. qla4xxx_get_firmware_state(ha);
  820. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags)) {
  821. /* Set time-between-relogin timer */
  822. qla4xxx_process_aen(ha, RELOGIN_DDB_CHANGED_AENS);
  823. }
  824. /* if no relogins active or needed, halt discvery wait */
  825. rs.halt_wait = 1;
  826. error = qla4_scan_for_relogin(ha, &rs);
  827. if (rs.halt_wait) {
  828. DEBUG2(printk("scsi%ld: %s: Delay halted. Devices "
  829. "Ready.\n", ha->host_no, __func__));
  830. return QLA_SUCCESS;
  831. }
  832. msleep(2000);
  833. } while (!time_after_eq(jiffies, discovery_wtime));
  834. DEBUG3(qla4xxx_get_conn_event_log(ha));
  835. return QLA_SUCCESS;
  836. }
  837. static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
  838. {
  839. unsigned long wtime;
  840. /* Flush the 0x8014 AEN from the firmware as a result of
  841. * Auto connect. We are basically doing get_firmware_ddb()
  842. * to determine whether we need to log back in or not.
  843. * Trying to do a set ddb before we have processed 0x8014
  844. * will result in another set_ddb() for the same ddb. In other
  845. * words there will be stale entries in the aen_q.
  846. */
  847. wtime = jiffies + (2 * HZ);
  848. do {
  849. if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
  850. if (ha->firmware_state & (BIT_2 | BIT_0))
  851. return;
  852. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  853. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  854. msleep(1000);
  855. } while (!time_after_eq(jiffies, wtime));
  856. }
  857. static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
  858. {
  859. uint16_t fw_ddb_index;
  860. int status = QLA_SUCCESS;
  861. /* free the ddb list if is not empty */
  862. if (!list_empty(&ha->ddb_list))
  863. qla4xxx_free_ddb_list(ha);
  864. for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
  865. ha->fw_ddb_index_map[fw_ddb_index] =
  866. (struct ddb_entry *)INVALID_ENTRY;
  867. ha->tot_ddbs = 0;
  868. qla4xxx_flush_AENS(ha);
  869. /* Wait for an AEN */
  870. qla4xxx_devices_ready(ha);
  871. /*
  872. * First perform device discovery for active
  873. * fw ddb indexes and build
  874. * ddb list.
  875. */
  876. if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
  877. return status;
  878. /*
  879. * Targets can come online after the inital discovery, so processing
  880. * the aens here will catch them.
  881. */
  882. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  883. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  884. return status;
  885. }
  886. /**
  887. * qla4xxx_reinitialize_ddb_list - update the driver ddb list
  888. * @ha: pointer to host adapter structure.
  889. *
  890. * This routine obtains device information from the F/W database after
  891. * firmware or adapter resets. The device table is preserved.
  892. **/
  893. int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
  894. {
  895. int status = QLA_SUCCESS;
  896. struct ddb_entry *ddb_entry, *detemp;
  897. /* Update the device information for all devices. */
  898. list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
  899. qla4xxx_update_ddb_entry(ha, ddb_entry,
  900. ddb_entry->fw_ddb_index);
  901. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  902. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  903. DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
  904. "ONLINE\n", ha->host_no, __func__,
  905. ddb_entry->fw_ddb_index));
  906. iscsi_unblock_session(ddb_entry->sess);
  907. } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
  908. qla4xxx_mark_device_missing(ha, ddb_entry);
  909. }
  910. return status;
  911. }
  912. /**
  913. * qla4xxx_relogin_device - re-establish session
  914. * @ha: Pointer to host adapter structure.
  915. * @ddb_entry: Pointer to device database entry
  916. *
  917. * This routine does a session relogin with the specified device.
  918. * The ddb entry must be assigned prior to making this call.
  919. **/
  920. int qla4xxx_relogin_device(struct scsi_qla_host *ha,
  921. struct ddb_entry * ddb_entry)
  922. {
  923. uint16_t relogin_timer;
  924. relogin_timer = max(ddb_entry->default_relogin_timeout,
  925. (uint16_t)RELOGIN_TOV);
  926. atomic_set(&ddb_entry->relogin_timer, relogin_timer);
  927. DEBUG2(printk("scsi%ld: Relogin ddb [%d]. TOV=%d\n", ha->host_no,
  928. ddb_entry->fw_ddb_index, relogin_timer));
  929. qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
  930. return QLA_SUCCESS;
  931. }
  932. static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
  933. {
  934. unsigned long flags;
  935. union external_hw_config_reg extHwConfig;
  936. DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
  937. __func__));
  938. if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
  939. return QLA_ERROR;
  940. if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
  941. ql4xxx_unlock_flash(ha);
  942. return QLA_ERROR;
  943. }
  944. /* Get EEPRom Parameters from NVRAM and validate */
  945. ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
  946. if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
  947. spin_lock_irqsave(&ha->hardware_lock, flags);
  948. extHwConfig.Asuint32_t =
  949. rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
  950. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  951. } else {
  952. ql4_printk(KERN_WARNING, ha,
  953. "scsi%ld: %s: EEProm checksum invalid. "
  954. "Please update your EEPROM\n", ha->host_no,
  955. __func__);
  956. /* Attempt to set defaults */
  957. if (is_qla4010(ha))
  958. extHwConfig.Asuint32_t = 0x1912;
  959. else if (is_qla4022(ha) | is_qla4032(ha))
  960. extHwConfig.Asuint32_t = 0x0023;
  961. else
  962. return QLA_ERROR;
  963. }
  964. DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
  965. ha->host_no, __func__, extHwConfig.Asuint32_t));
  966. spin_lock_irqsave(&ha->hardware_lock, flags);
  967. writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
  968. readl(isp_ext_hw_conf(ha));
  969. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  970. ql4xxx_unlock_nvram(ha);
  971. ql4xxx_unlock_flash(ha);
  972. return QLA_SUCCESS;
  973. }
  974. /**
  975. * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
  976. * @ha: HA context
  977. */
  978. void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
  979. {
  980. pci_set_master(ha->pdev);
  981. }
  982. void qla4xxx_pci_config(struct scsi_qla_host *ha)
  983. {
  984. uint16_t w;
  985. int status;
  986. ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  987. pci_set_master(ha->pdev);
  988. status = pci_set_mwi(ha->pdev);
  989. /*
  990. * We want to respect framework's setting of PCI configuration space
  991. * command register and also want to make sure that all bits of
  992. * interest to us are properly set in command register.
  993. */
  994. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  995. w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  996. w &= ~PCI_COMMAND_INTX_DISABLE;
  997. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  998. }
  999. static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
  1000. {
  1001. int status = QLA_ERROR;
  1002. unsigned long max_wait_time;
  1003. unsigned long flags;
  1004. uint32_t mbox_status;
  1005. ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
  1006. /*
  1007. * Start firmware from flash ROM
  1008. *
  1009. * WORKAROUND: Stuff a non-constant value that the firmware can
  1010. * use as a seed for a random number generator in MB7 prior to
  1011. * setting BOOT_ENABLE. Fixes problem where the TCP
  1012. * connections use the same TCP ports after each reboot,
  1013. * causing some connections to not get re-established.
  1014. */
  1015. DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
  1016. ha->host_no, __func__));
  1017. spin_lock_irqsave(&ha->hardware_lock, flags);
  1018. writel(jiffies, &ha->reg->mailbox[7]);
  1019. if (is_qla4022(ha) | is_qla4032(ha))
  1020. writel(set_rmask(NVR_WRITE_ENABLE),
  1021. &ha->reg->u1.isp4022.nvram);
  1022. writel(2, &ha->reg->mailbox[6]);
  1023. readl(&ha->reg->mailbox[6]);
  1024. writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
  1025. readl(&ha->reg->ctrl_status);
  1026. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1027. /* Wait for firmware to come UP. */
  1028. DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
  1029. "boot firmware to complete...\n",
  1030. ha->host_no, __func__, FIRMWARE_UP_TOV));
  1031. max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
  1032. do {
  1033. uint32_t ctrl_status;
  1034. spin_lock_irqsave(&ha->hardware_lock, flags);
  1035. ctrl_status = readw(&ha->reg->ctrl_status);
  1036. mbox_status = readw(&ha->reg->mailbox[0]);
  1037. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1038. if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
  1039. break;
  1040. if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
  1041. break;
  1042. DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
  1043. "firmware to complete... ctrl_sts=0x%x\n",
  1044. ha->host_no, __func__, ctrl_status));
  1045. msleep_interruptible(250);
  1046. } while (!time_after_eq(jiffies, max_wait_time));
  1047. if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
  1048. DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
  1049. ha->host_no, __func__));
  1050. spin_lock_irqsave(&ha->hardware_lock, flags);
  1051. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  1052. &ha->reg->ctrl_status);
  1053. readl(&ha->reg->ctrl_status);
  1054. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1055. status = QLA_SUCCESS;
  1056. } else {
  1057. printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
  1058. "- mbox status 0x%x\n", ha->host_no, __func__,
  1059. mbox_status);
  1060. status = QLA_ERROR;
  1061. }
  1062. return status;
  1063. }
  1064. int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
  1065. {
  1066. #define QL4_LOCK_DRVR_WAIT 60
  1067. #define QL4_LOCK_DRVR_SLEEP 1
  1068. int drvr_wait = QL4_LOCK_DRVR_WAIT;
  1069. while (drvr_wait) {
  1070. if (ql4xxx_lock_drvr(a) == 0) {
  1071. ssleep(QL4_LOCK_DRVR_SLEEP);
  1072. if (drvr_wait) {
  1073. DEBUG2(printk("scsi%ld: %s: Waiting for "
  1074. "Global Init Semaphore(%d)...\n",
  1075. a->host_no,
  1076. __func__, drvr_wait));
  1077. }
  1078. drvr_wait -= QL4_LOCK_DRVR_SLEEP;
  1079. } else {
  1080. DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
  1081. "acquired\n", a->host_no, __func__));
  1082. return QLA_SUCCESS;
  1083. }
  1084. }
  1085. return QLA_ERROR;
  1086. }
  1087. /**
  1088. * qla4xxx_start_firmware - starts qla4xxx firmware
  1089. * @ha: Pointer to host adapter structure.
  1090. *
  1091. * This routine performs the necessary steps to start the firmware for
  1092. * the QLA4010 adapter.
  1093. **/
  1094. int qla4xxx_start_firmware(struct scsi_qla_host *ha)
  1095. {
  1096. unsigned long flags = 0;
  1097. uint32_t mbox_status;
  1098. int status = QLA_ERROR;
  1099. int soft_reset = 1;
  1100. int config_chip = 0;
  1101. if (is_qla4022(ha) | is_qla4032(ha))
  1102. ql4xxx_set_mac_number(ha);
  1103. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  1104. return QLA_ERROR;
  1105. spin_lock_irqsave(&ha->hardware_lock, flags);
  1106. DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
  1107. __func__, readw(isp_port_ctrl(ha))));
  1108. DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
  1109. __func__, readw(isp_port_status(ha))));
  1110. /* Is Hardware already initialized? */
  1111. if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
  1112. DEBUG(printk("scsi%ld: %s: Hardware has already been "
  1113. "initialized\n", ha->host_no, __func__));
  1114. /* Receive firmware boot acknowledgement */
  1115. mbox_status = readw(&ha->reg->mailbox[0]);
  1116. DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
  1117. "0x%x\n", ha->host_no, __func__, mbox_status));
  1118. /* Is firmware already booted? */
  1119. if (mbox_status == 0) {
  1120. /* F/W not running, must be config by net driver */
  1121. config_chip = 1;
  1122. soft_reset = 0;
  1123. } else {
  1124. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  1125. &ha->reg->ctrl_status);
  1126. readl(&ha->reg->ctrl_status);
  1127. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1128. if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
  1129. DEBUG2(printk("scsi%ld: %s: Get firmware "
  1130. "state -- state = 0x%x\n",
  1131. ha->host_no,
  1132. __func__, ha->firmware_state));
  1133. /* F/W is running */
  1134. if (ha->firmware_state &
  1135. FW_STATE_CONFIG_WAIT) {
  1136. DEBUG2(printk("scsi%ld: %s: Firmware "
  1137. "in known state -- "
  1138. "config and "
  1139. "boot, state = 0x%x\n",
  1140. ha->host_no, __func__,
  1141. ha->firmware_state));
  1142. config_chip = 1;
  1143. soft_reset = 0;
  1144. }
  1145. } else {
  1146. DEBUG2(printk("scsi%ld: %s: Firmware in "
  1147. "unknown state -- resetting,"
  1148. " state = "
  1149. "0x%x\n", ha->host_no, __func__,
  1150. ha->firmware_state));
  1151. }
  1152. spin_lock_irqsave(&ha->hardware_lock, flags);
  1153. }
  1154. } else {
  1155. DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
  1156. "started - resetting\n", ha->host_no, __func__));
  1157. }
  1158. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1159. DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
  1160. ha->host_no, __func__, soft_reset, config_chip));
  1161. if (soft_reset) {
  1162. DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
  1163. __func__));
  1164. status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
  1165. * lock again, but ok */
  1166. if (status == QLA_ERROR) {
  1167. DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
  1168. ha->host_no, __func__));
  1169. ql4xxx_unlock_drvr(ha);
  1170. return QLA_ERROR;
  1171. }
  1172. config_chip = 1;
  1173. /* Reset clears the semaphore, so acquire again */
  1174. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  1175. return QLA_ERROR;
  1176. }
  1177. if (config_chip) {
  1178. if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
  1179. status = qla4xxx_start_firmware_from_flash(ha);
  1180. }
  1181. ql4xxx_unlock_drvr(ha);
  1182. if (status == QLA_SUCCESS) {
  1183. if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
  1184. qla4xxx_get_crash_record(ha);
  1185. } else {
  1186. DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
  1187. ha->host_no, __func__));
  1188. }
  1189. return status;
  1190. }
  1191. /**
  1192. * qla4xxx_initialize_adapter - initiailizes hba
  1193. * @ha: Pointer to host adapter structure.
  1194. * @renew_ddb_list: Indicates what to do with the adapter's ddb list
  1195. * after adapter recovery has completed.
  1196. * 0=preserve ddb list, 1=destroy and rebuild ddb list
  1197. *
  1198. * This routine parforms all of the steps necessary to initialize the adapter.
  1199. *
  1200. **/
  1201. int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
  1202. uint8_t renew_ddb_list)
  1203. {
  1204. int status = QLA_ERROR;
  1205. int8_t ip_address[IP_ADDR_LEN] = {0} ;
  1206. ha->eeprom_cmd_data = 0;
  1207. ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  1208. ha->isp_ops->pci_config(ha);
  1209. ha->isp_ops->disable_intrs(ha);
  1210. /* Initialize the Host adapter request/response queues and firmware */
  1211. if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
  1212. goto exit_init_hba;
  1213. if (qla4xxx_get_fw_version(ha) == QLA_ERROR)
  1214. goto exit_init_hba;
  1215. if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
  1216. goto exit_init_hba;
  1217. if (qla4xxx_init_local_data(ha) == QLA_ERROR)
  1218. goto exit_init_hba;
  1219. status = qla4xxx_init_firmware(ha);
  1220. if (status == QLA_ERROR)
  1221. goto exit_init_hba;
  1222. /*
  1223. * FW is waiting to get an IP address from DHCP server: Skip building
  1224. * the ddb_list and wait for DHCP lease acquired aen to come in
  1225. * followed by 0x8014 aen" to trigger the tgt discovery process.
  1226. */
  1227. if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
  1228. goto exit_init_online;
  1229. /* Skip device discovery if ip and subnet is zero */
  1230. if (memcmp(ha->ip_address, ip_address, IP_ADDR_LEN) == 0 ||
  1231. memcmp(ha->subnet_mask, ip_address, IP_ADDR_LEN) == 0)
  1232. goto exit_init_online;
  1233. if (renew_ddb_list == PRESERVE_DDB_LIST) {
  1234. /*
  1235. * We want to preserve lun states (i.e. suspended, etc.)
  1236. * for recovery initiated by the driver. So just update
  1237. * the device states for the existing ddb_list.
  1238. */
  1239. qla4xxx_reinitialize_ddb_list(ha);
  1240. } else if (renew_ddb_list == REBUILD_DDB_LIST) {
  1241. /*
  1242. * We want to build the ddb_list from scratch during
  1243. * driver initialization and recovery initiated by the
  1244. * INT_HBA_RESET IOCTL.
  1245. */
  1246. status = qla4xxx_initialize_ddb_list(ha);
  1247. if (status == QLA_ERROR) {
  1248. DEBUG2(printk("%s(%ld) Error occurred during build"
  1249. "ddb list\n", __func__, ha->host_no));
  1250. goto exit_init_hba;
  1251. }
  1252. }
  1253. if (!ha->tot_ddbs) {
  1254. DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
  1255. "present in Firmware device database\n",
  1256. ha->host_no));
  1257. }
  1258. exit_init_online:
  1259. set_bit(AF_ONLINE, &ha->flags);
  1260. exit_init_hba:
  1261. DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
  1262. status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
  1263. return status;
  1264. }
  1265. /**
  1266. * qla4xxx_add_device_dynamically - ddb addition due to an AEN
  1267. * @ha: Pointer to host adapter structure.
  1268. * @fw_ddb_index: Firmware's device database index
  1269. *
  1270. * This routine processes adds a device as a result of an 8014h AEN.
  1271. **/
  1272. static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
  1273. uint32_t fw_ddb_index)
  1274. {
  1275. struct ddb_entry * ddb_entry;
  1276. uint32_t new_tgt;
  1277. /* First allocate a device structure */
  1278. ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
  1279. if (ddb_entry == NULL) {
  1280. DEBUG2(printk(KERN_WARNING
  1281. "scsi%ld: Unable to allocate memory to add "
  1282. "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
  1283. return;
  1284. }
  1285. if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
  1286. /* Target has been bound to a new fw_ddb_index */
  1287. qla4xxx_free_ddb(ha, ddb_entry);
  1288. ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
  1289. if (ddb_entry == NULL) {
  1290. DEBUG2(printk(KERN_WARNING
  1291. "scsi%ld: Unable to allocate memory"
  1292. " to add fw_ddb_index %d\n",
  1293. ha->host_no, fw_ddb_index));
  1294. return;
  1295. }
  1296. }
  1297. if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
  1298. QLA_ERROR) {
  1299. ha->fw_ddb_index_map[fw_ddb_index] =
  1300. (struct ddb_entry *)INVALID_ENTRY;
  1301. DEBUG2(printk(KERN_WARNING
  1302. "scsi%ld: failed to add new device at index "
  1303. "[%d]\n Unable to retrieve fw ddb entry\n",
  1304. ha->host_no, fw_ddb_index));
  1305. qla4xxx_free_ddb(ha, ddb_entry);
  1306. return;
  1307. }
  1308. if (qla4xxx_add_sess(ddb_entry)) {
  1309. DEBUG2(printk(KERN_WARNING
  1310. "scsi%ld: failed to add new device at index "
  1311. "[%d]\n Unable to add connection and session\n",
  1312. ha->host_no, fw_ddb_index));
  1313. qla4xxx_free_ddb(ha, ddb_entry);
  1314. }
  1315. }
  1316. /**
  1317. * qla4xxx_process_ddb_changed - process ddb state change
  1318. * @ha - Pointer to host adapter structure.
  1319. * @fw_ddb_index - Firmware's device database index
  1320. * @state - Device state
  1321. *
  1322. * This routine processes a Decive Database Changed AEN Event.
  1323. **/
  1324. int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
  1325. uint32_t state, uint32_t conn_err)
  1326. {
  1327. struct ddb_entry * ddb_entry;
  1328. uint32_t old_fw_ddb_device_state;
  1329. /* check for out of range index */
  1330. if (fw_ddb_index >= MAX_DDB_ENTRIES)
  1331. return QLA_ERROR;
  1332. /* Get the corresponging ddb entry */
  1333. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
  1334. /* Device does not currently exist in our database. */
  1335. if (ddb_entry == NULL) {
  1336. if (state == DDB_DS_SESSION_ACTIVE)
  1337. qla4xxx_add_device_dynamically(ha, fw_ddb_index);
  1338. return QLA_SUCCESS;
  1339. }
  1340. /* Device already exists in our database. */
  1341. old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
  1342. DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
  1343. "index [%d]\n", ha->host_no, __func__,
  1344. ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
  1345. if (old_fw_ddb_device_state == state &&
  1346. state == DDB_DS_SESSION_ACTIVE) {
  1347. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  1348. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  1349. iscsi_unblock_session(ddb_entry->sess);
  1350. }
  1351. return QLA_SUCCESS;
  1352. }
  1353. ddb_entry->fw_ddb_device_state = state;
  1354. /* Device is back online. */
  1355. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  1356. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  1357. atomic_set(&ddb_entry->port_down_timer,
  1358. ha->port_down_retry_count);
  1359. atomic_set(&ddb_entry->relogin_retry_count, 0);
  1360. atomic_set(&ddb_entry->relogin_timer, 0);
  1361. clear_bit(DF_RELOGIN, &ddb_entry->flags);
  1362. clear_bit(DF_NO_RELOGIN, &ddb_entry->flags);
  1363. iscsi_unblock_session(ddb_entry->sess);
  1364. iscsi_session_event(ddb_entry->sess,
  1365. ISCSI_KEVENT_CREATE_SESSION);
  1366. /*
  1367. * Change the lun state to READY in case the lun TIMEOUT before
  1368. * the device came back.
  1369. */
  1370. } else {
  1371. /* Device went away, mark device missing */
  1372. if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE) {
  1373. DEBUG2(ql4_printk(KERN_INFO, ha, "%s mark missing "
  1374. "ddb_entry 0x%p sess 0x%p conn 0x%p\n",
  1375. __func__, ddb_entry,
  1376. ddb_entry->sess, ddb_entry->conn));
  1377. qla4xxx_mark_device_missing(ha, ddb_entry);
  1378. }
  1379. /*
  1380. * Relogin if device state changed to a not active state.
  1381. * However, do not relogin if a RELOGIN is in process, or
  1382. * we are not allowed to relogin to this DDB.
  1383. */
  1384. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
  1385. !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1386. !test_bit(DF_NO_RELOGIN, &ddb_entry->flags) &&
  1387. qla4_is_relogin_allowed(ha, conn_err)) {
  1388. /*
  1389. * This triggers a relogin. After the relogin_timer
  1390. * expires, the relogin gets scheduled. We must wait a
  1391. * minimum amount of time since receiving an 0x8014 AEN
  1392. * with failed device_state or a logout response before
  1393. * we can issue another relogin.
  1394. */
  1395. /* Firmware pads this timeout: (time2wait +1).
  1396. * Driver retry to login should be longer than F/W.
  1397. * Otherwise F/W will fail
  1398. * set_ddb() mbx cmd with 0x4005 since it still
  1399. * counting down its time2wait.
  1400. */
  1401. atomic_set(&ddb_entry->relogin_timer, 0);
  1402. atomic_set(&ddb_entry->retry_relogin_timer,
  1403. ddb_entry->default_time2wait + 4);
  1404. DEBUG(printk("scsi%ld: %s: ddb[%d] "
  1405. "initiate relogin after %d seconds\n",
  1406. ha->host_no, __func__,
  1407. ddb_entry->fw_ddb_index,
  1408. ddb_entry->default_time2wait + 4));
  1409. } else {
  1410. DEBUG(printk("scsi%ld: %s: ddb[%d] "
  1411. "relogin not initiated, state = %d, "
  1412. "ddb_entry->flags = 0x%lx\n",
  1413. ha->host_no, __func__,
  1414. ddb_entry->fw_ddb_index,
  1415. ddb_entry->fw_ddb_device_state,
  1416. ddb_entry->flags));
  1417. }
  1418. }
  1419. return QLA_SUCCESS;
  1420. }