fc_npiv.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright(c) 2009 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. /*
  20. * NPIV VN_Port helper functions for libfc
  21. */
  22. #include <scsi/libfc.h>
  23. /**
  24. * fc_vport_create() - Create a new NPIV vport instance
  25. * @vport: fc_vport structure from scsi_transport_fc
  26. * @privsize: driver private data size to allocate along with the Scsi_Host
  27. */
  28. struct fc_lport *libfc_vport_create(struct fc_vport *vport, int privsize)
  29. {
  30. struct Scsi_Host *shost = vport_to_shost(vport);
  31. struct fc_lport *n_port = shost_priv(shost);
  32. struct fc_lport *vn_port;
  33. vn_port = libfc_host_alloc(shost->hostt, privsize);
  34. if (!vn_port)
  35. goto err_out;
  36. if (fc_exch_mgr_list_clone(n_port, vn_port))
  37. goto err_put;
  38. vn_port->vport = vport;
  39. vport->dd_data = vn_port;
  40. mutex_lock(&n_port->lp_mutex);
  41. list_add_tail(&vn_port->list, &n_port->vports);
  42. mutex_unlock(&n_port->lp_mutex);
  43. return vn_port;
  44. err_put:
  45. scsi_host_put(vn_port->host);
  46. err_out:
  47. return NULL;
  48. }
  49. EXPORT_SYMBOL(libfc_vport_create);
  50. /**
  51. * fc_vport_id_lookup() - find NPIV lport that matches a given fabric ID
  52. * @n_port: Top level N_Port which may have multiple NPIV VN_Ports
  53. * @port_id: Fabric ID to find a match for
  54. *
  55. * Returns: matching lport pointer or NULL if there is no match
  56. */
  57. struct fc_lport *fc_vport_id_lookup(struct fc_lport *n_port, u32 port_id)
  58. {
  59. struct fc_lport *lport = NULL;
  60. struct fc_lport *vn_port;
  61. if (n_port->port_id == port_id)
  62. return n_port;
  63. if (port_id == FC_FID_FLOGI)
  64. return n_port; /* for point-to-point */
  65. mutex_lock(&n_port->lp_mutex);
  66. list_for_each_entry(vn_port, &n_port->vports, list) {
  67. if (vn_port->port_id == port_id) {
  68. lport = vn_port;
  69. break;
  70. }
  71. }
  72. mutex_unlock(&n_port->lp_mutex);
  73. return lport;
  74. }
  75. /*
  76. * When setting the link state of vports during an lport state change, it's
  77. * necessary to hold the lp_mutex of both the N_Port and the VN_Port.
  78. * This tells the lockdep engine to treat the nested locking of the VN_Port
  79. * as a different lock class.
  80. */
  81. enum libfc_lport_mutex_class {
  82. LPORT_MUTEX_NORMAL = 0,
  83. LPORT_MUTEX_VN_PORT = 1,
  84. };
  85. /**
  86. * __fc_vport_setlink() - update link and status on a VN_Port
  87. * @n_port: parent N_Port
  88. * @vn_port: VN_Port to update
  89. *
  90. * Locking: must be called with both the N_Port and VN_Port lp_mutex held
  91. */
  92. static void __fc_vport_setlink(struct fc_lport *n_port,
  93. struct fc_lport *vn_port)
  94. {
  95. struct fc_vport *vport = vn_port->vport;
  96. if (vn_port->state == LPORT_ST_DISABLED)
  97. return;
  98. if (n_port->state == LPORT_ST_READY) {
  99. if (n_port->npiv_enabled) {
  100. fc_vport_set_state(vport, FC_VPORT_INITIALIZING);
  101. __fc_linkup(vn_port);
  102. } else {
  103. fc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  104. __fc_linkdown(vn_port);
  105. }
  106. } else {
  107. fc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  108. __fc_linkdown(vn_port);
  109. }
  110. }
  111. /**
  112. * fc_vport_setlink() - update link and status on a VN_Port
  113. * @vn_port: virtual port to update
  114. */
  115. void fc_vport_setlink(struct fc_lport *vn_port)
  116. {
  117. struct fc_vport *vport = vn_port->vport;
  118. struct Scsi_Host *shost = vport_to_shost(vport);
  119. struct fc_lport *n_port = shost_priv(shost);
  120. mutex_lock(&n_port->lp_mutex);
  121. mutex_lock_nested(&vn_port->lp_mutex, LPORT_MUTEX_VN_PORT);
  122. __fc_vport_setlink(n_port, vn_port);
  123. mutex_unlock(&vn_port->lp_mutex);
  124. mutex_unlock(&n_port->lp_mutex);
  125. }
  126. EXPORT_SYMBOL(fc_vport_setlink);
  127. /**
  128. * fc_vports_linkchange() - change the link state of all vports
  129. * @n_port: Parent N_Port that has changed state
  130. *
  131. * Locking: called with the n_port lp_mutex held
  132. */
  133. void fc_vports_linkchange(struct fc_lport *n_port)
  134. {
  135. struct fc_lport *vn_port;
  136. list_for_each_entry(vn_port, &n_port->vports, list) {
  137. mutex_lock_nested(&vn_port->lp_mutex, LPORT_MUTEX_VN_PORT);
  138. __fc_vport_setlink(n_port, vn_port);
  139. mutex_unlock(&vn_port->lp_mutex);
  140. }
  141. }