ql4_init.c 45 KB

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