lpfc_vport.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2017-2018 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/kthread.h>
  29. #include <linux/pci.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/sched/signal.h>
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_device.h>
  35. #include <scsi/scsi_host.h>
  36. #include <scsi/scsi_transport_fc.h>
  37. #include "lpfc_hw4.h"
  38. #include "lpfc_hw.h"
  39. #include "lpfc_sli.h"
  40. #include "lpfc_sli4.h"
  41. #include "lpfc_nl.h"
  42. #include "lpfc_disc.h"
  43. #include "lpfc_scsi.h"
  44. #include "lpfc.h"
  45. #include "lpfc_logmsg.h"
  46. #include "lpfc_crtn.h"
  47. #include "lpfc_version.h"
  48. #include "lpfc_vport.h"
  49. inline void lpfc_vport_set_state(struct lpfc_vport *vport,
  50. enum fc_vport_state new_state)
  51. {
  52. struct fc_vport *fc_vport = vport->fc_vport;
  53. if (fc_vport) {
  54. /*
  55. * When the transport defines fc_vport_set state we will replace
  56. * this code with the following line
  57. */
  58. /* fc_vport_set_state(fc_vport, new_state); */
  59. if (new_state != FC_VPORT_INITIALIZING)
  60. fc_vport->vport_last_state = fc_vport->vport_state;
  61. fc_vport->vport_state = new_state;
  62. }
  63. /* for all the error states we will set the invternal state to FAILED */
  64. switch (new_state) {
  65. case FC_VPORT_NO_FABRIC_SUPP:
  66. case FC_VPORT_NO_FABRIC_RSCS:
  67. case FC_VPORT_FABRIC_LOGOUT:
  68. case FC_VPORT_FABRIC_REJ_WWN:
  69. case FC_VPORT_FAILED:
  70. vport->port_state = LPFC_VPORT_FAILED;
  71. break;
  72. case FC_VPORT_LINKDOWN:
  73. vport->port_state = LPFC_VPORT_UNKNOWN;
  74. break;
  75. default:
  76. /* do nothing */
  77. break;
  78. }
  79. }
  80. int
  81. lpfc_alloc_vpi(struct lpfc_hba *phba)
  82. {
  83. unsigned long vpi;
  84. spin_lock_irq(&phba->hbalock);
  85. /* Start at bit 1 because vpi zero is reserved for the physical port */
  86. vpi = find_next_zero_bit(phba->vpi_bmask, (phba->max_vpi + 1), 1);
  87. if (vpi > phba->max_vpi)
  88. vpi = 0;
  89. else
  90. set_bit(vpi, phba->vpi_bmask);
  91. if (phba->sli_rev == LPFC_SLI_REV4)
  92. phba->sli4_hba.max_cfg_param.vpi_used++;
  93. spin_unlock_irq(&phba->hbalock);
  94. return vpi;
  95. }
  96. static void
  97. lpfc_free_vpi(struct lpfc_hba *phba, int vpi)
  98. {
  99. if (vpi == 0)
  100. return;
  101. spin_lock_irq(&phba->hbalock);
  102. clear_bit(vpi, phba->vpi_bmask);
  103. if (phba->sli_rev == LPFC_SLI_REV4)
  104. phba->sli4_hba.max_cfg_param.vpi_used--;
  105. spin_unlock_irq(&phba->hbalock);
  106. }
  107. static int
  108. lpfc_vport_sparm(struct lpfc_hba *phba, struct lpfc_vport *vport)
  109. {
  110. LPFC_MBOXQ_t *pmb;
  111. MAILBOX_t *mb;
  112. struct lpfc_dmabuf *mp;
  113. int rc;
  114. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  115. if (!pmb) {
  116. return -ENOMEM;
  117. }
  118. mb = &pmb->u.mb;
  119. rc = lpfc_read_sparam(phba, pmb, vport->vpi);
  120. if (rc) {
  121. mempool_free(pmb, phba->mbox_mem_pool);
  122. return -ENOMEM;
  123. }
  124. /*
  125. * Grab buffer pointer and clear context1 so we can use
  126. * lpfc_sli_issue_box_wait
  127. */
  128. mp = (struct lpfc_dmabuf *) pmb->context1;
  129. pmb->context1 = NULL;
  130. pmb->vport = vport;
  131. rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
  132. if (rc != MBX_SUCCESS) {
  133. if (signal_pending(current)) {
  134. lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
  135. "1830 Signal aborted mbxCmd x%x\n",
  136. mb->mbxCommand);
  137. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  138. kfree(mp);
  139. if (rc != MBX_TIMEOUT)
  140. mempool_free(pmb, phba->mbox_mem_pool);
  141. return -EINTR;
  142. } else {
  143. lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT | LOG_VPORT,
  144. "1818 VPort failed init, mbxCmd x%x "
  145. "READ_SPARM mbxStatus x%x, rc = x%x\n",
  146. mb->mbxCommand, mb->mbxStatus, rc);
  147. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  148. kfree(mp);
  149. if (rc != MBX_TIMEOUT)
  150. mempool_free(pmb, phba->mbox_mem_pool);
  151. return -EIO;
  152. }
  153. }
  154. memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
  155. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  156. sizeof (struct lpfc_name));
  157. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  158. sizeof (struct lpfc_name));
  159. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  160. kfree(mp);
  161. mempool_free(pmb, phba->mbox_mem_pool);
  162. return 0;
  163. }
  164. static int
  165. lpfc_valid_wwn_format(struct lpfc_hba *phba, struct lpfc_name *wwn,
  166. const char *name_type)
  167. {
  168. /* ensure that IEEE format 1 addresses
  169. * contain zeros in bits 59-48
  170. */
  171. if (!((wwn->u.wwn[0] >> 4) == 1 &&
  172. ((wwn->u.wwn[0] & 0xf) != 0 || (wwn->u.wwn[1] & 0xf) != 0)))
  173. return 1;
  174. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  175. "1822 Invalid %s: %02x:%02x:%02x:%02x:"
  176. "%02x:%02x:%02x:%02x\n",
  177. name_type,
  178. wwn->u.wwn[0], wwn->u.wwn[1],
  179. wwn->u.wwn[2], wwn->u.wwn[3],
  180. wwn->u.wwn[4], wwn->u.wwn[5],
  181. wwn->u.wwn[6], wwn->u.wwn[7]);
  182. return 0;
  183. }
  184. static int
  185. lpfc_unique_wwpn(struct lpfc_hba *phba, struct lpfc_vport *new_vport)
  186. {
  187. struct lpfc_vport *vport;
  188. unsigned long flags;
  189. spin_lock_irqsave(&phba->hbalock, flags);
  190. list_for_each_entry(vport, &phba->port_list, listentry) {
  191. if (vport == new_vport)
  192. continue;
  193. /* If they match, return not unique */
  194. if (memcmp(&vport->fc_sparam.portName,
  195. &new_vport->fc_sparam.portName,
  196. sizeof(struct lpfc_name)) == 0) {
  197. spin_unlock_irqrestore(&phba->hbalock, flags);
  198. return 0;
  199. }
  200. }
  201. spin_unlock_irqrestore(&phba->hbalock, flags);
  202. return 1;
  203. }
  204. /**
  205. * lpfc_discovery_wait - Wait for driver discovery to quiesce
  206. * @vport: The virtual port for which this call is being executed.
  207. *
  208. * This driver calls this routine specifically from lpfc_vport_delete
  209. * to enforce a synchronous execution of vport
  210. * delete relative to discovery activities. The
  211. * lpfc_vport_delete routine should not return until it
  212. * can reasonably guarantee that discovery has quiesced.
  213. * Post FDISC LOGO, the driver must wait until its SAN teardown is
  214. * complete and all resources recovered before allowing
  215. * cleanup.
  216. *
  217. * This routine does not require any locks held.
  218. **/
  219. static void lpfc_discovery_wait(struct lpfc_vport *vport)
  220. {
  221. struct lpfc_hba *phba = vport->phba;
  222. uint32_t wait_flags = 0;
  223. unsigned long wait_time_max;
  224. unsigned long start_time;
  225. wait_flags = FC_RSCN_MODE | FC_RSCN_DISCOVERY | FC_NLP_MORE |
  226. FC_RSCN_DEFERRED | FC_NDISC_ACTIVE | FC_DISC_TMO;
  227. /*
  228. * The time constraint on this loop is a balance between the
  229. * fabric RA_TOV value and dev_loss tmo. The driver's
  230. * devloss_tmo is 10 giving this loop a 3x multiplier minimally.
  231. */
  232. wait_time_max = msecs_to_jiffies(((phba->fc_ratov * 3) + 3) * 1000);
  233. wait_time_max += jiffies;
  234. start_time = jiffies;
  235. while (time_before(jiffies, wait_time_max)) {
  236. if ((vport->num_disc_nodes > 0) ||
  237. (vport->fc_flag & wait_flags) ||
  238. ((vport->port_state > LPFC_VPORT_FAILED) &&
  239. (vport->port_state < LPFC_VPORT_READY))) {
  240. lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
  241. "1833 Vport discovery quiesce Wait:"
  242. " state x%x fc_flags x%x"
  243. " num_nodes x%x, waiting 1000 msecs"
  244. " total wait msecs x%x\n",
  245. vport->port_state, vport->fc_flag,
  246. vport->num_disc_nodes,
  247. jiffies_to_msecs(jiffies - start_time));
  248. msleep(1000);
  249. } else {
  250. /* Base case. Wait variants satisfied. Break out */
  251. lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
  252. "1834 Vport discovery quiesced:"
  253. " state x%x fc_flags x%x"
  254. " wait msecs x%x\n",
  255. vport->port_state, vport->fc_flag,
  256. jiffies_to_msecs(jiffies
  257. - start_time));
  258. break;
  259. }
  260. }
  261. if (time_after(jiffies, wait_time_max))
  262. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  263. "1835 Vport discovery quiesce failed:"
  264. " state x%x fc_flags x%x wait msecs x%x\n",
  265. vport->port_state, vport->fc_flag,
  266. jiffies_to_msecs(jiffies - start_time));
  267. }
  268. int
  269. lpfc_vport_create(struct fc_vport *fc_vport, bool disable)
  270. {
  271. struct lpfc_nodelist *ndlp;
  272. struct Scsi_Host *shost = fc_vport->shost;
  273. struct lpfc_vport *pport = (struct lpfc_vport *) shost->hostdata;
  274. struct lpfc_hba *phba = pport->phba;
  275. struct lpfc_vport *vport = NULL;
  276. int instance;
  277. int vpi;
  278. int rc = VPORT_ERROR;
  279. int status;
  280. if ((phba->sli_rev < 3) || !(phba->cfg_enable_npiv)) {
  281. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  282. "1808 Create VPORT failed: "
  283. "NPIV is not enabled: SLImode:%d\n",
  284. phba->sli_rev);
  285. rc = VPORT_INVAL;
  286. goto error_out;
  287. }
  288. /* NPIV is not supported if HBA has NVME enabled */
  289. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  290. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  291. "3189 Create VPORT failed: "
  292. "NPIV is not supported on NVME\n");
  293. rc = VPORT_INVAL;
  294. goto error_out;
  295. }
  296. vpi = lpfc_alloc_vpi(phba);
  297. if (vpi == 0) {
  298. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  299. "1809 Create VPORT failed: "
  300. "Max VPORTs (%d) exceeded\n",
  301. phba->max_vpi);
  302. rc = VPORT_NORESOURCES;
  303. goto error_out;
  304. }
  305. /* Assign an unused board number */
  306. if ((instance = lpfc_get_instance()) < 0) {
  307. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  308. "1810 Create VPORT failed: Cannot get "
  309. "instance number\n");
  310. lpfc_free_vpi(phba, vpi);
  311. rc = VPORT_NORESOURCES;
  312. goto error_out;
  313. }
  314. vport = lpfc_create_port(phba, instance, &fc_vport->dev);
  315. if (!vport) {
  316. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  317. "1811 Create VPORT failed: vpi x%x\n", vpi);
  318. lpfc_free_vpi(phba, vpi);
  319. rc = VPORT_NORESOURCES;
  320. goto error_out;
  321. }
  322. vport->vpi = vpi;
  323. lpfc_debugfs_initialize(vport);
  324. if ((status = lpfc_vport_sparm(phba, vport))) {
  325. if (status == -EINTR) {
  326. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  327. "1831 Create VPORT Interrupted.\n");
  328. rc = VPORT_ERROR;
  329. } else {
  330. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  331. "1813 Create VPORT failed. "
  332. "Cannot get sparam\n");
  333. rc = VPORT_NORESOURCES;
  334. }
  335. lpfc_free_vpi(phba, vpi);
  336. destroy_port(vport);
  337. goto error_out;
  338. }
  339. u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
  340. u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
  341. memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
  342. memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
  343. if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
  344. !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
  345. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  346. "1821 Create VPORT failed. "
  347. "Invalid WWN format\n");
  348. lpfc_free_vpi(phba, vpi);
  349. destroy_port(vport);
  350. rc = VPORT_INVAL;
  351. goto error_out;
  352. }
  353. if (!lpfc_unique_wwpn(phba, vport)) {
  354. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  355. "1823 Create VPORT failed. "
  356. "Duplicate WWN on HBA\n");
  357. lpfc_free_vpi(phba, vpi);
  358. destroy_port(vport);
  359. rc = VPORT_INVAL;
  360. goto error_out;
  361. }
  362. /* Create binary sysfs attribute for vport */
  363. lpfc_alloc_sysfs_attr(vport);
  364. /* Set the DFT_LUN_Q_DEPTH accordingly */
  365. vport->cfg_lun_queue_depth = phba->pport->cfg_lun_queue_depth;
  366. *(struct lpfc_vport **)fc_vport->dd_data = vport;
  367. vport->fc_vport = fc_vport;
  368. /* At this point we are fully registered with SCSI Layer. */
  369. vport->load_flag |= FC_ALLOW_FDMI;
  370. if (phba->cfg_enable_SmartSAN ||
  371. (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
  372. /* Setup appropriate attribute masks */
  373. vport->fdmi_hba_mask = phba->pport->fdmi_hba_mask;
  374. vport->fdmi_port_mask = phba->pport->fdmi_port_mask;
  375. }
  376. if ((phba->nvmet_support == 0) &&
  377. ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
  378. (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))) {
  379. /* Create NVME binding with nvme_fc_transport. This
  380. * ensures the vport is initialized.
  381. */
  382. rc = lpfc_nvme_create_localport(vport);
  383. if (rc) {
  384. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  385. "6003 %s status x%x\n",
  386. "NVME registration failed, ",
  387. rc);
  388. goto error_out;
  389. }
  390. }
  391. /*
  392. * In SLI4, the vpi must be activated before it can be used
  393. * by the port.
  394. */
  395. if ((phba->sli_rev == LPFC_SLI_REV4) &&
  396. (pport->fc_flag & FC_VFI_REGISTERED)) {
  397. rc = lpfc_sli4_init_vpi(vport);
  398. if (rc) {
  399. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  400. "1838 Failed to INIT_VPI on vpi %d "
  401. "status %d\n", vpi, rc);
  402. rc = VPORT_NORESOURCES;
  403. lpfc_free_vpi(phba, vpi);
  404. goto error_out;
  405. }
  406. } else if (phba->sli_rev == LPFC_SLI_REV4) {
  407. /*
  408. * Driver cannot INIT_VPI now. Set the flags to
  409. * init_vpi when reg_vfi complete.
  410. */
  411. vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
  412. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  413. rc = VPORT_OK;
  414. goto out;
  415. }
  416. if ((phba->link_state < LPFC_LINK_UP) ||
  417. (pport->port_state < LPFC_FABRIC_CFG_LINK) ||
  418. (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
  419. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  420. rc = VPORT_OK;
  421. goto out;
  422. }
  423. if (disable) {
  424. lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
  425. rc = VPORT_OK;
  426. goto out;
  427. }
  428. /* Use the Physical nodes Fabric NDLP to determine if the link is
  429. * up and ready to FDISC.
  430. */
  431. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  432. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  433. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
  434. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
  435. lpfc_set_disctmo(vport);
  436. lpfc_initial_fdisc(vport);
  437. } else {
  438. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  439. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  440. "0262 No NPIV Fabric support\n");
  441. }
  442. } else {
  443. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  444. }
  445. rc = VPORT_OK;
  446. out:
  447. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  448. "1825 Vport Created.\n");
  449. lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
  450. error_out:
  451. return rc;
  452. }
  453. static int
  454. disable_vport(struct fc_vport *fc_vport)
  455. {
  456. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  457. struct lpfc_hba *phba = vport->phba;
  458. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  459. long timeout;
  460. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  461. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  462. if (ndlp && NLP_CHK_NODE_ACT(ndlp)
  463. && phba->link_state >= LPFC_LINK_UP) {
  464. vport->unreg_vpi_cmpl = VPORT_INVAL;
  465. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  466. if (!lpfc_issue_els_npiv_logo(vport, ndlp))
  467. while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
  468. timeout = schedule_timeout(timeout);
  469. }
  470. lpfc_sli_host_down(vport);
  471. /* Mark all nodes for discovery so we can remove them by
  472. * calling lpfc_cleanup_rpis(vport, 1)
  473. */
  474. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  475. if (!NLP_CHK_NODE_ACT(ndlp))
  476. continue;
  477. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  478. continue;
  479. lpfc_disc_state_machine(vport, ndlp, NULL,
  480. NLP_EVT_DEVICE_RECOVERY);
  481. }
  482. lpfc_cleanup_rpis(vport, 1);
  483. lpfc_stop_vport_timers(vport);
  484. lpfc_unreg_all_rpis(vport);
  485. lpfc_unreg_default_rpis(vport);
  486. /*
  487. * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
  488. * scsi_host_put() to release the vport.
  489. */
  490. lpfc_mbx_unreg_vpi(vport);
  491. spin_lock_irq(shost->host_lock);
  492. vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
  493. spin_unlock_irq(shost->host_lock);
  494. lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
  495. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  496. "1826 Vport Disabled.\n");
  497. return VPORT_OK;
  498. }
  499. static int
  500. enable_vport(struct fc_vport *fc_vport)
  501. {
  502. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  503. struct lpfc_hba *phba = vport->phba;
  504. struct lpfc_nodelist *ndlp = NULL;
  505. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  506. if ((phba->link_state < LPFC_LINK_UP) ||
  507. (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
  508. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  509. return VPORT_OK;
  510. }
  511. spin_lock_irq(shost->host_lock);
  512. vport->load_flag |= FC_LOADING;
  513. if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
  514. spin_unlock_irq(shost->host_lock);
  515. lpfc_issue_init_vpi(vport);
  516. goto out;
  517. }
  518. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  519. spin_unlock_irq(shost->host_lock);
  520. /* Use the Physical nodes Fabric NDLP to determine if the link is
  521. * up and ready to FDISC.
  522. */
  523. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  524. if (ndlp && NLP_CHK_NODE_ACT(ndlp)
  525. && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
  526. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
  527. lpfc_set_disctmo(vport);
  528. lpfc_initial_fdisc(vport);
  529. } else {
  530. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  531. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  532. "0264 No NPIV Fabric support\n");
  533. }
  534. } else {
  535. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  536. }
  537. out:
  538. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  539. "1827 Vport Enabled.\n");
  540. return VPORT_OK;
  541. }
  542. int
  543. lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
  544. {
  545. if (disable)
  546. return disable_vport(fc_vport);
  547. else
  548. return enable_vport(fc_vport);
  549. }
  550. int
  551. lpfc_vport_delete(struct fc_vport *fc_vport)
  552. {
  553. struct lpfc_nodelist *ndlp = NULL;
  554. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  555. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  556. struct lpfc_hba *phba = vport->phba;
  557. long timeout;
  558. bool ns_ndlp_referenced = false;
  559. if (vport->port_type == LPFC_PHYSICAL_PORT) {
  560. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  561. "1812 vport_delete failed: Cannot delete "
  562. "physical host\n");
  563. return VPORT_ERROR;
  564. }
  565. /* If the vport is a static vport fail the deletion. */
  566. if ((vport->vport_flag & STATIC_VPORT) &&
  567. !(phba->pport->load_flag & FC_UNLOADING)) {
  568. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  569. "1837 vport_delete failed: Cannot delete "
  570. "static vport.\n");
  571. return VPORT_ERROR;
  572. }
  573. spin_lock_irq(&phba->hbalock);
  574. vport->load_flag |= FC_UNLOADING;
  575. spin_unlock_irq(&phba->hbalock);
  576. /*
  577. * If we are not unloading the driver then prevent the vport_delete
  578. * from happening until after this vport's discovery is finished.
  579. */
  580. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  581. int check_count = 0;
  582. while (check_count < ((phba->fc_ratov * 3) + 3) &&
  583. vport->port_state > LPFC_VPORT_FAILED &&
  584. vport->port_state < LPFC_VPORT_READY) {
  585. check_count++;
  586. msleep(1000);
  587. }
  588. if (vport->port_state > LPFC_VPORT_FAILED &&
  589. vport->port_state < LPFC_VPORT_READY)
  590. return -EAGAIN;
  591. }
  592. /*
  593. * This is a bit of a mess. We want to ensure the shost doesn't get
  594. * torn down until we're done with the embedded lpfc_vport structure.
  595. *
  596. * Beyond holding a reference for this function, we also need a
  597. * reference for outstanding I/O requests we schedule during delete
  598. * processing. But once we scsi_remove_host() we can no longer obtain
  599. * a reference through scsi_host_get().
  600. *
  601. * So we take two references here. We release one reference at the
  602. * bottom of the function -- after delinking the vport. And we
  603. * release the other at the completion of the unreg_vpi that get's
  604. * initiated after we've disposed of all other resources associated
  605. * with the port.
  606. */
  607. if (!scsi_host_get(shost))
  608. return VPORT_INVAL;
  609. if (!scsi_host_get(shost)) {
  610. scsi_host_put(shost);
  611. return VPORT_INVAL;
  612. }
  613. lpfc_free_sysfs_attr(vport);
  614. lpfc_debugfs_terminate(vport);
  615. /*
  616. * The call to fc_remove_host might release the NameServer ndlp. Since
  617. * we might need to use the ndlp to send the DA_ID CT command,
  618. * increment the reference for the NameServer ndlp to prevent it from
  619. * being released.
  620. */
  621. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  622. if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
  623. lpfc_nlp_get(ndlp);
  624. ns_ndlp_referenced = true;
  625. }
  626. /* Remove FC host and then SCSI host with the vport */
  627. fc_remove_host(shost);
  628. scsi_remove_host(shost);
  629. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  630. /* In case of driver unload, we shall not perform fabric logo as the
  631. * worker thread already stopped at this stage and, in this case, we
  632. * can safely skip the fabric logo.
  633. */
  634. if (phba->pport->load_flag & FC_UNLOADING) {
  635. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  636. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  637. phba->link_state >= LPFC_LINK_UP) {
  638. /* First look for the Fabric ndlp */
  639. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  640. if (!ndlp)
  641. goto skip_logo;
  642. else if (!NLP_CHK_NODE_ACT(ndlp)) {
  643. ndlp = lpfc_enable_node(vport, ndlp,
  644. NLP_STE_UNUSED_NODE);
  645. if (!ndlp)
  646. goto skip_logo;
  647. }
  648. /* Remove ndlp from vport npld list */
  649. lpfc_dequeue_node(vport, ndlp);
  650. /* Indicate free memory when release */
  651. spin_lock_irq(&phba->ndlp_lock);
  652. NLP_SET_FREE_REQ(ndlp);
  653. spin_unlock_irq(&phba->ndlp_lock);
  654. /* Kick off release ndlp when it can be safely done */
  655. lpfc_nlp_put(ndlp);
  656. }
  657. goto skip_logo;
  658. }
  659. /* Otherwise, we will perform fabric logo as needed */
  660. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  661. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  662. phba->link_state >= LPFC_LINK_UP &&
  663. phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
  664. if (vport->cfg_enable_da_id) {
  665. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  666. if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
  667. while (vport->ct_flags && timeout)
  668. timeout = schedule_timeout(timeout);
  669. else
  670. lpfc_printf_log(vport->phba, KERN_WARNING,
  671. LOG_VPORT,
  672. "1829 CT command failed to "
  673. "delete objects on fabric\n");
  674. }
  675. /* First look for the Fabric ndlp */
  676. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  677. if (!ndlp) {
  678. /* Cannot find existing Fabric ndlp, allocate one */
  679. ndlp = lpfc_nlp_init(vport, Fabric_DID);
  680. if (!ndlp)
  681. goto skip_logo;
  682. /* Indicate free memory when release */
  683. NLP_SET_FREE_REQ(ndlp);
  684. } else {
  685. if (!NLP_CHK_NODE_ACT(ndlp)) {
  686. ndlp = lpfc_enable_node(vport, ndlp,
  687. NLP_STE_UNUSED_NODE);
  688. if (!ndlp)
  689. goto skip_logo;
  690. }
  691. /* Remove ndlp from vport list */
  692. lpfc_dequeue_node(vport, ndlp);
  693. spin_lock_irq(&phba->ndlp_lock);
  694. if (!NLP_CHK_FREE_REQ(ndlp))
  695. /* Indicate free memory when release */
  696. NLP_SET_FREE_REQ(ndlp);
  697. else {
  698. /* Skip this if ndlp is already in free mode */
  699. spin_unlock_irq(&phba->ndlp_lock);
  700. goto skip_logo;
  701. }
  702. spin_unlock_irq(&phba->ndlp_lock);
  703. }
  704. /*
  705. * If the vpi is not registered, then a valid FDISC doesn't
  706. * exist and there is no need for a ELS LOGO. Just cleanup
  707. * the ndlp.
  708. */
  709. if (!(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  710. lpfc_nlp_put(ndlp);
  711. goto skip_logo;
  712. }
  713. vport->unreg_vpi_cmpl = VPORT_INVAL;
  714. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  715. if (!lpfc_issue_els_npiv_logo(vport, ndlp))
  716. while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
  717. timeout = schedule_timeout(timeout);
  718. }
  719. if (!(phba->pport->load_flag & FC_UNLOADING))
  720. lpfc_discovery_wait(vport);
  721. skip_logo:
  722. /*
  723. * If the NameServer ndlp has been incremented to allow the DA_ID CT
  724. * command to be sent, decrement the ndlp now.
  725. */
  726. if (ns_ndlp_referenced) {
  727. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  728. lpfc_nlp_put(ndlp);
  729. }
  730. lpfc_cleanup(vport);
  731. lpfc_sli_host_down(vport);
  732. lpfc_stop_vport_timers(vport);
  733. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  734. lpfc_unreg_all_rpis(vport);
  735. lpfc_unreg_default_rpis(vport);
  736. /*
  737. * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
  738. * does the scsi_host_put() to release the vport.
  739. */
  740. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
  741. lpfc_mbx_unreg_vpi(vport))
  742. scsi_host_put(shost);
  743. } else
  744. scsi_host_put(shost);
  745. lpfc_free_vpi(phba, vport->vpi);
  746. vport->work_port_events = 0;
  747. spin_lock_irq(&phba->hbalock);
  748. list_del_init(&vport->listentry);
  749. spin_unlock_irq(&phba->hbalock);
  750. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  751. "1828 Vport Deleted.\n");
  752. scsi_host_put(shost);
  753. return VPORT_OK;
  754. }
  755. struct lpfc_vport **
  756. lpfc_create_vport_work_array(struct lpfc_hba *phba)
  757. {
  758. struct lpfc_vport *port_iterator;
  759. struct lpfc_vport **vports;
  760. int index = 0;
  761. vports = kcalloc(phba->max_vports + 1, sizeof(struct lpfc_vport *),
  762. GFP_KERNEL);
  763. if (vports == NULL)
  764. return NULL;
  765. spin_lock_irq(&phba->hbalock);
  766. list_for_each_entry(port_iterator, &phba->port_list, listentry) {
  767. if (port_iterator->load_flag & FC_UNLOADING)
  768. continue;
  769. if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
  770. lpfc_printf_vlog(port_iterator, KERN_ERR, LOG_VPORT,
  771. "1801 Create vport work array FAILED: "
  772. "cannot do scsi_host_get\n");
  773. continue;
  774. }
  775. vports[index++] = port_iterator;
  776. }
  777. spin_unlock_irq(&phba->hbalock);
  778. return vports;
  779. }
  780. void
  781. lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
  782. {
  783. int i;
  784. if (vports == NULL)
  785. return;
  786. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  787. scsi_host_put(lpfc_shost_from_vport(vports[i]));
  788. kfree(vports);
  789. }
  790. /**
  791. * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
  792. * @vport: Pointer to vport object.
  793. *
  794. * This function resets the statistical data for the vport. This function
  795. * is called with the host_lock held
  796. **/
  797. void
  798. lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
  799. {
  800. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  801. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  802. if (!NLP_CHK_NODE_ACT(ndlp))
  803. continue;
  804. if (ndlp->lat_data)
  805. memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
  806. sizeof(struct lpfc_scsicmd_bkt));
  807. }
  808. }
  809. /**
  810. * lpfc_alloc_bucket - Allocate data buffer required for statistical data
  811. * @vport: Pointer to vport object.
  812. *
  813. * This function allocates data buffer required for all the FC
  814. * nodes of the vport to collect statistical data.
  815. **/
  816. void
  817. lpfc_alloc_bucket(struct lpfc_vport *vport)
  818. {
  819. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  820. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  821. if (!NLP_CHK_NODE_ACT(ndlp))
  822. continue;
  823. kfree(ndlp->lat_data);
  824. ndlp->lat_data = NULL;
  825. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
  826. ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
  827. sizeof(struct lpfc_scsicmd_bkt),
  828. GFP_ATOMIC);
  829. if (!ndlp->lat_data)
  830. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  831. "0287 lpfc_alloc_bucket failed to "
  832. "allocate statistical data buffer DID "
  833. "0x%x\n", ndlp->nlp_DID);
  834. }
  835. }
  836. }
  837. /**
  838. * lpfc_free_bucket - Free data buffer required for statistical data
  839. * @vport: Pointer to vport object.
  840. *
  841. * Th function frees statistical data buffer of all the FC
  842. * nodes of the vport.
  843. **/
  844. void
  845. lpfc_free_bucket(struct lpfc_vport *vport)
  846. {
  847. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  848. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  849. if (!NLP_CHK_NODE_ACT(ndlp))
  850. continue;
  851. kfree(ndlp->lat_data);
  852. ndlp->lat_data = NULL;
  853. }
  854. }