lpfc_vport.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. /*
  290. * In SLI4, the vpi must be activated before it can be used
  291. * by the port.
  292. */
  293. if (phba->sli_rev == LPFC_SLI_REV4) {
  294. rc = lpfc_sli4_init_vpi(phba, vpi);
  295. if (rc) {
  296. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  297. "1838 Failed to INIT_VPI on vpi %d "
  298. "status %d\n", vpi, rc);
  299. rc = VPORT_NORESOURCES;
  300. lpfc_free_vpi(phba, vpi);
  301. goto error_out;
  302. }
  303. }
  304. /* Assign an unused board number */
  305. if ((instance = lpfc_get_instance()) < 0) {
  306. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  307. "1810 Create VPORT failed: Cannot get "
  308. "instance number\n");
  309. lpfc_free_vpi(phba, vpi);
  310. rc = VPORT_NORESOURCES;
  311. goto error_out;
  312. }
  313. vport = lpfc_create_port(phba, instance, &fc_vport->dev);
  314. if (!vport) {
  315. lpfc_printf_log(phba, KERN_ERR, LOG_VPORT,
  316. "1811 Create VPORT failed: vpi x%x\n", vpi);
  317. lpfc_free_vpi(phba, vpi);
  318. rc = VPORT_NORESOURCES;
  319. goto error_out;
  320. }
  321. vport->vpi = vpi;
  322. lpfc_debugfs_initialize(vport);
  323. if ((status = lpfc_vport_sparm(phba, vport))) {
  324. if (status == -EINTR) {
  325. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  326. "1831 Create VPORT Interrupted.\n");
  327. rc = VPORT_ERROR;
  328. } else {
  329. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  330. "1813 Create VPORT failed. "
  331. "Cannot get sparam\n");
  332. rc = VPORT_NORESOURCES;
  333. }
  334. lpfc_free_vpi(phba, vpi);
  335. destroy_port(vport);
  336. goto error_out;
  337. }
  338. memcpy(vport->fc_portname.u.wwn, vport->fc_sparam.portName.u.wwn, 8);
  339. memcpy(vport->fc_nodename.u.wwn, vport->fc_sparam.nodeName.u.wwn, 8);
  340. if (fc_vport->node_name != 0)
  341. u64_to_wwn(fc_vport->node_name, vport->fc_nodename.u.wwn);
  342. if (fc_vport->port_name != 0)
  343. u64_to_wwn(fc_vport->port_name, vport->fc_portname.u.wwn);
  344. memcpy(&vport->fc_sparam.portName, vport->fc_portname.u.wwn, 8);
  345. memcpy(&vport->fc_sparam.nodeName, vport->fc_nodename.u.wwn, 8);
  346. if (!lpfc_valid_wwn_format(phba, &vport->fc_sparam.nodeName, "WWNN") ||
  347. !lpfc_valid_wwn_format(phba, &vport->fc_sparam.portName, "WWPN")) {
  348. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  349. "1821 Create VPORT failed. "
  350. "Invalid WWN format\n");
  351. lpfc_free_vpi(phba, vpi);
  352. destroy_port(vport);
  353. rc = VPORT_INVAL;
  354. goto error_out;
  355. }
  356. if (!lpfc_unique_wwpn(phba, vport)) {
  357. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  358. "1823 Create VPORT failed. "
  359. "Duplicate WWN on HBA\n");
  360. lpfc_free_vpi(phba, vpi);
  361. destroy_port(vport);
  362. rc = VPORT_INVAL;
  363. goto error_out;
  364. }
  365. /* Create binary sysfs attribute for vport */
  366. lpfc_alloc_sysfs_attr(vport);
  367. *(struct lpfc_vport **)fc_vport->dd_data = vport;
  368. vport->fc_vport = fc_vport;
  369. if ((phba->link_state < LPFC_LINK_UP) ||
  370. (phba->fc_topology == TOPOLOGY_LOOP)) {
  371. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  372. rc = VPORT_OK;
  373. goto out;
  374. }
  375. if (disable) {
  376. lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
  377. rc = VPORT_OK;
  378. goto out;
  379. }
  380. /* Use the Physical nodes Fabric NDLP to determine if the link is
  381. * up and ready to FDISC.
  382. */
  383. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  384. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  385. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
  386. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
  387. lpfc_set_disctmo(vport);
  388. lpfc_initial_fdisc(vport);
  389. } else {
  390. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  391. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  392. "0262 No NPIV Fabric support\n");
  393. }
  394. } else {
  395. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  396. }
  397. rc = VPORT_OK;
  398. out:
  399. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  400. "1825 Vport Created.\n");
  401. lpfc_host_attrib_init(lpfc_shost_from_vport(vport));
  402. error_out:
  403. return rc;
  404. }
  405. static int
  406. disable_vport(struct fc_vport *fc_vport)
  407. {
  408. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  409. struct lpfc_hba *phba = vport->phba;
  410. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  411. long timeout;
  412. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  413. if (ndlp && NLP_CHK_NODE_ACT(ndlp)
  414. && phba->link_state >= LPFC_LINK_UP) {
  415. vport->unreg_vpi_cmpl = VPORT_INVAL;
  416. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  417. if (!lpfc_issue_els_npiv_logo(vport, ndlp))
  418. while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
  419. timeout = schedule_timeout(timeout);
  420. }
  421. lpfc_sli_host_down(vport);
  422. /* Mark all nodes for discovery so we can remove them by
  423. * calling lpfc_cleanup_rpis(vport, 1)
  424. */
  425. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  426. if (!NLP_CHK_NODE_ACT(ndlp))
  427. continue;
  428. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  429. continue;
  430. lpfc_disc_state_machine(vport, ndlp, NULL,
  431. NLP_EVT_DEVICE_RECOVERY);
  432. }
  433. lpfc_cleanup_rpis(vport, 1);
  434. lpfc_stop_vport_timers(vport);
  435. lpfc_unreg_all_rpis(vport);
  436. lpfc_unreg_default_rpis(vport);
  437. /*
  438. * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi) does the
  439. * scsi_host_put() to release the vport.
  440. */
  441. lpfc_mbx_unreg_vpi(vport);
  442. lpfc_vport_set_state(vport, FC_VPORT_DISABLED);
  443. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  444. "1826 Vport Disabled.\n");
  445. return VPORT_OK;
  446. }
  447. static int
  448. enable_vport(struct fc_vport *fc_vport)
  449. {
  450. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  451. struct lpfc_hba *phba = vport->phba;
  452. struct lpfc_nodelist *ndlp = NULL;
  453. if ((phba->link_state < LPFC_LINK_UP) ||
  454. (phba->fc_topology == TOPOLOGY_LOOP)) {
  455. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  456. return VPORT_OK;
  457. }
  458. vport->load_flag |= FC_LOADING;
  459. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  460. /* Use the Physical nodes Fabric NDLP to determine if the link is
  461. * up and ready to FDISC.
  462. */
  463. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  464. if (ndlp && NLP_CHK_NODE_ACT(ndlp)
  465. && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
  466. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED) {
  467. lpfc_set_disctmo(vport);
  468. lpfc_initial_fdisc(vport);
  469. } else {
  470. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  471. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  472. "0264 No NPIV Fabric support\n");
  473. }
  474. } else {
  475. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  476. }
  477. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  478. "1827 Vport Enabled.\n");
  479. return VPORT_OK;
  480. }
  481. int
  482. lpfc_vport_disable(struct fc_vport *fc_vport, bool disable)
  483. {
  484. if (disable)
  485. return disable_vport(fc_vport);
  486. else
  487. return enable_vport(fc_vport);
  488. }
  489. int
  490. lpfc_vport_delete(struct fc_vport *fc_vport)
  491. {
  492. struct lpfc_nodelist *ndlp = NULL;
  493. struct Scsi_Host *shost = (struct Scsi_Host *) fc_vport->shost;
  494. struct lpfc_vport *vport = *(struct lpfc_vport **)fc_vport->dd_data;
  495. struct lpfc_hba *phba = vport->phba;
  496. long timeout;
  497. if (vport->port_type == LPFC_PHYSICAL_PORT) {
  498. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  499. "1812 vport_delete failed: Cannot delete "
  500. "physical host\n");
  501. return VPORT_ERROR;
  502. }
  503. /* If the vport is a static vport fail the deletion. */
  504. if ((vport->vport_flag & STATIC_VPORT) &&
  505. !(phba->pport->load_flag & FC_UNLOADING)) {
  506. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  507. "1837 vport_delete failed: Cannot delete "
  508. "static vport.\n");
  509. return VPORT_ERROR;
  510. }
  511. /*
  512. * If we are not unloading the driver then prevent the vport_delete
  513. * from happening until after this vport's discovery is finished.
  514. */
  515. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  516. int check_count = 0;
  517. while (check_count < ((phba->fc_ratov * 3) + 3) &&
  518. vport->port_state > LPFC_VPORT_FAILED &&
  519. vport->port_state < LPFC_VPORT_READY) {
  520. check_count++;
  521. msleep(1000);
  522. }
  523. if (vport->port_state > LPFC_VPORT_FAILED &&
  524. vport->port_state < LPFC_VPORT_READY)
  525. return -EAGAIN;
  526. }
  527. /*
  528. * This is a bit of a mess. We want to ensure the shost doesn't get
  529. * torn down until we're done with the embedded lpfc_vport structure.
  530. *
  531. * Beyond holding a reference for this function, we also need a
  532. * reference for outstanding I/O requests we schedule during delete
  533. * processing. But once we scsi_remove_host() we can no longer obtain
  534. * a reference through scsi_host_get().
  535. *
  536. * So we take two references here. We release one reference at the
  537. * bottom of the function -- after delinking the vport. And we
  538. * release the other at the completion of the unreg_vpi that get's
  539. * initiated after we've disposed of all other resources associated
  540. * with the port.
  541. */
  542. if (!scsi_host_get(shost))
  543. return VPORT_INVAL;
  544. if (!scsi_host_get(shost)) {
  545. scsi_host_put(shost);
  546. return VPORT_INVAL;
  547. }
  548. spin_lock_irq(&phba->hbalock);
  549. vport->load_flag |= FC_UNLOADING;
  550. spin_unlock_irq(&phba->hbalock);
  551. lpfc_free_sysfs_attr(vport);
  552. lpfc_debugfs_terminate(vport);
  553. /* Remove FC host and then SCSI host with the vport */
  554. fc_remove_host(lpfc_shost_from_vport(vport));
  555. scsi_remove_host(lpfc_shost_from_vport(vport));
  556. ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
  557. /* In case of driver unload, we shall not perform fabric logo as the
  558. * worker thread already stopped at this stage and, in this case, we
  559. * can safely skip the fabric logo.
  560. */
  561. if (phba->pport->load_flag & FC_UNLOADING) {
  562. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  563. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  564. phba->link_state >= LPFC_LINK_UP) {
  565. /* First look for the Fabric ndlp */
  566. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  567. if (!ndlp)
  568. goto skip_logo;
  569. else if (!NLP_CHK_NODE_ACT(ndlp)) {
  570. ndlp = lpfc_enable_node(vport, ndlp,
  571. NLP_STE_UNUSED_NODE);
  572. if (!ndlp)
  573. goto skip_logo;
  574. }
  575. /* Remove ndlp from vport npld list */
  576. lpfc_dequeue_node(vport, ndlp);
  577. /* Indicate free memory when release */
  578. spin_lock_irq(&phba->ndlp_lock);
  579. NLP_SET_FREE_REQ(ndlp);
  580. spin_unlock_irq(&phba->ndlp_lock);
  581. /* Kick off release ndlp when it can be safely done */
  582. lpfc_nlp_put(ndlp);
  583. }
  584. goto skip_logo;
  585. }
  586. /* Otherwise, we will perform fabric logo as needed */
  587. if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
  588. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE &&
  589. phba->link_state >= LPFC_LINK_UP &&
  590. phba->fc_topology != TOPOLOGY_LOOP) {
  591. if (vport->cfg_enable_da_id) {
  592. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  593. if (!lpfc_ns_cmd(vport, SLI_CTNS_DA_ID, 0, 0))
  594. while (vport->ct_flags && timeout)
  595. timeout = schedule_timeout(timeout);
  596. else
  597. lpfc_printf_log(vport->phba, KERN_WARNING,
  598. LOG_VPORT,
  599. "1829 CT command failed to "
  600. "delete objects on fabric. \n");
  601. }
  602. /* First look for the Fabric ndlp */
  603. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  604. if (!ndlp) {
  605. /* Cannot find existing Fabric ndlp, allocate one */
  606. ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
  607. if (!ndlp)
  608. goto skip_logo;
  609. lpfc_nlp_init(vport, ndlp, Fabric_DID);
  610. /* Indicate free memory when release */
  611. NLP_SET_FREE_REQ(ndlp);
  612. } else {
  613. if (!NLP_CHK_NODE_ACT(ndlp))
  614. ndlp = lpfc_enable_node(vport, ndlp,
  615. NLP_STE_UNUSED_NODE);
  616. if (!ndlp)
  617. goto skip_logo;
  618. /* Remove ndlp from vport npld list */
  619. lpfc_dequeue_node(vport, ndlp);
  620. spin_lock_irq(&phba->ndlp_lock);
  621. if (!NLP_CHK_FREE_REQ(ndlp))
  622. /* Indicate free memory when release */
  623. NLP_SET_FREE_REQ(ndlp);
  624. else {
  625. /* Skip this if ndlp is already in free mode */
  626. spin_unlock_irq(&phba->ndlp_lock);
  627. goto skip_logo;
  628. }
  629. spin_unlock_irq(&phba->ndlp_lock);
  630. }
  631. vport->unreg_vpi_cmpl = VPORT_INVAL;
  632. timeout = msecs_to_jiffies(phba->fc_ratov * 2000);
  633. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  634. goto skip_logo;
  635. if (!lpfc_issue_els_npiv_logo(vport, ndlp))
  636. while (vport->unreg_vpi_cmpl == VPORT_INVAL && timeout)
  637. timeout = schedule_timeout(timeout);
  638. }
  639. if (!(phba->pport->load_flag & FC_UNLOADING))
  640. lpfc_discovery_wait(vport);
  641. skip_logo:
  642. lpfc_cleanup(vport);
  643. lpfc_sli_host_down(vport);
  644. lpfc_stop_vport_timers(vport);
  645. if (!(phba->pport->load_flag & FC_UNLOADING)) {
  646. lpfc_unreg_all_rpis(vport);
  647. lpfc_unreg_default_rpis(vport);
  648. /*
  649. * Completion of unreg_vpi (lpfc_mbx_cmpl_unreg_vpi)
  650. * does the scsi_host_put() to release the vport.
  651. */
  652. if (lpfc_mbx_unreg_vpi(vport))
  653. scsi_host_put(shost);
  654. } else
  655. scsi_host_put(shost);
  656. lpfc_free_vpi(phba, vport->vpi);
  657. vport->work_port_events = 0;
  658. spin_lock_irq(&phba->hbalock);
  659. list_del_init(&vport->listentry);
  660. spin_unlock_irq(&phba->hbalock);
  661. lpfc_printf_vlog(vport, KERN_ERR, LOG_VPORT,
  662. "1828 Vport Deleted.\n");
  663. scsi_host_put(shost);
  664. return VPORT_OK;
  665. }
  666. struct lpfc_vport **
  667. lpfc_create_vport_work_array(struct lpfc_hba *phba)
  668. {
  669. struct lpfc_vport *port_iterator;
  670. struct lpfc_vport **vports;
  671. int index = 0;
  672. vports = kzalloc((phba->max_vports + 1) * sizeof(struct lpfc_vport *),
  673. GFP_KERNEL);
  674. if (vports == NULL)
  675. return NULL;
  676. spin_lock_irq(&phba->hbalock);
  677. list_for_each_entry(port_iterator, &phba->port_list, listentry) {
  678. if (!scsi_host_get(lpfc_shost_from_vport(port_iterator))) {
  679. lpfc_printf_vlog(port_iterator, KERN_WARNING, LOG_VPORT,
  680. "1801 Create vport work array FAILED: "
  681. "cannot do scsi_host_get\n");
  682. continue;
  683. }
  684. vports[index++] = port_iterator;
  685. }
  686. spin_unlock_irq(&phba->hbalock);
  687. return vports;
  688. }
  689. void
  690. lpfc_destroy_vport_work_array(struct lpfc_hba *phba, struct lpfc_vport **vports)
  691. {
  692. int i;
  693. if (vports == NULL)
  694. return;
  695. for (i = 0; vports[i] != NULL && i <= phba->max_vports; i++)
  696. scsi_host_put(lpfc_shost_from_vport(vports[i]));
  697. kfree(vports);
  698. }
  699. /**
  700. * lpfc_vport_reset_stat_data - Reset the statistical data for the vport
  701. * @vport: Pointer to vport object.
  702. *
  703. * This function resets the statistical data for the vport. This function
  704. * is called with the host_lock held
  705. **/
  706. void
  707. lpfc_vport_reset_stat_data(struct lpfc_vport *vport)
  708. {
  709. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  710. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  711. if (!NLP_CHK_NODE_ACT(ndlp))
  712. continue;
  713. if (ndlp->lat_data)
  714. memset(ndlp->lat_data, 0, LPFC_MAX_BUCKET_COUNT *
  715. sizeof(struct lpfc_scsicmd_bkt));
  716. }
  717. }
  718. /**
  719. * lpfc_alloc_bucket - Allocate data buffer required for statistical data
  720. * @vport: Pointer to vport object.
  721. *
  722. * This function allocates data buffer required for all the FC
  723. * nodes of the vport to collect statistical data.
  724. **/
  725. void
  726. lpfc_alloc_bucket(struct lpfc_vport *vport)
  727. {
  728. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  729. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  730. if (!NLP_CHK_NODE_ACT(ndlp))
  731. continue;
  732. kfree(ndlp->lat_data);
  733. ndlp->lat_data = NULL;
  734. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
  735. ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
  736. sizeof(struct lpfc_scsicmd_bkt),
  737. GFP_ATOMIC);
  738. if (!ndlp->lat_data)
  739. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  740. "0287 lpfc_alloc_bucket failed to "
  741. "allocate statistical data buffer DID "
  742. "0x%x\n", ndlp->nlp_DID);
  743. }
  744. }
  745. }
  746. /**
  747. * lpfc_free_bucket - Free data buffer required for statistical data
  748. * @vport: Pointer to vport object.
  749. *
  750. * Th function frees statistical data buffer of all the FC
  751. * nodes of the vport.
  752. **/
  753. void
  754. lpfc_free_bucket(struct lpfc_vport *vport)
  755. {
  756. struct lpfc_nodelist *ndlp = NULL, *next_ndlp = NULL;
  757. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  758. if (!NLP_CHK_NODE_ACT(ndlp))
  759. continue;
  760. kfree(ndlp->lat_data);
  761. ndlp->lat_data = NULL;
  762. }
  763. }