ql4_init.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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 void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
  13. {
  14. uint32_t value;
  15. uint8_t func_number;
  16. unsigned long flags;
  17. /* Get the function number */
  18. spin_lock_irqsave(&ha->hardware_lock, flags);
  19. value = readw(&ha->reg->ctrl_status);
  20. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  21. func_number = (uint8_t) ((value >> 4) & 0x30);
  22. switch (value & ISP_CONTROL_FN_MASK) {
  23. case ISP_CONTROL_FN0_SCSI:
  24. ha->mac_index = 1;
  25. break;
  26. case ISP_CONTROL_FN1_SCSI:
  27. ha->mac_index = 3;
  28. break;
  29. default:
  30. DEBUG2(printk("scsi%ld: %s: Invalid function number, "
  31. "ispControlStatus = 0x%x\n", ha->host_no,
  32. __func__, value));
  33. break;
  34. }
  35. DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
  36. ha->mac_index));
  37. }
  38. /**
  39. * qla4xxx_free_ddb - deallocate ddb
  40. * @ha: pointer to host adapter structure.
  41. * @ddb_entry: pointer to device database entry
  42. *
  43. * This routine marks a DDB entry INVALID
  44. **/
  45. void qla4xxx_free_ddb(struct scsi_qla_host *ha,
  46. struct ddb_entry *ddb_entry)
  47. {
  48. /* Remove device pointer from index mapping arrays */
  49. ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
  50. (struct ddb_entry *) INVALID_ENTRY;
  51. ha->tot_ddbs--;
  52. }
  53. /**
  54. * qla4xxx_init_response_q_entries() - Initializes response queue entries.
  55. * @ha: HA context
  56. *
  57. * Beginning of request ring has initialization control block already built
  58. * by nvram config routine.
  59. **/
  60. static void qla4xxx_init_response_q_entries(struct scsi_qla_host *ha)
  61. {
  62. uint16_t cnt;
  63. struct response *pkt;
  64. pkt = (struct response *)ha->response_ptr;
  65. for (cnt = 0; cnt < RESPONSE_QUEUE_DEPTH; cnt++) {
  66. pkt->signature = RESPONSE_PROCESSED;
  67. pkt++;
  68. }
  69. }
  70. /**
  71. * qla4xxx_init_rings - initialize hw queues
  72. * @ha: pointer to host adapter structure.
  73. *
  74. * This routine initializes the internal queues for the specified adapter.
  75. * The QLA4010 requires us to restart the queues at index 0.
  76. * The QLA4000 doesn't care, so just default to QLA4010's requirement.
  77. **/
  78. int qla4xxx_init_rings(struct scsi_qla_host *ha)
  79. {
  80. unsigned long flags = 0;
  81. /* Initialize request queue. */
  82. spin_lock_irqsave(&ha->hardware_lock, flags);
  83. ha->request_out = 0;
  84. ha->request_in = 0;
  85. ha->request_ptr = &ha->request_ring[ha->request_in];
  86. ha->req_q_count = REQUEST_QUEUE_DEPTH;
  87. /* Initialize response queue. */
  88. ha->response_in = 0;
  89. ha->response_out = 0;
  90. ha->response_ptr = &ha->response_ring[ha->response_out];
  91. if (is_qla8022(ha)) {
  92. writel(0,
  93. (unsigned long __iomem *)&ha->qla4_8xxx_reg->req_q_out);
  94. writel(0,
  95. (unsigned long __iomem *)&ha->qla4_8xxx_reg->rsp_q_in);
  96. writel(0,
  97. (unsigned long __iomem *)&ha->qla4_8xxx_reg->rsp_q_out);
  98. } else {
  99. /*
  100. * Initialize DMA Shadow registers. The firmware is really
  101. * supposed to take care of this, but on some uniprocessor
  102. * systems, the shadow registers aren't cleared-- causing
  103. * the interrupt_handler to think there are responses to be
  104. * processed when there aren't.
  105. */
  106. ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
  107. ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
  108. wmb();
  109. writel(0, &ha->reg->req_q_in);
  110. writel(0, &ha->reg->rsp_q_out);
  111. readl(&ha->reg->rsp_q_out);
  112. }
  113. qla4xxx_init_response_q_entries(ha);
  114. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  115. return QLA_SUCCESS;
  116. }
  117. /**
  118. * qla4xxx_get_sys_info - validate adapter MAC address(es)
  119. * @ha: pointer to host adapter structure.
  120. *
  121. **/
  122. int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
  123. {
  124. struct flash_sys_info *sys_info;
  125. dma_addr_t sys_info_dma;
  126. int status = QLA_ERROR;
  127. sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
  128. &sys_info_dma, GFP_KERNEL);
  129. if (sys_info == NULL) {
  130. DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
  131. ha->host_no, __func__));
  132. goto exit_get_sys_info_no_free;
  133. }
  134. memset(sys_info, 0, sizeof(*sys_info));
  135. /* Get flash sys info */
  136. if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
  137. sizeof(*sys_info)) != QLA_SUCCESS) {
  138. DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
  139. "failed\n", ha->host_no, __func__));
  140. goto exit_get_sys_info;
  141. }
  142. /* Save M.A.C. address & serial_number */
  143. memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
  144. min(sizeof(ha->my_mac),
  145. sizeof(sys_info->physAddr[0].address)));
  146. memcpy(ha->serial_number, &sys_info->acSerialNumber,
  147. min(sizeof(ha->serial_number),
  148. sizeof(sys_info->acSerialNumber)));
  149. status = QLA_SUCCESS;
  150. exit_get_sys_info:
  151. dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
  152. sys_info_dma);
  153. exit_get_sys_info_no_free:
  154. return status;
  155. }
  156. /**
  157. * qla4xxx_init_local_data - initialize adapter specific local data
  158. * @ha: pointer to host adapter structure.
  159. *
  160. **/
  161. static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
  162. {
  163. /* Initialize aen queue */
  164. ha->aen_q_count = MAX_AEN_ENTRIES;
  165. return qla4xxx_get_firmware_status(ha);
  166. }
  167. static uint8_t
  168. qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
  169. {
  170. uint8_t ipv4_wait = 0;
  171. uint8_t ipv6_wait = 0;
  172. int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
  173. /* If both IPv4 & IPv6 are enabled, possibly only one
  174. * IP address may be acquired, so check to see if we
  175. * need to wait for another */
  176. if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
  177. if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
  178. ((ha->addl_fw_state &
  179. FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
  180. ipv4_wait = 1;
  181. }
  182. if (((ha->ip_config.ipv6_addl_options &
  183. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
  184. ((ha->ip_config.ipv6_link_local_state ==
  185. IP_ADDRSTATE_ACQUIRING) ||
  186. (ha->ip_config.ipv6_addr0_state ==
  187. IP_ADDRSTATE_ACQUIRING) ||
  188. (ha->ip_config.ipv6_addr1_state ==
  189. IP_ADDRSTATE_ACQUIRING))) {
  190. ipv6_wait = 1;
  191. if ((ha->ip_config.ipv6_link_local_state ==
  192. IP_ADDRSTATE_PREFERRED) ||
  193. (ha->ip_config.ipv6_addr0_state ==
  194. IP_ADDRSTATE_PREFERRED) ||
  195. (ha->ip_config.ipv6_addr1_state ==
  196. IP_ADDRSTATE_PREFERRED)) {
  197. DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
  198. "Preferred IP configured."
  199. " Don't wait!\n", ha->host_no,
  200. __func__));
  201. ipv6_wait = 0;
  202. }
  203. if (memcmp(&ha->ip_config.ipv6_default_router_addr,
  204. ip_address, IPv6_ADDR_LEN) == 0) {
  205. DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
  206. "No Router configured. "
  207. "Don't wait!\n", ha->host_no,
  208. __func__));
  209. ipv6_wait = 0;
  210. }
  211. if ((ha->ip_config.ipv6_default_router_state ==
  212. IPV6_RTRSTATE_MANUAL) &&
  213. (ha->ip_config.ipv6_link_local_state ==
  214. IP_ADDRSTATE_TENTATIVE) &&
  215. (memcmp(&ha->ip_config.ipv6_link_local_addr,
  216. &ha->ip_config.ipv6_default_router_addr, 4) ==
  217. 0)) {
  218. DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
  219. "IP configured. Don't wait!\n",
  220. ha->host_no, __func__));
  221. ipv6_wait = 0;
  222. }
  223. }
  224. if (ipv4_wait || ipv6_wait) {
  225. DEBUG2(printk("scsi%ld: %s: Wait for additional "
  226. "IP(s) \"", ha->host_no, __func__));
  227. if (ipv4_wait)
  228. DEBUG2(printk("IPv4 "));
  229. if (ha->ip_config.ipv6_link_local_state ==
  230. IP_ADDRSTATE_ACQUIRING)
  231. DEBUG2(printk("IPv6LinkLocal "));
  232. if (ha->ip_config.ipv6_addr0_state ==
  233. IP_ADDRSTATE_ACQUIRING)
  234. DEBUG2(printk("IPv6Addr0 "));
  235. if (ha->ip_config.ipv6_addr1_state ==
  236. IP_ADDRSTATE_ACQUIRING)
  237. DEBUG2(printk("IPv6Addr1 "));
  238. DEBUG2(printk("\"\n"));
  239. }
  240. }
  241. return ipv4_wait|ipv6_wait;
  242. }
  243. static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
  244. {
  245. uint32_t timeout_count;
  246. int ready = 0;
  247. DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
  248. for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
  249. timeout_count--) {
  250. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  251. qla4xxx_get_dhcp_ip_address(ha);
  252. /* Get firmware state. */
  253. if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
  254. DEBUG2(printk("scsi%ld: %s: unable to get firmware "
  255. "state\n", ha->host_no, __func__));
  256. break;
  257. }
  258. if (ha->firmware_state & FW_STATE_ERROR) {
  259. DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
  260. " occurred\n", ha->host_no, __func__));
  261. break;
  262. }
  263. if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
  264. /*
  265. * The firmware has not yet been issued an Initialize
  266. * Firmware command, so issue it now.
  267. */
  268. if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
  269. break;
  270. /* Go back and test for ready state - no wait. */
  271. continue;
  272. }
  273. if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
  274. DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
  275. "AUTOCONNECT in progress\n",
  276. ha->host_no, __func__));
  277. }
  278. if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
  279. DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
  280. " CONFIGURING IP\n",
  281. ha->host_no, __func__));
  282. /*
  283. * Check for link state after 15 secs and if link is
  284. * still DOWN then, cable is unplugged. Ignore "DHCP
  285. * in Progress/CONFIGURING IP" bit to check if firmware
  286. * is in ready state or not after 15 secs.
  287. * This is applicable for both 2.x & 3.x firmware
  288. */
  289. if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
  290. if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
  291. DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
  292. " LINK UP (Cable plugged)\n",
  293. ha->host_no, __func__));
  294. } else if (ha->firmware_state &
  295. (FW_STATE_CONFIGURING_IP |
  296. FW_STATE_READY)) {
  297. DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
  298. "LINK DOWN (Cable unplugged)\n",
  299. ha->host_no, __func__));
  300. ha->firmware_state = FW_STATE_READY;
  301. }
  302. }
  303. }
  304. if (ha->firmware_state == FW_STATE_READY) {
  305. /* If DHCP IP Addr is available, retrieve it now. */
  306. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
  307. &ha->dpc_flags))
  308. qla4xxx_get_dhcp_ip_address(ha);
  309. if (!qla4xxx_wait_for_ip_config(ha) ||
  310. timeout_count == 1) {
  311. DEBUG2(ql4_printk(KERN_INFO, ha,
  312. "Firmware Ready..\n"));
  313. /* The firmware is ready to process SCSI
  314. commands. */
  315. DEBUG2(ql4_printk(KERN_INFO, ha,
  316. "scsi%ld: %s: MEDIA TYPE"
  317. " - %s\n", ha->host_no,
  318. __func__, (ha->addl_fw_state &
  319. FW_ADDSTATE_OPTICAL_MEDIA)
  320. != 0 ? "OPTICAL" : "COPPER"));
  321. DEBUG2(ql4_printk(KERN_INFO, ha,
  322. "scsi%ld: %s: DHCPv4 STATE"
  323. " Enabled %s\n", ha->host_no,
  324. __func__, (ha->addl_fw_state &
  325. FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
  326. "YES" : "NO"));
  327. DEBUG2(ql4_printk(KERN_INFO, ha,
  328. "scsi%ld: %s: LINK %s\n",
  329. ha->host_no, __func__,
  330. (ha->addl_fw_state &
  331. FW_ADDSTATE_LINK_UP) != 0 ?
  332. "UP" : "DOWN"));
  333. DEBUG2(ql4_printk(KERN_INFO, ha,
  334. "scsi%ld: %s: iSNS Service "
  335. "Started %s\n",
  336. ha->host_no, __func__,
  337. (ha->addl_fw_state &
  338. FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
  339. "YES" : "NO"));
  340. ready = 1;
  341. break;
  342. }
  343. }
  344. DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
  345. "seconds expired= %d\n", ha->host_no, __func__,
  346. ha->firmware_state, ha->addl_fw_state,
  347. timeout_count));
  348. if (is_qla4032(ha) &&
  349. !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
  350. (timeout_count < ADAPTER_INIT_TOV - 5)) {
  351. break;
  352. }
  353. msleep(1000);
  354. } /* end of for */
  355. if (timeout_count <= 0)
  356. DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
  357. ha->host_no, __func__));
  358. if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
  359. DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
  360. "it's waiting to configure an IP address\n",
  361. ha->host_no, __func__));
  362. ready = 1;
  363. } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
  364. DEBUG2(printk("scsi%ld: %s: FW initialized, but "
  365. "auto-discovery still in process\n",
  366. ha->host_no, __func__));
  367. ready = 1;
  368. }
  369. return ready;
  370. }
  371. /**
  372. * qla4xxx_init_firmware - initializes the firmware.
  373. * @ha: pointer to host adapter structure.
  374. *
  375. **/
  376. static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
  377. {
  378. int status = QLA_ERROR;
  379. if (is_aer_supported(ha) &&
  380. test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  381. return status;
  382. /* For 82xx, stop firmware before initializing because if BIOS
  383. * has previously initialized firmware, then driver's initialize
  384. * firmware will fail. */
  385. if (is_qla8022(ha))
  386. qla4_8xxx_stop_firmware(ha);
  387. ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
  388. if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
  389. DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
  390. "control block\n", ha->host_no, __func__));
  391. return status;
  392. }
  393. if (!qla4xxx_fw_ready(ha))
  394. return status;
  395. return qla4xxx_get_firmware_status(ha);
  396. }
  397. static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
  398. {
  399. unsigned long flags;
  400. union external_hw_config_reg extHwConfig;
  401. DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
  402. __func__));
  403. if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
  404. return QLA_ERROR;
  405. if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
  406. ql4xxx_unlock_flash(ha);
  407. return QLA_ERROR;
  408. }
  409. /* Get EEPRom Parameters from NVRAM and validate */
  410. ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
  411. if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
  412. spin_lock_irqsave(&ha->hardware_lock, flags);
  413. extHwConfig.Asuint32_t =
  414. rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
  415. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  416. } else {
  417. ql4_printk(KERN_WARNING, ha,
  418. "scsi%ld: %s: EEProm checksum invalid. "
  419. "Please update your EEPROM\n", ha->host_no,
  420. __func__);
  421. /* Attempt to set defaults */
  422. if (is_qla4010(ha))
  423. extHwConfig.Asuint32_t = 0x1912;
  424. else if (is_qla4022(ha) | is_qla4032(ha))
  425. extHwConfig.Asuint32_t = 0x0023;
  426. else
  427. return QLA_ERROR;
  428. }
  429. DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
  430. ha->host_no, __func__, extHwConfig.Asuint32_t));
  431. spin_lock_irqsave(&ha->hardware_lock, flags);
  432. writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
  433. readl(isp_ext_hw_conf(ha));
  434. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  435. ql4xxx_unlock_nvram(ha);
  436. ql4xxx_unlock_flash(ha);
  437. return QLA_SUCCESS;
  438. }
  439. /**
  440. * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
  441. * @ha: HA context
  442. */
  443. void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
  444. {
  445. pci_set_master(ha->pdev);
  446. }
  447. void qla4xxx_pci_config(struct scsi_qla_host *ha)
  448. {
  449. uint16_t w;
  450. int status;
  451. ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  452. pci_set_master(ha->pdev);
  453. status = pci_set_mwi(ha->pdev);
  454. /*
  455. * We want to respect framework's setting of PCI configuration space
  456. * command register and also want to make sure that all bits of
  457. * interest to us are properly set in command register.
  458. */
  459. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  460. w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  461. w &= ~PCI_COMMAND_INTX_DISABLE;
  462. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  463. }
  464. static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
  465. {
  466. int status = QLA_ERROR;
  467. unsigned long max_wait_time;
  468. unsigned long flags;
  469. uint32_t mbox_status;
  470. ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
  471. /*
  472. * Start firmware from flash ROM
  473. *
  474. * WORKAROUND: Stuff a non-constant value that the firmware can
  475. * use as a seed for a random number generator in MB7 prior to
  476. * setting BOOT_ENABLE. Fixes problem where the TCP
  477. * connections use the same TCP ports after each reboot,
  478. * causing some connections to not get re-established.
  479. */
  480. DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
  481. ha->host_no, __func__));
  482. spin_lock_irqsave(&ha->hardware_lock, flags);
  483. writel(jiffies, &ha->reg->mailbox[7]);
  484. if (is_qla4022(ha) | is_qla4032(ha))
  485. writel(set_rmask(NVR_WRITE_ENABLE),
  486. &ha->reg->u1.isp4022.nvram);
  487. writel(2, &ha->reg->mailbox[6]);
  488. readl(&ha->reg->mailbox[6]);
  489. writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
  490. readl(&ha->reg->ctrl_status);
  491. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  492. /* Wait for firmware to come UP. */
  493. DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
  494. "boot firmware to complete...\n",
  495. ha->host_no, __func__, FIRMWARE_UP_TOV));
  496. max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
  497. do {
  498. uint32_t ctrl_status;
  499. spin_lock_irqsave(&ha->hardware_lock, flags);
  500. ctrl_status = readw(&ha->reg->ctrl_status);
  501. mbox_status = readw(&ha->reg->mailbox[0]);
  502. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  503. if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
  504. break;
  505. if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
  506. break;
  507. DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
  508. "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
  509. ha->host_no, __func__, ctrl_status, max_wait_time));
  510. msleep_interruptible(250);
  511. } while (!time_after_eq(jiffies, max_wait_time));
  512. if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
  513. DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
  514. ha->host_no, __func__));
  515. spin_lock_irqsave(&ha->hardware_lock, flags);
  516. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  517. &ha->reg->ctrl_status);
  518. readl(&ha->reg->ctrl_status);
  519. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  520. status = QLA_SUCCESS;
  521. } else {
  522. printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
  523. "- mbox status 0x%x\n", ha->host_no, __func__,
  524. mbox_status);
  525. status = QLA_ERROR;
  526. }
  527. return status;
  528. }
  529. int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
  530. {
  531. #define QL4_LOCK_DRVR_WAIT 60
  532. #define QL4_LOCK_DRVR_SLEEP 1
  533. int drvr_wait = QL4_LOCK_DRVR_WAIT;
  534. while (drvr_wait) {
  535. if (ql4xxx_lock_drvr(a) == 0) {
  536. ssleep(QL4_LOCK_DRVR_SLEEP);
  537. if (drvr_wait) {
  538. DEBUG2(printk("scsi%ld: %s: Waiting for "
  539. "Global Init Semaphore(%d)...\n",
  540. a->host_no,
  541. __func__, drvr_wait));
  542. }
  543. drvr_wait -= QL4_LOCK_DRVR_SLEEP;
  544. } else {
  545. DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
  546. "acquired\n", a->host_no, __func__));
  547. return QLA_SUCCESS;
  548. }
  549. }
  550. return QLA_ERROR;
  551. }
  552. /**
  553. * qla4xxx_start_firmware - starts qla4xxx firmware
  554. * @ha: Pointer to host adapter structure.
  555. *
  556. * This routine performs the necessary steps to start the firmware for
  557. * the QLA4010 adapter.
  558. **/
  559. int qla4xxx_start_firmware(struct scsi_qla_host *ha)
  560. {
  561. unsigned long flags = 0;
  562. uint32_t mbox_status;
  563. int status = QLA_ERROR;
  564. int soft_reset = 1;
  565. int config_chip = 0;
  566. if (is_qla4022(ha) | is_qla4032(ha))
  567. ql4xxx_set_mac_number(ha);
  568. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  569. return QLA_ERROR;
  570. spin_lock_irqsave(&ha->hardware_lock, flags);
  571. DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
  572. __func__, readw(isp_port_ctrl(ha))));
  573. DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
  574. __func__, readw(isp_port_status(ha))));
  575. /* Is Hardware already initialized? */
  576. if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
  577. DEBUG(printk("scsi%ld: %s: Hardware has already been "
  578. "initialized\n", ha->host_no, __func__));
  579. /* Receive firmware boot acknowledgement */
  580. mbox_status = readw(&ha->reg->mailbox[0]);
  581. DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
  582. "0x%x\n", ha->host_no, __func__, mbox_status));
  583. /* Is firmware already booted? */
  584. if (mbox_status == 0) {
  585. /* F/W not running, must be config by net driver */
  586. config_chip = 1;
  587. soft_reset = 0;
  588. } else {
  589. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  590. &ha->reg->ctrl_status);
  591. readl(&ha->reg->ctrl_status);
  592. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  593. if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
  594. DEBUG2(printk("scsi%ld: %s: Get firmware "
  595. "state -- state = 0x%x\n",
  596. ha->host_no,
  597. __func__, ha->firmware_state));
  598. /* F/W is running */
  599. if (ha->firmware_state &
  600. FW_STATE_CONFIG_WAIT) {
  601. DEBUG2(printk("scsi%ld: %s: Firmware "
  602. "in known state -- "
  603. "config and "
  604. "boot, state = 0x%x\n",
  605. ha->host_no, __func__,
  606. ha->firmware_state));
  607. config_chip = 1;
  608. soft_reset = 0;
  609. }
  610. } else {
  611. DEBUG2(printk("scsi%ld: %s: Firmware in "
  612. "unknown state -- resetting,"
  613. " state = "
  614. "0x%x\n", ha->host_no, __func__,
  615. ha->firmware_state));
  616. }
  617. spin_lock_irqsave(&ha->hardware_lock, flags);
  618. }
  619. } else {
  620. DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
  621. "started - resetting\n", ha->host_no, __func__));
  622. }
  623. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  624. DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
  625. ha->host_no, __func__, soft_reset, config_chip));
  626. if (soft_reset) {
  627. DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
  628. __func__));
  629. status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
  630. * lock again, but ok */
  631. if (status == QLA_ERROR) {
  632. DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
  633. ha->host_no, __func__));
  634. ql4xxx_unlock_drvr(ha);
  635. return QLA_ERROR;
  636. }
  637. config_chip = 1;
  638. /* Reset clears the semaphore, so acquire again */
  639. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  640. return QLA_ERROR;
  641. }
  642. if (config_chip) {
  643. if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
  644. status = qla4xxx_start_firmware_from_flash(ha);
  645. }
  646. ql4xxx_unlock_drvr(ha);
  647. if (status == QLA_SUCCESS) {
  648. if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
  649. qla4xxx_get_crash_record(ha);
  650. } else {
  651. DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
  652. ha->host_no, __func__));
  653. }
  654. return status;
  655. }
  656. /**
  657. * qla4xxx_initialize_adapter - initiailizes hba
  658. * @ha: Pointer to host adapter structure.
  659. *
  660. * This routine parforms all of the steps necessary to initialize the adapter.
  661. *
  662. **/
  663. int qla4xxx_initialize_adapter(struct scsi_qla_host *ha)
  664. {
  665. int status = QLA_ERROR;
  666. ha->eeprom_cmd_data = 0;
  667. ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  668. ha->isp_ops->pci_config(ha);
  669. ha->isp_ops->disable_intrs(ha);
  670. /* Initialize the Host adapter request/response queues and firmware */
  671. if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
  672. goto exit_init_hba;
  673. if (qla4xxx_about_firmware(ha) == QLA_ERROR)
  674. goto exit_init_hba;
  675. if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
  676. goto exit_init_hba;
  677. if (qla4xxx_init_local_data(ha) == QLA_ERROR)
  678. goto exit_init_hba;
  679. status = qla4xxx_init_firmware(ha);
  680. if (status == QLA_ERROR)
  681. goto exit_init_hba;
  682. set_bit(AF_ONLINE, &ha->flags);
  683. exit_init_hba:
  684. if (is_qla8022(ha) && (status == QLA_ERROR)) {
  685. /* Since interrupts are registered in start_firmware for
  686. * 82xx, release them here if initialize_adapter fails */
  687. qla4xxx_free_irqs(ha);
  688. }
  689. DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
  690. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  691. return status;
  692. }
  693. /**
  694. * qla4xxx_process_ddb_changed - process ddb state change
  695. * @ha - Pointer to host adapter structure.
  696. * @fw_ddb_index - Firmware's device database index
  697. * @state - Device state
  698. *
  699. * This routine processes a Decive Database Changed AEN Event.
  700. **/
  701. int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
  702. uint32_t state, uint32_t conn_err)
  703. {
  704. struct ddb_entry * ddb_entry;
  705. uint32_t old_fw_ddb_device_state;
  706. int status = QLA_ERROR;
  707. /* check for out of range index */
  708. if (fw_ddb_index >= MAX_DDB_ENTRIES)
  709. goto exit_ddb_event;
  710. /* Get the corresponging ddb entry */
  711. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
  712. /* Device does not currently exist in our database. */
  713. if (ddb_entry == NULL) {
  714. ql4_printk(KERN_ERR, ha, "%s: No ddb_entry at FW index [%d]\n",
  715. __func__, fw_ddb_index);
  716. goto exit_ddb_event;
  717. }
  718. old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
  719. DEBUG2(ql4_printk(KERN_INFO, ha,
  720. "%s: DDB - old state = 0x%x, new state = 0x%x for "
  721. "index [%d]\n", __func__,
  722. ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
  723. ddb_entry->fw_ddb_device_state = state;
  724. switch (old_fw_ddb_device_state) {
  725. case DDB_DS_LOGIN_IN_PROCESS:
  726. switch (state) {
  727. case DDB_DS_SESSION_ACTIVE:
  728. case DDB_DS_DISCOVERY:
  729. iscsi_conn_login_event(ddb_entry->conn,
  730. ISCSI_CONN_STATE_LOGGED_IN);
  731. qla4xxx_update_session_conn_param(ha, ddb_entry);
  732. status = QLA_SUCCESS;
  733. break;
  734. case DDB_DS_SESSION_FAILED:
  735. case DDB_DS_NO_CONNECTION_ACTIVE:
  736. iscsi_conn_login_event(ddb_entry->conn,
  737. ISCSI_CONN_STATE_FREE);
  738. status = QLA_SUCCESS;
  739. break;
  740. }
  741. break;
  742. case DDB_DS_SESSION_ACTIVE:
  743. if (state == DDB_DS_SESSION_FAILED) {
  744. /*
  745. * iscsi_session failure will cause userspace to
  746. * stop the connection which in turn would block the
  747. * iscsi_session and start relogin
  748. */
  749. iscsi_session_failure(ddb_entry->sess->dd_data,
  750. ISCSI_ERR_CONN_FAILED);
  751. status = QLA_SUCCESS;
  752. }
  753. break;
  754. default:
  755. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
  756. __func__));
  757. break;
  758. }
  759. exit_ddb_event:
  760. return status;
  761. }