lpfc_vport.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2017-2022 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. * Wait for the read_sparams mailbox to complete. Driver needs
  126. * this per vport to start the FDISC. If the mailbox fails,
  127. * just cleanup and return an error unless the failure is a
  128. * mailbox timeout. For MBX_TIMEOUT, allow the default
  129. * mbox completion handler to take care of the cleanup. This
  130. * is safe as the mailbox command isn't one that triggers
  131. * another mailbox.
  132. */
  133. pmb->vport = vport;
  134. rc = lpfc_sli_issue_mbox_wait(phba, pmb, phba->fc_ratov * 2);
  135. if (rc != MBX_SUCCESS) {
  136. if (signal_pending(current)) {
  137. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  138. "1830 Signal aborted mbxCmd x%x\n",
  139. mb->mbxCommand);
  140. if (rc != MBX_TIMEOUT)
  141. lpfc_mbox_rsrc_cleanup(phba, pmb,
  142. MBOX_THD_UNLOCKED);
  143. return -EINTR;
  144. } else {
  145. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  146. "1818 VPort failed init, mbxCmd x%x "
  147. "READ_SPARM mbxStatus x%x, rc = x%x\n",
  148. mb->mbxCommand, mb->mbxStatus, rc);
  149. if (rc != MBX_TIMEOUT)
  150. lpfc_mbox_rsrc_cleanup(phba, pmb,
  151. MBOX_THD_UNLOCKED);
  152. return -EIO;
  153. }
  154. }
  155. mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
  156. memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
  157. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  158. sizeof (struct lpfc_name));
  159. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  160. sizeof (struct lpfc_name));
  161. lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
  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->port_list_lock, 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->port_list_lock, flags);
  198. return 0;
  199. }
  200. }
  201. spin_unlock_irqrestore(&phba->port_list_lock, 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_TRACE_EVENT,
  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_TRACE_EVENT,
  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 Target enabled */
  289. if (phba->nvmet_support) {
  290. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  291. "3189 Create VPORT failed: "
  292. "NPIV is not supported on NVME Target\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_TRACE_EVENT,
  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_TRACE_EVENT,
  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_TRACE_EVENT,
  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_TRACE_EVENT,
  327. "1831 Create VPORT Interrupted.\n");
  328. rc = VPORT_ERROR;
  329. } else {
  330. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  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_TRACE_EVENT,
  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_TRACE_EVENT,
  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. /* Only the physical port can support NVME for now */
  367. vport->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
  368. *(struct lpfc_vport **)fc_vport->dd_data = vport;
  369. vport->fc_vport = fc_vport;
  370. /* At this point we are fully registered with SCSI Layer. */
  371. vport->load_flag |= FC_ALLOW_FDMI;
  372. if (phba->cfg_enable_SmartSAN ||
  373. (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
  374. /* Setup appropriate attribute masks */
  375. vport->fdmi_hba_mask = phba->pport->fdmi_hba_mask;
  376. vport->fdmi_port_mask = phba->pport->fdmi_port_mask;
  377. }
  378. /*
  379. * In SLI4, the vpi must be activated before it can be used
  380. * by the port.
  381. */
  382. if ((phba->sli_rev == LPFC_SLI_REV4) &&
  383. (pport->fc_flag & FC_VFI_REGISTERED)) {
  384. rc = lpfc_sli4_init_vpi(vport);
  385. if (rc) {
  386. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  387. "1838 Failed to INIT_VPI on vpi %d "
  388. "status %d\n", vpi, rc);
  389. rc = VPORT_NORESOURCES;
  390. lpfc_free_vpi(phba, vpi);
  391. goto error_out;
  392. }
  393. } else if (phba->sli_rev == LPFC_SLI_REV4) {
  394. /*
  395. * Driver cannot INIT_VPI now. Set the flags to
  396. * init_vpi when reg_vfi complete.
  397. */
  398. vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
  399. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  400. rc = VPORT_OK;
  401. goto out;
  402. }
  403. if ((phba->link_state < LPFC_LINK_UP) ||
  404. (pport->port_state < LPFC_FABRIC_CFG_LINK) ||
  405. (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
  406. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  407. rc = VPORT_OK;
  408. goto out;
  409. }
  410. if (disable) {
  411. lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
  412. rc = VPORT_OK;
  413. goto out;
  414. }
  415. /* Use the Physical nodes Fabric NDLP to determine if the link is
  416. * up and ready to FDISC.
  417. */
  418. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  419. if (ndlp &&
  420. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
  421. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
  422. lpfc_set_disctmo(vport);
  423. lpfc_initial_fdisc(vport);
  424. } else {
  425. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  426. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  427. "0262 No NPIV Fabric support\n");
  428. }
  429. } else {
  430. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  431. }
  432. rc = VPORT_OK;
  433. out:
  434. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  435. "1825 Vport Created.\n");
  436. lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
  437. error_out:
  438. return rc;
  439. }
  440. static int
  441. lpfc_send_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  442. {
  443. int rc;
  444. struct lpfc_hba *phba = vport->phba;
  445. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  446. spin_lock_irq(&ndlp->lock);
  447. if (!(ndlp->save_flags & NLP_WAIT_FOR_LOGO) &&
  448. !ndlp->logo_waitq) {
  449. ndlp->logo_waitq = &waitq;
  450. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  451. ndlp->nlp_flag |= NLP_ISSUE_LOGO;
  452. ndlp->save_flags |= NLP_WAIT_FOR_LOGO;
  453. }
  454. spin_unlock_irq(&ndlp->lock);
  455. rc = lpfc_issue_els_npiv_logo(vport, ndlp);
  456. if (!rc) {
  457. wait_event_timeout(waitq,
  458. (!(ndlp->save_flags & NLP_WAIT_FOR_LOGO)),
  459. msecs_to_jiffies(phba->fc_ratov * 2000));
  460. if (!(ndlp->save_flags & NLP_WAIT_FOR_LOGO))
  461. goto logo_cmpl;
  462. /* LOGO wait failed. Correct status. */
  463. rc = -EINTR;
  464. } else {
  465. rc = -EIO;
  466. }
  467. /* Error - clean up node flags. */
  468. spin_lock_irq(&ndlp->lock);
  469. ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
  470. ndlp->save_flags &= ~NLP_WAIT_FOR_LOGO;
  471. spin_unlock_irq(&ndlp->lock);
  472. logo_cmpl:
  473. lpfc_printf_vlog(vport, KERN_INFO, LOG_VPORT,
  474. "1824 Issue LOGO completes with status %d\n",
  475. rc);
  476. spin_lock_irq(&ndlp->lock);
  477. ndlp->logo_waitq = NULL;
  478. spin_unlock_irq(&ndlp->lock);
  479. return rc;
  480. }
  481. static int
  482. disable_vport(struct fc_vport *fc_vport)
  483. {
  484. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  485. struct lpfc_hba *phba = vport->phba;
  486. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  487. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  488. /* Can't disable during an outstanding delete. */
  489. if (vport->load_flag & FC_UNLOADING)
  490. return 0;
  491. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  492. if (ndlp && phba->link_state >= LPFC_LINK_UP)
  493. (void)lpfc_send_npiv_logo(vport, ndlp);
  494. lpfc_sli_host_down(vport);
  495. /* Mark all nodes for discovery so we can remove them by
  496. * calling lpfc_cleanup_rpis(vport, 1)
  497. */
  498. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  499. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  500. continue;
  501. lpfc_disc_state_machine(vport, ndlp, NULL,
  502. NLP_EVT_DEVICE_RECOVERY);
  503. }
  504. lpfc_cleanup_rpis(vport, 1);
  505. lpfc_stop_vport_timers(vport);
  506. lpfc_unreg_all_rpis(vport);
  507. lpfc_unreg_default_rpis(vport);
  508. /*
  509. * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
  510. * scsi_host_put() to release the vport.
  511. */
  512. lpfc_mbx_unreg_vpi(vport);
  513. if (phba->sli_rev == LPFC_SLI_REV4) {
  514. spin_lock_irq(shost->host_lock);
  515. vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
  516. spin_unlock_irq(shost->host_lock);
  517. }
  518. lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
  519. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  520. "1826 Vport Disabled.\n");
  521. return VPORT_OK;
  522. }
  523. static int
  524. enable_vport(struct fc_vport *fc_vport)
  525. {
  526. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  527. struct lpfc_hba *phba = vport->phba;
  528. struct lpfc_nodelist *ndlp = NULL;
  529. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  530. if ((phba->link_state < LPFC_LINK_UP) ||
  531. (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
  532. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  533. return VPORT_OK;
  534. }
  535. spin_lock_irq(shost->host_lock);
  536. vport->load_flag |= FC_LOADING;
  537. if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
  538. spin_unlock_irq(shost->host_lock);
  539. lpfc_issue_init_vpi(vport);
  540. goto out;
  541. }
  542. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  543. spin_unlock_irq(shost->host_lock);
  544. /* Use the Physical nodes Fabric NDLP to determine if the link is
  545. * up and ready to FDISC.
  546. */
  547. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  548. if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
  549. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
  550. lpfc_set_disctmo(vport);
  551. lpfc_initial_fdisc(vport);
  552. } else {
  553. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  554. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  555. "0264 No NPIV Fabric support\n");
  556. }
  557. } else {
  558. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  559. }
  560. out:
  561. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  562. "1827 Vport Enabled.\n");
  563. return VPORT_OK;
  564. }
  565. int
  566. lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
  567. {
  568. if (disable)
  569. return disable_vport(fc_vport);
  570. else
  571. return enable_vport(fc_vport);
  572. }
  573. int
  574. lpfc_vport_delete(struct fc_vport *fc_vport)
  575. {
  576. struct lpfc_nodelist *ndlp = NULL;
  577. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  578. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  579. struct lpfc_hba *phba = vport->phba;
  580. int rc;
  581. if (vport->port_type == LPFC_PHYSICAL_PORT) {
  582. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  583. "1812 vport_delete failed: Cannot delete "
  584. "physical host\n");
  585. return VPORT_ERROR;
  586. }
  587. /* If the vport is a static vport fail the deletion. */
  588. if ((vport->vport_flag & STATIC_VPORT) &&
  589. !(phba->pport->load_flag & FC_UNLOADING)) {
  590. lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
  591. "1837 vport_delete failed: Cannot delete "
  592. "static vport.\n");
  593. return VPORT_ERROR;
  594. }
  595. spin_lock_irq(&phba->hbalock);
  596. vport->load_flag |= FC_UNLOADING;
  597. spin_unlock_irq(&phba->hbalock);
  598. /*
  599. * If we are not unloading the driver then prevent the vport_delete
  600. * from happening until after this vport's discovery is finished.
  601. */
  602. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  603. int check_count = 0;
  604. while (check_count < ((phba->fc_ratov * 3) + 3) &&
  605. vport->port_state > LPFC_VPORT_FAILED &&
  606. vport->port_state < LPFC_VPORT_READY) {
  607. check_count++;
  608. msleep(1000);
  609. }
  610. if (vport->port_state > LPFC_VPORT_FAILED &&
  611. vport->port_state < LPFC_VPORT_READY)
  612. return -EAGAIN;
  613. }
  614. /*
  615. * Take early refcount for outstanding I/O requests we schedule during
  616. * delete processing for unreg_vpi. Always keep this before
  617. * scsi_remove_host() as we can no longer obtain a reference through
  618. * scsi_host_get() after scsi_host_remove as shost is set to SHOST_DEL.
  619. */
  620. if (!scsi_host_get(shost))
  621. return VPORT_INVAL;
  622. lpfc_free_sysfs_attr(vport);
  623. lpfc_debugfs_terminate(vport);
  624. /* Remove FC host to break driver binding. */
  625. fc_remove_host(shost);
  626. scsi_remove_host(shost);
  627. /* Send the DA_ID and Fabric LOGO to cleanup Nameserver entries. */
  628. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  629. if (!ndlp)
  630. goto skip_logo;
  631. if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  632. phba->link_state >= LPFC_LINK_UP &&
  633. phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
  634. if (vport->cfg_enable_da_id) {
  635. /* Send DA_ID and wait for a completion. */
  636. rc = lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0);
  637. if (rc) {
  638. lpfc_printf_log(vport->phba, KERN_WARNING,
  639. LOG_VPORT,
  640. "1829 CT command failed to "
  641. "delete objects on fabric, "
  642. "rc %d\n", rc);
  643. }
  644. }
  645. /*
  646. * If the vpi is not registered, then a valid FDISC doesn't
  647. * exist and there is no need for a ELS LOGO. Just cleanup
  648. * the ndlp.
  649. */
  650. if (!(vport->vpi_state & LPFC_VPI_REGISTERED))
  651. goto skip_logo;
  652. /* Issue a Fabric LOGO to cleanup fabric resources. */
  653. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  654. if (!ndlp)
  655. goto skip_logo;
  656. rc = lpfc_send_npiv_logo(vport, ndlp);
  657. if (rc)
  658. goto skip_logo;
  659. }
  660. if (!(phba->pport->load_flag & FC_UNLOADING))
  661. lpfc_discovery_wait(vport);
  662. skip_logo:
  663. lpfc_cleanup(vport);
  664. /* Remove scsi host now. The nodes are cleaned up. */
  665. lpfc_sli_host_down(vport);
  666. lpfc_stop_vport_timers(vport);
  667. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  668. lpfc_unreg_all_rpis(vport);
  669. lpfc_unreg_default_rpis(vport);
  670. /*
  671. * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
  672. * does the scsi_host_put() to release the vport.
  673. */
  674. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
  675. lpfc_mbx_unreg_vpi(vport))
  676. scsi_host_put(shost);
  677. } else {
  678. scsi_host_put(shost);
  679. }
  680. lpfc_free_vpi(phba, vport->vpi);
  681. vport->work_port_events = 0;
  682. spin_lock_irq(&phba->port_list_lock);
  683. list_del_init(&vport->listentry);
  684. spin_unlock_irq(&phba->port_list_lock);
  685. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  686. "1828 Vport Deleted.\n");
  687. scsi_host_put(shost);
  688. return VPORT_OK;
  689. }
  690. struct lpfc_vport **
  691. lpfc_create_vport_work_array(struct lpfc_hba *phba)
  692. {
  693. struct lpfc_vport *port_iterator;
  694. struct lpfc_vport **vports;
  695. int index = 0;
  696. vports = kcalloc(phba->max_vports + 1, sizeof(struct lpfc_vport *),
  697. GFP_KERNEL);
  698. if (vports == NULL)
  699. return NULL;
  700. spin_lock_irq(&phba->port_list_lock);
  701. list_for_each_entry(port_iterator, &phba->port_list, listentry) {
  702. if (port_iterator->load_flag & FC_UNLOADING)
  703. continue;
  704. if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
  705. lpfc_printf_vlog(port_iterator, KERN_ERR,
  706. LOG_TRACE_EVENT,
  707. "1801 Create vport work array FAILED: "
  708. "cannot do scsi_host_get\n");
  709. continue;
  710. }
  711. vports[index++] = port_iterator;
  712. }
  713. spin_unlock_irq(&phba->port_list_lock);
  714. return vports;
  715. }
  716. void
  717. lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
  718. {
  719. int i;
  720. if (vports == NULL)
  721. return;
  722. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  723. scsi_host_put(lpfc_shost_from_vport(vports[i]));
  724. kfree(vports);
  725. }