blob: ddfcecd5099f5688d1ec7667d050eb1671e78267 [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>
Christof Schmitt65d430f2009-10-30 17:59:29 +010030#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <scsi/scsi_device.h>
32#include <scsi/scsi_host.h>
33#include <scsi/scsi_transport.h>
34#include <scsi/scsi_transport_fc.h>
James Smartc829c392006-03-13 08:28:57 -050035#include <scsi/scsi_cmnd.h>
James Smart84314fd2006-08-18 17:30:09 -040036#include <linux/netlink.h>
37#include <net/netlink.h>
38#include <scsi/scsi_netlink_fc.h>
James Smart9e4f5e22009-03-26 13:33:19 -040039#include <scsi/scsi_bsg_fc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "scsi_priv.h"
FUJITA Tomonori75252362007-09-01 02:02:27 +090041#include "scsi_transport_fc_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
James Smartaedf3492006-04-10 10:14:05 -040043static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
James Smart9ef3e4a2007-05-24 19:04:44 -040044static void fc_vport_sched_delete(struct work_struct *work);
Andrew Vasqueza30c3f62008-07-18 08:32:52 -070045static int fc_vport_setup(struct Scsi_Host *shost, int channel,
James Smarta53eb5e2007-04-27 12:41:09 -040046 struct device *pdev, struct fc_vport_identifiers *ids,
47 struct fc_vport **vport);
James Smart9e4f5e22009-03-26 13:33:19 -040048static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
49static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
50static void fc_bsg_remove(struct request_queue *);
51static void fc_bsg_goose_queue(struct fc_rport *);
James Smarta53eb5e2007-04-27 12:41:09 -040052
53/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * Redefine so that we can have same named attributes in the
55 * sdev/starget/host objects.
56 */
Tony Jonesee959b02008-02-22 00:13:36 +010057#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
58struct device_attribute device_attr_##_prefix##_##_name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 __ATTR(_name,_mode,_show,_store)
60
61#define fc_enum_name_search(title, table_type, table) \
62static const char *get_fc_##title##_name(enum table_type table_key) \
63{ \
64 int i; \
65 char *name = NULL; \
66 \
Tobias Klauser6391a112006-06-08 22:23:48 -070067 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (table[i].value == table_key) { \
69 name = table[i].name; \
70 break; \
71 } \
72 } \
73 return name; \
74}
75
76#define fc_enum_name_match(title, table_type, table) \
77static int get_fc_##title##_match(const char *table_key, \
78 enum table_type *value) \
79{ \
80 int i; \
81 \
Tobias Klauser6391a112006-06-08 22:23:48 -070082 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (strncmp(table_key, table[i].name, \
84 table[i].matchlen) == 0) { \
85 *value = table[i].value; \
86 return 0; /* success */ \
87 } \
88 } \
89 return 1; /* failure */ \
90}
91
92
93/* Convert fc_port_type values to ascii string name */
94static struct {
95 enum fc_port_type value;
96 char *name;
97} fc_port_type_names[] = {
98 { FC_PORTTYPE_UNKNOWN, "Unknown" },
99 { FC_PORTTYPE_OTHER, "Other" },
100 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
101 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
102 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
103 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
Christof Schmitt951948a2009-01-15 16:51:48 +0100104 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
James Smarta53eb5e2007-04-27 12:41:09 -0400105 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
108#define FC_PORTTYPE_MAX_NAMELEN 50
109
James Smarta53eb5e2007-04-27 12:41:09 -0400110/* Reuse fc_port_type enum function for vport_type */
111#define get_fc_vport_type_name get_fc_port_type_name
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
James Smart84314fd2006-08-18 17:30:09 -0400114/* Convert fc_host_event_code values to ascii string name */
115static const struct {
116 enum fc_host_event_code value;
117 char *name;
118} fc_host_event_code_names[] = {
119 { FCH_EVT_LIP, "lip" },
120 { FCH_EVT_LINKUP, "link_up" },
121 { FCH_EVT_LINKDOWN, "link_down" },
122 { FCH_EVT_LIPRESET, "lip_reset" },
123 { FCH_EVT_RSCN, "rscn" },
124 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
125 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
126 { FCH_EVT_PORT_ONLINE, "port_online" },
127 { FCH_EVT_PORT_OFFLINE, "port_offline" },
128 { FCH_EVT_PORT_FABRIC, "port_fabric" },
129 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
130 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
131};
132fc_enum_name_search(host_event_code, fc_host_event_code,
133 fc_host_event_code_names)
134#define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
135
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/* Convert fc_port_state values to ascii string name */
138static struct {
139 enum fc_port_state value;
140 char *name;
141} fc_port_state_names[] = {
142 { FC_PORTSTATE_UNKNOWN, "Unknown" },
143 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
144 { FC_PORTSTATE_ONLINE, "Online" },
145 { FC_PORTSTATE_OFFLINE, "Offline" },
146 { FC_PORTSTATE_BLOCKED, "Blocked" },
147 { FC_PORTSTATE_BYPASSED, "Bypassed" },
148 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
149 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
150 { FC_PORTSTATE_ERROR, "Error" },
151 { FC_PORTSTATE_LOOPBACK, "Loopback" },
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500152 { FC_PORTSTATE_DELETED, "Deleted" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
155#define FC_PORTSTATE_MAX_NAMELEN 20
156
157
James Smarta53eb5e2007-04-27 12:41:09 -0400158/* Convert fc_vport_state values to ascii string name */
159static struct {
160 enum fc_vport_state value;
161 char *name;
162} fc_vport_state_names[] = {
163 { FC_VPORT_UNKNOWN, "Unknown" },
164 { FC_VPORT_ACTIVE, "Active" },
165 { FC_VPORT_DISABLED, "Disabled" },
166 { FC_VPORT_LINKDOWN, "Linkdown" },
167 { FC_VPORT_INITIALIZING, "Initializing" },
168 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
169 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
170 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
171 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
172 { FC_VPORT_FAILED, "VPort Failed" },
173};
174fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
175#define FC_VPORTSTATE_MAX_NAMELEN 24
176
177/* Reuse fc_vport_state enum function for vport_last_state */
178#define get_fc_vport_last_state_name get_fc_vport_state_name
179
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* Convert fc_tgtid_binding_type values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100182static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 enum fc_tgtid_binding_type value;
184 char *name;
185 int matchlen;
186} fc_tgtid_binding_type_names[] = {
187 { FC_TGTID_BIND_NONE, "none", 4 },
188 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
189 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
190 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
191};
192fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
193 fc_tgtid_binding_type_names)
194fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
195 fc_tgtid_binding_type_names)
196#define FC_BINDTYPE_MAX_NAMELEN 30
197
198
199#define fc_bitfield_name_search(title, table) \
200static ssize_t \
201get_fc_##title##_names(u32 table_key, char *buf) \
202{ \
203 char *prefix = ""; \
204 ssize_t len = 0; \
205 int i; \
206 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700207 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (table[i].value & table_key) { \
209 len += sprintf(buf + len, "%s%s", \
210 prefix, table[i].name); \
211 prefix = ", "; \
212 } \
213 } \
214 len += sprintf(buf + len, "\n"); \
215 return len; \
216}
217
218
219/* Convert FC_COS bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100220static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 u32 value;
222 char *name;
223} fc_cos_names[] = {
224 { FC_COS_CLASS1, "Class 1" },
225 { FC_COS_CLASS2, "Class 2" },
226 { FC_COS_CLASS3, "Class 3" },
227 { FC_COS_CLASS4, "Class 4" },
228 { FC_COS_CLASS6, "Class 6" },
229};
230fc_bitfield_name_search(cos, fc_cos_names)
231
232
233/* Convert FC_PORTSPEED bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100234static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 u32 value;
236 char *name;
237} fc_port_speed_names[] = {
238 { FC_PORTSPEED_1GBIT, "1 Gbit" },
239 { FC_PORTSPEED_2GBIT, "2 Gbit" },
240 { FC_PORTSPEED_4GBIT, "4 Gbit" },
241 { FC_PORTSPEED_10GBIT, "10 Gbit" },
James Smartc3d23502007-03-12 14:16:35 -0500242 { FC_PORTSPEED_8GBIT, "8 Gbit" },
243 { FC_PORTSPEED_16GBIT, "16 Gbit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
245};
246fc_bitfield_name_search(port_speed, fc_port_speed_names)
247
248
249static int
250show_fc_fc4s (char *buf, u8 *fc4_list)
251{
252 int i, len=0;
253
254 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
255 len += sprintf(buf + len , "0x%02x ", *fc4_list);
256 len += sprintf(buf + len, "\n");
257 return len;
258}
259
260
James Smarta53eb5e2007-04-27 12:41:09 -0400261/* Convert FC_PORT_ROLE bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100262static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 u32 value;
264 char *name;
James Smarta53eb5e2007-04-27 12:41:09 -0400265} fc_port_role_names[] = {
266 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
267 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
268 { FC_PORT_ROLE_IP_PORT, "IP Port" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
James Smarta53eb5e2007-04-27 12:41:09 -0400270fc_bitfield_name_search(port_roles, fc_port_role_names)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/*
273 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
274 */
275#define FC_WELLKNOWN_PORTID_MASK 0xfffff0
276#define FC_WELLKNOWN_ROLE_MASK 0x00000f
277#define FC_FPORT_PORTID 0x00000e
278#define FC_FABCTLR_PORTID 0x00000d
279#define FC_DIRSRVR_PORTID 0x00000c
280#define FC_TIMESRVR_PORTID 0x00000b
281#define FC_MGMTSRVR_PORTID 0x00000a
282
283
David Howellsc4028952006-11-22 14:57:56 +0000284static void fc_timeout_deleted_rport(struct work_struct *work);
285static void fc_timeout_fail_rport_io(struct work_struct *work);
286static void fc_scsi_scan_rport(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/*
289 * Attribute counts pre object type...
290 * Increase these values if you add attributes
291 */
292#define FC_STARGET_NUM_ATTRS 3
James Smart0f29b962006-08-18 17:33:29 -0400293#define FC_RPORT_NUM_ATTRS 10
James Smarta53eb5e2007-04-27 12:41:09 -0400294#define FC_VPORT_NUM_ATTRS 9
James Smart46436822009-07-28 12:30:01 -0400295#define FC_HOST_NUM_ATTRS 22
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297struct fc_internal {
298 struct scsi_transport_template t;
299 struct fc_function_template *f;
300
301 /*
302 * For attributes : each object has :
303 * An array of the actual attributes structures
304 * An array of null-terminated pointers to the attribute
305 * structures - used for mid-layer interaction.
306 *
307 * The attribute containers for the starget and host are are
308 * part of the midlayer. As the remote port is specific to the
309 * fc transport, we must provide the attribute container.
310 */
Tony Jonesee959b02008-02-22 00:13:36 +0100311 struct device_attribute private_starget_attrs[
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 FC_STARGET_NUM_ATTRS];
Tony Jonesee959b02008-02-22 00:13:36 +0100313 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Tony Jonesee959b02008-02-22 00:13:36 +0100315 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
316 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 struct transport_container rport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100319 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
320 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
James Smarta53eb5e2007-04-27 12:41:09 -0400321
322 struct transport_container vport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100323 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
324 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325};
326
327#define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
328
James Bottomleyd0a7e572005-08-14 17:09:01 -0500329static int fc_target_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100330 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct scsi_target *starget = to_scsi_target(dev);
333 struct fc_rport *rport = starget_to_rport(starget);
334
335 /*
336 * if parent is remote port, use values from remote port.
337 * Otherwise, this host uses the fc_transport, but not the
338 * remote port interface. As such, initialize to known non-values.
339 */
340 if (rport) {
341 fc_starget_node_name(starget) = rport->node_name;
342 fc_starget_port_name(starget) = rport->port_name;
343 fc_starget_port_id(starget) = rport->port_id;
344 } else {
345 fc_starget_node_name(starget) = -1;
346 fc_starget_port_name(starget) = -1;
347 fc_starget_port_id(starget) = -1;
348 }
349
350 return 0;
351}
352
353static DECLARE_TRANSPORT_CLASS(fc_transport_class,
354 "fc_transport",
355 fc_target_setup,
356 NULL,
357 NULL);
358
James Bottomleyd0a7e572005-08-14 17:09:01 -0500359static int fc_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100360 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct Scsi_Host *shost = dev_to_shost(dev);
James Smartaedf3492006-04-10 10:14:05 -0400363 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
James Smart9ef3e4a2007-05-24 19:04:44 -0400365 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * Set default values easily detected by the midlayer as
367 * failure cases. The scsi lldd is responsible for initializing
368 * all transport attributes to valid values per host.
369 */
James Smartaedf3492006-04-10 10:14:05 -0400370 fc_host->node_name = -1;
371 fc_host->port_name = -1;
372 fc_host->permanent_port_name = -1;
373 fc_host->supported_classes = FC_COS_UNSPECIFIED;
374 memset(fc_host->supported_fc4s, 0,
375 sizeof(fc_host->supported_fc4s));
James Smartaedf3492006-04-10 10:14:05 -0400376 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
377 fc_host->maxframe_size = -1;
James Smarta53eb5e2007-04-27 12:41:09 -0400378 fc_host->max_npiv_vports = 0;
James Smartaedf3492006-04-10 10:14:05 -0400379 memset(fc_host->serial_number, 0,
380 sizeof(fc_host->serial_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
James Smartaedf3492006-04-10 10:14:05 -0400382 fc_host->port_id = -1;
383 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
384 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
385 memset(fc_host->active_fc4s, 0,
386 sizeof(fc_host->active_fc4s));
387 fc_host->speed = FC_PORTSPEED_UNKNOWN;
388 fc_host->fabric_name = -1;
James Smartb8d08212006-08-17 08:00:43 -0400389 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
390 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
James Smartaedf3492006-04-10 10:14:05 -0400392 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
James Smartaedf3492006-04-10 10:14:05 -0400394 INIT_LIST_HEAD(&fc_host->rports);
395 INIT_LIST_HEAD(&fc_host->rport_bindings);
James Smarta53eb5e2007-04-27 12:41:09 -0400396 INIT_LIST_HEAD(&fc_host->vports);
James Smartaedf3492006-04-10 10:14:05 -0400397 fc_host->next_rport_number = 0;
398 fc_host->next_target_id = 0;
James Smarta53eb5e2007-04-27 12:41:09 -0400399 fc_host->next_vport_number = 0;
400 fc_host->npiv_vports_inuse = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Kay Sieversaab0de22008-05-02 06:02:41 +0200402 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
403 "fc_wq_%d", shost->host_no);
James Smartaedf3492006-04-10 10:14:05 -0400404 fc_host->work_q = create_singlethread_workqueue(
405 fc_host->work_q_name);
406 if (!fc_host->work_q)
407 return -ENOMEM;
408
Kay Sieversaab0de22008-05-02 06:02:41 +0200409 snprintf(fc_host->devloss_work_q_name,
410 sizeof(fc_host->devloss_work_q_name),
411 "fc_dl_%d", shost->host_no);
James Smartaedf3492006-04-10 10:14:05 -0400412 fc_host->devloss_work_q = create_singlethread_workqueue(
413 fc_host->devloss_work_q_name);
414 if (!fc_host->devloss_work_q) {
415 destroy_workqueue(fc_host->work_q);
416 fc_host->work_q = NULL;
417 return -ENOMEM;
418 }
419
James Smart9e4f5e22009-03-26 13:33:19 -0400420 fc_bsg_hostadd(shost, fc_host);
421 /* ignore any bsg add error - we just can't do sgio */
422
423 return 0;
424}
425
426static int fc_host_remove(struct transport_container *tc, struct device *dev,
427 struct device *cdev)
428{
429 struct Scsi_Host *shost = dev_to_shost(dev);
430 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
431
432 fc_bsg_remove(fc_host->rqst_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
434}
435
436static DECLARE_TRANSPORT_CLASS(fc_host_class,
437 "fc_host",
438 fc_host_setup,
James Smart9e4f5e22009-03-26 13:33:19 -0400439 fc_host_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 NULL);
441
442/*
443 * Setup and Remove actions for remote ports are handled
444 * in the service functions below.
445 */
446static DECLARE_TRANSPORT_CLASS(fc_rport_class,
447 "fc_remote_ports",
448 NULL,
449 NULL,
450 NULL);
451
452/*
James Smarta53eb5e2007-04-27 12:41:09 -0400453 * Setup and Remove actions for virtual ports are handled
454 * in the service functions below.
455 */
456static DECLARE_TRANSPORT_CLASS(fc_vport_class,
457 "fc_vports",
458 NULL,
459 NULL,
460 NULL);
461
462/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * Module Parameters
464 */
465
466/*
467 * dev_loss_tmo: the default number of seconds that the FC transport
468 * should insulate the loss of a remote port.
469 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
470 */
James Smart1c9e16e2006-05-16 16:13:36 -0400471static unsigned int fc_dev_loss_tmo = 60; /* seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Masatake YAMATO10f4b892007-09-19 22:59:16 +0900473module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474MODULE_PARM_DESC(dev_loss_tmo,
475 "Maximum number of seconds that the FC transport should"
476 " insulate the loss of a remote port. Once this value is"
477 " exceeded, the scsi target is removed. Value should be"
478 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
479
Rob Landleyeb448202007-11-03 13:30:39 -0500480/*
James Smart84314fd2006-08-18 17:30:09 -0400481 * Netlink Infrastructure
Rob Landleyeb448202007-11-03 13:30:39 -0500482 */
James Smart84314fd2006-08-18 17:30:09 -0400483
484static atomic_t fc_event_seq;
485
486/**
487 * fc_get_event_number - Obtain the next sequential FC event number
488 *
489 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -0500490 * We could have inlined this, but it would have required fc_event_seq to
James Smart84314fd2006-08-18 17:30:09 -0400491 * be exposed. For now, live with the subroutine call.
492 * Atomic used to avoid lock/unlock...
Rob Landleyeb448202007-11-03 13:30:39 -0500493 */
James Smart84314fd2006-08-18 17:30:09 -0400494u32
495fc_get_event_number(void)
496{
497 return atomic_add_return(1, &fc_event_seq);
498}
499EXPORT_SYMBOL(fc_get_event_number);
500
501
502/**
503 * fc_host_post_event - called to post an even on an fc_host.
James Smart84314fd2006-08-18 17:30:09 -0400504 * @shost: host the event occurred on
505 * @event_number: fc event number obtained from get_fc_event_number()
506 * @event_code: fc_host event being posted
507 * @event_data: 32bits of data for the event being posted
508 *
509 * Notes:
510 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500511 */
James Smart84314fd2006-08-18 17:30:09 -0400512void
513fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
514 enum fc_host_event_code event_code, u32 event_data)
515{
516 struct sk_buff *skb;
517 struct nlmsghdr *nlh;
518 struct fc_nl_event *event;
519 const char *name;
520 u32 len, skblen;
521 int err;
522
523 if (!scsi_nl_sock) {
524 err = -ENOENT;
525 goto send_fail;
526 }
527
528 len = FC_NL_MSGALIGN(sizeof(*event));
529 skblen = NLMSG_SPACE(len);
530
531 skb = alloc_skb(skblen, GFP_KERNEL);
532 if (!skb) {
533 err = -ENOBUFS;
534 goto send_fail;
535 }
536
537 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
538 skblen - sizeof(*nlh), 0);
539 if (!nlh) {
540 err = -ENOBUFS;
541 goto send_fail_skb;
542 }
543 event = NLMSG_DATA(nlh);
544
545 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
546 FC_NL_ASYNC_EVENT, len);
547 event->seconds = get_seconds();
548 event->vendor_id = 0;
549 event->host_no = shost->host_no;
550 event->event_datalen = sizeof(u32); /* bytes */
551 event->event_num = event_number;
552 event->event_code = event_code;
553 event->event_data = event_data;
554
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800555 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
556 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400557 return;
558
559send_fail_skb:
560 kfree_skb(skb);
561send_fail:
562 name = get_fc_host_event_code_name(event_code);
563 printk(KERN_WARNING
564 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700565 __func__, shost->host_no,
James Smart84314fd2006-08-18 17:30:09 -0400566 (name) ? name : "<unknown>", event_data, err);
567 return;
568}
569EXPORT_SYMBOL(fc_host_post_event);
570
571
572/**
Rob Landleyeb448202007-11-03 13:30:39 -0500573 * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
James Smart84314fd2006-08-18 17:30:09 -0400574 * @shost: host the event occurred on
575 * @event_number: fc event number obtained from get_fc_event_number()
576 * @data_len: amount, in bytes, of vendor unique data
577 * @data_buf: pointer to vendor unique data
Rob Landleyeb448202007-11-03 13:30:39 -0500578 * @vendor_id: Vendor id
James Smart84314fd2006-08-18 17:30:09 -0400579 *
580 * Notes:
581 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500582 */
James Smart84314fd2006-08-18 17:30:09 -0400583void
584fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
James Smartf14e2e22006-08-22 09:55:23 -0400585 u32 data_len, char * data_buf, u64 vendor_id)
James Smart84314fd2006-08-18 17:30:09 -0400586{
587 struct sk_buff *skb;
588 struct nlmsghdr *nlh;
589 struct fc_nl_event *event;
590 u32 len, skblen;
591 int err;
592
593 if (!scsi_nl_sock) {
594 err = -ENOENT;
595 goto send_vendor_fail;
596 }
597
598 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
599 skblen = NLMSG_SPACE(len);
600
601 skb = alloc_skb(skblen, GFP_KERNEL);
602 if (!skb) {
603 err = -ENOBUFS;
604 goto send_vendor_fail;
605 }
606
607 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
608 skblen - sizeof(*nlh), 0);
609 if (!nlh) {
610 err = -ENOBUFS;
611 goto send_vendor_fail_skb;
612 }
613 event = NLMSG_DATA(nlh);
614
615 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
616 FC_NL_ASYNC_EVENT, len);
617 event->seconds = get_seconds();
618 event->vendor_id = vendor_id;
619 event->host_no = shost->host_no;
620 event->event_datalen = data_len; /* bytes */
621 event->event_num = event_number;
622 event->event_code = FCH_EVT_VENDOR_UNIQUE;
623 memcpy(&event->event_data, data_buf, data_len);
624
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800625 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
626 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400627 return;
628
629send_vendor_fail_skb:
630 kfree_skb(skb);
631send_vendor_fail:
632 printk(KERN_WARNING
633 "%s: Dropped Event : host %d vendor_unique - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700634 __func__, shost->host_no, err);
James Smart84314fd2006-08-18 17:30:09 -0400635 return;
636}
637EXPORT_SYMBOL(fc_host_post_vendor_event);
638
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641static __init int fc_transport_init(void)
642{
James Smart84314fd2006-08-18 17:30:09 -0400643 int error;
644
645 atomic_set(&fc_event_seq, 0);
646
647 error = transport_class_register(&fc_host_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (error)
649 return error;
James Smarta53eb5e2007-04-27 12:41:09 -0400650 error = transport_class_register(&fc_vport_class);
651 if (error)
Mike Christie48de68a2009-11-17 21:25:16 -0600652 goto unreg_host_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 error = transport_class_register(&fc_rport_class);
654 if (error)
Mike Christie48de68a2009-11-17 21:25:16 -0600655 goto unreg_vport_class;
656 error = transport_class_register(&fc_transport_class);
657 if (error)
658 goto unreg_rport_class;
659 return 0;
660
661unreg_rport_class:
662 transport_class_unregister(&fc_rport_class);
663unreg_vport_class:
664 transport_class_unregister(&fc_vport_class);
665unreg_host_class:
666 transport_class_unregister(&fc_host_class);
667 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670static void __exit fc_transport_exit(void)
671{
672 transport_class_unregister(&fc_transport_class);
673 transport_class_unregister(&fc_rport_class);
674 transport_class_unregister(&fc_host_class);
James Smarta53eb5e2007-04-27 12:41:09 -0400675 transport_class_unregister(&fc_vport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678/*
679 * FC Remote Port Attribute Management
680 */
681
682#define fc_rport_show_function(field, format_string, sz, cast) \
683static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100684show_fc_rport_##field (struct device *dev, \
685 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100687 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct Scsi_Host *shost = rport_to_shost(rport); \
689 struct fc_internal *i = to_fc_internal(shost->transportt); \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400690 if ((i->f->get_rport_##field) && \
691 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500692 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400693 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 i->f->get_rport_##field(rport); \
695 return snprintf(buf, sz, format_string, cast rport->field); \
696}
697
698#define fc_rport_store_function(field) \
699static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100700store_fc_rport_##field(struct device *dev, \
701 struct device_attribute *attr, \
702 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{ \
704 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100705 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct Scsi_Host *shost = rport_to_shost(rport); \
707 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -0400708 char *cp; \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400709 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500710 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400711 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
712 return -EBUSY; \
James Smart0f29b962006-08-18 17:33:29 -0400713 val = simple_strtoul(buf, &cp, 0); \
714 if (*cp && (*cp != '\n')) \
715 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 i->f->set_rport_##field(rport, val); \
717 return count; \
718}
719
720#define fc_rport_rd_attr(field, format_string, sz) \
721 fc_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100722static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 show_fc_rport_##field, NULL)
724
725#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
726 fc_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100727static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 show_fc_rport_##field, NULL)
729
730#define fc_rport_rw_attr(field, format_string, sz) \
731 fc_rport_show_function(field, format_string, sz, ) \
732 fc_rport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100733static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 show_fc_rport_##field, \
735 store_fc_rport_##field)
736
737
738#define fc_private_rport_show_function(field, format_string, sz, cast) \
739static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100740show_fc_rport_##field (struct device *dev, \
741 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100743 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return snprintf(buf, sz, format_string, cast rport->field); \
745}
746
747#define fc_private_rport_rd_attr(field, format_string, sz) \
748 fc_private_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100749static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 show_fc_rport_##field, NULL)
751
752#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
753 fc_private_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100754static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 show_fc_rport_##field, NULL)
756
757
758#define fc_private_rport_rd_enum_attr(title, maxlen) \
759static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100760show_fc_rport_##title (struct device *dev, \
761 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100763 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 const char *name; \
765 name = get_fc_##title##_name(rport->title); \
766 if (!name) \
767 return -EINVAL; \
768 return snprintf(buf, maxlen, "%s\n", name); \
769} \
Tony Jonesee959b02008-02-22 00:13:36 +0100770static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 show_fc_rport_##title, NULL)
772
773
774#define SETUP_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100775 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
777 i->private_rport_attrs[count].store = NULL; \
778 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
779 if (i->f->show_rport_##field) \
780 count++
781
782#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100783 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
785 i->private_rport_attrs[count].store = NULL; \
786 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
787 count++
788
789#define SETUP_RPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100790 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (!i->f->set_rport_##field) { \
792 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
793 i->private_rport_attrs[count].store = NULL; \
794 } \
795 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
796 if (i->f->show_rport_##field) \
797 count++
798
James Smart0f29b962006-08-18 17:33:29 -0400799#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
800{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100801 i->private_rport_attrs[count] = device_attr_rport_##field; \
James Smart0f29b962006-08-18 17:33:29 -0400802 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
803 count++; \
804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807/* The FC Transport Remote Port Attributes: */
808
809/* Fixed Remote Port Attributes */
810
811fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
812
813static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100814show_fc_rport_supported_classes (struct device *dev,
815 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Tony Jonesee959b02008-02-22 00:13:36 +0100817 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (rport->supported_classes == FC_COS_UNSPECIFIED)
819 return snprintf(buf, 20, "unspecified\n");
820 return get_fc_cos_names(rport->supported_classes, buf);
821}
Tony Jonesee959b02008-02-22 00:13:36 +0100822static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 show_fc_rport_supported_classes, NULL);
824
825/* Dynamic Remote Port Attributes */
826
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400827/*
828 * dev_loss_tmo attribute
829 */
830fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
831static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100832store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
833 const char *buf, size_t count)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400834{
835 int val;
Tony Jonesee959b02008-02-22 00:13:36 +0100836 struct fc_rport *rport = transport_class_to_rport(dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400837 struct Scsi_Host *shost = rport_to_shost(rport);
838 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart0f29b962006-08-18 17:33:29 -0400839 char *cp;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400840 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500841 (rport->port_state == FC_PORTSTATE_DELETED) ||
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400842 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
843 return -EBUSY;
James Smart0f29b962006-08-18 17:33:29 -0400844 val = simple_strtoul(buf, &cp, 0);
845 if ((*cp && (*cp != '\n')) ||
846 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400847 return -EINVAL;
848 i->f->set_rport_dev_loss_tmo(rport, val);
849 return count;
850}
Tony Jonesee959b02008-02-22 00:13:36 +0100851static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400852 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854
855/* Private Remote Port Attributes */
856
857fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
858fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
859fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
860
861static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100862show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
863 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Tony Jonesee959b02008-02-22 00:13:36 +0100865 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /* identify any roles that are port_id specific */
868 if ((rport->port_id != -1) &&
869 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
870 FC_WELLKNOWN_PORTID_MASK) {
871 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
872 case FC_FPORT_PORTID:
873 return snprintf(buf, 30, "Fabric Port\n");
874 case FC_FABCTLR_PORTID:
875 return snprintf(buf, 30, "Fabric Controller\n");
876 case FC_DIRSRVR_PORTID:
877 return snprintf(buf, 30, "Directory Server\n");
878 case FC_TIMESRVR_PORTID:
879 return snprintf(buf, 30, "Time Server\n");
880 case FC_MGMTSRVR_PORTID:
881 return snprintf(buf, 30, "Management Server\n");
882 default:
883 return snprintf(buf, 30, "Unknown Fabric Entity\n");
884 }
885 } else {
James Smarta53eb5e2007-04-27 12:41:09 -0400886 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return snprintf(buf, 20, "unknown\n");
James Smarta53eb5e2007-04-27 12:41:09 -0400888 return get_fc_port_roles_names(rport->roles, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890}
Tony Jonesee959b02008-02-22 00:13:36 +0100891static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 show_fc_rport_roles, NULL);
893
894fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
895fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
896
James Smart0f29b962006-08-18 17:33:29 -0400897/*
898 * fast_io_fail_tmo attribute
899 */
900static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100901show_fc_rport_fast_io_fail_tmo (struct device *dev,
902 struct device_attribute *attr, char *buf)
James Smart0f29b962006-08-18 17:33:29 -0400903{
Tony Jonesee959b02008-02-22 00:13:36 +0100904 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400905
906 if (rport->fast_io_fail_tmo == -1)
907 return snprintf(buf, 5, "off\n");
908 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
909}
910
911static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100912store_fc_rport_fast_io_fail_tmo(struct device *dev,
913 struct device_attribute *attr, const char *buf,
914 size_t count)
James Smart0f29b962006-08-18 17:33:29 -0400915{
916 int val;
917 char *cp;
Tony Jonesee959b02008-02-22 00:13:36 +0100918 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400919
920 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
921 (rport->port_state == FC_PORTSTATE_DELETED) ||
922 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
923 return -EBUSY;
924 if (strncmp(buf, "off", 3) == 0)
925 rport->fast_io_fail_tmo = -1;
926 else {
927 val = simple_strtoul(buf, &cp, 0);
928 if ((*cp && (*cp != '\n')) ||
929 (val < 0) || (val >= rport->dev_loss_tmo))
930 return -EINVAL;
931 rport->fast_io_fail_tmo = val;
932 }
933 return count;
934}
Tony Jonesee959b02008-02-22 00:13:36 +0100935static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
James Smart0f29b962006-08-18 17:33:29 -0400936 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938
939/*
940 * FC SCSI Target Attribute Management
941 */
942
943/*
944 * Note: in the target show function we recognize when the remote
James Smarta53eb5e2007-04-27 12:41:09 -0400945 * port is in the heirarchy and do not allow the driver to get
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * involved in sysfs functions. The driver only gets involved if
947 * it's the "old" style that doesn't use rports.
948 */
949#define fc_starget_show_function(field, format_string, sz, cast) \
950static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100951show_fc_starget_##field (struct device *dev, \
952 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100954 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
956 struct fc_internal *i = to_fc_internal(shost->transportt); \
957 struct fc_rport *rport = starget_to_rport(starget); \
958 if (rport) \
959 fc_starget_##field(starget) = rport->field; \
960 else if (i->f->get_starget_##field) \
961 i->f->get_starget_##field(starget); \
962 return snprintf(buf, sz, format_string, \
963 cast fc_starget_##field(starget)); \
964}
965
966#define fc_starget_rd_attr(field, format_string, sz) \
967 fc_starget_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100968static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 show_fc_starget_##field, NULL)
970
971#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
972 fc_starget_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100973static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 show_fc_starget_##field, NULL)
975
976#define SETUP_STARGET_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100977 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
979 i->private_starget_attrs[count].store = NULL; \
980 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
981 if (i->f->show_starget_##field) \
982 count++
983
984#define SETUP_STARGET_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100985 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (!i->f->set_starget_##field) { \
987 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
988 i->private_starget_attrs[count].store = NULL; \
989 } \
990 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
991 if (i->f->show_starget_##field) \
992 count++
993
994/* The FC Transport SCSI Target Attributes: */
995fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
996fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
997fc_starget_rd_attr(port_id, "0x%06x\n", 20);
998
999
1000/*
James Smarta53eb5e2007-04-27 12:41:09 -04001001 * FC Virtual Port Attribute Management
1002 */
1003
1004#define fc_vport_show_function(field, format_string, sz, cast) \
1005static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001006show_fc_vport_##field (struct device *dev, \
1007 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001008{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001009 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001010 struct Scsi_Host *shost = vport_to_shost(vport); \
1011 struct fc_internal *i = to_fc_internal(shost->transportt); \
1012 if ((i->f->get_vport_##field) && \
1013 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1014 i->f->get_vport_##field(vport); \
1015 return snprintf(buf, sz, format_string, cast vport->field); \
1016}
1017
1018#define fc_vport_store_function(field) \
1019static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001020store_fc_vport_##field(struct device *dev, \
1021 struct device_attribute *attr, \
1022 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001023{ \
1024 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001025 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001026 struct Scsi_Host *shost = vport_to_shost(vport); \
1027 struct fc_internal *i = to_fc_internal(shost->transportt); \
1028 char *cp; \
1029 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1030 return -EBUSY; \
1031 val = simple_strtoul(buf, &cp, 0); \
1032 if (*cp && (*cp != '\n')) \
1033 return -EINVAL; \
1034 i->f->set_vport_##field(vport, val); \
1035 return count; \
1036}
1037
1038#define fc_vport_store_str_function(field, slen) \
1039static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001040store_fc_vport_##field(struct device *dev, \
1041 struct device_attribute *attr, \
1042 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001043{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001044 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001045 struct Scsi_Host *shost = vport_to_shost(vport); \
1046 struct fc_internal *i = to_fc_internal(shost->transportt); \
1047 unsigned int cnt=count; \
1048 \
1049 /* count may include a LF at end of string */ \
1050 if (buf[cnt-1] == '\n') \
1051 cnt--; \
1052 if (cnt > ((slen) - 1)) \
1053 return -EINVAL; \
1054 memcpy(vport->field, buf, cnt); \
1055 i->f->set_vport_##field(vport); \
1056 return count; \
1057}
1058
1059#define fc_vport_rd_attr(field, format_string, sz) \
1060 fc_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001061static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001062 show_fc_vport_##field, NULL)
1063
1064#define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1065 fc_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001066static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001067 show_fc_vport_##field, NULL)
1068
1069#define fc_vport_rw_attr(field, format_string, sz) \
1070 fc_vport_show_function(field, format_string, sz, ) \
1071 fc_vport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001072static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001073 show_fc_vport_##field, \
1074 store_fc_vport_##field)
1075
1076#define fc_private_vport_show_function(field, format_string, sz, cast) \
1077static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001078show_fc_vport_##field (struct device *dev, \
1079 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001080{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001081 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001082 return snprintf(buf, sz, format_string, cast vport->field); \
1083}
1084
1085#define fc_private_vport_store_u32_function(field) \
1086static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001087store_fc_vport_##field(struct device *dev, \
1088 struct device_attribute *attr, \
1089 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001090{ \
1091 u32 val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001092 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001093 char *cp; \
1094 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1095 return -EBUSY; \
1096 val = simple_strtoul(buf, &cp, 0); \
1097 if (*cp && (*cp != '\n')) \
1098 return -EINVAL; \
1099 vport->field = val; \
1100 return count; \
1101}
1102
1103
1104#define fc_private_vport_rd_attr(field, format_string, sz) \
1105 fc_private_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001106static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001107 show_fc_vport_##field, NULL)
1108
1109#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1110 fc_private_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001111static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001112 show_fc_vport_##field, NULL)
1113
1114#define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1115 fc_private_vport_show_function(field, format_string, sz, ) \
1116 fc_private_vport_store_u32_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001117static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001118 show_fc_vport_##field, \
1119 store_fc_vport_##field)
1120
1121
1122#define fc_private_vport_rd_enum_attr(title, maxlen) \
1123static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001124show_fc_vport_##title (struct device *dev, \
1125 struct device_attribute *attr, \
1126 char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001127{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001128 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001129 const char *name; \
1130 name = get_fc_##title##_name(vport->title); \
1131 if (!name) \
1132 return -EINVAL; \
1133 return snprintf(buf, maxlen, "%s\n", name); \
1134} \
Tony Jonesee959b02008-02-22 00:13:36 +01001135static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001136 show_fc_vport_##title, NULL)
1137
1138
1139#define SETUP_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001140 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001141 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1142 i->private_vport_attrs[count].store = NULL; \
1143 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1144 if (i->f->get_##field) \
1145 count++
1146 /* NOTE: Above MACRO differs: checks function not show bit */
1147
1148#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001149 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001150 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1151 i->private_vport_attrs[count].store = NULL; \
1152 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1153 count++
1154
1155#define SETUP_VPORT_ATTRIBUTE_WR(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001156 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001157 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1158 if (i->f->field) \
1159 count++
1160 /* NOTE: Above MACRO differs: checks function */
1161
1162#define SETUP_VPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001163 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001164 if (!i->f->set_vport_##field) { \
1165 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1166 i->private_vport_attrs[count].store = NULL; \
1167 } \
1168 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1169 count++
1170 /* NOTE: Above MACRO differs: does not check show bit */
1171
1172#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1173{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001174 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001175 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1176 count++; \
1177}
1178
1179
1180/* The FC Transport Virtual Port Attributes: */
1181
1182/* Fixed Virtual Port Attributes */
1183
1184/* Dynamic Virtual Port Attributes */
1185
1186/* Private Virtual Port Attributes */
1187
1188fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1189fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1190fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1191fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1192
1193static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001194show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1195 char *buf)
James Smarta53eb5e2007-04-27 12:41:09 -04001196{
Tony Jonesee959b02008-02-22 00:13:36 +01001197 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001198
1199 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1200 return snprintf(buf, 20, "unknown\n");
1201 return get_fc_port_roles_names(vport->roles, buf);
1202}
Tony Jonesee959b02008-02-22 00:13:36 +01001203static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
James Smarta53eb5e2007-04-27 12:41:09 -04001204
1205fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1206
1207fc_private_vport_show_function(symbolic_name, "%s\n",
1208 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1209fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
Tony Jonesee959b02008-02-22 00:13:36 +01001210static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001211 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1212
1213static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001214store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1215 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001216{
Tony Jonesee959b02008-02-22 00:13:36 +01001217 struct fc_vport *vport = transport_class_to_vport(dev);
James Smart9ef3e4a2007-05-24 19:04:44 -04001218 struct Scsi_Host *shost = vport_to_shost(vport);
James Smarta53eb5e2007-04-27 12:41:09 -04001219
James Smart9ef3e4a2007-05-24 19:04:44 -04001220 fc_queue_work(shost, &vport->vport_delete_work);
James Smarta53eb5e2007-04-27 12:41:09 -04001221 return count;
1222}
Tony Jonesee959b02008-02-22 00:13:36 +01001223static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001224 NULL, store_fc_vport_delete);
1225
1226
1227/*
1228 * Enable/Disable vport
1229 * Write "1" to disable, write "0" to enable
1230 */
1231static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001232store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1233 const char *buf,
James Smarta53eb5e2007-04-27 12:41:09 -04001234 size_t count)
1235{
Tony Jonesee959b02008-02-22 00:13:36 +01001236 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001237 struct Scsi_Host *shost = vport_to_shost(vport);
1238 struct fc_internal *i = to_fc_internal(shost->transportt);
1239 int stat;
1240
1241 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1242 return -EBUSY;
1243
1244 if (*buf == '0') {
1245 if (vport->vport_state != FC_VPORT_DISABLED)
1246 return -EALREADY;
1247 } else if (*buf == '1') {
1248 if (vport->vport_state == FC_VPORT_DISABLED)
1249 return -EALREADY;
1250 } else
1251 return -EINVAL;
1252
1253 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1254 return stat ? stat : count;
1255}
Tony Jonesee959b02008-02-22 00:13:36 +01001256static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001257 NULL, store_fc_vport_disable);
1258
1259
1260/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * Host Attribute Management
1262 */
1263
1264#define fc_host_show_function(field, format_string, sz, cast) \
1265static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001266show_fc_host_##field (struct device *dev, \
1267 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001269 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct fc_internal *i = to_fc_internal(shost->transportt); \
1271 if (i->f->get_host_##field) \
1272 i->f->get_host_##field(shost); \
1273 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1274}
1275
1276#define fc_host_store_function(field) \
1277static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001278store_fc_host_##field(struct device *dev, \
1279 struct device_attribute *attr, \
1280 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{ \
1282 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001283 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -04001285 char *cp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 \
James Smart0f29b962006-08-18 17:33:29 -04001287 val = simple_strtoul(buf, &cp, 0); \
1288 if (*cp && (*cp != '\n')) \
1289 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 i->f->set_host_##field(shost, val); \
1291 return count; \
1292}
1293
James Smartb8d08212006-08-17 08:00:43 -04001294#define fc_host_store_str_function(field, slen) \
1295static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001296store_fc_host_##field(struct device *dev, \
1297 struct device_attribute *attr, \
1298 const char *buf, size_t count) \
James Smartb8d08212006-08-17 08:00:43 -04001299{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001300 struct Scsi_Host *shost = transport_class_to_shost(dev); \
James Smartb8d08212006-08-17 08:00:43 -04001301 struct fc_internal *i = to_fc_internal(shost->transportt); \
1302 unsigned int cnt=count; \
1303 \
1304 /* count may include a LF at end of string */ \
1305 if (buf[cnt-1] == '\n') \
1306 cnt--; \
1307 if (cnt > ((slen) - 1)) \
1308 return -EINVAL; \
1309 memcpy(fc_host_##field(shost), buf, cnt); \
1310 i->f->set_host_##field(shost); \
1311 return count; \
1312}
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314#define fc_host_rd_attr(field, format_string, sz) \
1315 fc_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001316static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 show_fc_host_##field, NULL)
1318
1319#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1320 fc_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001321static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 show_fc_host_##field, NULL)
1323
1324#define fc_host_rw_attr(field, format_string, sz) \
1325 fc_host_show_function(field, format_string, sz, ) \
1326 fc_host_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001327static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 show_fc_host_##field, \
1329 store_fc_host_##field)
1330
1331#define fc_host_rd_enum_attr(title, maxlen) \
1332static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001333show_fc_host_##title (struct device *dev, \
1334 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001336 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 struct fc_internal *i = to_fc_internal(shost->transportt); \
1338 const char *name; \
1339 if (i->f->get_host_##title) \
1340 i->f->get_host_##title(shost); \
1341 name = get_fc_##title##_name(fc_host_##title(shost)); \
1342 if (!name) \
1343 return -EINVAL; \
1344 return snprintf(buf, maxlen, "%s\n", name); \
1345} \
Tony Jonesee959b02008-02-22 00:13:36 +01001346static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348#define SETUP_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001349 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1351 i->private_host_attrs[count].store = NULL; \
1352 i->host_attrs[count] = &i->private_host_attrs[count]; \
1353 if (i->f->show_host_##field) \
1354 count++
1355
James Smarta53eb5e2007-04-27 12:41:09 -04001356#define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001357 i->private_host_attrs[count] = device_attr_host_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001358 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1359 i->private_host_attrs[count].store = NULL; \
1360 i->host_attrs[count] = &i->private_host_attrs[count]; \
1361 count++
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363#define SETUP_HOST_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001364 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (!i->f->set_host_##field) { \
1366 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1367 i->private_host_attrs[count].store = NULL; \
1368 } \
1369 i->host_attrs[count] = &i->private_host_attrs[count]; \
1370 if (i->f->show_host_##field) \
1371 count++
1372
1373
1374#define fc_private_host_show_function(field, format_string, sz, cast) \
1375static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001376show_fc_host_##field (struct device *dev, \
1377 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001379 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1381}
1382
1383#define fc_private_host_rd_attr(field, format_string, sz) \
1384 fc_private_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001385static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 show_fc_host_##field, NULL)
1387
1388#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1389 fc_private_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001390static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 show_fc_host_##field, NULL)
1392
1393#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001394 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1396 i->private_host_attrs[count].store = NULL; \
1397 i->host_attrs[count] = &i->private_host_attrs[count]; \
1398 count++
1399
1400#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001401{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001402 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 i->host_attrs[count] = &i->private_host_attrs[count]; \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001404 count++; \
1405}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407
1408/* Fixed Host Attributes */
1409
1410static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001411show_fc_host_supported_classes (struct device *dev,
1412 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Tony Jonesee959b02008-02-22 00:13:36 +01001414 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1417 return snprintf(buf, 20, "unspecified\n");
1418
1419 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1420}
Tony Jonesee959b02008-02-22 00:13:36 +01001421static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 show_fc_host_supported_classes, NULL);
1423
1424static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001425show_fc_host_supported_fc4s (struct device *dev,
1426 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Tony Jonesee959b02008-02-22 00:13:36 +01001428 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1430}
Tony Jonesee959b02008-02-22 00:13:36 +01001431static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 show_fc_host_supported_fc4s, NULL);
1433
1434static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001435show_fc_host_supported_speeds (struct device *dev,
1436 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Tony Jonesee959b02008-02-22 00:13:36 +01001438 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1441 return snprintf(buf, 20, "unknown\n");
1442
1443 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1444}
Tony Jonesee959b02008-02-22 00:13:36 +01001445static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 show_fc_host_supported_speeds, NULL);
1447
1448
1449fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1450fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01001451fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1452 unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
James Smarta53eb5e2007-04-27 12:41:09 -04001454fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1456
1457
1458/* Dynamic Host Attributes */
1459
1460static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001461show_fc_host_active_fc4s (struct device *dev,
1462 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Tony Jonesee959b02008-02-22 00:13:36 +01001464 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 struct fc_internal *i = to_fc_internal(shost->transportt);
1466
1467 if (i->f->get_host_active_fc4s)
1468 i->f->get_host_active_fc4s(shost);
1469
1470 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1471}
Tony Jonesee959b02008-02-22 00:13:36 +01001472static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 show_fc_host_active_fc4s, NULL);
1474
1475static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001476show_fc_host_speed (struct device *dev,
1477 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Tony Jonesee959b02008-02-22 00:13:36 +01001479 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 struct fc_internal *i = to_fc_internal(shost->transportt);
1481
1482 if (i->f->get_host_speed)
1483 i->f->get_host_speed(shost);
1484
1485 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1486 return snprintf(buf, 20, "unknown\n");
1487
1488 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1489}
Tony Jonesee959b02008-02-22 00:13:36 +01001490static FC_DEVICE_ATTR(host, speed, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 show_fc_host_speed, NULL);
1492
1493
1494fc_host_rd_attr(port_id, "0x%06x\n", 20);
1495fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1496fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1497fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
James Smart016131b2006-08-14 08:20:25 -04001498fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
James Smartb8d08212006-08-17 08:00:43 -04001500fc_private_host_show_function(system_hostname, "%s\n",
1501 FC_SYMBOLIC_NAME_SIZE + 1, )
1502fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
Tony Jonesee959b02008-02-22 00:13:36 +01001503static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
James Smartb8d08212006-08-17 08:00:43 -04001504 show_fc_host_system_hostname, store_fc_host_system_hostname);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507/* Private Host Attributes */
1508
1509static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001510show_fc_private_host_tgtid_bind_type(struct device *dev,
1511 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Tony Jonesee959b02008-02-22 00:13:36 +01001513 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 const char *name;
1515
1516 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1517 if (!name)
1518 return -EINVAL;
1519 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1520}
1521
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001522#define get_list_head_entry(pos, head, member) \
1523 pos = list_entry((head)->next, typeof(*pos), member)
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001526store_fc_private_host_tgtid_bind_type(struct device *dev,
1527 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Tony Jonesee959b02008-02-22 00:13:36 +01001529 struct Scsi_Host *shost = transport_class_to_shost(dev);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001530 struct fc_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 enum fc_tgtid_binding_type val;
1532 unsigned long flags;
1533
1534 if (get_fc_tgtid_bind_type_match(buf, &val))
1535 return -EINVAL;
1536
1537 /* if changing bind type, purge all unused consistent bindings */
1538 if (val != fc_host_tgtid_bind_type(shost)) {
1539 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001540 while (!list_empty(&fc_host_rport_bindings(shost))) {
1541 get_list_head_entry(rport,
1542 &fc_host_rport_bindings(shost), peers);
James Smartaedf3492006-04-10 10:14:05 -04001543 list_del(&rport->peers);
1544 rport->port_state = FC_PORTSTATE_DELETED;
1545 fc_queue_work(shost, &rport->rport_delete_work);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 spin_unlock_irqrestore(shost->host_lock, flags);
1548 }
1549
1550 fc_host_tgtid_bind_type(shost) = val;
1551 return count;
1552}
1553
Tony Jonesee959b02008-02-22 00:13:36 +01001554static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 show_fc_private_host_tgtid_bind_type,
1556 store_fc_private_host_tgtid_bind_type);
1557
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001558static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001559store_fc_private_host_issue_lip(struct device *dev,
1560 struct device_attribute *attr, const char *buf, size_t count)
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001561{
Tony Jonesee959b02008-02-22 00:13:36 +01001562 struct Scsi_Host *shost = transport_class_to_shost(dev);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001563 struct fc_internal *i = to_fc_internal(shost->transportt);
1564 int ret;
1565
1566 /* ignore any data value written to the attribute */
1567 if (i->f->issue_fc_host_lip) {
1568 ret = i->f->issue_fc_host_lip(shost);
1569 return ret ? ret: count;
1570 }
1571
1572 return -ENOENT;
1573}
1574
Tony Jonesee959b02008-02-22 00:13:36 +01001575static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001576 store_fc_private_host_issue_lip);
1577
James Smarta53eb5e2007-04-27 12:41:09 -04001578fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1579
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581/*
1582 * Host Statistics Management
1583 */
1584
1585/* Show a given an attribute in the statistics group */
1586static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001587fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Tony Jonesee959b02008-02-22 00:13:36 +01001589 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 struct fc_internal *i = to_fc_internal(shost->transportt);
1591 struct fc_host_statistics *stats;
1592 ssize_t ret = -ENOENT;
1593
1594 if (offset > sizeof(struct fc_host_statistics) ||
1595 offset % sizeof(u64) != 0)
1596 WARN_ON(1);
1597
1598 if (i->f->get_fc_host_stats) {
1599 stats = (i->f->get_fc_host_stats)(shost);
1600 if (stats)
1601 ret = snprintf(buf, 20, "0x%llx\n",
1602 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1603 }
1604 return ret;
1605}
1606
1607
1608/* generate a read-only statistics attribute */
1609#define fc_host_statistic(name) \
Tony Jonesee959b02008-02-22 00:13:36 +01001610static ssize_t show_fcstat_##name(struct device *cd, \
1611 struct device_attribute *attr, \
1612 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{ \
1614 return fc_stat_show(cd, buf, \
1615 offsetof(struct fc_host_statistics, name)); \
1616} \
Tony Jonesee959b02008-02-22 00:13:36 +01001617static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619fc_host_statistic(seconds_since_last_reset);
1620fc_host_statistic(tx_frames);
1621fc_host_statistic(tx_words);
1622fc_host_statistic(rx_frames);
1623fc_host_statistic(rx_words);
1624fc_host_statistic(lip_count);
1625fc_host_statistic(nos_count);
1626fc_host_statistic(error_frames);
1627fc_host_statistic(dumped_frames);
1628fc_host_statistic(link_failure_count);
1629fc_host_statistic(loss_of_sync_count);
1630fc_host_statistic(loss_of_signal_count);
1631fc_host_statistic(prim_seq_protocol_err_count);
1632fc_host_statistic(invalid_tx_word_count);
1633fc_host_statistic(invalid_crc_count);
1634fc_host_statistic(fcp_input_requests);
1635fc_host_statistic(fcp_output_requests);
1636fc_host_statistic(fcp_control_requests);
1637fc_host_statistic(fcp_input_megabytes);
1638fc_host_statistic(fcp_output_megabytes);
1639
1640static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001641fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1642 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Tony Jonesee959b02008-02-22 00:13:36 +01001644 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 struct fc_internal *i = to_fc_internal(shost->transportt);
1646
1647 /* ignore any data value written to the attribute */
1648 if (i->f->reset_fc_host_stats) {
1649 i->f->reset_fc_host_stats(shost);
1650 return count;
1651 }
1652
1653 return -ENOENT;
1654}
Tony Jonesee959b02008-02-22 00:13:36 +01001655static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 fc_reset_statistics);
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658static struct attribute *fc_statistics_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001659 &device_attr_host_seconds_since_last_reset.attr,
1660 &device_attr_host_tx_frames.attr,
1661 &device_attr_host_tx_words.attr,
1662 &device_attr_host_rx_frames.attr,
1663 &device_attr_host_rx_words.attr,
1664 &device_attr_host_lip_count.attr,
1665 &device_attr_host_nos_count.attr,
1666 &device_attr_host_error_frames.attr,
1667 &device_attr_host_dumped_frames.attr,
1668 &device_attr_host_link_failure_count.attr,
1669 &device_attr_host_loss_of_sync_count.attr,
1670 &device_attr_host_loss_of_signal_count.attr,
1671 &device_attr_host_prim_seq_protocol_err_count.attr,
1672 &device_attr_host_invalid_tx_word_count.attr,
1673 &device_attr_host_invalid_crc_count.attr,
1674 &device_attr_host_fcp_input_requests.attr,
1675 &device_attr_host_fcp_output_requests.attr,
1676 &device_attr_host_fcp_control_requests.attr,
1677 &device_attr_host_fcp_input_megabytes.attr,
1678 &device_attr_host_fcp_output_megabytes.attr,
1679 &device_attr_host_reset_statistics.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 NULL
1681};
1682
1683static struct attribute_group fc_statistics_group = {
1684 .name = "statistics",
1685 .attrs = fc_statistics_attrs,
1686};
1687
James Smarta53eb5e2007-04-27 12:41:09 -04001688
1689/* Host Vport Attributes */
1690
1691static int
1692fc_parse_wwn(const char *ns, u64 *nm)
1693{
1694 unsigned int i, j;
1695 u8 wwn[8];
1696
1697 memset(wwn, 0, sizeof(wwn));
1698
1699 /* Validate and store the new name */
1700 for (i=0, j=0; i < 16; i++) {
1701 if ((*ns >= 'a') && (*ns <= 'f'))
1702 j = ((j << 4) | ((*ns++ -'a') + 10));
1703 else if ((*ns >= 'A') && (*ns <= 'F'))
1704 j = ((j << 4) | ((*ns++ -'A') + 10));
1705 else if ((*ns >= '0') && (*ns <= '9'))
1706 j = ((j << 4) | (*ns++ -'0'));
1707 else
1708 return -EINVAL;
1709 if (i % 2) {
1710 wwn[i/2] = j & 0xff;
1711 j = 0;
1712 }
1713 }
1714
1715 *nm = wwn_to_u64(wwn);
1716
1717 return 0;
1718}
1719
1720
1721/*
1722 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1723 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1724 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1725 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1726 */
1727static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001728store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1729 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001730{
Tony Jonesee959b02008-02-22 00:13:36 +01001731 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001732 struct fc_vport_identifiers vid;
1733 struct fc_vport *vport;
1734 unsigned int cnt=count;
1735 int stat;
1736
1737 memset(&vid, 0, sizeof(vid));
1738
1739 /* count may include a LF at end of string */
1740 if (buf[cnt-1] == '\n')
1741 cnt--;
1742
1743 /* validate we have enough characters for WWPN */
1744 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1745 return -EINVAL;
1746
1747 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1748 if (stat)
1749 return stat;
1750
1751 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1752 if (stat)
1753 return stat;
1754
1755 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1756 vid.vport_type = FC_PORTTYPE_NPIV;
1757 /* vid.symbolic_name is already zero/NULL's */
1758 vid.disable = false; /* always enabled */
1759
1760 /* we only allow support on Channel 0 !!! */
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07001761 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
James Smarta53eb5e2007-04-27 12:41:09 -04001762 return stat ? stat : count;
1763}
Tony Jonesee959b02008-02-22 00:13:36 +01001764static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001765 store_fc_host_vport_create);
1766
1767
1768/*
1769 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1770 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1771 * The WWNs are specified as hex characters, and may *not* contain
1772 * any prefixes (e.g. 0x, x, etc)
1773 */
1774static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001775store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1776 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001777{
Tony Jonesee959b02008-02-22 00:13:36 +01001778 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001779 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1780 struct fc_vport *vport;
1781 u64 wwpn, wwnn;
1782 unsigned long flags;
1783 unsigned int cnt=count;
1784 int stat, match;
1785
1786 /* count may include a LF at end of string */
1787 if (buf[cnt-1] == '\n')
1788 cnt--;
1789
1790 /* validate we have enough characters for WWPN */
1791 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1792 return -EINVAL;
1793
1794 stat = fc_parse_wwn(&buf[0], &wwpn);
1795 if (stat)
1796 return stat;
1797
1798 stat = fc_parse_wwn(&buf[17], &wwnn);
1799 if (stat)
1800 return stat;
1801
1802 spin_lock_irqsave(shost->host_lock, flags);
1803 match = 0;
1804 /* we only allow support on Channel 0 !!! */
1805 list_for_each_entry(vport, &fc_host->vports, peers) {
1806 if ((vport->channel == 0) &&
1807 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1808 match = 1;
1809 break;
1810 }
1811 }
1812 spin_unlock_irqrestore(shost->host_lock, flags);
1813
1814 if (!match)
1815 return -ENODEV;
1816
1817 stat = fc_vport_terminate(vport);
1818 return stat ? stat : count;
1819}
Tony Jonesee959b02008-02-22 00:13:36 +01001820static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001821 store_fc_host_vport_delete);
1822
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824static int fc_host_match(struct attribute_container *cont,
1825 struct device *dev)
1826{
1827 struct Scsi_Host *shost;
1828 struct fc_internal *i;
1829
1830 if (!scsi_is_host_device(dev))
1831 return 0;
1832
1833 shost = dev_to_shost(dev);
1834 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1835 != &fc_host_class.class)
1836 return 0;
1837
1838 i = to_fc_internal(shost->transportt);
1839
1840 return &i->t.host_attrs.ac == cont;
1841}
1842
1843static int fc_target_match(struct attribute_container *cont,
1844 struct device *dev)
1845{
1846 struct Scsi_Host *shost;
1847 struct fc_internal *i;
1848
1849 if (!scsi_is_target_device(dev))
1850 return 0;
1851
1852 shost = dev_to_shost(dev->parent);
1853 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1854 != &fc_host_class.class)
1855 return 0;
1856
1857 i = to_fc_internal(shost->transportt);
1858
1859 return &i->t.target_attrs.ac == cont;
1860}
1861
1862static void fc_rport_dev_release(struct device *dev)
1863{
1864 struct fc_rport *rport = dev_to_rport(dev);
1865 put_device(dev->parent);
1866 kfree(rport);
1867}
1868
1869int scsi_is_fc_rport(const struct device *dev)
1870{
1871 return dev->release == fc_rport_dev_release;
1872}
1873EXPORT_SYMBOL(scsi_is_fc_rport);
1874
1875static int fc_rport_match(struct attribute_container *cont,
1876 struct device *dev)
1877{
1878 struct Scsi_Host *shost;
1879 struct fc_internal *i;
1880
1881 if (!scsi_is_fc_rport(dev))
1882 return 0;
1883
1884 shost = dev_to_shost(dev->parent);
1885 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1886 != &fc_host_class.class)
1887 return 0;
1888
1889 i = to_fc_internal(shost->transportt);
1890
1891 return &i->rport_attr_cont.ac == cont;
1892}
1893
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001894
James Smarta53eb5e2007-04-27 12:41:09 -04001895static void fc_vport_dev_release(struct device *dev)
1896{
1897 struct fc_vport *vport = dev_to_vport(dev);
1898 put_device(dev->parent); /* release kobj parent */
1899 kfree(vport);
1900}
1901
1902int scsi_is_fc_vport(const struct device *dev)
1903{
1904 return dev->release == fc_vport_dev_release;
1905}
1906EXPORT_SYMBOL(scsi_is_fc_vport);
1907
1908static int fc_vport_match(struct attribute_container *cont,
1909 struct device *dev)
1910{
1911 struct fc_vport *vport;
1912 struct Scsi_Host *shost;
1913 struct fc_internal *i;
1914
1915 if (!scsi_is_fc_vport(dev))
1916 return 0;
1917 vport = dev_to_vport(dev);
1918
1919 shost = vport_to_shost(vport);
1920 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1921 != &fc_host_class.class)
1922 return 0;
1923
1924 i = to_fc_internal(shost->transportt);
1925 return &i->vport_attr_cont.ac == cont;
1926}
1927
1928
James Smartc829c392006-03-13 08:28:57 -05001929/**
1930 * fc_timed_out - FC Transport I/O timeout intercept handler
James Smartc829c392006-03-13 08:28:57 -05001931 * @scmd: The SCSI command which timed out
1932 *
1933 * This routine protects against error handlers getting invoked while a
1934 * rport is in a blocked state, typically due to a temporarily loss of
1935 * connectivity. If the error handlers are allowed to proceed, requests
1936 * to abort i/o, reset the target, etc will likely fail as there is no way
1937 * to communicate with the device to perform the requested function. These
1938 * failures may result in the midlayer taking the device offline, requiring
1939 * manual intervention to restore operation.
1940 *
1941 * This routine, called whenever an i/o times out, validates the state of
1942 * the underlying rport. If the rport is blocked, it returns
1943 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1944 * Eventually, either the device will return, or devloss_tmo will fire,
1945 * and when the timeout then fires, it will be handled normally.
1946 * If the rport is not blocked, normal error handling continues.
1947 *
1948 * Notes:
1949 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05001950 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001951static enum blk_eh_timer_return
James Smartc829c392006-03-13 08:28:57 -05001952fc_timed_out(struct scsi_cmnd *scmd)
1953{
1954 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1955
1956 if (rport->port_state == FC_PORTSTATE_BLOCKED)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001957 return BLK_EH_RESET_TIMER;
James Smartc829c392006-03-13 08:28:57 -05001958
Jens Axboe242f9dc2008-09-14 05:55:09 -07001959 return BLK_EH_NOT_HANDLED;
James Smartc829c392006-03-13 08:28:57 -05001960}
1961
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001962/*
James Smartbda23252008-04-24 12:12:46 -04001963 * Called by fc_user_scan to locate an rport on the shost that
1964 * matches the channel and target id, and invoke scsi_scan_target()
1965 * on the rport.
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001966 */
James Smartbda23252008-04-24 12:12:46 -04001967static void
1968fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001969{
1970 struct fc_rport *rport;
James Smartbda23252008-04-24 12:12:46 -04001971 unsigned long flags;
1972
1973 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001974
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001975 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1976 if (rport->scsi_target_id == -1)
1977 continue;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001978
Hannes Reinecke0d2fcd92007-06-14 15:16:45 +02001979 if (rport->port_state != FC_PORTSTATE_ONLINE)
1980 continue;
1981
James Smartbda23252008-04-24 12:12:46 -04001982 if ((channel == rport->channel) &&
1983 (id == rport->scsi_target_id)) {
1984 spin_unlock_irqrestore(shost->host_lock, flags);
1985 scsi_scan_target(&rport->dev, channel, id, lun, 1);
1986 return;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001987 }
1988 }
1989
James Smartbda23252008-04-24 12:12:46 -04001990 spin_unlock_irqrestore(shost->host_lock, flags);
1991}
1992
1993/*
1994 * Called via sysfs scan routines. Necessary, as the FC transport
1995 * wants to place all target objects below the rport object. So this
1996 * routine must invoke the scsi_scan_target() routine with the rport
1997 * object as the parent.
1998 */
1999static int
2000fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2001{
2002 uint chlo, chhi;
2003 uint tgtlo, tgthi;
2004
2005 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2006 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2007 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2008 return -EINVAL;
2009
2010 if (channel == SCAN_WILD_CARD) {
2011 chlo = 0;
2012 chhi = shost->max_channel + 1;
2013 } else {
2014 chlo = channel;
2015 chhi = channel + 1;
2016 }
2017
2018 if (id == SCAN_WILD_CARD) {
2019 tgtlo = 0;
2020 tgthi = shost->max_id;
2021 } else {
2022 tgtlo = id;
2023 tgthi = id + 1;
2024 }
2025
2026 for ( ; chlo < chhi; chlo++)
2027 for ( ; tgtlo < tgthi; tgtlo++)
2028 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2029
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002030 return 0;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002031}
2032
FUJITA Tomonori75252362007-09-01 02:02:27 +09002033static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
2034 int result)
2035{
2036 struct fc_internal *i = to_fc_internal(shost->transportt);
2037 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
2038}
2039
2040static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
2041{
2042 struct fc_internal *i = to_fc_internal(shost->transportt);
2043 return i->f->it_nexus_response(shost, nexus, result);
2044}
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046struct scsi_transport_template *
2047fc_attach_transport(struct fc_function_template *ft)
2048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 int count;
Jes Sorensen24669f752006-01-16 10:31:18 -05002050 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2051 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 if (unlikely(!i))
2054 return NULL;
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2057 i->t.target_attrs.ac.class = &fc_transport_class.class;
2058 i->t.target_attrs.ac.match = fc_target_match;
2059 i->t.target_size = sizeof(struct fc_starget_attrs);
2060 transport_container_register(&i->t.target_attrs);
2061
2062 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2063 i->t.host_attrs.ac.class = &fc_host_class.class;
2064 i->t.host_attrs.ac.match = fc_host_match;
2065 i->t.host_size = sizeof(struct fc_host_attrs);
2066 if (ft->get_fc_host_stats)
2067 i->t.host_attrs.statistics = &fc_statistics_group;
2068 transport_container_register(&i->t.host_attrs);
2069
2070 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2071 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2072 i->rport_attr_cont.ac.match = fc_rport_match;
2073 transport_container_register(&i->rport_attr_cont);
2074
James Smarta53eb5e2007-04-27 12:41:09 -04002075 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2076 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2077 i->vport_attr_cont.ac.match = fc_vport_match;
2078 transport_container_register(&i->vport_attr_cont);
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 i->f = ft;
2081
2082 /* Transport uses the shost workq for scsi scanning */
2083 i->t.create_work_queue = 1;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002084
James Smartc829c392006-03-13 08:28:57 -05002085 i->t.eh_timed_out = fc_timed_out;
2086
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002087 i->t.user_scan = fc_user_scan;
James Smart9ef3e4a2007-05-24 19:04:44 -04002088
FUJITA Tomonori75252362007-09-01 02:02:27 +09002089 /* target-mode drivers' functions */
2090 i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2091 i->t.it_nexus_response = fc_it_nexus_response;
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 /*
2094 * Setup SCSI Target Attributes.
2095 */
2096 count = 0;
2097 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2098 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2099 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2100
2101 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2102
2103 i->starget_attrs[count] = NULL;
2104
2105
2106 /*
2107 * Setup SCSI Host Attributes.
2108 */
2109 count=0;
2110 SETUP_HOST_ATTRIBUTE_RD(node_name);
2111 SETUP_HOST_ATTRIBUTE_RD(port_name);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01002112 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2114 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2116 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
James Smarta53eb5e2007-04-27 12:41:09 -04002117 if (ft->vport_create) {
2118 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2119 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2122
2123 SETUP_HOST_ATTRIBUTE_RD(port_id);
2124 SETUP_HOST_ATTRIBUTE_RD(port_type);
2125 SETUP_HOST_ATTRIBUTE_RD(port_state);
2126 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2127 SETUP_HOST_ATTRIBUTE_RD(speed);
2128 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
James Smart016131b2006-08-14 08:20:25 -04002129 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
James Smartb8d08212006-08-17 08:00:43 -04002130 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 /* Transport-managed attributes */
2133 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002134 if (ft->issue_fc_host_lip)
2135 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
James Smarta53eb5e2007-04-27 12:41:09 -04002136 if (ft->vport_create)
2137 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2138 if (ft->vport_delete)
2139 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 BUG_ON(count > FC_HOST_NUM_ATTRS);
2142
2143 i->host_attrs[count] = NULL;
2144
2145 /*
2146 * Setup Remote Port Attributes.
2147 */
2148 count=0;
2149 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2150 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2151 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2152 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2153 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2154 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2155 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2156 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2157 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
Mike Christiefff9d402008-08-19 18:45:23 -05002158 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2161
2162 i->rport_attrs[count] = NULL;
2163
James Smarta53eb5e2007-04-27 12:41:09 -04002164 /*
2165 * Setup Virtual Port Attributes.
2166 */
2167 count=0;
2168 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2169 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2170 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2171 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2172 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2173 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2174 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2175 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2176 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2177
2178 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2179
2180 i->vport_attrs[count] = NULL;
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return &i->t;
2183}
2184EXPORT_SYMBOL(fc_attach_transport);
2185
2186void fc_release_transport(struct scsi_transport_template *t)
2187{
2188 struct fc_internal *i = to_fc_internal(t);
2189
2190 transport_container_unregister(&i->t.target_attrs);
2191 transport_container_unregister(&i->t.host_attrs);
2192 transport_container_unregister(&i->rport_attr_cont);
James Smarta53eb5e2007-04-27 12:41:09 -04002193 transport_container_unregister(&i->vport_attr_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 kfree(i);
2196}
2197EXPORT_SYMBOL(fc_release_transport);
2198
James Smartaedf3492006-04-10 10:14:05 -04002199/**
2200 * fc_queue_work - Queue work to the fc_host workqueue.
2201 * @shost: Pointer to Scsi_Host bound to fc_host.
2202 * @work: Work to queue for execution.
2203 *
2204 * Return value:
James Smarta0785ed2006-05-11 13:27:09 -04002205 * 1 - work queued for execution
2206 * 0 - work is already queued
2207 * -EINVAL - work queue doesn't exist
Rob Landleyeb448202007-11-03 13:30:39 -05002208 */
James Smartaedf3492006-04-10 10:14:05 -04002209static int
2210fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2211{
2212 if (unlikely(!fc_host_work_q(shost))) {
2213 printk(KERN_ERR
2214 "ERROR: FC host '%s' attempted to queue work, "
2215 "when no workqueue created.\n", shost->hostt->name);
2216 dump_stack();
2217
2218 return -EINVAL;
2219 }
2220
2221 return queue_work(fc_host_work_q(shost), work);
2222}
2223
2224/**
2225 * fc_flush_work - Flush a fc_host's workqueue.
2226 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002227 */
James Smartaedf3492006-04-10 10:14:05 -04002228static void
2229fc_flush_work(struct Scsi_Host *shost)
2230{
2231 if (!fc_host_work_q(shost)) {
2232 printk(KERN_ERR
2233 "ERROR: FC host '%s' attempted to flush work, "
2234 "when no workqueue created.\n", shost->hostt->name);
2235 dump_stack();
2236 return;
2237 }
2238
2239 flush_workqueue(fc_host_work_q(shost));
2240}
2241
2242/**
2243 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2244 * @shost: Pointer to Scsi_Host bound to fc_host.
2245 * @work: Work to queue for execution.
2246 * @delay: jiffies to delay the work queuing
2247 *
2248 * Return value:
James Smart0f29b962006-08-18 17:33:29 -04002249 * 1 on success / 0 already queued / < 0 for error
Rob Landleyeb448202007-11-03 13:30:39 -05002250 */
James Smartaedf3492006-04-10 10:14:05 -04002251static int
David Howellsc4028952006-11-22 14:57:56 +00002252fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
James Smartaedf3492006-04-10 10:14:05 -04002253 unsigned long delay)
2254{
2255 if (unlikely(!fc_host_devloss_work_q(shost))) {
2256 printk(KERN_ERR
2257 "ERROR: FC host '%s' attempted to queue work, "
2258 "when no workqueue created.\n", shost->hostt->name);
2259 dump_stack();
2260
2261 return -EINVAL;
2262 }
2263
2264 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2265}
2266
2267/**
2268 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2269 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002270 */
James Smartaedf3492006-04-10 10:14:05 -04002271static void
2272fc_flush_devloss(struct Scsi_Host *shost)
2273{
2274 if (!fc_host_devloss_work_q(shost)) {
2275 printk(KERN_ERR
2276 "ERROR: FC host '%s' attempted to flush work, "
2277 "when no workqueue created.\n", shost->hostt->name);
2278 dump_stack();
2279 return;
2280 }
2281
2282 flush_workqueue(fc_host_devloss_work_q(shost));
2283}
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286/**
Rob Landleyeb448202007-11-03 13:30:39 -05002287 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2288 * @shost: Which &Scsi_Host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 *
2290 * This routine is expected to be called immediately preceeding the
2291 * a driver's call to scsi_remove_host().
2292 *
2293 * WARNING: A driver utilizing the fc_transport, which fails to call
Rob Landleyeb448202007-11-03 13:30:39 -05002294 * this routine prior to scsi_remove_host(), will leave dangling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 * objects in /sys/class/fc_remote_ports. Access to any of these
2296 * objects can result in a system crash !!!
2297 *
2298 * Notes:
2299 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002300 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301void
2302fc_remove_host(struct Scsi_Host *shost)
2303{
James Smarta53eb5e2007-04-27 12:41:09 -04002304 struct fc_vport *vport = NULL, *next_vport = NULL;
2305 struct fc_rport *rport = NULL, *next_rport = NULL;
James Smartaedf3492006-04-10 10:14:05 -04002306 struct workqueue_struct *work_q;
2307 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James Smarta53eb5e2007-04-27 12:41:09 -04002308 unsigned long flags;
James Smarta53eb5e2007-04-27 12:41:09 -04002309
2310 spin_lock_irqsave(shost->host_lock, flags);
2311
2312 /* Remove any vports */
James Smart9ef3e4a2007-05-24 19:04:44 -04002313 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2314 fc_queue_work(shost, &vport->vport_delete_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 /* Remove any remote ports */
2317 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002318 &fc_host->rports, peers) {
2319 list_del(&rport->peers);
2320 rport->port_state = FC_PORTSTATE_DELETED;
2321 fc_queue_work(shost, &rport->rport_delete_work);
2322 }
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002325 &fc_host->rport_bindings, peers) {
2326 list_del(&rport->peers);
2327 rport->port_state = FC_PORTSTATE_DELETED;
2328 fc_queue_work(shost, &rport->rport_delete_work);
2329 }
2330
James Smarta53eb5e2007-04-27 12:41:09 -04002331 spin_unlock_irqrestore(shost->host_lock, flags);
2332
James Smartaedf3492006-04-10 10:14:05 -04002333 /* flush all scan work items */
2334 scsi_flush_work(shost);
2335
2336 /* flush all stgt delete, and rport delete work items, then kill it */
2337 if (fc_host->work_q) {
2338 work_q = fc_host->work_q;
2339 fc_host->work_q = NULL;
2340 destroy_workqueue(work_q);
2341 }
2342
2343 /* flush all devloss work items, then kill it */
2344 if (fc_host->devloss_work_q) {
2345 work_q = fc_host->devloss_work_q;
2346 fc_host->devloss_work_q = NULL;
2347 destroy_workqueue(work_q);
2348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350EXPORT_SYMBOL(fc_remove_host);
2351
Mike Christiefff9d402008-08-19 18:45:23 -05002352static void fc_terminate_rport_io(struct fc_rport *rport)
2353{
2354 struct Scsi_Host *shost = rport_to_shost(rport);
2355 struct fc_internal *i = to_fc_internal(shost->transportt);
2356
2357 /* Involve the LLDD if possible to terminate all io on the rport. */
2358 if (i->f->terminate_rport_io)
2359 i->f->terminate_rport_io(rport);
2360
2361 /*
2362 * must unblock to flush queued IO. The caller will have set
2363 * the port_state or flags, so that fc_remote_port_chkready will
2364 * fail IO.
2365 */
2366 scsi_target_unblock(&rport->dev);
2367}
James Smartaedf3492006-04-10 10:14:05 -04002368
2369/**
2370 * fc_starget_delete - called to delete the scsi decendents of an rport
David Howellsc4028952006-11-22 14:57:56 +00002371 * @work: remote port to be operated on.
Rob Landleyeb448202007-11-03 13:30:39 -05002372 *
2373 * Deletes target and all sdevs.
2374 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002375static void
David Howellsc4028952006-11-22 14:57:56 +00002376fc_starget_delete(struct work_struct *work)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002377{
David Howellsc4028952006-11-22 14:57:56 +00002378 struct fc_rport *rport =
2379 container_of(work, struct fc_rport, stgt_delete_work);
James Smart0f29b962006-08-18 17:33:29 -04002380
Mike Christiefff9d402008-08-19 18:45:23 -05002381 fc_terminate_rport_io(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002382 scsi_remove_target(&rport->dev);
2383}
2384
James Smartaedf3492006-04-10 10:14:05 -04002385
2386/**
2387 * fc_rport_final_delete - finish rport termination and delete it.
David Howellsc4028952006-11-22 14:57:56 +00002388 * @work: remote port to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05002389 */
James Smartaedf3492006-04-10 10:14:05 -04002390static void
David Howellsc4028952006-11-22 14:57:56 +00002391fc_rport_final_delete(struct work_struct *work)
James Smartaedf3492006-04-10 10:14:05 -04002392{
David Howellsc4028952006-11-22 14:57:56 +00002393 struct fc_rport *rport =
2394 container_of(work, struct fc_rport, rport_delete_work);
James Smartaedf3492006-04-10 10:14:05 -04002395 struct device *dev = &rport->dev;
2396 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002397 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart92740b22007-04-27 11:53:17 -04002398 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05002399 int do_callback = 0;
James Smartaedf3492006-04-10 10:14:05 -04002400
2401 /*
James Smart9ef3e4a2007-05-24 19:04:44 -04002402 * if a scan is pending, flush the SCSI Host work_q so that
James Smartaedf3492006-04-10 10:14:05 -04002403 * that we can reclaim the rport scan work element.
2404 */
2405 if (rport->flags & FC_RPORT_SCAN_PENDING)
2406 scsi_flush_work(shost);
2407
Mike Christiefff9d402008-08-19 18:45:23 -05002408 fc_terminate_rport_io(rport);
James Smart9e4f5e22009-03-26 13:33:19 -04002409
James Smart92740b22007-04-27 11:53:17 -04002410 /*
2411 * Cancel any outstanding timers. These should really exist
2412 * only when rmmod'ing the LLDD and we're asking for
2413 * immediate termination of the rports
2414 */
2415 spin_lock_irqsave(shost->host_lock, flags);
2416 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2417 spin_unlock_irqrestore(shost->host_lock, flags);
2418 if (!cancel_delayed_work(&rport->fail_io_work))
2419 fc_flush_devloss(shost);
2420 if (!cancel_delayed_work(&rport->dev_loss_work))
2421 fc_flush_devloss(shost);
2422 spin_lock_irqsave(shost->host_lock, flags);
2423 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2424 }
2425 spin_unlock_irqrestore(shost->host_lock, flags);
2426
James Smart0f29b962006-08-18 17:33:29 -04002427 /* Delete SCSI target and sdevs */
2428 if (rport->scsi_target_id != -1)
David Howellsc4028952006-11-22 14:57:56 +00002429 fc_starget_delete(&rport->stgt_delete_work);
James Smart92740b22007-04-27 11:53:17 -04002430
2431 /*
2432 * Notify the driver that the rport is now dead. The LLDD will
2433 * also guarantee that any communication to the rport is terminated
James Smart4be98c02009-01-05 12:14:18 -05002434 *
2435 * Avoid this call if we already called it when we preserved the
2436 * rport for the binding.
James Smart92740b22007-04-27 11:53:17 -04002437 */
Michael Reed8798a692009-10-09 14:15:59 -05002438 spin_lock_irqsave(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05002439 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
Michael Reed8798a692009-10-09 14:15:59 -05002440 (i->f->dev_loss_tmo_callbk)) {
2441 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2442 do_callback = 1;
2443 }
2444 spin_unlock_irqrestore(shost->host_lock, flags);
2445
2446 if (do_callback)
James Smart0f29b962006-08-18 17:33:29 -04002447 i->f->dev_loss_tmo_callbk(rport);
James Smart0f29b962006-08-18 17:33:29 -04002448
James Smart9e4f5e22009-03-26 13:33:19 -04002449 fc_bsg_remove(rport->rqst_q);
2450
James Smartaedf3492006-04-10 10:14:05 -04002451 transport_remove_device(dev);
2452 device_del(dev);
2453 transport_destroy_device(dev);
James Smart3bdad7b2006-06-26 14:19:59 -04002454 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2455 put_device(dev); /* for self-reference */
James Smartaedf3492006-04-10 10:14:05 -04002456}
2457
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459/**
2460 * fc_rport_create - allocates and creates a remote FC port.
2461 * @shost: scsi host the remote port is connected to.
2462 * @channel: Channel on shost port connected to.
2463 * @ids: The world wide names, fc address, and FC4 port
2464 * roles for the remote port.
2465 *
2466 * Allocates and creates the remoter port structure, including the
2467 * class and sysfs creation.
2468 *
2469 * Notes:
2470 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002471 */
Adrian Bunk44818ef2007-07-09 11:59:59 -07002472static struct fc_rport *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473fc_rport_create(struct Scsi_Host *shost, int channel,
2474 struct fc_rport_identifiers *ids)
2475{
James Smartaedf3492006-04-10 10:14:05 -04002476 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 struct fc_internal *fci = to_fc_internal(shost->transportt);
2478 struct fc_rport *rport;
2479 struct device *dev;
2480 unsigned long flags;
2481 int error;
2482 size_t size;
2483
2484 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
Jes Sorensen24669f752006-01-16 10:31:18 -05002485 rport = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (unlikely(!rport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002487 printk(KERN_ERR "%s: allocation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return NULL;
2489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 rport->maxframe_size = -1;
2492 rport->supported_classes = FC_COS_UNSPECIFIED;
2493 rport->dev_loss_tmo = fc_dev_loss_tmo;
2494 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2495 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2496 rport->port_id = ids->port_id;
2497 rport->roles = ids->roles;
2498 rport->port_state = FC_PORTSTATE_ONLINE;
2499 if (fci->f->dd_fcrport_size)
2500 rport->dd_data = &rport[1];
2501 rport->channel = channel;
James Smart0f29b962006-08-18 17:33:29 -04002502 rport->fast_io_fail_tmo = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
David Howellsc4028952006-11-22 14:57:56 +00002504 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2505 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2506 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2507 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2508 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 spin_lock_irqsave(shost->host_lock, flags);
2511
2512 rport->number = fc_host->next_rport_number++;
James Smarta53eb5e2007-04-27 12:41:09 -04002513 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 rport->scsi_target_id = fc_host->next_target_id++;
2515 else
2516 rport->scsi_target_id = -1;
James Smartaedf3492006-04-10 10:14:05 -04002517 list_add_tail(&rport->peers, &fc_host->rports);
James Smart3bdad7b2006-06-26 14:19:59 -04002518 get_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 spin_unlock_irqrestore(shost->host_lock, flags);
2521
2522 dev = &rport->dev;
James Smart3bdad7b2006-06-26 14:19:59 -04002523 device_initialize(dev); /* takes self reference */
2524 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 dev->release = fc_rport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01002526 dev_set_name(dev, "rport-%d:%d-%d",
2527 shost->host_no, channel, rport->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 transport_setup_device(dev);
2529
2530 error = device_add(dev);
2531 if (error) {
2532 printk(KERN_ERR "FC Remote Port device_add failed\n");
2533 goto delete_rport;
2534 }
2535 transport_add_device(dev);
2536 transport_configure_device(dev);
2537
James Smart9e4f5e22009-03-26 13:33:19 -04002538 fc_bsg_rportadd(shost, rport);
2539 /* ignore any bsg add error - we just can't do sgio */
2540
James Smarta53eb5e2007-04-27 12:41:09 -04002541 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002543 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 return rport;
2548
2549delete_rport:
2550 transport_destroy_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 spin_lock_irqsave(shost->host_lock, flags);
2552 list_del(&rport->peers);
James Smart3bdad7b2006-06-26 14:19:59 -04002553 put_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 spin_unlock_irqrestore(shost->host_lock, flags);
2555 put_device(dev->parent);
2556 kfree(rport);
2557 return NULL;
2558}
2559
2560/**
Rob Landleyeb448202007-11-03 13:30:39 -05002561 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 * @shost: scsi host the remote port is connected to.
2563 * @channel: Channel on shost port connected to.
2564 * @ids: The world wide names, fc address, and FC4 port
2565 * roles for the remote port.
2566 *
2567 * The LLDD calls this routine to notify the transport of the existence
2568 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2569 * of the port, it's FC address (port_id), and the FC4 roles that are
2570 * active for the port.
2571 *
2572 * For ports that are FCP targets (aka scsi targets), the FC transport
2573 * maintains consistent target id bindings on behalf of the LLDD.
2574 * A consistent target id binding is an assignment of a target id to
2575 * a remote port identifier, which persists while the scsi host is
2576 * attached. The remote port can disappear, then later reappear, and
2577 * it's target id assignment remains the same. This allows for shifts
2578 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2579 * changes to the scsi subsystem which is based on scsi host number and
2580 * target id values. Bindings are only valid during the attachment of
2581 * the scsi host. If the host detaches, then later re-attaches, target
2582 * id bindings may change.
2583 *
2584 * This routine is responsible for returning a remote port structure.
2585 * The routine will search the list of remote ports it maintains
2586 * internally on behalf of consistent target id mappings. If found, the
2587 * remote port structure will be reused. Otherwise, a new remote port
2588 * structure will be allocated.
2589 *
2590 * Whenever a remote port is allocated, a new fc_remote_port class
2591 * device is created.
2592 *
2593 * Should not be called from interrupt context.
2594 *
2595 * Notes:
2596 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598struct fc_rport *
2599fc_remote_port_add(struct Scsi_Host *shost, int channel,
2600 struct fc_rport_identifiers *ids)
2601{
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002602 struct fc_internal *fci = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04002603 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 struct fc_rport *rport;
2605 unsigned long flags;
2606 int match = 0;
2607
James Smartaedf3492006-04-10 10:14:05 -04002608 /* ensure any stgt delete functions are done */
2609 fc_flush_work(shost);
2610
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002611 /*
2612 * Search the list of "active" rports, for an rport that has been
2613 * deleted, but we've held off the real delete while the target
2614 * is in a "blocked" state.
2615 */
2616 spin_lock_irqsave(shost->host_lock, flags);
2617
James Smartaedf3492006-04-10 10:14:05 -04002618 list_for_each_entry(rport, &fc_host->rports, peers) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002619
2620 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2621 (rport->channel == channel)) {
2622
James Smartaedf3492006-04-10 10:14:05 -04002623 switch (fc_host->tgtid_bind_type) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002624 case FC_TGTID_BIND_BY_WWPN:
2625 case FC_TGTID_BIND_NONE:
2626 if (rport->port_name == ids->port_name)
2627 match = 1;
2628 break;
2629 case FC_TGTID_BIND_BY_WWNN:
2630 if (rport->node_name == ids->node_name)
2631 match = 1;
2632 break;
2633 case FC_TGTID_BIND_BY_ID:
2634 if (rport->port_id == ids->port_id)
2635 match = 1;
2636 break;
2637 }
2638
2639 if (match) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002640
2641 memcpy(&rport->node_name, &ids->node_name,
2642 sizeof(rport->node_name));
2643 memcpy(&rport->port_name, &ids->port_name,
2644 sizeof(rport->port_name));
2645 rport->port_id = ids->port_id;
2646
2647 rport->port_state = FC_PORTSTATE_ONLINE;
2648 rport->roles = ids->roles;
2649
2650 spin_unlock_irqrestore(shost->host_lock, flags);
2651
2652 if (fci->f->dd_fcrport_size)
2653 memset(rport->dd_data, 0,
2654 fci->f->dd_fcrport_size);
2655
2656 /*
James Smart92740b22007-04-27 11:53:17 -04002657 * If we were not a target, cancel the
2658 * io terminate and rport timers, and
2659 * we're done.
2660 *
2661 * If we were a target, but our new role
2662 * doesn't indicate a target, leave the
2663 * timers running expecting the role to
2664 * change as the target fully logs in. If
2665 * it doesn't, the target will be torn down.
2666 *
2667 * If we were a target, and our role shows
2668 * we're still a target, cancel the timers
2669 * and kick off a scan.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002670 */
James Smart92740b22007-04-27 11:53:17 -04002671
2672 /* was a target, not in roles */
2673 if ((rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04002674 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002675 return rport;
2676
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002677 /*
James Smart92740b22007-04-27 11:53:17 -04002678 * Stop the fail io and dev_loss timers.
2679 * If they flush, the port_state will
2680 * be checked and will NOOP the function.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002681 */
James Smart0f29b962006-08-18 17:33:29 -04002682 if (!cancel_delayed_work(&rport->fail_io_work))
2683 fc_flush_devloss(shost);
James Smart92740b22007-04-27 11:53:17 -04002684 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002685 fc_flush_devloss(shost);
2686
2687 spin_lock_irqsave(shost->host_lock, flags);
2688
Mike Christiefff9d402008-08-19 18:45:23 -05002689 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
James Smart4be98c02009-01-05 12:14:18 -05002690 FC_RPORT_DEVLOSS_PENDING |
2691 FC_RPORT_DEVLOSS_CALLBK_DONE);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002692
James Smart92740b22007-04-27 11:53:17 -04002693 /* if target, initiate a scan */
2694 if (rport->scsi_target_id != -1) {
2695 rport->flags |= FC_RPORT_SCAN_PENDING;
2696 scsi_queue_work(shost,
2697 &rport->scan_work);
2698 spin_unlock_irqrestore(shost->host_lock,
2699 flags);
2700 scsi_target_unblock(&rport->dev);
2701 } else
2702 spin_unlock_irqrestore(shost->host_lock,
2703 flags);
James Smarta0785ed2006-05-11 13:27:09 -04002704
James Smart9e4f5e22009-03-26 13:33:19 -04002705 fc_bsg_goose_queue(rport);
2706
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002707 return rport;
2708 }
2709 }
2710 }
2711
James Smart92740b22007-04-27 11:53:17 -04002712 /*
2713 * Search the bindings array
2714 * Note: if never a FCP target, you won't be on this list
2715 */
James Smartaedf3492006-04-10 10:14:05 -04002716 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 /* search for a matching consistent binding */
2719
James Smartaedf3492006-04-10 10:14:05 -04002720 list_for_each_entry(rport, &fc_host->rport_bindings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 peers) {
2722 if (rport->channel != channel)
2723 continue;
2724
James Smartaedf3492006-04-10 10:14:05 -04002725 switch (fc_host->tgtid_bind_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 case FC_TGTID_BIND_BY_WWPN:
2727 if (rport->port_name == ids->port_name)
2728 match = 1;
2729 break;
2730 case FC_TGTID_BIND_BY_WWNN:
2731 if (rport->node_name == ids->node_name)
2732 match = 1;
2733 break;
2734 case FC_TGTID_BIND_BY_ID:
2735 if (rport->port_id == ids->port_id)
2736 match = 1;
2737 break;
2738 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2739 break;
2740 }
2741
2742 if (match) {
James Smartaedf3492006-04-10 10:14:05 -04002743 list_move_tail(&rport->peers, &fc_host->rports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 break;
2745 }
2746 }
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (match) {
2749 memcpy(&rport->node_name, &ids->node_name,
2750 sizeof(rport->node_name));
2751 memcpy(&rport->port_name, &ids->port_name,
2752 sizeof(rport->port_name));
2753 rport->port_id = ids->port_id;
2754 rport->roles = ids->roles;
2755 rport->port_state = FC_PORTSTATE_ONLINE;
Mike Christiefff9d402008-08-19 18:45:23 -05002756 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002758 if (fci->f->dd_fcrport_size)
2759 memset(rport->dd_data, 0,
2760 fci->f->dd_fcrport_size);
2761
James Smarta53eb5e2007-04-27 12:41:09 -04002762 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002764 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 scsi_queue_work(shost, &rport->scan_work);
James Smarta0785ed2006-05-11 13:27:09 -04002766 spin_unlock_irqrestore(shost->host_lock, flags);
2767 scsi_target_unblock(&rport->dev);
2768 } else
2769 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 return rport;
2772 }
2773 }
2774
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002775 spin_unlock_irqrestore(shost->host_lock, flags);
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 /* No consistent binding found - create new remote port entry */
2778 rport = fc_rport_create(shost, channel, ids);
2779
2780 return rport;
2781}
2782EXPORT_SYMBOL(fc_remote_port_add);
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785/**
Rob Landleyeb448202007-11-03 13:30:39 -05002786 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 * @rport: The remote port that no longer exists
2788 *
2789 * The LLDD calls this routine to notify the transport that a remote
2790 * port is no longer part of the topology. Note: Although a port
2791 * may no longer be part of the topology, it may persist in the remote
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002792 * ports displayed by the fc_host. We do this under 2 conditions:
Rob Landleyeb448202007-11-03 13:30:39 -05002793 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002794 * This allows the port to temporarily disappear, then reappear without
2795 * disrupting the SCSI device tree attached to it. During the "blocked"
2796 * period the port will still exist.
Rob Landleyeb448202007-11-03 13:30:39 -05002797 * 2) If the port was a scsi target and disappears for longer than we
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002798 * expect, we'll delete the port and the tear down the SCSI device tree
2799 * attached to it. However, we want to semi-persist the target id assigned
2800 * to that port if it eventually does exist. The port structure will
2801 * remain (although with minimal information) so that the target id
2802 * bindings remails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 *
2804 * If the remote port is not an FCP Target, it will be fully torn down
2805 * and deallocated, including the fc_remote_port class device.
2806 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002807 * If the remote port is an FCP Target, the port will be placed in a
2808 * temporary blocked state. From the LLDD's perspective, the rport no
2809 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2810 * exists, but all sdevs on it are blocked from further I/O. The following
Rob Landleyeb448202007-11-03 13:30:39 -05002811 * is then expected.
2812 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002813 * If the remote port does not return (signaled by a LLDD call to
2814 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2815 * scsi target is removed - killing all outstanding i/o and removing the
2816 * scsi devices attached ot it. The port structure will be marked Not
2817 * Present and be partially cleared, leaving only enough information to
2818 * recognize the remote port relative to the scsi target id binding if
2819 * it later appears. The port will remain as long as there is a valid
2820 * binding (e.g. until the user changes the binding type or unloads the
2821 * scsi host with the binding).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002823 * If the remote port returns within the dev_loss_tmo value (and matches
2824 * according to the target id binding type), the port structure will be
2825 * reused. If it is no longer a SCSI target, the target will be torn
2826 * down. If it continues to be a SCSI target, then the target will be
2827 * unblocked (allowing i/o to be resumed), and a scan will be activated
2828 * to ensure that all luns are detected.
2829 *
2830 * Called from normal process context only - cannot be called from interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 *
2832 * Notes:
2833 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835void
2836fc_remote_port_delete(struct fc_rport *rport)
2837{
James Smartaedf3492006-04-10 10:14:05 -04002838 struct Scsi_Host *shost = rport_to_shost(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002839 int timeout = rport->dev_loss_tmo;
James Smartaedf3492006-04-10 10:14:05 -04002840 unsigned long flags;
2841
2842 /*
2843 * No need to flush the fc_host work_q's, as all adds are synchronous.
2844 *
2845 * We do need to reclaim the rport scan work element, so eventually
2846 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2847 * there's still a scan pending.
2848 */
2849
2850 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
James Smart92740b22007-04-27 11:53:17 -04002852 if (rport->port_state != FC_PORTSTATE_ONLINE) {
James Smartaedf3492006-04-10 10:14:05 -04002853 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return;
2855 }
2856
James Smart92740b22007-04-27 11:53:17 -04002857 /*
2858 * In the past, we if this was not an FCP-Target, we would
2859 * unconditionally just jump to deleting the rport.
2860 * However, rports can be used as node containers by the LLDD,
2861 * and its not appropriate to just terminate the rport at the
2862 * first sign of a loss in connectivity. The LLDD may want to
2863 * send ELS traffic to re-validate the login. If the rport is
2864 * immediately deleted, it makes it inappropriate for a node
2865 * container.
2866 * So... we now unconditionally wait dev_loss_tmo before
2867 * destroying an rport.
2868 */
2869
James Smartaedf3492006-04-10 10:14:05 -04002870 rport->port_state = FC_PORTSTATE_BLOCKED;
2871
2872 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2873
2874 spin_unlock_irqrestore(shost->host_lock, flags);
2875
FUJITA Tomonori75252362007-09-01 02:02:27 +09002876 if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
2877 shost->active_mode & MODE_TARGET)
2878 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
2879
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002880 scsi_target_block(&rport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
James Smart0f29b962006-08-18 17:33:29 -04002882 /* see if we need to kill io faster than waiting for device loss */
2883 if ((rport->fast_io_fail_tmo != -1) &&
Mike Christiefff9d402008-08-19 18:45:23 -05002884 (rport->fast_io_fail_tmo < timeout))
James Smart0f29b962006-08-18 17:33:29 -04002885 fc_queue_devloss_work(shost, &rport->fail_io_work,
2886 rport->fast_io_fail_tmo * HZ);
2887
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002888 /* cap the length the devices can be blocked until they are deleted */
James Smartaedf3492006-04-10 10:14:05 -04002889 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891EXPORT_SYMBOL(fc_remote_port_delete);
2892
2893/**
Rob Landleyeb448202007-11-03 13:30:39 -05002894 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 * @rport: The remote port that changed.
Rob Landleyeb448202007-11-03 13:30:39 -05002896 * @roles: New roles for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 *
Rob Landleyeb448202007-11-03 13:30:39 -05002898 * Description: The LLDD calls this routine to notify the transport that the
2899 * roles on a remote port may have changed. The largest effect of this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 * if a port now becomes a FCP Target, it must be allocated a
2901 * scsi target id. If the port is no longer a FCP target, any
2902 * scsi target id value assigned to it will persist in case the
2903 * role changes back to include FCP Target. No changes in the scsi
2904 * midlayer will be invoked if the role changes (in the expectation
2905 * that the role will be resumed. If it doesn't normal error processing
2906 * will take place).
2907 *
2908 * Should not be called from interrupt context.
2909 *
2910 * Notes:
2911 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002912 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913void
2914fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2915{
2916 struct Scsi_Host *shost = rport_to_shost(rport);
James Smartaedf3492006-04-10 10:14:05 -04002917 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 unsigned long flags;
2919 int create = 0;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002920 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 spin_lock_irqsave(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04002923 if (roles & FC_PORT_ROLE_FCP_TARGET) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002924 if (rport->scsi_target_id == -1) {
2925 rport->scsi_target_id = fc_host->next_target_id++;
2926 create = 1;
James Smarta53eb5e2007-04-27 12:41:09 -04002927 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002928 create = 1;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002929 } else if (shost->active_mode & MODE_TARGET) {
2930 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
2931 (char *)&rport->node_name);
2932 if (ret)
2933 printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
2934 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002937 rport->roles = roles;
2938
James Smartaedf3492006-04-10 10:14:05 -04002939 spin_unlock_irqrestore(shost->host_lock, flags);
2940
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002941 if (create) {
2942 /*
2943 * There may have been a delete timer running on the
2944 * port. Ensure that it is cancelled as we now know
2945 * the port is an FCP Target.
2946 * Note: we know the rport is exists and in an online
2947 * state as the LLDD would not have had an rport
2948 * reference to pass us.
2949 *
2950 * Take no action on the del_timer failure as the state
2951 * machine state change will validate the
2952 * transaction.
2953 */
James Smart0f29b962006-08-18 17:33:29 -04002954 if (!cancel_delayed_work(&rport->fail_io_work))
2955 fc_flush_devloss(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002956 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002957 fc_flush_devloss(shost);
2958
2959 spin_lock_irqsave(shost->host_lock, flags);
Mike Christiefff9d402008-08-19 18:45:23 -05002960 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2961 FC_RPORT_DEVLOSS_PENDING);
James Smartaedf3492006-04-10 10:14:05 -04002962 spin_unlock_irqrestore(shost->host_lock, flags);
2963
2964 /* ensure any stgt delete functions are done */
2965 fc_flush_work(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002968 spin_lock_irqsave(shost->host_lock, flags);
2969 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002971 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04002972 scsi_target_unblock(&rport->dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974}
2975EXPORT_SYMBOL(fc_remote_port_rolechg);
2976
2977/**
Rob Landleyeb448202007-11-03 13:30:39 -05002978 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
James Smart92740b22007-04-27 11:53:17 -04002979 * @work: rport target that failed to reappear in the allotted time.
Rob Landleyeb448202007-11-03 13:30:39 -05002980 *
2981 * Description: An attempt to delete a remote port blocks, and if it fails
2982 * to return in the allotted time this gets called.
2983 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984static void
David Howellsc4028952006-11-22 14:57:56 +00002985fc_timeout_deleted_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
David Howellsc4028952006-11-22 14:57:56 +00002987 struct fc_rport *rport =
2988 container_of(work, struct fc_rport, dev_loss_work.work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002989 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart4be98c02009-01-05 12:14:18 -05002990 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04002991 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002992 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05002993 int do_callback = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002995 spin_lock_irqsave(shost->host_lock, flags);
2996
James Smartaedf3492006-04-10 10:14:05 -04002997 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2998
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002999 /*
James Smart92740b22007-04-27 11:53:17 -04003000 * If the port is ONLINE, then it came back. If it was a SCSI
3001 * target, validate it still is. If not, tear down the
3002 * scsi_target on it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003003 */
James Smartaedf3492006-04-10 10:14:05 -04003004 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
James Smart92740b22007-04-27 11:53:17 -04003005 (rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04003006 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003007 dev_printk(KERN_ERR, &rport->dev,
James Smartaedf3492006-04-10 10:14:05 -04003008 "blocked FC remote port time out: no longer"
3009 " a FCP target, removing starget\n");
James Smartaedf3492006-04-10 10:14:05 -04003010 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04003011 scsi_target_unblock(&rport->dev);
3012 fc_queue_work(shost, &rport->stgt_delete_work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003013 return;
3014 }
3015
James Smart92740b22007-04-27 11:53:17 -04003016 /* NOOP state - we're flushing workq's */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003017 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3018 spin_unlock_irqrestore(shost->host_lock, flags);
3019 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003020 "blocked FC remote port time out: leaving"
3021 " rport%s alone\n",
3022 (rport->scsi_target_id != -1) ? " and starget" : "");
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003023 return;
3024 }
3025
James Smart92740b22007-04-27 11:53:17 -04003026 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3027 (rport->scsi_target_id == -1)) {
James Smartaedf3492006-04-10 10:14:05 -04003028 list_del(&rport->peers);
3029 rport->port_state = FC_PORTSTATE_DELETED;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003030 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003031 "blocked FC remote port time out: removing"
3032 " rport%s\n",
3033 (rport->scsi_target_id != -1) ? " and starget" : "");
James Smartaedf3492006-04-10 10:14:05 -04003034 fc_queue_work(shost, &rport->rport_delete_work);
3035 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003036 return;
3037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
3039 dev_printk(KERN_ERR, &rport->dev,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003040 "blocked FC remote port time out: removing target and "
3041 "saving binding\n");
3042
James Smartaedf3492006-04-10 10:14:05 -04003043 list_move_tail(&rport->peers, &fc_host->rport_bindings);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003044
3045 /*
3046 * Note: We do not remove or clear the hostdata area. This allows
3047 * host-specific target data to persist along with the
3048 * scsi_target_id. It's up to the host to manage it's hostdata area.
3049 */
3050
3051 /*
3052 * Reinitialize port attributes that may change if the port comes back.
3053 */
3054 rport->maxframe_size = -1;
3055 rport->supported_classes = FC_COS_UNSPECIFIED;
James Smarta53eb5e2007-04-27 12:41:09 -04003056 rport->roles = FC_PORT_ROLE_UNKNOWN;
James Smartaedf3492006-04-10 10:14:05 -04003057 rport->port_state = FC_PORTSTATE_NOTPRESENT;
Mike Christiefff9d402008-08-19 18:45:23 -05003058 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003059
James Smartf78badb2008-12-05 16:29:59 -06003060 /*
3061 * Pre-emptively kill I/O rather than waiting for the work queue
3062 * item to teardown the starget. (FCOE libFC folks prefer this
3063 * and to have the rport_port_id still set when it's done).
3064 */
3065 spin_unlock_irqrestore(shost->host_lock, flags);
3066 fc_terminate_rport_io(rport);
3067
Michael Reed8798a692009-10-09 14:15:59 -05003068 spin_lock_irqsave(shost->host_lock, flags);
James Smartf78badb2008-12-05 16:29:59 -06003069
Michael Reed8798a692009-10-09 14:15:59 -05003070 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3071
3072 /* remove the identifiers that aren't used in the consisting binding */
3073 switch (fc_host->tgtid_bind_type) {
3074 case FC_TGTID_BIND_BY_WWPN:
3075 rport->node_name = -1;
3076 rport->port_id = -1;
3077 break;
3078 case FC_TGTID_BIND_BY_WWNN:
3079 rport->port_name = -1;
3080 rport->port_id = -1;
3081 break;
3082 case FC_TGTID_BIND_BY_ID:
3083 rport->node_name = -1;
3084 rport->port_name = -1;
3085 break;
3086 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3087 break;
3088 }
3089
3090 /*
3091 * As this only occurs if the remote port (scsi target)
3092 * went away and didn't come back - we'll remove
3093 * all attached scsi devices.
3094 */
3095 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3096 fc_queue_work(shost, &rport->stgt_delete_work);
3097
3098 do_callback = 1;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003099 }
3100
Michael Reed8798a692009-10-09 14:15:59 -05003101 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05003102
3103 /*
3104 * Notify the driver that the rport is now dead. The LLDD will
3105 * also guarantee that any communication to the rport is terminated
3106 *
3107 * Note: we set the CALLBK_DONE flag above to correspond
3108 */
Michael Reed8798a692009-10-09 14:15:59 -05003109 if (do_callback && i->f->dev_loss_tmo_callbk)
James Smart4be98c02009-01-05 12:14:18 -05003110 i->f->dev_loss_tmo_callbk(rport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
James Smart4be98c02009-01-05 12:14:18 -05003113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114/**
Rob Landleyeb448202007-11-03 13:30:39 -05003115 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
David Howellsc4028952006-11-22 14:57:56 +00003116 * @work: rport to terminate io on.
James Smart0f29b962006-08-18 17:33:29 -04003117 *
3118 * Notes: Only requests the failure of the io, not that all are flushed
3119 * prior to returning.
Rob Landleyeb448202007-11-03 13:30:39 -05003120 */
James Smart0f29b962006-08-18 17:33:29 -04003121static void
David Howellsc4028952006-11-22 14:57:56 +00003122fc_timeout_fail_rport_io(struct work_struct *work)
James Smart0f29b962006-08-18 17:33:29 -04003123{
David Howellsc4028952006-11-22 14:57:56 +00003124 struct fc_rport *rport =
3125 container_of(work, struct fc_rport, fail_io_work.work);
James Smart0f29b962006-08-18 17:33:29 -04003126
3127 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3128 return;
3129
Mike Christiefff9d402008-08-19 18:45:23 -05003130 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3131 fc_terminate_rport_io(rport);
James Smart0f29b962006-08-18 17:33:29 -04003132}
3133
3134/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
David Howellsc4028952006-11-22 14:57:56 +00003136 * @work: remote port to be scanned.
Rob Landleyeb448202007-11-03 13:30:39 -05003137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138static void
David Howellsc4028952006-11-22 14:57:56 +00003139fc_scsi_scan_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
David Howellsc4028952006-11-22 14:57:56 +00003141 struct fc_rport *rport =
3142 container_of(work, struct fc_rport, scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003143 struct Scsi_Host *shost = rport_to_shost(rport);
Christof Schmitt03f002f2007-08-28 09:31:21 +02003144 struct fc_internal *i = to_fc_internal(shost->transportt);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003145 unsigned long flags;
3146
James Smartaedf3492006-04-10 10:14:05 -04003147 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
Christof Schmitt03f002f2007-08-28 09:31:21 +02003148 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3149 !(i->f->disable_target_scan)) {
James Smartaedf3492006-04-10 10:14:05 -04003150 scsi_scan_target(&rport->dev, rport->channel,
3151 rport->scsi_target_id, SCAN_WILD_CARD, 1);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003152 }
James Smartaedf3492006-04-10 10:14:05 -04003153
3154 spin_lock_irqsave(shost->host_lock, flags);
3155 rport->flags &= ~FC_RPORT_SCAN_PENDING;
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003156 spin_unlock_irqrestore(shost->host_lock, flags);
3157}
3158
Christof Schmitt65d430f2009-10-30 17:59:29 +01003159/**
3160 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3161 * @cmnd: SCSI command that scsi_eh is trying to recover
3162 *
3163 * This routine can be called from a FC LLD scsi_eh callback. It
3164 * blocks the scsi_eh thread until the fc_rport leaves the
3165 * FC_PORTSTATE_BLOCKED. This is necessary to avoid the scsi_eh
3166 * failing recovery actions for blocked rports which would lead to
3167 * offlined SCSI devices.
3168 */
3169void fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3170{
3171 struct Scsi_Host *shost = cmnd->device->host;
3172 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3173 unsigned long flags;
3174
3175 spin_lock_irqsave(shost->host_lock, flags);
3176 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
3177 spin_unlock_irqrestore(shost->host_lock, flags);
3178 msleep(1000);
3179 spin_lock_irqsave(shost->host_lock, flags);
3180 }
3181 spin_unlock_irqrestore(shost->host_lock, flags);
3182}
3183EXPORT_SYMBOL(fc_block_scsi_eh);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003184
James Smarta53eb5e2007-04-27 12:41:09 -04003185/**
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003186 * fc_vport_setup - allocates and creates a FC virtual port.
James Smarta53eb5e2007-04-27 12:41:09 -04003187 * @shost: scsi host the virtual port is connected to.
3188 * @channel: Channel on shost port connected to.
3189 * @pdev: parent device for vport
3190 * @ids: The world wide names, FC4 port roles, etc for
3191 * the virtual port.
3192 * @ret_vport: The pointer to the created vport.
3193 *
3194 * Allocates and creates the vport structure, calls the parent host
3195 * to instantiate the vport, the completes w/ class and sysfs creation.
3196 *
3197 * Notes:
3198 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003199 */
James Smarta53eb5e2007-04-27 12:41:09 -04003200static int
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003201fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
James Smarta53eb5e2007-04-27 12:41:09 -04003202 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3203{
3204 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3205 struct fc_internal *fci = to_fc_internal(shost->transportt);
3206 struct fc_vport *vport;
3207 struct device *dev;
3208 unsigned long flags;
3209 size_t size;
3210 int error;
3211
3212 *ret_vport = NULL;
3213
3214 if ( ! fci->f->vport_create)
3215 return -ENOENT;
3216
3217 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3218 vport = kzalloc(size, GFP_KERNEL);
3219 if (unlikely(!vport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003220 printk(KERN_ERR "%s: allocation failure\n", __func__);
James Smarta53eb5e2007-04-27 12:41:09 -04003221 return -ENOMEM;
3222 }
3223
3224 vport->vport_state = FC_VPORT_UNKNOWN;
3225 vport->vport_last_state = FC_VPORT_UNKNOWN;
3226 vport->node_name = ids->node_name;
3227 vport->port_name = ids->port_name;
3228 vport->roles = ids->roles;
3229 vport->vport_type = ids->vport_type;
3230 if (fci->f->dd_fcvport_size)
3231 vport->dd_data = &vport[1];
3232 vport->shost = shost;
3233 vport->channel = channel;
3234 vport->flags = FC_VPORT_CREATING;
James Smart9ef3e4a2007-05-24 19:04:44 -04003235 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
James Smarta53eb5e2007-04-27 12:41:09 -04003236
3237 spin_lock_irqsave(shost->host_lock, flags);
3238
3239 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3240 spin_unlock_irqrestore(shost->host_lock, flags);
3241 kfree(vport);
3242 return -ENOSPC;
3243 }
3244 fc_host->npiv_vports_inuse++;
3245 vport->number = fc_host->next_vport_number++;
3246 list_add_tail(&vport->peers, &fc_host->vports);
3247 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3248
3249 spin_unlock_irqrestore(shost->host_lock, flags);
3250
3251 dev = &vport->dev;
3252 device_initialize(dev); /* takes self reference */
3253 dev->parent = get_device(pdev); /* takes parent reference */
3254 dev->release = fc_vport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01003255 dev_set_name(dev, "vport-%d:%d-%d",
3256 shost->host_no, channel, vport->number);
James Smarta53eb5e2007-04-27 12:41:09 -04003257 transport_setup_device(dev);
3258
3259 error = device_add(dev);
3260 if (error) {
3261 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3262 goto delete_vport;
3263 }
3264 transport_add_device(dev);
3265 transport_configure_device(dev);
3266
3267 error = fci->f->vport_create(vport, ids->disable);
3268 if (error) {
3269 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3270 goto delete_vport_all;
3271 }
3272
3273 /*
3274 * if the parent isn't the physical adapter's Scsi_Host, ensure
3275 * the Scsi_Host at least contains ia symlink to the vport.
3276 */
3277 if (pdev != &shost->shost_gendev) {
3278 error = sysfs_create_link(&shost->shost_gendev.kobj,
Kay Sievers71610f52008-12-03 22:41:36 +01003279 &dev->kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003280 if (error)
3281 printk(KERN_ERR
3282 "%s: Cannot create vport symlinks for "
3283 "%s, err=%d\n",
Kay Sievers71610f52008-12-03 22:41:36 +01003284 __func__, dev_name(dev), error);
James Smarta53eb5e2007-04-27 12:41:09 -04003285 }
3286 spin_lock_irqsave(shost->host_lock, flags);
3287 vport->flags &= ~FC_VPORT_CREATING;
3288 spin_unlock_irqrestore(shost->host_lock, flags);
3289
3290 dev_printk(KERN_NOTICE, pdev,
Kay Sievers71610f52008-12-03 22:41:36 +01003291 "%s created via shost%d channel %d\n", dev_name(dev),
James Smarta53eb5e2007-04-27 12:41:09 -04003292 shost->host_no, channel);
3293
3294 *ret_vport = vport;
3295
3296 return 0;
3297
3298delete_vport_all:
3299 transport_remove_device(dev);
3300 device_del(dev);
3301delete_vport:
3302 transport_destroy_device(dev);
3303 spin_lock_irqsave(shost->host_lock, flags);
3304 list_del(&vport->peers);
3305 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3306 fc_host->npiv_vports_inuse--;
3307 spin_unlock_irqrestore(shost->host_lock, flags);
3308 put_device(dev->parent);
3309 kfree(vport);
3310
3311 return error;
3312}
3313
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003314/**
3315 * fc_vport_create - Admin App or LLDD requests creation of a vport
3316 * @shost: scsi host the virtual port is connected to.
3317 * @channel: channel on shost port connected to.
3318 * @ids: The world wide names, FC4 port roles, etc for
3319 * the virtual port.
3320 *
3321 * Notes:
3322 * This routine assumes no locks are held on entry.
3323 */
3324struct fc_vport *
3325fc_vport_create(struct Scsi_Host *shost, int channel,
3326 struct fc_vport_identifiers *ids)
3327{
3328 int stat;
3329 struct fc_vport *vport;
3330
3331 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3332 ids, &vport);
3333 return stat ? NULL : vport;
3334}
3335EXPORT_SYMBOL(fc_vport_create);
James Smarta53eb5e2007-04-27 12:41:09 -04003336
3337/**
3338 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3339 * @vport: fc_vport to be terminated
3340 *
3341 * Calls the LLDD vport_delete() function, then deallocates and removes
3342 * the vport from the shost and object tree.
3343 *
3344 * Notes:
3345 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003346 */
James Smarta53eb5e2007-04-27 12:41:09 -04003347int
3348fc_vport_terminate(struct fc_vport *vport)
3349{
3350 struct Scsi_Host *shost = vport_to_shost(vport);
3351 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3352 struct fc_internal *i = to_fc_internal(shost->transportt);
3353 struct device *dev = &vport->dev;
3354 unsigned long flags;
3355 int stat;
3356
3357 spin_lock_irqsave(shost->host_lock, flags);
3358 if (vport->flags & FC_VPORT_CREATING) {
3359 spin_unlock_irqrestore(shost->host_lock, flags);
3360 return -EBUSY;
3361 }
3362 if (vport->flags & (FC_VPORT_DEL)) {
3363 spin_unlock_irqrestore(shost->host_lock, flags);
3364 return -EALREADY;
3365 }
3366 vport->flags |= FC_VPORT_DELETING;
3367 spin_unlock_irqrestore(shost->host_lock, flags);
3368
3369 if (i->f->vport_delete)
3370 stat = i->f->vport_delete(vport);
3371 else
3372 stat = -ENOENT;
3373
3374 spin_lock_irqsave(shost->host_lock, flags);
3375 vport->flags &= ~FC_VPORT_DELETING;
3376 if (!stat) {
3377 vport->flags |= FC_VPORT_DELETED;
3378 list_del(&vport->peers);
3379 fc_host->npiv_vports_inuse--;
3380 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3381 }
3382 spin_unlock_irqrestore(shost->host_lock, flags);
3383
3384 if (stat)
3385 return stat;
3386
3387 if (dev->parent != &shost->shost_gendev)
Kay Sievers71610f52008-12-03 22:41:36 +01003388 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003389 transport_remove_device(dev);
3390 device_del(dev);
3391 transport_destroy_device(dev);
3392
3393 /*
3394 * Removing our self-reference should mean our
3395 * release function gets called, which will drop the remaining
3396 * parent reference and free the data structure.
3397 */
3398 put_device(dev); /* for self-reference */
3399
3400 return 0; /* SUCCESS */
3401}
3402EXPORT_SYMBOL(fc_vport_terminate);
3403
James Smart9ef3e4a2007-05-24 19:04:44 -04003404/**
3405 * fc_vport_sched_delete - workq-based delete request for a vport
James Smart9ef3e4a2007-05-24 19:04:44 -04003406 * @work: vport to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05003407 */
James Smart9ef3e4a2007-05-24 19:04:44 -04003408static void
3409fc_vport_sched_delete(struct work_struct *work)
3410{
3411 struct fc_vport *vport =
3412 container_of(work, struct fc_vport, vport_delete_work);
3413 int stat;
James Smarta53eb5e2007-04-27 12:41:09 -04003414
James Smart9ef3e4a2007-05-24 19:04:44 -04003415 stat = fc_vport_terminate(vport);
3416 if (stat)
3417 dev_printk(KERN_ERR, vport->dev.parent,
3418 "%s: %s could not be deleted created via "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003419 "shost%d channel %d - error %d\n", __func__,
Kay Sievers71610f52008-12-03 22:41:36 +01003420 dev_name(&vport->dev), vport->shost->host_no,
James Smart9ef3e4a2007-05-24 19:04:44 -04003421 vport->channel, stat);
3422}
3423
3424
James Smart9e4f5e22009-03-26 13:33:19 -04003425/*
3426 * BSG support
3427 */
3428
3429
3430/**
3431 * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
3432 * @job: fc_bsg_job that is to be torn down
3433 */
3434static void
3435fc_destroy_bsgjob(struct fc_bsg_job *job)
3436{
3437 unsigned long flags;
3438
3439 spin_lock_irqsave(&job->job_lock, flags);
3440 if (job->ref_cnt) {
3441 spin_unlock_irqrestore(&job->job_lock, flags);
3442 return;
3443 }
3444 spin_unlock_irqrestore(&job->job_lock, flags);
3445
3446 put_device(job->dev); /* release reference for the request */
3447
3448 kfree(job->request_payload.sg_list);
3449 kfree(job->reply_payload.sg_list);
3450 kfree(job);
3451}
3452
James Smart9e4f5e22009-03-26 13:33:19 -04003453/**
3454 * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
3455 * completed
3456 * @job: fc_bsg_job that is complete
3457 */
3458static void
3459fc_bsg_jobdone(struct fc_bsg_job *job)
3460{
3461 struct request *req = job->req;
3462 struct request *rsp = req->next_rq;
James Smart9e4f5e22009-03-26 13:33:19 -04003463 int err;
3464
James Smart9e4f5e22009-03-26 13:33:19 -04003465 err = job->req->errors = job->reply->result;
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003466
James Smart9e4f5e22009-03-26 13:33:19 -04003467 if (err < 0)
3468 /* we're only returning the result field in the reply */
3469 job->req->sense_len = sizeof(uint32_t);
3470 else
3471 job->req->sense_len = job->reply_len;
3472
3473 /* we assume all request payload was transferred, residual == 0 */
3474 req->resid_len = 0;
3475
3476 if (rsp) {
3477 WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
3478
3479 /* set reply (bidi) residual */
3480 rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
3481 rsp->resid_len);
3482 }
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003483 blk_complete_request(req);
James Smart9e4f5e22009-03-26 13:33:19 -04003484}
3485
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003486/**
3487 * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
Randy Dunlapfe5d20c2009-07-04 13:10:41 -07003488 * @rq: BSG request that holds the job to be destroyed
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003489 */
3490static void fc_bsg_softirq_done(struct request *rq)
3491{
3492 struct fc_bsg_job *job = rq->special;
3493 unsigned long flags;
3494
3495 spin_lock_irqsave(&job->job_lock, flags);
3496 job->state_flags |= FC_RQST_STATE_DONE;
3497 job->ref_cnt--;
3498 spin_unlock_irqrestore(&job->job_lock, flags);
3499
3500 blk_end_request_all(rq, rq->errors);
3501 fc_destroy_bsgjob(job);
3502}
James Smart9e4f5e22009-03-26 13:33:19 -04003503
3504/**
3505 * fc_bsg_job_timeout - handler for when a bsg request timesout
3506 * @req: request that timed out
3507 */
3508static enum blk_eh_timer_return
3509fc_bsg_job_timeout(struct request *req)
3510{
3511 struct fc_bsg_job *job = (void *) req->special;
3512 struct Scsi_Host *shost = job->shost;
3513 struct fc_internal *i = to_fc_internal(shost->transportt);
3514 unsigned long flags;
3515 int err = 0, done = 0;
3516
3517 if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
3518 return BLK_EH_RESET_TIMER;
3519
3520 spin_lock_irqsave(&job->job_lock, flags);
3521 if (job->state_flags & FC_RQST_STATE_DONE)
3522 done = 1;
3523 else
3524 job->ref_cnt++;
3525 spin_unlock_irqrestore(&job->job_lock, flags);
3526
3527 if (!done && i->f->bsg_timeout) {
3528 /* call LLDD to abort the i/o as it has timed out */
3529 err = i->f->bsg_timeout(job);
3530 if (err)
3531 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3532 "abort failed with status %d\n", err);
3533 }
3534
James Smart9e4f5e22009-03-26 13:33:19 -04003535 /* the blk_end_sync_io() doesn't check the error */
Giridhar Malavali47e7e892009-06-19 16:26:54 -07003536 if (done)
3537 return BLK_EH_NOT_HANDLED;
3538 else
3539 return BLK_EH_HANDLED;
James Smart9e4f5e22009-03-26 13:33:19 -04003540}
3541
James Smart9e4f5e22009-03-26 13:33:19 -04003542static int
3543fc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
3544{
3545 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
3546
3547 BUG_ON(!req->nr_phys_segments);
3548
3549 buf->sg_list = kzalloc(sz, GFP_KERNEL);
3550 if (!buf->sg_list)
3551 return -ENOMEM;
3552 sg_init_table(buf->sg_list, req->nr_phys_segments);
3553 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
3554 buf->payload_len = blk_rq_bytes(req);
3555 return 0;
3556}
3557
3558
3559/**
3560 * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
3561 * bsg request
3562 * @shost: SCSI Host corresponding to the bsg object
3563 * @rport: (optional) FC Remote Port corresponding to the bsg object
3564 * @req: BSG request that needs a job structure
3565 */
3566static int
3567fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
3568 struct request *req)
3569{
3570 struct fc_internal *i = to_fc_internal(shost->transportt);
3571 struct request *rsp = req->next_rq;
3572 struct fc_bsg_job *job;
3573 int ret;
3574
3575 BUG_ON(req->special);
3576
3577 job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
3578 GFP_KERNEL);
3579 if (!job)
3580 return -ENOMEM;
3581
3582 /*
3583 * Note: this is a bit silly.
3584 * The request gets formatted as a SGIO v4 ioctl request, which
3585 * then gets reformatted as a blk request, which then gets
3586 * reformatted as a fc bsg request. And on completion, we have
3587 * to wrap return results such that SGIO v4 thinks it was a scsi
3588 * status. I hope this was all worth it.
3589 */
3590
3591 req->special = job;
3592 job->shost = shost;
3593 job->rport = rport;
3594 job->req = req;
3595 if (i->f->dd_bsg_size)
3596 job->dd_data = (void *)&job[1];
3597 spin_lock_init(&job->job_lock);
3598 job->request = (struct fc_bsg_request *)req->cmd;
3599 job->request_len = req->cmd_len;
3600 job->reply = req->sense;
3601 job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
3602 * allocated */
3603 if (req->bio) {
3604 ret = fc_bsg_map_buffer(&job->request_payload, req);
3605 if (ret)
3606 goto failjob_rls_job;
3607 }
3608 if (rsp && rsp->bio) {
3609 ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
3610 if (ret)
3611 goto failjob_rls_rqst_payload;
3612 }
3613 job->job_done = fc_bsg_jobdone;
3614 if (rport)
3615 job->dev = &rport->dev;
3616 else
3617 job->dev = &shost->shost_gendev;
3618 get_device(job->dev); /* take a reference for the request */
3619
3620 job->ref_cnt = 1;
3621
3622 return 0;
3623
3624
3625failjob_rls_rqst_payload:
3626 kfree(job->request_payload.sg_list);
3627failjob_rls_job:
3628 kfree(job);
3629 return -ENOMEM;
3630}
3631
3632
3633enum fc_dispatch_result {
3634 FC_DISPATCH_BREAK, /* on return, q is locked, break from q loop */
3635 FC_DISPATCH_LOCKED, /* on return, q is locked, continue on */
3636 FC_DISPATCH_UNLOCKED, /* on return, q is unlocked, continue on */
3637};
3638
3639
3640/**
3641 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
Randy Dunlap54159072009-06-18 19:39:57 -07003642 * @q: fc host request queue
James Smart9e4f5e22009-03-26 13:33:19 -04003643 * @shost: scsi host rport attached to
3644 * @job: bsg job to be processed
3645 */
3646static enum fc_dispatch_result
3647fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3648 struct fc_bsg_job *job)
3649{
3650 struct fc_internal *i = to_fc_internal(shost->transportt);
3651 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3652 int ret;
3653
3654 /* Validate the host command */
3655 switch (job->request->msgcode) {
3656 case FC_BSG_HST_ADD_RPORT:
3657 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3658 break;
3659
3660 case FC_BSG_HST_DEL_RPORT:
3661 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3662 break;
3663
3664 case FC_BSG_HST_ELS_NOLOGIN:
3665 cmdlen += sizeof(struct fc_bsg_host_els);
3666 /* there better be a xmt and rcv payloads */
3667 if ((!job->request_payload.payload_len) ||
3668 (!job->reply_payload.payload_len)) {
3669 ret = -EINVAL;
3670 goto fail_host_msg;
3671 }
3672 break;
3673
3674 case FC_BSG_HST_CT:
3675 cmdlen += sizeof(struct fc_bsg_host_ct);
3676 /* there better be xmt and rcv payloads */
3677 if ((!job->request_payload.payload_len) ||
3678 (!job->reply_payload.payload_len)) {
3679 ret = -EINVAL;
3680 goto fail_host_msg;
3681 }
3682 break;
3683
3684 case FC_BSG_HST_VENDOR:
3685 cmdlen += sizeof(struct fc_bsg_host_vendor);
3686 if ((shost->hostt->vendor_id == 0L) ||
3687 (job->request->rqst_data.h_vendor.vendor_id !=
3688 shost->hostt->vendor_id)) {
3689 ret = -ESRCH;
3690 goto fail_host_msg;
3691 }
3692 break;
3693
3694 default:
3695 ret = -EBADR;
3696 goto fail_host_msg;
3697 }
3698
3699 /* check if we really have all the request data needed */
3700 if (job->request_len < cmdlen) {
3701 ret = -ENOMSG;
3702 goto fail_host_msg;
3703 }
3704
3705 ret = i->f->bsg_request(job);
3706 if (!ret)
3707 return FC_DISPATCH_UNLOCKED;
3708
3709fail_host_msg:
3710 /* return the errno failure code as the only status */
3711 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003712 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003713 job->reply->result = ret;
3714 job->reply_len = sizeof(uint32_t);
3715 fc_bsg_jobdone(job);
3716 return FC_DISPATCH_UNLOCKED;
3717}
3718
3719
3720/*
3721 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3722 * @rport: rport to be restarted
3723 */
3724static void
3725fc_bsg_goose_queue(struct fc_rport *rport)
3726{
3727 int flagset;
Christof Schmitt39562e72009-06-26 16:30:43 +02003728 unsigned long flags;
James Smart9e4f5e22009-03-26 13:33:19 -04003729
3730 if (!rport->rqst_q)
3731 return;
3732
3733 get_device(&rport->dev);
3734
Christof Schmitt39562e72009-06-26 16:30:43 +02003735 spin_lock_irqsave(rport->rqst_q->queue_lock, flags);
James Smart9e4f5e22009-03-26 13:33:19 -04003736 flagset = test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags) &&
3737 !test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags);
3738 if (flagset)
3739 queue_flag_set(QUEUE_FLAG_REENTER, rport->rqst_q);
3740 __blk_run_queue(rport->rqst_q);
3741 if (flagset)
3742 queue_flag_clear(QUEUE_FLAG_REENTER, rport->rqst_q);
Christof Schmitt39562e72009-06-26 16:30:43 +02003743 spin_unlock_irqrestore(rport->rqst_q->queue_lock, flags);
James Smart9e4f5e22009-03-26 13:33:19 -04003744
3745 put_device(&rport->dev);
3746}
3747
3748
3749/**
3750 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
Randy Dunlap54159072009-06-18 19:39:57 -07003751 * @q: rport request queue
James Smart9e4f5e22009-03-26 13:33:19 -04003752 * @shost: scsi host rport attached to
3753 * @rport: rport request destined to
3754 * @job: bsg job to be processed
3755 */
3756static enum fc_dispatch_result
3757fc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3758 struct fc_rport *rport, struct fc_bsg_job *job)
3759{
3760 struct fc_internal *i = to_fc_internal(shost->transportt);
3761 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3762 int ret;
3763
3764 /* Validate the rport command */
3765 switch (job->request->msgcode) {
3766 case FC_BSG_RPT_ELS:
3767 cmdlen += sizeof(struct fc_bsg_rport_els);
3768 goto check_bidi;
3769
3770 case FC_BSG_RPT_CT:
3771 cmdlen += sizeof(struct fc_bsg_rport_ct);
3772check_bidi:
3773 /* there better be xmt and rcv payloads */
3774 if ((!job->request_payload.payload_len) ||
3775 (!job->reply_payload.payload_len)) {
3776 ret = -EINVAL;
3777 goto fail_rport_msg;
3778 }
3779 break;
3780 default:
3781 ret = -EBADR;
3782 goto fail_rport_msg;
3783 }
3784
3785 /* check if we really have all the request data needed */
3786 if (job->request_len < cmdlen) {
3787 ret = -ENOMSG;
3788 goto fail_rport_msg;
3789 }
3790
3791 ret = i->f->bsg_request(job);
3792 if (!ret)
3793 return FC_DISPATCH_UNLOCKED;
3794
3795fail_rport_msg:
3796 /* return the errno failure code as the only status */
3797 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003798 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003799 job->reply->result = ret;
3800 job->reply_len = sizeof(uint32_t);
3801 fc_bsg_jobdone(job);
3802 return FC_DISPATCH_UNLOCKED;
3803}
3804
3805
3806/**
3807 * fc_bsg_request_handler - generic handler for bsg requests
3808 * @q: request queue to manage
3809 * @shost: Scsi_Host related to the bsg object
3810 * @rport: FC remote port related to the bsg object (optional)
3811 * @dev: device structure for bsg object
3812 */
3813static void
3814fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
3815 struct fc_rport *rport, struct device *dev)
3816{
3817 struct request *req;
3818 struct fc_bsg_job *job;
3819 enum fc_dispatch_result ret;
3820
3821 if (!get_device(dev))
3822 return;
3823
3824 while (!blk_queue_plugged(q)) {
Mike Christie2bc1c592009-11-05 11:18:09 -06003825 if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED) &&
3826 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
3827 break;
James Smart9e4f5e22009-03-26 13:33:19 -04003828
3829 req = blk_fetch_request(q);
3830 if (!req)
3831 break;
3832
3833 if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
3834 req->errors = -ENXIO;
3835 spin_unlock_irq(q->queue_lock);
3836 blk_end_request(req, -ENXIO, blk_rq_bytes(req));
3837 spin_lock_irq(q->queue_lock);
3838 continue;
3839 }
3840
3841 spin_unlock_irq(q->queue_lock);
3842
3843 ret = fc_req_to_bsgjob(shost, rport, req);
3844 if (ret) {
3845 req->errors = ret;
3846 blk_end_request(req, ret, blk_rq_bytes(req));
3847 spin_lock_irq(q->queue_lock);
3848 continue;
3849 }
3850
3851 job = req->special;
3852
3853 /* check if we have the msgcode value at least */
3854 if (job->request_len < sizeof(uint32_t)) {
3855 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003856 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003857 job->reply->result = -ENOMSG;
3858 job->reply_len = sizeof(uint32_t);
3859 fc_bsg_jobdone(job);
3860 spin_lock_irq(q->queue_lock);
3861 continue;
3862 }
3863
3864 /* the dispatch routines will unlock the queue_lock */
3865 if (rport)
3866 ret = fc_bsg_rport_dispatch(q, shost, rport, job);
3867 else
3868 ret = fc_bsg_host_dispatch(q, shost, job);
3869
3870 /* did dispatcher hit state that can't process any more */
3871 if (ret == FC_DISPATCH_BREAK)
3872 break;
3873
3874 /* did dispatcher had released the lock */
3875 if (ret == FC_DISPATCH_UNLOCKED)
3876 spin_lock_irq(q->queue_lock);
3877 }
3878
3879 spin_unlock_irq(q->queue_lock);
3880 put_device(dev);
3881 spin_lock_irq(q->queue_lock);
3882}
3883
3884
3885/**
3886 * fc_bsg_host_handler - handler for bsg requests for a fc host
3887 * @q: fc host request queue
3888 */
3889static void
3890fc_bsg_host_handler(struct request_queue *q)
3891{
3892 struct Scsi_Host *shost = q->queuedata;
3893
3894 fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
3895}
3896
3897
3898/**
3899 * fc_bsg_rport_handler - handler for bsg requests for a fc rport
3900 * @q: rport request queue
3901 */
3902static void
3903fc_bsg_rport_handler(struct request_queue *q)
3904{
3905 struct fc_rport *rport = q->queuedata;
3906 struct Scsi_Host *shost = rport_to_shost(rport);
3907
3908 fc_bsg_request_handler(q, shost, rport, &rport->dev);
3909}
3910
3911
3912/**
3913 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3914 * @shost: shost for fc_host
3915 * @fc_host: fc_host adding the structures to
3916 */
3917static int
3918fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
3919{
3920 struct device *dev = &shost->shost_gendev;
3921 struct fc_internal *i = to_fc_internal(shost->transportt);
3922 struct request_queue *q;
3923 int err;
James Bottomley3c559ea2009-06-21 12:11:43 -05003924 char bsg_name[20];
James Smart9e4f5e22009-03-26 13:33:19 -04003925
3926 fc_host->rqst_q = NULL;
3927
3928 if (!i->f->bsg_request)
3929 return -ENOTSUPP;
3930
3931 snprintf(bsg_name, sizeof(bsg_name),
3932 "fc_host%d", shost->host_no);
3933
3934 q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
3935 if (!q) {
3936 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3937 "initialize - no request queue\n",
3938 shost->host_no);
3939 return -ENOMEM;
3940 }
3941
3942 q->queuedata = shost;
3943 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003944 blk_queue_softirq_done(q, fc_bsg_softirq_done);
James Smart9e4f5e22009-03-26 13:33:19 -04003945 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
3946 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
3947
3948 err = bsg_register_queue(q, dev, bsg_name, NULL);
3949 if (err) {
3950 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3951 "initialize - register queue\n",
3952 shost->host_no);
3953 blk_cleanup_queue(q);
3954 return err;
3955 }
3956
3957 fc_host->rqst_q = q;
3958 return 0;
3959}
3960
3961
3962/**
3963 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3964 * @shost: shost that rport is attached to
3965 * @rport: rport that the bsg hooks are being attached to
3966 */
3967static int
3968fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
3969{
3970 struct device *dev = &rport->dev;
3971 struct fc_internal *i = to_fc_internal(shost->transportt);
3972 struct request_queue *q;
3973 int err;
3974
3975 rport->rqst_q = NULL;
3976
3977 if (!i->f->bsg_request)
3978 return -ENOTSUPP;
3979
3980 q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
3981 if (!q) {
3982 printk(KERN_ERR "%s: bsg interface failed to "
3983 "initialize - no request queue\n",
3984 dev->kobj.name);
3985 return -ENOMEM;
3986 }
3987
3988 q->queuedata = rport;
3989 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003990 blk_queue_softirq_done(q, fc_bsg_softirq_done);
James Smart9e4f5e22009-03-26 13:33:19 -04003991 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
3992 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
3993
3994 err = bsg_register_queue(q, dev, NULL, NULL);
3995 if (err) {
3996 printk(KERN_ERR "%s: bsg interface failed to "
3997 "initialize - register queue\n",
3998 dev->kobj.name);
3999 blk_cleanup_queue(q);
4000 return err;
4001 }
4002
4003 rport->rqst_q = q;
4004 return 0;
4005}
4006
4007
4008/**
4009 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
4010 * @q: the request_queue that is to be torn down.
4011 */
4012static void
4013fc_bsg_remove(struct request_queue *q)
4014{
4015 if (q) {
4016 bsg_unregister_queue(q);
4017 blk_cleanup_queue(q);
4018 }
4019}
4020
4021
James Smart9ef3e4a2007-05-24 19:04:44 -04004022/* Original Author: Martin Hicks */
4023MODULE_AUTHOR("James Smart");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024MODULE_DESCRIPTION("FC Transport Attributes");
4025MODULE_LICENSE("GPL");
4026
4027module_init(fc_transport_init);
4028module_exit(fc_transport_exit);