blob: 56823fd1fb8418811da8fc4418ec271b846b65da [file] [log] [blame]
James Smart9ef3e4a2007-05-24 19:04:44 -04001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * FiberChannel transport specific attributes exported to sysfs.
3 *
4 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * ========
21 *
James Smarta53eb5e2007-04-27 12:41:09 -040022 * Copyright (C) 2004-2007 James Smart, Emulex Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * Rewrite for host, target, device, and remote port attributes,
24 * statistics, and service functions...
James Smart9ef3e4a2007-05-24 19:04:44 -040025 * Add vports, etc
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28#include <linux/module.h>
29#include <linux/init.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
32#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_fc.h>
James Smartc829c392006-03-13 08:28:57 -050034#include <scsi/scsi_cmnd.h>
James Smart84314fd2006-08-18 17:30:09 -040035#include <linux/netlink.h>
36#include <net/netlink.h>
37#include <scsi/scsi_netlink_fc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "scsi_priv.h"
FUJITA Tomonori75252362007-09-01 02:02:27 +090039#include "scsi_transport_fc_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
James Smartaedf3492006-04-10 10:14:05 -040041static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
James Smart9ef3e4a2007-05-24 19:04:44 -040042static void fc_vport_sched_delete(struct work_struct *work);
James Smartaedf3492006-04-10 10:14:05 -040043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
James Smarta53eb5e2007-04-27 12:41:09 -040045 * This is a temporary carrier for creating a vport. It will eventually
46 * be replaced by a real message definition for sgio or netlink.
47 *
48 * fc_vport_identifiers: This set of data contains all elements
49 * to uniquely identify and instantiate a FC virtual port.
50 *
51 * Notes:
52 * symbolic_name: The driver is to append the symbolic_name string data
53 * to the symbolic_node_name data that it generates by default.
54 * the resulting combination should then be registered with the switch.
55 * It is expected that things like Xen may stuff a VM title into
56 * this field.
57 */
58struct fc_vport_identifiers {
59 u64 node_name;
60 u64 port_name;
61 u32 roles;
62 bool disable;
63 enum fc_port_type vport_type; /* only FC_PORTTYPE_NPIV allowed */
64 char symbolic_name[FC_VPORT_SYMBOLIC_NAMELEN];
65};
66
67static int fc_vport_create(struct Scsi_Host *shost, int channel,
68 struct device *pdev, struct fc_vport_identifiers *ids,
69 struct fc_vport **vport);
70
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * Redefine so that we can have same named attributes in the
73 * sdev/starget/host objects.
74 */
Tony Jonesee959b02008-02-22 00:13:36 +010075#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
76struct device_attribute device_attr_##_prefix##_##_name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 __ATTR(_name,_mode,_show,_store)
78
79#define fc_enum_name_search(title, table_type, table) \
80static const char *get_fc_##title##_name(enum table_type table_key) \
81{ \
82 int i; \
83 char *name = NULL; \
84 \
Tobias Klauser6391a112006-06-08 22:23:48 -070085 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (table[i].value == table_key) { \
87 name = table[i].name; \
88 break; \
89 } \
90 } \
91 return name; \
92}
93
94#define fc_enum_name_match(title, table_type, table) \
95static int get_fc_##title##_match(const char *table_key, \
96 enum table_type *value) \
97{ \
98 int i; \
99 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700100 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (strncmp(table_key, table[i].name, \
102 table[i].matchlen) == 0) { \
103 *value = table[i].value; \
104 return 0; /* success */ \
105 } \
106 } \
107 return 1; /* failure */ \
108}
109
110
111/* Convert fc_port_type values to ascii string name */
112static struct {
113 enum fc_port_type value;
114 char *name;
115} fc_port_type_names[] = {
116 { FC_PORTTYPE_UNKNOWN, "Unknown" },
117 { FC_PORTTYPE_OTHER, "Other" },
118 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
119 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
120 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
121 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
122 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
James Smarta53eb5e2007-04-27 12:41:09 -0400123 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
126#define FC_PORTTYPE_MAX_NAMELEN 50
127
James Smarta53eb5e2007-04-27 12:41:09 -0400128/* Reuse fc_port_type enum function for vport_type */
129#define get_fc_vport_type_name get_fc_port_type_name
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
James Smart84314fd2006-08-18 17:30:09 -0400132/* Convert fc_host_event_code values to ascii string name */
133static const struct {
134 enum fc_host_event_code value;
135 char *name;
136} fc_host_event_code_names[] = {
137 { FCH_EVT_LIP, "lip" },
138 { FCH_EVT_LINKUP, "link_up" },
139 { FCH_EVT_LINKDOWN, "link_down" },
140 { FCH_EVT_LIPRESET, "lip_reset" },
141 { FCH_EVT_RSCN, "rscn" },
142 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
143 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
144 { FCH_EVT_PORT_ONLINE, "port_online" },
145 { FCH_EVT_PORT_OFFLINE, "port_offline" },
146 { FCH_EVT_PORT_FABRIC, "port_fabric" },
147 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
148 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
149};
150fc_enum_name_search(host_event_code, fc_host_event_code,
151 fc_host_event_code_names)
152#define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
153
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Convert fc_port_state values to ascii string name */
156static struct {
157 enum fc_port_state value;
158 char *name;
159} fc_port_state_names[] = {
160 { FC_PORTSTATE_UNKNOWN, "Unknown" },
161 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
162 { FC_PORTSTATE_ONLINE, "Online" },
163 { FC_PORTSTATE_OFFLINE, "Offline" },
164 { FC_PORTSTATE_BLOCKED, "Blocked" },
165 { FC_PORTSTATE_BYPASSED, "Bypassed" },
166 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
167 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
168 { FC_PORTSTATE_ERROR, "Error" },
169 { FC_PORTSTATE_LOOPBACK, "Loopback" },
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500170 { FC_PORTSTATE_DELETED, "Deleted" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
173#define FC_PORTSTATE_MAX_NAMELEN 20
174
175
James Smarta53eb5e2007-04-27 12:41:09 -0400176/* Convert fc_vport_state values to ascii string name */
177static struct {
178 enum fc_vport_state value;
179 char *name;
180} fc_vport_state_names[] = {
181 { FC_VPORT_UNKNOWN, "Unknown" },
182 { FC_VPORT_ACTIVE, "Active" },
183 { FC_VPORT_DISABLED, "Disabled" },
184 { FC_VPORT_LINKDOWN, "Linkdown" },
185 { FC_VPORT_INITIALIZING, "Initializing" },
186 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
187 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
188 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
189 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
190 { FC_VPORT_FAILED, "VPort Failed" },
191};
192fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
193#define FC_VPORTSTATE_MAX_NAMELEN 24
194
195/* Reuse fc_vport_state enum function for vport_last_state */
196#define get_fc_vport_last_state_name get_fc_vport_state_name
197
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* Convert fc_tgtid_binding_type values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100200static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 enum fc_tgtid_binding_type value;
202 char *name;
203 int matchlen;
204} fc_tgtid_binding_type_names[] = {
205 { FC_TGTID_BIND_NONE, "none", 4 },
206 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
207 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
208 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
209};
210fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
211 fc_tgtid_binding_type_names)
212fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
213 fc_tgtid_binding_type_names)
214#define FC_BINDTYPE_MAX_NAMELEN 30
215
216
217#define fc_bitfield_name_search(title, table) \
218static ssize_t \
219get_fc_##title##_names(u32 table_key, char *buf) \
220{ \
221 char *prefix = ""; \
222 ssize_t len = 0; \
223 int i; \
224 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700225 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (table[i].value & table_key) { \
227 len += sprintf(buf + len, "%s%s", \
228 prefix, table[i].name); \
229 prefix = ", "; \
230 } \
231 } \
232 len += sprintf(buf + len, "\n"); \
233 return len; \
234}
235
236
237/* Convert FC_COS bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100238static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 u32 value;
240 char *name;
241} fc_cos_names[] = {
242 { FC_COS_CLASS1, "Class 1" },
243 { FC_COS_CLASS2, "Class 2" },
244 { FC_COS_CLASS3, "Class 3" },
245 { FC_COS_CLASS4, "Class 4" },
246 { FC_COS_CLASS6, "Class 6" },
247};
248fc_bitfield_name_search(cos, fc_cos_names)
249
250
251/* Convert FC_PORTSPEED bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100252static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 u32 value;
254 char *name;
255} fc_port_speed_names[] = {
256 { FC_PORTSPEED_1GBIT, "1 Gbit" },
257 { FC_PORTSPEED_2GBIT, "2 Gbit" },
258 { FC_PORTSPEED_4GBIT, "4 Gbit" },
259 { FC_PORTSPEED_10GBIT, "10 Gbit" },
James Smartc3d23502007-03-12 14:16:35 -0500260 { FC_PORTSPEED_8GBIT, "8 Gbit" },
261 { FC_PORTSPEED_16GBIT, "16 Gbit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
263};
264fc_bitfield_name_search(port_speed, fc_port_speed_names)
265
266
267static int
268show_fc_fc4s (char *buf, u8 *fc4_list)
269{
270 int i, len=0;
271
272 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
273 len += sprintf(buf + len , "0x%02x ", *fc4_list);
274 len += sprintf(buf + len, "\n");
275 return len;
276}
277
278
James Smarta53eb5e2007-04-27 12:41:09 -0400279/* Convert FC_PORT_ROLE bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100280static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 u32 value;
282 char *name;
James Smarta53eb5e2007-04-27 12:41:09 -0400283} fc_port_role_names[] = {
284 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
285 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
286 { FC_PORT_ROLE_IP_PORT, "IP Port" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287};
James Smarta53eb5e2007-04-27 12:41:09 -0400288fc_bitfield_name_search(port_roles, fc_port_role_names)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290/*
291 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
292 */
293#define FC_WELLKNOWN_PORTID_MASK 0xfffff0
294#define FC_WELLKNOWN_ROLE_MASK 0x00000f
295#define FC_FPORT_PORTID 0x00000e
296#define FC_FABCTLR_PORTID 0x00000d
297#define FC_DIRSRVR_PORTID 0x00000c
298#define FC_TIMESRVR_PORTID 0x00000b
299#define FC_MGMTSRVR_PORTID 0x00000a
300
301
David Howellsc4028952006-11-22 14:57:56 +0000302static void fc_timeout_deleted_rport(struct work_struct *work);
303static void fc_timeout_fail_rport_io(struct work_struct *work);
304static void fc_scsi_scan_rport(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306/*
307 * Attribute counts pre object type...
308 * Increase these values if you add attributes
309 */
310#define FC_STARGET_NUM_ATTRS 3
James Smart0f29b962006-08-18 17:33:29 -0400311#define FC_RPORT_NUM_ATTRS 10
James Smarta53eb5e2007-04-27 12:41:09 -0400312#define FC_VPORT_NUM_ATTRS 9
313#define FC_HOST_NUM_ATTRS 21
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315struct fc_internal {
316 struct scsi_transport_template t;
317 struct fc_function_template *f;
318
319 /*
320 * For attributes : each object has :
321 * An array of the actual attributes structures
322 * An array of null-terminated pointers to the attribute
323 * structures - used for mid-layer interaction.
324 *
325 * The attribute containers for the starget and host are are
326 * part of the midlayer. As the remote port is specific to the
327 * fc transport, we must provide the attribute container.
328 */
Tony Jonesee959b02008-02-22 00:13:36 +0100329 struct device_attribute private_starget_attrs[
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 FC_STARGET_NUM_ATTRS];
Tony Jonesee959b02008-02-22 00:13:36 +0100331 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Tony Jonesee959b02008-02-22 00:13:36 +0100333 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
334 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 struct transport_container rport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100337 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
338 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
James Smarta53eb5e2007-04-27 12:41:09 -0400339
340 struct transport_container vport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100341 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
342 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343};
344
345#define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
346
James Bottomleyd0a7e572005-08-14 17:09:01 -0500347static int fc_target_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100348 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct scsi_target *starget = to_scsi_target(dev);
351 struct fc_rport *rport = starget_to_rport(starget);
352
353 /*
354 * if parent is remote port, use values from remote port.
355 * Otherwise, this host uses the fc_transport, but not the
356 * remote port interface. As such, initialize to known non-values.
357 */
358 if (rport) {
359 fc_starget_node_name(starget) = rport->node_name;
360 fc_starget_port_name(starget) = rport->port_name;
361 fc_starget_port_id(starget) = rport->port_id;
362 } else {
363 fc_starget_node_name(starget) = -1;
364 fc_starget_port_name(starget) = -1;
365 fc_starget_port_id(starget) = -1;
366 }
367
368 return 0;
369}
370
371static DECLARE_TRANSPORT_CLASS(fc_transport_class,
372 "fc_transport",
373 fc_target_setup,
374 NULL,
375 NULL);
376
James Bottomleyd0a7e572005-08-14 17:09:01 -0500377static int fc_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100378 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct Scsi_Host *shost = dev_to_shost(dev);
James Smartaedf3492006-04-10 10:14:05 -0400381 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
James Smart9ef3e4a2007-05-24 19:04:44 -0400383 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * Set default values easily detected by the midlayer as
385 * failure cases. The scsi lldd is responsible for initializing
386 * all transport attributes to valid values per host.
387 */
James Smartaedf3492006-04-10 10:14:05 -0400388 fc_host->node_name = -1;
389 fc_host->port_name = -1;
390 fc_host->permanent_port_name = -1;
391 fc_host->supported_classes = FC_COS_UNSPECIFIED;
392 memset(fc_host->supported_fc4s, 0,
393 sizeof(fc_host->supported_fc4s));
James Smartaedf3492006-04-10 10:14:05 -0400394 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
395 fc_host->maxframe_size = -1;
James Smarta53eb5e2007-04-27 12:41:09 -0400396 fc_host->max_npiv_vports = 0;
James Smartaedf3492006-04-10 10:14:05 -0400397 memset(fc_host->serial_number, 0,
398 sizeof(fc_host->serial_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
James Smartaedf3492006-04-10 10:14:05 -0400400 fc_host->port_id = -1;
401 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
402 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
403 memset(fc_host->active_fc4s, 0,
404 sizeof(fc_host->active_fc4s));
405 fc_host->speed = FC_PORTSPEED_UNKNOWN;
406 fc_host->fabric_name = -1;
James Smartb8d08212006-08-17 08:00:43 -0400407 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
408 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
James Smartaedf3492006-04-10 10:14:05 -0400410 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
James Smartaedf3492006-04-10 10:14:05 -0400412 INIT_LIST_HEAD(&fc_host->rports);
413 INIT_LIST_HEAD(&fc_host->rport_bindings);
James Smarta53eb5e2007-04-27 12:41:09 -0400414 INIT_LIST_HEAD(&fc_host->vports);
James Smartaedf3492006-04-10 10:14:05 -0400415 fc_host->next_rport_number = 0;
416 fc_host->next_target_id = 0;
James Smarta53eb5e2007-04-27 12:41:09 -0400417 fc_host->next_vport_number = 0;
418 fc_host->npiv_vports_inuse = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Kay Sieversaab0de22008-05-02 06:02:41 +0200420 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
421 "fc_wq_%d", shost->host_no);
James Smartaedf3492006-04-10 10:14:05 -0400422 fc_host->work_q = create_singlethread_workqueue(
423 fc_host->work_q_name);
424 if (!fc_host->work_q)
425 return -ENOMEM;
426
Kay Sieversaab0de22008-05-02 06:02:41 +0200427 snprintf(fc_host->devloss_work_q_name,
428 sizeof(fc_host->devloss_work_q_name),
429 "fc_dl_%d", shost->host_no);
James Smartaedf3492006-04-10 10:14:05 -0400430 fc_host->devloss_work_q = create_singlethread_workqueue(
431 fc_host->devloss_work_q_name);
432 if (!fc_host->devloss_work_q) {
433 destroy_workqueue(fc_host->work_q);
434 fc_host->work_q = NULL;
435 return -ENOMEM;
436 }
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439}
440
441static DECLARE_TRANSPORT_CLASS(fc_host_class,
442 "fc_host",
443 fc_host_setup,
444 NULL,
445 NULL);
446
447/*
448 * Setup and Remove actions for remote ports are handled
449 * in the service functions below.
450 */
451static DECLARE_TRANSPORT_CLASS(fc_rport_class,
452 "fc_remote_ports",
453 NULL,
454 NULL,
455 NULL);
456
457/*
James Smarta53eb5e2007-04-27 12:41:09 -0400458 * Setup and Remove actions for virtual ports are handled
459 * in the service functions below.
460 */
461static DECLARE_TRANSPORT_CLASS(fc_vport_class,
462 "fc_vports",
463 NULL,
464 NULL,
465 NULL);
466
467/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * Module Parameters
469 */
470
471/*
472 * dev_loss_tmo: the default number of seconds that the FC transport
473 * should insulate the loss of a remote port.
474 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
475 */
James Smart1c9e16e2006-05-16 16:13:36 -0400476static unsigned int fc_dev_loss_tmo = 60; /* seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Masatake YAMATO10f4b892007-09-19 22:59:16 +0900478module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479MODULE_PARM_DESC(dev_loss_tmo,
480 "Maximum number of seconds that the FC transport should"
481 " insulate the loss of a remote port. Once this value is"
482 " exceeded, the scsi target is removed. Value should be"
483 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
484
Rob Landleyeb448202007-11-03 13:30:39 -0500485/*
James Smart84314fd2006-08-18 17:30:09 -0400486 * Netlink Infrastructure
Rob Landleyeb448202007-11-03 13:30:39 -0500487 */
James Smart84314fd2006-08-18 17:30:09 -0400488
489static atomic_t fc_event_seq;
490
491/**
492 * fc_get_event_number - Obtain the next sequential FC event number
493 *
494 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -0500495 * We could have inlined this, but it would have required fc_event_seq to
James Smart84314fd2006-08-18 17:30:09 -0400496 * be exposed. For now, live with the subroutine call.
497 * Atomic used to avoid lock/unlock...
Rob Landleyeb448202007-11-03 13:30:39 -0500498 */
James Smart84314fd2006-08-18 17:30:09 -0400499u32
500fc_get_event_number(void)
501{
502 return atomic_add_return(1, &fc_event_seq);
503}
504EXPORT_SYMBOL(fc_get_event_number);
505
506
507/**
508 * fc_host_post_event - called to post an even on an fc_host.
James Smart84314fd2006-08-18 17:30:09 -0400509 * @shost: host the event occurred on
510 * @event_number: fc event number obtained from get_fc_event_number()
511 * @event_code: fc_host event being posted
512 * @event_data: 32bits of data for the event being posted
513 *
514 * Notes:
515 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500516 */
James Smart84314fd2006-08-18 17:30:09 -0400517void
518fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
519 enum fc_host_event_code event_code, u32 event_data)
520{
521 struct sk_buff *skb;
522 struct nlmsghdr *nlh;
523 struct fc_nl_event *event;
524 const char *name;
525 u32 len, skblen;
526 int err;
527
528 if (!scsi_nl_sock) {
529 err = -ENOENT;
530 goto send_fail;
531 }
532
533 len = FC_NL_MSGALIGN(sizeof(*event));
534 skblen = NLMSG_SPACE(len);
535
536 skb = alloc_skb(skblen, GFP_KERNEL);
537 if (!skb) {
538 err = -ENOBUFS;
539 goto send_fail;
540 }
541
542 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
543 skblen - sizeof(*nlh), 0);
544 if (!nlh) {
545 err = -ENOBUFS;
546 goto send_fail_skb;
547 }
548 event = NLMSG_DATA(nlh);
549
550 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
551 FC_NL_ASYNC_EVENT, len);
552 event->seconds = get_seconds();
553 event->vendor_id = 0;
554 event->host_no = shost->host_no;
555 event->event_datalen = sizeof(u32); /* bytes */
556 event->event_num = event_number;
557 event->event_code = event_code;
558 event->event_data = event_data;
559
James Bottomley1b73c4b2006-09-23 22:07:20 -0500560 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
561 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400562 if (err && (err != -ESRCH)) /* filter no recipient errors */
563 /* nlmsg_multicast already kfree_skb'd */
564 goto send_fail;
565
566 return;
567
568send_fail_skb:
569 kfree_skb(skb);
570send_fail:
571 name = get_fc_host_event_code_name(event_code);
572 printk(KERN_WARNING
573 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700574 __func__, shost->host_no,
James Smart84314fd2006-08-18 17:30:09 -0400575 (name) ? name : "<unknown>", event_data, err);
576 return;
577}
578EXPORT_SYMBOL(fc_host_post_event);
579
580
581/**
Rob Landleyeb448202007-11-03 13:30:39 -0500582 * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
James Smart84314fd2006-08-18 17:30:09 -0400583 * @shost: host the event occurred on
584 * @event_number: fc event number obtained from get_fc_event_number()
585 * @data_len: amount, in bytes, of vendor unique data
586 * @data_buf: pointer to vendor unique data
Rob Landleyeb448202007-11-03 13:30:39 -0500587 * @vendor_id: Vendor id
James Smart84314fd2006-08-18 17:30:09 -0400588 *
589 * Notes:
590 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500591 */
James Smart84314fd2006-08-18 17:30:09 -0400592void
593fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
James Smartf14e2e22006-08-22 09:55:23 -0400594 u32 data_len, char * data_buf, u64 vendor_id)
James Smart84314fd2006-08-18 17:30:09 -0400595{
596 struct sk_buff *skb;
597 struct nlmsghdr *nlh;
598 struct fc_nl_event *event;
599 u32 len, skblen;
600 int err;
601
602 if (!scsi_nl_sock) {
603 err = -ENOENT;
604 goto send_vendor_fail;
605 }
606
607 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
608 skblen = NLMSG_SPACE(len);
609
610 skb = alloc_skb(skblen, GFP_KERNEL);
611 if (!skb) {
612 err = -ENOBUFS;
613 goto send_vendor_fail;
614 }
615
616 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
617 skblen - sizeof(*nlh), 0);
618 if (!nlh) {
619 err = -ENOBUFS;
620 goto send_vendor_fail_skb;
621 }
622 event = NLMSG_DATA(nlh);
623
624 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
625 FC_NL_ASYNC_EVENT, len);
626 event->seconds = get_seconds();
627 event->vendor_id = vendor_id;
628 event->host_no = shost->host_no;
629 event->event_datalen = data_len; /* bytes */
630 event->event_num = event_number;
631 event->event_code = FCH_EVT_VENDOR_UNIQUE;
632 memcpy(&event->event_data, data_buf, data_len);
633
James Bottomley1b73c4b2006-09-23 22:07:20 -0500634 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
635 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400636 if (err && (err != -ESRCH)) /* filter no recipient errors */
637 /* nlmsg_multicast already kfree_skb'd */
638 goto send_vendor_fail;
639
640 return;
641
642send_vendor_fail_skb:
643 kfree_skb(skb);
644send_vendor_fail:
645 printk(KERN_WARNING
646 "%s: Dropped Event : host %d vendor_unique - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700647 __func__, shost->host_no, err);
James Smart84314fd2006-08-18 17:30:09 -0400648 return;
649}
650EXPORT_SYMBOL(fc_host_post_vendor_event);
651
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654static __init int fc_transport_init(void)
655{
James Smart84314fd2006-08-18 17:30:09 -0400656 int error;
657
658 atomic_set(&fc_event_seq, 0);
659
660 error = transport_class_register(&fc_host_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (error)
662 return error;
James Smarta53eb5e2007-04-27 12:41:09 -0400663 error = transport_class_register(&fc_vport_class);
664 if (error)
665 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 error = transport_class_register(&fc_rport_class);
667 if (error)
668 return error;
669 return transport_class_register(&fc_transport_class);
670}
671
672static void __exit fc_transport_exit(void)
673{
674 transport_class_unregister(&fc_transport_class);
675 transport_class_unregister(&fc_rport_class);
676 transport_class_unregister(&fc_host_class);
James Smarta53eb5e2007-04-27 12:41:09 -0400677 transport_class_unregister(&fc_vport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680/*
681 * FC Remote Port Attribute Management
682 */
683
684#define fc_rport_show_function(field, format_string, sz, cast) \
685static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100686show_fc_rport_##field (struct device *dev, \
687 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100689 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct Scsi_Host *shost = rport_to_shost(rport); \
691 struct fc_internal *i = to_fc_internal(shost->transportt); \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400692 if ((i->f->get_rport_##field) && \
693 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500694 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400695 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 i->f->get_rport_##field(rport); \
697 return snprintf(buf, sz, format_string, cast rport->field); \
698}
699
700#define fc_rport_store_function(field) \
701static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100702store_fc_rport_##field(struct device *dev, \
703 struct device_attribute *attr, \
704 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{ \
706 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100707 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct Scsi_Host *shost = rport_to_shost(rport); \
709 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -0400710 char *cp; \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400711 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500712 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400713 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
714 return -EBUSY; \
James Smart0f29b962006-08-18 17:33:29 -0400715 val = simple_strtoul(buf, &cp, 0); \
716 if (*cp && (*cp != '\n')) \
717 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 i->f->set_rport_##field(rport, val); \
719 return count; \
720}
721
722#define fc_rport_rd_attr(field, format_string, sz) \
723 fc_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100724static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 show_fc_rport_##field, NULL)
726
727#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
728 fc_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100729static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 show_fc_rport_##field, NULL)
731
732#define fc_rport_rw_attr(field, format_string, sz) \
733 fc_rport_show_function(field, format_string, sz, ) \
734 fc_rport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100735static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 show_fc_rport_##field, \
737 store_fc_rport_##field)
738
739
740#define fc_private_rport_show_function(field, format_string, sz, cast) \
741static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100742show_fc_rport_##field (struct device *dev, \
743 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100745 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return snprintf(buf, sz, format_string, cast rport->field); \
747}
748
749#define fc_private_rport_rd_attr(field, format_string, sz) \
750 fc_private_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100751static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 show_fc_rport_##field, NULL)
753
754#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
755 fc_private_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100756static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 show_fc_rport_##field, NULL)
758
759
760#define fc_private_rport_rd_enum_attr(title, maxlen) \
761static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100762show_fc_rport_##title (struct device *dev, \
763 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100765 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 const char *name; \
767 name = get_fc_##title##_name(rport->title); \
768 if (!name) \
769 return -EINVAL; \
770 return snprintf(buf, maxlen, "%s\n", name); \
771} \
Tony Jonesee959b02008-02-22 00:13:36 +0100772static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 show_fc_rport_##title, NULL)
774
775
776#define SETUP_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100777 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
779 i->private_rport_attrs[count].store = NULL; \
780 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
781 if (i->f->show_rport_##field) \
782 count++
783
784#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100785 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
787 i->private_rport_attrs[count].store = NULL; \
788 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
789 count++
790
791#define SETUP_RPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100792 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!i->f->set_rport_##field) { \
794 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
795 i->private_rport_attrs[count].store = NULL; \
796 } \
797 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
798 if (i->f->show_rport_##field) \
799 count++
800
James Smart0f29b962006-08-18 17:33:29 -0400801#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
802{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100803 i->private_rport_attrs[count] = device_attr_rport_##field; \
James Smart0f29b962006-08-18 17:33:29 -0400804 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
805 count++; \
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809/* The FC Transport Remote Port Attributes: */
810
811/* Fixed Remote Port Attributes */
812
813fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
814
815static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100816show_fc_rport_supported_classes (struct device *dev,
817 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Tony Jonesee959b02008-02-22 00:13:36 +0100819 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (rport->supported_classes == FC_COS_UNSPECIFIED)
821 return snprintf(buf, 20, "unspecified\n");
822 return get_fc_cos_names(rport->supported_classes, buf);
823}
Tony Jonesee959b02008-02-22 00:13:36 +0100824static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 show_fc_rport_supported_classes, NULL);
826
827/* Dynamic Remote Port Attributes */
828
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400829/*
830 * dev_loss_tmo attribute
831 */
832fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
833static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100834store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
835 const char *buf, size_t count)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400836{
837 int val;
Tony Jonesee959b02008-02-22 00:13:36 +0100838 struct fc_rport *rport = transport_class_to_rport(dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400839 struct Scsi_Host *shost = rport_to_shost(rport);
840 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart0f29b962006-08-18 17:33:29 -0400841 char *cp;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400842 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500843 (rport->port_state == FC_PORTSTATE_DELETED) ||
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400844 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
845 return -EBUSY;
James Smart0f29b962006-08-18 17:33:29 -0400846 val = simple_strtoul(buf, &cp, 0);
847 if ((*cp && (*cp != '\n')) ||
848 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400849 return -EINVAL;
850 i->f->set_rport_dev_loss_tmo(rport, val);
851 return count;
852}
Tony Jonesee959b02008-02-22 00:13:36 +0100853static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400854 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856
857/* Private Remote Port Attributes */
858
859fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
860fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
861fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
862
863static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100864show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
865 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Tony Jonesee959b02008-02-22 00:13:36 +0100867 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 /* identify any roles that are port_id specific */
870 if ((rport->port_id != -1) &&
871 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
872 FC_WELLKNOWN_PORTID_MASK) {
873 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
874 case FC_FPORT_PORTID:
875 return snprintf(buf, 30, "Fabric Port\n");
876 case FC_FABCTLR_PORTID:
877 return snprintf(buf, 30, "Fabric Controller\n");
878 case FC_DIRSRVR_PORTID:
879 return snprintf(buf, 30, "Directory Server\n");
880 case FC_TIMESRVR_PORTID:
881 return snprintf(buf, 30, "Time Server\n");
882 case FC_MGMTSRVR_PORTID:
883 return snprintf(buf, 30, "Management Server\n");
884 default:
885 return snprintf(buf, 30, "Unknown Fabric Entity\n");
886 }
887 } else {
James Smarta53eb5e2007-04-27 12:41:09 -0400888 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return snprintf(buf, 20, "unknown\n");
James Smarta53eb5e2007-04-27 12:41:09 -0400890 return get_fc_port_roles_names(rport->roles, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892}
Tony Jonesee959b02008-02-22 00:13:36 +0100893static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 show_fc_rport_roles, NULL);
895
896fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
897fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
898
James Smart0f29b962006-08-18 17:33:29 -0400899/*
900 * fast_io_fail_tmo attribute
901 */
902static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100903show_fc_rport_fast_io_fail_tmo (struct device *dev,
904 struct device_attribute *attr, char *buf)
James Smart0f29b962006-08-18 17:33:29 -0400905{
Tony Jonesee959b02008-02-22 00:13:36 +0100906 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400907
908 if (rport->fast_io_fail_tmo == -1)
909 return snprintf(buf, 5, "off\n");
910 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
911}
912
913static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100914store_fc_rport_fast_io_fail_tmo(struct device *dev,
915 struct device_attribute *attr, const char *buf,
916 size_t count)
James Smart0f29b962006-08-18 17:33:29 -0400917{
918 int val;
919 char *cp;
Tony Jonesee959b02008-02-22 00:13:36 +0100920 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400921
922 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
923 (rport->port_state == FC_PORTSTATE_DELETED) ||
924 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
925 return -EBUSY;
926 if (strncmp(buf, "off", 3) == 0)
927 rport->fast_io_fail_tmo = -1;
928 else {
929 val = simple_strtoul(buf, &cp, 0);
930 if ((*cp && (*cp != '\n')) ||
931 (val < 0) || (val >= rport->dev_loss_tmo))
932 return -EINVAL;
933 rport->fast_io_fail_tmo = val;
934 }
935 return count;
936}
Tony Jonesee959b02008-02-22 00:13:36 +0100937static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
James Smart0f29b962006-08-18 17:33:29 -0400938 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940
941/*
942 * FC SCSI Target Attribute Management
943 */
944
945/*
946 * Note: in the target show function we recognize when the remote
James Smarta53eb5e2007-04-27 12:41:09 -0400947 * port is in the heirarchy and do not allow the driver to get
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * involved in sysfs functions. The driver only gets involved if
949 * it's the "old" style that doesn't use rports.
950 */
951#define fc_starget_show_function(field, format_string, sz, cast) \
952static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100953show_fc_starget_##field (struct device *dev, \
954 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100956 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
958 struct fc_internal *i = to_fc_internal(shost->transportt); \
959 struct fc_rport *rport = starget_to_rport(starget); \
960 if (rport) \
961 fc_starget_##field(starget) = rport->field; \
962 else if (i->f->get_starget_##field) \
963 i->f->get_starget_##field(starget); \
964 return snprintf(buf, sz, format_string, \
965 cast fc_starget_##field(starget)); \
966}
967
968#define fc_starget_rd_attr(field, format_string, sz) \
969 fc_starget_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100970static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 show_fc_starget_##field, NULL)
972
973#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
974 fc_starget_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100975static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 show_fc_starget_##field, NULL)
977
978#define SETUP_STARGET_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100979 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
981 i->private_starget_attrs[count].store = NULL; \
982 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
983 if (i->f->show_starget_##field) \
984 count++
985
986#define SETUP_STARGET_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100987 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (!i->f->set_starget_##field) { \
989 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
990 i->private_starget_attrs[count].store = NULL; \
991 } \
992 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
993 if (i->f->show_starget_##field) \
994 count++
995
996/* The FC Transport SCSI Target Attributes: */
997fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
998fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
999fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1000
1001
1002/*
James Smarta53eb5e2007-04-27 12:41:09 -04001003 * FC Virtual Port Attribute Management
1004 */
1005
1006#define fc_vport_show_function(field, format_string, sz, cast) \
1007static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001008show_fc_vport_##field (struct device *dev, \
1009 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001010{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001011 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001012 struct Scsi_Host *shost = vport_to_shost(vport); \
1013 struct fc_internal *i = to_fc_internal(shost->transportt); \
1014 if ((i->f->get_vport_##field) && \
1015 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1016 i->f->get_vport_##field(vport); \
1017 return snprintf(buf, sz, format_string, cast vport->field); \
1018}
1019
1020#define fc_vport_store_function(field) \
1021static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001022store_fc_vport_##field(struct device *dev, \
1023 struct device_attribute *attr, \
1024 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001025{ \
1026 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001027 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001028 struct Scsi_Host *shost = vport_to_shost(vport); \
1029 struct fc_internal *i = to_fc_internal(shost->transportt); \
1030 char *cp; \
1031 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1032 return -EBUSY; \
1033 val = simple_strtoul(buf, &cp, 0); \
1034 if (*cp && (*cp != '\n')) \
1035 return -EINVAL; \
1036 i->f->set_vport_##field(vport, val); \
1037 return count; \
1038}
1039
1040#define fc_vport_store_str_function(field, slen) \
1041static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001042store_fc_vport_##field(struct device *dev, \
1043 struct device_attribute *attr, \
1044 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001045{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001046 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001047 struct Scsi_Host *shost = vport_to_shost(vport); \
1048 struct fc_internal *i = to_fc_internal(shost->transportt); \
1049 unsigned int cnt=count; \
1050 \
1051 /* count may include a LF at end of string */ \
1052 if (buf[cnt-1] == '\n') \
1053 cnt--; \
1054 if (cnt > ((slen) - 1)) \
1055 return -EINVAL; \
1056 memcpy(vport->field, buf, cnt); \
1057 i->f->set_vport_##field(vport); \
1058 return count; \
1059}
1060
1061#define fc_vport_rd_attr(field, format_string, sz) \
1062 fc_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001063static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001064 show_fc_vport_##field, NULL)
1065
1066#define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1067 fc_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001068static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001069 show_fc_vport_##field, NULL)
1070
1071#define fc_vport_rw_attr(field, format_string, sz) \
1072 fc_vport_show_function(field, format_string, sz, ) \
1073 fc_vport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001074static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001075 show_fc_vport_##field, \
1076 store_fc_vport_##field)
1077
1078#define fc_private_vport_show_function(field, format_string, sz, cast) \
1079static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001080show_fc_vport_##field (struct device *dev, \
1081 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001082{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001083 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001084 return snprintf(buf, sz, format_string, cast vport->field); \
1085}
1086
1087#define fc_private_vport_store_u32_function(field) \
1088static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001089store_fc_vport_##field(struct device *dev, \
1090 struct device_attribute *attr, \
1091 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001092{ \
1093 u32 val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001094 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001095 char *cp; \
1096 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1097 return -EBUSY; \
1098 val = simple_strtoul(buf, &cp, 0); \
1099 if (*cp && (*cp != '\n')) \
1100 return -EINVAL; \
1101 vport->field = val; \
1102 return count; \
1103}
1104
1105
1106#define fc_private_vport_rd_attr(field, format_string, sz) \
1107 fc_private_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001108static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001109 show_fc_vport_##field, NULL)
1110
1111#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1112 fc_private_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001113static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001114 show_fc_vport_##field, NULL)
1115
1116#define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1117 fc_private_vport_show_function(field, format_string, sz, ) \
1118 fc_private_vport_store_u32_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001119static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001120 show_fc_vport_##field, \
1121 store_fc_vport_##field)
1122
1123
1124#define fc_private_vport_rd_enum_attr(title, maxlen) \
1125static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001126show_fc_vport_##title (struct device *dev, \
1127 struct device_attribute *attr, \
1128 char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001129{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001130 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001131 const char *name; \
1132 name = get_fc_##title##_name(vport->title); \
1133 if (!name) \
1134 return -EINVAL; \
1135 return snprintf(buf, maxlen, "%s\n", name); \
1136} \
Tony Jonesee959b02008-02-22 00:13:36 +01001137static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001138 show_fc_vport_##title, NULL)
1139
1140
1141#define SETUP_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001142 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001143 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1144 i->private_vport_attrs[count].store = NULL; \
1145 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1146 if (i->f->get_##field) \
1147 count++
1148 /* NOTE: Above MACRO differs: checks function not show bit */
1149
1150#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001151 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001152 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1153 i->private_vport_attrs[count].store = NULL; \
1154 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1155 count++
1156
1157#define SETUP_VPORT_ATTRIBUTE_WR(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001158 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001159 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1160 if (i->f->field) \
1161 count++
1162 /* NOTE: Above MACRO differs: checks function */
1163
1164#define SETUP_VPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001165 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001166 if (!i->f->set_vport_##field) { \
1167 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1168 i->private_vport_attrs[count].store = NULL; \
1169 } \
1170 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1171 count++
1172 /* NOTE: Above MACRO differs: does not check show bit */
1173
1174#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1175{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001176 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001177 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1178 count++; \
1179}
1180
1181
1182/* The FC Transport Virtual Port Attributes: */
1183
1184/* Fixed Virtual Port Attributes */
1185
1186/* Dynamic Virtual Port Attributes */
1187
1188/* Private Virtual Port Attributes */
1189
1190fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1191fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1192fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1193fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1194
1195static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001196show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1197 char *buf)
James Smarta53eb5e2007-04-27 12:41:09 -04001198{
Tony Jonesee959b02008-02-22 00:13:36 +01001199 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001200
1201 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1202 return snprintf(buf, 20, "unknown\n");
1203 return get_fc_port_roles_names(vport->roles, buf);
1204}
Tony Jonesee959b02008-02-22 00:13:36 +01001205static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
James Smarta53eb5e2007-04-27 12:41:09 -04001206
1207fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1208
1209fc_private_vport_show_function(symbolic_name, "%s\n",
1210 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1211fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
Tony Jonesee959b02008-02-22 00:13:36 +01001212static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001213 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1214
1215static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001216store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1217 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001218{
Tony Jonesee959b02008-02-22 00:13:36 +01001219 struct fc_vport *vport = transport_class_to_vport(dev);
James Smart9ef3e4a2007-05-24 19:04:44 -04001220 struct Scsi_Host *shost = vport_to_shost(vport);
James Smarta53eb5e2007-04-27 12:41:09 -04001221
James Smart9ef3e4a2007-05-24 19:04:44 -04001222 fc_queue_work(shost, &vport->vport_delete_work);
James Smarta53eb5e2007-04-27 12:41:09 -04001223 return count;
1224}
Tony Jonesee959b02008-02-22 00:13:36 +01001225static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001226 NULL, store_fc_vport_delete);
1227
1228
1229/*
1230 * Enable/Disable vport
1231 * Write "1" to disable, write "0" to enable
1232 */
1233static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001234store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1235 const char *buf,
James Smarta53eb5e2007-04-27 12:41:09 -04001236 size_t count)
1237{
Tony Jonesee959b02008-02-22 00:13:36 +01001238 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001239 struct Scsi_Host *shost = vport_to_shost(vport);
1240 struct fc_internal *i = to_fc_internal(shost->transportt);
1241 int stat;
1242
1243 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1244 return -EBUSY;
1245
1246 if (*buf == '0') {
1247 if (vport->vport_state != FC_VPORT_DISABLED)
1248 return -EALREADY;
1249 } else if (*buf == '1') {
1250 if (vport->vport_state == FC_VPORT_DISABLED)
1251 return -EALREADY;
1252 } else
1253 return -EINVAL;
1254
1255 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1256 return stat ? stat : count;
1257}
Tony Jonesee959b02008-02-22 00:13:36 +01001258static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001259 NULL, store_fc_vport_disable);
1260
1261
1262/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 * Host Attribute Management
1264 */
1265
1266#define fc_host_show_function(field, format_string, sz, cast) \
1267static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001268show_fc_host_##field (struct device *dev, \
1269 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001271 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 struct fc_internal *i = to_fc_internal(shost->transportt); \
1273 if (i->f->get_host_##field) \
1274 i->f->get_host_##field(shost); \
1275 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1276}
1277
1278#define fc_host_store_function(field) \
1279static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001280store_fc_host_##field(struct device *dev, \
1281 struct device_attribute *attr, \
1282 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{ \
1284 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001285 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -04001287 char *cp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 \
James Smart0f29b962006-08-18 17:33:29 -04001289 val = simple_strtoul(buf, &cp, 0); \
1290 if (*cp && (*cp != '\n')) \
1291 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 i->f->set_host_##field(shost, val); \
1293 return count; \
1294}
1295
James Smartb8d08212006-08-17 08:00:43 -04001296#define fc_host_store_str_function(field, slen) \
1297static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001298store_fc_host_##field(struct device *dev, \
1299 struct device_attribute *attr, \
1300 const char *buf, size_t count) \
James Smartb8d08212006-08-17 08:00:43 -04001301{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001302 struct Scsi_Host *shost = transport_class_to_shost(dev); \
James Smartb8d08212006-08-17 08:00:43 -04001303 struct fc_internal *i = to_fc_internal(shost->transportt); \
1304 unsigned int cnt=count; \
1305 \
1306 /* count may include a LF at end of string */ \
1307 if (buf[cnt-1] == '\n') \
1308 cnt--; \
1309 if (cnt > ((slen) - 1)) \
1310 return -EINVAL; \
1311 memcpy(fc_host_##field(shost), buf, cnt); \
1312 i->f->set_host_##field(shost); \
1313 return count; \
1314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316#define fc_host_rd_attr(field, format_string, sz) \
1317 fc_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001318static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 show_fc_host_##field, NULL)
1320
1321#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1322 fc_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001323static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 show_fc_host_##field, NULL)
1325
1326#define fc_host_rw_attr(field, format_string, sz) \
1327 fc_host_show_function(field, format_string, sz, ) \
1328 fc_host_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001329static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 show_fc_host_##field, \
1331 store_fc_host_##field)
1332
1333#define fc_host_rd_enum_attr(title, maxlen) \
1334static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001335show_fc_host_##title (struct device *dev, \
1336 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001338 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct fc_internal *i = to_fc_internal(shost->transportt); \
1340 const char *name; \
1341 if (i->f->get_host_##title) \
1342 i->f->get_host_##title(shost); \
1343 name = get_fc_##title##_name(fc_host_##title(shost)); \
1344 if (!name) \
1345 return -EINVAL; \
1346 return snprintf(buf, maxlen, "%s\n", name); \
1347} \
Tony Jonesee959b02008-02-22 00:13:36 +01001348static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350#define SETUP_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001351 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1353 i->private_host_attrs[count].store = NULL; \
1354 i->host_attrs[count] = &i->private_host_attrs[count]; \
1355 if (i->f->show_host_##field) \
1356 count++
1357
James Smarta53eb5e2007-04-27 12:41:09 -04001358#define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001359 i->private_host_attrs[count] = device_attr_host_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001360 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1361 i->private_host_attrs[count].store = NULL; \
1362 i->host_attrs[count] = &i->private_host_attrs[count]; \
1363 count++
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365#define SETUP_HOST_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001366 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (!i->f->set_host_##field) { \
1368 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1369 i->private_host_attrs[count].store = NULL; \
1370 } \
1371 i->host_attrs[count] = &i->private_host_attrs[count]; \
1372 if (i->f->show_host_##field) \
1373 count++
1374
1375
1376#define fc_private_host_show_function(field, format_string, sz, cast) \
1377static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001378show_fc_host_##field (struct device *dev, \
1379 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001381 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1383}
1384
1385#define fc_private_host_rd_attr(field, format_string, sz) \
1386 fc_private_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001387static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 show_fc_host_##field, NULL)
1389
1390#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1391 fc_private_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001392static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 show_fc_host_##field, NULL)
1394
1395#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001396 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1398 i->private_host_attrs[count].store = NULL; \
1399 i->host_attrs[count] = &i->private_host_attrs[count]; \
1400 count++
1401
1402#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001403{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001404 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 i->host_attrs[count] = &i->private_host_attrs[count]; \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001406 count++; \
1407}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409
1410/* Fixed Host Attributes */
1411
1412static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001413show_fc_host_supported_classes (struct device *dev,
1414 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Tony Jonesee959b02008-02-22 00:13:36 +01001416 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1419 return snprintf(buf, 20, "unspecified\n");
1420
1421 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1422}
Tony Jonesee959b02008-02-22 00:13:36 +01001423static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 show_fc_host_supported_classes, NULL);
1425
1426static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001427show_fc_host_supported_fc4s (struct device *dev,
1428 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Tony Jonesee959b02008-02-22 00:13:36 +01001430 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1432}
Tony Jonesee959b02008-02-22 00:13:36 +01001433static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 show_fc_host_supported_fc4s, NULL);
1435
1436static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001437show_fc_host_supported_speeds (struct device *dev,
1438 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Tony Jonesee959b02008-02-22 00:13:36 +01001440 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1443 return snprintf(buf, 20, "unknown\n");
1444
1445 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1446}
Tony Jonesee959b02008-02-22 00:13:36 +01001447static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 show_fc_host_supported_speeds, NULL);
1449
1450
1451fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1452fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01001453fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1454 unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
James Smarta53eb5e2007-04-27 12:41:09 -04001456fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1458
1459
1460/* Dynamic Host Attributes */
1461
1462static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001463show_fc_host_active_fc4s (struct device *dev,
1464 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Tony Jonesee959b02008-02-22 00:13:36 +01001466 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 struct fc_internal *i = to_fc_internal(shost->transportt);
1468
1469 if (i->f->get_host_active_fc4s)
1470 i->f->get_host_active_fc4s(shost);
1471
1472 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1473}
Tony Jonesee959b02008-02-22 00:13:36 +01001474static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 show_fc_host_active_fc4s, NULL);
1476
1477static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001478show_fc_host_speed (struct device *dev,
1479 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Tony Jonesee959b02008-02-22 00:13:36 +01001481 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct fc_internal *i = to_fc_internal(shost->transportt);
1483
1484 if (i->f->get_host_speed)
1485 i->f->get_host_speed(shost);
1486
1487 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1488 return snprintf(buf, 20, "unknown\n");
1489
1490 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1491}
Tony Jonesee959b02008-02-22 00:13:36 +01001492static FC_DEVICE_ATTR(host, speed, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 show_fc_host_speed, NULL);
1494
1495
1496fc_host_rd_attr(port_id, "0x%06x\n", 20);
1497fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1498fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1499fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
James Smart016131b2006-08-14 08:20:25 -04001500fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
James Smartb8d08212006-08-17 08:00:43 -04001502fc_private_host_show_function(system_hostname, "%s\n",
1503 FC_SYMBOLIC_NAME_SIZE + 1, )
1504fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
Tony Jonesee959b02008-02-22 00:13:36 +01001505static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
James Smartb8d08212006-08-17 08:00:43 -04001506 show_fc_host_system_hostname, store_fc_host_system_hostname);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509/* Private Host Attributes */
1510
1511static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001512show_fc_private_host_tgtid_bind_type(struct device *dev,
1513 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Tony Jonesee959b02008-02-22 00:13:36 +01001515 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 const char *name;
1517
1518 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1519 if (!name)
1520 return -EINVAL;
1521 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1522}
1523
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001524#define get_list_head_entry(pos, head, member) \
1525 pos = list_entry((head)->next, typeof(*pos), member)
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001528store_fc_private_host_tgtid_bind_type(struct device *dev,
1529 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Tony Jonesee959b02008-02-22 00:13:36 +01001531 struct Scsi_Host *shost = transport_class_to_shost(dev);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001532 struct fc_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 enum fc_tgtid_binding_type val;
1534 unsigned long flags;
1535
1536 if (get_fc_tgtid_bind_type_match(buf, &val))
1537 return -EINVAL;
1538
1539 /* if changing bind type, purge all unused consistent bindings */
1540 if (val != fc_host_tgtid_bind_type(shost)) {
1541 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001542 while (!list_empty(&fc_host_rport_bindings(shost))) {
1543 get_list_head_entry(rport,
1544 &fc_host_rport_bindings(shost), peers);
James Smartaedf3492006-04-10 10:14:05 -04001545 list_del(&rport->peers);
1546 rport->port_state = FC_PORTSTATE_DELETED;
1547 fc_queue_work(shost, &rport->rport_delete_work);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 spin_unlock_irqrestore(shost->host_lock, flags);
1550 }
1551
1552 fc_host_tgtid_bind_type(shost) = val;
1553 return count;
1554}
1555
Tony Jonesee959b02008-02-22 00:13:36 +01001556static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 show_fc_private_host_tgtid_bind_type,
1558 store_fc_private_host_tgtid_bind_type);
1559
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001560static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001561store_fc_private_host_issue_lip(struct device *dev,
1562 struct device_attribute *attr, const char *buf, size_t count)
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001563{
Tony Jonesee959b02008-02-22 00:13:36 +01001564 struct Scsi_Host *shost = transport_class_to_shost(dev);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001565 struct fc_internal *i = to_fc_internal(shost->transportt);
1566 int ret;
1567
1568 /* ignore any data value written to the attribute */
1569 if (i->f->issue_fc_host_lip) {
1570 ret = i->f->issue_fc_host_lip(shost);
1571 return ret ? ret: count;
1572 }
1573
1574 return -ENOENT;
1575}
1576
Tony Jonesee959b02008-02-22 00:13:36 +01001577static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001578 store_fc_private_host_issue_lip);
1579
James Smarta53eb5e2007-04-27 12:41:09 -04001580fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1581
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583/*
1584 * Host Statistics Management
1585 */
1586
1587/* Show a given an attribute in the statistics group */
1588static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001589fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Tony Jonesee959b02008-02-22 00:13:36 +01001591 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 struct fc_internal *i = to_fc_internal(shost->transportt);
1593 struct fc_host_statistics *stats;
1594 ssize_t ret = -ENOENT;
1595
1596 if (offset > sizeof(struct fc_host_statistics) ||
1597 offset % sizeof(u64) != 0)
1598 WARN_ON(1);
1599
1600 if (i->f->get_fc_host_stats) {
1601 stats = (i->f->get_fc_host_stats)(shost);
1602 if (stats)
1603 ret = snprintf(buf, 20, "0x%llx\n",
1604 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1605 }
1606 return ret;
1607}
1608
1609
1610/* generate a read-only statistics attribute */
1611#define fc_host_statistic(name) \
Tony Jonesee959b02008-02-22 00:13:36 +01001612static ssize_t show_fcstat_##name(struct device *cd, \
1613 struct device_attribute *attr, \
1614 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{ \
1616 return fc_stat_show(cd, buf, \
1617 offsetof(struct fc_host_statistics, name)); \
1618} \
Tony Jonesee959b02008-02-22 00:13:36 +01001619static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621fc_host_statistic(seconds_since_last_reset);
1622fc_host_statistic(tx_frames);
1623fc_host_statistic(tx_words);
1624fc_host_statistic(rx_frames);
1625fc_host_statistic(rx_words);
1626fc_host_statistic(lip_count);
1627fc_host_statistic(nos_count);
1628fc_host_statistic(error_frames);
1629fc_host_statistic(dumped_frames);
1630fc_host_statistic(link_failure_count);
1631fc_host_statistic(loss_of_sync_count);
1632fc_host_statistic(loss_of_signal_count);
1633fc_host_statistic(prim_seq_protocol_err_count);
1634fc_host_statistic(invalid_tx_word_count);
1635fc_host_statistic(invalid_crc_count);
1636fc_host_statistic(fcp_input_requests);
1637fc_host_statistic(fcp_output_requests);
1638fc_host_statistic(fcp_control_requests);
1639fc_host_statistic(fcp_input_megabytes);
1640fc_host_statistic(fcp_output_megabytes);
1641
1642static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001643fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1644 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Tony Jonesee959b02008-02-22 00:13:36 +01001646 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 struct fc_internal *i = to_fc_internal(shost->transportt);
1648
1649 /* ignore any data value written to the attribute */
1650 if (i->f->reset_fc_host_stats) {
1651 i->f->reset_fc_host_stats(shost);
1652 return count;
1653 }
1654
1655 return -ENOENT;
1656}
Tony Jonesee959b02008-02-22 00:13:36 +01001657static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 fc_reset_statistics);
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660static struct attribute *fc_statistics_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001661 &device_attr_host_seconds_since_last_reset.attr,
1662 &device_attr_host_tx_frames.attr,
1663 &device_attr_host_tx_words.attr,
1664 &device_attr_host_rx_frames.attr,
1665 &device_attr_host_rx_words.attr,
1666 &device_attr_host_lip_count.attr,
1667 &device_attr_host_nos_count.attr,
1668 &device_attr_host_error_frames.attr,
1669 &device_attr_host_dumped_frames.attr,
1670 &device_attr_host_link_failure_count.attr,
1671 &device_attr_host_loss_of_sync_count.attr,
1672 &device_attr_host_loss_of_signal_count.attr,
1673 &device_attr_host_prim_seq_protocol_err_count.attr,
1674 &device_attr_host_invalid_tx_word_count.attr,
1675 &device_attr_host_invalid_crc_count.attr,
1676 &device_attr_host_fcp_input_requests.attr,
1677 &device_attr_host_fcp_output_requests.attr,
1678 &device_attr_host_fcp_control_requests.attr,
1679 &device_attr_host_fcp_input_megabytes.attr,
1680 &device_attr_host_fcp_output_megabytes.attr,
1681 &device_attr_host_reset_statistics.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 NULL
1683};
1684
1685static struct attribute_group fc_statistics_group = {
1686 .name = "statistics",
1687 .attrs = fc_statistics_attrs,
1688};
1689
James Smarta53eb5e2007-04-27 12:41:09 -04001690
1691/* Host Vport Attributes */
1692
1693static int
1694fc_parse_wwn(const char *ns, u64 *nm)
1695{
1696 unsigned int i, j;
1697 u8 wwn[8];
1698
1699 memset(wwn, 0, sizeof(wwn));
1700
1701 /* Validate and store the new name */
1702 for (i=0, j=0; i < 16; i++) {
1703 if ((*ns >= 'a') && (*ns <= 'f'))
1704 j = ((j << 4) | ((*ns++ -'a') + 10));
1705 else if ((*ns >= 'A') && (*ns <= 'F'))
1706 j = ((j << 4) | ((*ns++ -'A') + 10));
1707 else if ((*ns >= '0') && (*ns <= '9'))
1708 j = ((j << 4) | (*ns++ -'0'));
1709 else
1710 return -EINVAL;
1711 if (i % 2) {
1712 wwn[i/2] = j & 0xff;
1713 j = 0;
1714 }
1715 }
1716
1717 *nm = wwn_to_u64(wwn);
1718
1719 return 0;
1720}
1721
1722
1723/*
1724 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1725 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1726 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1727 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1728 */
1729static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001730store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1731 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001732{
Tony Jonesee959b02008-02-22 00:13:36 +01001733 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001734 struct fc_vport_identifiers vid;
1735 struct fc_vport *vport;
1736 unsigned int cnt=count;
1737 int stat;
1738
1739 memset(&vid, 0, sizeof(vid));
1740
1741 /* count may include a LF at end of string */
1742 if (buf[cnt-1] == '\n')
1743 cnt--;
1744
1745 /* validate we have enough characters for WWPN */
1746 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1747 return -EINVAL;
1748
1749 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1750 if (stat)
1751 return stat;
1752
1753 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1754 if (stat)
1755 return stat;
1756
1757 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1758 vid.vport_type = FC_PORTTYPE_NPIV;
1759 /* vid.symbolic_name is already zero/NULL's */
1760 vid.disable = false; /* always enabled */
1761
1762 /* we only allow support on Channel 0 !!! */
1763 stat = fc_vport_create(shost, 0, &shost->shost_gendev, &vid, &vport);
1764 return stat ? stat : count;
1765}
Tony Jonesee959b02008-02-22 00:13:36 +01001766static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001767 store_fc_host_vport_create);
1768
1769
1770/*
1771 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1772 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1773 * The WWNs are specified as hex characters, and may *not* contain
1774 * any prefixes (e.g. 0x, x, etc)
1775 */
1776static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001777store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1778 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001779{
Tony Jonesee959b02008-02-22 00:13:36 +01001780 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001781 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1782 struct fc_vport *vport;
1783 u64 wwpn, wwnn;
1784 unsigned long flags;
1785 unsigned int cnt=count;
1786 int stat, match;
1787
1788 /* count may include a LF at end of string */
1789 if (buf[cnt-1] == '\n')
1790 cnt--;
1791
1792 /* validate we have enough characters for WWPN */
1793 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1794 return -EINVAL;
1795
1796 stat = fc_parse_wwn(&buf[0], &wwpn);
1797 if (stat)
1798 return stat;
1799
1800 stat = fc_parse_wwn(&buf[17], &wwnn);
1801 if (stat)
1802 return stat;
1803
1804 spin_lock_irqsave(shost->host_lock, flags);
1805 match = 0;
1806 /* we only allow support on Channel 0 !!! */
1807 list_for_each_entry(vport, &fc_host->vports, peers) {
1808 if ((vport->channel == 0) &&
1809 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1810 match = 1;
1811 break;
1812 }
1813 }
1814 spin_unlock_irqrestore(shost->host_lock, flags);
1815
1816 if (!match)
1817 return -ENODEV;
1818
1819 stat = fc_vport_terminate(vport);
1820 return stat ? stat : count;
1821}
Tony Jonesee959b02008-02-22 00:13:36 +01001822static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001823 store_fc_host_vport_delete);
1824
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826static int fc_host_match(struct attribute_container *cont,
1827 struct device *dev)
1828{
1829 struct Scsi_Host *shost;
1830 struct fc_internal *i;
1831
1832 if (!scsi_is_host_device(dev))
1833 return 0;
1834
1835 shost = dev_to_shost(dev);
1836 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1837 != &fc_host_class.class)
1838 return 0;
1839
1840 i = to_fc_internal(shost->transportt);
1841
1842 return &i->t.host_attrs.ac == cont;
1843}
1844
1845static int fc_target_match(struct attribute_container *cont,
1846 struct device *dev)
1847{
1848 struct Scsi_Host *shost;
1849 struct fc_internal *i;
1850
1851 if (!scsi_is_target_device(dev))
1852 return 0;
1853
1854 shost = dev_to_shost(dev->parent);
1855 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1856 != &fc_host_class.class)
1857 return 0;
1858
1859 i = to_fc_internal(shost->transportt);
1860
1861 return &i->t.target_attrs.ac == cont;
1862}
1863
1864static void fc_rport_dev_release(struct device *dev)
1865{
1866 struct fc_rport *rport = dev_to_rport(dev);
1867 put_device(dev->parent);
1868 kfree(rport);
1869}
1870
1871int scsi_is_fc_rport(const struct device *dev)
1872{
1873 return dev->release == fc_rport_dev_release;
1874}
1875EXPORT_SYMBOL(scsi_is_fc_rport);
1876
1877static int fc_rport_match(struct attribute_container *cont,
1878 struct device *dev)
1879{
1880 struct Scsi_Host *shost;
1881 struct fc_internal *i;
1882
1883 if (!scsi_is_fc_rport(dev))
1884 return 0;
1885
1886 shost = dev_to_shost(dev->parent);
1887 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1888 != &fc_host_class.class)
1889 return 0;
1890
1891 i = to_fc_internal(shost->transportt);
1892
1893 return &i->rport_attr_cont.ac == cont;
1894}
1895
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001896
James Smarta53eb5e2007-04-27 12:41:09 -04001897static void fc_vport_dev_release(struct device *dev)
1898{
1899 struct fc_vport *vport = dev_to_vport(dev);
1900 put_device(dev->parent); /* release kobj parent */
1901 kfree(vport);
1902}
1903
1904int scsi_is_fc_vport(const struct device *dev)
1905{
1906 return dev->release == fc_vport_dev_release;
1907}
1908EXPORT_SYMBOL(scsi_is_fc_vport);
1909
1910static int fc_vport_match(struct attribute_container *cont,
1911 struct device *dev)
1912{
1913 struct fc_vport *vport;
1914 struct Scsi_Host *shost;
1915 struct fc_internal *i;
1916
1917 if (!scsi_is_fc_vport(dev))
1918 return 0;
1919 vport = dev_to_vport(dev);
1920
1921 shost = vport_to_shost(vport);
1922 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1923 != &fc_host_class.class)
1924 return 0;
1925
1926 i = to_fc_internal(shost->transportt);
1927 return &i->vport_attr_cont.ac == cont;
1928}
1929
1930
James Smartc829c392006-03-13 08:28:57 -05001931/**
1932 * fc_timed_out - FC Transport I/O timeout intercept handler
James Smartc829c392006-03-13 08:28:57 -05001933 * @scmd: The SCSI command which timed out
1934 *
1935 * This routine protects against error handlers getting invoked while a
1936 * rport is in a blocked state, typically due to a temporarily loss of
1937 * connectivity. If the error handlers are allowed to proceed, requests
1938 * to abort i/o, reset the target, etc will likely fail as there is no way
1939 * to communicate with the device to perform the requested function. These
1940 * failures may result in the midlayer taking the device offline, requiring
1941 * manual intervention to restore operation.
1942 *
1943 * This routine, called whenever an i/o times out, validates the state of
1944 * the underlying rport. If the rport is blocked, it returns
1945 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1946 * Eventually, either the device will return, or devloss_tmo will fire,
1947 * and when the timeout then fires, it will be handled normally.
1948 * If the rport is not blocked, normal error handling continues.
1949 *
1950 * Notes:
1951 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05001952 */
James Smartc829c392006-03-13 08:28:57 -05001953static enum scsi_eh_timer_return
1954fc_timed_out(struct scsi_cmnd *scmd)
1955{
1956 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1957
1958 if (rport->port_state == FC_PORTSTATE_BLOCKED)
1959 return EH_RESET_TIMER;
1960
1961 return EH_NOT_HANDLED;
1962}
1963
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001964/*
James Smartbda23252008-04-24 12:12:46 -04001965 * Called by fc_user_scan to locate an rport on the shost that
1966 * matches the channel and target id, and invoke scsi_scan_target()
1967 * on the rport.
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001968 */
James Smartbda23252008-04-24 12:12:46 -04001969static void
1970fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001971{
1972 struct fc_rport *rport;
James Smartbda23252008-04-24 12:12:46 -04001973 unsigned long flags;
1974
1975 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001976
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001977 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1978 if (rport->scsi_target_id == -1)
1979 continue;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001980
Hannes Reinecke0d2fcd92007-06-14 15:16:45 +02001981 if (rport->port_state != FC_PORTSTATE_ONLINE)
1982 continue;
1983
James Smartbda23252008-04-24 12:12:46 -04001984 if ((channel == rport->channel) &&
1985 (id == rport->scsi_target_id)) {
1986 spin_unlock_irqrestore(shost->host_lock, flags);
1987 scsi_scan_target(&rport->dev, channel, id, lun, 1);
1988 return;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001989 }
1990 }
1991
James Smartbda23252008-04-24 12:12:46 -04001992 spin_unlock_irqrestore(shost->host_lock, flags);
1993}
1994
1995/*
1996 * Called via sysfs scan routines. Necessary, as the FC transport
1997 * wants to place all target objects below the rport object. So this
1998 * routine must invoke the scsi_scan_target() routine with the rport
1999 * object as the parent.
2000 */
2001static int
2002fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2003{
2004 uint chlo, chhi;
2005 uint tgtlo, tgthi;
2006
2007 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2008 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2009 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2010 return -EINVAL;
2011
2012 if (channel == SCAN_WILD_CARD) {
2013 chlo = 0;
2014 chhi = shost->max_channel + 1;
2015 } else {
2016 chlo = channel;
2017 chhi = channel + 1;
2018 }
2019
2020 if (id == SCAN_WILD_CARD) {
2021 tgtlo = 0;
2022 tgthi = shost->max_id;
2023 } else {
2024 tgtlo = id;
2025 tgthi = id + 1;
2026 }
2027
2028 for ( ; chlo < chhi; chlo++)
2029 for ( ; tgtlo < tgthi; tgtlo++)
2030 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2031
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002032 return 0;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002033}
2034
FUJITA Tomonori75252362007-09-01 02:02:27 +09002035static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
2036 int result)
2037{
2038 struct fc_internal *i = to_fc_internal(shost->transportt);
2039 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
2040}
2041
2042static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
2043{
2044 struct fc_internal *i = to_fc_internal(shost->transportt);
2045 return i->f->it_nexus_response(shost, nexus, result);
2046}
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048struct scsi_transport_template *
2049fc_attach_transport(struct fc_function_template *ft)
2050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int count;
Jes Sorensen24669f752006-01-16 10:31:18 -05002052 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2053 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 if (unlikely(!i))
2056 return NULL;
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2059 i->t.target_attrs.ac.class = &fc_transport_class.class;
2060 i->t.target_attrs.ac.match = fc_target_match;
2061 i->t.target_size = sizeof(struct fc_starget_attrs);
2062 transport_container_register(&i->t.target_attrs);
2063
2064 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2065 i->t.host_attrs.ac.class = &fc_host_class.class;
2066 i->t.host_attrs.ac.match = fc_host_match;
2067 i->t.host_size = sizeof(struct fc_host_attrs);
2068 if (ft->get_fc_host_stats)
2069 i->t.host_attrs.statistics = &fc_statistics_group;
2070 transport_container_register(&i->t.host_attrs);
2071
2072 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2073 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2074 i->rport_attr_cont.ac.match = fc_rport_match;
2075 transport_container_register(&i->rport_attr_cont);
2076
James Smarta53eb5e2007-04-27 12:41:09 -04002077 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2078 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2079 i->vport_attr_cont.ac.match = fc_vport_match;
2080 transport_container_register(&i->vport_attr_cont);
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 i->f = ft;
2083
2084 /* Transport uses the shost workq for scsi scanning */
2085 i->t.create_work_queue = 1;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002086
James Smartc829c392006-03-13 08:28:57 -05002087 i->t.eh_timed_out = fc_timed_out;
2088
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002089 i->t.user_scan = fc_user_scan;
James Smart9ef3e4a2007-05-24 19:04:44 -04002090
FUJITA Tomonori75252362007-09-01 02:02:27 +09002091 /* target-mode drivers' functions */
2092 i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2093 i->t.it_nexus_response = fc_it_nexus_response;
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 /*
2096 * Setup SCSI Target Attributes.
2097 */
2098 count = 0;
2099 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2100 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2101 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2102
2103 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2104
2105 i->starget_attrs[count] = NULL;
2106
2107
2108 /*
2109 * Setup SCSI Host Attributes.
2110 */
2111 count=0;
2112 SETUP_HOST_ATTRIBUTE_RD(node_name);
2113 SETUP_HOST_ATTRIBUTE_RD(port_name);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01002114 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2116 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2118 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
James Smarta53eb5e2007-04-27 12:41:09 -04002119 if (ft->vport_create) {
2120 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2121 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2124
2125 SETUP_HOST_ATTRIBUTE_RD(port_id);
2126 SETUP_HOST_ATTRIBUTE_RD(port_type);
2127 SETUP_HOST_ATTRIBUTE_RD(port_state);
2128 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2129 SETUP_HOST_ATTRIBUTE_RD(speed);
2130 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
James Smart016131b2006-08-14 08:20:25 -04002131 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
James Smartb8d08212006-08-17 08:00:43 -04002132 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /* Transport-managed attributes */
2135 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002136 if (ft->issue_fc_host_lip)
2137 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
James Smarta53eb5e2007-04-27 12:41:09 -04002138 if (ft->vport_create)
2139 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2140 if (ft->vport_delete)
2141 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 BUG_ON(count > FC_HOST_NUM_ATTRS);
2144
2145 i->host_attrs[count] = NULL;
2146
2147 /*
2148 * Setup Remote Port Attributes.
2149 */
2150 count=0;
2151 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2152 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2153 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2154 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2155 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2156 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2157 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2158 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2159 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
James Smart0f29b962006-08-18 17:33:29 -04002160 if (ft->terminate_rport_io)
2161 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2164
2165 i->rport_attrs[count] = NULL;
2166
James Smarta53eb5e2007-04-27 12:41:09 -04002167 /*
2168 * Setup Virtual Port Attributes.
2169 */
2170 count=0;
2171 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2172 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2173 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2174 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2175 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2176 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2177 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2178 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2179 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2180
2181 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2182
2183 i->vport_attrs[count] = NULL;
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return &i->t;
2186}
2187EXPORT_SYMBOL(fc_attach_transport);
2188
2189void fc_release_transport(struct scsi_transport_template *t)
2190{
2191 struct fc_internal *i = to_fc_internal(t);
2192
2193 transport_container_unregister(&i->t.target_attrs);
2194 transport_container_unregister(&i->t.host_attrs);
2195 transport_container_unregister(&i->rport_attr_cont);
James Smarta53eb5e2007-04-27 12:41:09 -04002196 transport_container_unregister(&i->vport_attr_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 kfree(i);
2199}
2200EXPORT_SYMBOL(fc_release_transport);
2201
James Smartaedf3492006-04-10 10:14:05 -04002202/**
2203 * fc_queue_work - Queue work to the fc_host workqueue.
2204 * @shost: Pointer to Scsi_Host bound to fc_host.
2205 * @work: Work to queue for execution.
2206 *
2207 * Return value:
James Smarta0785ed2006-05-11 13:27:09 -04002208 * 1 - work queued for execution
2209 * 0 - work is already queued
2210 * -EINVAL - work queue doesn't exist
Rob Landleyeb448202007-11-03 13:30:39 -05002211 */
James Smartaedf3492006-04-10 10:14:05 -04002212static int
2213fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2214{
2215 if (unlikely(!fc_host_work_q(shost))) {
2216 printk(KERN_ERR
2217 "ERROR: FC host '%s' attempted to queue work, "
2218 "when no workqueue created.\n", shost->hostt->name);
2219 dump_stack();
2220
2221 return -EINVAL;
2222 }
2223
2224 return queue_work(fc_host_work_q(shost), work);
2225}
2226
2227/**
2228 * fc_flush_work - Flush a fc_host's workqueue.
2229 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002230 */
James Smartaedf3492006-04-10 10:14:05 -04002231static void
2232fc_flush_work(struct Scsi_Host *shost)
2233{
2234 if (!fc_host_work_q(shost)) {
2235 printk(KERN_ERR
2236 "ERROR: FC host '%s' attempted to flush work, "
2237 "when no workqueue created.\n", shost->hostt->name);
2238 dump_stack();
2239 return;
2240 }
2241
2242 flush_workqueue(fc_host_work_q(shost));
2243}
2244
2245/**
2246 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2247 * @shost: Pointer to Scsi_Host bound to fc_host.
2248 * @work: Work to queue for execution.
2249 * @delay: jiffies to delay the work queuing
2250 *
2251 * Return value:
James Smart0f29b962006-08-18 17:33:29 -04002252 * 1 on success / 0 already queued / < 0 for error
Rob Landleyeb448202007-11-03 13:30:39 -05002253 */
James Smartaedf3492006-04-10 10:14:05 -04002254static int
David Howellsc4028952006-11-22 14:57:56 +00002255fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
James Smartaedf3492006-04-10 10:14:05 -04002256 unsigned long delay)
2257{
2258 if (unlikely(!fc_host_devloss_work_q(shost))) {
2259 printk(KERN_ERR
2260 "ERROR: FC host '%s' attempted to queue work, "
2261 "when no workqueue created.\n", shost->hostt->name);
2262 dump_stack();
2263
2264 return -EINVAL;
2265 }
2266
2267 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2268}
2269
2270/**
2271 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2272 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002273 */
James Smartaedf3492006-04-10 10:14:05 -04002274static void
2275fc_flush_devloss(struct Scsi_Host *shost)
2276{
2277 if (!fc_host_devloss_work_q(shost)) {
2278 printk(KERN_ERR
2279 "ERROR: FC host '%s' attempted to flush work, "
2280 "when no workqueue created.\n", shost->hostt->name);
2281 dump_stack();
2282 return;
2283 }
2284
2285 flush_workqueue(fc_host_devloss_work_q(shost));
2286}
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289/**
Rob Landleyeb448202007-11-03 13:30:39 -05002290 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2291 * @shost: Which &Scsi_Host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 *
2293 * This routine is expected to be called immediately preceeding the
2294 * a driver's call to scsi_remove_host().
2295 *
2296 * WARNING: A driver utilizing the fc_transport, which fails to call
Rob Landleyeb448202007-11-03 13:30:39 -05002297 * this routine prior to scsi_remove_host(), will leave dangling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 * objects in /sys/class/fc_remote_ports. Access to any of these
2299 * objects can result in a system crash !!!
2300 *
2301 * Notes:
2302 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304void
2305fc_remove_host(struct Scsi_Host *shost)
2306{
James Smarta53eb5e2007-04-27 12:41:09 -04002307 struct fc_vport *vport = NULL, *next_vport = NULL;
2308 struct fc_rport *rport = NULL, *next_rport = NULL;
James Smartaedf3492006-04-10 10:14:05 -04002309 struct workqueue_struct *work_q;
2310 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James Smarta53eb5e2007-04-27 12:41:09 -04002311 unsigned long flags;
James Smarta53eb5e2007-04-27 12:41:09 -04002312
2313 spin_lock_irqsave(shost->host_lock, flags);
2314
2315 /* Remove any vports */
James Smart9ef3e4a2007-05-24 19:04:44 -04002316 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2317 fc_queue_work(shost, &vport->vport_delete_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 /* Remove any remote ports */
2320 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002321 &fc_host->rports, peers) {
2322 list_del(&rport->peers);
2323 rport->port_state = FC_PORTSTATE_DELETED;
2324 fc_queue_work(shost, &rport->rport_delete_work);
2325 }
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002328 &fc_host->rport_bindings, peers) {
2329 list_del(&rport->peers);
2330 rport->port_state = FC_PORTSTATE_DELETED;
2331 fc_queue_work(shost, &rport->rport_delete_work);
2332 }
2333
James Smarta53eb5e2007-04-27 12:41:09 -04002334 spin_unlock_irqrestore(shost->host_lock, flags);
2335
James Smartaedf3492006-04-10 10:14:05 -04002336 /* flush all scan work items */
2337 scsi_flush_work(shost);
2338
2339 /* flush all stgt delete, and rport delete work items, then kill it */
2340 if (fc_host->work_q) {
2341 work_q = fc_host->work_q;
2342 fc_host->work_q = NULL;
2343 destroy_workqueue(work_q);
2344 }
2345
2346 /* flush all devloss work items, then kill it */
2347 if (fc_host->devloss_work_q) {
2348 work_q = fc_host->devloss_work_q;
2349 fc_host->devloss_work_q = NULL;
2350 destroy_workqueue(work_q);
2351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353EXPORT_SYMBOL(fc_remove_host);
2354
James Smartaedf3492006-04-10 10:14:05 -04002355
2356/**
2357 * fc_starget_delete - called to delete the scsi decendents of an rport
David Howellsc4028952006-11-22 14:57:56 +00002358 * @work: remote port to be operated on.
Rob Landleyeb448202007-11-03 13:30:39 -05002359 *
2360 * Deletes target and all sdevs.
2361 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002362static void
David Howellsc4028952006-11-22 14:57:56 +00002363fc_starget_delete(struct work_struct *work)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002364{
David Howellsc4028952006-11-22 14:57:56 +00002365 struct fc_rport *rport =
2366 container_of(work, struct fc_rport, stgt_delete_work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002367 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002368 struct fc_internal *i = to_fc_internal(shost->transportt);
2369
James Smart92740b22007-04-27 11:53:17 -04002370 /* Involve the LLDD if possible to terminate all io on the rport. */
2371 if (i->f->terminate_rport_io)
James Smart0f29b962006-08-18 17:33:29 -04002372 i->f->terminate_rport_io(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002373
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002374 scsi_remove_target(&rport->dev);
2375}
2376
James Smartaedf3492006-04-10 10:14:05 -04002377
2378/**
2379 * fc_rport_final_delete - finish rport termination and delete it.
David Howellsc4028952006-11-22 14:57:56 +00002380 * @work: remote port to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05002381 */
James Smartaedf3492006-04-10 10:14:05 -04002382static void
David Howellsc4028952006-11-22 14:57:56 +00002383fc_rport_final_delete(struct work_struct *work)
James Smartaedf3492006-04-10 10:14:05 -04002384{
David Howellsc4028952006-11-22 14:57:56 +00002385 struct fc_rport *rport =
2386 container_of(work, struct fc_rport, rport_delete_work);
James Smartaedf3492006-04-10 10:14:05 -04002387 struct device *dev = &rport->dev;
2388 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002389 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart92740b22007-04-27 11:53:17 -04002390 unsigned long flags;
James Smartaedf3492006-04-10 10:14:05 -04002391
2392 /*
James Smart9ef3e4a2007-05-24 19:04:44 -04002393 * if a scan is pending, flush the SCSI Host work_q so that
James Smartaedf3492006-04-10 10:14:05 -04002394 * that we can reclaim the rport scan work element.
2395 */
2396 if (rport->flags & FC_RPORT_SCAN_PENDING)
2397 scsi_flush_work(shost);
2398
James Smart92740b22007-04-27 11:53:17 -04002399 /* involve the LLDD to terminate all pending i/o */
2400 if (i->f->terminate_rport_io)
2401 i->f->terminate_rport_io(rport);
2402
2403 /*
2404 * Cancel any outstanding timers. These should really exist
2405 * only when rmmod'ing the LLDD and we're asking for
2406 * immediate termination of the rports
2407 */
2408 spin_lock_irqsave(shost->host_lock, flags);
2409 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2410 spin_unlock_irqrestore(shost->host_lock, flags);
2411 if (!cancel_delayed_work(&rport->fail_io_work))
2412 fc_flush_devloss(shost);
2413 if (!cancel_delayed_work(&rport->dev_loss_work))
2414 fc_flush_devloss(shost);
2415 spin_lock_irqsave(shost->host_lock, flags);
2416 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2417 }
2418 spin_unlock_irqrestore(shost->host_lock, flags);
2419
James Smart0f29b962006-08-18 17:33:29 -04002420 /* Delete SCSI target and sdevs */
2421 if (rport->scsi_target_id != -1)
David Howellsc4028952006-11-22 14:57:56 +00002422 fc_starget_delete(&rport->stgt_delete_work);
James Smart92740b22007-04-27 11:53:17 -04002423
2424 /*
2425 * Notify the driver that the rport is now dead. The LLDD will
2426 * also guarantee that any communication to the rport is terminated
2427 */
2428 if (i->f->dev_loss_tmo_callbk)
James Smart0f29b962006-08-18 17:33:29 -04002429 i->f->dev_loss_tmo_callbk(rport);
James Smart0f29b962006-08-18 17:33:29 -04002430
James Smartaedf3492006-04-10 10:14:05 -04002431 transport_remove_device(dev);
2432 device_del(dev);
2433 transport_destroy_device(dev);
James Smart3bdad7b2006-06-26 14:19:59 -04002434 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2435 put_device(dev); /* for self-reference */
James Smartaedf3492006-04-10 10:14:05 -04002436}
2437
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439/**
2440 * fc_rport_create - allocates and creates a remote FC port.
2441 * @shost: scsi host the remote port is connected to.
2442 * @channel: Channel on shost port connected to.
2443 * @ids: The world wide names, fc address, and FC4 port
2444 * roles for the remote port.
2445 *
2446 * Allocates and creates the remoter port structure, including the
2447 * class and sysfs creation.
2448 *
2449 * Notes:
2450 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002451 */
Adrian Bunk44818ef2007-07-09 11:59:59 -07002452static struct fc_rport *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453fc_rport_create(struct Scsi_Host *shost, int channel,
2454 struct fc_rport_identifiers *ids)
2455{
James Smartaedf3492006-04-10 10:14:05 -04002456 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct fc_internal *fci = to_fc_internal(shost->transportt);
2458 struct fc_rport *rport;
2459 struct device *dev;
2460 unsigned long flags;
2461 int error;
2462 size_t size;
2463
2464 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
Jes Sorensen24669f752006-01-16 10:31:18 -05002465 rport = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (unlikely(!rport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002467 printk(KERN_ERR "%s: allocation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return NULL;
2469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 rport->maxframe_size = -1;
2472 rport->supported_classes = FC_COS_UNSPECIFIED;
2473 rport->dev_loss_tmo = fc_dev_loss_tmo;
2474 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2475 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2476 rport->port_id = ids->port_id;
2477 rport->roles = ids->roles;
2478 rport->port_state = FC_PORTSTATE_ONLINE;
2479 if (fci->f->dd_fcrport_size)
2480 rport->dd_data = &rport[1];
2481 rport->channel = channel;
James Smart0f29b962006-08-18 17:33:29 -04002482 rport->fast_io_fail_tmo = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
David Howellsc4028952006-11-22 14:57:56 +00002484 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2485 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2486 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2487 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2488 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 spin_lock_irqsave(shost->host_lock, flags);
2491
2492 rport->number = fc_host->next_rport_number++;
James Smarta53eb5e2007-04-27 12:41:09 -04002493 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 rport->scsi_target_id = fc_host->next_target_id++;
2495 else
2496 rport->scsi_target_id = -1;
James Smartaedf3492006-04-10 10:14:05 -04002497 list_add_tail(&rport->peers, &fc_host->rports);
James Smart3bdad7b2006-06-26 14:19:59 -04002498 get_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 spin_unlock_irqrestore(shost->host_lock, flags);
2501
2502 dev = &rport->dev;
James Smart3bdad7b2006-06-26 14:19:59 -04002503 device_initialize(dev); /* takes self reference */
2504 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 dev->release = fc_rport_dev_release;
2506 sprintf(dev->bus_id, "rport-%d:%d-%d",
2507 shost->host_no, channel, rport->number);
2508 transport_setup_device(dev);
2509
2510 error = device_add(dev);
2511 if (error) {
2512 printk(KERN_ERR "FC Remote Port device_add failed\n");
2513 goto delete_rport;
2514 }
2515 transport_add_device(dev);
2516 transport_configure_device(dev);
2517
James Smarta53eb5e2007-04-27 12:41:09 -04002518 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002520 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 return rport;
2525
2526delete_rport:
2527 transport_destroy_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 spin_lock_irqsave(shost->host_lock, flags);
2529 list_del(&rport->peers);
James Smart3bdad7b2006-06-26 14:19:59 -04002530 put_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 spin_unlock_irqrestore(shost->host_lock, flags);
2532 put_device(dev->parent);
2533 kfree(rport);
2534 return NULL;
2535}
2536
2537/**
Rob Landleyeb448202007-11-03 13:30:39 -05002538 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 * @shost: scsi host the remote port is connected to.
2540 * @channel: Channel on shost port connected to.
2541 * @ids: The world wide names, fc address, and FC4 port
2542 * roles for the remote port.
2543 *
2544 * The LLDD calls this routine to notify the transport of the existence
2545 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2546 * of the port, it's FC address (port_id), and the FC4 roles that are
2547 * active for the port.
2548 *
2549 * For ports that are FCP targets (aka scsi targets), the FC transport
2550 * maintains consistent target id bindings on behalf of the LLDD.
2551 * A consistent target id binding is an assignment of a target id to
2552 * a remote port identifier, which persists while the scsi host is
2553 * attached. The remote port can disappear, then later reappear, and
2554 * it's target id assignment remains the same. This allows for shifts
2555 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2556 * changes to the scsi subsystem which is based on scsi host number and
2557 * target id values. Bindings are only valid during the attachment of
2558 * the scsi host. If the host detaches, then later re-attaches, target
2559 * id bindings may change.
2560 *
2561 * This routine is responsible for returning a remote port structure.
2562 * The routine will search the list of remote ports it maintains
2563 * internally on behalf of consistent target id mappings. If found, the
2564 * remote port structure will be reused. Otherwise, a new remote port
2565 * structure will be allocated.
2566 *
2567 * Whenever a remote port is allocated, a new fc_remote_port class
2568 * device is created.
2569 *
2570 * Should not be called from interrupt context.
2571 *
2572 * Notes:
2573 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575struct fc_rport *
2576fc_remote_port_add(struct Scsi_Host *shost, int channel,
2577 struct fc_rport_identifiers *ids)
2578{
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002579 struct fc_internal *fci = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04002580 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 struct fc_rport *rport;
2582 unsigned long flags;
2583 int match = 0;
2584
James Smartaedf3492006-04-10 10:14:05 -04002585 /* ensure any stgt delete functions are done */
2586 fc_flush_work(shost);
2587
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002588 /*
2589 * Search the list of "active" rports, for an rport that has been
2590 * deleted, but we've held off the real delete while the target
2591 * is in a "blocked" state.
2592 */
2593 spin_lock_irqsave(shost->host_lock, flags);
2594
James Smartaedf3492006-04-10 10:14:05 -04002595 list_for_each_entry(rport, &fc_host->rports, peers) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002596
2597 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2598 (rport->channel == channel)) {
2599
James Smartaedf3492006-04-10 10:14:05 -04002600 switch (fc_host->tgtid_bind_type) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002601 case FC_TGTID_BIND_BY_WWPN:
2602 case FC_TGTID_BIND_NONE:
2603 if (rport->port_name == ids->port_name)
2604 match = 1;
2605 break;
2606 case FC_TGTID_BIND_BY_WWNN:
2607 if (rport->node_name == ids->node_name)
2608 match = 1;
2609 break;
2610 case FC_TGTID_BIND_BY_ID:
2611 if (rport->port_id == ids->port_id)
2612 match = 1;
2613 break;
2614 }
2615
2616 if (match) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002617
2618 memcpy(&rport->node_name, &ids->node_name,
2619 sizeof(rport->node_name));
2620 memcpy(&rport->port_name, &ids->port_name,
2621 sizeof(rport->port_name));
2622 rport->port_id = ids->port_id;
2623
2624 rport->port_state = FC_PORTSTATE_ONLINE;
2625 rport->roles = ids->roles;
2626
2627 spin_unlock_irqrestore(shost->host_lock, flags);
2628
2629 if (fci->f->dd_fcrport_size)
2630 memset(rport->dd_data, 0,
2631 fci->f->dd_fcrport_size);
2632
2633 /*
James Smart92740b22007-04-27 11:53:17 -04002634 * If we were not a target, cancel the
2635 * io terminate and rport timers, and
2636 * we're done.
2637 *
2638 * If we were a target, but our new role
2639 * doesn't indicate a target, leave the
2640 * timers running expecting the role to
2641 * change as the target fully logs in. If
2642 * it doesn't, the target will be torn down.
2643 *
2644 * If we were a target, and our role shows
2645 * we're still a target, cancel the timers
2646 * and kick off a scan.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002647 */
James Smart92740b22007-04-27 11:53:17 -04002648
2649 /* was a target, not in roles */
2650 if ((rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04002651 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002652 return rport;
2653
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002654 /*
James Smart92740b22007-04-27 11:53:17 -04002655 * Stop the fail io and dev_loss timers.
2656 * If they flush, the port_state will
2657 * be checked and will NOOP the function.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002658 */
James Smart0f29b962006-08-18 17:33:29 -04002659 if (!cancel_delayed_work(&rport->fail_io_work))
2660 fc_flush_devloss(shost);
James Smart92740b22007-04-27 11:53:17 -04002661 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002662 fc_flush_devloss(shost);
2663
2664 spin_lock_irqsave(shost->host_lock, flags);
2665
2666 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002667
James Smart92740b22007-04-27 11:53:17 -04002668 /* if target, initiate a scan */
2669 if (rport->scsi_target_id != -1) {
2670 rport->flags |= FC_RPORT_SCAN_PENDING;
2671 scsi_queue_work(shost,
2672 &rport->scan_work);
2673 spin_unlock_irqrestore(shost->host_lock,
2674 flags);
2675 scsi_target_unblock(&rport->dev);
2676 } else
2677 spin_unlock_irqrestore(shost->host_lock,
2678 flags);
James Smarta0785ed2006-05-11 13:27:09 -04002679
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002680 return rport;
2681 }
2682 }
2683 }
2684
James Smart92740b22007-04-27 11:53:17 -04002685 /*
2686 * Search the bindings array
2687 * Note: if never a FCP target, you won't be on this list
2688 */
James Smartaedf3492006-04-10 10:14:05 -04002689 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 /* search for a matching consistent binding */
2692
James Smartaedf3492006-04-10 10:14:05 -04002693 list_for_each_entry(rport, &fc_host->rport_bindings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 peers) {
2695 if (rport->channel != channel)
2696 continue;
2697
James Smartaedf3492006-04-10 10:14:05 -04002698 switch (fc_host->tgtid_bind_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 case FC_TGTID_BIND_BY_WWPN:
2700 if (rport->port_name == ids->port_name)
2701 match = 1;
2702 break;
2703 case FC_TGTID_BIND_BY_WWNN:
2704 if (rport->node_name == ids->node_name)
2705 match = 1;
2706 break;
2707 case FC_TGTID_BIND_BY_ID:
2708 if (rport->port_id == ids->port_id)
2709 match = 1;
2710 break;
2711 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2712 break;
2713 }
2714
2715 if (match) {
James Smartaedf3492006-04-10 10:14:05 -04002716 list_move_tail(&rport->peers, &fc_host->rports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 break;
2718 }
2719 }
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (match) {
2722 memcpy(&rport->node_name, &ids->node_name,
2723 sizeof(rport->node_name));
2724 memcpy(&rport->port_name, &ids->port_name,
2725 sizeof(rport->port_name));
2726 rport->port_id = ids->port_id;
2727 rport->roles = ids->roles;
2728 rport->port_state = FC_PORTSTATE_ONLINE;
2729
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002730 if (fci->f->dd_fcrport_size)
2731 memset(rport->dd_data, 0,
2732 fci->f->dd_fcrport_size);
2733
James Smarta53eb5e2007-04-27 12:41:09 -04002734 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002736 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 scsi_queue_work(shost, &rport->scan_work);
James Smarta0785ed2006-05-11 13:27:09 -04002738 spin_unlock_irqrestore(shost->host_lock, flags);
2739 scsi_target_unblock(&rport->dev);
2740 } else
2741 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743 return rport;
2744 }
2745 }
2746
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002747 spin_unlock_irqrestore(shost->host_lock, flags);
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /* No consistent binding found - create new remote port entry */
2750 rport = fc_rport_create(shost, channel, ids);
2751
2752 return rport;
2753}
2754EXPORT_SYMBOL(fc_remote_port_add);
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757/**
Rob Landleyeb448202007-11-03 13:30:39 -05002758 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 * @rport: The remote port that no longer exists
2760 *
2761 * The LLDD calls this routine to notify the transport that a remote
2762 * port is no longer part of the topology. Note: Although a port
2763 * may no longer be part of the topology, it may persist in the remote
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002764 * ports displayed by the fc_host. We do this under 2 conditions:
Rob Landleyeb448202007-11-03 13:30:39 -05002765 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002766 * This allows the port to temporarily disappear, then reappear without
2767 * disrupting the SCSI device tree attached to it. During the "blocked"
2768 * period the port will still exist.
Rob Landleyeb448202007-11-03 13:30:39 -05002769 * 2) If the port was a scsi target and disappears for longer than we
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002770 * expect, we'll delete the port and the tear down the SCSI device tree
2771 * attached to it. However, we want to semi-persist the target id assigned
2772 * to that port if it eventually does exist. The port structure will
2773 * remain (although with minimal information) so that the target id
2774 * bindings remails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 *
2776 * If the remote port is not an FCP Target, it will be fully torn down
2777 * and deallocated, including the fc_remote_port class device.
2778 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002779 * If the remote port is an FCP Target, the port will be placed in a
2780 * temporary blocked state. From the LLDD's perspective, the rport no
2781 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2782 * exists, but all sdevs on it are blocked from further I/O. The following
Rob Landleyeb448202007-11-03 13:30:39 -05002783 * is then expected.
2784 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002785 * If the remote port does not return (signaled by a LLDD call to
2786 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2787 * scsi target is removed - killing all outstanding i/o and removing the
2788 * scsi devices attached ot it. The port structure will be marked Not
2789 * Present and be partially cleared, leaving only enough information to
2790 * recognize the remote port relative to the scsi target id binding if
2791 * it later appears. The port will remain as long as there is a valid
2792 * binding (e.g. until the user changes the binding type or unloads the
2793 * scsi host with the binding).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002795 * If the remote port returns within the dev_loss_tmo value (and matches
2796 * according to the target id binding type), the port structure will be
2797 * reused. If it is no longer a SCSI target, the target will be torn
2798 * down. If it continues to be a SCSI target, then the target will be
2799 * unblocked (allowing i/o to be resumed), and a scan will be activated
2800 * to ensure that all luns are detected.
2801 *
2802 * Called from normal process context only - cannot be called from interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 *
2804 * Notes:
2805 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807void
2808fc_remote_port_delete(struct fc_rport *rport)
2809{
James Smartaedf3492006-04-10 10:14:05 -04002810 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002811 struct fc_internal *i = to_fc_internal(shost->transportt);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002812 int timeout = rport->dev_loss_tmo;
James Smartaedf3492006-04-10 10:14:05 -04002813 unsigned long flags;
2814
2815 /*
2816 * No need to flush the fc_host work_q's, as all adds are synchronous.
2817 *
2818 * We do need to reclaim the rport scan work element, so eventually
2819 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2820 * there's still a scan pending.
2821 */
2822
2823 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
James Smart92740b22007-04-27 11:53:17 -04002825 if (rport->port_state != FC_PORTSTATE_ONLINE) {
James Smartaedf3492006-04-10 10:14:05 -04002826 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return;
2828 }
2829
James Smart92740b22007-04-27 11:53:17 -04002830 /*
2831 * In the past, we if this was not an FCP-Target, we would
2832 * unconditionally just jump to deleting the rport.
2833 * However, rports can be used as node containers by the LLDD,
2834 * and its not appropriate to just terminate the rport at the
2835 * first sign of a loss in connectivity. The LLDD may want to
2836 * send ELS traffic to re-validate the login. If the rport is
2837 * immediately deleted, it makes it inappropriate for a node
2838 * container.
2839 * So... we now unconditionally wait dev_loss_tmo before
2840 * destroying an rport.
2841 */
2842
James Smartaedf3492006-04-10 10:14:05 -04002843 rport->port_state = FC_PORTSTATE_BLOCKED;
2844
2845 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2846
2847 spin_unlock_irqrestore(shost->host_lock, flags);
2848
FUJITA Tomonori75252362007-09-01 02:02:27 +09002849 if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
2850 shost->active_mode & MODE_TARGET)
2851 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
2852
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002853 scsi_target_block(&rport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
James Smart0f29b962006-08-18 17:33:29 -04002855 /* see if we need to kill io faster than waiting for device loss */
2856 if ((rport->fast_io_fail_tmo != -1) &&
2857 (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
2858 fc_queue_devloss_work(shost, &rport->fail_io_work,
2859 rport->fast_io_fail_tmo * HZ);
2860
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002861 /* cap the length the devices can be blocked until they are deleted */
James Smartaedf3492006-04-10 10:14:05 -04002862 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
2864EXPORT_SYMBOL(fc_remote_port_delete);
2865
2866/**
Rob Landleyeb448202007-11-03 13:30:39 -05002867 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 * @rport: The remote port that changed.
Rob Landleyeb448202007-11-03 13:30:39 -05002869 * @roles: New roles for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 *
Rob Landleyeb448202007-11-03 13:30:39 -05002871 * Description: The LLDD calls this routine to notify the transport that the
2872 * roles on a remote port may have changed. The largest effect of this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 * if a port now becomes a FCP Target, it must be allocated a
2874 * scsi target id. If the port is no longer a FCP target, any
2875 * scsi target id value assigned to it will persist in case the
2876 * role changes back to include FCP Target. No changes in the scsi
2877 * midlayer will be invoked if the role changes (in the expectation
2878 * that the role will be resumed. If it doesn't normal error processing
2879 * will take place).
2880 *
2881 * Should not be called from interrupt context.
2882 *
2883 * Notes:
2884 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002885 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886void
2887fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2888{
2889 struct Scsi_Host *shost = rport_to_shost(rport);
James Smartaedf3492006-04-10 10:14:05 -04002890 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 unsigned long flags;
2892 int create = 0;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002893 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 spin_lock_irqsave(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04002896 if (roles & FC_PORT_ROLE_FCP_TARGET) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002897 if (rport->scsi_target_id == -1) {
2898 rport->scsi_target_id = fc_host->next_target_id++;
2899 create = 1;
James Smarta53eb5e2007-04-27 12:41:09 -04002900 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002901 create = 1;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002902 } else if (shost->active_mode & MODE_TARGET) {
2903 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
2904 (char *)&rport->node_name);
2905 if (ret)
2906 printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
2907 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002910 rport->roles = roles;
2911
James Smartaedf3492006-04-10 10:14:05 -04002912 spin_unlock_irqrestore(shost->host_lock, flags);
2913
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002914 if (create) {
2915 /*
2916 * There may have been a delete timer running on the
2917 * port. Ensure that it is cancelled as we now know
2918 * the port is an FCP Target.
2919 * Note: we know the rport is exists and in an online
2920 * state as the LLDD would not have had an rport
2921 * reference to pass us.
2922 *
2923 * Take no action on the del_timer failure as the state
2924 * machine state change will validate the
2925 * transaction.
2926 */
James Smart0f29b962006-08-18 17:33:29 -04002927 if (!cancel_delayed_work(&rport->fail_io_work))
2928 fc_flush_devloss(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002929 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002930 fc_flush_devloss(shost);
2931
2932 spin_lock_irqsave(shost->host_lock, flags);
2933 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2934 spin_unlock_irqrestore(shost->host_lock, flags);
2935
2936 /* ensure any stgt delete functions are done */
2937 fc_flush_work(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002940 spin_lock_irqsave(shost->host_lock, flags);
2941 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002943 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04002944 scsi_target_unblock(&rport->dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947EXPORT_SYMBOL(fc_remote_port_rolechg);
2948
2949/**
Rob Landleyeb448202007-11-03 13:30:39 -05002950 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
James Smart92740b22007-04-27 11:53:17 -04002951 * @work: rport target that failed to reappear in the allotted time.
Rob Landleyeb448202007-11-03 13:30:39 -05002952 *
2953 * Description: An attempt to delete a remote port blocks, and if it fails
2954 * to return in the allotted time this gets called.
2955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956static void
David Howellsc4028952006-11-22 14:57:56 +00002957fc_timeout_deleted_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
David Howellsc4028952006-11-22 14:57:56 +00002959 struct fc_rport *rport =
2960 container_of(work, struct fc_rport, dev_loss_work.work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002961 struct Scsi_Host *shost = rport_to_shost(rport);
James Smartaedf3492006-04-10 10:14:05 -04002962 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002963 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002965 spin_lock_irqsave(shost->host_lock, flags);
2966
James Smartaedf3492006-04-10 10:14:05 -04002967 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2968
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002969 /*
James Smart92740b22007-04-27 11:53:17 -04002970 * If the port is ONLINE, then it came back. If it was a SCSI
2971 * target, validate it still is. If not, tear down the
2972 * scsi_target on it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002973 */
James Smartaedf3492006-04-10 10:14:05 -04002974 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
James Smart92740b22007-04-27 11:53:17 -04002975 (rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04002976 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002977 dev_printk(KERN_ERR, &rport->dev,
James Smartaedf3492006-04-10 10:14:05 -04002978 "blocked FC remote port time out: no longer"
2979 " a FCP target, removing starget\n");
James Smartaedf3492006-04-10 10:14:05 -04002980 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04002981 scsi_target_unblock(&rport->dev);
2982 fc_queue_work(shost, &rport->stgt_delete_work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002983 return;
2984 }
2985
James Smart92740b22007-04-27 11:53:17 -04002986 /* NOOP state - we're flushing workq's */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002987 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
2988 spin_unlock_irqrestore(shost->host_lock, flags);
2989 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04002990 "blocked FC remote port time out: leaving"
2991 " rport%s alone\n",
2992 (rport->scsi_target_id != -1) ? " and starget" : "");
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002993 return;
2994 }
2995
James Smart92740b22007-04-27 11:53:17 -04002996 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
2997 (rport->scsi_target_id == -1)) {
James Smartaedf3492006-04-10 10:14:05 -04002998 list_del(&rport->peers);
2999 rport->port_state = FC_PORTSTATE_DELETED;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003000 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003001 "blocked FC remote port time out: removing"
3002 " rport%s\n",
3003 (rport->scsi_target_id != -1) ? " and starget" : "");
James Smartaedf3492006-04-10 10:14:05 -04003004 fc_queue_work(shost, &rport->rport_delete_work);
3005 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003006 return;
3007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 dev_printk(KERN_ERR, &rport->dev,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003010 "blocked FC remote port time out: removing target and "
3011 "saving binding\n");
3012
James Smartaedf3492006-04-10 10:14:05 -04003013 list_move_tail(&rport->peers, &fc_host->rport_bindings);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003014
3015 /*
3016 * Note: We do not remove or clear the hostdata area. This allows
3017 * host-specific target data to persist along with the
3018 * scsi_target_id. It's up to the host to manage it's hostdata area.
3019 */
3020
3021 /*
3022 * Reinitialize port attributes that may change if the port comes back.
3023 */
3024 rport->maxframe_size = -1;
3025 rport->supported_classes = FC_COS_UNSPECIFIED;
James Smarta53eb5e2007-04-27 12:41:09 -04003026 rport->roles = FC_PORT_ROLE_UNKNOWN;
James Smartaedf3492006-04-10 10:14:05 -04003027 rport->port_state = FC_PORTSTATE_NOTPRESENT;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003028
3029 /* remove the identifiers that aren't used in the consisting binding */
James Smartaedf3492006-04-10 10:14:05 -04003030 switch (fc_host->tgtid_bind_type) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003031 case FC_TGTID_BIND_BY_WWPN:
3032 rport->node_name = -1;
3033 rport->port_id = -1;
3034 break;
3035 case FC_TGTID_BIND_BY_WWNN:
3036 rport->port_name = -1;
3037 rport->port_id = -1;
3038 break;
3039 case FC_TGTID_BIND_BY_ID:
3040 rport->node_name = -1;
3041 rport->port_name = -1;
3042 break;
3043 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3044 break;
3045 }
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 /*
3048 * As this only occurs if the remote port (scsi target)
3049 * went away and didn't come back - we'll remove
3050 * all attached scsi devices.
3051 */
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003052 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04003053
3054 scsi_target_unblock(&rport->dev);
3055 fc_queue_work(shost, &rport->stgt_delete_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056}
3057
3058/**
Rob Landleyeb448202007-11-03 13:30:39 -05003059 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
David Howellsc4028952006-11-22 14:57:56 +00003060 * @work: rport to terminate io on.
James Smart0f29b962006-08-18 17:33:29 -04003061 *
3062 * Notes: Only requests the failure of the io, not that all are flushed
3063 * prior to returning.
Rob Landleyeb448202007-11-03 13:30:39 -05003064 */
James Smart0f29b962006-08-18 17:33:29 -04003065static void
David Howellsc4028952006-11-22 14:57:56 +00003066fc_timeout_fail_rport_io(struct work_struct *work)
James Smart0f29b962006-08-18 17:33:29 -04003067{
David Howellsc4028952006-11-22 14:57:56 +00003068 struct fc_rport *rport =
3069 container_of(work, struct fc_rport, fail_io_work.work);
James Smart0f29b962006-08-18 17:33:29 -04003070 struct Scsi_Host *shost = rport_to_shost(rport);
3071 struct fc_internal *i = to_fc_internal(shost->transportt);
3072
3073 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3074 return;
3075
3076 i->f->terminate_rport_io(rport);
3077}
3078
3079/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
David Howellsc4028952006-11-22 14:57:56 +00003081 * @work: remote port to be scanned.
Rob Landleyeb448202007-11-03 13:30:39 -05003082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083static void
David Howellsc4028952006-11-22 14:57:56 +00003084fc_scsi_scan_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
David Howellsc4028952006-11-22 14:57:56 +00003086 struct fc_rport *rport =
3087 container_of(work, struct fc_rport, scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003088 struct Scsi_Host *shost = rport_to_shost(rport);
Christof Schmitt03f002f2007-08-28 09:31:21 +02003089 struct fc_internal *i = to_fc_internal(shost->transportt);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003090 unsigned long flags;
3091
James Smartaedf3492006-04-10 10:14:05 -04003092 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
Christof Schmitt03f002f2007-08-28 09:31:21 +02003093 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3094 !(i->f->disable_target_scan)) {
James Smartaedf3492006-04-10 10:14:05 -04003095 scsi_scan_target(&rport->dev, rport->channel,
3096 rport->scsi_target_id, SCAN_WILD_CARD, 1);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003097 }
James Smartaedf3492006-04-10 10:14:05 -04003098
3099 spin_lock_irqsave(shost->host_lock, flags);
3100 rport->flags &= ~FC_RPORT_SCAN_PENDING;
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003101 spin_unlock_irqrestore(shost->host_lock, flags);
3102}
3103
3104
James Smarta53eb5e2007-04-27 12:41:09 -04003105/**
3106 * fc_vport_create - allocates and creates a FC virtual port.
3107 * @shost: scsi host the virtual port is connected to.
3108 * @channel: Channel on shost port connected to.
3109 * @pdev: parent device for vport
3110 * @ids: The world wide names, FC4 port roles, etc for
3111 * the virtual port.
3112 * @ret_vport: The pointer to the created vport.
3113 *
3114 * Allocates and creates the vport structure, calls the parent host
3115 * to instantiate the vport, the completes w/ class and sysfs creation.
3116 *
3117 * Notes:
3118 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003119 */
James Smarta53eb5e2007-04-27 12:41:09 -04003120static int
3121fc_vport_create(struct Scsi_Host *shost, int channel, struct device *pdev,
3122 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3123{
3124 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3125 struct fc_internal *fci = to_fc_internal(shost->transportt);
3126 struct fc_vport *vport;
3127 struct device *dev;
3128 unsigned long flags;
3129 size_t size;
3130 int error;
3131
3132 *ret_vport = NULL;
3133
3134 if ( ! fci->f->vport_create)
3135 return -ENOENT;
3136
3137 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3138 vport = kzalloc(size, GFP_KERNEL);
3139 if (unlikely(!vport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003140 printk(KERN_ERR "%s: allocation failure\n", __func__);
James Smarta53eb5e2007-04-27 12:41:09 -04003141 return -ENOMEM;
3142 }
3143
3144 vport->vport_state = FC_VPORT_UNKNOWN;
3145 vport->vport_last_state = FC_VPORT_UNKNOWN;
3146 vport->node_name = ids->node_name;
3147 vport->port_name = ids->port_name;
3148 vport->roles = ids->roles;
3149 vport->vport_type = ids->vport_type;
3150 if (fci->f->dd_fcvport_size)
3151 vport->dd_data = &vport[1];
3152 vport->shost = shost;
3153 vport->channel = channel;
3154 vport->flags = FC_VPORT_CREATING;
James Smart9ef3e4a2007-05-24 19:04:44 -04003155 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
James Smarta53eb5e2007-04-27 12:41:09 -04003156
3157 spin_lock_irqsave(shost->host_lock, flags);
3158
3159 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3160 spin_unlock_irqrestore(shost->host_lock, flags);
3161 kfree(vport);
3162 return -ENOSPC;
3163 }
3164 fc_host->npiv_vports_inuse++;
3165 vport->number = fc_host->next_vport_number++;
3166 list_add_tail(&vport->peers, &fc_host->vports);
3167 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3168
3169 spin_unlock_irqrestore(shost->host_lock, flags);
3170
3171 dev = &vport->dev;
3172 device_initialize(dev); /* takes self reference */
3173 dev->parent = get_device(pdev); /* takes parent reference */
3174 dev->release = fc_vport_dev_release;
3175 sprintf(dev->bus_id, "vport-%d:%d-%d",
3176 shost->host_no, channel, vport->number);
3177 transport_setup_device(dev);
3178
3179 error = device_add(dev);
3180 if (error) {
3181 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3182 goto delete_vport;
3183 }
3184 transport_add_device(dev);
3185 transport_configure_device(dev);
3186
3187 error = fci->f->vport_create(vport, ids->disable);
3188 if (error) {
3189 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3190 goto delete_vport_all;
3191 }
3192
3193 /*
3194 * if the parent isn't the physical adapter's Scsi_Host, ensure
3195 * the Scsi_Host at least contains ia symlink to the vport.
3196 */
3197 if (pdev != &shost->shost_gendev) {
3198 error = sysfs_create_link(&shost->shost_gendev.kobj,
3199 &dev->kobj, dev->bus_id);
3200 if (error)
3201 printk(KERN_ERR
3202 "%s: Cannot create vport symlinks for "
3203 "%s, err=%d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003204 __func__, dev->bus_id, error);
James Smarta53eb5e2007-04-27 12:41:09 -04003205 }
3206 spin_lock_irqsave(shost->host_lock, flags);
3207 vport->flags &= ~FC_VPORT_CREATING;
3208 spin_unlock_irqrestore(shost->host_lock, flags);
3209
3210 dev_printk(KERN_NOTICE, pdev,
3211 "%s created via shost%d channel %d\n", dev->bus_id,
3212 shost->host_no, channel);
3213
3214 *ret_vport = vport;
3215
3216 return 0;
3217
3218delete_vport_all:
3219 transport_remove_device(dev);
3220 device_del(dev);
3221delete_vport:
3222 transport_destroy_device(dev);
3223 spin_lock_irqsave(shost->host_lock, flags);
3224 list_del(&vport->peers);
3225 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3226 fc_host->npiv_vports_inuse--;
3227 spin_unlock_irqrestore(shost->host_lock, flags);
3228 put_device(dev->parent);
3229 kfree(vport);
3230
3231 return error;
3232}
3233
3234
3235/**
3236 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3237 * @vport: fc_vport to be terminated
3238 *
3239 * Calls the LLDD vport_delete() function, then deallocates and removes
3240 * the vport from the shost and object tree.
3241 *
3242 * Notes:
3243 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003244 */
James Smarta53eb5e2007-04-27 12:41:09 -04003245int
3246fc_vport_terminate(struct fc_vport *vport)
3247{
3248 struct Scsi_Host *shost = vport_to_shost(vport);
3249 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3250 struct fc_internal *i = to_fc_internal(shost->transportt);
3251 struct device *dev = &vport->dev;
3252 unsigned long flags;
3253 int stat;
3254
3255 spin_lock_irqsave(shost->host_lock, flags);
3256 if (vport->flags & FC_VPORT_CREATING) {
3257 spin_unlock_irqrestore(shost->host_lock, flags);
3258 return -EBUSY;
3259 }
3260 if (vport->flags & (FC_VPORT_DEL)) {
3261 spin_unlock_irqrestore(shost->host_lock, flags);
3262 return -EALREADY;
3263 }
3264 vport->flags |= FC_VPORT_DELETING;
3265 spin_unlock_irqrestore(shost->host_lock, flags);
3266
3267 if (i->f->vport_delete)
3268 stat = i->f->vport_delete(vport);
3269 else
3270 stat = -ENOENT;
3271
3272 spin_lock_irqsave(shost->host_lock, flags);
3273 vport->flags &= ~FC_VPORT_DELETING;
3274 if (!stat) {
3275 vport->flags |= FC_VPORT_DELETED;
3276 list_del(&vport->peers);
3277 fc_host->npiv_vports_inuse--;
3278 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3279 }
3280 spin_unlock_irqrestore(shost->host_lock, flags);
3281
3282 if (stat)
3283 return stat;
3284
3285 if (dev->parent != &shost->shost_gendev)
3286 sysfs_remove_link(&shost->shost_gendev.kobj, dev->bus_id);
3287 transport_remove_device(dev);
3288 device_del(dev);
3289 transport_destroy_device(dev);
3290
3291 /*
3292 * Removing our self-reference should mean our
3293 * release function gets called, which will drop the remaining
3294 * parent reference and free the data structure.
3295 */
3296 put_device(dev); /* for self-reference */
3297
3298 return 0; /* SUCCESS */
3299}
3300EXPORT_SYMBOL(fc_vport_terminate);
3301
James Smart9ef3e4a2007-05-24 19:04:44 -04003302/**
3303 * fc_vport_sched_delete - workq-based delete request for a vport
James Smart9ef3e4a2007-05-24 19:04:44 -04003304 * @work: vport to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05003305 */
James Smart9ef3e4a2007-05-24 19:04:44 -04003306static void
3307fc_vport_sched_delete(struct work_struct *work)
3308{
3309 struct fc_vport *vport =
3310 container_of(work, struct fc_vport, vport_delete_work);
3311 int stat;
James Smarta53eb5e2007-04-27 12:41:09 -04003312
James Smart9ef3e4a2007-05-24 19:04:44 -04003313 stat = fc_vport_terminate(vport);
3314 if (stat)
3315 dev_printk(KERN_ERR, vport->dev.parent,
3316 "%s: %s could not be deleted created via "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003317 "shost%d channel %d - error %d\n", __func__,
James Smart9ef3e4a2007-05-24 19:04:44 -04003318 vport->dev.bus_id, vport->shost->host_no,
3319 vport->channel, stat);
3320}
3321
3322
3323/* Original Author: Martin Hicks */
3324MODULE_AUTHOR("James Smart");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325MODULE_DESCRIPTION("FC Transport Attributes");
3326MODULE_LICENSE("GPL");
3327
3328module_init(fc_transport_init);
3329module_exit(fc_transport_exit);