lpfc_vport.c 24 KB

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