lpfc_vport.c 24 KB

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