lpfc_vport.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. * Take early refcount for outstanding I/O requests we schedule during
  594. * delete processing for unreg_vpi. Always keep this before
  595. * scsi_remove_host() as we can no longer obtain a reference through
  596. * scsi_host_get() after scsi_host_remove as shost is set to SHOST_DEL.
  597. */
  598. if (!scsi_host_get(shost))
  599. return VPORT_INVAL;
  600. lpfc_free_sysfs_attr(vport);
  601. lpfc_debugfs_terminate(vport);
  602. /*
  603. * The call to fc_remove_host might release the NameServer ndlp. Since
  604. * we might need to use the ndlp to send the DA_ID CT command,
  605. * increment the reference for the NameServer ndlp to prevent it from
  606. * being released.
  607. */
  608. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  609. if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
  610. lpfc_nlp_get(ndlp);
  611. ns_ndlp_referenced = true;
  612. }
  613. /* Remove FC host and then SCSI host with the vport */
  614. fc_remove_host(shost);
  615. scsi_remove_host(shost);
  616. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  617. /* In case of driver unload, we shall not perform fabric logo as the
  618. * worker thread already stopped at this stage and, in this case, we
  619. * can safely skip the fabric logo.
  620. */
  621. if (phba->pport->load_flag & FC_UNLOADING) {
  622. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  623. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  624. phba->link_state >= LPFC_LINK_UP) {
  625. /* First look for the Fabric ndlp */
  626. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  627. if (!ndlp)
  628. goto skip_logo;
  629. else if (!NLP_CHK_NODE_ACT(ndlp)) {
  630. ndlp = lpfc_enable_node(vport, ndlp,
  631. NLP_STE_UNUSED_NODE);
  632. if (!ndlp)
  633. goto skip_logo;
  634. }
  635. /* Remove ndlp from vport npld list */
  636. lpfc_dequeue_node(vport, ndlp);
  637. /* Indicate free memory when release */
  638. spin_lock_irq(&phba->ndlp_lock);
  639. NLP_SET_FREE_REQ(ndlp);
  640. spin_unlock_irq(&phba->ndlp_lock);
  641. /* Kick off release ndlp when it can be safely done */
  642. lpfc_nlp_put(ndlp);
  643. }
  644. goto skip_logo;
  645. }
  646. /* Otherwise, we will perform fabric logo as needed */
  647. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  648. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  649. phba->link_state >= LPFC_LINK_UP &&
  650. phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
  651. if (vport->cfg_enable_da_id) {
  652. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  653. if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
  654. while (vport->ct_flags && timeout)
  655. timeout = schedule_timeout(timeout);
  656. else
  657. lpfc_printf_log(vport->phba, KERN_WARNING,
  658. LOG_VPORT,
  659. "1829 CT command failed to "
  660. "delete objects on fabric\n");
  661. }
  662. /* First look for the Fabric ndlp */
  663. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  664. if (!ndlp) {
  665. /* Cannot find existing Fabric ndlp, allocate one */
  666. ndlp = lpfc_nlp_init(vport, Fabric_DID);
  667. if (!ndlp)
  668. goto skip_logo;
  669. /* Indicate free memory when release */
  670. NLP_SET_FREE_REQ(ndlp);
  671. } else {
  672. if (!NLP_CHK_NODE_ACT(ndlp)) {
  673. ndlp = lpfc_enable_node(vport, ndlp,
  674. NLP_STE_UNUSED_NODE);
  675. if (!ndlp)
  676. goto skip_logo;
  677. }
  678. /* Remove ndlp from vport list */
  679. lpfc_dequeue_node(vport, ndlp);
  680. spin_lock_irq(&phba->ndlp_lock);
  681. if (!NLP_CHK_FREE_REQ(ndlp))
  682. /* Indicate free memory when release */
  683. NLP_SET_FREE_REQ(ndlp);
  684. else {
  685. /* Skip this if ndlp is already in free mode */
  686. spin_unlock_irq(&phba->ndlp_lock);
  687. goto skip_logo;
  688. }
  689. spin_unlock_irq(&phba->ndlp_lock);
  690. }
  691. /*
  692. * If the vpi is not registered, then a valid FDISC doesn't
  693. * exist and there is no need for a ELS LOGO. Just cleanup
  694. * the ndlp.
  695. */
  696. if (!(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  697. lpfc_nlp_put(ndlp);
  698. goto skip_logo;
  699. }
  700. vport->unreg_vpi_cmpl = VPORT_INVAL;
  701. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  702. if (!lpfc_issue_els_npiv_logo(vport, ndlp))
  703. while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
  704. timeout = schedule_timeout(timeout);
  705. }
  706. if (!(phba->pport->load_flag & FC_UNLOADING))
  707. lpfc_discovery_wait(vport);
  708. skip_logo:
  709. /*
  710. * If the NameServer ndlp has been incremented to allow the DA_ID CT
  711. * command to be sent, decrement the ndlp now.
  712. */
  713. if (ns_ndlp_referenced) {
  714. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  715. lpfc_nlp_put(ndlp);
  716. }
  717. lpfc_cleanup(vport);
  718. lpfc_sli_host_down(vport);
  719. lpfc_stop_vport_timers(vport);
  720. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  721. lpfc_unreg_all_rpis(vport);
  722. lpfc_unreg_default_rpis(vport);
  723. /*
  724. * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
  725. * does the scsi_host_put() to release the vport.
  726. */
  727. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
  728. lpfc_mbx_unreg_vpi(vport))
  729. scsi_host_put(shost);
  730. } else {
  731. scsi_host_put(shost);
  732. }
  733. lpfc_free_vpi(phba, vport->vpi);
  734. vport->work_port_events = 0;
  735. spin_lock_irq(&phba->hbalock);
  736. list_del_init(&vport->listentry);
  737. spin_unlock_irq(&phba->hbalock);
  738. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  739. "1828 Vport Deleted.\n");
  740. scsi_host_put(shost);
  741. return VPORT_OK;
  742. }
  743. struct lpfc_vport **
  744. lpfc_create_vport_work_array(struct lpfc_hba *phba)
  745. {
  746. struct lpfc_vport *port_iterator;
  747. struct lpfc_vport **vports;
  748. int index = 0;
  749. vports = kcalloc(phba->max_vports + 1, sizeof(struct lpfc_vport *),
  750. GFP_KERNEL);
  751. if (vports == NULL)
  752. return NULL;
  753. spin_lock_irq(&phba->hbalock);
  754. list_for_each_entry(port_iterator, &phba->port_list, listentry) {
  755. if (port_iterator->load_flag & FC_UNLOADING)
  756. continue;
  757. if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
  758. lpfc_printf_vlog(port_iterator, KERN_ERR, LOG_VPORT,
  759. "1801 Create vport work array FAILED: "
  760. "cannot do scsi_host_get\n");
  761. continue;
  762. }
  763. vports[index++] = port_iterator;
  764. }
  765. spin_unlock_irq(&phba->hbalock);
  766. return vports;
  767. }
  768. void
  769. lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
  770. {
  771. int i;
  772. if (vports == NULL)
  773. return;
  774. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  775. scsi_host_put(lpfc_shost_from_vport(vports[i]));
  776. kfree(vports);
  777. }
  778. /**
  779. * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
  780. * @vport: Pointer to vport object.
  781. *
  782. * This function resets the statistical data for the vport. This function
  783. * is called with the host_lock held
  784. **/
  785. void
  786. lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
  787. {
  788. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  789. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  790. if (!NLP_CHK_NODE_ACT(ndlp))
  791. continue;
  792. if (ndlp->lat_data)
  793. memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
  794. sizeof(struct lpfc_scsicmd_bkt));
  795. }
  796. }
  797. /**
  798. * lpfc_alloc_bucket - Allocate data buffer required for statistical data
  799. * @vport: Pointer to vport object.
  800. *
  801. * This function allocates data buffer required for all the FC
  802. * nodes of the vport to collect statistical data.
  803. **/
  804. void
  805. lpfc_alloc_bucket(struct lpfc_vport *vport)
  806. {
  807. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  808. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  809. if (!NLP_CHK_NODE_ACT(ndlp))
  810. continue;
  811. kfree(ndlp->lat_data);
  812. ndlp->lat_data = NULL;
  813. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
  814. ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
  815. sizeof(struct lpfc_scsicmd_bkt),
  816. GFP_ATOMIC);
  817. if (!ndlp->lat_data)
  818. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  819. "0287 lpfc_alloc_bucket failed to "
  820. "allocate statistical data buffer DID "
  821. "0x%x\n", ndlp->nlp_DID);
  822. }
  823. }
  824. }
  825. /**
  826. * lpfc_free_bucket - Free data buffer required for statistical data
  827. * @vport: Pointer to vport object.
  828. *
  829. * Th function frees statistical data buffer of all the FC
  830. * nodes of the vport.
  831. **/
  832. void
  833. lpfc_free_bucket(struct lpfc_vport *vport)
  834. {
  835. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  836. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  837. if (!NLP_CHK_NODE_ACT(ndlp))
  838. continue;
  839. kfree(ndlp->lat_data);
  840. ndlp->lat_data = NULL;
  841. }
  842. }