blob: 3ce56b3b2cd7939ae82f4c39ea9f85d994259aa4 [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)
652 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 error = transport_class_register(&fc_rport_class);
654 if (error)
655 return error;
656 return transport_class_register(&fc_transport_class);
657}
658
659static void __exit fc_transport_exit(void)
660{
661 transport_class_unregister(&fc_transport_class);
662 transport_class_unregister(&fc_rport_class);
663 transport_class_unregister(&fc_host_class);
James Smarta53eb5e2007-04-27 12:41:09 -0400664 transport_class_unregister(&fc_vport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667/*
668 * FC Remote Port Attribute Management
669 */
670
671#define fc_rport_show_function(field, format_string, sz, cast) \
672static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100673show_fc_rport_##field (struct device *dev, \
674 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100676 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct Scsi_Host *shost = rport_to_shost(rport); \
678 struct fc_internal *i = to_fc_internal(shost->transportt); \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400679 if ((i->f->get_rport_##field) && \
680 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500681 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400682 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 i->f->get_rport_##field(rport); \
684 return snprintf(buf, sz, format_string, cast rport->field); \
685}
686
687#define fc_rport_store_function(field) \
688static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100689store_fc_rport_##field(struct device *dev, \
690 struct device_attribute *attr, \
691 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{ \
693 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100694 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct Scsi_Host *shost = rport_to_shost(rport); \
696 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -0400697 char *cp; \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400698 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500699 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400700 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
701 return -EBUSY; \
James Smart0f29b962006-08-18 17:33:29 -0400702 val = simple_strtoul(buf, &cp, 0); \
703 if (*cp && (*cp != '\n')) \
704 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 i->f->set_rport_##field(rport, val); \
706 return count; \
707}
708
709#define fc_rport_rd_attr(field, format_string, sz) \
710 fc_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100711static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 show_fc_rport_##field, NULL)
713
714#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
715 fc_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100716static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 show_fc_rport_##field, NULL)
718
719#define fc_rport_rw_attr(field, format_string, sz) \
720 fc_rport_show_function(field, format_string, sz, ) \
721 fc_rport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100722static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 show_fc_rport_##field, \
724 store_fc_rport_##field)
725
726
727#define fc_private_rport_show_function(field, format_string, sz, cast) \
728static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100729show_fc_rport_##field (struct device *dev, \
730 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100732 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return snprintf(buf, sz, format_string, cast rport->field); \
734}
735
736#define fc_private_rport_rd_attr(field, format_string, sz) \
737 fc_private_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100738static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 show_fc_rport_##field, NULL)
740
741#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
742 fc_private_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100743static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 show_fc_rport_##field, NULL)
745
746
747#define fc_private_rport_rd_enum_attr(title, maxlen) \
748static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100749show_fc_rport_##title (struct device *dev, \
750 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100752 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 const char *name; \
754 name = get_fc_##title##_name(rport->title); \
755 if (!name) \
756 return -EINVAL; \
757 return snprintf(buf, maxlen, "%s\n", name); \
758} \
Tony Jonesee959b02008-02-22 00:13:36 +0100759static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 show_fc_rport_##title, NULL)
761
762
763#define SETUP_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100764 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
766 i->private_rport_attrs[count].store = NULL; \
767 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
768 if (i->f->show_rport_##field) \
769 count++
770
771#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100772 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
774 i->private_rport_attrs[count].store = NULL; \
775 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
776 count++
777
778#define SETUP_RPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100779 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (!i->f->set_rport_##field) { \
781 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
782 i->private_rport_attrs[count].store = NULL; \
783 } \
784 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
785 if (i->f->show_rport_##field) \
786 count++
787
James Smart0f29b962006-08-18 17:33:29 -0400788#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
789{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100790 i->private_rport_attrs[count] = device_attr_rport_##field; \
James Smart0f29b962006-08-18 17:33:29 -0400791 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
792 count++; \
793}
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796/* The FC Transport Remote Port Attributes: */
797
798/* Fixed Remote Port Attributes */
799
800fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
801
802static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100803show_fc_rport_supported_classes (struct device *dev,
804 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Tony Jonesee959b02008-02-22 00:13:36 +0100806 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (rport->supported_classes == FC_COS_UNSPECIFIED)
808 return snprintf(buf, 20, "unspecified\n");
809 return get_fc_cos_names(rport->supported_classes, buf);
810}
Tony Jonesee959b02008-02-22 00:13:36 +0100811static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 show_fc_rport_supported_classes, NULL);
813
814/* Dynamic Remote Port Attributes */
815
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400816/*
817 * dev_loss_tmo attribute
818 */
819fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
820static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100821store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
822 const char *buf, size_t count)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400823{
824 int val;
Tony Jonesee959b02008-02-22 00:13:36 +0100825 struct fc_rport *rport = transport_class_to_rport(dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400826 struct Scsi_Host *shost = rport_to_shost(rport);
827 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart0f29b962006-08-18 17:33:29 -0400828 char *cp;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400829 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500830 (rport->port_state == FC_PORTSTATE_DELETED) ||
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400831 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
832 return -EBUSY;
James Smart0f29b962006-08-18 17:33:29 -0400833 val = simple_strtoul(buf, &cp, 0);
834 if ((*cp && (*cp != '\n')) ||
835 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400836 return -EINVAL;
837 i->f->set_rport_dev_loss_tmo(rport, val);
838 return count;
839}
Tony Jonesee959b02008-02-22 00:13:36 +0100840static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400841 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843
844/* Private Remote Port Attributes */
845
846fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
847fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
848fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
849
850static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100851show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
852 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Tony Jonesee959b02008-02-22 00:13:36 +0100854 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /* identify any roles that are port_id specific */
857 if ((rport->port_id != -1) &&
858 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
859 FC_WELLKNOWN_PORTID_MASK) {
860 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
861 case FC_FPORT_PORTID:
862 return snprintf(buf, 30, "Fabric Port\n");
863 case FC_FABCTLR_PORTID:
864 return snprintf(buf, 30, "Fabric Controller\n");
865 case FC_DIRSRVR_PORTID:
866 return snprintf(buf, 30, "Directory Server\n");
867 case FC_TIMESRVR_PORTID:
868 return snprintf(buf, 30, "Time Server\n");
869 case FC_MGMTSRVR_PORTID:
870 return snprintf(buf, 30, "Management Server\n");
871 default:
872 return snprintf(buf, 30, "Unknown Fabric Entity\n");
873 }
874 } else {
James Smarta53eb5e2007-04-27 12:41:09 -0400875 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return snprintf(buf, 20, "unknown\n");
James Smarta53eb5e2007-04-27 12:41:09 -0400877 return get_fc_port_roles_names(rport->roles, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879}
Tony Jonesee959b02008-02-22 00:13:36 +0100880static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 show_fc_rport_roles, NULL);
882
883fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
884fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
885
James Smart0f29b962006-08-18 17:33:29 -0400886/*
887 * fast_io_fail_tmo attribute
888 */
889static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100890show_fc_rport_fast_io_fail_tmo (struct device *dev,
891 struct device_attribute *attr, char *buf)
James Smart0f29b962006-08-18 17:33:29 -0400892{
Tony Jonesee959b02008-02-22 00:13:36 +0100893 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400894
895 if (rport->fast_io_fail_tmo == -1)
896 return snprintf(buf, 5, "off\n");
897 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
898}
899
900static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100901store_fc_rport_fast_io_fail_tmo(struct device *dev,
902 struct device_attribute *attr, const char *buf,
903 size_t count)
James Smart0f29b962006-08-18 17:33:29 -0400904{
905 int val;
906 char *cp;
Tony Jonesee959b02008-02-22 00:13:36 +0100907 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400908
909 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
910 (rport->port_state == FC_PORTSTATE_DELETED) ||
911 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
912 return -EBUSY;
913 if (strncmp(buf, "off", 3) == 0)
914 rport->fast_io_fail_tmo = -1;
915 else {
916 val = simple_strtoul(buf, &cp, 0);
917 if ((*cp && (*cp != '\n')) ||
918 (val < 0) || (val >= rport->dev_loss_tmo))
919 return -EINVAL;
920 rport->fast_io_fail_tmo = val;
921 }
922 return count;
923}
Tony Jonesee959b02008-02-22 00:13:36 +0100924static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
James Smart0f29b962006-08-18 17:33:29 -0400925 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927
928/*
929 * FC SCSI Target Attribute Management
930 */
931
932/*
933 * Note: in the target show function we recognize when the remote
James Smarta53eb5e2007-04-27 12:41:09 -0400934 * port is in the heirarchy and do not allow the driver to get
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 * involved in sysfs functions. The driver only gets involved if
936 * it's the "old" style that doesn't use rports.
937 */
938#define fc_starget_show_function(field, format_string, sz, cast) \
939static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100940show_fc_starget_##field (struct device *dev, \
941 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100943 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
945 struct fc_internal *i = to_fc_internal(shost->transportt); \
946 struct fc_rport *rport = starget_to_rport(starget); \
947 if (rport) \
948 fc_starget_##field(starget) = rport->field; \
949 else if (i->f->get_starget_##field) \
950 i->f->get_starget_##field(starget); \
951 return snprintf(buf, sz, format_string, \
952 cast fc_starget_##field(starget)); \
953}
954
955#define fc_starget_rd_attr(field, format_string, sz) \
956 fc_starget_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100957static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 show_fc_starget_##field, NULL)
959
960#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
961 fc_starget_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100962static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 show_fc_starget_##field, NULL)
964
965#define SETUP_STARGET_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100966 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
968 i->private_starget_attrs[count].store = NULL; \
969 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
970 if (i->f->show_starget_##field) \
971 count++
972
973#define SETUP_STARGET_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100974 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!i->f->set_starget_##field) { \
976 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
977 i->private_starget_attrs[count].store = NULL; \
978 } \
979 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
980 if (i->f->show_starget_##field) \
981 count++
982
983/* The FC Transport SCSI Target Attributes: */
984fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
985fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
986fc_starget_rd_attr(port_id, "0x%06x\n", 20);
987
988
989/*
James Smarta53eb5e2007-04-27 12:41:09 -0400990 * FC Virtual Port Attribute Management
991 */
992
993#define fc_vport_show_function(field, format_string, sz, cast) \
994static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100995show_fc_vport_##field (struct device *dev, \
996 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -0400997{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100998 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -0400999 struct Scsi_Host *shost = vport_to_shost(vport); \
1000 struct fc_internal *i = to_fc_internal(shost->transportt); \
1001 if ((i->f->get_vport_##field) && \
1002 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1003 i->f->get_vport_##field(vport); \
1004 return snprintf(buf, sz, format_string, cast vport->field); \
1005}
1006
1007#define fc_vport_store_function(field) \
1008static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001009store_fc_vport_##field(struct device *dev, \
1010 struct device_attribute *attr, \
1011 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001012{ \
1013 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001014 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001015 struct Scsi_Host *shost = vport_to_shost(vport); \
1016 struct fc_internal *i = to_fc_internal(shost->transportt); \
1017 char *cp; \
1018 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1019 return -EBUSY; \
1020 val = simple_strtoul(buf, &cp, 0); \
1021 if (*cp && (*cp != '\n')) \
1022 return -EINVAL; \
1023 i->f->set_vport_##field(vport, val); \
1024 return count; \
1025}
1026
1027#define fc_vport_store_str_function(field, slen) \
1028static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001029store_fc_vport_##field(struct device *dev, \
1030 struct device_attribute *attr, \
1031 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001032{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001033 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001034 struct Scsi_Host *shost = vport_to_shost(vport); \
1035 struct fc_internal *i = to_fc_internal(shost->transportt); \
1036 unsigned int cnt=count; \
1037 \
1038 /* count may include a LF at end of string */ \
1039 if (buf[cnt-1] == '\n') \
1040 cnt--; \
1041 if (cnt > ((slen) - 1)) \
1042 return -EINVAL; \
1043 memcpy(vport->field, buf, cnt); \
1044 i->f->set_vport_##field(vport); \
1045 return count; \
1046}
1047
1048#define fc_vport_rd_attr(field, format_string, sz) \
1049 fc_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001050static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001051 show_fc_vport_##field, NULL)
1052
1053#define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1054 fc_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001055static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001056 show_fc_vport_##field, NULL)
1057
1058#define fc_vport_rw_attr(field, format_string, sz) \
1059 fc_vport_show_function(field, format_string, sz, ) \
1060 fc_vport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001061static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001062 show_fc_vport_##field, \
1063 store_fc_vport_##field)
1064
1065#define fc_private_vport_show_function(field, format_string, sz, cast) \
1066static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001067show_fc_vport_##field (struct device *dev, \
1068 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001069{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001070 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001071 return snprintf(buf, sz, format_string, cast vport->field); \
1072}
1073
1074#define fc_private_vport_store_u32_function(field) \
1075static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001076store_fc_vport_##field(struct device *dev, \
1077 struct device_attribute *attr, \
1078 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001079{ \
1080 u32 val; \
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 char *cp; \
1083 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1084 return -EBUSY; \
1085 val = simple_strtoul(buf, &cp, 0); \
1086 if (*cp && (*cp != '\n')) \
1087 return -EINVAL; \
1088 vport->field = val; \
1089 return count; \
1090}
1091
1092
1093#define fc_private_vport_rd_attr(field, format_string, sz) \
1094 fc_private_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001095static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001096 show_fc_vport_##field, NULL)
1097
1098#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1099 fc_private_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001100static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001101 show_fc_vport_##field, NULL)
1102
1103#define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1104 fc_private_vport_show_function(field, format_string, sz, ) \
1105 fc_private_vport_store_u32_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001106static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001107 show_fc_vport_##field, \
1108 store_fc_vport_##field)
1109
1110
1111#define fc_private_vport_rd_enum_attr(title, maxlen) \
1112static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001113show_fc_vport_##title (struct device *dev, \
1114 struct device_attribute *attr, \
1115 char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001116{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001117 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001118 const char *name; \
1119 name = get_fc_##title##_name(vport->title); \
1120 if (!name) \
1121 return -EINVAL; \
1122 return snprintf(buf, maxlen, "%s\n", name); \
1123} \
Tony Jonesee959b02008-02-22 00:13:36 +01001124static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001125 show_fc_vport_##title, NULL)
1126
1127
1128#define SETUP_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001129 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001130 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1131 i->private_vport_attrs[count].store = NULL; \
1132 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1133 if (i->f->get_##field) \
1134 count++
1135 /* NOTE: Above MACRO differs: checks function not show bit */
1136
1137#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001138 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001139 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1140 i->private_vport_attrs[count].store = NULL; \
1141 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1142 count++
1143
1144#define SETUP_VPORT_ATTRIBUTE_WR(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001145 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001146 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1147 if (i->f->field) \
1148 count++
1149 /* NOTE: Above MACRO differs: checks function */
1150
1151#define SETUP_VPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001152 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001153 if (!i->f->set_vport_##field) { \
1154 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1155 i->private_vport_attrs[count].store = NULL; \
1156 } \
1157 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1158 count++
1159 /* NOTE: Above MACRO differs: does not check show bit */
1160
1161#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1162{ \
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 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1165 count++; \
1166}
1167
1168
1169/* The FC Transport Virtual Port Attributes: */
1170
1171/* Fixed Virtual Port Attributes */
1172
1173/* Dynamic Virtual Port Attributes */
1174
1175/* Private Virtual Port Attributes */
1176
1177fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1178fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1179fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1180fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1181
1182static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001183show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1184 char *buf)
James Smarta53eb5e2007-04-27 12:41:09 -04001185{
Tony Jonesee959b02008-02-22 00:13:36 +01001186 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001187
1188 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1189 return snprintf(buf, 20, "unknown\n");
1190 return get_fc_port_roles_names(vport->roles, buf);
1191}
Tony Jonesee959b02008-02-22 00:13:36 +01001192static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
James Smarta53eb5e2007-04-27 12:41:09 -04001193
1194fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1195
1196fc_private_vport_show_function(symbolic_name, "%s\n",
1197 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1198fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
Tony Jonesee959b02008-02-22 00:13:36 +01001199static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001200 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1201
1202static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001203store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1204 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001205{
Tony Jonesee959b02008-02-22 00:13:36 +01001206 struct fc_vport *vport = transport_class_to_vport(dev);
James Smart9ef3e4a2007-05-24 19:04:44 -04001207 struct Scsi_Host *shost = vport_to_shost(vport);
James Smarta53eb5e2007-04-27 12:41:09 -04001208
James Smart9ef3e4a2007-05-24 19:04:44 -04001209 fc_queue_work(shost, &vport->vport_delete_work);
James Smarta53eb5e2007-04-27 12:41:09 -04001210 return count;
1211}
Tony Jonesee959b02008-02-22 00:13:36 +01001212static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001213 NULL, store_fc_vport_delete);
1214
1215
1216/*
1217 * Enable/Disable vport
1218 * Write "1" to disable, write "0" to enable
1219 */
1220static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001221store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1222 const char *buf,
James Smarta53eb5e2007-04-27 12:41:09 -04001223 size_t count)
1224{
Tony Jonesee959b02008-02-22 00:13:36 +01001225 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001226 struct Scsi_Host *shost = vport_to_shost(vport);
1227 struct fc_internal *i = to_fc_internal(shost->transportt);
1228 int stat;
1229
1230 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1231 return -EBUSY;
1232
1233 if (*buf == '0') {
1234 if (vport->vport_state != FC_VPORT_DISABLED)
1235 return -EALREADY;
1236 } else if (*buf == '1') {
1237 if (vport->vport_state == FC_VPORT_DISABLED)
1238 return -EALREADY;
1239 } else
1240 return -EINVAL;
1241
1242 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1243 return stat ? stat : count;
1244}
Tony Jonesee959b02008-02-22 00:13:36 +01001245static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001246 NULL, store_fc_vport_disable);
1247
1248
1249/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 * Host Attribute Management
1251 */
1252
1253#define fc_host_show_function(field, format_string, sz, cast) \
1254static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001255show_fc_host_##field (struct device *dev, \
1256 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001258 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct fc_internal *i = to_fc_internal(shost->transportt); \
1260 if (i->f->get_host_##field) \
1261 i->f->get_host_##field(shost); \
1262 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1263}
1264
1265#define fc_host_store_function(field) \
1266static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001267store_fc_host_##field(struct device *dev, \
1268 struct device_attribute *attr, \
1269 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{ \
1271 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001272 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -04001274 char *cp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 \
James Smart0f29b962006-08-18 17:33:29 -04001276 val = simple_strtoul(buf, &cp, 0); \
1277 if (*cp && (*cp != '\n')) \
1278 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 i->f->set_host_##field(shost, val); \
1280 return count; \
1281}
1282
James Smartb8d08212006-08-17 08:00:43 -04001283#define fc_host_store_str_function(field, slen) \
1284static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001285store_fc_host_##field(struct device *dev, \
1286 struct device_attribute *attr, \
1287 const char *buf, size_t count) \
James Smartb8d08212006-08-17 08:00:43 -04001288{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001289 struct Scsi_Host *shost = transport_class_to_shost(dev); \
James Smartb8d08212006-08-17 08:00:43 -04001290 struct fc_internal *i = to_fc_internal(shost->transportt); \
1291 unsigned int cnt=count; \
1292 \
1293 /* count may include a LF at end of string */ \
1294 if (buf[cnt-1] == '\n') \
1295 cnt--; \
1296 if (cnt > ((slen) - 1)) \
1297 return -EINVAL; \
1298 memcpy(fc_host_##field(shost), buf, cnt); \
1299 i->f->set_host_##field(shost); \
1300 return count; \
1301}
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303#define fc_host_rd_attr(field, format_string, sz) \
1304 fc_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001305static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 show_fc_host_##field, NULL)
1307
1308#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1309 fc_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001310static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 show_fc_host_##field, NULL)
1312
1313#define fc_host_rw_attr(field, format_string, sz) \
1314 fc_host_show_function(field, format_string, sz, ) \
1315 fc_host_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001316static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 show_fc_host_##field, \
1318 store_fc_host_##field)
1319
1320#define fc_host_rd_enum_attr(title, maxlen) \
1321static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001322show_fc_host_##title (struct device *dev, \
1323 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001325 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct fc_internal *i = to_fc_internal(shost->transportt); \
1327 const char *name; \
1328 if (i->f->get_host_##title) \
1329 i->f->get_host_##title(shost); \
1330 name = get_fc_##title##_name(fc_host_##title(shost)); \
1331 if (!name) \
1332 return -EINVAL; \
1333 return snprintf(buf, maxlen, "%s\n", name); \
1334} \
Tony Jonesee959b02008-02-22 00:13:36 +01001335static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337#define SETUP_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001338 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1340 i->private_host_attrs[count].store = NULL; \
1341 i->host_attrs[count] = &i->private_host_attrs[count]; \
1342 if (i->f->show_host_##field) \
1343 count++
1344
James Smarta53eb5e2007-04-27 12:41:09 -04001345#define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001346 i->private_host_attrs[count] = device_attr_host_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001347 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1348 i->private_host_attrs[count].store = NULL; \
1349 i->host_attrs[count] = &i->private_host_attrs[count]; \
1350 count++
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352#define SETUP_HOST_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001353 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (!i->f->set_host_##field) { \
1355 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1356 i->private_host_attrs[count].store = NULL; \
1357 } \
1358 i->host_attrs[count] = &i->private_host_attrs[count]; \
1359 if (i->f->show_host_##field) \
1360 count++
1361
1362
1363#define fc_private_host_show_function(field, format_string, sz, cast) \
1364static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001365show_fc_host_##field (struct device *dev, \
1366 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001368 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1370}
1371
1372#define fc_private_host_rd_attr(field, format_string, sz) \
1373 fc_private_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001374static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 show_fc_host_##field, NULL)
1376
1377#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1378 fc_private_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001379static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 show_fc_host_##field, NULL)
1381
1382#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001383 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1385 i->private_host_attrs[count].store = NULL; \
1386 i->host_attrs[count] = &i->private_host_attrs[count]; \
1387 count++
1388
1389#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001390{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001391 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 i->host_attrs[count] = &i->private_host_attrs[count]; \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001393 count++; \
1394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396
1397/* Fixed Host Attributes */
1398
1399static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001400show_fc_host_supported_classes (struct device *dev,
1401 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Tony Jonesee959b02008-02-22 00:13:36 +01001403 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1406 return snprintf(buf, 20, "unspecified\n");
1407
1408 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1409}
Tony Jonesee959b02008-02-22 00:13:36 +01001410static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 show_fc_host_supported_classes, NULL);
1412
1413static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001414show_fc_host_supported_fc4s (struct device *dev,
1415 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Tony Jonesee959b02008-02-22 00:13:36 +01001417 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1419}
Tony Jonesee959b02008-02-22 00:13:36 +01001420static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 show_fc_host_supported_fc4s, NULL);
1422
1423static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001424show_fc_host_supported_speeds (struct device *dev,
1425 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Tony Jonesee959b02008-02-22 00:13:36 +01001427 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1430 return snprintf(buf, 20, "unknown\n");
1431
1432 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1433}
Tony Jonesee959b02008-02-22 00:13:36 +01001434static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 show_fc_host_supported_speeds, NULL);
1436
1437
1438fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1439fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01001440fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1441 unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
James Smarta53eb5e2007-04-27 12:41:09 -04001443fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1445
1446
1447/* Dynamic Host Attributes */
1448
1449static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001450show_fc_host_active_fc4s (struct device *dev,
1451 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Tony Jonesee959b02008-02-22 00:13:36 +01001453 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 struct fc_internal *i = to_fc_internal(shost->transportt);
1455
1456 if (i->f->get_host_active_fc4s)
1457 i->f->get_host_active_fc4s(shost);
1458
1459 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1460}
Tony Jonesee959b02008-02-22 00:13:36 +01001461static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 show_fc_host_active_fc4s, NULL);
1463
1464static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001465show_fc_host_speed (struct device *dev,
1466 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Tony Jonesee959b02008-02-22 00:13:36 +01001468 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 struct fc_internal *i = to_fc_internal(shost->transportt);
1470
1471 if (i->f->get_host_speed)
1472 i->f->get_host_speed(shost);
1473
1474 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1475 return snprintf(buf, 20, "unknown\n");
1476
1477 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1478}
Tony Jonesee959b02008-02-22 00:13:36 +01001479static FC_DEVICE_ATTR(host, speed, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 show_fc_host_speed, NULL);
1481
1482
1483fc_host_rd_attr(port_id, "0x%06x\n", 20);
1484fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1485fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1486fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
James Smart016131b2006-08-14 08:20:25 -04001487fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
James Smartb8d08212006-08-17 08:00:43 -04001489fc_private_host_show_function(system_hostname, "%s\n",
1490 FC_SYMBOLIC_NAME_SIZE + 1, )
1491fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
Tony Jonesee959b02008-02-22 00:13:36 +01001492static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
James Smartb8d08212006-08-17 08:00:43 -04001493 show_fc_host_system_hostname, store_fc_host_system_hostname);
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496/* Private Host Attributes */
1497
1498static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001499show_fc_private_host_tgtid_bind_type(struct device *dev,
1500 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Tony Jonesee959b02008-02-22 00:13:36 +01001502 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 const char *name;
1504
1505 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1506 if (!name)
1507 return -EINVAL;
1508 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1509}
1510
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001511#define get_list_head_entry(pos, head, member) \
1512 pos = list_entry((head)->next, typeof(*pos), member)
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001515store_fc_private_host_tgtid_bind_type(struct device *dev,
1516 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Tony Jonesee959b02008-02-22 00:13:36 +01001518 struct Scsi_Host *shost = transport_class_to_shost(dev);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001519 struct fc_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 enum fc_tgtid_binding_type val;
1521 unsigned long flags;
1522
1523 if (get_fc_tgtid_bind_type_match(buf, &val))
1524 return -EINVAL;
1525
1526 /* if changing bind type, purge all unused consistent bindings */
1527 if (val != fc_host_tgtid_bind_type(shost)) {
1528 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001529 while (!list_empty(&fc_host_rport_bindings(shost))) {
1530 get_list_head_entry(rport,
1531 &fc_host_rport_bindings(shost), peers);
James Smartaedf3492006-04-10 10:14:05 -04001532 list_del(&rport->peers);
1533 rport->port_state = FC_PORTSTATE_DELETED;
1534 fc_queue_work(shost, &rport->rport_delete_work);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 spin_unlock_irqrestore(shost->host_lock, flags);
1537 }
1538
1539 fc_host_tgtid_bind_type(shost) = val;
1540 return count;
1541}
1542
Tony Jonesee959b02008-02-22 00:13:36 +01001543static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 show_fc_private_host_tgtid_bind_type,
1545 store_fc_private_host_tgtid_bind_type);
1546
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001547static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001548store_fc_private_host_issue_lip(struct device *dev,
1549 struct device_attribute *attr, const char *buf, size_t count)
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001550{
Tony Jonesee959b02008-02-22 00:13:36 +01001551 struct Scsi_Host *shost = transport_class_to_shost(dev);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001552 struct fc_internal *i = to_fc_internal(shost->transportt);
1553 int ret;
1554
1555 /* ignore any data value written to the attribute */
1556 if (i->f->issue_fc_host_lip) {
1557 ret = i->f->issue_fc_host_lip(shost);
1558 return ret ? ret: count;
1559 }
1560
1561 return -ENOENT;
1562}
1563
Tony Jonesee959b02008-02-22 00:13:36 +01001564static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001565 store_fc_private_host_issue_lip);
1566
James Smarta53eb5e2007-04-27 12:41:09 -04001567fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1568
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570/*
1571 * Host Statistics Management
1572 */
1573
1574/* Show a given an attribute in the statistics group */
1575static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001576fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Tony Jonesee959b02008-02-22 00:13:36 +01001578 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct fc_internal *i = to_fc_internal(shost->transportt);
1580 struct fc_host_statistics *stats;
1581 ssize_t ret = -ENOENT;
1582
1583 if (offset > sizeof(struct fc_host_statistics) ||
1584 offset % sizeof(u64) != 0)
1585 WARN_ON(1);
1586
1587 if (i->f->get_fc_host_stats) {
1588 stats = (i->f->get_fc_host_stats)(shost);
1589 if (stats)
1590 ret = snprintf(buf, 20, "0x%llx\n",
1591 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1592 }
1593 return ret;
1594}
1595
1596
1597/* generate a read-only statistics attribute */
1598#define fc_host_statistic(name) \
Tony Jonesee959b02008-02-22 00:13:36 +01001599static ssize_t show_fcstat_##name(struct device *cd, \
1600 struct device_attribute *attr, \
1601 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{ \
1603 return fc_stat_show(cd, buf, \
1604 offsetof(struct fc_host_statistics, name)); \
1605} \
Tony Jonesee959b02008-02-22 00:13:36 +01001606static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608fc_host_statistic(seconds_since_last_reset);
1609fc_host_statistic(tx_frames);
1610fc_host_statistic(tx_words);
1611fc_host_statistic(rx_frames);
1612fc_host_statistic(rx_words);
1613fc_host_statistic(lip_count);
1614fc_host_statistic(nos_count);
1615fc_host_statistic(error_frames);
1616fc_host_statistic(dumped_frames);
1617fc_host_statistic(link_failure_count);
1618fc_host_statistic(loss_of_sync_count);
1619fc_host_statistic(loss_of_signal_count);
1620fc_host_statistic(prim_seq_protocol_err_count);
1621fc_host_statistic(invalid_tx_word_count);
1622fc_host_statistic(invalid_crc_count);
1623fc_host_statistic(fcp_input_requests);
1624fc_host_statistic(fcp_output_requests);
1625fc_host_statistic(fcp_control_requests);
1626fc_host_statistic(fcp_input_megabytes);
1627fc_host_statistic(fcp_output_megabytes);
1628
1629static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001630fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1631 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Tony Jonesee959b02008-02-22 00:13:36 +01001633 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct fc_internal *i = to_fc_internal(shost->transportt);
1635
1636 /* ignore any data value written to the attribute */
1637 if (i->f->reset_fc_host_stats) {
1638 i->f->reset_fc_host_stats(shost);
1639 return count;
1640 }
1641
1642 return -ENOENT;
1643}
Tony Jonesee959b02008-02-22 00:13:36 +01001644static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 fc_reset_statistics);
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647static struct attribute *fc_statistics_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001648 &device_attr_host_seconds_since_last_reset.attr,
1649 &device_attr_host_tx_frames.attr,
1650 &device_attr_host_tx_words.attr,
1651 &device_attr_host_rx_frames.attr,
1652 &device_attr_host_rx_words.attr,
1653 &device_attr_host_lip_count.attr,
1654 &device_attr_host_nos_count.attr,
1655 &device_attr_host_error_frames.attr,
1656 &device_attr_host_dumped_frames.attr,
1657 &device_attr_host_link_failure_count.attr,
1658 &device_attr_host_loss_of_sync_count.attr,
1659 &device_attr_host_loss_of_signal_count.attr,
1660 &device_attr_host_prim_seq_protocol_err_count.attr,
1661 &device_attr_host_invalid_tx_word_count.attr,
1662 &device_attr_host_invalid_crc_count.attr,
1663 &device_attr_host_fcp_input_requests.attr,
1664 &device_attr_host_fcp_output_requests.attr,
1665 &device_attr_host_fcp_control_requests.attr,
1666 &device_attr_host_fcp_input_megabytes.attr,
1667 &device_attr_host_fcp_output_megabytes.attr,
1668 &device_attr_host_reset_statistics.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 NULL
1670};
1671
1672static struct attribute_group fc_statistics_group = {
1673 .name = "statistics",
1674 .attrs = fc_statistics_attrs,
1675};
1676
James Smarta53eb5e2007-04-27 12:41:09 -04001677
1678/* Host Vport Attributes */
1679
1680static int
1681fc_parse_wwn(const char *ns, u64 *nm)
1682{
1683 unsigned int i, j;
1684 u8 wwn[8];
1685
1686 memset(wwn, 0, sizeof(wwn));
1687
1688 /* Validate and store the new name */
1689 for (i=0, j=0; i < 16; i++) {
1690 if ((*ns >= 'a') && (*ns <= 'f'))
1691 j = ((j << 4) | ((*ns++ -'a') + 10));
1692 else if ((*ns >= 'A') && (*ns <= 'F'))
1693 j = ((j << 4) | ((*ns++ -'A') + 10));
1694 else if ((*ns >= '0') && (*ns <= '9'))
1695 j = ((j << 4) | (*ns++ -'0'));
1696 else
1697 return -EINVAL;
1698 if (i % 2) {
1699 wwn[i/2] = j & 0xff;
1700 j = 0;
1701 }
1702 }
1703
1704 *nm = wwn_to_u64(wwn);
1705
1706 return 0;
1707}
1708
1709
1710/*
1711 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1712 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1713 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1714 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1715 */
1716static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001717store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1718 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001719{
Tony Jonesee959b02008-02-22 00:13:36 +01001720 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001721 struct fc_vport_identifiers vid;
1722 struct fc_vport *vport;
1723 unsigned int cnt=count;
1724 int stat;
1725
1726 memset(&vid, 0, sizeof(vid));
1727
1728 /* count may include a LF at end of string */
1729 if (buf[cnt-1] == '\n')
1730 cnt--;
1731
1732 /* validate we have enough characters for WWPN */
1733 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1734 return -EINVAL;
1735
1736 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1737 if (stat)
1738 return stat;
1739
1740 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1741 if (stat)
1742 return stat;
1743
1744 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1745 vid.vport_type = FC_PORTTYPE_NPIV;
1746 /* vid.symbolic_name is already zero/NULL's */
1747 vid.disable = false; /* always enabled */
1748
1749 /* we only allow support on Channel 0 !!! */
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07001750 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
James Smarta53eb5e2007-04-27 12:41:09 -04001751 return stat ? stat : count;
1752}
Tony Jonesee959b02008-02-22 00:13:36 +01001753static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001754 store_fc_host_vport_create);
1755
1756
1757/*
1758 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1759 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1760 * The WWNs are specified as hex characters, and may *not* contain
1761 * any prefixes (e.g. 0x, x, etc)
1762 */
1763static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001764store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1765 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001766{
Tony Jonesee959b02008-02-22 00:13:36 +01001767 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001768 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1769 struct fc_vport *vport;
1770 u64 wwpn, wwnn;
1771 unsigned long flags;
1772 unsigned int cnt=count;
1773 int stat, match;
1774
1775 /* count may include a LF at end of string */
1776 if (buf[cnt-1] == '\n')
1777 cnt--;
1778
1779 /* validate we have enough characters for WWPN */
1780 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1781 return -EINVAL;
1782
1783 stat = fc_parse_wwn(&buf[0], &wwpn);
1784 if (stat)
1785 return stat;
1786
1787 stat = fc_parse_wwn(&buf[17], &wwnn);
1788 if (stat)
1789 return stat;
1790
1791 spin_lock_irqsave(shost->host_lock, flags);
1792 match = 0;
1793 /* we only allow support on Channel 0 !!! */
1794 list_for_each_entry(vport, &fc_host->vports, peers) {
1795 if ((vport->channel == 0) &&
1796 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1797 match = 1;
1798 break;
1799 }
1800 }
1801 spin_unlock_irqrestore(shost->host_lock, flags);
1802
1803 if (!match)
1804 return -ENODEV;
1805
1806 stat = fc_vport_terminate(vport);
1807 return stat ? stat : count;
1808}
Tony Jonesee959b02008-02-22 00:13:36 +01001809static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001810 store_fc_host_vport_delete);
1811
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813static int fc_host_match(struct attribute_container *cont,
1814 struct device *dev)
1815{
1816 struct Scsi_Host *shost;
1817 struct fc_internal *i;
1818
1819 if (!scsi_is_host_device(dev))
1820 return 0;
1821
1822 shost = dev_to_shost(dev);
1823 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1824 != &fc_host_class.class)
1825 return 0;
1826
1827 i = to_fc_internal(shost->transportt);
1828
1829 return &i->t.host_attrs.ac == cont;
1830}
1831
1832static int fc_target_match(struct attribute_container *cont,
1833 struct device *dev)
1834{
1835 struct Scsi_Host *shost;
1836 struct fc_internal *i;
1837
1838 if (!scsi_is_target_device(dev))
1839 return 0;
1840
1841 shost = dev_to_shost(dev->parent);
1842 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1843 != &fc_host_class.class)
1844 return 0;
1845
1846 i = to_fc_internal(shost->transportt);
1847
1848 return &i->t.target_attrs.ac == cont;
1849}
1850
1851static void fc_rport_dev_release(struct device *dev)
1852{
1853 struct fc_rport *rport = dev_to_rport(dev);
1854 put_device(dev->parent);
1855 kfree(rport);
1856}
1857
1858int scsi_is_fc_rport(const struct device *dev)
1859{
1860 return dev->release == fc_rport_dev_release;
1861}
1862EXPORT_SYMBOL(scsi_is_fc_rport);
1863
1864static int fc_rport_match(struct attribute_container *cont,
1865 struct device *dev)
1866{
1867 struct Scsi_Host *shost;
1868 struct fc_internal *i;
1869
1870 if (!scsi_is_fc_rport(dev))
1871 return 0;
1872
1873 shost = dev_to_shost(dev->parent);
1874 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1875 != &fc_host_class.class)
1876 return 0;
1877
1878 i = to_fc_internal(shost->transportt);
1879
1880 return &i->rport_attr_cont.ac == cont;
1881}
1882
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001883
James Smarta53eb5e2007-04-27 12:41:09 -04001884static void fc_vport_dev_release(struct device *dev)
1885{
1886 struct fc_vport *vport = dev_to_vport(dev);
1887 put_device(dev->parent); /* release kobj parent */
1888 kfree(vport);
1889}
1890
1891int scsi_is_fc_vport(const struct device *dev)
1892{
1893 return dev->release == fc_vport_dev_release;
1894}
1895EXPORT_SYMBOL(scsi_is_fc_vport);
1896
1897static int fc_vport_match(struct attribute_container *cont,
1898 struct device *dev)
1899{
1900 struct fc_vport *vport;
1901 struct Scsi_Host *shost;
1902 struct fc_internal *i;
1903
1904 if (!scsi_is_fc_vport(dev))
1905 return 0;
1906 vport = dev_to_vport(dev);
1907
1908 shost = vport_to_shost(vport);
1909 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1910 != &fc_host_class.class)
1911 return 0;
1912
1913 i = to_fc_internal(shost->transportt);
1914 return &i->vport_attr_cont.ac == cont;
1915}
1916
1917
James Smartc829c392006-03-13 08:28:57 -05001918/**
1919 * fc_timed_out - FC Transport I/O timeout intercept handler
James Smartc829c392006-03-13 08:28:57 -05001920 * @scmd: The SCSI command which timed out
1921 *
1922 * This routine protects against error handlers getting invoked while a
1923 * rport is in a blocked state, typically due to a temporarily loss of
1924 * connectivity. If the error handlers are allowed to proceed, requests
1925 * to abort i/o, reset the target, etc will likely fail as there is no way
1926 * to communicate with the device to perform the requested function. These
1927 * failures may result in the midlayer taking the device offline, requiring
1928 * manual intervention to restore operation.
1929 *
1930 * This routine, called whenever an i/o times out, validates the state of
1931 * the underlying rport. If the rport is blocked, it returns
1932 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1933 * Eventually, either the device will return, or devloss_tmo will fire,
1934 * and when the timeout then fires, it will be handled normally.
1935 * If the rport is not blocked, normal error handling continues.
1936 *
1937 * Notes:
1938 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05001939 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001940static enum blk_eh_timer_return
James Smartc829c392006-03-13 08:28:57 -05001941fc_timed_out(struct scsi_cmnd *scmd)
1942{
1943 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1944
1945 if (rport->port_state == FC_PORTSTATE_BLOCKED)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001946 return BLK_EH_RESET_TIMER;
James Smartc829c392006-03-13 08:28:57 -05001947
Jens Axboe242f9dc2008-09-14 05:55:09 -07001948 return BLK_EH_NOT_HANDLED;
James Smartc829c392006-03-13 08:28:57 -05001949}
1950
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001951/*
James Smartbda23252008-04-24 12:12:46 -04001952 * Called by fc_user_scan to locate an rport on the shost that
1953 * matches the channel and target id, and invoke scsi_scan_target()
1954 * on the rport.
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001955 */
James Smartbda23252008-04-24 12:12:46 -04001956static void
1957fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001958{
1959 struct fc_rport *rport;
James Smartbda23252008-04-24 12:12:46 -04001960 unsigned long flags;
1961
1962 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001963
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001964 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1965 if (rport->scsi_target_id == -1)
1966 continue;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001967
Hannes Reinecke0d2fcd92007-06-14 15:16:45 +02001968 if (rport->port_state != FC_PORTSTATE_ONLINE)
1969 continue;
1970
James Smartbda23252008-04-24 12:12:46 -04001971 if ((channel == rport->channel) &&
1972 (id == rport->scsi_target_id)) {
1973 spin_unlock_irqrestore(shost->host_lock, flags);
1974 scsi_scan_target(&rport->dev, channel, id, lun, 1);
1975 return;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001976 }
1977 }
1978
James Smartbda23252008-04-24 12:12:46 -04001979 spin_unlock_irqrestore(shost->host_lock, flags);
1980}
1981
1982/*
1983 * Called via sysfs scan routines. Necessary, as the FC transport
1984 * wants to place all target objects below the rport object. So this
1985 * routine must invoke the scsi_scan_target() routine with the rport
1986 * object as the parent.
1987 */
1988static int
1989fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
1990{
1991 uint chlo, chhi;
1992 uint tgtlo, tgthi;
1993
1994 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1995 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1996 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1997 return -EINVAL;
1998
1999 if (channel == SCAN_WILD_CARD) {
2000 chlo = 0;
2001 chhi = shost->max_channel + 1;
2002 } else {
2003 chlo = channel;
2004 chhi = channel + 1;
2005 }
2006
2007 if (id == SCAN_WILD_CARD) {
2008 tgtlo = 0;
2009 tgthi = shost->max_id;
2010 } else {
2011 tgtlo = id;
2012 tgthi = id + 1;
2013 }
2014
2015 for ( ; chlo < chhi; chlo++)
2016 for ( ; tgtlo < tgthi; tgtlo++)
2017 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2018
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002019 return 0;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002020}
2021
FUJITA Tomonori75252362007-09-01 02:02:27 +09002022static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
2023 int result)
2024{
2025 struct fc_internal *i = to_fc_internal(shost->transportt);
2026 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
2027}
2028
2029static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
2030{
2031 struct fc_internal *i = to_fc_internal(shost->transportt);
2032 return i->f->it_nexus_response(shost, nexus, result);
2033}
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035struct scsi_transport_template *
2036fc_attach_transport(struct fc_function_template *ft)
2037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 int count;
Jes Sorensen24669f752006-01-16 10:31:18 -05002039 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2040 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 if (unlikely(!i))
2043 return NULL;
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2046 i->t.target_attrs.ac.class = &fc_transport_class.class;
2047 i->t.target_attrs.ac.match = fc_target_match;
2048 i->t.target_size = sizeof(struct fc_starget_attrs);
2049 transport_container_register(&i->t.target_attrs);
2050
2051 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2052 i->t.host_attrs.ac.class = &fc_host_class.class;
2053 i->t.host_attrs.ac.match = fc_host_match;
2054 i->t.host_size = sizeof(struct fc_host_attrs);
2055 if (ft->get_fc_host_stats)
2056 i->t.host_attrs.statistics = &fc_statistics_group;
2057 transport_container_register(&i->t.host_attrs);
2058
2059 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2060 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2061 i->rport_attr_cont.ac.match = fc_rport_match;
2062 transport_container_register(&i->rport_attr_cont);
2063
James Smarta53eb5e2007-04-27 12:41:09 -04002064 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2065 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2066 i->vport_attr_cont.ac.match = fc_vport_match;
2067 transport_container_register(&i->vport_attr_cont);
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 i->f = ft;
2070
2071 /* Transport uses the shost workq for scsi scanning */
2072 i->t.create_work_queue = 1;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002073
James Smartc829c392006-03-13 08:28:57 -05002074 i->t.eh_timed_out = fc_timed_out;
2075
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002076 i->t.user_scan = fc_user_scan;
James Smart9ef3e4a2007-05-24 19:04:44 -04002077
FUJITA Tomonori75252362007-09-01 02:02:27 +09002078 /* target-mode drivers' functions */
2079 i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2080 i->t.it_nexus_response = fc_it_nexus_response;
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /*
2083 * Setup SCSI Target Attributes.
2084 */
2085 count = 0;
2086 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2087 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2088 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2089
2090 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2091
2092 i->starget_attrs[count] = NULL;
2093
2094
2095 /*
2096 * Setup SCSI Host Attributes.
2097 */
2098 count=0;
2099 SETUP_HOST_ATTRIBUTE_RD(node_name);
2100 SETUP_HOST_ATTRIBUTE_RD(port_name);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01002101 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2103 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2105 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
James Smarta53eb5e2007-04-27 12:41:09 -04002106 if (ft->vport_create) {
2107 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2108 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2111
2112 SETUP_HOST_ATTRIBUTE_RD(port_id);
2113 SETUP_HOST_ATTRIBUTE_RD(port_type);
2114 SETUP_HOST_ATTRIBUTE_RD(port_state);
2115 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2116 SETUP_HOST_ATTRIBUTE_RD(speed);
2117 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
James Smart016131b2006-08-14 08:20:25 -04002118 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
James Smartb8d08212006-08-17 08:00:43 -04002119 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /* Transport-managed attributes */
2122 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002123 if (ft->issue_fc_host_lip)
2124 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
James Smarta53eb5e2007-04-27 12:41:09 -04002125 if (ft->vport_create)
2126 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2127 if (ft->vport_delete)
2128 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 BUG_ON(count > FC_HOST_NUM_ATTRS);
2131
2132 i->host_attrs[count] = NULL;
2133
2134 /*
2135 * Setup Remote Port Attributes.
2136 */
2137 count=0;
2138 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2139 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2140 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2141 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2142 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2143 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2144 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2145 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2146 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
Mike Christiefff9d402008-08-19 18:45:23 -05002147 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2150
2151 i->rport_attrs[count] = NULL;
2152
James Smarta53eb5e2007-04-27 12:41:09 -04002153 /*
2154 * Setup Virtual Port Attributes.
2155 */
2156 count=0;
2157 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2158 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2159 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2160 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2161 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2162 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2163 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2164 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2165 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2166
2167 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2168
2169 i->vport_attrs[count] = NULL;
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return &i->t;
2172}
2173EXPORT_SYMBOL(fc_attach_transport);
2174
2175void fc_release_transport(struct scsi_transport_template *t)
2176{
2177 struct fc_internal *i = to_fc_internal(t);
2178
2179 transport_container_unregister(&i->t.target_attrs);
2180 transport_container_unregister(&i->t.host_attrs);
2181 transport_container_unregister(&i->rport_attr_cont);
James Smarta53eb5e2007-04-27 12:41:09 -04002182 transport_container_unregister(&i->vport_attr_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 kfree(i);
2185}
2186EXPORT_SYMBOL(fc_release_transport);
2187
James Smartaedf3492006-04-10 10:14:05 -04002188/**
2189 * fc_queue_work - Queue work to the fc_host workqueue.
2190 * @shost: Pointer to Scsi_Host bound to fc_host.
2191 * @work: Work to queue for execution.
2192 *
2193 * Return value:
James Smarta0785ed2006-05-11 13:27:09 -04002194 * 1 - work queued for execution
2195 * 0 - work is already queued
2196 * -EINVAL - work queue doesn't exist
Rob Landleyeb448202007-11-03 13:30:39 -05002197 */
James Smartaedf3492006-04-10 10:14:05 -04002198static int
2199fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2200{
2201 if (unlikely(!fc_host_work_q(shost))) {
2202 printk(KERN_ERR
2203 "ERROR: FC host '%s' attempted to queue work, "
2204 "when no workqueue created.\n", shost->hostt->name);
2205 dump_stack();
2206
2207 return -EINVAL;
2208 }
2209
2210 return queue_work(fc_host_work_q(shost), work);
2211}
2212
2213/**
2214 * fc_flush_work - Flush a fc_host's workqueue.
2215 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002216 */
James Smartaedf3492006-04-10 10:14:05 -04002217static void
2218fc_flush_work(struct Scsi_Host *shost)
2219{
2220 if (!fc_host_work_q(shost)) {
2221 printk(KERN_ERR
2222 "ERROR: FC host '%s' attempted to flush work, "
2223 "when no workqueue created.\n", shost->hostt->name);
2224 dump_stack();
2225 return;
2226 }
2227
2228 flush_workqueue(fc_host_work_q(shost));
2229}
2230
2231/**
2232 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2233 * @shost: Pointer to Scsi_Host bound to fc_host.
2234 * @work: Work to queue for execution.
2235 * @delay: jiffies to delay the work queuing
2236 *
2237 * Return value:
James Smart0f29b962006-08-18 17:33:29 -04002238 * 1 on success / 0 already queued / < 0 for error
Rob Landleyeb448202007-11-03 13:30:39 -05002239 */
James Smartaedf3492006-04-10 10:14:05 -04002240static int
David Howellsc4028952006-11-22 14:57:56 +00002241fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
James Smartaedf3492006-04-10 10:14:05 -04002242 unsigned long delay)
2243{
2244 if (unlikely(!fc_host_devloss_work_q(shost))) {
2245 printk(KERN_ERR
2246 "ERROR: FC host '%s' attempted to queue work, "
2247 "when no workqueue created.\n", shost->hostt->name);
2248 dump_stack();
2249
2250 return -EINVAL;
2251 }
2252
2253 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2254}
2255
2256/**
2257 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2258 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002259 */
James Smartaedf3492006-04-10 10:14:05 -04002260static void
2261fc_flush_devloss(struct Scsi_Host *shost)
2262{
2263 if (!fc_host_devloss_work_q(shost)) {
2264 printk(KERN_ERR
2265 "ERROR: FC host '%s' attempted to flush work, "
2266 "when no workqueue created.\n", shost->hostt->name);
2267 dump_stack();
2268 return;
2269 }
2270
2271 flush_workqueue(fc_host_devloss_work_q(shost));
2272}
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275/**
Rob Landleyeb448202007-11-03 13:30:39 -05002276 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2277 * @shost: Which &Scsi_Host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 *
2279 * This routine is expected to be called immediately preceeding the
2280 * a driver's call to scsi_remove_host().
2281 *
2282 * WARNING: A driver utilizing the fc_transport, which fails to call
Rob Landleyeb448202007-11-03 13:30:39 -05002283 * this routine prior to scsi_remove_host(), will leave dangling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 * objects in /sys/class/fc_remote_ports. Access to any of these
2285 * objects can result in a system crash !!!
2286 *
2287 * Notes:
2288 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290void
2291fc_remove_host(struct Scsi_Host *shost)
2292{
James Smarta53eb5e2007-04-27 12:41:09 -04002293 struct fc_vport *vport = NULL, *next_vport = NULL;
2294 struct fc_rport *rport = NULL, *next_rport = NULL;
James Smartaedf3492006-04-10 10:14:05 -04002295 struct workqueue_struct *work_q;
2296 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James Smarta53eb5e2007-04-27 12:41:09 -04002297 unsigned long flags;
James Smarta53eb5e2007-04-27 12:41:09 -04002298
2299 spin_lock_irqsave(shost->host_lock, flags);
2300
2301 /* Remove any vports */
James Smart9ef3e4a2007-05-24 19:04:44 -04002302 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2303 fc_queue_work(shost, &vport->vport_delete_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 /* Remove any remote ports */
2306 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002307 &fc_host->rports, peers) {
2308 list_del(&rport->peers);
2309 rport->port_state = FC_PORTSTATE_DELETED;
2310 fc_queue_work(shost, &rport->rport_delete_work);
2311 }
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002314 &fc_host->rport_bindings, peers) {
2315 list_del(&rport->peers);
2316 rport->port_state = FC_PORTSTATE_DELETED;
2317 fc_queue_work(shost, &rport->rport_delete_work);
2318 }
2319
James Smarta53eb5e2007-04-27 12:41:09 -04002320 spin_unlock_irqrestore(shost->host_lock, flags);
2321
James Smartaedf3492006-04-10 10:14:05 -04002322 /* flush all scan work items */
2323 scsi_flush_work(shost);
2324
2325 /* flush all stgt delete, and rport delete work items, then kill it */
2326 if (fc_host->work_q) {
2327 work_q = fc_host->work_q;
2328 fc_host->work_q = NULL;
2329 destroy_workqueue(work_q);
2330 }
2331
2332 /* flush all devloss work items, then kill it */
2333 if (fc_host->devloss_work_q) {
2334 work_q = fc_host->devloss_work_q;
2335 fc_host->devloss_work_q = NULL;
2336 destroy_workqueue(work_q);
2337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339EXPORT_SYMBOL(fc_remove_host);
2340
Mike Christiefff9d402008-08-19 18:45:23 -05002341static void fc_terminate_rport_io(struct fc_rport *rport)
2342{
2343 struct Scsi_Host *shost = rport_to_shost(rport);
2344 struct fc_internal *i = to_fc_internal(shost->transportt);
2345
2346 /* Involve the LLDD if possible to terminate all io on the rport. */
2347 if (i->f->terminate_rport_io)
2348 i->f->terminate_rport_io(rport);
2349
2350 /*
2351 * must unblock to flush queued IO. The caller will have set
2352 * the port_state or flags, so that fc_remote_port_chkready will
2353 * fail IO.
2354 */
2355 scsi_target_unblock(&rport->dev);
2356}
James Smartaedf3492006-04-10 10:14:05 -04002357
2358/**
2359 * fc_starget_delete - called to delete the scsi decendents of an rport
David Howellsc4028952006-11-22 14:57:56 +00002360 * @work: remote port to be operated on.
Rob Landleyeb448202007-11-03 13:30:39 -05002361 *
2362 * Deletes target and all sdevs.
2363 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002364static void
David Howellsc4028952006-11-22 14:57:56 +00002365fc_starget_delete(struct work_struct *work)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002366{
David Howellsc4028952006-11-22 14:57:56 +00002367 struct fc_rport *rport =
2368 container_of(work, struct fc_rport, stgt_delete_work);
James Smart0f29b962006-08-18 17:33:29 -04002369
Mike Christiefff9d402008-08-19 18:45:23 -05002370 fc_terminate_rport_io(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002371 scsi_remove_target(&rport->dev);
2372}
2373
James Smartaedf3492006-04-10 10:14:05 -04002374
2375/**
2376 * fc_rport_final_delete - finish rport termination and delete it.
David Howellsc4028952006-11-22 14:57:56 +00002377 * @work: remote port to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05002378 */
James Smartaedf3492006-04-10 10:14:05 -04002379static void
David Howellsc4028952006-11-22 14:57:56 +00002380fc_rport_final_delete(struct work_struct *work)
James Smartaedf3492006-04-10 10:14:05 -04002381{
David Howellsc4028952006-11-22 14:57:56 +00002382 struct fc_rport *rport =
2383 container_of(work, struct fc_rport, rport_delete_work);
James Smartaedf3492006-04-10 10:14:05 -04002384 struct device *dev = &rport->dev;
2385 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002386 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart92740b22007-04-27 11:53:17 -04002387 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05002388 int do_callback = 0;
James Smartaedf3492006-04-10 10:14:05 -04002389
2390 /*
James Smart9ef3e4a2007-05-24 19:04:44 -04002391 * if a scan is pending, flush the SCSI Host work_q so that
James Smartaedf3492006-04-10 10:14:05 -04002392 * that we can reclaim the rport scan work element.
2393 */
2394 if (rport->flags & FC_RPORT_SCAN_PENDING)
2395 scsi_flush_work(shost);
2396
Mike Christiefff9d402008-08-19 18:45:23 -05002397 fc_terminate_rport_io(rport);
James Smart9e4f5e22009-03-26 13:33:19 -04002398
James Smart92740b22007-04-27 11:53:17 -04002399 /*
2400 * Cancel any outstanding timers. These should really exist
2401 * only when rmmod'ing the LLDD and we're asking for
2402 * immediate termination of the rports
2403 */
2404 spin_lock_irqsave(shost->host_lock, flags);
2405 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2406 spin_unlock_irqrestore(shost->host_lock, flags);
2407 if (!cancel_delayed_work(&rport->fail_io_work))
2408 fc_flush_devloss(shost);
2409 if (!cancel_delayed_work(&rport->dev_loss_work))
2410 fc_flush_devloss(shost);
2411 spin_lock_irqsave(shost->host_lock, flags);
2412 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2413 }
2414 spin_unlock_irqrestore(shost->host_lock, flags);
2415
James Smart0f29b962006-08-18 17:33:29 -04002416 /* Delete SCSI target and sdevs */
2417 if (rport->scsi_target_id != -1)
David Howellsc4028952006-11-22 14:57:56 +00002418 fc_starget_delete(&rport->stgt_delete_work);
James Smart92740b22007-04-27 11:53:17 -04002419
2420 /*
2421 * Notify the driver that the rport is now dead. The LLDD will
2422 * also guarantee that any communication to the rport is terminated
James Smart4be98c02009-01-05 12:14:18 -05002423 *
2424 * Avoid this call if we already called it when we preserved the
2425 * rport for the binding.
James Smart92740b22007-04-27 11:53:17 -04002426 */
Michael Reed8798a692009-10-09 14:15:59 -05002427 spin_lock_irqsave(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05002428 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
Michael Reed8798a692009-10-09 14:15:59 -05002429 (i->f->dev_loss_tmo_callbk)) {
2430 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2431 do_callback = 1;
2432 }
2433 spin_unlock_irqrestore(shost->host_lock, flags);
2434
2435 if (do_callback)
James Smart0f29b962006-08-18 17:33:29 -04002436 i->f->dev_loss_tmo_callbk(rport);
James Smart0f29b962006-08-18 17:33:29 -04002437
James Smart9e4f5e22009-03-26 13:33:19 -04002438 fc_bsg_remove(rport->rqst_q);
2439
James Smartaedf3492006-04-10 10:14:05 -04002440 transport_remove_device(dev);
2441 device_del(dev);
2442 transport_destroy_device(dev);
James Smart3bdad7b2006-06-26 14:19:59 -04002443 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2444 put_device(dev); /* for self-reference */
James Smartaedf3492006-04-10 10:14:05 -04002445}
2446
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448/**
2449 * fc_rport_create - allocates and creates a remote FC port.
2450 * @shost: scsi host the remote port is connected to.
2451 * @channel: Channel on shost port connected to.
2452 * @ids: The world wide names, fc address, and FC4 port
2453 * roles for the remote port.
2454 *
2455 * Allocates and creates the remoter port structure, including the
2456 * class and sysfs creation.
2457 *
2458 * Notes:
2459 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002460 */
Adrian Bunk44818ef2007-07-09 11:59:59 -07002461static struct fc_rport *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462fc_rport_create(struct Scsi_Host *shost, int channel,
2463 struct fc_rport_identifiers *ids)
2464{
James Smartaedf3492006-04-10 10:14:05 -04002465 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 struct fc_internal *fci = to_fc_internal(shost->transportt);
2467 struct fc_rport *rport;
2468 struct device *dev;
2469 unsigned long flags;
2470 int error;
2471 size_t size;
2472
2473 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
Jes Sorensen24669f752006-01-16 10:31:18 -05002474 rport = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (unlikely(!rport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002476 printk(KERN_ERR "%s: allocation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return NULL;
2478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 rport->maxframe_size = -1;
2481 rport->supported_classes = FC_COS_UNSPECIFIED;
2482 rport->dev_loss_tmo = fc_dev_loss_tmo;
2483 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2484 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2485 rport->port_id = ids->port_id;
2486 rport->roles = ids->roles;
2487 rport->port_state = FC_PORTSTATE_ONLINE;
2488 if (fci->f->dd_fcrport_size)
2489 rport->dd_data = &rport[1];
2490 rport->channel = channel;
James Smart0f29b962006-08-18 17:33:29 -04002491 rport->fast_io_fail_tmo = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
David Howellsc4028952006-11-22 14:57:56 +00002493 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2494 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2495 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2496 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2497 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 spin_lock_irqsave(shost->host_lock, flags);
2500
2501 rport->number = fc_host->next_rport_number++;
James Smarta53eb5e2007-04-27 12:41:09 -04002502 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 rport->scsi_target_id = fc_host->next_target_id++;
2504 else
2505 rport->scsi_target_id = -1;
James Smartaedf3492006-04-10 10:14:05 -04002506 list_add_tail(&rport->peers, &fc_host->rports);
James Smart3bdad7b2006-06-26 14:19:59 -04002507 get_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 spin_unlock_irqrestore(shost->host_lock, flags);
2510
2511 dev = &rport->dev;
James Smart3bdad7b2006-06-26 14:19:59 -04002512 device_initialize(dev); /* takes self reference */
2513 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 dev->release = fc_rport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01002515 dev_set_name(dev, "rport-%d:%d-%d",
2516 shost->host_no, channel, rport->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 transport_setup_device(dev);
2518
2519 error = device_add(dev);
2520 if (error) {
2521 printk(KERN_ERR "FC Remote Port device_add failed\n");
2522 goto delete_rport;
2523 }
2524 transport_add_device(dev);
2525 transport_configure_device(dev);
2526
James Smart9e4f5e22009-03-26 13:33:19 -04002527 fc_bsg_rportadd(shost, rport);
2528 /* ignore any bsg add error - we just can't do sgio */
2529
James Smarta53eb5e2007-04-27 12:41:09 -04002530 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002532 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 return rport;
2537
2538delete_rport:
2539 transport_destroy_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 spin_lock_irqsave(shost->host_lock, flags);
2541 list_del(&rport->peers);
James Smart3bdad7b2006-06-26 14:19:59 -04002542 put_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 spin_unlock_irqrestore(shost->host_lock, flags);
2544 put_device(dev->parent);
2545 kfree(rport);
2546 return NULL;
2547}
2548
2549/**
Rob Landleyeb448202007-11-03 13:30:39 -05002550 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 * @shost: scsi host the remote port is connected to.
2552 * @channel: Channel on shost port connected to.
2553 * @ids: The world wide names, fc address, and FC4 port
2554 * roles for the remote port.
2555 *
2556 * The LLDD calls this routine to notify the transport of the existence
2557 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2558 * of the port, it's FC address (port_id), and the FC4 roles that are
2559 * active for the port.
2560 *
2561 * For ports that are FCP targets (aka scsi targets), the FC transport
2562 * maintains consistent target id bindings on behalf of the LLDD.
2563 * A consistent target id binding is an assignment of a target id to
2564 * a remote port identifier, which persists while the scsi host is
2565 * attached. The remote port can disappear, then later reappear, and
2566 * it's target id assignment remains the same. This allows for shifts
2567 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2568 * changes to the scsi subsystem which is based on scsi host number and
2569 * target id values. Bindings are only valid during the attachment of
2570 * the scsi host. If the host detaches, then later re-attaches, target
2571 * id bindings may change.
2572 *
2573 * This routine is responsible for returning a remote port structure.
2574 * The routine will search the list of remote ports it maintains
2575 * internally on behalf of consistent target id mappings. If found, the
2576 * remote port structure will be reused. Otherwise, a new remote port
2577 * structure will be allocated.
2578 *
2579 * Whenever a remote port is allocated, a new fc_remote_port class
2580 * device is created.
2581 *
2582 * Should not be called from interrupt context.
2583 *
2584 * Notes:
2585 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002586 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587struct fc_rport *
2588fc_remote_port_add(struct Scsi_Host *shost, int channel,
2589 struct fc_rport_identifiers *ids)
2590{
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002591 struct fc_internal *fci = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04002592 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 struct fc_rport *rport;
2594 unsigned long flags;
2595 int match = 0;
2596
James Smartaedf3492006-04-10 10:14:05 -04002597 /* ensure any stgt delete functions are done */
2598 fc_flush_work(shost);
2599
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002600 /*
2601 * Search the list of "active" rports, for an rport that has been
2602 * deleted, but we've held off the real delete while the target
2603 * is in a "blocked" state.
2604 */
2605 spin_lock_irqsave(shost->host_lock, flags);
2606
James Smartaedf3492006-04-10 10:14:05 -04002607 list_for_each_entry(rport, &fc_host->rports, peers) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002608
2609 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2610 (rport->channel == channel)) {
2611
James Smartaedf3492006-04-10 10:14:05 -04002612 switch (fc_host->tgtid_bind_type) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002613 case FC_TGTID_BIND_BY_WWPN:
2614 case FC_TGTID_BIND_NONE:
2615 if (rport->port_name == ids->port_name)
2616 match = 1;
2617 break;
2618 case FC_TGTID_BIND_BY_WWNN:
2619 if (rport->node_name == ids->node_name)
2620 match = 1;
2621 break;
2622 case FC_TGTID_BIND_BY_ID:
2623 if (rport->port_id == ids->port_id)
2624 match = 1;
2625 break;
2626 }
2627
2628 if (match) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002629
2630 memcpy(&rport->node_name, &ids->node_name,
2631 sizeof(rport->node_name));
2632 memcpy(&rport->port_name, &ids->port_name,
2633 sizeof(rport->port_name));
2634 rport->port_id = ids->port_id;
2635
2636 rport->port_state = FC_PORTSTATE_ONLINE;
2637 rport->roles = ids->roles;
2638
2639 spin_unlock_irqrestore(shost->host_lock, flags);
2640
2641 if (fci->f->dd_fcrport_size)
2642 memset(rport->dd_data, 0,
2643 fci->f->dd_fcrport_size);
2644
2645 /*
James Smart92740b22007-04-27 11:53:17 -04002646 * If we were not a target, cancel the
2647 * io terminate and rport timers, and
2648 * we're done.
2649 *
2650 * If we were a target, but our new role
2651 * doesn't indicate a target, leave the
2652 * timers running expecting the role to
2653 * change as the target fully logs in. If
2654 * it doesn't, the target will be torn down.
2655 *
2656 * If we were a target, and our role shows
2657 * we're still a target, cancel the timers
2658 * and kick off a scan.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002659 */
James Smart92740b22007-04-27 11:53:17 -04002660
2661 /* was a target, not in roles */
2662 if ((rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04002663 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002664 return rport;
2665
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002666 /*
James Smart92740b22007-04-27 11:53:17 -04002667 * Stop the fail io and dev_loss timers.
2668 * If they flush, the port_state will
2669 * be checked and will NOOP the function.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002670 */
James Smart0f29b962006-08-18 17:33:29 -04002671 if (!cancel_delayed_work(&rport->fail_io_work))
2672 fc_flush_devloss(shost);
James Smart92740b22007-04-27 11:53:17 -04002673 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002674 fc_flush_devloss(shost);
2675
2676 spin_lock_irqsave(shost->host_lock, flags);
2677
Mike Christiefff9d402008-08-19 18:45:23 -05002678 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
James Smart4be98c02009-01-05 12:14:18 -05002679 FC_RPORT_DEVLOSS_PENDING |
2680 FC_RPORT_DEVLOSS_CALLBK_DONE);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002681
James Smart92740b22007-04-27 11:53:17 -04002682 /* if target, initiate a scan */
2683 if (rport->scsi_target_id != -1) {
2684 rport->flags |= FC_RPORT_SCAN_PENDING;
2685 scsi_queue_work(shost,
2686 &rport->scan_work);
2687 spin_unlock_irqrestore(shost->host_lock,
2688 flags);
2689 scsi_target_unblock(&rport->dev);
2690 } else
2691 spin_unlock_irqrestore(shost->host_lock,
2692 flags);
James Smarta0785ed2006-05-11 13:27:09 -04002693
James Smart9e4f5e22009-03-26 13:33:19 -04002694 fc_bsg_goose_queue(rport);
2695
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002696 return rport;
2697 }
2698 }
2699 }
2700
James Smart92740b22007-04-27 11:53:17 -04002701 /*
2702 * Search the bindings array
2703 * Note: if never a FCP target, you won't be on this list
2704 */
James Smartaedf3492006-04-10 10:14:05 -04002705 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 /* search for a matching consistent binding */
2708
James Smartaedf3492006-04-10 10:14:05 -04002709 list_for_each_entry(rport, &fc_host->rport_bindings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 peers) {
2711 if (rport->channel != channel)
2712 continue;
2713
James Smartaedf3492006-04-10 10:14:05 -04002714 switch (fc_host->tgtid_bind_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 case FC_TGTID_BIND_BY_WWPN:
2716 if (rport->port_name == ids->port_name)
2717 match = 1;
2718 break;
2719 case FC_TGTID_BIND_BY_WWNN:
2720 if (rport->node_name == ids->node_name)
2721 match = 1;
2722 break;
2723 case FC_TGTID_BIND_BY_ID:
2724 if (rport->port_id == ids->port_id)
2725 match = 1;
2726 break;
2727 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2728 break;
2729 }
2730
2731 if (match) {
James Smartaedf3492006-04-10 10:14:05 -04002732 list_move_tail(&rport->peers, &fc_host->rports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 break;
2734 }
2735 }
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 if (match) {
2738 memcpy(&rport->node_name, &ids->node_name,
2739 sizeof(rport->node_name));
2740 memcpy(&rport->port_name, &ids->port_name,
2741 sizeof(rport->port_name));
2742 rport->port_id = ids->port_id;
2743 rport->roles = ids->roles;
2744 rport->port_state = FC_PORTSTATE_ONLINE;
Mike Christiefff9d402008-08-19 18:45:23 -05002745 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002747 if (fci->f->dd_fcrport_size)
2748 memset(rport->dd_data, 0,
2749 fci->f->dd_fcrport_size);
2750
James Smarta53eb5e2007-04-27 12:41:09 -04002751 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002753 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 scsi_queue_work(shost, &rport->scan_work);
James Smarta0785ed2006-05-11 13:27:09 -04002755 spin_unlock_irqrestore(shost->host_lock, flags);
2756 scsi_target_unblock(&rport->dev);
2757 } else
2758 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 return rport;
2761 }
2762 }
2763
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002764 spin_unlock_irqrestore(shost->host_lock, flags);
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 /* No consistent binding found - create new remote port entry */
2767 rport = fc_rport_create(shost, channel, ids);
2768
2769 return rport;
2770}
2771EXPORT_SYMBOL(fc_remote_port_add);
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774/**
Rob Landleyeb448202007-11-03 13:30:39 -05002775 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 * @rport: The remote port that no longer exists
2777 *
2778 * The LLDD calls this routine to notify the transport that a remote
2779 * port is no longer part of the topology. Note: Although a port
2780 * may no longer be part of the topology, it may persist in the remote
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002781 * ports displayed by the fc_host. We do this under 2 conditions:
Rob Landleyeb448202007-11-03 13:30:39 -05002782 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002783 * This allows the port to temporarily disappear, then reappear without
2784 * disrupting the SCSI device tree attached to it. During the "blocked"
2785 * period the port will still exist.
Rob Landleyeb448202007-11-03 13:30:39 -05002786 * 2) If the port was a scsi target and disappears for longer than we
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002787 * expect, we'll delete the port and the tear down the SCSI device tree
2788 * attached to it. However, we want to semi-persist the target id assigned
2789 * to that port if it eventually does exist. The port structure will
2790 * remain (although with minimal information) so that the target id
2791 * bindings remails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 *
2793 * If the remote port is not an FCP Target, it will be fully torn down
2794 * and deallocated, including the fc_remote_port class device.
2795 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002796 * If the remote port is an FCP Target, the port will be placed in a
2797 * temporary blocked state. From the LLDD's perspective, the rport no
2798 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2799 * exists, but all sdevs on it are blocked from further I/O. The following
Rob Landleyeb448202007-11-03 13:30:39 -05002800 * is then expected.
2801 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002802 * If the remote port does not return (signaled by a LLDD call to
2803 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2804 * scsi target is removed - killing all outstanding i/o and removing the
2805 * scsi devices attached ot it. The port structure will be marked Not
2806 * Present and be partially cleared, leaving only enough information to
2807 * recognize the remote port relative to the scsi target id binding if
2808 * it later appears. The port will remain as long as there is a valid
2809 * binding (e.g. until the user changes the binding type or unloads the
2810 * scsi host with the binding).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002812 * If the remote port returns within the dev_loss_tmo value (and matches
2813 * according to the target id binding type), the port structure will be
2814 * reused. If it is no longer a SCSI target, the target will be torn
2815 * down. If it continues to be a SCSI target, then the target will be
2816 * unblocked (allowing i/o to be resumed), and a scan will be activated
2817 * to ensure that all luns are detected.
2818 *
2819 * Called from normal process context only - cannot be called from interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 *
2821 * Notes:
2822 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002823 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824void
2825fc_remote_port_delete(struct fc_rport *rport)
2826{
James Smartaedf3492006-04-10 10:14:05 -04002827 struct Scsi_Host *shost = rport_to_shost(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002828 int timeout = rport->dev_loss_tmo;
James Smartaedf3492006-04-10 10:14:05 -04002829 unsigned long flags;
2830
2831 /*
2832 * No need to flush the fc_host work_q's, as all adds are synchronous.
2833 *
2834 * We do need to reclaim the rport scan work element, so eventually
2835 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2836 * there's still a scan pending.
2837 */
2838
2839 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
James Smart92740b22007-04-27 11:53:17 -04002841 if (rport->port_state != FC_PORTSTATE_ONLINE) {
James Smartaedf3492006-04-10 10:14:05 -04002842 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return;
2844 }
2845
James Smart92740b22007-04-27 11:53:17 -04002846 /*
2847 * In the past, we if this was not an FCP-Target, we would
2848 * unconditionally just jump to deleting the rport.
2849 * However, rports can be used as node containers by the LLDD,
2850 * and its not appropriate to just terminate the rport at the
2851 * first sign of a loss in connectivity. The LLDD may want to
2852 * send ELS traffic to re-validate the login. If the rport is
2853 * immediately deleted, it makes it inappropriate for a node
2854 * container.
2855 * So... we now unconditionally wait dev_loss_tmo before
2856 * destroying an rport.
2857 */
2858
James Smartaedf3492006-04-10 10:14:05 -04002859 rport->port_state = FC_PORTSTATE_BLOCKED;
2860
2861 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2862
2863 spin_unlock_irqrestore(shost->host_lock, flags);
2864
FUJITA Tomonori75252362007-09-01 02:02:27 +09002865 if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
2866 shost->active_mode & MODE_TARGET)
2867 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
2868
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002869 scsi_target_block(&rport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
James Smart0f29b962006-08-18 17:33:29 -04002871 /* see if we need to kill io faster than waiting for device loss */
2872 if ((rport->fast_io_fail_tmo != -1) &&
Mike Christiefff9d402008-08-19 18:45:23 -05002873 (rport->fast_io_fail_tmo < timeout))
James Smart0f29b962006-08-18 17:33:29 -04002874 fc_queue_devloss_work(shost, &rport->fail_io_work,
2875 rport->fast_io_fail_tmo * HZ);
2876
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002877 /* cap the length the devices can be blocked until they are deleted */
James Smartaedf3492006-04-10 10:14:05 -04002878 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
2880EXPORT_SYMBOL(fc_remote_port_delete);
2881
2882/**
Rob Landleyeb448202007-11-03 13:30:39 -05002883 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 * @rport: The remote port that changed.
Rob Landleyeb448202007-11-03 13:30:39 -05002885 * @roles: New roles for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 *
Rob Landleyeb448202007-11-03 13:30:39 -05002887 * Description: The LLDD calls this routine to notify the transport that the
2888 * roles on a remote port may have changed. The largest effect of this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 * if a port now becomes a FCP Target, it must be allocated a
2890 * scsi target id. If the port is no longer a FCP target, any
2891 * scsi target id value assigned to it will persist in case the
2892 * role changes back to include FCP Target. No changes in the scsi
2893 * midlayer will be invoked if the role changes (in the expectation
2894 * that the role will be resumed. If it doesn't normal error processing
2895 * will take place).
2896 *
2897 * Should not be called from interrupt context.
2898 *
2899 * Notes:
2900 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902void
2903fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2904{
2905 struct Scsi_Host *shost = rport_to_shost(rport);
James Smartaedf3492006-04-10 10:14:05 -04002906 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 unsigned long flags;
2908 int create = 0;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002909 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 spin_lock_irqsave(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04002912 if (roles & FC_PORT_ROLE_FCP_TARGET) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002913 if (rport->scsi_target_id == -1) {
2914 rport->scsi_target_id = fc_host->next_target_id++;
2915 create = 1;
James Smarta53eb5e2007-04-27 12:41:09 -04002916 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002917 create = 1;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002918 } else if (shost->active_mode & MODE_TARGET) {
2919 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
2920 (char *)&rport->node_name);
2921 if (ret)
2922 printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
2923 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002926 rport->roles = roles;
2927
James Smartaedf3492006-04-10 10:14:05 -04002928 spin_unlock_irqrestore(shost->host_lock, flags);
2929
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002930 if (create) {
2931 /*
2932 * There may have been a delete timer running on the
2933 * port. Ensure that it is cancelled as we now know
2934 * the port is an FCP Target.
2935 * Note: we know the rport is exists and in an online
2936 * state as the LLDD would not have had an rport
2937 * reference to pass us.
2938 *
2939 * Take no action on the del_timer failure as the state
2940 * machine state change will validate the
2941 * transaction.
2942 */
James Smart0f29b962006-08-18 17:33:29 -04002943 if (!cancel_delayed_work(&rport->fail_io_work))
2944 fc_flush_devloss(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002945 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002946 fc_flush_devloss(shost);
2947
2948 spin_lock_irqsave(shost->host_lock, flags);
Mike Christiefff9d402008-08-19 18:45:23 -05002949 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2950 FC_RPORT_DEVLOSS_PENDING);
James Smartaedf3492006-04-10 10:14:05 -04002951 spin_unlock_irqrestore(shost->host_lock, flags);
2952
2953 /* ensure any stgt delete functions are done */
2954 fc_flush_work(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002957 spin_lock_irqsave(shost->host_lock, flags);
2958 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002960 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04002961 scsi_target_unblock(&rport->dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964EXPORT_SYMBOL(fc_remote_port_rolechg);
2965
2966/**
Rob Landleyeb448202007-11-03 13:30:39 -05002967 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
James Smart92740b22007-04-27 11:53:17 -04002968 * @work: rport target that failed to reappear in the allotted time.
Rob Landleyeb448202007-11-03 13:30:39 -05002969 *
2970 * Description: An attempt to delete a remote port blocks, and if it fails
2971 * to return in the allotted time this gets called.
2972 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973static void
David Howellsc4028952006-11-22 14:57:56 +00002974fc_timeout_deleted_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
David Howellsc4028952006-11-22 14:57:56 +00002976 struct fc_rport *rport =
2977 container_of(work, struct fc_rport, dev_loss_work.work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002978 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart4be98c02009-01-05 12:14:18 -05002979 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04002980 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002981 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05002982 int do_callback = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002984 spin_lock_irqsave(shost->host_lock, flags);
2985
James Smartaedf3492006-04-10 10:14:05 -04002986 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2987
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002988 /*
James Smart92740b22007-04-27 11:53:17 -04002989 * If the port is ONLINE, then it came back. If it was a SCSI
2990 * target, validate it still is. If not, tear down the
2991 * scsi_target on it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002992 */
James Smartaedf3492006-04-10 10:14:05 -04002993 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
James Smart92740b22007-04-27 11:53:17 -04002994 (rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04002995 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002996 dev_printk(KERN_ERR, &rport->dev,
James Smartaedf3492006-04-10 10:14:05 -04002997 "blocked FC remote port time out: no longer"
2998 " a FCP target, removing starget\n");
James Smartaedf3492006-04-10 10:14:05 -04002999 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04003000 scsi_target_unblock(&rport->dev);
3001 fc_queue_work(shost, &rport->stgt_delete_work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003002 return;
3003 }
3004
James Smart92740b22007-04-27 11:53:17 -04003005 /* NOOP state - we're flushing workq's */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003006 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3007 spin_unlock_irqrestore(shost->host_lock, flags);
3008 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003009 "blocked FC remote port time out: leaving"
3010 " rport%s alone\n",
3011 (rport->scsi_target_id != -1) ? " and starget" : "");
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003012 return;
3013 }
3014
James Smart92740b22007-04-27 11:53:17 -04003015 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3016 (rport->scsi_target_id == -1)) {
James Smartaedf3492006-04-10 10:14:05 -04003017 list_del(&rport->peers);
3018 rport->port_state = FC_PORTSTATE_DELETED;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003019 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003020 "blocked FC remote port time out: removing"
3021 " rport%s\n",
3022 (rport->scsi_target_id != -1) ? " and starget" : "");
James Smartaedf3492006-04-10 10:14:05 -04003023 fc_queue_work(shost, &rport->rport_delete_work);
3024 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003025 return;
3026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028 dev_printk(KERN_ERR, &rport->dev,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003029 "blocked FC remote port time out: removing target and "
3030 "saving binding\n");
3031
James Smartaedf3492006-04-10 10:14:05 -04003032 list_move_tail(&rport->peers, &fc_host->rport_bindings);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003033
3034 /*
3035 * Note: We do not remove or clear the hostdata area. This allows
3036 * host-specific target data to persist along with the
3037 * scsi_target_id. It's up to the host to manage it's hostdata area.
3038 */
3039
3040 /*
3041 * Reinitialize port attributes that may change if the port comes back.
3042 */
3043 rport->maxframe_size = -1;
3044 rport->supported_classes = FC_COS_UNSPECIFIED;
James Smarta53eb5e2007-04-27 12:41:09 -04003045 rport->roles = FC_PORT_ROLE_UNKNOWN;
James Smartaedf3492006-04-10 10:14:05 -04003046 rport->port_state = FC_PORTSTATE_NOTPRESENT;
Mike Christiefff9d402008-08-19 18:45:23 -05003047 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003048
James Smartf78badb2008-12-05 16:29:59 -06003049 /*
3050 * Pre-emptively kill I/O rather than waiting for the work queue
3051 * item to teardown the starget. (FCOE libFC folks prefer this
3052 * and to have the rport_port_id still set when it's done).
3053 */
3054 spin_unlock_irqrestore(shost->host_lock, flags);
3055 fc_terminate_rport_io(rport);
3056
Michael Reed8798a692009-10-09 14:15:59 -05003057 spin_lock_irqsave(shost->host_lock, flags);
James Smartf78badb2008-12-05 16:29:59 -06003058
Michael Reed8798a692009-10-09 14:15:59 -05003059 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3060
3061 /* remove the identifiers that aren't used in the consisting binding */
3062 switch (fc_host->tgtid_bind_type) {
3063 case FC_TGTID_BIND_BY_WWPN:
3064 rport->node_name = -1;
3065 rport->port_id = -1;
3066 break;
3067 case FC_TGTID_BIND_BY_WWNN:
3068 rport->port_name = -1;
3069 rport->port_id = -1;
3070 break;
3071 case FC_TGTID_BIND_BY_ID:
3072 rport->node_name = -1;
3073 rport->port_name = -1;
3074 break;
3075 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3076 break;
3077 }
3078
3079 /*
3080 * As this only occurs if the remote port (scsi target)
3081 * went away and didn't come back - we'll remove
3082 * all attached scsi devices.
3083 */
3084 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3085 fc_queue_work(shost, &rport->stgt_delete_work);
3086
3087 do_callback = 1;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003088 }
3089
Michael Reed8798a692009-10-09 14:15:59 -05003090 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05003091
3092 /*
3093 * Notify the driver that the rport is now dead. The LLDD will
3094 * also guarantee that any communication to the rport is terminated
3095 *
3096 * Note: we set the CALLBK_DONE flag above to correspond
3097 */
Michael Reed8798a692009-10-09 14:15:59 -05003098 if (do_callback && i->f->dev_loss_tmo_callbk)
James Smart4be98c02009-01-05 12:14:18 -05003099 i->f->dev_loss_tmo_callbk(rport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
James Smart4be98c02009-01-05 12:14:18 -05003102
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103/**
Rob Landleyeb448202007-11-03 13:30:39 -05003104 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
David Howellsc4028952006-11-22 14:57:56 +00003105 * @work: rport to terminate io on.
James Smart0f29b962006-08-18 17:33:29 -04003106 *
3107 * Notes: Only requests the failure of the io, not that all are flushed
3108 * prior to returning.
Rob Landleyeb448202007-11-03 13:30:39 -05003109 */
James Smart0f29b962006-08-18 17:33:29 -04003110static void
David Howellsc4028952006-11-22 14:57:56 +00003111fc_timeout_fail_rport_io(struct work_struct *work)
James Smart0f29b962006-08-18 17:33:29 -04003112{
David Howellsc4028952006-11-22 14:57:56 +00003113 struct fc_rport *rport =
3114 container_of(work, struct fc_rport, fail_io_work.work);
James Smart0f29b962006-08-18 17:33:29 -04003115
3116 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3117 return;
3118
Mike Christiefff9d402008-08-19 18:45:23 -05003119 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3120 fc_terminate_rport_io(rport);
James Smart0f29b962006-08-18 17:33:29 -04003121}
3122
3123/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
David Howellsc4028952006-11-22 14:57:56 +00003125 * @work: remote port to be scanned.
Rob Landleyeb448202007-11-03 13:30:39 -05003126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127static void
David Howellsc4028952006-11-22 14:57:56 +00003128fc_scsi_scan_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
David Howellsc4028952006-11-22 14:57:56 +00003130 struct fc_rport *rport =
3131 container_of(work, struct fc_rport, scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003132 struct Scsi_Host *shost = rport_to_shost(rport);
Christof Schmitt03f002f2007-08-28 09:31:21 +02003133 struct fc_internal *i = to_fc_internal(shost->transportt);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003134 unsigned long flags;
3135
James Smartaedf3492006-04-10 10:14:05 -04003136 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
Christof Schmitt03f002f2007-08-28 09:31:21 +02003137 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3138 !(i->f->disable_target_scan)) {
James Smartaedf3492006-04-10 10:14:05 -04003139 scsi_scan_target(&rport->dev, rport->channel,
3140 rport->scsi_target_id, SCAN_WILD_CARD, 1);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003141 }
James Smartaedf3492006-04-10 10:14:05 -04003142
3143 spin_lock_irqsave(shost->host_lock, flags);
3144 rport->flags &= ~FC_RPORT_SCAN_PENDING;
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003145 spin_unlock_irqrestore(shost->host_lock, flags);
3146}
3147
Christof Schmitt65d430f2009-10-30 17:59:29 +01003148/**
3149 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3150 * @cmnd: SCSI command that scsi_eh is trying to recover
3151 *
3152 * This routine can be called from a FC LLD scsi_eh callback. It
3153 * blocks the scsi_eh thread until the fc_rport leaves the
3154 * FC_PORTSTATE_BLOCKED. This is necessary to avoid the scsi_eh
3155 * failing recovery actions for blocked rports which would lead to
3156 * offlined SCSI devices.
3157 */
3158void fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3159{
3160 struct Scsi_Host *shost = cmnd->device->host;
3161 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3162 unsigned long flags;
3163
3164 spin_lock_irqsave(shost->host_lock, flags);
3165 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
3166 spin_unlock_irqrestore(shost->host_lock, flags);
3167 msleep(1000);
3168 spin_lock_irqsave(shost->host_lock, flags);
3169 }
3170 spin_unlock_irqrestore(shost->host_lock, flags);
3171}
3172EXPORT_SYMBOL(fc_block_scsi_eh);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003173
James Smarta53eb5e2007-04-27 12:41:09 -04003174/**
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003175 * fc_vport_setup - allocates and creates a FC virtual port.
James Smarta53eb5e2007-04-27 12:41:09 -04003176 * @shost: scsi host the virtual port is connected to.
3177 * @channel: Channel on shost port connected to.
3178 * @pdev: parent device for vport
3179 * @ids: The world wide names, FC4 port roles, etc for
3180 * the virtual port.
3181 * @ret_vport: The pointer to the created vport.
3182 *
3183 * Allocates and creates the vport structure, calls the parent host
3184 * to instantiate the vport, the completes w/ class and sysfs creation.
3185 *
3186 * Notes:
3187 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003188 */
James Smarta53eb5e2007-04-27 12:41:09 -04003189static int
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003190fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
James Smarta53eb5e2007-04-27 12:41:09 -04003191 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3192{
3193 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3194 struct fc_internal *fci = to_fc_internal(shost->transportt);
3195 struct fc_vport *vport;
3196 struct device *dev;
3197 unsigned long flags;
3198 size_t size;
3199 int error;
3200
3201 *ret_vport = NULL;
3202
3203 if ( ! fci->f->vport_create)
3204 return -ENOENT;
3205
3206 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3207 vport = kzalloc(size, GFP_KERNEL);
3208 if (unlikely(!vport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003209 printk(KERN_ERR "%s: allocation failure\n", __func__);
James Smarta53eb5e2007-04-27 12:41:09 -04003210 return -ENOMEM;
3211 }
3212
3213 vport->vport_state = FC_VPORT_UNKNOWN;
3214 vport->vport_last_state = FC_VPORT_UNKNOWN;
3215 vport->node_name = ids->node_name;
3216 vport->port_name = ids->port_name;
3217 vport->roles = ids->roles;
3218 vport->vport_type = ids->vport_type;
3219 if (fci->f->dd_fcvport_size)
3220 vport->dd_data = &vport[1];
3221 vport->shost = shost;
3222 vport->channel = channel;
3223 vport->flags = FC_VPORT_CREATING;
James Smart9ef3e4a2007-05-24 19:04:44 -04003224 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
James Smarta53eb5e2007-04-27 12:41:09 -04003225
3226 spin_lock_irqsave(shost->host_lock, flags);
3227
3228 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3229 spin_unlock_irqrestore(shost->host_lock, flags);
3230 kfree(vport);
3231 return -ENOSPC;
3232 }
3233 fc_host->npiv_vports_inuse++;
3234 vport->number = fc_host->next_vport_number++;
3235 list_add_tail(&vport->peers, &fc_host->vports);
3236 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3237
3238 spin_unlock_irqrestore(shost->host_lock, flags);
3239
3240 dev = &vport->dev;
3241 device_initialize(dev); /* takes self reference */
3242 dev->parent = get_device(pdev); /* takes parent reference */
3243 dev->release = fc_vport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01003244 dev_set_name(dev, "vport-%d:%d-%d",
3245 shost->host_no, channel, vport->number);
James Smarta53eb5e2007-04-27 12:41:09 -04003246 transport_setup_device(dev);
3247
3248 error = device_add(dev);
3249 if (error) {
3250 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3251 goto delete_vport;
3252 }
3253 transport_add_device(dev);
3254 transport_configure_device(dev);
3255
3256 error = fci->f->vport_create(vport, ids->disable);
3257 if (error) {
3258 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3259 goto delete_vport_all;
3260 }
3261
3262 /*
3263 * if the parent isn't the physical adapter's Scsi_Host, ensure
3264 * the Scsi_Host at least contains ia symlink to the vport.
3265 */
3266 if (pdev != &shost->shost_gendev) {
3267 error = sysfs_create_link(&shost->shost_gendev.kobj,
Kay Sievers71610f52008-12-03 22:41:36 +01003268 &dev->kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003269 if (error)
3270 printk(KERN_ERR
3271 "%s: Cannot create vport symlinks for "
3272 "%s, err=%d\n",
Kay Sievers71610f52008-12-03 22:41:36 +01003273 __func__, dev_name(dev), error);
James Smarta53eb5e2007-04-27 12:41:09 -04003274 }
3275 spin_lock_irqsave(shost->host_lock, flags);
3276 vport->flags &= ~FC_VPORT_CREATING;
3277 spin_unlock_irqrestore(shost->host_lock, flags);
3278
3279 dev_printk(KERN_NOTICE, pdev,
Kay Sievers71610f52008-12-03 22:41:36 +01003280 "%s created via shost%d channel %d\n", dev_name(dev),
James Smarta53eb5e2007-04-27 12:41:09 -04003281 shost->host_no, channel);
3282
3283 *ret_vport = vport;
3284
3285 return 0;
3286
3287delete_vport_all:
3288 transport_remove_device(dev);
3289 device_del(dev);
3290delete_vport:
3291 transport_destroy_device(dev);
3292 spin_lock_irqsave(shost->host_lock, flags);
3293 list_del(&vport->peers);
3294 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3295 fc_host->npiv_vports_inuse--;
3296 spin_unlock_irqrestore(shost->host_lock, flags);
3297 put_device(dev->parent);
3298 kfree(vport);
3299
3300 return error;
3301}
3302
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003303/**
3304 * fc_vport_create - Admin App or LLDD requests creation of a vport
3305 * @shost: scsi host the virtual port is connected to.
3306 * @channel: channel on shost port connected to.
3307 * @ids: The world wide names, FC4 port roles, etc for
3308 * the virtual port.
3309 *
3310 * Notes:
3311 * This routine assumes no locks are held on entry.
3312 */
3313struct fc_vport *
3314fc_vport_create(struct Scsi_Host *shost, int channel,
3315 struct fc_vport_identifiers *ids)
3316{
3317 int stat;
3318 struct fc_vport *vport;
3319
3320 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3321 ids, &vport);
3322 return stat ? NULL : vport;
3323}
3324EXPORT_SYMBOL(fc_vport_create);
James Smarta53eb5e2007-04-27 12:41:09 -04003325
3326/**
3327 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3328 * @vport: fc_vport to be terminated
3329 *
3330 * Calls the LLDD vport_delete() function, then deallocates and removes
3331 * the vport from the shost and object tree.
3332 *
3333 * Notes:
3334 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003335 */
James Smarta53eb5e2007-04-27 12:41:09 -04003336int
3337fc_vport_terminate(struct fc_vport *vport)
3338{
3339 struct Scsi_Host *shost = vport_to_shost(vport);
3340 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3341 struct fc_internal *i = to_fc_internal(shost->transportt);
3342 struct device *dev = &vport->dev;
3343 unsigned long flags;
3344 int stat;
3345
3346 spin_lock_irqsave(shost->host_lock, flags);
3347 if (vport->flags & FC_VPORT_CREATING) {
3348 spin_unlock_irqrestore(shost->host_lock, flags);
3349 return -EBUSY;
3350 }
3351 if (vport->flags & (FC_VPORT_DEL)) {
3352 spin_unlock_irqrestore(shost->host_lock, flags);
3353 return -EALREADY;
3354 }
3355 vport->flags |= FC_VPORT_DELETING;
3356 spin_unlock_irqrestore(shost->host_lock, flags);
3357
3358 if (i->f->vport_delete)
3359 stat = i->f->vport_delete(vport);
3360 else
3361 stat = -ENOENT;
3362
3363 spin_lock_irqsave(shost->host_lock, flags);
3364 vport->flags &= ~FC_VPORT_DELETING;
3365 if (!stat) {
3366 vport->flags |= FC_VPORT_DELETED;
3367 list_del(&vport->peers);
3368 fc_host->npiv_vports_inuse--;
3369 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3370 }
3371 spin_unlock_irqrestore(shost->host_lock, flags);
3372
3373 if (stat)
3374 return stat;
3375
3376 if (dev->parent != &shost->shost_gendev)
Kay Sievers71610f52008-12-03 22:41:36 +01003377 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003378 transport_remove_device(dev);
3379 device_del(dev);
3380 transport_destroy_device(dev);
3381
3382 /*
3383 * Removing our self-reference should mean our
3384 * release function gets called, which will drop the remaining
3385 * parent reference and free the data structure.
3386 */
3387 put_device(dev); /* for self-reference */
3388
3389 return 0; /* SUCCESS */
3390}
3391EXPORT_SYMBOL(fc_vport_terminate);
3392
James Smart9ef3e4a2007-05-24 19:04:44 -04003393/**
3394 * fc_vport_sched_delete - workq-based delete request for a vport
James Smart9ef3e4a2007-05-24 19:04:44 -04003395 * @work: vport to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05003396 */
James Smart9ef3e4a2007-05-24 19:04:44 -04003397static void
3398fc_vport_sched_delete(struct work_struct *work)
3399{
3400 struct fc_vport *vport =
3401 container_of(work, struct fc_vport, vport_delete_work);
3402 int stat;
James Smarta53eb5e2007-04-27 12:41:09 -04003403
James Smart9ef3e4a2007-05-24 19:04:44 -04003404 stat = fc_vport_terminate(vport);
3405 if (stat)
3406 dev_printk(KERN_ERR, vport->dev.parent,
3407 "%s: %s could not be deleted created via "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003408 "shost%d channel %d - error %d\n", __func__,
Kay Sievers71610f52008-12-03 22:41:36 +01003409 dev_name(&vport->dev), vport->shost->host_no,
James Smart9ef3e4a2007-05-24 19:04:44 -04003410 vport->channel, stat);
3411}
3412
3413
James Smart9e4f5e22009-03-26 13:33:19 -04003414/*
3415 * BSG support
3416 */
3417
3418
3419/**
3420 * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
3421 * @job: fc_bsg_job that is to be torn down
3422 */
3423static void
3424fc_destroy_bsgjob(struct fc_bsg_job *job)
3425{
3426 unsigned long flags;
3427
3428 spin_lock_irqsave(&job->job_lock, flags);
3429 if (job->ref_cnt) {
3430 spin_unlock_irqrestore(&job->job_lock, flags);
3431 return;
3432 }
3433 spin_unlock_irqrestore(&job->job_lock, flags);
3434
3435 put_device(job->dev); /* release reference for the request */
3436
3437 kfree(job->request_payload.sg_list);
3438 kfree(job->reply_payload.sg_list);
3439 kfree(job);
3440}
3441
James Smart9e4f5e22009-03-26 13:33:19 -04003442/**
3443 * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
3444 * completed
3445 * @job: fc_bsg_job that is complete
3446 */
3447static void
3448fc_bsg_jobdone(struct fc_bsg_job *job)
3449{
3450 struct request *req = job->req;
3451 struct request *rsp = req->next_rq;
James Smart9e4f5e22009-03-26 13:33:19 -04003452 int err;
3453
James Smart9e4f5e22009-03-26 13:33:19 -04003454 err = job->req->errors = job->reply->result;
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003455
James Smart9e4f5e22009-03-26 13:33:19 -04003456 if (err < 0)
3457 /* we're only returning the result field in the reply */
3458 job->req->sense_len = sizeof(uint32_t);
3459 else
3460 job->req->sense_len = job->reply_len;
3461
3462 /* we assume all request payload was transferred, residual == 0 */
3463 req->resid_len = 0;
3464
3465 if (rsp) {
3466 WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
3467
3468 /* set reply (bidi) residual */
3469 rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
3470 rsp->resid_len);
3471 }
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003472 blk_complete_request(req);
James Smart9e4f5e22009-03-26 13:33:19 -04003473}
3474
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003475/**
3476 * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
Randy Dunlapfe5d20c2009-07-04 13:10:41 -07003477 * @rq: BSG request that holds the job to be destroyed
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003478 */
3479static void fc_bsg_softirq_done(struct request *rq)
3480{
3481 struct fc_bsg_job *job = rq->special;
3482 unsigned long flags;
3483
3484 spin_lock_irqsave(&job->job_lock, flags);
3485 job->state_flags |= FC_RQST_STATE_DONE;
3486 job->ref_cnt--;
3487 spin_unlock_irqrestore(&job->job_lock, flags);
3488
3489 blk_end_request_all(rq, rq->errors);
3490 fc_destroy_bsgjob(job);
3491}
James Smart9e4f5e22009-03-26 13:33:19 -04003492
3493/**
3494 * fc_bsg_job_timeout - handler for when a bsg request timesout
3495 * @req: request that timed out
3496 */
3497static enum blk_eh_timer_return
3498fc_bsg_job_timeout(struct request *req)
3499{
3500 struct fc_bsg_job *job = (void *) req->special;
3501 struct Scsi_Host *shost = job->shost;
3502 struct fc_internal *i = to_fc_internal(shost->transportt);
3503 unsigned long flags;
3504 int err = 0, done = 0;
3505
3506 if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
3507 return BLK_EH_RESET_TIMER;
3508
3509 spin_lock_irqsave(&job->job_lock, flags);
3510 if (job->state_flags & FC_RQST_STATE_DONE)
3511 done = 1;
3512 else
3513 job->ref_cnt++;
3514 spin_unlock_irqrestore(&job->job_lock, flags);
3515
3516 if (!done && i->f->bsg_timeout) {
3517 /* call LLDD to abort the i/o as it has timed out */
3518 err = i->f->bsg_timeout(job);
3519 if (err)
3520 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3521 "abort failed with status %d\n", err);
3522 }
3523
James Smart9e4f5e22009-03-26 13:33:19 -04003524 /* the blk_end_sync_io() doesn't check the error */
Giridhar Malavali47e7e892009-06-19 16:26:54 -07003525 if (done)
3526 return BLK_EH_NOT_HANDLED;
3527 else
3528 return BLK_EH_HANDLED;
James Smart9e4f5e22009-03-26 13:33:19 -04003529}
3530
James Smart9e4f5e22009-03-26 13:33:19 -04003531static int
3532fc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
3533{
3534 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
3535
3536 BUG_ON(!req->nr_phys_segments);
3537
3538 buf->sg_list = kzalloc(sz, GFP_KERNEL);
3539 if (!buf->sg_list)
3540 return -ENOMEM;
3541 sg_init_table(buf->sg_list, req->nr_phys_segments);
3542 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
3543 buf->payload_len = blk_rq_bytes(req);
3544 return 0;
3545}
3546
3547
3548/**
3549 * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
3550 * bsg request
3551 * @shost: SCSI Host corresponding to the bsg object
3552 * @rport: (optional) FC Remote Port corresponding to the bsg object
3553 * @req: BSG request that needs a job structure
3554 */
3555static int
3556fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
3557 struct request *req)
3558{
3559 struct fc_internal *i = to_fc_internal(shost->transportt);
3560 struct request *rsp = req->next_rq;
3561 struct fc_bsg_job *job;
3562 int ret;
3563
3564 BUG_ON(req->special);
3565
3566 job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
3567 GFP_KERNEL);
3568 if (!job)
3569 return -ENOMEM;
3570
3571 /*
3572 * Note: this is a bit silly.
3573 * The request gets formatted as a SGIO v4 ioctl request, which
3574 * then gets reformatted as a blk request, which then gets
3575 * reformatted as a fc bsg request. And on completion, we have
3576 * to wrap return results such that SGIO v4 thinks it was a scsi
3577 * status. I hope this was all worth it.
3578 */
3579
3580 req->special = job;
3581 job->shost = shost;
3582 job->rport = rport;
3583 job->req = req;
3584 if (i->f->dd_bsg_size)
3585 job->dd_data = (void *)&job[1];
3586 spin_lock_init(&job->job_lock);
3587 job->request = (struct fc_bsg_request *)req->cmd;
3588 job->request_len = req->cmd_len;
3589 job->reply = req->sense;
3590 job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
3591 * allocated */
3592 if (req->bio) {
3593 ret = fc_bsg_map_buffer(&job->request_payload, req);
3594 if (ret)
3595 goto failjob_rls_job;
3596 }
3597 if (rsp && rsp->bio) {
3598 ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
3599 if (ret)
3600 goto failjob_rls_rqst_payload;
3601 }
3602 job->job_done = fc_bsg_jobdone;
3603 if (rport)
3604 job->dev = &rport->dev;
3605 else
3606 job->dev = &shost->shost_gendev;
3607 get_device(job->dev); /* take a reference for the request */
3608
3609 job->ref_cnt = 1;
3610
3611 return 0;
3612
3613
3614failjob_rls_rqst_payload:
3615 kfree(job->request_payload.sg_list);
3616failjob_rls_job:
3617 kfree(job);
3618 return -ENOMEM;
3619}
3620
3621
3622enum fc_dispatch_result {
3623 FC_DISPATCH_BREAK, /* on return, q is locked, break from q loop */
3624 FC_DISPATCH_LOCKED, /* on return, q is locked, continue on */
3625 FC_DISPATCH_UNLOCKED, /* on return, q is unlocked, continue on */
3626};
3627
3628
3629/**
3630 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
Randy Dunlap54159072009-06-18 19:39:57 -07003631 * @q: fc host request queue
James Smart9e4f5e22009-03-26 13:33:19 -04003632 * @shost: scsi host rport attached to
3633 * @job: bsg job to be processed
3634 */
3635static enum fc_dispatch_result
3636fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3637 struct fc_bsg_job *job)
3638{
3639 struct fc_internal *i = to_fc_internal(shost->transportt);
3640 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3641 int ret;
3642
3643 /* Validate the host command */
3644 switch (job->request->msgcode) {
3645 case FC_BSG_HST_ADD_RPORT:
3646 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3647 break;
3648
3649 case FC_BSG_HST_DEL_RPORT:
3650 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3651 break;
3652
3653 case FC_BSG_HST_ELS_NOLOGIN:
3654 cmdlen += sizeof(struct fc_bsg_host_els);
3655 /* there better be a xmt and rcv payloads */
3656 if ((!job->request_payload.payload_len) ||
3657 (!job->reply_payload.payload_len)) {
3658 ret = -EINVAL;
3659 goto fail_host_msg;
3660 }
3661 break;
3662
3663 case FC_BSG_HST_CT:
3664 cmdlen += sizeof(struct fc_bsg_host_ct);
3665 /* there better be xmt and rcv payloads */
3666 if ((!job->request_payload.payload_len) ||
3667 (!job->reply_payload.payload_len)) {
3668 ret = -EINVAL;
3669 goto fail_host_msg;
3670 }
3671 break;
3672
3673 case FC_BSG_HST_VENDOR:
3674 cmdlen += sizeof(struct fc_bsg_host_vendor);
3675 if ((shost->hostt->vendor_id == 0L) ||
3676 (job->request->rqst_data.h_vendor.vendor_id !=
3677 shost->hostt->vendor_id)) {
3678 ret = -ESRCH;
3679 goto fail_host_msg;
3680 }
3681 break;
3682
3683 default:
3684 ret = -EBADR;
3685 goto fail_host_msg;
3686 }
3687
3688 /* check if we really have all the request data needed */
3689 if (job->request_len < cmdlen) {
3690 ret = -ENOMSG;
3691 goto fail_host_msg;
3692 }
3693
3694 ret = i->f->bsg_request(job);
3695 if (!ret)
3696 return FC_DISPATCH_UNLOCKED;
3697
3698fail_host_msg:
3699 /* return the errno failure code as the only status */
3700 BUG_ON(job->reply_len < sizeof(uint32_t));
3701 job->reply->result = ret;
3702 job->reply_len = sizeof(uint32_t);
3703 fc_bsg_jobdone(job);
3704 return FC_DISPATCH_UNLOCKED;
3705}
3706
3707
3708/*
3709 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3710 * @rport: rport to be restarted
3711 */
3712static void
3713fc_bsg_goose_queue(struct fc_rport *rport)
3714{
3715 int flagset;
Christof Schmitt39562e72009-06-26 16:30:43 +02003716 unsigned long flags;
James Smart9e4f5e22009-03-26 13:33:19 -04003717
3718 if (!rport->rqst_q)
3719 return;
3720
3721 get_device(&rport->dev);
3722
Christof Schmitt39562e72009-06-26 16:30:43 +02003723 spin_lock_irqsave(rport->rqst_q->queue_lock, flags);
James Smart9e4f5e22009-03-26 13:33:19 -04003724 flagset = test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags) &&
3725 !test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags);
3726 if (flagset)
3727 queue_flag_set(QUEUE_FLAG_REENTER, rport->rqst_q);
3728 __blk_run_queue(rport->rqst_q);
3729 if (flagset)
3730 queue_flag_clear(QUEUE_FLAG_REENTER, rport->rqst_q);
Christof Schmitt39562e72009-06-26 16:30:43 +02003731 spin_unlock_irqrestore(rport->rqst_q->queue_lock, flags);
James Smart9e4f5e22009-03-26 13:33:19 -04003732
3733 put_device(&rport->dev);
3734}
3735
3736
3737/**
3738 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
Randy Dunlap54159072009-06-18 19:39:57 -07003739 * @q: rport request queue
James Smart9e4f5e22009-03-26 13:33:19 -04003740 * @shost: scsi host rport attached to
3741 * @rport: rport request destined to
3742 * @job: bsg job to be processed
3743 */
3744static enum fc_dispatch_result
3745fc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3746 struct fc_rport *rport, struct fc_bsg_job *job)
3747{
3748 struct fc_internal *i = to_fc_internal(shost->transportt);
3749 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3750 int ret;
3751
3752 /* Validate the rport command */
3753 switch (job->request->msgcode) {
3754 case FC_BSG_RPT_ELS:
3755 cmdlen += sizeof(struct fc_bsg_rport_els);
3756 goto check_bidi;
3757
3758 case FC_BSG_RPT_CT:
3759 cmdlen += sizeof(struct fc_bsg_rport_ct);
3760check_bidi:
3761 /* there better be xmt and rcv payloads */
3762 if ((!job->request_payload.payload_len) ||
3763 (!job->reply_payload.payload_len)) {
3764 ret = -EINVAL;
3765 goto fail_rport_msg;
3766 }
3767 break;
3768 default:
3769 ret = -EBADR;
3770 goto fail_rport_msg;
3771 }
3772
3773 /* check if we really have all the request data needed */
3774 if (job->request_len < cmdlen) {
3775 ret = -ENOMSG;
3776 goto fail_rport_msg;
3777 }
3778
3779 ret = i->f->bsg_request(job);
3780 if (!ret)
3781 return FC_DISPATCH_UNLOCKED;
3782
3783fail_rport_msg:
3784 /* return the errno failure code as the only status */
3785 BUG_ON(job->reply_len < sizeof(uint32_t));
3786 job->reply->result = ret;
3787 job->reply_len = sizeof(uint32_t);
3788 fc_bsg_jobdone(job);
3789 return FC_DISPATCH_UNLOCKED;
3790}
3791
3792
3793/**
3794 * fc_bsg_request_handler - generic handler for bsg requests
3795 * @q: request queue to manage
3796 * @shost: Scsi_Host related to the bsg object
3797 * @rport: FC remote port related to the bsg object (optional)
3798 * @dev: device structure for bsg object
3799 */
3800static void
3801fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
3802 struct fc_rport *rport, struct device *dev)
3803{
3804 struct request *req;
3805 struct fc_bsg_job *job;
3806 enum fc_dispatch_result ret;
3807
3808 if (!get_device(dev))
3809 return;
3810
3811 while (!blk_queue_plugged(q)) {
3812 if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED))
3813 break;
3814
3815 req = blk_fetch_request(q);
3816 if (!req)
3817 break;
3818
3819 if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
3820 req->errors = -ENXIO;
3821 spin_unlock_irq(q->queue_lock);
3822 blk_end_request(req, -ENXIO, blk_rq_bytes(req));
3823 spin_lock_irq(q->queue_lock);
3824 continue;
3825 }
3826
3827 spin_unlock_irq(q->queue_lock);
3828
3829 ret = fc_req_to_bsgjob(shost, rport, req);
3830 if (ret) {
3831 req->errors = ret;
3832 blk_end_request(req, ret, blk_rq_bytes(req));
3833 spin_lock_irq(q->queue_lock);
3834 continue;
3835 }
3836
3837 job = req->special;
3838
3839 /* check if we have the msgcode value at least */
3840 if (job->request_len < sizeof(uint32_t)) {
3841 BUG_ON(job->reply_len < sizeof(uint32_t));
3842 job->reply->result = -ENOMSG;
3843 job->reply_len = sizeof(uint32_t);
3844 fc_bsg_jobdone(job);
3845 spin_lock_irq(q->queue_lock);
3846 continue;
3847 }
3848
3849 /* the dispatch routines will unlock the queue_lock */
3850 if (rport)
3851 ret = fc_bsg_rport_dispatch(q, shost, rport, job);
3852 else
3853 ret = fc_bsg_host_dispatch(q, shost, job);
3854
3855 /* did dispatcher hit state that can't process any more */
3856 if (ret == FC_DISPATCH_BREAK)
3857 break;
3858
3859 /* did dispatcher had released the lock */
3860 if (ret == FC_DISPATCH_UNLOCKED)
3861 spin_lock_irq(q->queue_lock);
3862 }
3863
3864 spin_unlock_irq(q->queue_lock);
3865 put_device(dev);
3866 spin_lock_irq(q->queue_lock);
3867}
3868
3869
3870/**
3871 * fc_bsg_host_handler - handler for bsg requests for a fc host
3872 * @q: fc host request queue
3873 */
3874static void
3875fc_bsg_host_handler(struct request_queue *q)
3876{
3877 struct Scsi_Host *shost = q->queuedata;
3878
3879 fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
3880}
3881
3882
3883/**
3884 * fc_bsg_rport_handler - handler for bsg requests for a fc rport
3885 * @q: rport request queue
3886 */
3887static void
3888fc_bsg_rport_handler(struct request_queue *q)
3889{
3890 struct fc_rport *rport = q->queuedata;
3891 struct Scsi_Host *shost = rport_to_shost(rport);
3892
3893 fc_bsg_request_handler(q, shost, rport, &rport->dev);
3894}
3895
3896
3897/**
3898 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3899 * @shost: shost for fc_host
3900 * @fc_host: fc_host adding the structures to
3901 */
3902static int
3903fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
3904{
3905 struct device *dev = &shost->shost_gendev;
3906 struct fc_internal *i = to_fc_internal(shost->transportt);
3907 struct request_queue *q;
3908 int err;
James Bottomley3c559ea2009-06-21 12:11:43 -05003909 char bsg_name[20];
James Smart9e4f5e22009-03-26 13:33:19 -04003910
3911 fc_host->rqst_q = NULL;
3912
3913 if (!i->f->bsg_request)
3914 return -ENOTSUPP;
3915
3916 snprintf(bsg_name, sizeof(bsg_name),
3917 "fc_host%d", shost->host_no);
3918
3919 q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
3920 if (!q) {
3921 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3922 "initialize - no request queue\n",
3923 shost->host_no);
3924 return -ENOMEM;
3925 }
3926
3927 q->queuedata = shost;
3928 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003929 blk_queue_softirq_done(q, fc_bsg_softirq_done);
James Smart9e4f5e22009-03-26 13:33:19 -04003930 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
3931 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
3932
3933 err = bsg_register_queue(q, dev, bsg_name, NULL);
3934 if (err) {
3935 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3936 "initialize - register queue\n",
3937 shost->host_no);
3938 blk_cleanup_queue(q);
3939 return err;
3940 }
3941
3942 fc_host->rqst_q = q;
3943 return 0;
3944}
3945
3946
3947/**
3948 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3949 * @shost: shost that rport is attached to
3950 * @rport: rport that the bsg hooks are being attached to
3951 */
3952static int
3953fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
3954{
3955 struct device *dev = &rport->dev;
3956 struct fc_internal *i = to_fc_internal(shost->transportt);
3957 struct request_queue *q;
3958 int err;
3959
3960 rport->rqst_q = NULL;
3961
3962 if (!i->f->bsg_request)
3963 return -ENOTSUPP;
3964
3965 q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
3966 if (!q) {
3967 printk(KERN_ERR "%s: bsg interface failed to "
3968 "initialize - no request queue\n",
3969 dev->kobj.name);
3970 return -ENOMEM;
3971 }
3972
3973 q->queuedata = rport;
3974 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003975 blk_queue_softirq_done(q, fc_bsg_softirq_done);
James Smart9e4f5e22009-03-26 13:33:19 -04003976 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
3977 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
3978
3979 err = bsg_register_queue(q, dev, NULL, NULL);
3980 if (err) {
3981 printk(KERN_ERR "%s: bsg interface failed to "
3982 "initialize - register queue\n",
3983 dev->kobj.name);
3984 blk_cleanup_queue(q);
3985 return err;
3986 }
3987
3988 rport->rqst_q = q;
3989 return 0;
3990}
3991
3992
3993/**
3994 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
3995 * @q: the request_queue that is to be torn down.
3996 */
3997static void
3998fc_bsg_remove(struct request_queue *q)
3999{
4000 if (q) {
4001 bsg_unregister_queue(q);
4002 blk_cleanup_queue(q);
4003 }
4004}
4005
4006
James Smart9ef3e4a2007-05-24 19:04:44 -04004007/* Original Author: Martin Hicks */
4008MODULE_AUTHOR("James Smart");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009MODULE_DESCRIPTION("FC Transport Attributes");
4010MODULE_LICENSE("GPL");
4011
4012module_init(fc_transport_init);
4013module_exit(fc_transport_exit);