| Chris Leech | 174e1eb | 2009-11-03 11:46:14 -0800 | [diff] [blame] | 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 |  | 
 | 32 | struct 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 | 		goto err_out; | 
 | 41 | 	if (fc_exch_mgr_list_clone(n_port, vn_port)) | 
 | 42 | 		goto err_put; | 
 | 43 |  | 
 | 44 | 	vn_port->vport = vport; | 
 | 45 | 	vport->dd_data = vn_port; | 
 | 46 |  | 
 | 47 | 	mutex_lock(&n_port->lp_mutex); | 
 | 48 | 	list_add_tail(&vn_port->list, &n_port->vports); | 
 | 49 | 	mutex_unlock(&n_port->lp_mutex); | 
 | 50 |  | 
 | 51 | 	return vn_port; | 
 | 52 |  | 
 | 53 | err_put: | 
 | 54 | 	scsi_host_put(vn_port->host); | 
 | 55 | err_out: | 
 | 56 | 	return NULL; | 
 | 57 | } | 
 | 58 | EXPORT_SYMBOL(libfc_vport_create); | 
 | 59 |  | 
 | 60 | /** | 
 | 61 |  * fc_vport_id_lookup() - find NPIV lport that matches a given fabric ID | 
 | 62 |  * @n_port: Top level N_Port which may have multiple NPIV VN_Ports | 
 | 63 |  * @port_id: Fabric ID to find a match for | 
 | 64 |  * | 
 | 65 |  * Returns: matching lport pointer or NULL if there is no match | 
 | 66 |  */ | 
 | 67 | struct fc_lport *fc_vport_id_lookup(struct fc_lport *n_port, u32 port_id) | 
 | 68 | { | 
 | 69 | 	struct fc_lport *lport = NULL; | 
 | 70 | 	struct fc_lport *vn_port; | 
 | 71 |  | 
 | 72 | 	if (fc_host_port_id(n_port->host) == port_id) | 
 | 73 | 		return n_port; | 
 | 74 |  | 
 | 75 | 	mutex_lock(&n_port->lp_mutex); | 
 | 76 | 	list_for_each_entry(vn_port, &n_port->vports, list) { | 
 | 77 | 		if (fc_host_port_id(vn_port->host) == port_id) { | 
 | 78 | 			lport = vn_port; | 
 | 79 | 			break; | 
 | 80 | 		} | 
 | 81 | 	} | 
 | 82 | 	mutex_unlock(&n_port->lp_mutex); | 
 | 83 |  | 
 | 84 | 	return lport; | 
 | 85 | } | 
 | 86 |  | 
| Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 87 | /* | 
 | 88 |  * When setting the link state of vports during an lport state change, it's | 
 | 89 |  * necessary to hold the lp_mutex of both the N_Port and the VN_Port. | 
 | 90 |  * This tells the lockdep engine to treat the nested locking of the VN_Port | 
 | 91 |  * as a different lock class. | 
 | 92 |  */ | 
 | 93 | enum libfc_lport_mutex_class { | 
 | 94 | 	LPORT_MUTEX_NORMAL = 0, | 
 | 95 | 	LPORT_MUTEX_VN_PORT = 1, | 
 | 96 | }; | 
 | 97 |  | 
 | 98 | /** | 
 | 99 |  * __fc_vport_setlink() - update link and status on a VN_Port | 
 | 100 |  * @n_port: parent N_Port | 
 | 101 |  * @vn_port: VN_Port to update | 
 | 102 |  * | 
 | 103 |  * Locking: must be called with both the N_Port and VN_Port lp_mutex held | 
 | 104 |  */ | 
 | 105 | static void __fc_vport_setlink(struct fc_lport *n_port, | 
 | 106 | 			       struct fc_lport *vn_port) | 
 | 107 | { | 
 | 108 | 	struct fc_vport *vport = vn_port->vport; | 
 | 109 |  | 
 | 110 | 	if (vn_port->state == LPORT_ST_DISABLED) | 
 | 111 | 		return; | 
 | 112 |  | 
 | 113 | 	if (n_port->state == LPORT_ST_READY) { | 
 | 114 | 		if (n_port->npiv_enabled) { | 
 | 115 | 			fc_vport_set_state(vport, FC_VPORT_INITIALIZING); | 
 | 116 | 			__fc_linkup(vn_port); | 
 | 117 | 		} else { | 
 | 118 | 			fc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP); | 
 | 119 | 			__fc_linkdown(vn_port); | 
 | 120 | 		} | 
 | 121 | 	} else { | 
 | 122 | 		fc_vport_set_state(vport, FC_VPORT_LINKDOWN); | 
 | 123 | 		__fc_linkdown(vn_port); | 
 | 124 | 	} | 
 | 125 | } | 
 | 126 |  | 
 | 127 | /** | 
 | 128 |  * fc_vport_setlink() - update link and status on a VN_Port | 
 | 129 |  * @vn_port: virtual port to update | 
 | 130 |  */ | 
 | 131 | void fc_vport_setlink(struct fc_lport *vn_port) | 
 | 132 | { | 
 | 133 | 	struct fc_vport *vport = vn_port->vport; | 
 | 134 | 	struct Scsi_Host *shost = vport_to_shost(vport); | 
 | 135 | 	struct fc_lport *n_port = shost_priv(shost); | 
 | 136 |  | 
 | 137 | 	mutex_lock(&n_port->lp_mutex); | 
 | 138 | 	mutex_lock_nested(&vn_port->lp_mutex, LPORT_MUTEX_VN_PORT); | 
 | 139 | 	__fc_vport_setlink(n_port, vn_port); | 
 | 140 | 	mutex_unlock(&vn_port->lp_mutex); | 
 | 141 | 	mutex_unlock(&n_port->lp_mutex); | 
 | 142 | } | 
 | 143 | EXPORT_SYMBOL(fc_vport_setlink); | 
 | 144 |  | 
 | 145 | /** | 
 | 146 |  * fc_vports_linkchange() - change the link state of all vports | 
 | 147 |  * @n_port: Parent N_Port that has changed state | 
 | 148 |  * | 
 | 149 |  * Locking: called with the n_port lp_mutex held | 
 | 150 |  */ | 
 | 151 | void fc_vports_linkchange(struct fc_lport *n_port) | 
 | 152 | { | 
 | 153 | 	struct fc_lport *vn_port; | 
 | 154 |  | 
 | 155 | 	list_for_each_entry(vn_port, &n_port->vports, list) { | 
 | 156 | 		mutex_lock_nested(&vn_port->lp_mutex, LPORT_MUTEX_VN_PORT); | 
 | 157 | 		__fc_vport_setlink(n_port, vn_port); | 
 | 158 | 		mutex_unlock(&vn_port->lp_mutex); | 
 | 159 | 	} | 
 | 160 | } | 
 | 161 |  |