ql4_init.c 39 KB

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