lpfc_vport.c 24 KB

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