blob: 5fd64e70029dcd30d4c30c8c9dffd5a0f77648a6 [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
James Smartaedf3492006-04-10 10:14:05 -0400420 snprintf(fc_host->work_q_name, KOBJ_NAME_LEN, "fc_wq_%d",
421 shost->host_no);
422 fc_host->work_q = create_singlethread_workqueue(
423 fc_host->work_q_name);
424 if (!fc_host->work_q)
425 return -ENOMEM;
426
427 snprintf(fc_host->devloss_work_q_name, KOBJ_NAME_LEN, "fc_dl_%d",
428 shost->host_no);
429 fc_host->devloss_work_q = create_singlethread_workqueue(
430 fc_host->devloss_work_q_name);
431 if (!fc_host->devloss_work_q) {
432 destroy_workqueue(fc_host->work_q);
433 fc_host->work_q = NULL;
434 return -ENOMEM;
435 }
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
438}
439
440static DECLARE_TRANSPORT_CLASS(fc_host_class,
441 "fc_host",
442 fc_host_setup,
443 NULL,
444 NULL);
445
446/*
447 * Setup and Remove actions for remote ports are handled
448 * in the service functions below.
449 */
450static DECLARE_TRANSPORT_CLASS(fc_rport_class,
451 "fc_remote_ports",
452 NULL,
453 NULL,
454 NULL);
455
456/*
James Smarta53eb5e2007-04-27 12:41:09 -0400457 * Setup and Remove actions for virtual ports are handled
458 * in the service functions below.
459 */
460static DECLARE_TRANSPORT_CLASS(fc_vport_class,
461 "fc_vports",
462 NULL,
463 NULL,
464 NULL);
465
466/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * Module Parameters
468 */
469
470/*
471 * dev_loss_tmo: the default number of seconds that the FC transport
472 * should insulate the loss of a remote port.
473 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
474 */
James Smart1c9e16e2006-05-16 16:13:36 -0400475static unsigned int fc_dev_loss_tmo = 60; /* seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Masatake YAMATO10f4b892007-09-19 22:59:16 +0900477module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478MODULE_PARM_DESC(dev_loss_tmo,
479 "Maximum number of seconds that the FC transport should"
480 " insulate the loss of a remote port. Once this value is"
481 " exceeded, the scsi target is removed. Value should be"
482 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
483
Rob Landleyeb448202007-11-03 13:30:39 -0500484/*
James Smart84314fd2006-08-18 17:30:09 -0400485 * Netlink Infrastructure
Rob Landleyeb448202007-11-03 13:30:39 -0500486 */
James Smart84314fd2006-08-18 17:30:09 -0400487
488static atomic_t fc_event_seq;
489
490/**
491 * fc_get_event_number - Obtain the next sequential FC event number
492 *
493 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -0500494 * We could have inlined this, but it would have required fc_event_seq to
James Smart84314fd2006-08-18 17:30:09 -0400495 * be exposed. For now, live with the subroutine call.
496 * Atomic used to avoid lock/unlock...
Rob Landleyeb448202007-11-03 13:30:39 -0500497 */
James Smart84314fd2006-08-18 17:30:09 -0400498u32
499fc_get_event_number(void)
500{
501 return atomic_add_return(1, &fc_event_seq);
502}
503EXPORT_SYMBOL(fc_get_event_number);
504
505
506/**
507 * fc_host_post_event - called to post an even on an fc_host.
James Smart84314fd2006-08-18 17:30:09 -0400508 * @shost: host the event occurred on
509 * @event_number: fc event number obtained from get_fc_event_number()
510 * @event_code: fc_host event being posted
511 * @event_data: 32bits of data for the event being posted
512 *
513 * Notes:
514 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500515 */
James Smart84314fd2006-08-18 17:30:09 -0400516void
517fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
518 enum fc_host_event_code event_code, u32 event_data)
519{
520 struct sk_buff *skb;
521 struct nlmsghdr *nlh;
522 struct fc_nl_event *event;
523 const char *name;
524 u32 len, skblen;
525 int err;
526
527 if (!scsi_nl_sock) {
528 err = -ENOENT;
529 goto send_fail;
530 }
531
532 len = FC_NL_MSGALIGN(sizeof(*event));
533 skblen = NLMSG_SPACE(len);
534
535 skb = alloc_skb(skblen, GFP_KERNEL);
536 if (!skb) {
537 err = -ENOBUFS;
538 goto send_fail;
539 }
540
541 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
542 skblen - sizeof(*nlh), 0);
543 if (!nlh) {
544 err = -ENOBUFS;
545 goto send_fail_skb;
546 }
547 event = NLMSG_DATA(nlh);
548
549 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
550 FC_NL_ASYNC_EVENT, len);
551 event->seconds = get_seconds();
552 event->vendor_id = 0;
553 event->host_no = shost->host_no;
554 event->event_datalen = sizeof(u32); /* bytes */
555 event->event_num = event_number;
556 event->event_code = event_code;
557 event->event_data = event_data;
558
James Bottomley1b73c4b2006-09-23 22:07:20 -0500559 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
560 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400561 if (err && (err != -ESRCH)) /* filter no recipient errors */
562 /* nlmsg_multicast already kfree_skb'd */
563 goto send_fail;
564
565 return;
566
567send_fail_skb:
568 kfree_skb(skb);
569send_fail:
570 name = get_fc_host_event_code_name(event_code);
571 printk(KERN_WARNING
572 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
573 __FUNCTION__, shost->host_no,
574 (name) ? name : "<unknown>", event_data, err);
575 return;
576}
577EXPORT_SYMBOL(fc_host_post_event);
578
579
580/**
Rob Landleyeb448202007-11-03 13:30:39 -0500581 * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
James Smart84314fd2006-08-18 17:30:09 -0400582 * @shost: host the event occurred on
583 * @event_number: fc event number obtained from get_fc_event_number()
584 * @data_len: amount, in bytes, of vendor unique data
585 * @data_buf: pointer to vendor unique data
Rob Landleyeb448202007-11-03 13:30:39 -0500586 * @vendor_id: Vendor id
James Smart84314fd2006-08-18 17:30:09 -0400587 *
588 * Notes:
589 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500590 */
James Smart84314fd2006-08-18 17:30:09 -0400591void
592fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
James Smartf14e2e22006-08-22 09:55:23 -0400593 u32 data_len, char * data_buf, u64 vendor_id)
James Smart84314fd2006-08-18 17:30:09 -0400594{
595 struct sk_buff *skb;
596 struct nlmsghdr *nlh;
597 struct fc_nl_event *event;
598 u32 len, skblen;
599 int err;
600
601 if (!scsi_nl_sock) {
602 err = -ENOENT;
603 goto send_vendor_fail;
604 }
605
606 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
607 skblen = NLMSG_SPACE(len);
608
609 skb = alloc_skb(skblen, GFP_KERNEL);
610 if (!skb) {
611 err = -ENOBUFS;
612 goto send_vendor_fail;
613 }
614
615 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
616 skblen - sizeof(*nlh), 0);
617 if (!nlh) {
618 err = -ENOBUFS;
619 goto send_vendor_fail_skb;
620 }
621 event = NLMSG_DATA(nlh);
622
623 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
624 FC_NL_ASYNC_EVENT, len);
625 event->seconds = get_seconds();
626 event->vendor_id = vendor_id;
627 event->host_no = shost->host_no;
628 event->event_datalen = data_len; /* bytes */
629 event->event_num = event_number;
630 event->event_code = FCH_EVT_VENDOR_UNIQUE;
631 memcpy(&event->event_data, data_buf, data_len);
632
James Bottomley1b73c4b2006-09-23 22:07:20 -0500633 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
634 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400635 if (err && (err != -ESRCH)) /* filter no recipient errors */
636 /* nlmsg_multicast already kfree_skb'd */
637 goto send_vendor_fail;
638
639 return;
640
641send_vendor_fail_skb:
642 kfree_skb(skb);
643send_vendor_fail:
644 printk(KERN_WARNING
645 "%s: Dropped Event : host %d vendor_unique - err %d\n",
646 __FUNCTION__, shost->host_no, err);
647 return;
648}
649EXPORT_SYMBOL(fc_host_post_vendor_event);
650
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653static __init int fc_transport_init(void)
654{
James Smart84314fd2006-08-18 17:30:09 -0400655 int error;
656
657 atomic_set(&fc_event_seq, 0);
658
659 error = transport_class_register(&fc_host_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (error)
661 return error;
James Smarta53eb5e2007-04-27 12:41:09 -0400662 error = transport_class_register(&fc_vport_class);
663 if (error)
664 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 error = transport_class_register(&fc_rport_class);
666 if (error)
667 return error;
668 return transport_class_register(&fc_transport_class);
669}
670
671static void __exit fc_transport_exit(void)
672{
673 transport_class_unregister(&fc_transport_class);
674 transport_class_unregister(&fc_rport_class);
675 transport_class_unregister(&fc_host_class);
James Smarta53eb5e2007-04-27 12:41:09 -0400676 transport_class_unregister(&fc_vport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679/*
680 * FC Remote Port Attribute Management
681 */
682
683#define fc_rport_show_function(field, format_string, sz, cast) \
684static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100685show_fc_rport_##field (struct device *dev, \
686 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100688 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct Scsi_Host *shost = rport_to_shost(rport); \
690 struct fc_internal *i = to_fc_internal(shost->transportt); \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400691 if ((i->f->get_rport_##field) && \
692 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500693 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400694 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 i->f->get_rport_##field(rport); \
696 return snprintf(buf, sz, format_string, cast rport->field); \
697}
698
699#define fc_rport_store_function(field) \
700static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100701store_fc_rport_##field(struct device *dev, \
702 struct device_attribute *attr, \
703 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{ \
705 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100706 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct Scsi_Host *shost = rport_to_shost(rport); \
708 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -0400709 char *cp; \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400710 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500711 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400712 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
713 return -EBUSY; \
James Smart0f29b962006-08-18 17:33:29 -0400714 val = simple_strtoul(buf, &cp, 0); \
715 if (*cp && (*cp != '\n')) \
716 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 i->f->set_rport_##field(rport, val); \
718 return count; \
719}
720
721#define fc_rport_rd_attr(field, format_string, sz) \
722 fc_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100723static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 show_fc_rport_##field, NULL)
725
726#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
727 fc_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100728static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 show_fc_rport_##field, NULL)
730
731#define fc_rport_rw_attr(field, format_string, sz) \
732 fc_rport_show_function(field, format_string, sz, ) \
733 fc_rport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100734static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 show_fc_rport_##field, \
736 store_fc_rport_##field)
737
738
739#define fc_private_rport_show_function(field, format_string, sz, cast) \
740static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100741show_fc_rport_##field (struct device *dev, \
742 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100744 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return snprintf(buf, sz, format_string, cast rport->field); \
746}
747
748#define fc_private_rport_rd_attr(field, format_string, sz) \
749 fc_private_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100750static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 show_fc_rport_##field, NULL)
752
753#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
754 fc_private_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100755static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 show_fc_rport_##field, NULL)
757
758
759#define fc_private_rport_rd_enum_attr(title, maxlen) \
760static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100761show_fc_rport_##title (struct device *dev, \
762 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100764 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 const char *name; \
766 name = get_fc_##title##_name(rport->title); \
767 if (!name) \
768 return -EINVAL; \
769 return snprintf(buf, maxlen, "%s\n", name); \
770} \
Tony Jonesee959b02008-02-22 00:13:36 +0100771static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 show_fc_rport_##title, NULL)
773
774
775#define SETUP_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100776 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
778 i->private_rport_attrs[count].store = NULL; \
779 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
780 if (i->f->show_rport_##field) \
781 count++
782
783#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100784 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
786 i->private_rport_attrs[count].store = NULL; \
787 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
788 count++
789
790#define SETUP_RPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100791 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!i->f->set_rport_##field) { \
793 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
794 i->private_rport_attrs[count].store = NULL; \
795 } \
796 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
797 if (i->f->show_rport_##field) \
798 count++
799
James Smart0f29b962006-08-18 17:33:29 -0400800#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
801{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100802 i->private_rport_attrs[count] = device_attr_rport_##field; \
James Smart0f29b962006-08-18 17:33:29 -0400803 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
804 count++; \
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808/* The FC Transport Remote Port Attributes: */
809
810/* Fixed Remote Port Attributes */
811
812fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
813
814static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100815show_fc_rport_supported_classes (struct device *dev,
816 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Tony Jonesee959b02008-02-22 00:13:36 +0100818 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (rport->supported_classes == FC_COS_UNSPECIFIED)
820 return snprintf(buf, 20, "unspecified\n");
821 return get_fc_cos_names(rport->supported_classes, buf);
822}
Tony Jonesee959b02008-02-22 00:13:36 +0100823static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 show_fc_rport_supported_classes, NULL);
825
826/* Dynamic Remote Port Attributes */
827
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400828/*
829 * dev_loss_tmo attribute
830 */
831fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
832static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100833store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
834 const char *buf, size_t count)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400835{
836 int val;
Tony Jonesee959b02008-02-22 00:13:36 +0100837 struct fc_rport *rport = transport_class_to_rport(dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400838 struct Scsi_Host *shost = rport_to_shost(rport);
839 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart0f29b962006-08-18 17:33:29 -0400840 char *cp;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400841 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500842 (rport->port_state == FC_PORTSTATE_DELETED) ||
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400843 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
844 return -EBUSY;
James Smart0f29b962006-08-18 17:33:29 -0400845 val = simple_strtoul(buf, &cp, 0);
846 if ((*cp && (*cp != '\n')) ||
847 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400848 return -EINVAL;
849 i->f->set_rport_dev_loss_tmo(rport, val);
850 return count;
851}
Tony Jonesee959b02008-02-22 00:13:36 +0100852static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400853 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855
856/* Private Remote Port Attributes */
857
858fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
859fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
860fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
861
862static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100863show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
864 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Tony Jonesee959b02008-02-22 00:13:36 +0100866 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 /* identify any roles that are port_id specific */
869 if ((rport->port_id != -1) &&
870 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
871 FC_WELLKNOWN_PORTID_MASK) {
872 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
873 case FC_FPORT_PORTID:
874 return snprintf(buf, 30, "Fabric Port\n");
875 case FC_FABCTLR_PORTID:
876 return snprintf(buf, 30, "Fabric Controller\n");
877 case FC_DIRSRVR_PORTID:
878 return snprintf(buf, 30, "Directory Server\n");
879 case FC_TIMESRVR_PORTID:
880 return snprintf(buf, 30, "Time Server\n");
881 case FC_MGMTSRVR_PORTID:
882 return snprintf(buf, 30, "Management Server\n");
883 default:
884 return snprintf(buf, 30, "Unknown Fabric Entity\n");
885 }
886 } else {
James Smarta53eb5e2007-04-27 12:41:09 -0400887 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return snprintf(buf, 20, "unknown\n");
James Smarta53eb5e2007-04-27 12:41:09 -0400889 return get_fc_port_roles_names(rport->roles, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891}
Tony Jonesee959b02008-02-22 00:13:36 +0100892static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 show_fc_rport_roles, NULL);
894
895fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
896fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
897
James Smart0f29b962006-08-18 17:33:29 -0400898/*
899 * fast_io_fail_tmo attribute
900 */
901static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100902show_fc_rport_fast_io_fail_tmo (struct device *dev,
903 struct device_attribute *attr, char *buf)
James Smart0f29b962006-08-18 17:33:29 -0400904{
Tony Jonesee959b02008-02-22 00:13:36 +0100905 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400906
907 if (rport->fast_io_fail_tmo == -1)
908 return snprintf(buf, 5, "off\n");
909 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
910}
911
912static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100913store_fc_rport_fast_io_fail_tmo(struct device *dev,
914 struct device_attribute *attr, const char *buf,
915 size_t count)
James Smart0f29b962006-08-18 17:33:29 -0400916{
917 int val;
918 char *cp;
Tony Jonesee959b02008-02-22 00:13:36 +0100919 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400920
921 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
922 (rport->port_state == FC_PORTSTATE_DELETED) ||
923 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
924 return -EBUSY;
925 if (strncmp(buf, "off", 3) == 0)
926 rport->fast_io_fail_tmo = -1;
927 else {
928 val = simple_strtoul(buf, &cp, 0);
929 if ((*cp && (*cp != '\n')) ||
930 (val < 0) || (val >= rport->dev_loss_tmo))
931 return -EINVAL;
932 rport->fast_io_fail_tmo = val;
933 }
934 return count;
935}
Tony Jonesee959b02008-02-22 00:13:36 +0100936static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
James Smart0f29b962006-08-18 17:33:29 -0400937 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939
940/*
941 * FC SCSI Target Attribute Management
942 */
943
944/*
945 * Note: in the target show function we recognize when the remote
James Smarta53eb5e2007-04-27 12:41:09 -0400946 * port is in the heirarchy and do not allow the driver to get
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 * involved in sysfs functions. The driver only gets involved if
948 * it's the "old" style that doesn't use rports.
949 */
950#define fc_starget_show_function(field, format_string, sz, cast) \
951static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100952show_fc_starget_##field (struct device *dev, \
953 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100955 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
957 struct fc_internal *i = to_fc_internal(shost->transportt); \
958 struct fc_rport *rport = starget_to_rport(starget); \
959 if (rport) \
960 fc_starget_##field(starget) = rport->field; \
961 else if (i->f->get_starget_##field) \
962 i->f->get_starget_##field(starget); \
963 return snprintf(buf, sz, format_string, \
964 cast fc_starget_##field(starget)); \
965}
966
967#define fc_starget_rd_attr(field, format_string, sz) \
968 fc_starget_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100969static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 show_fc_starget_##field, NULL)
971
972#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
973 fc_starget_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100974static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 show_fc_starget_##field, NULL)
976
977#define SETUP_STARGET_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100978 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
980 i->private_starget_attrs[count].store = NULL; \
981 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
982 if (i->f->show_starget_##field) \
983 count++
984
985#define SETUP_STARGET_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100986 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (!i->f->set_starget_##field) { \
988 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
989 i->private_starget_attrs[count].store = NULL; \
990 } \
991 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
992 if (i->f->show_starget_##field) \
993 count++
994
995/* The FC Transport SCSI Target Attributes: */
996fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
997fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
998fc_starget_rd_attr(port_id, "0x%06x\n", 20);
999
1000
1001/*
James Smarta53eb5e2007-04-27 12:41:09 -04001002 * FC Virtual Port Attribute Management
1003 */
1004
1005#define fc_vport_show_function(field, format_string, sz, cast) \
1006static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001007show_fc_vport_##field (struct device *dev, \
1008 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001009{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001010 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001011 struct Scsi_Host *shost = vport_to_shost(vport); \
1012 struct fc_internal *i = to_fc_internal(shost->transportt); \
1013 if ((i->f->get_vport_##field) && \
1014 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1015 i->f->get_vport_##field(vport); \
1016 return snprintf(buf, sz, format_string, cast vport->field); \
1017}
1018
1019#define fc_vport_store_function(field) \
1020static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001021store_fc_vport_##field(struct device *dev, \
1022 struct device_attribute *attr, \
1023 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001024{ \
1025 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001026 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001027 struct Scsi_Host *shost = vport_to_shost(vport); \
1028 struct fc_internal *i = to_fc_internal(shost->transportt); \
1029 char *cp; \
1030 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1031 return -EBUSY; \
1032 val = simple_strtoul(buf, &cp, 0); \
1033 if (*cp && (*cp != '\n')) \
1034 return -EINVAL; \
1035 i->f->set_vport_##field(vport, val); \
1036 return count; \
1037}
1038
1039#define fc_vport_store_str_function(field, slen) \
1040static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001041store_fc_vport_##field(struct device *dev, \
1042 struct device_attribute *attr, \
1043 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001044{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001045 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001046 struct Scsi_Host *shost = vport_to_shost(vport); \
1047 struct fc_internal *i = to_fc_internal(shost->transportt); \
1048 unsigned int cnt=count; \
1049 \
1050 /* count may include a LF at end of string */ \
1051 if (buf[cnt-1] == '\n') \
1052 cnt--; \
1053 if (cnt > ((slen) - 1)) \
1054 return -EINVAL; \
1055 memcpy(vport->field, buf, cnt); \
1056 i->f->set_vport_##field(vport); \
1057 return count; \
1058}
1059
1060#define fc_vport_rd_attr(field, format_string, sz) \
1061 fc_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001062static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001063 show_fc_vport_##field, NULL)
1064
1065#define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1066 fc_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001067static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001068 show_fc_vport_##field, NULL)
1069
1070#define fc_vport_rw_attr(field, format_string, sz) \
1071 fc_vport_show_function(field, format_string, sz, ) \
1072 fc_vport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001073static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001074 show_fc_vport_##field, \
1075 store_fc_vport_##field)
1076
1077#define fc_private_vport_show_function(field, format_string, sz, cast) \
1078static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001079show_fc_vport_##field (struct device *dev, \
1080 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001081{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001082 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001083 return snprintf(buf, sz, format_string, cast vport->field); \
1084}
1085
1086#define fc_private_vport_store_u32_function(field) \
1087static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001088store_fc_vport_##field(struct device *dev, \
1089 struct device_attribute *attr, \
1090 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001091{ \
1092 u32 val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001093 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001094 char *cp; \
1095 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1096 return -EBUSY; \
1097 val = simple_strtoul(buf, &cp, 0); \
1098 if (*cp && (*cp != '\n')) \
1099 return -EINVAL; \
1100 vport->field = val; \
1101 return count; \
1102}
1103
1104
1105#define fc_private_vport_rd_attr(field, format_string, sz) \
1106 fc_private_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001107static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001108 show_fc_vport_##field, NULL)
1109
1110#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1111 fc_private_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001112static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001113 show_fc_vport_##field, NULL)
1114
1115#define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1116 fc_private_vport_show_function(field, format_string, sz, ) \
1117 fc_private_vport_store_u32_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001118static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001119 show_fc_vport_##field, \
1120 store_fc_vport_##field)
1121
1122
1123#define fc_private_vport_rd_enum_attr(title, maxlen) \
1124static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001125show_fc_vport_##title (struct device *dev, \
1126 struct device_attribute *attr, \
1127 char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001128{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001129 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001130 const char *name; \
1131 name = get_fc_##title##_name(vport->title); \
1132 if (!name) \
1133 return -EINVAL; \
1134 return snprintf(buf, maxlen, "%s\n", name); \
1135} \
Tony Jonesee959b02008-02-22 00:13:36 +01001136static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001137 show_fc_vport_##title, NULL)
1138
1139
1140#define SETUP_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001141 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001142 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1143 i->private_vport_attrs[count].store = NULL; \
1144 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1145 if (i->f->get_##field) \
1146 count++
1147 /* NOTE: Above MACRO differs: checks function not show bit */
1148
1149#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001150 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001151 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1152 i->private_vport_attrs[count].store = NULL; \
1153 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1154 count++
1155
1156#define SETUP_VPORT_ATTRIBUTE_WR(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001157 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001158 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1159 if (i->f->field) \
1160 count++
1161 /* NOTE: Above MACRO differs: checks function */
1162
1163#define SETUP_VPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001164 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001165 if (!i->f->set_vport_##field) { \
1166 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1167 i->private_vport_attrs[count].store = NULL; \
1168 } \
1169 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1170 count++
1171 /* NOTE: Above MACRO differs: does not check show bit */
1172
1173#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1174{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001175 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001176 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1177 count++; \
1178}
1179
1180
1181/* The FC Transport Virtual Port Attributes: */
1182
1183/* Fixed Virtual Port Attributes */
1184
1185/* Dynamic Virtual Port Attributes */
1186
1187/* Private Virtual Port Attributes */
1188
1189fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1190fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1191fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1192fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1193
1194static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001195show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1196 char *buf)
James Smarta53eb5e2007-04-27 12:41:09 -04001197{
Tony Jonesee959b02008-02-22 00:13:36 +01001198 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001199
1200 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1201 return snprintf(buf, 20, "unknown\n");
1202 return get_fc_port_roles_names(vport->roles, buf);
1203}
Tony Jonesee959b02008-02-22 00:13:36 +01001204static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
James Smarta53eb5e2007-04-27 12:41:09 -04001205
1206fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1207
1208fc_private_vport_show_function(symbolic_name, "%s\n",
1209 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1210fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
Tony Jonesee959b02008-02-22 00:13:36 +01001211static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001212 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1213
1214static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001215store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1216 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001217{
Tony Jonesee959b02008-02-22 00:13:36 +01001218 struct fc_vport *vport = transport_class_to_vport(dev);
James Smart9ef3e4a2007-05-24 19:04:44 -04001219 struct Scsi_Host *shost = vport_to_shost(vport);
James Smarta53eb5e2007-04-27 12:41:09 -04001220
James Smart9ef3e4a2007-05-24 19:04:44 -04001221 fc_queue_work(shost, &vport->vport_delete_work);
James Smarta53eb5e2007-04-27 12:41:09 -04001222 return count;
1223}
Tony Jonesee959b02008-02-22 00:13:36 +01001224static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001225 NULL, store_fc_vport_delete);
1226
1227
1228/*
1229 * Enable/Disable vport
1230 * Write "1" to disable, write "0" to enable
1231 */
1232static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001233store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1234 const char *buf,
James Smarta53eb5e2007-04-27 12:41:09 -04001235 size_t count)
1236{
Tony Jonesee959b02008-02-22 00:13:36 +01001237 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001238 struct Scsi_Host *shost = vport_to_shost(vport);
1239 struct fc_internal *i = to_fc_internal(shost->transportt);
1240 int stat;
1241
1242 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1243 return -EBUSY;
1244
1245 if (*buf == '0') {
1246 if (vport->vport_state != FC_VPORT_DISABLED)
1247 return -EALREADY;
1248 } else if (*buf == '1') {
1249 if (vport->vport_state == FC_VPORT_DISABLED)
1250 return -EALREADY;
1251 } else
1252 return -EINVAL;
1253
1254 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1255 return stat ? stat : count;
1256}
Tony Jonesee959b02008-02-22 00:13:36 +01001257static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001258 NULL, store_fc_vport_disable);
1259
1260
1261/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * Host Attribute Management
1263 */
1264
1265#define fc_host_show_function(field, format_string, sz, cast) \
1266static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001267show_fc_host_##field (struct device *dev, \
1268 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001270 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct fc_internal *i = to_fc_internal(shost->transportt); \
1272 if (i->f->get_host_##field) \
1273 i->f->get_host_##field(shost); \
1274 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1275}
1276
1277#define fc_host_store_function(field) \
1278static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001279store_fc_host_##field(struct device *dev, \
1280 struct device_attribute *attr, \
1281 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{ \
1283 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001284 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -04001286 char *cp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 \
James Smart0f29b962006-08-18 17:33:29 -04001288 val = simple_strtoul(buf, &cp, 0); \
1289 if (*cp && (*cp != '\n')) \
1290 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 i->f->set_host_##field(shost, val); \
1292 return count; \
1293}
1294
James Smartb8d08212006-08-17 08:00:43 -04001295#define fc_host_store_str_function(field, slen) \
1296static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001297store_fc_host_##field(struct device *dev, \
1298 struct device_attribute *attr, \
1299 const char *buf, size_t count) \
James Smartb8d08212006-08-17 08:00:43 -04001300{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001301 struct Scsi_Host *shost = transport_class_to_shost(dev); \
James Smartb8d08212006-08-17 08:00:43 -04001302 struct fc_internal *i = to_fc_internal(shost->transportt); \
1303 unsigned int cnt=count; \
1304 \
1305 /* count may include a LF at end of string */ \
1306 if (buf[cnt-1] == '\n') \
1307 cnt--; \
1308 if (cnt > ((slen) - 1)) \
1309 return -EINVAL; \
1310 memcpy(fc_host_##field(shost), buf, cnt); \
1311 i->f->set_host_##field(shost); \
1312 return count; \
1313}
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315#define fc_host_rd_attr(field, format_string, sz) \
1316 fc_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001317static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 show_fc_host_##field, NULL)
1319
1320#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1321 fc_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001322static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 show_fc_host_##field, NULL)
1324
1325#define fc_host_rw_attr(field, format_string, sz) \
1326 fc_host_show_function(field, format_string, sz, ) \
1327 fc_host_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001328static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 show_fc_host_##field, \
1330 store_fc_host_##field)
1331
1332#define fc_host_rd_enum_attr(title, maxlen) \
1333static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001334show_fc_host_##title (struct device *dev, \
1335 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001337 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 struct fc_internal *i = to_fc_internal(shost->transportt); \
1339 const char *name; \
1340 if (i->f->get_host_##title) \
1341 i->f->get_host_##title(shost); \
1342 name = get_fc_##title##_name(fc_host_##title(shost)); \
1343 if (!name) \
1344 return -EINVAL; \
1345 return snprintf(buf, maxlen, "%s\n", name); \
1346} \
Tony Jonesee959b02008-02-22 00:13:36 +01001347static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349#define SETUP_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001350 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1352 i->private_host_attrs[count].store = NULL; \
1353 i->host_attrs[count] = &i->private_host_attrs[count]; \
1354 if (i->f->show_host_##field) \
1355 count++
1356
James Smarta53eb5e2007-04-27 12:41:09 -04001357#define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001358 i->private_host_attrs[count] = device_attr_host_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001359 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1360 i->private_host_attrs[count].store = NULL; \
1361 i->host_attrs[count] = &i->private_host_attrs[count]; \
1362 count++
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364#define SETUP_HOST_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001365 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (!i->f->set_host_##field) { \
1367 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1368 i->private_host_attrs[count].store = NULL; \
1369 } \
1370 i->host_attrs[count] = &i->private_host_attrs[count]; \
1371 if (i->f->show_host_##field) \
1372 count++
1373
1374
1375#define fc_private_host_show_function(field, format_string, sz, cast) \
1376static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001377show_fc_host_##field (struct device *dev, \
1378 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001380 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1382}
1383
1384#define fc_private_host_rd_attr(field, format_string, sz) \
1385 fc_private_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001386static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 show_fc_host_##field, NULL)
1388
1389#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1390 fc_private_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001391static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 show_fc_host_##field, NULL)
1393
1394#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001395 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1397 i->private_host_attrs[count].store = NULL; \
1398 i->host_attrs[count] = &i->private_host_attrs[count]; \
1399 count++
1400
1401#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001402{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001403 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 i->host_attrs[count] = &i->private_host_attrs[count]; \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001405 count++; \
1406}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408
1409/* Fixed Host Attributes */
1410
1411static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001412show_fc_host_supported_classes (struct device *dev,
1413 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Tony Jonesee959b02008-02-22 00:13:36 +01001415 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1418 return snprintf(buf, 20, "unspecified\n");
1419
1420 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1421}
Tony Jonesee959b02008-02-22 00:13:36 +01001422static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 show_fc_host_supported_classes, NULL);
1424
1425static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001426show_fc_host_supported_fc4s (struct device *dev,
1427 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Tony Jonesee959b02008-02-22 00:13:36 +01001429 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1431}
Tony Jonesee959b02008-02-22 00:13:36 +01001432static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 show_fc_host_supported_fc4s, NULL);
1434
1435static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001436show_fc_host_supported_speeds (struct device *dev,
1437 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Tony Jonesee959b02008-02-22 00:13:36 +01001439 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1442 return snprintf(buf, 20, "unknown\n");
1443
1444 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1445}
Tony Jonesee959b02008-02-22 00:13:36 +01001446static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 show_fc_host_supported_speeds, NULL);
1448
1449
1450fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1451fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01001452fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1453 unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
James Smarta53eb5e2007-04-27 12:41:09 -04001455fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1457
1458
1459/* Dynamic Host Attributes */
1460
1461static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001462show_fc_host_active_fc4s (struct device *dev,
1463 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Tony Jonesee959b02008-02-22 00:13:36 +01001465 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct fc_internal *i = to_fc_internal(shost->transportt);
1467
1468 if (i->f->get_host_active_fc4s)
1469 i->f->get_host_active_fc4s(shost);
1470
1471 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1472}
Tony Jonesee959b02008-02-22 00:13:36 +01001473static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 show_fc_host_active_fc4s, NULL);
1475
1476static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001477show_fc_host_speed (struct device *dev,
1478 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Tony Jonesee959b02008-02-22 00:13:36 +01001480 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct fc_internal *i = to_fc_internal(shost->transportt);
1482
1483 if (i->f->get_host_speed)
1484 i->f->get_host_speed(shost);
1485
1486 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1487 return snprintf(buf, 20, "unknown\n");
1488
1489 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1490}
Tony Jonesee959b02008-02-22 00:13:36 +01001491static FC_DEVICE_ATTR(host, speed, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 show_fc_host_speed, NULL);
1493
1494
1495fc_host_rd_attr(port_id, "0x%06x\n", 20);
1496fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1497fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1498fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
James Smart016131b2006-08-14 08:20:25 -04001499fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
James Smartb8d08212006-08-17 08:00:43 -04001501fc_private_host_show_function(system_hostname, "%s\n",
1502 FC_SYMBOLIC_NAME_SIZE + 1, )
1503fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
Tony Jonesee959b02008-02-22 00:13:36 +01001504static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
James Smartb8d08212006-08-17 08:00:43 -04001505 show_fc_host_system_hostname, store_fc_host_system_hostname);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508/* Private Host Attributes */
1509
1510static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001511show_fc_private_host_tgtid_bind_type(struct device *dev,
1512 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Tony Jonesee959b02008-02-22 00:13:36 +01001514 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 const char *name;
1516
1517 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1518 if (!name)
1519 return -EINVAL;
1520 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1521}
1522
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001523#define get_list_head_entry(pos, head, member) \
1524 pos = list_entry((head)->next, typeof(*pos), member)
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001527store_fc_private_host_tgtid_bind_type(struct device *dev,
1528 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Tony Jonesee959b02008-02-22 00:13:36 +01001530 struct Scsi_Host *shost = transport_class_to_shost(dev);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001531 struct fc_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 enum fc_tgtid_binding_type val;
1533 unsigned long flags;
1534
1535 if (get_fc_tgtid_bind_type_match(buf, &val))
1536 return -EINVAL;
1537
1538 /* if changing bind type, purge all unused consistent bindings */
1539 if (val != fc_host_tgtid_bind_type(shost)) {
1540 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001541 while (!list_empty(&fc_host_rport_bindings(shost))) {
1542 get_list_head_entry(rport,
1543 &fc_host_rport_bindings(shost), peers);
James Smartaedf3492006-04-10 10:14:05 -04001544 list_del(&rport->peers);
1545 rport->port_state = FC_PORTSTATE_DELETED;
1546 fc_queue_work(shost, &rport->rport_delete_work);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_unlock_irqrestore(shost->host_lock, flags);
1549 }
1550
1551 fc_host_tgtid_bind_type(shost) = val;
1552 return count;
1553}
1554
Tony Jonesee959b02008-02-22 00:13:36 +01001555static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 show_fc_private_host_tgtid_bind_type,
1557 store_fc_private_host_tgtid_bind_type);
1558
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001559static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001560store_fc_private_host_issue_lip(struct device *dev,
1561 struct device_attribute *attr, const char *buf, size_t count)
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001562{
Tony Jonesee959b02008-02-22 00:13:36 +01001563 struct Scsi_Host *shost = transport_class_to_shost(dev);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001564 struct fc_internal *i = to_fc_internal(shost->transportt);
1565 int ret;
1566
1567 /* ignore any data value written to the attribute */
1568 if (i->f->issue_fc_host_lip) {
1569 ret = i->f->issue_fc_host_lip(shost);
1570 return ret ? ret: count;
1571 }
1572
1573 return -ENOENT;
1574}
1575
Tony Jonesee959b02008-02-22 00:13:36 +01001576static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001577 store_fc_private_host_issue_lip);
1578
James Smarta53eb5e2007-04-27 12:41:09 -04001579fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1580
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582/*
1583 * Host Statistics Management
1584 */
1585
1586/* Show a given an attribute in the statistics group */
1587static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001588fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Tony Jonesee959b02008-02-22 00:13:36 +01001590 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 struct fc_internal *i = to_fc_internal(shost->transportt);
1592 struct fc_host_statistics *stats;
1593 ssize_t ret = -ENOENT;
1594
1595 if (offset > sizeof(struct fc_host_statistics) ||
1596 offset % sizeof(u64) != 0)
1597 WARN_ON(1);
1598
1599 if (i->f->get_fc_host_stats) {
1600 stats = (i->f->get_fc_host_stats)(shost);
1601 if (stats)
1602 ret = snprintf(buf, 20, "0x%llx\n",
1603 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1604 }
1605 return ret;
1606}
1607
1608
1609/* generate a read-only statistics attribute */
1610#define fc_host_statistic(name) \
Tony Jonesee959b02008-02-22 00:13:36 +01001611static ssize_t show_fcstat_##name(struct device *cd, \
1612 struct device_attribute *attr, \
1613 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{ \
1615 return fc_stat_show(cd, buf, \
1616 offsetof(struct fc_host_statistics, name)); \
1617} \
Tony Jonesee959b02008-02-22 00:13:36 +01001618static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620fc_host_statistic(seconds_since_last_reset);
1621fc_host_statistic(tx_frames);
1622fc_host_statistic(tx_words);
1623fc_host_statistic(rx_frames);
1624fc_host_statistic(rx_words);
1625fc_host_statistic(lip_count);
1626fc_host_statistic(nos_count);
1627fc_host_statistic(error_frames);
1628fc_host_statistic(dumped_frames);
1629fc_host_statistic(link_failure_count);
1630fc_host_statistic(loss_of_sync_count);
1631fc_host_statistic(loss_of_signal_count);
1632fc_host_statistic(prim_seq_protocol_err_count);
1633fc_host_statistic(invalid_tx_word_count);
1634fc_host_statistic(invalid_crc_count);
1635fc_host_statistic(fcp_input_requests);
1636fc_host_statistic(fcp_output_requests);
1637fc_host_statistic(fcp_control_requests);
1638fc_host_statistic(fcp_input_megabytes);
1639fc_host_statistic(fcp_output_megabytes);
1640
1641static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001642fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1643 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Tony Jonesee959b02008-02-22 00:13:36 +01001645 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 struct fc_internal *i = to_fc_internal(shost->transportt);
1647
1648 /* ignore any data value written to the attribute */
1649 if (i->f->reset_fc_host_stats) {
1650 i->f->reset_fc_host_stats(shost);
1651 return count;
1652 }
1653
1654 return -ENOENT;
1655}
Tony Jonesee959b02008-02-22 00:13:36 +01001656static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 fc_reset_statistics);
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659static struct attribute *fc_statistics_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001660 &device_attr_host_seconds_since_last_reset.attr,
1661 &device_attr_host_tx_frames.attr,
1662 &device_attr_host_tx_words.attr,
1663 &device_attr_host_rx_frames.attr,
1664 &device_attr_host_rx_words.attr,
1665 &device_attr_host_lip_count.attr,
1666 &device_attr_host_nos_count.attr,
1667 &device_attr_host_error_frames.attr,
1668 &device_attr_host_dumped_frames.attr,
1669 &device_attr_host_link_failure_count.attr,
1670 &device_attr_host_loss_of_sync_count.attr,
1671 &device_attr_host_loss_of_signal_count.attr,
1672 &device_attr_host_prim_seq_protocol_err_count.attr,
1673 &device_attr_host_invalid_tx_word_count.attr,
1674 &device_attr_host_invalid_crc_count.attr,
1675 &device_attr_host_fcp_input_requests.attr,
1676 &device_attr_host_fcp_output_requests.attr,
1677 &device_attr_host_fcp_control_requests.attr,
1678 &device_attr_host_fcp_input_megabytes.attr,
1679 &device_attr_host_fcp_output_megabytes.attr,
1680 &device_attr_host_reset_statistics.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 NULL
1682};
1683
1684static struct attribute_group fc_statistics_group = {
1685 .name = "statistics",
1686 .attrs = fc_statistics_attrs,
1687};
1688
James Smarta53eb5e2007-04-27 12:41:09 -04001689
1690/* Host Vport Attributes */
1691
1692static int
1693fc_parse_wwn(const char *ns, u64 *nm)
1694{
1695 unsigned int i, j;
1696 u8 wwn[8];
1697
1698 memset(wwn, 0, sizeof(wwn));
1699
1700 /* Validate and store the new name */
1701 for (i=0, j=0; i < 16; i++) {
1702 if ((*ns >= 'a') && (*ns <= 'f'))
1703 j = ((j << 4) | ((*ns++ -'a') + 10));
1704 else if ((*ns >= 'A') && (*ns <= 'F'))
1705 j = ((j << 4) | ((*ns++ -'A') + 10));
1706 else if ((*ns >= '0') && (*ns <= '9'))
1707 j = ((j << 4) | (*ns++ -'0'));
1708 else
1709 return -EINVAL;
1710 if (i % 2) {
1711 wwn[i/2] = j & 0xff;
1712 j = 0;
1713 }
1714 }
1715
1716 *nm = wwn_to_u64(wwn);
1717
1718 return 0;
1719}
1720
1721
1722/*
1723 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1724 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1725 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1726 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1727 */
1728static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001729store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1730 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001731{
Tony Jonesee959b02008-02-22 00:13:36 +01001732 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001733 struct fc_vport_identifiers vid;
1734 struct fc_vport *vport;
1735 unsigned int cnt=count;
1736 int stat;
1737
1738 memset(&vid, 0, sizeof(vid));
1739
1740 /* count may include a LF at end of string */
1741 if (buf[cnt-1] == '\n')
1742 cnt--;
1743
1744 /* validate we have enough characters for WWPN */
1745 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1746 return -EINVAL;
1747
1748 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1749 if (stat)
1750 return stat;
1751
1752 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1753 if (stat)
1754 return stat;
1755
1756 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1757 vid.vport_type = FC_PORTTYPE_NPIV;
1758 /* vid.symbolic_name is already zero/NULL's */
1759 vid.disable = false; /* always enabled */
1760
1761 /* we only allow support on Channel 0 !!! */
1762 stat = fc_vport_create(shost, 0, &shost->shost_gendev, &vid, &vport);
1763 return stat ? stat : count;
1764}
Tony Jonesee959b02008-02-22 00:13:36 +01001765static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001766 store_fc_host_vport_create);
1767
1768
1769/*
1770 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1771 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1772 * The WWNs are specified as hex characters, and may *not* contain
1773 * any prefixes (e.g. 0x, x, etc)
1774 */
1775static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001776store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1777 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001778{
Tony Jonesee959b02008-02-22 00:13:36 +01001779 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001780 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1781 struct fc_vport *vport;
1782 u64 wwpn, wwnn;
1783 unsigned long flags;
1784 unsigned int cnt=count;
1785 int stat, match;
1786
1787 /* count may include a LF at end of string */
1788 if (buf[cnt-1] == '\n')
1789 cnt--;
1790
1791 /* validate we have enough characters for WWPN */
1792 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1793 return -EINVAL;
1794
1795 stat = fc_parse_wwn(&buf[0], &wwpn);
1796 if (stat)
1797 return stat;
1798
1799 stat = fc_parse_wwn(&buf[17], &wwnn);
1800 if (stat)
1801 return stat;
1802
1803 spin_lock_irqsave(shost->host_lock, flags);
1804 match = 0;
1805 /* we only allow support on Channel 0 !!! */
1806 list_for_each_entry(vport, &fc_host->vports, peers) {
1807 if ((vport->channel == 0) &&
1808 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1809 match = 1;
1810 break;
1811 }
1812 }
1813 spin_unlock_irqrestore(shost->host_lock, flags);
1814
1815 if (!match)
1816 return -ENODEV;
1817
1818 stat = fc_vport_terminate(vport);
1819 return stat ? stat : count;
1820}
Tony Jonesee959b02008-02-22 00:13:36 +01001821static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001822 store_fc_host_vport_delete);
1823
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825static int fc_host_match(struct attribute_container *cont,
1826 struct device *dev)
1827{
1828 struct Scsi_Host *shost;
1829 struct fc_internal *i;
1830
1831 if (!scsi_is_host_device(dev))
1832 return 0;
1833
1834 shost = dev_to_shost(dev);
1835 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1836 != &fc_host_class.class)
1837 return 0;
1838
1839 i = to_fc_internal(shost->transportt);
1840
1841 return &i->t.host_attrs.ac == cont;
1842}
1843
1844static int fc_target_match(struct attribute_container *cont,
1845 struct device *dev)
1846{
1847 struct Scsi_Host *shost;
1848 struct fc_internal *i;
1849
1850 if (!scsi_is_target_device(dev))
1851 return 0;
1852
1853 shost = dev_to_shost(dev->parent);
1854 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1855 != &fc_host_class.class)
1856 return 0;
1857
1858 i = to_fc_internal(shost->transportt);
1859
1860 return &i->t.target_attrs.ac == cont;
1861}
1862
1863static void fc_rport_dev_release(struct device *dev)
1864{
1865 struct fc_rport *rport = dev_to_rport(dev);
1866 put_device(dev->parent);
1867 kfree(rport);
1868}
1869
1870int scsi_is_fc_rport(const struct device *dev)
1871{
1872 return dev->release == fc_rport_dev_release;
1873}
1874EXPORT_SYMBOL(scsi_is_fc_rport);
1875
1876static int fc_rport_match(struct attribute_container *cont,
1877 struct device *dev)
1878{
1879 struct Scsi_Host *shost;
1880 struct fc_internal *i;
1881
1882 if (!scsi_is_fc_rport(dev))
1883 return 0;
1884
1885 shost = dev_to_shost(dev->parent);
1886 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1887 != &fc_host_class.class)
1888 return 0;
1889
1890 i = to_fc_internal(shost->transportt);
1891
1892 return &i->rport_attr_cont.ac == cont;
1893}
1894
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001895
James Smarta53eb5e2007-04-27 12:41:09 -04001896static void fc_vport_dev_release(struct device *dev)
1897{
1898 struct fc_vport *vport = dev_to_vport(dev);
1899 put_device(dev->parent); /* release kobj parent */
1900 kfree(vport);
1901}
1902
1903int scsi_is_fc_vport(const struct device *dev)
1904{
1905 return dev->release == fc_vport_dev_release;
1906}
1907EXPORT_SYMBOL(scsi_is_fc_vport);
1908
1909static int fc_vport_match(struct attribute_container *cont,
1910 struct device *dev)
1911{
1912 struct fc_vport *vport;
1913 struct Scsi_Host *shost;
1914 struct fc_internal *i;
1915
1916 if (!scsi_is_fc_vport(dev))
1917 return 0;
1918 vport = dev_to_vport(dev);
1919
1920 shost = vport_to_shost(vport);
1921 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1922 != &fc_host_class.class)
1923 return 0;
1924
1925 i = to_fc_internal(shost->transportt);
1926 return &i->vport_attr_cont.ac == cont;
1927}
1928
1929
James Smartc829c392006-03-13 08:28:57 -05001930/**
1931 * fc_timed_out - FC Transport I/O timeout intercept handler
James Smartc829c392006-03-13 08:28:57 -05001932 * @scmd: The SCSI command which timed out
1933 *
1934 * This routine protects against error handlers getting invoked while a
1935 * rport is in a blocked state, typically due to a temporarily loss of
1936 * connectivity. If the error handlers are allowed to proceed, requests
1937 * to abort i/o, reset the target, etc will likely fail as there is no way
1938 * to communicate with the device to perform the requested function. These
1939 * failures may result in the midlayer taking the device offline, requiring
1940 * manual intervention to restore operation.
1941 *
1942 * This routine, called whenever an i/o times out, validates the state of
1943 * the underlying rport. If the rport is blocked, it returns
1944 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1945 * Eventually, either the device will return, or devloss_tmo will fire,
1946 * and when the timeout then fires, it will be handled normally.
1947 * If the rport is not blocked, normal error handling continues.
1948 *
1949 * Notes:
1950 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05001951 */
James Smartc829c392006-03-13 08:28:57 -05001952static enum scsi_eh_timer_return
1953fc_timed_out(struct scsi_cmnd *scmd)
1954{
1955 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1956
1957 if (rport->port_state == FC_PORTSTATE_BLOCKED)
1958 return EH_RESET_TIMER;
1959
1960 return EH_NOT_HANDLED;
1961}
1962
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001963/*
James Smartbda23252008-04-24 12:12:46 -04001964 * Called by fc_user_scan to locate an rport on the shost that
1965 * matches the channel and target id, and invoke scsi_scan_target()
1966 * on the rport.
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001967 */
James Smartbda23252008-04-24 12:12:46 -04001968static void
1969fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001970{
1971 struct fc_rport *rport;
James Smartbda23252008-04-24 12:12:46 -04001972 unsigned long flags;
1973
1974 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001975
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001976 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1977 if (rport->scsi_target_id == -1)
1978 continue;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001979
Hannes Reinecke0d2fcd92007-06-14 15:16:45 +02001980 if (rport->port_state != FC_PORTSTATE_ONLINE)
1981 continue;
1982
James Smartbda23252008-04-24 12:12:46 -04001983 if ((channel == rport->channel) &&
1984 (id == rport->scsi_target_id)) {
1985 spin_unlock_irqrestore(shost->host_lock, flags);
1986 scsi_scan_target(&rport->dev, channel, id, lun, 1);
1987 return;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001988 }
1989 }
1990
James Smartbda23252008-04-24 12:12:46 -04001991 spin_unlock_irqrestore(shost->host_lock, flags);
1992}
1993
1994/*
1995 * Called via sysfs scan routines. Necessary, as the FC transport
1996 * wants to place all target objects below the rport object. So this
1997 * routine must invoke the scsi_scan_target() routine with the rport
1998 * object as the parent.
1999 */
2000static int
2001fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2002{
2003 uint chlo, chhi;
2004 uint tgtlo, tgthi;
2005
2006 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2007 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2008 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2009 return -EINVAL;
2010
2011 if (channel == SCAN_WILD_CARD) {
2012 chlo = 0;
2013 chhi = shost->max_channel + 1;
2014 } else {
2015 chlo = channel;
2016 chhi = channel + 1;
2017 }
2018
2019 if (id == SCAN_WILD_CARD) {
2020 tgtlo = 0;
2021 tgthi = shost->max_id;
2022 } else {
2023 tgtlo = id;
2024 tgthi = id + 1;
2025 }
2026
2027 for ( ; chlo < chhi; chlo++)
2028 for ( ; tgtlo < tgthi; tgtlo++)
2029 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2030
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002031 return 0;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002032}
2033
FUJITA Tomonori75252362007-09-01 02:02:27 +09002034static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
2035 int result)
2036{
2037 struct fc_internal *i = to_fc_internal(shost->transportt);
2038 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
2039}
2040
2041static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
2042{
2043 struct fc_internal *i = to_fc_internal(shost->transportt);
2044 return i->f->it_nexus_response(shost, nexus, result);
2045}
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047struct scsi_transport_template *
2048fc_attach_transport(struct fc_function_template *ft)
2049{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 int count;
Jes Sorensen24669f752006-01-16 10:31:18 -05002051 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2052 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 if (unlikely(!i))
2055 return NULL;
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2058 i->t.target_attrs.ac.class = &fc_transport_class.class;
2059 i->t.target_attrs.ac.match = fc_target_match;
2060 i->t.target_size = sizeof(struct fc_starget_attrs);
2061 transport_container_register(&i->t.target_attrs);
2062
2063 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2064 i->t.host_attrs.ac.class = &fc_host_class.class;
2065 i->t.host_attrs.ac.match = fc_host_match;
2066 i->t.host_size = sizeof(struct fc_host_attrs);
2067 if (ft->get_fc_host_stats)
2068 i->t.host_attrs.statistics = &fc_statistics_group;
2069 transport_container_register(&i->t.host_attrs);
2070
2071 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2072 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2073 i->rport_attr_cont.ac.match = fc_rport_match;
2074 transport_container_register(&i->rport_attr_cont);
2075
James Smarta53eb5e2007-04-27 12:41:09 -04002076 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2077 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2078 i->vport_attr_cont.ac.match = fc_vport_match;
2079 transport_container_register(&i->vport_attr_cont);
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 i->f = ft;
2082
2083 /* Transport uses the shost workq for scsi scanning */
2084 i->t.create_work_queue = 1;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002085
James Smartc829c392006-03-13 08:28:57 -05002086 i->t.eh_timed_out = fc_timed_out;
2087
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002088 i->t.user_scan = fc_user_scan;
James Smart9ef3e4a2007-05-24 19:04:44 -04002089
FUJITA Tomonori75252362007-09-01 02:02:27 +09002090 /* target-mode drivers' functions */
2091 i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2092 i->t.it_nexus_response = fc_it_nexus_response;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /*
2095 * Setup SCSI Target Attributes.
2096 */
2097 count = 0;
2098 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2099 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2100 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2101
2102 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2103
2104 i->starget_attrs[count] = NULL;
2105
2106
2107 /*
2108 * Setup SCSI Host Attributes.
2109 */
2110 count=0;
2111 SETUP_HOST_ATTRIBUTE_RD(node_name);
2112 SETUP_HOST_ATTRIBUTE_RD(port_name);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01002113 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2115 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2117 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
James Smarta53eb5e2007-04-27 12:41:09 -04002118 if (ft->vport_create) {
2119 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2120 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2123
2124 SETUP_HOST_ATTRIBUTE_RD(port_id);
2125 SETUP_HOST_ATTRIBUTE_RD(port_type);
2126 SETUP_HOST_ATTRIBUTE_RD(port_state);
2127 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2128 SETUP_HOST_ATTRIBUTE_RD(speed);
2129 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
James Smart016131b2006-08-14 08:20:25 -04002130 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
James Smartb8d08212006-08-17 08:00:43 -04002131 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 /* Transport-managed attributes */
2134 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002135 if (ft->issue_fc_host_lip)
2136 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
James Smarta53eb5e2007-04-27 12:41:09 -04002137 if (ft->vport_create)
2138 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2139 if (ft->vport_delete)
2140 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 BUG_ON(count > FC_HOST_NUM_ATTRS);
2143
2144 i->host_attrs[count] = NULL;
2145
2146 /*
2147 * Setup Remote Port Attributes.
2148 */
2149 count=0;
2150 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2151 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2152 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2153 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2154 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2155 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2156 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2157 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2158 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
James Smart0f29b962006-08-18 17:33:29 -04002159 if (ft->terminate_rport_io)
2160 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2163
2164 i->rport_attrs[count] = NULL;
2165
James Smarta53eb5e2007-04-27 12:41:09 -04002166 /*
2167 * Setup Virtual Port Attributes.
2168 */
2169 count=0;
2170 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2171 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2172 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2173 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2174 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2175 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2176 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2177 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2178 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2179
2180 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2181
2182 i->vport_attrs[count] = NULL;
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return &i->t;
2185}
2186EXPORT_SYMBOL(fc_attach_transport);
2187
2188void fc_release_transport(struct scsi_transport_template *t)
2189{
2190 struct fc_internal *i = to_fc_internal(t);
2191
2192 transport_container_unregister(&i->t.target_attrs);
2193 transport_container_unregister(&i->t.host_attrs);
2194 transport_container_unregister(&i->rport_attr_cont);
James Smarta53eb5e2007-04-27 12:41:09 -04002195 transport_container_unregister(&i->vport_attr_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 kfree(i);
2198}
2199EXPORT_SYMBOL(fc_release_transport);
2200
James Smartaedf3492006-04-10 10:14:05 -04002201/**
2202 * fc_queue_work - Queue work to the fc_host workqueue.
2203 * @shost: Pointer to Scsi_Host bound to fc_host.
2204 * @work: Work to queue for execution.
2205 *
2206 * Return value:
James Smarta0785ed2006-05-11 13:27:09 -04002207 * 1 - work queued for execution
2208 * 0 - work is already queued
2209 * -EINVAL - work queue doesn't exist
Rob Landleyeb448202007-11-03 13:30:39 -05002210 */
James Smartaedf3492006-04-10 10:14:05 -04002211static int
2212fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2213{
2214 if (unlikely(!fc_host_work_q(shost))) {
2215 printk(KERN_ERR
2216 "ERROR: FC host '%s' attempted to queue work, "
2217 "when no workqueue created.\n", shost->hostt->name);
2218 dump_stack();
2219
2220 return -EINVAL;
2221 }
2222
2223 return queue_work(fc_host_work_q(shost), work);
2224}
2225
2226/**
2227 * fc_flush_work - Flush a fc_host's workqueue.
2228 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002229 */
James Smartaedf3492006-04-10 10:14:05 -04002230static void
2231fc_flush_work(struct Scsi_Host *shost)
2232{
2233 if (!fc_host_work_q(shost)) {
2234 printk(KERN_ERR
2235 "ERROR: FC host '%s' attempted to flush work, "
2236 "when no workqueue created.\n", shost->hostt->name);
2237 dump_stack();
2238 return;
2239 }
2240
2241 flush_workqueue(fc_host_work_q(shost));
2242}
2243
2244/**
2245 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2246 * @shost: Pointer to Scsi_Host bound to fc_host.
2247 * @work: Work to queue for execution.
2248 * @delay: jiffies to delay the work queuing
2249 *
2250 * Return value:
James Smart0f29b962006-08-18 17:33:29 -04002251 * 1 on success / 0 already queued / < 0 for error
Rob Landleyeb448202007-11-03 13:30:39 -05002252 */
James Smartaedf3492006-04-10 10:14:05 -04002253static int
David Howellsc4028952006-11-22 14:57:56 +00002254fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
James Smartaedf3492006-04-10 10:14:05 -04002255 unsigned long delay)
2256{
2257 if (unlikely(!fc_host_devloss_work_q(shost))) {
2258 printk(KERN_ERR
2259 "ERROR: FC host '%s' attempted to queue work, "
2260 "when no workqueue created.\n", shost->hostt->name);
2261 dump_stack();
2262
2263 return -EINVAL;
2264 }
2265
2266 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2267}
2268
2269/**
2270 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2271 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002272 */
James Smartaedf3492006-04-10 10:14:05 -04002273static void
2274fc_flush_devloss(struct Scsi_Host *shost)
2275{
2276 if (!fc_host_devloss_work_q(shost)) {
2277 printk(KERN_ERR
2278 "ERROR: FC host '%s' attempted to flush work, "
2279 "when no workqueue created.\n", shost->hostt->name);
2280 dump_stack();
2281 return;
2282 }
2283
2284 flush_workqueue(fc_host_devloss_work_q(shost));
2285}
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288/**
Rob Landleyeb448202007-11-03 13:30:39 -05002289 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2290 * @shost: Which &Scsi_Host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 *
2292 * This routine is expected to be called immediately preceeding the
2293 * a driver's call to scsi_remove_host().
2294 *
2295 * WARNING: A driver utilizing the fc_transport, which fails to call
Rob Landleyeb448202007-11-03 13:30:39 -05002296 * this routine prior to scsi_remove_host(), will leave dangling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 * objects in /sys/class/fc_remote_ports. Access to any of these
2298 * objects can result in a system crash !!!
2299 *
2300 * Notes:
2301 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002302 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303void
2304fc_remove_host(struct Scsi_Host *shost)
2305{
James Smarta53eb5e2007-04-27 12:41:09 -04002306 struct fc_vport *vport = NULL, *next_vport = NULL;
2307 struct fc_rport *rport = NULL, *next_rport = NULL;
James Smartaedf3492006-04-10 10:14:05 -04002308 struct workqueue_struct *work_q;
2309 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James Smarta53eb5e2007-04-27 12:41:09 -04002310 unsigned long flags;
James Smarta53eb5e2007-04-27 12:41:09 -04002311
2312 spin_lock_irqsave(shost->host_lock, flags);
2313
2314 /* Remove any vports */
James Smart9ef3e4a2007-05-24 19:04:44 -04002315 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2316 fc_queue_work(shost, &vport->vport_delete_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 /* Remove any remote ports */
2319 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002320 &fc_host->rports, peers) {
2321 list_del(&rport->peers);
2322 rport->port_state = FC_PORTSTATE_DELETED;
2323 fc_queue_work(shost, &rport->rport_delete_work);
2324 }
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002327 &fc_host->rport_bindings, peers) {
2328 list_del(&rport->peers);
2329 rport->port_state = FC_PORTSTATE_DELETED;
2330 fc_queue_work(shost, &rport->rport_delete_work);
2331 }
2332
James Smarta53eb5e2007-04-27 12:41:09 -04002333 spin_unlock_irqrestore(shost->host_lock, flags);
2334
James Smartaedf3492006-04-10 10:14:05 -04002335 /* flush all scan work items */
2336 scsi_flush_work(shost);
2337
2338 /* flush all stgt delete, and rport delete work items, then kill it */
2339 if (fc_host->work_q) {
2340 work_q = fc_host->work_q;
2341 fc_host->work_q = NULL;
2342 destroy_workqueue(work_q);
2343 }
2344
2345 /* flush all devloss work items, then kill it */
2346 if (fc_host->devloss_work_q) {
2347 work_q = fc_host->devloss_work_q;
2348 fc_host->devloss_work_q = NULL;
2349 destroy_workqueue(work_q);
2350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352EXPORT_SYMBOL(fc_remove_host);
2353
James Smartaedf3492006-04-10 10:14:05 -04002354
2355/**
2356 * fc_starget_delete - called to delete the scsi decendents of an rport
David Howellsc4028952006-11-22 14:57:56 +00002357 * @work: remote port to be operated on.
Rob Landleyeb448202007-11-03 13:30:39 -05002358 *
2359 * Deletes target and all sdevs.
2360 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002361static void
David Howellsc4028952006-11-22 14:57:56 +00002362fc_starget_delete(struct work_struct *work)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002363{
David Howellsc4028952006-11-22 14:57:56 +00002364 struct fc_rport *rport =
2365 container_of(work, struct fc_rport, stgt_delete_work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002366 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002367 struct fc_internal *i = to_fc_internal(shost->transportt);
2368
James Smart92740b22007-04-27 11:53:17 -04002369 /* Involve the LLDD if possible to terminate all io on the rport. */
2370 if (i->f->terminate_rport_io)
James Smart0f29b962006-08-18 17:33:29 -04002371 i->f->terminate_rport_io(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002372
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002373 scsi_remove_target(&rport->dev);
2374}
2375
James Smartaedf3492006-04-10 10:14:05 -04002376
2377/**
2378 * fc_rport_final_delete - finish rport termination and delete it.
David Howellsc4028952006-11-22 14:57:56 +00002379 * @work: remote port to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05002380 */
James Smartaedf3492006-04-10 10:14:05 -04002381static void
David Howellsc4028952006-11-22 14:57:56 +00002382fc_rport_final_delete(struct work_struct *work)
James Smartaedf3492006-04-10 10:14:05 -04002383{
David Howellsc4028952006-11-22 14:57:56 +00002384 struct fc_rport *rport =
2385 container_of(work, struct fc_rport, rport_delete_work);
James Smartaedf3492006-04-10 10:14:05 -04002386 struct device *dev = &rport->dev;
2387 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002388 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart92740b22007-04-27 11:53:17 -04002389 unsigned long flags;
James Smartaedf3492006-04-10 10:14:05 -04002390
2391 /*
James Smart9ef3e4a2007-05-24 19:04:44 -04002392 * if a scan is pending, flush the SCSI Host work_q so that
James Smartaedf3492006-04-10 10:14:05 -04002393 * that we can reclaim the rport scan work element.
2394 */
2395 if (rport->flags & FC_RPORT_SCAN_PENDING)
2396 scsi_flush_work(shost);
2397
James Smart92740b22007-04-27 11:53:17 -04002398 /* involve the LLDD to terminate all pending i/o */
2399 if (i->f->terminate_rport_io)
2400 i->f->terminate_rport_io(rport);
2401
2402 /*
2403 * Cancel any outstanding timers. These should really exist
2404 * only when rmmod'ing the LLDD and we're asking for
2405 * immediate termination of the rports
2406 */
2407 spin_lock_irqsave(shost->host_lock, flags);
2408 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2409 spin_unlock_irqrestore(shost->host_lock, flags);
2410 if (!cancel_delayed_work(&rport->fail_io_work))
2411 fc_flush_devloss(shost);
2412 if (!cancel_delayed_work(&rport->dev_loss_work))
2413 fc_flush_devloss(shost);
2414 spin_lock_irqsave(shost->host_lock, flags);
2415 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2416 }
2417 spin_unlock_irqrestore(shost->host_lock, flags);
2418
James Smart0f29b962006-08-18 17:33:29 -04002419 /* Delete SCSI target and sdevs */
2420 if (rport->scsi_target_id != -1)
David Howellsc4028952006-11-22 14:57:56 +00002421 fc_starget_delete(&rport->stgt_delete_work);
James Smart92740b22007-04-27 11:53:17 -04002422
2423 /*
2424 * Notify the driver that the rport is now dead. The LLDD will
2425 * also guarantee that any communication to the rport is terminated
2426 */
2427 if (i->f->dev_loss_tmo_callbk)
James Smart0f29b962006-08-18 17:33:29 -04002428 i->f->dev_loss_tmo_callbk(rport);
James Smart0f29b962006-08-18 17:33:29 -04002429
James Smartaedf3492006-04-10 10:14:05 -04002430 transport_remove_device(dev);
2431 device_del(dev);
2432 transport_destroy_device(dev);
James Smart3bdad7b2006-06-26 14:19:59 -04002433 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2434 put_device(dev); /* for self-reference */
James Smartaedf3492006-04-10 10:14:05 -04002435}
2436
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438/**
2439 * fc_rport_create - allocates and creates a remote FC port.
2440 * @shost: scsi host the remote port is connected to.
2441 * @channel: Channel on shost port connected to.
2442 * @ids: The world wide names, fc address, and FC4 port
2443 * roles for the remote port.
2444 *
2445 * Allocates and creates the remoter port structure, including the
2446 * class and sysfs creation.
2447 *
2448 * Notes:
2449 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002450 */
Adrian Bunk44818ef2007-07-09 11:59:59 -07002451static struct fc_rport *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452fc_rport_create(struct Scsi_Host *shost, int channel,
2453 struct fc_rport_identifiers *ids)
2454{
James Smartaedf3492006-04-10 10:14:05 -04002455 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 struct fc_internal *fci = to_fc_internal(shost->transportt);
2457 struct fc_rport *rport;
2458 struct device *dev;
2459 unsigned long flags;
2460 int error;
2461 size_t size;
2462
2463 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
Jes Sorensen24669f752006-01-16 10:31:18 -05002464 rport = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (unlikely(!rport)) {
2466 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
2467 return NULL;
2468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 rport->maxframe_size = -1;
2471 rport->supported_classes = FC_COS_UNSPECIFIED;
2472 rport->dev_loss_tmo = fc_dev_loss_tmo;
2473 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2474 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2475 rport->port_id = ids->port_id;
2476 rport->roles = ids->roles;
2477 rport->port_state = FC_PORTSTATE_ONLINE;
2478 if (fci->f->dd_fcrport_size)
2479 rport->dd_data = &rport[1];
2480 rport->channel = channel;
James Smart0f29b962006-08-18 17:33:29 -04002481 rport->fast_io_fail_tmo = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
David Howellsc4028952006-11-22 14:57:56 +00002483 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2484 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2485 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2486 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2487 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 spin_lock_irqsave(shost->host_lock, flags);
2490
2491 rport->number = fc_host->next_rport_number++;
James Smarta53eb5e2007-04-27 12:41:09 -04002492 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 rport->scsi_target_id = fc_host->next_target_id++;
2494 else
2495 rport->scsi_target_id = -1;
James Smartaedf3492006-04-10 10:14:05 -04002496 list_add_tail(&rport->peers, &fc_host->rports);
James Smart3bdad7b2006-06-26 14:19:59 -04002497 get_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 spin_unlock_irqrestore(shost->host_lock, flags);
2500
2501 dev = &rport->dev;
James Smart3bdad7b2006-06-26 14:19:59 -04002502 device_initialize(dev); /* takes self reference */
2503 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 dev->release = fc_rport_dev_release;
2505 sprintf(dev->bus_id, "rport-%d:%d-%d",
2506 shost->host_no, channel, rport->number);
2507 transport_setup_device(dev);
2508
2509 error = device_add(dev);
2510 if (error) {
2511 printk(KERN_ERR "FC Remote Port device_add failed\n");
2512 goto delete_rport;
2513 }
2514 transport_add_device(dev);
2515 transport_configure_device(dev);
2516
James Smarta53eb5e2007-04-27 12:41:09 -04002517 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002519 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 return rport;
2524
2525delete_rport:
2526 transport_destroy_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 spin_lock_irqsave(shost->host_lock, flags);
2528 list_del(&rport->peers);
James Smart3bdad7b2006-06-26 14:19:59 -04002529 put_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 spin_unlock_irqrestore(shost->host_lock, flags);
2531 put_device(dev->parent);
2532 kfree(rport);
2533 return NULL;
2534}
2535
2536/**
Rob Landleyeb448202007-11-03 13:30:39 -05002537 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 * @shost: scsi host the remote port is connected to.
2539 * @channel: Channel on shost port connected to.
2540 * @ids: The world wide names, fc address, and FC4 port
2541 * roles for the remote port.
2542 *
2543 * The LLDD calls this routine to notify the transport of the existence
2544 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2545 * of the port, it's FC address (port_id), and the FC4 roles that are
2546 * active for the port.
2547 *
2548 * For ports that are FCP targets (aka scsi targets), the FC transport
2549 * maintains consistent target id bindings on behalf of the LLDD.
2550 * A consistent target id binding is an assignment of a target id to
2551 * a remote port identifier, which persists while the scsi host is
2552 * attached. The remote port can disappear, then later reappear, and
2553 * it's target id assignment remains the same. This allows for shifts
2554 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2555 * changes to the scsi subsystem which is based on scsi host number and
2556 * target id values. Bindings are only valid during the attachment of
2557 * the scsi host. If the host detaches, then later re-attaches, target
2558 * id bindings may change.
2559 *
2560 * This routine is responsible for returning a remote port structure.
2561 * The routine will search the list of remote ports it maintains
2562 * internally on behalf of consistent target id mappings. If found, the
2563 * remote port structure will be reused. Otherwise, a new remote port
2564 * structure will be allocated.
2565 *
2566 * Whenever a remote port is allocated, a new fc_remote_port class
2567 * device is created.
2568 *
2569 * Should not be called from interrupt context.
2570 *
2571 * Notes:
2572 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574struct fc_rport *
2575fc_remote_port_add(struct Scsi_Host *shost, int channel,
2576 struct fc_rport_identifiers *ids)
2577{
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002578 struct fc_internal *fci = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04002579 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 struct fc_rport *rport;
2581 unsigned long flags;
2582 int match = 0;
2583
James Smartaedf3492006-04-10 10:14:05 -04002584 /* ensure any stgt delete functions are done */
2585 fc_flush_work(shost);
2586
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002587 /*
2588 * Search the list of "active" rports, for an rport that has been
2589 * deleted, but we've held off the real delete while the target
2590 * is in a "blocked" state.
2591 */
2592 spin_lock_irqsave(shost->host_lock, flags);
2593
James Smartaedf3492006-04-10 10:14:05 -04002594 list_for_each_entry(rport, &fc_host->rports, peers) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002595
2596 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2597 (rport->channel == channel)) {
2598
James Smartaedf3492006-04-10 10:14:05 -04002599 switch (fc_host->tgtid_bind_type) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002600 case FC_TGTID_BIND_BY_WWPN:
2601 case FC_TGTID_BIND_NONE:
2602 if (rport->port_name == ids->port_name)
2603 match = 1;
2604 break;
2605 case FC_TGTID_BIND_BY_WWNN:
2606 if (rport->node_name == ids->node_name)
2607 match = 1;
2608 break;
2609 case FC_TGTID_BIND_BY_ID:
2610 if (rport->port_id == ids->port_id)
2611 match = 1;
2612 break;
2613 }
2614
2615 if (match) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002616
2617 memcpy(&rport->node_name, &ids->node_name,
2618 sizeof(rport->node_name));
2619 memcpy(&rport->port_name, &ids->port_name,
2620 sizeof(rport->port_name));
2621 rport->port_id = ids->port_id;
2622
2623 rport->port_state = FC_PORTSTATE_ONLINE;
2624 rport->roles = ids->roles;
2625
2626 spin_unlock_irqrestore(shost->host_lock, flags);
2627
2628 if (fci->f->dd_fcrport_size)
2629 memset(rport->dd_data, 0,
2630 fci->f->dd_fcrport_size);
2631
2632 /*
James Smart92740b22007-04-27 11:53:17 -04002633 * If we were not a target, cancel the
2634 * io terminate and rport timers, and
2635 * we're done.
2636 *
2637 * If we were a target, but our new role
2638 * doesn't indicate a target, leave the
2639 * timers running expecting the role to
2640 * change as the target fully logs in. If
2641 * it doesn't, the target will be torn down.
2642 *
2643 * If we were a target, and our role shows
2644 * we're still a target, cancel the timers
2645 * and kick off a scan.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002646 */
James Smart92740b22007-04-27 11:53:17 -04002647
2648 /* was a target, not in roles */
2649 if ((rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04002650 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002651 return rport;
2652
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002653 /*
James Smart92740b22007-04-27 11:53:17 -04002654 * Stop the fail io and dev_loss timers.
2655 * If they flush, the port_state will
2656 * be checked and will NOOP the function.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002657 */
James Smart0f29b962006-08-18 17:33:29 -04002658 if (!cancel_delayed_work(&rport->fail_io_work))
2659 fc_flush_devloss(shost);
James Smart92740b22007-04-27 11:53:17 -04002660 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002661 fc_flush_devloss(shost);
2662
2663 spin_lock_irqsave(shost->host_lock, flags);
2664
2665 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002666
James Smart92740b22007-04-27 11:53:17 -04002667 /* if target, initiate a scan */
2668 if (rport->scsi_target_id != -1) {
2669 rport->flags |= FC_RPORT_SCAN_PENDING;
2670 scsi_queue_work(shost,
2671 &rport->scan_work);
2672 spin_unlock_irqrestore(shost->host_lock,
2673 flags);
2674 scsi_target_unblock(&rport->dev);
2675 } else
2676 spin_unlock_irqrestore(shost->host_lock,
2677 flags);
James Smarta0785ed2006-05-11 13:27:09 -04002678
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002679 return rport;
2680 }
2681 }
2682 }
2683
James Smart92740b22007-04-27 11:53:17 -04002684 /*
2685 * Search the bindings array
2686 * Note: if never a FCP target, you won't be on this list
2687 */
James Smartaedf3492006-04-10 10:14:05 -04002688 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
2690 /* search for a matching consistent binding */
2691
James Smartaedf3492006-04-10 10:14:05 -04002692 list_for_each_entry(rport, &fc_host->rport_bindings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 peers) {
2694 if (rport->channel != channel)
2695 continue;
2696
James Smartaedf3492006-04-10 10:14:05 -04002697 switch (fc_host->tgtid_bind_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 case FC_TGTID_BIND_BY_WWPN:
2699 if (rport->port_name == ids->port_name)
2700 match = 1;
2701 break;
2702 case FC_TGTID_BIND_BY_WWNN:
2703 if (rport->node_name == ids->node_name)
2704 match = 1;
2705 break;
2706 case FC_TGTID_BIND_BY_ID:
2707 if (rport->port_id == ids->port_id)
2708 match = 1;
2709 break;
2710 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2711 break;
2712 }
2713
2714 if (match) {
James Smartaedf3492006-04-10 10:14:05 -04002715 list_move_tail(&rport->peers, &fc_host->rports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 break;
2717 }
2718 }
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if (match) {
2721 memcpy(&rport->node_name, &ids->node_name,
2722 sizeof(rport->node_name));
2723 memcpy(&rport->port_name, &ids->port_name,
2724 sizeof(rport->port_name));
2725 rport->port_id = ids->port_id;
2726 rport->roles = ids->roles;
2727 rport->port_state = FC_PORTSTATE_ONLINE;
2728
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002729 if (fci->f->dd_fcrport_size)
2730 memset(rport->dd_data, 0,
2731 fci->f->dd_fcrport_size);
2732
James Smarta53eb5e2007-04-27 12:41:09 -04002733 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002735 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 scsi_queue_work(shost, &rport->scan_work);
James Smarta0785ed2006-05-11 13:27:09 -04002737 spin_unlock_irqrestore(shost->host_lock, flags);
2738 scsi_target_unblock(&rport->dev);
2739 } else
2740 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 return rport;
2743 }
2744 }
2745
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002746 spin_unlock_irqrestore(shost->host_lock, flags);
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 /* No consistent binding found - create new remote port entry */
2749 rport = fc_rport_create(shost, channel, ids);
2750
2751 return rport;
2752}
2753EXPORT_SYMBOL(fc_remote_port_add);
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756/**
Rob Landleyeb448202007-11-03 13:30:39 -05002757 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 * @rport: The remote port that no longer exists
2759 *
2760 * The LLDD calls this routine to notify the transport that a remote
2761 * port is no longer part of the topology. Note: Although a port
2762 * may no longer be part of the topology, it may persist in the remote
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002763 * ports displayed by the fc_host. We do this under 2 conditions:
Rob Landleyeb448202007-11-03 13:30:39 -05002764 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002765 * This allows the port to temporarily disappear, then reappear without
2766 * disrupting the SCSI device tree attached to it. During the "blocked"
2767 * period the port will still exist.
Rob Landleyeb448202007-11-03 13:30:39 -05002768 * 2) If the port was a scsi target and disappears for longer than we
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002769 * expect, we'll delete the port and the tear down the SCSI device tree
2770 * attached to it. However, we want to semi-persist the target id assigned
2771 * to that port if it eventually does exist. The port structure will
2772 * remain (although with minimal information) so that the target id
2773 * bindings remails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 *
2775 * If the remote port is not an FCP Target, it will be fully torn down
2776 * and deallocated, including the fc_remote_port class device.
2777 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002778 * If the remote port is an FCP Target, the port will be placed in a
2779 * temporary blocked state. From the LLDD's perspective, the rport no
2780 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2781 * exists, but all sdevs on it are blocked from further I/O. The following
Rob Landleyeb448202007-11-03 13:30:39 -05002782 * is then expected.
2783 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002784 * If the remote port does not return (signaled by a LLDD call to
2785 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2786 * scsi target is removed - killing all outstanding i/o and removing the
2787 * scsi devices attached ot it. The port structure will be marked Not
2788 * Present and be partially cleared, leaving only enough information to
2789 * recognize the remote port relative to the scsi target id binding if
2790 * it later appears. The port will remain as long as there is a valid
2791 * binding (e.g. until the user changes the binding type or unloads the
2792 * scsi host with the binding).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002794 * If the remote port returns within the dev_loss_tmo value (and matches
2795 * according to the target id binding type), the port structure will be
2796 * reused. If it is no longer a SCSI target, the target will be torn
2797 * down. If it continues to be a SCSI target, then the target will be
2798 * unblocked (allowing i/o to be resumed), and a scan will be activated
2799 * to ensure that all luns are detected.
2800 *
2801 * Called from normal process context only - cannot be called from interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 *
2803 * Notes:
2804 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806void
2807fc_remote_port_delete(struct fc_rport *rport)
2808{
James Smartaedf3492006-04-10 10:14:05 -04002809 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002810 struct fc_internal *i = to_fc_internal(shost->transportt);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002811 int timeout = rport->dev_loss_tmo;
James Smartaedf3492006-04-10 10:14:05 -04002812 unsigned long flags;
2813
2814 /*
2815 * No need to flush the fc_host work_q's, as all adds are synchronous.
2816 *
2817 * We do need to reclaim the rport scan work element, so eventually
2818 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2819 * there's still a scan pending.
2820 */
2821
2822 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
James Smart92740b22007-04-27 11:53:17 -04002824 if (rport->port_state != FC_PORTSTATE_ONLINE) {
James Smartaedf3492006-04-10 10:14:05 -04002825 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return;
2827 }
2828
James Smart92740b22007-04-27 11:53:17 -04002829 /*
2830 * In the past, we if this was not an FCP-Target, we would
2831 * unconditionally just jump to deleting the rport.
2832 * However, rports can be used as node containers by the LLDD,
2833 * and its not appropriate to just terminate the rport at the
2834 * first sign of a loss in connectivity. The LLDD may want to
2835 * send ELS traffic to re-validate the login. If the rport is
2836 * immediately deleted, it makes it inappropriate for a node
2837 * container.
2838 * So... we now unconditionally wait dev_loss_tmo before
2839 * destroying an rport.
2840 */
2841
James Smartaedf3492006-04-10 10:14:05 -04002842 rport->port_state = FC_PORTSTATE_BLOCKED;
2843
2844 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2845
2846 spin_unlock_irqrestore(shost->host_lock, flags);
2847
FUJITA Tomonori75252362007-09-01 02:02:27 +09002848 if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
2849 shost->active_mode & MODE_TARGET)
2850 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
2851
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002852 scsi_target_block(&rport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
James Smart0f29b962006-08-18 17:33:29 -04002854 /* see if we need to kill io faster than waiting for device loss */
2855 if ((rport->fast_io_fail_tmo != -1) &&
2856 (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
2857 fc_queue_devloss_work(shost, &rport->fail_io_work,
2858 rport->fast_io_fail_tmo * HZ);
2859
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002860 /* cap the length the devices can be blocked until they are deleted */
James Smartaedf3492006-04-10 10:14:05 -04002861 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862}
2863EXPORT_SYMBOL(fc_remote_port_delete);
2864
2865/**
Rob Landleyeb448202007-11-03 13:30:39 -05002866 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 * @rport: The remote port that changed.
Rob Landleyeb448202007-11-03 13:30:39 -05002868 * @roles: New roles for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 *
Rob Landleyeb448202007-11-03 13:30:39 -05002870 * Description: The LLDD calls this routine to notify the transport that the
2871 * roles on a remote port may have changed. The largest effect of this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 * if a port now becomes a FCP Target, it must be allocated a
2873 * scsi target id. If the port is no longer a FCP target, any
2874 * scsi target id value assigned to it will persist in case the
2875 * role changes back to include FCP Target. No changes in the scsi
2876 * midlayer will be invoked if the role changes (in the expectation
2877 * that the role will be resumed. If it doesn't normal error processing
2878 * will take place).
2879 *
2880 * Should not be called from interrupt context.
2881 *
2882 * Notes:
2883 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002884 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885void
2886fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2887{
2888 struct Scsi_Host *shost = rport_to_shost(rport);
James Smartaedf3492006-04-10 10:14:05 -04002889 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 unsigned long flags;
2891 int create = 0;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002892 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 spin_lock_irqsave(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04002895 if (roles & FC_PORT_ROLE_FCP_TARGET) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002896 if (rport->scsi_target_id == -1) {
2897 rport->scsi_target_id = fc_host->next_target_id++;
2898 create = 1;
James Smarta53eb5e2007-04-27 12:41:09 -04002899 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002900 create = 1;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002901 } else if (shost->active_mode & MODE_TARGET) {
2902 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
2903 (char *)&rport->node_name);
2904 if (ret)
2905 printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
2906 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002909 rport->roles = roles;
2910
James Smartaedf3492006-04-10 10:14:05 -04002911 spin_unlock_irqrestore(shost->host_lock, flags);
2912
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002913 if (create) {
2914 /*
2915 * There may have been a delete timer running on the
2916 * port. Ensure that it is cancelled as we now know
2917 * the port is an FCP Target.
2918 * Note: we know the rport is exists and in an online
2919 * state as the LLDD would not have had an rport
2920 * reference to pass us.
2921 *
2922 * Take no action on the del_timer failure as the state
2923 * machine state change will validate the
2924 * transaction.
2925 */
James Smart0f29b962006-08-18 17:33:29 -04002926 if (!cancel_delayed_work(&rport->fail_io_work))
2927 fc_flush_devloss(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002928 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002929 fc_flush_devloss(shost);
2930
2931 spin_lock_irqsave(shost->host_lock, flags);
2932 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2933 spin_unlock_irqrestore(shost->host_lock, flags);
2934
2935 /* ensure any stgt delete functions are done */
2936 fc_flush_work(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002939 spin_lock_irqsave(shost->host_lock, flags);
2940 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002942 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04002943 scsi_target_unblock(&rport->dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946EXPORT_SYMBOL(fc_remote_port_rolechg);
2947
2948/**
Rob Landleyeb448202007-11-03 13:30:39 -05002949 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
James Smart92740b22007-04-27 11:53:17 -04002950 * @work: rport target that failed to reappear in the allotted time.
Rob Landleyeb448202007-11-03 13:30:39 -05002951 *
2952 * Description: An attempt to delete a remote port blocks, and if it fails
2953 * to return in the allotted time this gets called.
2954 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955static void
David Howellsc4028952006-11-22 14:57:56 +00002956fc_timeout_deleted_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
David Howellsc4028952006-11-22 14:57:56 +00002958 struct fc_rport *rport =
2959 container_of(work, struct fc_rport, dev_loss_work.work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002960 struct Scsi_Host *shost = rport_to_shost(rport);
James Smartaedf3492006-04-10 10:14:05 -04002961 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002962 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002964 spin_lock_irqsave(shost->host_lock, flags);
2965
James Smartaedf3492006-04-10 10:14:05 -04002966 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2967
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002968 /*
James Smart92740b22007-04-27 11:53:17 -04002969 * If the port is ONLINE, then it came back. If it was a SCSI
2970 * target, validate it still is. If not, tear down the
2971 * scsi_target on it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002972 */
James Smartaedf3492006-04-10 10:14:05 -04002973 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
James Smart92740b22007-04-27 11:53:17 -04002974 (rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04002975 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002976 dev_printk(KERN_ERR, &rport->dev,
James Smartaedf3492006-04-10 10:14:05 -04002977 "blocked FC remote port time out: no longer"
2978 " a FCP target, removing starget\n");
James Smartaedf3492006-04-10 10:14:05 -04002979 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04002980 scsi_target_unblock(&rport->dev);
2981 fc_queue_work(shost, &rport->stgt_delete_work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002982 return;
2983 }
2984
James Smart92740b22007-04-27 11:53:17 -04002985 /* NOOP state - we're flushing workq's */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002986 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
2987 spin_unlock_irqrestore(shost->host_lock, flags);
2988 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04002989 "blocked FC remote port time out: leaving"
2990 " rport%s alone\n",
2991 (rport->scsi_target_id != -1) ? " and starget" : "");
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002992 return;
2993 }
2994
James Smart92740b22007-04-27 11:53:17 -04002995 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
2996 (rport->scsi_target_id == -1)) {
James Smartaedf3492006-04-10 10:14:05 -04002997 list_del(&rport->peers);
2998 rport->port_state = FC_PORTSTATE_DELETED;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002999 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003000 "blocked FC remote port time out: removing"
3001 " rport%s\n",
3002 (rport->scsi_target_id != -1) ? " and starget" : "");
James Smartaedf3492006-04-10 10:14:05 -04003003 fc_queue_work(shost, &rport->rport_delete_work);
3004 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003005 return;
3006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008 dev_printk(KERN_ERR, &rport->dev,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003009 "blocked FC remote port time out: removing target and "
3010 "saving binding\n");
3011
James Smartaedf3492006-04-10 10:14:05 -04003012 list_move_tail(&rport->peers, &fc_host->rport_bindings);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003013
3014 /*
3015 * Note: We do not remove or clear the hostdata area. This allows
3016 * host-specific target data to persist along with the
3017 * scsi_target_id. It's up to the host to manage it's hostdata area.
3018 */
3019
3020 /*
3021 * Reinitialize port attributes that may change if the port comes back.
3022 */
3023 rport->maxframe_size = -1;
3024 rport->supported_classes = FC_COS_UNSPECIFIED;
James Smarta53eb5e2007-04-27 12:41:09 -04003025 rport->roles = FC_PORT_ROLE_UNKNOWN;
James Smartaedf3492006-04-10 10:14:05 -04003026 rport->port_state = FC_PORTSTATE_NOTPRESENT;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003027
3028 /* remove the identifiers that aren't used in the consisting binding */
James Smartaedf3492006-04-10 10:14:05 -04003029 switch (fc_host->tgtid_bind_type) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003030 case FC_TGTID_BIND_BY_WWPN:
3031 rport->node_name = -1;
3032 rport->port_id = -1;
3033 break;
3034 case FC_TGTID_BIND_BY_WWNN:
3035 rport->port_name = -1;
3036 rport->port_id = -1;
3037 break;
3038 case FC_TGTID_BIND_BY_ID:
3039 rport->node_name = -1;
3040 rport->port_name = -1;
3041 break;
3042 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3043 break;
3044 }
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 /*
3047 * As this only occurs if the remote port (scsi target)
3048 * went away and didn't come back - we'll remove
3049 * all attached scsi devices.
3050 */
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003051 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04003052
3053 scsi_target_unblock(&rport->dev);
3054 fc_queue_work(shost, &rport->stgt_delete_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055}
3056
3057/**
Rob Landleyeb448202007-11-03 13:30:39 -05003058 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
David Howellsc4028952006-11-22 14:57:56 +00003059 * @work: rport to terminate io on.
James Smart0f29b962006-08-18 17:33:29 -04003060 *
3061 * Notes: Only requests the failure of the io, not that all are flushed
3062 * prior to returning.
Rob Landleyeb448202007-11-03 13:30:39 -05003063 */
James Smart0f29b962006-08-18 17:33:29 -04003064static void
David Howellsc4028952006-11-22 14:57:56 +00003065fc_timeout_fail_rport_io(struct work_struct *work)
James Smart0f29b962006-08-18 17:33:29 -04003066{
David Howellsc4028952006-11-22 14:57:56 +00003067 struct fc_rport *rport =
3068 container_of(work, struct fc_rport, fail_io_work.work);
James Smart0f29b962006-08-18 17:33:29 -04003069 struct Scsi_Host *shost = rport_to_shost(rport);
3070 struct fc_internal *i = to_fc_internal(shost->transportt);
3071
3072 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3073 return;
3074
3075 i->f->terminate_rport_io(rport);
3076}
3077
3078/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
David Howellsc4028952006-11-22 14:57:56 +00003080 * @work: remote port to be scanned.
Rob Landleyeb448202007-11-03 13:30:39 -05003081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082static void
David Howellsc4028952006-11-22 14:57:56 +00003083fc_scsi_scan_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
David Howellsc4028952006-11-22 14:57:56 +00003085 struct fc_rport *rport =
3086 container_of(work, struct fc_rport, scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003087 struct Scsi_Host *shost = rport_to_shost(rport);
Christof Schmitt03f002f2007-08-28 09:31:21 +02003088 struct fc_internal *i = to_fc_internal(shost->transportt);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003089 unsigned long flags;
3090
James Smartaedf3492006-04-10 10:14:05 -04003091 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
Christof Schmitt03f002f2007-08-28 09:31:21 +02003092 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3093 !(i->f->disable_target_scan)) {
James Smartaedf3492006-04-10 10:14:05 -04003094 scsi_scan_target(&rport->dev, rport->channel,
3095 rport->scsi_target_id, SCAN_WILD_CARD, 1);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003096 }
James Smartaedf3492006-04-10 10:14:05 -04003097
3098 spin_lock_irqsave(shost->host_lock, flags);
3099 rport->flags &= ~FC_RPORT_SCAN_PENDING;
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003100 spin_unlock_irqrestore(shost->host_lock, flags);
3101}
3102
3103
James Smarta53eb5e2007-04-27 12:41:09 -04003104/**
3105 * fc_vport_create - allocates and creates a FC virtual port.
3106 * @shost: scsi host the virtual port is connected to.
3107 * @channel: Channel on shost port connected to.
3108 * @pdev: parent device for vport
3109 * @ids: The world wide names, FC4 port roles, etc for
3110 * the virtual port.
3111 * @ret_vport: The pointer to the created vport.
3112 *
3113 * Allocates and creates the vport structure, calls the parent host
3114 * to instantiate the vport, the completes w/ class and sysfs creation.
3115 *
3116 * Notes:
3117 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003118 */
James Smarta53eb5e2007-04-27 12:41:09 -04003119static int
3120fc_vport_create(struct Scsi_Host *shost, int channel, struct device *pdev,
3121 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3122{
3123 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3124 struct fc_internal *fci = to_fc_internal(shost->transportt);
3125 struct fc_vport *vport;
3126 struct device *dev;
3127 unsigned long flags;
3128 size_t size;
3129 int error;
3130
3131 *ret_vport = NULL;
3132
3133 if ( ! fci->f->vport_create)
3134 return -ENOENT;
3135
3136 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3137 vport = kzalloc(size, GFP_KERNEL);
3138 if (unlikely(!vport)) {
3139 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
3140 return -ENOMEM;
3141 }
3142
3143 vport->vport_state = FC_VPORT_UNKNOWN;
3144 vport->vport_last_state = FC_VPORT_UNKNOWN;
3145 vport->node_name = ids->node_name;
3146 vport->port_name = ids->port_name;
3147 vport->roles = ids->roles;
3148 vport->vport_type = ids->vport_type;
3149 if (fci->f->dd_fcvport_size)
3150 vport->dd_data = &vport[1];
3151 vport->shost = shost;
3152 vport->channel = channel;
3153 vport->flags = FC_VPORT_CREATING;
James Smart9ef3e4a2007-05-24 19:04:44 -04003154 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
James Smarta53eb5e2007-04-27 12:41:09 -04003155
3156 spin_lock_irqsave(shost->host_lock, flags);
3157
3158 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3159 spin_unlock_irqrestore(shost->host_lock, flags);
3160 kfree(vport);
3161 return -ENOSPC;
3162 }
3163 fc_host->npiv_vports_inuse++;
3164 vport->number = fc_host->next_vport_number++;
3165 list_add_tail(&vport->peers, &fc_host->vports);
3166 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3167
3168 spin_unlock_irqrestore(shost->host_lock, flags);
3169
3170 dev = &vport->dev;
3171 device_initialize(dev); /* takes self reference */
3172 dev->parent = get_device(pdev); /* takes parent reference */
3173 dev->release = fc_vport_dev_release;
3174 sprintf(dev->bus_id, "vport-%d:%d-%d",
3175 shost->host_no, channel, vport->number);
3176 transport_setup_device(dev);
3177
3178 error = device_add(dev);
3179 if (error) {
3180 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3181 goto delete_vport;
3182 }
3183 transport_add_device(dev);
3184 transport_configure_device(dev);
3185
3186 error = fci->f->vport_create(vport, ids->disable);
3187 if (error) {
3188 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3189 goto delete_vport_all;
3190 }
3191
3192 /*
3193 * if the parent isn't the physical adapter's Scsi_Host, ensure
3194 * the Scsi_Host at least contains ia symlink to the vport.
3195 */
3196 if (pdev != &shost->shost_gendev) {
3197 error = sysfs_create_link(&shost->shost_gendev.kobj,
3198 &dev->kobj, dev->bus_id);
3199 if (error)
3200 printk(KERN_ERR
3201 "%s: Cannot create vport symlinks for "
3202 "%s, err=%d\n",
3203 __FUNCTION__, dev->bus_id, error);
3204 }
3205 spin_lock_irqsave(shost->host_lock, flags);
3206 vport->flags &= ~FC_VPORT_CREATING;
3207 spin_unlock_irqrestore(shost->host_lock, flags);
3208
3209 dev_printk(KERN_NOTICE, pdev,
3210 "%s created via shost%d channel %d\n", dev->bus_id,
3211 shost->host_no, channel);
3212
3213 *ret_vport = vport;
3214
3215 return 0;
3216
3217delete_vport_all:
3218 transport_remove_device(dev);
3219 device_del(dev);
3220delete_vport:
3221 transport_destroy_device(dev);
3222 spin_lock_irqsave(shost->host_lock, flags);
3223 list_del(&vport->peers);
3224 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3225 fc_host->npiv_vports_inuse--;
3226 spin_unlock_irqrestore(shost->host_lock, flags);
3227 put_device(dev->parent);
3228 kfree(vport);
3229
3230 return error;
3231}
3232
3233
3234/**
3235 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3236 * @vport: fc_vport to be terminated
3237 *
3238 * Calls the LLDD vport_delete() function, then deallocates and removes
3239 * the vport from the shost and object tree.
3240 *
3241 * Notes:
3242 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003243 */
James Smarta53eb5e2007-04-27 12:41:09 -04003244int
3245fc_vport_terminate(struct fc_vport *vport)
3246{
3247 struct Scsi_Host *shost = vport_to_shost(vport);
3248 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3249 struct fc_internal *i = to_fc_internal(shost->transportt);
3250 struct device *dev = &vport->dev;
3251 unsigned long flags;
3252 int stat;
3253
3254 spin_lock_irqsave(shost->host_lock, flags);
3255 if (vport->flags & FC_VPORT_CREATING) {
3256 spin_unlock_irqrestore(shost->host_lock, flags);
3257 return -EBUSY;
3258 }
3259 if (vport->flags & (FC_VPORT_DEL)) {
3260 spin_unlock_irqrestore(shost->host_lock, flags);
3261 return -EALREADY;
3262 }
3263 vport->flags |= FC_VPORT_DELETING;
3264 spin_unlock_irqrestore(shost->host_lock, flags);
3265
3266 if (i->f->vport_delete)
3267 stat = i->f->vport_delete(vport);
3268 else
3269 stat = -ENOENT;
3270
3271 spin_lock_irqsave(shost->host_lock, flags);
3272 vport->flags &= ~FC_VPORT_DELETING;
3273 if (!stat) {
3274 vport->flags |= FC_VPORT_DELETED;
3275 list_del(&vport->peers);
3276 fc_host->npiv_vports_inuse--;
3277 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3278 }
3279 spin_unlock_irqrestore(shost->host_lock, flags);
3280
3281 if (stat)
3282 return stat;
3283
3284 if (dev->parent != &shost->shost_gendev)
3285 sysfs_remove_link(&shost->shost_gendev.kobj, dev->bus_id);
3286 transport_remove_device(dev);
3287 device_del(dev);
3288 transport_destroy_device(dev);
3289
3290 /*
3291 * Removing our self-reference should mean our
3292 * release function gets called, which will drop the remaining
3293 * parent reference and free the data structure.
3294 */
3295 put_device(dev); /* for self-reference */
3296
3297 return 0; /* SUCCESS */
3298}
3299EXPORT_SYMBOL(fc_vport_terminate);
3300
James Smart9ef3e4a2007-05-24 19:04:44 -04003301/**
3302 * fc_vport_sched_delete - workq-based delete request for a vport
James Smart9ef3e4a2007-05-24 19:04:44 -04003303 * @work: vport to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05003304 */
James Smart9ef3e4a2007-05-24 19:04:44 -04003305static void
3306fc_vport_sched_delete(struct work_struct *work)
3307{
3308 struct fc_vport *vport =
3309 container_of(work, struct fc_vport, vport_delete_work);
3310 int stat;
James Smarta53eb5e2007-04-27 12:41:09 -04003311
James Smart9ef3e4a2007-05-24 19:04:44 -04003312 stat = fc_vport_terminate(vport);
3313 if (stat)
3314 dev_printk(KERN_ERR, vport->dev.parent,
3315 "%s: %s could not be deleted created via "
3316 "shost%d channel %d - error %d\n", __FUNCTION__,
3317 vport->dev.bus_id, vport->shost->host_no,
3318 vport->channel, stat);
3319}
3320
3321
3322/* Original Author: Martin Hicks */
3323MODULE_AUTHOR("James Smart");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324MODULE_DESCRIPTION("FC Transport Attributes");
3325MODULE_LICENSE("GPL");
3326
3327module_init(fc_transport_init);
3328module_exit(fc_transport_exit);