lpfc_vport.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  480. spin_unlock_irq(shost->host_lock);
  481. /* Use the Physical nodes Fabric NDLP to determine if the link is
  482. * up and ready to FDISC.
  483. */
  484. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  485. if (ndlp && NLP_CHK_NODE_ACT(ndlp)
  486. && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
  487. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
  488. lpfc_set_disctmo(vport);
  489. lpfc_initial_fdisc(vport);
  490. } else {
  491. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  492. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  493. "0264 No NPIV Fabric support\n");
  494. }
  495. } else {
  496. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  497. }
  498. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  499. "1827 Vport Enabled.\n");
  500. return VPORT_OK;
  501. }
  502. int
  503. lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
  504. {
  505. if (disable)
  506. return disable_vport(fc_vport);
  507. else
  508. return enable_vport(fc_vport);
  509. }
  510. int
  511. lpfc_vport_delete(struct fc_vport *fc_vport)
  512. {
  513. struct lpfc_nodelist *ndlp = NULL;
  514. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  515. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  516. struct lpfc_hba *phba = vport->phba;
  517. long timeout;
  518. bool ns_ndlp_referenced = false;
  519. if (vport->port_type == LPFC_PHYSICAL_PORT) {
  520. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  521. "1812 vport_delete failed: Cannot delete "
  522. "physical host\n");
  523. return VPORT_ERROR;
  524. }
  525. /* If the vport is a static vport fail the deletion. */
  526. if ((vport->vport_flag & STATIC_VPORT) &&
  527. !(phba->pport->load_flag & FC_UNLOADING)) {
  528. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  529. "1837 vport_delete failed: Cannot delete "
  530. "static vport.\n");
  531. return VPORT_ERROR;
  532. }
  533. spin_lock_irq(&phba->hbalock);
  534. vport->load_flag |= FC_UNLOADING;
  535. spin_unlock_irq(&phba->hbalock);
  536. /*
  537. * If we are not unloading the driver then prevent the vport_delete
  538. * from happening until after this vport's discovery is finished.
  539. */
  540. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  541. int check_count = 0;
  542. while (check_count < ((phba->fc_ratov * 3) + 3) &&
  543. vport->port_state > LPFC_VPORT_FAILED &&
  544. vport->port_state < LPFC_VPORT_READY) {
  545. check_count++;
  546. msleep(1000);
  547. }
  548. if (vport->port_state > LPFC_VPORT_FAILED &&
  549. vport->port_state < LPFC_VPORT_READY)
  550. return -EAGAIN;
  551. }
  552. /*
  553. * This is a bit of a mess. We want to ensure the shost doesn't get
  554. * torn down until we're done with the embedded lpfc_vport structure.
  555. *
  556. * Beyond holding a reference for this function, we also need a
  557. * reference for outstanding I/O requests we schedule during delete
  558. * processing. But once we scsi_remove_host() we can no longer obtain
  559. * a reference through scsi_host_get().
  560. *
  561. * So we take two references here. We release one reference at the
  562. * bottom of the function -- after delinking the vport. And we
  563. * release the other at the completion of the unreg_vpi that get's
  564. * initiated after we've disposed of all other resources associated
  565. * with the port.
  566. */
  567. if (!scsi_host_get(shost))
  568. return VPORT_INVAL;
  569. if (!scsi_host_get(shost)) {
  570. scsi_host_put(shost);
  571. return VPORT_INVAL;
  572. }
  573. lpfc_free_sysfs_attr(vport);
  574. lpfc_debugfs_terminate(vport);
  575. /*
  576. * The call to fc_remove_host might release the NameServer ndlp. Since
  577. * we might need to use the ndlp to send the DA_ID CT command,
  578. * increment the reference for the NameServer ndlp to prevent it from
  579. * being released.
  580. */
  581. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  582. if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
  583. lpfc_nlp_get(ndlp);
  584. ns_ndlp_referenced = true;
  585. }
  586. /* Remove FC host and then SCSI host with the vport */
  587. fc_remove_host(shost);
  588. scsi_remove_host(shost);
  589. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  590. /* In case of driver unload, we shall not perform fabric logo as the
  591. * worker thread already stopped at this stage and, in this case, we
  592. * can safely skip the fabric logo.
  593. */
  594. if (phba->pport->load_flag & FC_UNLOADING) {
  595. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  596. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  597. phba->link_state >= LPFC_LINK_UP) {
  598. /* First look for the Fabric ndlp */
  599. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  600. if (!ndlp)
  601. goto skip_logo;
  602. else if (!NLP_CHK_NODE_ACT(ndlp)) {
  603. ndlp = lpfc_enable_node(vport, ndlp,
  604. NLP_STE_UNUSED_NODE);
  605. if (!ndlp)
  606. goto skip_logo;
  607. }
  608. /* Remove ndlp from vport npld list */
  609. lpfc_dequeue_node(vport, ndlp);
  610. /* Indicate free memory when release */
  611. spin_lock_irq(&phba->ndlp_lock);
  612. NLP_SET_FREE_REQ(ndlp);
  613. spin_unlock_irq(&phba->ndlp_lock);
  614. /* Kick off release ndlp when it can be safely done */
  615. lpfc_nlp_put(ndlp);
  616. }
  617. goto skip_logo;
  618. }
  619. /* Otherwise, we will perform fabric logo as needed */
  620. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  621. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  622. phba->link_state >= LPFC_LINK_UP &&
  623. phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
  624. if (vport->cfg_enable_da_id) {
  625. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  626. if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
  627. while (vport->ct_flags && timeout)
  628. timeout = schedule_timeout(timeout);
  629. else
  630. lpfc_printf_log(vport->phba, KERN_WARNING,
  631. LOG_VPORT,
  632. "1829 CT command failed to "
  633. "delete objects on fabric\n");
  634. }
  635. /* First look for the Fabric ndlp */
  636. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  637. if (!ndlp) {
  638. /* Cannot find existing Fabric ndlp, allocate one */
  639. ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
  640. if (!ndlp)
  641. goto skip_logo;
  642. lpfc_nlp_init(vport, ndlp, Fabric_DID);
  643. /* Indicate free memory when release */
  644. NLP_SET_FREE_REQ(ndlp);
  645. } else {
  646. if (!NLP_CHK_NODE_ACT(ndlp)) {
  647. ndlp = lpfc_enable_node(vport, ndlp,
  648. NLP_STE_UNUSED_NODE);
  649. if (!ndlp)
  650. goto skip_logo;
  651. }
  652. /* Remove ndlp from vport list */
  653. lpfc_dequeue_node(vport, ndlp);
  654. spin_lock_irq(&phba->ndlp_lock);
  655. if (!NLP_CHK_FREE_REQ(ndlp))
  656. /* Indicate free memory when release */
  657. NLP_SET_FREE_REQ(ndlp);
  658. else {
  659. /* Skip this if ndlp is already in free mode */
  660. spin_unlock_irq(&phba->ndlp_lock);
  661. goto skip_logo;
  662. }
  663. spin_unlock_irq(&phba->ndlp_lock);
  664. }
  665. /*
  666. * If the vpi is not registered, then a valid FDISC doesn't
  667. * exist and there is no need for a ELS LOGO. Just cleanup
  668. * the ndlp.
  669. */
  670. if (!(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  671. lpfc_nlp_put(ndlp);
  672. goto skip_logo;
  673. }
  674. vport->unreg_vpi_cmpl = VPORT_INVAL;
  675. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  676. if (!lpfc_issue_els_npiv_logo(vport, ndlp))
  677. while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
  678. timeout = schedule_timeout(timeout);
  679. }
  680. if (!(phba->pport->load_flag & FC_UNLOADING))
  681. lpfc_discovery_wait(vport);
  682. skip_logo:
  683. /*
  684. * If the NameServer ndlp has been incremented to allow the DA_ID CT
  685. * command to be sent, decrement the ndlp now.
  686. */
  687. if (ns_ndlp_referenced) {
  688. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  689. lpfc_nlp_put(ndlp);
  690. }
  691. lpfc_cleanup(vport);
  692. lpfc_sli_host_down(vport);
  693. lpfc_stop_vport_timers(vport);
  694. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  695. lpfc_unreg_all_rpis(vport);
  696. lpfc_unreg_default_rpis(vport);
  697. /*
  698. * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
  699. * does the scsi_host_put() to release the vport.
  700. */
  701. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) ||
  702. lpfc_mbx_unreg_vpi(vport))
  703. scsi_host_put(shost);
  704. } else
  705. scsi_host_put(shost);
  706. lpfc_free_vpi(phba, vport->vpi);
  707. vport->work_port_events = 0;
  708. spin_lock_irq(&phba->hbalock);
  709. list_del_init(&vport->listentry);
  710. spin_unlock_irq(&phba->hbalock);
  711. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  712. "1828 Vport Deleted.\n");
  713. scsi_host_put(shost);
  714. return VPORT_OK;
  715. }
  716. struct lpfc_vport **
  717. lpfc_create_vport_work_array(struct lpfc_hba *phba)
  718. {
  719. struct lpfc_vport *port_iterator;
  720. struct lpfc_vport **vports;
  721. int index = 0;
  722. vports = kzalloc((phba->max_vports + 1) * sizeof(struct lpfc_vport *),
  723. GFP_KERNEL);
  724. if (vports == NULL)
  725. return NULL;
  726. spin_lock_irq(&phba->hbalock);
  727. list_for_each_entry(port_iterator, &phba->port_list, listentry) {
  728. if (port_iterator->load_flag & FC_UNLOADING)
  729. continue;
  730. if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
  731. lpfc_printf_vlog(port_iterator, KERN_ERR, LOG_VPORT,
  732. "1801 Create vport work array FAILED: "
  733. "cannot do scsi_host_get\n");
  734. continue;
  735. }
  736. vports[index++] = port_iterator;
  737. }
  738. spin_unlock_irq(&phba->hbalock);
  739. return vports;
  740. }
  741. void
  742. lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
  743. {
  744. int i;
  745. if (vports == NULL)
  746. return;
  747. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  748. scsi_host_put(lpfc_shost_from_vport(vports[i]));
  749. kfree(vports);
  750. }
  751. /**
  752. * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
  753. * @vport: Pointer to vport object.
  754. *
  755. * This function resets the statistical data for the vport. This function
  756. * is called with the host_lock held
  757. **/
  758. void
  759. lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
  760. {
  761. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  762. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  763. if (!NLP_CHK_NODE_ACT(ndlp))
  764. continue;
  765. if (ndlp->lat_data)
  766. memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
  767. sizeof(struct lpfc_scsicmd_bkt));
  768. }
  769. }
  770. /**
  771. * lpfc_alloc_bucket - Allocate data buffer required for statistical data
  772. * @vport: Pointer to vport object.
  773. *
  774. * This function allocates data buffer required for all the FC
  775. * nodes of the vport to collect statistical data.
  776. **/
  777. void
  778. lpfc_alloc_bucket(struct lpfc_vport *vport)
  779. {
  780. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  781. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  782. if (!NLP_CHK_NODE_ACT(ndlp))
  783. continue;
  784. kfree(ndlp->lat_data);
  785. ndlp->lat_data = NULL;
  786. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
  787. ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
  788. sizeof(struct lpfc_scsicmd_bkt),
  789. GFP_ATOMIC);
  790. if (!ndlp->lat_data)
  791. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  792. "0287 lpfc_alloc_bucket failed to "
  793. "allocate statistical data buffer DID "
  794. "0x%x\n", ndlp->nlp_DID);
  795. }
  796. }
  797. }
  798. /**
  799. * lpfc_free_bucket - Free data buffer required for statistical data
  800. * @vport: Pointer to vport object.
  801. *
  802. * Th function frees statistical data buffer of all the FC
  803. * nodes of the vport.
  804. **/
  805. void
  806. lpfc_free_bucket(struct lpfc_vport *vport)
  807. {
  808. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  809. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  810. if (!NLP_CHK_NODE_ACT(ndlp))
  811. continue;
  812. kfree(ndlp->lat_data);
  813. ndlp->lat_data = NULL;
  814. }
  815. }