lpfc_vport.c 24 KB

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