ql4_init.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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 uint8_t
  164. qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
  165. {
  166. uint8_t ipv4_wait = 0;
  167. uint8_t ipv6_wait = 0;
  168. int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
  169. /* If both IPv4 & IPv6 are enabled, possibly only one
  170. * IP address may be acquired, so check to see if we
  171. * need to wait for another */
  172. if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
  173. if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
  174. ((ha->addl_fw_state &
  175. FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
  176. ipv4_wait = 1;
  177. }
  178. if (((ha->ipv6_addl_options &
  179. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
  180. ((ha->ipv6_link_local_state == IP_ADDRSTATE_ACQUIRING) ||
  181. (ha->ipv6_addr0_state == IP_ADDRSTATE_ACQUIRING) ||
  182. (ha->ipv6_addr1_state == IP_ADDRSTATE_ACQUIRING))) {
  183. ipv6_wait = 1;
  184. if ((ha->ipv6_link_local_state ==
  185. IP_ADDRSTATE_PREFERRED) ||
  186. (ha->ipv6_addr0_state == IP_ADDRSTATE_PREFERRED) ||
  187. (ha->ipv6_addr1_state == IP_ADDRSTATE_PREFERRED)) {
  188. DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
  189. "Preferred IP configured."
  190. " Don't wait!\n", ha->host_no,
  191. __func__));
  192. ipv6_wait = 0;
  193. }
  194. if (memcmp(&ha->ipv6_default_router_addr, ip_address,
  195. IPv6_ADDR_LEN) == 0) {
  196. DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
  197. "No Router configured. "
  198. "Don't wait!\n", ha->host_no,
  199. __func__));
  200. ipv6_wait = 0;
  201. }
  202. if ((ha->ipv6_default_router_state ==
  203. IPV6_RTRSTATE_MANUAL) &&
  204. (ha->ipv6_link_local_state ==
  205. IP_ADDRSTATE_TENTATIVE) &&
  206. (memcmp(&ha->ipv6_link_local_addr,
  207. &ha->ipv6_default_router_addr, 4) == 0)) {
  208. DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
  209. "IP configured. Don't wait!\n",
  210. ha->host_no, __func__));
  211. ipv6_wait = 0;
  212. }
  213. }
  214. if (ipv4_wait || ipv6_wait) {
  215. DEBUG2(printk("scsi%ld: %s: Wait for additional "
  216. "IP(s) \"", ha->host_no, __func__));
  217. if (ipv4_wait)
  218. DEBUG2(printk("IPv4 "));
  219. if (ha->ipv6_link_local_state == IP_ADDRSTATE_ACQUIRING)
  220. DEBUG2(printk("IPv6LinkLocal "));
  221. if (ha->ipv6_addr0_state == IP_ADDRSTATE_ACQUIRING)
  222. DEBUG2(printk("IPv6Addr0 "));
  223. if (ha->ipv6_addr1_state == IP_ADDRSTATE_ACQUIRING)
  224. DEBUG2(printk("IPv6Addr1 "));
  225. DEBUG2(printk("\"\n"));
  226. }
  227. }
  228. return ipv4_wait|ipv6_wait;
  229. }
  230. static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
  231. {
  232. uint32_t timeout_count;
  233. int ready = 0;
  234. DEBUG2(dev_info(&ha->pdev->dev, "Waiting for Firmware Ready..\n"));
  235. for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
  236. timeout_count--) {
  237. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  238. qla4xxx_get_dhcp_ip_address(ha);
  239. /* Get firmware state. */
  240. if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
  241. DEBUG2(printk("scsi%ld: %s: unable to get firmware "
  242. "state\n", ha->host_no, __func__));
  243. break;
  244. }
  245. if (ha->firmware_state & FW_STATE_ERROR) {
  246. DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
  247. " occurred\n", ha->host_no, __func__));
  248. break;
  249. }
  250. if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
  251. /*
  252. * The firmware has not yet been issued an Initialize
  253. * Firmware command, so issue it now.
  254. */
  255. if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
  256. break;
  257. /* Go back and test for ready state - no wait. */
  258. continue;
  259. }
  260. if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
  261. DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
  262. "AUTOCONNECT in progress\n",
  263. ha->host_no, __func__));
  264. }
  265. if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
  266. DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
  267. " CONFIGURING IP\n",
  268. ha->host_no, __func__));
  269. /*
  270. * Check for link state after 15 secs and if link is
  271. * still DOWN then, cable is unplugged. Ignore "DHCP
  272. * in Progress/CONFIGURING IP" bit to check if firmware
  273. * is in ready state or not after 15 secs.
  274. * This is applicable for both 2.x & 3.x firmware
  275. */
  276. if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
  277. if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
  278. DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
  279. " LINK UP (Cable plugged)\n",
  280. ha->host_no, __func__));
  281. } else if (ha->firmware_state &
  282. (FW_STATE_CONFIGURING_IP |
  283. FW_STATE_READY)) {
  284. DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
  285. "LINK DOWN (Cable unplugged)\n",
  286. ha->host_no, __func__));
  287. ha->firmware_state = FW_STATE_READY;
  288. }
  289. }
  290. }
  291. if (ha->firmware_state == FW_STATE_READY) {
  292. /* If DHCP IP Addr is available, retrieve it now. */
  293. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
  294. &ha->dpc_flags))
  295. qla4xxx_get_dhcp_ip_address(ha);
  296. if (!qla4xxx_wait_for_ip_config(ha) ||
  297. timeout_count == 1) {
  298. DEBUG2(dev_info(&ha->pdev->dev,
  299. "Firmware Ready..\n"));
  300. /* The firmware is ready to process SCSI
  301. commands. */
  302. DEBUG2(dev_info(&ha->pdev->dev,
  303. "scsi%ld: %s: MEDIA TYPE"
  304. " - %s\n", ha->host_no,
  305. __func__, (ha->addl_fw_state &
  306. FW_ADDSTATE_OPTICAL_MEDIA)
  307. != 0 ? "OPTICAL" : "COPPER"));
  308. DEBUG2(dev_info(&ha->pdev->dev,
  309. "scsi%ld: %s: DHCPv4 STATE"
  310. " Enabled %s\n", ha->host_no,
  311. __func__, (ha->addl_fw_state &
  312. FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
  313. "YES" : "NO"));
  314. DEBUG2(dev_info(&ha->pdev->dev,
  315. "scsi%ld: %s: LINK %s\n",
  316. ha->host_no, __func__,
  317. (ha->addl_fw_state &
  318. FW_ADDSTATE_LINK_UP) != 0 ?
  319. "UP" : "DOWN"));
  320. DEBUG2(dev_info(&ha->pdev->dev,
  321. "scsi%ld: %s: iSNS Service "
  322. "Started %s\n",
  323. ha->host_no, __func__,
  324. (ha->addl_fw_state &
  325. FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
  326. "YES" : "NO"));
  327. ready = 1;
  328. break;
  329. }
  330. }
  331. DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
  332. "seconds expired= %d\n", ha->host_no, __func__,
  333. ha->firmware_state, ha->addl_fw_state,
  334. timeout_count));
  335. if (is_qla4032(ha) &&
  336. !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
  337. (timeout_count < ADAPTER_INIT_TOV - 5)) {
  338. break;
  339. }
  340. msleep(1000);
  341. } /* end of for */
  342. if (timeout_count <= 0)
  343. DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
  344. ha->host_no, __func__));
  345. if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
  346. DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
  347. "it's waiting to configure an IP address\n",
  348. ha->host_no, __func__));
  349. ready = 1;
  350. } else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
  351. DEBUG2(printk("scsi%ld: %s: FW initialized, but "
  352. "auto-discovery still in process\n",
  353. ha->host_no, __func__));
  354. ready = 1;
  355. }
  356. return ready;
  357. }
  358. /**
  359. * qla4xxx_init_firmware - initializes the firmware.
  360. * @ha: pointer to host adapter structure.
  361. *
  362. **/
  363. static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
  364. {
  365. int status = QLA_ERROR;
  366. dev_info(&ha->pdev->dev, "Initializing firmware..\n");
  367. if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
  368. DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
  369. "control block\n", ha->host_no, __func__));
  370. return status;
  371. }
  372. if (!qla4xxx_fw_ready(ha))
  373. return status;
  374. return qla4xxx_get_firmware_status(ha);
  375. }
  376. static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
  377. uint32_t fw_ddb_index,
  378. uint32_t *new_tgt)
  379. {
  380. struct dev_db_entry *fw_ddb_entry = NULL;
  381. dma_addr_t fw_ddb_entry_dma;
  382. struct ddb_entry *ddb_entry = NULL;
  383. int found = 0;
  384. uint32_t device_state;
  385. *new_tgt = 0;
  386. /* Make sure the dma buffer is valid */
  387. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
  388. sizeof(*fw_ddb_entry),
  389. &fw_ddb_entry_dma, GFP_KERNEL);
  390. if (fw_ddb_entry == NULL) {
  391. DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
  392. ha->host_no, __func__));
  393. return NULL;
  394. }
  395. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
  396. fw_ddb_entry_dma, NULL, NULL,
  397. &device_state, NULL, NULL, NULL) ==
  398. QLA_ERROR) {
  399. DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
  400. "fw_ddb_index %d\n", ha->host_no, __func__,
  401. fw_ddb_index));
  402. return NULL;
  403. }
  404. /* Allocate DDB if not already allocated. */
  405. DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
  406. __func__, fw_ddb_index));
  407. list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
  408. if ((memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
  409. ISCSI_NAME_SIZE) == 0) &&
  410. (ddb_entry->tpgt ==
  411. le32_to_cpu(fw_ddb_entry->tgt_portal_grp)) &&
  412. (memcmp(ddb_entry->isid, fw_ddb_entry->isid,
  413. sizeof(ddb_entry->isid)) == 0)) {
  414. found++;
  415. break;
  416. }
  417. }
  418. if (!found) {
  419. DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
  420. "new ddb\n", ha->host_no, __func__,
  421. fw_ddb_index));
  422. *new_tgt = 1;
  423. ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
  424. }
  425. /* if not found allocate new ddb */
  426. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
  427. fw_ddb_entry_dma);
  428. return ddb_entry;
  429. }
  430. /**
  431. * qla4xxx_update_ddb_entry - update driver's internal ddb
  432. * @ha: pointer to host adapter structure.
  433. * @ddb_entry: pointer to device database structure to be filled
  434. * @fw_ddb_index: index of the ddb entry in fw ddb table
  435. *
  436. * This routine updates the driver's internal device database entry
  437. * with information retrieved from the firmware's device database
  438. * entry for the specified device. The ddb_entry->fw_ddb_index field
  439. * must be initialized prior to calling this routine
  440. *
  441. **/
  442. static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
  443. struct ddb_entry *ddb_entry,
  444. uint32_t fw_ddb_index)
  445. {
  446. struct dev_db_entry *fw_ddb_entry = NULL;
  447. dma_addr_t fw_ddb_entry_dma;
  448. int status = QLA_ERROR;
  449. uint32_t conn_err;
  450. if (ddb_entry == NULL) {
  451. DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
  452. __func__));
  453. goto exit_update_ddb;
  454. }
  455. /* Make sure the dma buffer is valid */
  456. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
  457. sizeof(*fw_ddb_entry),
  458. &fw_ddb_entry_dma, GFP_KERNEL);
  459. if (fw_ddb_entry == NULL) {
  460. DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
  461. ha->host_no, __func__));
  462. goto exit_update_ddb;
  463. }
  464. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
  465. fw_ddb_entry_dma, NULL, NULL,
  466. &ddb_entry->fw_ddb_device_state, &conn_err,
  467. &ddb_entry->tcp_source_port_num,
  468. &ddb_entry->connection_id) ==
  469. QLA_ERROR) {
  470. DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
  471. "fw_ddb_index %d\n", ha->host_no, __func__,
  472. fw_ddb_index));
  473. goto exit_update_ddb;
  474. }
  475. status = QLA_SUCCESS;
  476. ddb_entry->options = le16_to_cpu(fw_ddb_entry->options);
  477. ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
  478. ddb_entry->task_mgmt_timeout =
  479. le16_to_cpu(fw_ddb_entry->def_timeout);
  480. ddb_entry->CmdSn = 0;
  481. ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
  482. ddb_entry->default_relogin_timeout =
  483. le16_to_cpu(fw_ddb_entry->def_timeout);
  484. ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  485. /* Update index in case it changed */
  486. ddb_entry->fw_ddb_index = fw_ddb_index;
  487. ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
  488. ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
  489. ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  490. memcpy(ddb_entry->isid, fw_ddb_entry->isid, sizeof(ddb_entry->isid));
  491. memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
  492. min(sizeof(ddb_entry->iscsi_name),
  493. sizeof(fw_ddb_entry->iscsi_name)));
  494. memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
  495. min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
  496. ddb_entry->iscsi_max_burst_len = fw_ddb_entry->iscsi_max_burst_len;
  497. ddb_entry->iscsi_max_outsnd_r2t = fw_ddb_entry->iscsi_max_outsnd_r2t;
  498. ddb_entry->iscsi_first_burst_len = fw_ddb_entry->iscsi_first_burst_len;
  499. ddb_entry->iscsi_max_rcv_data_seg_len =
  500. fw_ddb_entry->iscsi_max_rcv_data_seg_len;
  501. ddb_entry->iscsi_max_snd_data_seg_len =
  502. fw_ddb_entry->iscsi_max_snd_data_seg_len;
  503. if (ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
  504. memcpy(&ddb_entry->remote_ipv6_addr,
  505. fw_ddb_entry->ip_addr,
  506. min(sizeof(ddb_entry->remote_ipv6_addr),
  507. sizeof(fw_ddb_entry->ip_addr)));
  508. memcpy(&ddb_entry->link_local_ipv6_addr,
  509. fw_ddb_entry->link_local_ipv6_addr,
  510. min(sizeof(ddb_entry->link_local_ipv6_addr),
  511. sizeof(fw_ddb_entry->link_local_ipv6_addr)));
  512. DEBUG2(dev_info(&ha->pdev->dev, "%s: DDB[%d] osIdx = %d "
  513. "State %04x ConnErr %08x IP %pI6 "
  514. ":%04d \"%s\"\n",
  515. __func__, fw_ddb_index,
  516. ddb_entry->os_target_id,
  517. ddb_entry->fw_ddb_device_state,
  518. conn_err, fw_ddb_entry->ip_addr,
  519. le16_to_cpu(fw_ddb_entry->port),
  520. fw_ddb_entry->iscsi_name));
  521. } else
  522. DEBUG2(dev_info(&ha->pdev->dev, "%s: DDB[%d] osIdx = %d "
  523. "State %04x ConnErr %08x IP %pI4 "
  524. ":%04d \"%s\"\n",
  525. __func__, fw_ddb_index,
  526. ddb_entry->os_target_id,
  527. ddb_entry->fw_ddb_device_state,
  528. conn_err, fw_ddb_entry->ip_addr,
  529. le16_to_cpu(fw_ddb_entry->port),
  530. fw_ddb_entry->iscsi_name));
  531. exit_update_ddb:
  532. if (fw_ddb_entry)
  533. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  534. fw_ddb_entry, fw_ddb_entry_dma);
  535. return status;
  536. }
  537. /**
  538. * qla4xxx_alloc_ddb - allocate device database entry
  539. * @ha: Pointer to host adapter structure.
  540. * @fw_ddb_index: Firmware's device database index
  541. *
  542. * This routine allocates a ddb_entry, ititializes some values, and
  543. * inserts it into the ddb list.
  544. **/
  545. static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
  546. uint32_t fw_ddb_index)
  547. {
  548. struct ddb_entry *ddb_entry;
  549. DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
  550. __func__, fw_ddb_index));
  551. ddb_entry = qla4xxx_alloc_sess(ha);
  552. if (ddb_entry == NULL) {
  553. DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
  554. "to add fw_ddb_index [%d]\n",
  555. ha->host_no, __func__, fw_ddb_index));
  556. return ddb_entry;
  557. }
  558. ddb_entry->fw_ddb_index = fw_ddb_index;
  559. atomic_set(&ddb_entry->port_down_timer, ha->port_down_retry_count);
  560. atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
  561. atomic_set(&ddb_entry->relogin_timer, 0);
  562. atomic_set(&ddb_entry->relogin_retry_count, 0);
  563. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  564. list_add_tail(&ddb_entry->list, &ha->ddb_list);
  565. ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
  566. ha->tot_ddbs++;
  567. return ddb_entry;
  568. }
  569. /**
  570. * qla4_is_relogin_allowed - Are we allowed to login?
  571. * @ha: Pointer to host adapter structure.
  572. * @conn_err: Last connection error associated with the ddb
  573. *
  574. * This routine tests the given connection error to determine if
  575. * we are allowed to login.
  576. **/
  577. int qla4_is_relogin_allowed(struct scsi_qla_host *ha, uint32_t conn_err)
  578. {
  579. uint32_t err_code, login_rsp_sts_class;
  580. int relogin = 1;
  581. err_code = ((conn_err & 0x00ff0000) >> 16);
  582. login_rsp_sts_class = ((conn_err & 0x0000ff00) >> 8);
  583. if (err_code == 0x1c || err_code == 0x06) {
  584. DEBUG2(dev_info(&ha->pdev->dev,
  585. ": conn_err=0x%08x, send target completed"
  586. " or access denied failure\n", conn_err));
  587. relogin = 0;
  588. }
  589. if ((err_code == 0x08) && (login_rsp_sts_class == 0x02)) {
  590. /* Login Response PDU returned an error.
  591. Login Response Status in Error Code Detail
  592. indicates login should not be retried.*/
  593. DEBUG2(dev_info(&ha->pdev->dev,
  594. ": conn_err=0x%08x, do not retry relogin\n",
  595. conn_err));
  596. relogin = 0;
  597. }
  598. return relogin;
  599. }
  600. /**
  601. * qla4xxx_configure_ddbs - builds driver ddb list
  602. * @ha: Pointer to host adapter structure.
  603. *
  604. * This routine searches for all valid firmware ddb entries and builds
  605. * an internal ddb list. Ddbs that are considered valid are those with
  606. * a device state of SESSION_ACTIVE.
  607. **/
  608. static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
  609. {
  610. int status = QLA_SUCCESS;
  611. uint32_t fw_ddb_index = 0;
  612. uint32_t next_fw_ddb_index = 0;
  613. uint32_t ddb_state;
  614. uint32_t conn_err;
  615. struct ddb_entry *ddb_entry;
  616. struct dev_db_entry *fw_ddb_entry = NULL;
  617. dma_addr_t fw_ddb_entry_dma;
  618. uint32_t ipv6_device;
  619. uint32_t new_tgt;
  620. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  621. &fw_ddb_entry_dma, GFP_KERNEL);
  622. if (fw_ddb_entry == NULL) {
  623. DEBUG2(dev_info(&ha->pdev->dev, "%s: DMA alloc failed\n",
  624. __func__));
  625. return QLA_ERROR;
  626. }
  627. dev_info(&ha->pdev->dev, "Initializing DDBs ...\n");
  628. for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
  629. fw_ddb_index = next_fw_ddb_index) {
  630. /* First, let's see if a device exists here */
  631. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
  632. 0, NULL, &next_fw_ddb_index,
  633. &ddb_state, &conn_err,
  634. NULL, NULL) ==
  635. QLA_ERROR) {
  636. DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
  637. "fw_ddb_index %d failed", ha->host_no,
  638. __func__, fw_ddb_index));
  639. return QLA_ERROR;
  640. }
  641. DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
  642. "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
  643. fw_ddb_index, ddb_state, next_fw_ddb_index));
  644. /* Issue relogin, if necessary. */
  645. if (ddb_state == DDB_DS_SESSION_FAILED ||
  646. ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
  647. /* Try and login to device */
  648. DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
  649. ha->host_no, __func__, fw_ddb_index));
  650. ipv6_device = le16_to_cpu(fw_ddb_entry->options) &
  651. DDB_OPT_IPV6_DEVICE;
  652. if (qla4_is_relogin_allowed(ha, conn_err) &&
  653. ((!ipv6_device &&
  654. *((uint32_t *)fw_ddb_entry->ip_addr))
  655. || ipv6_device)) {
  656. qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
  657. if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
  658. NULL, 0, NULL,
  659. &next_fw_ddb_index,
  660. &ddb_state, &conn_err,
  661. NULL, NULL)
  662. == QLA_ERROR) {
  663. DEBUG2(printk("scsi%ld: %s:"
  664. "get_ddb_entry %d failed\n",
  665. ha->host_no,
  666. __func__, fw_ddb_index));
  667. return QLA_ERROR;
  668. }
  669. }
  670. }
  671. if (ddb_state != DDB_DS_SESSION_ACTIVE)
  672. goto next_one;
  673. /*
  674. * if fw_ddb with session active state found,
  675. * add to ddb_list
  676. */
  677. DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
  678. ha->host_no, __func__, fw_ddb_index));
  679. /* Add DDB to internal our ddb list. */
  680. ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
  681. if (ddb_entry == NULL) {
  682. DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
  683. "for device at fw_ddb_index %d\n",
  684. ha->host_no, __func__, fw_ddb_index));
  685. return QLA_ERROR;
  686. }
  687. /* Fill in the device structure */
  688. if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
  689. QLA_ERROR) {
  690. ha->fw_ddb_index_map[fw_ddb_index] =
  691. (struct ddb_entry *)INVALID_ENTRY;
  692. DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
  693. "for fw_ddb_index %d.\n",
  694. ha->host_no, __func__, fw_ddb_index));
  695. return QLA_ERROR;
  696. }
  697. next_one:
  698. /* We know we've reached the last device when
  699. * next_fw_ddb_index is 0 */
  700. if (next_fw_ddb_index == 0)
  701. break;
  702. }
  703. dev_info(&ha->pdev->dev, "DDB list done..\n");
  704. return status;
  705. }
  706. struct qla4_relog_scan {
  707. int halt_wait;
  708. uint32_t conn_err;
  709. uint32_t fw_ddb_index;
  710. uint32_t next_fw_ddb_index;
  711. uint32_t fw_ddb_device_state;
  712. };
  713. static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
  714. {
  715. struct ddb_entry *ddb_entry;
  716. if (qla4_is_relogin_allowed(ha, rs->conn_err)) {
  717. /* We either have a device that is in
  718. * the process of relogging in or a
  719. * device that is waiting to be
  720. * relogged in */
  721. rs->halt_wait = 0;
  722. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
  723. rs->fw_ddb_index);
  724. if (ddb_entry == NULL)
  725. return QLA_ERROR;
  726. if (ddb_entry->dev_scan_wait_to_start_relogin != 0
  727. && time_after_eq(jiffies,
  728. ddb_entry->
  729. dev_scan_wait_to_start_relogin))
  730. {
  731. ddb_entry->dev_scan_wait_to_start_relogin = 0;
  732. qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
  733. }
  734. }
  735. return QLA_SUCCESS;
  736. }
  737. static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
  738. struct qla4_relog_scan *rs)
  739. {
  740. int error;
  741. /* scan for relogins
  742. * ----------------- */
  743. for (rs->fw_ddb_index = 0; rs->fw_ddb_index < MAX_DDB_ENTRIES;
  744. rs->fw_ddb_index = rs->next_fw_ddb_index) {
  745. if (qla4xxx_get_fwddb_entry(ha, rs->fw_ddb_index, NULL, 0,
  746. NULL, &rs->next_fw_ddb_index,
  747. &rs->fw_ddb_device_state,
  748. &rs->conn_err, NULL, NULL)
  749. == QLA_ERROR)
  750. return QLA_ERROR;
  751. if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
  752. rs->halt_wait = 0;
  753. if (rs->fw_ddb_device_state == DDB_DS_SESSION_FAILED ||
  754. rs->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE) {
  755. error = qla4_test_rdy(ha, rs);
  756. if (error)
  757. return error;
  758. }
  759. /* We know we've reached the last device when
  760. * next_fw_ddb_index is 0 */
  761. if (rs->next_fw_ddb_index == 0)
  762. break;
  763. }
  764. return QLA_SUCCESS;
  765. }
  766. /**
  767. * qla4xxx_devices_ready - wait for target devices to be logged in
  768. * @ha: pointer to adapter structure
  769. *
  770. * This routine waits up to ql4xdiscoverywait seconds
  771. * F/W database during driver load time.
  772. **/
  773. static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
  774. {
  775. int error;
  776. unsigned long discovery_wtime;
  777. struct qla4_relog_scan rs;
  778. discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
  779. DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
  780. do {
  781. /* poll for AEN. */
  782. qla4xxx_get_firmware_state(ha);
  783. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags)) {
  784. /* Set time-between-relogin timer */
  785. qla4xxx_process_aen(ha, RELOGIN_DDB_CHANGED_AENS);
  786. }
  787. /* if no relogins active or needed, halt discvery wait */
  788. rs.halt_wait = 1;
  789. error = qla4_scan_for_relogin(ha, &rs);
  790. if (rs.halt_wait) {
  791. DEBUG2(printk("scsi%ld: %s: Delay halted. Devices "
  792. "Ready.\n", ha->host_no, __func__));
  793. return QLA_SUCCESS;
  794. }
  795. msleep(2000);
  796. } while (!time_after_eq(jiffies, discovery_wtime));
  797. DEBUG3(qla4xxx_get_conn_event_log(ha));
  798. return QLA_SUCCESS;
  799. }
  800. static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
  801. {
  802. unsigned long wtime;
  803. /* Flush the 0x8014 AEN from the firmware as a result of
  804. * Auto connect. We are basically doing get_firmware_ddb()
  805. * to determine whether we need to log back in or not.
  806. * Trying to do a set ddb before we have processed 0x8014
  807. * will result in another set_ddb() for the same ddb. In other
  808. * words there will be stale entries in the aen_q.
  809. */
  810. wtime = jiffies + (2 * HZ);
  811. do {
  812. if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
  813. if (ha->firmware_state & (BIT_2 | BIT_0))
  814. return;
  815. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  816. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  817. msleep(1000);
  818. } while (!time_after_eq(jiffies, wtime));
  819. }
  820. static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
  821. {
  822. uint16_t fw_ddb_index;
  823. int status = QLA_SUCCESS;
  824. /* free the ddb list if is not empty */
  825. if (!list_empty(&ha->ddb_list))
  826. qla4xxx_free_ddb_list(ha);
  827. for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
  828. ha->fw_ddb_index_map[fw_ddb_index] =
  829. (struct ddb_entry *)INVALID_ENTRY;
  830. ha->tot_ddbs = 0;
  831. qla4xxx_flush_AENS(ha);
  832. /*
  833. * First perform device discovery for active
  834. * fw ddb indexes and build
  835. * ddb list.
  836. */
  837. if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
  838. return status;
  839. /* Wait for an AEN */
  840. qla4xxx_devices_ready(ha);
  841. /*
  842. * Targets can come online after the inital discovery, so processing
  843. * the aens here will catch them.
  844. */
  845. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  846. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  847. return status;
  848. }
  849. /**
  850. * qla4xxx_update_ddb_list - update the driver ddb list
  851. * @ha: pointer to host adapter structure.
  852. *
  853. * This routine obtains device information from the F/W database after
  854. * firmware or adapter resets. The device table is preserved.
  855. **/
  856. int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
  857. {
  858. int status = QLA_SUCCESS;
  859. struct ddb_entry *ddb_entry, *detemp;
  860. /* Update the device information for all devices. */
  861. list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
  862. qla4xxx_update_ddb_entry(ha, ddb_entry,
  863. ddb_entry->fw_ddb_index);
  864. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  865. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  866. DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
  867. "ONLINE\n", ha->host_no, __func__,
  868. ddb_entry->fw_ddb_index));
  869. iscsi_unblock_session(ddb_entry->sess);
  870. } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
  871. qla4xxx_mark_device_missing(ha, ddb_entry);
  872. }
  873. return status;
  874. }
  875. /**
  876. * qla4xxx_relogin_device - re-establish session
  877. * @ha: Pointer to host adapter structure.
  878. * @ddb_entry: Pointer to device database entry
  879. *
  880. * This routine does a session relogin with the specified device.
  881. * The ddb entry must be assigned prior to making this call.
  882. **/
  883. int qla4xxx_relogin_device(struct scsi_qla_host *ha,
  884. struct ddb_entry * ddb_entry)
  885. {
  886. uint16_t relogin_timer;
  887. relogin_timer = max(ddb_entry->default_relogin_timeout,
  888. (uint16_t)RELOGIN_TOV);
  889. atomic_set(&ddb_entry->relogin_timer, relogin_timer);
  890. DEBUG2(printk("scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
  891. ddb_entry->fw_ddb_index, relogin_timer));
  892. qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
  893. return QLA_SUCCESS;
  894. }
  895. static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
  896. {
  897. unsigned long flags;
  898. union external_hw_config_reg extHwConfig;
  899. DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
  900. __func__));
  901. if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
  902. return QLA_ERROR;
  903. if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
  904. ql4xxx_unlock_flash(ha);
  905. return QLA_ERROR;
  906. }
  907. /* Get EEPRom Parameters from NVRAM and validate */
  908. dev_info(&ha->pdev->dev, "Configuring NVRAM ...\n");
  909. if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
  910. spin_lock_irqsave(&ha->hardware_lock, flags);
  911. extHwConfig.Asuint32_t =
  912. rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
  913. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  914. } else {
  915. dev_warn(&ha->pdev->dev,
  916. "scsi%ld: %s: EEProm checksum invalid. "
  917. "Please update your EEPROM\n", ha->host_no,
  918. __func__);
  919. /* Attempt to set defaults */
  920. if (is_qla4010(ha))
  921. extHwConfig.Asuint32_t = 0x1912;
  922. else if (is_qla4022(ha) | is_qla4032(ha))
  923. extHwConfig.Asuint32_t = 0x0023;
  924. else
  925. return QLA_ERROR;
  926. }
  927. DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
  928. ha->host_no, __func__, extHwConfig.Asuint32_t));
  929. spin_lock_irqsave(&ha->hardware_lock, flags);
  930. writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
  931. readl(isp_ext_hw_conf(ha));
  932. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  933. ql4xxx_unlock_nvram(ha);
  934. ql4xxx_unlock_flash(ha);
  935. return QLA_SUCCESS;
  936. }
  937. static void qla4x00_pci_config(struct scsi_qla_host *ha)
  938. {
  939. uint16_t w;
  940. int status;
  941. dev_info(&ha->pdev->dev, "Configuring PCI space...\n");
  942. pci_set_master(ha->pdev);
  943. status = pci_set_mwi(ha->pdev);
  944. /*
  945. * We want to respect framework's setting of PCI configuration space
  946. * command register and also want to make sure that all bits of
  947. * interest to us are properly set in command register.
  948. */
  949. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  950. w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  951. w &= ~PCI_COMMAND_INTX_DISABLE;
  952. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  953. }
  954. static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
  955. {
  956. int status = QLA_ERROR;
  957. unsigned long max_wait_time;
  958. unsigned long flags;
  959. uint32_t mbox_status;
  960. dev_info(&ha->pdev->dev, "Starting firmware ...\n");
  961. /*
  962. * Start firmware from flash ROM
  963. *
  964. * WORKAROUND: Stuff a non-constant value that the firmware can
  965. * use as a seed for a random number generator in MB7 prior to
  966. * setting BOOT_ENABLE. Fixes problem where the TCP
  967. * connections use the same TCP ports after each reboot,
  968. * causing some connections to not get re-established.
  969. */
  970. DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
  971. ha->host_no, __func__));
  972. spin_lock_irqsave(&ha->hardware_lock, flags);
  973. writel(jiffies, &ha->reg->mailbox[7]);
  974. if (is_qla4022(ha) | is_qla4032(ha))
  975. writel(set_rmask(NVR_WRITE_ENABLE),
  976. &ha->reg->u1.isp4022.nvram);
  977. writel(2, &ha->reg->mailbox[6]);
  978. readl(&ha->reg->mailbox[6]);
  979. writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
  980. readl(&ha->reg->ctrl_status);
  981. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  982. /* Wait for firmware to come UP. */
  983. DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
  984. "boot firmware to complete...\n",
  985. ha->host_no, __func__, FIRMWARE_UP_TOV));
  986. max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
  987. do {
  988. uint32_t ctrl_status;
  989. spin_lock_irqsave(&ha->hardware_lock, flags);
  990. ctrl_status = readw(&ha->reg->ctrl_status);
  991. mbox_status = readw(&ha->reg->mailbox[0]);
  992. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  993. if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
  994. break;
  995. if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
  996. break;
  997. DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
  998. "firmware to complete... ctrl_sts=0x%x\n",
  999. ha->host_no, __func__, ctrl_status));
  1000. msleep_interruptible(250);
  1001. } while (!time_after_eq(jiffies, max_wait_time));
  1002. if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
  1003. DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
  1004. ha->host_no, __func__));
  1005. spin_lock_irqsave(&ha->hardware_lock, flags);
  1006. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  1007. &ha->reg->ctrl_status);
  1008. readl(&ha->reg->ctrl_status);
  1009. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1010. status = QLA_SUCCESS;
  1011. } else {
  1012. printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
  1013. "- mbox status 0x%x\n", ha->host_no, __func__,
  1014. mbox_status);
  1015. status = QLA_ERROR;
  1016. }
  1017. return status;
  1018. }
  1019. int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
  1020. {
  1021. #define QL4_LOCK_DRVR_WAIT 60
  1022. #define QL4_LOCK_DRVR_SLEEP 1
  1023. int drvr_wait = QL4_LOCK_DRVR_WAIT;
  1024. while (drvr_wait) {
  1025. if (ql4xxx_lock_drvr(a) == 0) {
  1026. ssleep(QL4_LOCK_DRVR_SLEEP);
  1027. if (drvr_wait) {
  1028. DEBUG2(printk("scsi%ld: %s: Waiting for "
  1029. "Global Init Semaphore(%d)...\n",
  1030. a->host_no,
  1031. __func__, drvr_wait));
  1032. }
  1033. drvr_wait -= QL4_LOCK_DRVR_SLEEP;
  1034. } else {
  1035. DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
  1036. "acquired\n", a->host_no, __func__));
  1037. return QLA_SUCCESS;
  1038. }
  1039. }
  1040. return QLA_ERROR;
  1041. }
  1042. /**
  1043. * qla4xxx_start_firmware - starts qla4xxx firmware
  1044. * @ha: Pointer to host adapter structure.
  1045. *
  1046. * This routine performs the necessary steps to start the firmware for
  1047. * the QLA4010 adapter.
  1048. **/
  1049. static int qla4xxx_start_firmware(struct scsi_qla_host *ha)
  1050. {
  1051. unsigned long flags = 0;
  1052. uint32_t mbox_status;
  1053. int status = QLA_ERROR;
  1054. int soft_reset = 1;
  1055. int config_chip = 0;
  1056. if (is_qla4022(ha) | is_qla4032(ha))
  1057. ql4xxx_set_mac_number(ha);
  1058. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  1059. return QLA_ERROR;
  1060. spin_lock_irqsave(&ha->hardware_lock, flags);
  1061. DEBUG2(printk("scsi%ld: %s: port_ctrl = 0x%08X\n", ha->host_no,
  1062. __func__, readw(isp_port_ctrl(ha))));
  1063. DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
  1064. __func__, readw(isp_port_status(ha))));
  1065. /* Is Hardware already initialized? */
  1066. if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
  1067. DEBUG(printk("scsi%ld: %s: Hardware has already been "
  1068. "initialized\n", ha->host_no, __func__));
  1069. /* Receive firmware boot acknowledgement */
  1070. mbox_status = readw(&ha->reg->mailbox[0]);
  1071. DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
  1072. "0x%x\n", ha->host_no, __func__, mbox_status));
  1073. /* Is firmware already booted? */
  1074. if (mbox_status == 0) {
  1075. /* F/W not running, must be config by net driver */
  1076. config_chip = 1;
  1077. soft_reset = 0;
  1078. } else {
  1079. writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
  1080. &ha->reg->ctrl_status);
  1081. readl(&ha->reg->ctrl_status);
  1082. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1083. if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
  1084. DEBUG2(printk("scsi%ld: %s: Get firmware "
  1085. "state -- state = 0x%x\n",
  1086. ha->host_no,
  1087. __func__, ha->firmware_state));
  1088. /* F/W is running */
  1089. if (ha->firmware_state &
  1090. FW_STATE_CONFIG_WAIT) {
  1091. DEBUG2(printk("scsi%ld: %s: Firmware "
  1092. "in known state -- "
  1093. "config and "
  1094. "boot, state = 0x%x\n",
  1095. ha->host_no, __func__,
  1096. ha->firmware_state));
  1097. config_chip = 1;
  1098. soft_reset = 0;
  1099. }
  1100. } else {
  1101. DEBUG2(printk("scsi%ld: %s: Firmware in "
  1102. "unknown state -- resetting,"
  1103. " state = "
  1104. "0x%x\n", ha->host_no, __func__,
  1105. ha->firmware_state));
  1106. }
  1107. spin_lock_irqsave(&ha->hardware_lock, flags);
  1108. }
  1109. } else {
  1110. DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
  1111. "started - resetting\n", ha->host_no, __func__));
  1112. }
  1113. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1114. DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
  1115. ha->host_no, __func__, soft_reset, config_chip));
  1116. if (soft_reset) {
  1117. DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
  1118. __func__));
  1119. status = qla4xxx_soft_reset(ha);
  1120. if (status == QLA_ERROR) {
  1121. DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
  1122. ha->host_no, __func__));
  1123. ql4xxx_unlock_drvr(ha);
  1124. return QLA_ERROR;
  1125. }
  1126. config_chip = 1;
  1127. /* Reset clears the semaphore, so acquire again */
  1128. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  1129. return QLA_ERROR;
  1130. }
  1131. if (config_chip) {
  1132. if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
  1133. status = qla4xxx_start_firmware_from_flash(ha);
  1134. }
  1135. ql4xxx_unlock_drvr(ha);
  1136. if (status == QLA_SUCCESS) {
  1137. qla4xxx_get_fw_version(ha);
  1138. if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
  1139. qla4xxx_get_crash_record(ha);
  1140. } else {
  1141. DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
  1142. ha->host_no, __func__));
  1143. }
  1144. return status;
  1145. }
  1146. /**
  1147. * qla4xxx_initialize_adapter - initiailizes hba
  1148. * @ha: Pointer to host adapter structure.
  1149. * @renew_ddb_list: Indicates what to do with the adapter's ddb list
  1150. * after adapter recovery has completed.
  1151. * 0=preserve ddb list, 1=destroy and rebuild ddb list
  1152. *
  1153. * This routine parforms all of the steps necessary to initialize the adapter.
  1154. *
  1155. **/
  1156. int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
  1157. uint8_t renew_ddb_list)
  1158. {
  1159. int status = QLA_ERROR;
  1160. int8_t ip_address[IP_ADDR_LEN] = {0} ;
  1161. clear_bit(AF_ONLINE, &ha->flags);
  1162. ha->eeprom_cmd_data = 0;
  1163. qla4x00_pci_config(ha);
  1164. qla4xxx_disable_intrs(ha);
  1165. /* Initialize the Host adapter request/response queues and firmware */
  1166. if (qla4xxx_start_firmware(ha) == QLA_ERROR)
  1167. goto exit_init_hba;
  1168. if (qla4xxx_validate_mac_address(ha) == QLA_ERROR)
  1169. goto exit_init_hba;
  1170. if (qla4xxx_init_local_data(ha) == QLA_ERROR)
  1171. goto exit_init_hba;
  1172. status = qla4xxx_init_firmware(ha);
  1173. if (status == QLA_ERROR)
  1174. goto exit_init_hba;
  1175. /*
  1176. * FW is waiting to get an IP address from DHCP server: Skip building
  1177. * the ddb_list and wait for DHCP lease acquired aen to come in
  1178. * followed by 0x8014 aen" to trigger the tgt discovery process.
  1179. */
  1180. if (ha->firmware_state & FW_STATE_CONFIGURING_IP)
  1181. goto exit_init_online;
  1182. /* Skip device discovery if ip and subnet is zero */
  1183. if (memcmp(ha->ip_address, ip_address, IP_ADDR_LEN) == 0 ||
  1184. memcmp(ha->subnet_mask, ip_address, IP_ADDR_LEN) == 0)
  1185. goto exit_init_online;
  1186. if (renew_ddb_list == PRESERVE_DDB_LIST) {
  1187. /*
  1188. * We want to preserve lun states (i.e. suspended, etc.)
  1189. * for recovery initiated by the driver. So just update
  1190. * the device states for the existing ddb_list.
  1191. */
  1192. qla4xxx_reinitialize_ddb_list(ha);
  1193. } else if (renew_ddb_list == REBUILD_DDB_LIST) {
  1194. /*
  1195. * We want to build the ddb_list from scratch during
  1196. * driver initialization and recovery initiated by the
  1197. * INT_HBA_RESET IOCTL.
  1198. */
  1199. status = qla4xxx_initialize_ddb_list(ha);
  1200. if (status == QLA_ERROR) {
  1201. DEBUG2(printk("%s(%ld) Error occurred during build"
  1202. "ddb list\n", __func__, ha->host_no));
  1203. goto exit_init_hba;
  1204. }
  1205. }
  1206. if (!ha->tot_ddbs) {
  1207. DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
  1208. "present in Firmware device database\n",
  1209. ha->host_no));
  1210. }
  1211. exit_init_online:
  1212. set_bit(AF_ONLINE, &ha->flags);
  1213. exit_init_hba:
  1214. return status;
  1215. }
  1216. /**
  1217. * qla4xxx_add_device_dynamically - ddb addition due to an AEN
  1218. * @ha: Pointer to host adapter structure.
  1219. * @fw_ddb_index: Firmware's device database index
  1220. *
  1221. * This routine processes adds a device as a result of an 8014h AEN.
  1222. **/
  1223. static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
  1224. uint32_t fw_ddb_index)
  1225. {
  1226. struct ddb_entry * ddb_entry;
  1227. uint32_t new_tgt;
  1228. /* First allocate a device structure */
  1229. ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
  1230. if (ddb_entry == NULL) {
  1231. DEBUG2(printk(KERN_WARNING
  1232. "scsi%ld: Unable to allocate memory to add "
  1233. "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
  1234. return;
  1235. }
  1236. if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
  1237. /* Target has been bound to a new fw_ddb_index */
  1238. qla4xxx_free_ddb(ha, ddb_entry);
  1239. ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
  1240. if (ddb_entry == NULL) {
  1241. DEBUG2(printk(KERN_WARNING
  1242. "scsi%ld: Unable to allocate memory"
  1243. " to add fw_ddb_index %d\n",
  1244. ha->host_no, fw_ddb_index));
  1245. return;
  1246. }
  1247. }
  1248. if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
  1249. QLA_ERROR) {
  1250. ha->fw_ddb_index_map[fw_ddb_index] =
  1251. (struct ddb_entry *)INVALID_ENTRY;
  1252. DEBUG2(printk(KERN_WARNING
  1253. "scsi%ld: failed to add new device at index "
  1254. "[%d]\n Unable to retrieve fw ddb entry\n",
  1255. ha->host_no, fw_ddb_index));
  1256. qla4xxx_free_ddb(ha, ddb_entry);
  1257. return;
  1258. }
  1259. if (qla4xxx_add_sess(ddb_entry)) {
  1260. DEBUG2(printk(KERN_WARNING
  1261. "scsi%ld: failed to add new device at index "
  1262. "[%d]\n Unable to add connection and session\n",
  1263. ha->host_no, fw_ddb_index));
  1264. qla4xxx_free_ddb(ha, ddb_entry);
  1265. }
  1266. }
  1267. /**
  1268. * qla4xxx_process_ddb_changed - process ddb state change
  1269. * @ha - Pointer to host adapter structure.
  1270. * @fw_ddb_index - Firmware's device database index
  1271. * @state - Device state
  1272. *
  1273. * This routine processes a Decive Database Changed AEN Event.
  1274. **/
  1275. int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
  1276. uint32_t state, uint32_t conn_err)
  1277. {
  1278. struct ddb_entry * ddb_entry;
  1279. uint32_t old_fw_ddb_device_state;
  1280. /* check for out of range index */
  1281. if (fw_ddb_index >= MAX_DDB_ENTRIES)
  1282. return QLA_ERROR;
  1283. /* Get the corresponging ddb entry */
  1284. ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
  1285. /* Device does not currently exist in our database. */
  1286. if (ddb_entry == NULL) {
  1287. if (state == DDB_DS_SESSION_ACTIVE)
  1288. qla4xxx_add_device_dynamically(ha, fw_ddb_index);
  1289. return QLA_SUCCESS;
  1290. }
  1291. /* Device already exists in our database. */
  1292. old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
  1293. DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
  1294. "index [%d]\n", ha->host_no, __func__,
  1295. ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
  1296. if (old_fw_ddb_device_state == state &&
  1297. state == DDB_DS_SESSION_ACTIVE) {
  1298. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  1299. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  1300. iscsi_unblock_session(ddb_entry->sess);
  1301. }
  1302. return QLA_SUCCESS;
  1303. }
  1304. ddb_entry->fw_ddb_device_state = state;
  1305. /* Device is back online. */
  1306. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  1307. atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
  1308. atomic_set(&ddb_entry->port_down_timer,
  1309. ha->port_down_retry_count);
  1310. atomic_set(&ddb_entry->relogin_retry_count, 0);
  1311. atomic_set(&ddb_entry->relogin_timer, 0);
  1312. clear_bit(DF_RELOGIN, &ddb_entry->flags);
  1313. clear_bit(DF_NO_RELOGIN, &ddb_entry->flags);
  1314. iscsi_unblock_session(ddb_entry->sess);
  1315. iscsi_session_event(ddb_entry->sess,
  1316. ISCSI_KEVENT_CREATE_SESSION);
  1317. /*
  1318. * Change the lun state to READY in case the lun TIMEOUT before
  1319. * the device came back.
  1320. */
  1321. } else {
  1322. /* Device went away, mark device missing */
  1323. if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE) {
  1324. DEBUG2(dev_info(&ha->pdev->dev, "%s mark missing "
  1325. "ddb_entry 0x%p sess 0x%p conn 0x%p\n",
  1326. __func__, ddb_entry,
  1327. ddb_entry->sess, ddb_entry->conn));
  1328. qla4xxx_mark_device_missing(ha, ddb_entry);
  1329. }
  1330. /*
  1331. * Relogin if device state changed to a not active state.
  1332. * However, do not relogin if a RELOGIN is in process, or
  1333. * we are not allowed to relogin to this DDB.
  1334. */
  1335. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
  1336. !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1337. !test_bit(DF_NO_RELOGIN, &ddb_entry->flags) &&
  1338. qla4_is_relogin_allowed(ha, conn_err)) {
  1339. /*
  1340. * This triggers a relogin. After the relogin_timer
  1341. * expires, the relogin gets scheduled. We must wait a
  1342. * minimum amount of time since receiving an 0x8014 AEN
  1343. * with failed device_state or a logout response before
  1344. * we can issue another relogin.
  1345. */
  1346. /* Firmware pads this timeout: (time2wait +1).
  1347. * Driver retry to login should be longer than F/W.
  1348. * Otherwise F/W will fail
  1349. * set_ddb() mbx cmd with 0x4005 since it still
  1350. * counting down its time2wait.
  1351. */
  1352. atomic_set(&ddb_entry->relogin_timer, 0);
  1353. atomic_set(&ddb_entry->retry_relogin_timer,
  1354. ddb_entry->default_time2wait + 4);
  1355. }
  1356. }
  1357. return QLA_SUCCESS;
  1358. }