blob: 6cfffc88022a317490a91e34704b3d3bbbddffd8 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Christof Schmitt65d430f2009-10-30 17:59:29 +010031#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <scsi/scsi_device.h>
33#include <scsi/scsi_host.h>
34#include <scsi/scsi_transport.h>
35#include <scsi/scsi_transport_fc.h>
James Smartc829c392006-03-13 08:28:57 -050036#include <scsi/scsi_cmnd.h>
James Smart84314fd2006-08-18 17:30:09 -040037#include <linux/netlink.h>
38#include <net/netlink.h>
39#include <scsi/scsi_netlink_fc.h>
James Smart9e4f5e22009-03-26 13:33:19 -040040#include <scsi/scsi_bsg_fc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "scsi_priv.h"
FUJITA Tomonori75252362007-09-01 02:02:27 +090042#include "scsi_transport_fc_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
James Smartaedf3492006-04-10 10:14:05 -040044static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
James Smart9ef3e4a2007-05-24 19:04:44 -040045static void fc_vport_sched_delete(struct work_struct *work);
Andrew Vasqueza30c3f62008-07-18 08:32:52 -070046static int fc_vport_setup(struct Scsi_Host *shost, int channel,
James Smarta53eb5e2007-04-27 12:41:09 -040047 struct device *pdev, struct fc_vport_identifiers *ids,
48 struct fc_vport **vport);
James Smart9e4f5e22009-03-26 13:33:19 -040049static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
50static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
51static void fc_bsg_remove(struct request_queue *);
52static void fc_bsg_goose_queue(struct fc_rport *);
James Smarta53eb5e2007-04-27 12:41:09 -040053
54/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * Redefine so that we can have same named attributes in the
56 * sdev/starget/host objects.
57 */
Tony Jonesee959b02008-02-22 00:13:36 +010058#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
59struct device_attribute device_attr_##_prefix##_##_name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 __ATTR(_name,_mode,_show,_store)
61
62#define fc_enum_name_search(title, table_type, table) \
63static const char *get_fc_##title##_name(enum table_type table_key) \
64{ \
65 int i; \
66 char *name = NULL; \
67 \
Tobias Klauser6391a112006-06-08 22:23:48 -070068 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (table[i].value == table_key) { \
70 name = table[i].name; \
71 break; \
72 } \
73 } \
74 return name; \
75}
76
77#define fc_enum_name_match(title, table_type, table) \
78static int get_fc_##title##_match(const char *table_key, \
79 enum table_type *value) \
80{ \
81 int i; \
82 \
Tobias Klauser6391a112006-06-08 22:23:48 -070083 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (strncmp(table_key, table[i].name, \
85 table[i].matchlen) == 0) { \
86 *value = table[i].value; \
87 return 0; /* success */ \
88 } \
89 } \
90 return 1; /* failure */ \
91}
92
93
94/* Convert fc_port_type values to ascii string name */
95static struct {
96 enum fc_port_type value;
97 char *name;
98} fc_port_type_names[] = {
99 { FC_PORTTYPE_UNKNOWN, "Unknown" },
100 { FC_PORTTYPE_OTHER, "Other" },
101 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
102 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
103 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
104 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
Christof Schmitt951948a2009-01-15 16:51:48 +0100105 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
James Smarta53eb5e2007-04-27 12:41:09 -0400106 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
109#define FC_PORTTYPE_MAX_NAMELEN 50
110
James Smarta53eb5e2007-04-27 12:41:09 -0400111/* Reuse fc_port_type enum function for vport_type */
112#define get_fc_vport_type_name get_fc_port_type_name
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
James Smart84314fd2006-08-18 17:30:09 -0400115/* Convert fc_host_event_code values to ascii string name */
116static const struct {
117 enum fc_host_event_code value;
118 char *name;
119} fc_host_event_code_names[] = {
120 { FCH_EVT_LIP, "lip" },
121 { FCH_EVT_LINKUP, "link_up" },
122 { FCH_EVT_LINKDOWN, "link_down" },
123 { FCH_EVT_LIPRESET, "lip_reset" },
124 { FCH_EVT_RSCN, "rscn" },
125 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
126 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
127 { FCH_EVT_PORT_ONLINE, "port_online" },
128 { FCH_EVT_PORT_OFFLINE, "port_offline" },
129 { FCH_EVT_PORT_FABRIC, "port_fabric" },
130 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
131 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
132};
133fc_enum_name_search(host_event_code, fc_host_event_code,
134 fc_host_event_code_names)
135#define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
136
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Convert fc_port_state values to ascii string name */
139static struct {
140 enum fc_port_state value;
141 char *name;
142} fc_port_state_names[] = {
143 { FC_PORTSTATE_UNKNOWN, "Unknown" },
144 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
145 { FC_PORTSTATE_ONLINE, "Online" },
146 { FC_PORTSTATE_OFFLINE, "Offline" },
147 { FC_PORTSTATE_BLOCKED, "Blocked" },
148 { FC_PORTSTATE_BYPASSED, "Bypassed" },
149 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
150 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
151 { FC_PORTSTATE_ERROR, "Error" },
152 { FC_PORTSTATE_LOOPBACK, "Loopback" },
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500153 { FC_PORTSTATE_DELETED, "Deleted" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
156#define FC_PORTSTATE_MAX_NAMELEN 20
157
158
James Smarta53eb5e2007-04-27 12:41:09 -0400159/* Convert fc_vport_state values to ascii string name */
160static struct {
161 enum fc_vport_state value;
162 char *name;
163} fc_vport_state_names[] = {
164 { FC_VPORT_UNKNOWN, "Unknown" },
165 { FC_VPORT_ACTIVE, "Active" },
166 { FC_VPORT_DISABLED, "Disabled" },
167 { FC_VPORT_LINKDOWN, "Linkdown" },
168 { FC_VPORT_INITIALIZING, "Initializing" },
169 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
170 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
171 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
172 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
173 { FC_VPORT_FAILED, "VPort Failed" },
174};
175fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
176#define FC_VPORTSTATE_MAX_NAMELEN 24
177
178/* Reuse fc_vport_state enum function for vport_last_state */
179#define get_fc_vport_last_state_name get_fc_vport_state_name
180
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* Convert fc_tgtid_binding_type values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100183static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 enum fc_tgtid_binding_type value;
185 char *name;
186 int matchlen;
187} fc_tgtid_binding_type_names[] = {
188 { FC_TGTID_BIND_NONE, "none", 4 },
189 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
190 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
191 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
192};
193fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
194 fc_tgtid_binding_type_names)
195fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
196 fc_tgtid_binding_type_names)
197#define FC_BINDTYPE_MAX_NAMELEN 30
198
199
200#define fc_bitfield_name_search(title, table) \
201static ssize_t \
202get_fc_##title##_names(u32 table_key, char *buf) \
203{ \
204 char *prefix = ""; \
205 ssize_t len = 0; \
206 int i; \
207 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700208 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (table[i].value & table_key) { \
210 len += sprintf(buf + len, "%s%s", \
211 prefix, table[i].name); \
212 prefix = ", "; \
213 } \
214 } \
215 len += sprintf(buf + len, "\n"); \
216 return len; \
217}
218
219
220/* Convert FC_COS bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100221static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 u32 value;
223 char *name;
224} fc_cos_names[] = {
225 { FC_COS_CLASS1, "Class 1" },
226 { FC_COS_CLASS2, "Class 2" },
227 { FC_COS_CLASS3, "Class 3" },
228 { FC_COS_CLASS4, "Class 4" },
229 { FC_COS_CLASS6, "Class 6" },
230};
231fc_bitfield_name_search(cos, fc_cos_names)
232
233
234/* Convert FC_PORTSPEED bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100235static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 u32 value;
237 char *name;
238} fc_port_speed_names[] = {
239 { FC_PORTSPEED_1GBIT, "1 Gbit" },
240 { FC_PORTSPEED_2GBIT, "2 Gbit" },
241 { FC_PORTSPEED_4GBIT, "4 Gbit" },
242 { FC_PORTSPEED_10GBIT, "10 Gbit" },
James Smartc3d23502007-03-12 14:16:35 -0500243 { FC_PORTSPEED_8GBIT, "8 Gbit" },
244 { FC_PORTSPEED_16GBIT, "16 Gbit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
246};
247fc_bitfield_name_search(port_speed, fc_port_speed_names)
248
249
250static int
251show_fc_fc4s (char *buf, u8 *fc4_list)
252{
253 int i, len=0;
254
255 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
256 len += sprintf(buf + len , "0x%02x ", *fc4_list);
257 len += sprintf(buf + len, "\n");
258 return len;
259}
260
261
James Smarta53eb5e2007-04-27 12:41:09 -0400262/* Convert FC_PORT_ROLE bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100263static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 u32 value;
265 char *name;
James Smarta53eb5e2007-04-27 12:41:09 -0400266} fc_port_role_names[] = {
267 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
268 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
269 { FC_PORT_ROLE_IP_PORT, "IP Port" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270};
James Smarta53eb5e2007-04-27 12:41:09 -0400271fc_bitfield_name_search(port_roles, fc_port_role_names)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/*
274 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
275 */
276#define FC_WELLKNOWN_PORTID_MASK 0xfffff0
277#define FC_WELLKNOWN_ROLE_MASK 0x00000f
278#define FC_FPORT_PORTID 0x00000e
279#define FC_FABCTLR_PORTID 0x00000d
280#define FC_DIRSRVR_PORTID 0x00000c
281#define FC_TIMESRVR_PORTID 0x00000b
282#define FC_MGMTSRVR_PORTID 0x00000a
283
284
David Howellsc4028952006-11-22 14:57:56 +0000285static void fc_timeout_deleted_rport(struct work_struct *work);
286static void fc_timeout_fail_rport_io(struct work_struct *work);
287static void fc_scsi_scan_rport(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/*
290 * Attribute counts pre object type...
291 * Increase these values if you add attributes
292 */
293#define FC_STARGET_NUM_ATTRS 3
James Smart0f29b962006-08-18 17:33:29 -0400294#define FC_RPORT_NUM_ATTRS 10
James Smarta53eb5e2007-04-27 12:41:09 -0400295#define FC_VPORT_NUM_ATTRS 9
James Smart46436822009-07-28 12:30:01 -0400296#define FC_HOST_NUM_ATTRS 22
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298struct fc_internal {
299 struct scsi_transport_template t;
300 struct fc_function_template *f;
301
302 /*
303 * For attributes : each object has :
304 * An array of the actual attributes structures
305 * An array of null-terminated pointers to the attribute
306 * structures - used for mid-layer interaction.
307 *
308 * The attribute containers for the starget and host are are
309 * part of the midlayer. As the remote port is specific to the
310 * fc transport, we must provide the attribute container.
311 */
Tony Jonesee959b02008-02-22 00:13:36 +0100312 struct device_attribute private_starget_attrs[
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 FC_STARGET_NUM_ATTRS];
Tony Jonesee959b02008-02-22 00:13:36 +0100314 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Tony Jonesee959b02008-02-22 00:13:36 +0100316 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
317 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 struct transport_container rport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100320 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
321 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
James Smarta53eb5e2007-04-27 12:41:09 -0400322
323 struct transport_container vport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100324 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
325 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326};
327
328#define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
329
James Bottomleyd0a7e572005-08-14 17:09:01 -0500330static int fc_target_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100331 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct scsi_target *starget = to_scsi_target(dev);
334 struct fc_rport *rport = starget_to_rport(starget);
335
336 /*
337 * if parent is remote port, use values from remote port.
338 * Otherwise, this host uses the fc_transport, but not the
339 * remote port interface. As such, initialize to known non-values.
340 */
341 if (rport) {
342 fc_starget_node_name(starget) = rport->node_name;
343 fc_starget_port_name(starget) = rport->port_name;
344 fc_starget_port_id(starget) = rport->port_id;
345 } else {
346 fc_starget_node_name(starget) = -1;
347 fc_starget_port_name(starget) = -1;
348 fc_starget_port_id(starget) = -1;
349 }
350
351 return 0;
352}
353
354static DECLARE_TRANSPORT_CLASS(fc_transport_class,
355 "fc_transport",
356 fc_target_setup,
357 NULL,
358 NULL);
359
James Bottomleyd0a7e572005-08-14 17:09:01 -0500360static int fc_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100361 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct Scsi_Host *shost = dev_to_shost(dev);
James Smartaedf3492006-04-10 10:14:05 -0400364 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
James Smart9ef3e4a2007-05-24 19:04:44 -0400366 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * Set default values easily detected by the midlayer as
368 * failure cases. The scsi lldd is responsible for initializing
369 * all transport attributes to valid values per host.
370 */
James Smartaedf3492006-04-10 10:14:05 -0400371 fc_host->node_name = -1;
372 fc_host->port_name = -1;
373 fc_host->permanent_port_name = -1;
374 fc_host->supported_classes = FC_COS_UNSPECIFIED;
375 memset(fc_host->supported_fc4s, 0,
376 sizeof(fc_host->supported_fc4s));
James Smartaedf3492006-04-10 10:14:05 -0400377 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
378 fc_host->maxframe_size = -1;
James Smarta53eb5e2007-04-27 12:41:09 -0400379 fc_host->max_npiv_vports = 0;
James Smartaedf3492006-04-10 10:14:05 -0400380 memset(fc_host->serial_number, 0,
381 sizeof(fc_host->serial_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
James Smartaedf3492006-04-10 10:14:05 -0400383 fc_host->port_id = -1;
384 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
385 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
386 memset(fc_host->active_fc4s, 0,
387 sizeof(fc_host->active_fc4s));
388 fc_host->speed = FC_PORTSPEED_UNKNOWN;
389 fc_host->fabric_name = -1;
James Smartb8d08212006-08-17 08:00:43 -0400390 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
391 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
James Smartaedf3492006-04-10 10:14:05 -0400393 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
James Smartaedf3492006-04-10 10:14:05 -0400395 INIT_LIST_HEAD(&fc_host->rports);
396 INIT_LIST_HEAD(&fc_host->rport_bindings);
James Smarta53eb5e2007-04-27 12:41:09 -0400397 INIT_LIST_HEAD(&fc_host->vports);
James Smartaedf3492006-04-10 10:14:05 -0400398 fc_host->next_rport_number = 0;
399 fc_host->next_target_id = 0;
James Smarta53eb5e2007-04-27 12:41:09 -0400400 fc_host->next_vport_number = 0;
401 fc_host->npiv_vports_inuse = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Kay Sieversaab0de22008-05-02 06:02:41 +0200403 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
404 "fc_wq_%d", shost->host_no);
James Smartaedf3492006-04-10 10:14:05 -0400405 fc_host->work_q = create_singlethread_workqueue(
406 fc_host->work_q_name);
407 if (!fc_host->work_q)
408 return -ENOMEM;
409
Kay Sieversaab0de22008-05-02 06:02:41 +0200410 snprintf(fc_host->devloss_work_q_name,
411 sizeof(fc_host->devloss_work_q_name),
412 "fc_dl_%d", shost->host_no);
James Smartaedf3492006-04-10 10:14:05 -0400413 fc_host->devloss_work_q = create_singlethread_workqueue(
414 fc_host->devloss_work_q_name);
415 if (!fc_host->devloss_work_q) {
416 destroy_workqueue(fc_host->work_q);
417 fc_host->work_q = NULL;
418 return -ENOMEM;
419 }
420
James Smart9e4f5e22009-03-26 13:33:19 -0400421 fc_bsg_hostadd(shost, fc_host);
422 /* ignore any bsg add error - we just can't do sgio */
423
424 return 0;
425}
426
427static int fc_host_remove(struct transport_container *tc, struct device *dev,
428 struct device *cdev)
429{
430 struct Scsi_Host *shost = dev_to_shost(dev);
431 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
432
433 fc_bsg_remove(fc_host->rqst_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435}
436
437static DECLARE_TRANSPORT_CLASS(fc_host_class,
438 "fc_host",
439 fc_host_setup,
James Smart9e4f5e22009-03-26 13:33:19 -0400440 fc_host_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 NULL);
442
443/*
444 * Setup and Remove actions for remote ports are handled
445 * in the service functions below.
446 */
447static DECLARE_TRANSPORT_CLASS(fc_rport_class,
448 "fc_remote_ports",
449 NULL,
450 NULL,
451 NULL);
452
453/*
James Smarta53eb5e2007-04-27 12:41:09 -0400454 * Setup and Remove actions for virtual ports are handled
455 * in the service functions below.
456 */
457static DECLARE_TRANSPORT_CLASS(fc_vport_class,
458 "fc_vports",
459 NULL,
460 NULL,
461 NULL);
462
463/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * Module Parameters
465 */
466
467/*
468 * dev_loss_tmo: the default number of seconds that the FC transport
469 * should insulate the loss of a remote port.
470 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
471 */
James Smart1c9e16e2006-05-16 16:13:36 -0400472static unsigned int fc_dev_loss_tmo = 60; /* seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Masatake YAMATO10f4b892007-09-19 22:59:16 +0900474module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475MODULE_PARM_DESC(dev_loss_tmo,
476 "Maximum number of seconds that the FC transport should"
477 " insulate the loss of a remote port. Once this value is"
478 " exceeded, the scsi target is removed. Value should be"
Hannes Reineckef28186632009-12-15 09:26:06 +0100479 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
480 " fast_io_fail_tmo is not set.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Rob Landleyeb448202007-11-03 13:30:39 -0500482/*
James Smart84314fd2006-08-18 17:30:09 -0400483 * Netlink Infrastructure
Rob Landleyeb448202007-11-03 13:30:39 -0500484 */
James Smart84314fd2006-08-18 17:30:09 -0400485
486static atomic_t fc_event_seq;
487
488/**
489 * fc_get_event_number - Obtain the next sequential FC event number
490 *
491 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -0500492 * We could have inlined this, but it would have required fc_event_seq to
James Smart84314fd2006-08-18 17:30:09 -0400493 * be exposed. For now, live with the subroutine call.
494 * Atomic used to avoid lock/unlock...
Rob Landleyeb448202007-11-03 13:30:39 -0500495 */
James Smart84314fd2006-08-18 17:30:09 -0400496u32
497fc_get_event_number(void)
498{
499 return atomic_add_return(1, &fc_event_seq);
500}
501EXPORT_SYMBOL(fc_get_event_number);
502
503
504/**
505 * fc_host_post_event - called to post an even on an fc_host.
James Smart84314fd2006-08-18 17:30:09 -0400506 * @shost: host the event occurred on
507 * @event_number: fc event number obtained from get_fc_event_number()
508 * @event_code: fc_host event being posted
509 * @event_data: 32bits of data for the event being posted
510 *
511 * Notes:
512 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500513 */
James Smart84314fd2006-08-18 17:30:09 -0400514void
515fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
516 enum fc_host_event_code event_code, u32 event_data)
517{
518 struct sk_buff *skb;
519 struct nlmsghdr *nlh;
520 struct fc_nl_event *event;
521 const char *name;
522 u32 len, skblen;
523 int err;
524
525 if (!scsi_nl_sock) {
526 err = -ENOENT;
527 goto send_fail;
528 }
529
530 len = FC_NL_MSGALIGN(sizeof(*event));
531 skblen = NLMSG_SPACE(len);
532
533 skb = alloc_skb(skblen, GFP_KERNEL);
534 if (!skb) {
535 err = -ENOBUFS;
536 goto send_fail;
537 }
538
539 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
540 skblen - sizeof(*nlh), 0);
541 if (!nlh) {
542 err = -ENOBUFS;
543 goto send_fail_skb;
544 }
545 event = NLMSG_DATA(nlh);
546
547 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
548 FC_NL_ASYNC_EVENT, len);
549 event->seconds = get_seconds();
550 event->vendor_id = 0;
551 event->host_no = shost->host_no;
552 event->event_datalen = sizeof(u32); /* bytes */
553 event->event_num = event_number;
554 event->event_code = event_code;
555 event->event_data = event_data;
556
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800557 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
558 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400559 return;
560
561send_fail_skb:
562 kfree_skb(skb);
563send_fail:
564 name = get_fc_host_event_code_name(event_code);
565 printk(KERN_WARNING
566 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700567 __func__, shost->host_no,
James Smart84314fd2006-08-18 17:30:09 -0400568 (name) ? name : "<unknown>", event_data, err);
569 return;
570}
571EXPORT_SYMBOL(fc_host_post_event);
572
573
574/**
Rob Landleyeb448202007-11-03 13:30:39 -0500575 * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
James Smart84314fd2006-08-18 17:30:09 -0400576 * @shost: host the event occurred on
577 * @event_number: fc event number obtained from get_fc_event_number()
578 * @data_len: amount, in bytes, of vendor unique data
579 * @data_buf: pointer to vendor unique data
Rob Landleyeb448202007-11-03 13:30:39 -0500580 * @vendor_id: Vendor id
James Smart84314fd2006-08-18 17:30:09 -0400581 *
582 * Notes:
583 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500584 */
James Smart84314fd2006-08-18 17:30:09 -0400585void
586fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
James Smartf14e2e22006-08-22 09:55:23 -0400587 u32 data_len, char * data_buf, u64 vendor_id)
James Smart84314fd2006-08-18 17:30:09 -0400588{
589 struct sk_buff *skb;
590 struct nlmsghdr *nlh;
591 struct fc_nl_event *event;
592 u32 len, skblen;
593 int err;
594
595 if (!scsi_nl_sock) {
596 err = -ENOENT;
597 goto send_vendor_fail;
598 }
599
600 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
601 skblen = NLMSG_SPACE(len);
602
603 skb = alloc_skb(skblen, GFP_KERNEL);
604 if (!skb) {
605 err = -ENOBUFS;
606 goto send_vendor_fail;
607 }
608
609 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
610 skblen - sizeof(*nlh), 0);
611 if (!nlh) {
612 err = -ENOBUFS;
613 goto send_vendor_fail_skb;
614 }
615 event = NLMSG_DATA(nlh);
616
617 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
618 FC_NL_ASYNC_EVENT, len);
619 event->seconds = get_seconds();
620 event->vendor_id = vendor_id;
621 event->host_no = shost->host_no;
622 event->event_datalen = data_len; /* bytes */
623 event->event_num = event_number;
624 event->event_code = FCH_EVT_VENDOR_UNIQUE;
625 memcpy(&event->event_data, data_buf, data_len);
626
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800627 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
628 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400629 return;
630
631send_vendor_fail_skb:
632 kfree_skb(skb);
633send_vendor_fail:
634 printk(KERN_WARNING
635 "%s: Dropped Event : host %d vendor_unique - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700636 __func__, shost->host_no, err);
James Smart84314fd2006-08-18 17:30:09 -0400637 return;
638}
639EXPORT_SYMBOL(fc_host_post_vendor_event);
640
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643static __init int fc_transport_init(void)
644{
James Smart84314fd2006-08-18 17:30:09 -0400645 int error;
646
647 atomic_set(&fc_event_seq, 0);
648
649 error = transport_class_register(&fc_host_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (error)
651 return error;
James Smarta53eb5e2007-04-27 12:41:09 -0400652 error = transport_class_register(&fc_vport_class);
653 if (error)
Mike Christie48de68a2009-11-17 21:25:16 -0600654 goto unreg_host_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 error = transport_class_register(&fc_rport_class);
656 if (error)
Mike Christie48de68a2009-11-17 21:25:16 -0600657 goto unreg_vport_class;
658 error = transport_class_register(&fc_transport_class);
659 if (error)
660 goto unreg_rport_class;
661 return 0;
662
663unreg_rport_class:
664 transport_class_unregister(&fc_rport_class);
665unreg_vport_class:
666 transport_class_unregister(&fc_vport_class);
667unreg_host_class:
668 transport_class_unregister(&fc_host_class);
669 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672static void __exit fc_transport_exit(void)
673{
674 transport_class_unregister(&fc_transport_class);
675 transport_class_unregister(&fc_rport_class);
676 transport_class_unregister(&fc_host_class);
James Smarta53eb5e2007-04-27 12:41:09 -0400677 transport_class_unregister(&fc_vport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680/*
681 * FC Remote Port Attribute Management
682 */
683
684#define fc_rport_show_function(field, format_string, sz, cast) \
685static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100686show_fc_rport_##field (struct device *dev, \
687 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100689 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct Scsi_Host *shost = rport_to_shost(rport); \
691 struct fc_internal *i = to_fc_internal(shost->transportt); \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400692 if ((i->f->get_rport_##field) && \
693 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500694 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400695 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 i->f->get_rport_##field(rport); \
697 return snprintf(buf, sz, format_string, cast rport->field); \
698}
699
700#define fc_rport_store_function(field) \
701static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100702store_fc_rport_##field(struct device *dev, \
703 struct device_attribute *attr, \
704 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{ \
706 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100707 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct Scsi_Host *shost = rport_to_shost(rport); \
709 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -0400710 char *cp; \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400711 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500712 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400713 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
714 return -EBUSY; \
James Smart0f29b962006-08-18 17:33:29 -0400715 val = simple_strtoul(buf, &cp, 0); \
716 if (*cp && (*cp != '\n')) \
717 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 i->f->set_rport_##field(rport, val); \
719 return count; \
720}
721
722#define fc_rport_rd_attr(field, format_string, sz) \
723 fc_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100724static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 show_fc_rport_##field, NULL)
726
727#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
728 fc_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100729static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 show_fc_rport_##field, NULL)
731
732#define fc_rport_rw_attr(field, format_string, sz) \
733 fc_rport_show_function(field, format_string, sz, ) \
734 fc_rport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100735static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 show_fc_rport_##field, \
737 store_fc_rport_##field)
738
739
740#define fc_private_rport_show_function(field, format_string, sz, cast) \
741static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100742show_fc_rport_##field (struct device *dev, \
743 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100745 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return snprintf(buf, sz, format_string, cast rport->field); \
747}
748
749#define fc_private_rport_rd_attr(field, format_string, sz) \
750 fc_private_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100751static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 show_fc_rport_##field, NULL)
753
754#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
755 fc_private_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100756static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 show_fc_rport_##field, NULL)
758
759
760#define fc_private_rport_rd_enum_attr(title, maxlen) \
761static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100762show_fc_rport_##title (struct device *dev, \
763 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100765 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 const char *name; \
767 name = get_fc_##title##_name(rport->title); \
768 if (!name) \
769 return -EINVAL; \
770 return snprintf(buf, maxlen, "%s\n", name); \
771} \
Tony Jonesee959b02008-02-22 00:13:36 +0100772static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 show_fc_rport_##title, NULL)
774
775
776#define SETUP_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100777 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
779 i->private_rport_attrs[count].store = NULL; \
780 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
781 if (i->f->show_rport_##field) \
782 count++
783
784#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100785 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
787 i->private_rport_attrs[count].store = NULL; \
788 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
789 count++
790
791#define SETUP_RPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100792 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!i->f->set_rport_##field) { \
794 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
795 i->private_rport_attrs[count].store = NULL; \
796 } \
797 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
798 if (i->f->show_rport_##field) \
799 count++
800
James Smart0f29b962006-08-18 17:33:29 -0400801#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
802{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100803 i->private_rport_attrs[count] = device_attr_rport_##field; \
James Smart0f29b962006-08-18 17:33:29 -0400804 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
805 count++; \
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809/* The FC Transport Remote Port Attributes: */
810
811/* Fixed Remote Port Attributes */
812
813fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
814
815static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100816show_fc_rport_supported_classes (struct device *dev,
817 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Tony Jonesee959b02008-02-22 00:13:36 +0100819 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (rport->supported_classes == FC_COS_UNSPECIFIED)
821 return snprintf(buf, 20, "unspecified\n");
822 return get_fc_cos_names(rport->supported_classes, buf);
823}
Tony Jonesee959b02008-02-22 00:13:36 +0100824static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 show_fc_rport_supported_classes, NULL);
826
827/* Dynamic Remote Port Attributes */
828
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400829/*
830 * dev_loss_tmo attribute
831 */
832fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
833static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100834store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
835 const char *buf, size_t count)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400836{
837 int val;
Tony Jonesee959b02008-02-22 00:13:36 +0100838 struct fc_rport *rport = transport_class_to_rport(dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400839 struct Scsi_Host *shost = rport_to_shost(rport);
840 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart0f29b962006-08-18 17:33:29 -0400841 char *cp;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400842 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500843 (rport->port_state == FC_PORTSTATE_DELETED) ||
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400844 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
845 return -EBUSY;
James Smart0f29b962006-08-18 17:33:29 -0400846 val = simple_strtoul(buf, &cp, 0);
Hannes Reineckef28186632009-12-15 09:26:06 +0100847 if ((*cp && (*cp != '\n')) || (val < 0))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400848 return -EINVAL;
Hannes Reineckef28186632009-12-15 09:26:06 +0100849
850 /*
851 * If fast_io_fail is off we have to cap
852 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
853 */
854 if (rport->fast_io_fail_tmo == -1 &&
855 val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
856 return -EINVAL;
857
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400858 i->f->set_rport_dev_loss_tmo(rport, val);
859 return count;
860}
Tony Jonesee959b02008-02-22 00:13:36 +0100861static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400862 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864
865/* Private Remote Port Attributes */
866
867fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
868fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
869fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
870
871static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100872show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
873 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Tony Jonesee959b02008-02-22 00:13:36 +0100875 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /* identify any roles that are port_id specific */
878 if ((rport->port_id != -1) &&
879 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
880 FC_WELLKNOWN_PORTID_MASK) {
881 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
882 case FC_FPORT_PORTID:
883 return snprintf(buf, 30, "Fabric Port\n");
884 case FC_FABCTLR_PORTID:
885 return snprintf(buf, 30, "Fabric Controller\n");
886 case FC_DIRSRVR_PORTID:
887 return snprintf(buf, 30, "Directory Server\n");
888 case FC_TIMESRVR_PORTID:
889 return snprintf(buf, 30, "Time Server\n");
890 case FC_MGMTSRVR_PORTID:
891 return snprintf(buf, 30, "Management Server\n");
892 default:
893 return snprintf(buf, 30, "Unknown Fabric Entity\n");
894 }
895 } else {
James Smarta53eb5e2007-04-27 12:41:09 -0400896 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return snprintf(buf, 20, "unknown\n");
James Smarta53eb5e2007-04-27 12:41:09 -0400898 return get_fc_port_roles_names(rport->roles, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900}
Tony Jonesee959b02008-02-22 00:13:36 +0100901static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 show_fc_rport_roles, NULL);
903
904fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
905fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
906
James Smart0f29b962006-08-18 17:33:29 -0400907/*
908 * fast_io_fail_tmo attribute
909 */
910static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100911show_fc_rport_fast_io_fail_tmo (struct device *dev,
912 struct device_attribute *attr, char *buf)
James Smart0f29b962006-08-18 17:33:29 -0400913{
Tony Jonesee959b02008-02-22 00:13:36 +0100914 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400915
916 if (rport->fast_io_fail_tmo == -1)
917 return snprintf(buf, 5, "off\n");
918 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
919}
920
921static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100922store_fc_rport_fast_io_fail_tmo(struct device *dev,
923 struct device_attribute *attr, const char *buf,
924 size_t count)
James Smart0f29b962006-08-18 17:33:29 -0400925{
926 int val;
927 char *cp;
Tony Jonesee959b02008-02-22 00:13:36 +0100928 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400929
930 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
931 (rport->port_state == FC_PORTSTATE_DELETED) ||
932 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
933 return -EBUSY;
934 if (strncmp(buf, "off", 3) == 0)
935 rport->fast_io_fail_tmo = -1;
936 else {
937 val = simple_strtoul(buf, &cp, 0);
Hannes Reineckef28186632009-12-15 09:26:06 +0100938 if ((*cp && (*cp != '\n')) || (val < 0))
James Smart0f29b962006-08-18 17:33:29 -0400939 return -EINVAL;
Hannes Reineckef28186632009-12-15 09:26:06 +0100940 /*
941 * Cap fast_io_fail by dev_loss_tmo or
942 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
943 */
944 if ((val >= rport->dev_loss_tmo) ||
945 (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
946 return -EINVAL;
947
James Smart0f29b962006-08-18 17:33:29 -0400948 rport->fast_io_fail_tmo = val;
949 }
950 return count;
951}
Tony Jonesee959b02008-02-22 00:13:36 +0100952static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
James Smart0f29b962006-08-18 17:33:29 -0400953 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955
956/*
957 * FC SCSI Target Attribute Management
958 */
959
960/*
961 * Note: in the target show function we recognize when the remote
James Smarta53eb5e2007-04-27 12:41:09 -0400962 * port is in the heirarchy and do not allow the driver to get
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * involved in sysfs functions. The driver only gets involved if
964 * it's the "old" style that doesn't use rports.
965 */
966#define fc_starget_show_function(field, format_string, sz, cast) \
967static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100968show_fc_starget_##field (struct device *dev, \
969 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100971 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
973 struct fc_internal *i = to_fc_internal(shost->transportt); \
974 struct fc_rport *rport = starget_to_rport(starget); \
975 if (rport) \
976 fc_starget_##field(starget) = rport->field; \
977 else if (i->f->get_starget_##field) \
978 i->f->get_starget_##field(starget); \
979 return snprintf(buf, sz, format_string, \
980 cast fc_starget_##field(starget)); \
981}
982
983#define fc_starget_rd_attr(field, format_string, sz) \
984 fc_starget_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100985static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 show_fc_starget_##field, NULL)
987
988#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
989 fc_starget_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100990static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 show_fc_starget_##field, NULL)
992
993#define SETUP_STARGET_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100994 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
996 i->private_starget_attrs[count].store = NULL; \
997 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
998 if (i->f->show_starget_##field) \
999 count++
1000
1001#define SETUP_STARGET_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001002 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (!i->f->set_starget_##field) { \
1004 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1005 i->private_starget_attrs[count].store = NULL; \
1006 } \
1007 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1008 if (i->f->show_starget_##field) \
1009 count++
1010
1011/* The FC Transport SCSI Target Attributes: */
1012fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1013fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1014fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1015
1016
1017/*
James Smarta53eb5e2007-04-27 12:41:09 -04001018 * FC Virtual Port Attribute Management
1019 */
1020
1021#define fc_vport_show_function(field, format_string, sz, cast) \
1022static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001023show_fc_vport_##field (struct device *dev, \
1024 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001025{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001026 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001027 struct Scsi_Host *shost = vport_to_shost(vport); \
1028 struct fc_internal *i = to_fc_internal(shost->transportt); \
1029 if ((i->f->get_vport_##field) && \
1030 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1031 i->f->get_vport_##field(vport); \
1032 return snprintf(buf, sz, format_string, cast vport->field); \
1033}
1034
1035#define fc_vport_store_function(field) \
1036static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001037store_fc_vport_##field(struct device *dev, \
1038 struct device_attribute *attr, \
1039 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001040{ \
1041 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001042 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001043 struct Scsi_Host *shost = vport_to_shost(vport); \
1044 struct fc_internal *i = to_fc_internal(shost->transportt); \
1045 char *cp; \
1046 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1047 return -EBUSY; \
1048 val = simple_strtoul(buf, &cp, 0); \
1049 if (*cp && (*cp != '\n')) \
1050 return -EINVAL; \
1051 i->f->set_vport_##field(vport, val); \
1052 return count; \
1053}
1054
1055#define fc_vport_store_str_function(field, slen) \
1056static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001057store_fc_vport_##field(struct device *dev, \
1058 struct device_attribute *attr, \
1059 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001060{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001061 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001062 struct Scsi_Host *shost = vport_to_shost(vport); \
1063 struct fc_internal *i = to_fc_internal(shost->transportt); \
1064 unsigned int cnt=count; \
1065 \
1066 /* count may include a LF at end of string */ \
1067 if (buf[cnt-1] == '\n') \
1068 cnt--; \
1069 if (cnt > ((slen) - 1)) \
1070 return -EINVAL; \
1071 memcpy(vport->field, buf, cnt); \
1072 i->f->set_vport_##field(vport); \
1073 return count; \
1074}
1075
1076#define fc_vport_rd_attr(field, format_string, sz) \
1077 fc_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001078static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001079 show_fc_vport_##field, NULL)
1080
1081#define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1082 fc_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001083static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001084 show_fc_vport_##field, NULL)
1085
1086#define fc_vport_rw_attr(field, format_string, sz) \
1087 fc_vport_show_function(field, format_string, sz, ) \
1088 fc_vport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001089static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001090 show_fc_vport_##field, \
1091 store_fc_vport_##field)
1092
1093#define fc_private_vport_show_function(field, format_string, sz, cast) \
1094static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001095show_fc_vport_##field (struct device *dev, \
1096 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001097{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001098 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001099 return snprintf(buf, sz, format_string, cast vport->field); \
1100}
1101
1102#define fc_private_vport_store_u32_function(field) \
1103static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001104store_fc_vport_##field(struct device *dev, \
1105 struct device_attribute *attr, \
1106 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001107{ \
1108 u32 val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001109 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001110 char *cp; \
1111 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1112 return -EBUSY; \
1113 val = simple_strtoul(buf, &cp, 0); \
1114 if (*cp && (*cp != '\n')) \
1115 return -EINVAL; \
1116 vport->field = val; \
1117 return count; \
1118}
1119
1120
1121#define fc_private_vport_rd_attr(field, format_string, sz) \
1122 fc_private_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001123static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001124 show_fc_vport_##field, NULL)
1125
1126#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1127 fc_private_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001128static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001129 show_fc_vport_##field, NULL)
1130
1131#define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1132 fc_private_vport_show_function(field, format_string, sz, ) \
1133 fc_private_vport_store_u32_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001134static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001135 show_fc_vport_##field, \
1136 store_fc_vport_##field)
1137
1138
1139#define fc_private_vport_rd_enum_attr(title, maxlen) \
1140static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001141show_fc_vport_##title (struct device *dev, \
1142 struct device_attribute *attr, \
1143 char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001144{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001145 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001146 const char *name; \
1147 name = get_fc_##title##_name(vport->title); \
1148 if (!name) \
1149 return -EINVAL; \
1150 return snprintf(buf, maxlen, "%s\n", name); \
1151} \
Tony Jonesee959b02008-02-22 00:13:36 +01001152static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001153 show_fc_vport_##title, NULL)
1154
1155
1156#define SETUP_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001157 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001158 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1159 i->private_vport_attrs[count].store = NULL; \
1160 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1161 if (i->f->get_##field) \
1162 count++
1163 /* NOTE: Above MACRO differs: checks function not show bit */
1164
1165#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001166 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001167 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1168 i->private_vport_attrs[count].store = NULL; \
1169 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1170 count++
1171
1172#define SETUP_VPORT_ATTRIBUTE_WR(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001173 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001174 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1175 if (i->f->field) \
1176 count++
1177 /* NOTE: Above MACRO differs: checks function */
1178
1179#define SETUP_VPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001180 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001181 if (!i->f->set_vport_##field) { \
1182 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1183 i->private_vport_attrs[count].store = NULL; \
1184 } \
1185 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1186 count++
1187 /* NOTE: Above MACRO differs: does not check show bit */
1188
1189#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1190{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001191 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001192 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1193 count++; \
1194}
1195
1196
1197/* The FC Transport Virtual Port Attributes: */
1198
1199/* Fixed Virtual Port Attributes */
1200
1201/* Dynamic Virtual Port Attributes */
1202
1203/* Private Virtual Port Attributes */
1204
1205fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1206fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1207fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1208fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1209
1210static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001211show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1212 char *buf)
James Smarta53eb5e2007-04-27 12:41:09 -04001213{
Tony Jonesee959b02008-02-22 00:13:36 +01001214 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001215
1216 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1217 return snprintf(buf, 20, "unknown\n");
1218 return get_fc_port_roles_names(vport->roles, buf);
1219}
Tony Jonesee959b02008-02-22 00:13:36 +01001220static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
James Smarta53eb5e2007-04-27 12:41:09 -04001221
1222fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1223
1224fc_private_vport_show_function(symbolic_name, "%s\n",
1225 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1226fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
Tony Jonesee959b02008-02-22 00:13:36 +01001227static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001228 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1229
1230static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001231store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1232 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001233{
Tony Jonesee959b02008-02-22 00:13:36 +01001234 struct fc_vport *vport = transport_class_to_vport(dev);
James Smart9ef3e4a2007-05-24 19:04:44 -04001235 struct Scsi_Host *shost = vport_to_shost(vport);
Gal Rosen0d9dc7c2010-01-21 10:15:32 +02001236 unsigned long flags;
1237
1238 spin_lock_irqsave(shost->host_lock, flags);
1239 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
1240 spin_unlock_irqrestore(shost->host_lock, flags);
1241 return -EBUSY;
1242 }
1243 vport->flags |= FC_VPORT_DELETING;
1244 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04001245
James Smart9ef3e4a2007-05-24 19:04:44 -04001246 fc_queue_work(shost, &vport->vport_delete_work);
James Smarta53eb5e2007-04-27 12:41:09 -04001247 return count;
1248}
Tony Jonesee959b02008-02-22 00:13:36 +01001249static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001250 NULL, store_fc_vport_delete);
1251
1252
1253/*
1254 * Enable/Disable vport
1255 * Write "1" to disable, write "0" to enable
1256 */
1257static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001258store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1259 const char *buf,
James Smarta53eb5e2007-04-27 12:41:09 -04001260 size_t count)
1261{
Tony Jonesee959b02008-02-22 00:13:36 +01001262 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001263 struct Scsi_Host *shost = vport_to_shost(vport);
1264 struct fc_internal *i = to_fc_internal(shost->transportt);
1265 int stat;
1266
1267 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1268 return -EBUSY;
1269
1270 if (*buf == '0') {
1271 if (vport->vport_state != FC_VPORT_DISABLED)
1272 return -EALREADY;
1273 } else if (*buf == '1') {
1274 if (vport->vport_state == FC_VPORT_DISABLED)
1275 return -EALREADY;
1276 } else
1277 return -EINVAL;
1278
1279 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1280 return stat ? stat : count;
1281}
Tony Jonesee959b02008-02-22 00:13:36 +01001282static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001283 NULL, store_fc_vport_disable);
1284
1285
1286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 * Host Attribute Management
1288 */
1289
1290#define fc_host_show_function(field, format_string, sz, cast) \
1291static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001292show_fc_host_##field (struct device *dev, \
1293 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001295 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 struct fc_internal *i = to_fc_internal(shost->transportt); \
1297 if (i->f->get_host_##field) \
1298 i->f->get_host_##field(shost); \
1299 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1300}
1301
1302#define fc_host_store_function(field) \
1303static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001304store_fc_host_##field(struct device *dev, \
1305 struct device_attribute *attr, \
1306 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{ \
1308 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001309 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -04001311 char *cp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 \
James Smart0f29b962006-08-18 17:33:29 -04001313 val = simple_strtoul(buf, &cp, 0); \
1314 if (*cp && (*cp != '\n')) \
1315 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 i->f->set_host_##field(shost, val); \
1317 return count; \
1318}
1319
James Smartb8d08212006-08-17 08:00:43 -04001320#define fc_host_store_str_function(field, slen) \
1321static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001322store_fc_host_##field(struct device *dev, \
1323 struct device_attribute *attr, \
1324 const char *buf, size_t count) \
James Smartb8d08212006-08-17 08:00:43 -04001325{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001326 struct Scsi_Host *shost = transport_class_to_shost(dev); \
James Smartb8d08212006-08-17 08:00:43 -04001327 struct fc_internal *i = to_fc_internal(shost->transportt); \
1328 unsigned int cnt=count; \
1329 \
1330 /* count may include a LF at end of string */ \
1331 if (buf[cnt-1] == '\n') \
1332 cnt--; \
1333 if (cnt > ((slen) - 1)) \
1334 return -EINVAL; \
1335 memcpy(fc_host_##field(shost), buf, cnt); \
1336 i->f->set_host_##field(shost); \
1337 return count; \
1338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340#define fc_host_rd_attr(field, format_string, sz) \
1341 fc_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001342static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 show_fc_host_##field, NULL)
1344
1345#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1346 fc_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001347static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 show_fc_host_##field, NULL)
1349
1350#define fc_host_rw_attr(field, format_string, sz) \
1351 fc_host_show_function(field, format_string, sz, ) \
1352 fc_host_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001353static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 show_fc_host_##field, \
1355 store_fc_host_##field)
1356
1357#define fc_host_rd_enum_attr(title, maxlen) \
1358static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001359show_fc_host_##title (struct device *dev, \
1360 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001362 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 struct fc_internal *i = to_fc_internal(shost->transportt); \
1364 const char *name; \
1365 if (i->f->get_host_##title) \
1366 i->f->get_host_##title(shost); \
1367 name = get_fc_##title##_name(fc_host_##title(shost)); \
1368 if (!name) \
1369 return -EINVAL; \
1370 return snprintf(buf, maxlen, "%s\n", name); \
1371} \
Tony Jonesee959b02008-02-22 00:13:36 +01001372static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374#define SETUP_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001375 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1377 i->private_host_attrs[count].store = NULL; \
1378 i->host_attrs[count] = &i->private_host_attrs[count]; \
1379 if (i->f->show_host_##field) \
1380 count++
1381
James Smarta53eb5e2007-04-27 12:41:09 -04001382#define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001383 i->private_host_attrs[count] = device_attr_host_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001384 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389#define SETUP_HOST_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001390 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (!i->f->set_host_##field) { \
1392 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1393 i->private_host_attrs[count].store = NULL; \
1394 } \
1395 i->host_attrs[count] = &i->private_host_attrs[count]; \
1396 if (i->f->show_host_##field) \
1397 count++
1398
1399
1400#define fc_private_host_show_function(field, format_string, sz, cast) \
1401static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001402show_fc_host_##field (struct device *dev, \
1403 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001405 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1407}
1408
1409#define fc_private_host_rd_attr(field, format_string, sz) \
1410 fc_private_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001411static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 show_fc_host_##field, NULL)
1413
1414#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1415 fc_private_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001416static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 show_fc_host_##field, NULL)
1418
1419#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001420 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1422 i->private_host_attrs[count].store = NULL; \
1423 i->host_attrs[count] = &i->private_host_attrs[count]; \
1424 count++
1425
1426#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001427{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001428 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 i->host_attrs[count] = &i->private_host_attrs[count]; \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001430 count++; \
1431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433
1434/* Fixed Host Attributes */
1435
1436static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001437show_fc_host_supported_classes (struct device *dev,
1438 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Tony Jonesee959b02008-02-22 00:13:36 +01001440 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1443 return snprintf(buf, 20, "unspecified\n");
1444
1445 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1446}
Tony Jonesee959b02008-02-22 00:13:36 +01001447static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 show_fc_host_supported_classes, NULL);
1449
1450static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001451show_fc_host_supported_fc4s (struct device *dev,
1452 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Tony Jonesee959b02008-02-22 00:13:36 +01001454 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1456}
Tony Jonesee959b02008-02-22 00:13:36 +01001457static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 show_fc_host_supported_fc4s, NULL);
1459
1460static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001461show_fc_host_supported_speeds (struct device *dev,
1462 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Tony Jonesee959b02008-02-22 00:13:36 +01001464 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1467 return snprintf(buf, 20, "unknown\n");
1468
1469 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1470}
Tony Jonesee959b02008-02-22 00:13:36 +01001471static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 show_fc_host_supported_speeds, NULL);
1473
1474
1475fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1476fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01001477fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1478 unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
James Smarta53eb5e2007-04-27 12:41:09 -04001480fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1482
1483
1484/* Dynamic Host Attributes */
1485
1486static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001487show_fc_host_active_fc4s (struct device *dev,
1488 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Tony Jonesee959b02008-02-22 00:13:36 +01001490 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 struct fc_internal *i = to_fc_internal(shost->transportt);
1492
1493 if (i->f->get_host_active_fc4s)
1494 i->f->get_host_active_fc4s(shost);
1495
1496 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1497}
Tony Jonesee959b02008-02-22 00:13:36 +01001498static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 show_fc_host_active_fc4s, NULL);
1500
1501static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001502show_fc_host_speed (struct device *dev,
1503 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Tony Jonesee959b02008-02-22 00:13:36 +01001505 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 struct fc_internal *i = to_fc_internal(shost->transportt);
1507
1508 if (i->f->get_host_speed)
1509 i->f->get_host_speed(shost);
1510
1511 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1512 return snprintf(buf, 20, "unknown\n");
1513
1514 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1515}
Tony Jonesee959b02008-02-22 00:13:36 +01001516static FC_DEVICE_ATTR(host, speed, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 show_fc_host_speed, NULL);
1518
1519
1520fc_host_rd_attr(port_id, "0x%06x\n", 20);
1521fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1522fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1523fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
James Smart016131b2006-08-14 08:20:25 -04001524fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
James Smartb8d08212006-08-17 08:00:43 -04001526fc_private_host_show_function(system_hostname, "%s\n",
1527 FC_SYMBOLIC_NAME_SIZE + 1, )
1528fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
Tony Jonesee959b02008-02-22 00:13:36 +01001529static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
James Smartb8d08212006-08-17 08:00:43 -04001530 show_fc_host_system_hostname, store_fc_host_system_hostname);
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533/* Private Host Attributes */
1534
1535static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001536show_fc_private_host_tgtid_bind_type(struct device *dev,
1537 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Tony Jonesee959b02008-02-22 00:13:36 +01001539 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 const char *name;
1541
1542 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1543 if (!name)
1544 return -EINVAL;
1545 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1546}
1547
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001548#define get_list_head_entry(pos, head, member) \
1549 pos = list_entry((head)->next, typeof(*pos), member)
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001552store_fc_private_host_tgtid_bind_type(struct device *dev,
1553 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Tony Jonesee959b02008-02-22 00:13:36 +01001555 struct Scsi_Host *shost = transport_class_to_shost(dev);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001556 struct fc_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 enum fc_tgtid_binding_type val;
1558 unsigned long flags;
1559
1560 if (get_fc_tgtid_bind_type_match(buf, &val))
1561 return -EINVAL;
1562
1563 /* if changing bind type, purge all unused consistent bindings */
1564 if (val != fc_host_tgtid_bind_type(shost)) {
1565 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001566 while (!list_empty(&fc_host_rport_bindings(shost))) {
1567 get_list_head_entry(rport,
1568 &fc_host_rport_bindings(shost), peers);
James Smartaedf3492006-04-10 10:14:05 -04001569 list_del(&rport->peers);
1570 rport->port_state = FC_PORTSTATE_DELETED;
1571 fc_queue_work(shost, &rport->rport_delete_work);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 spin_unlock_irqrestore(shost->host_lock, flags);
1574 }
1575
1576 fc_host_tgtid_bind_type(shost) = val;
1577 return count;
1578}
1579
Tony Jonesee959b02008-02-22 00:13:36 +01001580static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 show_fc_private_host_tgtid_bind_type,
1582 store_fc_private_host_tgtid_bind_type);
1583
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001584static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001585store_fc_private_host_issue_lip(struct device *dev,
1586 struct device_attribute *attr, const char *buf, size_t count)
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001587{
Tony Jonesee959b02008-02-22 00:13:36 +01001588 struct Scsi_Host *shost = transport_class_to_shost(dev);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001589 struct fc_internal *i = to_fc_internal(shost->transportt);
1590 int ret;
1591
1592 /* ignore any data value written to the attribute */
1593 if (i->f->issue_fc_host_lip) {
1594 ret = i->f->issue_fc_host_lip(shost);
1595 return ret ? ret: count;
1596 }
1597
1598 return -ENOENT;
1599}
1600
Tony Jonesee959b02008-02-22 00:13:36 +01001601static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001602 store_fc_private_host_issue_lip);
1603
James Smarta53eb5e2007-04-27 12:41:09 -04001604fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1605
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607/*
1608 * Host Statistics Management
1609 */
1610
1611/* Show a given an attribute in the statistics group */
1612static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001613fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Tony Jonesee959b02008-02-22 00:13:36 +01001615 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 struct fc_internal *i = to_fc_internal(shost->transportt);
1617 struct fc_host_statistics *stats;
1618 ssize_t ret = -ENOENT;
1619
1620 if (offset > sizeof(struct fc_host_statistics) ||
1621 offset % sizeof(u64) != 0)
1622 WARN_ON(1);
1623
1624 if (i->f->get_fc_host_stats) {
1625 stats = (i->f->get_fc_host_stats)(shost);
1626 if (stats)
1627 ret = snprintf(buf, 20, "0x%llx\n",
1628 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1629 }
1630 return ret;
1631}
1632
1633
1634/* generate a read-only statistics attribute */
1635#define fc_host_statistic(name) \
Tony Jonesee959b02008-02-22 00:13:36 +01001636static ssize_t show_fcstat_##name(struct device *cd, \
1637 struct device_attribute *attr, \
1638 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{ \
1640 return fc_stat_show(cd, buf, \
1641 offsetof(struct fc_host_statistics, name)); \
1642} \
Tony Jonesee959b02008-02-22 00:13:36 +01001643static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645fc_host_statistic(seconds_since_last_reset);
1646fc_host_statistic(tx_frames);
1647fc_host_statistic(tx_words);
1648fc_host_statistic(rx_frames);
1649fc_host_statistic(rx_words);
1650fc_host_statistic(lip_count);
1651fc_host_statistic(nos_count);
1652fc_host_statistic(error_frames);
1653fc_host_statistic(dumped_frames);
1654fc_host_statistic(link_failure_count);
1655fc_host_statistic(loss_of_sync_count);
1656fc_host_statistic(loss_of_signal_count);
1657fc_host_statistic(prim_seq_protocol_err_count);
1658fc_host_statistic(invalid_tx_word_count);
1659fc_host_statistic(invalid_crc_count);
1660fc_host_statistic(fcp_input_requests);
1661fc_host_statistic(fcp_output_requests);
1662fc_host_statistic(fcp_control_requests);
1663fc_host_statistic(fcp_input_megabytes);
1664fc_host_statistic(fcp_output_megabytes);
1665
1666static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001667fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1668 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Tony Jonesee959b02008-02-22 00:13:36 +01001670 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct fc_internal *i = to_fc_internal(shost->transportt);
1672
1673 /* ignore any data value written to the attribute */
1674 if (i->f->reset_fc_host_stats) {
1675 i->f->reset_fc_host_stats(shost);
1676 return count;
1677 }
1678
1679 return -ENOENT;
1680}
Tony Jonesee959b02008-02-22 00:13:36 +01001681static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 fc_reset_statistics);
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684static struct attribute *fc_statistics_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001685 &device_attr_host_seconds_since_last_reset.attr,
1686 &device_attr_host_tx_frames.attr,
1687 &device_attr_host_tx_words.attr,
1688 &device_attr_host_rx_frames.attr,
1689 &device_attr_host_rx_words.attr,
1690 &device_attr_host_lip_count.attr,
1691 &device_attr_host_nos_count.attr,
1692 &device_attr_host_error_frames.attr,
1693 &device_attr_host_dumped_frames.attr,
1694 &device_attr_host_link_failure_count.attr,
1695 &device_attr_host_loss_of_sync_count.attr,
1696 &device_attr_host_loss_of_signal_count.attr,
1697 &device_attr_host_prim_seq_protocol_err_count.attr,
1698 &device_attr_host_invalid_tx_word_count.attr,
1699 &device_attr_host_invalid_crc_count.attr,
1700 &device_attr_host_fcp_input_requests.attr,
1701 &device_attr_host_fcp_output_requests.attr,
1702 &device_attr_host_fcp_control_requests.attr,
1703 &device_attr_host_fcp_input_megabytes.attr,
1704 &device_attr_host_fcp_output_megabytes.attr,
1705 &device_attr_host_reset_statistics.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 NULL
1707};
1708
1709static struct attribute_group fc_statistics_group = {
1710 .name = "statistics",
1711 .attrs = fc_statistics_attrs,
1712};
1713
James Smarta53eb5e2007-04-27 12:41:09 -04001714
1715/* Host Vport Attributes */
1716
1717static int
1718fc_parse_wwn(const char *ns, u64 *nm)
1719{
1720 unsigned int i, j;
1721 u8 wwn[8];
1722
1723 memset(wwn, 0, sizeof(wwn));
1724
1725 /* Validate and store the new name */
1726 for (i=0, j=0; i < 16; i++) {
1727 if ((*ns >= 'a') && (*ns <= 'f'))
1728 j = ((j << 4) | ((*ns++ -'a') + 10));
1729 else if ((*ns >= 'A') && (*ns <= 'F'))
1730 j = ((j << 4) | ((*ns++ -'A') + 10));
1731 else if ((*ns >= '0') && (*ns <= '9'))
1732 j = ((j << 4) | (*ns++ -'0'));
1733 else
1734 return -EINVAL;
1735 if (i % 2) {
1736 wwn[i/2] = j & 0xff;
1737 j = 0;
1738 }
1739 }
1740
1741 *nm = wwn_to_u64(wwn);
1742
1743 return 0;
1744}
1745
1746
1747/*
1748 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1749 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1750 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1751 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1752 */
1753static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001754store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1755 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001756{
Tony Jonesee959b02008-02-22 00:13:36 +01001757 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001758 struct fc_vport_identifiers vid;
1759 struct fc_vport *vport;
1760 unsigned int cnt=count;
1761 int stat;
1762
1763 memset(&vid, 0, sizeof(vid));
1764
1765 /* count may include a LF at end of string */
1766 if (buf[cnt-1] == '\n')
1767 cnt--;
1768
1769 /* validate we have enough characters for WWPN */
1770 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1771 return -EINVAL;
1772
1773 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1774 if (stat)
1775 return stat;
1776
1777 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1778 if (stat)
1779 return stat;
1780
1781 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1782 vid.vport_type = FC_PORTTYPE_NPIV;
1783 /* vid.symbolic_name is already zero/NULL's */
1784 vid.disable = false; /* always enabled */
1785
1786 /* we only allow support on Channel 0 !!! */
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07001787 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
James Smarta53eb5e2007-04-27 12:41:09 -04001788 return stat ? stat : count;
1789}
Tony Jonesee959b02008-02-22 00:13:36 +01001790static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001791 store_fc_host_vport_create);
1792
1793
1794/*
1795 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1796 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1797 * The WWNs are specified as hex characters, and may *not* contain
1798 * any prefixes (e.g. 0x, x, etc)
1799 */
1800static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001801store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1802 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001803{
Tony Jonesee959b02008-02-22 00:13:36 +01001804 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001805 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1806 struct fc_vport *vport;
1807 u64 wwpn, wwnn;
1808 unsigned long flags;
1809 unsigned int cnt=count;
1810 int stat, match;
1811
1812 /* count may include a LF at end of string */
1813 if (buf[cnt-1] == '\n')
1814 cnt--;
1815
1816 /* validate we have enough characters for WWPN */
1817 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1818 return -EINVAL;
1819
1820 stat = fc_parse_wwn(&buf[0], &wwpn);
1821 if (stat)
1822 return stat;
1823
1824 stat = fc_parse_wwn(&buf[17], &wwnn);
1825 if (stat)
1826 return stat;
1827
1828 spin_lock_irqsave(shost->host_lock, flags);
1829 match = 0;
1830 /* we only allow support on Channel 0 !!! */
1831 list_for_each_entry(vport, &fc_host->vports, peers) {
1832 if ((vport->channel == 0) &&
1833 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
Gal Rosen0d9dc7c2010-01-21 10:15:32 +02001834 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1835 break;
1836 vport->flags |= FC_VPORT_DELETING;
James Smarta53eb5e2007-04-27 12:41:09 -04001837 match = 1;
1838 break;
1839 }
1840 }
1841 spin_unlock_irqrestore(shost->host_lock, flags);
1842
1843 if (!match)
1844 return -ENODEV;
1845
1846 stat = fc_vport_terminate(vport);
1847 return stat ? stat : count;
1848}
Tony Jonesee959b02008-02-22 00:13:36 +01001849static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001850 store_fc_host_vport_delete);
1851
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853static int fc_host_match(struct attribute_container *cont,
1854 struct device *dev)
1855{
1856 struct Scsi_Host *shost;
1857 struct fc_internal *i;
1858
1859 if (!scsi_is_host_device(dev))
1860 return 0;
1861
1862 shost = dev_to_shost(dev);
1863 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1864 != &fc_host_class.class)
1865 return 0;
1866
1867 i = to_fc_internal(shost->transportt);
1868
1869 return &i->t.host_attrs.ac == cont;
1870}
1871
1872static int fc_target_match(struct attribute_container *cont,
1873 struct device *dev)
1874{
1875 struct Scsi_Host *shost;
1876 struct fc_internal *i;
1877
1878 if (!scsi_is_target_device(dev))
1879 return 0;
1880
1881 shost = dev_to_shost(dev->parent);
1882 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1883 != &fc_host_class.class)
1884 return 0;
1885
1886 i = to_fc_internal(shost->transportt);
1887
1888 return &i->t.target_attrs.ac == cont;
1889}
1890
1891static void fc_rport_dev_release(struct device *dev)
1892{
1893 struct fc_rport *rport = dev_to_rport(dev);
1894 put_device(dev->parent);
1895 kfree(rport);
1896}
1897
1898int scsi_is_fc_rport(const struct device *dev)
1899{
1900 return dev->release == fc_rport_dev_release;
1901}
1902EXPORT_SYMBOL(scsi_is_fc_rport);
1903
1904static int fc_rport_match(struct attribute_container *cont,
1905 struct device *dev)
1906{
1907 struct Scsi_Host *shost;
1908 struct fc_internal *i;
1909
1910 if (!scsi_is_fc_rport(dev))
1911 return 0;
1912
1913 shost = dev_to_shost(dev->parent);
1914 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1915 != &fc_host_class.class)
1916 return 0;
1917
1918 i = to_fc_internal(shost->transportt);
1919
1920 return &i->rport_attr_cont.ac == cont;
1921}
1922
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001923
James Smarta53eb5e2007-04-27 12:41:09 -04001924static void fc_vport_dev_release(struct device *dev)
1925{
1926 struct fc_vport *vport = dev_to_vport(dev);
1927 put_device(dev->parent); /* release kobj parent */
1928 kfree(vport);
1929}
1930
1931int scsi_is_fc_vport(const struct device *dev)
1932{
1933 return dev->release == fc_vport_dev_release;
1934}
1935EXPORT_SYMBOL(scsi_is_fc_vport);
1936
1937static int fc_vport_match(struct attribute_container *cont,
1938 struct device *dev)
1939{
1940 struct fc_vport *vport;
1941 struct Scsi_Host *shost;
1942 struct fc_internal *i;
1943
1944 if (!scsi_is_fc_vport(dev))
1945 return 0;
1946 vport = dev_to_vport(dev);
1947
1948 shost = vport_to_shost(vport);
1949 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1950 != &fc_host_class.class)
1951 return 0;
1952
1953 i = to_fc_internal(shost->transportt);
1954 return &i->vport_attr_cont.ac == cont;
1955}
1956
1957
James Smartc829c392006-03-13 08:28:57 -05001958/**
1959 * fc_timed_out - FC Transport I/O timeout intercept handler
James Smartc829c392006-03-13 08:28:57 -05001960 * @scmd: The SCSI command which timed out
1961 *
1962 * This routine protects against error handlers getting invoked while a
1963 * rport is in a blocked state, typically due to a temporarily loss of
1964 * connectivity. If the error handlers are allowed to proceed, requests
1965 * to abort i/o, reset the target, etc will likely fail as there is no way
1966 * to communicate with the device to perform the requested function. These
1967 * failures may result in the midlayer taking the device offline, requiring
1968 * manual intervention to restore operation.
1969 *
1970 * This routine, called whenever an i/o times out, validates the state of
1971 * the underlying rport. If the rport is blocked, it returns
1972 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1973 * Eventually, either the device will return, or devloss_tmo will fire,
1974 * and when the timeout then fires, it will be handled normally.
1975 * If the rport is not blocked, normal error handling continues.
1976 *
1977 * Notes:
1978 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05001979 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001980static enum blk_eh_timer_return
James Smartc829c392006-03-13 08:28:57 -05001981fc_timed_out(struct scsi_cmnd *scmd)
1982{
1983 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1984
1985 if (rport->port_state == FC_PORTSTATE_BLOCKED)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001986 return BLK_EH_RESET_TIMER;
James Smartc829c392006-03-13 08:28:57 -05001987
Jens Axboe242f9dc2008-09-14 05:55:09 -07001988 return BLK_EH_NOT_HANDLED;
James Smartc829c392006-03-13 08:28:57 -05001989}
1990
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001991/*
James Smartbda23252008-04-24 12:12:46 -04001992 * Called by fc_user_scan to locate an rport on the shost that
1993 * matches the channel and target id, and invoke scsi_scan_target()
1994 * on the rport.
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001995 */
James Smartbda23252008-04-24 12:12:46 -04001996static void
1997fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001998{
1999 struct fc_rport *rport;
James Smartbda23252008-04-24 12:12:46 -04002000 unsigned long flags;
2001
2002 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002003
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002004 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2005 if (rport->scsi_target_id == -1)
2006 continue;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002007
Hannes Reinecke0d2fcd92007-06-14 15:16:45 +02002008 if (rport->port_state != FC_PORTSTATE_ONLINE)
2009 continue;
2010
James Smartbda23252008-04-24 12:12:46 -04002011 if ((channel == rport->channel) &&
2012 (id == rport->scsi_target_id)) {
2013 spin_unlock_irqrestore(shost->host_lock, flags);
2014 scsi_scan_target(&rport->dev, channel, id, lun, 1);
2015 return;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002016 }
2017 }
2018
James Smartbda23252008-04-24 12:12:46 -04002019 spin_unlock_irqrestore(shost->host_lock, flags);
2020}
2021
2022/*
2023 * Called via sysfs scan routines. Necessary, as the FC transport
2024 * wants to place all target objects below the rport object. So this
2025 * routine must invoke the scsi_scan_target() routine with the rport
2026 * object as the parent.
2027 */
2028static int
2029fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2030{
2031 uint chlo, chhi;
2032 uint tgtlo, tgthi;
2033
2034 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2035 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2036 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2037 return -EINVAL;
2038
2039 if (channel == SCAN_WILD_CARD) {
2040 chlo = 0;
2041 chhi = shost->max_channel + 1;
2042 } else {
2043 chlo = channel;
2044 chhi = channel + 1;
2045 }
2046
2047 if (id == SCAN_WILD_CARD) {
2048 tgtlo = 0;
2049 tgthi = shost->max_id;
2050 } else {
2051 tgtlo = id;
2052 tgthi = id + 1;
2053 }
2054
2055 for ( ; chlo < chhi; chlo++)
2056 for ( ; tgtlo < tgthi; tgtlo++)
2057 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2058
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002059 return 0;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002060}
2061
FUJITA Tomonori75252362007-09-01 02:02:27 +09002062static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
2063 int result)
2064{
2065 struct fc_internal *i = to_fc_internal(shost->transportt);
2066 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
2067}
2068
2069static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
2070{
2071 struct fc_internal *i = to_fc_internal(shost->transportt);
2072 return i->f->it_nexus_response(shost, nexus, result);
2073}
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075struct scsi_transport_template *
2076fc_attach_transport(struct fc_function_template *ft)
2077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 int count;
Jes Sorensen24669f752006-01-16 10:31:18 -05002079 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2080 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 if (unlikely(!i))
2083 return NULL;
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2086 i->t.target_attrs.ac.class = &fc_transport_class.class;
2087 i->t.target_attrs.ac.match = fc_target_match;
2088 i->t.target_size = sizeof(struct fc_starget_attrs);
2089 transport_container_register(&i->t.target_attrs);
2090
2091 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2092 i->t.host_attrs.ac.class = &fc_host_class.class;
2093 i->t.host_attrs.ac.match = fc_host_match;
2094 i->t.host_size = sizeof(struct fc_host_attrs);
2095 if (ft->get_fc_host_stats)
2096 i->t.host_attrs.statistics = &fc_statistics_group;
2097 transport_container_register(&i->t.host_attrs);
2098
2099 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2100 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2101 i->rport_attr_cont.ac.match = fc_rport_match;
2102 transport_container_register(&i->rport_attr_cont);
2103
James Smarta53eb5e2007-04-27 12:41:09 -04002104 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2105 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2106 i->vport_attr_cont.ac.match = fc_vport_match;
2107 transport_container_register(&i->vport_attr_cont);
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 i->f = ft;
2110
2111 /* Transport uses the shost workq for scsi scanning */
2112 i->t.create_work_queue = 1;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002113
James Smartc829c392006-03-13 08:28:57 -05002114 i->t.eh_timed_out = fc_timed_out;
2115
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002116 i->t.user_scan = fc_user_scan;
James Smart9ef3e4a2007-05-24 19:04:44 -04002117
FUJITA Tomonori75252362007-09-01 02:02:27 +09002118 /* target-mode drivers' functions */
2119 i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2120 i->t.it_nexus_response = fc_it_nexus_response;
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /*
2123 * Setup SCSI Target Attributes.
2124 */
2125 count = 0;
2126 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2127 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2128 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2129
2130 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2131
2132 i->starget_attrs[count] = NULL;
2133
2134
2135 /*
2136 * Setup SCSI Host Attributes.
2137 */
2138 count=0;
2139 SETUP_HOST_ATTRIBUTE_RD(node_name);
2140 SETUP_HOST_ATTRIBUTE_RD(port_name);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01002141 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2143 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2145 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
James Smarta53eb5e2007-04-27 12:41:09 -04002146 if (ft->vport_create) {
2147 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2148 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2151
2152 SETUP_HOST_ATTRIBUTE_RD(port_id);
2153 SETUP_HOST_ATTRIBUTE_RD(port_type);
2154 SETUP_HOST_ATTRIBUTE_RD(port_state);
2155 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2156 SETUP_HOST_ATTRIBUTE_RD(speed);
2157 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
James Smart016131b2006-08-14 08:20:25 -04002158 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
James Smartb8d08212006-08-17 08:00:43 -04002159 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 /* Transport-managed attributes */
2162 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002163 if (ft->issue_fc_host_lip)
2164 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
James Smarta53eb5e2007-04-27 12:41:09 -04002165 if (ft->vport_create)
2166 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2167 if (ft->vport_delete)
2168 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 BUG_ON(count > FC_HOST_NUM_ATTRS);
2171
2172 i->host_attrs[count] = NULL;
2173
2174 /*
2175 * Setup Remote Port Attributes.
2176 */
2177 count=0;
2178 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2179 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2180 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2181 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2182 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2183 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2184 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2185 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2186 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
Mike Christiefff9d402008-08-19 18:45:23 -05002187 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2190
2191 i->rport_attrs[count] = NULL;
2192
James Smarta53eb5e2007-04-27 12:41:09 -04002193 /*
2194 * Setup Virtual Port Attributes.
2195 */
2196 count=0;
2197 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2198 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2199 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2200 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2201 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2202 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2203 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2204 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2205 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2206
2207 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2208
2209 i->vport_attrs[count] = NULL;
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return &i->t;
2212}
2213EXPORT_SYMBOL(fc_attach_transport);
2214
2215void fc_release_transport(struct scsi_transport_template *t)
2216{
2217 struct fc_internal *i = to_fc_internal(t);
2218
2219 transport_container_unregister(&i->t.target_attrs);
2220 transport_container_unregister(&i->t.host_attrs);
2221 transport_container_unregister(&i->rport_attr_cont);
James Smarta53eb5e2007-04-27 12:41:09 -04002222 transport_container_unregister(&i->vport_attr_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 kfree(i);
2225}
2226EXPORT_SYMBOL(fc_release_transport);
2227
James Smartaedf3492006-04-10 10:14:05 -04002228/**
2229 * fc_queue_work - Queue work to the fc_host workqueue.
2230 * @shost: Pointer to Scsi_Host bound to fc_host.
2231 * @work: Work to queue for execution.
2232 *
2233 * Return value:
James Smarta0785ed2006-05-11 13:27:09 -04002234 * 1 - work queued for execution
2235 * 0 - work is already queued
2236 * -EINVAL - work queue doesn't exist
Rob Landleyeb448202007-11-03 13:30:39 -05002237 */
James Smartaedf3492006-04-10 10:14:05 -04002238static int
2239fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2240{
2241 if (unlikely(!fc_host_work_q(shost))) {
2242 printk(KERN_ERR
2243 "ERROR: FC host '%s' attempted to queue work, "
2244 "when no workqueue created.\n", shost->hostt->name);
2245 dump_stack();
2246
2247 return -EINVAL;
2248 }
2249
2250 return queue_work(fc_host_work_q(shost), work);
2251}
2252
2253/**
2254 * fc_flush_work - Flush a fc_host's workqueue.
2255 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002256 */
James Smartaedf3492006-04-10 10:14:05 -04002257static void
2258fc_flush_work(struct Scsi_Host *shost)
2259{
2260 if (!fc_host_work_q(shost)) {
2261 printk(KERN_ERR
2262 "ERROR: FC host '%s' attempted to flush work, "
2263 "when no workqueue created.\n", shost->hostt->name);
2264 dump_stack();
2265 return;
2266 }
2267
2268 flush_workqueue(fc_host_work_q(shost));
2269}
2270
2271/**
2272 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2273 * @shost: Pointer to Scsi_Host bound to fc_host.
2274 * @work: Work to queue for execution.
2275 * @delay: jiffies to delay the work queuing
2276 *
2277 * Return value:
James Smart0f29b962006-08-18 17:33:29 -04002278 * 1 on success / 0 already queued / < 0 for error
Rob Landleyeb448202007-11-03 13:30:39 -05002279 */
James Smartaedf3492006-04-10 10:14:05 -04002280static int
David Howellsc4028952006-11-22 14:57:56 +00002281fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
James Smartaedf3492006-04-10 10:14:05 -04002282 unsigned long delay)
2283{
2284 if (unlikely(!fc_host_devloss_work_q(shost))) {
2285 printk(KERN_ERR
2286 "ERROR: FC host '%s' attempted to queue work, "
2287 "when no workqueue created.\n", shost->hostt->name);
2288 dump_stack();
2289
2290 return -EINVAL;
2291 }
2292
2293 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2294}
2295
2296/**
2297 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2298 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002299 */
James Smartaedf3492006-04-10 10:14:05 -04002300static void
2301fc_flush_devloss(struct Scsi_Host *shost)
2302{
2303 if (!fc_host_devloss_work_q(shost)) {
2304 printk(KERN_ERR
2305 "ERROR: FC host '%s' attempted to flush work, "
2306 "when no workqueue created.\n", shost->hostt->name);
2307 dump_stack();
2308 return;
2309 }
2310
2311 flush_workqueue(fc_host_devloss_work_q(shost));
2312}
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315/**
Rob Landleyeb448202007-11-03 13:30:39 -05002316 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2317 * @shost: Which &Scsi_Host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 *
2319 * This routine is expected to be called immediately preceeding the
2320 * a driver's call to scsi_remove_host().
2321 *
2322 * WARNING: A driver utilizing the fc_transport, which fails to call
Rob Landleyeb448202007-11-03 13:30:39 -05002323 * this routine prior to scsi_remove_host(), will leave dangling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 * objects in /sys/class/fc_remote_ports. Access to any of these
2325 * objects can result in a system crash !!!
2326 *
2327 * Notes:
2328 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002329 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330void
2331fc_remove_host(struct Scsi_Host *shost)
2332{
James Smarta53eb5e2007-04-27 12:41:09 -04002333 struct fc_vport *vport = NULL, *next_vport = NULL;
2334 struct fc_rport *rport = NULL, *next_rport = NULL;
James Smartaedf3492006-04-10 10:14:05 -04002335 struct workqueue_struct *work_q;
2336 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James Smarta53eb5e2007-04-27 12:41:09 -04002337 unsigned long flags;
James Smarta53eb5e2007-04-27 12:41:09 -04002338
2339 spin_lock_irqsave(shost->host_lock, flags);
2340
2341 /* Remove any vports */
James Smart9ef3e4a2007-05-24 19:04:44 -04002342 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2343 fc_queue_work(shost, &vport->vport_delete_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 /* Remove any remote ports */
2346 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002347 &fc_host->rports, peers) {
2348 list_del(&rport->peers);
2349 rport->port_state = FC_PORTSTATE_DELETED;
2350 fc_queue_work(shost, &rport->rport_delete_work);
2351 }
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002354 &fc_host->rport_bindings, peers) {
2355 list_del(&rport->peers);
2356 rport->port_state = FC_PORTSTATE_DELETED;
2357 fc_queue_work(shost, &rport->rport_delete_work);
2358 }
2359
James Smarta53eb5e2007-04-27 12:41:09 -04002360 spin_unlock_irqrestore(shost->host_lock, flags);
2361
James Smartaedf3492006-04-10 10:14:05 -04002362 /* flush all scan work items */
2363 scsi_flush_work(shost);
2364
2365 /* flush all stgt delete, and rport delete work items, then kill it */
2366 if (fc_host->work_q) {
2367 work_q = fc_host->work_q;
2368 fc_host->work_q = NULL;
2369 destroy_workqueue(work_q);
2370 }
2371
2372 /* flush all devloss work items, then kill it */
2373 if (fc_host->devloss_work_q) {
2374 work_q = fc_host->devloss_work_q;
2375 fc_host->devloss_work_q = NULL;
2376 destroy_workqueue(work_q);
2377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379EXPORT_SYMBOL(fc_remove_host);
2380
Mike Christiefff9d402008-08-19 18:45:23 -05002381static void fc_terminate_rport_io(struct fc_rport *rport)
2382{
2383 struct Scsi_Host *shost = rport_to_shost(rport);
2384 struct fc_internal *i = to_fc_internal(shost->transportt);
2385
2386 /* Involve the LLDD if possible to terminate all io on the rport. */
2387 if (i->f->terminate_rport_io)
2388 i->f->terminate_rport_io(rport);
2389
2390 /*
2391 * must unblock to flush queued IO. The caller will have set
2392 * the port_state or flags, so that fc_remote_port_chkready will
2393 * fail IO.
2394 */
2395 scsi_target_unblock(&rport->dev);
2396}
James Smartaedf3492006-04-10 10:14:05 -04002397
2398/**
2399 * fc_starget_delete - called to delete the scsi decendents of an rport
David Howellsc4028952006-11-22 14:57:56 +00002400 * @work: remote port to be operated on.
Rob Landleyeb448202007-11-03 13:30:39 -05002401 *
2402 * Deletes target and all sdevs.
2403 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002404static void
David Howellsc4028952006-11-22 14:57:56 +00002405fc_starget_delete(struct work_struct *work)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002406{
David Howellsc4028952006-11-22 14:57:56 +00002407 struct fc_rport *rport =
2408 container_of(work, struct fc_rport, stgt_delete_work);
James Smart0f29b962006-08-18 17:33:29 -04002409
Mike Christiefff9d402008-08-19 18:45:23 -05002410 fc_terminate_rport_io(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002411 scsi_remove_target(&rport->dev);
2412}
2413
James Smartaedf3492006-04-10 10:14:05 -04002414
2415/**
2416 * fc_rport_final_delete - finish rport termination and delete it.
David Howellsc4028952006-11-22 14:57:56 +00002417 * @work: remote port to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05002418 */
James Smartaedf3492006-04-10 10:14:05 -04002419static void
David Howellsc4028952006-11-22 14:57:56 +00002420fc_rport_final_delete(struct work_struct *work)
James Smartaedf3492006-04-10 10:14:05 -04002421{
David Howellsc4028952006-11-22 14:57:56 +00002422 struct fc_rport *rport =
2423 container_of(work, struct fc_rport, rport_delete_work);
James Smartaedf3492006-04-10 10:14:05 -04002424 struct device *dev = &rport->dev;
2425 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002426 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart92740b22007-04-27 11:53:17 -04002427 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05002428 int do_callback = 0;
James Smartaedf3492006-04-10 10:14:05 -04002429
2430 /*
James Smart9ef3e4a2007-05-24 19:04:44 -04002431 * if a scan is pending, flush the SCSI Host work_q so that
James Smartaedf3492006-04-10 10:14:05 -04002432 * that we can reclaim the rport scan work element.
2433 */
2434 if (rport->flags & FC_RPORT_SCAN_PENDING)
2435 scsi_flush_work(shost);
2436
Mike Christiefff9d402008-08-19 18:45:23 -05002437 fc_terminate_rport_io(rport);
James Smart9e4f5e22009-03-26 13:33:19 -04002438
James Smart92740b22007-04-27 11:53:17 -04002439 /*
2440 * Cancel any outstanding timers. These should really exist
2441 * only when rmmod'ing the LLDD and we're asking for
2442 * immediate termination of the rports
2443 */
2444 spin_lock_irqsave(shost->host_lock, flags);
2445 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2446 spin_unlock_irqrestore(shost->host_lock, flags);
2447 if (!cancel_delayed_work(&rport->fail_io_work))
2448 fc_flush_devloss(shost);
2449 if (!cancel_delayed_work(&rport->dev_loss_work))
2450 fc_flush_devloss(shost);
2451 spin_lock_irqsave(shost->host_lock, flags);
2452 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2453 }
2454 spin_unlock_irqrestore(shost->host_lock, flags);
2455
James Smart0f29b962006-08-18 17:33:29 -04002456 /* Delete SCSI target and sdevs */
2457 if (rport->scsi_target_id != -1)
David Howellsc4028952006-11-22 14:57:56 +00002458 fc_starget_delete(&rport->stgt_delete_work);
James Smart92740b22007-04-27 11:53:17 -04002459
2460 /*
2461 * Notify the driver that the rport is now dead. The LLDD will
2462 * also guarantee that any communication to the rport is terminated
James Smart4be98c02009-01-05 12:14:18 -05002463 *
2464 * Avoid this call if we already called it when we preserved the
2465 * rport for the binding.
James Smart92740b22007-04-27 11:53:17 -04002466 */
Michael Reed8798a692009-10-09 14:15:59 -05002467 spin_lock_irqsave(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05002468 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
Michael Reed8798a692009-10-09 14:15:59 -05002469 (i->f->dev_loss_tmo_callbk)) {
2470 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2471 do_callback = 1;
2472 }
2473 spin_unlock_irqrestore(shost->host_lock, flags);
2474
2475 if (do_callback)
James Smart0f29b962006-08-18 17:33:29 -04002476 i->f->dev_loss_tmo_callbk(rport);
James Smart0f29b962006-08-18 17:33:29 -04002477
James Smart9e4f5e22009-03-26 13:33:19 -04002478 fc_bsg_remove(rport->rqst_q);
2479
James Smartaedf3492006-04-10 10:14:05 -04002480 transport_remove_device(dev);
2481 device_del(dev);
2482 transport_destroy_device(dev);
James Smart3bdad7b2006-06-26 14:19:59 -04002483 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2484 put_device(dev); /* for self-reference */
James Smartaedf3492006-04-10 10:14:05 -04002485}
2486
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488/**
2489 * fc_rport_create - allocates and creates a remote FC port.
2490 * @shost: scsi host the remote port is connected to.
2491 * @channel: Channel on shost port connected to.
2492 * @ids: The world wide names, fc address, and FC4 port
2493 * roles for the remote port.
2494 *
2495 * Allocates and creates the remoter port structure, including the
2496 * class and sysfs creation.
2497 *
2498 * Notes:
2499 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002500 */
Adrian Bunk44818ef2007-07-09 11:59:59 -07002501static struct fc_rport *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502fc_rport_create(struct Scsi_Host *shost, int channel,
2503 struct fc_rport_identifiers *ids)
2504{
James Smartaedf3492006-04-10 10:14:05 -04002505 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 struct fc_internal *fci = to_fc_internal(shost->transportt);
2507 struct fc_rport *rport;
2508 struct device *dev;
2509 unsigned long flags;
2510 int error;
2511 size_t size;
2512
2513 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
Jes Sorensen24669f752006-01-16 10:31:18 -05002514 rport = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (unlikely(!rport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002516 printk(KERN_ERR "%s: allocation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return NULL;
2518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 rport->maxframe_size = -1;
2521 rport->supported_classes = FC_COS_UNSPECIFIED;
2522 rport->dev_loss_tmo = fc_dev_loss_tmo;
2523 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2524 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2525 rport->port_id = ids->port_id;
2526 rport->roles = ids->roles;
2527 rport->port_state = FC_PORTSTATE_ONLINE;
2528 if (fci->f->dd_fcrport_size)
2529 rport->dd_data = &rport[1];
2530 rport->channel = channel;
James Smart0f29b962006-08-18 17:33:29 -04002531 rport->fast_io_fail_tmo = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
David Howellsc4028952006-11-22 14:57:56 +00002533 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2534 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2535 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2536 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2537 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 spin_lock_irqsave(shost->host_lock, flags);
2540
2541 rport->number = fc_host->next_rport_number++;
James Smarta53eb5e2007-04-27 12:41:09 -04002542 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 rport->scsi_target_id = fc_host->next_target_id++;
2544 else
2545 rport->scsi_target_id = -1;
James Smartaedf3492006-04-10 10:14:05 -04002546 list_add_tail(&rport->peers, &fc_host->rports);
James Smart3bdad7b2006-06-26 14:19:59 -04002547 get_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 spin_unlock_irqrestore(shost->host_lock, flags);
2550
2551 dev = &rport->dev;
James Smart3bdad7b2006-06-26 14:19:59 -04002552 device_initialize(dev); /* takes self reference */
2553 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 dev->release = fc_rport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01002555 dev_set_name(dev, "rport-%d:%d-%d",
2556 shost->host_no, channel, rport->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 transport_setup_device(dev);
2558
2559 error = device_add(dev);
2560 if (error) {
2561 printk(KERN_ERR "FC Remote Port device_add failed\n");
2562 goto delete_rport;
2563 }
2564 transport_add_device(dev);
2565 transport_configure_device(dev);
2566
James Smart9e4f5e22009-03-26 13:33:19 -04002567 fc_bsg_rportadd(shost, rport);
2568 /* ignore any bsg add error - we just can't do sgio */
2569
James Smarta53eb5e2007-04-27 12:41:09 -04002570 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002572 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 return rport;
2577
2578delete_rport:
2579 transport_destroy_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 spin_lock_irqsave(shost->host_lock, flags);
2581 list_del(&rport->peers);
James Smart3bdad7b2006-06-26 14:19:59 -04002582 put_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 spin_unlock_irqrestore(shost->host_lock, flags);
2584 put_device(dev->parent);
2585 kfree(rport);
2586 return NULL;
2587}
2588
2589/**
Rob Landleyeb448202007-11-03 13:30:39 -05002590 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 * @shost: scsi host the remote port is connected to.
2592 * @channel: Channel on shost port connected to.
2593 * @ids: The world wide names, fc address, and FC4 port
2594 * roles for the remote port.
2595 *
2596 * The LLDD calls this routine to notify the transport of the existence
2597 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2598 * of the port, it's FC address (port_id), and the FC4 roles that are
2599 * active for the port.
2600 *
2601 * For ports that are FCP targets (aka scsi targets), the FC transport
2602 * maintains consistent target id bindings on behalf of the LLDD.
2603 * A consistent target id binding is an assignment of a target id to
2604 * a remote port identifier, which persists while the scsi host is
2605 * attached. The remote port can disappear, then later reappear, and
2606 * it's target id assignment remains the same. This allows for shifts
2607 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2608 * changes to the scsi subsystem which is based on scsi host number and
2609 * target id values. Bindings are only valid during the attachment of
2610 * the scsi host. If the host detaches, then later re-attaches, target
2611 * id bindings may change.
2612 *
2613 * This routine is responsible for returning a remote port structure.
2614 * The routine will search the list of remote ports it maintains
2615 * internally on behalf of consistent target id mappings. If found, the
2616 * remote port structure will be reused. Otherwise, a new remote port
2617 * structure will be allocated.
2618 *
2619 * Whenever a remote port is allocated, a new fc_remote_port class
2620 * device is created.
2621 *
2622 * Should not be called from interrupt context.
2623 *
2624 * Notes:
2625 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002626 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627struct fc_rport *
2628fc_remote_port_add(struct Scsi_Host *shost, int channel,
2629 struct fc_rport_identifiers *ids)
2630{
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002631 struct fc_internal *fci = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04002632 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 struct fc_rport *rport;
2634 unsigned long flags;
2635 int match = 0;
2636
James Smartaedf3492006-04-10 10:14:05 -04002637 /* ensure any stgt delete functions are done */
2638 fc_flush_work(shost);
2639
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002640 /*
2641 * Search the list of "active" rports, for an rport that has been
2642 * deleted, but we've held off the real delete while the target
2643 * is in a "blocked" state.
2644 */
2645 spin_lock_irqsave(shost->host_lock, flags);
2646
James Smartaedf3492006-04-10 10:14:05 -04002647 list_for_each_entry(rport, &fc_host->rports, peers) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002648
2649 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2650 (rport->channel == channel)) {
2651
James Smartaedf3492006-04-10 10:14:05 -04002652 switch (fc_host->tgtid_bind_type) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002653 case FC_TGTID_BIND_BY_WWPN:
2654 case FC_TGTID_BIND_NONE:
2655 if (rport->port_name == ids->port_name)
2656 match = 1;
2657 break;
2658 case FC_TGTID_BIND_BY_WWNN:
2659 if (rport->node_name == ids->node_name)
2660 match = 1;
2661 break;
2662 case FC_TGTID_BIND_BY_ID:
2663 if (rport->port_id == ids->port_id)
2664 match = 1;
2665 break;
2666 }
2667
2668 if (match) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002669
2670 memcpy(&rport->node_name, &ids->node_name,
2671 sizeof(rport->node_name));
2672 memcpy(&rport->port_name, &ids->port_name,
2673 sizeof(rport->port_name));
2674 rport->port_id = ids->port_id;
2675
2676 rport->port_state = FC_PORTSTATE_ONLINE;
2677 rport->roles = ids->roles;
2678
2679 spin_unlock_irqrestore(shost->host_lock, flags);
2680
2681 if (fci->f->dd_fcrport_size)
2682 memset(rport->dd_data, 0,
2683 fci->f->dd_fcrport_size);
2684
2685 /*
James Smart92740b22007-04-27 11:53:17 -04002686 * If we were not a target, cancel the
2687 * io terminate and rport timers, and
2688 * we're done.
2689 *
2690 * If we were a target, but our new role
2691 * doesn't indicate a target, leave the
2692 * timers running expecting the role to
2693 * change as the target fully logs in. If
2694 * it doesn't, the target will be torn down.
2695 *
2696 * If we were a target, and our role shows
2697 * we're still a target, cancel the timers
2698 * and kick off a scan.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002699 */
James Smart92740b22007-04-27 11:53:17 -04002700
2701 /* was a target, not in roles */
2702 if ((rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04002703 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002704 return rport;
2705
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002706 /*
James Smart92740b22007-04-27 11:53:17 -04002707 * Stop the fail io and dev_loss timers.
2708 * If they flush, the port_state will
2709 * be checked and will NOOP the function.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002710 */
James Smart0f29b962006-08-18 17:33:29 -04002711 if (!cancel_delayed_work(&rport->fail_io_work))
2712 fc_flush_devloss(shost);
James Smart92740b22007-04-27 11:53:17 -04002713 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002714 fc_flush_devloss(shost);
2715
2716 spin_lock_irqsave(shost->host_lock, flags);
2717
Mike Christiefff9d402008-08-19 18:45:23 -05002718 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
James Smart4be98c02009-01-05 12:14:18 -05002719 FC_RPORT_DEVLOSS_PENDING |
2720 FC_RPORT_DEVLOSS_CALLBK_DONE);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002721
James Smart92740b22007-04-27 11:53:17 -04002722 /* if target, initiate a scan */
2723 if (rport->scsi_target_id != -1) {
2724 rport->flags |= FC_RPORT_SCAN_PENDING;
2725 scsi_queue_work(shost,
2726 &rport->scan_work);
2727 spin_unlock_irqrestore(shost->host_lock,
2728 flags);
2729 scsi_target_unblock(&rport->dev);
2730 } else
2731 spin_unlock_irqrestore(shost->host_lock,
2732 flags);
James Smarta0785ed2006-05-11 13:27:09 -04002733
James Smart9e4f5e22009-03-26 13:33:19 -04002734 fc_bsg_goose_queue(rport);
2735
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002736 return rport;
2737 }
2738 }
2739 }
2740
James Smart92740b22007-04-27 11:53:17 -04002741 /*
2742 * Search the bindings array
2743 * Note: if never a FCP target, you won't be on this list
2744 */
James Smartaedf3492006-04-10 10:14:05 -04002745 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
2747 /* search for a matching consistent binding */
2748
James Smartaedf3492006-04-10 10:14:05 -04002749 list_for_each_entry(rport, &fc_host->rport_bindings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 peers) {
2751 if (rport->channel != channel)
2752 continue;
2753
James Smartaedf3492006-04-10 10:14:05 -04002754 switch (fc_host->tgtid_bind_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 case FC_TGTID_BIND_BY_WWPN:
2756 if (rport->port_name == ids->port_name)
2757 match = 1;
2758 break;
2759 case FC_TGTID_BIND_BY_WWNN:
2760 if (rport->node_name == ids->node_name)
2761 match = 1;
2762 break;
2763 case FC_TGTID_BIND_BY_ID:
2764 if (rport->port_id == ids->port_id)
2765 match = 1;
2766 break;
2767 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2768 break;
2769 }
2770
2771 if (match) {
James Smartaedf3492006-04-10 10:14:05 -04002772 list_move_tail(&rport->peers, &fc_host->rports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 break;
2774 }
2775 }
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 if (match) {
2778 memcpy(&rport->node_name, &ids->node_name,
2779 sizeof(rport->node_name));
2780 memcpy(&rport->port_name, &ids->port_name,
2781 sizeof(rport->port_name));
2782 rport->port_id = ids->port_id;
2783 rport->roles = ids->roles;
2784 rport->port_state = FC_PORTSTATE_ONLINE;
Mike Christiefff9d402008-08-19 18:45:23 -05002785 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002787 if (fci->f->dd_fcrport_size)
2788 memset(rport->dd_data, 0,
2789 fci->f->dd_fcrport_size);
2790
James Smarta53eb5e2007-04-27 12:41:09 -04002791 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002793 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 scsi_queue_work(shost, &rport->scan_work);
James Smarta0785ed2006-05-11 13:27:09 -04002795 spin_unlock_irqrestore(shost->host_lock, flags);
2796 scsi_target_unblock(&rport->dev);
2797 } else
2798 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 return rport;
2801 }
2802 }
2803
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002804 spin_unlock_irqrestore(shost->host_lock, flags);
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 /* No consistent binding found - create new remote port entry */
2807 rport = fc_rport_create(shost, channel, ids);
2808
2809 return rport;
2810}
2811EXPORT_SYMBOL(fc_remote_port_add);
2812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814/**
Rob Landleyeb448202007-11-03 13:30:39 -05002815 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 * @rport: The remote port that no longer exists
2817 *
2818 * The LLDD calls this routine to notify the transport that a remote
2819 * port is no longer part of the topology. Note: Although a port
2820 * may no longer be part of the topology, it may persist in the remote
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002821 * ports displayed by the fc_host. We do this under 2 conditions:
Rob Landleyeb448202007-11-03 13:30:39 -05002822 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002823 * This allows the port to temporarily disappear, then reappear without
2824 * disrupting the SCSI device tree attached to it. During the "blocked"
2825 * period the port will still exist.
Rob Landleyeb448202007-11-03 13:30:39 -05002826 * 2) If the port was a scsi target and disappears for longer than we
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002827 * expect, we'll delete the port and the tear down the SCSI device tree
2828 * attached to it. However, we want to semi-persist the target id assigned
2829 * to that port if it eventually does exist. The port structure will
2830 * remain (although with minimal information) so that the target id
2831 * bindings remails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 *
2833 * If the remote port is not an FCP Target, it will be fully torn down
2834 * and deallocated, including the fc_remote_port class device.
2835 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002836 * If the remote port is an FCP Target, the port will be placed in a
2837 * temporary blocked state. From the LLDD's perspective, the rport no
2838 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2839 * exists, but all sdevs on it are blocked from further I/O. The following
Rob Landleyeb448202007-11-03 13:30:39 -05002840 * is then expected.
2841 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002842 * If the remote port does not return (signaled by a LLDD call to
2843 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2844 * scsi target is removed - killing all outstanding i/o and removing the
2845 * scsi devices attached ot it. The port structure will be marked Not
2846 * Present and be partially cleared, leaving only enough information to
2847 * recognize the remote port relative to the scsi target id binding if
2848 * it later appears. The port will remain as long as there is a valid
2849 * binding (e.g. until the user changes the binding type or unloads the
2850 * scsi host with the binding).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002852 * If the remote port returns within the dev_loss_tmo value (and matches
2853 * according to the target id binding type), the port structure will be
2854 * reused. If it is no longer a SCSI target, the target will be torn
2855 * down. If it continues to be a SCSI target, then the target will be
2856 * unblocked (allowing i/o to be resumed), and a scan will be activated
2857 * to ensure that all luns are detected.
2858 *
2859 * Called from normal process context only - cannot be called from interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 *
2861 * Notes:
2862 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864void
2865fc_remote_port_delete(struct fc_rport *rport)
2866{
James Smartaedf3492006-04-10 10:14:05 -04002867 struct Scsi_Host *shost = rport_to_shost(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002868 int timeout = rport->dev_loss_tmo;
James Smartaedf3492006-04-10 10:14:05 -04002869 unsigned long flags;
2870
2871 /*
2872 * No need to flush the fc_host work_q's, as all adds are synchronous.
2873 *
2874 * We do need to reclaim the rport scan work element, so eventually
2875 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2876 * there's still a scan pending.
2877 */
2878
2879 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
James Smart92740b22007-04-27 11:53:17 -04002881 if (rport->port_state != FC_PORTSTATE_ONLINE) {
James Smartaedf3492006-04-10 10:14:05 -04002882 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return;
2884 }
2885
James Smart92740b22007-04-27 11:53:17 -04002886 /*
2887 * In the past, we if this was not an FCP-Target, we would
2888 * unconditionally just jump to deleting the rport.
2889 * However, rports can be used as node containers by the LLDD,
2890 * and its not appropriate to just terminate the rport at the
2891 * first sign of a loss in connectivity. The LLDD may want to
2892 * send ELS traffic to re-validate the login. If the rport is
2893 * immediately deleted, it makes it inappropriate for a node
2894 * container.
2895 * So... we now unconditionally wait dev_loss_tmo before
2896 * destroying an rport.
2897 */
2898
James Smartaedf3492006-04-10 10:14:05 -04002899 rport->port_state = FC_PORTSTATE_BLOCKED;
2900
2901 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2902
2903 spin_unlock_irqrestore(shost->host_lock, flags);
2904
FUJITA Tomonori75252362007-09-01 02:02:27 +09002905 if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
2906 shost->active_mode & MODE_TARGET)
2907 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
2908
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002909 scsi_target_block(&rport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
James Smart0f29b962006-08-18 17:33:29 -04002911 /* see if we need to kill io faster than waiting for device loss */
2912 if ((rport->fast_io_fail_tmo != -1) &&
Mike Christiefff9d402008-08-19 18:45:23 -05002913 (rport->fast_io_fail_tmo < timeout))
James Smart0f29b962006-08-18 17:33:29 -04002914 fc_queue_devloss_work(shost, &rport->fail_io_work,
2915 rport->fast_io_fail_tmo * HZ);
2916
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002917 /* cap the length the devices can be blocked until they are deleted */
James Smartaedf3492006-04-10 10:14:05 -04002918 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919}
2920EXPORT_SYMBOL(fc_remote_port_delete);
2921
2922/**
Rob Landleyeb448202007-11-03 13:30:39 -05002923 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 * @rport: The remote port that changed.
Rob Landleyeb448202007-11-03 13:30:39 -05002925 * @roles: New roles for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 *
Rob Landleyeb448202007-11-03 13:30:39 -05002927 * Description: The LLDD calls this routine to notify the transport that the
2928 * roles on a remote port may have changed. The largest effect of this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 * if a port now becomes a FCP Target, it must be allocated a
2930 * scsi target id. If the port is no longer a FCP target, any
2931 * scsi target id value assigned to it will persist in case the
2932 * role changes back to include FCP Target. No changes in the scsi
2933 * midlayer will be invoked if the role changes (in the expectation
2934 * that the role will be resumed. If it doesn't normal error processing
2935 * will take place).
2936 *
2937 * Should not be called from interrupt context.
2938 *
2939 * Notes:
2940 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002941 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942void
2943fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2944{
2945 struct Scsi_Host *shost = rport_to_shost(rport);
James Smartaedf3492006-04-10 10:14:05 -04002946 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 unsigned long flags;
2948 int create = 0;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002949 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 spin_lock_irqsave(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04002952 if (roles & FC_PORT_ROLE_FCP_TARGET) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002953 if (rport->scsi_target_id == -1) {
2954 rport->scsi_target_id = fc_host->next_target_id++;
2955 create = 1;
James Smarta53eb5e2007-04-27 12:41:09 -04002956 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002957 create = 1;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002958 } else if (shost->active_mode & MODE_TARGET) {
2959 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
2960 (char *)&rport->node_name);
2961 if (ret)
2962 printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
2963 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002966 rport->roles = roles;
2967
James Smartaedf3492006-04-10 10:14:05 -04002968 spin_unlock_irqrestore(shost->host_lock, flags);
2969
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002970 if (create) {
2971 /*
2972 * There may have been a delete timer running on the
2973 * port. Ensure that it is cancelled as we now know
2974 * the port is an FCP Target.
2975 * Note: we know the rport is exists and in an online
2976 * state as the LLDD would not have had an rport
2977 * reference to pass us.
2978 *
2979 * Take no action on the del_timer failure as the state
2980 * machine state change will validate the
2981 * transaction.
2982 */
James Smart0f29b962006-08-18 17:33:29 -04002983 if (!cancel_delayed_work(&rport->fail_io_work))
2984 fc_flush_devloss(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002985 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002986 fc_flush_devloss(shost);
2987
2988 spin_lock_irqsave(shost->host_lock, flags);
Mike Christiefff9d402008-08-19 18:45:23 -05002989 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2990 FC_RPORT_DEVLOSS_PENDING);
James Smartaedf3492006-04-10 10:14:05 -04002991 spin_unlock_irqrestore(shost->host_lock, flags);
2992
2993 /* ensure any stgt delete functions are done */
2994 fc_flush_work(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002997 spin_lock_irqsave(shost->host_lock, flags);
2998 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003000 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04003001 scsi_target_unblock(&rport->dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004EXPORT_SYMBOL(fc_remote_port_rolechg);
3005
3006/**
Rob Landleyeb448202007-11-03 13:30:39 -05003007 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
James Smart92740b22007-04-27 11:53:17 -04003008 * @work: rport target that failed to reappear in the allotted time.
Rob Landleyeb448202007-11-03 13:30:39 -05003009 *
3010 * Description: An attempt to delete a remote port blocks, and if it fails
3011 * to return in the allotted time this gets called.
3012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013static void
David Howellsc4028952006-11-22 14:57:56 +00003014fc_timeout_deleted_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
David Howellsc4028952006-11-22 14:57:56 +00003016 struct fc_rport *rport =
3017 container_of(work, struct fc_rport, dev_loss_work.work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003018 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart4be98c02009-01-05 12:14:18 -05003019 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04003020 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003021 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05003022 int do_callback = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003024 spin_lock_irqsave(shost->host_lock, flags);
3025
James Smartaedf3492006-04-10 10:14:05 -04003026 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3027
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003028 /*
James Smart92740b22007-04-27 11:53:17 -04003029 * If the port is ONLINE, then it came back. If it was a SCSI
3030 * target, validate it still is. If not, tear down the
3031 * scsi_target on it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003032 */
James Smartaedf3492006-04-10 10:14:05 -04003033 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
James Smart92740b22007-04-27 11:53:17 -04003034 (rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04003035 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003036 dev_printk(KERN_ERR, &rport->dev,
James Smartaedf3492006-04-10 10:14:05 -04003037 "blocked FC remote port time out: no longer"
3038 " a FCP target, removing starget\n");
James Smartaedf3492006-04-10 10:14:05 -04003039 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04003040 scsi_target_unblock(&rport->dev);
3041 fc_queue_work(shost, &rport->stgt_delete_work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003042 return;
3043 }
3044
James Smart92740b22007-04-27 11:53:17 -04003045 /* NOOP state - we're flushing workq's */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003046 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3047 spin_unlock_irqrestore(shost->host_lock, flags);
3048 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003049 "blocked FC remote port time out: leaving"
3050 " rport%s alone\n",
3051 (rport->scsi_target_id != -1) ? " and starget" : "");
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003052 return;
3053 }
3054
James Smart92740b22007-04-27 11:53:17 -04003055 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3056 (rport->scsi_target_id == -1)) {
James Smartaedf3492006-04-10 10:14:05 -04003057 list_del(&rport->peers);
3058 rport->port_state = FC_PORTSTATE_DELETED;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003059 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003060 "blocked FC remote port time out: removing"
3061 " rport%s\n",
3062 (rport->scsi_target_id != -1) ? " and starget" : "");
James Smartaedf3492006-04-10 10:14:05 -04003063 fc_queue_work(shost, &rport->rport_delete_work);
3064 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003065 return;
3066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 dev_printk(KERN_ERR, &rport->dev,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003069 "blocked FC remote port time out: removing target and "
3070 "saving binding\n");
3071
James Smartaedf3492006-04-10 10:14:05 -04003072 list_move_tail(&rport->peers, &fc_host->rport_bindings);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003073
3074 /*
3075 * Note: We do not remove or clear the hostdata area. This allows
3076 * host-specific target data to persist along with the
3077 * scsi_target_id. It's up to the host to manage it's hostdata area.
3078 */
3079
3080 /*
3081 * Reinitialize port attributes that may change if the port comes back.
3082 */
3083 rport->maxframe_size = -1;
3084 rport->supported_classes = FC_COS_UNSPECIFIED;
James Smarta53eb5e2007-04-27 12:41:09 -04003085 rport->roles = FC_PORT_ROLE_UNKNOWN;
James Smartaedf3492006-04-10 10:14:05 -04003086 rport->port_state = FC_PORTSTATE_NOTPRESENT;
Mike Christiefff9d402008-08-19 18:45:23 -05003087 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003088
James Smartf78badb2008-12-05 16:29:59 -06003089 /*
3090 * Pre-emptively kill I/O rather than waiting for the work queue
3091 * item to teardown the starget. (FCOE libFC folks prefer this
3092 * and to have the rport_port_id still set when it's done).
3093 */
3094 spin_unlock_irqrestore(shost->host_lock, flags);
3095 fc_terminate_rport_io(rport);
3096
Michael Reed8798a692009-10-09 14:15:59 -05003097 spin_lock_irqsave(shost->host_lock, flags);
James Smartf78badb2008-12-05 16:29:59 -06003098
Michael Reed8798a692009-10-09 14:15:59 -05003099 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3100
3101 /* remove the identifiers that aren't used in the consisting binding */
3102 switch (fc_host->tgtid_bind_type) {
3103 case FC_TGTID_BIND_BY_WWPN:
3104 rport->node_name = -1;
3105 rport->port_id = -1;
3106 break;
3107 case FC_TGTID_BIND_BY_WWNN:
3108 rport->port_name = -1;
3109 rport->port_id = -1;
3110 break;
3111 case FC_TGTID_BIND_BY_ID:
3112 rport->node_name = -1;
3113 rport->port_name = -1;
3114 break;
3115 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3116 break;
3117 }
3118
3119 /*
3120 * As this only occurs if the remote port (scsi target)
3121 * went away and didn't come back - we'll remove
3122 * all attached scsi devices.
3123 */
3124 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3125 fc_queue_work(shost, &rport->stgt_delete_work);
3126
3127 do_callback = 1;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003128 }
3129
Michael Reed8798a692009-10-09 14:15:59 -05003130 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05003131
3132 /*
3133 * Notify the driver that the rport is now dead. The LLDD will
3134 * also guarantee that any communication to the rport is terminated
3135 *
3136 * Note: we set the CALLBK_DONE flag above to correspond
3137 */
Michael Reed8798a692009-10-09 14:15:59 -05003138 if (do_callback && i->f->dev_loss_tmo_callbk)
James Smart4be98c02009-01-05 12:14:18 -05003139 i->f->dev_loss_tmo_callbk(rport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
James Smart4be98c02009-01-05 12:14:18 -05003142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143/**
Rob Landleyeb448202007-11-03 13:30:39 -05003144 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
David Howellsc4028952006-11-22 14:57:56 +00003145 * @work: rport to terminate io on.
James Smart0f29b962006-08-18 17:33:29 -04003146 *
3147 * Notes: Only requests the failure of the io, not that all are flushed
3148 * prior to returning.
Rob Landleyeb448202007-11-03 13:30:39 -05003149 */
James Smart0f29b962006-08-18 17:33:29 -04003150static void
David Howellsc4028952006-11-22 14:57:56 +00003151fc_timeout_fail_rport_io(struct work_struct *work)
James Smart0f29b962006-08-18 17:33:29 -04003152{
David Howellsc4028952006-11-22 14:57:56 +00003153 struct fc_rport *rport =
3154 container_of(work, struct fc_rport, fail_io_work.work);
James Smart0f29b962006-08-18 17:33:29 -04003155
3156 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3157 return;
3158
Mike Christiefff9d402008-08-19 18:45:23 -05003159 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3160 fc_terminate_rport_io(rport);
James Smart0f29b962006-08-18 17:33:29 -04003161}
3162
3163/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
David Howellsc4028952006-11-22 14:57:56 +00003165 * @work: remote port to be scanned.
Rob Landleyeb448202007-11-03 13:30:39 -05003166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167static void
David Howellsc4028952006-11-22 14:57:56 +00003168fc_scsi_scan_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
David Howellsc4028952006-11-22 14:57:56 +00003170 struct fc_rport *rport =
3171 container_of(work, struct fc_rport, scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003172 struct Scsi_Host *shost = rport_to_shost(rport);
Christof Schmitt03f002f2007-08-28 09:31:21 +02003173 struct fc_internal *i = to_fc_internal(shost->transportt);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003174 unsigned long flags;
3175
James Smartaedf3492006-04-10 10:14:05 -04003176 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
Christof Schmitt03f002f2007-08-28 09:31:21 +02003177 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3178 !(i->f->disable_target_scan)) {
James Smartaedf3492006-04-10 10:14:05 -04003179 scsi_scan_target(&rport->dev, rport->channel,
3180 rport->scsi_target_id, SCAN_WILD_CARD, 1);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003181 }
James Smartaedf3492006-04-10 10:14:05 -04003182
3183 spin_lock_irqsave(shost->host_lock, flags);
3184 rport->flags &= ~FC_RPORT_SCAN_PENDING;
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003185 spin_unlock_irqrestore(shost->host_lock, flags);
3186}
3187
Christof Schmitt65d430f2009-10-30 17:59:29 +01003188/**
3189 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3190 * @cmnd: SCSI command that scsi_eh is trying to recover
3191 *
3192 * This routine can be called from a FC LLD scsi_eh callback. It
3193 * blocks the scsi_eh thread until the fc_rport leaves the
3194 * FC_PORTSTATE_BLOCKED. This is necessary to avoid the scsi_eh
3195 * failing recovery actions for blocked rports which would lead to
3196 * offlined SCSI devices.
3197 */
3198void fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3199{
3200 struct Scsi_Host *shost = cmnd->device->host;
3201 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3202 unsigned long flags;
3203
3204 spin_lock_irqsave(shost->host_lock, flags);
3205 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
3206 spin_unlock_irqrestore(shost->host_lock, flags);
3207 msleep(1000);
3208 spin_lock_irqsave(shost->host_lock, flags);
3209 }
3210 spin_unlock_irqrestore(shost->host_lock, flags);
3211}
3212EXPORT_SYMBOL(fc_block_scsi_eh);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003213
James Smarta53eb5e2007-04-27 12:41:09 -04003214/**
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003215 * fc_vport_setup - allocates and creates a FC virtual port.
James Smarta53eb5e2007-04-27 12:41:09 -04003216 * @shost: scsi host the virtual port is connected to.
3217 * @channel: Channel on shost port connected to.
3218 * @pdev: parent device for vport
3219 * @ids: The world wide names, FC4 port roles, etc for
3220 * the virtual port.
3221 * @ret_vport: The pointer to the created vport.
3222 *
3223 * Allocates and creates the vport structure, calls the parent host
3224 * to instantiate the vport, the completes w/ class and sysfs creation.
3225 *
3226 * Notes:
3227 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003228 */
James Smarta53eb5e2007-04-27 12:41:09 -04003229static int
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003230fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
James Smarta53eb5e2007-04-27 12:41:09 -04003231 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3232{
3233 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3234 struct fc_internal *fci = to_fc_internal(shost->transportt);
3235 struct fc_vport *vport;
3236 struct device *dev;
3237 unsigned long flags;
3238 size_t size;
3239 int error;
3240
3241 *ret_vport = NULL;
3242
3243 if ( ! fci->f->vport_create)
3244 return -ENOENT;
3245
3246 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3247 vport = kzalloc(size, GFP_KERNEL);
3248 if (unlikely(!vport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003249 printk(KERN_ERR "%s: allocation failure\n", __func__);
James Smarta53eb5e2007-04-27 12:41:09 -04003250 return -ENOMEM;
3251 }
3252
3253 vport->vport_state = FC_VPORT_UNKNOWN;
3254 vport->vport_last_state = FC_VPORT_UNKNOWN;
3255 vport->node_name = ids->node_name;
3256 vport->port_name = ids->port_name;
3257 vport->roles = ids->roles;
3258 vport->vport_type = ids->vport_type;
3259 if (fci->f->dd_fcvport_size)
3260 vport->dd_data = &vport[1];
3261 vport->shost = shost;
3262 vport->channel = channel;
3263 vport->flags = FC_VPORT_CREATING;
James Smart9ef3e4a2007-05-24 19:04:44 -04003264 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
James Smarta53eb5e2007-04-27 12:41:09 -04003265
3266 spin_lock_irqsave(shost->host_lock, flags);
3267
3268 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3269 spin_unlock_irqrestore(shost->host_lock, flags);
3270 kfree(vport);
3271 return -ENOSPC;
3272 }
3273 fc_host->npiv_vports_inuse++;
3274 vport->number = fc_host->next_vport_number++;
3275 list_add_tail(&vport->peers, &fc_host->vports);
3276 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3277
3278 spin_unlock_irqrestore(shost->host_lock, flags);
3279
3280 dev = &vport->dev;
3281 device_initialize(dev); /* takes self reference */
3282 dev->parent = get_device(pdev); /* takes parent reference */
3283 dev->release = fc_vport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01003284 dev_set_name(dev, "vport-%d:%d-%d",
3285 shost->host_no, channel, vport->number);
James Smarta53eb5e2007-04-27 12:41:09 -04003286 transport_setup_device(dev);
3287
3288 error = device_add(dev);
3289 if (error) {
3290 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3291 goto delete_vport;
3292 }
3293 transport_add_device(dev);
3294 transport_configure_device(dev);
3295
3296 error = fci->f->vport_create(vport, ids->disable);
3297 if (error) {
3298 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3299 goto delete_vport_all;
3300 }
3301
3302 /*
3303 * if the parent isn't the physical adapter's Scsi_Host, ensure
3304 * the Scsi_Host at least contains ia symlink to the vport.
3305 */
3306 if (pdev != &shost->shost_gendev) {
3307 error = sysfs_create_link(&shost->shost_gendev.kobj,
Kay Sievers71610f52008-12-03 22:41:36 +01003308 &dev->kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003309 if (error)
3310 printk(KERN_ERR
3311 "%s: Cannot create vport symlinks for "
3312 "%s, err=%d\n",
Kay Sievers71610f52008-12-03 22:41:36 +01003313 __func__, dev_name(dev), error);
James Smarta53eb5e2007-04-27 12:41:09 -04003314 }
3315 spin_lock_irqsave(shost->host_lock, flags);
3316 vport->flags &= ~FC_VPORT_CREATING;
3317 spin_unlock_irqrestore(shost->host_lock, flags);
3318
3319 dev_printk(KERN_NOTICE, pdev,
Kay Sievers71610f52008-12-03 22:41:36 +01003320 "%s created via shost%d channel %d\n", dev_name(dev),
James Smarta53eb5e2007-04-27 12:41:09 -04003321 shost->host_no, channel);
3322
3323 *ret_vport = vport;
3324
3325 return 0;
3326
3327delete_vport_all:
3328 transport_remove_device(dev);
3329 device_del(dev);
3330delete_vport:
3331 transport_destroy_device(dev);
3332 spin_lock_irqsave(shost->host_lock, flags);
3333 list_del(&vport->peers);
3334 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3335 fc_host->npiv_vports_inuse--;
3336 spin_unlock_irqrestore(shost->host_lock, flags);
3337 put_device(dev->parent);
3338 kfree(vport);
3339
3340 return error;
3341}
3342
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003343/**
3344 * fc_vport_create - Admin App or LLDD requests creation of a vport
3345 * @shost: scsi host the virtual port is connected to.
3346 * @channel: channel on shost port connected to.
3347 * @ids: The world wide names, FC4 port roles, etc for
3348 * the virtual port.
3349 *
3350 * Notes:
3351 * This routine assumes no locks are held on entry.
3352 */
3353struct fc_vport *
3354fc_vport_create(struct Scsi_Host *shost, int channel,
3355 struct fc_vport_identifiers *ids)
3356{
3357 int stat;
3358 struct fc_vport *vport;
3359
3360 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3361 ids, &vport);
3362 return stat ? NULL : vport;
3363}
3364EXPORT_SYMBOL(fc_vport_create);
James Smarta53eb5e2007-04-27 12:41:09 -04003365
3366/**
3367 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3368 * @vport: fc_vport to be terminated
3369 *
3370 * Calls the LLDD vport_delete() function, then deallocates and removes
3371 * the vport from the shost and object tree.
3372 *
3373 * Notes:
3374 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003375 */
James Smarta53eb5e2007-04-27 12:41:09 -04003376int
3377fc_vport_terminate(struct fc_vport *vport)
3378{
3379 struct Scsi_Host *shost = vport_to_shost(vport);
3380 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3381 struct fc_internal *i = to_fc_internal(shost->transportt);
3382 struct device *dev = &vport->dev;
3383 unsigned long flags;
3384 int stat;
3385
James Smarta53eb5e2007-04-27 12:41:09 -04003386 if (i->f->vport_delete)
3387 stat = i->f->vport_delete(vport);
3388 else
3389 stat = -ENOENT;
3390
3391 spin_lock_irqsave(shost->host_lock, flags);
3392 vport->flags &= ~FC_VPORT_DELETING;
3393 if (!stat) {
3394 vport->flags |= FC_VPORT_DELETED;
3395 list_del(&vport->peers);
3396 fc_host->npiv_vports_inuse--;
3397 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3398 }
3399 spin_unlock_irqrestore(shost->host_lock, flags);
3400
3401 if (stat)
3402 return stat;
3403
3404 if (dev->parent != &shost->shost_gendev)
Kay Sievers71610f52008-12-03 22:41:36 +01003405 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003406 transport_remove_device(dev);
3407 device_del(dev);
3408 transport_destroy_device(dev);
3409
3410 /*
3411 * Removing our self-reference should mean our
3412 * release function gets called, which will drop the remaining
3413 * parent reference and free the data structure.
3414 */
3415 put_device(dev); /* for self-reference */
3416
3417 return 0; /* SUCCESS */
3418}
3419EXPORT_SYMBOL(fc_vport_terminate);
3420
James Smart9ef3e4a2007-05-24 19:04:44 -04003421/**
3422 * fc_vport_sched_delete - workq-based delete request for a vport
James Smart9ef3e4a2007-05-24 19:04:44 -04003423 * @work: vport to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05003424 */
James Smart9ef3e4a2007-05-24 19:04:44 -04003425static void
3426fc_vport_sched_delete(struct work_struct *work)
3427{
3428 struct fc_vport *vport =
3429 container_of(work, struct fc_vport, vport_delete_work);
3430 int stat;
James Smarta53eb5e2007-04-27 12:41:09 -04003431
James Smart9ef3e4a2007-05-24 19:04:44 -04003432 stat = fc_vport_terminate(vport);
3433 if (stat)
3434 dev_printk(KERN_ERR, vport->dev.parent,
3435 "%s: %s could not be deleted created via "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003436 "shost%d channel %d - error %d\n", __func__,
Kay Sievers71610f52008-12-03 22:41:36 +01003437 dev_name(&vport->dev), vport->shost->host_no,
James Smart9ef3e4a2007-05-24 19:04:44 -04003438 vport->channel, stat);
3439}
3440
3441
James Smart9e4f5e22009-03-26 13:33:19 -04003442/*
3443 * BSG support
3444 */
3445
3446
3447/**
3448 * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
3449 * @job: fc_bsg_job that is to be torn down
3450 */
3451static void
3452fc_destroy_bsgjob(struct fc_bsg_job *job)
3453{
3454 unsigned long flags;
3455
3456 spin_lock_irqsave(&job->job_lock, flags);
3457 if (job->ref_cnt) {
3458 spin_unlock_irqrestore(&job->job_lock, flags);
3459 return;
3460 }
3461 spin_unlock_irqrestore(&job->job_lock, flags);
3462
3463 put_device(job->dev); /* release reference for the request */
3464
3465 kfree(job->request_payload.sg_list);
3466 kfree(job->reply_payload.sg_list);
3467 kfree(job);
3468}
3469
James Smart9e4f5e22009-03-26 13:33:19 -04003470/**
3471 * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
3472 * completed
3473 * @job: fc_bsg_job that is complete
3474 */
3475static void
3476fc_bsg_jobdone(struct fc_bsg_job *job)
3477{
3478 struct request *req = job->req;
3479 struct request *rsp = req->next_rq;
James Smart9e4f5e22009-03-26 13:33:19 -04003480 int err;
3481
James Smart9e4f5e22009-03-26 13:33:19 -04003482 err = job->req->errors = job->reply->result;
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003483
James Smart9e4f5e22009-03-26 13:33:19 -04003484 if (err < 0)
3485 /* we're only returning the result field in the reply */
3486 job->req->sense_len = sizeof(uint32_t);
3487 else
3488 job->req->sense_len = job->reply_len;
3489
3490 /* we assume all request payload was transferred, residual == 0 */
3491 req->resid_len = 0;
3492
3493 if (rsp) {
3494 WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
3495
3496 /* set reply (bidi) residual */
3497 rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
3498 rsp->resid_len);
3499 }
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003500 blk_complete_request(req);
James Smart9e4f5e22009-03-26 13:33:19 -04003501}
3502
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003503/**
3504 * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
Randy Dunlapfe5d20c2009-07-04 13:10:41 -07003505 * @rq: BSG request that holds the job to be destroyed
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003506 */
3507static void fc_bsg_softirq_done(struct request *rq)
3508{
3509 struct fc_bsg_job *job = rq->special;
3510 unsigned long flags;
3511
3512 spin_lock_irqsave(&job->job_lock, flags);
3513 job->state_flags |= FC_RQST_STATE_DONE;
3514 job->ref_cnt--;
3515 spin_unlock_irqrestore(&job->job_lock, flags);
3516
3517 blk_end_request_all(rq, rq->errors);
3518 fc_destroy_bsgjob(job);
3519}
James Smart9e4f5e22009-03-26 13:33:19 -04003520
3521/**
3522 * fc_bsg_job_timeout - handler for when a bsg request timesout
3523 * @req: request that timed out
3524 */
3525static enum blk_eh_timer_return
3526fc_bsg_job_timeout(struct request *req)
3527{
3528 struct fc_bsg_job *job = (void *) req->special;
3529 struct Scsi_Host *shost = job->shost;
3530 struct fc_internal *i = to_fc_internal(shost->transportt);
3531 unsigned long flags;
3532 int err = 0, done = 0;
3533
3534 if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
3535 return BLK_EH_RESET_TIMER;
3536
3537 spin_lock_irqsave(&job->job_lock, flags);
3538 if (job->state_flags & FC_RQST_STATE_DONE)
3539 done = 1;
3540 else
3541 job->ref_cnt++;
3542 spin_unlock_irqrestore(&job->job_lock, flags);
3543
3544 if (!done && i->f->bsg_timeout) {
3545 /* call LLDD to abort the i/o as it has timed out */
3546 err = i->f->bsg_timeout(job);
Swen Schilligb8f08642010-01-14 17:19:00 +01003547 if (err == -EAGAIN) {
3548 job->ref_cnt--;
3549 return BLK_EH_RESET_TIMER;
3550 } else if (err)
James Smart9e4f5e22009-03-26 13:33:19 -04003551 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3552 "abort failed with status %d\n", err);
3553 }
3554
James Smart9e4f5e22009-03-26 13:33:19 -04003555 /* the blk_end_sync_io() doesn't check the error */
Giridhar Malavali47e7e892009-06-19 16:26:54 -07003556 if (done)
3557 return BLK_EH_NOT_HANDLED;
3558 else
3559 return BLK_EH_HANDLED;
James Smart9e4f5e22009-03-26 13:33:19 -04003560}
3561
James Smart9e4f5e22009-03-26 13:33:19 -04003562static int
3563fc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
3564{
3565 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
3566
3567 BUG_ON(!req->nr_phys_segments);
3568
3569 buf->sg_list = kzalloc(sz, GFP_KERNEL);
3570 if (!buf->sg_list)
3571 return -ENOMEM;
3572 sg_init_table(buf->sg_list, req->nr_phys_segments);
3573 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
3574 buf->payload_len = blk_rq_bytes(req);
3575 return 0;
3576}
3577
3578
3579/**
3580 * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
3581 * bsg request
3582 * @shost: SCSI Host corresponding to the bsg object
3583 * @rport: (optional) FC Remote Port corresponding to the bsg object
3584 * @req: BSG request that needs a job structure
3585 */
3586static int
3587fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
3588 struct request *req)
3589{
3590 struct fc_internal *i = to_fc_internal(shost->transportt);
3591 struct request *rsp = req->next_rq;
3592 struct fc_bsg_job *job;
3593 int ret;
3594
3595 BUG_ON(req->special);
3596
3597 job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
3598 GFP_KERNEL);
3599 if (!job)
3600 return -ENOMEM;
3601
3602 /*
3603 * Note: this is a bit silly.
3604 * The request gets formatted as a SGIO v4 ioctl request, which
3605 * then gets reformatted as a blk request, which then gets
3606 * reformatted as a fc bsg request. And on completion, we have
3607 * to wrap return results such that SGIO v4 thinks it was a scsi
3608 * status. I hope this was all worth it.
3609 */
3610
3611 req->special = job;
3612 job->shost = shost;
3613 job->rport = rport;
3614 job->req = req;
3615 if (i->f->dd_bsg_size)
3616 job->dd_data = (void *)&job[1];
3617 spin_lock_init(&job->job_lock);
3618 job->request = (struct fc_bsg_request *)req->cmd;
3619 job->request_len = req->cmd_len;
3620 job->reply = req->sense;
3621 job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
3622 * allocated */
3623 if (req->bio) {
3624 ret = fc_bsg_map_buffer(&job->request_payload, req);
3625 if (ret)
3626 goto failjob_rls_job;
3627 }
3628 if (rsp && rsp->bio) {
3629 ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
3630 if (ret)
3631 goto failjob_rls_rqst_payload;
3632 }
3633 job->job_done = fc_bsg_jobdone;
3634 if (rport)
3635 job->dev = &rport->dev;
3636 else
3637 job->dev = &shost->shost_gendev;
3638 get_device(job->dev); /* take a reference for the request */
3639
3640 job->ref_cnt = 1;
3641
3642 return 0;
3643
3644
3645failjob_rls_rqst_payload:
3646 kfree(job->request_payload.sg_list);
3647failjob_rls_job:
3648 kfree(job);
3649 return -ENOMEM;
3650}
3651
3652
3653enum fc_dispatch_result {
3654 FC_DISPATCH_BREAK, /* on return, q is locked, break from q loop */
3655 FC_DISPATCH_LOCKED, /* on return, q is locked, continue on */
3656 FC_DISPATCH_UNLOCKED, /* on return, q is unlocked, continue on */
3657};
3658
3659
3660/**
3661 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
Randy Dunlap54159072009-06-18 19:39:57 -07003662 * @q: fc host request queue
James Smart9e4f5e22009-03-26 13:33:19 -04003663 * @shost: scsi host rport attached to
3664 * @job: bsg job to be processed
3665 */
3666static enum fc_dispatch_result
3667fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3668 struct fc_bsg_job *job)
3669{
3670 struct fc_internal *i = to_fc_internal(shost->transportt);
3671 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3672 int ret;
3673
3674 /* Validate the host command */
3675 switch (job->request->msgcode) {
3676 case FC_BSG_HST_ADD_RPORT:
3677 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3678 break;
3679
3680 case FC_BSG_HST_DEL_RPORT:
3681 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3682 break;
3683
3684 case FC_BSG_HST_ELS_NOLOGIN:
3685 cmdlen += sizeof(struct fc_bsg_host_els);
3686 /* there better be a xmt and rcv payloads */
3687 if ((!job->request_payload.payload_len) ||
3688 (!job->reply_payload.payload_len)) {
3689 ret = -EINVAL;
3690 goto fail_host_msg;
3691 }
3692 break;
3693
3694 case FC_BSG_HST_CT:
3695 cmdlen += sizeof(struct fc_bsg_host_ct);
3696 /* there better be xmt and rcv payloads */
3697 if ((!job->request_payload.payload_len) ||
3698 (!job->reply_payload.payload_len)) {
3699 ret = -EINVAL;
3700 goto fail_host_msg;
3701 }
3702 break;
3703
3704 case FC_BSG_HST_VENDOR:
3705 cmdlen += sizeof(struct fc_bsg_host_vendor);
3706 if ((shost->hostt->vendor_id == 0L) ||
3707 (job->request->rqst_data.h_vendor.vendor_id !=
3708 shost->hostt->vendor_id)) {
3709 ret = -ESRCH;
3710 goto fail_host_msg;
3711 }
3712 break;
3713
3714 default:
3715 ret = -EBADR;
3716 goto fail_host_msg;
3717 }
3718
3719 /* check if we really have all the request data needed */
3720 if (job->request_len < cmdlen) {
3721 ret = -ENOMSG;
3722 goto fail_host_msg;
3723 }
3724
3725 ret = i->f->bsg_request(job);
3726 if (!ret)
3727 return FC_DISPATCH_UNLOCKED;
3728
3729fail_host_msg:
3730 /* return the errno failure code as the only status */
3731 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003732 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003733 job->reply->result = ret;
3734 job->reply_len = sizeof(uint32_t);
3735 fc_bsg_jobdone(job);
3736 return FC_DISPATCH_UNLOCKED;
3737}
3738
3739
3740/*
3741 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3742 * @rport: rport to be restarted
3743 */
3744static void
3745fc_bsg_goose_queue(struct fc_rport *rport)
3746{
3747 int flagset;
Christof Schmitt39562e72009-06-26 16:30:43 +02003748 unsigned long flags;
James Smart9e4f5e22009-03-26 13:33:19 -04003749
3750 if (!rport->rqst_q)
3751 return;
3752
3753 get_device(&rport->dev);
3754
Christof Schmitt39562e72009-06-26 16:30:43 +02003755 spin_lock_irqsave(rport->rqst_q->queue_lock, flags);
James Smart9e4f5e22009-03-26 13:33:19 -04003756 flagset = test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags) &&
3757 !test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags);
3758 if (flagset)
3759 queue_flag_set(QUEUE_FLAG_REENTER, rport->rqst_q);
3760 __blk_run_queue(rport->rqst_q);
3761 if (flagset)
3762 queue_flag_clear(QUEUE_FLAG_REENTER, rport->rqst_q);
Christof Schmitt39562e72009-06-26 16:30:43 +02003763 spin_unlock_irqrestore(rport->rqst_q->queue_lock, flags);
James Smart9e4f5e22009-03-26 13:33:19 -04003764
3765 put_device(&rport->dev);
3766}
3767
3768
3769/**
3770 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
Randy Dunlap54159072009-06-18 19:39:57 -07003771 * @q: rport request queue
James Smart9e4f5e22009-03-26 13:33:19 -04003772 * @shost: scsi host rport attached to
3773 * @rport: rport request destined to
3774 * @job: bsg job to be processed
3775 */
3776static enum fc_dispatch_result
3777fc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3778 struct fc_rport *rport, struct fc_bsg_job *job)
3779{
3780 struct fc_internal *i = to_fc_internal(shost->transportt);
3781 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3782 int ret;
3783
3784 /* Validate the rport command */
3785 switch (job->request->msgcode) {
3786 case FC_BSG_RPT_ELS:
3787 cmdlen += sizeof(struct fc_bsg_rport_els);
3788 goto check_bidi;
3789
3790 case FC_BSG_RPT_CT:
3791 cmdlen += sizeof(struct fc_bsg_rport_ct);
3792check_bidi:
3793 /* there better be xmt and rcv payloads */
3794 if ((!job->request_payload.payload_len) ||
3795 (!job->reply_payload.payload_len)) {
3796 ret = -EINVAL;
3797 goto fail_rport_msg;
3798 }
3799 break;
3800 default:
3801 ret = -EBADR;
3802 goto fail_rport_msg;
3803 }
3804
3805 /* check if we really have all the request data needed */
3806 if (job->request_len < cmdlen) {
3807 ret = -ENOMSG;
3808 goto fail_rport_msg;
3809 }
3810
3811 ret = i->f->bsg_request(job);
3812 if (!ret)
3813 return FC_DISPATCH_UNLOCKED;
3814
3815fail_rport_msg:
3816 /* return the errno failure code as the only status */
3817 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003818 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003819 job->reply->result = ret;
3820 job->reply_len = sizeof(uint32_t);
3821 fc_bsg_jobdone(job);
3822 return FC_DISPATCH_UNLOCKED;
3823}
3824
3825
3826/**
3827 * fc_bsg_request_handler - generic handler for bsg requests
3828 * @q: request queue to manage
3829 * @shost: Scsi_Host related to the bsg object
3830 * @rport: FC remote port related to the bsg object (optional)
3831 * @dev: device structure for bsg object
3832 */
3833static void
3834fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
3835 struct fc_rport *rport, struct device *dev)
3836{
3837 struct request *req;
3838 struct fc_bsg_job *job;
3839 enum fc_dispatch_result ret;
3840
3841 if (!get_device(dev))
3842 return;
3843
3844 while (!blk_queue_plugged(q)) {
Mike Christie2bc1c592009-11-05 11:18:09 -06003845 if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED) &&
3846 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
3847 break;
James Smart9e4f5e22009-03-26 13:33:19 -04003848
3849 req = blk_fetch_request(q);
3850 if (!req)
3851 break;
3852
3853 if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
3854 req->errors = -ENXIO;
3855 spin_unlock_irq(q->queue_lock);
Sarang Radked88a7142010-03-10 04:03:04 -06003856 blk_end_request_all(req, -ENXIO);
James Smart9e4f5e22009-03-26 13:33:19 -04003857 spin_lock_irq(q->queue_lock);
3858 continue;
3859 }
3860
3861 spin_unlock_irq(q->queue_lock);
3862
3863 ret = fc_req_to_bsgjob(shost, rport, req);
3864 if (ret) {
3865 req->errors = ret;
Sarang Radked88a7142010-03-10 04:03:04 -06003866 blk_end_request_all(req, ret);
James Smart9e4f5e22009-03-26 13:33:19 -04003867 spin_lock_irq(q->queue_lock);
3868 continue;
3869 }
3870
3871 job = req->special;
3872
3873 /* check if we have the msgcode value at least */
3874 if (job->request_len < sizeof(uint32_t)) {
3875 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003876 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003877 job->reply->result = -ENOMSG;
3878 job->reply_len = sizeof(uint32_t);
3879 fc_bsg_jobdone(job);
3880 spin_lock_irq(q->queue_lock);
3881 continue;
3882 }
3883
3884 /* the dispatch routines will unlock the queue_lock */
3885 if (rport)
3886 ret = fc_bsg_rport_dispatch(q, shost, rport, job);
3887 else
3888 ret = fc_bsg_host_dispatch(q, shost, job);
3889
3890 /* did dispatcher hit state that can't process any more */
3891 if (ret == FC_DISPATCH_BREAK)
3892 break;
3893
3894 /* did dispatcher had released the lock */
3895 if (ret == FC_DISPATCH_UNLOCKED)
3896 spin_lock_irq(q->queue_lock);
3897 }
3898
3899 spin_unlock_irq(q->queue_lock);
3900 put_device(dev);
3901 spin_lock_irq(q->queue_lock);
3902}
3903
3904
3905/**
3906 * fc_bsg_host_handler - handler for bsg requests for a fc host
3907 * @q: fc host request queue
3908 */
3909static void
3910fc_bsg_host_handler(struct request_queue *q)
3911{
3912 struct Scsi_Host *shost = q->queuedata;
3913
3914 fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
3915}
3916
3917
3918/**
3919 * fc_bsg_rport_handler - handler for bsg requests for a fc rport
3920 * @q: rport request queue
3921 */
3922static void
3923fc_bsg_rport_handler(struct request_queue *q)
3924{
3925 struct fc_rport *rport = q->queuedata;
3926 struct Scsi_Host *shost = rport_to_shost(rport);
3927
3928 fc_bsg_request_handler(q, shost, rport, &rport->dev);
3929}
3930
3931
3932/**
3933 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3934 * @shost: shost for fc_host
3935 * @fc_host: fc_host adding the structures to
3936 */
3937static int
3938fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
3939{
3940 struct device *dev = &shost->shost_gendev;
3941 struct fc_internal *i = to_fc_internal(shost->transportt);
3942 struct request_queue *q;
3943 int err;
James Bottomley3c559ea2009-06-21 12:11:43 -05003944 char bsg_name[20];
James Smart9e4f5e22009-03-26 13:33:19 -04003945
3946 fc_host->rqst_q = NULL;
3947
3948 if (!i->f->bsg_request)
3949 return -ENOTSUPP;
3950
3951 snprintf(bsg_name, sizeof(bsg_name),
3952 "fc_host%d", shost->host_no);
3953
3954 q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
3955 if (!q) {
3956 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3957 "initialize - no request queue\n",
3958 shost->host_no);
3959 return -ENOMEM;
3960 }
3961
3962 q->queuedata = shost;
3963 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003964 blk_queue_softirq_done(q, fc_bsg_softirq_done);
James Smart9e4f5e22009-03-26 13:33:19 -04003965 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
3966 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
3967
3968 err = bsg_register_queue(q, dev, bsg_name, NULL);
3969 if (err) {
3970 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3971 "initialize - register queue\n",
3972 shost->host_no);
3973 blk_cleanup_queue(q);
3974 return err;
3975 }
3976
3977 fc_host->rqst_q = q;
3978 return 0;
3979}
3980
3981
3982/**
3983 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3984 * @shost: shost that rport is attached to
3985 * @rport: rport that the bsg hooks are being attached to
3986 */
3987static int
3988fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
3989{
3990 struct device *dev = &rport->dev;
3991 struct fc_internal *i = to_fc_internal(shost->transportt);
3992 struct request_queue *q;
3993 int err;
3994
3995 rport->rqst_q = NULL;
3996
3997 if (!i->f->bsg_request)
3998 return -ENOTSUPP;
3999
4000 q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
4001 if (!q) {
4002 printk(KERN_ERR "%s: bsg interface failed to "
4003 "initialize - no request queue\n",
4004 dev->kobj.name);
4005 return -ENOMEM;
4006 }
4007
4008 q->queuedata = rport;
4009 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07004010 blk_queue_softirq_done(q, fc_bsg_softirq_done);
James Smart9e4f5e22009-03-26 13:33:19 -04004011 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
4012 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
4013
4014 err = bsg_register_queue(q, dev, NULL, NULL);
4015 if (err) {
4016 printk(KERN_ERR "%s: bsg interface failed to "
4017 "initialize - register queue\n",
4018 dev->kobj.name);
4019 blk_cleanup_queue(q);
4020 return err;
4021 }
4022
4023 rport->rqst_q = q;
4024 return 0;
4025}
4026
4027
4028/**
4029 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
4030 * @q: the request_queue that is to be torn down.
4031 */
4032static void
4033fc_bsg_remove(struct request_queue *q)
4034{
4035 if (q) {
4036 bsg_unregister_queue(q);
4037 blk_cleanup_queue(q);
4038 }
4039}
4040
4041
James Smart9ef3e4a2007-05-24 19:04:44 -04004042/* Original Author: Martin Hicks */
4043MODULE_AUTHOR("James Smart");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044MODULE_DESCRIPTION("FC Transport Attributes");
4045MODULE_LICENSE("GPL");
4046
4047module_init(fc_transport_init);
4048module_exit(fc_transport_exit);