fc_npiv.c 3.7 KB

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