ql4_init.c 39 KB

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