PageRenderTime 121ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 3ms

/drivers/scsi/lpfc/lpfc_init.c

http://github.com/mirrors/linux
C | 14064 lines | 9295 code | 1414 blank | 3355 comment | 1383 complexity | 79910e1fdb09cd24495d6340221fbfd4 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2017-2020 Broadcom. All Rights Reserved. The term *
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
  6. * Copyright (C) 2004-2016 Emulex. All rights reserved. *
  7. * EMULEX and SLI are trademarks of Emulex. *
  8. * www.broadcom.com *
  9. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  10. * *
  11. * This program is free software; you can redistribute it and/or *
  12. * modify it under the terms of version 2 of the GNU General *
  13. * Public License as published by the Free Software Foundation. *
  14. * This program is distributed in the hope that it will be useful. *
  15. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  16. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  17. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  18. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  20. * more details, a copy of which can be found in the file COPYING *
  21. * included with this package. *
  22. *******************************************************************/
  23. #include <linux/blkdev.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/idr.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/kthread.h>
  30. #include <linux/pci.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/ctype.h>
  33. #include <linux/aer.h>
  34. #include <linux/slab.h>
  35. #include <linux/firmware.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/percpu.h>
  38. #include <linux/msi.h>
  39. #include <linux/irq.h>
  40. #include <linux/bitops.h>
  41. #include <linux/crash_dump.h>
  42. #include <linux/cpu.h>
  43. #include <linux/cpuhotplug.h>
  44. #include <scsi/scsi.h>
  45. #include <scsi/scsi_device.h>
  46. #include <scsi/scsi_host.h>
  47. #include <scsi/scsi_transport_fc.h>
  48. #include <scsi/scsi_tcq.h>
  49. #include <scsi/fc/fc_fs.h>
  50. #include <linux/nvme-fc-driver.h>
  51. #include "lpfc_hw4.h"
  52. #include "lpfc_hw.h"
  53. #include "lpfc_sli.h"
  54. #include "lpfc_sli4.h"
  55. #include "lpfc_nl.h"
  56. #include "lpfc_disc.h"
  57. #include "lpfc.h"
  58. #include "lpfc_scsi.h"
  59. #include "lpfc_nvme.h"
  60. #include "lpfc_nvmet.h"
  61. #include "lpfc_logmsg.h"
  62. #include "lpfc_crtn.h"
  63. #include "lpfc_vport.h"
  64. #include "lpfc_version.h"
  65. #include "lpfc_ids.h"
  66. static enum cpuhp_state lpfc_cpuhp_state;
  67. /* Used when mapping IRQ vectors in a driver centric manner */
  68. static uint32_t lpfc_present_cpu;
  69. static void __lpfc_cpuhp_remove(struct lpfc_hba *phba);
  70. static void lpfc_cpuhp_remove(struct lpfc_hba *phba);
  71. static void lpfc_cpuhp_add(struct lpfc_hba *phba);
  72. static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
  73. static int lpfc_post_rcv_buf(struct lpfc_hba *);
  74. static int lpfc_sli4_queue_verify(struct lpfc_hba *);
  75. static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
  76. static int lpfc_setup_endian_order(struct lpfc_hba *);
  77. static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
  78. static void lpfc_free_els_sgl_list(struct lpfc_hba *);
  79. static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
  80. static void lpfc_init_sgl_list(struct lpfc_hba *);
  81. static int lpfc_init_active_sgl_array(struct lpfc_hba *);
  82. static void lpfc_free_active_sgl(struct lpfc_hba *);
  83. static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
  84. static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
  85. static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
  86. static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
  87. static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
  88. static void lpfc_sli4_disable_intr(struct lpfc_hba *);
  89. static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
  90. static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
  91. static uint16_t lpfc_find_cpu_handle(struct lpfc_hba *, uint16_t, int);
  92. static void lpfc_setup_bg(struct lpfc_hba *, struct Scsi_Host *);
  93. static struct scsi_transport_template *lpfc_transport_template = NULL;
  94. static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
  95. static DEFINE_IDR(lpfc_hba_index);
  96. #define LPFC_NVMET_BUF_POST 254
  97. /**
  98. * lpfc_config_port_prep - Perform lpfc initialization prior to config port
  99. * @phba: pointer to lpfc hba data structure.
  100. *
  101. * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
  102. * mailbox command. It retrieves the revision information from the HBA and
  103. * collects the Vital Product Data (VPD) about the HBA for preparing the
  104. * configuration of the HBA.
  105. *
  106. * Return codes:
  107. * 0 - success.
  108. * -ERESTART - requests the SLI layer to reset the HBA and try again.
  109. * Any other value - indicates an error.
  110. **/
  111. int
  112. lpfc_config_port_prep(struct lpfc_hba *phba)
  113. {
  114. lpfc_vpd_t *vp = &phba->vpd;
  115. int i = 0, rc;
  116. LPFC_MBOXQ_t *pmb;
  117. MAILBOX_t *mb;
  118. char *lpfc_vpd_data = NULL;
  119. uint16_t offset = 0;
  120. static char licensed[56] =
  121. "key unlock for use with gnu public licensed code only\0";
  122. static int init_key = 1;
  123. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  124. if (!pmb) {
  125. phba->link_state = LPFC_HBA_ERROR;
  126. return -ENOMEM;
  127. }
  128. mb = &pmb->u.mb;
  129. phba->link_state = LPFC_INIT_MBX_CMDS;
  130. if (lpfc_is_LC_HBA(phba->pcidev->device)) {
  131. if (init_key) {
  132. uint32_t *ptext = (uint32_t *) licensed;
  133. for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
  134. *ptext = cpu_to_be32(*ptext);
  135. init_key = 0;
  136. }
  137. lpfc_read_nv(phba, pmb);
  138. memset((char*)mb->un.varRDnvp.rsvd3, 0,
  139. sizeof (mb->un.varRDnvp.rsvd3));
  140. memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
  141. sizeof (licensed));
  142. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  143. if (rc != MBX_SUCCESS) {
  144. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  145. "0324 Config Port initialization "
  146. "error, mbxCmd x%x READ_NVPARM, "
  147. "mbxStatus x%x\n",
  148. mb->mbxCommand, mb->mbxStatus);
  149. mempool_free(pmb, phba->mbox_mem_pool);
  150. return -ERESTART;
  151. }
  152. memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
  153. sizeof(phba->wwnn));
  154. memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
  155. sizeof(phba->wwpn));
  156. }
  157. /*
  158. * Clear all option bits except LPFC_SLI3_BG_ENABLED,
  159. * which was already set in lpfc_get_cfgparam()
  160. */
  161. phba->sli3_options &= (uint32_t)LPFC_SLI3_BG_ENABLED;
  162. /* Setup and issue mailbox READ REV command */
  163. lpfc_read_rev(phba, pmb);
  164. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  165. if (rc != MBX_SUCCESS) {
  166. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  167. "0439 Adapter failed to init, mbxCmd x%x "
  168. "READ_REV, mbxStatus x%x\n",
  169. mb->mbxCommand, mb->mbxStatus);
  170. mempool_free( pmb, phba->mbox_mem_pool);
  171. return -ERESTART;
  172. }
  173. /*
  174. * The value of rr must be 1 since the driver set the cv field to 1.
  175. * This setting requires the FW to set all revision fields.
  176. */
  177. if (mb->un.varRdRev.rr == 0) {
  178. vp->rev.rBit = 0;
  179. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  180. "0440 Adapter failed to init, READ_REV has "
  181. "missing revision information.\n");
  182. mempool_free(pmb, phba->mbox_mem_pool);
  183. return -ERESTART;
  184. }
  185. if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
  186. mempool_free(pmb, phba->mbox_mem_pool);
  187. return -EINVAL;
  188. }
  189. /* Save information as VPD data */
  190. vp->rev.rBit = 1;
  191. memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
  192. vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
  193. memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
  194. vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
  195. memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
  196. vp->rev.biuRev = mb->un.varRdRev.biuRev;
  197. vp->rev.smRev = mb->un.varRdRev.smRev;
  198. vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
  199. vp->rev.endecRev = mb->un.varRdRev.endecRev;
  200. vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
  201. vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
  202. vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
  203. vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
  204. vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
  205. vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
  206. /* If the sli feature level is less then 9, we must
  207. * tear down all RPIs and VPIs on link down if NPIV
  208. * is enabled.
  209. */
  210. if (vp->rev.feaLevelHigh < 9)
  211. phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
  212. if (lpfc_is_LC_HBA(phba->pcidev->device))
  213. memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
  214. sizeof (phba->RandomData));
  215. /* Get adapter VPD information */
  216. lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
  217. if (!lpfc_vpd_data)
  218. goto out_free_mbox;
  219. do {
  220. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
  221. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  222. if (rc != MBX_SUCCESS) {
  223. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  224. "0441 VPD not present on adapter, "
  225. "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
  226. mb->mbxCommand, mb->mbxStatus);
  227. mb->un.varDmp.word_cnt = 0;
  228. }
  229. /* dump mem may return a zero when finished or we got a
  230. * mailbox error, either way we are done.
  231. */
  232. if (mb->un.varDmp.word_cnt == 0)
  233. break;
  234. if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
  235. mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
  236. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  237. lpfc_vpd_data + offset,
  238. mb->un.varDmp.word_cnt);
  239. offset += mb->un.varDmp.word_cnt;
  240. } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
  241. lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
  242. kfree(lpfc_vpd_data);
  243. out_free_mbox:
  244. mempool_free(pmb, phba->mbox_mem_pool);
  245. return 0;
  246. }
  247. /**
  248. * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
  249. * @phba: pointer to lpfc hba data structure.
  250. * @pmboxq: pointer to the driver internal queue element for mailbox command.
  251. *
  252. * This is the completion handler for driver's configuring asynchronous event
  253. * mailbox command to the device. If the mailbox command returns successfully,
  254. * it will set internal async event support flag to 1; otherwise, it will
  255. * set internal async event support flag to 0.
  256. **/
  257. static void
  258. lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
  259. {
  260. if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
  261. phba->temp_sensor_support = 1;
  262. else
  263. phba->temp_sensor_support = 0;
  264. mempool_free(pmboxq, phba->mbox_mem_pool);
  265. return;
  266. }
  267. /**
  268. * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
  269. * @phba: pointer to lpfc hba data structure.
  270. * @pmboxq: pointer to the driver internal queue element for mailbox command.
  271. *
  272. * This is the completion handler for dump mailbox command for getting
  273. * wake up parameters. When this command complete, the response contain
  274. * Option rom version of the HBA. This function translate the version number
  275. * into a human readable string and store it in OptionROMVersion.
  276. **/
  277. static void
  278. lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  279. {
  280. struct prog_id *prg;
  281. uint32_t prog_id_word;
  282. char dist = ' ';
  283. /* character array used for decoding dist type. */
  284. char dist_char[] = "nabx";
  285. if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
  286. mempool_free(pmboxq, phba->mbox_mem_pool);
  287. return;
  288. }
  289. prg = (struct prog_id *) &prog_id_word;
  290. /* word 7 contain option rom version */
  291. prog_id_word = pmboxq->u.mb.un.varWords[7];
  292. /* Decode the Option rom version word to a readable string */
  293. if (prg->dist < 4)
  294. dist = dist_char[prg->dist];
  295. if ((prg->dist == 3) && (prg->num == 0))
  296. snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
  297. prg->ver, prg->rev, prg->lev);
  298. else
  299. snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
  300. prg->ver, prg->rev, prg->lev,
  301. dist, prg->num);
  302. mempool_free(pmboxq, phba->mbox_mem_pool);
  303. return;
  304. }
  305. /**
  306. * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
  307. * cfg_soft_wwnn, cfg_soft_wwpn
  308. * @vport: pointer to lpfc vport data structure.
  309. *
  310. *
  311. * Return codes
  312. * None.
  313. **/
  314. void
  315. lpfc_update_vport_wwn(struct lpfc_vport *vport)
  316. {
  317. uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
  318. u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
  319. /* If the soft name exists then update it using the service params */
  320. if (vport->phba->cfg_soft_wwnn)
  321. u64_to_wwn(vport->phba->cfg_soft_wwnn,
  322. vport->fc_sparam.nodeName.u.wwn);
  323. if (vport->phba->cfg_soft_wwpn)
  324. u64_to_wwn(vport->phba->cfg_soft_wwpn,
  325. vport->fc_sparam.portName.u.wwn);
  326. /*
  327. * If the name is empty or there exists a soft name
  328. * then copy the service params name, otherwise use the fc name
  329. */
  330. if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
  331. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  332. sizeof(struct lpfc_name));
  333. else
  334. memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
  335. sizeof(struct lpfc_name));
  336. /*
  337. * If the port name has changed, then set the Param changes flag
  338. * to unreg the login
  339. */
  340. if (vport->fc_portname.u.wwn[0] != 0 &&
  341. memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
  342. sizeof(struct lpfc_name)))
  343. vport->vport_flag |= FAWWPN_PARAM_CHG;
  344. if (vport->fc_portname.u.wwn[0] == 0 ||
  345. vport->phba->cfg_soft_wwpn ||
  346. (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
  347. vport->vport_flag & FAWWPN_SET) {
  348. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  349. sizeof(struct lpfc_name));
  350. vport->vport_flag &= ~FAWWPN_SET;
  351. if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
  352. vport->vport_flag |= FAWWPN_SET;
  353. }
  354. else
  355. memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
  356. sizeof(struct lpfc_name));
  357. }
  358. /**
  359. * lpfc_config_port_post - Perform lpfc initialization after config port
  360. * @phba: pointer to lpfc hba data structure.
  361. *
  362. * This routine will do LPFC initialization after the CONFIG_PORT mailbox
  363. * command call. It performs all internal resource and state setups on the
  364. * port: post IOCB buffers, enable appropriate host interrupt attentions,
  365. * ELS ring timers, etc.
  366. *
  367. * Return codes
  368. * 0 - success.
  369. * Any other value - error.
  370. **/
  371. int
  372. lpfc_config_port_post(struct lpfc_hba *phba)
  373. {
  374. struct lpfc_vport *vport = phba->pport;
  375. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  376. LPFC_MBOXQ_t *pmb;
  377. MAILBOX_t *mb;
  378. struct lpfc_dmabuf *mp;
  379. struct lpfc_sli *psli = &phba->sli;
  380. uint32_t status, timeout;
  381. int i, j;
  382. int rc;
  383. spin_lock_irq(&phba->hbalock);
  384. /*
  385. * If the Config port completed correctly the HBA is not
  386. * over heated any more.
  387. */
  388. if (phba->over_temp_state == HBA_OVER_TEMP)
  389. phba->over_temp_state = HBA_NORMAL_TEMP;
  390. spin_unlock_irq(&phba->hbalock);
  391. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  392. if (!pmb) {
  393. phba->link_state = LPFC_HBA_ERROR;
  394. return -ENOMEM;
  395. }
  396. mb = &pmb->u.mb;
  397. /* Get login parameters for NID. */
  398. rc = lpfc_read_sparam(phba, pmb, 0);
  399. if (rc) {
  400. mempool_free(pmb, phba->mbox_mem_pool);
  401. return -ENOMEM;
  402. }
  403. pmb->vport = vport;
  404. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  405. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  406. "0448 Adapter failed init, mbxCmd x%x "
  407. "READ_SPARM mbxStatus x%x\n",
  408. mb->mbxCommand, mb->mbxStatus);
  409. phba->link_state = LPFC_HBA_ERROR;
  410. mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
  411. mempool_free(pmb, phba->mbox_mem_pool);
  412. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  413. kfree(mp);
  414. return -EIO;
  415. }
  416. mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
  417. memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
  418. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  419. kfree(mp);
  420. pmb->ctx_buf = NULL;
  421. lpfc_update_vport_wwn(vport);
  422. /* Update the fc_host data structures with new wwn. */
  423. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  424. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  425. fc_host_max_npiv_vports(shost) = phba->max_vpi;
  426. /* If no serial number in VPD data, use low 6 bytes of WWNN */
  427. /* This should be consolidated into parse_vpd ? - mr */
  428. if (phba->SerialNumber[0] == 0) {
  429. uint8_t *outptr;
  430. outptr = &vport->fc_nodename.u.s.IEEE[0];
  431. for (i = 0; i < 12; i++) {
  432. status = *outptr++;
  433. j = ((status & 0xf0) >> 4);
  434. if (j <= 9)
  435. phba->SerialNumber[i] =
  436. (char)((uint8_t) 0x30 + (uint8_t) j);
  437. else
  438. phba->SerialNumber[i] =
  439. (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
  440. i++;
  441. j = (status & 0xf);
  442. if (j <= 9)
  443. phba->SerialNumber[i] =
  444. (char)((uint8_t) 0x30 + (uint8_t) j);
  445. else
  446. phba->SerialNumber[i] =
  447. (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
  448. }
  449. }
  450. lpfc_read_config(phba, pmb);
  451. pmb->vport = vport;
  452. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  453. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  454. "0453 Adapter failed to init, mbxCmd x%x "
  455. "READ_CONFIG, mbxStatus x%x\n",
  456. mb->mbxCommand, mb->mbxStatus);
  457. phba->link_state = LPFC_HBA_ERROR;
  458. mempool_free( pmb, phba->mbox_mem_pool);
  459. return -EIO;
  460. }
  461. /* Check if the port is disabled */
  462. lpfc_sli_read_link_ste(phba);
  463. /* Reset the DFT_HBA_Q_DEPTH to the max xri */
  464. if (phba->cfg_hba_queue_depth > mb->un.varRdConfig.max_xri) {
  465. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  466. "3359 HBA queue depth changed from %d to %d\n",
  467. phba->cfg_hba_queue_depth,
  468. mb->un.varRdConfig.max_xri);
  469. phba->cfg_hba_queue_depth = mb->un.varRdConfig.max_xri;
  470. }
  471. phba->lmt = mb->un.varRdConfig.lmt;
  472. /* Get the default values for Model Name and Description */
  473. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  474. phba->link_state = LPFC_LINK_DOWN;
  475. /* Only process IOCBs on ELS ring till hba_state is READY */
  476. if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
  477. psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
  478. if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
  479. psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
  480. /* Post receive buffers for desired rings */
  481. if (phba->sli_rev != 3)
  482. lpfc_post_rcv_buf(phba);
  483. /*
  484. * Configure HBA MSI-X attention conditions to messages if MSI-X mode
  485. */
  486. if (phba->intr_type == MSIX) {
  487. rc = lpfc_config_msi(phba, pmb);
  488. if (rc) {
  489. mempool_free(pmb, phba->mbox_mem_pool);
  490. return -EIO;
  491. }
  492. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  493. if (rc != MBX_SUCCESS) {
  494. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  495. "0352 Config MSI mailbox command "
  496. "failed, mbxCmd x%x, mbxStatus x%x\n",
  497. pmb->u.mb.mbxCommand,
  498. pmb->u.mb.mbxStatus);
  499. mempool_free(pmb, phba->mbox_mem_pool);
  500. return -EIO;
  501. }
  502. }
  503. spin_lock_irq(&phba->hbalock);
  504. /* Initialize ERATT handling flag */
  505. phba->hba_flag &= ~HBA_ERATT_HANDLED;
  506. /* Enable appropriate host interrupts */
  507. if (lpfc_readl(phba->HCregaddr, &status)) {
  508. spin_unlock_irq(&phba->hbalock);
  509. return -EIO;
  510. }
  511. status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
  512. if (psli->num_rings > 0)
  513. status |= HC_R0INT_ENA;
  514. if (psli->num_rings > 1)
  515. status |= HC_R1INT_ENA;
  516. if (psli->num_rings > 2)
  517. status |= HC_R2INT_ENA;
  518. if (psli->num_rings > 3)
  519. status |= HC_R3INT_ENA;
  520. if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
  521. (phba->cfg_poll & DISABLE_FCP_RING_INT))
  522. status &= ~(HC_R0INT_ENA);
  523. writel(status, phba->HCregaddr);
  524. readl(phba->HCregaddr); /* flush */
  525. spin_unlock_irq(&phba->hbalock);
  526. /* Set up ring-0 (ELS) timer */
  527. timeout = phba->fc_ratov * 2;
  528. mod_timer(&vport->els_tmofunc,
  529. jiffies + msecs_to_jiffies(1000 * timeout));
  530. /* Set up heart beat (HB) timer */
  531. mod_timer(&phba->hb_tmofunc,
  532. jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  533. phba->hb_outstanding = 0;
  534. phba->last_completion_time = jiffies;
  535. /* Set up error attention (ERATT) polling timer */
  536. mod_timer(&phba->eratt_poll,
  537. jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
  538. if (phba->hba_flag & LINK_DISABLED) {
  539. lpfc_printf_log(phba,
  540. KERN_ERR, LOG_INIT,
  541. "2598 Adapter Link is disabled.\n");
  542. lpfc_down_link(phba, pmb);
  543. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  544. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  545. if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
  546. lpfc_printf_log(phba,
  547. KERN_ERR, LOG_INIT,
  548. "2599 Adapter failed to issue DOWN_LINK"
  549. " mbox command rc 0x%x\n", rc);
  550. mempool_free(pmb, phba->mbox_mem_pool);
  551. return -EIO;
  552. }
  553. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  554. mempool_free(pmb, phba->mbox_mem_pool);
  555. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  556. if (rc)
  557. return rc;
  558. }
  559. /* MBOX buffer will be freed in mbox compl */
  560. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  561. if (!pmb) {
  562. phba->link_state = LPFC_HBA_ERROR;
  563. return -ENOMEM;
  564. }
  565. lpfc_config_async(phba, pmb, LPFC_ELS_RING);
  566. pmb->mbox_cmpl = lpfc_config_async_cmpl;
  567. pmb->vport = phba->pport;
  568. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  569. if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
  570. lpfc_printf_log(phba,
  571. KERN_ERR,
  572. LOG_INIT,
  573. "0456 Adapter failed to issue "
  574. "ASYNCEVT_ENABLE mbox status x%x\n",
  575. rc);
  576. mempool_free(pmb, phba->mbox_mem_pool);
  577. }
  578. /* Get Option rom version */
  579. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  580. if (!pmb) {
  581. phba->link_state = LPFC_HBA_ERROR;
  582. return -ENOMEM;
  583. }
  584. lpfc_dump_wakeup_param(phba, pmb);
  585. pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
  586. pmb->vport = phba->pport;
  587. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  588. if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
  589. lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
  590. "to get Option ROM version status x%x\n", rc);
  591. mempool_free(pmb, phba->mbox_mem_pool);
  592. }
  593. return 0;
  594. }
  595. /**
  596. * lpfc_hba_init_link - Initialize the FC link
  597. * @phba: pointer to lpfc hba data structure.
  598. * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
  599. *
  600. * This routine will issue the INIT_LINK mailbox command call.
  601. * It is available to other drivers through the lpfc_hba data
  602. * structure for use as a delayed link up mechanism with the
  603. * module parameter lpfc_suppress_link_up.
  604. *
  605. * Return code
  606. * 0 - success
  607. * Any other value - error
  608. **/
  609. static int
  610. lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
  611. {
  612. return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
  613. }
  614. /**
  615. * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
  616. * @phba: pointer to lpfc hba data structure.
  617. * @fc_topology: desired fc topology.
  618. * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
  619. *
  620. * This routine will issue the INIT_LINK mailbox command call.
  621. * It is available to other drivers through the lpfc_hba data
  622. * structure for use as a delayed link up mechanism with the
  623. * module parameter lpfc_suppress_link_up.
  624. *
  625. * Return code
  626. * 0 - success
  627. * Any other value - error
  628. **/
  629. int
  630. lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
  631. uint32_t flag)
  632. {
  633. struct lpfc_vport *vport = phba->pport;
  634. LPFC_MBOXQ_t *pmb;
  635. MAILBOX_t *mb;
  636. int rc;
  637. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  638. if (!pmb) {
  639. phba->link_state = LPFC_HBA_ERROR;
  640. return -ENOMEM;
  641. }
  642. mb = &pmb->u.mb;
  643. pmb->vport = vport;
  644. if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
  645. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
  646. !(phba->lmt & LMT_1Gb)) ||
  647. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
  648. !(phba->lmt & LMT_2Gb)) ||
  649. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
  650. !(phba->lmt & LMT_4Gb)) ||
  651. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
  652. !(phba->lmt & LMT_8Gb)) ||
  653. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
  654. !(phba->lmt & LMT_10Gb)) ||
  655. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
  656. !(phba->lmt & LMT_16Gb)) ||
  657. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
  658. !(phba->lmt & LMT_32Gb)) ||
  659. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
  660. !(phba->lmt & LMT_64Gb))) {
  661. /* Reset link speed to auto */
  662. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  663. "1302 Invalid speed for this board:%d "
  664. "Reset link speed to auto.\n",
  665. phba->cfg_link_speed);
  666. phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
  667. }
  668. lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
  669. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  670. if (phba->sli_rev < LPFC_SLI_REV4)
  671. lpfc_set_loopback_flag(phba);
  672. rc = lpfc_sli_issue_mbox(phba, pmb, flag);
  673. if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
  674. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  675. "0498 Adapter failed to init, mbxCmd x%x "
  676. "INIT_LINK, mbxStatus x%x\n",
  677. mb->mbxCommand, mb->mbxStatus);
  678. if (phba->sli_rev <= LPFC_SLI_REV3) {
  679. /* Clear all interrupt enable conditions */
  680. writel(0, phba->HCregaddr);
  681. readl(phba->HCregaddr); /* flush */
  682. /* Clear all pending interrupts */
  683. writel(0xffffffff, phba->HAregaddr);
  684. readl(phba->HAregaddr); /* flush */
  685. }
  686. phba->link_state = LPFC_HBA_ERROR;
  687. if (rc != MBX_BUSY || flag == MBX_POLL)
  688. mempool_free(pmb, phba->mbox_mem_pool);
  689. return -EIO;
  690. }
  691. phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
  692. if (flag == MBX_POLL)
  693. mempool_free(pmb, phba->mbox_mem_pool);
  694. return 0;
  695. }
  696. /**
  697. * lpfc_hba_down_link - this routine downs the FC link
  698. * @phba: pointer to lpfc hba data structure.
  699. * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
  700. *
  701. * This routine will issue the DOWN_LINK mailbox command call.
  702. * It is available to other drivers through the lpfc_hba data
  703. * structure for use to stop the link.
  704. *
  705. * Return code
  706. * 0 - success
  707. * Any other value - error
  708. **/
  709. static int
  710. lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
  711. {
  712. LPFC_MBOXQ_t *pmb;
  713. int rc;
  714. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  715. if (!pmb) {
  716. phba->link_state = LPFC_HBA_ERROR;
  717. return -ENOMEM;
  718. }
  719. lpfc_printf_log(phba,
  720. KERN_ERR, LOG_INIT,
  721. "0491 Adapter Link is disabled.\n");
  722. lpfc_down_link(phba, pmb);
  723. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  724. rc = lpfc_sli_issue_mbox(phba, pmb, flag);
  725. if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
  726. lpfc_printf_log(phba,
  727. KERN_ERR, LOG_INIT,
  728. "2522 Adapter failed to issue DOWN_LINK"
  729. " mbox command rc 0x%x\n", rc);
  730. mempool_free(pmb, phba->mbox_mem_pool);
  731. return -EIO;
  732. }
  733. if (flag == MBX_POLL)
  734. mempool_free(pmb, phba->mbox_mem_pool);
  735. return 0;
  736. }
  737. /**
  738. * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
  739. * @phba: pointer to lpfc HBA data structure.
  740. *
  741. * This routine will do LPFC uninitialization before the HBA is reset when
  742. * bringing down the SLI Layer.
  743. *
  744. * Return codes
  745. * 0 - success.
  746. * Any other value - error.
  747. **/
  748. int
  749. lpfc_hba_down_prep(struct lpfc_hba *phba)
  750. {
  751. struct lpfc_vport **vports;
  752. int i;
  753. if (phba->sli_rev <= LPFC_SLI_REV3) {
  754. /* Disable interrupts */
  755. writel(0, phba->HCregaddr);
  756. readl(phba->HCregaddr); /* flush */
  757. }
  758. if (phba->pport->load_flag & FC_UNLOADING)
  759. lpfc_cleanup_discovery_resources(phba->pport);
  760. else {
  761. vports = lpfc_create_vport_work_array(phba);
  762. if (vports != NULL)
  763. for (i = 0; i <= phba->max_vports &&
  764. vports[i] != NULL; i++)
  765. lpfc_cleanup_discovery_resources(vports[i]);
  766. lpfc_destroy_vport_work_array(phba, vports);
  767. }
  768. return 0;
  769. }
  770. /**
  771. * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
  772. * rspiocb which got deferred
  773. *
  774. * @phba: pointer to lpfc HBA data structure.
  775. *
  776. * This routine will cleanup completed slow path events after HBA is reset
  777. * when bringing down the SLI Layer.
  778. *
  779. *
  780. * Return codes
  781. * void.
  782. **/
  783. static void
  784. lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
  785. {
  786. struct lpfc_iocbq *rspiocbq;
  787. struct hbq_dmabuf *dmabuf;
  788. struct lpfc_cq_event *cq_event;
  789. spin_lock_irq(&phba->hbalock);
  790. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  791. spin_unlock_irq(&phba->hbalock);
  792. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  793. /* Get the response iocb from the head of work queue */
  794. spin_lock_irq(&phba->hbalock);
  795. list_remove_head(&phba->sli4_hba.sp_queue_event,
  796. cq_event, struct lpfc_cq_event, list);
  797. spin_unlock_irq(&phba->hbalock);
  798. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  799. case CQE_CODE_COMPL_WQE:
  800. rspiocbq = container_of(cq_event, struct lpfc_iocbq,
  801. cq_event);
  802. lpfc_sli_release_iocbq(phba, rspiocbq);
  803. break;
  804. case CQE_CODE_RECEIVE:
  805. case CQE_CODE_RECEIVE_V1:
  806. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  807. cq_event);
  808. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  809. }
  810. }
  811. }
  812. /**
  813. * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
  814. * @phba: pointer to lpfc HBA data structure.
  815. *
  816. * This routine will cleanup posted ELS buffers after the HBA is reset
  817. * when bringing down the SLI Layer.
  818. *
  819. *
  820. * Return codes
  821. * void.
  822. **/
  823. static void
  824. lpfc_hba_free_post_buf(struct lpfc_hba *phba)
  825. {
  826. struct lpfc_sli *psli = &phba->sli;
  827. struct lpfc_sli_ring *pring;
  828. struct lpfc_dmabuf *mp, *next_mp;
  829. LIST_HEAD(buflist);
  830. int count;
  831. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
  832. lpfc_sli_hbqbuf_free_all(phba);
  833. else {
  834. /* Cleanup preposted buffers on the ELS ring */
  835. pring = &psli->sli3_ring[LPFC_ELS_RING];
  836. spin_lock_irq(&phba->hbalock);
  837. list_splice_init(&pring->postbufq, &buflist);
  838. spin_unlock_irq(&phba->hbalock);
  839. count = 0;
  840. list_for_each_entry_safe(mp, next_mp, &buflist, list) {
  841. list_del(&mp->list);
  842. count++;
  843. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  844. kfree(mp);
  845. }
  846. spin_lock_irq(&phba->hbalock);
  847. pring->postbufq_cnt -= count;
  848. spin_unlock_irq(&phba->hbalock);
  849. }
  850. }
  851. /**
  852. * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
  853. * @phba: pointer to lpfc HBA data structure.
  854. *
  855. * This routine will cleanup the txcmplq after the HBA is reset when bringing
  856. * down the SLI Layer.
  857. *
  858. * Return codes
  859. * void
  860. **/
  861. static void
  862. lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
  863. {
  864. struct lpfc_sli *psli = &phba->sli;
  865. struct lpfc_queue *qp = NULL;
  866. struct lpfc_sli_ring *pring;
  867. LIST_HEAD(completions);
  868. int i;
  869. struct lpfc_iocbq *piocb, *next_iocb;
  870. if (phba->sli_rev != LPFC_SLI_REV4) {
  871. for (i = 0; i < psli->num_rings; i++) {
  872. pring = &psli->sli3_ring[i];
  873. spin_lock_irq(&phba->hbalock);
  874. /* At this point in time the HBA is either reset or DOA
  875. * Nothing should be on txcmplq as it will
  876. * NEVER complete.
  877. */
  878. list_splice_init(&pring->txcmplq, &completions);
  879. pring->txcmplq_cnt = 0;
  880. spin_unlock_irq(&phba->hbalock);
  881. lpfc_sli_abort_iocb_ring(phba, pring);
  882. }
  883. /* Cancel all the IOCBs from the completions list */
  884. lpfc_sli_cancel_iocbs(phba, &completions,
  885. IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
  886. return;
  887. }
  888. list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
  889. pring = qp->pring;
  890. if (!pring)
  891. continue;
  892. spin_lock_irq(&pring->ring_lock);
  893. list_for_each_entry_safe(piocb, next_iocb,
  894. &pring->txcmplq, list)
  895. piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  896. list_splice_init(&pring->txcmplq, &completions);
  897. pring->txcmplq_cnt = 0;
  898. spin_unlock_irq(&pring->ring_lock);
  899. lpfc_sli_abort_iocb_ring(phba, pring);
  900. }
  901. /* Cancel all the IOCBs from the completions list */
  902. lpfc_sli_cancel_iocbs(phba, &completions,
  903. IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
  904. }
  905. /**
  906. * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
  907. int i;
  908. * @phba: pointer to lpfc HBA data structure.
  909. *
  910. * This routine will do uninitialization after the HBA is reset when bring
  911. * down the SLI Layer.
  912. *
  913. * Return codes
  914. * 0 - success.
  915. * Any other value - error.
  916. **/
  917. static int
  918. lpfc_hba_down_post_s3(struct lpfc_hba *phba)
  919. {
  920. lpfc_hba_free_post_buf(phba);
  921. lpfc_hba_clean_txcmplq(phba);
  922. return 0;
  923. }
  924. /**
  925. * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
  926. * @phba: pointer to lpfc HBA data structure.
  927. *
  928. * This routine will do uninitialization after the HBA is reset when bring
  929. * down the SLI Layer.
  930. *
  931. * Return codes
  932. * 0 - success.
  933. * Any other value - error.
  934. **/
  935. static int
  936. lpfc_hba_down_post_s4(struct lpfc_hba *phba)
  937. {
  938. struct lpfc_io_buf *psb, *psb_next;
  939. struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
  940. struct lpfc_sli4_hdw_queue *qp;
  941. LIST_HEAD(aborts);
  942. LIST_HEAD(nvme_aborts);
  943. LIST_HEAD(nvmet_aborts);
  944. struct lpfc_sglq *sglq_entry = NULL;
  945. int cnt, idx;
  946. lpfc_sli_hbqbuf_free_all(phba);
  947. lpfc_hba_clean_txcmplq(phba);
  948. /* At this point in time the HBA is either reset or DOA. Either
  949. * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
  950. * on the lpfc_els_sgl_list so that it can either be freed if the
  951. * driver is unloading or reposted if the driver is restarting
  952. * the port.
  953. */
  954. spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
  955. /* scsl_buf_list */
  956. /* sgl_list_lock required because worker thread uses this
  957. * list.
  958. */
  959. spin_lock(&phba->sli4_hba.sgl_list_lock);
  960. list_for_each_entry(sglq_entry,
  961. &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
  962. sglq_entry->state = SGL_FREED;
  963. list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
  964. &phba->sli4_hba.lpfc_els_sgl_list);
  965. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  966. /* abts_xxxx_buf_list_lock required because worker thread uses this
  967. * list.
  968. */
  969. cnt = 0;
  970. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  971. qp = &phba->sli4_hba.hdwq[idx];
  972. spin_lock(&qp->abts_io_buf_list_lock);
  973. list_splice_init(&qp->lpfc_abts_io_buf_list,
  974. &aborts);
  975. list_for_each_entry_safe(psb, psb_next, &aborts, list) {
  976. psb->pCmd = NULL;
  977. psb->status = IOSTAT_SUCCESS;
  978. cnt++;
  979. }
  980. spin_lock(&qp->io_buf_list_put_lock);
  981. list_splice_init(&aborts, &qp->lpfc_io_buf_list_put);
  982. qp->put_io_bufs += qp->abts_scsi_io_bufs;
  983. qp->put_io_bufs += qp->abts_nvme_io_bufs;
  984. qp->abts_scsi_io_bufs = 0;
  985. qp->abts_nvme_io_bufs = 0;
  986. spin_unlock(&qp->io_buf_list_put_lock);
  987. spin_unlock(&qp->abts_io_buf_list_lock);
  988. }
  989. spin_unlock_irq(&phba->hbalock);
  990. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  991. spin_lock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
  992. list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
  993. &nvmet_aborts);
  994. spin_unlock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
  995. list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
  996. ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
  997. lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
  998. }
  999. }
  1000. lpfc_sli4_free_sp_events(phba);
  1001. return cnt;
  1002. }
  1003. /**
  1004. * lpfc_hba_down_post - Wrapper func for hba down post routine
  1005. * @phba: pointer to lpfc HBA data structure.
  1006. *
  1007. * This routine wraps the actual SLI3 or SLI4 routine for performing
  1008. * uninitialization after the HBA is reset when bring down the SLI Layer.
  1009. *
  1010. * Return codes
  1011. * 0 - success.
  1012. * Any other value - error.
  1013. **/
  1014. int
  1015. lpfc_hba_down_post(struct lpfc_hba *phba)
  1016. {
  1017. return (*phba->lpfc_hba_down_post)(phba);
  1018. }
  1019. /**
  1020. * lpfc_hb_timeout - The HBA-timer timeout handler
  1021. * @ptr: unsigned long holds the pointer to lpfc hba data structure.
  1022. *
  1023. * This is the HBA-timer timeout handler registered to the lpfc driver. When
  1024. * this timer fires, a HBA timeout event shall be posted to the lpfc driver
  1025. * work-port-events bitmap and the worker thread is notified. This timeout
  1026. * event will be used by the worker thread to invoke the actual timeout
  1027. * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
  1028. * be performed in the timeout handler and the HBA timeout event bit shall
  1029. * be cleared by the worker thread after it has taken the event bitmap out.
  1030. **/
  1031. static void
  1032. lpfc_hb_timeout(struct timer_list *t)
  1033. {
  1034. struct lpfc_hba *phba;
  1035. uint32_t tmo_posted;
  1036. unsigned long iflag;
  1037. phba = from_timer(phba, t, hb_tmofunc);
  1038. /* Check for heart beat timeout conditions */
  1039. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  1040. tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
  1041. if (!tmo_posted)
  1042. phba->pport->work_port_events |= WORKER_HB_TMO;
  1043. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  1044. /* Tell the worker thread there is work to do */
  1045. if (!tmo_posted)
  1046. lpfc_worker_wake_up(phba);
  1047. return;
  1048. }
  1049. /**
  1050. * lpfc_rrq_timeout - The RRQ-timer timeout handler
  1051. * @ptr: unsigned long holds the pointer to lpfc hba data structure.
  1052. *
  1053. * This is the RRQ-timer timeout handler registered to the lpfc driver. When
  1054. * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
  1055. * work-port-events bitmap and the worker thread is notified. This timeout
  1056. * event will be used by the worker thread to invoke the actual timeout
  1057. * handler routine, lpfc_rrq_handler. Any periodical operations will
  1058. * be performed in the timeout handler and the RRQ timeout event bit shall
  1059. * be cleared by the worker thread after it has taken the event bitmap out.
  1060. **/
  1061. static void
  1062. lpfc_rrq_timeout(struct timer_list *t)
  1063. {
  1064. struct lpfc_hba *phba;
  1065. unsigned long iflag;
  1066. phba = from_timer(phba, t, rrq_tmr);
  1067. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  1068. if (!(phba->pport->load_flag & FC_UNLOADING))
  1069. phba->hba_flag |= HBA_RRQ_ACTIVE;
  1070. else
  1071. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  1072. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  1073. if (!(phba->pport->load_flag & FC_UNLOADING))
  1074. lpfc_worker_wake_up(phba);
  1075. }
  1076. /**
  1077. * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
  1078. * @phba: pointer to lpfc hba data structure.
  1079. * @pmboxq: pointer to the driver internal queue element for mailbox command.
  1080. *
  1081. * This is the callback function to the lpfc heart-beat mailbox command.
  1082. * If configured, the lpfc driver issues the heart-beat mailbox command to
  1083. * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
  1084. * heart-beat mailbox command is issued, the driver shall set up heart-beat
  1085. * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
  1086. * heart-beat outstanding state. Once the mailbox command comes back and
  1087. * no error conditions detected, the heart-beat mailbox command timer is
  1088. * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
  1089. * state is cleared for the next heart-beat. If the timer expired with the
  1090. * heart-beat outstanding state set, the driver will put the HBA offline.
  1091. **/
  1092. static void
  1093. lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
  1094. {
  1095. unsigned long drvr_flag;
  1096. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1097. phba->hb_outstanding = 0;
  1098. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1099. /* Check and reset heart-beat timer is necessary */
  1100. mempool_free(pmboxq, phba->mbox_mem_pool);
  1101. if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
  1102. !(phba->link_state == LPFC_HBA_ERROR) &&
  1103. !(phba->pport->load_flag & FC_UNLOADING))
  1104. mod_timer(&phba->hb_tmofunc,
  1105. jiffies +
  1106. msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  1107. return;
  1108. }
  1109. static void
  1110. lpfc_hb_eq_delay_work(struct work_struct *work)
  1111. {
  1112. struct lpfc_hba *phba = container_of(to_delayed_work(work),
  1113. struct lpfc_hba, eq_delay_work);
  1114. struct lpfc_eq_intr_info *eqi, *eqi_new;
  1115. struct lpfc_queue *eq, *eq_next;
  1116. unsigned char *ena_delay = NULL;
  1117. uint32_t usdelay;
  1118. int i;
  1119. if (!phba->cfg_auto_imax || phba->pport->load_flag & FC_UNLOADING)
  1120. return;
  1121. if (phba->link_state == LPFC_HBA_ERROR ||
  1122. phba->pport->fc_flag & FC_OFFLINE_MODE)
  1123. goto requeue;
  1124. ena_delay = kcalloc(phba->sli4_hba.num_possible_cpu, sizeof(*ena_delay),
  1125. GFP_KERNEL);
  1126. if (!ena_delay)
  1127. goto requeue;
  1128. for (i = 0; i < phba->cfg_irq_chann; i++) {
  1129. /* Get the EQ corresponding to the IRQ vector */
  1130. eq = phba->sli4_hba.hba_eq_hdl[i].eq;
  1131. if (!eq)
  1132. continue;
  1133. if (eq->q_mode || eq->q_flag & HBA_EQ_DELAY_CHK) {
  1134. eq->q_flag &= ~HBA_EQ_DELAY_CHK;
  1135. ena_delay[eq->last_cpu] = 1;
  1136. }
  1137. }
  1138. for_each_present_cpu(i) {
  1139. eqi = per_cpu_ptr(phba->sli4_hba.eq_info, i);
  1140. if (ena_delay[i]) {
  1141. usdelay = (eqi->icnt >> 10) * LPFC_EQ_DELAY_STEP;
  1142. if (usdelay > LPFC_MAX_AUTO_EQ_DELAY)
  1143. usdelay = LPFC_MAX_AUTO_EQ_DELAY;
  1144. } else {
  1145. usdelay = 0;
  1146. }
  1147. eqi->icnt = 0;
  1148. list_for_each_entry_safe(eq, eq_next, &eqi->list, cpu_list) {
  1149. if (unlikely(eq->last_cpu != i)) {
  1150. eqi_new = per_cpu_ptr(phba->sli4_hba.eq_info,
  1151. eq->last_cpu);
  1152. list_move_tail(&eq->cpu_list, &eqi_new->list);
  1153. continue;
  1154. }
  1155. if (usdelay != eq->q_mode)
  1156. lpfc_modify_hba_eq_delay(phba, eq->hdwq, 1,
  1157. usdelay);
  1158. }
  1159. }
  1160. kfree(ena_delay);
  1161. requeue:
  1162. queue_delayed_work(phba->wq, &phba->eq_delay_work,
  1163. msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
  1164. }
  1165. /**
  1166. * lpfc_hb_mxp_handler - Multi-XRI pools handler to adjust XRI distribution
  1167. * @phba: pointer to lpfc hba data structure.
  1168. *
  1169. * For each heartbeat, this routine does some heuristic methods to adjust
  1170. * XRI distribution. The goal is to fully utilize free XRIs.
  1171. **/
  1172. static void lpfc_hb_mxp_handler(struct lpfc_hba *phba)
  1173. {
  1174. u32 i;
  1175. u32 hwq_count;
  1176. hwq_count = phba->cfg_hdw_queue;
  1177. for (i = 0; i < hwq_count; i++) {
  1178. /* Adjust XRIs in private pool */
  1179. lpfc_adjust_pvt_pool_count(phba, i);
  1180. /* Adjust high watermark */
  1181. lpfc_adjust_high_watermark(phba, i);
  1182. #ifdef LPFC_MXP_STAT
  1183. /* Snapshot pbl, pvt and busy count */
  1184. lpfc_snapshot_mxp(phba, i);
  1185. #endif
  1186. }
  1187. }
  1188. /**
  1189. * lpfc_hb_timeout_handler - The HBA-timer timeout handler
  1190. * @phba: pointer to lpfc hba data structure.
  1191. *
  1192. * This is the actual HBA-timer timeout handler to be invoked by the worker
  1193. * thread whenever the HBA timer fired and HBA-timeout event posted. This
  1194. * handler performs any periodic operations needed for the device. If such
  1195. * periodic event has already been attended to either in the interrupt handler
  1196. * or by processing slow-ring or fast-ring events within the HBA-timer
  1197. * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
  1198. * the timer for the next timeout period. If lpfc heart-beat mailbox command
  1199. * is configured and there is no heart-beat mailbox command outstanding, a
  1200. * heart-beat mailbox is issued and timer set properly. Otherwise, if there
  1201. * has been a heart-beat mailbox command outstanding, the HBA shall be put
  1202. * to offline.
  1203. **/
  1204. void
  1205. lpfc_hb_timeout_handler(struct lpfc_hba *phba)
  1206. {
  1207. struct lpfc_vport **vports;
  1208. LPFC_MBOXQ_t *pmboxq;
  1209. struct lpfc_dmabuf *buf_ptr;
  1210. int retval, i;
  1211. struct lpfc_sli *psli = &phba->sli;
  1212. LIST_HEAD(completions);
  1213. if (phba->cfg_xri_rebalancing) {
  1214. /* Multi-XRI pools handler */
  1215. lpfc_hb_mxp_handler(phba);
  1216. }
  1217. vports = lpfc_create_vport_work_array(phba);
  1218. if (vports != NULL)
  1219. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  1220. lpfc_rcv_seq_check_edtov(vports[i]);
  1221. lpfc_fdmi_change_check(vports[i]);
  1222. }
  1223. lpfc_destroy_vport_work_array(phba, vports);
  1224. if ((phba->link_state == LPFC_HBA_ERROR) ||
  1225. (phba->pport->load_flag & FC_UNLOADING) ||
  1226. (phba->pport->fc_flag & FC_OFFLINE_MODE))
  1227. return;
  1228. spin_lock_irq(&phba->pport->work_port_lock);
  1229. if (time_after(phba->last_completion_time +
  1230. msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
  1231. jiffies)) {
  1232. spin_unlock_irq(&phba->pport->work_port_lock);
  1233. if (!phba->hb_outstanding)
  1234. mod_timer(&phba->hb_tmofunc,
  1235. jiffies +
  1236. msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  1237. else
  1238. mod_timer(&phba->hb_tmofunc,
  1239. jiffies +
  1240. msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
  1241. return;
  1242. }
  1243. spin_unlock_irq(&phba->pport->work_port_lock);
  1244. if (phba->elsbuf_cnt &&
  1245. (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
  1246. spin_lock_irq(&phba->hbalock);
  1247. list_splice_init(&phba->elsbuf, &completions);
  1248. phba->elsbuf_cnt = 0;
  1249. phba->elsbuf_prev_cnt = 0;
  1250. spin_unlock_irq(&phba->hbalock);
  1251. while (!list_empty(&completions)) {
  1252. list_remove_head(&completions, buf_ptr,
  1253. struct lpfc_dmabuf, list);
  1254. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  1255. kfree(buf_ptr);
  1256. }
  1257. }
  1258. phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
  1259. /* If there is no heart beat outstanding, issue a heartbeat command */
  1260. if (phba->cfg_enable_hba_heartbeat) {
  1261. if (!phba->hb_outstanding) {
  1262. if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
  1263. (list_empty(&psli->mboxq))) {
  1264. pmboxq = mempool_alloc(phba->mbox_mem_pool,
  1265. GFP_KERNEL);
  1266. if (!pmboxq) {
  1267. mod_timer(&phba->hb_tmofunc,
  1268. jiffies +
  1269. msecs_to_jiffies(1000 *
  1270. LPFC_HB_MBOX_INTERVAL));
  1271. return;
  1272. }
  1273. lpfc_heart_beat(phba, pmboxq);
  1274. pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
  1275. pmboxq->vport = phba->pport;
  1276. retval = lpfc_sli_issue_mbox(phba, pmboxq,
  1277. MBX_NOWAIT);
  1278. if (retval != MBX_BUSY &&
  1279. retval != MBX_SUCCESS) {
  1280. mempool_free(pmboxq,
  1281. phba->mbox_mem_pool);
  1282. mod_timer(&phba->hb_tmofunc,
  1283. jiffies +
  1284. msecs_to_jiffies(1000 *
  1285. LPFC_HB_MBOX_INTERVAL));
  1286. return;
  1287. }
  1288. phba->skipped_hb = 0;
  1289. phba->hb_outstanding = 1;
  1290. } else if (time_before_eq(phba->last_completion_time,
  1291. phba->skipped_hb)) {
  1292. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  1293. "2857 Last completion time not "
  1294. " updated in %d ms\n",
  1295. jiffies_to_msecs(jiffies
  1296. - phba->last_completion_time));
  1297. } else
  1298. phba->skipped_hb = jiffies;
  1299. mod_timer(&phba->hb_tmofunc,
  1300. jiffies +
  1301. msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
  1302. return;
  1303. } else {
  1304. /*
  1305. * If heart beat timeout called with hb_outstanding set
  1306. * we need to give the hb mailbox cmd a chance to
  1307. * complete or TMO.
  1308. */
  1309. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1310. "0459 Adapter heartbeat still out"
  1311. "standing:last compl time was %d ms.\n",
  1312. jiffies_to_msecs(jiffies
  1313. - phba->last_completion_time));
  1314. mod_timer(&phba->hb_tmofunc,
  1315. jiffies +
  1316. msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
  1317. }
  1318. } else {
  1319. mod_timer(&phba->hb_tmofunc,
  1320. jiffies +
  1321. msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  1322. }
  1323. }
  1324. /**
  1325. * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
  1326. * @phba: pointer to lpfc hba data structure.
  1327. *
  1328. * This routine is called to bring the HBA offline when HBA hardware error
  1329. * other than Port Error 6 has been detected.
  1330. **/
  1331. static void
  1332. lpfc_offline_eratt(struct lpfc_hba *phba)
  1333. {
  1334. struct lpfc_sli *psli = &phba->sli;
  1335. spin_lock_irq(&phba->hbalock);
  1336. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  1337. spin_unlock_irq(&phba->hbalock);
  1338. lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
  1339. lpfc_offline(phba);
  1340. lpfc_reset_barrier(phba);
  1341. spin_lock_irq(&phba->hbalock);
  1342. lpfc_sli_brdreset(phba);
  1343. spin_unlock_irq(&phba->hbalock);
  1344. lpfc_hba_down_post(phba);
  1345. lpfc_sli_brdready(phba, HS_MBRDY);
  1346. lpfc_unblock_mgmt_io(phba);
  1347. phba->link_state = LPFC_HBA_ERROR;
  1348. return;
  1349. }
  1350. /**
  1351. * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
  1352. * @phba: pointer to lpfc hba data structure.
  1353. *
  1354. * This routine is called to bring a SLI4 HBA offline when HBA hardware error
  1355. * other than Port Error 6 has been detected.
  1356. **/
  1357. void
  1358. lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
  1359. {
  1360. spin_lock_irq(&phba->hbalock);
  1361. phba->link_state = LPFC_HBA_ERROR;
  1362. spin_unlock_irq(&phba->hbalock);
  1363. lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
  1364. lpfc_sli_flush_io_rings(phba);
  1365. lpfc_offline(phba);
  1366. lpfc_hba_down_post(phba);
  1367. lpfc_unblock_mgmt_io(phba);
  1368. }
  1369. /**
  1370. * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
  1371. * @phba: pointer to lpfc hba data structure.
  1372. *
  1373. * This routine is invoked to handle the deferred HBA hardware error
  1374. * conditions. This type of error is indicated by HBA by setting ER1
  1375. * and another ER bit in the host status register. The driver will
  1376. * wait until the ER1 bit clears before handling the error condition.
  1377. **/
  1378. static void
  1379. lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
  1380. {
  1381. uint32_t old_host_status = phba->work_hs;
  1382. struct lpfc_sli *psli = &phba->sli;
  1383. /* If the pci channel is offline, ignore possible errors,
  1384. * since we cannot communicate with the pci card anyway.
  1385. */
  1386. if (pci_channel_offline(phba->pcidev)) {
  1387. spin_lock_irq(&phba->hbalock);
  1388. phba->hba_flag &= ~DEFER_ERATT;
  1389. spin_unlock_irq(&phba->hbalock);
  1390. return;
  1391. }
  1392. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1393. "0479 Deferred Adapter Hardware Error "
  1394. "Data: x%x x%x x%x\n",
  1395. phba->work_hs,
  1396. phba->work_status[0], phba->work_status[1]);
  1397. spin_lock_irq(&phba->hbalock);
  1398. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  1399. spin_unlock_irq(&phba->hbalock);
  1400. /*
  1401. * Firmware stops when it triggred erratt. That could cause the I/Os
  1402. * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
  1403. * SCSI layer retry it after re-establishing link.
  1404. */
  1405. lpfc_sli_abort_fcp_rings(phba);
  1406. /*
  1407. * There was a firmware error. Take the hba offline and then
  1408. * attempt to restart it.
  1409. */
  1410. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  1411. lpfc_offline(phba);
  1412. /* Wait for the ER1 bit to clear.*/
  1413. while (phba->work_hs & HS_FFER1) {
  1414. msleep(100);
  1415. if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
  1416. phba->work_hs = UNPLUG_ERR ;
  1417. break;
  1418. }
  1419. /* If driver is unloading let the worker thread continue */
  1420. if (phba->pport->load_flag & FC_UNLOADING) {
  1421. phba->work_hs = 0;
  1422. break;
  1423. }
  1424. }
  1425. /*
  1426. * This is to ptrotect against a race condition in which
  1427. * first write to the host attention register clear the
  1428. * host status register.
  1429. */
  1430. if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
  1431. phba->work_hs = old_host_status & ~HS_FFER1;
  1432. spin_lock_irq(&phba->hbalock);
  1433. phba->hba_flag &= ~DEFER_ERATT;
  1434. spin_unlock_irq(&phba->hbalock);
  1435. phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
  1436. phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
  1437. }
  1438. static void
  1439. lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
  1440. {
  1441. struct lpfc_board_event_header board_event;
  1442. struct Scsi_Host *shost;
  1443. board_event.event_type = FC_REG_BOARD_EVENT;
  1444. board_event.subcategory = LPFC_EVENT_PORTINTERR;
  1445. shost = lpfc_shost_from_vport(phba->pport);
  1446. fc_host_post_vendor_event(shost, fc_get_event_number(),
  1447. sizeof(board_event),
  1448. (char *) &board_event,
  1449. LPFC_NL_VENDOR_ID);
  1450. }
  1451. /**
  1452. * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
  1453. * @phba: pointer to lpfc hba data structure.
  1454. *
  1455. * This routine is invoked to handle the following HBA hardware error
  1456. * conditions:
  1457. * 1 - HBA error attention interrupt
  1458. * 2 - DMA ring index out of range
  1459. * 3 - Mailbox command came back as unknown
  1460. **/
  1461. static void
  1462. lpfc_handle_eratt_s3(struct lpfc_hba *phba)
  1463. {
  1464. struct lpfc_vport *vport = phba->pport;
  1465. struct lpfc_sli *psli = &phba->sli;
  1466. uint32_t event_data;
  1467. unsigned long temperature;
  1468. struct temp_event temp_event_data;
  1469. struct Scsi_Host *shost;
  1470. /* If the pci channel is offline, ignore possible errors,
  1471. * since we cannot communicate with the pci card anyway.
  1472. */
  1473. if (pci_channel_offline(phba->pcidev)) {
  1474. spin_lock_irq(&phba->hbalock);
  1475. phba->hba_flag &= ~DEFER_ERATT;
  1476. spin_unlock_irq(&phba->hbalock);
  1477. return;
  1478. }
  1479. /* If resets are disabled then leave the HBA alone and return */
  1480. if (!phba->cfg_enable_hba_reset)
  1481. return;
  1482. /* Send an internal error event to mgmt application */
  1483. lpfc_board_errevt_to_mgmt(phba);
  1484. if (phba->hba_flag & DEFER_ERATT)
  1485. lpfc_handle_deferred_eratt(phba);
  1486. if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
  1487. if (phba->work_hs & HS_FFER6)
  1488. /* Re-establishing Link */
  1489. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  1490. "1301 Re-establishing Link "
  1491. "Data: x%x x%x x%x\n",
  1492. phba->work_hs, phba->work_status[0],
  1493. phba->work_status[1]);
  1494. if (phba->work_hs & HS_FFER8)
  1495. /* Device Zeroization */
  1496. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  1497. "2861 Host Authentication device "
  1498. "zeroization Data:x%x x%x x%x\n",
  1499. phba->work_hs, phba->work_status[0],
  1500. phba->work_status[1]);
  1501. spin_lock_irq(&phba->hbalock);
  1502. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  1503. spin_unlock_irq(&phba->hbalock);
  1504. /*
  1505. * Firmware stops when it triggled erratt with HS_FFER6.
  1506. * That could cause the I/Os dropped by the firmware.
  1507. * Error iocb (I/O) on txcmplq and let the SCSI layer
  1508. * retry it after re-establishing link.
  1509. */
  1510. lpfc_sli_abort_fcp_rings(phba);
  1511. /*
  1512. * There was a firmware error. Take the hba offline and then
  1513. * attempt to restart it.
  1514. */
  1515. lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
  1516. lpfc_offline(phba);
  1517. lpfc_sli_brdrestart(phba);
  1518. if (lpfc_online(phba) == 0) { /* Initialize the HBA */
  1519. lpfc_unblock_mgmt_io(phba);
  1520. return;
  1521. }
  1522. lpfc_unblock_mgmt_io(phba);
  1523. } else if (phba->work_hs & HS_CRIT_TEMP) {
  1524. temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
  1525. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  1526. temp_event_data.event_code = LPFC_CRIT_TEMP;
  1527. temp_event_data.data = (uint32_t)temperature;
  1528. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1529. "0406 Adapter maximum temperature exceeded "
  1530. "(%ld), taking this port offline "
  1531. "Data: x%x x%x x%x\n",
  1532. temperature, phba->work_hs,
  1533. phba->work_status[0], phba->work_status[1]);
  1534. shost = lpfc_shost_from_vport(phba->pport);
  1535. fc_host_post_vendor_event(shost, fc_get_event_number(),
  1536. sizeof(temp_event_data),
  1537. (char *) &temp_event_data,
  1538. SCSI_NL_VID_TYPE_PCI
  1539. | PCI_VENDOR_ID_EMULEX);
  1540. spin_lock_irq(&phba->hbalock);
  1541. phba->over_temp_state = HBA_OVER_TEMP;
  1542. spin_unlock_irq(&phba->hbalock);
  1543. lpfc_offline_eratt(phba);
  1544. } else {
  1545. /* The if clause above forces this code path when the status
  1546. * failure is a value other than FFER6. Do not call the offline
  1547. * twice. This is the adapter hardware error path.
  1548. */
  1549. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1550. "0457 Adapter Hardware Error "
  1551. "Data: x%x x%x x%x\n",
  1552. phba->work_hs,
  1553. phba->work_status[0], phba->work_status[1]);
  1554. event_data = FC_REG_DUMP_EVENT;
  1555. shost = lpfc_shost_from_vport(vport);
  1556. fc_host_post_vendor_event(shost, fc_get_event_number(),
  1557. sizeof(event_data), (char *) &event_data,
  1558. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  1559. lpfc_offline_eratt(phba);
  1560. }
  1561. return;
  1562. }
  1563. /**
  1564. * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
  1565. * @phba: pointer to lpfc hba data structure.
  1566. * @mbx_action: flag for mailbox shutdown action.
  1567. *
  1568. * This routine is invoked to perform an SLI4 port PCI function reset in
  1569. * response to port status register polling attention. It waits for port
  1570. * status register (ERR, RDY, RN) bits before proceeding with function reset.
  1571. * During this process, interrupt vectors are freed and later requested
  1572. * for handling possible port resource change.
  1573. **/
  1574. static int
  1575. lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
  1576. bool en_rn_msg)
  1577. {
  1578. int rc;
  1579. uint32_t intr_mode;
  1580. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
  1581. LPFC_SLI_INTF_IF_TYPE_2) {
  1582. /*
  1583. * On error status condition, driver need to wait for port
  1584. * ready before performing reset.
  1585. */
  1586. rc = lpfc_sli4_pdev_status_reg_wait(phba);
  1587. if (rc)
  1588. return rc;
  1589. }
  1590. /* need reset: attempt for port recovery */
  1591. if (en_rn_msg)
  1592. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1593. "2887 Reset Needed: Attempting Port "
  1594. "Recovery...\n");
  1595. lpfc_offline_prep(phba, mbx_action);
  1596. lpfc_sli_flush_io_rings(phba);
  1597. lpfc_offline(phba);
  1598. /* release interrupt for possible resource change */
  1599. lpfc_sli4_disable_intr(phba);
  1600. rc = lpfc_sli_brdrestart(phba);
  1601. if (rc) {
  1602. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1603. "6309 Failed to restart board\n");
  1604. return rc;
  1605. }
  1606. /* request and enable interrupt */
  1607. intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
  1608. if (intr_mode == LPFC_INTR_ERROR) {
  1609. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1610. "3175 Failed to enable interrupt\n");
  1611. return -EIO;
  1612. }
  1613. phba->intr_mode = intr_mode;
  1614. rc = lpfc_online(phba);
  1615. if (rc == 0)
  1616. lpfc_unblock_mgmt_io(phba);
  1617. return rc;
  1618. }
  1619. /**
  1620. * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
  1621. * @phba: pointer to lpfc hba data structure.
  1622. *
  1623. * This routine is invoked to handle the SLI4 HBA hardware error attention
  1624. * conditions.
  1625. **/
  1626. static void
  1627. lpfc_handle_eratt_s4(struct lpfc_hba *phba)
  1628. {
  1629. struct lpfc_vport *vport = phba->pport;
  1630. uint32_t event_data;
  1631. struct Scsi_Host *shost;
  1632. uint32_t if_type;
  1633. struct lpfc_register portstat_reg = {0};
  1634. uint32_t reg_err1, reg_err2;
  1635. uint32_t uerrlo_reg, uemasklo_reg;
  1636. uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
  1637. bool en_rn_msg = true;
  1638. struct temp_event temp_event_data;
  1639. struct lpfc_register portsmphr_reg;
  1640. int rc, i;
  1641. /* If the pci channel is offline, ignore possible errors, since
  1642. * we cannot communicate with the pci card anyway.
  1643. */
  1644. if (pci_channel_offline(phba->pcidev)) {
  1645. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1646. "3166 pci channel is offline\n");
  1647. lpfc_sli4_offline_eratt(phba);
  1648. return;
  1649. }
  1650. memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
  1651. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  1652. switch (if_type) {
  1653. case LPFC_SLI_INTF_IF_TYPE_0:
  1654. pci_rd_rc1 = lpfc_readl(
  1655. phba->sli4_hba.u.if_type0.UERRLOregaddr,
  1656. &uerrlo_reg);
  1657. pci_rd_rc2 = lpfc_readl(
  1658. phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
  1659. &uemasklo_reg);
  1660. /* consider PCI bus read error as pci_channel_offline */
  1661. if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
  1662. return;
  1663. if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
  1664. lpfc_sli4_offline_eratt(phba);
  1665. return;
  1666. }
  1667. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1668. "7623 Checking UE recoverable");
  1669. for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
  1670. if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  1671. &portsmphr_reg.word0))
  1672. continue;
  1673. smphr_port_status = bf_get(lpfc_port_smphr_port_status,
  1674. &portsmphr_reg);
  1675. if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
  1676. LPFC_PORT_SEM_UE_RECOVERABLE)
  1677. break;
  1678. /*Sleep for 1Sec, before checking SEMAPHORE */
  1679. msleep(1000);
  1680. }
  1681. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1682. "4827 smphr_port_status x%x : Waited %dSec",
  1683. smphr_port_status, i);
  1684. /* Recoverable UE, reset the HBA device */
  1685. if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
  1686. LPFC_PORT_SEM_UE_RECOVERABLE) {
  1687. for (i = 0; i < 20; i++) {
  1688. msleep(1000);
  1689. if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  1690. &portsmphr_reg.word0) &&
  1691. (LPFC_POST_STAGE_PORT_READY ==
  1692. bf_get(lpfc_port_smphr_port_status,
  1693. &portsmphr_reg))) {
  1694. rc = lpfc_sli4_port_sta_fn_reset(phba,
  1695. LPFC_MBX_NO_WAIT, en_rn_msg);
  1696. if (rc == 0)
  1697. return;
  1698. lpfc_printf_log(phba,
  1699. KERN_ERR, LOG_INIT,
  1700. "4215 Failed to recover UE");
  1701. break;
  1702. }
  1703. }
  1704. }
  1705. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1706. "7624 Firmware not ready: Failing UE recovery,"
  1707. " waited %dSec", i);
  1708. phba->link_state = LPFC_HBA_ERROR;
  1709. break;
  1710. case LPFC_SLI_INTF_IF_TYPE_2:
  1711. case LPFC_SLI_INTF_IF_TYPE_6:
  1712. pci_rd_rc1 = lpfc_readl(
  1713. phba->sli4_hba.u.if_type2.STATUSregaddr,
  1714. &portstat_reg.word0);
  1715. /* consider PCI bus read error as pci_channel_offline */
  1716. if (pci_rd_rc1 == -EIO) {
  1717. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1718. "3151 PCI bus read access failure: x%x\n",
  1719. readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
  1720. lpfc_sli4_offline_eratt(phba);
  1721. return;
  1722. }
  1723. reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  1724. reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  1725. if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
  1726. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1727. "2889 Port Overtemperature event, "
  1728. "taking port offline Data: x%x x%x\n",
  1729. reg_err1, reg_err2);
  1730. phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
  1731. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  1732. temp_event_data.event_code = LPFC_CRIT_TEMP;
  1733. temp_event_data.data = 0xFFFFFFFF;
  1734. shost = lpfc_shost_from_vport(phba->pport);
  1735. fc_host_post_vendor_event(shost, fc_get_event_number(),
  1736. sizeof(temp_event_data),
  1737. (char *)&temp_event_data,
  1738. SCSI_NL_VID_TYPE_PCI
  1739. | PCI_VENDOR_ID_EMULEX);
  1740. spin_lock_irq(&phba->hbalock);
  1741. phba->over_temp_state = HBA_OVER_TEMP;
  1742. spin_unlock_irq(&phba->hbalock);
  1743. lpfc_sli4_offline_eratt(phba);
  1744. return;
  1745. }
  1746. if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
  1747. reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
  1748. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1749. "3143 Port Down: Firmware Update "
  1750. "Detected\n");
  1751. en_rn_msg = false;
  1752. } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
  1753. reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
  1754. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1755. "3144 Port Down: Debug Dump\n");
  1756. else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
  1757. reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
  1758. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1759. "3145 Port Down: Provisioning\n");
  1760. /* If resets are disabled then leave the HBA alone and return */
  1761. if (!phba->cfg_enable_hba_reset)
  1762. return;
  1763. /* Check port status register for function reset */
  1764. rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
  1765. en_rn_msg);
  1766. if (rc == 0) {
  1767. /* don't report event on forced debug dump */
  1768. if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
  1769. reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
  1770. return;
  1771. else
  1772. break;
  1773. }
  1774. /* fall through for not able to recover */
  1775. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1776. "3152 Unrecoverable error\n");
  1777. phba->link_state = LPFC_HBA_ERROR;
  1778. break;
  1779. case LPFC_SLI_INTF_IF_TYPE_1:
  1780. default:
  1781. break;
  1782. }
  1783. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1784. "3123 Report dump event to upper layer\n");
  1785. /* Send an internal error event to mgmt application */
  1786. lpfc_board_errevt_to_mgmt(phba);
  1787. event_data = FC_REG_DUMP_EVENT;
  1788. shost = lpfc_shost_from_vport(vport);
  1789. fc_host_post_vendor_event(shost, fc_get_event_number(),
  1790. sizeof(event_data), (char *) &event_data,
  1791. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  1792. }
  1793. /**
  1794. * lpfc_handle_eratt - Wrapper func for handling hba error attention
  1795. * @phba: pointer to lpfc HBA data structure.
  1796. *
  1797. * This routine wraps the actual SLI3 or SLI4 hba error attention handling
  1798. * routine from the API jump table function pointer from the lpfc_hba struct.
  1799. *
  1800. * Return codes
  1801. * 0 - success.
  1802. * Any other value - error.
  1803. **/
  1804. void
  1805. lpfc_handle_eratt(struct lpfc_hba *phba)
  1806. {
  1807. (*phba->lpfc_handle_eratt)(phba);
  1808. }
  1809. /**
  1810. * lpfc_handle_latt - The HBA link event handler
  1811. * @phba: pointer to lpfc hba data structure.
  1812. *
  1813. * This routine is invoked from the worker thread to handle a HBA host
  1814. * attention link event. SLI3 only.
  1815. **/
  1816. void
  1817. lpfc_handle_latt(struct lpfc_hba *phba)
  1818. {
  1819. struct lpfc_vport *vport = phba->pport;
  1820. struct lpfc_sli *psli = &phba->sli;
  1821. LPFC_MBOXQ_t *pmb;
  1822. volatile uint32_t control;
  1823. struct lpfc_dmabuf *mp;
  1824. int rc = 0;
  1825. pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1826. if (!pmb) {
  1827. rc = 1;
  1828. goto lpfc_handle_latt_err_exit;
  1829. }
  1830. mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  1831. if (!mp) {
  1832. rc = 2;
  1833. goto lpfc_handle_latt_free_pmb;
  1834. }
  1835. mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
  1836. if (!mp->virt) {
  1837. rc = 3;
  1838. goto lpfc_handle_latt_free_mp;
  1839. }
  1840. /* Cleanup any outstanding ELS commands */
  1841. lpfc_els_flush_all_cmd(phba);
  1842. psli->slistat.link_event++;
  1843. lpfc_read_topology(phba, pmb, mp);
  1844. pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
  1845. pmb->vport = vport;
  1846. /* Block ELS IOCBs until we have processed this mbox command */
  1847. phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
  1848. rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
  1849. if (rc == MBX_NOT_FINISHED) {
  1850. rc = 4;
  1851. goto lpfc_handle_latt_free_mbuf;
  1852. }
  1853. /* Clear Link Attention in HA REG */
  1854. spin_lock_irq(&phba->hbalock);
  1855. writel(HA_LATT, phba->HAregaddr);
  1856. readl(phba->HAregaddr); /* flush */
  1857. spin_unlock_irq(&phba->hbalock);
  1858. return;
  1859. lpfc_handle_latt_free_mbuf:
  1860. phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
  1861. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1862. lpfc_handle_latt_free_mp:
  1863. kfree(mp);
  1864. lpfc_handle_latt_free_pmb:
  1865. mempool_free(pmb, phba->mbox_mem_pool);
  1866. lpfc_handle_latt_err_exit:
  1867. /* Enable Link attention interrupts */
  1868. spin_lock_irq(&phba->hbalock);
  1869. psli->sli_flag |= LPFC_PROCESS_LA;
  1870. control = readl(phba->HCregaddr);
  1871. control |= HC_LAINT_ENA;
  1872. writel(control, phba->HCregaddr);
  1873. readl(phba->HCregaddr); /* flush */
  1874. /* Clear Link Attention in HA REG */
  1875. writel(HA_LATT, phba->HAregaddr);
  1876. readl(phba->HAregaddr); /* flush */
  1877. spin_unlock_irq(&phba->hbalock);
  1878. lpfc_linkdown(phba);
  1879. phba->link_state = LPFC_HBA_ERROR;
  1880. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  1881. "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
  1882. return;
  1883. }
  1884. /**
  1885. * lpfc_parse_vpd - Parse VPD (Vital Product Data)
  1886. * @phba: pointer to lpfc hba data structure.
  1887. * @vpd: pointer to the vital product data.
  1888. * @len: length of the vital product data in bytes.
  1889. *
  1890. * This routine parses the Vital Product Data (VPD). The VPD is treated as
  1891. * an array of characters. In this routine, the ModelName, ProgramType, and
  1892. * ModelDesc, etc. fields of the phba data structure will be populated.
  1893. *
  1894. * Return codes
  1895. * 0 - pointer to the VPD passed in is NULL
  1896. * 1 - success
  1897. **/
  1898. int
  1899. lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
  1900. {
  1901. uint8_t lenlo, lenhi;
  1902. int Length;
  1903. int i, j;
  1904. int finished = 0;
  1905. int index = 0;
  1906. if (!vpd)
  1907. return 0;
  1908. /* Vital Product */
  1909. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  1910. "0455 Vital Product Data: x%x x%x x%x x%x\n",
  1911. (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
  1912. (uint32_t) vpd[3]);
  1913. while (!finished && (index < (len - 4))) {
  1914. switch (vpd[index]) {
  1915. case 0x82:
  1916. case 0x91:
  1917. index += 1;
  1918. lenlo = vpd[index];
  1919. index += 1;
  1920. lenhi = vpd[index];
  1921. index += 1;
  1922. i = ((((unsigned short)lenhi) << 8) + lenlo);
  1923. index += i;
  1924. break;
  1925. case 0x90:
  1926. index += 1;
  1927. lenlo = vpd[index];
  1928. index += 1;
  1929. lenhi = vpd[index];
  1930. index += 1;
  1931. Length = ((((unsigned short)lenhi) << 8) + lenlo);
  1932. if (Length > len - index)
  1933. Length = len - index;
  1934. while (Length > 0) {
  1935. /* Look for Serial Number */
  1936. if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
  1937. index += 2;
  1938. i = vpd[index];
  1939. index += 1;
  1940. j = 0;
  1941. Length -= (3+i);
  1942. while(i--) {
  1943. phba->SerialNumber[j++] = vpd[index++];
  1944. if (j == 31)
  1945. break;
  1946. }
  1947. phba->SerialNumber[j] = 0;
  1948. continue;
  1949. }
  1950. else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
  1951. phba->vpd_flag |= VPD_MODEL_DESC;
  1952. index += 2;
  1953. i = vpd[index];
  1954. index += 1;
  1955. j = 0;
  1956. Length -= (3+i);
  1957. while(i--) {
  1958. phba->ModelDesc[j++] = vpd[index++];
  1959. if (j == 255)
  1960. break;
  1961. }
  1962. phba->ModelDesc[j] = 0;
  1963. continue;
  1964. }
  1965. else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
  1966. phba->vpd_flag |= VPD_MODEL_NAME;
  1967. index += 2;
  1968. i = vpd[index];
  1969. index += 1;
  1970. j = 0;
  1971. Length -= (3+i);
  1972. while(i--) {
  1973. phba->ModelName[j++] = vpd[index++];
  1974. if (j == 79)
  1975. break;
  1976. }
  1977. phba->ModelName[j] = 0;
  1978. continue;
  1979. }
  1980. else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
  1981. phba->vpd_flag |= VPD_PROGRAM_TYPE;
  1982. index += 2;
  1983. i = vpd[index];
  1984. index += 1;
  1985. j = 0;
  1986. Length -= (3+i);
  1987. while(i--) {
  1988. phba->ProgramType[j++] = vpd[index++];
  1989. if (j == 255)
  1990. break;
  1991. }
  1992. phba->ProgramType[j] = 0;
  1993. continue;
  1994. }
  1995. else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
  1996. phba->vpd_flag |= VPD_PORT;
  1997. index += 2;
  1998. i = vpd[index];
  1999. index += 1;
  2000. j = 0;
  2001. Length -= (3+i);
  2002. while(i--) {
  2003. if ((phba->sli_rev == LPFC_SLI_REV4) &&
  2004. (phba->sli4_hba.pport_name_sta ==
  2005. LPFC_SLI4_PPNAME_GET)) {
  2006. j++;
  2007. index++;
  2008. } else
  2009. phba->Port[j++] = vpd[index++];
  2010. if (j == 19)
  2011. break;
  2012. }
  2013. if ((phba->sli_rev != LPFC_SLI_REV4) ||
  2014. (phba->sli4_hba.pport_name_sta ==
  2015. LPFC_SLI4_PPNAME_NON))
  2016. phba->Port[j] = 0;
  2017. continue;
  2018. }
  2019. else {
  2020. index += 2;
  2021. i = vpd[index];
  2022. index += 1;
  2023. index += i;
  2024. Length -= (3 + i);
  2025. }
  2026. }
  2027. finished = 0;
  2028. break;
  2029. case 0x78:
  2030. finished = 1;
  2031. break;
  2032. default:
  2033. index ++;
  2034. break;
  2035. }
  2036. }
  2037. return(1);
  2038. }
  2039. /**
  2040. * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
  2041. * @phba: pointer to lpfc hba data structure.
  2042. * @mdp: pointer to the data structure to hold the derived model name.
  2043. * @descp: pointer to the data structure to hold the derived description.
  2044. *
  2045. * This routine retrieves HBA's description based on its registered PCI device
  2046. * ID. The @descp passed into this function points to an array of 256 chars. It
  2047. * shall be returned with the model name, maximum speed, and the host bus type.
  2048. * The @mdp passed into this function points to an array of 80 chars. When the
  2049. * function returns, the @mdp will be filled with the model name.
  2050. **/
  2051. static void
  2052. lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
  2053. {
  2054. lpfc_vpd_t *vp;
  2055. uint16_t dev_id = phba->pcidev->device;
  2056. int max_speed;
  2057. int GE = 0;
  2058. int oneConnect = 0; /* default is not a oneConnect */
  2059. struct {
  2060. char *name;
  2061. char *bus;
  2062. char *function;
  2063. } m = {"<Unknown>", "", ""};
  2064. if (mdp && mdp[0] != '\0'
  2065. && descp && descp[0] != '\0')
  2066. return;
  2067. if (phba->lmt & LMT_64Gb)
  2068. max_speed = 64;
  2069. else if (phba->lmt & LMT_32Gb)
  2070. max_speed = 32;
  2071. else if (phba->lmt & LMT_16Gb)
  2072. max_speed = 16;
  2073. else if (phba->lmt & LMT_10Gb)
  2074. max_speed = 10;
  2075. else if (phba->lmt & LMT_8Gb)
  2076. max_speed = 8;
  2077. else if (phba->lmt & LMT_4Gb)
  2078. max_speed = 4;
  2079. else if (phba->lmt & LMT_2Gb)
  2080. max_speed = 2;
  2081. else if (phba->lmt & LMT_1Gb)
  2082. max_speed = 1;
  2083. else
  2084. max_speed = 0;
  2085. vp = &phba->vpd;
  2086. switch (dev_id) {
  2087. case PCI_DEVICE_ID_FIREFLY:
  2088. m = (typeof(m)){"LP6000", "PCI",
  2089. "Obsolete, Unsupported Fibre Channel Adapter"};
  2090. break;
  2091. case PCI_DEVICE_ID_SUPERFLY:
  2092. if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
  2093. m = (typeof(m)){"LP7000", "PCI", ""};
  2094. else
  2095. m = (typeof(m)){"LP7000E", "PCI", ""};
  2096. m.function = "Obsolete, Unsupported Fibre Channel Adapter";
  2097. break;
  2098. case PCI_DEVICE_ID_DRAGONFLY:
  2099. m = (typeof(m)){"LP8000", "PCI",
  2100. "Obsolete, Unsupported Fibre Channel Adapter"};
  2101. break;
  2102. case PCI_DEVICE_ID_CENTAUR:
  2103. if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
  2104. m = (typeof(m)){"LP9002", "PCI", ""};
  2105. else
  2106. m = (typeof(m)){"LP9000", "PCI", ""};
  2107. m.function = "Obsolete, Unsupported Fibre Channel Adapter";
  2108. break;
  2109. case PCI_DEVICE_ID_RFLY:
  2110. m = (typeof(m)){"LP952", "PCI",
  2111. "Obsolete, Unsupported Fibre Channel Adapter"};
  2112. break;
  2113. case PCI_DEVICE_ID_PEGASUS:
  2114. m = (typeof(m)){"LP9802", "PCI-X",
  2115. "Obsolete, Unsupported Fibre Channel Adapter"};
  2116. break;
  2117. case PCI_DEVICE_ID_THOR:
  2118. m = (typeof(m)){"LP10000", "PCI-X",
  2119. "Obsolete, Unsupported Fibre Channel Adapter"};
  2120. break;
  2121. case PCI_DEVICE_ID_VIPER:
  2122. m = (typeof(m)){"LPX1000", "PCI-X",
  2123. "Obsolete, Unsupported Fibre Channel Adapter"};
  2124. break;
  2125. case PCI_DEVICE_ID_PFLY:
  2126. m = (typeof(m)){"LP982", "PCI-X",
  2127. "Obsolete, Unsupported Fibre Channel Adapter"};
  2128. break;
  2129. case PCI_DEVICE_ID_TFLY:
  2130. m = (typeof(m)){"LP1050", "PCI-X",
  2131. "Obsolete, Unsupported Fibre Channel Adapter"};
  2132. break;
  2133. case PCI_DEVICE_ID_HELIOS:
  2134. m = (typeof(m)){"LP11000", "PCI-X2",
  2135. "Obsolete, Unsupported Fibre Channel Adapter"};
  2136. break;
  2137. case PCI_DEVICE_ID_HELIOS_SCSP:
  2138. m = (typeof(m)){"LP11000-SP", "PCI-X2",
  2139. "Obsolete, Unsupported Fibre Channel Adapter"};
  2140. break;
  2141. case PCI_DEVICE_ID_HELIOS_DCSP:
  2142. m = (typeof(m)){"LP11002-SP", "PCI-X2",
  2143. "Obsolete, Unsupported Fibre Channel Adapter"};
  2144. break;
  2145. case PCI_DEVICE_ID_NEPTUNE:
  2146. m = (typeof(m)){"LPe1000", "PCIe",
  2147. "Obsolete, Unsupported Fibre Channel Adapter"};
  2148. break;
  2149. case PCI_DEVICE_ID_NEPTUNE_SCSP:
  2150. m = (typeof(m)){"LPe1000-SP", "PCIe",
  2151. "Obsolete, Unsupported Fibre Channel Adapter"};
  2152. break;
  2153. case PCI_DEVICE_ID_NEPTUNE_DCSP:
  2154. m = (typeof(m)){"LPe1002-SP", "PCIe",
  2155. "Obsolete, Unsupported Fibre Channel Adapter"};
  2156. break;
  2157. case PCI_DEVICE_ID_BMID:
  2158. m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
  2159. break;
  2160. case PCI_DEVICE_ID_BSMB:
  2161. m = (typeof(m)){"LP111", "PCI-X2",
  2162. "Obsolete, Unsupported Fibre Channel Adapter"};
  2163. break;
  2164. case PCI_DEVICE_ID_ZEPHYR:
  2165. m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
  2166. break;
  2167. case PCI_DEVICE_ID_ZEPHYR_SCSP:
  2168. m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
  2169. break;
  2170. case PCI_DEVICE_ID_ZEPHYR_DCSP:
  2171. m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
  2172. GE = 1;
  2173. break;
  2174. case PCI_DEVICE_ID_ZMID:
  2175. m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
  2176. break;
  2177. case PCI_DEVICE_ID_ZSMB:
  2178. m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
  2179. break;
  2180. case PCI_DEVICE_ID_LP101:
  2181. m = (typeof(m)){"LP101", "PCI-X",
  2182. "Obsolete, Unsupported Fibre Channel Adapter"};
  2183. break;
  2184. case PCI_DEVICE_ID_LP10000S:
  2185. m = (typeof(m)){"LP10000-S", "PCI",
  2186. "Obsolete, Unsupported Fibre Channel Adapter"};
  2187. break;
  2188. case PCI_DEVICE_ID_LP11000S:
  2189. m = (typeof(m)){"LP11000-S", "PCI-X2",
  2190. "Obsolete, Unsupported Fibre Channel Adapter"};
  2191. break;
  2192. case PCI_DEVICE_ID_LPE11000S:
  2193. m = (typeof(m)){"LPe11000-S", "PCIe",
  2194. "Obsolete, Unsupported Fibre Channel Adapter"};
  2195. break;
  2196. case PCI_DEVICE_ID_SAT:
  2197. m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
  2198. break;
  2199. case PCI_DEVICE_ID_SAT_MID:
  2200. m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
  2201. break;
  2202. case PCI_DEVICE_ID_SAT_SMB:
  2203. m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
  2204. break;
  2205. case PCI_DEVICE_ID_SAT_DCSP:
  2206. m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
  2207. break;
  2208. case PCI_DEVICE_ID_SAT_SCSP:
  2209. m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
  2210. break;
  2211. case PCI_DEVICE_ID_SAT_S:
  2212. m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
  2213. break;
  2214. case PCI_DEVICE_ID_HORNET:
  2215. m = (typeof(m)){"LP21000", "PCIe",
  2216. "Obsolete, Unsupported FCoE Adapter"};
  2217. GE = 1;
  2218. break;
  2219. case PCI_DEVICE_ID_PROTEUS_VF:
  2220. m = (typeof(m)){"LPev12000", "PCIe IOV",
  2221. "Obsolete, Unsupported Fibre Channel Adapter"};
  2222. break;
  2223. case PCI_DEVICE_ID_PROTEUS_PF:
  2224. m = (typeof(m)){"LPev12000", "PCIe IOV",
  2225. "Obsolete, Unsupported Fibre Channel Adapter"};
  2226. break;
  2227. case PCI_DEVICE_ID_PROTEUS_S:
  2228. m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
  2229. "Obsolete, Unsupported Fibre Channel Adapter"};
  2230. break;
  2231. case PCI_DEVICE_ID_TIGERSHARK:
  2232. oneConnect = 1;
  2233. m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
  2234. break;
  2235. case PCI_DEVICE_ID_TOMCAT:
  2236. oneConnect = 1;
  2237. m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
  2238. break;
  2239. case PCI_DEVICE_ID_FALCON:
  2240. m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
  2241. "EmulexSecure Fibre"};
  2242. break;
  2243. case PCI_DEVICE_ID_BALIUS:
  2244. m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
  2245. "Obsolete, Unsupported Fibre Channel Adapter"};
  2246. break;
  2247. case PCI_DEVICE_ID_LANCER_FC:
  2248. m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
  2249. break;
  2250. case PCI_DEVICE_ID_LANCER_FC_VF:
  2251. m = (typeof(m)){"LPe16000", "PCIe",
  2252. "Obsolete, Unsupported Fibre Channel Adapter"};
  2253. break;
  2254. case PCI_DEVICE_ID_LANCER_FCOE:
  2255. oneConnect = 1;
  2256. m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
  2257. break;
  2258. case PCI_DEVICE_ID_LANCER_FCOE_VF:
  2259. oneConnect = 1;
  2260. m = (typeof(m)){"OCe15100", "PCIe",
  2261. "Obsolete, Unsupported FCoE"};
  2262. break;
  2263. case PCI_DEVICE_ID_LANCER_G6_FC:
  2264. m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
  2265. break;
  2266. case PCI_DEVICE_ID_LANCER_G7_FC:
  2267. m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
  2268. break;
  2269. case PCI_DEVICE_ID_SKYHAWK:
  2270. case PCI_DEVICE_ID_SKYHAWK_VF:
  2271. oneConnect = 1;
  2272. m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
  2273. break;
  2274. default:
  2275. m = (typeof(m)){"Unknown", "", ""};
  2276. break;
  2277. }
  2278. if (mdp && mdp[0] == '\0')
  2279. snprintf(mdp, 79,"%s", m.name);
  2280. /*
  2281. * oneConnect hba requires special processing, they are all initiators
  2282. * and we put the port number on the end
  2283. */
  2284. if (descp && descp[0] == '\0') {
  2285. if (oneConnect)
  2286. snprintf(descp, 255,
  2287. "Emulex OneConnect %s, %s Initiator %s",
  2288. m.name, m.function,
  2289. phba->Port);
  2290. else if (max_speed == 0)
  2291. snprintf(descp, 255,
  2292. "Emulex %s %s %s",
  2293. m.name, m.bus, m.function);
  2294. else
  2295. snprintf(descp, 255,
  2296. "Emulex %s %d%s %s %s",
  2297. m.name, max_speed, (GE) ? "GE" : "Gb",
  2298. m.bus, m.function);
  2299. }
  2300. }
  2301. /**
  2302. * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
  2303. * @phba: pointer to lpfc hba data structure.
  2304. * @pring: pointer to a IOCB ring.
  2305. * @cnt: the number of IOCBs to be posted to the IOCB ring.
  2306. *
  2307. * This routine posts a given number of IOCBs with the associated DMA buffer
  2308. * descriptors specified by the cnt argument to the given IOCB ring.
  2309. *
  2310. * Return codes
  2311. * The number of IOCBs NOT able to be posted to the IOCB ring.
  2312. **/
  2313. int
  2314. lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
  2315. {
  2316. IOCB_t *icmd;
  2317. struct lpfc_iocbq *iocb;
  2318. struct lpfc_dmabuf *mp1, *mp2;
  2319. cnt += pring->missbufcnt;
  2320. /* While there are buffers to post */
  2321. while (cnt > 0) {
  2322. /* Allocate buffer for command iocb */
  2323. iocb = lpfc_sli_get_iocbq(phba);
  2324. if (iocb == NULL) {
  2325. pring->missbufcnt = cnt;
  2326. return cnt;
  2327. }
  2328. icmd = &iocb->iocb;
  2329. /* 2 buffers can be posted per command */
  2330. /* Allocate buffer to post */
  2331. mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  2332. if (mp1)
  2333. mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
  2334. if (!mp1 || !mp1->virt) {
  2335. kfree(mp1);
  2336. lpfc_sli_release_iocbq(phba, iocb);
  2337. pring->missbufcnt = cnt;
  2338. return cnt;
  2339. }
  2340. INIT_LIST_HEAD(&mp1->list);
  2341. /* Allocate buffer to post */
  2342. if (cnt > 1) {
  2343. mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  2344. if (mp2)
  2345. mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  2346. &mp2->phys);
  2347. if (!mp2 || !mp2->virt) {
  2348. kfree(mp2);
  2349. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  2350. kfree(mp1);
  2351. lpfc_sli_release_iocbq(phba, iocb);
  2352. pring->missbufcnt = cnt;
  2353. return cnt;
  2354. }
  2355. INIT_LIST_HEAD(&mp2->list);
  2356. } else {
  2357. mp2 = NULL;
  2358. }
  2359. icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
  2360. icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
  2361. icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
  2362. icmd->ulpBdeCount = 1;
  2363. cnt--;
  2364. if (mp2) {
  2365. icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
  2366. icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
  2367. icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
  2368. cnt--;
  2369. icmd->ulpBdeCount = 2;
  2370. }
  2371. icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
  2372. icmd->ulpLe = 1;
  2373. if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
  2374. IOCB_ERROR) {
  2375. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  2376. kfree(mp1);
  2377. cnt++;
  2378. if (mp2) {
  2379. lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
  2380. kfree(mp2);
  2381. cnt++;
  2382. }
  2383. lpfc_sli_release_iocbq(phba, iocb);
  2384. pring->missbufcnt = cnt;
  2385. return cnt;
  2386. }
  2387. lpfc_sli_ringpostbuf_put(phba, pring, mp1);
  2388. if (mp2)
  2389. lpfc_sli_ringpostbuf_put(phba, pring, mp2);
  2390. }
  2391. pring->missbufcnt = 0;
  2392. return 0;
  2393. }
  2394. /**
  2395. * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
  2396. * @phba: pointer to lpfc hba data structure.
  2397. *
  2398. * This routine posts initial receive IOCB buffers to the ELS ring. The
  2399. * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
  2400. * set to 64 IOCBs. SLI3 only.
  2401. *
  2402. * Return codes
  2403. * 0 - success (currently always success)
  2404. **/
  2405. static int
  2406. lpfc_post_rcv_buf(struct lpfc_hba *phba)
  2407. {
  2408. struct lpfc_sli *psli = &phba->sli;
  2409. /* Ring 0, ELS / CT buffers */
  2410. lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
  2411. /* Ring 2 - FCP no buffers needed */
  2412. return 0;
  2413. }
  2414. #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
  2415. /**
  2416. * lpfc_sha_init - Set up initial array of hash table entries
  2417. * @HashResultPointer: pointer to an array as hash table.
  2418. *
  2419. * This routine sets up the initial values to the array of hash table entries
  2420. * for the LC HBAs.
  2421. **/
  2422. static void
  2423. lpfc_sha_init(uint32_t * HashResultPointer)
  2424. {
  2425. HashResultPointer[0] = 0x67452301;
  2426. HashResultPointer[1] = 0xEFCDAB89;
  2427. HashResultPointer[2] = 0x98BADCFE;
  2428. HashResultPointer[3] = 0x10325476;
  2429. HashResultPointer[4] = 0xC3D2E1F0;
  2430. }
  2431. /**
  2432. * lpfc_sha_iterate - Iterate initial hash table with the working hash table
  2433. * @HashResultPointer: pointer to an initial/result hash table.
  2434. * @HashWorkingPointer: pointer to an working hash table.
  2435. *
  2436. * This routine iterates an initial hash table pointed by @HashResultPointer
  2437. * with the values from the working hash table pointeed by @HashWorkingPointer.
  2438. * The results are putting back to the initial hash table, returned through
  2439. * the @HashResultPointer as the result hash table.
  2440. **/
  2441. static void
  2442. lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
  2443. {
  2444. int t;
  2445. uint32_t TEMP;
  2446. uint32_t A, B, C, D, E;
  2447. t = 16;
  2448. do {
  2449. HashWorkingPointer[t] =
  2450. S(1,
  2451. HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
  2452. 8] ^
  2453. HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
  2454. } while (++t <= 79);
  2455. t = 0;
  2456. A = HashResultPointer[0];
  2457. B = HashResultPointer[1];
  2458. C = HashResultPointer[2];
  2459. D = HashResultPointer[3];
  2460. E = HashResultPointer[4];
  2461. do {
  2462. if (t < 20) {
  2463. TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
  2464. } else if (t < 40) {
  2465. TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
  2466. } else if (t < 60) {
  2467. TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
  2468. } else {
  2469. TEMP = (B ^ C ^ D) + 0xCA62C1D6;
  2470. }
  2471. TEMP += S(5, A) + E + HashWorkingPointer[t];
  2472. E = D;
  2473. D = C;
  2474. C = S(30, B);
  2475. B = A;
  2476. A = TEMP;
  2477. } while (++t <= 79);
  2478. HashResultPointer[0] += A;
  2479. HashResultPointer[1] += B;
  2480. HashResultPointer[2] += C;
  2481. HashResultPointer[3] += D;
  2482. HashResultPointer[4] += E;
  2483. }
  2484. /**
  2485. * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
  2486. * @RandomChallenge: pointer to the entry of host challenge random number array.
  2487. * @HashWorking: pointer to the entry of the working hash array.
  2488. *
  2489. * This routine calculates the working hash array referred by @HashWorking
  2490. * from the challenge random numbers associated with the host, referred by
  2491. * @RandomChallenge. The result is put into the entry of the working hash
  2492. * array and returned by reference through @HashWorking.
  2493. **/
  2494. static void
  2495. lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
  2496. {
  2497. *HashWorking = (*RandomChallenge ^ *HashWorking);
  2498. }
  2499. /**
  2500. * lpfc_hba_init - Perform special handling for LC HBA initialization
  2501. * @phba: pointer to lpfc hba data structure.
  2502. * @hbainit: pointer to an array of unsigned 32-bit integers.
  2503. *
  2504. * This routine performs the special handling for LC HBA initialization.
  2505. **/
  2506. void
  2507. lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
  2508. {
  2509. int t;
  2510. uint32_t *HashWorking;
  2511. uint32_t *pwwnn = (uint32_t *) phba->wwnn;
  2512. HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
  2513. if (!HashWorking)
  2514. return;
  2515. HashWorking[0] = HashWorking[78] = *pwwnn++;
  2516. HashWorking[1] = HashWorking[79] = *pwwnn;
  2517. for (t = 0; t < 7; t++)
  2518. lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
  2519. lpfc_sha_init(hbainit);
  2520. lpfc_sha_iterate(hbainit, HashWorking);
  2521. kfree(HashWorking);
  2522. }
  2523. /**
  2524. * lpfc_cleanup - Performs vport cleanups before deleting a vport
  2525. * @vport: pointer to a virtual N_Port data structure.
  2526. *
  2527. * This routine performs the necessary cleanups before deleting the @vport.
  2528. * It invokes the discovery state machine to perform necessary state
  2529. * transitions and to release the ndlps associated with the @vport. Note,
  2530. * the physical port is treated as @vport 0.
  2531. **/
  2532. void
  2533. lpfc_cleanup(struct lpfc_vport *vport)
  2534. {
  2535. struct lpfc_hba *phba = vport->phba;
  2536. struct lpfc_nodelist *ndlp, *next_ndlp;
  2537. int i = 0;
  2538. if (phba->link_state > LPFC_LINK_DOWN)
  2539. lpfc_port_link_failure(vport);
  2540. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  2541. if (!NLP_CHK_NODE_ACT(ndlp)) {
  2542. ndlp = lpfc_enable_node(vport, ndlp,
  2543. NLP_STE_UNUSED_NODE);
  2544. if (!ndlp)
  2545. continue;
  2546. spin_lock_irq(&phba->ndlp_lock);
  2547. NLP_SET_FREE_REQ(ndlp);
  2548. spin_unlock_irq(&phba->ndlp_lock);
  2549. /* Trigger the release of the ndlp memory */
  2550. lpfc_nlp_put(ndlp);
  2551. continue;
  2552. }
  2553. spin_lock_irq(&phba->ndlp_lock);
  2554. if (NLP_CHK_FREE_REQ(ndlp)) {
  2555. /* The ndlp should not be in memory free mode already */
  2556. spin_unlock_irq(&phba->ndlp_lock);
  2557. continue;
  2558. } else
  2559. /* Indicate request for freeing ndlp memory */
  2560. NLP_SET_FREE_REQ(ndlp);
  2561. spin_unlock_irq(&phba->ndlp_lock);
  2562. if (vport->port_type != LPFC_PHYSICAL_PORT &&
  2563. ndlp->nlp_DID == Fabric_DID) {
  2564. /* Just free up ndlp with Fabric_DID for vports */
  2565. lpfc_nlp_put(ndlp);
  2566. continue;
  2567. }
  2568. /* take care of nodes in unused state before the state
  2569. * machine taking action.
  2570. */
  2571. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
  2572. lpfc_nlp_put(ndlp);
  2573. continue;
  2574. }
  2575. if (ndlp->nlp_type & NLP_FABRIC)
  2576. lpfc_disc_state_machine(vport, ndlp, NULL,
  2577. NLP_EVT_DEVICE_RECOVERY);
  2578. lpfc_disc_state_machine(vport, ndlp, NULL,
  2579. NLP_EVT_DEVICE_RM);
  2580. }
  2581. /* At this point, ALL ndlp's should be gone
  2582. * because of the previous NLP_EVT_DEVICE_RM.
  2583. * Lets wait for this to happen, if needed.
  2584. */
  2585. while (!list_empty(&vport->fc_nodes)) {
  2586. if (i++ > 3000) {
  2587. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  2588. "0233 Nodelist not empty\n");
  2589. list_for_each_entry_safe(ndlp, next_ndlp,
  2590. &vport->fc_nodes, nlp_listp) {
  2591. lpfc_printf_vlog(ndlp->vport, KERN_ERR,
  2592. LOG_NODE,
  2593. "0282 did:x%x ndlp:x%px "
  2594. "usgmap:x%x refcnt:%d\n",
  2595. ndlp->nlp_DID, (void *)ndlp,
  2596. ndlp->nlp_usg_map,
  2597. kref_read(&ndlp->kref));
  2598. }
  2599. break;
  2600. }
  2601. /* Wait for any activity on ndlps to settle */
  2602. msleep(10);
  2603. }
  2604. lpfc_cleanup_vports_rrqs(vport, NULL);
  2605. }
  2606. /**
  2607. * lpfc_stop_vport_timers - Stop all the timers associated with a vport
  2608. * @vport: pointer to a virtual N_Port data structure.
  2609. *
  2610. * This routine stops all the timers associated with a @vport. This function
  2611. * is invoked before disabling or deleting a @vport. Note that the physical
  2612. * port is treated as @vport 0.
  2613. **/
  2614. void
  2615. lpfc_stop_vport_timers(struct lpfc_vport *vport)
  2616. {
  2617. del_timer_sync(&vport->els_tmofunc);
  2618. del_timer_sync(&vport->delayed_disc_tmo);
  2619. lpfc_can_disctmo(vport);
  2620. return;
  2621. }
  2622. /**
  2623. * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
  2624. * @phba: pointer to lpfc hba data structure.
  2625. *
  2626. * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
  2627. * caller of this routine should already hold the host lock.
  2628. **/
  2629. void
  2630. __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
  2631. {
  2632. /* Clear pending FCF rediscovery wait flag */
  2633. phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
  2634. /* Now, try to stop the timer */
  2635. del_timer(&phba->fcf.redisc_wait);
  2636. }
  2637. /**
  2638. * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
  2639. * @phba: pointer to lpfc hba data structure.
  2640. *
  2641. * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
  2642. * checks whether the FCF rediscovery wait timer is pending with the host
  2643. * lock held before proceeding with disabling the timer and clearing the
  2644. * wait timer pendig flag.
  2645. **/
  2646. void
  2647. lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
  2648. {
  2649. spin_lock_irq(&phba->hbalock);
  2650. if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
  2651. /* FCF rediscovery timer already fired or stopped */
  2652. spin_unlock_irq(&phba->hbalock);
  2653. return;
  2654. }
  2655. __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
  2656. /* Clear failover in progress flags */
  2657. phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
  2658. spin_unlock_irq(&phba->hbalock);
  2659. }
  2660. /**
  2661. * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
  2662. * @phba: pointer to lpfc hba data structure.
  2663. *
  2664. * This routine stops all the timers associated with a HBA. This function is
  2665. * invoked before either putting a HBA offline or unloading the driver.
  2666. **/
  2667. void
  2668. lpfc_stop_hba_timers(struct lpfc_hba *phba)
  2669. {
  2670. if (phba->pport)
  2671. lpfc_stop_vport_timers(phba->pport);
  2672. cancel_delayed_work_sync(&phba->eq_delay_work);
  2673. del_timer_sync(&phba->sli.mbox_tmo);
  2674. del_timer_sync(&phba->fabric_block_timer);
  2675. del_timer_sync(&phba->eratt_poll);
  2676. del_timer_sync(&phba->hb_tmofunc);
  2677. if (phba->sli_rev == LPFC_SLI_REV4) {
  2678. del_timer_sync(&phba->rrq_tmr);
  2679. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  2680. }
  2681. phba->hb_outstanding = 0;
  2682. switch (phba->pci_dev_grp) {
  2683. case LPFC_PCI_DEV_LP:
  2684. /* Stop any LightPulse device specific driver timers */
  2685. del_timer_sync(&phba->fcp_poll_timer);
  2686. break;
  2687. case LPFC_PCI_DEV_OC:
  2688. /* Stop any OneConnect device specific driver timers */
  2689. lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
  2690. break;
  2691. default:
  2692. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2693. "0297 Invalid device group (x%x)\n",
  2694. phba->pci_dev_grp);
  2695. break;
  2696. }
  2697. return;
  2698. }
  2699. /**
  2700. * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
  2701. * @phba: pointer to lpfc hba data structure.
  2702. *
  2703. * This routine marks a HBA's management interface as blocked. Once the HBA's
  2704. * management interface is marked as blocked, all the user space access to
  2705. * the HBA, whether they are from sysfs interface or libdfc interface will
  2706. * all be blocked. The HBA is set to block the management interface when the
  2707. * driver prepares the HBA interface for online or offline.
  2708. **/
  2709. static void
  2710. lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
  2711. {
  2712. unsigned long iflag;
  2713. uint8_t actcmd = MBX_HEARTBEAT;
  2714. unsigned long timeout;
  2715. spin_lock_irqsave(&phba->hbalock, iflag);
  2716. phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
  2717. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2718. if (mbx_action == LPFC_MBX_NO_WAIT)
  2719. return;
  2720. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  2721. spin_lock_irqsave(&phba->hbalock, iflag);
  2722. if (phba->sli.mbox_active) {
  2723. actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
  2724. /* Determine how long we might wait for the active mailbox
  2725. * command to be gracefully completed by firmware.
  2726. */
  2727. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  2728. phba->sli.mbox_active) * 1000) + jiffies;
  2729. }
  2730. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2731. /* Wait for the outstnading mailbox command to complete */
  2732. while (phba->sli.mbox_active) {
  2733. /* Check active mailbox complete status every 2ms */
  2734. msleep(2);
  2735. if (time_after(jiffies, timeout)) {
  2736. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2737. "2813 Mgmt IO is Blocked %x "
  2738. "- mbox cmd %x still active\n",
  2739. phba->sli.sli_flag, actcmd);
  2740. break;
  2741. }
  2742. }
  2743. }
  2744. /**
  2745. * lpfc_sli4_node_prep - Assign RPIs for active nodes.
  2746. * @phba: pointer to lpfc hba data structure.
  2747. *
  2748. * Allocate RPIs for all active remote nodes. This is needed whenever
  2749. * an SLI4 adapter is reset and the driver is not unloading. Its purpose
  2750. * is to fixup the temporary rpi assignments.
  2751. **/
  2752. void
  2753. lpfc_sli4_node_prep(struct lpfc_hba *phba)
  2754. {
  2755. struct lpfc_nodelist *ndlp, *next_ndlp;
  2756. struct lpfc_vport **vports;
  2757. int i, rpi;
  2758. unsigned long flags;
  2759. if (phba->sli_rev != LPFC_SLI_REV4)
  2760. return;
  2761. vports = lpfc_create_vport_work_array(phba);
  2762. if (vports == NULL)
  2763. return;
  2764. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  2765. if (vports[i]->load_flag & FC_UNLOADING)
  2766. continue;
  2767. list_for_each_entry_safe(ndlp, next_ndlp,
  2768. &vports[i]->fc_nodes,
  2769. nlp_listp) {
  2770. if (!NLP_CHK_NODE_ACT(ndlp))
  2771. continue;
  2772. rpi = lpfc_sli4_alloc_rpi(phba);
  2773. if (rpi == LPFC_RPI_ALLOC_ERROR) {
  2774. spin_lock_irqsave(&phba->ndlp_lock, flags);
  2775. NLP_CLR_NODE_ACT(ndlp);
  2776. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  2777. continue;
  2778. }
  2779. ndlp->nlp_rpi = rpi;
  2780. lpfc_printf_vlog(ndlp->vport, KERN_INFO,
  2781. LOG_NODE | LOG_DISCOVERY,
  2782. "0009 Assign RPI x%x to ndlp x%px "
  2783. "DID:x%06x flg:x%x map:x%x\n",
  2784. ndlp->nlp_rpi, ndlp, ndlp->nlp_DID,
  2785. ndlp->nlp_flag, ndlp->nlp_usg_map);
  2786. }
  2787. }
  2788. lpfc_destroy_vport_work_array(phba, vports);
  2789. }
  2790. /**
  2791. * lpfc_create_expedite_pool - create expedite pool
  2792. * @phba: pointer to lpfc hba data structure.
  2793. *
  2794. * This routine moves a batch of XRIs from lpfc_io_buf_list_put of HWQ 0
  2795. * to expedite pool. Mark them as expedite.
  2796. **/
  2797. static void lpfc_create_expedite_pool(struct lpfc_hba *phba)
  2798. {
  2799. struct lpfc_sli4_hdw_queue *qp;
  2800. struct lpfc_io_buf *lpfc_ncmd;
  2801. struct lpfc_io_buf *lpfc_ncmd_next;
  2802. struct lpfc_epd_pool *epd_pool;
  2803. unsigned long iflag;
  2804. epd_pool = &phba->epd_pool;
  2805. qp = &phba->sli4_hba.hdwq[0];
  2806. spin_lock_init(&epd_pool->lock);
  2807. spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
  2808. spin_lock(&epd_pool->lock);
  2809. INIT_LIST_HEAD(&epd_pool->list);
  2810. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  2811. &qp->lpfc_io_buf_list_put, list) {
  2812. list_move_tail(&lpfc_ncmd->list, &epd_pool->list);
  2813. lpfc_ncmd->expedite = true;
  2814. qp->put_io_bufs--;
  2815. epd_pool->count++;
  2816. if (epd_pool->count >= XRI_BATCH)
  2817. break;
  2818. }
  2819. spin_unlock(&epd_pool->lock);
  2820. spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
  2821. }
  2822. /**
  2823. * lpfc_destroy_expedite_pool - destroy expedite pool
  2824. * @phba: pointer to lpfc hba data structure.
  2825. *
  2826. * This routine returns XRIs from expedite pool to lpfc_io_buf_list_put
  2827. * of HWQ 0. Clear the mark.
  2828. **/
  2829. static void lpfc_destroy_expedite_pool(struct lpfc_hba *phba)
  2830. {
  2831. struct lpfc_sli4_hdw_queue *qp;
  2832. struct lpfc_io_buf *lpfc_ncmd;
  2833. struct lpfc_io_buf *lpfc_ncmd_next;
  2834. struct lpfc_epd_pool *epd_pool;
  2835. unsigned long iflag;
  2836. epd_pool = &phba->epd_pool;
  2837. qp = &phba->sli4_hba.hdwq[0];
  2838. spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
  2839. spin_lock(&epd_pool->lock);
  2840. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  2841. &epd_pool->list, list) {
  2842. list_move_tail(&lpfc_ncmd->list,
  2843. &qp->lpfc_io_buf_list_put);
  2844. lpfc_ncmd->flags = false;
  2845. qp->put_io_bufs++;
  2846. epd_pool->count--;
  2847. }
  2848. spin_unlock(&epd_pool->lock);
  2849. spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
  2850. }
  2851. /**
  2852. * lpfc_create_multixri_pools - create multi-XRI pools
  2853. * @phba: pointer to lpfc hba data structure.
  2854. *
  2855. * This routine initialize public, private per HWQ. Then, move XRIs from
  2856. * lpfc_io_buf_list_put to public pool. High and low watermark are also
  2857. * Initialized.
  2858. **/
  2859. void lpfc_create_multixri_pools(struct lpfc_hba *phba)
  2860. {
  2861. u32 i, j;
  2862. u32 hwq_count;
  2863. u32 count_per_hwq;
  2864. struct lpfc_io_buf *lpfc_ncmd;
  2865. struct lpfc_io_buf *lpfc_ncmd_next;
  2866. unsigned long iflag;
  2867. struct lpfc_sli4_hdw_queue *qp;
  2868. struct lpfc_multixri_pool *multixri_pool;
  2869. struct lpfc_pbl_pool *pbl_pool;
  2870. struct lpfc_pvt_pool *pvt_pool;
  2871. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2872. "1234 num_hdw_queue=%d num_present_cpu=%d common_xri_cnt=%d\n",
  2873. phba->cfg_hdw_queue, phba->sli4_hba.num_present_cpu,
  2874. phba->sli4_hba.io_xri_cnt);
  2875. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  2876. lpfc_create_expedite_pool(phba);
  2877. hwq_count = phba->cfg_hdw_queue;
  2878. count_per_hwq = phba->sli4_hba.io_xri_cnt / hwq_count;
  2879. for (i = 0; i < hwq_count; i++) {
  2880. multixri_pool = kzalloc(sizeof(*multixri_pool), GFP_KERNEL);
  2881. if (!multixri_pool) {
  2882. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2883. "1238 Failed to allocate memory for "
  2884. "multixri_pool\n");
  2885. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  2886. lpfc_destroy_expedite_pool(phba);
  2887. j = 0;
  2888. while (j < i) {
  2889. qp = &phba->sli4_hba.hdwq[j];
  2890. kfree(qp->p_multixri_pool);
  2891. j++;
  2892. }
  2893. phba->cfg_xri_rebalancing = 0;
  2894. return;
  2895. }
  2896. qp = &phba->sli4_hba.hdwq[i];
  2897. qp->p_multixri_pool = multixri_pool;
  2898. multixri_pool->xri_limit = count_per_hwq;
  2899. multixri_pool->rrb_next_hwqid = i;
  2900. /* Deal with public free xri pool */
  2901. pbl_pool = &multixri_pool->pbl_pool;
  2902. spin_lock_init(&pbl_pool->lock);
  2903. spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
  2904. spin_lock(&pbl_pool->lock);
  2905. INIT_LIST_HEAD(&pbl_pool->list);
  2906. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  2907. &qp->lpfc_io_buf_list_put, list) {
  2908. list_move_tail(&lpfc_ncmd->list, &pbl_pool->list);
  2909. qp->put_io_bufs--;
  2910. pbl_pool->count++;
  2911. }
  2912. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2913. "1235 Moved %d buffers from PUT list over to pbl_pool[%d]\n",
  2914. pbl_pool->count, i);
  2915. spin_unlock(&pbl_pool->lock);
  2916. spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
  2917. /* Deal with private free xri pool */
  2918. pvt_pool = &multixri_pool->pvt_pool;
  2919. pvt_pool->high_watermark = multixri_pool->xri_limit / 2;
  2920. pvt_pool->low_watermark = XRI_BATCH;
  2921. spin_lock_init(&pvt_pool->lock);
  2922. spin_lock_irqsave(&pvt_pool->lock, iflag);
  2923. INIT_LIST_HEAD(&pvt_pool->list);
  2924. pvt_pool->count = 0;
  2925. spin_unlock_irqrestore(&pvt_pool->lock, iflag);
  2926. }
  2927. }
  2928. /**
  2929. * lpfc_destroy_multixri_pools - destroy multi-XRI pools
  2930. * @phba: pointer to lpfc hba data structure.
  2931. *
  2932. * This routine returns XRIs from public/private to lpfc_io_buf_list_put.
  2933. **/
  2934. static void lpfc_destroy_multixri_pools(struct lpfc_hba *phba)
  2935. {
  2936. u32 i;
  2937. u32 hwq_count;
  2938. struct lpfc_io_buf *lpfc_ncmd;
  2939. struct lpfc_io_buf *lpfc_ncmd_next;
  2940. unsigned long iflag;
  2941. struct lpfc_sli4_hdw_queue *qp;
  2942. struct lpfc_multixri_pool *multixri_pool;
  2943. struct lpfc_pbl_pool *pbl_pool;
  2944. struct lpfc_pvt_pool *pvt_pool;
  2945. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  2946. lpfc_destroy_expedite_pool(phba);
  2947. if (!(phba->pport->load_flag & FC_UNLOADING))
  2948. lpfc_sli_flush_io_rings(phba);
  2949. hwq_count = phba->cfg_hdw_queue;
  2950. for (i = 0; i < hwq_count; i++) {
  2951. qp = &phba->sli4_hba.hdwq[i];
  2952. multixri_pool = qp->p_multixri_pool;
  2953. if (!multixri_pool)
  2954. continue;
  2955. qp->p_multixri_pool = NULL;
  2956. spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
  2957. /* Deal with public free xri pool */
  2958. pbl_pool = &multixri_pool->pbl_pool;
  2959. spin_lock(&pbl_pool->lock);
  2960. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2961. "1236 Moving %d buffers from pbl_pool[%d] TO PUT list\n",
  2962. pbl_pool->count, i);
  2963. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  2964. &pbl_pool->list, list) {
  2965. list_move_tail(&lpfc_ncmd->list,
  2966. &qp->lpfc_io_buf_list_put);
  2967. qp->put_io_bufs++;
  2968. pbl_pool->count--;
  2969. }
  2970. INIT_LIST_HEAD(&pbl_pool->list);
  2971. pbl_pool->count = 0;
  2972. spin_unlock(&pbl_pool->lock);
  2973. /* Deal with private free xri pool */
  2974. pvt_pool = &multixri_pool->pvt_pool;
  2975. spin_lock(&pvt_pool->lock);
  2976. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2977. "1237 Moving %d buffers from pvt_pool[%d] TO PUT list\n",
  2978. pvt_pool->count, i);
  2979. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  2980. &pvt_pool->list, list) {
  2981. list_move_tail(&lpfc_ncmd->list,
  2982. &qp->lpfc_io_buf_list_put);
  2983. qp->put_io_bufs++;
  2984. pvt_pool->count--;
  2985. }
  2986. INIT_LIST_HEAD(&pvt_pool->list);
  2987. pvt_pool->count = 0;
  2988. spin_unlock(&pvt_pool->lock);
  2989. spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
  2990. kfree(multixri_pool);
  2991. }
  2992. }
  2993. /**
  2994. * lpfc_online - Initialize and bring a HBA online
  2995. * @phba: pointer to lpfc hba data structure.
  2996. *
  2997. * This routine initializes the HBA and brings a HBA online. During this
  2998. * process, the management interface is blocked to prevent user space access
  2999. * to the HBA interfering with the driver initialization.
  3000. *
  3001. * Return codes
  3002. * 0 - successful
  3003. * 1 - failed
  3004. **/
  3005. int
  3006. lpfc_online(struct lpfc_hba *phba)
  3007. {
  3008. struct lpfc_vport *vport;
  3009. struct lpfc_vport **vports;
  3010. int i, error = 0;
  3011. bool vpis_cleared = false;
  3012. if (!phba)
  3013. return 0;
  3014. vport = phba->pport;
  3015. if (!(vport->fc_flag & FC_OFFLINE_MODE))
  3016. return 0;
  3017. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3018. "0458 Bring Adapter online\n");
  3019. lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
  3020. if (phba->sli_rev == LPFC_SLI_REV4) {
  3021. if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
  3022. lpfc_unblock_mgmt_io(phba);
  3023. return 1;
  3024. }
  3025. spin_lock_irq(&phba->hbalock);
  3026. if (!phba->sli4_hba.max_cfg_param.vpi_used)
  3027. vpis_cleared = true;
  3028. spin_unlock_irq(&phba->hbalock);
  3029. /* Reestablish the local initiator port.
  3030. * The offline process destroyed the previous lport.
  3031. */
  3032. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
  3033. !phba->nvmet_support) {
  3034. error = lpfc_nvme_create_localport(phba->pport);
  3035. if (error)
  3036. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3037. "6132 NVME restore reg failed "
  3038. "on nvmei error x%x\n", error);
  3039. }
  3040. } else {
  3041. lpfc_sli_queue_init(phba);
  3042. if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
  3043. lpfc_unblock_mgmt_io(phba);
  3044. return 1;
  3045. }
  3046. }
  3047. vports = lpfc_create_vport_work_array(phba);
  3048. if (vports != NULL) {
  3049. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3050. struct Scsi_Host *shost;
  3051. shost = lpfc_shost_from_vport(vports[i]);
  3052. spin_lock_irq(shost->host_lock);
  3053. vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
  3054. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  3055. vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  3056. if (phba->sli_rev == LPFC_SLI_REV4) {
  3057. vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
  3058. if ((vpis_cleared) &&
  3059. (vports[i]->port_type !=
  3060. LPFC_PHYSICAL_PORT))
  3061. vports[i]->vpi = 0;
  3062. }
  3063. spin_unlock_irq(shost->host_lock);
  3064. }
  3065. }
  3066. lpfc_destroy_vport_work_array(phba, vports);
  3067. if (phba->cfg_xri_rebalancing)
  3068. lpfc_create_multixri_pools(phba);
  3069. lpfc_cpuhp_add(phba);
  3070. lpfc_unblock_mgmt_io(phba);
  3071. return 0;
  3072. }
  3073. /**
  3074. * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
  3075. * @phba: pointer to lpfc hba data structure.
  3076. *
  3077. * This routine marks a HBA's management interface as not blocked. Once the
  3078. * HBA's management interface is marked as not blocked, all the user space
  3079. * access to the HBA, whether they are from sysfs interface or libdfc
  3080. * interface will be allowed. The HBA is set to block the management interface
  3081. * when the driver prepares the HBA interface for online or offline and then
  3082. * set to unblock the management interface afterwards.
  3083. **/
  3084. void
  3085. lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
  3086. {
  3087. unsigned long iflag;
  3088. spin_lock_irqsave(&phba->hbalock, iflag);
  3089. phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
  3090. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3091. }
  3092. /**
  3093. * lpfc_offline_prep - Prepare a HBA to be brought offline
  3094. * @phba: pointer to lpfc hba data structure.
  3095. *
  3096. * This routine is invoked to prepare a HBA to be brought offline. It performs
  3097. * unregistration login to all the nodes on all vports and flushes the mailbox
  3098. * queue to make it ready to be brought offline.
  3099. **/
  3100. void
  3101. lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
  3102. {
  3103. struct lpfc_vport *vport = phba->pport;
  3104. struct lpfc_nodelist *ndlp, *next_ndlp;
  3105. struct lpfc_vport **vports;
  3106. struct Scsi_Host *shost;
  3107. int i;
  3108. if (vport->fc_flag & FC_OFFLINE_MODE)
  3109. return;
  3110. lpfc_block_mgmt_io(phba, mbx_action);
  3111. lpfc_linkdown(phba);
  3112. /* Issue an unreg_login to all nodes on all vports */
  3113. vports = lpfc_create_vport_work_array(phba);
  3114. if (vports != NULL) {
  3115. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3116. if (vports[i]->load_flag & FC_UNLOADING)
  3117. continue;
  3118. shost = lpfc_shost_from_vport(vports[i]);
  3119. spin_lock_irq(shost->host_lock);
  3120. vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
  3121. vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  3122. vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
  3123. spin_unlock_irq(shost->host_lock);
  3124. shost = lpfc_shost_from_vport(vports[i]);
  3125. list_for_each_entry_safe(ndlp, next_ndlp,
  3126. &vports[i]->fc_nodes,
  3127. nlp_listp) {
  3128. if ((!NLP_CHK_NODE_ACT(ndlp)) ||
  3129. ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
  3130. /* Driver must assume RPI is invalid for
  3131. * any unused or inactive node.
  3132. */
  3133. ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
  3134. continue;
  3135. }
  3136. if (ndlp->nlp_type & NLP_FABRIC) {
  3137. lpfc_disc_state_machine(vports[i], ndlp,
  3138. NULL, NLP_EVT_DEVICE_RECOVERY);
  3139. lpfc_disc_state_machine(vports[i], ndlp,
  3140. NULL, NLP_EVT_DEVICE_RM);
  3141. }
  3142. spin_lock_irq(shost->host_lock);
  3143. ndlp->nlp_flag &= ~NLP_NPR_ADISC;
  3144. spin_unlock_irq(shost->host_lock);
  3145. /*
  3146. * Whenever an SLI4 port goes offline, free the
  3147. * RPI. Get a new RPI when the adapter port
  3148. * comes back online.
  3149. */
  3150. if (phba->sli_rev == LPFC_SLI_REV4) {
  3151. lpfc_printf_vlog(ndlp->vport, KERN_INFO,
  3152. LOG_NODE | LOG_DISCOVERY,
  3153. "0011 Free RPI x%x on "
  3154. "ndlp:x%px did x%x "
  3155. "usgmap:x%x\n",
  3156. ndlp->nlp_rpi, ndlp,
  3157. ndlp->nlp_DID,
  3158. ndlp->nlp_usg_map);
  3159. lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
  3160. ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
  3161. }
  3162. lpfc_unreg_rpi(vports[i], ndlp);
  3163. }
  3164. }
  3165. }
  3166. lpfc_destroy_vport_work_array(phba, vports);
  3167. lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
  3168. if (phba->wq)
  3169. flush_workqueue(phba->wq);
  3170. }
  3171. /**
  3172. * lpfc_offline - Bring a HBA offline
  3173. * @phba: pointer to lpfc hba data structure.
  3174. *
  3175. * This routine actually brings a HBA offline. It stops all the timers
  3176. * associated with the HBA, brings down the SLI layer, and eventually
  3177. * marks the HBA as in offline state for the upper layer protocol.
  3178. **/
  3179. void
  3180. lpfc_offline(struct lpfc_hba *phba)
  3181. {
  3182. struct Scsi_Host *shost;
  3183. struct lpfc_vport **vports;
  3184. int i;
  3185. if (phba->pport->fc_flag & FC_OFFLINE_MODE)
  3186. return;
  3187. /* stop port and all timers associated with this hba */
  3188. lpfc_stop_port(phba);
  3189. /* Tear down the local and target port registrations. The
  3190. * nvme transports need to cleanup.
  3191. */
  3192. lpfc_nvmet_destroy_targetport(phba);
  3193. lpfc_nvme_destroy_localport(phba->pport);
  3194. vports = lpfc_create_vport_work_array(phba);
  3195. if (vports != NULL)
  3196. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  3197. lpfc_stop_vport_timers(vports[i]);
  3198. lpfc_destroy_vport_work_array(phba, vports);
  3199. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3200. "0460 Bring Adapter offline\n");
  3201. /* Bring down the SLI Layer and cleanup. The HBA is offline
  3202. now. */
  3203. lpfc_sli_hba_down(phba);
  3204. spin_lock_irq(&phba->hbalock);
  3205. phba->work_ha = 0;
  3206. spin_unlock_irq(&phba->hbalock);
  3207. vports = lpfc_create_vport_work_array(phba);
  3208. if (vports != NULL)
  3209. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3210. shost = lpfc_shost_from_vport(vports[i]);
  3211. spin_lock_irq(shost->host_lock);
  3212. vports[i]->work_port_events = 0;
  3213. vports[i]->fc_flag |= FC_OFFLINE_MODE;
  3214. spin_unlock_irq(shost->host_lock);
  3215. }
  3216. lpfc_destroy_vport_work_array(phba, vports);
  3217. __lpfc_cpuhp_remove(phba);
  3218. if (phba->cfg_xri_rebalancing)
  3219. lpfc_destroy_multixri_pools(phba);
  3220. }
  3221. /**
  3222. * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
  3223. * @phba: pointer to lpfc hba data structure.
  3224. *
  3225. * This routine is to free all the SCSI buffers and IOCBs from the driver
  3226. * list back to kernel. It is called from lpfc_pci_remove_one to free
  3227. * the internal resources before the device is removed from the system.
  3228. **/
  3229. static void
  3230. lpfc_scsi_free(struct lpfc_hba *phba)
  3231. {
  3232. struct lpfc_io_buf *sb, *sb_next;
  3233. if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
  3234. return;
  3235. spin_lock_irq(&phba->hbalock);
  3236. /* Release all the lpfc_scsi_bufs maintained by this host. */
  3237. spin_lock(&phba->scsi_buf_list_put_lock);
  3238. list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
  3239. list) {
  3240. list_del(&sb->list);
  3241. dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
  3242. sb->dma_handle);
  3243. kfree(sb);
  3244. phba->total_scsi_bufs--;
  3245. }
  3246. spin_unlock(&phba->scsi_buf_list_put_lock);
  3247. spin_lock(&phba->scsi_buf_list_get_lock);
  3248. list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
  3249. list) {
  3250. list_del(&sb->list);
  3251. dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
  3252. sb->dma_handle);
  3253. kfree(sb);
  3254. phba->total_scsi_bufs--;
  3255. }
  3256. spin_unlock(&phba->scsi_buf_list_get_lock);
  3257. spin_unlock_irq(&phba->hbalock);
  3258. }
  3259. /**
  3260. * lpfc_io_free - Free all the IO buffers and IOCBs from driver lists
  3261. * @phba: pointer to lpfc hba data structure.
  3262. *
  3263. * This routine is to free all the IO buffers and IOCBs from the driver
  3264. * list back to kernel. It is called from lpfc_pci_remove_one to free
  3265. * the internal resources before the device is removed from the system.
  3266. **/
  3267. void
  3268. lpfc_io_free(struct lpfc_hba *phba)
  3269. {
  3270. struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
  3271. struct lpfc_sli4_hdw_queue *qp;
  3272. int idx;
  3273. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  3274. qp = &phba->sli4_hba.hdwq[idx];
  3275. /* Release all the lpfc_nvme_bufs maintained by this host. */
  3276. spin_lock(&qp->io_buf_list_put_lock);
  3277. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  3278. &qp->lpfc_io_buf_list_put,
  3279. list) {
  3280. list_del(&lpfc_ncmd->list);
  3281. qp->put_io_bufs--;
  3282. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  3283. lpfc_ncmd->data, lpfc_ncmd->dma_handle);
  3284. if (phba->cfg_xpsgl && !phba->nvmet_support)
  3285. lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
  3286. lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
  3287. kfree(lpfc_ncmd);
  3288. qp->total_io_bufs--;
  3289. }
  3290. spin_unlock(&qp->io_buf_list_put_lock);
  3291. spin_lock(&qp->io_buf_list_get_lock);
  3292. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  3293. &qp->lpfc_io_buf_list_get,
  3294. list) {
  3295. list_del(&lpfc_ncmd->list);
  3296. qp->get_io_bufs--;
  3297. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  3298. lpfc_ncmd->data, lpfc_ncmd->dma_handle);
  3299. if (phba->cfg_xpsgl && !phba->nvmet_support)
  3300. lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
  3301. lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
  3302. kfree(lpfc_ncmd);
  3303. qp->total_io_bufs--;
  3304. }
  3305. spin_unlock(&qp->io_buf_list_get_lock);
  3306. }
  3307. }
  3308. /**
  3309. * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
  3310. * @phba: pointer to lpfc hba data structure.
  3311. *
  3312. * This routine first calculates the sizes of the current els and allocated
  3313. * scsi sgl lists, and then goes through all sgls to updates the physical
  3314. * XRIs assigned due to port function reset. During port initialization, the
  3315. * current els and allocated scsi sgl lists are 0s.
  3316. *
  3317. * Return codes
  3318. * 0 - successful (for now, it always returns 0)
  3319. **/
  3320. int
  3321. lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
  3322. {
  3323. struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
  3324. uint16_t i, lxri, xri_cnt, els_xri_cnt;
  3325. LIST_HEAD(els_sgl_list);
  3326. int rc;
  3327. /*
  3328. * update on pci function's els xri-sgl list
  3329. */
  3330. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  3331. if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
  3332. /* els xri-sgl expanded */
  3333. xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
  3334. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3335. "3157 ELS xri-sgl count increased from "
  3336. "%d to %d\n", phba->sli4_hba.els_xri_cnt,
  3337. els_xri_cnt);
  3338. /* allocate the additional els sgls */
  3339. for (i = 0; i < xri_cnt; i++) {
  3340. sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
  3341. GFP_KERNEL);
  3342. if (sglq_entry == NULL) {
  3343. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3344. "2562 Failure to allocate an "
  3345. "ELS sgl entry:%d\n", i);
  3346. rc = -ENOMEM;
  3347. goto out_free_mem;
  3348. }
  3349. sglq_entry->buff_type = GEN_BUFF_TYPE;
  3350. sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
  3351. &sglq_entry->phys);
  3352. if (sglq_entry->virt == NULL) {
  3353. kfree(sglq_entry);
  3354. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3355. "2563 Failure to allocate an "
  3356. "ELS mbuf:%d\n", i);
  3357. rc = -ENOMEM;
  3358. goto out_free_mem;
  3359. }
  3360. sglq_entry->sgl = sglq_entry->virt;
  3361. memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
  3362. sglq_entry->state = SGL_FREED;
  3363. list_add_tail(&sglq_entry->list, &els_sgl_list);
  3364. }
  3365. spin_lock_irq(&phba->hbalock);
  3366. spin_lock(&phba->sli4_hba.sgl_list_lock);
  3367. list_splice_init(&els_sgl_list,
  3368. &phba->sli4_hba.lpfc_els_sgl_list);
  3369. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  3370. spin_unlock_irq(&phba->hbalock);
  3371. } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
  3372. /* els xri-sgl shrinked */
  3373. xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
  3374. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3375. "3158 ELS xri-sgl count decreased from "
  3376. "%d to %d\n", phba->sli4_hba.els_xri_cnt,
  3377. els_xri_cnt);
  3378. spin_lock_irq(&phba->hbalock);
  3379. spin_lock(&phba->sli4_hba.sgl_list_lock);
  3380. list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
  3381. &els_sgl_list);
  3382. /* release extra els sgls from list */
  3383. for (i = 0; i < xri_cnt; i++) {
  3384. list_remove_head(&els_sgl_list,
  3385. sglq_entry, struct lpfc_sglq, list);
  3386. if (sglq_entry) {
  3387. __lpfc_mbuf_free(phba, sglq_entry->virt,
  3388. sglq_entry->phys);
  3389. kfree(sglq_entry);
  3390. }
  3391. }
  3392. list_splice_init(&els_sgl_list,
  3393. &phba->sli4_hba.lpfc_els_sgl_list);
  3394. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  3395. spin_unlock_irq(&phba->hbalock);
  3396. } else
  3397. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3398. "3163 ELS xri-sgl count unchanged: %d\n",
  3399. els_xri_cnt);
  3400. phba->sli4_hba.els_xri_cnt = els_xri_cnt;
  3401. /* update xris to els sgls on the list */
  3402. sglq_entry = NULL;
  3403. sglq_entry_next = NULL;
  3404. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  3405. &phba->sli4_hba.lpfc_els_sgl_list, list) {
  3406. lxri = lpfc_sli4_next_xritag(phba);
  3407. if (lxri == NO_XRI) {
  3408. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3409. "2400 Failed to allocate xri for "
  3410. "ELS sgl\n");
  3411. rc = -ENOMEM;
  3412. goto out_free_mem;
  3413. }
  3414. sglq_entry->sli4_lxritag = lxri;
  3415. sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  3416. }
  3417. return 0;
  3418. out_free_mem:
  3419. lpfc_free_els_sgl_list(phba);
  3420. return rc;
  3421. }
  3422. /**
  3423. * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
  3424. * @phba: pointer to lpfc hba data structure.
  3425. *
  3426. * This routine first calculates the sizes of the current els and allocated
  3427. * scsi sgl lists, and then goes through all sgls to updates the physical
  3428. * XRIs assigned due to port function reset. During port initialization, the
  3429. * current els and allocated scsi sgl lists are 0s.
  3430. *
  3431. * Return codes
  3432. * 0 - successful (for now, it always returns 0)
  3433. **/
  3434. int
  3435. lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
  3436. {
  3437. struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
  3438. uint16_t i, lxri, xri_cnt, els_xri_cnt;
  3439. uint16_t nvmet_xri_cnt;
  3440. LIST_HEAD(nvmet_sgl_list);
  3441. int rc;
  3442. /*
  3443. * update on pci function's nvmet xri-sgl list
  3444. */
  3445. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  3446. /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
  3447. nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
  3448. if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
  3449. /* els xri-sgl expanded */
  3450. xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
  3451. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3452. "6302 NVMET xri-sgl cnt grew from %d to %d\n",
  3453. phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
  3454. /* allocate the additional nvmet sgls */
  3455. for (i = 0; i < xri_cnt; i++) {
  3456. sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
  3457. GFP_KERNEL);
  3458. if (sglq_entry == NULL) {
  3459. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3460. "6303 Failure to allocate an "
  3461. "NVMET sgl entry:%d\n", i);
  3462. rc = -ENOMEM;
  3463. goto out_free_mem;
  3464. }
  3465. sglq_entry->buff_type = NVMET_BUFF_TYPE;
  3466. sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
  3467. &sglq_entry->phys);
  3468. if (sglq_entry->virt == NULL) {
  3469. kfree(sglq_entry);
  3470. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3471. "6304 Failure to allocate an "
  3472. "NVMET buf:%d\n", i);
  3473. rc = -ENOMEM;
  3474. goto out_free_mem;
  3475. }
  3476. sglq_entry->sgl = sglq_entry->virt;
  3477. memset(sglq_entry->sgl, 0,
  3478. phba->cfg_sg_dma_buf_size);
  3479. sglq_entry->state = SGL_FREED;
  3480. list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
  3481. }
  3482. spin_lock_irq(&phba->hbalock);
  3483. spin_lock(&phba->sli4_hba.sgl_list_lock);
  3484. list_splice_init(&nvmet_sgl_list,
  3485. &phba->sli4_hba.lpfc_nvmet_sgl_list);
  3486. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  3487. spin_unlock_irq(&phba->hbalock);
  3488. } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
  3489. /* nvmet xri-sgl shrunk */
  3490. xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
  3491. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3492. "6305 NVMET xri-sgl count decreased from "
  3493. "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
  3494. nvmet_xri_cnt);
  3495. spin_lock_irq(&phba->hbalock);
  3496. spin_lock(&phba->sli4_hba.sgl_list_lock);
  3497. list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
  3498. &nvmet_sgl_list);
  3499. /* release extra nvmet sgls from list */
  3500. for (i = 0; i < xri_cnt; i++) {
  3501. list_remove_head(&nvmet_sgl_list,
  3502. sglq_entry, struct lpfc_sglq, list);
  3503. if (sglq_entry) {
  3504. lpfc_nvmet_buf_free(phba, sglq_entry->virt,
  3505. sglq_entry->phys);
  3506. kfree(sglq_entry);
  3507. }
  3508. }
  3509. list_splice_init(&nvmet_sgl_list,
  3510. &phba->sli4_hba.lpfc_nvmet_sgl_list);
  3511. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  3512. spin_unlock_irq(&phba->hbalock);
  3513. } else
  3514. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3515. "6306 NVMET xri-sgl count unchanged: %d\n",
  3516. nvmet_xri_cnt);
  3517. phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
  3518. /* update xris to nvmet sgls on the list */
  3519. sglq_entry = NULL;
  3520. sglq_entry_next = NULL;
  3521. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  3522. &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
  3523. lxri = lpfc_sli4_next_xritag(phba);
  3524. if (lxri == NO_XRI) {
  3525. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3526. "6307 Failed to allocate xri for "
  3527. "NVMET sgl\n");
  3528. rc = -ENOMEM;
  3529. goto out_free_mem;
  3530. }
  3531. sglq_entry->sli4_lxritag = lxri;
  3532. sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  3533. }
  3534. return 0;
  3535. out_free_mem:
  3536. lpfc_free_nvmet_sgl_list(phba);
  3537. return rc;
  3538. }
  3539. int
  3540. lpfc_io_buf_flush(struct lpfc_hba *phba, struct list_head *cbuf)
  3541. {
  3542. LIST_HEAD(blist);
  3543. struct lpfc_sli4_hdw_queue *qp;
  3544. struct lpfc_io_buf *lpfc_cmd;
  3545. struct lpfc_io_buf *iobufp, *prev_iobufp;
  3546. int idx, cnt, xri, inserted;
  3547. cnt = 0;
  3548. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  3549. qp = &phba->sli4_hba.hdwq[idx];
  3550. spin_lock_irq(&qp->io_buf_list_get_lock);
  3551. spin_lock(&qp->io_buf_list_put_lock);
  3552. /* Take everything off the get and put lists */
  3553. list_splice_init(&qp->lpfc_io_buf_list_get, &blist);
  3554. list_splice(&qp->lpfc_io_buf_list_put, &blist);
  3555. INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
  3556. INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
  3557. cnt += qp->get_io_bufs + qp->put_io_bufs;
  3558. qp->get_io_bufs = 0;
  3559. qp->put_io_bufs = 0;
  3560. qp->total_io_bufs = 0;
  3561. spin_unlock(&qp->io_buf_list_put_lock);
  3562. spin_unlock_irq(&qp->io_buf_list_get_lock);
  3563. }
  3564. /*
  3565. * Take IO buffers off blist and put on cbuf sorted by XRI.
  3566. * This is because POST_SGL takes a sequential range of XRIs
  3567. * to post to the firmware.
  3568. */
  3569. for (idx = 0; idx < cnt; idx++) {
  3570. list_remove_head(&blist, lpfc_cmd, struct lpfc_io_buf, list);
  3571. if (!lpfc_cmd)
  3572. return cnt;
  3573. if (idx == 0) {
  3574. list_add_tail(&lpfc_cmd->list, cbuf);
  3575. continue;
  3576. }
  3577. xri = lpfc_cmd->cur_iocbq.sli4_xritag;
  3578. inserted = 0;
  3579. prev_iobufp = NULL;
  3580. list_for_each_entry(iobufp, cbuf, list) {
  3581. if (xri < iobufp->cur_iocbq.sli4_xritag) {
  3582. if (prev_iobufp)
  3583. list_add(&lpfc_cmd->list,
  3584. &prev_iobufp->list);
  3585. else
  3586. list_add(&lpfc_cmd->list, cbuf);
  3587. inserted = 1;
  3588. break;
  3589. }
  3590. prev_iobufp = iobufp;
  3591. }
  3592. if (!inserted)
  3593. list_add_tail(&lpfc_cmd->list, cbuf);
  3594. }
  3595. return cnt;
  3596. }
  3597. int
  3598. lpfc_io_buf_replenish(struct lpfc_hba *phba, struct list_head *cbuf)
  3599. {
  3600. struct lpfc_sli4_hdw_queue *qp;
  3601. struct lpfc_io_buf *lpfc_cmd;
  3602. int idx, cnt;
  3603. qp = phba->sli4_hba.hdwq;
  3604. cnt = 0;
  3605. while (!list_empty(cbuf)) {
  3606. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  3607. list_remove_head(cbuf, lpfc_cmd,
  3608. struct lpfc_io_buf, list);
  3609. if (!lpfc_cmd)
  3610. return cnt;
  3611. cnt++;
  3612. qp = &phba->sli4_hba.hdwq[idx];
  3613. lpfc_cmd->hdwq_no = idx;
  3614. lpfc_cmd->hdwq = qp;
  3615. lpfc_cmd->cur_iocbq.wqe_cmpl = NULL;
  3616. lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
  3617. spin_lock(&qp->io_buf_list_put_lock);
  3618. list_add_tail(&lpfc_cmd->list,
  3619. &qp->lpfc_io_buf_list_put);
  3620. qp->put_io_bufs++;
  3621. qp->total_io_bufs++;
  3622. spin_unlock(&qp->io_buf_list_put_lock);
  3623. }
  3624. }
  3625. return cnt;
  3626. }
  3627. /**
  3628. * lpfc_sli4_io_sgl_update - update xri-sgl sizing and mapping
  3629. * @phba: pointer to lpfc hba data structure.
  3630. *
  3631. * This routine first calculates the sizes of the current els and allocated
  3632. * scsi sgl lists, and then goes through all sgls to updates the physical
  3633. * XRIs assigned due to port function reset. During port initialization, the
  3634. * current els and allocated scsi sgl lists are 0s.
  3635. *
  3636. * Return codes
  3637. * 0 - successful (for now, it always returns 0)
  3638. **/
  3639. int
  3640. lpfc_sli4_io_sgl_update(struct lpfc_hba *phba)
  3641. {
  3642. struct lpfc_io_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
  3643. uint16_t i, lxri, els_xri_cnt;
  3644. uint16_t io_xri_cnt, io_xri_max;
  3645. LIST_HEAD(io_sgl_list);
  3646. int rc, cnt;
  3647. /*
  3648. * update on pci function's allocated nvme xri-sgl list
  3649. */
  3650. /* maximum number of xris available for nvme buffers */
  3651. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  3652. io_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
  3653. phba->sli4_hba.io_xri_max = io_xri_max;
  3654. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3655. "6074 Current allocated XRI sgl count:%d, "
  3656. "maximum XRI count:%d\n",
  3657. phba->sli4_hba.io_xri_cnt,
  3658. phba->sli4_hba.io_xri_max);
  3659. cnt = lpfc_io_buf_flush(phba, &io_sgl_list);
  3660. if (phba->sli4_hba.io_xri_cnt > phba->sli4_hba.io_xri_max) {
  3661. /* max nvme xri shrunk below the allocated nvme buffers */
  3662. io_xri_cnt = phba->sli4_hba.io_xri_cnt -
  3663. phba->sli4_hba.io_xri_max;
  3664. /* release the extra allocated nvme buffers */
  3665. for (i = 0; i < io_xri_cnt; i++) {
  3666. list_remove_head(&io_sgl_list, lpfc_ncmd,
  3667. struct lpfc_io_buf, list);
  3668. if (lpfc_ncmd) {
  3669. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  3670. lpfc_ncmd->data,
  3671. lpfc_ncmd->dma_handle);
  3672. kfree(lpfc_ncmd);
  3673. }
  3674. }
  3675. phba->sli4_hba.io_xri_cnt -= io_xri_cnt;
  3676. }
  3677. /* update xris associated to remaining allocated nvme buffers */
  3678. lpfc_ncmd = NULL;
  3679. lpfc_ncmd_next = NULL;
  3680. phba->sli4_hba.io_xri_cnt = cnt;
  3681. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  3682. &io_sgl_list, list) {
  3683. lxri = lpfc_sli4_next_xritag(phba);
  3684. if (lxri == NO_XRI) {
  3685. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3686. "6075 Failed to allocate xri for "
  3687. "nvme buffer\n");
  3688. rc = -ENOMEM;
  3689. goto out_free_mem;
  3690. }
  3691. lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
  3692. lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  3693. }
  3694. cnt = lpfc_io_buf_replenish(phba, &io_sgl_list);
  3695. return 0;
  3696. out_free_mem:
  3697. lpfc_io_free(phba);
  3698. return rc;
  3699. }
  3700. /**
  3701. * lpfc_new_io_buf - IO buffer allocator for HBA with SLI4 IF spec
  3702. * @vport: The virtual port for which this call being executed.
  3703. * @num_to_allocate: The requested number of buffers to allocate.
  3704. *
  3705. * This routine allocates nvme buffers for device with SLI-4 interface spec,
  3706. * the nvme buffer contains all the necessary information needed to initiate
  3707. * an I/O. After allocating up to @num_to_allocate IO buffers and put
  3708. * them on a list, it post them to the port by using SGL block post.
  3709. *
  3710. * Return codes:
  3711. * int - number of IO buffers that were allocated and posted.
  3712. * 0 = failure, less than num_to_alloc is a partial failure.
  3713. **/
  3714. int
  3715. lpfc_new_io_buf(struct lpfc_hba *phba, int num_to_alloc)
  3716. {
  3717. struct lpfc_io_buf *lpfc_ncmd;
  3718. struct lpfc_iocbq *pwqeq;
  3719. uint16_t iotag, lxri = 0;
  3720. int bcnt, num_posted;
  3721. LIST_HEAD(prep_nblist);
  3722. LIST_HEAD(post_nblist);
  3723. LIST_HEAD(nvme_nblist);
  3724. phba->sli4_hba.io_xri_cnt = 0;
  3725. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  3726. lpfc_ncmd = kzalloc(sizeof(*lpfc_ncmd), GFP_KERNEL);
  3727. if (!lpfc_ncmd)
  3728. break;
  3729. /*
  3730. * Get memory from the pci pool to map the virt space to
  3731. * pci bus space for an I/O. The DMA buffer includes the
  3732. * number of SGE's necessary to support the sg_tablesize.
  3733. */
  3734. lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
  3735. GFP_KERNEL,
  3736. &lpfc_ncmd->dma_handle);
  3737. if (!lpfc_ncmd->data) {
  3738. kfree(lpfc_ncmd);
  3739. break;
  3740. }
  3741. if (phba->cfg_xpsgl && !phba->nvmet_support) {
  3742. INIT_LIST_HEAD(&lpfc_ncmd->dma_sgl_xtra_list);
  3743. } else {
  3744. /*
  3745. * 4K Page alignment is CRITICAL to BlockGuard, double
  3746. * check to be sure.
  3747. */
  3748. if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  3749. (((unsigned long)(lpfc_ncmd->data) &
  3750. (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
  3751. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  3752. "3369 Memory alignment err: "
  3753. "addr=%lx\n",
  3754. (unsigned long)lpfc_ncmd->data);
  3755. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  3756. lpfc_ncmd->data,
  3757. lpfc_ncmd->dma_handle);
  3758. kfree(lpfc_ncmd);
  3759. break;
  3760. }
  3761. }
  3762. INIT_LIST_HEAD(&lpfc_ncmd->dma_cmd_rsp_list);
  3763. lxri = lpfc_sli4_next_xritag(phba);
  3764. if (lxri == NO_XRI) {
  3765. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  3766. lpfc_ncmd->data, lpfc_ncmd->dma_handle);
  3767. kfree(lpfc_ncmd);
  3768. break;
  3769. }
  3770. pwqeq = &lpfc_ncmd->cur_iocbq;
  3771. /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
  3772. iotag = lpfc_sli_next_iotag(phba, pwqeq);
  3773. if (iotag == 0) {
  3774. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  3775. lpfc_ncmd->data, lpfc_ncmd->dma_handle);
  3776. kfree(lpfc_ncmd);
  3777. lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
  3778. "6121 Failed to allocate IOTAG for"
  3779. " XRI:0x%x\n", lxri);
  3780. lpfc_sli4_free_xri(phba, lxri);
  3781. break;
  3782. }
  3783. pwqeq->sli4_lxritag = lxri;
  3784. pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  3785. pwqeq->context1 = lpfc_ncmd;
  3786. /* Initialize local short-hand pointers. */
  3787. lpfc_ncmd->dma_sgl = lpfc_ncmd->data;
  3788. lpfc_ncmd->dma_phys_sgl = lpfc_ncmd->dma_handle;
  3789. lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
  3790. spin_lock_init(&lpfc_ncmd->buf_lock);
  3791. /* add the nvme buffer to a post list */
  3792. list_add_tail(&lpfc_ncmd->list, &post_nblist);
  3793. phba->sli4_hba.io_xri_cnt++;
  3794. }
  3795. lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
  3796. "6114 Allocate %d out of %d requested new NVME "
  3797. "buffers\n", bcnt, num_to_alloc);
  3798. /* post the list of nvme buffer sgls to port if available */
  3799. if (!list_empty(&post_nblist))
  3800. num_posted = lpfc_sli4_post_io_sgl_list(
  3801. phba, &post_nblist, bcnt);
  3802. else
  3803. num_posted = 0;
  3804. return num_posted;
  3805. }
  3806. static uint64_t
  3807. lpfc_get_wwpn(struct lpfc_hba *phba)
  3808. {
  3809. uint64_t wwn;
  3810. int rc;
  3811. LPFC_MBOXQ_t *mboxq;
  3812. MAILBOX_t *mb;
  3813. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
  3814. GFP_KERNEL);
  3815. if (!mboxq)
  3816. return (uint64_t)-1;
  3817. /* First get WWN of HBA instance */
  3818. lpfc_read_nv(phba, mboxq);
  3819. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  3820. if (rc != MBX_SUCCESS) {
  3821. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3822. "6019 Mailbox failed , mbxCmd x%x "
  3823. "READ_NV, mbxStatus x%x\n",
  3824. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  3825. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  3826. mempool_free(mboxq, phba->mbox_mem_pool);
  3827. return (uint64_t) -1;
  3828. }
  3829. mb = &mboxq->u.mb;
  3830. memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
  3831. /* wwn is WWPN of HBA instance */
  3832. mempool_free(mboxq, phba->mbox_mem_pool);
  3833. if (phba->sli_rev == LPFC_SLI_REV4)
  3834. return be64_to_cpu(wwn);
  3835. else
  3836. return rol64(wwn, 32);
  3837. }
  3838. /**
  3839. * lpfc_create_port - Create an FC port
  3840. * @phba: pointer to lpfc hba data structure.
  3841. * @instance: a unique integer ID to this FC port.
  3842. * @dev: pointer to the device data structure.
  3843. *
  3844. * This routine creates a FC port for the upper layer protocol. The FC port
  3845. * can be created on top of either a physical port or a virtual port provided
  3846. * by the HBA. This routine also allocates a SCSI host data structure (shost)
  3847. * and associates the FC port created before adding the shost into the SCSI
  3848. * layer.
  3849. *
  3850. * Return codes
  3851. * @vport - pointer to the virtual N_Port data structure.
  3852. * NULL - port create failed.
  3853. **/
  3854. struct lpfc_vport *
  3855. lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
  3856. {
  3857. struct lpfc_vport *vport;
  3858. struct Scsi_Host *shost = NULL;
  3859. struct scsi_host_template *template;
  3860. int error = 0;
  3861. int i;
  3862. uint64_t wwn;
  3863. bool use_no_reset_hba = false;
  3864. int rc;
  3865. if (lpfc_no_hba_reset_cnt) {
  3866. if (phba->sli_rev < LPFC_SLI_REV4 &&
  3867. dev == &phba->pcidev->dev) {
  3868. /* Reset the port first */
  3869. lpfc_sli_brdrestart(phba);
  3870. rc = lpfc_sli_chipset_init(phba);
  3871. if (rc)
  3872. return NULL;
  3873. }
  3874. wwn = lpfc_get_wwpn(phba);
  3875. }
  3876. for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
  3877. if (wwn == lpfc_no_hba_reset[i]) {
  3878. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3879. "6020 Setting use_no_reset port=%llx\n",
  3880. wwn);
  3881. use_no_reset_hba = true;
  3882. break;
  3883. }
  3884. }
  3885. /* Seed template for SCSI host registration */
  3886. if (dev == &phba->pcidev->dev) {
  3887. template = &phba->port_template;
  3888. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
  3889. /* Seed physical port template */
  3890. memcpy(template, &lpfc_template, sizeof(*template));
  3891. if (use_no_reset_hba) {
  3892. /* template is for a no reset SCSI Host */
  3893. template->max_sectors = 0xffff;
  3894. template->eh_host_reset_handler = NULL;
  3895. }
  3896. /* Template for all vports this physical port creates */
  3897. memcpy(&phba->vport_template, &lpfc_template,
  3898. sizeof(*template));
  3899. phba->vport_template.max_sectors = 0xffff;
  3900. phba->vport_template.shost_attrs = lpfc_vport_attrs;
  3901. phba->vport_template.eh_bus_reset_handler = NULL;
  3902. phba->vport_template.eh_host_reset_handler = NULL;
  3903. phba->vport_template.vendor_id = 0;
  3904. /* Initialize the host templates with updated value */
  3905. if (phba->sli_rev == LPFC_SLI_REV4) {
  3906. template->sg_tablesize = phba->cfg_scsi_seg_cnt;
  3907. phba->vport_template.sg_tablesize =
  3908. phba->cfg_scsi_seg_cnt;
  3909. } else {
  3910. template->sg_tablesize = phba->cfg_sg_seg_cnt;
  3911. phba->vport_template.sg_tablesize =
  3912. phba->cfg_sg_seg_cnt;
  3913. }
  3914. } else {
  3915. /* NVMET is for physical port only */
  3916. memcpy(template, &lpfc_template_nvme,
  3917. sizeof(*template));
  3918. }
  3919. } else {
  3920. template = &phba->vport_template;
  3921. }
  3922. shost = scsi_host_alloc(template, sizeof(struct lpfc_vport));
  3923. if (!shost)
  3924. goto out;
  3925. vport = (struct lpfc_vport *) shost->hostdata;
  3926. vport->phba = phba;
  3927. vport->load_flag |= FC_LOADING;
  3928. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  3929. vport->fc_rscn_flush = 0;
  3930. lpfc_get_vport_cfgparam(vport);
  3931. /* Adjust value in vport */
  3932. vport->cfg_enable_fc4_type = phba->cfg_enable_fc4_type;
  3933. shost->unique_id = instance;
  3934. shost->max_id = LPFC_MAX_TARGET;
  3935. shost->max_lun = vport->cfg_max_luns;
  3936. shost->this_id = -1;
  3937. shost->max_cmd_len = 16;
  3938. if (phba->sli_rev == LPFC_SLI_REV4) {
  3939. if (!phba->cfg_fcp_mq_threshold ||
  3940. phba->cfg_fcp_mq_threshold > phba->cfg_hdw_queue)
  3941. phba->cfg_fcp_mq_threshold = phba->cfg_hdw_queue;
  3942. shost->nr_hw_queues = min_t(int, 2 * num_possible_nodes(),
  3943. phba->cfg_fcp_mq_threshold);
  3944. shost->dma_boundary =
  3945. phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
  3946. if (phba->cfg_xpsgl && !phba->nvmet_support)
  3947. shost->sg_tablesize = LPFC_MAX_SG_TABLESIZE;
  3948. else
  3949. shost->sg_tablesize = phba->cfg_scsi_seg_cnt;
  3950. } else
  3951. /* SLI-3 has a limited number of hardware queues (3),
  3952. * thus there is only one for FCP processing.
  3953. */
  3954. shost->nr_hw_queues = 1;
  3955. /*
  3956. * Set initial can_queue value since 0 is no longer supported and
  3957. * scsi_add_host will fail. This will be adjusted later based on the
  3958. * max xri value determined in hba setup.
  3959. */
  3960. shost->can_queue = phba->cfg_hba_queue_depth - 10;
  3961. if (dev != &phba->pcidev->dev) {
  3962. shost->transportt = lpfc_vport_transport_template;
  3963. vport->port_type = LPFC_NPIV_PORT;
  3964. } else {
  3965. shost->transportt = lpfc_transport_template;
  3966. vport->port_type = LPFC_PHYSICAL_PORT;
  3967. }
  3968. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
  3969. "9081 CreatePort TMPLATE type %x TBLsize %d "
  3970. "SEGcnt %d/%d\n",
  3971. vport->port_type, shost->sg_tablesize,
  3972. phba->cfg_scsi_seg_cnt, phba->cfg_sg_seg_cnt);
  3973. /* Initialize all internally managed lists. */
  3974. INIT_LIST_HEAD(&vport->fc_nodes);
  3975. INIT_LIST_HEAD(&vport->rcv_buffer_list);
  3976. spin_lock_init(&vport->work_port_lock);
  3977. timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
  3978. timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
  3979. timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
  3980. if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
  3981. lpfc_setup_bg(phba, shost);
  3982. error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
  3983. if (error)
  3984. goto out_put_shost;
  3985. spin_lock_irq(&phba->port_list_lock);
  3986. list_add_tail(&vport->listentry, &phba->port_list);
  3987. spin_unlock_irq(&phba->port_list_lock);
  3988. return vport;
  3989. out_put_shost:
  3990. scsi_host_put(shost);
  3991. out:
  3992. return NULL;
  3993. }
  3994. /**
  3995. * destroy_port - destroy an FC port
  3996. * @vport: pointer to an lpfc virtual N_Port data structure.
  3997. *
  3998. * This routine destroys a FC port from the upper layer protocol. All the
  3999. * resources associated with the port are released.
  4000. **/
  4001. void
  4002. destroy_port(struct lpfc_vport *vport)
  4003. {
  4004. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4005. struct lpfc_hba *phba = vport->phba;
  4006. lpfc_debugfs_terminate(vport);
  4007. fc_remove_host(shost);
  4008. scsi_remove_host(shost);
  4009. spin_lock_irq(&phba->port_list_lock);
  4010. list_del_init(&vport->listentry);
  4011. spin_unlock_irq(&phba->port_list_lock);
  4012. lpfc_cleanup(vport);
  4013. return;
  4014. }
  4015. /**
  4016. * lpfc_get_instance - Get a unique integer ID
  4017. *
  4018. * This routine allocates a unique integer ID from lpfc_hba_index pool. It
  4019. * uses the kernel idr facility to perform the task.
  4020. *
  4021. * Return codes:
  4022. * instance - a unique integer ID allocated as the new instance.
  4023. * -1 - lpfc get instance failed.
  4024. **/
  4025. int
  4026. lpfc_get_instance(void)
  4027. {
  4028. int ret;
  4029. ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
  4030. return ret < 0 ? -1 : ret;
  4031. }
  4032. /**
  4033. * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
  4034. * @shost: pointer to SCSI host data structure.
  4035. * @time: elapsed time of the scan in jiffies.
  4036. *
  4037. * This routine is called by the SCSI layer with a SCSI host to determine
  4038. * whether the scan host is finished.
  4039. *
  4040. * Note: there is no scan_start function as adapter initialization will have
  4041. * asynchronously kicked off the link initialization.
  4042. *
  4043. * Return codes
  4044. * 0 - SCSI host scan is not over yet.
  4045. * 1 - SCSI host scan is over.
  4046. **/
  4047. int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  4048. {
  4049. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4050. struct lpfc_hba *phba = vport->phba;
  4051. int stat = 0;
  4052. spin_lock_irq(shost->host_lock);
  4053. if (vport->load_flag & FC_UNLOADING) {
  4054. stat = 1;
  4055. goto finished;
  4056. }
  4057. if (time >= msecs_to_jiffies(30 * 1000)) {
  4058. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4059. "0461 Scanning longer than 30 "
  4060. "seconds. Continuing initialization\n");
  4061. stat = 1;
  4062. goto finished;
  4063. }
  4064. if (time >= msecs_to_jiffies(15 * 1000) &&
  4065. phba->link_state <= LPFC_LINK_DOWN) {
  4066. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4067. "0465 Link down longer than 15 "
  4068. "seconds. Continuing initialization\n");
  4069. stat = 1;
  4070. goto finished;
  4071. }
  4072. if (vport->port_state != LPFC_VPORT_READY)
  4073. goto finished;
  4074. if (vport->num_disc_nodes || vport->fc_prli_sent)
  4075. goto finished;
  4076. if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
  4077. goto finished;
  4078. if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
  4079. goto finished;
  4080. stat = 1;
  4081. finished:
  4082. spin_unlock_irq(shost->host_lock);
  4083. return stat;
  4084. }
  4085. static void lpfc_host_supported_speeds_set(struct Scsi_Host *shost)
  4086. {
  4087. struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
  4088. struct lpfc_hba *phba = vport->phba;
  4089. fc_host_supported_speeds(shost) = 0;
  4090. if (phba->lmt & LMT_128Gb)
  4091. fc_host_supported_speeds(shost) |= FC_PORTSPEED_128GBIT;
  4092. if (phba->lmt & LMT_64Gb)
  4093. fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
  4094. if (phba->lmt & LMT_32Gb)
  4095. fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
  4096. if (phba->lmt & LMT_16Gb)
  4097. fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
  4098. if (phba->lmt & LMT_10Gb)
  4099. fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
  4100. if (phba->lmt & LMT_8Gb)
  4101. fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
  4102. if (phba->lmt & LMT_4Gb)
  4103. fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
  4104. if (phba->lmt & LMT_2Gb)
  4105. fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
  4106. if (phba->lmt & LMT_1Gb)
  4107. fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
  4108. }
  4109. /**
  4110. * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
  4111. * @shost: pointer to SCSI host data structure.
  4112. *
  4113. * This routine initializes a given SCSI host attributes on a FC port. The
  4114. * SCSI host can be either on top of a physical port or a virtual port.
  4115. **/
  4116. void lpfc_host_attrib_init(struct Scsi_Host *shost)
  4117. {
  4118. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4119. struct lpfc_hba *phba = vport->phba;
  4120. /*
  4121. * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
  4122. */
  4123. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  4124. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  4125. fc_host_supported_classes(shost) = FC_COS_CLASS3;
  4126. memset(fc_host_supported_fc4s(shost), 0,
  4127. sizeof(fc_host_supported_fc4s(shost)));
  4128. fc_host_supported_fc4s(shost)[2] = 1;
  4129. fc_host_supported_fc4s(shost)[7] = 1;
  4130. lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
  4131. sizeof fc_host_symbolic_name(shost));
  4132. lpfc_host_supported_speeds_set(shost);
  4133. fc_host_maxframe_size(shost) =
  4134. (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
  4135. (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
  4136. fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
  4137. /* This value is also unchanging */
  4138. memset(fc_host_active_fc4s(shost), 0,
  4139. sizeof(fc_host_active_fc4s(shost)));
  4140. fc_host_active_fc4s(shost)[2] = 1;
  4141. fc_host_active_fc4s(shost)[7] = 1;
  4142. fc_host_max_npiv_vports(shost) = phba->max_vpi;
  4143. spin_lock_irq(shost->host_lock);
  4144. vport->load_flag &= ~FC_LOADING;
  4145. spin_unlock_irq(shost->host_lock);
  4146. }
  4147. /**
  4148. * lpfc_stop_port_s3 - Stop SLI3 device port
  4149. * @phba: pointer to lpfc hba data structure.
  4150. *
  4151. * This routine is invoked to stop an SLI3 device port, it stops the device
  4152. * from generating interrupts and stops the device driver's timers for the
  4153. * device.
  4154. **/
  4155. static void
  4156. lpfc_stop_port_s3(struct lpfc_hba *phba)
  4157. {
  4158. /* Clear all interrupt enable conditions */
  4159. writel(0, phba->HCregaddr);
  4160. readl(phba->HCregaddr); /* flush */
  4161. /* Clear all pending interrupts */
  4162. writel(0xffffffff, phba->HAregaddr);
  4163. readl(phba->HAregaddr); /* flush */
  4164. /* Reset some HBA SLI setup states */
  4165. lpfc_stop_hba_timers(phba);
  4166. phba->pport->work_port_events = 0;
  4167. }
  4168. /**
  4169. * lpfc_stop_port_s4 - Stop SLI4 device port
  4170. * @phba: pointer to lpfc hba data structure.
  4171. *
  4172. * This routine is invoked to stop an SLI4 device port, it stops the device
  4173. * from generating interrupts and stops the device driver's timers for the
  4174. * device.
  4175. **/
  4176. static void
  4177. lpfc_stop_port_s4(struct lpfc_hba *phba)
  4178. {
  4179. /* Reset some HBA SLI4 setup states */
  4180. lpfc_stop_hba_timers(phba);
  4181. if (phba->pport)
  4182. phba->pport->work_port_events = 0;
  4183. phba->sli4_hba.intr_enable = 0;
  4184. }
  4185. /**
  4186. * lpfc_stop_port - Wrapper function for stopping hba port
  4187. * @phba: Pointer to HBA context object.
  4188. *
  4189. * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
  4190. * the API jump table function pointer from the lpfc_hba struct.
  4191. **/
  4192. void
  4193. lpfc_stop_port(struct lpfc_hba *phba)
  4194. {
  4195. phba->lpfc_stop_port(phba);
  4196. if (phba->wq)
  4197. flush_workqueue(phba->wq);
  4198. }
  4199. /**
  4200. * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
  4201. * @phba: Pointer to hba for which this call is being executed.
  4202. *
  4203. * This routine starts the timer waiting for the FCF rediscovery to complete.
  4204. **/
  4205. void
  4206. lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
  4207. {
  4208. unsigned long fcf_redisc_wait_tmo =
  4209. (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
  4210. /* Start fcf rediscovery wait period timer */
  4211. mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
  4212. spin_lock_irq(&phba->hbalock);
  4213. /* Allow action to new fcf asynchronous event */
  4214. phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
  4215. /* Mark the FCF rediscovery pending state */
  4216. phba->fcf.fcf_flag |= FCF_REDISC_PEND;
  4217. spin_unlock_irq(&phba->hbalock);
  4218. }
  4219. /**
  4220. * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
  4221. * @ptr: Map to lpfc_hba data structure pointer.
  4222. *
  4223. * This routine is invoked when waiting for FCF table rediscover has been
  4224. * timed out. If new FCF record(s) has (have) been discovered during the
  4225. * wait period, a new FCF event shall be added to the FCOE async event
  4226. * list, and then worker thread shall be waked up for processing from the
  4227. * worker thread context.
  4228. **/
  4229. static void
  4230. lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
  4231. {
  4232. struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
  4233. /* Don't send FCF rediscovery event if timer cancelled */
  4234. spin_lock_irq(&phba->hbalock);
  4235. if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
  4236. spin_unlock_irq(&phba->hbalock);
  4237. return;
  4238. }
  4239. /* Clear FCF rediscovery timer pending flag */
  4240. phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
  4241. /* FCF rediscovery event to worker thread */
  4242. phba->fcf.fcf_flag |= FCF_REDISC_EVT;
  4243. spin_unlock_irq(&phba->hbalock);
  4244. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  4245. "2776 FCF rediscover quiescent timer expired\n");
  4246. /* wake up worker thread */
  4247. lpfc_worker_wake_up(phba);
  4248. }
  4249. /**
  4250. * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
  4251. * @phba: pointer to lpfc hba data structure.
  4252. * @acqe_link: pointer to the async link completion queue entry.
  4253. *
  4254. * This routine is to parse the SLI4 link-attention link fault code.
  4255. **/
  4256. static void
  4257. lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
  4258. struct lpfc_acqe_link *acqe_link)
  4259. {
  4260. switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
  4261. case LPFC_ASYNC_LINK_FAULT_NONE:
  4262. case LPFC_ASYNC_LINK_FAULT_LOCAL:
  4263. case LPFC_ASYNC_LINK_FAULT_REMOTE:
  4264. case LPFC_ASYNC_LINK_FAULT_LR_LRR:
  4265. break;
  4266. default:
  4267. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4268. "0398 Unknown link fault code: x%x\n",
  4269. bf_get(lpfc_acqe_link_fault, acqe_link));
  4270. break;
  4271. }
  4272. }
  4273. /**
  4274. * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
  4275. * @phba: pointer to lpfc hba data structure.
  4276. * @acqe_link: pointer to the async link completion queue entry.
  4277. *
  4278. * This routine is to parse the SLI4 link attention type and translate it
  4279. * into the base driver's link attention type coding.
  4280. *
  4281. * Return: Link attention type in terms of base driver's coding.
  4282. **/
  4283. static uint8_t
  4284. lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
  4285. struct lpfc_acqe_link *acqe_link)
  4286. {
  4287. uint8_t att_type;
  4288. switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
  4289. case LPFC_ASYNC_LINK_STATUS_DOWN:
  4290. case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
  4291. att_type = LPFC_ATT_LINK_DOWN;
  4292. break;
  4293. case LPFC_ASYNC_LINK_STATUS_UP:
  4294. /* Ignore physical link up events - wait for logical link up */
  4295. att_type = LPFC_ATT_RESERVED;
  4296. break;
  4297. case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
  4298. att_type = LPFC_ATT_LINK_UP;
  4299. break;
  4300. default:
  4301. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4302. "0399 Invalid link attention type: x%x\n",
  4303. bf_get(lpfc_acqe_link_status, acqe_link));
  4304. att_type = LPFC_ATT_RESERVED;
  4305. break;
  4306. }
  4307. return att_type;
  4308. }
  4309. /**
  4310. * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
  4311. * @phba: pointer to lpfc hba data structure.
  4312. *
  4313. * This routine is to get an SLI3 FC port's link speed in Mbps.
  4314. *
  4315. * Return: link speed in terms of Mbps.
  4316. **/
  4317. uint32_t
  4318. lpfc_sli_port_speed_get(struct lpfc_hba *phba)
  4319. {
  4320. uint32_t link_speed;
  4321. if (!lpfc_is_link_up(phba))
  4322. return 0;
  4323. if (phba->sli_rev <= LPFC_SLI_REV3) {
  4324. switch (phba->fc_linkspeed) {
  4325. case LPFC_LINK_SPEED_1GHZ:
  4326. link_speed = 1000;
  4327. break;
  4328. case LPFC_LINK_SPEED_2GHZ:
  4329. link_speed = 2000;
  4330. break;
  4331. case LPFC_LINK_SPEED_4GHZ:
  4332. link_speed = 4000;
  4333. break;
  4334. case LPFC_LINK_SPEED_8GHZ:
  4335. link_speed = 8000;
  4336. break;
  4337. case LPFC_LINK_SPEED_10GHZ:
  4338. link_speed = 10000;
  4339. break;
  4340. case LPFC_LINK_SPEED_16GHZ:
  4341. link_speed = 16000;
  4342. break;
  4343. default:
  4344. link_speed = 0;
  4345. }
  4346. } else {
  4347. if (phba->sli4_hba.link_state.logical_speed)
  4348. link_speed =
  4349. phba->sli4_hba.link_state.logical_speed;
  4350. else
  4351. link_speed = phba->sli4_hba.link_state.speed;
  4352. }
  4353. return link_speed;
  4354. }
  4355. /**
  4356. * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
  4357. * @phba: pointer to lpfc hba data structure.
  4358. * @evt_code: asynchronous event code.
  4359. * @speed_code: asynchronous event link speed code.
  4360. *
  4361. * This routine is to parse the giving SLI4 async event link speed code into
  4362. * value of Mbps for the link speed.
  4363. *
  4364. * Return: link speed in terms of Mbps.
  4365. **/
  4366. static uint32_t
  4367. lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
  4368. uint8_t speed_code)
  4369. {
  4370. uint32_t port_speed;
  4371. switch (evt_code) {
  4372. case LPFC_TRAILER_CODE_LINK:
  4373. switch (speed_code) {
  4374. case LPFC_ASYNC_LINK_SPEED_ZERO:
  4375. port_speed = 0;
  4376. break;
  4377. case LPFC_ASYNC_LINK_SPEED_10MBPS:
  4378. port_speed = 10;
  4379. break;
  4380. case LPFC_ASYNC_LINK_SPEED_100MBPS:
  4381. port_speed = 100;
  4382. break;
  4383. case LPFC_ASYNC_LINK_SPEED_1GBPS:
  4384. port_speed = 1000;
  4385. break;
  4386. case LPFC_ASYNC_LINK_SPEED_10GBPS:
  4387. port_speed = 10000;
  4388. break;
  4389. case LPFC_ASYNC_LINK_SPEED_20GBPS:
  4390. port_speed = 20000;
  4391. break;
  4392. case LPFC_ASYNC_LINK_SPEED_25GBPS:
  4393. port_speed = 25000;
  4394. break;
  4395. case LPFC_ASYNC_LINK_SPEED_40GBPS:
  4396. port_speed = 40000;
  4397. break;
  4398. default:
  4399. port_speed = 0;
  4400. }
  4401. break;
  4402. case LPFC_TRAILER_CODE_FC:
  4403. switch (speed_code) {
  4404. case LPFC_FC_LA_SPEED_UNKNOWN:
  4405. port_speed = 0;
  4406. break;
  4407. case LPFC_FC_LA_SPEED_1G:
  4408. port_speed = 1000;
  4409. break;
  4410. case LPFC_FC_LA_SPEED_2G:
  4411. port_speed = 2000;
  4412. break;
  4413. case LPFC_FC_LA_SPEED_4G:
  4414. port_speed = 4000;
  4415. break;
  4416. case LPFC_FC_LA_SPEED_8G:
  4417. port_speed = 8000;
  4418. break;
  4419. case LPFC_FC_LA_SPEED_10G:
  4420. port_speed = 10000;
  4421. break;
  4422. case LPFC_FC_LA_SPEED_16G:
  4423. port_speed = 16000;
  4424. break;
  4425. case LPFC_FC_LA_SPEED_32G:
  4426. port_speed = 32000;
  4427. break;
  4428. case LPFC_FC_LA_SPEED_64G:
  4429. port_speed = 64000;
  4430. break;
  4431. case LPFC_FC_LA_SPEED_128G:
  4432. port_speed = 128000;
  4433. break;
  4434. default:
  4435. port_speed = 0;
  4436. }
  4437. break;
  4438. default:
  4439. port_speed = 0;
  4440. }
  4441. return port_speed;
  4442. }
  4443. /**
  4444. * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
  4445. * @phba: pointer to lpfc hba data structure.
  4446. * @acqe_link: pointer to the async link completion queue entry.
  4447. *
  4448. * This routine is to handle the SLI4 asynchronous FCoE link event.
  4449. **/
  4450. static void
  4451. lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
  4452. struct lpfc_acqe_link *acqe_link)
  4453. {
  4454. struct lpfc_dmabuf *mp;
  4455. LPFC_MBOXQ_t *pmb;
  4456. MAILBOX_t *mb;
  4457. struct lpfc_mbx_read_top *la;
  4458. uint8_t att_type;
  4459. int rc;
  4460. att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
  4461. if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
  4462. return;
  4463. phba->fcoe_eventtag = acqe_link->event_tag;
  4464. pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4465. if (!pmb) {
  4466. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4467. "0395 The mboxq allocation failed\n");
  4468. return;
  4469. }
  4470. mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4471. if (!mp) {
  4472. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4473. "0396 The lpfc_dmabuf allocation failed\n");
  4474. goto out_free_pmb;
  4475. }
  4476. mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
  4477. if (!mp->virt) {
  4478. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4479. "0397 The mbuf allocation failed\n");
  4480. goto out_free_dmabuf;
  4481. }
  4482. /* Cleanup any outstanding ELS commands */
  4483. lpfc_els_flush_all_cmd(phba);
  4484. /* Block ELS IOCBs until we have done process link event */
  4485. phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
  4486. /* Update link event statistics */
  4487. phba->sli.slistat.link_event++;
  4488. /* Create lpfc_handle_latt mailbox command from link ACQE */
  4489. lpfc_read_topology(phba, pmb, mp);
  4490. pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
  4491. pmb->vport = phba->pport;
  4492. /* Keep the link status for extra SLI4 state machine reference */
  4493. phba->sli4_hba.link_state.speed =
  4494. lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
  4495. bf_get(lpfc_acqe_link_speed, acqe_link));
  4496. phba->sli4_hba.link_state.duplex =
  4497. bf_get(lpfc_acqe_link_duplex, acqe_link);
  4498. phba->sli4_hba.link_state.status =
  4499. bf_get(lpfc_acqe_link_status, acqe_link);
  4500. phba->sli4_hba.link_state.type =
  4501. bf_get(lpfc_acqe_link_type, acqe_link);
  4502. phba->sli4_hba.link_state.number =
  4503. bf_get(lpfc_acqe_link_number, acqe_link);
  4504. phba->sli4_hba.link_state.fault =
  4505. bf_get(lpfc_acqe_link_fault, acqe_link);
  4506. phba->sli4_hba.link_state.logical_speed =
  4507. bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
  4508. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4509. "2900 Async FC/FCoE Link event - Speed:%dGBit "
  4510. "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
  4511. "Logical speed:%dMbps Fault:%d\n",
  4512. phba->sli4_hba.link_state.speed,
  4513. phba->sli4_hba.link_state.topology,
  4514. phba->sli4_hba.link_state.status,
  4515. phba->sli4_hba.link_state.type,
  4516. phba->sli4_hba.link_state.number,
  4517. phba->sli4_hba.link_state.logical_speed,
  4518. phba->sli4_hba.link_state.fault);
  4519. /*
  4520. * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
  4521. * topology info. Note: Optional for non FC-AL ports.
  4522. */
  4523. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  4524. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  4525. if (rc == MBX_NOT_FINISHED)
  4526. goto out_free_dmabuf;
  4527. return;
  4528. }
  4529. /*
  4530. * For FCoE Mode: fill in all the topology information we need and call
  4531. * the READ_TOPOLOGY completion routine to continue without actually
  4532. * sending the READ_TOPOLOGY mailbox command to the port.
  4533. */
  4534. /* Initialize completion status */
  4535. mb = &pmb->u.mb;
  4536. mb->mbxStatus = MBX_SUCCESS;
  4537. /* Parse port fault information field */
  4538. lpfc_sli4_parse_latt_fault(phba, acqe_link);
  4539. /* Parse and translate link attention fields */
  4540. la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
  4541. la->eventTag = acqe_link->event_tag;
  4542. bf_set(lpfc_mbx_read_top_att_type, la, att_type);
  4543. bf_set(lpfc_mbx_read_top_link_spd, la,
  4544. (bf_get(lpfc_acqe_link_speed, acqe_link)));
  4545. /* Fake the the following irrelvant fields */
  4546. bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
  4547. bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
  4548. bf_set(lpfc_mbx_read_top_il, la, 0);
  4549. bf_set(lpfc_mbx_read_top_pb, la, 0);
  4550. bf_set(lpfc_mbx_read_top_fa, la, 0);
  4551. bf_set(lpfc_mbx_read_top_mm, la, 0);
  4552. /* Invoke the lpfc_handle_latt mailbox command callback function */
  4553. lpfc_mbx_cmpl_read_topology(phba, pmb);
  4554. return;
  4555. out_free_dmabuf:
  4556. kfree(mp);
  4557. out_free_pmb:
  4558. mempool_free(pmb, phba->mbox_mem_pool);
  4559. }
  4560. /**
  4561. * lpfc_async_link_speed_to_read_top - Parse async evt link speed code to read
  4562. * topology.
  4563. * @phba: pointer to lpfc hba data structure.
  4564. * @evt_code: asynchronous event code.
  4565. * @speed_code: asynchronous event link speed code.
  4566. *
  4567. * This routine is to parse the giving SLI4 async event link speed code into
  4568. * value of Read topology link speed.
  4569. *
  4570. * Return: link speed in terms of Read topology.
  4571. **/
  4572. static uint8_t
  4573. lpfc_async_link_speed_to_read_top(struct lpfc_hba *phba, uint8_t speed_code)
  4574. {
  4575. uint8_t port_speed;
  4576. switch (speed_code) {
  4577. case LPFC_FC_LA_SPEED_1G:
  4578. port_speed = LPFC_LINK_SPEED_1GHZ;
  4579. break;
  4580. case LPFC_FC_LA_SPEED_2G:
  4581. port_speed = LPFC_LINK_SPEED_2GHZ;
  4582. break;
  4583. case LPFC_FC_LA_SPEED_4G:
  4584. port_speed = LPFC_LINK_SPEED_4GHZ;
  4585. break;
  4586. case LPFC_FC_LA_SPEED_8G:
  4587. port_speed = LPFC_LINK_SPEED_8GHZ;
  4588. break;
  4589. case LPFC_FC_LA_SPEED_16G:
  4590. port_speed = LPFC_LINK_SPEED_16GHZ;
  4591. break;
  4592. case LPFC_FC_LA_SPEED_32G:
  4593. port_speed = LPFC_LINK_SPEED_32GHZ;
  4594. break;
  4595. case LPFC_FC_LA_SPEED_64G:
  4596. port_speed = LPFC_LINK_SPEED_64GHZ;
  4597. break;
  4598. case LPFC_FC_LA_SPEED_128G:
  4599. port_speed = LPFC_LINK_SPEED_128GHZ;
  4600. break;
  4601. case LPFC_FC_LA_SPEED_256G:
  4602. port_speed = LPFC_LINK_SPEED_256GHZ;
  4603. break;
  4604. default:
  4605. port_speed = 0;
  4606. break;
  4607. }
  4608. return port_speed;
  4609. }
  4610. #define trunk_link_status(__idx)\
  4611. bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
  4612. ((phba->trunk_link.link##__idx.state == LPFC_LINK_UP) ?\
  4613. "Link up" : "Link down") : "NA"
  4614. /* Did port __idx reported an error */
  4615. #define trunk_port_fault(__idx)\
  4616. bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
  4617. (port_fault & (1 << __idx) ? "YES" : "NO") : "NA"
  4618. static void
  4619. lpfc_update_trunk_link_status(struct lpfc_hba *phba,
  4620. struct lpfc_acqe_fc_la *acqe_fc)
  4621. {
  4622. uint8_t port_fault = bf_get(lpfc_acqe_fc_la_trunk_linkmask, acqe_fc);
  4623. uint8_t err = bf_get(lpfc_acqe_fc_la_trunk_fault, acqe_fc);
  4624. phba->sli4_hba.link_state.speed =
  4625. lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
  4626. bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
  4627. phba->sli4_hba.link_state.logical_speed =
  4628. bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
  4629. /* We got FC link speed, convert to fc_linkspeed (READ_TOPOLOGY) */
  4630. phba->fc_linkspeed =
  4631. lpfc_async_link_speed_to_read_top(
  4632. phba,
  4633. bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
  4634. if (bf_get(lpfc_acqe_fc_la_trunk_config_port0, acqe_fc)) {
  4635. phba->trunk_link.link0.state =
  4636. bf_get(lpfc_acqe_fc_la_trunk_link_status_port0, acqe_fc)
  4637. ? LPFC_LINK_UP : LPFC_LINK_DOWN;
  4638. phba->trunk_link.link0.fault = port_fault & 0x1 ? err : 0;
  4639. }
  4640. if (bf_get(lpfc_acqe_fc_la_trunk_config_port1, acqe_fc)) {
  4641. phba->trunk_link.link1.state =
  4642. bf_get(lpfc_acqe_fc_la_trunk_link_status_port1, acqe_fc)
  4643. ? LPFC_LINK_UP : LPFC_LINK_DOWN;
  4644. phba->trunk_link.link1.fault = port_fault & 0x2 ? err : 0;
  4645. }
  4646. if (bf_get(lpfc_acqe_fc_la_trunk_config_port2, acqe_fc)) {
  4647. phba->trunk_link.link2.state =
  4648. bf_get(lpfc_acqe_fc_la_trunk_link_status_port2, acqe_fc)
  4649. ? LPFC_LINK_UP : LPFC_LINK_DOWN;
  4650. phba->trunk_link.link2.fault = port_fault & 0x4 ? err : 0;
  4651. }
  4652. if (bf_get(lpfc_acqe_fc_la_trunk_config_port3, acqe_fc)) {
  4653. phba->trunk_link.link3.state =
  4654. bf_get(lpfc_acqe_fc_la_trunk_link_status_port3, acqe_fc)
  4655. ? LPFC_LINK_UP : LPFC_LINK_DOWN;
  4656. phba->trunk_link.link3.fault = port_fault & 0x8 ? err : 0;
  4657. }
  4658. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4659. "2910 Async FC Trunking Event - Speed:%d\n"
  4660. "\tLogical speed:%d "
  4661. "port0: %s port1: %s port2: %s port3: %s\n",
  4662. phba->sli4_hba.link_state.speed,
  4663. phba->sli4_hba.link_state.logical_speed,
  4664. trunk_link_status(0), trunk_link_status(1),
  4665. trunk_link_status(2), trunk_link_status(3));
  4666. if (port_fault)
  4667. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4668. "3202 trunk error:0x%x (%s) seen on port0:%s "
  4669. /*
  4670. * SLI-4: We have only 0xA error codes
  4671. * defined as of now. print an appropriate
  4672. * message in case driver needs to be updated.
  4673. */
  4674. "port1:%s port2:%s port3:%s\n", err, err > 0xA ?
  4675. "UNDEFINED. update driver." : trunk_errmsg[err],
  4676. trunk_port_fault(0), trunk_port_fault(1),
  4677. trunk_port_fault(2), trunk_port_fault(3));
  4678. }
  4679. /**
  4680. * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
  4681. * @phba: pointer to lpfc hba data structure.
  4682. * @acqe_fc: pointer to the async fc completion queue entry.
  4683. *
  4684. * This routine is to handle the SLI4 asynchronous FC event. It will simply log
  4685. * that the event was received and then issue a read_topology mailbox command so
  4686. * that the rest of the driver will treat it the same as SLI3.
  4687. **/
  4688. static void
  4689. lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
  4690. {
  4691. struct lpfc_dmabuf *mp;
  4692. LPFC_MBOXQ_t *pmb;
  4693. MAILBOX_t *mb;
  4694. struct lpfc_mbx_read_top *la;
  4695. int rc;
  4696. if (bf_get(lpfc_trailer_type, acqe_fc) !=
  4697. LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
  4698. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4699. "2895 Non FC link Event detected.(%d)\n",
  4700. bf_get(lpfc_trailer_type, acqe_fc));
  4701. return;
  4702. }
  4703. if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
  4704. LPFC_FC_LA_TYPE_TRUNKING_EVENT) {
  4705. lpfc_update_trunk_link_status(phba, acqe_fc);
  4706. return;
  4707. }
  4708. /* Keep the link status for extra SLI4 state machine reference */
  4709. phba->sli4_hba.link_state.speed =
  4710. lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
  4711. bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
  4712. phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
  4713. phba->sli4_hba.link_state.topology =
  4714. bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
  4715. phba->sli4_hba.link_state.status =
  4716. bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
  4717. phba->sli4_hba.link_state.type =
  4718. bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
  4719. phba->sli4_hba.link_state.number =
  4720. bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
  4721. phba->sli4_hba.link_state.fault =
  4722. bf_get(lpfc_acqe_link_fault, acqe_fc);
  4723. if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
  4724. LPFC_FC_LA_TYPE_LINK_DOWN)
  4725. phba->sli4_hba.link_state.logical_speed = 0;
  4726. else if (!phba->sli4_hba.conf_trunk)
  4727. phba->sli4_hba.link_state.logical_speed =
  4728. bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
  4729. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4730. "2896 Async FC event - Speed:%dGBaud Topology:x%x "
  4731. "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
  4732. "%dMbps Fault:%d\n",
  4733. phba->sli4_hba.link_state.speed,
  4734. phba->sli4_hba.link_state.topology,
  4735. phba->sli4_hba.link_state.status,
  4736. phba->sli4_hba.link_state.type,
  4737. phba->sli4_hba.link_state.number,
  4738. phba->sli4_hba.link_state.logical_speed,
  4739. phba->sli4_hba.link_state.fault);
  4740. pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4741. if (!pmb) {
  4742. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4743. "2897 The mboxq allocation failed\n");
  4744. return;
  4745. }
  4746. mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4747. if (!mp) {
  4748. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4749. "2898 The lpfc_dmabuf allocation failed\n");
  4750. goto out_free_pmb;
  4751. }
  4752. mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
  4753. if (!mp->virt) {
  4754. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4755. "2899 The mbuf allocation failed\n");
  4756. goto out_free_dmabuf;
  4757. }
  4758. /* Cleanup any outstanding ELS commands */
  4759. lpfc_els_flush_all_cmd(phba);
  4760. /* Block ELS IOCBs until we have done process link event */
  4761. phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
  4762. /* Update link event statistics */
  4763. phba->sli.slistat.link_event++;
  4764. /* Create lpfc_handle_latt mailbox command from link ACQE */
  4765. lpfc_read_topology(phba, pmb, mp);
  4766. pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
  4767. pmb->vport = phba->pport;
  4768. if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
  4769. phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
  4770. switch (phba->sli4_hba.link_state.status) {
  4771. case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
  4772. phba->link_flag |= LS_MDS_LINK_DOWN;
  4773. break;
  4774. case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
  4775. phba->link_flag |= LS_MDS_LOOPBACK;
  4776. break;
  4777. default:
  4778. break;
  4779. }
  4780. /* Initialize completion status */
  4781. mb = &pmb->u.mb;
  4782. mb->mbxStatus = MBX_SUCCESS;
  4783. /* Parse port fault information field */
  4784. lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
  4785. /* Parse and translate link attention fields */
  4786. la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
  4787. la->eventTag = acqe_fc->event_tag;
  4788. if (phba->sli4_hba.link_state.status ==
  4789. LPFC_FC_LA_TYPE_UNEXP_WWPN) {
  4790. bf_set(lpfc_mbx_read_top_att_type, la,
  4791. LPFC_FC_LA_TYPE_UNEXP_WWPN);
  4792. } else {
  4793. bf_set(lpfc_mbx_read_top_att_type, la,
  4794. LPFC_FC_LA_TYPE_LINK_DOWN);
  4795. }
  4796. /* Invoke the mailbox command callback function */
  4797. lpfc_mbx_cmpl_read_topology(phba, pmb);
  4798. return;
  4799. }
  4800. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  4801. if (rc == MBX_NOT_FINISHED)
  4802. goto out_free_dmabuf;
  4803. return;
  4804. out_free_dmabuf:
  4805. kfree(mp);
  4806. out_free_pmb:
  4807. mempool_free(pmb, phba->mbox_mem_pool);
  4808. }
  4809. /**
  4810. * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
  4811. * @phba: pointer to lpfc hba data structure.
  4812. * @acqe_fc: pointer to the async SLI completion queue entry.
  4813. *
  4814. * This routine is to handle the SLI4 asynchronous SLI events.
  4815. **/
  4816. static void
  4817. lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
  4818. {
  4819. char port_name;
  4820. char message[128];
  4821. uint8_t status;
  4822. uint8_t evt_type;
  4823. uint8_t operational = 0;
  4824. struct temp_event temp_event_data;
  4825. struct lpfc_acqe_misconfigured_event *misconfigured;
  4826. struct Scsi_Host *shost;
  4827. struct lpfc_vport **vports;
  4828. int rc, i;
  4829. evt_type = bf_get(lpfc_trailer_type, acqe_sli);
  4830. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4831. "2901 Async SLI event - Type:%d, Event Data: x%08x "
  4832. "x%08x x%08x x%08x\n", evt_type,
  4833. acqe_sli->event_data1, acqe_sli->event_data2,
  4834. acqe_sli->reserved, acqe_sli->trailer);
  4835. port_name = phba->Port[0];
  4836. if (port_name == 0x00)
  4837. port_name = '?'; /* get port name is empty */
  4838. switch (evt_type) {
  4839. case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
  4840. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  4841. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  4842. temp_event_data.data = (uint32_t)acqe_sli->event_data1;
  4843. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4844. "3190 Over Temperature:%d Celsius- Port Name %c\n",
  4845. acqe_sli->event_data1, port_name);
  4846. phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
  4847. shost = lpfc_shost_from_vport(phba->pport);
  4848. fc_host_post_vendor_event(shost, fc_get_event_number(),
  4849. sizeof(temp_event_data),
  4850. (char *)&temp_event_data,
  4851. SCSI_NL_VID_TYPE_PCI
  4852. | PCI_VENDOR_ID_EMULEX);
  4853. break;
  4854. case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
  4855. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  4856. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  4857. temp_event_data.data = (uint32_t)acqe_sli->event_data1;
  4858. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4859. "3191 Normal Temperature:%d Celsius - Port Name %c\n",
  4860. acqe_sli->event_data1, port_name);
  4861. shost = lpfc_shost_from_vport(phba->pport);
  4862. fc_host_post_vendor_event(shost, fc_get_event_number(),
  4863. sizeof(temp_event_data),
  4864. (char *)&temp_event_data,
  4865. SCSI_NL_VID_TYPE_PCI
  4866. | PCI_VENDOR_ID_EMULEX);
  4867. break;
  4868. case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
  4869. misconfigured = (struct lpfc_acqe_misconfigured_event *)
  4870. &acqe_sli->event_data1;
  4871. /* fetch the status for this port */
  4872. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4873. case LPFC_LINK_NUMBER_0:
  4874. status = bf_get(lpfc_sli_misconfigured_port0_state,
  4875. &misconfigured->theEvent);
  4876. operational = bf_get(lpfc_sli_misconfigured_port0_op,
  4877. &misconfigured->theEvent);
  4878. break;
  4879. case LPFC_LINK_NUMBER_1:
  4880. status = bf_get(lpfc_sli_misconfigured_port1_state,
  4881. &misconfigured->theEvent);
  4882. operational = bf_get(lpfc_sli_misconfigured_port1_op,
  4883. &misconfigured->theEvent);
  4884. break;
  4885. case LPFC_LINK_NUMBER_2:
  4886. status = bf_get(lpfc_sli_misconfigured_port2_state,
  4887. &misconfigured->theEvent);
  4888. operational = bf_get(lpfc_sli_misconfigured_port2_op,
  4889. &misconfigured->theEvent);
  4890. break;
  4891. case LPFC_LINK_NUMBER_3:
  4892. status = bf_get(lpfc_sli_misconfigured_port3_state,
  4893. &misconfigured->theEvent);
  4894. operational = bf_get(lpfc_sli_misconfigured_port3_op,
  4895. &misconfigured->theEvent);
  4896. break;
  4897. default:
  4898. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4899. "3296 "
  4900. "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
  4901. "event: Invalid link %d",
  4902. phba->sli4_hba.lnk_info.lnk_no);
  4903. return;
  4904. }
  4905. /* Skip if optic state unchanged */
  4906. if (phba->sli4_hba.lnk_info.optic_state == status)
  4907. return;
  4908. switch (status) {
  4909. case LPFC_SLI_EVENT_STATUS_VALID:
  4910. sprintf(message, "Physical Link is functional");
  4911. break;
  4912. case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
  4913. sprintf(message, "Optics faulted/incorrectly "
  4914. "installed/not installed - Reseat optics, "
  4915. "if issue not resolved, replace.");
  4916. break;
  4917. case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
  4918. sprintf(message,
  4919. "Optics of two types installed - Remove one "
  4920. "optic or install matching pair of optics.");
  4921. break;
  4922. case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
  4923. sprintf(message, "Incompatible optics - Replace with "
  4924. "compatible optics for card to function.");
  4925. break;
  4926. case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
  4927. sprintf(message, "Unqualified optics - Replace with "
  4928. "Avago optics for Warranty and Technical "
  4929. "Support - Link is%s operational",
  4930. (operational) ? " not" : "");
  4931. break;
  4932. case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
  4933. sprintf(message, "Uncertified optics - Replace with "
  4934. "Avago-certified optics to enable link "
  4935. "operation - Link is%s operational",
  4936. (operational) ? " not" : "");
  4937. break;
  4938. default:
  4939. /* firmware is reporting a status we don't know about */
  4940. sprintf(message, "Unknown event status x%02x", status);
  4941. break;
  4942. }
  4943. /* Issue READ_CONFIG mbox command to refresh supported speeds */
  4944. rc = lpfc_sli4_read_config(phba);
  4945. if (rc) {
  4946. phba->lmt = 0;
  4947. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4948. "3194 Unable to retrieve supported "
  4949. "speeds, rc = 0x%x\n", rc);
  4950. }
  4951. vports = lpfc_create_vport_work_array(phba);
  4952. if (vports != NULL) {
  4953. for (i = 0; i <= phba->max_vports && vports[i] != NULL;
  4954. i++) {
  4955. shost = lpfc_shost_from_vport(vports[i]);
  4956. lpfc_host_supported_speeds_set(shost);
  4957. }
  4958. }
  4959. lpfc_destroy_vport_work_array(phba, vports);
  4960. phba->sli4_hba.lnk_info.optic_state = status;
  4961. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4962. "3176 Port Name %c %s\n", port_name, message);
  4963. break;
  4964. case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
  4965. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4966. "3192 Remote DPort Test Initiated - "
  4967. "Event Data1:x%08x Event Data2: x%08x\n",
  4968. acqe_sli->event_data1, acqe_sli->event_data2);
  4969. break;
  4970. case LPFC_SLI_EVENT_TYPE_MISCONF_FAWWN:
  4971. /* Misconfigured WWN. Reports that the SLI Port is configured
  4972. * to use FA-WWN, but the attached device doesn’t support it.
  4973. * No driver action is required.
  4974. * Event Data1 - N.A, Event Data2 - N.A
  4975. */
  4976. lpfc_log_msg(phba, KERN_WARNING, LOG_SLI,
  4977. "2699 Misconfigured FA-WWN - Attached device does "
  4978. "not support FA-WWN\n");
  4979. break;
  4980. case LPFC_SLI_EVENT_TYPE_EEPROM_FAILURE:
  4981. /* EEPROM failure. No driver action is required */
  4982. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4983. "2518 EEPROM failure - "
  4984. "Event Data1: x%08x Event Data2: x%08x\n",
  4985. acqe_sli->event_data1, acqe_sli->event_data2);
  4986. break;
  4987. default:
  4988. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4989. "3193 Unrecognized SLI event, type: 0x%x",
  4990. evt_type);
  4991. break;
  4992. }
  4993. }
  4994. /**
  4995. * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
  4996. * @vport: pointer to vport data structure.
  4997. *
  4998. * This routine is to perform Clear Virtual Link (CVL) on a vport in
  4999. * response to a CVL event.
  5000. *
  5001. * Return the pointer to the ndlp with the vport if successful, otherwise
  5002. * return NULL.
  5003. **/
  5004. static struct lpfc_nodelist *
  5005. lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
  5006. {
  5007. struct lpfc_nodelist *ndlp;
  5008. struct Scsi_Host *shost;
  5009. struct lpfc_hba *phba;
  5010. if (!vport)
  5011. return NULL;
  5012. phba = vport->phba;
  5013. if (!phba)
  5014. return NULL;
  5015. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  5016. if (!ndlp) {
  5017. /* Cannot find existing Fabric ndlp, so allocate a new one */
  5018. ndlp = lpfc_nlp_init(vport, Fabric_DID);
  5019. if (!ndlp)
  5020. return 0;
  5021. /* Set the node type */
  5022. ndlp->nlp_type |= NLP_FABRIC;
  5023. /* Put ndlp onto node list */
  5024. lpfc_enqueue_node(vport, ndlp);
  5025. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  5026. /* re-setup ndlp without removing from node list */
  5027. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
  5028. if (!ndlp)
  5029. return 0;
  5030. }
  5031. if ((phba->pport->port_state < LPFC_FLOGI) &&
  5032. (phba->pport->port_state != LPFC_VPORT_FAILED))
  5033. return NULL;
  5034. /* If virtual link is not yet instantiated ignore CVL */
  5035. if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
  5036. && (vport->port_state != LPFC_VPORT_FAILED))
  5037. return NULL;
  5038. shost = lpfc_shost_from_vport(vport);
  5039. if (!shost)
  5040. return NULL;
  5041. lpfc_linkdown_port(vport);
  5042. lpfc_cleanup_pending_mbox(vport);
  5043. spin_lock_irq(shost->host_lock);
  5044. vport->fc_flag |= FC_VPORT_CVL_RCVD;
  5045. spin_unlock_irq(shost->host_lock);
  5046. return ndlp;
  5047. }
  5048. /**
  5049. * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
  5050. * @vport: pointer to lpfc hba data structure.
  5051. *
  5052. * This routine is to perform Clear Virtual Link (CVL) on all vports in
  5053. * response to a FCF dead event.
  5054. **/
  5055. static void
  5056. lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
  5057. {
  5058. struct lpfc_vport **vports;
  5059. int i;
  5060. vports = lpfc_create_vport_work_array(phba);
  5061. if (vports)
  5062. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  5063. lpfc_sli4_perform_vport_cvl(vports[i]);
  5064. lpfc_destroy_vport_work_array(phba, vports);
  5065. }
  5066. /**
  5067. * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
  5068. * @phba: pointer to lpfc hba data structure.
  5069. * @acqe_link: pointer to the async fcoe completion queue entry.
  5070. *
  5071. * This routine is to handle the SLI4 asynchronous fcoe event.
  5072. **/
  5073. static void
  5074. lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
  5075. struct lpfc_acqe_fip *acqe_fip)
  5076. {
  5077. uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
  5078. int rc;
  5079. struct lpfc_vport *vport;
  5080. struct lpfc_nodelist *ndlp;
  5081. struct Scsi_Host *shost;
  5082. int active_vlink_present;
  5083. struct lpfc_vport **vports;
  5084. int i;
  5085. phba->fc_eventTag = acqe_fip->event_tag;
  5086. phba->fcoe_eventtag = acqe_fip->event_tag;
  5087. switch (event_type) {
  5088. case LPFC_FIP_EVENT_TYPE_NEW_FCF:
  5089. case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
  5090. if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
  5091. lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
  5092. LOG_DISCOVERY,
  5093. "2546 New FCF event, evt_tag:x%x, "
  5094. "index:x%x\n",
  5095. acqe_fip->event_tag,
  5096. acqe_fip->index);
  5097. else
  5098. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
  5099. LOG_DISCOVERY,
  5100. "2788 FCF param modified event, "
  5101. "evt_tag:x%x, index:x%x\n",
  5102. acqe_fip->event_tag,
  5103. acqe_fip->index);
  5104. if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
  5105. /*
  5106. * During period of FCF discovery, read the FCF
  5107. * table record indexed by the event to update
  5108. * FCF roundrobin failover eligible FCF bmask.
  5109. */
  5110. lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
  5111. LOG_DISCOVERY,
  5112. "2779 Read FCF (x%x) for updating "
  5113. "roundrobin FCF failover bmask\n",
  5114. acqe_fip->index);
  5115. rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
  5116. }
  5117. /* If the FCF discovery is in progress, do nothing. */
  5118. spin_lock_irq(&phba->hbalock);
  5119. if (phba->hba_flag & FCF_TS_INPROG) {
  5120. spin_unlock_irq(&phba->hbalock);
  5121. break;
  5122. }
  5123. /* If fast FCF failover rescan event is pending, do nothing */
  5124. if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
  5125. spin_unlock_irq(&phba->hbalock);
  5126. break;
  5127. }
  5128. /* If the FCF has been in discovered state, do nothing. */
  5129. if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
  5130. spin_unlock_irq(&phba->hbalock);
  5131. break;
  5132. }
  5133. spin_unlock_irq(&phba->hbalock);
  5134. /* Otherwise, scan the entire FCF table and re-discover SAN */
  5135. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  5136. "2770 Start FCF table scan per async FCF "
  5137. "event, evt_tag:x%x, index:x%x\n",
  5138. acqe_fip->event_tag, acqe_fip->index);
  5139. rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
  5140. LPFC_FCOE_FCF_GET_FIRST);
  5141. if (rc)
  5142. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
  5143. "2547 Issue FCF scan read FCF mailbox "
  5144. "command failed (x%x)\n", rc);
  5145. break;
  5146. case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
  5147. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5148. "2548 FCF Table full count 0x%x tag 0x%x\n",
  5149. bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
  5150. acqe_fip->event_tag);
  5151. break;
  5152. case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
  5153. phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
  5154. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
  5155. "2549 FCF (x%x) disconnected from network, "
  5156. "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
  5157. /*
  5158. * If we are in the middle of FCF failover process, clear
  5159. * the corresponding FCF bit in the roundrobin bitmap.
  5160. */
  5161. spin_lock_irq(&phba->hbalock);
  5162. if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
  5163. (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
  5164. spin_unlock_irq(&phba->hbalock);
  5165. /* Update FLOGI FCF failover eligible FCF bmask */
  5166. lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
  5167. break;
  5168. }
  5169. spin_unlock_irq(&phba->hbalock);
  5170. /* If the event is not for currently used fcf do nothing */
  5171. if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
  5172. break;
  5173. /*
  5174. * Otherwise, request the port to rediscover the entire FCF
  5175. * table for a fast recovery from case that the current FCF
  5176. * is no longer valid as we are not in the middle of FCF
  5177. * failover process already.
  5178. */
  5179. spin_lock_irq(&phba->hbalock);
  5180. /* Mark the fast failover process in progress */
  5181. phba->fcf.fcf_flag |= FCF_DEAD_DISC;
  5182. spin_unlock_irq(&phba->hbalock);
  5183. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  5184. "2771 Start FCF fast failover process due to "
  5185. "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
  5186. "\n", acqe_fip->event_tag, acqe_fip->index);
  5187. rc = lpfc_sli4_redisc_fcf_table(phba);
  5188. if (rc) {
  5189. lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
  5190. LOG_DISCOVERY,
  5191. "2772 Issue FCF rediscover mailbox "
  5192. "command failed, fail through to FCF "
  5193. "dead event\n");
  5194. spin_lock_irq(&phba->hbalock);
  5195. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  5196. spin_unlock_irq(&phba->hbalock);
  5197. /*
  5198. * Last resort will fail over by treating this
  5199. * as a link down to FCF registration.
  5200. */
  5201. lpfc_sli4_fcf_dead_failthrough(phba);
  5202. } else {
  5203. /* Reset FCF roundrobin bmask for new discovery */
  5204. lpfc_sli4_clear_fcf_rr_bmask(phba);
  5205. /*
  5206. * Handling fast FCF failover to a DEAD FCF event is
  5207. * considered equalivant to receiving CVL to all vports.
  5208. */
  5209. lpfc_sli4_perform_all_vport_cvl(phba);
  5210. }
  5211. break;
  5212. case LPFC_FIP_EVENT_TYPE_CVL:
  5213. phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
  5214. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
  5215. "2718 Clear Virtual Link Received for VPI 0x%x"
  5216. " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
  5217. vport = lpfc_find_vport_by_vpid(phba,
  5218. acqe_fip->index);
  5219. ndlp = lpfc_sli4_perform_vport_cvl(vport);
  5220. if (!ndlp)
  5221. break;
  5222. active_vlink_present = 0;
  5223. vports = lpfc_create_vport_work_array(phba);
  5224. if (vports) {
  5225. for (i = 0; i <= phba->max_vports && vports[i] != NULL;
  5226. i++) {
  5227. if ((!(vports[i]->fc_flag &
  5228. FC_VPORT_CVL_RCVD)) &&
  5229. (vports[i]->port_state > LPFC_FDISC)) {
  5230. active_vlink_present = 1;
  5231. break;
  5232. }
  5233. }
  5234. lpfc_destroy_vport_work_array(phba, vports);
  5235. }
  5236. /*
  5237. * Don't re-instantiate if vport is marked for deletion.
  5238. * If we are here first then vport_delete is going to wait
  5239. * for discovery to complete.
  5240. */
  5241. if (!(vport->load_flag & FC_UNLOADING) &&
  5242. active_vlink_present) {
  5243. /*
  5244. * If there are other active VLinks present,
  5245. * re-instantiate the Vlink using FDISC.
  5246. */
  5247. mod_timer(&ndlp->nlp_delayfunc,
  5248. jiffies + msecs_to_jiffies(1000));
  5249. shost = lpfc_shost_from_vport(vport);
  5250. spin_lock_irq(shost->host_lock);
  5251. ndlp->nlp_flag |= NLP_DELAY_TMO;
  5252. spin_unlock_irq(shost->host_lock);
  5253. ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
  5254. vport->port_state = LPFC_FDISC;
  5255. } else {
  5256. /*
  5257. * Otherwise, we request port to rediscover
  5258. * the entire FCF table for a fast recovery
  5259. * from possible case that the current FCF
  5260. * is no longer valid if we are not already
  5261. * in the FCF failover process.
  5262. */
  5263. spin_lock_irq(&phba->hbalock);
  5264. if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
  5265. spin_unlock_irq(&phba->hbalock);
  5266. break;
  5267. }
  5268. /* Mark the fast failover process in progress */
  5269. phba->fcf.fcf_flag |= FCF_ACVL_DISC;
  5270. spin_unlock_irq(&phba->hbalock);
  5271. lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
  5272. LOG_DISCOVERY,
  5273. "2773 Start FCF failover per CVL, "
  5274. "evt_tag:x%x\n", acqe_fip->event_tag);
  5275. rc = lpfc_sli4_redisc_fcf_table(phba);
  5276. if (rc) {
  5277. lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
  5278. LOG_DISCOVERY,
  5279. "2774 Issue FCF rediscover "
  5280. "mailbox command failed, "
  5281. "through to CVL event\n");
  5282. spin_lock_irq(&phba->hbalock);
  5283. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  5284. spin_unlock_irq(&phba->hbalock);
  5285. /*
  5286. * Last resort will be re-try on the
  5287. * the current registered FCF entry.
  5288. */
  5289. lpfc_retry_pport_discovery(phba);
  5290. } else
  5291. /*
  5292. * Reset FCF roundrobin bmask for new
  5293. * discovery.
  5294. */
  5295. lpfc_sli4_clear_fcf_rr_bmask(phba);
  5296. }
  5297. break;
  5298. default:
  5299. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5300. "0288 Unknown FCoE event type 0x%x event tag "
  5301. "0x%x\n", event_type, acqe_fip->event_tag);
  5302. break;
  5303. }
  5304. }
  5305. /**
  5306. * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
  5307. * @phba: pointer to lpfc hba data structure.
  5308. * @acqe_link: pointer to the async dcbx completion queue entry.
  5309. *
  5310. * This routine is to handle the SLI4 asynchronous dcbx event.
  5311. **/
  5312. static void
  5313. lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
  5314. struct lpfc_acqe_dcbx *acqe_dcbx)
  5315. {
  5316. phba->fc_eventTag = acqe_dcbx->event_tag;
  5317. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5318. "0290 The SLI4 DCBX asynchronous event is not "
  5319. "handled yet\n");
  5320. }
  5321. /**
  5322. * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
  5323. * @phba: pointer to lpfc hba data structure.
  5324. * @acqe_link: pointer to the async grp5 completion queue entry.
  5325. *
  5326. * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
  5327. * is an asynchronous notified of a logical link speed change. The Port
  5328. * reports the logical link speed in units of 10Mbps.
  5329. **/
  5330. static void
  5331. lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
  5332. struct lpfc_acqe_grp5 *acqe_grp5)
  5333. {
  5334. uint16_t prev_ll_spd;
  5335. phba->fc_eventTag = acqe_grp5->event_tag;
  5336. phba->fcoe_eventtag = acqe_grp5->event_tag;
  5337. prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
  5338. phba->sli4_hba.link_state.logical_speed =
  5339. (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
  5340. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  5341. "2789 GRP5 Async Event: Updating logical link speed "
  5342. "from %dMbps to %dMbps\n", prev_ll_spd,
  5343. phba->sli4_hba.link_state.logical_speed);
  5344. }
  5345. /**
  5346. * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
  5347. * @phba: pointer to lpfc hba data structure.
  5348. *
  5349. * This routine is invoked by the worker thread to process all the pending
  5350. * SLI4 asynchronous events.
  5351. **/
  5352. void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
  5353. {
  5354. struct lpfc_cq_event *cq_event;
  5355. /* First, declare the async event has been handled */
  5356. spin_lock_irq(&phba->hbalock);
  5357. phba->hba_flag &= ~ASYNC_EVENT;
  5358. spin_unlock_irq(&phba->hbalock);
  5359. /* Now, handle all the async events */
  5360. while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
  5361. /* Get the first event from the head of the event queue */
  5362. spin_lock_irq(&phba->hbalock);
  5363. list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
  5364. cq_event, struct lpfc_cq_event, list);
  5365. spin_unlock_irq(&phba->hbalock);
  5366. /* Process the asynchronous event */
  5367. switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
  5368. case LPFC_TRAILER_CODE_LINK:
  5369. lpfc_sli4_async_link_evt(phba,
  5370. &cq_event->cqe.acqe_link);
  5371. break;
  5372. case LPFC_TRAILER_CODE_FCOE:
  5373. lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
  5374. break;
  5375. case LPFC_TRAILER_CODE_DCBX:
  5376. lpfc_sli4_async_dcbx_evt(phba,
  5377. &cq_event->cqe.acqe_dcbx);
  5378. break;
  5379. case LPFC_TRAILER_CODE_GRP5:
  5380. lpfc_sli4_async_grp5_evt(phba,
  5381. &cq_event->cqe.acqe_grp5);
  5382. break;
  5383. case LPFC_TRAILER_CODE_FC:
  5384. lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
  5385. break;
  5386. case LPFC_TRAILER_CODE_SLI:
  5387. lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
  5388. break;
  5389. default:
  5390. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5391. "1804 Invalid asynchronous event code: "
  5392. "x%x\n", bf_get(lpfc_trailer_code,
  5393. &cq_event->cqe.mcqe_cmpl));
  5394. break;
  5395. }
  5396. /* Free the completion event processed to the free pool */
  5397. lpfc_sli4_cq_event_release(phba, cq_event);
  5398. }
  5399. }
  5400. /**
  5401. * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
  5402. * @phba: pointer to lpfc hba data structure.
  5403. *
  5404. * This routine is invoked by the worker thread to process FCF table
  5405. * rediscovery pending completion event.
  5406. **/
  5407. void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
  5408. {
  5409. int rc;
  5410. spin_lock_irq(&phba->hbalock);
  5411. /* Clear FCF rediscovery timeout event */
  5412. phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
  5413. /* Clear driver fast failover FCF record flag */
  5414. phba->fcf.failover_rec.flag = 0;
  5415. /* Set state for FCF fast failover */
  5416. phba->fcf.fcf_flag |= FCF_REDISC_FOV;
  5417. spin_unlock_irq(&phba->hbalock);
  5418. /* Scan FCF table from the first entry to re-discover SAN */
  5419. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  5420. "2777 Start post-quiescent FCF table scan\n");
  5421. rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
  5422. if (rc)
  5423. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
  5424. "2747 Issue FCF scan read FCF mailbox "
  5425. "command failed 0x%x\n", rc);
  5426. }
  5427. /**
  5428. * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
  5429. * @phba: pointer to lpfc hba data structure.
  5430. * @dev_grp: The HBA PCI-Device group number.
  5431. *
  5432. * This routine is invoked to set up the per HBA PCI-Device group function
  5433. * API jump table entries.
  5434. *
  5435. * Return: 0 if success, otherwise -ENODEV
  5436. **/
  5437. int
  5438. lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  5439. {
  5440. int rc;
  5441. /* Set up lpfc PCI-device group */
  5442. phba->pci_dev_grp = dev_grp;
  5443. /* The LPFC_PCI_DEV_OC uses SLI4 */
  5444. if (dev_grp == LPFC_PCI_DEV_OC)
  5445. phba->sli_rev = LPFC_SLI_REV4;
  5446. /* Set up device INIT API function jump table */
  5447. rc = lpfc_init_api_table_setup(phba, dev_grp);
  5448. if (rc)
  5449. return -ENODEV;
  5450. /* Set up SCSI API function jump table */
  5451. rc = lpfc_scsi_api_table_setup(phba, dev_grp);
  5452. if (rc)
  5453. return -ENODEV;
  5454. /* Set up SLI API function jump table */
  5455. rc = lpfc_sli_api_table_setup(phba, dev_grp);
  5456. if (rc)
  5457. return -ENODEV;
  5458. /* Set up MBOX API function jump table */
  5459. rc = lpfc_mbox_api_table_setup(phba, dev_grp);
  5460. if (rc)
  5461. return -ENODEV;
  5462. return 0;
  5463. }
  5464. /**
  5465. * lpfc_log_intr_mode - Log the active interrupt mode
  5466. * @phba: pointer to lpfc hba data structure.
  5467. * @intr_mode: active interrupt mode adopted.
  5468. *
  5469. * This routine it invoked to log the currently used active interrupt mode
  5470. * to the device.
  5471. **/
  5472. static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
  5473. {
  5474. switch (intr_mode) {
  5475. case 0:
  5476. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5477. "0470 Enable INTx interrupt mode.\n");
  5478. break;
  5479. case 1:
  5480. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5481. "0481 Enabled MSI interrupt mode.\n");
  5482. break;
  5483. case 2:
  5484. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5485. "0480 Enabled MSI-X interrupt mode.\n");
  5486. break;
  5487. default:
  5488. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5489. "0482 Illegal interrupt mode.\n");
  5490. break;
  5491. }
  5492. return;
  5493. }
  5494. /**
  5495. * lpfc_cpumask_of_node_init - initalizes cpumask of phba's NUMA node
  5496. * @phba: Pointer to HBA context object.
  5497. *
  5498. **/
  5499. static void
  5500. lpfc_cpumask_of_node_init(struct lpfc_hba *phba)
  5501. {
  5502. unsigned int cpu, numa_node;
  5503. struct cpumask *numa_mask = &phba->sli4_hba.numa_mask;
  5504. cpumask_clear(numa_mask);
  5505. /* Check if we're a NUMA architecture */
  5506. numa_node = dev_to_node(&phba->pcidev->dev);
  5507. if (numa_node == NUMA_NO_NODE)
  5508. return;
  5509. for_each_possible_cpu(cpu)
  5510. if (cpu_to_node(cpu) == numa_node)
  5511. cpumask_set_cpu(cpu, numa_mask);
  5512. }
  5513. /**
  5514. * lpfc_enable_pci_dev - Enable a generic PCI device.
  5515. * @phba: pointer to lpfc hba data structure.
  5516. *
  5517. * This routine is invoked to enable the PCI device that is common to all
  5518. * PCI devices.
  5519. *
  5520. * Return codes
  5521. * 0 - successful
  5522. * other values - error
  5523. **/
  5524. static int
  5525. lpfc_enable_pci_dev(struct lpfc_hba *phba)
  5526. {
  5527. struct pci_dev *pdev;
  5528. /* Obtain PCI device reference */
  5529. if (!phba->pcidev)
  5530. goto out_error;
  5531. else
  5532. pdev = phba->pcidev;
  5533. /* Enable PCI device */
  5534. if (pci_enable_device_mem(pdev))
  5535. goto out_error;
  5536. /* Request PCI resource for the device */
  5537. if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
  5538. goto out_disable_device;
  5539. /* Set up device as PCI master and save state for EEH */
  5540. pci_set_master(pdev);
  5541. pci_try_set_mwi(pdev);
  5542. pci_save_state(pdev);
  5543. /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
  5544. if (pci_is_pcie(pdev))
  5545. pdev->needs_freset = 1;
  5546. return 0;
  5547. out_disable_device:
  5548. pci_disable_device(pdev);
  5549. out_error:
  5550. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5551. "1401 Failed to enable pci device\n");
  5552. return -ENODEV;
  5553. }
  5554. /**
  5555. * lpfc_disable_pci_dev - Disable a generic PCI device.
  5556. * @phba: pointer to lpfc hba data structure.
  5557. *
  5558. * This routine is invoked to disable the PCI device that is common to all
  5559. * PCI devices.
  5560. **/
  5561. static void
  5562. lpfc_disable_pci_dev(struct lpfc_hba *phba)
  5563. {
  5564. struct pci_dev *pdev;
  5565. /* Obtain PCI device reference */
  5566. if (!phba->pcidev)
  5567. return;
  5568. else
  5569. pdev = phba->pcidev;
  5570. /* Release PCI resource and disable PCI device */
  5571. pci_release_mem_regions(pdev);
  5572. pci_disable_device(pdev);
  5573. return;
  5574. }
  5575. /**
  5576. * lpfc_reset_hba - Reset a hba
  5577. * @phba: pointer to lpfc hba data structure.
  5578. *
  5579. * This routine is invoked to reset a hba device. It brings the HBA
  5580. * offline, performs a board restart, and then brings the board back
  5581. * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
  5582. * on outstanding mailbox commands.
  5583. **/
  5584. void
  5585. lpfc_reset_hba(struct lpfc_hba *phba)
  5586. {
  5587. /* If resets are disabled then set error state and return. */
  5588. if (!phba->cfg_enable_hba_reset) {
  5589. phba->link_state = LPFC_HBA_ERROR;
  5590. return;
  5591. }
  5592. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  5593. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  5594. else
  5595. lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
  5596. lpfc_offline(phba);
  5597. lpfc_sli_brdrestart(phba);
  5598. lpfc_online(phba);
  5599. lpfc_unblock_mgmt_io(phba);
  5600. }
  5601. /**
  5602. * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
  5603. * @phba: pointer to lpfc hba data structure.
  5604. *
  5605. * This function enables the PCI SR-IOV virtual functions to a physical
  5606. * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
  5607. * enable the number of virtual functions to the physical function. As
  5608. * not all devices support SR-IOV, the return code from the pci_enable_sriov()
  5609. * API call does not considered as an error condition for most of the device.
  5610. **/
  5611. uint16_t
  5612. lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
  5613. {
  5614. struct pci_dev *pdev = phba->pcidev;
  5615. uint16_t nr_virtfn;
  5616. int pos;
  5617. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
  5618. if (pos == 0)
  5619. return 0;
  5620. pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
  5621. return nr_virtfn;
  5622. }
  5623. /**
  5624. * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
  5625. * @phba: pointer to lpfc hba data structure.
  5626. * @nr_vfn: number of virtual functions to be enabled.
  5627. *
  5628. * This function enables the PCI SR-IOV virtual functions to a physical
  5629. * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
  5630. * enable the number of virtual functions to the physical function. As
  5631. * not all devices support SR-IOV, the return code from the pci_enable_sriov()
  5632. * API call does not considered as an error condition for most of the device.
  5633. **/
  5634. int
  5635. lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
  5636. {
  5637. struct pci_dev *pdev = phba->pcidev;
  5638. uint16_t max_nr_vfn;
  5639. int rc;
  5640. max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
  5641. if (nr_vfn > max_nr_vfn) {
  5642. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5643. "3057 Requested vfs (%d) greater than "
  5644. "supported vfs (%d)", nr_vfn, max_nr_vfn);
  5645. return -EINVAL;
  5646. }
  5647. rc = pci_enable_sriov(pdev, nr_vfn);
  5648. if (rc) {
  5649. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  5650. "2806 Failed to enable sriov on this device "
  5651. "with vfn number nr_vf:%d, rc:%d\n",
  5652. nr_vfn, rc);
  5653. } else
  5654. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  5655. "2807 Successful enable sriov on this device "
  5656. "with vfn number nr_vf:%d\n", nr_vfn);
  5657. return rc;
  5658. }
  5659. /**
  5660. * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
  5661. * @phba: pointer to lpfc hba data structure.
  5662. *
  5663. * This routine is invoked to set up the driver internal resources before the
  5664. * device specific resource setup to support the HBA device it attached to.
  5665. *
  5666. * Return codes
  5667. * 0 - successful
  5668. * other values - error
  5669. **/
  5670. static int
  5671. lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
  5672. {
  5673. struct lpfc_sli *psli = &phba->sli;
  5674. /*
  5675. * Driver resources common to all SLI revisions
  5676. */
  5677. atomic_set(&phba->fast_event_count, 0);
  5678. spin_lock_init(&phba->hbalock);
  5679. /* Initialize ndlp management spinlock */
  5680. spin_lock_init(&phba->ndlp_lock);
  5681. /* Initialize port_list spinlock */
  5682. spin_lock_init(&phba->port_list_lock);
  5683. INIT_LIST_HEAD(&phba->port_list);
  5684. INIT_LIST_HEAD(&phba->work_list);
  5685. init_waitqueue_head(&phba->wait_4_mlo_m_q);
  5686. /* Initialize the wait queue head for the kernel thread */
  5687. init_waitqueue_head(&phba->work_waitq);
  5688. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5689. "1403 Protocols supported %s %s %s\n",
  5690. ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
  5691. "SCSI" : " "),
  5692. ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
  5693. "NVME" : " "),
  5694. (phba->nvmet_support ? "NVMET" : " "));
  5695. /* Initialize the IO buffer list used by driver for SLI3 SCSI */
  5696. spin_lock_init(&phba->scsi_buf_list_get_lock);
  5697. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
  5698. spin_lock_init(&phba->scsi_buf_list_put_lock);
  5699. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
  5700. /* Initialize the fabric iocb list */
  5701. INIT_LIST_HEAD(&phba->fabric_iocb_list);
  5702. /* Initialize list to save ELS buffers */
  5703. INIT_LIST_HEAD(&phba->elsbuf);
  5704. /* Initialize FCF connection rec list */
  5705. INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
  5706. /* Initialize OAS configuration list */
  5707. spin_lock_init(&phba->devicelock);
  5708. INIT_LIST_HEAD(&phba->luns);
  5709. /* MBOX heartbeat timer */
  5710. timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
  5711. /* Fabric block timer */
  5712. timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
  5713. /* EA polling mode timer */
  5714. timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
  5715. /* Heartbeat timer */
  5716. timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
  5717. INIT_DELAYED_WORK(&phba->eq_delay_work, lpfc_hb_eq_delay_work);
  5718. return 0;
  5719. }
  5720. /**
  5721. * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
  5722. * @phba: pointer to lpfc hba data structure.
  5723. *
  5724. * This routine is invoked to set up the driver internal resources specific to
  5725. * support the SLI-3 HBA device it attached to.
  5726. *
  5727. * Return codes
  5728. * 0 - successful
  5729. * other values - error
  5730. **/
  5731. static int
  5732. lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
  5733. {
  5734. int rc, entry_sz;
  5735. /*
  5736. * Initialize timers used by driver
  5737. */
  5738. /* FCP polling mode timer */
  5739. timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
  5740. /* Host attention work mask setup */
  5741. phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
  5742. phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
  5743. /* Get all the module params for configuring this host */
  5744. lpfc_get_cfgparam(phba);
  5745. /* Set up phase-1 common device driver resources */
  5746. rc = lpfc_setup_driver_resource_phase1(phba);
  5747. if (rc)
  5748. return -ENODEV;
  5749. if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
  5750. phba->menlo_flag |= HBA_MENLO_SUPPORT;
  5751. /* check for menlo minimum sg count */
  5752. if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
  5753. phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
  5754. }
  5755. if (!phba->sli.sli3_ring)
  5756. phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
  5757. sizeof(struct lpfc_sli_ring),
  5758. GFP_KERNEL);
  5759. if (!phba->sli.sli3_ring)
  5760. return -ENOMEM;
  5761. /*
  5762. * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
  5763. * used to create the sg_dma_buf_pool must be dynamically calculated.
  5764. */
  5765. if (phba->sli_rev == LPFC_SLI_REV4)
  5766. entry_sz = sizeof(struct sli4_sge);
  5767. else
  5768. entry_sz = sizeof(struct ulp_bde64);
  5769. /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
  5770. if (phba->cfg_enable_bg) {
  5771. /*
  5772. * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
  5773. * the FCP rsp, and a BDE for each. Sice we have no control
  5774. * over how many protection data segments the SCSI Layer
  5775. * will hand us (ie: there could be one for every block
  5776. * in the IO), we just allocate enough BDEs to accomidate
  5777. * our max amount and we need to limit lpfc_sg_seg_cnt to
  5778. * minimize the risk of running out.
  5779. */
  5780. phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
  5781. sizeof(struct fcp_rsp) +
  5782. (LPFC_MAX_SG_SEG_CNT * entry_sz);
  5783. if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
  5784. phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
  5785. /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
  5786. phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
  5787. } else {
  5788. /*
  5789. * The scsi_buf for a regular I/O will hold the FCP cmnd,
  5790. * the FCP rsp, a BDE for each, and a BDE for up to
  5791. * cfg_sg_seg_cnt data segments.
  5792. */
  5793. phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
  5794. sizeof(struct fcp_rsp) +
  5795. ((phba->cfg_sg_seg_cnt + 2) * entry_sz);
  5796. /* Total BDEs in BPL for scsi_sg_list */
  5797. phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
  5798. }
  5799. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
  5800. "9088 INIT sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
  5801. phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
  5802. phba->cfg_total_seg_cnt);
  5803. phba->max_vpi = LPFC_MAX_VPI;
  5804. /* This will be set to correct value after config_port mbox */
  5805. phba->max_vports = 0;
  5806. /*
  5807. * Initialize the SLI Layer to run with lpfc HBAs.
  5808. */
  5809. lpfc_sli_setup(phba);
  5810. lpfc_sli_queue_init(phba);
  5811. /* Allocate device driver memory */
  5812. if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
  5813. return -ENOMEM;
  5814. phba->lpfc_sg_dma_buf_pool =
  5815. dma_pool_create("lpfc_sg_dma_buf_pool",
  5816. &phba->pcidev->dev, phba->cfg_sg_dma_buf_size,
  5817. BPL_ALIGN_SZ, 0);
  5818. if (!phba->lpfc_sg_dma_buf_pool)
  5819. goto fail_free_mem;
  5820. phba->lpfc_cmd_rsp_buf_pool =
  5821. dma_pool_create("lpfc_cmd_rsp_buf_pool",
  5822. &phba->pcidev->dev,
  5823. sizeof(struct fcp_cmnd) +
  5824. sizeof(struct fcp_rsp),
  5825. BPL_ALIGN_SZ, 0);
  5826. if (!phba->lpfc_cmd_rsp_buf_pool)
  5827. goto fail_free_dma_buf_pool;
  5828. /*
  5829. * Enable sr-iov virtual functions if supported and configured
  5830. * through the module parameter.
  5831. */
  5832. if (phba->cfg_sriov_nr_virtfn > 0) {
  5833. rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
  5834. phba->cfg_sriov_nr_virtfn);
  5835. if (rc) {
  5836. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  5837. "2808 Requested number of SR-IOV "
  5838. "virtual functions (%d) is not "
  5839. "supported\n",
  5840. phba->cfg_sriov_nr_virtfn);
  5841. phba->cfg_sriov_nr_virtfn = 0;
  5842. }
  5843. }
  5844. return 0;
  5845. fail_free_dma_buf_pool:
  5846. dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
  5847. phba->lpfc_sg_dma_buf_pool = NULL;
  5848. fail_free_mem:
  5849. lpfc_mem_free(phba);
  5850. return -ENOMEM;
  5851. }
  5852. /**
  5853. * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
  5854. * @phba: pointer to lpfc hba data structure.
  5855. *
  5856. * This routine is invoked to unset the driver internal resources set up
  5857. * specific for supporting the SLI-3 HBA device it attached to.
  5858. **/
  5859. static void
  5860. lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
  5861. {
  5862. /* Free device driver memory allocated */
  5863. lpfc_mem_free_all(phba);
  5864. return;
  5865. }
  5866. /**
  5867. * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
  5868. * @phba: pointer to lpfc hba data structure.
  5869. *
  5870. * This routine is invoked to set up the driver internal resources specific to
  5871. * support the SLI-4 HBA device it attached to.
  5872. *
  5873. * Return codes
  5874. * 0 - successful
  5875. * other values - error
  5876. **/
  5877. static int
  5878. lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
  5879. {
  5880. LPFC_MBOXQ_t *mboxq;
  5881. MAILBOX_t *mb;
  5882. int rc, i, max_buf_size;
  5883. uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
  5884. struct lpfc_mqe *mqe;
  5885. int longs;
  5886. int extra;
  5887. uint64_t wwn;
  5888. u32 if_type;
  5889. u32 if_fam;
  5890. phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
  5891. phba->sli4_hba.num_possible_cpu = cpumask_last(cpu_possible_mask) + 1;
  5892. phba->sli4_hba.curr_disp_cpu = 0;
  5893. lpfc_cpumask_of_node_init(phba);
  5894. /* Get all the module params for configuring this host */
  5895. lpfc_get_cfgparam(phba);
  5896. /* Set up phase-1 common device driver resources */
  5897. rc = lpfc_setup_driver_resource_phase1(phba);
  5898. if (rc)
  5899. return -ENODEV;
  5900. /* Before proceed, wait for POST done and device ready */
  5901. rc = lpfc_sli4_post_status_check(phba);
  5902. if (rc)
  5903. return -ENODEV;
  5904. /* Allocate all driver workqueues here */
  5905. /* The lpfc_wq workqueue for deferred irq use */
  5906. phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
  5907. /*
  5908. * Initialize timers used by driver
  5909. */
  5910. timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
  5911. /* FCF rediscover timer */
  5912. timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
  5913. /*
  5914. * Control structure for handling external multi-buffer mailbox
  5915. * command pass-through.
  5916. */
  5917. memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
  5918. sizeof(struct lpfc_mbox_ext_buf_ctx));
  5919. INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  5920. phba->max_vpi = LPFC_MAX_VPI;
  5921. /* This will be set to correct value after the read_config mbox */
  5922. phba->max_vports = 0;
  5923. /* Program the default value of vlan_id and fc_map */
  5924. phba->valid_vlan = 0;
  5925. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  5926. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  5927. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  5928. /*
  5929. * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
  5930. * we will associate a new ring, for each EQ/CQ/WQ tuple.
  5931. * The WQ create will allocate the ring.
  5932. */
  5933. /* Initialize buffer queue management fields */
  5934. INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
  5935. phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
  5936. phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
  5937. /*
  5938. * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
  5939. */
  5940. /* Initialize the Abort buffer list used by driver */
  5941. spin_lock_init(&phba->sli4_hba.abts_io_buf_list_lock);
  5942. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_io_buf_list);
  5943. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  5944. /* Initialize the Abort nvme buffer list used by driver */
  5945. spin_lock_init(&phba->sli4_hba.abts_nvmet_buf_list_lock);
  5946. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
  5947. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
  5948. spin_lock_init(&phba->sli4_hba.t_active_list_lock);
  5949. INIT_LIST_HEAD(&phba->sli4_hba.t_active_ctx_list);
  5950. }
  5951. /* This abort list used by worker thread */
  5952. spin_lock_init(&phba->sli4_hba.sgl_list_lock);
  5953. spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
  5954. /*
  5955. * Initialize driver internal slow-path work queues
  5956. */
  5957. /* Driver internel slow-path CQ Event pool */
  5958. INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
  5959. /* Response IOCB work queue list */
  5960. INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
  5961. /* Asynchronous event CQ Event work queue list */
  5962. INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
  5963. /* Fast-path XRI aborted CQ Event work queue list */
  5964. INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  5965. /* Slow-path XRI aborted CQ Event work queue list */
  5966. INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
  5967. /* Receive queue CQ Event work queue list */
  5968. INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
  5969. /* Initialize extent block lists. */
  5970. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
  5971. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
  5972. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
  5973. INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
  5974. /* Initialize mboxq lists. If the early init routines fail
  5975. * these lists need to be correctly initialized.
  5976. */
  5977. INIT_LIST_HEAD(&phba->sli.mboxq);
  5978. INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
  5979. /* initialize optic_state to 0xFF */
  5980. phba->sli4_hba.lnk_info.optic_state = 0xff;
  5981. /* Allocate device driver memory */
  5982. rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
  5983. if (rc)
  5984. return -ENOMEM;
  5985. /* IF Type 2 ports get initialized now. */
  5986. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
  5987. LPFC_SLI_INTF_IF_TYPE_2) {
  5988. rc = lpfc_pci_function_reset(phba);
  5989. if (unlikely(rc)) {
  5990. rc = -ENODEV;
  5991. goto out_free_mem;
  5992. }
  5993. phba->temp_sensor_support = 1;
  5994. }
  5995. /* Create the bootstrap mailbox command */
  5996. rc = lpfc_create_bootstrap_mbox(phba);
  5997. if (unlikely(rc))
  5998. goto out_free_mem;
  5999. /* Set up the host's endian order with the device. */
  6000. rc = lpfc_setup_endian_order(phba);
  6001. if (unlikely(rc))
  6002. goto out_free_bsmbx;
  6003. /* Set up the hba's configuration parameters. */
  6004. rc = lpfc_sli4_read_config(phba);
  6005. if (unlikely(rc))
  6006. goto out_free_bsmbx;
  6007. rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
  6008. if (unlikely(rc))
  6009. goto out_free_bsmbx;
  6010. /* IF Type 0 ports get initialized now. */
  6011. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  6012. LPFC_SLI_INTF_IF_TYPE_0) {
  6013. rc = lpfc_pci_function_reset(phba);
  6014. if (unlikely(rc))
  6015. goto out_free_bsmbx;
  6016. }
  6017. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
  6018. GFP_KERNEL);
  6019. if (!mboxq) {
  6020. rc = -ENOMEM;
  6021. goto out_free_bsmbx;
  6022. }
  6023. /* Check for NVMET being configured */
  6024. phba->nvmet_support = 0;
  6025. if (lpfc_enable_nvmet_cnt) {
  6026. /* First get WWN of HBA instance */
  6027. lpfc_read_nv(phba, mboxq);
  6028. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6029. if (rc != MBX_SUCCESS) {
  6030. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  6031. "6016 Mailbox failed , mbxCmd x%x "
  6032. "READ_NV, mbxStatus x%x\n",
  6033. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6034. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  6035. mempool_free(mboxq, phba->mbox_mem_pool);
  6036. rc = -EIO;
  6037. goto out_free_bsmbx;
  6038. }
  6039. mb = &mboxq->u.mb;
  6040. memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
  6041. sizeof(uint64_t));
  6042. wwn = cpu_to_be64(wwn);
  6043. phba->sli4_hba.wwnn.u.name = wwn;
  6044. memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
  6045. sizeof(uint64_t));
  6046. /* wwn is WWPN of HBA instance */
  6047. wwn = cpu_to_be64(wwn);
  6048. phba->sli4_hba.wwpn.u.name = wwn;
  6049. /* Check to see if it matches any module parameter */
  6050. for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
  6051. if (wwn == lpfc_enable_nvmet[i]) {
  6052. #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
  6053. if (lpfc_nvmet_mem_alloc(phba))
  6054. break;
  6055. phba->nvmet_support = 1; /* a match */
  6056. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6057. "6017 NVME Target %016llx\n",
  6058. wwn);
  6059. #else
  6060. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6061. "6021 Can't enable NVME Target."
  6062. " NVME_TARGET_FC infrastructure"
  6063. " is not in kernel\n");
  6064. #endif
  6065. /* Not supported for NVMET */
  6066. phba->cfg_xri_rebalancing = 0;
  6067. break;
  6068. }
  6069. }
  6070. }
  6071. lpfc_nvme_mod_param_dep(phba);
  6072. /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
  6073. lpfc_supported_pages(mboxq);
  6074. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6075. if (!rc) {
  6076. mqe = &mboxq->u.mqe;
  6077. memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
  6078. LPFC_MAX_SUPPORTED_PAGES);
  6079. for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
  6080. switch (pn_page[i]) {
  6081. case LPFC_SLI4_PARAMETERS:
  6082. phba->sli4_hba.pc_sli4_params.supported = 1;
  6083. break;
  6084. default:
  6085. break;
  6086. }
  6087. }
  6088. /* Read the port's SLI4 Parameters capabilities if supported. */
  6089. if (phba->sli4_hba.pc_sli4_params.supported)
  6090. rc = lpfc_pc_sli4_params_get(phba, mboxq);
  6091. if (rc) {
  6092. mempool_free(mboxq, phba->mbox_mem_pool);
  6093. rc = -EIO;
  6094. goto out_free_bsmbx;
  6095. }
  6096. }
  6097. /*
  6098. * Get sli4 parameters that override parameters from Port capabilities.
  6099. * If this call fails, it isn't critical unless the SLI4 parameters come
  6100. * back in conflict.
  6101. */
  6102. rc = lpfc_get_sli4_parameters(phba, mboxq);
  6103. if (rc) {
  6104. if_type = bf_get(lpfc_sli_intf_if_type,
  6105. &phba->sli4_hba.sli_intf);
  6106. if_fam = bf_get(lpfc_sli_intf_sli_family,
  6107. &phba->sli4_hba.sli_intf);
  6108. if (phba->sli4_hba.extents_in_use &&
  6109. phba->sli4_hba.rpi_hdrs_in_use) {
  6110. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6111. "2999 Unsupported SLI4 Parameters "
  6112. "Extents and RPI headers enabled.\n");
  6113. if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
  6114. if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
  6115. mempool_free(mboxq, phba->mbox_mem_pool);
  6116. rc = -EIO;
  6117. goto out_free_bsmbx;
  6118. }
  6119. }
  6120. if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
  6121. if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
  6122. mempool_free(mboxq, phba->mbox_mem_pool);
  6123. rc = -EIO;
  6124. goto out_free_bsmbx;
  6125. }
  6126. }
  6127. /*
  6128. * 1 for cmd, 1 for rsp, NVME adds an extra one
  6129. * for boundary conditions in its max_sgl_segment template.
  6130. */
  6131. extra = 2;
  6132. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  6133. extra++;
  6134. /*
  6135. * It doesn't matter what family our adapter is in, we are
  6136. * limited to 2 Pages, 512 SGEs, for our SGL.
  6137. * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
  6138. */
  6139. max_buf_size = (2 * SLI4_PAGE_SIZE);
  6140. /*
  6141. * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
  6142. * used to create the sg_dma_buf_pool must be calculated.
  6143. */
  6144. if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
  6145. /* Both cfg_enable_bg and cfg_external_dif code paths */
  6146. /*
  6147. * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
  6148. * the FCP rsp, and a SGE. Sice we have no control
  6149. * over how many protection segments the SCSI Layer
  6150. * will hand us (ie: there could be one for every block
  6151. * in the IO), just allocate enough SGEs to accomidate
  6152. * our max amount and we need to limit lpfc_sg_seg_cnt
  6153. * to minimize the risk of running out.
  6154. */
  6155. phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
  6156. sizeof(struct fcp_rsp) + max_buf_size;
  6157. /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
  6158. phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
  6159. /*
  6160. * If supporting DIF, reduce the seg count for scsi to
  6161. * allow room for the DIF sges.
  6162. */
  6163. if (phba->cfg_enable_bg &&
  6164. phba->cfg_sg_seg_cnt > LPFC_MAX_BG_SLI4_SEG_CNT_DIF)
  6165. phba->cfg_scsi_seg_cnt = LPFC_MAX_BG_SLI4_SEG_CNT_DIF;
  6166. else
  6167. phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
  6168. } else {
  6169. /*
  6170. * The scsi_buf for a regular I/O holds the FCP cmnd,
  6171. * the FCP rsp, a SGE for each, and a SGE for up to
  6172. * cfg_sg_seg_cnt data segments.
  6173. */
  6174. phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
  6175. sizeof(struct fcp_rsp) +
  6176. ((phba->cfg_sg_seg_cnt + extra) *
  6177. sizeof(struct sli4_sge));
  6178. /* Total SGEs for scsi_sg_list */
  6179. phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
  6180. phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
  6181. /*
  6182. * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
  6183. * need to post 1 page for the SGL.
  6184. */
  6185. }
  6186. if (phba->cfg_xpsgl && !phba->nvmet_support)
  6187. phba->cfg_sg_dma_buf_size = LPFC_DEFAULT_XPSGL_SIZE;
  6188. else if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
  6189. phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
  6190. else
  6191. phba->cfg_sg_dma_buf_size =
  6192. SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
  6193. phba->border_sge_num = phba->cfg_sg_dma_buf_size /
  6194. sizeof(struct sli4_sge);
  6195. /* Limit to LPFC_MAX_NVME_SEG_CNT for NVME. */
  6196. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  6197. if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
  6198. lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
  6199. "6300 Reducing NVME sg segment "
  6200. "cnt to %d\n",
  6201. LPFC_MAX_NVME_SEG_CNT);
  6202. phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
  6203. } else
  6204. phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
  6205. }
  6206. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
  6207. "9087 sg_seg_cnt:%d dmabuf_size:%d "
  6208. "total:%d scsi:%d nvme:%d\n",
  6209. phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
  6210. phba->cfg_total_seg_cnt, phba->cfg_scsi_seg_cnt,
  6211. phba->cfg_nvme_seg_cnt);
  6212. if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
  6213. i = phba->cfg_sg_dma_buf_size;
  6214. else
  6215. i = SLI4_PAGE_SIZE;
  6216. phba->lpfc_sg_dma_buf_pool =
  6217. dma_pool_create("lpfc_sg_dma_buf_pool",
  6218. &phba->pcidev->dev,
  6219. phba->cfg_sg_dma_buf_size,
  6220. i, 0);
  6221. if (!phba->lpfc_sg_dma_buf_pool)
  6222. goto out_free_bsmbx;
  6223. phba->lpfc_cmd_rsp_buf_pool =
  6224. dma_pool_create("lpfc_cmd_rsp_buf_pool",
  6225. &phba->pcidev->dev,
  6226. sizeof(struct fcp_cmnd) +
  6227. sizeof(struct fcp_rsp),
  6228. i, 0);
  6229. if (!phba->lpfc_cmd_rsp_buf_pool)
  6230. goto out_free_sg_dma_buf;
  6231. mempool_free(mboxq, phba->mbox_mem_pool);
  6232. /* Verify OAS is supported */
  6233. lpfc_sli4_oas_verify(phba);
  6234. /* Verify RAS support on adapter */
  6235. lpfc_sli4_ras_init(phba);
  6236. /* Verify all the SLI4 queues */
  6237. rc = lpfc_sli4_queue_verify(phba);
  6238. if (rc)
  6239. goto out_free_cmd_rsp_buf;
  6240. /* Create driver internal CQE event pool */
  6241. rc = lpfc_sli4_cq_event_pool_create(phba);
  6242. if (rc)
  6243. goto out_free_cmd_rsp_buf;
  6244. /* Initialize sgl lists per host */
  6245. lpfc_init_sgl_list(phba);
  6246. /* Allocate and initialize active sgl array */
  6247. rc = lpfc_init_active_sgl_array(phba);
  6248. if (rc) {
  6249. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6250. "1430 Failed to initialize sgl list.\n");
  6251. goto out_destroy_cq_event_pool;
  6252. }
  6253. rc = lpfc_sli4_init_rpi_hdrs(phba);
  6254. if (rc) {
  6255. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6256. "1432 Failed to initialize rpi headers.\n");
  6257. goto out_free_active_sgl;
  6258. }
  6259. /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
  6260. longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
  6261. phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
  6262. GFP_KERNEL);
  6263. if (!phba->fcf.fcf_rr_bmask) {
  6264. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6265. "2759 Failed allocate memory for FCF round "
  6266. "robin failover bmask\n");
  6267. rc = -ENOMEM;
  6268. goto out_remove_rpi_hdrs;
  6269. }
  6270. phba->sli4_hba.hba_eq_hdl = kcalloc(phba->cfg_irq_chann,
  6271. sizeof(struct lpfc_hba_eq_hdl),
  6272. GFP_KERNEL);
  6273. if (!phba->sli4_hba.hba_eq_hdl) {
  6274. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6275. "2572 Failed allocate memory for "
  6276. "fast-path per-EQ handle array\n");
  6277. rc = -ENOMEM;
  6278. goto out_free_fcf_rr_bmask;
  6279. }
  6280. phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_possible_cpu,
  6281. sizeof(struct lpfc_vector_map_info),
  6282. GFP_KERNEL);
  6283. if (!phba->sli4_hba.cpu_map) {
  6284. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6285. "3327 Failed allocate memory for msi-x "
  6286. "interrupt vector mapping\n");
  6287. rc = -ENOMEM;
  6288. goto out_free_hba_eq_hdl;
  6289. }
  6290. phba->sli4_hba.eq_info = alloc_percpu(struct lpfc_eq_intr_info);
  6291. if (!phba->sli4_hba.eq_info) {
  6292. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6293. "3321 Failed allocation for per_cpu stats\n");
  6294. rc = -ENOMEM;
  6295. goto out_free_hba_cpu_map;
  6296. }
  6297. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  6298. phba->sli4_hba.c_stat = alloc_percpu(struct lpfc_hdwq_stat);
  6299. if (!phba->sli4_hba.c_stat) {
  6300. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6301. "3332 Failed allocating per cpu hdwq stats\n");
  6302. rc = -ENOMEM;
  6303. goto out_free_hba_eq_info;
  6304. }
  6305. #endif
  6306. /*
  6307. * Enable sr-iov virtual functions if supported and configured
  6308. * through the module parameter.
  6309. */
  6310. if (phba->cfg_sriov_nr_virtfn > 0) {
  6311. rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
  6312. phba->cfg_sriov_nr_virtfn);
  6313. if (rc) {
  6314. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  6315. "3020 Requested number of SR-IOV "
  6316. "virtual functions (%d) is not "
  6317. "supported\n",
  6318. phba->cfg_sriov_nr_virtfn);
  6319. phba->cfg_sriov_nr_virtfn = 0;
  6320. }
  6321. }
  6322. return 0;
  6323. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  6324. out_free_hba_eq_info:
  6325. free_percpu(phba->sli4_hba.eq_info);
  6326. #endif
  6327. out_free_hba_cpu_map:
  6328. kfree(phba->sli4_hba.cpu_map);
  6329. out_free_hba_eq_hdl:
  6330. kfree(phba->sli4_hba.hba_eq_hdl);
  6331. out_free_fcf_rr_bmask:
  6332. kfree(phba->fcf.fcf_rr_bmask);
  6333. out_remove_rpi_hdrs:
  6334. lpfc_sli4_remove_rpi_hdrs(phba);
  6335. out_free_active_sgl:
  6336. lpfc_free_active_sgl(phba);
  6337. out_destroy_cq_event_pool:
  6338. lpfc_sli4_cq_event_pool_destroy(phba);
  6339. out_free_cmd_rsp_buf:
  6340. dma_pool_destroy(phba->lpfc_cmd_rsp_buf_pool);
  6341. phba->lpfc_cmd_rsp_buf_pool = NULL;
  6342. out_free_sg_dma_buf:
  6343. dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
  6344. phba->lpfc_sg_dma_buf_pool = NULL;
  6345. out_free_bsmbx:
  6346. lpfc_destroy_bootstrap_mbox(phba);
  6347. out_free_mem:
  6348. lpfc_mem_free(phba);
  6349. return rc;
  6350. }
  6351. /**
  6352. * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
  6353. * @phba: pointer to lpfc hba data structure.
  6354. *
  6355. * This routine is invoked to unset the driver internal resources set up
  6356. * specific for supporting the SLI-4 HBA device it attached to.
  6357. **/
  6358. static void
  6359. lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
  6360. {
  6361. struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
  6362. free_percpu(phba->sli4_hba.eq_info);
  6363. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  6364. free_percpu(phba->sli4_hba.c_stat);
  6365. #endif
  6366. /* Free memory allocated for msi-x interrupt vector to CPU mapping */
  6367. kfree(phba->sli4_hba.cpu_map);
  6368. phba->sli4_hba.num_possible_cpu = 0;
  6369. phba->sli4_hba.num_present_cpu = 0;
  6370. phba->sli4_hba.curr_disp_cpu = 0;
  6371. cpumask_clear(&phba->sli4_hba.numa_mask);
  6372. /* Free memory allocated for fast-path work queue handles */
  6373. kfree(phba->sli4_hba.hba_eq_hdl);
  6374. /* Free the allocated rpi headers. */
  6375. lpfc_sli4_remove_rpi_hdrs(phba);
  6376. lpfc_sli4_remove_rpis(phba);
  6377. /* Free eligible FCF index bmask */
  6378. kfree(phba->fcf.fcf_rr_bmask);
  6379. /* Free the ELS sgl list */
  6380. lpfc_free_active_sgl(phba);
  6381. lpfc_free_els_sgl_list(phba);
  6382. lpfc_free_nvmet_sgl_list(phba);
  6383. /* Free the completion queue EQ event pool */
  6384. lpfc_sli4_cq_event_release_all(phba);
  6385. lpfc_sli4_cq_event_pool_destroy(phba);
  6386. /* Release resource identifiers. */
  6387. lpfc_sli4_dealloc_resource_identifiers(phba);
  6388. /* Free the bsmbx region. */
  6389. lpfc_destroy_bootstrap_mbox(phba);
  6390. /* Free the SLI Layer memory with SLI4 HBAs */
  6391. lpfc_mem_free_all(phba);
  6392. /* Free the current connect table */
  6393. list_for_each_entry_safe(conn_entry, next_conn_entry,
  6394. &phba->fcf_conn_rec_list, list) {
  6395. list_del_init(&conn_entry->list);
  6396. kfree(conn_entry);
  6397. }
  6398. return;
  6399. }
  6400. /**
  6401. * lpfc_init_api_table_setup - Set up init api function jump table
  6402. * @phba: The hba struct for which this call is being executed.
  6403. * @dev_grp: The HBA PCI-Device group number.
  6404. *
  6405. * This routine sets up the device INIT interface API function jump table
  6406. * in @phba struct.
  6407. *
  6408. * Returns: 0 - success, -ENODEV - failure.
  6409. **/
  6410. int
  6411. lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6412. {
  6413. phba->lpfc_hba_init_link = lpfc_hba_init_link;
  6414. phba->lpfc_hba_down_link = lpfc_hba_down_link;
  6415. phba->lpfc_selective_reset = lpfc_selective_reset;
  6416. switch (dev_grp) {
  6417. case LPFC_PCI_DEV_LP:
  6418. phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
  6419. phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
  6420. phba->lpfc_stop_port = lpfc_stop_port_s3;
  6421. break;
  6422. case LPFC_PCI_DEV_OC:
  6423. phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
  6424. phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
  6425. phba->lpfc_stop_port = lpfc_stop_port_s4;
  6426. break;
  6427. default:
  6428. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6429. "1431 Invalid HBA PCI-device group: 0x%x\n",
  6430. dev_grp);
  6431. return -ENODEV;
  6432. break;
  6433. }
  6434. return 0;
  6435. }
  6436. /**
  6437. * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
  6438. * @phba: pointer to lpfc hba data structure.
  6439. *
  6440. * This routine is invoked to set up the driver internal resources after the
  6441. * device specific resource setup to support the HBA device it attached to.
  6442. *
  6443. * Return codes
  6444. * 0 - successful
  6445. * other values - error
  6446. **/
  6447. static int
  6448. lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
  6449. {
  6450. int error;
  6451. /* Startup the kernel thread for this host adapter. */
  6452. phba->worker_thread = kthread_run(lpfc_do_work, phba,
  6453. "lpfc_worker_%d", phba->brd_no);
  6454. if (IS_ERR(phba->worker_thread)) {
  6455. error = PTR_ERR(phba->worker_thread);
  6456. return error;
  6457. }
  6458. return 0;
  6459. }
  6460. /**
  6461. * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
  6462. * @phba: pointer to lpfc hba data structure.
  6463. *
  6464. * This routine is invoked to unset the driver internal resources set up after
  6465. * the device specific resource setup for supporting the HBA device it
  6466. * attached to.
  6467. **/
  6468. static void
  6469. lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
  6470. {
  6471. if (phba->wq) {
  6472. flush_workqueue(phba->wq);
  6473. destroy_workqueue(phba->wq);
  6474. phba->wq = NULL;
  6475. }
  6476. /* Stop kernel worker thread */
  6477. if (phba->worker_thread)
  6478. kthread_stop(phba->worker_thread);
  6479. }
  6480. /**
  6481. * lpfc_free_iocb_list - Free iocb list.
  6482. * @phba: pointer to lpfc hba data structure.
  6483. *
  6484. * This routine is invoked to free the driver's IOCB list and memory.
  6485. **/
  6486. void
  6487. lpfc_free_iocb_list(struct lpfc_hba *phba)
  6488. {
  6489. struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
  6490. spin_lock_irq(&phba->hbalock);
  6491. list_for_each_entry_safe(iocbq_entry, iocbq_next,
  6492. &phba->lpfc_iocb_list, list) {
  6493. list_del(&iocbq_entry->list);
  6494. kfree(iocbq_entry);
  6495. phba->total_iocbq_bufs--;
  6496. }
  6497. spin_unlock_irq(&phba->hbalock);
  6498. return;
  6499. }
  6500. /**
  6501. * lpfc_init_iocb_list - Allocate and initialize iocb list.
  6502. * @phba: pointer to lpfc hba data structure.
  6503. *
  6504. * This routine is invoked to allocate and initizlize the driver's IOCB
  6505. * list and set up the IOCB tag array accordingly.
  6506. *
  6507. * Return codes
  6508. * 0 - successful
  6509. * other values - error
  6510. **/
  6511. int
  6512. lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
  6513. {
  6514. struct lpfc_iocbq *iocbq_entry = NULL;
  6515. uint16_t iotag;
  6516. int i;
  6517. /* Initialize and populate the iocb list per host. */
  6518. INIT_LIST_HEAD(&phba->lpfc_iocb_list);
  6519. for (i = 0; i < iocb_count; i++) {
  6520. iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
  6521. if (iocbq_entry == NULL) {
  6522. printk(KERN_ERR "%s: only allocated %d iocbs of "
  6523. "expected %d count. Unloading driver.\n",
  6524. __func__, i, iocb_count);
  6525. goto out_free_iocbq;
  6526. }
  6527. iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
  6528. if (iotag == 0) {
  6529. kfree(iocbq_entry);
  6530. printk(KERN_ERR "%s: failed to allocate IOTAG. "
  6531. "Unloading driver.\n", __func__);
  6532. goto out_free_iocbq;
  6533. }
  6534. iocbq_entry->sli4_lxritag = NO_XRI;
  6535. iocbq_entry->sli4_xritag = NO_XRI;
  6536. spin_lock_irq(&phba->hbalock);
  6537. list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
  6538. phba->total_iocbq_bufs++;
  6539. spin_unlock_irq(&phba->hbalock);
  6540. }
  6541. return 0;
  6542. out_free_iocbq:
  6543. lpfc_free_iocb_list(phba);
  6544. return -ENOMEM;
  6545. }
  6546. /**
  6547. * lpfc_free_sgl_list - Free a given sgl list.
  6548. * @phba: pointer to lpfc hba data structure.
  6549. * @sglq_list: pointer to the head of sgl list.
  6550. *
  6551. * This routine is invoked to free a give sgl list and memory.
  6552. **/
  6553. void
  6554. lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
  6555. {
  6556. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  6557. list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
  6558. list_del(&sglq_entry->list);
  6559. lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
  6560. kfree(sglq_entry);
  6561. }
  6562. }
  6563. /**
  6564. * lpfc_free_els_sgl_list - Free els sgl list.
  6565. * @phba: pointer to lpfc hba data structure.
  6566. *
  6567. * This routine is invoked to free the driver's els sgl list and memory.
  6568. **/
  6569. static void
  6570. lpfc_free_els_sgl_list(struct lpfc_hba *phba)
  6571. {
  6572. LIST_HEAD(sglq_list);
  6573. /* Retrieve all els sgls from driver list */
  6574. spin_lock_irq(&phba->hbalock);
  6575. spin_lock(&phba->sli4_hba.sgl_list_lock);
  6576. list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
  6577. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  6578. spin_unlock_irq(&phba->hbalock);
  6579. /* Now free the sgl list */
  6580. lpfc_free_sgl_list(phba, &sglq_list);
  6581. }
  6582. /**
  6583. * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
  6584. * @phba: pointer to lpfc hba data structure.
  6585. *
  6586. * This routine is invoked to free the driver's nvmet sgl list and memory.
  6587. **/
  6588. static void
  6589. lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
  6590. {
  6591. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  6592. LIST_HEAD(sglq_list);
  6593. /* Retrieve all nvmet sgls from driver list */
  6594. spin_lock_irq(&phba->hbalock);
  6595. spin_lock(&phba->sli4_hba.sgl_list_lock);
  6596. list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
  6597. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  6598. spin_unlock_irq(&phba->hbalock);
  6599. /* Now free the sgl list */
  6600. list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
  6601. list_del(&sglq_entry->list);
  6602. lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
  6603. kfree(sglq_entry);
  6604. }
  6605. /* Update the nvmet_xri_cnt to reflect no current sgls.
  6606. * The next initialization cycle sets the count and allocates
  6607. * the sgls over again.
  6608. */
  6609. phba->sli4_hba.nvmet_xri_cnt = 0;
  6610. }
  6611. /**
  6612. * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
  6613. * @phba: pointer to lpfc hba data structure.
  6614. *
  6615. * This routine is invoked to allocate the driver's active sgl memory.
  6616. * This array will hold the sglq_entry's for active IOs.
  6617. **/
  6618. static int
  6619. lpfc_init_active_sgl_array(struct lpfc_hba *phba)
  6620. {
  6621. int size;
  6622. size = sizeof(struct lpfc_sglq *);
  6623. size *= phba->sli4_hba.max_cfg_param.max_xri;
  6624. phba->sli4_hba.lpfc_sglq_active_list =
  6625. kzalloc(size, GFP_KERNEL);
  6626. if (!phba->sli4_hba.lpfc_sglq_active_list)
  6627. return -ENOMEM;
  6628. return 0;
  6629. }
  6630. /**
  6631. * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
  6632. * @phba: pointer to lpfc hba data structure.
  6633. *
  6634. * This routine is invoked to walk through the array of active sglq entries
  6635. * and free all of the resources.
  6636. * This is just a place holder for now.
  6637. **/
  6638. static void
  6639. lpfc_free_active_sgl(struct lpfc_hba *phba)
  6640. {
  6641. kfree(phba->sli4_hba.lpfc_sglq_active_list);
  6642. }
  6643. /**
  6644. * lpfc_init_sgl_list - Allocate and initialize sgl list.
  6645. * @phba: pointer to lpfc hba data structure.
  6646. *
  6647. * This routine is invoked to allocate and initizlize the driver's sgl
  6648. * list and set up the sgl xritag tag array accordingly.
  6649. *
  6650. **/
  6651. static void
  6652. lpfc_init_sgl_list(struct lpfc_hba *phba)
  6653. {
  6654. /* Initialize and populate the sglq list per host/VF. */
  6655. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
  6656. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
  6657. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
  6658. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
  6659. /* els xri-sgl book keeping */
  6660. phba->sli4_hba.els_xri_cnt = 0;
  6661. /* nvme xri-buffer book keeping */
  6662. phba->sli4_hba.io_xri_cnt = 0;
  6663. }
  6664. /**
  6665. * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
  6666. * @phba: pointer to lpfc hba data structure.
  6667. *
  6668. * This routine is invoked to post rpi header templates to the
  6669. * port for those SLI4 ports that do not support extents. This routine
  6670. * posts a PAGE_SIZE memory region to the port to hold up to
  6671. * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
  6672. * and should be called only when interrupts are disabled.
  6673. *
  6674. * Return codes
  6675. * 0 - successful
  6676. * -ERROR - otherwise.
  6677. **/
  6678. int
  6679. lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
  6680. {
  6681. int rc = 0;
  6682. struct lpfc_rpi_hdr *rpi_hdr;
  6683. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
  6684. if (!phba->sli4_hba.rpi_hdrs_in_use)
  6685. return rc;
  6686. if (phba->sli4_hba.extents_in_use)
  6687. return -EIO;
  6688. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  6689. if (!rpi_hdr) {
  6690. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6691. "0391 Error during rpi post operation\n");
  6692. lpfc_sli4_remove_rpis(phba);
  6693. rc = -ENODEV;
  6694. }
  6695. return rc;
  6696. }
  6697. /**
  6698. * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
  6699. * @phba: pointer to lpfc hba data structure.
  6700. *
  6701. * This routine is invoked to allocate a single 4KB memory region to
  6702. * support rpis and stores them in the phba. This single region
  6703. * provides support for up to 64 rpis. The region is used globally
  6704. * by the device.
  6705. *
  6706. * Returns:
  6707. * A valid rpi hdr on success.
  6708. * A NULL pointer on any failure.
  6709. **/
  6710. struct lpfc_rpi_hdr *
  6711. lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
  6712. {
  6713. uint16_t rpi_limit, curr_rpi_range;
  6714. struct lpfc_dmabuf *dmabuf;
  6715. struct lpfc_rpi_hdr *rpi_hdr;
  6716. /*
  6717. * If the SLI4 port supports extents, posting the rpi header isn't
  6718. * required. Set the expected maximum count and let the actual value
  6719. * get set when extents are fully allocated.
  6720. */
  6721. if (!phba->sli4_hba.rpi_hdrs_in_use)
  6722. return NULL;
  6723. if (phba->sli4_hba.extents_in_use)
  6724. return NULL;
  6725. /* The limit on the logical index is just the max_rpi count. */
  6726. rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
  6727. spin_lock_irq(&phba->hbalock);
  6728. /*
  6729. * Establish the starting RPI in this header block. The starting
  6730. * rpi is normalized to a zero base because the physical rpi is
  6731. * port based.
  6732. */
  6733. curr_rpi_range = phba->sli4_hba.next_rpi;
  6734. spin_unlock_irq(&phba->hbalock);
  6735. /* Reached full RPI range */
  6736. if (curr_rpi_range == rpi_limit)
  6737. return NULL;
  6738. /*
  6739. * First allocate the protocol header region for the port. The
  6740. * port expects a 4KB DMA-mapped memory region that is 4K aligned.
  6741. */
  6742. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  6743. if (!dmabuf)
  6744. return NULL;
  6745. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  6746. LPFC_HDR_TEMPLATE_SIZE,
  6747. &dmabuf->phys, GFP_KERNEL);
  6748. if (!dmabuf->virt) {
  6749. rpi_hdr = NULL;
  6750. goto err_free_dmabuf;
  6751. }
  6752. if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
  6753. rpi_hdr = NULL;
  6754. goto err_free_coherent;
  6755. }
  6756. /* Save the rpi header data for cleanup later. */
  6757. rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
  6758. if (!rpi_hdr)
  6759. goto err_free_coherent;
  6760. rpi_hdr->dmabuf = dmabuf;
  6761. rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
  6762. rpi_hdr->page_count = 1;
  6763. spin_lock_irq(&phba->hbalock);
  6764. /* The rpi_hdr stores the logical index only. */
  6765. rpi_hdr->start_rpi = curr_rpi_range;
  6766. rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
  6767. list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
  6768. spin_unlock_irq(&phba->hbalock);
  6769. return rpi_hdr;
  6770. err_free_coherent:
  6771. dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
  6772. dmabuf->virt, dmabuf->phys);
  6773. err_free_dmabuf:
  6774. kfree(dmabuf);
  6775. return NULL;
  6776. }
  6777. /**
  6778. * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
  6779. * @phba: pointer to lpfc hba data structure.
  6780. *
  6781. * This routine is invoked to remove all memory resources allocated
  6782. * to support rpis for SLI4 ports not supporting extents. This routine
  6783. * presumes the caller has released all rpis consumed by fabric or port
  6784. * logins and is prepared to have the header pages removed.
  6785. **/
  6786. void
  6787. lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
  6788. {
  6789. struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
  6790. if (!phba->sli4_hba.rpi_hdrs_in_use)
  6791. goto exit;
  6792. list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
  6793. &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  6794. list_del(&rpi_hdr->list);
  6795. dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
  6796. rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
  6797. kfree(rpi_hdr->dmabuf);
  6798. kfree(rpi_hdr);
  6799. }
  6800. exit:
  6801. /* There are no rpis available to the port now. */
  6802. phba->sli4_hba.next_rpi = 0;
  6803. }
  6804. /**
  6805. * lpfc_hba_alloc - Allocate driver hba data structure for a device.
  6806. * @pdev: pointer to pci device data structure.
  6807. *
  6808. * This routine is invoked to allocate the driver hba data structure for an
  6809. * HBA device. If the allocation is successful, the phba reference to the
  6810. * PCI device data structure is set.
  6811. *
  6812. * Return codes
  6813. * pointer to @phba - successful
  6814. * NULL - error
  6815. **/
  6816. static struct lpfc_hba *
  6817. lpfc_hba_alloc(struct pci_dev *pdev)
  6818. {
  6819. struct lpfc_hba *phba;
  6820. /* Allocate memory for HBA structure */
  6821. phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
  6822. if (!phba) {
  6823. dev_err(&pdev->dev, "failed to allocate hba struct\n");
  6824. return NULL;
  6825. }
  6826. /* Set reference to PCI device in HBA structure */
  6827. phba->pcidev = pdev;
  6828. /* Assign an unused board number */
  6829. phba->brd_no = lpfc_get_instance();
  6830. if (phba->brd_no < 0) {
  6831. kfree(phba);
  6832. return NULL;
  6833. }
  6834. phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
  6835. spin_lock_init(&phba->ct_ev_lock);
  6836. INIT_LIST_HEAD(&phba->ct_ev_waiters);
  6837. return phba;
  6838. }
  6839. /**
  6840. * lpfc_hba_free - Free driver hba data structure with a device.
  6841. * @phba: pointer to lpfc hba data structure.
  6842. *
  6843. * This routine is invoked to free the driver hba data structure with an
  6844. * HBA device.
  6845. **/
  6846. static void
  6847. lpfc_hba_free(struct lpfc_hba *phba)
  6848. {
  6849. if (phba->sli_rev == LPFC_SLI_REV4)
  6850. kfree(phba->sli4_hba.hdwq);
  6851. /* Release the driver assigned board number */
  6852. idr_remove(&lpfc_hba_index, phba->brd_no);
  6853. /* Free memory allocated with sli3 rings */
  6854. kfree(phba->sli.sli3_ring);
  6855. phba->sli.sli3_ring = NULL;
  6856. kfree(phba);
  6857. return;
  6858. }
  6859. /**
  6860. * lpfc_create_shost - Create hba physical port with associated scsi host.
  6861. * @phba: pointer to lpfc hba data structure.
  6862. *
  6863. * This routine is invoked to create HBA physical port and associate a SCSI
  6864. * host with it.
  6865. *
  6866. * Return codes
  6867. * 0 - successful
  6868. * other values - error
  6869. **/
  6870. static int
  6871. lpfc_create_shost(struct lpfc_hba *phba)
  6872. {
  6873. struct lpfc_vport *vport;
  6874. struct Scsi_Host *shost;
  6875. /* Initialize HBA FC structure */
  6876. phba->fc_edtov = FF_DEF_EDTOV;
  6877. phba->fc_ratov = FF_DEF_RATOV;
  6878. phba->fc_altov = FF_DEF_ALTOV;
  6879. phba->fc_arbtov = FF_DEF_ARBTOV;
  6880. atomic_set(&phba->sdev_cnt, 0);
  6881. vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
  6882. if (!vport)
  6883. return -ENODEV;
  6884. shost = lpfc_shost_from_vport(vport);
  6885. phba->pport = vport;
  6886. if (phba->nvmet_support) {
  6887. /* Only 1 vport (pport) will support NVME target */
  6888. phba->targetport = NULL;
  6889. phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
  6890. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME_DISC,
  6891. "6076 NVME Target Found\n");
  6892. }
  6893. lpfc_debugfs_initialize(vport);
  6894. /* Put reference to SCSI host to driver's device private data */
  6895. pci_set_drvdata(phba->pcidev, shost);
  6896. /*
  6897. * At this point we are fully registered with PSA. In addition,
  6898. * any initial discovery should be completed.
  6899. */
  6900. vport->load_flag |= FC_ALLOW_FDMI;
  6901. if (phba->cfg_enable_SmartSAN ||
  6902. (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
  6903. /* Setup appropriate attribute masks */
  6904. vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
  6905. if (phba->cfg_enable_SmartSAN)
  6906. vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
  6907. else
  6908. vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
  6909. }
  6910. return 0;
  6911. }
  6912. /**
  6913. * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
  6914. * @phba: pointer to lpfc hba data structure.
  6915. *
  6916. * This routine is invoked to destroy HBA physical port and the associated
  6917. * SCSI host.
  6918. **/
  6919. static void
  6920. lpfc_destroy_shost(struct lpfc_hba *phba)
  6921. {
  6922. struct lpfc_vport *vport = phba->pport;
  6923. /* Destroy physical port that associated with the SCSI host */
  6924. destroy_port(vport);
  6925. return;
  6926. }
  6927. /**
  6928. * lpfc_setup_bg - Setup Block guard structures and debug areas.
  6929. * @phba: pointer to lpfc hba data structure.
  6930. * @shost: the shost to be used to detect Block guard settings.
  6931. *
  6932. * This routine sets up the local Block guard protocol settings for @shost.
  6933. * This routine also allocates memory for debugging bg buffers.
  6934. **/
  6935. static void
  6936. lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
  6937. {
  6938. uint32_t old_mask;
  6939. uint32_t old_guard;
  6940. if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
  6941. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6942. "1478 Registering BlockGuard with the "
  6943. "SCSI layer\n");
  6944. old_mask = phba->cfg_prot_mask;
  6945. old_guard = phba->cfg_prot_guard;
  6946. /* Only allow supported values */
  6947. phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
  6948. SHOST_DIX_TYPE0_PROTECTION |
  6949. SHOST_DIX_TYPE1_PROTECTION);
  6950. phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
  6951. SHOST_DIX_GUARD_CRC);
  6952. /* DIF Type 1 protection for profiles AST1/C1 is end to end */
  6953. if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
  6954. phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
  6955. if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
  6956. if ((old_mask != phba->cfg_prot_mask) ||
  6957. (old_guard != phba->cfg_prot_guard))
  6958. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6959. "1475 Registering BlockGuard with the "
  6960. "SCSI layer: mask %d guard %d\n",
  6961. phba->cfg_prot_mask,
  6962. phba->cfg_prot_guard);
  6963. scsi_host_set_prot(shost, phba->cfg_prot_mask);
  6964. scsi_host_set_guard(shost, phba->cfg_prot_guard);
  6965. } else
  6966. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6967. "1479 Not Registering BlockGuard with the SCSI "
  6968. "layer, Bad protection parameters: %d %d\n",
  6969. old_mask, old_guard);
  6970. }
  6971. }
  6972. /**
  6973. * lpfc_post_init_setup - Perform necessary device post initialization setup.
  6974. * @phba: pointer to lpfc hba data structure.
  6975. *
  6976. * This routine is invoked to perform all the necessary post initialization
  6977. * setup for the device.
  6978. **/
  6979. static void
  6980. lpfc_post_init_setup(struct lpfc_hba *phba)
  6981. {
  6982. struct Scsi_Host *shost;
  6983. struct lpfc_adapter_event_header adapter_event;
  6984. /* Get the default values for Model Name and Description */
  6985. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  6986. /*
  6987. * hba setup may have changed the hba_queue_depth so we need to
  6988. * adjust the value of can_queue.
  6989. */
  6990. shost = pci_get_drvdata(phba->pcidev);
  6991. shost->can_queue = phba->cfg_hba_queue_depth - 10;
  6992. lpfc_host_attrib_init(shost);
  6993. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  6994. spin_lock_irq(shost->host_lock);
  6995. lpfc_poll_start_timer(phba);
  6996. spin_unlock_irq(shost->host_lock);
  6997. }
  6998. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6999. "0428 Perform SCSI scan\n");
  7000. /* Send board arrival event to upper layer */
  7001. adapter_event.event_type = FC_REG_ADAPTER_EVENT;
  7002. adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
  7003. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7004. sizeof(adapter_event),
  7005. (char *) &adapter_event,
  7006. LPFC_NL_VENDOR_ID);
  7007. return;
  7008. }
  7009. /**
  7010. * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
  7011. * @phba: pointer to lpfc hba data structure.
  7012. *
  7013. * This routine is invoked to set up the PCI device memory space for device
  7014. * with SLI-3 interface spec.
  7015. *
  7016. * Return codes
  7017. * 0 - successful
  7018. * other values - error
  7019. **/
  7020. static int
  7021. lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
  7022. {
  7023. struct pci_dev *pdev = phba->pcidev;
  7024. unsigned long bar0map_len, bar2map_len;
  7025. int i, hbq_count;
  7026. void *ptr;
  7027. int error;
  7028. if (!pdev)
  7029. return -ENODEV;
  7030. /* Set the device DMA mask size */
  7031. error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  7032. if (error)
  7033. error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  7034. if (error)
  7035. return error;
  7036. error = -ENODEV;
  7037. /* Get the bus address of Bar0 and Bar2 and the number of bytes
  7038. * required by each mapping.
  7039. */
  7040. phba->pci_bar0_map = pci_resource_start(pdev, 0);
  7041. bar0map_len = pci_resource_len(pdev, 0);
  7042. phba->pci_bar2_map = pci_resource_start(pdev, 2);
  7043. bar2map_len = pci_resource_len(pdev, 2);
  7044. /* Map HBA SLIM to a kernel virtual address. */
  7045. phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
  7046. if (!phba->slim_memmap_p) {
  7047. dev_printk(KERN_ERR, &pdev->dev,
  7048. "ioremap failed for SLIM memory.\n");
  7049. goto out;
  7050. }
  7051. /* Map HBA Control Registers to a kernel virtual address. */
  7052. phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
  7053. if (!phba->ctrl_regs_memmap_p) {
  7054. dev_printk(KERN_ERR, &pdev->dev,
  7055. "ioremap failed for HBA control registers.\n");
  7056. goto out_iounmap_slim;
  7057. }
  7058. /* Allocate memory for SLI-2 structures */
  7059. phba->slim2p.virt = dma_alloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  7060. &phba->slim2p.phys, GFP_KERNEL);
  7061. if (!phba->slim2p.virt)
  7062. goto out_iounmap;
  7063. phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
  7064. phba->mbox_ext = (phba->slim2p.virt +
  7065. offsetof(struct lpfc_sli2_slim, mbx_ext_words));
  7066. phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
  7067. phba->IOCBs = (phba->slim2p.virt +
  7068. offsetof(struct lpfc_sli2_slim, IOCBs));
  7069. phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
  7070. lpfc_sli_hbq_size(),
  7071. &phba->hbqslimp.phys,
  7072. GFP_KERNEL);
  7073. if (!phba->hbqslimp.virt)
  7074. goto out_free_slim;
  7075. hbq_count = lpfc_sli_hbq_count();
  7076. ptr = phba->hbqslimp.virt;
  7077. for (i = 0; i < hbq_count; ++i) {
  7078. phba->hbqs[i].hbq_virt = ptr;
  7079. INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
  7080. ptr += (lpfc_hbq_defs[i]->entry_count *
  7081. sizeof(struct lpfc_hbq_entry));
  7082. }
  7083. phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
  7084. phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
  7085. memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
  7086. phba->MBslimaddr = phba->slim_memmap_p;
  7087. phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
  7088. phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
  7089. phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
  7090. phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
  7091. return 0;
  7092. out_free_slim:
  7093. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  7094. phba->slim2p.virt, phba->slim2p.phys);
  7095. out_iounmap:
  7096. iounmap(phba->ctrl_regs_memmap_p);
  7097. out_iounmap_slim:
  7098. iounmap(phba->slim_memmap_p);
  7099. out:
  7100. return error;
  7101. }
  7102. /**
  7103. * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
  7104. * @phba: pointer to lpfc hba data structure.
  7105. *
  7106. * This routine is invoked to unset the PCI device memory space for device
  7107. * with SLI-3 interface spec.
  7108. **/
  7109. static void
  7110. lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
  7111. {
  7112. struct pci_dev *pdev;
  7113. /* Obtain PCI device reference */
  7114. if (!phba->pcidev)
  7115. return;
  7116. else
  7117. pdev = phba->pcidev;
  7118. /* Free coherent DMA memory allocated */
  7119. dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
  7120. phba->hbqslimp.virt, phba->hbqslimp.phys);
  7121. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  7122. phba->slim2p.virt, phba->slim2p.phys);
  7123. /* I/O memory unmap */
  7124. iounmap(phba->ctrl_regs_memmap_p);
  7125. iounmap(phba->slim_memmap_p);
  7126. return;
  7127. }
  7128. /**
  7129. * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
  7130. * @phba: pointer to lpfc hba data structure.
  7131. *
  7132. * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
  7133. * done and check status.
  7134. *
  7135. * Return 0 if successful, otherwise -ENODEV.
  7136. **/
  7137. int
  7138. lpfc_sli4_post_status_check(struct lpfc_hba *phba)
  7139. {
  7140. struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
  7141. struct lpfc_register reg_data;
  7142. int i, port_error = 0;
  7143. uint32_t if_type;
  7144. memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
  7145. memset(&reg_data, 0, sizeof(reg_data));
  7146. if (!phba->sli4_hba.PSMPHRregaddr)
  7147. return -ENODEV;
  7148. /* Wait up to 30 seconds for the SLI Port POST done and ready */
  7149. for (i = 0; i < 3000; i++) {
  7150. if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  7151. &portsmphr_reg.word0) ||
  7152. (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
  7153. /* Port has a fatal POST error, break out */
  7154. port_error = -ENODEV;
  7155. break;
  7156. }
  7157. if (LPFC_POST_STAGE_PORT_READY ==
  7158. bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
  7159. break;
  7160. msleep(10);
  7161. }
  7162. /*
  7163. * If there was a port error during POST, then don't proceed with
  7164. * other register reads as the data may not be valid. Just exit.
  7165. */
  7166. if (port_error) {
  7167. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7168. "1408 Port Failed POST - portsmphr=0x%x, "
  7169. "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
  7170. "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
  7171. portsmphr_reg.word0,
  7172. bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
  7173. bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
  7174. bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
  7175. bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
  7176. bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
  7177. bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
  7178. bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
  7179. bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
  7180. } else {
  7181. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  7182. "2534 Device Info: SLIFamily=0x%x, "
  7183. "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
  7184. "SLIHint_2=0x%x, FT=0x%x\n",
  7185. bf_get(lpfc_sli_intf_sli_family,
  7186. &phba->sli4_hba.sli_intf),
  7187. bf_get(lpfc_sli_intf_slirev,
  7188. &phba->sli4_hba.sli_intf),
  7189. bf_get(lpfc_sli_intf_if_type,
  7190. &phba->sli4_hba.sli_intf),
  7191. bf_get(lpfc_sli_intf_sli_hint1,
  7192. &phba->sli4_hba.sli_intf),
  7193. bf_get(lpfc_sli_intf_sli_hint2,
  7194. &phba->sli4_hba.sli_intf),
  7195. bf_get(lpfc_sli_intf_func_type,
  7196. &phba->sli4_hba.sli_intf));
  7197. /*
  7198. * Check for other Port errors during the initialization
  7199. * process. Fail the load if the port did not come up
  7200. * correctly.
  7201. */
  7202. if_type = bf_get(lpfc_sli_intf_if_type,
  7203. &phba->sli4_hba.sli_intf);
  7204. switch (if_type) {
  7205. case LPFC_SLI_INTF_IF_TYPE_0:
  7206. phba->sli4_hba.ue_mask_lo =
  7207. readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
  7208. phba->sli4_hba.ue_mask_hi =
  7209. readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
  7210. uerrlo_reg.word0 =
  7211. readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
  7212. uerrhi_reg.word0 =
  7213. readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
  7214. if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
  7215. (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
  7216. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7217. "1422 Unrecoverable Error "
  7218. "Detected during POST "
  7219. "uerr_lo_reg=0x%x, "
  7220. "uerr_hi_reg=0x%x, "
  7221. "ue_mask_lo_reg=0x%x, "
  7222. "ue_mask_hi_reg=0x%x\n",
  7223. uerrlo_reg.word0,
  7224. uerrhi_reg.word0,
  7225. phba->sli4_hba.ue_mask_lo,
  7226. phba->sli4_hba.ue_mask_hi);
  7227. port_error = -ENODEV;
  7228. }
  7229. break;
  7230. case LPFC_SLI_INTF_IF_TYPE_2:
  7231. case LPFC_SLI_INTF_IF_TYPE_6:
  7232. /* Final checks. The port status should be clean. */
  7233. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  7234. &reg_data.word0) ||
  7235. (bf_get(lpfc_sliport_status_err, &reg_data) &&
  7236. !bf_get(lpfc_sliport_status_rn, &reg_data))) {
  7237. phba->work_status[0] =
  7238. readl(phba->sli4_hba.u.if_type2.
  7239. ERR1regaddr);
  7240. phba->work_status[1] =
  7241. readl(phba->sli4_hba.u.if_type2.
  7242. ERR2regaddr);
  7243. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7244. "2888 Unrecoverable port error "
  7245. "following POST: port status reg "
  7246. "0x%x, port_smphr reg 0x%x, "
  7247. "error 1=0x%x, error 2=0x%x\n",
  7248. reg_data.word0,
  7249. portsmphr_reg.word0,
  7250. phba->work_status[0],
  7251. phba->work_status[1]);
  7252. port_error = -ENODEV;
  7253. }
  7254. break;
  7255. case LPFC_SLI_INTF_IF_TYPE_1:
  7256. default:
  7257. break;
  7258. }
  7259. }
  7260. return port_error;
  7261. }
  7262. /**
  7263. * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
  7264. * @phba: pointer to lpfc hba data structure.
  7265. * @if_type: The SLI4 interface type getting configured.
  7266. *
  7267. * This routine is invoked to set up SLI4 BAR0 PCI config space register
  7268. * memory map.
  7269. **/
  7270. static void
  7271. lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
  7272. {
  7273. switch (if_type) {
  7274. case LPFC_SLI_INTF_IF_TYPE_0:
  7275. phba->sli4_hba.u.if_type0.UERRLOregaddr =
  7276. phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
  7277. phba->sli4_hba.u.if_type0.UERRHIregaddr =
  7278. phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
  7279. phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
  7280. phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
  7281. phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
  7282. phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
  7283. phba->sli4_hba.SLIINTFregaddr =
  7284. phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
  7285. break;
  7286. case LPFC_SLI_INTF_IF_TYPE_2:
  7287. phba->sli4_hba.u.if_type2.EQDregaddr =
  7288. phba->sli4_hba.conf_regs_memmap_p +
  7289. LPFC_CTL_PORT_EQ_DELAY_OFFSET;
  7290. phba->sli4_hba.u.if_type2.ERR1regaddr =
  7291. phba->sli4_hba.conf_regs_memmap_p +
  7292. LPFC_CTL_PORT_ER1_OFFSET;
  7293. phba->sli4_hba.u.if_type2.ERR2regaddr =
  7294. phba->sli4_hba.conf_regs_memmap_p +
  7295. LPFC_CTL_PORT_ER2_OFFSET;
  7296. phba->sli4_hba.u.if_type2.CTRLregaddr =
  7297. phba->sli4_hba.conf_regs_memmap_p +
  7298. LPFC_CTL_PORT_CTL_OFFSET;
  7299. phba->sli4_hba.u.if_type2.STATUSregaddr =
  7300. phba->sli4_hba.conf_regs_memmap_p +
  7301. LPFC_CTL_PORT_STA_OFFSET;
  7302. phba->sli4_hba.SLIINTFregaddr =
  7303. phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
  7304. phba->sli4_hba.PSMPHRregaddr =
  7305. phba->sli4_hba.conf_regs_memmap_p +
  7306. LPFC_CTL_PORT_SEM_OFFSET;
  7307. phba->sli4_hba.RQDBregaddr =
  7308. phba->sli4_hba.conf_regs_memmap_p +
  7309. LPFC_ULP0_RQ_DOORBELL;
  7310. phba->sli4_hba.WQDBregaddr =
  7311. phba->sli4_hba.conf_regs_memmap_p +
  7312. LPFC_ULP0_WQ_DOORBELL;
  7313. phba->sli4_hba.CQDBregaddr =
  7314. phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
  7315. phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
  7316. phba->sli4_hba.MQDBregaddr =
  7317. phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
  7318. phba->sli4_hba.BMBXregaddr =
  7319. phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
  7320. break;
  7321. case LPFC_SLI_INTF_IF_TYPE_6:
  7322. phba->sli4_hba.u.if_type2.EQDregaddr =
  7323. phba->sli4_hba.conf_regs_memmap_p +
  7324. LPFC_CTL_PORT_EQ_DELAY_OFFSET;
  7325. phba->sli4_hba.u.if_type2.ERR1regaddr =
  7326. phba->sli4_hba.conf_regs_memmap_p +
  7327. LPFC_CTL_PORT_ER1_OFFSET;
  7328. phba->sli4_hba.u.if_type2.ERR2regaddr =
  7329. phba->sli4_hba.conf_regs_memmap_p +
  7330. LPFC_CTL_PORT_ER2_OFFSET;
  7331. phba->sli4_hba.u.if_type2.CTRLregaddr =
  7332. phba->sli4_hba.conf_regs_memmap_p +
  7333. LPFC_CTL_PORT_CTL_OFFSET;
  7334. phba->sli4_hba.u.if_type2.STATUSregaddr =
  7335. phba->sli4_hba.conf_regs_memmap_p +
  7336. LPFC_CTL_PORT_STA_OFFSET;
  7337. phba->sli4_hba.PSMPHRregaddr =
  7338. phba->sli4_hba.conf_regs_memmap_p +
  7339. LPFC_CTL_PORT_SEM_OFFSET;
  7340. phba->sli4_hba.BMBXregaddr =
  7341. phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
  7342. break;
  7343. case LPFC_SLI_INTF_IF_TYPE_1:
  7344. default:
  7345. dev_printk(KERN_ERR, &phba->pcidev->dev,
  7346. "FATAL - unsupported SLI4 interface type - %d\n",
  7347. if_type);
  7348. break;
  7349. }
  7350. }
  7351. /**
  7352. * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
  7353. * @phba: pointer to lpfc hba data structure.
  7354. *
  7355. * This routine is invoked to set up SLI4 BAR1 register memory map.
  7356. **/
  7357. static void
  7358. lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
  7359. {
  7360. switch (if_type) {
  7361. case LPFC_SLI_INTF_IF_TYPE_0:
  7362. phba->sli4_hba.PSMPHRregaddr =
  7363. phba->sli4_hba.ctrl_regs_memmap_p +
  7364. LPFC_SLIPORT_IF0_SMPHR;
  7365. phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
  7366. LPFC_HST_ISR0;
  7367. phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
  7368. LPFC_HST_IMR0;
  7369. phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
  7370. LPFC_HST_ISCR0;
  7371. break;
  7372. case LPFC_SLI_INTF_IF_TYPE_6:
  7373. phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
  7374. LPFC_IF6_RQ_DOORBELL;
  7375. phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
  7376. LPFC_IF6_WQ_DOORBELL;
  7377. phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
  7378. LPFC_IF6_CQ_DOORBELL;
  7379. phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
  7380. LPFC_IF6_EQ_DOORBELL;
  7381. phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
  7382. LPFC_IF6_MQ_DOORBELL;
  7383. break;
  7384. case LPFC_SLI_INTF_IF_TYPE_2:
  7385. case LPFC_SLI_INTF_IF_TYPE_1:
  7386. default:
  7387. dev_err(&phba->pcidev->dev,
  7388. "FATAL - unsupported SLI4 interface type - %d\n",
  7389. if_type);
  7390. break;
  7391. }
  7392. }
  7393. /**
  7394. * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
  7395. * @phba: pointer to lpfc hba data structure.
  7396. * @vf: virtual function number
  7397. *
  7398. * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
  7399. * based on the given viftual function number, @vf.
  7400. *
  7401. * Return 0 if successful, otherwise -ENODEV.
  7402. **/
  7403. static int
  7404. lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
  7405. {
  7406. if (vf > LPFC_VIR_FUNC_MAX)
  7407. return -ENODEV;
  7408. phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
  7409. vf * LPFC_VFR_PAGE_SIZE +
  7410. LPFC_ULP0_RQ_DOORBELL);
  7411. phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
  7412. vf * LPFC_VFR_PAGE_SIZE +
  7413. LPFC_ULP0_WQ_DOORBELL);
  7414. phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
  7415. vf * LPFC_VFR_PAGE_SIZE +
  7416. LPFC_EQCQ_DOORBELL);
  7417. phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
  7418. phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
  7419. vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
  7420. phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
  7421. vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
  7422. return 0;
  7423. }
  7424. /**
  7425. * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
  7426. * @phba: pointer to lpfc hba data structure.
  7427. *
  7428. * This routine is invoked to create the bootstrap mailbox
  7429. * region consistent with the SLI-4 interface spec. This
  7430. * routine allocates all memory necessary to communicate
  7431. * mailbox commands to the port and sets up all alignment
  7432. * needs. No locks are expected to be held when calling
  7433. * this routine.
  7434. *
  7435. * Return codes
  7436. * 0 - successful
  7437. * -ENOMEM - could not allocated memory.
  7438. **/
  7439. static int
  7440. lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
  7441. {
  7442. uint32_t bmbx_size;
  7443. struct lpfc_dmabuf *dmabuf;
  7444. struct dma_address *dma_address;
  7445. uint32_t pa_addr;
  7446. uint64_t phys_addr;
  7447. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  7448. if (!dmabuf)
  7449. return -ENOMEM;
  7450. /*
  7451. * The bootstrap mailbox region is comprised of 2 parts
  7452. * plus an alignment restriction of 16 bytes.
  7453. */
  7454. bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
  7455. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, bmbx_size,
  7456. &dmabuf->phys, GFP_KERNEL);
  7457. if (!dmabuf->virt) {
  7458. kfree(dmabuf);
  7459. return -ENOMEM;
  7460. }
  7461. /*
  7462. * Initialize the bootstrap mailbox pointers now so that the register
  7463. * operations are simple later. The mailbox dma address is required
  7464. * to be 16-byte aligned. Also align the virtual memory as each
  7465. * maibox is copied into the bmbx mailbox region before issuing the
  7466. * command to the port.
  7467. */
  7468. phba->sli4_hba.bmbx.dmabuf = dmabuf;
  7469. phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
  7470. phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
  7471. LPFC_ALIGN_16_BYTE);
  7472. phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
  7473. LPFC_ALIGN_16_BYTE);
  7474. /*
  7475. * Set the high and low physical addresses now. The SLI4 alignment
  7476. * requirement is 16 bytes and the mailbox is posted to the port
  7477. * as two 30-bit addresses. The other data is a bit marking whether
  7478. * the 30-bit address is the high or low address.
  7479. * Upcast bmbx aphys to 64bits so shift instruction compiles
  7480. * clean on 32 bit machines.
  7481. */
  7482. dma_address = &phba->sli4_hba.bmbx.dma_address;
  7483. phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
  7484. pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
  7485. dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
  7486. LPFC_BMBX_BIT1_ADDR_HI);
  7487. pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
  7488. dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
  7489. LPFC_BMBX_BIT1_ADDR_LO);
  7490. return 0;
  7491. }
  7492. /**
  7493. * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
  7494. * @phba: pointer to lpfc hba data structure.
  7495. *
  7496. * This routine is invoked to teardown the bootstrap mailbox
  7497. * region and release all host resources. This routine requires
  7498. * the caller to ensure all mailbox commands recovered, no
  7499. * additional mailbox comands are sent, and interrupts are disabled
  7500. * before calling this routine.
  7501. *
  7502. **/
  7503. static void
  7504. lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
  7505. {
  7506. dma_free_coherent(&phba->pcidev->dev,
  7507. phba->sli4_hba.bmbx.bmbx_size,
  7508. phba->sli4_hba.bmbx.dmabuf->virt,
  7509. phba->sli4_hba.bmbx.dmabuf->phys);
  7510. kfree(phba->sli4_hba.bmbx.dmabuf);
  7511. memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
  7512. }
  7513. static const char * const lpfc_topo_to_str[] = {
  7514. "Loop then P2P",
  7515. "Loopback",
  7516. "P2P Only",
  7517. "Unsupported",
  7518. "Loop Only",
  7519. "Unsupported",
  7520. "P2P then Loop",
  7521. };
  7522. /**
  7523. * lpfc_map_topology - Map the topology read from READ_CONFIG
  7524. * @phba: pointer to lpfc hba data structure.
  7525. * @rdconf: pointer to read config data
  7526. *
  7527. * This routine is invoked to map the topology values as read
  7528. * from the read config mailbox command. If the persistent
  7529. * topology feature is supported, the firmware will provide the
  7530. * saved topology information to be used in INIT_LINK
  7531. *
  7532. **/
  7533. #define LINK_FLAGS_DEF 0x0
  7534. #define LINK_FLAGS_P2P 0x1
  7535. #define LINK_FLAGS_LOOP 0x2
  7536. static void
  7537. lpfc_map_topology(struct lpfc_hba *phba, struct lpfc_mbx_read_config *rd_config)
  7538. {
  7539. u8 ptv, tf, pt;
  7540. ptv = bf_get(lpfc_mbx_rd_conf_ptv, rd_config);
  7541. tf = bf_get(lpfc_mbx_rd_conf_tf, rd_config);
  7542. pt = bf_get(lpfc_mbx_rd_conf_pt, rd_config);
  7543. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7544. "2027 Read Config Data : ptv:0x%x, tf:0x%x pt:0x%x",
  7545. ptv, tf, pt);
  7546. if (!ptv) {
  7547. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7548. "2019 FW does not support persistent topology "
  7549. "Using driver parameter defined value [%s]",
  7550. lpfc_topo_to_str[phba->cfg_topology]);
  7551. return;
  7552. }
  7553. /* FW supports persistent topology - override module parameter value */
  7554. phba->hba_flag |= HBA_PERSISTENT_TOPO;
  7555. switch (phba->pcidev->device) {
  7556. case PCI_DEVICE_ID_LANCER_G7_FC:
  7557. case PCI_DEVICE_ID_LANCER_G6_FC:
  7558. if (!tf) {
  7559. phba->cfg_topology = ((pt == LINK_FLAGS_LOOP)
  7560. ? FLAGS_TOPOLOGY_MODE_LOOP
  7561. : FLAGS_TOPOLOGY_MODE_PT_PT);
  7562. } else {
  7563. phba->hba_flag &= ~HBA_PERSISTENT_TOPO;
  7564. }
  7565. break;
  7566. default: /* G5 */
  7567. if (tf) {
  7568. /* If topology failover set - pt is '0' or '1' */
  7569. phba->cfg_topology = (pt ? FLAGS_TOPOLOGY_MODE_PT_LOOP :
  7570. FLAGS_TOPOLOGY_MODE_LOOP_PT);
  7571. } else {
  7572. phba->cfg_topology = ((pt == LINK_FLAGS_P2P)
  7573. ? FLAGS_TOPOLOGY_MODE_PT_PT
  7574. : FLAGS_TOPOLOGY_MODE_LOOP);
  7575. }
  7576. break;
  7577. }
  7578. if (phba->hba_flag & HBA_PERSISTENT_TOPO) {
  7579. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7580. "2020 Using persistent topology value [%s]",
  7581. lpfc_topo_to_str[phba->cfg_topology]);
  7582. } else {
  7583. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7584. "2021 Invalid topology values from FW "
  7585. "Using driver parameter defined value [%s]",
  7586. lpfc_topo_to_str[phba->cfg_topology]);
  7587. }
  7588. }
  7589. /**
  7590. * lpfc_sli4_read_config - Get the config parameters.
  7591. * @phba: pointer to lpfc hba data structure.
  7592. *
  7593. * This routine is invoked to read the configuration parameters from the HBA.
  7594. * The configuration parameters are used to set the base and maximum values
  7595. * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
  7596. * allocation for the port.
  7597. *
  7598. * Return codes
  7599. * 0 - successful
  7600. * -ENOMEM - No available memory
  7601. * -EIO - The mailbox failed to complete successfully.
  7602. **/
  7603. int
  7604. lpfc_sli4_read_config(struct lpfc_hba *phba)
  7605. {
  7606. LPFC_MBOXQ_t *pmb;
  7607. struct lpfc_mbx_read_config *rd_config;
  7608. union lpfc_sli4_cfg_shdr *shdr;
  7609. uint32_t shdr_status, shdr_add_status;
  7610. struct lpfc_mbx_get_func_cfg *get_func_cfg;
  7611. struct lpfc_rsrc_desc_fcfcoe *desc;
  7612. char *pdesc_0;
  7613. uint16_t forced_link_speed;
  7614. uint32_t if_type, qmin;
  7615. int length, i, rc = 0, rc2;
  7616. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  7617. if (!pmb) {
  7618. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7619. "2011 Unable to allocate memory for issuing "
  7620. "SLI_CONFIG_SPECIAL mailbox command\n");
  7621. return -ENOMEM;
  7622. }
  7623. lpfc_read_config(phba, pmb);
  7624. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  7625. if (rc != MBX_SUCCESS) {
  7626. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7627. "2012 Mailbox failed , mbxCmd x%x "
  7628. "READ_CONFIG, mbxStatus x%x\n",
  7629. bf_get(lpfc_mqe_command, &pmb->u.mqe),
  7630. bf_get(lpfc_mqe_status, &pmb->u.mqe));
  7631. rc = -EIO;
  7632. } else {
  7633. rd_config = &pmb->u.mqe.un.rd_config;
  7634. if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
  7635. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  7636. phba->sli4_hba.lnk_info.lnk_tp =
  7637. bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
  7638. phba->sli4_hba.lnk_info.lnk_no =
  7639. bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
  7640. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7641. "3081 lnk_type:%d, lnk_numb:%d\n",
  7642. phba->sli4_hba.lnk_info.lnk_tp,
  7643. phba->sli4_hba.lnk_info.lnk_no);
  7644. } else
  7645. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7646. "3082 Mailbox (x%x) returned ldv:x0\n",
  7647. bf_get(lpfc_mqe_command, &pmb->u.mqe));
  7648. if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
  7649. phba->bbcredit_support = 1;
  7650. phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
  7651. }
  7652. phba->sli4_hba.conf_trunk =
  7653. bf_get(lpfc_mbx_rd_conf_trunk, rd_config);
  7654. phba->sli4_hba.extents_in_use =
  7655. bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
  7656. phba->sli4_hba.max_cfg_param.max_xri =
  7657. bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
  7658. /* Reduce resource usage in kdump environment */
  7659. if (is_kdump_kernel() &&
  7660. phba->sli4_hba.max_cfg_param.max_xri > 512)
  7661. phba->sli4_hba.max_cfg_param.max_xri = 512;
  7662. phba->sli4_hba.max_cfg_param.xri_base =
  7663. bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
  7664. phba->sli4_hba.max_cfg_param.max_vpi =
  7665. bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
  7666. /* Limit the max we support */
  7667. if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
  7668. phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
  7669. phba->sli4_hba.max_cfg_param.vpi_base =
  7670. bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
  7671. phba->sli4_hba.max_cfg_param.max_rpi =
  7672. bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
  7673. phba->sli4_hba.max_cfg_param.rpi_base =
  7674. bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
  7675. phba->sli4_hba.max_cfg_param.max_vfi =
  7676. bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
  7677. phba->sli4_hba.max_cfg_param.vfi_base =
  7678. bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
  7679. phba->sli4_hba.max_cfg_param.max_fcfi =
  7680. bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
  7681. phba->sli4_hba.max_cfg_param.max_eq =
  7682. bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
  7683. phba->sli4_hba.max_cfg_param.max_rq =
  7684. bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
  7685. phba->sli4_hba.max_cfg_param.max_wq =
  7686. bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
  7687. phba->sli4_hba.max_cfg_param.max_cq =
  7688. bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
  7689. phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
  7690. phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
  7691. phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
  7692. phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
  7693. phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
  7694. (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
  7695. phba->max_vports = phba->max_vpi;
  7696. lpfc_map_topology(phba, rd_config);
  7697. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7698. "2003 cfg params Extents? %d "
  7699. "XRI(B:%d M:%d), "
  7700. "VPI(B:%d M:%d) "
  7701. "VFI(B:%d M:%d) "
  7702. "RPI(B:%d M:%d) "
  7703. "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
  7704. phba->sli4_hba.extents_in_use,
  7705. phba->sli4_hba.max_cfg_param.xri_base,
  7706. phba->sli4_hba.max_cfg_param.max_xri,
  7707. phba->sli4_hba.max_cfg_param.vpi_base,
  7708. phba->sli4_hba.max_cfg_param.max_vpi,
  7709. phba->sli4_hba.max_cfg_param.vfi_base,
  7710. phba->sli4_hba.max_cfg_param.max_vfi,
  7711. phba->sli4_hba.max_cfg_param.rpi_base,
  7712. phba->sli4_hba.max_cfg_param.max_rpi,
  7713. phba->sli4_hba.max_cfg_param.max_fcfi,
  7714. phba->sli4_hba.max_cfg_param.max_eq,
  7715. phba->sli4_hba.max_cfg_param.max_cq,
  7716. phba->sli4_hba.max_cfg_param.max_wq,
  7717. phba->sli4_hba.max_cfg_param.max_rq);
  7718. /*
  7719. * Calculate queue resources based on how
  7720. * many WQ/CQ/EQs are available.
  7721. */
  7722. qmin = phba->sli4_hba.max_cfg_param.max_wq;
  7723. if (phba->sli4_hba.max_cfg_param.max_cq < qmin)
  7724. qmin = phba->sli4_hba.max_cfg_param.max_cq;
  7725. if (phba->sli4_hba.max_cfg_param.max_eq < qmin)
  7726. qmin = phba->sli4_hba.max_cfg_param.max_eq;
  7727. /*
  7728. * Whats left after this can go toward NVME / FCP.
  7729. * The minus 4 accounts for ELS, NVME LS, MBOX
  7730. * plus one extra. When configured for
  7731. * NVMET, FCP io channel WQs are not created.
  7732. */
  7733. qmin -= 4;
  7734. /* Check to see if there is enough for NVME */
  7735. if ((phba->cfg_irq_chann > qmin) ||
  7736. (phba->cfg_hdw_queue > qmin)) {
  7737. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7738. "2005 Reducing Queues: "
  7739. "WQ %d CQ %d EQ %d: min %d: "
  7740. "IRQ %d HDWQ %d\n",
  7741. phba->sli4_hba.max_cfg_param.max_wq,
  7742. phba->sli4_hba.max_cfg_param.max_cq,
  7743. phba->sli4_hba.max_cfg_param.max_eq,
  7744. qmin, phba->cfg_irq_chann,
  7745. phba->cfg_hdw_queue);
  7746. if (phba->cfg_irq_chann > qmin)
  7747. phba->cfg_irq_chann = qmin;
  7748. if (phba->cfg_hdw_queue > qmin)
  7749. phba->cfg_hdw_queue = qmin;
  7750. }
  7751. }
  7752. if (rc)
  7753. goto read_cfg_out;
  7754. /* Update link speed if forced link speed is supported */
  7755. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  7756. if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
  7757. forced_link_speed =
  7758. bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
  7759. if (forced_link_speed) {
  7760. phba->hba_flag |= HBA_FORCED_LINK_SPEED;
  7761. switch (forced_link_speed) {
  7762. case LINK_SPEED_1G:
  7763. phba->cfg_link_speed =
  7764. LPFC_USER_LINK_SPEED_1G;
  7765. break;
  7766. case LINK_SPEED_2G:
  7767. phba->cfg_link_speed =
  7768. LPFC_USER_LINK_SPEED_2G;
  7769. break;
  7770. case LINK_SPEED_4G:
  7771. phba->cfg_link_speed =
  7772. LPFC_USER_LINK_SPEED_4G;
  7773. break;
  7774. case LINK_SPEED_8G:
  7775. phba->cfg_link_speed =
  7776. LPFC_USER_LINK_SPEED_8G;
  7777. break;
  7778. case LINK_SPEED_10G:
  7779. phba->cfg_link_speed =
  7780. LPFC_USER_LINK_SPEED_10G;
  7781. break;
  7782. case LINK_SPEED_16G:
  7783. phba->cfg_link_speed =
  7784. LPFC_USER_LINK_SPEED_16G;
  7785. break;
  7786. case LINK_SPEED_32G:
  7787. phba->cfg_link_speed =
  7788. LPFC_USER_LINK_SPEED_32G;
  7789. break;
  7790. case LINK_SPEED_64G:
  7791. phba->cfg_link_speed =
  7792. LPFC_USER_LINK_SPEED_64G;
  7793. break;
  7794. case 0xffff:
  7795. phba->cfg_link_speed =
  7796. LPFC_USER_LINK_SPEED_AUTO;
  7797. break;
  7798. default:
  7799. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7800. "0047 Unrecognized link "
  7801. "speed : %d\n",
  7802. forced_link_speed);
  7803. phba->cfg_link_speed =
  7804. LPFC_USER_LINK_SPEED_AUTO;
  7805. }
  7806. }
  7807. }
  7808. /* Reset the DFT_HBA_Q_DEPTH to the max xri */
  7809. length = phba->sli4_hba.max_cfg_param.max_xri -
  7810. lpfc_sli4_get_els_iocb_cnt(phba);
  7811. if (phba->cfg_hba_queue_depth > length) {
  7812. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  7813. "3361 HBA queue depth changed from %d to %d\n",
  7814. phba->cfg_hba_queue_depth, length);
  7815. phba->cfg_hba_queue_depth = length;
  7816. }
  7817. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
  7818. LPFC_SLI_INTF_IF_TYPE_2)
  7819. goto read_cfg_out;
  7820. /* get the pf# and vf# for SLI4 if_type 2 port */
  7821. length = (sizeof(struct lpfc_mbx_get_func_cfg) -
  7822. sizeof(struct lpfc_sli4_cfg_mhdr));
  7823. lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
  7824. LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
  7825. length, LPFC_SLI4_MBX_EMBED);
  7826. rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  7827. shdr = (union lpfc_sli4_cfg_shdr *)
  7828. &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
  7829. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  7830. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  7831. if (rc2 || shdr_status || shdr_add_status) {
  7832. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7833. "3026 Mailbox failed , mbxCmd x%x "
  7834. "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
  7835. bf_get(lpfc_mqe_command, &pmb->u.mqe),
  7836. bf_get(lpfc_mqe_status, &pmb->u.mqe));
  7837. goto read_cfg_out;
  7838. }
  7839. /* search for fc_fcoe resrouce descriptor */
  7840. get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
  7841. pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
  7842. desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
  7843. length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
  7844. if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
  7845. length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
  7846. else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
  7847. goto read_cfg_out;
  7848. for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
  7849. desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
  7850. if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
  7851. bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
  7852. phba->sli4_hba.iov.pf_number =
  7853. bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
  7854. phba->sli4_hba.iov.vf_number =
  7855. bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
  7856. break;
  7857. }
  7858. }
  7859. if (i < LPFC_RSRC_DESC_MAX_NUM)
  7860. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7861. "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
  7862. "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
  7863. phba->sli4_hba.iov.vf_number);
  7864. else
  7865. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7866. "3028 GET_FUNCTION_CONFIG: failed to find "
  7867. "Resource Descriptor:x%x\n",
  7868. LPFC_RSRC_DESC_TYPE_FCFCOE);
  7869. read_cfg_out:
  7870. mempool_free(pmb, phba->mbox_mem_pool);
  7871. return rc;
  7872. }
  7873. /**
  7874. * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
  7875. * @phba: pointer to lpfc hba data structure.
  7876. *
  7877. * This routine is invoked to setup the port-side endian order when
  7878. * the port if_type is 0. This routine has no function for other
  7879. * if_types.
  7880. *
  7881. * Return codes
  7882. * 0 - successful
  7883. * -ENOMEM - No available memory
  7884. * -EIO - The mailbox failed to complete successfully.
  7885. **/
  7886. static int
  7887. lpfc_setup_endian_order(struct lpfc_hba *phba)
  7888. {
  7889. LPFC_MBOXQ_t *mboxq;
  7890. uint32_t if_type, rc = 0;
  7891. uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
  7892. HOST_ENDIAN_HIGH_WORD1};
  7893. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  7894. switch (if_type) {
  7895. case LPFC_SLI_INTF_IF_TYPE_0:
  7896. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
  7897. GFP_KERNEL);
  7898. if (!mboxq) {
  7899. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7900. "0492 Unable to allocate memory for "
  7901. "issuing SLI_CONFIG_SPECIAL mailbox "
  7902. "command\n");
  7903. return -ENOMEM;
  7904. }
  7905. /*
  7906. * The SLI4_CONFIG_SPECIAL mailbox command requires the first
  7907. * two words to contain special data values and no other data.
  7908. */
  7909. memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
  7910. memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
  7911. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  7912. if (rc != MBX_SUCCESS) {
  7913. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7914. "0493 SLI_CONFIG_SPECIAL mailbox "
  7915. "failed with status x%x\n",
  7916. rc);
  7917. rc = -EIO;
  7918. }
  7919. mempool_free(mboxq, phba->mbox_mem_pool);
  7920. break;
  7921. case LPFC_SLI_INTF_IF_TYPE_6:
  7922. case LPFC_SLI_INTF_IF_TYPE_2:
  7923. case LPFC_SLI_INTF_IF_TYPE_1:
  7924. default:
  7925. break;
  7926. }
  7927. return rc;
  7928. }
  7929. /**
  7930. * lpfc_sli4_queue_verify - Verify and update EQ counts
  7931. * @phba: pointer to lpfc hba data structure.
  7932. *
  7933. * This routine is invoked to check the user settable queue counts for EQs.
  7934. * After this routine is called the counts will be set to valid values that
  7935. * adhere to the constraints of the system's interrupt vectors and the port's
  7936. * queue resources.
  7937. *
  7938. * Return codes
  7939. * 0 - successful
  7940. * -ENOMEM - No available memory
  7941. **/
  7942. static int
  7943. lpfc_sli4_queue_verify(struct lpfc_hba *phba)
  7944. {
  7945. /*
  7946. * Sanity check for configured queue parameters against the run-time
  7947. * device parameters
  7948. */
  7949. if (phba->nvmet_support) {
  7950. if (phba->cfg_hdw_queue < phba->cfg_nvmet_mrq)
  7951. phba->cfg_nvmet_mrq = phba->cfg_hdw_queue;
  7952. if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
  7953. phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
  7954. }
  7955. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7956. "2574 IO channels: hdwQ %d IRQ %d MRQ: %d\n",
  7957. phba->cfg_hdw_queue, phba->cfg_irq_chann,
  7958. phba->cfg_nvmet_mrq);
  7959. /* Get EQ depth from module parameter, fake the default for now */
  7960. phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
  7961. phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
  7962. /* Get CQ depth from module parameter, fake the default for now */
  7963. phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
  7964. phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
  7965. return 0;
  7966. }
  7967. static int
  7968. lpfc_alloc_io_wq_cq(struct lpfc_hba *phba, int idx)
  7969. {
  7970. struct lpfc_queue *qdesc;
  7971. u32 wqesize;
  7972. int cpu;
  7973. cpu = lpfc_find_cpu_handle(phba, idx, LPFC_FIND_BY_HDWQ);
  7974. /* Create Fast Path IO CQs */
  7975. if (phba->enab_exp_wqcq_pages)
  7976. /* Increase the CQ size when WQEs contain an embedded cdb */
  7977. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
  7978. phba->sli4_hba.cq_esize,
  7979. LPFC_CQE_EXP_COUNT, cpu);
  7980. else
  7981. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  7982. phba->sli4_hba.cq_esize,
  7983. phba->sli4_hba.cq_ecount, cpu);
  7984. if (!qdesc) {
  7985. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7986. "0499 Failed allocate fast-path IO CQ (%d)\n", idx);
  7987. return 1;
  7988. }
  7989. qdesc->qe_valid = 1;
  7990. qdesc->hdwq = idx;
  7991. qdesc->chann = cpu;
  7992. phba->sli4_hba.hdwq[idx].io_cq = qdesc;
  7993. /* Create Fast Path IO WQs */
  7994. if (phba->enab_exp_wqcq_pages) {
  7995. /* Increase the WQ size when WQEs contain an embedded cdb */
  7996. wqesize = (phba->fcp_embed_io) ?
  7997. LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
  7998. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
  7999. wqesize,
  8000. LPFC_WQE_EXP_COUNT, cpu);
  8001. } else
  8002. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  8003. phba->sli4_hba.wq_esize,
  8004. phba->sli4_hba.wq_ecount, cpu);
  8005. if (!qdesc) {
  8006. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8007. "0503 Failed allocate fast-path IO WQ (%d)\n",
  8008. idx);
  8009. return 1;
  8010. }
  8011. qdesc->hdwq = idx;
  8012. qdesc->chann = cpu;
  8013. phba->sli4_hba.hdwq[idx].io_wq = qdesc;
  8014. list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
  8015. return 0;
  8016. }
  8017. /**
  8018. * lpfc_sli4_queue_create - Create all the SLI4 queues
  8019. * @phba: pointer to lpfc hba data structure.
  8020. *
  8021. * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
  8022. * operation. For each SLI4 queue type, the parameters such as queue entry
  8023. * count (queue depth) shall be taken from the module parameter. For now,
  8024. * we just use some constant number as place holder.
  8025. *
  8026. * Return codes
  8027. * 0 - successful
  8028. * -ENOMEM - No availble memory
  8029. * -EIO - The mailbox failed to complete successfully.
  8030. **/
  8031. int
  8032. lpfc_sli4_queue_create(struct lpfc_hba *phba)
  8033. {
  8034. struct lpfc_queue *qdesc;
  8035. int idx, cpu, eqcpu;
  8036. struct lpfc_sli4_hdw_queue *qp;
  8037. struct lpfc_vector_map_info *cpup;
  8038. struct lpfc_vector_map_info *eqcpup;
  8039. struct lpfc_eq_intr_info *eqi;
  8040. /*
  8041. * Create HBA Record arrays.
  8042. * Both NVME and FCP will share that same vectors / EQs
  8043. */
  8044. phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
  8045. phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
  8046. phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
  8047. phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
  8048. phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
  8049. phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
  8050. phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
  8051. phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
  8052. phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
  8053. phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
  8054. if (!phba->sli4_hba.hdwq) {
  8055. phba->sli4_hba.hdwq = kcalloc(
  8056. phba->cfg_hdw_queue, sizeof(struct lpfc_sli4_hdw_queue),
  8057. GFP_KERNEL);
  8058. if (!phba->sli4_hba.hdwq) {
  8059. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8060. "6427 Failed allocate memory for "
  8061. "fast-path Hardware Queue array\n");
  8062. goto out_error;
  8063. }
  8064. /* Prepare hardware queues to take IO buffers */
  8065. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  8066. qp = &phba->sli4_hba.hdwq[idx];
  8067. spin_lock_init(&qp->io_buf_list_get_lock);
  8068. spin_lock_init(&qp->io_buf_list_put_lock);
  8069. INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
  8070. INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
  8071. qp->get_io_bufs = 0;
  8072. qp->put_io_bufs = 0;
  8073. qp->total_io_bufs = 0;
  8074. spin_lock_init(&qp->abts_io_buf_list_lock);
  8075. INIT_LIST_HEAD(&qp->lpfc_abts_io_buf_list);
  8076. qp->abts_scsi_io_bufs = 0;
  8077. qp->abts_nvme_io_bufs = 0;
  8078. INIT_LIST_HEAD(&qp->sgl_list);
  8079. INIT_LIST_HEAD(&qp->cmd_rsp_buf_list);
  8080. spin_lock_init(&qp->hdwq_lock);
  8081. }
  8082. }
  8083. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  8084. if (phba->nvmet_support) {
  8085. phba->sli4_hba.nvmet_cqset = kcalloc(
  8086. phba->cfg_nvmet_mrq,
  8087. sizeof(struct lpfc_queue *),
  8088. GFP_KERNEL);
  8089. if (!phba->sli4_hba.nvmet_cqset) {
  8090. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8091. "3121 Fail allocate memory for "
  8092. "fast-path CQ set array\n");
  8093. goto out_error;
  8094. }
  8095. phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
  8096. phba->cfg_nvmet_mrq,
  8097. sizeof(struct lpfc_queue *),
  8098. GFP_KERNEL);
  8099. if (!phba->sli4_hba.nvmet_mrq_hdr) {
  8100. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8101. "3122 Fail allocate memory for "
  8102. "fast-path RQ set hdr array\n");
  8103. goto out_error;
  8104. }
  8105. phba->sli4_hba.nvmet_mrq_data = kcalloc(
  8106. phba->cfg_nvmet_mrq,
  8107. sizeof(struct lpfc_queue *),
  8108. GFP_KERNEL);
  8109. if (!phba->sli4_hba.nvmet_mrq_data) {
  8110. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8111. "3124 Fail allocate memory for "
  8112. "fast-path RQ set data array\n");
  8113. goto out_error;
  8114. }
  8115. }
  8116. }
  8117. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
  8118. /* Create HBA Event Queues (EQs) */
  8119. for_each_present_cpu(cpu) {
  8120. /* We only want to create 1 EQ per vector, even though
  8121. * multiple CPUs might be using that vector. so only
  8122. * selects the CPUs that are LPFC_CPU_FIRST_IRQ.
  8123. */
  8124. cpup = &phba->sli4_hba.cpu_map[cpu];
  8125. if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
  8126. continue;
  8127. /* Get a ptr to the Hardware Queue associated with this CPU */
  8128. qp = &phba->sli4_hba.hdwq[cpup->hdwq];
  8129. /* Allocate an EQ */
  8130. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  8131. phba->sli4_hba.eq_esize,
  8132. phba->sli4_hba.eq_ecount, cpu);
  8133. if (!qdesc) {
  8134. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8135. "0497 Failed allocate EQ (%d)\n",
  8136. cpup->hdwq);
  8137. goto out_error;
  8138. }
  8139. qdesc->qe_valid = 1;
  8140. qdesc->hdwq = cpup->hdwq;
  8141. qdesc->chann = cpu; /* First CPU this EQ is affinitized to */
  8142. qdesc->last_cpu = qdesc->chann;
  8143. /* Save the allocated EQ in the Hardware Queue */
  8144. qp->hba_eq = qdesc;
  8145. eqi = per_cpu_ptr(phba->sli4_hba.eq_info, qdesc->last_cpu);
  8146. list_add(&qdesc->cpu_list, &eqi->list);
  8147. }
  8148. /* Now we need to populate the other Hardware Queues, that share
  8149. * an IRQ vector, with the associated EQ ptr.
  8150. */
  8151. for_each_present_cpu(cpu) {
  8152. cpup = &phba->sli4_hba.cpu_map[cpu];
  8153. /* Check for EQ already allocated in previous loop */
  8154. if (cpup->flag & LPFC_CPU_FIRST_IRQ)
  8155. continue;
  8156. /* Check for multiple CPUs per hdwq */
  8157. qp = &phba->sli4_hba.hdwq[cpup->hdwq];
  8158. if (qp->hba_eq)
  8159. continue;
  8160. /* We need to share an EQ for this hdwq */
  8161. eqcpu = lpfc_find_cpu_handle(phba, cpup->eq, LPFC_FIND_BY_EQ);
  8162. eqcpup = &phba->sli4_hba.cpu_map[eqcpu];
  8163. qp->hba_eq = phba->sli4_hba.hdwq[eqcpup->hdwq].hba_eq;
  8164. }
  8165. /* Allocate IO Path SLI4 CQ/WQs */
  8166. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  8167. if (lpfc_alloc_io_wq_cq(phba, idx))
  8168. goto out_error;
  8169. }
  8170. if (phba->nvmet_support) {
  8171. for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
  8172. cpu = lpfc_find_cpu_handle(phba, idx,
  8173. LPFC_FIND_BY_HDWQ);
  8174. qdesc = lpfc_sli4_queue_alloc(phba,
  8175. LPFC_DEFAULT_PAGE_SIZE,
  8176. phba->sli4_hba.cq_esize,
  8177. phba->sli4_hba.cq_ecount,
  8178. cpu);
  8179. if (!qdesc) {
  8180. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8181. "3142 Failed allocate NVME "
  8182. "CQ Set (%d)\n", idx);
  8183. goto out_error;
  8184. }
  8185. qdesc->qe_valid = 1;
  8186. qdesc->hdwq = idx;
  8187. qdesc->chann = cpu;
  8188. phba->sli4_hba.nvmet_cqset[idx] = qdesc;
  8189. }
  8190. }
  8191. /*
  8192. * Create Slow Path Completion Queues (CQs)
  8193. */
  8194. cpu = lpfc_find_cpu_handle(phba, 0, LPFC_FIND_BY_EQ);
  8195. /* Create slow-path Mailbox Command Complete Queue */
  8196. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  8197. phba->sli4_hba.cq_esize,
  8198. phba->sli4_hba.cq_ecount, cpu);
  8199. if (!qdesc) {
  8200. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8201. "0500 Failed allocate slow-path mailbox CQ\n");
  8202. goto out_error;
  8203. }
  8204. qdesc->qe_valid = 1;
  8205. phba->sli4_hba.mbx_cq = qdesc;
  8206. /* Create slow-path ELS Complete Queue */
  8207. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  8208. phba->sli4_hba.cq_esize,
  8209. phba->sli4_hba.cq_ecount, cpu);
  8210. if (!qdesc) {
  8211. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8212. "0501 Failed allocate slow-path ELS CQ\n");
  8213. goto out_error;
  8214. }
  8215. qdesc->qe_valid = 1;
  8216. qdesc->chann = cpu;
  8217. phba->sli4_hba.els_cq = qdesc;
  8218. /*
  8219. * Create Slow Path Work Queues (WQs)
  8220. */
  8221. /* Create Mailbox Command Queue */
  8222. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  8223. phba->sli4_hba.mq_esize,
  8224. phba->sli4_hba.mq_ecount, cpu);
  8225. if (!qdesc) {
  8226. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8227. "0505 Failed allocate slow-path MQ\n");
  8228. goto out_error;
  8229. }
  8230. qdesc->chann = cpu;
  8231. phba->sli4_hba.mbx_wq = qdesc;
  8232. /*
  8233. * Create ELS Work Queues
  8234. */
  8235. /* Create slow-path ELS Work Queue */
  8236. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  8237. phba->sli4_hba.wq_esize,
  8238. phba->sli4_hba.wq_ecount, cpu);
  8239. if (!qdesc) {
  8240. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8241. "0504 Failed allocate slow-path ELS WQ\n");
  8242. goto out_error;
  8243. }
  8244. qdesc->chann = cpu;
  8245. phba->sli4_hba.els_wq = qdesc;
  8246. list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
  8247. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  8248. /* Create NVME LS Complete Queue */
  8249. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  8250. phba->sli4_hba.cq_esize,
  8251. phba->sli4_hba.cq_ecount, cpu);
  8252. if (!qdesc) {
  8253. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8254. "6079 Failed allocate NVME LS CQ\n");
  8255. goto out_error;
  8256. }
  8257. qdesc->chann = cpu;
  8258. qdesc->qe_valid = 1;
  8259. phba->sli4_hba.nvmels_cq = qdesc;
  8260. /* Create NVME LS Work Queue */
  8261. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  8262. phba->sli4_hba.wq_esize,
  8263. phba->sli4_hba.wq_ecount, cpu);
  8264. if (!qdesc) {
  8265. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8266. "6080 Failed allocate NVME LS WQ\n");
  8267. goto out_error;
  8268. }
  8269. qdesc->chann = cpu;
  8270. phba->sli4_hba.nvmels_wq = qdesc;
  8271. list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
  8272. }
  8273. /*
  8274. * Create Receive Queue (RQ)
  8275. */
  8276. /* Create Receive Queue for header */
  8277. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  8278. phba->sli4_hba.rq_esize,
  8279. phba->sli4_hba.rq_ecount, cpu);
  8280. if (!qdesc) {
  8281. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8282. "0506 Failed allocate receive HRQ\n");
  8283. goto out_error;
  8284. }
  8285. phba->sli4_hba.hdr_rq = qdesc;
  8286. /* Create Receive Queue for data */
  8287. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  8288. phba->sli4_hba.rq_esize,
  8289. phba->sli4_hba.rq_ecount, cpu);
  8290. if (!qdesc) {
  8291. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8292. "0507 Failed allocate receive DRQ\n");
  8293. goto out_error;
  8294. }
  8295. phba->sli4_hba.dat_rq = qdesc;
  8296. if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
  8297. phba->nvmet_support) {
  8298. for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
  8299. cpu = lpfc_find_cpu_handle(phba, idx,
  8300. LPFC_FIND_BY_HDWQ);
  8301. /* Create NVMET Receive Queue for header */
  8302. qdesc = lpfc_sli4_queue_alloc(phba,
  8303. LPFC_DEFAULT_PAGE_SIZE,
  8304. phba->sli4_hba.rq_esize,
  8305. LPFC_NVMET_RQE_DEF_COUNT,
  8306. cpu);
  8307. if (!qdesc) {
  8308. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8309. "3146 Failed allocate "
  8310. "receive HRQ\n");
  8311. goto out_error;
  8312. }
  8313. qdesc->hdwq = idx;
  8314. phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
  8315. /* Only needed for header of RQ pair */
  8316. qdesc->rqbp = kzalloc_node(sizeof(*qdesc->rqbp),
  8317. GFP_KERNEL,
  8318. cpu_to_node(cpu));
  8319. if (qdesc->rqbp == NULL) {
  8320. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8321. "6131 Failed allocate "
  8322. "Header RQBP\n");
  8323. goto out_error;
  8324. }
  8325. /* Put list in known state in case driver load fails. */
  8326. INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
  8327. /* Create NVMET Receive Queue for data */
  8328. qdesc = lpfc_sli4_queue_alloc(phba,
  8329. LPFC_DEFAULT_PAGE_SIZE,
  8330. phba->sli4_hba.rq_esize,
  8331. LPFC_NVMET_RQE_DEF_COUNT,
  8332. cpu);
  8333. if (!qdesc) {
  8334. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8335. "3156 Failed allocate "
  8336. "receive DRQ\n");
  8337. goto out_error;
  8338. }
  8339. qdesc->hdwq = idx;
  8340. phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
  8341. }
  8342. }
  8343. /* Clear NVME stats */
  8344. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  8345. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  8346. memset(&phba->sli4_hba.hdwq[idx].nvme_cstat, 0,
  8347. sizeof(phba->sli4_hba.hdwq[idx].nvme_cstat));
  8348. }
  8349. }
  8350. /* Clear SCSI stats */
  8351. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
  8352. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  8353. memset(&phba->sli4_hba.hdwq[idx].scsi_cstat, 0,
  8354. sizeof(phba->sli4_hba.hdwq[idx].scsi_cstat));
  8355. }
  8356. }
  8357. return 0;
  8358. out_error:
  8359. lpfc_sli4_queue_destroy(phba);
  8360. return -ENOMEM;
  8361. }
  8362. static inline void
  8363. __lpfc_sli4_release_queue(struct lpfc_queue **qp)
  8364. {
  8365. if (*qp != NULL) {
  8366. lpfc_sli4_queue_free(*qp);
  8367. *qp = NULL;
  8368. }
  8369. }
  8370. static inline void
  8371. lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
  8372. {
  8373. int idx;
  8374. if (*qs == NULL)
  8375. return;
  8376. for (idx = 0; idx < max; idx++)
  8377. __lpfc_sli4_release_queue(&(*qs)[idx]);
  8378. kfree(*qs);
  8379. *qs = NULL;
  8380. }
  8381. static inline void
  8382. lpfc_sli4_release_hdwq(struct lpfc_hba *phba)
  8383. {
  8384. struct lpfc_sli4_hdw_queue *hdwq;
  8385. struct lpfc_queue *eq;
  8386. uint32_t idx;
  8387. hdwq = phba->sli4_hba.hdwq;
  8388. /* Loop thru all Hardware Queues */
  8389. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  8390. /* Free the CQ/WQ corresponding to the Hardware Queue */
  8391. lpfc_sli4_queue_free(hdwq[idx].io_cq);
  8392. lpfc_sli4_queue_free(hdwq[idx].io_wq);
  8393. hdwq[idx].hba_eq = NULL;
  8394. hdwq[idx].io_cq = NULL;
  8395. hdwq[idx].io_wq = NULL;
  8396. if (phba->cfg_xpsgl && !phba->nvmet_support)
  8397. lpfc_free_sgl_per_hdwq(phba, &hdwq[idx]);
  8398. lpfc_free_cmd_rsp_buf_per_hdwq(phba, &hdwq[idx]);
  8399. }
  8400. /* Loop thru all IRQ vectors */
  8401. for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
  8402. /* Free the EQ corresponding to the IRQ vector */
  8403. eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
  8404. lpfc_sli4_queue_free(eq);
  8405. phba->sli4_hba.hba_eq_hdl[idx].eq = NULL;
  8406. }
  8407. }
  8408. /**
  8409. * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
  8410. * @phba: pointer to lpfc hba data structure.
  8411. *
  8412. * This routine is invoked to release all the SLI4 queues with the FCoE HBA
  8413. * operation.
  8414. *
  8415. * Return codes
  8416. * 0 - successful
  8417. * -ENOMEM - No available memory
  8418. * -EIO - The mailbox failed to complete successfully.
  8419. **/
  8420. void
  8421. lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
  8422. {
  8423. /*
  8424. * Set FREE_INIT before beginning to free the queues.
  8425. * Wait until the users of queues to acknowledge to
  8426. * release queues by clearing FREE_WAIT.
  8427. */
  8428. spin_lock_irq(&phba->hbalock);
  8429. phba->sli.sli_flag |= LPFC_QUEUE_FREE_INIT;
  8430. while (phba->sli.sli_flag & LPFC_QUEUE_FREE_WAIT) {
  8431. spin_unlock_irq(&phba->hbalock);
  8432. msleep(20);
  8433. spin_lock_irq(&phba->hbalock);
  8434. }
  8435. spin_unlock_irq(&phba->hbalock);
  8436. lpfc_sli4_cleanup_poll_list(phba);
  8437. /* Release HBA eqs */
  8438. if (phba->sli4_hba.hdwq)
  8439. lpfc_sli4_release_hdwq(phba);
  8440. if (phba->nvmet_support) {
  8441. lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
  8442. phba->cfg_nvmet_mrq);
  8443. lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
  8444. phba->cfg_nvmet_mrq);
  8445. lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
  8446. phba->cfg_nvmet_mrq);
  8447. }
  8448. /* Release mailbox command work queue */
  8449. __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
  8450. /* Release ELS work queue */
  8451. __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
  8452. /* Release ELS work queue */
  8453. __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
  8454. /* Release unsolicited receive queue */
  8455. __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
  8456. __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
  8457. /* Release ELS complete queue */
  8458. __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
  8459. /* Release NVME LS complete queue */
  8460. __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
  8461. /* Release mailbox command complete queue */
  8462. __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
  8463. /* Everything on this list has been freed */
  8464. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
  8465. /* Done with freeing the queues */
  8466. spin_lock_irq(&phba->hbalock);
  8467. phba->sli.sli_flag &= ~LPFC_QUEUE_FREE_INIT;
  8468. spin_unlock_irq(&phba->hbalock);
  8469. }
  8470. int
  8471. lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
  8472. {
  8473. struct lpfc_rqb *rqbp;
  8474. struct lpfc_dmabuf *h_buf;
  8475. struct rqb_dmabuf *rqb_buffer;
  8476. rqbp = rq->rqbp;
  8477. while (!list_empty(&rqbp->rqb_buffer_list)) {
  8478. list_remove_head(&rqbp->rqb_buffer_list, h_buf,
  8479. struct lpfc_dmabuf, list);
  8480. rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
  8481. (rqbp->rqb_free_buffer)(phba, rqb_buffer);
  8482. rqbp->buffer_count--;
  8483. }
  8484. return 1;
  8485. }
  8486. static int
  8487. lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
  8488. struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
  8489. int qidx, uint32_t qtype)
  8490. {
  8491. struct lpfc_sli_ring *pring;
  8492. int rc;
  8493. if (!eq || !cq || !wq) {
  8494. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8495. "6085 Fast-path %s (%d) not allocated\n",
  8496. ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
  8497. return -ENOMEM;
  8498. }
  8499. /* create the Cq first */
  8500. rc = lpfc_cq_create(phba, cq, eq,
  8501. (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
  8502. if (rc) {
  8503. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8504. "6086 Failed setup of CQ (%d), rc = 0x%x\n",
  8505. qidx, (uint32_t)rc);
  8506. return rc;
  8507. }
  8508. if (qtype != LPFC_MBOX) {
  8509. /* Setup cq_map for fast lookup */
  8510. if (cq_map)
  8511. *cq_map = cq->queue_id;
  8512. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8513. "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
  8514. qidx, cq->queue_id, qidx, eq->queue_id);
  8515. /* create the wq */
  8516. rc = lpfc_wq_create(phba, wq, cq, qtype);
  8517. if (rc) {
  8518. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8519. "4618 Fail setup fastpath WQ (%d), rc = 0x%x\n",
  8520. qidx, (uint32_t)rc);
  8521. /* no need to tear down cq - caller will do so */
  8522. return rc;
  8523. }
  8524. /* Bind this CQ/WQ to the NVME ring */
  8525. pring = wq->pring;
  8526. pring->sli.sli4.wqp = (void *)wq;
  8527. cq->pring = pring;
  8528. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8529. "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
  8530. qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
  8531. } else {
  8532. rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
  8533. if (rc) {
  8534. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8535. "0539 Failed setup of slow-path MQ: "
  8536. "rc = 0x%x\n", rc);
  8537. /* no need to tear down cq - caller will do so */
  8538. return rc;
  8539. }
  8540. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8541. "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
  8542. phba->sli4_hba.mbx_wq->queue_id,
  8543. phba->sli4_hba.mbx_cq->queue_id);
  8544. }
  8545. return 0;
  8546. }
  8547. /**
  8548. * lpfc_setup_cq_lookup - Setup the CQ lookup table
  8549. * @phba: pointer to lpfc hba data structure.
  8550. *
  8551. * This routine will populate the cq_lookup table by all
  8552. * available CQ queue_id's.
  8553. **/
  8554. static void
  8555. lpfc_setup_cq_lookup(struct lpfc_hba *phba)
  8556. {
  8557. struct lpfc_queue *eq, *childq;
  8558. int qidx;
  8559. memset(phba->sli4_hba.cq_lookup, 0,
  8560. (sizeof(struct lpfc_queue *) * (phba->sli4_hba.cq_max + 1)));
  8561. /* Loop thru all IRQ vectors */
  8562. for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
  8563. /* Get the EQ corresponding to the IRQ vector */
  8564. eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
  8565. if (!eq)
  8566. continue;
  8567. /* Loop through all CQs associated with that EQ */
  8568. list_for_each_entry(childq, &eq->child_list, list) {
  8569. if (childq->queue_id > phba->sli4_hba.cq_max)
  8570. continue;
  8571. if (childq->subtype == LPFC_IO)
  8572. phba->sli4_hba.cq_lookup[childq->queue_id] =
  8573. childq;
  8574. }
  8575. }
  8576. }
  8577. /**
  8578. * lpfc_sli4_queue_setup - Set up all the SLI4 queues
  8579. * @phba: pointer to lpfc hba data structure.
  8580. *
  8581. * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
  8582. * operation.
  8583. *
  8584. * Return codes
  8585. * 0 - successful
  8586. * -ENOMEM - No available memory
  8587. * -EIO - The mailbox failed to complete successfully.
  8588. **/
  8589. int
  8590. lpfc_sli4_queue_setup(struct lpfc_hba *phba)
  8591. {
  8592. uint32_t shdr_status, shdr_add_status;
  8593. union lpfc_sli4_cfg_shdr *shdr;
  8594. struct lpfc_vector_map_info *cpup;
  8595. struct lpfc_sli4_hdw_queue *qp;
  8596. LPFC_MBOXQ_t *mboxq;
  8597. int qidx, cpu;
  8598. uint32_t length, usdelay;
  8599. int rc = -ENOMEM;
  8600. /* Check for dual-ULP support */
  8601. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8602. if (!mboxq) {
  8603. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8604. "3249 Unable to allocate memory for "
  8605. "QUERY_FW_CFG mailbox command\n");
  8606. return -ENOMEM;
  8607. }
  8608. length = (sizeof(struct lpfc_mbx_query_fw_config) -
  8609. sizeof(struct lpfc_sli4_cfg_mhdr));
  8610. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  8611. LPFC_MBOX_OPCODE_QUERY_FW_CFG,
  8612. length, LPFC_SLI4_MBX_EMBED);
  8613. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  8614. shdr = (union lpfc_sli4_cfg_shdr *)
  8615. &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
  8616. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  8617. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  8618. if (shdr_status || shdr_add_status || rc) {
  8619. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8620. "3250 QUERY_FW_CFG mailbox failed with status "
  8621. "x%x add_status x%x, mbx status x%x\n",
  8622. shdr_status, shdr_add_status, rc);
  8623. if (rc != MBX_TIMEOUT)
  8624. mempool_free(mboxq, phba->mbox_mem_pool);
  8625. rc = -ENXIO;
  8626. goto out_error;
  8627. }
  8628. phba->sli4_hba.fw_func_mode =
  8629. mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
  8630. phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
  8631. phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
  8632. phba->sli4_hba.physical_port =
  8633. mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
  8634. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8635. "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
  8636. "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
  8637. phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
  8638. if (rc != MBX_TIMEOUT)
  8639. mempool_free(mboxq, phba->mbox_mem_pool);
  8640. /*
  8641. * Set up HBA Event Queues (EQs)
  8642. */
  8643. qp = phba->sli4_hba.hdwq;
  8644. /* Set up HBA event queue */
  8645. if (!qp) {
  8646. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8647. "3147 Fast-path EQs not allocated\n");
  8648. rc = -ENOMEM;
  8649. goto out_error;
  8650. }
  8651. /* Loop thru all IRQ vectors */
  8652. for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
  8653. /* Create HBA Event Queues (EQs) in order */
  8654. for_each_present_cpu(cpu) {
  8655. cpup = &phba->sli4_hba.cpu_map[cpu];
  8656. /* Look for the CPU thats using that vector with
  8657. * LPFC_CPU_FIRST_IRQ set.
  8658. */
  8659. if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
  8660. continue;
  8661. if (qidx != cpup->eq)
  8662. continue;
  8663. /* Create an EQ for that vector */
  8664. rc = lpfc_eq_create(phba, qp[cpup->hdwq].hba_eq,
  8665. phba->cfg_fcp_imax);
  8666. if (rc) {
  8667. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8668. "0523 Failed setup of fast-path"
  8669. " EQ (%d), rc = 0x%x\n",
  8670. cpup->eq, (uint32_t)rc);
  8671. goto out_destroy;
  8672. }
  8673. /* Save the EQ for that vector in the hba_eq_hdl */
  8674. phba->sli4_hba.hba_eq_hdl[cpup->eq].eq =
  8675. qp[cpup->hdwq].hba_eq;
  8676. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8677. "2584 HBA EQ setup: queue[%d]-id=%d\n",
  8678. cpup->eq,
  8679. qp[cpup->hdwq].hba_eq->queue_id);
  8680. }
  8681. }
  8682. /* Loop thru all Hardware Queues */
  8683. for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
  8684. cpu = lpfc_find_cpu_handle(phba, qidx, LPFC_FIND_BY_HDWQ);
  8685. cpup = &phba->sli4_hba.cpu_map[cpu];
  8686. /* Create the CQ/WQ corresponding to the Hardware Queue */
  8687. rc = lpfc_create_wq_cq(phba,
  8688. phba->sli4_hba.hdwq[cpup->hdwq].hba_eq,
  8689. qp[qidx].io_cq,
  8690. qp[qidx].io_wq,
  8691. &phba->sli4_hba.hdwq[qidx].io_cq_map,
  8692. qidx,
  8693. LPFC_IO);
  8694. if (rc) {
  8695. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8696. "0535 Failed to setup fastpath "
  8697. "IO WQ/CQ (%d), rc = 0x%x\n",
  8698. qidx, (uint32_t)rc);
  8699. goto out_destroy;
  8700. }
  8701. }
  8702. /*
  8703. * Set up Slow Path Complete Queues (CQs)
  8704. */
  8705. /* Set up slow-path MBOX CQ/MQ */
  8706. if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
  8707. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8708. "0528 %s not allocated\n",
  8709. phba->sli4_hba.mbx_cq ?
  8710. "Mailbox WQ" : "Mailbox CQ");
  8711. rc = -ENOMEM;
  8712. goto out_destroy;
  8713. }
  8714. rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
  8715. phba->sli4_hba.mbx_cq,
  8716. phba->sli4_hba.mbx_wq,
  8717. NULL, 0, LPFC_MBOX);
  8718. if (rc) {
  8719. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8720. "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
  8721. (uint32_t)rc);
  8722. goto out_destroy;
  8723. }
  8724. if (phba->nvmet_support) {
  8725. if (!phba->sli4_hba.nvmet_cqset) {
  8726. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8727. "3165 Fast-path NVME CQ Set "
  8728. "array not allocated\n");
  8729. rc = -ENOMEM;
  8730. goto out_destroy;
  8731. }
  8732. if (phba->cfg_nvmet_mrq > 1) {
  8733. rc = lpfc_cq_create_set(phba,
  8734. phba->sli4_hba.nvmet_cqset,
  8735. qp,
  8736. LPFC_WCQ, LPFC_NVMET);
  8737. if (rc) {
  8738. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8739. "3164 Failed setup of NVME CQ "
  8740. "Set, rc = 0x%x\n",
  8741. (uint32_t)rc);
  8742. goto out_destroy;
  8743. }
  8744. } else {
  8745. /* Set up NVMET Receive Complete Queue */
  8746. rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
  8747. qp[0].hba_eq,
  8748. LPFC_WCQ, LPFC_NVMET);
  8749. if (rc) {
  8750. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8751. "6089 Failed setup NVMET CQ: "
  8752. "rc = 0x%x\n", (uint32_t)rc);
  8753. goto out_destroy;
  8754. }
  8755. phba->sli4_hba.nvmet_cqset[0]->chann = 0;
  8756. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8757. "6090 NVMET CQ setup: cq-id=%d, "
  8758. "parent eq-id=%d\n",
  8759. phba->sli4_hba.nvmet_cqset[0]->queue_id,
  8760. qp[0].hba_eq->queue_id);
  8761. }
  8762. }
  8763. /* Set up slow-path ELS WQ/CQ */
  8764. if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
  8765. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8766. "0530 ELS %s not allocated\n",
  8767. phba->sli4_hba.els_cq ? "WQ" : "CQ");
  8768. rc = -ENOMEM;
  8769. goto out_destroy;
  8770. }
  8771. rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
  8772. phba->sli4_hba.els_cq,
  8773. phba->sli4_hba.els_wq,
  8774. NULL, 0, LPFC_ELS);
  8775. if (rc) {
  8776. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8777. "0525 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
  8778. (uint32_t)rc);
  8779. goto out_destroy;
  8780. }
  8781. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8782. "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
  8783. phba->sli4_hba.els_wq->queue_id,
  8784. phba->sli4_hba.els_cq->queue_id);
  8785. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  8786. /* Set up NVME LS Complete Queue */
  8787. if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
  8788. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8789. "6091 LS %s not allocated\n",
  8790. phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
  8791. rc = -ENOMEM;
  8792. goto out_destroy;
  8793. }
  8794. rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
  8795. phba->sli4_hba.nvmels_cq,
  8796. phba->sli4_hba.nvmels_wq,
  8797. NULL, 0, LPFC_NVME_LS);
  8798. if (rc) {
  8799. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8800. "0526 Failed setup of NVVME LS WQ/CQ: "
  8801. "rc = 0x%x\n", (uint32_t)rc);
  8802. goto out_destroy;
  8803. }
  8804. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8805. "6096 ELS WQ setup: wq-id=%d, "
  8806. "parent cq-id=%d\n",
  8807. phba->sli4_hba.nvmels_wq->queue_id,
  8808. phba->sli4_hba.nvmels_cq->queue_id);
  8809. }
  8810. /*
  8811. * Create NVMET Receive Queue (RQ)
  8812. */
  8813. if (phba->nvmet_support) {
  8814. if ((!phba->sli4_hba.nvmet_cqset) ||
  8815. (!phba->sli4_hba.nvmet_mrq_hdr) ||
  8816. (!phba->sli4_hba.nvmet_mrq_data)) {
  8817. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8818. "6130 MRQ CQ Queues not "
  8819. "allocated\n");
  8820. rc = -ENOMEM;
  8821. goto out_destroy;
  8822. }
  8823. if (phba->cfg_nvmet_mrq > 1) {
  8824. rc = lpfc_mrq_create(phba,
  8825. phba->sli4_hba.nvmet_mrq_hdr,
  8826. phba->sli4_hba.nvmet_mrq_data,
  8827. phba->sli4_hba.nvmet_cqset,
  8828. LPFC_NVMET);
  8829. if (rc) {
  8830. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8831. "6098 Failed setup of NVMET "
  8832. "MRQ: rc = 0x%x\n",
  8833. (uint32_t)rc);
  8834. goto out_destroy;
  8835. }
  8836. } else {
  8837. rc = lpfc_rq_create(phba,
  8838. phba->sli4_hba.nvmet_mrq_hdr[0],
  8839. phba->sli4_hba.nvmet_mrq_data[0],
  8840. phba->sli4_hba.nvmet_cqset[0],
  8841. LPFC_NVMET);
  8842. if (rc) {
  8843. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8844. "6057 Failed setup of NVMET "
  8845. "Receive Queue: rc = 0x%x\n",
  8846. (uint32_t)rc);
  8847. goto out_destroy;
  8848. }
  8849. lpfc_printf_log(
  8850. phba, KERN_INFO, LOG_INIT,
  8851. "6099 NVMET RQ setup: hdr-rq-id=%d, "
  8852. "dat-rq-id=%d parent cq-id=%d\n",
  8853. phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
  8854. phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
  8855. phba->sli4_hba.nvmet_cqset[0]->queue_id);
  8856. }
  8857. }
  8858. if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
  8859. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8860. "0540 Receive Queue not allocated\n");
  8861. rc = -ENOMEM;
  8862. goto out_destroy;
  8863. }
  8864. rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  8865. phba->sli4_hba.els_cq, LPFC_USOL);
  8866. if (rc) {
  8867. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8868. "0541 Failed setup of Receive Queue: "
  8869. "rc = 0x%x\n", (uint32_t)rc);
  8870. goto out_destroy;
  8871. }
  8872. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8873. "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
  8874. "parent cq-id=%d\n",
  8875. phba->sli4_hba.hdr_rq->queue_id,
  8876. phba->sli4_hba.dat_rq->queue_id,
  8877. phba->sli4_hba.els_cq->queue_id);
  8878. if (phba->cfg_fcp_imax)
  8879. usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
  8880. else
  8881. usdelay = 0;
  8882. for (qidx = 0; qidx < phba->cfg_irq_chann;
  8883. qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
  8884. lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
  8885. usdelay);
  8886. if (phba->sli4_hba.cq_max) {
  8887. kfree(phba->sli4_hba.cq_lookup);
  8888. phba->sli4_hba.cq_lookup = kcalloc((phba->sli4_hba.cq_max + 1),
  8889. sizeof(struct lpfc_queue *), GFP_KERNEL);
  8890. if (!phba->sli4_hba.cq_lookup) {
  8891. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8892. "0549 Failed setup of CQ Lookup table: "
  8893. "size 0x%x\n", phba->sli4_hba.cq_max);
  8894. rc = -ENOMEM;
  8895. goto out_destroy;
  8896. }
  8897. lpfc_setup_cq_lookup(phba);
  8898. }
  8899. return 0;
  8900. out_destroy:
  8901. lpfc_sli4_queue_unset(phba);
  8902. out_error:
  8903. return rc;
  8904. }
  8905. /**
  8906. * lpfc_sli4_queue_unset - Unset all the SLI4 queues
  8907. * @phba: pointer to lpfc hba data structure.
  8908. *
  8909. * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
  8910. * operation.
  8911. *
  8912. * Return codes
  8913. * 0 - successful
  8914. * -ENOMEM - No available memory
  8915. * -EIO - The mailbox failed to complete successfully.
  8916. **/
  8917. void
  8918. lpfc_sli4_queue_unset(struct lpfc_hba *phba)
  8919. {
  8920. struct lpfc_sli4_hdw_queue *qp;
  8921. struct lpfc_queue *eq;
  8922. int qidx;
  8923. /* Unset mailbox command work queue */
  8924. if (phba->sli4_hba.mbx_wq)
  8925. lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
  8926. /* Unset NVME LS work queue */
  8927. if (phba->sli4_hba.nvmels_wq)
  8928. lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
  8929. /* Unset ELS work queue */
  8930. if (phba->sli4_hba.els_wq)
  8931. lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
  8932. /* Unset unsolicited receive queue */
  8933. if (phba->sli4_hba.hdr_rq)
  8934. lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
  8935. phba->sli4_hba.dat_rq);
  8936. /* Unset mailbox command complete queue */
  8937. if (phba->sli4_hba.mbx_cq)
  8938. lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
  8939. /* Unset ELS complete queue */
  8940. if (phba->sli4_hba.els_cq)
  8941. lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
  8942. /* Unset NVME LS complete queue */
  8943. if (phba->sli4_hba.nvmels_cq)
  8944. lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
  8945. if (phba->nvmet_support) {
  8946. /* Unset NVMET MRQ queue */
  8947. if (phba->sli4_hba.nvmet_mrq_hdr) {
  8948. for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
  8949. lpfc_rq_destroy(
  8950. phba,
  8951. phba->sli4_hba.nvmet_mrq_hdr[qidx],
  8952. phba->sli4_hba.nvmet_mrq_data[qidx]);
  8953. }
  8954. /* Unset NVMET CQ Set complete queue */
  8955. if (phba->sli4_hba.nvmet_cqset) {
  8956. for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
  8957. lpfc_cq_destroy(
  8958. phba, phba->sli4_hba.nvmet_cqset[qidx]);
  8959. }
  8960. }
  8961. /* Unset fast-path SLI4 queues */
  8962. if (phba->sli4_hba.hdwq) {
  8963. /* Loop thru all Hardware Queues */
  8964. for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
  8965. /* Destroy the CQ/WQ corresponding to Hardware Queue */
  8966. qp = &phba->sli4_hba.hdwq[qidx];
  8967. lpfc_wq_destroy(phba, qp->io_wq);
  8968. lpfc_cq_destroy(phba, qp->io_cq);
  8969. }
  8970. /* Loop thru all IRQ vectors */
  8971. for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
  8972. /* Destroy the EQ corresponding to the IRQ vector */
  8973. eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
  8974. lpfc_eq_destroy(phba, eq);
  8975. }
  8976. }
  8977. kfree(phba->sli4_hba.cq_lookup);
  8978. phba->sli4_hba.cq_lookup = NULL;
  8979. phba->sli4_hba.cq_max = 0;
  8980. }
  8981. /**
  8982. * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
  8983. * @phba: pointer to lpfc hba data structure.
  8984. *
  8985. * This routine is invoked to allocate and set up a pool of completion queue
  8986. * events. The body of the completion queue event is a completion queue entry
  8987. * CQE. For now, this pool is used for the interrupt service routine to queue
  8988. * the following HBA completion queue events for the worker thread to process:
  8989. * - Mailbox asynchronous events
  8990. * - Receive queue completion unsolicited events
  8991. * Later, this can be used for all the slow-path events.
  8992. *
  8993. * Return codes
  8994. * 0 - successful
  8995. * -ENOMEM - No available memory
  8996. **/
  8997. static int
  8998. lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
  8999. {
  9000. struct lpfc_cq_event *cq_event;
  9001. int i;
  9002. for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
  9003. cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
  9004. if (!cq_event)
  9005. goto out_pool_create_fail;
  9006. list_add_tail(&cq_event->list,
  9007. &phba->sli4_hba.sp_cqe_event_pool);
  9008. }
  9009. return 0;
  9010. out_pool_create_fail:
  9011. lpfc_sli4_cq_event_pool_destroy(phba);
  9012. return -ENOMEM;
  9013. }
  9014. /**
  9015. * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
  9016. * @phba: pointer to lpfc hba data structure.
  9017. *
  9018. * This routine is invoked to free the pool of completion queue events at
  9019. * driver unload time. Note that, it is the responsibility of the driver
  9020. * cleanup routine to free all the outstanding completion-queue events
  9021. * allocated from this pool back into the pool before invoking this routine
  9022. * to destroy the pool.
  9023. **/
  9024. static void
  9025. lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
  9026. {
  9027. struct lpfc_cq_event *cq_event, *next_cq_event;
  9028. list_for_each_entry_safe(cq_event, next_cq_event,
  9029. &phba->sli4_hba.sp_cqe_event_pool, list) {
  9030. list_del(&cq_event->list);
  9031. kfree(cq_event);
  9032. }
  9033. }
  9034. /**
  9035. * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
  9036. * @phba: pointer to lpfc hba data structure.
  9037. *
  9038. * This routine is the lock free version of the API invoked to allocate a
  9039. * completion-queue event from the free pool.
  9040. *
  9041. * Return: Pointer to the newly allocated completion-queue event if successful
  9042. * NULL otherwise.
  9043. **/
  9044. struct lpfc_cq_event *
  9045. __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
  9046. {
  9047. struct lpfc_cq_event *cq_event = NULL;
  9048. list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
  9049. struct lpfc_cq_event, list);
  9050. return cq_event;
  9051. }
  9052. /**
  9053. * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
  9054. * @phba: pointer to lpfc hba data structure.
  9055. *
  9056. * This routine is the lock version of the API invoked to allocate a
  9057. * completion-queue event from the free pool.
  9058. *
  9059. * Return: Pointer to the newly allocated completion-queue event if successful
  9060. * NULL otherwise.
  9061. **/
  9062. struct lpfc_cq_event *
  9063. lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
  9064. {
  9065. struct lpfc_cq_event *cq_event;
  9066. unsigned long iflags;
  9067. spin_lock_irqsave(&phba->hbalock, iflags);
  9068. cq_event = __lpfc_sli4_cq_event_alloc(phba);
  9069. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9070. return cq_event;
  9071. }
  9072. /**
  9073. * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
  9074. * @phba: pointer to lpfc hba data structure.
  9075. * @cq_event: pointer to the completion queue event to be freed.
  9076. *
  9077. * This routine is the lock free version of the API invoked to release a
  9078. * completion-queue event back into the free pool.
  9079. **/
  9080. void
  9081. __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
  9082. struct lpfc_cq_event *cq_event)
  9083. {
  9084. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
  9085. }
  9086. /**
  9087. * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
  9088. * @phba: pointer to lpfc hba data structure.
  9089. * @cq_event: pointer to the completion queue event to be freed.
  9090. *
  9091. * This routine is the lock version of the API invoked to release a
  9092. * completion-queue event back into the free pool.
  9093. **/
  9094. void
  9095. lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
  9096. struct lpfc_cq_event *cq_event)
  9097. {
  9098. unsigned long iflags;
  9099. spin_lock_irqsave(&phba->hbalock, iflags);
  9100. __lpfc_sli4_cq_event_release(phba, cq_event);
  9101. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9102. }
  9103. /**
  9104. * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
  9105. * @phba: pointer to lpfc hba data structure.
  9106. *
  9107. * This routine is to free all the pending completion-queue events to the
  9108. * back into the free pool for device reset.
  9109. **/
  9110. static void
  9111. lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
  9112. {
  9113. LIST_HEAD(cqelist);
  9114. struct lpfc_cq_event *cqe;
  9115. unsigned long iflags;
  9116. /* Retrieve all the pending WCQEs from pending WCQE lists */
  9117. spin_lock_irqsave(&phba->hbalock, iflags);
  9118. /* Pending FCP XRI abort events */
  9119. list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9120. &cqelist);
  9121. /* Pending ELS XRI abort events */
  9122. list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9123. &cqelist);
  9124. /* Pending asynnc events */
  9125. list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
  9126. &cqelist);
  9127. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9128. while (!list_empty(&cqelist)) {
  9129. list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
  9130. lpfc_sli4_cq_event_release(phba, cqe);
  9131. }
  9132. }
  9133. /**
  9134. * lpfc_pci_function_reset - Reset pci function.
  9135. * @phba: pointer to lpfc hba data structure.
  9136. *
  9137. * This routine is invoked to request a PCI function reset. It will destroys
  9138. * all resources assigned to the PCI function which originates this request.
  9139. *
  9140. * Return codes
  9141. * 0 - successful
  9142. * -ENOMEM - No available memory
  9143. * -EIO - The mailbox failed to complete successfully.
  9144. **/
  9145. int
  9146. lpfc_pci_function_reset(struct lpfc_hba *phba)
  9147. {
  9148. LPFC_MBOXQ_t *mboxq;
  9149. uint32_t rc = 0, if_type;
  9150. uint32_t shdr_status, shdr_add_status;
  9151. uint32_t rdy_chk;
  9152. uint32_t port_reset = 0;
  9153. union lpfc_sli4_cfg_shdr *shdr;
  9154. struct lpfc_register reg_data;
  9155. uint16_t devid;
  9156. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9157. switch (if_type) {
  9158. case LPFC_SLI_INTF_IF_TYPE_0:
  9159. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
  9160. GFP_KERNEL);
  9161. if (!mboxq) {
  9162. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9163. "0494 Unable to allocate memory for "
  9164. "issuing SLI_FUNCTION_RESET mailbox "
  9165. "command\n");
  9166. return -ENOMEM;
  9167. }
  9168. /* Setup PCI function reset mailbox-ioctl command */
  9169. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  9170. LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
  9171. LPFC_SLI4_MBX_EMBED);
  9172. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  9173. shdr = (union lpfc_sli4_cfg_shdr *)
  9174. &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
  9175. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9176. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  9177. &shdr->response);
  9178. if (rc != MBX_TIMEOUT)
  9179. mempool_free(mboxq, phba->mbox_mem_pool);
  9180. if (shdr_status || shdr_add_status || rc) {
  9181. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9182. "0495 SLI_FUNCTION_RESET mailbox "
  9183. "failed with status x%x add_status x%x,"
  9184. " mbx status x%x\n",
  9185. shdr_status, shdr_add_status, rc);
  9186. rc = -ENXIO;
  9187. }
  9188. break;
  9189. case LPFC_SLI_INTF_IF_TYPE_2:
  9190. case LPFC_SLI_INTF_IF_TYPE_6:
  9191. wait:
  9192. /*
  9193. * Poll the Port Status Register and wait for RDY for
  9194. * up to 30 seconds. If the port doesn't respond, treat
  9195. * it as an error.
  9196. */
  9197. for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
  9198. if (lpfc_readl(phba->sli4_hba.u.if_type2.
  9199. STATUSregaddr, &reg_data.word0)) {
  9200. rc = -ENODEV;
  9201. goto out;
  9202. }
  9203. if (bf_get(lpfc_sliport_status_rdy, &reg_data))
  9204. break;
  9205. msleep(20);
  9206. }
  9207. if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
  9208. phba->work_status[0] = readl(
  9209. phba->sli4_hba.u.if_type2.ERR1regaddr);
  9210. phba->work_status[1] = readl(
  9211. phba->sli4_hba.u.if_type2.ERR2regaddr);
  9212. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9213. "2890 Port not ready, port status reg "
  9214. "0x%x error 1=0x%x, error 2=0x%x\n",
  9215. reg_data.word0,
  9216. phba->work_status[0],
  9217. phba->work_status[1]);
  9218. rc = -ENODEV;
  9219. goto out;
  9220. }
  9221. if (!port_reset) {
  9222. /*
  9223. * Reset the port now
  9224. */
  9225. reg_data.word0 = 0;
  9226. bf_set(lpfc_sliport_ctrl_end, &reg_data,
  9227. LPFC_SLIPORT_LITTLE_ENDIAN);
  9228. bf_set(lpfc_sliport_ctrl_ip, &reg_data,
  9229. LPFC_SLIPORT_INIT_PORT);
  9230. writel(reg_data.word0, phba->sli4_hba.u.if_type2.
  9231. CTRLregaddr);
  9232. /* flush */
  9233. pci_read_config_word(phba->pcidev,
  9234. PCI_DEVICE_ID, &devid);
  9235. port_reset = 1;
  9236. msleep(20);
  9237. goto wait;
  9238. } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
  9239. rc = -ENODEV;
  9240. goto out;
  9241. }
  9242. break;
  9243. case LPFC_SLI_INTF_IF_TYPE_1:
  9244. default:
  9245. break;
  9246. }
  9247. out:
  9248. /* Catch the not-ready port failure after a port reset. */
  9249. if (rc) {
  9250. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9251. "3317 HBA not functional: IP Reset Failed "
  9252. "try: echo fw_reset > board_mode\n");
  9253. rc = -ENODEV;
  9254. }
  9255. return rc;
  9256. }
  9257. /**
  9258. * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
  9259. * @phba: pointer to lpfc hba data structure.
  9260. *
  9261. * This routine is invoked to set up the PCI device memory space for device
  9262. * with SLI-4 interface spec.
  9263. *
  9264. * Return codes
  9265. * 0 - successful
  9266. * other values - error
  9267. **/
  9268. static int
  9269. lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
  9270. {
  9271. struct pci_dev *pdev = phba->pcidev;
  9272. unsigned long bar0map_len, bar1map_len, bar2map_len;
  9273. int error;
  9274. uint32_t if_type;
  9275. if (!pdev)
  9276. return -ENODEV;
  9277. /* Set the device DMA mask size */
  9278. error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9279. if (error)
  9280. error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9281. if (error)
  9282. return error;
  9283. /*
  9284. * The BARs and register set definitions and offset locations are
  9285. * dependent on the if_type.
  9286. */
  9287. if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
  9288. &phba->sli4_hba.sli_intf.word0)) {
  9289. return -ENODEV;
  9290. }
  9291. /* There is no SLI3 failback for SLI4 devices. */
  9292. if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
  9293. LPFC_SLI_INTF_VALID) {
  9294. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9295. "2894 SLI_INTF reg contents invalid "
  9296. "sli_intf reg 0x%x\n",
  9297. phba->sli4_hba.sli_intf.word0);
  9298. return -ENODEV;
  9299. }
  9300. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9301. /*
  9302. * Get the bus address of SLI4 device Bar regions and the
  9303. * number of bytes required by each mapping. The mapping of the
  9304. * particular PCI BARs regions is dependent on the type of
  9305. * SLI4 device.
  9306. */
  9307. if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
  9308. phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
  9309. bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
  9310. /*
  9311. * Map SLI4 PCI Config Space Register base to a kernel virtual
  9312. * addr
  9313. */
  9314. phba->sli4_hba.conf_regs_memmap_p =
  9315. ioremap(phba->pci_bar0_map, bar0map_len);
  9316. if (!phba->sli4_hba.conf_regs_memmap_p) {
  9317. dev_printk(KERN_ERR, &pdev->dev,
  9318. "ioremap failed for SLI4 PCI config "
  9319. "registers.\n");
  9320. return -ENODEV;
  9321. }
  9322. phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
  9323. /* Set up BAR0 PCI config space register memory map */
  9324. lpfc_sli4_bar0_register_memmap(phba, if_type);
  9325. } else {
  9326. phba->pci_bar0_map = pci_resource_start(pdev, 1);
  9327. bar0map_len = pci_resource_len(pdev, 1);
  9328. if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
  9329. dev_printk(KERN_ERR, &pdev->dev,
  9330. "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
  9331. return -ENODEV;
  9332. }
  9333. phba->sli4_hba.conf_regs_memmap_p =
  9334. ioremap(phba->pci_bar0_map, bar0map_len);
  9335. if (!phba->sli4_hba.conf_regs_memmap_p) {
  9336. dev_printk(KERN_ERR, &pdev->dev,
  9337. "ioremap failed for SLI4 PCI config "
  9338. "registers.\n");
  9339. return -ENODEV;
  9340. }
  9341. lpfc_sli4_bar0_register_memmap(phba, if_type);
  9342. }
  9343. if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
  9344. if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
  9345. /*
  9346. * Map SLI4 if type 0 HBA Control Register base to a
  9347. * kernel virtual address and setup the registers.
  9348. */
  9349. phba->pci_bar1_map = pci_resource_start(pdev,
  9350. PCI_64BIT_BAR2);
  9351. bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
  9352. phba->sli4_hba.ctrl_regs_memmap_p =
  9353. ioremap(phba->pci_bar1_map,
  9354. bar1map_len);
  9355. if (!phba->sli4_hba.ctrl_regs_memmap_p) {
  9356. dev_err(&pdev->dev,
  9357. "ioremap failed for SLI4 HBA "
  9358. "control registers.\n");
  9359. error = -ENOMEM;
  9360. goto out_iounmap_conf;
  9361. }
  9362. phba->pci_bar2_memmap_p =
  9363. phba->sli4_hba.ctrl_regs_memmap_p;
  9364. lpfc_sli4_bar1_register_memmap(phba, if_type);
  9365. } else {
  9366. error = -ENOMEM;
  9367. goto out_iounmap_conf;
  9368. }
  9369. }
  9370. if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
  9371. (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
  9372. /*
  9373. * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
  9374. * virtual address and setup the registers.
  9375. */
  9376. phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
  9377. bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
  9378. phba->sli4_hba.drbl_regs_memmap_p =
  9379. ioremap(phba->pci_bar1_map, bar1map_len);
  9380. if (!phba->sli4_hba.drbl_regs_memmap_p) {
  9381. dev_err(&pdev->dev,
  9382. "ioremap failed for SLI4 HBA doorbell registers.\n");
  9383. error = -ENOMEM;
  9384. goto out_iounmap_conf;
  9385. }
  9386. phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
  9387. lpfc_sli4_bar1_register_memmap(phba, if_type);
  9388. }
  9389. if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
  9390. if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
  9391. /*
  9392. * Map SLI4 if type 0 HBA Doorbell Register base to
  9393. * a kernel virtual address and setup the registers.
  9394. */
  9395. phba->pci_bar2_map = pci_resource_start(pdev,
  9396. PCI_64BIT_BAR4);
  9397. bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
  9398. phba->sli4_hba.drbl_regs_memmap_p =
  9399. ioremap(phba->pci_bar2_map,
  9400. bar2map_len);
  9401. if (!phba->sli4_hba.drbl_regs_memmap_p) {
  9402. dev_err(&pdev->dev,
  9403. "ioremap failed for SLI4 HBA"
  9404. " doorbell registers.\n");
  9405. error = -ENOMEM;
  9406. goto out_iounmap_ctrl;
  9407. }
  9408. phba->pci_bar4_memmap_p =
  9409. phba->sli4_hba.drbl_regs_memmap_p;
  9410. error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
  9411. if (error)
  9412. goto out_iounmap_all;
  9413. } else {
  9414. error = -ENOMEM;
  9415. goto out_iounmap_all;
  9416. }
  9417. }
  9418. if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
  9419. pci_resource_start(pdev, PCI_64BIT_BAR4)) {
  9420. /*
  9421. * Map SLI4 if type 6 HBA DPP Register base to a kernel
  9422. * virtual address and setup the registers.
  9423. */
  9424. phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
  9425. bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
  9426. phba->sli4_hba.dpp_regs_memmap_p =
  9427. ioremap(phba->pci_bar2_map, bar2map_len);
  9428. if (!phba->sli4_hba.dpp_regs_memmap_p) {
  9429. dev_err(&pdev->dev,
  9430. "ioremap failed for SLI4 HBA dpp registers.\n");
  9431. error = -ENOMEM;
  9432. goto out_iounmap_ctrl;
  9433. }
  9434. phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
  9435. }
  9436. /* Set up the EQ/CQ register handeling functions now */
  9437. switch (if_type) {
  9438. case LPFC_SLI_INTF_IF_TYPE_0:
  9439. case LPFC_SLI_INTF_IF_TYPE_2:
  9440. phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
  9441. phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_write_eq_db;
  9442. phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_write_cq_db;
  9443. break;
  9444. case LPFC_SLI_INTF_IF_TYPE_6:
  9445. phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
  9446. phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_if6_write_eq_db;
  9447. phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_if6_write_cq_db;
  9448. break;
  9449. default:
  9450. break;
  9451. }
  9452. return 0;
  9453. out_iounmap_all:
  9454. iounmap(phba->sli4_hba.drbl_regs_memmap_p);
  9455. out_iounmap_ctrl:
  9456. iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
  9457. out_iounmap_conf:
  9458. iounmap(phba->sli4_hba.conf_regs_memmap_p);
  9459. return error;
  9460. }
  9461. /**
  9462. * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
  9463. * @phba: pointer to lpfc hba data structure.
  9464. *
  9465. * This routine is invoked to unset the PCI device memory space for device
  9466. * with SLI-4 interface spec.
  9467. **/
  9468. static void
  9469. lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
  9470. {
  9471. uint32_t if_type;
  9472. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9473. switch (if_type) {
  9474. case LPFC_SLI_INTF_IF_TYPE_0:
  9475. iounmap(phba->sli4_hba.drbl_regs_memmap_p);
  9476. iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
  9477. iounmap(phba->sli4_hba.conf_regs_memmap_p);
  9478. break;
  9479. case LPFC_SLI_INTF_IF_TYPE_2:
  9480. iounmap(phba->sli4_hba.conf_regs_memmap_p);
  9481. break;
  9482. case LPFC_SLI_INTF_IF_TYPE_6:
  9483. iounmap(phba->sli4_hba.drbl_regs_memmap_p);
  9484. iounmap(phba->sli4_hba.conf_regs_memmap_p);
  9485. if (phba->sli4_hba.dpp_regs_memmap_p)
  9486. iounmap(phba->sli4_hba.dpp_regs_memmap_p);
  9487. break;
  9488. case LPFC_SLI_INTF_IF_TYPE_1:
  9489. default:
  9490. dev_printk(KERN_ERR, &phba->pcidev->dev,
  9491. "FATAL - unsupported SLI4 interface type - %d\n",
  9492. if_type);
  9493. break;
  9494. }
  9495. }
  9496. /**
  9497. * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
  9498. * @phba: pointer to lpfc hba data structure.
  9499. *
  9500. * This routine is invoked to enable the MSI-X interrupt vectors to device
  9501. * with SLI-3 interface specs.
  9502. *
  9503. * Return codes
  9504. * 0 - successful
  9505. * other values - error
  9506. **/
  9507. static int
  9508. lpfc_sli_enable_msix(struct lpfc_hba *phba)
  9509. {
  9510. int rc;
  9511. LPFC_MBOXQ_t *pmb;
  9512. /* Set up MSI-X multi-message vectors */
  9513. rc = pci_alloc_irq_vectors(phba->pcidev,
  9514. LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
  9515. if (rc < 0) {
  9516. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  9517. "0420 PCI enable MSI-X failed (%d)\n", rc);
  9518. goto vec_fail_out;
  9519. }
  9520. /*
  9521. * Assign MSI-X vectors to interrupt handlers
  9522. */
  9523. /* vector-0 is associated to slow-path handler */
  9524. rc = request_irq(pci_irq_vector(phba->pcidev, 0),
  9525. &lpfc_sli_sp_intr_handler, 0,
  9526. LPFC_SP_DRIVER_HANDLER_NAME, phba);
  9527. if (rc) {
  9528. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  9529. "0421 MSI-X slow-path request_irq failed "
  9530. "(%d)\n", rc);
  9531. goto msi_fail_out;
  9532. }
  9533. /* vector-1 is associated to fast-path handler */
  9534. rc = request_irq(pci_irq_vector(phba->pcidev, 1),
  9535. &lpfc_sli_fp_intr_handler, 0,
  9536. LPFC_FP_DRIVER_HANDLER_NAME, phba);
  9537. if (rc) {
  9538. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  9539. "0429 MSI-X fast-path request_irq failed "
  9540. "(%d)\n", rc);
  9541. goto irq_fail_out;
  9542. }
  9543. /*
  9544. * Configure HBA MSI-X attention conditions to messages
  9545. */
  9546. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  9547. if (!pmb) {
  9548. rc = -ENOMEM;
  9549. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9550. "0474 Unable to allocate memory for issuing "
  9551. "MBOX_CONFIG_MSI command\n");
  9552. goto mem_fail_out;
  9553. }
  9554. rc = lpfc_config_msi(phba, pmb);
  9555. if (rc)
  9556. goto mbx_fail_out;
  9557. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  9558. if (rc != MBX_SUCCESS) {
  9559. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
  9560. "0351 Config MSI mailbox command failed, "
  9561. "mbxCmd x%x, mbxStatus x%x\n",
  9562. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
  9563. goto mbx_fail_out;
  9564. }
  9565. /* Free memory allocated for mailbox command */
  9566. mempool_free(pmb, phba->mbox_mem_pool);
  9567. return rc;
  9568. mbx_fail_out:
  9569. /* Free memory allocated for mailbox command */
  9570. mempool_free(pmb, phba->mbox_mem_pool);
  9571. mem_fail_out:
  9572. /* free the irq already requested */
  9573. free_irq(pci_irq_vector(phba->pcidev, 1), phba);
  9574. irq_fail_out:
  9575. /* free the irq already requested */
  9576. free_irq(pci_irq_vector(phba->pcidev, 0), phba);
  9577. msi_fail_out:
  9578. /* Unconfigure MSI-X capability structure */
  9579. pci_free_irq_vectors(phba->pcidev);
  9580. vec_fail_out:
  9581. return rc;
  9582. }
  9583. /**
  9584. * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
  9585. * @phba: pointer to lpfc hba data structure.
  9586. *
  9587. * This routine is invoked to enable the MSI interrupt mode to device with
  9588. * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
  9589. * enable the MSI vector. The device driver is responsible for calling the
  9590. * request_irq() to register MSI vector with a interrupt the handler, which
  9591. * is done in this function.
  9592. *
  9593. * Return codes
  9594. * 0 - successful
  9595. * other values - error
  9596. */
  9597. static int
  9598. lpfc_sli_enable_msi(struct lpfc_hba *phba)
  9599. {
  9600. int rc;
  9601. rc = pci_enable_msi(phba->pcidev);
  9602. if (!rc)
  9603. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  9604. "0462 PCI enable MSI mode success.\n");
  9605. else {
  9606. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  9607. "0471 PCI enable MSI mode failed (%d)\n", rc);
  9608. return rc;
  9609. }
  9610. rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
  9611. 0, LPFC_DRIVER_NAME, phba);
  9612. if (rc) {
  9613. pci_disable_msi(phba->pcidev);
  9614. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  9615. "0478 MSI request_irq failed (%d)\n", rc);
  9616. }
  9617. return rc;
  9618. }
  9619. /**
  9620. * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
  9621. * @phba: pointer to lpfc hba data structure.
  9622. *
  9623. * This routine is invoked to enable device interrupt and associate driver's
  9624. * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
  9625. * spec. Depends on the interrupt mode configured to the driver, the driver
  9626. * will try to fallback from the configured interrupt mode to an interrupt
  9627. * mode which is supported by the platform, kernel, and device in the order
  9628. * of:
  9629. * MSI-X -> MSI -> IRQ.
  9630. *
  9631. * Return codes
  9632. * 0 - successful
  9633. * other values - error
  9634. **/
  9635. static uint32_t
  9636. lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
  9637. {
  9638. uint32_t intr_mode = LPFC_INTR_ERROR;
  9639. int retval;
  9640. if (cfg_mode == 2) {
  9641. /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
  9642. retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
  9643. if (!retval) {
  9644. /* Now, try to enable MSI-X interrupt mode */
  9645. retval = lpfc_sli_enable_msix(phba);
  9646. if (!retval) {
  9647. /* Indicate initialization to MSI-X mode */
  9648. phba->intr_type = MSIX;
  9649. intr_mode = 2;
  9650. }
  9651. }
  9652. }
  9653. /* Fallback to MSI if MSI-X initialization failed */
  9654. if (cfg_mode >= 1 && phba->intr_type == NONE) {
  9655. retval = lpfc_sli_enable_msi(phba);
  9656. if (!retval) {
  9657. /* Indicate initialization to MSI mode */
  9658. phba->intr_type = MSI;
  9659. intr_mode = 1;
  9660. }
  9661. }
  9662. /* Fallback to INTx if both MSI-X/MSI initalization failed */
  9663. if (phba->intr_type == NONE) {
  9664. retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
  9665. IRQF_SHARED, LPFC_DRIVER_NAME, phba);
  9666. if (!retval) {
  9667. /* Indicate initialization to INTx mode */
  9668. phba->intr_type = INTx;
  9669. intr_mode = 0;
  9670. }
  9671. }
  9672. return intr_mode;
  9673. }
  9674. /**
  9675. * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
  9676. * @phba: pointer to lpfc hba data structure.
  9677. *
  9678. * This routine is invoked to disable device interrupt and disassociate the
  9679. * driver's interrupt handler(s) from interrupt vector(s) to device with
  9680. * SLI-3 interface spec. Depending on the interrupt mode, the driver will
  9681. * release the interrupt vector(s) for the message signaled interrupt.
  9682. **/
  9683. static void
  9684. lpfc_sli_disable_intr(struct lpfc_hba *phba)
  9685. {
  9686. int nr_irqs, i;
  9687. if (phba->intr_type == MSIX)
  9688. nr_irqs = LPFC_MSIX_VECTORS;
  9689. else
  9690. nr_irqs = 1;
  9691. for (i = 0; i < nr_irqs; i++)
  9692. free_irq(pci_irq_vector(phba->pcidev, i), phba);
  9693. pci_free_irq_vectors(phba->pcidev);
  9694. /* Reset interrupt management states */
  9695. phba->intr_type = NONE;
  9696. phba->sli.slistat.sli_intr = 0;
  9697. }
  9698. /**
  9699. * lpfc_find_cpu_handle - Find the CPU that corresponds to the specified Queue
  9700. * @phba: pointer to lpfc hba data structure.
  9701. * @id: EQ vector index or Hardware Queue index
  9702. * @match: LPFC_FIND_BY_EQ = match by EQ
  9703. * LPFC_FIND_BY_HDWQ = match by Hardware Queue
  9704. * Return the CPU that matches the selection criteria
  9705. */
  9706. static uint16_t
  9707. lpfc_find_cpu_handle(struct lpfc_hba *phba, uint16_t id, int match)
  9708. {
  9709. struct lpfc_vector_map_info *cpup;
  9710. int cpu;
  9711. /* Loop through all CPUs */
  9712. for_each_present_cpu(cpu) {
  9713. cpup = &phba->sli4_hba.cpu_map[cpu];
  9714. /* If we are matching by EQ, there may be multiple CPUs using
  9715. * using the same vector, so select the one with
  9716. * LPFC_CPU_FIRST_IRQ set.
  9717. */
  9718. if ((match == LPFC_FIND_BY_EQ) &&
  9719. (cpup->flag & LPFC_CPU_FIRST_IRQ) &&
  9720. (cpup->eq == id))
  9721. return cpu;
  9722. /* If matching by HDWQ, select the first CPU that matches */
  9723. if ((match == LPFC_FIND_BY_HDWQ) && (cpup->hdwq == id))
  9724. return cpu;
  9725. }
  9726. return 0;
  9727. }
  9728. #ifdef CONFIG_X86
  9729. /**
  9730. * lpfc_find_hyper - Determine if the CPU map entry is hyper-threaded
  9731. * @phba: pointer to lpfc hba data structure.
  9732. * @cpu: CPU map index
  9733. * @phys_id: CPU package physical id
  9734. * @core_id: CPU core id
  9735. */
  9736. static int
  9737. lpfc_find_hyper(struct lpfc_hba *phba, int cpu,
  9738. uint16_t phys_id, uint16_t core_id)
  9739. {
  9740. struct lpfc_vector_map_info *cpup;
  9741. int idx;
  9742. for_each_present_cpu(idx) {
  9743. cpup = &phba->sli4_hba.cpu_map[idx];
  9744. /* Does the cpup match the one we are looking for */
  9745. if ((cpup->phys_id == phys_id) &&
  9746. (cpup->core_id == core_id) &&
  9747. (cpu != idx))
  9748. return 1;
  9749. }
  9750. return 0;
  9751. }
  9752. #endif
  9753. /*
  9754. * lpfc_assign_eq_map_info - Assigns eq for vector_map structure
  9755. * @phba: pointer to lpfc hba data structure.
  9756. * @eqidx: index for eq and irq vector
  9757. * @flag: flags to set for vector_map structure
  9758. * @cpu: cpu used to index vector_map structure
  9759. *
  9760. * The routine assigns eq info into vector_map structure
  9761. */
  9762. static inline void
  9763. lpfc_assign_eq_map_info(struct lpfc_hba *phba, uint16_t eqidx, uint16_t flag,
  9764. unsigned int cpu)
  9765. {
  9766. struct lpfc_vector_map_info *cpup = &phba->sli4_hba.cpu_map[cpu];
  9767. struct lpfc_hba_eq_hdl *eqhdl = lpfc_get_eq_hdl(eqidx);
  9768. cpup->eq = eqidx;
  9769. cpup->flag |= flag;
  9770. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  9771. "3336 Set Affinity: CPU %d irq %d eq %d flag x%x\n",
  9772. cpu, eqhdl->irq, cpup->eq, cpup->flag);
  9773. }
  9774. /**
  9775. * lpfc_cpu_map_array_init - Initialize cpu_map structure
  9776. * @phba: pointer to lpfc hba data structure.
  9777. *
  9778. * The routine initializes the cpu_map array structure
  9779. */
  9780. static void
  9781. lpfc_cpu_map_array_init(struct lpfc_hba *phba)
  9782. {
  9783. struct lpfc_vector_map_info *cpup;
  9784. struct lpfc_eq_intr_info *eqi;
  9785. int cpu;
  9786. for_each_possible_cpu(cpu) {
  9787. cpup = &phba->sli4_hba.cpu_map[cpu];
  9788. cpup->phys_id = LPFC_VECTOR_MAP_EMPTY;
  9789. cpup->core_id = LPFC_VECTOR_MAP_EMPTY;
  9790. cpup->hdwq = LPFC_VECTOR_MAP_EMPTY;
  9791. cpup->eq = LPFC_VECTOR_MAP_EMPTY;
  9792. cpup->flag = 0;
  9793. eqi = per_cpu_ptr(phba->sli4_hba.eq_info, cpu);
  9794. INIT_LIST_HEAD(&eqi->list);
  9795. eqi->icnt = 0;
  9796. }
  9797. }
  9798. /**
  9799. * lpfc_hba_eq_hdl_array_init - Initialize hba_eq_hdl structure
  9800. * @phba: pointer to lpfc hba data structure.
  9801. *
  9802. * The routine initializes the hba_eq_hdl array structure
  9803. */
  9804. static void
  9805. lpfc_hba_eq_hdl_array_init(struct lpfc_hba *phba)
  9806. {
  9807. struct lpfc_hba_eq_hdl *eqhdl;
  9808. int i;
  9809. for (i = 0; i < phba->cfg_irq_chann; i++) {
  9810. eqhdl = lpfc_get_eq_hdl(i);
  9811. eqhdl->irq = LPFC_VECTOR_MAP_EMPTY;
  9812. eqhdl->phba = phba;
  9813. }
  9814. }
  9815. /**
  9816. * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
  9817. * @phba: pointer to lpfc hba data structure.
  9818. * @vectors: number of msix vectors allocated.
  9819. *
  9820. * The routine will figure out the CPU affinity assignment for every
  9821. * MSI-X vector allocated for the HBA.
  9822. * In addition, the CPU to IO channel mapping will be calculated
  9823. * and the phba->sli4_hba.cpu_map array will reflect this.
  9824. */
  9825. static void
  9826. lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
  9827. {
  9828. int i, cpu, idx, next_idx, new_cpu, start_cpu, first_cpu;
  9829. int max_phys_id, min_phys_id;
  9830. int max_core_id, min_core_id;
  9831. struct lpfc_vector_map_info *cpup;
  9832. struct lpfc_vector_map_info *new_cpup;
  9833. #ifdef CONFIG_X86
  9834. struct cpuinfo_x86 *cpuinfo;
  9835. #endif
  9836. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  9837. struct lpfc_hdwq_stat *c_stat;
  9838. #endif
  9839. max_phys_id = 0;
  9840. min_phys_id = LPFC_VECTOR_MAP_EMPTY;
  9841. max_core_id = 0;
  9842. min_core_id = LPFC_VECTOR_MAP_EMPTY;
  9843. /* Update CPU map with physical id and core id of each CPU */
  9844. for_each_present_cpu(cpu) {
  9845. cpup = &phba->sli4_hba.cpu_map[cpu];
  9846. #ifdef CONFIG_X86
  9847. cpuinfo = &cpu_data(cpu);
  9848. cpup->phys_id = cpuinfo->phys_proc_id;
  9849. cpup->core_id = cpuinfo->cpu_core_id;
  9850. if (lpfc_find_hyper(phba, cpu, cpup->phys_id, cpup->core_id))
  9851. cpup->flag |= LPFC_CPU_MAP_HYPER;
  9852. #else
  9853. /* No distinction between CPUs for other platforms */
  9854. cpup->phys_id = 0;
  9855. cpup->core_id = cpu;
  9856. #endif
  9857. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  9858. "3328 CPU %d physid %d coreid %d flag x%x\n",
  9859. cpu, cpup->phys_id, cpup->core_id, cpup->flag);
  9860. if (cpup->phys_id > max_phys_id)
  9861. max_phys_id = cpup->phys_id;
  9862. if (cpup->phys_id < min_phys_id)
  9863. min_phys_id = cpup->phys_id;
  9864. if (cpup->core_id > max_core_id)
  9865. max_core_id = cpup->core_id;
  9866. if (cpup->core_id < min_core_id)
  9867. min_core_id = cpup->core_id;
  9868. }
  9869. /* After looking at each irq vector assigned to this pcidev, its
  9870. * possible to see that not ALL CPUs have been accounted for.
  9871. * Next we will set any unassigned (unaffinitized) cpu map
  9872. * entries to a IRQ on the same phys_id.
  9873. */
  9874. first_cpu = cpumask_first(cpu_present_mask);
  9875. start_cpu = first_cpu;
  9876. for_each_present_cpu(cpu) {
  9877. cpup = &phba->sli4_hba.cpu_map[cpu];
  9878. /* Is this CPU entry unassigned */
  9879. if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
  9880. /* Mark CPU as IRQ not assigned by the kernel */
  9881. cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
  9882. /* If so, find a new_cpup thats on the the SAME
  9883. * phys_id as cpup. start_cpu will start where we
  9884. * left off so all unassigned entries don't get assgined
  9885. * the IRQ of the first entry.
  9886. */
  9887. new_cpu = start_cpu;
  9888. for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
  9889. new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
  9890. if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
  9891. (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY) &&
  9892. (new_cpup->phys_id == cpup->phys_id))
  9893. goto found_same;
  9894. new_cpu = cpumask_next(
  9895. new_cpu, cpu_present_mask);
  9896. if (new_cpu == nr_cpumask_bits)
  9897. new_cpu = first_cpu;
  9898. }
  9899. /* At this point, we leave the CPU as unassigned */
  9900. continue;
  9901. found_same:
  9902. /* We found a matching phys_id, so copy the IRQ info */
  9903. cpup->eq = new_cpup->eq;
  9904. /* Bump start_cpu to the next slot to minmize the
  9905. * chance of having multiple unassigned CPU entries
  9906. * selecting the same IRQ.
  9907. */
  9908. start_cpu = cpumask_next(new_cpu, cpu_present_mask);
  9909. if (start_cpu == nr_cpumask_bits)
  9910. start_cpu = first_cpu;
  9911. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  9912. "3337 Set Affinity: CPU %d "
  9913. "eq %d from peer cpu %d same "
  9914. "phys_id (%d)\n",
  9915. cpu, cpup->eq, new_cpu,
  9916. cpup->phys_id);
  9917. }
  9918. }
  9919. /* Set any unassigned cpu map entries to a IRQ on any phys_id */
  9920. start_cpu = first_cpu;
  9921. for_each_present_cpu(cpu) {
  9922. cpup = &phba->sli4_hba.cpu_map[cpu];
  9923. /* Is this entry unassigned */
  9924. if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
  9925. /* Mark it as IRQ not assigned by the kernel */
  9926. cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
  9927. /* If so, find a new_cpup thats on ANY phys_id
  9928. * as the cpup. start_cpu will start where we
  9929. * left off so all unassigned entries don't get
  9930. * assigned the IRQ of the first entry.
  9931. */
  9932. new_cpu = start_cpu;
  9933. for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
  9934. new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
  9935. if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
  9936. (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY))
  9937. goto found_any;
  9938. new_cpu = cpumask_next(
  9939. new_cpu, cpu_present_mask);
  9940. if (new_cpu == nr_cpumask_bits)
  9941. new_cpu = first_cpu;
  9942. }
  9943. /* We should never leave an entry unassigned */
  9944. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9945. "3339 Set Affinity: CPU %d "
  9946. "eq %d UNASSIGNED\n",
  9947. cpup->hdwq, cpup->eq);
  9948. continue;
  9949. found_any:
  9950. /* We found an available entry, copy the IRQ info */
  9951. cpup->eq = new_cpup->eq;
  9952. /* Bump start_cpu to the next slot to minmize the
  9953. * chance of having multiple unassigned CPU entries
  9954. * selecting the same IRQ.
  9955. */
  9956. start_cpu = cpumask_next(new_cpu, cpu_present_mask);
  9957. if (start_cpu == nr_cpumask_bits)
  9958. start_cpu = first_cpu;
  9959. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  9960. "3338 Set Affinity: CPU %d "
  9961. "eq %d from peer cpu %d (%d/%d)\n",
  9962. cpu, cpup->eq, new_cpu,
  9963. new_cpup->phys_id, new_cpup->core_id);
  9964. }
  9965. }
  9966. /* Assign hdwq indices that are unique across all cpus in the map
  9967. * that are also FIRST_CPUs.
  9968. */
  9969. idx = 0;
  9970. for_each_present_cpu(cpu) {
  9971. cpup = &phba->sli4_hba.cpu_map[cpu];
  9972. /* Only FIRST IRQs get a hdwq index assignment. */
  9973. if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
  9974. continue;
  9975. /* 1 to 1, the first LPFC_CPU_FIRST_IRQ cpus to a unique hdwq */
  9976. cpup->hdwq = idx;
  9977. idx++;
  9978. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9979. "3333 Set Affinity: CPU %d (phys %d core %d): "
  9980. "hdwq %d eq %d flg x%x\n",
  9981. cpu, cpup->phys_id, cpup->core_id,
  9982. cpup->hdwq, cpup->eq, cpup->flag);
  9983. }
  9984. /* Associate a hdwq with each cpu_map entry
  9985. * This will be 1 to 1 - hdwq to cpu, unless there are less
  9986. * hardware queues then CPUs. For that case we will just round-robin
  9987. * the available hardware queues as they get assigned to CPUs.
  9988. * The next_idx is the idx from the FIRST_CPU loop above to account
  9989. * for irq_chann < hdwq. The idx is used for round-robin assignments
  9990. * and needs to start at 0.
  9991. */
  9992. next_idx = idx;
  9993. start_cpu = 0;
  9994. idx = 0;
  9995. for_each_present_cpu(cpu) {
  9996. cpup = &phba->sli4_hba.cpu_map[cpu];
  9997. /* FIRST cpus are already mapped. */
  9998. if (cpup->flag & LPFC_CPU_FIRST_IRQ)
  9999. continue;
  10000. /* If the cfg_irq_chann < cfg_hdw_queue, set the hdwq
  10001. * of the unassigned cpus to the next idx so that all
  10002. * hdw queues are fully utilized.
  10003. */
  10004. if (next_idx < phba->cfg_hdw_queue) {
  10005. cpup->hdwq = next_idx;
  10006. next_idx++;
  10007. continue;
  10008. }
  10009. /* Not a First CPU and all hdw_queues are used. Reuse a
  10010. * Hardware Queue for another CPU, so be smart about it
  10011. * and pick one that has its IRQ/EQ mapped to the same phys_id
  10012. * (CPU package) and core_id.
  10013. */
  10014. new_cpu = start_cpu;
  10015. for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
  10016. new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
  10017. if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
  10018. new_cpup->phys_id == cpup->phys_id &&
  10019. new_cpup->core_id == cpup->core_id) {
  10020. goto found_hdwq;
  10021. }
  10022. new_cpu = cpumask_next(new_cpu, cpu_present_mask);
  10023. if (new_cpu == nr_cpumask_bits)
  10024. new_cpu = first_cpu;
  10025. }
  10026. /* If we can't match both phys_id and core_id,
  10027. * settle for just a phys_id match.
  10028. */
  10029. new_cpu = start_cpu;
  10030. for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
  10031. new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
  10032. if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
  10033. new_cpup->phys_id == cpup->phys_id)
  10034. goto found_hdwq;
  10035. new_cpu = cpumask_next(new_cpu, cpu_present_mask);
  10036. if (new_cpu == nr_cpumask_bits)
  10037. new_cpu = first_cpu;
  10038. }
  10039. /* Otherwise just round robin on cfg_hdw_queue */
  10040. cpup->hdwq = idx % phba->cfg_hdw_queue;
  10041. idx++;
  10042. goto logit;
  10043. found_hdwq:
  10044. /* We found an available entry, copy the IRQ info */
  10045. start_cpu = cpumask_next(new_cpu, cpu_present_mask);
  10046. if (start_cpu == nr_cpumask_bits)
  10047. start_cpu = first_cpu;
  10048. cpup->hdwq = new_cpup->hdwq;
  10049. logit:
  10050. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10051. "3335 Set Affinity: CPU %d (phys %d core %d): "
  10052. "hdwq %d eq %d flg x%x\n",
  10053. cpu, cpup->phys_id, cpup->core_id,
  10054. cpup->hdwq, cpup->eq, cpup->flag);
  10055. }
  10056. /*
  10057. * Initialize the cpu_map slots for not-present cpus in case
  10058. * a cpu is hot-added. Perform a simple hdwq round robin assignment.
  10059. */
  10060. idx = 0;
  10061. for_each_possible_cpu(cpu) {
  10062. cpup = &phba->sli4_hba.cpu_map[cpu];
  10063. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  10064. c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, cpu);
  10065. c_stat->hdwq_no = cpup->hdwq;
  10066. #endif
  10067. if (cpup->hdwq != LPFC_VECTOR_MAP_EMPTY)
  10068. continue;
  10069. cpup->hdwq = idx++ % phba->cfg_hdw_queue;
  10070. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  10071. c_stat->hdwq_no = cpup->hdwq;
  10072. #endif
  10073. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10074. "3340 Set Affinity: not present "
  10075. "CPU %d hdwq %d\n",
  10076. cpu, cpup->hdwq);
  10077. }
  10078. /* The cpu_map array will be used later during initialization
  10079. * when EQ / CQ / WQs are allocated and configured.
  10080. */
  10081. return;
  10082. }
  10083. /**
  10084. * lpfc_cpuhp_get_eq
  10085. *
  10086. * @phba: pointer to lpfc hba data structure.
  10087. * @cpu: cpu going offline
  10088. * @eqlist:
  10089. */
  10090. static int
  10091. lpfc_cpuhp_get_eq(struct lpfc_hba *phba, unsigned int cpu,
  10092. struct list_head *eqlist)
  10093. {
  10094. const struct cpumask *maskp;
  10095. struct lpfc_queue *eq;
  10096. struct cpumask *tmp;
  10097. u16 idx;
  10098. tmp = kzalloc(cpumask_size(), GFP_KERNEL);
  10099. if (!tmp)
  10100. return -ENOMEM;
  10101. for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
  10102. maskp = pci_irq_get_affinity(phba->pcidev, idx);
  10103. if (!maskp)
  10104. continue;
  10105. /*
  10106. * if irq is not affinitized to the cpu going
  10107. * then we don't need to poll the eq attached
  10108. * to it.
  10109. */
  10110. if (!cpumask_and(tmp, maskp, cpumask_of(cpu)))
  10111. continue;
  10112. /* get the cpus that are online and are affini-
  10113. * tized to this irq vector. If the count is
  10114. * more than 1 then cpuhp is not going to shut-
  10115. * down this vector. Since this cpu has not
  10116. * gone offline yet, we need >1.
  10117. */
  10118. cpumask_and(tmp, maskp, cpu_online_mask);
  10119. if (cpumask_weight(tmp) > 1)
  10120. continue;
  10121. /* Now that we have an irq to shutdown, get the eq
  10122. * mapped to this irq. Note: multiple hdwq's in
  10123. * the software can share an eq, but eventually
  10124. * only eq will be mapped to this vector
  10125. */
  10126. eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
  10127. list_add(&eq->_poll_list, eqlist);
  10128. }
  10129. kfree(tmp);
  10130. return 0;
  10131. }
  10132. static void __lpfc_cpuhp_remove(struct lpfc_hba *phba)
  10133. {
  10134. if (phba->sli_rev != LPFC_SLI_REV4)
  10135. return;
  10136. cpuhp_state_remove_instance_nocalls(lpfc_cpuhp_state,
  10137. &phba->cpuhp);
  10138. /*
  10139. * unregistering the instance doesn't stop the polling
  10140. * timer. Wait for the poll timer to retire.
  10141. */
  10142. synchronize_rcu();
  10143. del_timer_sync(&phba->cpuhp_poll_timer);
  10144. }
  10145. static void lpfc_cpuhp_remove(struct lpfc_hba *phba)
  10146. {
  10147. if (phba->pport->fc_flag & FC_OFFLINE_MODE)
  10148. return;
  10149. __lpfc_cpuhp_remove(phba);
  10150. }
  10151. static void lpfc_cpuhp_add(struct lpfc_hba *phba)
  10152. {
  10153. if (phba->sli_rev != LPFC_SLI_REV4)
  10154. return;
  10155. rcu_read_lock();
  10156. if (!list_empty(&phba->poll_list))
  10157. mod_timer(&phba->cpuhp_poll_timer,
  10158. jiffies + msecs_to_jiffies(LPFC_POLL_HB));
  10159. rcu_read_unlock();
  10160. cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state,
  10161. &phba->cpuhp);
  10162. }
  10163. static int __lpfc_cpuhp_checks(struct lpfc_hba *phba, int *retval)
  10164. {
  10165. if (phba->pport->load_flag & FC_UNLOADING) {
  10166. *retval = -EAGAIN;
  10167. return true;
  10168. }
  10169. if (phba->sli_rev != LPFC_SLI_REV4) {
  10170. *retval = 0;
  10171. return true;
  10172. }
  10173. /* proceed with the hotplug */
  10174. return false;
  10175. }
  10176. /**
  10177. * lpfc_irq_set_aff - set IRQ affinity
  10178. * @eqhdl: EQ handle
  10179. * @cpu: cpu to set affinity
  10180. *
  10181. **/
  10182. static inline void
  10183. lpfc_irq_set_aff(struct lpfc_hba_eq_hdl *eqhdl, unsigned int cpu)
  10184. {
  10185. cpumask_clear(&eqhdl->aff_mask);
  10186. cpumask_set_cpu(cpu, &eqhdl->aff_mask);
  10187. irq_set_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
  10188. irq_set_affinity_hint(eqhdl->irq, &eqhdl->aff_mask);
  10189. }
  10190. /**
  10191. * lpfc_irq_clear_aff - clear IRQ affinity
  10192. * @eqhdl: EQ handle
  10193. *
  10194. **/
  10195. static inline void
  10196. lpfc_irq_clear_aff(struct lpfc_hba_eq_hdl *eqhdl)
  10197. {
  10198. cpumask_clear(&eqhdl->aff_mask);
  10199. irq_clear_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
  10200. irq_set_affinity_hint(eqhdl->irq, &eqhdl->aff_mask);
  10201. }
  10202. /**
  10203. * lpfc_irq_rebalance - rebalances IRQ affinity according to cpuhp event
  10204. * @phba: pointer to HBA context object.
  10205. * @cpu: cpu going offline/online
  10206. * @offline: true, cpu is going offline. false, cpu is coming online.
  10207. *
  10208. * If cpu is going offline, we'll try our best effort to find the next
  10209. * online cpu on the phba's NUMA node and migrate all offlining IRQ affinities.
  10210. *
  10211. * If cpu is coming online, reaffinitize the IRQ back to the onlineng cpu.
  10212. *
  10213. * Note: Call only if cfg_irq_numa is enabled, otherwise rely on
  10214. * PCI_IRQ_AFFINITY to auto-manage IRQ affinity.
  10215. *
  10216. **/
  10217. static void
  10218. lpfc_irq_rebalance(struct lpfc_hba *phba, unsigned int cpu, bool offline)
  10219. {
  10220. struct lpfc_vector_map_info *cpup;
  10221. struct cpumask *aff_mask;
  10222. unsigned int cpu_select, cpu_next, idx;
  10223. const struct cpumask *numa_mask;
  10224. if (!phba->cfg_irq_numa)
  10225. return;
  10226. numa_mask = &phba->sli4_hba.numa_mask;
  10227. if (!cpumask_test_cpu(cpu, numa_mask))
  10228. return;
  10229. cpup = &phba->sli4_hba.cpu_map[cpu];
  10230. if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
  10231. return;
  10232. if (offline) {
  10233. /* Find next online CPU on NUMA node */
  10234. cpu_next = cpumask_next_wrap(cpu, numa_mask, cpu, true);
  10235. cpu_select = lpfc_next_online_numa_cpu(numa_mask, cpu_next);
  10236. /* Found a valid CPU */
  10237. if ((cpu_select < nr_cpu_ids) && (cpu_select != cpu)) {
  10238. /* Go through each eqhdl and ensure offlining
  10239. * cpu aff_mask is migrated
  10240. */
  10241. for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
  10242. aff_mask = lpfc_get_aff_mask(idx);
  10243. /* Migrate affinity */
  10244. if (cpumask_test_cpu(cpu, aff_mask))
  10245. lpfc_irq_set_aff(lpfc_get_eq_hdl(idx),
  10246. cpu_select);
  10247. }
  10248. } else {
  10249. /* Rely on irqbalance if no online CPUs left on NUMA */
  10250. for (idx = 0; idx < phba->cfg_irq_chann; idx++)
  10251. lpfc_irq_clear_aff(lpfc_get_eq_hdl(idx));
  10252. }
  10253. } else {
  10254. /* Migrate affinity back to this CPU */
  10255. lpfc_irq_set_aff(lpfc_get_eq_hdl(cpup->eq), cpu);
  10256. }
  10257. }
  10258. static int lpfc_cpu_offline(unsigned int cpu, struct hlist_node *node)
  10259. {
  10260. struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
  10261. struct lpfc_queue *eq, *next;
  10262. LIST_HEAD(eqlist);
  10263. int retval;
  10264. if (!phba) {
  10265. WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
  10266. return 0;
  10267. }
  10268. if (__lpfc_cpuhp_checks(phba, &retval))
  10269. return retval;
  10270. lpfc_irq_rebalance(phba, cpu, true);
  10271. retval = lpfc_cpuhp_get_eq(phba, cpu, &eqlist);
  10272. if (retval)
  10273. return retval;
  10274. /* start polling on these eq's */
  10275. list_for_each_entry_safe(eq, next, &eqlist, _poll_list) {
  10276. list_del_init(&eq->_poll_list);
  10277. lpfc_sli4_start_polling(eq);
  10278. }
  10279. return 0;
  10280. }
  10281. static int lpfc_cpu_online(unsigned int cpu, struct hlist_node *node)
  10282. {
  10283. struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
  10284. struct lpfc_queue *eq, *next;
  10285. unsigned int n;
  10286. int retval;
  10287. if (!phba) {
  10288. WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
  10289. return 0;
  10290. }
  10291. if (__lpfc_cpuhp_checks(phba, &retval))
  10292. return retval;
  10293. lpfc_irq_rebalance(phba, cpu, false);
  10294. list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list) {
  10295. n = lpfc_find_cpu_handle(phba, eq->hdwq, LPFC_FIND_BY_HDWQ);
  10296. if (n == cpu)
  10297. lpfc_sli4_stop_polling(eq);
  10298. }
  10299. return 0;
  10300. }
  10301. /**
  10302. * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
  10303. * @phba: pointer to lpfc hba data structure.
  10304. *
  10305. * This routine is invoked to enable the MSI-X interrupt vectors to device
  10306. * with SLI-4 interface spec. It also allocates MSI-X vectors and maps them
  10307. * to cpus on the system.
  10308. *
  10309. * When cfg_irq_numa is enabled, the adapter will only allocate vectors for
  10310. * the number of cpus on the same numa node as this adapter. The vectors are
  10311. * allocated without requesting OS affinity mapping. A vector will be
  10312. * allocated and assigned to each online and offline cpu. If the cpu is
  10313. * online, then affinity will be set to that cpu. If the cpu is offline, then
  10314. * affinity will be set to the nearest peer cpu within the numa node that is
  10315. * online. If there are no online cpus within the numa node, affinity is not
  10316. * assigned and the OS may do as it pleases. Note: cpu vector affinity mapping
  10317. * is consistent with the way cpu online/offline is handled when cfg_irq_numa is
  10318. * configured.
  10319. *
  10320. * If numa mode is not enabled and there is more than 1 vector allocated, then
  10321. * the driver relies on the managed irq interface where the OS assigns vector to
  10322. * cpu affinity. The driver will then use that affinity mapping to setup its
  10323. * cpu mapping table.
  10324. *
  10325. * Return codes
  10326. * 0 - successful
  10327. * other values - error
  10328. **/
  10329. static int
  10330. lpfc_sli4_enable_msix(struct lpfc_hba *phba)
  10331. {
  10332. int vectors, rc, index;
  10333. char *name;
  10334. const struct cpumask *numa_mask = NULL;
  10335. unsigned int cpu = 0, cpu_cnt = 0, cpu_select = nr_cpu_ids;
  10336. struct lpfc_hba_eq_hdl *eqhdl;
  10337. const struct cpumask *maskp;
  10338. bool first;
  10339. unsigned int flags = PCI_IRQ_MSIX;
  10340. /* Set up MSI-X multi-message vectors */
  10341. vectors = phba->cfg_irq_chann;
  10342. if (phba->cfg_irq_numa) {
  10343. numa_mask = &phba->sli4_hba.numa_mask;
  10344. cpu_cnt = cpumask_weight(numa_mask);
  10345. vectors = min(phba->cfg_irq_chann, cpu_cnt);
  10346. /* cpu: iterates over numa_mask including offline or online
  10347. * cpu_select: iterates over online numa_mask to set affinity
  10348. */
  10349. cpu = cpumask_first(numa_mask);
  10350. cpu_select = lpfc_next_online_numa_cpu(numa_mask, cpu);
  10351. } else {
  10352. flags |= PCI_IRQ_AFFINITY;
  10353. }
  10354. rc = pci_alloc_irq_vectors(phba->pcidev, 1, vectors, flags);
  10355. if (rc < 0) {
  10356. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10357. "0484 PCI enable MSI-X failed (%d)\n", rc);
  10358. goto vec_fail_out;
  10359. }
  10360. vectors = rc;
  10361. /* Assign MSI-X vectors to interrupt handlers */
  10362. for (index = 0; index < vectors; index++) {
  10363. eqhdl = lpfc_get_eq_hdl(index);
  10364. name = eqhdl->handler_name;
  10365. memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
  10366. snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
  10367. LPFC_DRIVER_HANDLER_NAME"%d", index);
  10368. eqhdl->idx = index;
  10369. rc = request_irq(pci_irq_vector(phba->pcidev, index),
  10370. &lpfc_sli4_hba_intr_handler, 0,
  10371. name, eqhdl);
  10372. if (rc) {
  10373. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  10374. "0486 MSI-X fast-path (%d) "
  10375. "request_irq failed (%d)\n", index, rc);
  10376. goto cfg_fail_out;
  10377. }
  10378. eqhdl->irq = pci_irq_vector(phba->pcidev, index);
  10379. if (phba->cfg_irq_numa) {
  10380. /* If found a neighboring online cpu, set affinity */
  10381. if (cpu_select < nr_cpu_ids)
  10382. lpfc_irq_set_aff(eqhdl, cpu_select);
  10383. /* Assign EQ to cpu_map */
  10384. lpfc_assign_eq_map_info(phba, index,
  10385. LPFC_CPU_FIRST_IRQ,
  10386. cpu);
  10387. /* Iterate to next offline or online cpu in numa_mask */
  10388. cpu = cpumask_next(cpu, numa_mask);
  10389. /* Find next online cpu in numa_mask to set affinity */
  10390. cpu_select = lpfc_next_online_numa_cpu(numa_mask, cpu);
  10391. } else if (vectors == 1) {
  10392. cpu = cpumask_first(cpu_present_mask);
  10393. lpfc_assign_eq_map_info(phba, index, LPFC_CPU_FIRST_IRQ,
  10394. cpu);
  10395. } else {
  10396. maskp = pci_irq_get_affinity(phba->pcidev, index);
  10397. first = true;
  10398. /* Loop through all CPUs associated with vector index */
  10399. for_each_cpu_and(cpu, maskp, cpu_present_mask) {
  10400. /* If this is the first CPU thats assigned to
  10401. * this vector, set LPFC_CPU_FIRST_IRQ.
  10402. */
  10403. lpfc_assign_eq_map_info(phba, index,
  10404. first ?
  10405. LPFC_CPU_FIRST_IRQ : 0,
  10406. cpu);
  10407. if (first)
  10408. first = false;
  10409. }
  10410. }
  10411. }
  10412. if (vectors != phba->cfg_irq_chann) {
  10413. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10414. "3238 Reducing IO channels to match number of "
  10415. "MSI-X vectors, requested %d got %d\n",
  10416. phba->cfg_irq_chann, vectors);
  10417. if (phba->cfg_irq_chann > vectors)
  10418. phba->cfg_irq_chann = vectors;
  10419. }
  10420. return rc;
  10421. cfg_fail_out:
  10422. /* free the irq already requested */
  10423. for (--index; index >= 0; index--) {
  10424. eqhdl = lpfc_get_eq_hdl(index);
  10425. lpfc_irq_clear_aff(eqhdl);
  10426. irq_set_affinity_hint(eqhdl->irq, NULL);
  10427. free_irq(eqhdl->irq, eqhdl);
  10428. }
  10429. /* Unconfigure MSI-X capability structure */
  10430. pci_free_irq_vectors(phba->pcidev);
  10431. vec_fail_out:
  10432. return rc;
  10433. }
  10434. /**
  10435. * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
  10436. * @phba: pointer to lpfc hba data structure.
  10437. *
  10438. * This routine is invoked to enable the MSI interrupt mode to device with
  10439. * SLI-4 interface spec. The kernel function pci_alloc_irq_vectors() is
  10440. * called to enable the MSI vector. The device driver is responsible for
  10441. * calling the request_irq() to register MSI vector with a interrupt the
  10442. * handler, which is done in this function.
  10443. *
  10444. * Return codes
  10445. * 0 - successful
  10446. * other values - error
  10447. **/
  10448. static int
  10449. lpfc_sli4_enable_msi(struct lpfc_hba *phba)
  10450. {
  10451. int rc, index;
  10452. unsigned int cpu;
  10453. struct lpfc_hba_eq_hdl *eqhdl;
  10454. rc = pci_alloc_irq_vectors(phba->pcidev, 1, 1,
  10455. PCI_IRQ_MSI | PCI_IRQ_AFFINITY);
  10456. if (rc > 0)
  10457. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10458. "0487 PCI enable MSI mode success.\n");
  10459. else {
  10460. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10461. "0488 PCI enable MSI mode failed (%d)\n", rc);
  10462. return rc ? rc : -1;
  10463. }
  10464. rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
  10465. 0, LPFC_DRIVER_NAME, phba);
  10466. if (rc) {
  10467. pci_free_irq_vectors(phba->pcidev);
  10468. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  10469. "0490 MSI request_irq failed (%d)\n", rc);
  10470. return rc;
  10471. }
  10472. eqhdl = lpfc_get_eq_hdl(0);
  10473. eqhdl->irq = pci_irq_vector(phba->pcidev, 0);
  10474. cpu = cpumask_first(cpu_present_mask);
  10475. lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ, cpu);
  10476. for (index = 0; index < phba->cfg_irq_chann; index++) {
  10477. eqhdl = lpfc_get_eq_hdl(index);
  10478. eqhdl->idx = index;
  10479. }
  10480. return 0;
  10481. }
  10482. /**
  10483. * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
  10484. * @phba: pointer to lpfc hba data structure.
  10485. *
  10486. * This routine is invoked to enable device interrupt and associate driver's
  10487. * interrupt handler(s) to interrupt vector(s) to device with SLI-4
  10488. * interface spec. Depends on the interrupt mode configured to the driver,
  10489. * the driver will try to fallback from the configured interrupt mode to an
  10490. * interrupt mode which is supported by the platform, kernel, and device in
  10491. * the order of:
  10492. * MSI-X -> MSI -> IRQ.
  10493. *
  10494. * Return codes
  10495. * 0 - successful
  10496. * other values - error
  10497. **/
  10498. static uint32_t
  10499. lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
  10500. {
  10501. uint32_t intr_mode = LPFC_INTR_ERROR;
  10502. int retval, idx;
  10503. if (cfg_mode == 2) {
  10504. /* Preparation before conf_msi mbox cmd */
  10505. retval = 0;
  10506. if (!retval) {
  10507. /* Now, try to enable MSI-X interrupt mode */
  10508. retval = lpfc_sli4_enable_msix(phba);
  10509. if (!retval) {
  10510. /* Indicate initialization to MSI-X mode */
  10511. phba->intr_type = MSIX;
  10512. intr_mode = 2;
  10513. }
  10514. }
  10515. }
  10516. /* Fallback to MSI if MSI-X initialization failed */
  10517. if (cfg_mode >= 1 && phba->intr_type == NONE) {
  10518. retval = lpfc_sli4_enable_msi(phba);
  10519. if (!retval) {
  10520. /* Indicate initialization to MSI mode */
  10521. phba->intr_type = MSI;
  10522. intr_mode = 1;
  10523. }
  10524. }
  10525. /* Fallback to INTx if both MSI-X/MSI initalization failed */
  10526. if (phba->intr_type == NONE) {
  10527. retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
  10528. IRQF_SHARED, LPFC_DRIVER_NAME, phba);
  10529. if (!retval) {
  10530. struct lpfc_hba_eq_hdl *eqhdl;
  10531. unsigned int cpu;
  10532. /* Indicate initialization to INTx mode */
  10533. phba->intr_type = INTx;
  10534. intr_mode = 0;
  10535. eqhdl = lpfc_get_eq_hdl(0);
  10536. eqhdl->irq = pci_irq_vector(phba->pcidev, 0);
  10537. cpu = cpumask_first(cpu_present_mask);
  10538. lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ,
  10539. cpu);
  10540. for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
  10541. eqhdl = lpfc_get_eq_hdl(idx);
  10542. eqhdl->idx = idx;
  10543. }
  10544. }
  10545. }
  10546. return intr_mode;
  10547. }
  10548. /**
  10549. * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
  10550. * @phba: pointer to lpfc hba data structure.
  10551. *
  10552. * This routine is invoked to disable device interrupt and disassociate
  10553. * the driver's interrupt handler(s) from interrupt vector(s) to device
  10554. * with SLI-4 interface spec. Depending on the interrupt mode, the driver
  10555. * will release the interrupt vector(s) for the message signaled interrupt.
  10556. **/
  10557. static void
  10558. lpfc_sli4_disable_intr(struct lpfc_hba *phba)
  10559. {
  10560. /* Disable the currently initialized interrupt mode */
  10561. if (phba->intr_type == MSIX) {
  10562. int index;
  10563. struct lpfc_hba_eq_hdl *eqhdl;
  10564. /* Free up MSI-X multi-message vectors */
  10565. for (index = 0; index < phba->cfg_irq_chann; index++) {
  10566. eqhdl = lpfc_get_eq_hdl(index);
  10567. lpfc_irq_clear_aff(eqhdl);
  10568. irq_set_affinity_hint(eqhdl->irq, NULL);
  10569. free_irq(eqhdl->irq, eqhdl);
  10570. }
  10571. } else {
  10572. free_irq(phba->pcidev->irq, phba);
  10573. }
  10574. pci_free_irq_vectors(phba->pcidev);
  10575. /* Reset interrupt management states */
  10576. phba->intr_type = NONE;
  10577. phba->sli.slistat.sli_intr = 0;
  10578. }
  10579. /**
  10580. * lpfc_unset_hba - Unset SLI3 hba device initialization
  10581. * @phba: pointer to lpfc hba data structure.
  10582. *
  10583. * This routine is invoked to unset the HBA device initialization steps to
  10584. * a device with SLI-3 interface spec.
  10585. **/
  10586. static void
  10587. lpfc_unset_hba(struct lpfc_hba *phba)
  10588. {
  10589. struct lpfc_vport *vport = phba->pport;
  10590. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  10591. spin_lock_irq(shost->host_lock);
  10592. vport->load_flag |= FC_UNLOADING;
  10593. spin_unlock_irq(shost->host_lock);
  10594. kfree(phba->vpi_bmask);
  10595. kfree(phba->vpi_ids);
  10596. lpfc_stop_hba_timers(phba);
  10597. phba->pport->work_port_events = 0;
  10598. lpfc_sli_hba_down(phba);
  10599. lpfc_sli_brdrestart(phba);
  10600. lpfc_sli_disable_intr(phba);
  10601. return;
  10602. }
  10603. /**
  10604. * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
  10605. * @phba: Pointer to HBA context object.
  10606. *
  10607. * This function is called in the SLI4 code path to wait for completion
  10608. * of device's XRIs exchange busy. It will check the XRI exchange busy
  10609. * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
  10610. * that, it will check the XRI exchange busy on outstanding FCP and ELS
  10611. * I/Os every 30 seconds, log error message, and wait forever. Only when
  10612. * all XRI exchange busy complete, the driver unload shall proceed with
  10613. * invoking the function reset ioctl mailbox command to the CNA and the
  10614. * the rest of the driver unload resource release.
  10615. **/
  10616. static void
  10617. lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
  10618. {
  10619. struct lpfc_sli4_hdw_queue *qp;
  10620. int idx, ccnt;
  10621. int wait_time = 0;
  10622. int io_xri_cmpl = 1;
  10623. int nvmet_xri_cmpl = 1;
  10624. int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
  10625. /* Driver just aborted IOs during the hba_unset process. Pause
  10626. * here to give the HBA time to complete the IO and get entries
  10627. * into the abts lists.
  10628. */
  10629. msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
  10630. /* Wait for NVME pending IO to flush back to transport. */
  10631. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  10632. lpfc_nvme_wait_for_io_drain(phba);
  10633. ccnt = 0;
  10634. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  10635. qp = &phba->sli4_hba.hdwq[idx];
  10636. io_xri_cmpl = list_empty(&qp->lpfc_abts_io_buf_list);
  10637. if (!io_xri_cmpl) /* if list is NOT empty */
  10638. ccnt++;
  10639. }
  10640. if (ccnt)
  10641. io_xri_cmpl = 0;
  10642. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  10643. nvmet_xri_cmpl =
  10644. list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
  10645. }
  10646. while (!els_xri_cmpl || !io_xri_cmpl || !nvmet_xri_cmpl) {
  10647. if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
  10648. if (!nvmet_xri_cmpl)
  10649. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10650. "6424 NVMET XRI exchange busy "
  10651. "wait time: %d seconds.\n",
  10652. wait_time/1000);
  10653. if (!io_xri_cmpl)
  10654. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10655. "6100 IO XRI exchange busy "
  10656. "wait time: %d seconds.\n",
  10657. wait_time/1000);
  10658. if (!els_xri_cmpl)
  10659. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10660. "2878 ELS XRI exchange busy "
  10661. "wait time: %d seconds.\n",
  10662. wait_time/1000);
  10663. msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
  10664. wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
  10665. } else {
  10666. msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
  10667. wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
  10668. }
  10669. ccnt = 0;
  10670. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  10671. qp = &phba->sli4_hba.hdwq[idx];
  10672. io_xri_cmpl = list_empty(
  10673. &qp->lpfc_abts_io_buf_list);
  10674. if (!io_xri_cmpl) /* if list is NOT empty */
  10675. ccnt++;
  10676. }
  10677. if (ccnt)
  10678. io_xri_cmpl = 0;
  10679. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  10680. nvmet_xri_cmpl = list_empty(
  10681. &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
  10682. }
  10683. els_xri_cmpl =
  10684. list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
  10685. }
  10686. }
  10687. /**
  10688. * lpfc_sli4_hba_unset - Unset the fcoe hba
  10689. * @phba: Pointer to HBA context object.
  10690. *
  10691. * This function is called in the SLI4 code path to reset the HBA's FCoE
  10692. * function. The caller is not required to hold any lock. This routine
  10693. * issues PCI function reset mailbox command to reset the FCoE function.
  10694. * At the end of the function, it calls lpfc_hba_down_post function to
  10695. * free any pending commands.
  10696. **/
  10697. static void
  10698. lpfc_sli4_hba_unset(struct lpfc_hba *phba)
  10699. {
  10700. int wait_cnt = 0;
  10701. LPFC_MBOXQ_t *mboxq;
  10702. struct pci_dev *pdev = phba->pcidev;
  10703. lpfc_stop_hba_timers(phba);
  10704. if (phba->pport)
  10705. phba->sli4_hba.intr_enable = 0;
  10706. /*
  10707. * Gracefully wait out the potential current outstanding asynchronous
  10708. * mailbox command.
  10709. */
  10710. /* First, block any pending async mailbox command from posted */
  10711. spin_lock_irq(&phba->hbalock);
  10712. phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  10713. spin_unlock_irq(&phba->hbalock);
  10714. /* Now, trying to wait it out if we can */
  10715. while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  10716. msleep(10);
  10717. if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
  10718. break;
  10719. }
  10720. /* Forcefully release the outstanding mailbox command if timed out */
  10721. if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  10722. spin_lock_irq(&phba->hbalock);
  10723. mboxq = phba->sli.mbox_active;
  10724. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  10725. __lpfc_mbox_cmpl_put(phba, mboxq);
  10726. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10727. phba->sli.mbox_active = NULL;
  10728. spin_unlock_irq(&phba->hbalock);
  10729. }
  10730. /* Abort all iocbs associated with the hba */
  10731. lpfc_sli_hba_iocb_abort(phba);
  10732. /* Wait for completion of device XRI exchange busy */
  10733. lpfc_sli4_xri_exchange_busy_wait(phba);
  10734. /* per-phba callback de-registration for hotplug event */
  10735. lpfc_cpuhp_remove(phba);
  10736. /* Disable PCI subsystem interrupt */
  10737. lpfc_sli4_disable_intr(phba);
  10738. /* Disable SR-IOV if enabled */
  10739. if (phba->cfg_sriov_nr_virtfn)
  10740. pci_disable_sriov(pdev);
  10741. /* Stop kthread signal shall trigger work_done one more time */
  10742. kthread_stop(phba->worker_thread);
  10743. /* Disable FW logging to host memory */
  10744. lpfc_ras_stop_fwlog(phba);
  10745. /* Unset the queues shared with the hardware then release all
  10746. * allocated resources.
  10747. */
  10748. lpfc_sli4_queue_unset(phba);
  10749. lpfc_sli4_queue_destroy(phba);
  10750. /* Reset SLI4 HBA FCoE function */
  10751. lpfc_pci_function_reset(phba);
  10752. /* Free RAS DMA memory */
  10753. if (phba->ras_fwlog.ras_enabled)
  10754. lpfc_sli4_ras_dma_free(phba);
  10755. /* Stop the SLI4 device port */
  10756. if (phba->pport)
  10757. phba->pport->work_port_events = 0;
  10758. }
  10759. /**
  10760. * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
  10761. * @phba: Pointer to HBA context object.
  10762. * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
  10763. *
  10764. * This function is called in the SLI4 code path to read the port's
  10765. * sli4 capabilities.
  10766. *
  10767. * This function may be be called from any context that can block-wait
  10768. * for the completion. The expectation is that this routine is called
  10769. * typically from probe_one or from the online routine.
  10770. **/
  10771. int
  10772. lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  10773. {
  10774. int rc;
  10775. struct lpfc_mqe *mqe;
  10776. struct lpfc_pc_sli4_params *sli4_params;
  10777. uint32_t mbox_tmo;
  10778. rc = 0;
  10779. mqe = &mboxq->u.mqe;
  10780. /* Read the port's SLI4 Parameters port capabilities */
  10781. lpfc_pc_sli4_params(mboxq);
  10782. if (!phba->sli4_hba.intr_enable)
  10783. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  10784. else {
  10785. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  10786. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  10787. }
  10788. if (unlikely(rc))
  10789. return 1;
  10790. sli4_params = &phba->sli4_hba.pc_sli4_params;
  10791. sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
  10792. sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
  10793. sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
  10794. sli4_params->featurelevel_1 = bf_get(featurelevel_1,
  10795. &mqe->un.sli4_params);
  10796. sli4_params->featurelevel_2 = bf_get(featurelevel_2,
  10797. &mqe->un.sli4_params);
  10798. sli4_params->proto_types = mqe->un.sli4_params.word3;
  10799. sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
  10800. sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
  10801. sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
  10802. sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
  10803. sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
  10804. sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
  10805. sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
  10806. sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
  10807. sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
  10808. sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
  10809. sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
  10810. sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
  10811. sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
  10812. sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
  10813. sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
  10814. sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
  10815. sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
  10816. sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
  10817. sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
  10818. sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
  10819. /* Make sure that sge_supp_len can be handled by the driver */
  10820. if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
  10821. sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
  10822. return rc;
  10823. }
  10824. /**
  10825. * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
  10826. * @phba: Pointer to HBA context object.
  10827. * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
  10828. *
  10829. * This function is called in the SLI4 code path to read the port's
  10830. * sli4 capabilities.
  10831. *
  10832. * This function may be be called from any context that can block-wait
  10833. * for the completion. The expectation is that this routine is called
  10834. * typically from probe_one or from the online routine.
  10835. **/
  10836. int
  10837. lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  10838. {
  10839. int rc;
  10840. struct lpfc_mqe *mqe = &mboxq->u.mqe;
  10841. struct lpfc_pc_sli4_params *sli4_params;
  10842. uint32_t mbox_tmo;
  10843. int length;
  10844. bool exp_wqcq_pages = true;
  10845. struct lpfc_sli4_parameters *mbx_sli4_parameters;
  10846. /*
  10847. * By default, the driver assumes the SLI4 port requires RPI
  10848. * header postings. The SLI4_PARAM response will correct this
  10849. * assumption.
  10850. */
  10851. phba->sli4_hba.rpi_hdrs_in_use = 1;
  10852. /* Read the port's SLI4 Config Parameters */
  10853. length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
  10854. sizeof(struct lpfc_sli4_cfg_mhdr));
  10855. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  10856. LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
  10857. length, LPFC_SLI4_MBX_EMBED);
  10858. if (!phba->sli4_hba.intr_enable)
  10859. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  10860. else {
  10861. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  10862. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  10863. }
  10864. if (unlikely(rc))
  10865. return rc;
  10866. sli4_params = &phba->sli4_hba.pc_sli4_params;
  10867. mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
  10868. sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
  10869. sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
  10870. sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
  10871. sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
  10872. mbx_sli4_parameters);
  10873. sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
  10874. mbx_sli4_parameters);
  10875. if (bf_get(cfg_phwq, mbx_sli4_parameters))
  10876. phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
  10877. else
  10878. phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
  10879. sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
  10880. sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
  10881. sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
  10882. sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
  10883. sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
  10884. sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
  10885. sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
  10886. sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
  10887. sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
  10888. sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
  10889. sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
  10890. sli4_params->pls = bf_get(cfg_pvl, mbx_sli4_parameters);
  10891. sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
  10892. mbx_sli4_parameters);
  10893. sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
  10894. sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
  10895. mbx_sli4_parameters);
  10896. phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
  10897. phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
  10898. /* Check for Extended Pre-Registered SGL support */
  10899. phba->cfg_xpsgl = bf_get(cfg_xpsgl, mbx_sli4_parameters);
  10900. /* Check for firmware nvme support */
  10901. rc = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
  10902. bf_get(cfg_xib, mbx_sli4_parameters));
  10903. if (rc) {
  10904. /* Save this to indicate the Firmware supports NVME */
  10905. sli4_params->nvme = 1;
  10906. /* Firmware NVME support, check driver FC4 NVME support */
  10907. if (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) {
  10908. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
  10909. "6133 Disabling NVME support: "
  10910. "FC4 type not supported: x%x\n",
  10911. phba->cfg_enable_fc4_type);
  10912. goto fcponly;
  10913. }
  10914. } else {
  10915. /* No firmware NVME support, check driver FC4 NVME support */
  10916. sli4_params->nvme = 0;
  10917. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  10918. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
  10919. "6101 Disabling NVME support: Not "
  10920. "supported by firmware (%d %d) x%x\n",
  10921. bf_get(cfg_nvme, mbx_sli4_parameters),
  10922. bf_get(cfg_xib, mbx_sli4_parameters),
  10923. phba->cfg_enable_fc4_type);
  10924. fcponly:
  10925. phba->nvme_support = 0;
  10926. phba->nvmet_support = 0;
  10927. phba->cfg_nvmet_mrq = 0;
  10928. phba->cfg_nvme_seg_cnt = 0;
  10929. /* If no FC4 type support, move to just SCSI support */
  10930. if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
  10931. return -ENODEV;
  10932. phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
  10933. }
  10934. }
  10935. /* If the NVME FC4 type is enabled, scale the sg_seg_cnt to
  10936. * accommodate 512K and 1M IOs in a single nvme buf.
  10937. */
  10938. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  10939. phba->cfg_sg_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
  10940. /* Only embed PBDE for if_type 6, PBDE support requires xib be set */
  10941. if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  10942. LPFC_SLI_INTF_IF_TYPE_6) || (!bf_get(cfg_xib, mbx_sli4_parameters)))
  10943. phba->cfg_enable_pbde = 0;
  10944. /*
  10945. * To support Suppress Response feature we must satisfy 3 conditions.
  10946. * lpfc_suppress_rsp module parameter must be set (default).
  10947. * In SLI4-Parameters Descriptor:
  10948. * Extended Inline Buffers (XIB) must be supported.
  10949. * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
  10950. * (double negative).
  10951. */
  10952. if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
  10953. !(bf_get(cfg_nosr, mbx_sli4_parameters)))
  10954. phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
  10955. else
  10956. phba->cfg_suppress_rsp = 0;
  10957. if (bf_get(cfg_eqdr, mbx_sli4_parameters))
  10958. phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
  10959. /* Make sure that sge_supp_len can be handled by the driver */
  10960. if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
  10961. sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
  10962. /*
  10963. * Check whether the adapter supports an embedded copy of the
  10964. * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
  10965. * to use this option, 128-byte WQEs must be used.
  10966. */
  10967. if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
  10968. phba->fcp_embed_io = 1;
  10969. else
  10970. phba->fcp_embed_io = 0;
  10971. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
  10972. "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
  10973. bf_get(cfg_xib, mbx_sli4_parameters),
  10974. phba->cfg_enable_pbde,
  10975. phba->fcp_embed_io, phba->nvme_support,
  10976. phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
  10977. if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  10978. LPFC_SLI_INTF_IF_TYPE_2) &&
  10979. (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
  10980. LPFC_SLI_INTF_FAMILY_LNCR_A0))
  10981. exp_wqcq_pages = false;
  10982. if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
  10983. (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
  10984. exp_wqcq_pages &&
  10985. (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
  10986. phba->enab_exp_wqcq_pages = 1;
  10987. else
  10988. phba->enab_exp_wqcq_pages = 0;
  10989. /*
  10990. * Check if the SLI port supports MDS Diagnostics
  10991. */
  10992. if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
  10993. phba->mds_diags_support = 1;
  10994. else
  10995. phba->mds_diags_support = 0;
  10996. /*
  10997. * Check if the SLI port supports NSLER
  10998. */
  10999. if (bf_get(cfg_nsler, mbx_sli4_parameters))
  11000. phba->nsler = 1;
  11001. else
  11002. phba->nsler = 0;
  11003. return 0;
  11004. }
  11005. /**
  11006. * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
  11007. * @pdev: pointer to PCI device
  11008. * @pid: pointer to PCI device identifier
  11009. *
  11010. * This routine is to be called to attach a device with SLI-3 interface spec
  11011. * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
  11012. * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
  11013. * information of the device and driver to see if the driver state that it can
  11014. * support this kind of device. If the match is successful, the driver core
  11015. * invokes this routine. If this routine determines it can claim the HBA, it
  11016. * does all the initialization that it needs to do to handle the HBA properly.
  11017. *
  11018. * Return code
  11019. * 0 - driver can claim the device
  11020. * negative value - driver can not claim the device
  11021. **/
  11022. static int
  11023. lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
  11024. {
  11025. struct lpfc_hba *phba;
  11026. struct lpfc_vport *vport = NULL;
  11027. struct Scsi_Host *shost = NULL;
  11028. int error;
  11029. uint32_t cfg_mode, intr_mode;
  11030. /* Allocate memory for HBA structure */
  11031. phba = lpfc_hba_alloc(pdev);
  11032. if (!phba)
  11033. return -ENOMEM;
  11034. /* Perform generic PCI device enabling operation */
  11035. error = lpfc_enable_pci_dev(phba);
  11036. if (error)
  11037. goto out_free_phba;
  11038. /* Set up SLI API function jump table for PCI-device group-0 HBAs */
  11039. error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
  11040. if (error)
  11041. goto out_disable_pci_dev;
  11042. /* Set up SLI-3 specific device PCI memory space */
  11043. error = lpfc_sli_pci_mem_setup(phba);
  11044. if (error) {
  11045. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11046. "1402 Failed to set up pci memory space.\n");
  11047. goto out_disable_pci_dev;
  11048. }
  11049. /* Set up SLI-3 specific device driver resources */
  11050. error = lpfc_sli_driver_resource_setup(phba);
  11051. if (error) {
  11052. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11053. "1404 Failed to set up driver resource.\n");
  11054. goto out_unset_pci_mem_s3;
  11055. }
  11056. /* Initialize and populate the iocb list per host */
  11057. error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
  11058. if (error) {
  11059. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11060. "1405 Failed to initialize iocb list.\n");
  11061. goto out_unset_driver_resource_s3;
  11062. }
  11063. /* Set up common device driver resources */
  11064. error = lpfc_setup_driver_resource_phase2(phba);
  11065. if (error) {
  11066. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11067. "1406 Failed to set up driver resource.\n");
  11068. goto out_free_iocb_list;
  11069. }
  11070. /* Get the default values for Model Name and Description */
  11071. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  11072. /* Create SCSI host to the physical port */
  11073. error = lpfc_create_shost(phba);
  11074. if (error) {
  11075. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11076. "1407 Failed to create scsi host.\n");
  11077. goto out_unset_driver_resource;
  11078. }
  11079. /* Configure sysfs attributes */
  11080. vport = phba->pport;
  11081. error = lpfc_alloc_sysfs_attr(vport);
  11082. if (error) {
  11083. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11084. "1476 Failed to allocate sysfs attr\n");
  11085. goto out_destroy_shost;
  11086. }
  11087. shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
  11088. /* Now, trying to enable interrupt and bring up the device */
  11089. cfg_mode = phba->cfg_use_msi;
  11090. while (true) {
  11091. /* Put device to a known state before enabling interrupt */
  11092. lpfc_stop_port(phba);
  11093. /* Configure and enable interrupt */
  11094. intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
  11095. if (intr_mode == LPFC_INTR_ERROR) {
  11096. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11097. "0431 Failed to enable interrupt.\n");
  11098. error = -ENODEV;
  11099. goto out_free_sysfs_attr;
  11100. }
  11101. /* SLI-3 HBA setup */
  11102. if (lpfc_sli_hba_setup(phba)) {
  11103. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11104. "1477 Failed to set up hba\n");
  11105. error = -ENODEV;
  11106. goto out_remove_device;
  11107. }
  11108. /* Wait 50ms for the interrupts of previous mailbox commands */
  11109. msleep(50);
  11110. /* Check active interrupts on message signaled interrupts */
  11111. if (intr_mode == 0 ||
  11112. phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
  11113. /* Log the current active interrupt mode */
  11114. phba->intr_mode = intr_mode;
  11115. lpfc_log_intr_mode(phba, intr_mode);
  11116. break;
  11117. } else {
  11118. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11119. "0447 Configure interrupt mode (%d) "
  11120. "failed active interrupt test.\n",
  11121. intr_mode);
  11122. /* Disable the current interrupt mode */
  11123. lpfc_sli_disable_intr(phba);
  11124. /* Try next level of interrupt mode */
  11125. cfg_mode = --intr_mode;
  11126. }
  11127. }
  11128. /* Perform post initialization setup */
  11129. lpfc_post_init_setup(phba);
  11130. /* Check if there are static vports to be created. */
  11131. lpfc_create_static_vport(phba);
  11132. return 0;
  11133. out_remove_device:
  11134. lpfc_unset_hba(phba);
  11135. out_free_sysfs_attr:
  11136. lpfc_free_sysfs_attr(vport);
  11137. out_destroy_shost:
  11138. lpfc_destroy_shost(phba);
  11139. out_unset_driver_resource:
  11140. lpfc_unset_driver_resource_phase2(phba);
  11141. out_free_iocb_list:
  11142. lpfc_free_iocb_list(phba);
  11143. out_unset_driver_resource_s3:
  11144. lpfc_sli_driver_resource_unset(phba);
  11145. out_unset_pci_mem_s3:
  11146. lpfc_sli_pci_mem_unset(phba);
  11147. out_disable_pci_dev:
  11148. lpfc_disable_pci_dev(phba);
  11149. if (shost)
  11150. scsi_host_put(shost);
  11151. out_free_phba:
  11152. lpfc_hba_free(phba);
  11153. return error;
  11154. }
  11155. /**
  11156. * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
  11157. * @pdev: pointer to PCI device
  11158. *
  11159. * This routine is to be called to disattach a device with SLI-3 interface
  11160. * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
  11161. * removed from PCI bus, it performs all the necessary cleanup for the HBA
  11162. * device to be removed from the PCI subsystem properly.
  11163. **/
  11164. static void
  11165. lpfc_pci_remove_one_s3(struct pci_dev *pdev)
  11166. {
  11167. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  11168. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  11169. struct lpfc_vport **vports;
  11170. struct lpfc_hba *phba = vport->phba;
  11171. int i;
  11172. spin_lock_irq(&phba->hbalock);
  11173. vport->load_flag |= FC_UNLOADING;
  11174. spin_unlock_irq(&phba->hbalock);
  11175. lpfc_free_sysfs_attr(vport);
  11176. /* Release all the vports against this physical port */
  11177. vports = lpfc_create_vport_work_array(phba);
  11178. if (vports != NULL)
  11179. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  11180. if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
  11181. continue;
  11182. fc_vport_terminate(vports[i]->fc_vport);
  11183. }
  11184. lpfc_destroy_vport_work_array(phba, vports);
  11185. /* Remove FC host and then SCSI host with the physical port */
  11186. fc_remove_host(shost);
  11187. scsi_remove_host(shost);
  11188. lpfc_cleanup(vport);
  11189. /*
  11190. * Bring down the SLI Layer. This step disable all interrupts,
  11191. * clears the rings, discards all mailbox commands, and resets
  11192. * the HBA.
  11193. */
  11194. /* HBA interrupt will be disabled after this call */
  11195. lpfc_sli_hba_down(phba);
  11196. /* Stop kthread signal shall trigger work_done one more time */
  11197. kthread_stop(phba->worker_thread);
  11198. /* Final cleanup of txcmplq and reset the HBA */
  11199. lpfc_sli_brdrestart(phba);
  11200. kfree(phba->vpi_bmask);
  11201. kfree(phba->vpi_ids);
  11202. lpfc_stop_hba_timers(phba);
  11203. spin_lock_irq(&phba->port_list_lock);
  11204. list_del_init(&vport->listentry);
  11205. spin_unlock_irq(&phba->port_list_lock);
  11206. lpfc_debugfs_terminate(vport);
  11207. /* Disable SR-IOV if enabled */
  11208. if (phba->cfg_sriov_nr_virtfn)
  11209. pci_disable_sriov(pdev);
  11210. /* Disable interrupt */
  11211. lpfc_sli_disable_intr(phba);
  11212. scsi_host_put(shost);
  11213. /*
  11214. * Call scsi_free before mem_free since scsi bufs are released to their
  11215. * corresponding pools here.
  11216. */
  11217. lpfc_scsi_free(phba);
  11218. lpfc_free_iocb_list(phba);
  11219. lpfc_mem_free_all(phba);
  11220. dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
  11221. phba->hbqslimp.virt, phba->hbqslimp.phys);
  11222. /* Free resources associated with SLI2 interface */
  11223. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  11224. phba->slim2p.virt, phba->slim2p.phys);
  11225. /* unmap adapter SLIM and Control Registers */
  11226. iounmap(phba->ctrl_regs_memmap_p);
  11227. iounmap(phba->slim_memmap_p);
  11228. lpfc_hba_free(phba);
  11229. pci_release_mem_regions(pdev);
  11230. pci_disable_device(pdev);
  11231. }
  11232. /**
  11233. * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
  11234. * @pdev: pointer to PCI device
  11235. * @msg: power management message
  11236. *
  11237. * This routine is to be called from the kernel's PCI subsystem to support
  11238. * system Power Management (PM) to device with SLI-3 interface spec. When
  11239. * PM invokes this method, it quiesces the device by stopping the driver's
  11240. * worker thread for the device, turning off device's interrupt and DMA,
  11241. * and bring the device offline. Note that as the driver implements the
  11242. * minimum PM requirements to a power-aware driver's PM support for the
  11243. * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
  11244. * to the suspend() method call will be treated as SUSPEND and the driver will
  11245. * fully reinitialize its device during resume() method call, the driver will
  11246. * set device to PCI_D3hot state in PCI config space instead of setting it
  11247. * according to the @msg provided by the PM.
  11248. *
  11249. * Return code
  11250. * 0 - driver suspended the device
  11251. * Error otherwise
  11252. **/
  11253. static int
  11254. lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
  11255. {
  11256. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  11257. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  11258. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11259. "0473 PCI device Power Management suspend.\n");
  11260. /* Bring down the device */
  11261. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  11262. lpfc_offline(phba);
  11263. kthread_stop(phba->worker_thread);
  11264. /* Disable interrupt from device */
  11265. lpfc_sli_disable_intr(phba);
  11266. /* Save device state to PCI config space */
  11267. pci_save_state(pdev);
  11268. pci_set_power_state(pdev, PCI_D3hot);
  11269. return 0;
  11270. }
  11271. /**
  11272. * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
  11273. * @pdev: pointer to PCI device
  11274. *
  11275. * This routine is to be called from the kernel's PCI subsystem to support
  11276. * system Power Management (PM) to device with SLI-3 interface spec. When PM
  11277. * invokes this method, it restores the device's PCI config space state and
  11278. * fully reinitializes the device and brings it online. Note that as the
  11279. * driver implements the minimum PM requirements to a power-aware driver's
  11280. * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
  11281. * FREEZE) to the suspend() method call will be treated as SUSPEND and the
  11282. * driver will fully reinitialize its device during resume() method call,
  11283. * the device will be set to PCI_D0 directly in PCI config space before
  11284. * restoring the state.
  11285. *
  11286. * Return code
  11287. * 0 - driver suspended the device
  11288. * Error otherwise
  11289. **/
  11290. static int
  11291. lpfc_pci_resume_one_s3(struct pci_dev *pdev)
  11292. {
  11293. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  11294. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  11295. uint32_t intr_mode;
  11296. int error;
  11297. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11298. "0452 PCI device Power Management resume.\n");
  11299. /* Restore device state from PCI config space */
  11300. pci_set_power_state(pdev, PCI_D0);
  11301. pci_restore_state(pdev);
  11302. /*
  11303. * As the new kernel behavior of pci_restore_state() API call clears
  11304. * device saved_state flag, need to save the restored state again.
  11305. */
  11306. pci_save_state(pdev);
  11307. if (pdev->is_busmaster)
  11308. pci_set_master(pdev);
  11309. /* Startup the kernel thread for this host adapter. */
  11310. phba->worker_thread = kthread_run(lpfc_do_work, phba,
  11311. "lpfc_worker_%d", phba->brd_no);
  11312. if (IS_ERR(phba->worker_thread)) {
  11313. error = PTR_ERR(phba->worker_thread);
  11314. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11315. "0434 PM resume failed to start worker "
  11316. "thread: error=x%x.\n", error);
  11317. return error;
  11318. }
  11319. /* Configure and enable interrupt */
  11320. intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
  11321. if (intr_mode == LPFC_INTR_ERROR) {
  11322. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11323. "0430 PM resume Failed to enable interrupt\n");
  11324. return -EIO;
  11325. } else
  11326. phba->intr_mode = intr_mode;
  11327. /* Restart HBA and bring it online */
  11328. lpfc_sli_brdrestart(phba);
  11329. lpfc_online(phba);
  11330. /* Log the current active interrupt mode */
  11331. lpfc_log_intr_mode(phba, phba->intr_mode);
  11332. return 0;
  11333. }
  11334. /**
  11335. * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
  11336. * @phba: pointer to lpfc hba data structure.
  11337. *
  11338. * This routine is called to prepare the SLI3 device for PCI slot recover. It
  11339. * aborts all the outstanding SCSI I/Os to the pci device.
  11340. **/
  11341. static void
  11342. lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
  11343. {
  11344. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11345. "2723 PCI channel I/O abort preparing for recovery\n");
  11346. /*
  11347. * There may be errored I/Os through HBA, abort all I/Os on txcmplq
  11348. * and let the SCSI mid-layer to retry them to recover.
  11349. */
  11350. lpfc_sli_abort_fcp_rings(phba);
  11351. }
  11352. /**
  11353. * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
  11354. * @phba: pointer to lpfc hba data structure.
  11355. *
  11356. * This routine is called to prepare the SLI3 device for PCI slot reset. It
  11357. * disables the device interrupt and pci device, and aborts the internal FCP
  11358. * pending I/Os.
  11359. **/
  11360. static void
  11361. lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
  11362. {
  11363. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11364. "2710 PCI channel disable preparing for reset\n");
  11365. /* Block any management I/Os to the device */
  11366. lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
  11367. /* Block all SCSI devices' I/Os on the host */
  11368. lpfc_scsi_dev_block(phba);
  11369. /* Flush all driver's outstanding SCSI I/Os as we are to reset */
  11370. lpfc_sli_flush_io_rings(phba);
  11371. /* stop all timers */
  11372. lpfc_stop_hba_timers(phba);
  11373. /* Disable interrupt and pci device */
  11374. lpfc_sli_disable_intr(phba);
  11375. pci_disable_device(phba->pcidev);
  11376. }
  11377. /**
  11378. * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
  11379. * @phba: pointer to lpfc hba data structure.
  11380. *
  11381. * This routine is called to prepare the SLI3 device for PCI slot permanently
  11382. * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
  11383. * pending I/Os.
  11384. **/
  11385. static void
  11386. lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
  11387. {
  11388. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11389. "2711 PCI channel permanent disable for failure\n");
  11390. /* Block all SCSI devices' I/Os on the host */
  11391. lpfc_scsi_dev_block(phba);
  11392. /* stop all timers */
  11393. lpfc_stop_hba_timers(phba);
  11394. /* Clean up all driver's outstanding SCSI I/Os */
  11395. lpfc_sli_flush_io_rings(phba);
  11396. }
  11397. /**
  11398. * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
  11399. * @pdev: pointer to PCI device.
  11400. * @state: the current PCI connection state.
  11401. *
  11402. * This routine is called from the PCI subsystem for I/O error handling to
  11403. * device with SLI-3 interface spec. This function is called by the PCI
  11404. * subsystem after a PCI bus error affecting this device has been detected.
  11405. * When this function is invoked, it will need to stop all the I/Os and
  11406. * interrupt(s) to the device. Once that is done, it will return
  11407. * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
  11408. * as desired.
  11409. *
  11410. * Return codes
  11411. * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
  11412. * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
  11413. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  11414. **/
  11415. static pci_ers_result_t
  11416. lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
  11417. {
  11418. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  11419. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  11420. switch (state) {
  11421. case pci_channel_io_normal:
  11422. /* Non-fatal error, prepare for recovery */
  11423. lpfc_sli_prep_dev_for_recover(phba);
  11424. return PCI_ERS_RESULT_CAN_RECOVER;
  11425. case pci_channel_io_frozen:
  11426. /* Fatal error, prepare for slot reset */
  11427. lpfc_sli_prep_dev_for_reset(phba);
  11428. return PCI_ERS_RESULT_NEED_RESET;
  11429. case pci_channel_io_perm_failure:
  11430. /* Permanent failure, prepare for device down */
  11431. lpfc_sli_prep_dev_for_perm_failure(phba);
  11432. return PCI_ERS_RESULT_DISCONNECT;
  11433. default:
  11434. /* Unknown state, prepare and request slot reset */
  11435. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11436. "0472 Unknown PCI error state: x%x\n", state);
  11437. lpfc_sli_prep_dev_for_reset(phba);
  11438. return PCI_ERS_RESULT_NEED_RESET;
  11439. }
  11440. }
  11441. /**
  11442. * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
  11443. * @pdev: pointer to PCI device.
  11444. *
  11445. * This routine is called from the PCI subsystem for error handling to
  11446. * device with SLI-3 interface spec. This is called after PCI bus has been
  11447. * reset to restart the PCI card from scratch, as if from a cold-boot.
  11448. * During the PCI subsystem error recovery, after driver returns
  11449. * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
  11450. * recovery and then call this routine before calling the .resume method
  11451. * to recover the device. This function will initialize the HBA device,
  11452. * enable the interrupt, but it will just put the HBA to offline state
  11453. * without passing any I/O traffic.
  11454. *
  11455. * Return codes
  11456. * PCI_ERS_RESULT_RECOVERED - the device has been recovered
  11457. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  11458. */
  11459. static pci_ers_result_t
  11460. lpfc_io_slot_reset_s3(struct pci_dev *pdev)
  11461. {
  11462. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  11463. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  11464. struct lpfc_sli *psli = &phba->sli;
  11465. uint32_t intr_mode;
  11466. dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
  11467. if (pci_enable_device_mem(pdev)) {
  11468. printk(KERN_ERR "lpfc: Cannot re-enable "
  11469. "PCI device after reset.\n");
  11470. return PCI_ERS_RESULT_DISCONNECT;
  11471. }
  11472. pci_restore_state(pdev);
  11473. /*
  11474. * As the new kernel behavior of pci_restore_state() API call clears
  11475. * device saved_state flag, need to save the restored state again.
  11476. */
  11477. pci_save_state(pdev);
  11478. if (pdev->is_busmaster)
  11479. pci_set_master(pdev);
  11480. spin_lock_irq(&phba->hbalock);
  11481. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  11482. spin_unlock_irq(&phba->hbalock);
  11483. /* Configure and enable interrupt */
  11484. intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
  11485. if (intr_mode == LPFC_INTR_ERROR) {
  11486. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11487. "0427 Cannot re-enable interrupt after "
  11488. "slot reset.\n");
  11489. return PCI_ERS_RESULT_DISCONNECT;
  11490. } else
  11491. phba->intr_mode = intr_mode;
  11492. /* Take device offline, it will perform cleanup */
  11493. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  11494. lpfc_offline(phba);
  11495. lpfc_sli_brdrestart(phba);
  11496. /* Log the current active interrupt mode */
  11497. lpfc_log_intr_mode(phba, phba->intr_mode);
  11498. return PCI_ERS_RESULT_RECOVERED;
  11499. }
  11500. /**
  11501. * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
  11502. * @pdev: pointer to PCI device
  11503. *
  11504. * This routine is called from the PCI subsystem for error handling to device
  11505. * with SLI-3 interface spec. It is called when kernel error recovery tells
  11506. * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
  11507. * error recovery. After this call, traffic can start to flow from this device
  11508. * again.
  11509. */
  11510. static void
  11511. lpfc_io_resume_s3(struct pci_dev *pdev)
  11512. {
  11513. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  11514. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  11515. /* Bring device online, it will be no-op for non-fatal error resume */
  11516. lpfc_online(phba);
  11517. }
  11518. /**
  11519. * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
  11520. * @phba: pointer to lpfc hba data structure.
  11521. *
  11522. * returns the number of ELS/CT IOCBs to reserve
  11523. **/
  11524. int
  11525. lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
  11526. {
  11527. int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
  11528. if (phba->sli_rev == LPFC_SLI_REV4) {
  11529. if (max_xri <= 100)
  11530. return 10;
  11531. else if (max_xri <= 256)
  11532. return 25;
  11533. else if (max_xri <= 512)
  11534. return 50;
  11535. else if (max_xri <= 1024)
  11536. return 100;
  11537. else if (max_xri <= 1536)
  11538. return 150;
  11539. else if (max_xri <= 2048)
  11540. return 200;
  11541. else
  11542. return 250;
  11543. } else
  11544. return 0;
  11545. }
  11546. /**
  11547. * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
  11548. * @phba: pointer to lpfc hba data structure.
  11549. *
  11550. * returns the number of ELS/CT + NVMET IOCBs to reserve
  11551. **/
  11552. int
  11553. lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
  11554. {
  11555. int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
  11556. if (phba->nvmet_support)
  11557. max_xri += LPFC_NVMET_BUF_POST;
  11558. return max_xri;
  11559. }
  11560. static int
  11561. lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
  11562. uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
  11563. const struct firmware *fw)
  11564. {
  11565. int rc;
  11566. /* Three cases: (1) FW was not supported on the detected adapter.
  11567. * (2) FW update has been locked out administratively.
  11568. * (3) Some other error during FW update.
  11569. * In each case, an unmaskable message is written to the console
  11570. * for admin diagnosis.
  11571. */
  11572. if (offset == ADD_STATUS_FW_NOT_SUPPORTED ||
  11573. (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G6_FC &&
  11574. magic_number != MAGIC_NUMBER_G6) ||
  11575. (phba->pcidev->device == PCI_DEVICE_ID_LANCER_G7_FC &&
  11576. magic_number != MAGIC_NUMBER_G7)) {
  11577. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11578. "3030 This firmware version is not supported on"
  11579. " this HBA model. Device:%x Magic:%x Type:%x "
  11580. "ID:%x Size %d %zd\n",
  11581. phba->pcidev->device, magic_number, ftype, fid,
  11582. fsize, fw->size);
  11583. rc = -EINVAL;
  11584. } else if (offset == ADD_STATUS_FW_DOWNLOAD_HW_DISABLED) {
  11585. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11586. "3021 Firmware downloads have been prohibited "
  11587. "by a system configuration setting on "
  11588. "Device:%x Magic:%x Type:%x ID:%x Size %d "
  11589. "%zd\n",
  11590. phba->pcidev->device, magic_number, ftype, fid,
  11591. fsize, fw->size);
  11592. rc = -EACCES;
  11593. } else {
  11594. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11595. "3022 FW Download failed. Add Status x%x "
  11596. "Device:%x Magic:%x Type:%x ID:%x Size %d "
  11597. "%zd\n",
  11598. offset, phba->pcidev->device, magic_number,
  11599. ftype, fid, fsize, fw->size);
  11600. rc = -EIO;
  11601. }
  11602. return rc;
  11603. }
  11604. /**
  11605. * lpfc_write_firmware - attempt to write a firmware image to the port
  11606. * @fw: pointer to firmware image returned from request_firmware.
  11607. * @context: pointer to firmware image returned from request_firmware.
  11608. * @ret: return value this routine provides to the caller.
  11609. *
  11610. **/
  11611. static void
  11612. lpfc_write_firmware(const struct firmware *fw, void *context)
  11613. {
  11614. struct lpfc_hba *phba = (struct lpfc_hba *)context;
  11615. char fwrev[FW_REV_STR_SIZE];
  11616. struct lpfc_grp_hdr *image;
  11617. struct list_head dma_buffer_list;
  11618. int i, rc = 0;
  11619. struct lpfc_dmabuf *dmabuf, *next;
  11620. uint32_t offset = 0, temp_offset = 0;
  11621. uint32_t magic_number, ftype, fid, fsize;
  11622. /* It can be null in no-wait mode, sanity check */
  11623. if (!fw) {
  11624. rc = -ENXIO;
  11625. goto out;
  11626. }
  11627. image = (struct lpfc_grp_hdr *)fw->data;
  11628. magic_number = be32_to_cpu(image->magic_number);
  11629. ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
  11630. fid = bf_get_be32(lpfc_grp_hdr_id, image);
  11631. fsize = be32_to_cpu(image->size);
  11632. INIT_LIST_HEAD(&dma_buffer_list);
  11633. lpfc_decode_firmware_rev(phba, fwrev, 1);
  11634. if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
  11635. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11636. "3023 Updating Firmware, Current Version:%s "
  11637. "New Version:%s\n",
  11638. fwrev, image->revision);
  11639. for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
  11640. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
  11641. GFP_KERNEL);
  11642. if (!dmabuf) {
  11643. rc = -ENOMEM;
  11644. goto release_out;
  11645. }
  11646. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  11647. SLI4_PAGE_SIZE,
  11648. &dmabuf->phys,
  11649. GFP_KERNEL);
  11650. if (!dmabuf->virt) {
  11651. kfree(dmabuf);
  11652. rc = -ENOMEM;
  11653. goto release_out;
  11654. }
  11655. list_add_tail(&dmabuf->list, &dma_buffer_list);
  11656. }
  11657. while (offset < fw->size) {
  11658. temp_offset = offset;
  11659. list_for_each_entry(dmabuf, &dma_buffer_list, list) {
  11660. if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
  11661. memcpy(dmabuf->virt,
  11662. fw->data + temp_offset,
  11663. fw->size - temp_offset);
  11664. temp_offset = fw->size;
  11665. break;
  11666. }
  11667. memcpy(dmabuf->virt, fw->data + temp_offset,
  11668. SLI4_PAGE_SIZE);
  11669. temp_offset += SLI4_PAGE_SIZE;
  11670. }
  11671. rc = lpfc_wr_object(phba, &dma_buffer_list,
  11672. (fw->size - offset), &offset);
  11673. if (rc) {
  11674. rc = lpfc_log_write_firmware_error(phba, offset,
  11675. magic_number,
  11676. ftype,
  11677. fid,
  11678. fsize,
  11679. fw);
  11680. goto release_out;
  11681. }
  11682. }
  11683. rc = offset;
  11684. } else
  11685. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11686. "3029 Skipped Firmware update, Current "
  11687. "Version:%s New Version:%s\n",
  11688. fwrev, image->revision);
  11689. release_out:
  11690. list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
  11691. list_del(&dmabuf->list);
  11692. dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
  11693. dmabuf->virt, dmabuf->phys);
  11694. kfree(dmabuf);
  11695. }
  11696. release_firmware(fw);
  11697. out:
  11698. if (rc < 0)
  11699. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11700. "3062 Firmware update error, status %d.\n", rc);
  11701. else
  11702. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11703. "3024 Firmware update success: size %d.\n", rc);
  11704. }
  11705. /**
  11706. * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
  11707. * @phba: pointer to lpfc hba data structure.
  11708. *
  11709. * This routine is called to perform Linux generic firmware upgrade on device
  11710. * that supports such feature.
  11711. **/
  11712. int
  11713. lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
  11714. {
  11715. uint8_t file_name[ELX_MODEL_NAME_SIZE];
  11716. int ret;
  11717. const struct firmware *fw;
  11718. /* Only supported on SLI4 interface type 2 for now */
  11719. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
  11720. LPFC_SLI_INTF_IF_TYPE_2)
  11721. return -EPERM;
  11722. snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
  11723. if (fw_upgrade == INT_FW_UPGRADE) {
  11724. ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
  11725. file_name, &phba->pcidev->dev,
  11726. GFP_KERNEL, (void *)phba,
  11727. lpfc_write_firmware);
  11728. } else if (fw_upgrade == RUN_FW_UPGRADE) {
  11729. ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
  11730. if (!ret)
  11731. lpfc_write_firmware(fw, (void *)phba);
  11732. } else {
  11733. ret = -EINVAL;
  11734. }
  11735. return ret;
  11736. }
  11737. /**
  11738. * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
  11739. * @pdev: pointer to PCI device
  11740. * @pid: pointer to PCI device identifier
  11741. *
  11742. * This routine is called from the kernel's PCI subsystem to device with
  11743. * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
  11744. * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
  11745. * information of the device and driver to see if the driver state that it
  11746. * can support this kind of device. If the match is successful, the driver
  11747. * core invokes this routine. If this routine determines it can claim the HBA,
  11748. * it does all the initialization that it needs to do to handle the HBA
  11749. * properly.
  11750. *
  11751. * Return code
  11752. * 0 - driver can claim the device
  11753. * negative value - driver can not claim the device
  11754. **/
  11755. static int
  11756. lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
  11757. {
  11758. struct lpfc_hba *phba;
  11759. struct lpfc_vport *vport = NULL;
  11760. struct Scsi_Host *shost = NULL;
  11761. int error;
  11762. uint32_t cfg_mode, intr_mode;
  11763. /* Allocate memory for HBA structure */
  11764. phba = lpfc_hba_alloc(pdev);
  11765. if (!phba)
  11766. return -ENOMEM;
  11767. /* Perform generic PCI device enabling operation */
  11768. error = lpfc_enable_pci_dev(phba);
  11769. if (error)
  11770. goto out_free_phba;
  11771. /* Set up SLI API function jump table for PCI-device group-1 HBAs */
  11772. error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
  11773. if (error)
  11774. goto out_disable_pci_dev;
  11775. /* Set up SLI-4 specific device PCI memory space */
  11776. error = lpfc_sli4_pci_mem_setup(phba);
  11777. if (error) {
  11778. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11779. "1410 Failed to set up pci memory space.\n");
  11780. goto out_disable_pci_dev;
  11781. }
  11782. /* Set up SLI-4 Specific device driver resources */
  11783. error = lpfc_sli4_driver_resource_setup(phba);
  11784. if (error) {
  11785. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11786. "1412 Failed to set up driver resource.\n");
  11787. goto out_unset_pci_mem_s4;
  11788. }
  11789. INIT_LIST_HEAD(&phba->active_rrq_list);
  11790. INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
  11791. /* Set up common device driver resources */
  11792. error = lpfc_setup_driver_resource_phase2(phba);
  11793. if (error) {
  11794. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11795. "1414 Failed to set up driver resource.\n");
  11796. goto out_unset_driver_resource_s4;
  11797. }
  11798. /* Get the default values for Model Name and Description */
  11799. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  11800. /* Now, trying to enable interrupt and bring up the device */
  11801. cfg_mode = phba->cfg_use_msi;
  11802. /* Put device to a known state before enabling interrupt */
  11803. phba->pport = NULL;
  11804. lpfc_stop_port(phba);
  11805. /* Init cpu_map array */
  11806. lpfc_cpu_map_array_init(phba);
  11807. /* Init hba_eq_hdl array */
  11808. lpfc_hba_eq_hdl_array_init(phba);
  11809. /* Configure and enable interrupt */
  11810. intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
  11811. if (intr_mode == LPFC_INTR_ERROR) {
  11812. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11813. "0426 Failed to enable interrupt.\n");
  11814. error = -ENODEV;
  11815. goto out_unset_driver_resource;
  11816. }
  11817. /* Default to single EQ for non-MSI-X */
  11818. if (phba->intr_type != MSIX) {
  11819. phba->cfg_irq_chann = 1;
  11820. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  11821. if (phba->nvmet_support)
  11822. phba->cfg_nvmet_mrq = 1;
  11823. }
  11824. }
  11825. lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
  11826. /* Create SCSI host to the physical port */
  11827. error = lpfc_create_shost(phba);
  11828. if (error) {
  11829. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11830. "1415 Failed to create scsi host.\n");
  11831. goto out_disable_intr;
  11832. }
  11833. vport = phba->pport;
  11834. shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
  11835. /* Configure sysfs attributes */
  11836. error = lpfc_alloc_sysfs_attr(vport);
  11837. if (error) {
  11838. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11839. "1416 Failed to allocate sysfs attr\n");
  11840. goto out_destroy_shost;
  11841. }
  11842. /* Set up SLI-4 HBA */
  11843. if (lpfc_sli4_hba_setup(phba)) {
  11844. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11845. "1421 Failed to set up hba\n");
  11846. error = -ENODEV;
  11847. goto out_free_sysfs_attr;
  11848. }
  11849. /* Log the current active interrupt mode */
  11850. phba->intr_mode = intr_mode;
  11851. lpfc_log_intr_mode(phba, intr_mode);
  11852. /* Perform post initialization setup */
  11853. lpfc_post_init_setup(phba);
  11854. /* NVME support in FW earlier in the driver load corrects the
  11855. * FC4 type making a check for nvme_support unnecessary.
  11856. */
  11857. if (phba->nvmet_support == 0) {
  11858. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  11859. /* Create NVME binding with nvme_fc_transport. This
  11860. * ensures the vport is initialized. If the localport
  11861. * create fails, it should not unload the driver to
  11862. * support field issues.
  11863. */
  11864. error = lpfc_nvme_create_localport(vport);
  11865. if (error) {
  11866. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11867. "6004 NVME registration "
  11868. "failed, error x%x\n",
  11869. error);
  11870. }
  11871. }
  11872. }
  11873. /* check for firmware upgrade or downgrade */
  11874. if (phba->cfg_request_firmware_upgrade)
  11875. lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
  11876. /* Check if there are static vports to be created. */
  11877. lpfc_create_static_vport(phba);
  11878. /* Enable RAS FW log support */
  11879. lpfc_sli4_ras_setup(phba);
  11880. INIT_LIST_HEAD(&phba->poll_list);
  11881. timer_setup(&phba->cpuhp_poll_timer, lpfc_sli4_poll_hbtimer, 0);
  11882. cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state, &phba->cpuhp);
  11883. return 0;
  11884. out_free_sysfs_attr:
  11885. lpfc_free_sysfs_attr(vport);
  11886. out_destroy_shost:
  11887. lpfc_destroy_shost(phba);
  11888. out_disable_intr:
  11889. lpfc_sli4_disable_intr(phba);
  11890. out_unset_driver_resource:
  11891. lpfc_unset_driver_resource_phase2(phba);
  11892. out_unset_driver_resource_s4:
  11893. lpfc_sli4_driver_resource_unset(phba);
  11894. out_unset_pci_mem_s4:
  11895. lpfc_sli4_pci_mem_unset(phba);
  11896. out_disable_pci_dev:
  11897. lpfc_disable_pci_dev(phba);
  11898. if (shost)
  11899. scsi_host_put(shost);
  11900. out_free_phba:
  11901. lpfc_hba_free(phba);
  11902. return error;
  11903. }
  11904. /**
  11905. * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
  11906. * @pdev: pointer to PCI device
  11907. *
  11908. * This routine is called from the kernel's PCI subsystem to device with
  11909. * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
  11910. * removed from PCI bus, it performs all the necessary cleanup for the HBA
  11911. * device to be removed from the PCI subsystem properly.
  11912. **/
  11913. static void
  11914. lpfc_pci_remove_one_s4(struct pci_dev *pdev)
  11915. {
  11916. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  11917. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  11918. struct lpfc_vport **vports;
  11919. struct lpfc_hba *phba = vport->phba;
  11920. int i;
  11921. /* Mark the device unloading flag */
  11922. spin_lock_irq(&phba->hbalock);
  11923. vport->load_flag |= FC_UNLOADING;
  11924. spin_unlock_irq(&phba->hbalock);
  11925. /* Free the HBA sysfs attributes */
  11926. lpfc_free_sysfs_attr(vport);
  11927. /* Release all the vports against this physical port */
  11928. vports = lpfc_create_vport_work_array(phba);
  11929. if (vports != NULL)
  11930. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  11931. if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
  11932. continue;
  11933. fc_vport_terminate(vports[i]->fc_vport);
  11934. }
  11935. lpfc_destroy_vport_work_array(phba, vports);
  11936. /* Remove FC host and then SCSI host with the physical port */
  11937. fc_remove_host(shost);
  11938. scsi_remove_host(shost);
  11939. /* Perform ndlp cleanup on the physical port. The nvme and nvmet
  11940. * localports are destroyed after to cleanup all transport memory.
  11941. */
  11942. lpfc_cleanup(vport);
  11943. lpfc_nvmet_destroy_targetport(phba);
  11944. lpfc_nvme_destroy_localport(vport);
  11945. /* De-allocate multi-XRI pools */
  11946. if (phba->cfg_xri_rebalancing)
  11947. lpfc_destroy_multixri_pools(phba);
  11948. /*
  11949. * Bring down the SLI Layer. This step disables all interrupts,
  11950. * clears the rings, discards all mailbox commands, and resets
  11951. * the HBA FCoE function.
  11952. */
  11953. lpfc_debugfs_terminate(vport);
  11954. lpfc_stop_hba_timers(phba);
  11955. spin_lock_irq(&phba->port_list_lock);
  11956. list_del_init(&vport->listentry);
  11957. spin_unlock_irq(&phba->port_list_lock);
  11958. /* Perform scsi free before driver resource_unset since scsi
  11959. * buffers are released to their corresponding pools here.
  11960. */
  11961. lpfc_io_free(phba);
  11962. lpfc_free_iocb_list(phba);
  11963. lpfc_sli4_hba_unset(phba);
  11964. lpfc_unset_driver_resource_phase2(phba);
  11965. lpfc_sli4_driver_resource_unset(phba);
  11966. /* Unmap adapter Control and Doorbell registers */
  11967. lpfc_sli4_pci_mem_unset(phba);
  11968. /* Release PCI resources and disable device's PCI function */
  11969. scsi_host_put(shost);
  11970. lpfc_disable_pci_dev(phba);
  11971. /* Finally, free the driver's device data structure */
  11972. lpfc_hba_free(phba);
  11973. return;
  11974. }
  11975. /**
  11976. * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
  11977. * @pdev: pointer to PCI device
  11978. * @msg: power management message
  11979. *
  11980. * This routine is called from the kernel's PCI subsystem to support system
  11981. * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
  11982. * this method, it quiesces the device by stopping the driver's worker
  11983. * thread for the device, turning off device's interrupt and DMA, and bring
  11984. * the device offline. Note that as the driver implements the minimum PM
  11985. * requirements to a power-aware driver's PM support for suspend/resume -- all
  11986. * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
  11987. * method call will be treated as SUSPEND and the driver will fully
  11988. * reinitialize its device during resume() method call, the driver will set
  11989. * device to PCI_D3hot state in PCI config space instead of setting it
  11990. * according to the @msg provided by the PM.
  11991. *
  11992. * Return code
  11993. * 0 - driver suspended the device
  11994. * Error otherwise
  11995. **/
  11996. static int
  11997. lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
  11998. {
  11999. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12000. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12001. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12002. "2843 PCI device Power Management suspend.\n");
  12003. /* Bring down the device */
  12004. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  12005. lpfc_offline(phba);
  12006. kthread_stop(phba->worker_thread);
  12007. /* Disable interrupt from device */
  12008. lpfc_sli4_disable_intr(phba);
  12009. lpfc_sli4_queue_destroy(phba);
  12010. /* Save device state to PCI config space */
  12011. pci_save_state(pdev);
  12012. pci_set_power_state(pdev, PCI_D3hot);
  12013. return 0;
  12014. }
  12015. /**
  12016. * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
  12017. * @pdev: pointer to PCI device
  12018. *
  12019. * This routine is called from the kernel's PCI subsystem to support system
  12020. * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
  12021. * this method, it restores the device's PCI config space state and fully
  12022. * reinitializes the device and brings it online. Note that as the driver
  12023. * implements the minimum PM requirements to a power-aware driver's PM for
  12024. * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
  12025. * to the suspend() method call will be treated as SUSPEND and the driver
  12026. * will fully reinitialize its device during resume() method call, the device
  12027. * will be set to PCI_D0 directly in PCI config space before restoring the
  12028. * state.
  12029. *
  12030. * Return code
  12031. * 0 - driver suspended the device
  12032. * Error otherwise
  12033. **/
  12034. static int
  12035. lpfc_pci_resume_one_s4(struct pci_dev *pdev)
  12036. {
  12037. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12038. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12039. uint32_t intr_mode;
  12040. int error;
  12041. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12042. "0292 PCI device Power Management resume.\n");
  12043. /* Restore device state from PCI config space */
  12044. pci_set_power_state(pdev, PCI_D0);
  12045. pci_restore_state(pdev);
  12046. /*
  12047. * As the new kernel behavior of pci_restore_state() API call clears
  12048. * device saved_state flag, need to save the restored state again.
  12049. */
  12050. pci_save_state(pdev);
  12051. if (pdev->is_busmaster)
  12052. pci_set_master(pdev);
  12053. /* Startup the kernel thread for this host adapter. */
  12054. phba->worker_thread = kthread_run(lpfc_do_work, phba,
  12055. "lpfc_worker_%d", phba->brd_no);
  12056. if (IS_ERR(phba->worker_thread)) {
  12057. error = PTR_ERR(phba->worker_thread);
  12058. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12059. "0293 PM resume failed to start worker "
  12060. "thread: error=x%x.\n", error);
  12061. return error;
  12062. }
  12063. /* Configure and enable interrupt */
  12064. intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
  12065. if (intr_mode == LPFC_INTR_ERROR) {
  12066. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12067. "0294 PM resume Failed to enable interrupt\n");
  12068. return -EIO;
  12069. } else
  12070. phba->intr_mode = intr_mode;
  12071. /* Restart HBA and bring it online */
  12072. lpfc_sli_brdrestart(phba);
  12073. lpfc_online(phba);
  12074. /* Log the current active interrupt mode */
  12075. lpfc_log_intr_mode(phba, phba->intr_mode);
  12076. return 0;
  12077. }
  12078. /**
  12079. * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
  12080. * @phba: pointer to lpfc hba data structure.
  12081. *
  12082. * This routine is called to prepare the SLI4 device for PCI slot recover. It
  12083. * aborts all the outstanding SCSI I/Os to the pci device.
  12084. **/
  12085. static void
  12086. lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
  12087. {
  12088. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12089. "2828 PCI channel I/O abort preparing for recovery\n");
  12090. /*
  12091. * There may be errored I/Os through HBA, abort all I/Os on txcmplq
  12092. * and let the SCSI mid-layer to retry them to recover.
  12093. */
  12094. lpfc_sli_abort_fcp_rings(phba);
  12095. }
  12096. /**
  12097. * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
  12098. * @phba: pointer to lpfc hba data structure.
  12099. *
  12100. * This routine is called to prepare the SLI4 device for PCI slot reset. It
  12101. * disables the device interrupt and pci device, and aborts the internal FCP
  12102. * pending I/Os.
  12103. **/
  12104. static void
  12105. lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
  12106. {
  12107. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12108. "2826 PCI channel disable preparing for reset\n");
  12109. /* Block any management I/Os to the device */
  12110. lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
  12111. /* Block all SCSI devices' I/Os on the host */
  12112. lpfc_scsi_dev_block(phba);
  12113. /* Flush all driver's outstanding I/Os as we are to reset */
  12114. lpfc_sli_flush_io_rings(phba);
  12115. /* stop all timers */
  12116. lpfc_stop_hba_timers(phba);
  12117. /* Disable interrupt and pci device */
  12118. lpfc_sli4_disable_intr(phba);
  12119. lpfc_sli4_queue_destroy(phba);
  12120. pci_disable_device(phba->pcidev);
  12121. }
  12122. /**
  12123. * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
  12124. * @phba: pointer to lpfc hba data structure.
  12125. *
  12126. * This routine is called to prepare the SLI4 device for PCI slot permanently
  12127. * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
  12128. * pending I/Os.
  12129. **/
  12130. static void
  12131. lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
  12132. {
  12133. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12134. "2827 PCI channel permanent disable for failure\n");
  12135. /* Block all SCSI devices' I/Os on the host */
  12136. lpfc_scsi_dev_block(phba);
  12137. /* stop all timers */
  12138. lpfc_stop_hba_timers(phba);
  12139. /* Clean up all driver's outstanding I/Os */
  12140. lpfc_sli_flush_io_rings(phba);
  12141. }
  12142. /**
  12143. * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
  12144. * @pdev: pointer to PCI device.
  12145. * @state: the current PCI connection state.
  12146. *
  12147. * This routine is called from the PCI subsystem for error handling to device
  12148. * with SLI-4 interface spec. This function is called by the PCI subsystem
  12149. * after a PCI bus error affecting this device has been detected. When this
  12150. * function is invoked, it will need to stop all the I/Os and interrupt(s)
  12151. * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
  12152. * for the PCI subsystem to perform proper recovery as desired.
  12153. *
  12154. * Return codes
  12155. * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
  12156. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  12157. **/
  12158. static pci_ers_result_t
  12159. lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
  12160. {
  12161. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12162. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12163. switch (state) {
  12164. case pci_channel_io_normal:
  12165. /* Non-fatal error, prepare for recovery */
  12166. lpfc_sli4_prep_dev_for_recover(phba);
  12167. return PCI_ERS_RESULT_CAN_RECOVER;
  12168. case pci_channel_io_frozen:
  12169. /* Fatal error, prepare for slot reset */
  12170. lpfc_sli4_prep_dev_for_reset(phba);
  12171. return PCI_ERS_RESULT_NEED_RESET;
  12172. case pci_channel_io_perm_failure:
  12173. /* Permanent failure, prepare for device down */
  12174. lpfc_sli4_prep_dev_for_perm_failure(phba);
  12175. return PCI_ERS_RESULT_DISCONNECT;
  12176. default:
  12177. /* Unknown state, prepare and request slot reset */
  12178. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12179. "2825 Unknown PCI error state: x%x\n", state);
  12180. lpfc_sli4_prep_dev_for_reset(phba);
  12181. return PCI_ERS_RESULT_NEED_RESET;
  12182. }
  12183. }
  12184. /**
  12185. * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
  12186. * @pdev: pointer to PCI device.
  12187. *
  12188. * This routine is called from the PCI subsystem for error handling to device
  12189. * with SLI-4 interface spec. It is called after PCI bus has been reset to
  12190. * restart the PCI card from scratch, as if from a cold-boot. During the
  12191. * PCI subsystem error recovery, after the driver returns
  12192. * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
  12193. * recovery and then call this routine before calling the .resume method to
  12194. * recover the device. This function will initialize the HBA device, enable
  12195. * the interrupt, but it will just put the HBA to offline state without
  12196. * passing any I/O traffic.
  12197. *
  12198. * Return codes
  12199. * PCI_ERS_RESULT_RECOVERED - the device has been recovered
  12200. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  12201. */
  12202. static pci_ers_result_t
  12203. lpfc_io_slot_reset_s4(struct pci_dev *pdev)
  12204. {
  12205. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12206. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12207. struct lpfc_sli *psli = &phba->sli;
  12208. uint32_t intr_mode;
  12209. dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
  12210. if (pci_enable_device_mem(pdev)) {
  12211. printk(KERN_ERR "lpfc: Cannot re-enable "
  12212. "PCI device after reset.\n");
  12213. return PCI_ERS_RESULT_DISCONNECT;
  12214. }
  12215. pci_restore_state(pdev);
  12216. /*
  12217. * As the new kernel behavior of pci_restore_state() API call clears
  12218. * device saved_state flag, need to save the restored state again.
  12219. */
  12220. pci_save_state(pdev);
  12221. if (pdev->is_busmaster)
  12222. pci_set_master(pdev);
  12223. spin_lock_irq(&phba->hbalock);
  12224. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  12225. spin_unlock_irq(&phba->hbalock);
  12226. /* Configure and enable interrupt */
  12227. intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
  12228. if (intr_mode == LPFC_INTR_ERROR) {
  12229. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12230. "2824 Cannot re-enable interrupt after "
  12231. "slot reset.\n");
  12232. return PCI_ERS_RESULT_DISCONNECT;
  12233. } else
  12234. phba->intr_mode = intr_mode;
  12235. /* Log the current active interrupt mode */
  12236. lpfc_log_intr_mode(phba, phba->intr_mode);
  12237. return PCI_ERS_RESULT_RECOVERED;
  12238. }
  12239. /**
  12240. * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
  12241. * @pdev: pointer to PCI device
  12242. *
  12243. * This routine is called from the PCI subsystem for error handling to device
  12244. * with SLI-4 interface spec. It is called when kernel error recovery tells
  12245. * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
  12246. * error recovery. After this call, traffic can start to flow from this device
  12247. * again.
  12248. **/
  12249. static void
  12250. lpfc_io_resume_s4(struct pci_dev *pdev)
  12251. {
  12252. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12253. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12254. /*
  12255. * In case of slot reset, as function reset is performed through
  12256. * mailbox command which needs DMA to be enabled, this operation
  12257. * has to be moved to the io resume phase. Taking device offline
  12258. * will perform the necessary cleanup.
  12259. */
  12260. if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
  12261. /* Perform device reset */
  12262. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  12263. lpfc_offline(phba);
  12264. lpfc_sli_brdrestart(phba);
  12265. /* Bring the device back online */
  12266. lpfc_online(phba);
  12267. }
  12268. }
  12269. /**
  12270. * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
  12271. * @pdev: pointer to PCI device
  12272. * @pid: pointer to PCI device identifier
  12273. *
  12274. * This routine is to be registered to the kernel's PCI subsystem. When an
  12275. * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
  12276. * at PCI device-specific information of the device and driver to see if the
  12277. * driver state that it can support this kind of device. If the match is
  12278. * successful, the driver core invokes this routine. This routine dispatches
  12279. * the action to the proper SLI-3 or SLI-4 device probing routine, which will
  12280. * do all the initialization that it needs to do to handle the HBA device
  12281. * properly.
  12282. *
  12283. * Return code
  12284. * 0 - driver can claim the device
  12285. * negative value - driver can not claim the device
  12286. **/
  12287. static int
  12288. lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
  12289. {
  12290. int rc;
  12291. struct lpfc_sli_intf intf;
  12292. if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
  12293. return -ENODEV;
  12294. if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
  12295. (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
  12296. rc = lpfc_pci_probe_one_s4(pdev, pid);
  12297. else
  12298. rc = lpfc_pci_probe_one_s3(pdev, pid);
  12299. return rc;
  12300. }
  12301. /**
  12302. * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
  12303. * @pdev: pointer to PCI device
  12304. *
  12305. * This routine is to be registered to the kernel's PCI subsystem. When an
  12306. * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
  12307. * This routine dispatches the action to the proper SLI-3 or SLI-4 device
  12308. * remove routine, which will perform all the necessary cleanup for the
  12309. * device to be removed from the PCI subsystem properly.
  12310. **/
  12311. static void
  12312. lpfc_pci_remove_one(struct pci_dev *pdev)
  12313. {
  12314. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12315. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12316. switch (phba->pci_dev_grp) {
  12317. case LPFC_PCI_DEV_LP:
  12318. lpfc_pci_remove_one_s3(pdev);
  12319. break;
  12320. case LPFC_PCI_DEV_OC:
  12321. lpfc_pci_remove_one_s4(pdev);
  12322. break;
  12323. default:
  12324. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12325. "1424 Invalid PCI device group: 0x%x\n",
  12326. phba->pci_dev_grp);
  12327. break;
  12328. }
  12329. return;
  12330. }
  12331. /**
  12332. * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
  12333. * @pdev: pointer to PCI device
  12334. * @msg: power management message
  12335. *
  12336. * This routine is to be registered to the kernel's PCI subsystem to support
  12337. * system Power Management (PM). When PM invokes this method, it dispatches
  12338. * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
  12339. * suspend the device.
  12340. *
  12341. * Return code
  12342. * 0 - driver suspended the device
  12343. * Error otherwise
  12344. **/
  12345. static int
  12346. lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
  12347. {
  12348. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12349. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12350. int rc = -ENODEV;
  12351. switch (phba->pci_dev_grp) {
  12352. case LPFC_PCI_DEV_LP:
  12353. rc = lpfc_pci_suspend_one_s3(pdev, msg);
  12354. break;
  12355. case LPFC_PCI_DEV_OC:
  12356. rc = lpfc_pci_suspend_one_s4(pdev, msg);
  12357. break;
  12358. default:
  12359. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12360. "1425 Invalid PCI device group: 0x%x\n",
  12361. phba->pci_dev_grp);
  12362. break;
  12363. }
  12364. return rc;
  12365. }
  12366. /**
  12367. * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
  12368. * @pdev: pointer to PCI device
  12369. *
  12370. * This routine is to be registered to the kernel's PCI subsystem to support
  12371. * system Power Management (PM). When PM invokes this method, it dispatches
  12372. * the action to the proper SLI-3 or SLI-4 device resume routine, which will
  12373. * resume the device.
  12374. *
  12375. * Return code
  12376. * 0 - driver suspended the device
  12377. * Error otherwise
  12378. **/
  12379. static int
  12380. lpfc_pci_resume_one(struct pci_dev *pdev)
  12381. {
  12382. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12383. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12384. int rc = -ENODEV;
  12385. switch (phba->pci_dev_grp) {
  12386. case LPFC_PCI_DEV_LP:
  12387. rc = lpfc_pci_resume_one_s3(pdev);
  12388. break;
  12389. case LPFC_PCI_DEV_OC:
  12390. rc = lpfc_pci_resume_one_s4(pdev);
  12391. break;
  12392. default:
  12393. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12394. "1426 Invalid PCI device group: 0x%x\n",
  12395. phba->pci_dev_grp);
  12396. break;
  12397. }
  12398. return rc;
  12399. }
  12400. /**
  12401. * lpfc_io_error_detected - lpfc method for handling PCI I/O error
  12402. * @pdev: pointer to PCI device.
  12403. * @state: the current PCI connection state.
  12404. *
  12405. * This routine is registered to the PCI subsystem for error handling. This
  12406. * function is called by the PCI subsystem after a PCI bus error affecting
  12407. * this device has been detected. When this routine is invoked, it dispatches
  12408. * the action to the proper SLI-3 or SLI-4 device error detected handling
  12409. * routine, which will perform the proper error detected operation.
  12410. *
  12411. * Return codes
  12412. * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
  12413. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  12414. **/
  12415. static pci_ers_result_t
  12416. lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  12417. {
  12418. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12419. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12420. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  12421. switch (phba->pci_dev_grp) {
  12422. case LPFC_PCI_DEV_LP:
  12423. rc = lpfc_io_error_detected_s3(pdev, state);
  12424. break;
  12425. case LPFC_PCI_DEV_OC:
  12426. rc = lpfc_io_error_detected_s4(pdev, state);
  12427. break;
  12428. default:
  12429. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12430. "1427 Invalid PCI device group: 0x%x\n",
  12431. phba->pci_dev_grp);
  12432. break;
  12433. }
  12434. return rc;
  12435. }
  12436. /**
  12437. * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
  12438. * @pdev: pointer to PCI device.
  12439. *
  12440. * This routine is registered to the PCI subsystem for error handling. This
  12441. * function is called after PCI bus has been reset to restart the PCI card
  12442. * from scratch, as if from a cold-boot. When this routine is invoked, it
  12443. * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
  12444. * routine, which will perform the proper device reset.
  12445. *
  12446. * Return codes
  12447. * PCI_ERS_RESULT_RECOVERED - the device has been recovered
  12448. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  12449. **/
  12450. static pci_ers_result_t
  12451. lpfc_io_slot_reset(struct pci_dev *pdev)
  12452. {
  12453. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12454. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12455. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  12456. switch (phba->pci_dev_grp) {
  12457. case LPFC_PCI_DEV_LP:
  12458. rc = lpfc_io_slot_reset_s3(pdev);
  12459. break;
  12460. case LPFC_PCI_DEV_OC:
  12461. rc = lpfc_io_slot_reset_s4(pdev);
  12462. break;
  12463. default:
  12464. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12465. "1428 Invalid PCI device group: 0x%x\n",
  12466. phba->pci_dev_grp);
  12467. break;
  12468. }
  12469. return rc;
  12470. }
  12471. /**
  12472. * lpfc_io_resume - lpfc method for resuming PCI I/O operation
  12473. * @pdev: pointer to PCI device
  12474. *
  12475. * This routine is registered to the PCI subsystem for error handling. It
  12476. * is called when kernel error recovery tells the lpfc driver that it is
  12477. * OK to resume normal PCI operation after PCI bus error recovery. When
  12478. * this routine is invoked, it dispatches the action to the proper SLI-3
  12479. * or SLI-4 device io_resume routine, which will resume the device operation.
  12480. **/
  12481. static void
  12482. lpfc_io_resume(struct pci_dev *pdev)
  12483. {
  12484. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12485. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12486. switch (phba->pci_dev_grp) {
  12487. case LPFC_PCI_DEV_LP:
  12488. lpfc_io_resume_s3(pdev);
  12489. break;
  12490. case LPFC_PCI_DEV_OC:
  12491. lpfc_io_resume_s4(pdev);
  12492. break;
  12493. default:
  12494. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12495. "1429 Invalid PCI device group: 0x%x\n",
  12496. phba->pci_dev_grp);
  12497. break;
  12498. }
  12499. return;
  12500. }
  12501. /**
  12502. * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
  12503. * @phba: pointer to lpfc hba data structure.
  12504. *
  12505. * This routine checks to see if OAS is supported for this adapter. If
  12506. * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
  12507. * the enable oas flag is cleared and the pool created for OAS device data
  12508. * is destroyed.
  12509. *
  12510. **/
  12511. static void
  12512. lpfc_sli4_oas_verify(struct lpfc_hba *phba)
  12513. {
  12514. if (!phba->cfg_EnableXLane)
  12515. return;
  12516. if (phba->sli4_hba.pc_sli4_params.oas_supported) {
  12517. phba->cfg_fof = 1;
  12518. } else {
  12519. phba->cfg_fof = 0;
  12520. mempool_destroy(phba->device_data_mem_pool);
  12521. phba->device_data_mem_pool = NULL;
  12522. }
  12523. return;
  12524. }
  12525. /**
  12526. * lpfc_sli4_ras_init - Verify RAS-FW log is supported by this adapter
  12527. * @phba: pointer to lpfc hba data structure.
  12528. *
  12529. * This routine checks to see if RAS is supported by the adapter. Check the
  12530. * function through which RAS support enablement is to be done.
  12531. **/
  12532. void
  12533. lpfc_sli4_ras_init(struct lpfc_hba *phba)
  12534. {
  12535. switch (phba->pcidev->device) {
  12536. case PCI_DEVICE_ID_LANCER_G6_FC:
  12537. case PCI_DEVICE_ID_LANCER_G7_FC:
  12538. phba->ras_fwlog.ras_hwsupport = true;
  12539. if (phba->cfg_ras_fwlog_func == PCI_FUNC(phba->pcidev->devfn) &&
  12540. phba->cfg_ras_fwlog_buffsize)
  12541. phba->ras_fwlog.ras_enabled = true;
  12542. else
  12543. phba->ras_fwlog.ras_enabled = false;
  12544. break;
  12545. default:
  12546. phba->ras_fwlog.ras_hwsupport = false;
  12547. }
  12548. }
  12549. MODULE_DEVICE_TABLE(pci, lpfc_id_table);
  12550. static const struct pci_error_handlers lpfc_err_handler = {
  12551. .error_detected = lpfc_io_error_detected,
  12552. .slot_reset = lpfc_io_slot_reset,
  12553. .resume = lpfc_io_resume,
  12554. };
  12555. static struct pci_driver lpfc_driver = {
  12556. .name = LPFC_DRIVER_NAME,
  12557. .id_table = lpfc_id_table,
  12558. .probe = lpfc_pci_probe_one,
  12559. .remove = lpfc_pci_remove_one,
  12560. .shutdown = lpfc_pci_remove_one,
  12561. .suspend = lpfc_pci_suspend_one,
  12562. .resume = lpfc_pci_resume_one,
  12563. .err_handler = &lpfc_err_handler,
  12564. };
  12565. static const struct file_operations lpfc_mgmt_fop = {
  12566. .owner = THIS_MODULE,
  12567. };
  12568. static struct miscdevice lpfc_mgmt_dev = {
  12569. .minor = MISC_DYNAMIC_MINOR,
  12570. .name = "lpfcmgmt",
  12571. .fops = &lpfc_mgmt_fop,
  12572. };
  12573. /**
  12574. * lpfc_init - lpfc module initialization routine
  12575. *
  12576. * This routine is to be invoked when the lpfc module is loaded into the
  12577. * kernel. The special kernel macro module_init() is used to indicate the
  12578. * role of this routine to the kernel as lpfc module entry point.
  12579. *
  12580. * Return codes
  12581. * 0 - successful
  12582. * -ENOMEM - FC attach transport failed
  12583. * all others - failed
  12584. */
  12585. static int __init
  12586. lpfc_init(void)
  12587. {
  12588. int error = 0;
  12589. printk(LPFC_MODULE_DESC "\n");
  12590. printk(LPFC_COPYRIGHT "\n");
  12591. error = misc_register(&lpfc_mgmt_dev);
  12592. if (error)
  12593. printk(KERN_ERR "Could not register lpfcmgmt device, "
  12594. "misc_register returned with status %d", error);
  12595. lpfc_transport_functions.vport_create = lpfc_vport_create;
  12596. lpfc_transport_functions.vport_delete = lpfc_vport_delete;
  12597. lpfc_transport_template =
  12598. fc_attach_transport(&lpfc_transport_functions);
  12599. if (lpfc_transport_template == NULL)
  12600. return -ENOMEM;
  12601. lpfc_vport_transport_template =
  12602. fc_attach_transport(&lpfc_vport_transport_functions);
  12603. if (lpfc_vport_transport_template == NULL) {
  12604. fc_release_transport(lpfc_transport_template);
  12605. return -ENOMEM;
  12606. }
  12607. lpfc_nvme_cmd_template();
  12608. lpfc_nvmet_cmd_template();
  12609. /* Initialize in case vector mapping is needed */
  12610. lpfc_present_cpu = num_present_cpus();
  12611. error = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
  12612. "lpfc/sli4:online",
  12613. lpfc_cpu_online, lpfc_cpu_offline);
  12614. if (error < 0)
  12615. goto cpuhp_failure;
  12616. lpfc_cpuhp_state = error;
  12617. error = pci_register_driver(&lpfc_driver);
  12618. if (error)
  12619. goto unwind;
  12620. return error;
  12621. unwind:
  12622. cpuhp_remove_multi_state(lpfc_cpuhp_state);
  12623. cpuhp_failure:
  12624. fc_release_transport(lpfc_transport_template);
  12625. fc_release_transport(lpfc_vport_transport_template);
  12626. return error;
  12627. }
  12628. /**
  12629. * lpfc_exit - lpfc module removal routine
  12630. *
  12631. * This routine is invoked when the lpfc module is removed from the kernel.
  12632. * The special kernel macro module_exit() is used to indicate the role of
  12633. * this routine to the kernel as lpfc module exit point.
  12634. */
  12635. static void __exit
  12636. lpfc_exit(void)
  12637. {
  12638. misc_deregister(&lpfc_mgmt_dev);
  12639. pci_unregister_driver(&lpfc_driver);
  12640. cpuhp_remove_multi_state(lpfc_cpuhp_state);
  12641. fc_release_transport(lpfc_transport_template);
  12642. fc_release_transport(lpfc_vport_transport_template);
  12643. idr_destroy(&lpfc_hba_index);
  12644. }
  12645. module_init(lpfc_init);
  12646. module_exit(lpfc_exit);
  12647. MODULE_LICENSE("GPL");
  12648. MODULE_DESCRIPTION(LPFC_MODULE_DESC);
  12649. MODULE_AUTHOR("Broadcom");
  12650. MODULE_VERSION("0:" LPFC_DRIVER_VERSION);