lpfc_vport.c 26 KB

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