ql4_init.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 void qla4xxx_set_model_info(struct scsi_qla_host *ha)
  398. {
  399. uint16_t board_id_string[8];
  400. int i;
  401. int size = sizeof(ha->nvram->isp4022.boardIdStr);
  402. int offset = offsetof(struct eeprom_data, isp4022.boardIdStr) / 2;
  403. for (i = 0; i < (size / 2) ; i++) {
  404. board_id_string[i] = rd_nvram_word(ha, offset);
  405. offset += 1;
  406. }
  407. memcpy(ha->model_name, board_id_string, size);
  408. }
  409. static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
  410. {
  411. unsigned long flags;
  412. union external_hw_config_reg extHwConfig;
  413. DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
  414. __func__));
  415. if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
  416. return QLA_ERROR;
  417. if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
  418. ql4xxx_unlock_flash(ha);
  419. return QLA_ERROR;
  420. }
  421. /* Get EEPRom Parameters from NVRAM and validate */
  422. ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
  423. if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
  424. spin_lock_irqsave(&ha->hardware_lock, flags);
  425. extHwConfig.Asuint32_t =
  426. rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
  427. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  428. } else {
  429. ql4_printk(KERN_WARNING, ha,
  430. "scsi%ld: %s: EEProm checksum invalid. "
  431. "Please update your EEPROM\n", ha->host_no,
  432. __func__);
  433. /* Attempt to set defaults */
  434. if (is_qla4010(ha))
  435. extHwConfig.Asuint32_t = 0x1912;
  436. else if (is_qla4022(ha) | is_qla4032(ha))
  437. extHwConfig.Asuint32_t = 0x0023;
  438. else
  439. return QLA_ERROR;
  440. }
  441. if (is_qla4022(ha) || is_qla4032(ha))
  442. qla4xxx_set_model_info(ha);
  443. else
  444. strcpy(ha->model_name, "QLA4010");
  445. DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
  446. ha->host_no, __func__, extHwConfig.Asuint32_t));
  447. spin_lock_irqsave(&ha->hardware_lock, flags);
  448. writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
  449. readl(isp_ext_hw_conf(ha));
  450. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  451. ql4xxx_unlock_nvram(ha);
  452. ql4xxx_unlock_flash(ha);
  453. return QLA_SUCCESS;
  454. }
  455. /**
  456. * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
  457. * @ha: HA context
  458. */
  459. void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
  460. {
  461. pci_set_master(ha->pdev);
  462. }
  463. void qla4xxx_pci_config(struct scsi_qla_host *ha)
  464. {
  465. uint16_t w;
  466. int status;
  467. ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  468. pci_set_master(ha->pdev);
  469. status = pci_set_mwi(ha->pdev);
  470. /*
  471. * We want to respect framework's setting of PCI configuration space
  472. * command register and also want to make sure that all bits of
  473. * interest to us are properly set in command register.
  474. */
  475. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  476. w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  477. w &= ~PCI_COMMAND_INTX_DISABLE;
  478. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  479. }
  480. static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
  481. {
  482. int status = QLA_ERROR;
  483. unsigned long max_wait_time;
  484. unsigned long flags;
  485. uint32_t mbox_status;
  486. ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
  487. /*
  488. * Start firmware from flash ROM
  489. *
  490. * WORKAROUND: Stuff a non-constant value that the firmware can
  491. * use as a seed for a random number generator in MB7 prior to
  492. * setting BOOT_ENABLE. Fixes problem where the TCP
  493. * connections use the same TCP ports after each reboot,
  494. * causing some connections to not get re-established.
  495. */
  496. DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
  497. ha->host_no, __func__));
  498. spin_lock_irqsave(&ha->hardware_lock, flags);
  499. writel(jiffies, &ha->reg->mailbox[7]);
  500. if (is_qla4022(ha) | is_qla4032(ha))
  501. writel(set_rmask(NVR_WRITE_ENABLE),
  502. &ha->reg->u1.isp4022.nvram);
  503. writel(2, &ha->reg->mailbox[6]);
  504. readl(&ha->reg->mailbox[6]);
  505. writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
  506. readl(&ha->reg->ctrl_status);
  507. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  508. /* Wait for firmware to come UP. */
  509. DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
  510. "boot firmware to complete...\n",
  511. ha->host_no, __func__, FIRMWARE_UP_TOV));
  512. max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
  513. do {
  514. uint32_t ctrl_status;
  515. spin_lock_irqsave(&ha->hardware_lock, flags);
  516. ctrl_status = readw(&ha->reg->ctrl_status);
  517. mbox_status = readw(&ha->reg->mailbox[0]);
  518. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  519. if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
  520. break;
  521. if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
  522. break;
  523. DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
  524. "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
  525. ha->host_no, __func__, ctrl_status, max_wait_time));
  526. msleep_interruptible(250);
  527. } while (!time_after_eq(jiffies, max_wait_time));
  528. if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
  529. DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
  530. ha->host_no, __func__));
  531. spin_lock_irqsave(&ha->hardware_lock, flags);
  532. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  533. &ha->reg->ctrl_status);
  534. readl(&ha->reg->ctrl_status);
  535. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  536. status = QLA_SUCCESS;
  537. } else {
  538. printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
  539. "- mbox status 0x%x\n", ha->host_no, __func__,
  540. mbox_status);
  541. status = QLA_ERROR;
  542. }
  543. return status;
  544. }
  545. int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
  546. {
  547. #define QL4_LOCK_DRVR_WAIT 60
  548. #define QL4_LOCK_DRVR_SLEEP 1
  549. int drvr_wait = QL4_LOCK_DRVR_WAIT;
  550. while (drvr_wait) {
  551. if (ql4xxx_lock_drvr(a) == 0) {
  552. ssleep(QL4_LOCK_DRVR_SLEEP);
  553. if (drvr_wait) {
  554. DEBUG2(printk("scsi%ld: %s: Waiting for "
  555. "Global Init Semaphore(%d)...\n",
  556. a->host_no,
  557. __func__, drvr_wait));
  558. }
  559. drvr_wait -= QL4_LOCK_DRVR_SLEEP;
  560. } else {
  561. DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
  562. "acquired\n", a->host_no, __func__));
  563. return QLA_SUCCESS;
  564. }
  565. }
  566. return QLA_ERROR;
  567. }
  568. /**
  569. * qla4xxx_start_firmware - starts qla4xxx firmware
  570. * @ha: Pointer to host adapter structure.
  571. *
  572. * This routine performs the necessary steps to start the firmware for
  573. * the QLA4010 adapter.
  574. **/
  575. int qla4xxx_start_firmware(struct scsi_qla_host *ha)
  576. {
  577. unsigned long flags = 0;
  578. uint32_t mbox_status;
  579. int status = QLA_ERROR;
  580. int soft_reset = 1;
  581. int config_chip = 0;
  582. if (is_qla4022(ha) | is_qla4032(ha))
  583. ql4xxx_set_mac_number(ha);
  584. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  585. return QLA_ERROR;
  586. spin_lock_irqsave(&ha->hardware_lock, flags);
  587. DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
  588. __func__, readw(isp_port_ctrl(ha))));
  589. DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
  590. __func__, readw(isp_port_status(ha))));
  591. /* Is Hardware already initialized? */
  592. if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
  593. DEBUG(printk("scsi%ld: %s: Hardware has already been "
  594. "initialized\n", ha->host_no, __func__));
  595. /* Receive firmware boot acknowledgement */
  596. mbox_status = readw(&ha->reg->mailbox[0]);
  597. DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
  598. "0x%x\n", ha->host_no, __func__, mbox_status));
  599. /* Is firmware already booted? */
  600. if (mbox_status == 0) {
  601. /* F/W not running, must be config by net driver */
  602. config_chip = 1;
  603. soft_reset = 0;
  604. } else {
  605. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  606. &ha->reg->ctrl_status);
  607. readl(&ha->reg->ctrl_status);
  608. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  609. if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
  610. DEBUG2(printk("scsi%ld: %s: Get firmware "
  611. "state -- state = 0x%x\n",
  612. ha->host_no,
  613. __func__, ha->firmware_state));
  614. /* F/W is running */
  615. if (ha->firmware_state &
  616. FW_STATE_CONFIG_WAIT) {
  617. DEBUG2(printk("scsi%ld: %s: Firmware "
  618. "in known state -- "
  619. "config and "
  620. "boot, state = 0x%x\n",
  621. ha->host_no, __func__,
  622. ha->firmware_state));
  623. config_chip = 1;
  624. soft_reset = 0;
  625. }
  626. } else {
  627. DEBUG2(printk("scsi%ld: %s: Firmware in "
  628. "unknown state -- resetting,"
  629. " state = "
  630. "0x%x\n", ha->host_no, __func__,
  631. ha->firmware_state));
  632. }
  633. spin_lock_irqsave(&ha->hardware_lock, flags);
  634. }
  635. } else {
  636. DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
  637. "started - resetting\n", ha->host_no, __func__));
  638. }
  639. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  640. DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
  641. ha->host_no, __func__, soft_reset, config_chip));
  642. if (soft_reset) {
  643. DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
  644. __func__));
  645. status = qla4xxx_soft_reset(ha); /* NOTE: acquires drvr
  646. * lock again, but ok */
  647. if (status == QLA_ERROR) {
  648. DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
  649. ha->host_no, __func__));
  650. ql4xxx_unlock_drvr(ha);
  651. return QLA_ERROR;
  652. }
  653. config_chip = 1;
  654. /* Reset clears the semaphore, so acquire again */
  655. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  656. return QLA_ERROR;
  657. }
  658. if (config_chip) {
  659. if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
  660. status = qla4xxx_start_firmware_from_flash(ha);
  661. }
  662. ql4xxx_unlock_drvr(ha);
  663. if (status == QLA_SUCCESS) {
  664. if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
  665. qla4xxx_get_crash_record(ha);
  666. } else {
  667. DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
  668. ha->host_no, __func__));
  669. }
  670. return status;
  671. }
  672. /**
  673. * qla4xxx_free_ddb_index - Free DDBs reserved by firmware
  674. * @ha: pointer to adapter structure
  675. *
  676. * Since firmware is not running in autoconnect mode the DDB indices should
  677. * be freed so that when login happens from user space there are free DDB
  678. * indices available.
  679. **/
  680. static void qla4xxx_free_ddb_index(struct scsi_qla_host *ha)
  681. {
  682. int max_ddbs;
  683. int ret;
  684. uint32_t idx = 0, next_idx = 0;
  685. uint32_t state = 0, conn_err = 0;
  686. max_ddbs = is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
  687. MAX_DEV_DB_ENTRIES;
  688. for (idx = 0; idx < max_ddbs; idx = next_idx) {
  689. ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
  690. &next_idx, &state, &conn_err,
  691. NULL, NULL);
  692. if (ret == QLA_ERROR)
  693. continue;
  694. if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
  695. state == DDB_DS_SESSION_FAILED) {
  696. DEBUG2(ql4_printk(KERN_INFO, ha,
  697. "Freeing DDB index = 0x%x\n", idx));
  698. ret = qla4xxx_clear_ddb_entry(ha, idx);
  699. if (ret == QLA_ERROR)
  700. ql4_printk(KERN_ERR, ha,
  701. "Unable to clear DDB index = "
  702. "0x%x\n", idx);
  703. }
  704. if (next_idx == 0)
  705. break;
  706. }
  707. }
  708. /**
  709. * qla4xxx_initialize_adapter - initiailizes hba
  710. * @ha: Pointer to host adapter structure.
  711. *
  712. * This routine parforms all of the steps necessary to initialize the adapter.
  713. *
  714. **/
  715. int qla4xxx_initialize_adapter(struct scsi_qla_host *ha)
  716. {
  717. int status = QLA_ERROR;
  718. ha->eeprom_cmd_data = 0;
  719. ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  720. ha->isp_ops->pci_config(ha);
  721. ha->isp_ops->disable_intrs(ha);
  722. /* Initialize the Host adapter request/response queues and firmware */
  723. if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
  724. goto exit_init_hba;
  725. if (qla4xxx_about_firmware(ha) == QLA_ERROR)
  726. goto exit_init_hba;
  727. if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
  728. goto exit_init_hba;
  729. if (qla4xxx_init_local_data(ha) == QLA_ERROR)
  730. goto exit_init_hba;
  731. status = qla4xxx_init_firmware(ha);
  732. if (status == QLA_ERROR)
  733. goto exit_init_hba;
  734. qla4xxx_free_ddb_index(ha);
  735. set_bit(AF_ONLINE, &ha->flags);
  736. exit_init_hba:
  737. if (is_qla8022(ha) && (status == QLA_ERROR)) {
  738. /* Since interrupts are registered in start_firmware for
  739. * 82xx, release them here if initialize_adapter fails */
  740. qla4xxx_free_irqs(ha);
  741. }
  742. DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
  743. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  744. return status;
  745. }
  746. /**
  747. * qla4xxx_process_ddb_changed - process ddb state change
  748. * @ha - Pointer to host adapter structure.
  749. * @fw_ddb_index - Firmware's device database index
  750. * @state - Device state
  751. *
  752. * This routine processes a Decive Database Changed AEN Event.
  753. **/
  754. int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
  755. uint32_t state, uint32_t conn_err)
  756. {
  757. struct ddb_entry * ddb_entry;
  758. uint32_t old_fw_ddb_device_state;
  759. int status = QLA_ERROR;
  760. /* check for out of range index */
  761. if (fw_ddb_index >= MAX_DDB_ENTRIES)
  762. goto exit_ddb_event;
  763. /* Get the corresponging ddb entry */
  764. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
  765. /* Device does not currently exist in our database. */
  766. if (ddb_entry == NULL) {
  767. ql4_printk(KERN_ERR, ha, "%s: No ddb_entry at FW index [%d]\n",
  768. __func__, fw_ddb_index);
  769. if (state == DDB_DS_NO_CONNECTION_ACTIVE)
  770. clear_bit(fw_ddb_index, ha->ddb_idx_map);
  771. goto exit_ddb_event;
  772. }
  773. old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
  774. DEBUG2(ql4_printk(KERN_INFO, ha,
  775. "%s: DDB - old state = 0x%x, new state = 0x%x for "
  776. "index [%d]\n", __func__,
  777. ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
  778. ddb_entry->fw_ddb_device_state = state;
  779. switch (old_fw_ddb_device_state) {
  780. case DDB_DS_LOGIN_IN_PROCESS:
  781. switch (state) {
  782. case DDB_DS_SESSION_ACTIVE:
  783. case DDB_DS_DISCOVERY:
  784. iscsi_conn_start(ddb_entry->conn);
  785. iscsi_conn_login_event(ddb_entry->conn,
  786. ISCSI_CONN_STATE_LOGGED_IN);
  787. qla4xxx_update_session_conn_param(ha, ddb_entry);
  788. status = QLA_SUCCESS;
  789. break;
  790. case DDB_DS_SESSION_FAILED:
  791. case DDB_DS_NO_CONNECTION_ACTIVE:
  792. iscsi_conn_login_event(ddb_entry->conn,
  793. ISCSI_CONN_STATE_FREE);
  794. status = QLA_SUCCESS;
  795. break;
  796. }
  797. break;
  798. case DDB_DS_SESSION_ACTIVE:
  799. switch (state) {
  800. case DDB_DS_SESSION_FAILED:
  801. /*
  802. * iscsi_session failure will cause userspace to
  803. * stop the connection which in turn would block the
  804. * iscsi_session and start relogin
  805. */
  806. iscsi_session_failure(ddb_entry->sess->dd_data,
  807. ISCSI_ERR_CONN_FAILED);
  808. status = QLA_SUCCESS;
  809. break;
  810. case DDB_DS_NO_CONNECTION_ACTIVE:
  811. clear_bit(fw_ddb_index, ha->ddb_idx_map);
  812. status = QLA_SUCCESS;
  813. break;
  814. }
  815. break;
  816. case DDB_DS_SESSION_FAILED:
  817. switch (state) {
  818. case DDB_DS_SESSION_ACTIVE:
  819. case DDB_DS_DISCOVERY:
  820. iscsi_conn_start(ddb_entry->conn);
  821. iscsi_conn_login_event(ddb_entry->conn,
  822. ISCSI_CONN_STATE_LOGGED_IN);
  823. qla4xxx_update_session_conn_param(ha, ddb_entry);
  824. status = QLA_SUCCESS;
  825. break;
  826. case DDB_DS_SESSION_FAILED:
  827. iscsi_session_failure(ddb_entry->sess->dd_data,
  828. ISCSI_ERR_CONN_FAILED);
  829. status = QLA_SUCCESS;
  830. break;
  831. }
  832. break;
  833. default:
  834. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
  835. __func__));
  836. break;
  837. }
  838. exit_ddb_event:
  839. return status;
  840. }