fc_npiv.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include <linux/export.h>
  24. /**
  25. * fc_vport_create() - Create a new NPIV vport instance
  26. * @vport: fc_vport structure from scsi_transport_fc
  27. * @privsize: driver private data size to allocate along with the Scsi_Host
  28. */
  29. struct fc_lport *libfc_vport_create(struct fc_vport *vport, int privsize)
  30. {
  31. struct Scsi_Host *shost = vport_to_shost(vport);
  32. struct fc_lport *n_port = shost_priv(shost);
  33. struct fc_lport *vn_port;
  34. vn_port = libfc_host_alloc(shost->hostt, privsize);
  35. if (!vn_port)
  36. return vn_port;
  37. vn_port->vport = vport;
  38. vport->dd_data = vn_port;
  39. mutex_lock(&n_port->lp_mutex);
  40. list_add_tail(&vn_port->list, &n_port->vports);
  41. mutex_unlock(&n_port->lp_mutex);
  42. return vn_port;
  43. }
  44. EXPORT_SYMBOL(libfc_vport_create);
  45. /**
  46. * fc_vport_id_lookup() - find NPIV lport that matches a given fabric ID
  47. * @n_port: Top level N_Port which may have multiple NPIV VN_Ports
  48. * @port_id: Fabric ID to find a match for
  49. *
  50. * Returns: matching lport pointer or NULL if there is no match
  51. */
  52. struct fc_lport *fc_vport_id_lookup(struct fc_lport *n_port, u32 port_id)
  53. {
  54. struct fc_lport *lport = NULL;
  55. struct fc_lport *vn_port;
  56. if (n_port->port_id == port_id)
  57. return n_port;
  58. if (port_id == FC_FID_FLOGI)
  59. return n_port; /* for point-to-point */
  60. mutex_lock(&n_port->lp_mutex);
  61. list_for_each_entry(vn_port, &n_port->vports, list) {
  62. if (vn_port->port_id == port_id) {
  63. lport = vn_port;
  64. break;
  65. }
  66. }
  67. mutex_unlock(&n_port->lp_mutex);
  68. return lport;
  69. }
  70. EXPORT_SYMBOL(fc_vport_id_lookup);
  71. /*
  72. * When setting the link state of vports during an lport state change, it's
  73. * necessary to hold the lp_mutex of both the N_Port and the VN_Port.
  74. * This tells the lockdep engine to treat the nested locking of the VN_Port
  75. * as a different lock class.
  76. */
  77. enum libfc_lport_mutex_class {
  78. LPORT_MUTEX_NORMAL = 0,
  79. LPORT_MUTEX_VN_PORT = 1,
  80. };
  81. /**
  82. * __fc_vport_setlink() - update link and status on a VN_Port
  83. * @n_port: parent N_Port
  84. * @vn_port: VN_Port to update
  85. *
  86. * Locking: must be called with both the N_Port and VN_Port lp_mutex held
  87. */
  88. static void __fc_vport_setlink(struct fc_lport *n_port,
  89. struct fc_lport *vn_port)
  90. {
  91. struct fc_vport *vport = vn_port->vport;
  92. if (vn_port->state == LPORT_ST_DISABLED)
  93. return;
  94. if (n_port->state == LPORT_ST_READY) {
  95. if (n_port->npiv_enabled) {
  96. fc_vport_set_state(vport, FC_VPORT_INITIALIZING);
  97. __fc_linkup(vn_port);
  98. } else {
  99. fc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  100. __fc_linkdown(vn_port);
  101. }
  102. } else {
  103. fc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  104. __fc_linkdown(vn_port);
  105. }
  106. }
  107. /**
  108. * fc_vport_setlink() - update link and status on a VN_Port
  109. * @vn_port: virtual port to update
  110. */
  111. void fc_vport_setlink(struct fc_lport *vn_port)
  112. {
  113. struct fc_vport *vport = vn_port->vport;
  114. struct Scsi_Host *shost = vport_to_shost(vport);
  115. struct fc_lport *n_port = shost_priv(shost);
  116. mutex_lock(&n_port->lp_mutex);
  117. mutex_lock_nested(&vn_port->lp_mutex, LPORT_MUTEX_VN_PORT);
  118. __fc_vport_setlink(n_port, vn_port);
  119. mutex_unlock(&vn_port->lp_mutex);
  120. mutex_unlock(&n_port->lp_mutex);
  121. }
  122. EXPORT_SYMBOL(fc_vport_setlink);
  123. /**
  124. * fc_vports_linkchange() - change the link state of all vports
  125. * @n_port: Parent N_Port that has changed state
  126. *
  127. * Locking: called with the n_port lp_mutex held
  128. */
  129. void fc_vports_linkchange(struct fc_lport *n_port)
  130. {
  131. struct fc_lport *vn_port;
  132. list_for_each_entry(vn_port, &n_port->vports, list) {
  133. mutex_lock_nested(&vn_port->lp_mutex, LPORT_MUTEX_VN_PORT);
  134. __fc_vport_setlink(n_port, vn_port);
  135. mutex_unlock(&vn_port->lp_mutex);
  136. }
  137. }