ql4_init.c 38 KB

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