Linux Audio

Check our new training course

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