ql4_init.c 44 KB

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