lpfc_vport.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2013 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. /*
  358. * In SLI4, the vpi must be activated before it can be used
  359. * by the port.
  360. */
  361. if ((phba->sli_rev == LPFC_SLI_REV4) &&
  362. (pport->fc_flag & FC_VFI_REGISTERED)) {
  363. rc = lpfc_sli4_init_vpi(vport);
  364. if (rc) {
  365. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  366. "1838 Failed to INIT_VPI on vpi %d "
  367. "status %d\n", vpi, rc);
  368. rc = VPORT_NORESOURCES;
  369. lpfc_free_vpi(phba, vpi);
  370. goto error_out;
  371. }
  372. } else if (phba->sli_rev == LPFC_SLI_REV4) {
  373. /*
  374. * Driver cannot INIT_VPI now. Set the flags to
  375. * init_vpi when reg_vfi complete.
  376. */
  377. vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
  378. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  379. rc = VPORT_OK;
  380. goto out;
  381. }
  382. if ((phba->link_state < LPFC_LINK_UP) ||
  383. (pport->port_state < LPFC_FABRIC_CFG_LINK) ||
  384. (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
  385. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  386. rc = VPORT_OK;
  387. goto out;
  388. }
  389. if (disable) {
  390. lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
  391. rc = VPORT_OK;
  392. goto out;
  393. }
  394. /* Use the Physical nodes Fabric NDLP to determine if the link is
  395. * up and ready to FDISC.
  396. */
  397. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  398. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  399. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
  400. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
  401. lpfc_set_disctmo(vport);
  402. lpfc_initial_fdisc(vport);
  403. } else {
  404. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  405. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  406. "0262 No NPIV Fabric support\n");
  407. }
  408. } else {
  409. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  410. }
  411. rc = VPORT_OK;
  412. out:
  413. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  414. "1825 Vport Created.\n");
  415. lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
  416. error_out:
  417. return rc;
  418. }
  419. static int
  420. disable_vport(struct fc_vport *fc_vport)
  421. {
  422. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  423. struct lpfc_hba *phba = vport->phba;
  424. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  425. long timeout;
  426. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  427. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  428. if (ndlp && NLP_CHK_NODE_ACT(ndlp)
  429. && phba->link_state >= LPFC_LINK_UP) {
  430. vport->unreg_vpi_cmpl = VPORT_INVAL;
  431. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  432. if (!lpfc_issue_els_npiv_logo(vport, ndlp))
  433. while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
  434. timeout = schedule_timeout(timeout);
  435. }
  436. lpfc_sli_host_down(vport);
  437. /* Mark all nodes for discovery so we can remove them by
  438. * calling lpfc_cleanup_rpis(vport, 1)
  439. */
  440. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  441. if (!NLP_CHK_NODE_ACT(ndlp))
  442. continue;
  443. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  444. continue;
  445. lpfc_disc_state_machine(vport, ndlp, NULL,
  446. NLP_EVT_DEVICE_RECOVERY);
  447. }
  448. lpfc_cleanup_rpis(vport, 1);
  449. lpfc_stop_vport_timers(vport);
  450. lpfc_unreg_all_rpis(vport);
  451. lpfc_unreg_default_rpis(vport);
  452. /*
  453. * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
  454. * scsi_host_put() to release the vport.
  455. */
  456. lpfc_mbx_unreg_vpi(vport);
  457. spin_lock_irq(shost->host_lock);
  458. vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
  459. spin_unlock_irq(shost->host_lock);
  460. lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
  461. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  462. "1826 Vport Disabled.\n");
  463. return VPORT_OK;
  464. }
  465. static int
  466. enable_vport(struct fc_vport *fc_vport)
  467. {
  468. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  469. struct lpfc_hba *phba = vport->phba;
  470. struct lpfc_nodelist *ndlp = NULL;
  471. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  472. if ((phba->link_state < LPFC_LINK_UP) ||
  473. (phba->fc_topology == LPFC_TOPOLOGY_LOOP)) {
  474. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  475. return VPORT_OK;
  476. }
  477. spin_lock_irq(shost->host_lock);
  478. vport->load_flag |= FC_LOADING;
  479. if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
  480. spin_unlock_irq(shost->host_lock);
  481. lpfc_issue_init_vpi(vport);
  482. goto out;
  483. }
  484. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  485. spin_unlock_irq(shost->host_lock);
  486. /* Use the Physical nodes Fabric NDLP to determine if the link is
  487. * up and ready to FDISC.
  488. */
  489. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  490. if (ndlp && NLP_CHK_NODE_ACT(ndlp)
  491. && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
  492. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
  493. lpfc_set_disctmo(vport);
  494. lpfc_initial_fdisc(vport);
  495. } else {
  496. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  497. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  498. "0264 No NPIV Fabric support\n");
  499. }
  500. } else {
  501. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  502. }
  503. out:
  504. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  505. "1827 Vport Enabled.\n");
  506. return VPORT_OK;
  507. }
  508. int
  509. lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
  510. {
  511. if (disable)
  512. return disable_vport(fc_vport);
  513. else
  514. return enable_vport(fc_vport);
  515. }
  516. int
  517. lpfc_vport_delete(struct fc_vport *fc_vport)
  518. {
  519. struct lpfc_nodelist *ndlp = NULL;
  520. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  521. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  522. struct lpfc_hba *phba = vport->phba;
  523. long timeout;
  524. bool ns_ndlp_referenced = false;
  525. if (vport->port_type == LPFC_PHYSICAL_PORT) {
  526. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  527. "1812 vport_delete failed: Cannot delete "
  528. "physical host\n");
  529. return VPORT_ERROR;
  530. }
  531. /* If the vport is a static vport fail the deletion. */
  532. if ((vport->vport_flag & STATIC_VPORT) &&
  533. !(phba->pport->load_flag & FC_UNLOADING)) {
  534. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  535. "1837 vport_delete failed: Cannot delete "
  536. "static vport.\n");
  537. return VPORT_ERROR;
  538. }
  539. spin_lock_irq(&phba->hbalock);
  540. vport->load_flag |= FC_UNLOADING;
  541. spin_unlock_irq(&phba->hbalock);
  542. /*
  543. * If we are not unloading the driver then prevent the vport_delete
  544. * from happening until after this vport's discovery is finished.
  545. */
  546. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  547. int check_count = 0;
  548. while (check_count < ((phba->fc_ratov * 3) + 3) &&
  549. vport->port_state > LPFC_VPORT_FAILED &&
  550. vport->port_state < LPFC_VPORT_READY) {
  551. check_count++;
  552. msleep(1000);
  553. }
  554. if (vport->port_state > LPFC_VPORT_FAILED &&
  555. vport->port_state < LPFC_VPORT_READY)
  556. return -EAGAIN;
  557. }
  558. /*
  559. * This is a bit of a mess. We want to ensure the shost doesn't get
  560. * torn down until we're done with the embedded lpfc_vport structure.
  561. *
  562. * Beyond holding a reference for this function, we also need a
  563. * reference for outstanding I/O requests we schedule during delete
  564. * processing. But once we scsi_remove_host() we can no longer obtain
  565. * a reference through scsi_host_get().
  566. *
  567. * So we take two references here. We release one reference at the
  568. * bottom of the function -- after delinking the vport. And we
  569. * release the other at the completion of the unreg_vpi that get's
  570. * initiated after we've disposed of all other resources associated
  571. * with the port.
  572. */
  573. if (!scsi_host_get(shost))
  574. return VPORT_INVAL;
  575. if (!scsi_host_get(shost)) {
  576. scsi_host_put(shost);
  577. return VPORT_INVAL;
  578. }
  579. lpfc_free_sysfs_attr(vport);
  580. lpfc_debugfs_terminate(vport);
  581. /*
  582. * The call to fc_remove_host might release the NameServer ndlp. Since
  583. * we might need to use the ndlp to send the DA_ID CT command,
  584. * increment the reference for the NameServer ndlp to prevent it from
  585. * being released.
  586. */
  587. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  588. if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
  589. lpfc_nlp_get(ndlp);
  590. ns_ndlp_referenced = true;
  591. }
  592. /* Remove FC host and then SCSI host with the vport */
  593. fc_remove_host(shost);
  594. scsi_remove_host(shost);
  595. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  596. /* In case of driver unload, we shall not perform fabric logo as the
  597. * worker thread already stopped at this stage and, in this case, we
  598. * can safely skip the fabric logo.
  599. */
  600. if (phba->pport->load_flag & FC_UNLOADING) {
  601. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  602. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  603. phba->link_state >= LPFC_LINK_UP) {
  604. /* First look for the Fabric ndlp */
  605. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  606. if (!ndlp)
  607. goto skip_logo;
  608. else if (!NLP_CHK_NODE_ACT(ndlp)) {
  609. ndlp = lpfc_enable_node(vport, ndlp,
  610. NLP_STE_UNUSED_NODE);
  611. if (!ndlp)
  612. goto skip_logo;
  613. }
  614. /* Remove ndlp from vport npld list */
  615. lpfc_dequeue_node(vport, ndlp);
  616. /* Indicate free memory when release */
  617. spin_lock_irq(&phba->ndlp_lock);
  618. NLP_SET_FREE_REQ(ndlp);
  619. spin_unlock_irq(&phba->ndlp_lock);
  620. /* Kick off release ndlp when it can be safely done */
  621. lpfc_nlp_put(ndlp);
  622. }
  623. goto skip_logo;
  624. }
  625. /* Otherwise, we will perform fabric logo as needed */
  626. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  627. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  628. phba->link_state >= LPFC_LINK_UP &&
  629. phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
  630. if (vport->cfg_enable_da_id) {
  631. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  632. if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
  633. while (vport->ct_flags && timeout)
  634. timeout = schedule_timeout(timeout);
  635. else
  636. lpfc_printf_log(vport->phba, KERN_WARNING,
  637. LOG_VPORT,
  638. "1829 CT command failed to "
  639. "delete objects on fabric\n");
  640. }
  641. /* First look for the Fabric ndlp */
  642. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  643. if (!ndlp) {
  644. /* Cannot find existing Fabric ndlp, allocate one */
  645. ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
  646. if (!ndlp)
  647. goto skip_logo;
  648. lpfc_nlp_init(vport, ndlp, Fabric_DID);
  649. /* Indicate free memory when release */
  650. NLP_SET_FREE_REQ(ndlp);
  651. } else {
  652. if (!NLP_CHK_NODE_ACT(ndlp)) {
  653. ndlp = lpfc_enable_node(vport, ndlp,
  654. NLP_STE_UNUSED_NODE);
  655. if (!ndlp)
  656. goto skip_logo;
  657. }
  658. /* Remove ndlp from vport list */
  659. lpfc_dequeue_node(vport, ndlp);
  660. spin_lock_irq(&phba->ndlp_lock);
  661. if (!NLP_CHK_FREE_REQ(ndlp))
  662. /* Indicate free memory when release */
  663. NLP_SET_FREE_REQ(ndlp);
  664. else {
  665. /* Skip this if ndlp is already in free mode */
  666. spin_unlock_irq(&phba->ndlp_lock);
  667. goto skip_logo;
  668. }
  669. spin_unlock_irq(&phba->ndlp_lock);
  670. }
  671. /*
  672. * If the vpi is not registered, then a valid FDISC doesn't
  673. * exist and there is no need for a ELS LOGO. Just cleanup
  674. * the ndlp.
  675. */
  676. if (!(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  677. lpfc_nlp_put(ndlp);
  678. goto skip_logo;
  679. }
  680. vport->unreg_vpi_cmpl = VPORT_INVAL;
  681. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  682. if (!lpfc_issue_els_npiv_logo(vport, ndlp))
  683. while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
  684. timeout = schedule_timeout(timeout);
  685. }
  686. if (!(phba->pport->load_flag & FC_UNLOADING))
  687. lpfc_discovery_wait(vport);
  688. skip_logo:
  689. /*
  690. * If the NameServer ndlp has been incremented to allow the DA_ID CT
  691. * command to be sent, decrement the ndlp now.
  692. */
  693. if (ns_ndlp_referenced) {
  694. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  695. lpfc_nlp_put(ndlp);
  696. }
  697. lpfc_cleanup(vport);
  698. lpfc_sli_host_down(vport);
  699. lpfc_stop_vport_timers(vport);
  700. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  701. lpfc_unreg_all_rpis(vport);
  702. lpfc_unreg_default_rpis(vport);
  703. /*
  704. * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
  705. * does the scsi_host_put() to release the vport.
  706. */
  707. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
  708. lpfc_mbx_unreg_vpi(vport))
  709. scsi_host_put(shost);
  710. } else
  711. scsi_host_put(shost);
  712. lpfc_free_vpi(phba, vport->vpi);
  713. vport->work_port_events = 0;
  714. spin_lock_irq(&phba->hbalock);
  715. list_del_init(&vport->listentry);
  716. spin_unlock_irq(&phba->hbalock);
  717. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  718. "1828 Vport Deleted.\n");
  719. scsi_host_put(shost);
  720. return VPORT_OK;
  721. }
  722. struct lpfc_vport **
  723. lpfc_create_vport_work_array(struct lpfc_hba *phba)
  724. {
  725. struct lpfc_vport *port_iterator;
  726. struct lpfc_vport **vports;
  727. int index = 0;
  728. vports = kzalloc((phba->max_vports + 1) * sizeof(struct lpfc_vport *),
  729. GFP_KERNEL);
  730. if (vports == NULL)
  731. return NULL;
  732. spin_lock_irq(&phba->hbalock);
  733. list_for_each_entry(port_iterator, &phba->port_list, listentry) {
  734. if (port_iterator->load_flag & FC_UNLOADING)
  735. continue;
  736. if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
  737. lpfc_printf_vlog(port_iterator, KERN_ERR, LOG_VPORT,
  738. "1801 Create vport work array FAILED: "
  739. "cannot do scsi_host_get\n");
  740. continue;
  741. }
  742. vports[index++] = port_iterator;
  743. }
  744. spin_unlock_irq(&phba->hbalock);
  745. return vports;
  746. }
  747. void
  748. lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
  749. {
  750. int i;
  751. if (vports == NULL)
  752. return;
  753. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  754. scsi_host_put(lpfc_shost_from_vport(vports[i]));
  755. kfree(vports);
  756. }
  757. /**
  758. * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
  759. * @vport: Pointer to vport object.
  760. *
  761. * This function resets the statistical data for the vport. This function
  762. * is called with the host_lock held
  763. **/
  764. void
  765. lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
  766. {
  767. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  768. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  769. if (!NLP_CHK_NODE_ACT(ndlp))
  770. continue;
  771. if (ndlp->lat_data)
  772. memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
  773. sizeof(struct lpfc_scsicmd_bkt));
  774. }
  775. }
  776. /**
  777. * lpfc_alloc_bucket - Allocate data buffer required for statistical data
  778. * @vport: Pointer to vport object.
  779. *
  780. * This function allocates data buffer required for all the FC
  781. * nodes of the vport to collect statistical data.
  782. **/
  783. void
  784. lpfc_alloc_bucket(struct lpfc_vport *vport)
  785. {
  786. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  787. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  788. if (!NLP_CHK_NODE_ACT(ndlp))
  789. continue;
  790. kfree(ndlp->lat_data);
  791. ndlp->lat_data = NULL;
  792. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
  793. ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
  794. sizeof(struct lpfc_scsicmd_bkt),
  795. GFP_ATOMIC);
  796. if (!ndlp->lat_data)
  797. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  798. "0287 lpfc_alloc_bucket failed to "
  799. "allocate statistical data buffer DID "
  800. "0x%x\n", ndlp->nlp_DID);
  801. }
  802. }
  803. }
  804. /**
  805. * lpfc_free_bucket - Free data buffer required for statistical data
  806. * @vport: Pointer to vport object.
  807. *
  808. * Th function frees statistical data buffer of all the FC
  809. * nodes of the vport.
  810. **/
  811. void
  812. lpfc_free_bucket(struct lpfc_vport *vport)
  813. {
  814. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  815. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  816. if (!NLP_CHK_NODE_ACT(ndlp))
  817. continue;
  818. kfree(ndlp->lat_data);
  819. ndlp->lat_data = NULL;
  820. }
  821. }