ql4_init.c 38 KB

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