lpfc_vport.c 23 KB

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