ql4_init.c 39 KB

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