ql4_init.c 39 KB

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