ql4_init.c 39 KB

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