blob: 78486d5406525df31776f79f3ed7be3c9198817b [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>
Andy Shevchenkoecc30992010-08-10 18:01:27 -070032#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_transport.h>
36#include <scsi/scsi_transport_fc.h>
James Smartc829c392006-03-13 08:28:57 -050037#include <scsi/scsi_cmnd.h>
James Smart84314fd2006-08-18 17:30:09 -040038#include <linux/netlink.h>
39#include <net/netlink.h>
40#include <scsi/scsi_netlink_fc.h>
James Smart9e4f5e22009-03-26 13:33:19 -040041#include <scsi/scsi_bsg_fc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "scsi_priv.h"
FUJITA Tomonori75252362007-09-01 02:02:27 +090043#include "scsi_transport_fc_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
James Smartaedf3492006-04-10 10:14:05 -040045static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
James Smart9ef3e4a2007-05-24 19:04:44 -040046static void fc_vport_sched_delete(struct work_struct *work);
Andrew Vasqueza30c3f62008-07-18 08:32:52 -070047static int fc_vport_setup(struct Scsi_Host *shost, int channel,
James Smarta53eb5e2007-04-27 12:41:09 -040048 struct device *pdev, struct fc_vport_identifiers *ids,
49 struct fc_vport **vport);
James Smart9e4f5e22009-03-26 13:33:19 -040050static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
51static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
52static void fc_bsg_remove(struct request_queue *);
53static void fc_bsg_goose_queue(struct fc_rport *);
James Smarta53eb5e2007-04-27 12:41:09 -040054
55/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * Redefine so that we can have same named attributes in the
57 * sdev/starget/host objects.
58 */
Tony Jonesee959b02008-02-22 00:13:36 +010059#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
60struct device_attribute device_attr_##_prefix##_##_name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 __ATTR(_name,_mode,_show,_store)
62
63#define fc_enum_name_search(title, table_type, table) \
64static const char *get_fc_##title##_name(enum table_type table_key) \
65{ \
66 int i; \
67 char *name = NULL; \
68 \
Tobias Klauser6391a112006-06-08 22:23:48 -070069 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (table[i].value == table_key) { \
71 name = table[i].name; \
72 break; \
73 } \
74 } \
75 return name; \
76}
77
78#define fc_enum_name_match(title, table_type, table) \
79static int get_fc_##title##_match(const char *table_key, \
80 enum table_type *value) \
81{ \
82 int i; \
83 \
Tobias Klauser6391a112006-06-08 22:23:48 -070084 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (strncmp(table_key, table[i].name, \
86 table[i].matchlen) == 0) { \
87 *value = table[i].value; \
88 return 0; /* success */ \
89 } \
90 } \
91 return 1; /* failure */ \
92}
93
94
95/* Convert fc_port_type values to ascii string name */
96static struct {
97 enum fc_port_type value;
98 char *name;
99} fc_port_type_names[] = {
100 { FC_PORTTYPE_UNKNOWN, "Unknown" },
101 { FC_PORTTYPE_OTHER, "Other" },
102 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
103 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
104 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
105 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
Christof Schmitt951948a2009-01-15 16:51:48 +0100106 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
James Smarta53eb5e2007-04-27 12:41:09 -0400107 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
110#define FC_PORTTYPE_MAX_NAMELEN 50
111
James Smarta53eb5e2007-04-27 12:41:09 -0400112/* Reuse fc_port_type enum function for vport_type */
113#define get_fc_vport_type_name get_fc_port_type_name
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
James Smart84314fd2006-08-18 17:30:09 -0400116/* Convert fc_host_event_code values to ascii string name */
117static const struct {
118 enum fc_host_event_code value;
119 char *name;
120} fc_host_event_code_names[] = {
121 { FCH_EVT_LIP, "lip" },
122 { FCH_EVT_LINKUP, "link_up" },
123 { FCH_EVT_LINKDOWN, "link_down" },
124 { FCH_EVT_LIPRESET, "lip_reset" },
125 { FCH_EVT_RSCN, "rscn" },
126 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
127 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
128 { FCH_EVT_PORT_ONLINE, "port_online" },
129 { FCH_EVT_PORT_OFFLINE, "port_offline" },
130 { FCH_EVT_PORT_FABRIC, "port_fabric" },
131 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
132 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
133};
134fc_enum_name_search(host_event_code, fc_host_event_code,
135 fc_host_event_code_names)
136#define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
137
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/* Convert fc_port_state values to ascii string name */
140static struct {
141 enum fc_port_state value;
142 char *name;
143} fc_port_state_names[] = {
144 { FC_PORTSTATE_UNKNOWN, "Unknown" },
145 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
146 { FC_PORTSTATE_ONLINE, "Online" },
147 { FC_PORTSTATE_OFFLINE, "Offline" },
148 { FC_PORTSTATE_BLOCKED, "Blocked" },
149 { FC_PORTSTATE_BYPASSED, "Bypassed" },
150 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
151 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
152 { FC_PORTSTATE_ERROR, "Error" },
153 { FC_PORTSTATE_LOOPBACK, "Loopback" },
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500154 { FC_PORTSTATE_DELETED, "Deleted" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
157#define FC_PORTSTATE_MAX_NAMELEN 20
158
159
James Smarta53eb5e2007-04-27 12:41:09 -0400160/* Convert fc_vport_state values to ascii string name */
161static struct {
162 enum fc_vport_state value;
163 char *name;
164} fc_vport_state_names[] = {
165 { FC_VPORT_UNKNOWN, "Unknown" },
166 { FC_VPORT_ACTIVE, "Active" },
167 { FC_VPORT_DISABLED, "Disabled" },
168 { FC_VPORT_LINKDOWN, "Linkdown" },
169 { FC_VPORT_INITIALIZING, "Initializing" },
170 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
171 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
172 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
173 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
174 { FC_VPORT_FAILED, "VPort Failed" },
175};
176fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
177#define FC_VPORTSTATE_MAX_NAMELEN 24
178
179/* Reuse fc_vport_state enum function for vport_last_state */
180#define get_fc_vport_last_state_name get_fc_vport_state_name
181
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/* Convert fc_tgtid_binding_type values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100184static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 enum fc_tgtid_binding_type value;
186 char *name;
187 int matchlen;
188} fc_tgtid_binding_type_names[] = {
189 { FC_TGTID_BIND_NONE, "none", 4 },
190 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
191 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
192 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
193};
194fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
195 fc_tgtid_binding_type_names)
196fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
197 fc_tgtid_binding_type_names)
198#define FC_BINDTYPE_MAX_NAMELEN 30
199
200
201#define fc_bitfield_name_search(title, table) \
202static ssize_t \
203get_fc_##title##_names(u32 table_key, char *buf) \
204{ \
205 char *prefix = ""; \
206 ssize_t len = 0; \
207 int i; \
208 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700209 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (table[i].value & table_key) { \
211 len += sprintf(buf + len, "%s%s", \
212 prefix, table[i].name); \
213 prefix = ", "; \
214 } \
215 } \
216 len += sprintf(buf + len, "\n"); \
217 return len; \
218}
219
220
221/* Convert FC_COS bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100222static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 u32 value;
224 char *name;
225} fc_cos_names[] = {
226 { FC_COS_CLASS1, "Class 1" },
227 { FC_COS_CLASS2, "Class 2" },
228 { FC_COS_CLASS3, "Class 3" },
229 { FC_COS_CLASS4, "Class 4" },
230 { FC_COS_CLASS6, "Class 6" },
231};
232fc_bitfield_name_search(cos, fc_cos_names)
233
234
235/* Convert FC_PORTSPEED bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100236static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 u32 value;
238 char *name;
239} fc_port_speed_names[] = {
240 { FC_PORTSPEED_1GBIT, "1 Gbit" },
241 { FC_PORTSPEED_2GBIT, "2 Gbit" },
242 { FC_PORTSPEED_4GBIT, "4 Gbit" },
243 { FC_PORTSPEED_10GBIT, "10 Gbit" },
James Smartc3d23502007-03-12 14:16:35 -0500244 { FC_PORTSPEED_8GBIT, "8 Gbit" },
245 { FC_PORTSPEED_16GBIT, "16 Gbit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
247};
248fc_bitfield_name_search(port_speed, fc_port_speed_names)
249
250
251static int
252show_fc_fc4s (char *buf, u8 *fc4_list)
253{
254 int i, len=0;
255
256 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
257 len += sprintf(buf + len , "0x%02x ", *fc4_list);
258 len += sprintf(buf + len, "\n");
259 return len;
260}
261
262
James Smarta53eb5e2007-04-27 12:41:09 -0400263/* Convert FC_PORT_ROLE bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100264static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 u32 value;
266 char *name;
James Smarta53eb5e2007-04-27 12:41:09 -0400267} fc_port_role_names[] = {
268 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
269 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
270 { FC_PORT_ROLE_IP_PORT, "IP Port" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
James Smarta53eb5e2007-04-27 12:41:09 -0400272fc_bitfield_name_search(port_roles, fc_port_role_names)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274/*
275 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
276 */
277#define FC_WELLKNOWN_PORTID_MASK 0xfffff0
278#define FC_WELLKNOWN_ROLE_MASK 0x00000f
279#define FC_FPORT_PORTID 0x00000e
280#define FC_FABCTLR_PORTID 0x00000d
281#define FC_DIRSRVR_PORTID 0x00000c
282#define FC_TIMESRVR_PORTID 0x00000b
283#define FC_MGMTSRVR_PORTID 0x00000a
284
285
David Howellsc4028952006-11-22 14:57:56 +0000286static void fc_timeout_deleted_rport(struct work_struct *work);
287static void fc_timeout_fail_rport_io(struct work_struct *work);
288static void fc_scsi_scan_rport(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290/*
291 * Attribute counts pre object type...
292 * Increase these values if you add attributes
293 */
294#define FC_STARGET_NUM_ATTRS 3
James Smart0f29b962006-08-18 17:33:29 -0400295#define FC_RPORT_NUM_ATTRS 10
James Smarta53eb5e2007-04-27 12:41:09 -0400296#define FC_VPORT_NUM_ATTRS 9
James Smart46436822009-07-28 12:30:01 -0400297#define FC_HOST_NUM_ATTRS 22
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299struct fc_internal {
300 struct scsi_transport_template t;
301 struct fc_function_template *f;
302
303 /*
304 * For attributes : each object has :
305 * An array of the actual attributes structures
306 * An array of null-terminated pointers to the attribute
307 * structures - used for mid-layer interaction.
308 *
309 * The attribute containers for the starget and host are are
310 * part of the midlayer. As the remote port is specific to the
311 * fc transport, we must provide the attribute container.
312 */
Tony Jonesee959b02008-02-22 00:13:36 +0100313 struct device_attribute private_starget_attrs[
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 FC_STARGET_NUM_ATTRS];
Tony Jonesee959b02008-02-22 00:13:36 +0100315 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Tony Jonesee959b02008-02-22 00:13:36 +0100317 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
318 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 struct transport_container rport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100321 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
322 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
James Smarta53eb5e2007-04-27 12:41:09 -0400323
324 struct transport_container vport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100325 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
326 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327};
328
329#define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
330
James Bottomleyd0a7e572005-08-14 17:09:01 -0500331static int fc_target_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100332 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct scsi_target *starget = to_scsi_target(dev);
335 struct fc_rport *rport = starget_to_rport(starget);
336
337 /*
338 * if parent is remote port, use values from remote port.
339 * Otherwise, this host uses the fc_transport, but not the
340 * remote port interface. As such, initialize to known non-values.
341 */
342 if (rport) {
343 fc_starget_node_name(starget) = rport->node_name;
344 fc_starget_port_name(starget) = rport->port_name;
345 fc_starget_port_id(starget) = rport->port_id;
346 } else {
347 fc_starget_node_name(starget) = -1;
348 fc_starget_port_name(starget) = -1;
349 fc_starget_port_id(starget) = -1;
350 }
351
352 return 0;
353}
354
355static DECLARE_TRANSPORT_CLASS(fc_transport_class,
356 "fc_transport",
357 fc_target_setup,
358 NULL,
359 NULL);
360
James Bottomleyd0a7e572005-08-14 17:09:01 -0500361static int fc_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100362 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct Scsi_Host *shost = dev_to_shost(dev);
James Smartaedf3492006-04-10 10:14:05 -0400365 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
James Smart9ef3e4a2007-05-24 19:04:44 -0400367 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * Set default values easily detected by the midlayer as
369 * failure cases. The scsi lldd is responsible for initializing
370 * all transport attributes to valid values per host.
371 */
James Smartaedf3492006-04-10 10:14:05 -0400372 fc_host->node_name = -1;
373 fc_host->port_name = -1;
374 fc_host->permanent_port_name = -1;
375 fc_host->supported_classes = FC_COS_UNSPECIFIED;
376 memset(fc_host->supported_fc4s, 0,
377 sizeof(fc_host->supported_fc4s));
James Smartaedf3492006-04-10 10:14:05 -0400378 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
379 fc_host->maxframe_size = -1;
James Smarta53eb5e2007-04-27 12:41:09 -0400380 fc_host->max_npiv_vports = 0;
James Smartaedf3492006-04-10 10:14:05 -0400381 memset(fc_host->serial_number, 0,
382 sizeof(fc_host->serial_number));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
James Smartaedf3492006-04-10 10:14:05 -0400384 fc_host->port_id = -1;
385 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
386 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
387 memset(fc_host->active_fc4s, 0,
388 sizeof(fc_host->active_fc4s));
389 fc_host->speed = FC_PORTSPEED_UNKNOWN;
390 fc_host->fabric_name = -1;
James Smartb8d08212006-08-17 08:00:43 -0400391 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
392 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
James Smartaedf3492006-04-10 10:14:05 -0400394 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
James Smartaedf3492006-04-10 10:14:05 -0400396 INIT_LIST_HEAD(&fc_host->rports);
397 INIT_LIST_HEAD(&fc_host->rport_bindings);
James Smarta53eb5e2007-04-27 12:41:09 -0400398 INIT_LIST_HEAD(&fc_host->vports);
James Smartaedf3492006-04-10 10:14:05 -0400399 fc_host->next_rport_number = 0;
400 fc_host->next_target_id = 0;
James Smarta53eb5e2007-04-27 12:41:09 -0400401 fc_host->next_vport_number = 0;
402 fc_host->npiv_vports_inuse = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Kay Sieversaab0de22008-05-02 06:02:41 +0200404 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
405 "fc_wq_%d", shost->host_no);
James Smartaedf3492006-04-10 10:14:05 -0400406 fc_host->work_q = create_singlethread_workqueue(
407 fc_host->work_q_name);
408 if (!fc_host->work_q)
409 return -ENOMEM;
410
Kay Sieversaab0de22008-05-02 06:02:41 +0200411 snprintf(fc_host->devloss_work_q_name,
412 sizeof(fc_host->devloss_work_q_name),
413 "fc_dl_%d", shost->host_no);
James Smartaedf3492006-04-10 10:14:05 -0400414 fc_host->devloss_work_q = create_singlethread_workqueue(
415 fc_host->devloss_work_q_name);
416 if (!fc_host->devloss_work_q) {
417 destroy_workqueue(fc_host->work_q);
418 fc_host->work_q = NULL;
419 return -ENOMEM;
420 }
421
James Smart9e4f5e22009-03-26 13:33:19 -0400422 fc_bsg_hostadd(shost, fc_host);
423 /* ignore any bsg add error - we just can't do sgio */
424
425 return 0;
426}
427
428static int fc_host_remove(struct transport_container *tc, struct device *dev,
429 struct device *cdev)
430{
431 struct Scsi_Host *shost = dev_to_shost(dev);
432 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
433
434 fc_bsg_remove(fc_host->rqst_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return 0;
436}
437
438static DECLARE_TRANSPORT_CLASS(fc_host_class,
439 "fc_host",
440 fc_host_setup,
James Smart9e4f5e22009-03-26 13:33:19 -0400441 fc_host_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 NULL);
443
444/*
445 * Setup and Remove actions for remote ports are handled
446 * in the service functions below.
447 */
448static DECLARE_TRANSPORT_CLASS(fc_rport_class,
449 "fc_remote_ports",
450 NULL,
451 NULL,
452 NULL);
453
454/*
James Smarta53eb5e2007-04-27 12:41:09 -0400455 * Setup and Remove actions for virtual ports are handled
456 * in the service functions below.
457 */
458static DECLARE_TRANSPORT_CLASS(fc_vport_class,
459 "fc_vports",
460 NULL,
461 NULL,
462 NULL);
463
464/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * Module Parameters
466 */
467
468/*
469 * dev_loss_tmo: the default number of seconds that the FC transport
470 * should insulate the loss of a remote port.
471 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
472 */
James Smart1c9e16e2006-05-16 16:13:36 -0400473static unsigned int fc_dev_loss_tmo = 60; /* seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Masatake YAMATO10f4b892007-09-19 22:59:16 +0900475module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476MODULE_PARM_DESC(dev_loss_tmo,
477 "Maximum number of seconds that the FC transport should"
478 " insulate the loss of a remote port. Once this value is"
479 " exceeded, the scsi target is removed. Value should be"
Hannes Reineckef28186632009-12-15 09:26:06 +0100480 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
481 " fast_io_fail_tmo is not set.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Rob Landleyeb448202007-11-03 13:30:39 -0500483/*
James Smart84314fd2006-08-18 17:30:09 -0400484 * Netlink Infrastructure
Rob Landleyeb448202007-11-03 13:30:39 -0500485 */
James Smart84314fd2006-08-18 17:30:09 -0400486
487static atomic_t fc_event_seq;
488
489/**
490 * fc_get_event_number - Obtain the next sequential FC event number
491 *
492 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -0500493 * We could have inlined this, but it would have required fc_event_seq to
James Smart84314fd2006-08-18 17:30:09 -0400494 * be exposed. For now, live with the subroutine call.
495 * Atomic used to avoid lock/unlock...
Rob Landleyeb448202007-11-03 13:30:39 -0500496 */
James Smart84314fd2006-08-18 17:30:09 -0400497u32
498fc_get_event_number(void)
499{
500 return atomic_add_return(1, &fc_event_seq);
501}
502EXPORT_SYMBOL(fc_get_event_number);
503
504
505/**
506 * fc_host_post_event - called to post an even on an fc_host.
James Smart84314fd2006-08-18 17:30:09 -0400507 * @shost: host the event occurred on
508 * @event_number: fc event number obtained from get_fc_event_number()
509 * @event_code: fc_host event being posted
510 * @event_data: 32bits of data for the event being posted
511 *
512 * Notes:
513 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500514 */
James Smart84314fd2006-08-18 17:30:09 -0400515void
516fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
517 enum fc_host_event_code event_code, u32 event_data)
518{
519 struct sk_buff *skb;
520 struct nlmsghdr *nlh;
521 struct fc_nl_event *event;
522 const char *name;
523 u32 len, skblen;
524 int err;
525
526 if (!scsi_nl_sock) {
527 err = -ENOENT;
528 goto send_fail;
529 }
530
531 len = FC_NL_MSGALIGN(sizeof(*event));
532 skblen = NLMSG_SPACE(len);
533
534 skb = alloc_skb(skblen, GFP_KERNEL);
535 if (!skb) {
536 err = -ENOBUFS;
537 goto send_fail;
538 }
539
540 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
541 skblen - sizeof(*nlh), 0);
542 if (!nlh) {
543 err = -ENOBUFS;
544 goto send_fail_skb;
545 }
546 event = NLMSG_DATA(nlh);
547
548 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
549 FC_NL_ASYNC_EVENT, len);
550 event->seconds = get_seconds();
551 event->vendor_id = 0;
552 event->host_no = shost->host_no;
553 event->event_datalen = sizeof(u32); /* bytes */
554 event->event_num = event_number;
555 event->event_code = event_code;
556 event->event_data = event_data;
557
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800558 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
559 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400560 return;
561
562send_fail_skb:
563 kfree_skb(skb);
564send_fail:
565 name = get_fc_host_event_code_name(event_code);
566 printk(KERN_WARNING
567 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700568 __func__, shost->host_no,
James Smart84314fd2006-08-18 17:30:09 -0400569 (name) ? name : "<unknown>", event_data, err);
570 return;
571}
572EXPORT_SYMBOL(fc_host_post_event);
573
574
575/**
Rob Landleyeb448202007-11-03 13:30:39 -0500576 * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
James Smart84314fd2006-08-18 17:30:09 -0400577 * @shost: host the event occurred on
578 * @event_number: fc event number obtained from get_fc_event_number()
579 * @data_len: amount, in bytes, of vendor unique data
580 * @data_buf: pointer to vendor unique data
Rob Landleyeb448202007-11-03 13:30:39 -0500581 * @vendor_id: Vendor id
James Smart84314fd2006-08-18 17:30:09 -0400582 *
583 * Notes:
584 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500585 */
James Smart84314fd2006-08-18 17:30:09 -0400586void
587fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
James Smartf14e2e22006-08-22 09:55:23 -0400588 u32 data_len, char * data_buf, u64 vendor_id)
James Smart84314fd2006-08-18 17:30:09 -0400589{
590 struct sk_buff *skb;
591 struct nlmsghdr *nlh;
592 struct fc_nl_event *event;
593 u32 len, skblen;
594 int err;
595
596 if (!scsi_nl_sock) {
597 err = -ENOENT;
598 goto send_vendor_fail;
599 }
600
601 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
602 skblen = NLMSG_SPACE(len);
603
604 skb = alloc_skb(skblen, GFP_KERNEL);
605 if (!skb) {
606 err = -ENOBUFS;
607 goto send_vendor_fail;
608 }
609
610 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
611 skblen - sizeof(*nlh), 0);
612 if (!nlh) {
613 err = -ENOBUFS;
614 goto send_vendor_fail_skb;
615 }
616 event = NLMSG_DATA(nlh);
617
618 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
619 FC_NL_ASYNC_EVENT, len);
620 event->seconds = get_seconds();
621 event->vendor_id = vendor_id;
622 event->host_no = shost->host_no;
623 event->event_datalen = data_len; /* bytes */
624 event->event_num = event_number;
625 event->event_code = FCH_EVT_VENDOR_UNIQUE;
626 memcpy(&event->event_data, data_buf, data_len);
627
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800628 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
629 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400630 return;
631
632send_vendor_fail_skb:
633 kfree_skb(skb);
634send_vendor_fail:
635 printk(KERN_WARNING
636 "%s: Dropped Event : host %d vendor_unique - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700637 __func__, shost->host_no, err);
James Smart84314fd2006-08-18 17:30:09 -0400638 return;
639}
640EXPORT_SYMBOL(fc_host_post_vendor_event);
641
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644static __init int fc_transport_init(void)
645{
James Smart84314fd2006-08-18 17:30:09 -0400646 int error;
647
648 atomic_set(&fc_event_seq, 0);
649
650 error = transport_class_register(&fc_host_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (error)
652 return error;
James Smarta53eb5e2007-04-27 12:41:09 -0400653 error = transport_class_register(&fc_vport_class);
654 if (error)
Mike Christie48de68a2009-11-17 21:25:16 -0600655 goto unreg_host_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 error = transport_class_register(&fc_rport_class);
657 if (error)
Mike Christie48de68a2009-11-17 21:25:16 -0600658 goto unreg_vport_class;
659 error = transport_class_register(&fc_transport_class);
660 if (error)
661 goto unreg_rport_class;
662 return 0;
663
664unreg_rport_class:
665 transport_class_unregister(&fc_rport_class);
666unreg_vport_class:
667 transport_class_unregister(&fc_vport_class);
668unreg_host_class:
669 transport_class_unregister(&fc_host_class);
670 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673static void __exit fc_transport_exit(void)
674{
675 transport_class_unregister(&fc_transport_class);
676 transport_class_unregister(&fc_rport_class);
677 transport_class_unregister(&fc_host_class);
James Smarta53eb5e2007-04-27 12:41:09 -0400678 transport_class_unregister(&fc_vport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681/*
682 * FC Remote Port Attribute Management
683 */
684
685#define fc_rport_show_function(field, format_string, sz, cast) \
686static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100687show_fc_rport_##field (struct device *dev, \
688 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100690 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct Scsi_Host *shost = rport_to_shost(rport); \
692 struct fc_internal *i = to_fc_internal(shost->transportt); \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400693 if ((i->f->get_rport_##field) && \
694 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500695 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400696 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 i->f->get_rport_##field(rport); \
698 return snprintf(buf, sz, format_string, cast rport->field); \
699}
700
701#define fc_rport_store_function(field) \
702static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100703store_fc_rport_##field(struct device *dev, \
704 struct device_attribute *attr, \
705 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{ \
707 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100708 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct Scsi_Host *shost = rport_to_shost(rport); \
710 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -0400711 char *cp; \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400712 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500713 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400714 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
715 return -EBUSY; \
James Smart0f29b962006-08-18 17:33:29 -0400716 val = simple_strtoul(buf, &cp, 0); \
717 if (*cp && (*cp != '\n')) \
718 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 i->f->set_rport_##field(rport, val); \
720 return count; \
721}
722
723#define fc_rport_rd_attr(field, format_string, sz) \
724 fc_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100725static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 show_fc_rport_##field, NULL)
727
728#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
729 fc_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100730static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 show_fc_rport_##field, NULL)
732
733#define fc_rport_rw_attr(field, format_string, sz) \
734 fc_rport_show_function(field, format_string, sz, ) \
735 fc_rport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100736static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 show_fc_rport_##field, \
738 store_fc_rport_##field)
739
740
741#define fc_private_rport_show_function(field, format_string, sz, cast) \
742static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100743show_fc_rport_##field (struct device *dev, \
744 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100746 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return snprintf(buf, sz, format_string, cast rport->field); \
748}
749
750#define fc_private_rport_rd_attr(field, format_string, sz) \
751 fc_private_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100752static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 show_fc_rport_##field, NULL)
754
755#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
756 fc_private_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100757static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 show_fc_rport_##field, NULL)
759
760
761#define fc_private_rport_rd_enum_attr(title, maxlen) \
762static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100763show_fc_rport_##title (struct device *dev, \
764 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100766 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 const char *name; \
768 name = get_fc_##title##_name(rport->title); \
769 if (!name) \
770 return -EINVAL; \
771 return snprintf(buf, maxlen, "%s\n", name); \
772} \
Tony Jonesee959b02008-02-22 00:13:36 +0100773static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 show_fc_rport_##title, NULL)
775
776
777#define SETUP_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100778 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
780 i->private_rport_attrs[count].store = NULL; \
781 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
782 if (i->f->show_rport_##field) \
783 count++
784
785#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100786 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
788 i->private_rport_attrs[count].store = NULL; \
789 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
790 count++
791
792#define SETUP_RPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100793 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (!i->f->set_rport_##field) { \
795 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
796 i->private_rport_attrs[count].store = NULL; \
797 } \
798 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
799 if (i->f->show_rport_##field) \
800 count++
801
James Smart0f29b962006-08-18 17:33:29 -0400802#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
803{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100804 i->private_rport_attrs[count] = device_attr_rport_##field; \
James Smart0f29b962006-08-18 17:33:29 -0400805 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
806 count++; \
807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810/* The FC Transport Remote Port Attributes: */
811
812/* Fixed Remote Port Attributes */
813
814fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
815
816static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100817show_fc_rport_supported_classes (struct device *dev,
818 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Tony Jonesee959b02008-02-22 00:13:36 +0100820 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (rport->supported_classes == FC_COS_UNSPECIFIED)
822 return snprintf(buf, 20, "unspecified\n");
823 return get_fc_cos_names(rport->supported_classes, buf);
824}
Tony Jonesee959b02008-02-22 00:13:36 +0100825static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 show_fc_rport_supported_classes, NULL);
827
828/* Dynamic Remote Port Attributes */
829
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400830/*
831 * dev_loss_tmo attribute
832 */
833fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
834static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100835store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
836 const char *buf, size_t count)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400837{
Hannes Reinecke36dd2882010-03-09 10:18:48 +0100838 unsigned long val;
Tony Jonesee959b02008-02-22 00:13:36 +0100839 struct fc_rport *rport = transport_class_to_rport(dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400840 struct Scsi_Host *shost = rport_to_shost(rport);
841 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart0f29b962006-08-18 17:33:29 -0400842 char *cp;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400843 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500844 (rport->port_state == FC_PORTSTATE_DELETED) ||
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400845 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
846 return -EBUSY;
James Smart0f29b962006-08-18 17:33:29 -0400847 val = simple_strtoul(buf, &cp, 0);
Hannes Reineckef28186632009-12-15 09:26:06 +0100848 if ((*cp && (*cp != '\n')) || (val < 0))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400849 return -EINVAL;
Hannes Reineckef28186632009-12-15 09:26:06 +0100850
851 /*
Hannes Reinecke36dd2882010-03-09 10:18:48 +0100852 * Check for overflow; dev_loss_tmo is u32
853 */
854 if (val > UINT_MAX)
855 return -EINVAL;
856
857 /*
Hannes Reineckef28186632009-12-15 09:26:06 +0100858 * If fast_io_fail is off we have to cap
859 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
860 */
861 if (rport->fast_io_fail_tmo == -1 &&
862 val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
863 return -EINVAL;
864
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400865 i->f->set_rport_dev_loss_tmo(rport, val);
866 return count;
867}
Tony Jonesee959b02008-02-22 00:13:36 +0100868static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400869 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871
872/* Private Remote Port Attributes */
873
874fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
875fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
876fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
877
878static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100879show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
880 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Tony Jonesee959b02008-02-22 00:13:36 +0100882 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 /* identify any roles that are port_id specific */
885 if ((rport->port_id != -1) &&
886 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
887 FC_WELLKNOWN_PORTID_MASK) {
888 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
889 case FC_FPORT_PORTID:
890 return snprintf(buf, 30, "Fabric Port\n");
891 case FC_FABCTLR_PORTID:
892 return snprintf(buf, 30, "Fabric Controller\n");
893 case FC_DIRSRVR_PORTID:
894 return snprintf(buf, 30, "Directory Server\n");
895 case FC_TIMESRVR_PORTID:
896 return snprintf(buf, 30, "Time Server\n");
897 case FC_MGMTSRVR_PORTID:
898 return snprintf(buf, 30, "Management Server\n");
899 default:
900 return snprintf(buf, 30, "Unknown Fabric Entity\n");
901 }
902 } else {
James Smarta53eb5e2007-04-27 12:41:09 -0400903 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return snprintf(buf, 20, "unknown\n");
James Smarta53eb5e2007-04-27 12:41:09 -0400905 return get_fc_port_roles_names(rport->roles, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907}
Tony Jonesee959b02008-02-22 00:13:36 +0100908static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 show_fc_rport_roles, NULL);
910
911fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
912fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
913
James Smart0f29b962006-08-18 17:33:29 -0400914/*
915 * fast_io_fail_tmo attribute
916 */
917static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100918show_fc_rport_fast_io_fail_tmo (struct device *dev,
919 struct device_attribute *attr, char *buf)
James Smart0f29b962006-08-18 17:33:29 -0400920{
Tony Jonesee959b02008-02-22 00:13:36 +0100921 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400922
923 if (rport->fast_io_fail_tmo == -1)
924 return snprintf(buf, 5, "off\n");
925 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
926}
927
928static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100929store_fc_rport_fast_io_fail_tmo(struct device *dev,
930 struct device_attribute *attr, const char *buf,
931 size_t count)
James Smart0f29b962006-08-18 17:33:29 -0400932{
933 int val;
934 char *cp;
Tony Jonesee959b02008-02-22 00:13:36 +0100935 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400936
937 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
938 (rport->port_state == FC_PORTSTATE_DELETED) ||
939 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
940 return -EBUSY;
941 if (strncmp(buf, "off", 3) == 0)
942 rport->fast_io_fail_tmo = -1;
943 else {
944 val = simple_strtoul(buf, &cp, 0);
Hannes Reineckef28186632009-12-15 09:26:06 +0100945 if ((*cp && (*cp != '\n')) || (val < 0))
James Smart0f29b962006-08-18 17:33:29 -0400946 return -EINVAL;
Hannes Reineckef28186632009-12-15 09:26:06 +0100947 /*
948 * Cap fast_io_fail by dev_loss_tmo or
949 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
950 */
951 if ((val >= rport->dev_loss_tmo) ||
952 (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
953 return -EINVAL;
954
James Smart0f29b962006-08-18 17:33:29 -0400955 rport->fast_io_fail_tmo = val;
956 }
957 return count;
958}
Tony Jonesee959b02008-02-22 00:13:36 +0100959static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
James Smart0f29b962006-08-18 17:33:29 -0400960 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962
963/*
964 * FC SCSI Target Attribute Management
965 */
966
967/*
968 * Note: in the target show function we recognize when the remote
Uwe Kleine-König732bee72010-06-11 12:16:59 +0200969 * port is in the hierarchy and do not allow the driver to get
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * involved in sysfs functions. The driver only gets involved if
971 * it's the "old" style that doesn't use rports.
972 */
973#define fc_starget_show_function(field, format_string, sz, cast) \
974static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100975show_fc_starget_##field (struct device *dev, \
976 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100978 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
980 struct fc_internal *i = to_fc_internal(shost->transportt); \
981 struct fc_rport *rport = starget_to_rport(starget); \
982 if (rport) \
983 fc_starget_##field(starget) = rport->field; \
984 else if (i->f->get_starget_##field) \
985 i->f->get_starget_##field(starget); \
986 return snprintf(buf, sz, format_string, \
987 cast fc_starget_##field(starget)); \
988}
989
990#define fc_starget_rd_attr(field, format_string, sz) \
991 fc_starget_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100992static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 show_fc_starget_##field, NULL)
994
995#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
996 fc_starget_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100997static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 show_fc_starget_##field, NULL)
999
1000#define SETUP_STARGET_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001001 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1003 i->private_starget_attrs[count].store = NULL; \
1004 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1005 if (i->f->show_starget_##field) \
1006 count++
1007
1008#define SETUP_STARGET_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001009 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!i->f->set_starget_##field) { \
1011 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1012 i->private_starget_attrs[count].store = NULL; \
1013 } \
1014 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1015 if (i->f->show_starget_##field) \
1016 count++
1017
1018/* The FC Transport SCSI Target Attributes: */
1019fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1020fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1021fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1022
1023
1024/*
James Smarta53eb5e2007-04-27 12:41:09 -04001025 * FC Virtual Port Attribute Management
1026 */
1027
1028#define fc_vport_show_function(field, format_string, sz, cast) \
1029static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001030show_fc_vport_##field (struct device *dev, \
1031 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001032{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001033 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001034 struct Scsi_Host *shost = vport_to_shost(vport); \
1035 struct fc_internal *i = to_fc_internal(shost->transportt); \
1036 if ((i->f->get_vport_##field) && \
1037 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1038 i->f->get_vport_##field(vport); \
1039 return snprintf(buf, sz, format_string, cast vport->field); \
1040}
1041
1042#define fc_vport_store_function(field) \
1043static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001044store_fc_vport_##field(struct device *dev, \
1045 struct device_attribute *attr, \
1046 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001047{ \
1048 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001049 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001050 struct Scsi_Host *shost = vport_to_shost(vport); \
1051 struct fc_internal *i = to_fc_internal(shost->transportt); \
1052 char *cp; \
1053 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1054 return -EBUSY; \
1055 val = simple_strtoul(buf, &cp, 0); \
1056 if (*cp && (*cp != '\n')) \
1057 return -EINVAL; \
1058 i->f->set_vport_##field(vport, val); \
1059 return count; \
1060}
1061
1062#define fc_vport_store_str_function(field, slen) \
1063static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001064store_fc_vport_##field(struct device *dev, \
1065 struct device_attribute *attr, \
1066 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001067{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001068 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001069 struct Scsi_Host *shost = vport_to_shost(vport); \
1070 struct fc_internal *i = to_fc_internal(shost->transportt); \
1071 unsigned int cnt=count; \
1072 \
1073 /* count may include a LF at end of string */ \
1074 if (buf[cnt-1] == '\n') \
1075 cnt--; \
1076 if (cnt > ((slen) - 1)) \
1077 return -EINVAL; \
1078 memcpy(vport->field, buf, cnt); \
1079 i->f->set_vport_##field(vport); \
1080 return count; \
1081}
1082
1083#define fc_vport_rd_attr(field, format_string, sz) \
1084 fc_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001085static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001086 show_fc_vport_##field, NULL)
1087
1088#define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1089 fc_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001090static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001091 show_fc_vport_##field, NULL)
1092
1093#define fc_vport_rw_attr(field, format_string, sz) \
1094 fc_vport_show_function(field, format_string, sz, ) \
1095 fc_vport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001096static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001097 show_fc_vport_##field, \
1098 store_fc_vport_##field)
1099
1100#define fc_private_vport_show_function(field, format_string, sz, cast) \
1101static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001102show_fc_vport_##field (struct device *dev, \
1103 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001104{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001105 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001106 return snprintf(buf, sz, format_string, cast vport->field); \
1107}
1108
1109#define fc_private_vport_store_u32_function(field) \
1110static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001111store_fc_vport_##field(struct device *dev, \
1112 struct device_attribute *attr, \
1113 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001114{ \
1115 u32 val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001116 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001117 char *cp; \
1118 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1119 return -EBUSY; \
1120 val = simple_strtoul(buf, &cp, 0); \
1121 if (*cp && (*cp != '\n')) \
1122 return -EINVAL; \
1123 vport->field = val; \
1124 return count; \
1125}
1126
1127
1128#define fc_private_vport_rd_attr(field, format_string, sz) \
1129 fc_private_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001130static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001131 show_fc_vport_##field, NULL)
1132
1133#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1134 fc_private_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001135static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001136 show_fc_vport_##field, NULL)
1137
1138#define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1139 fc_private_vport_show_function(field, format_string, sz, ) \
1140 fc_private_vport_store_u32_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001141static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001142 show_fc_vport_##field, \
1143 store_fc_vport_##field)
1144
1145
1146#define fc_private_vport_rd_enum_attr(title, maxlen) \
1147static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001148show_fc_vport_##title (struct device *dev, \
1149 struct device_attribute *attr, \
1150 char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001151{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001152 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001153 const char *name; \
1154 name = get_fc_##title##_name(vport->title); \
1155 if (!name) \
1156 return -EINVAL; \
1157 return snprintf(buf, maxlen, "%s\n", name); \
1158} \
Tony Jonesee959b02008-02-22 00:13:36 +01001159static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001160 show_fc_vport_##title, NULL)
1161
1162
1163#define SETUP_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001164 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001165 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1166 i->private_vport_attrs[count].store = NULL; \
1167 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1168 if (i->f->get_##field) \
1169 count++
1170 /* NOTE: Above MACRO differs: checks function not show bit */
1171
1172#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(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->private_vport_attrs[count].attr.mode = S_IRUGO; \
1175 i->private_vport_attrs[count].store = NULL; \
1176 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1177 count++
1178
1179#define SETUP_VPORT_ATTRIBUTE_WR(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 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1182 if (i->f->field) \
1183 count++
1184 /* NOTE: Above MACRO differs: checks function */
1185
1186#define SETUP_VPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001187 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001188 if (!i->f->set_vport_##field) { \
1189 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1190 i->private_vport_attrs[count].store = NULL; \
1191 } \
1192 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1193 count++
1194 /* NOTE: Above MACRO differs: does not check show bit */
1195
1196#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1197{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001198 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001199 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1200 count++; \
1201}
1202
1203
1204/* The FC Transport Virtual Port Attributes: */
1205
1206/* Fixed Virtual Port Attributes */
1207
1208/* Dynamic Virtual Port Attributes */
1209
1210/* Private Virtual Port Attributes */
1211
1212fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1213fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1214fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1215fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1216
1217static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001218show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1219 char *buf)
James Smarta53eb5e2007-04-27 12:41:09 -04001220{
Tony Jonesee959b02008-02-22 00:13:36 +01001221 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001222
1223 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1224 return snprintf(buf, 20, "unknown\n");
1225 return get_fc_port_roles_names(vport->roles, buf);
1226}
Tony Jonesee959b02008-02-22 00:13:36 +01001227static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
James Smarta53eb5e2007-04-27 12:41:09 -04001228
1229fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1230
1231fc_private_vport_show_function(symbolic_name, "%s\n",
1232 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1233fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
Tony Jonesee959b02008-02-22 00:13:36 +01001234static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001235 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1236
1237static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001238store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1239 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001240{
Tony Jonesee959b02008-02-22 00:13:36 +01001241 struct fc_vport *vport = transport_class_to_vport(dev);
James Smart9ef3e4a2007-05-24 19:04:44 -04001242 struct Scsi_Host *shost = vport_to_shost(vport);
Gal Rosen0d9dc7c2010-01-21 10:15:32 +02001243 unsigned long flags;
1244
1245 spin_lock_irqsave(shost->host_lock, flags);
1246 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
1247 spin_unlock_irqrestore(shost->host_lock, flags);
1248 return -EBUSY;
1249 }
1250 vport->flags |= FC_VPORT_DELETING;
1251 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04001252
James Smart9ef3e4a2007-05-24 19:04:44 -04001253 fc_queue_work(shost, &vport->vport_delete_work);
James Smarta53eb5e2007-04-27 12:41:09 -04001254 return count;
1255}
Tony Jonesee959b02008-02-22 00:13:36 +01001256static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001257 NULL, store_fc_vport_delete);
1258
1259
1260/*
1261 * Enable/Disable vport
1262 * Write "1" to disable, write "0" to enable
1263 */
1264static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001265store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1266 const char *buf,
James Smarta53eb5e2007-04-27 12:41:09 -04001267 size_t count)
1268{
Tony Jonesee959b02008-02-22 00:13:36 +01001269 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001270 struct Scsi_Host *shost = vport_to_shost(vport);
1271 struct fc_internal *i = to_fc_internal(shost->transportt);
1272 int stat;
1273
1274 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1275 return -EBUSY;
1276
1277 if (*buf == '0') {
1278 if (vport->vport_state != FC_VPORT_DISABLED)
1279 return -EALREADY;
1280 } else if (*buf == '1') {
1281 if (vport->vport_state == FC_VPORT_DISABLED)
1282 return -EALREADY;
1283 } else
1284 return -EINVAL;
1285
1286 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1287 return stat ? stat : count;
1288}
Tony Jonesee959b02008-02-22 00:13:36 +01001289static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001290 NULL, store_fc_vport_disable);
1291
1292
1293/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 * Host Attribute Management
1295 */
1296
1297#define fc_host_show_function(field, format_string, sz, cast) \
1298static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001299show_fc_host_##field (struct device *dev, \
1300 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001302 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 struct fc_internal *i = to_fc_internal(shost->transportt); \
1304 if (i->f->get_host_##field) \
1305 i->f->get_host_##field(shost); \
1306 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1307}
1308
1309#define fc_host_store_function(field) \
1310static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001311store_fc_host_##field(struct device *dev, \
1312 struct device_attribute *attr, \
1313 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{ \
1315 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001316 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -04001318 char *cp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 \
James Smart0f29b962006-08-18 17:33:29 -04001320 val = simple_strtoul(buf, &cp, 0); \
1321 if (*cp && (*cp != '\n')) \
1322 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 i->f->set_host_##field(shost, val); \
1324 return count; \
1325}
1326
James Smartb8d08212006-08-17 08:00:43 -04001327#define fc_host_store_str_function(field, slen) \
1328static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001329store_fc_host_##field(struct device *dev, \
1330 struct device_attribute *attr, \
1331 const char *buf, size_t count) \
James Smartb8d08212006-08-17 08:00:43 -04001332{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001333 struct Scsi_Host *shost = transport_class_to_shost(dev); \
James Smartb8d08212006-08-17 08:00:43 -04001334 struct fc_internal *i = to_fc_internal(shost->transportt); \
1335 unsigned int cnt=count; \
1336 \
1337 /* count may include a LF at end of string */ \
1338 if (buf[cnt-1] == '\n') \
1339 cnt--; \
1340 if (cnt > ((slen) - 1)) \
1341 return -EINVAL; \
1342 memcpy(fc_host_##field(shost), buf, cnt); \
1343 i->f->set_host_##field(shost); \
1344 return count; \
1345}
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347#define fc_host_rd_attr(field, format_string, sz) \
1348 fc_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001349static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 show_fc_host_##field, NULL)
1351
1352#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1353 fc_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001354static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 show_fc_host_##field, NULL)
1356
1357#define fc_host_rw_attr(field, format_string, sz) \
1358 fc_host_show_function(field, format_string, sz, ) \
1359 fc_host_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001360static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 show_fc_host_##field, \
1362 store_fc_host_##field)
1363
1364#define fc_host_rd_enum_attr(title, maxlen) \
1365static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001366show_fc_host_##title (struct device *dev, \
1367 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001369 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 struct fc_internal *i = to_fc_internal(shost->transportt); \
1371 const char *name; \
1372 if (i->f->get_host_##title) \
1373 i->f->get_host_##title(shost); \
1374 name = get_fc_##title##_name(fc_host_##title(shost)); \
1375 if (!name) \
1376 return -EINVAL; \
1377 return snprintf(buf, maxlen, "%s\n", name); \
1378} \
Tony Jonesee959b02008-02-22 00:13:36 +01001379static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381#define SETUP_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001382 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1384 i->private_host_attrs[count].store = NULL; \
1385 i->host_attrs[count] = &i->private_host_attrs[count]; \
1386 if (i->f->show_host_##field) \
1387 count++
1388
James Smarta53eb5e2007-04-27 12:41:09 -04001389#define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001390 i->private_host_attrs[count] = device_attr_host_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001391 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1392 i->private_host_attrs[count].store = NULL; \
1393 i->host_attrs[count] = &i->private_host_attrs[count]; \
1394 count++
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396#define SETUP_HOST_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001397 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (!i->f->set_host_##field) { \
1399 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1400 i->private_host_attrs[count].store = NULL; \
1401 } \
1402 i->host_attrs[count] = &i->private_host_attrs[count]; \
1403 if (i->f->show_host_##field) \
1404 count++
1405
1406
1407#define fc_private_host_show_function(field, format_string, sz, cast) \
1408static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001409show_fc_host_##field (struct device *dev, \
1410 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001412 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1414}
1415
1416#define fc_private_host_rd_attr(field, format_string, sz) \
1417 fc_private_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001418static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 show_fc_host_##field, NULL)
1420
1421#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1422 fc_private_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001423static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 show_fc_host_##field, NULL)
1425
1426#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001427 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1429 i->private_host_attrs[count].store = NULL; \
1430 i->host_attrs[count] = &i->private_host_attrs[count]; \
1431 count++
1432
1433#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001434{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001435 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 i->host_attrs[count] = &i->private_host_attrs[count]; \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001437 count++; \
1438}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440
1441/* Fixed Host Attributes */
1442
1443static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001444show_fc_host_supported_classes (struct device *dev,
1445 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Tony Jonesee959b02008-02-22 00:13:36 +01001447 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1450 return snprintf(buf, 20, "unspecified\n");
1451
1452 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1453}
Tony Jonesee959b02008-02-22 00:13:36 +01001454static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 show_fc_host_supported_classes, NULL);
1456
1457static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001458show_fc_host_supported_fc4s (struct device *dev,
1459 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Tony Jonesee959b02008-02-22 00:13:36 +01001461 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1463}
Tony Jonesee959b02008-02-22 00:13:36 +01001464static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 show_fc_host_supported_fc4s, NULL);
1466
1467static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001468show_fc_host_supported_speeds (struct device *dev,
1469 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Tony Jonesee959b02008-02-22 00:13:36 +01001471 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1474 return snprintf(buf, 20, "unknown\n");
1475
1476 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1477}
Tony Jonesee959b02008-02-22 00:13:36 +01001478static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 show_fc_host_supported_speeds, NULL);
1480
1481
1482fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1483fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01001484fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1485 unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
James Smarta53eb5e2007-04-27 12:41:09 -04001487fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1489
1490
1491/* Dynamic Host Attributes */
1492
1493static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001494show_fc_host_active_fc4s (struct device *dev,
1495 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Tony Jonesee959b02008-02-22 00:13:36 +01001497 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 struct fc_internal *i = to_fc_internal(shost->transportt);
1499
1500 if (i->f->get_host_active_fc4s)
1501 i->f->get_host_active_fc4s(shost);
1502
1503 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1504}
Tony Jonesee959b02008-02-22 00:13:36 +01001505static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 show_fc_host_active_fc4s, NULL);
1507
1508static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001509show_fc_host_speed (struct device *dev,
1510 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Tony Jonesee959b02008-02-22 00:13:36 +01001512 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct fc_internal *i = to_fc_internal(shost->transportt);
1514
1515 if (i->f->get_host_speed)
1516 i->f->get_host_speed(shost);
1517
1518 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1519 return snprintf(buf, 20, "unknown\n");
1520
1521 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1522}
Tony Jonesee959b02008-02-22 00:13:36 +01001523static FC_DEVICE_ATTR(host, speed, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 show_fc_host_speed, NULL);
1525
1526
1527fc_host_rd_attr(port_id, "0x%06x\n", 20);
1528fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1529fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1530fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
James Smart016131b2006-08-14 08:20:25 -04001531fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
James Smartb8d08212006-08-17 08:00:43 -04001533fc_private_host_show_function(system_hostname, "%s\n",
1534 FC_SYMBOLIC_NAME_SIZE + 1, )
1535fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
Tony Jonesee959b02008-02-22 00:13:36 +01001536static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
James Smartb8d08212006-08-17 08:00:43 -04001537 show_fc_host_system_hostname, store_fc_host_system_hostname);
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540/* Private Host Attributes */
1541
1542static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001543show_fc_private_host_tgtid_bind_type(struct device *dev,
1544 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Tony Jonesee959b02008-02-22 00:13:36 +01001546 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 const char *name;
1548
1549 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1550 if (!name)
1551 return -EINVAL;
1552 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1553}
1554
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001555#define get_list_head_entry(pos, head, member) \
1556 pos = list_entry((head)->next, typeof(*pos), member)
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001559store_fc_private_host_tgtid_bind_type(struct device *dev,
1560 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Tony Jonesee959b02008-02-22 00:13:36 +01001562 struct Scsi_Host *shost = transport_class_to_shost(dev);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001563 struct fc_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 enum fc_tgtid_binding_type val;
1565 unsigned long flags;
1566
1567 if (get_fc_tgtid_bind_type_match(buf, &val))
1568 return -EINVAL;
1569
1570 /* if changing bind type, purge all unused consistent bindings */
1571 if (val != fc_host_tgtid_bind_type(shost)) {
1572 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001573 while (!list_empty(&fc_host_rport_bindings(shost))) {
1574 get_list_head_entry(rport,
1575 &fc_host_rport_bindings(shost), peers);
James Smartaedf3492006-04-10 10:14:05 -04001576 list_del(&rport->peers);
1577 rport->port_state = FC_PORTSTATE_DELETED;
1578 fc_queue_work(shost, &rport->rport_delete_work);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 spin_unlock_irqrestore(shost->host_lock, flags);
1581 }
1582
1583 fc_host_tgtid_bind_type(shost) = val;
1584 return count;
1585}
1586
Tony Jonesee959b02008-02-22 00:13:36 +01001587static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 show_fc_private_host_tgtid_bind_type,
1589 store_fc_private_host_tgtid_bind_type);
1590
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001591static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001592store_fc_private_host_issue_lip(struct device *dev,
1593 struct device_attribute *attr, const char *buf, size_t count)
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001594{
Tony Jonesee959b02008-02-22 00:13:36 +01001595 struct Scsi_Host *shost = transport_class_to_shost(dev);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001596 struct fc_internal *i = to_fc_internal(shost->transportt);
1597 int ret;
1598
1599 /* ignore any data value written to the attribute */
1600 if (i->f->issue_fc_host_lip) {
1601 ret = i->f->issue_fc_host_lip(shost);
1602 return ret ? ret: count;
1603 }
1604
1605 return -ENOENT;
1606}
1607
Tony Jonesee959b02008-02-22 00:13:36 +01001608static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001609 store_fc_private_host_issue_lip);
1610
James Smarta53eb5e2007-04-27 12:41:09 -04001611fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1612
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614/*
1615 * Host Statistics Management
1616 */
1617
1618/* Show a given an attribute in the statistics group */
1619static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001620fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Tony Jonesee959b02008-02-22 00:13:36 +01001622 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 struct fc_internal *i = to_fc_internal(shost->transportt);
1624 struct fc_host_statistics *stats;
1625 ssize_t ret = -ENOENT;
1626
1627 if (offset > sizeof(struct fc_host_statistics) ||
1628 offset % sizeof(u64) != 0)
1629 WARN_ON(1);
1630
1631 if (i->f->get_fc_host_stats) {
1632 stats = (i->f->get_fc_host_stats)(shost);
1633 if (stats)
1634 ret = snprintf(buf, 20, "0x%llx\n",
1635 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1636 }
1637 return ret;
1638}
1639
1640
1641/* generate a read-only statistics attribute */
1642#define fc_host_statistic(name) \
Tony Jonesee959b02008-02-22 00:13:36 +01001643static ssize_t show_fcstat_##name(struct device *cd, \
1644 struct device_attribute *attr, \
1645 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{ \
1647 return fc_stat_show(cd, buf, \
1648 offsetof(struct fc_host_statistics, name)); \
1649} \
Tony Jonesee959b02008-02-22 00:13:36 +01001650static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652fc_host_statistic(seconds_since_last_reset);
1653fc_host_statistic(tx_frames);
1654fc_host_statistic(tx_words);
1655fc_host_statistic(rx_frames);
1656fc_host_statistic(rx_words);
1657fc_host_statistic(lip_count);
1658fc_host_statistic(nos_count);
1659fc_host_statistic(error_frames);
1660fc_host_statistic(dumped_frames);
1661fc_host_statistic(link_failure_count);
1662fc_host_statistic(loss_of_sync_count);
1663fc_host_statistic(loss_of_signal_count);
1664fc_host_statistic(prim_seq_protocol_err_count);
1665fc_host_statistic(invalid_tx_word_count);
1666fc_host_statistic(invalid_crc_count);
1667fc_host_statistic(fcp_input_requests);
1668fc_host_statistic(fcp_output_requests);
1669fc_host_statistic(fcp_control_requests);
1670fc_host_statistic(fcp_input_megabytes);
1671fc_host_statistic(fcp_output_megabytes);
1672
1673static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001674fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1675 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Tony Jonesee959b02008-02-22 00:13:36 +01001677 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 struct fc_internal *i = to_fc_internal(shost->transportt);
1679
1680 /* ignore any data value written to the attribute */
1681 if (i->f->reset_fc_host_stats) {
1682 i->f->reset_fc_host_stats(shost);
1683 return count;
1684 }
1685
1686 return -ENOENT;
1687}
Tony Jonesee959b02008-02-22 00:13:36 +01001688static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 fc_reset_statistics);
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691static struct attribute *fc_statistics_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001692 &device_attr_host_seconds_since_last_reset.attr,
1693 &device_attr_host_tx_frames.attr,
1694 &device_attr_host_tx_words.attr,
1695 &device_attr_host_rx_frames.attr,
1696 &device_attr_host_rx_words.attr,
1697 &device_attr_host_lip_count.attr,
1698 &device_attr_host_nos_count.attr,
1699 &device_attr_host_error_frames.attr,
1700 &device_attr_host_dumped_frames.attr,
1701 &device_attr_host_link_failure_count.attr,
1702 &device_attr_host_loss_of_sync_count.attr,
1703 &device_attr_host_loss_of_signal_count.attr,
1704 &device_attr_host_prim_seq_protocol_err_count.attr,
1705 &device_attr_host_invalid_tx_word_count.attr,
1706 &device_attr_host_invalid_crc_count.attr,
1707 &device_attr_host_fcp_input_requests.attr,
1708 &device_attr_host_fcp_output_requests.attr,
1709 &device_attr_host_fcp_control_requests.attr,
1710 &device_attr_host_fcp_input_megabytes.attr,
1711 &device_attr_host_fcp_output_megabytes.attr,
1712 &device_attr_host_reset_statistics.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 NULL
1714};
1715
1716static struct attribute_group fc_statistics_group = {
1717 .name = "statistics",
1718 .attrs = fc_statistics_attrs,
1719};
1720
James Smarta53eb5e2007-04-27 12:41:09 -04001721
1722/* Host Vport Attributes */
1723
1724static int
1725fc_parse_wwn(const char *ns, u64 *nm)
1726{
1727 unsigned int i, j;
1728 u8 wwn[8];
1729
1730 memset(wwn, 0, sizeof(wwn));
1731
1732 /* Validate and store the new name */
1733 for (i=0, j=0; i < 16; i++) {
Andy Shevchenkoecc30992010-08-10 18:01:27 -07001734 int value;
1735
1736 value = hex_to_bin(*ns++);
1737 if (value >= 0)
1738 j = (j << 4) | value;
James Smarta53eb5e2007-04-27 12:41:09 -04001739 else
1740 return -EINVAL;
1741 if (i % 2) {
1742 wwn[i/2] = j & 0xff;
1743 j = 0;
1744 }
1745 }
1746
1747 *nm = wwn_to_u64(wwn);
1748
1749 return 0;
1750}
1751
1752
1753/*
1754 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1755 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1756 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1757 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1758 */
1759static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001760store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1761 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001762{
Tony Jonesee959b02008-02-22 00:13:36 +01001763 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001764 struct fc_vport_identifiers vid;
1765 struct fc_vport *vport;
1766 unsigned int cnt=count;
1767 int stat;
1768
1769 memset(&vid, 0, sizeof(vid));
1770
1771 /* count may include a LF at end of string */
1772 if (buf[cnt-1] == '\n')
1773 cnt--;
1774
1775 /* validate we have enough characters for WWPN */
1776 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1777 return -EINVAL;
1778
1779 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1780 if (stat)
1781 return stat;
1782
1783 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1784 if (stat)
1785 return stat;
1786
1787 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1788 vid.vport_type = FC_PORTTYPE_NPIV;
1789 /* vid.symbolic_name is already zero/NULL's */
1790 vid.disable = false; /* always enabled */
1791
1792 /* we only allow support on Channel 0 !!! */
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07001793 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
James Smarta53eb5e2007-04-27 12:41:09 -04001794 return stat ? stat : count;
1795}
Tony Jonesee959b02008-02-22 00:13:36 +01001796static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001797 store_fc_host_vport_create);
1798
1799
1800/*
1801 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1802 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1803 * The WWNs are specified as hex characters, and may *not* contain
1804 * any prefixes (e.g. 0x, x, etc)
1805 */
1806static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001807store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1808 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001809{
Tony Jonesee959b02008-02-22 00:13:36 +01001810 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001811 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1812 struct fc_vport *vport;
1813 u64 wwpn, wwnn;
1814 unsigned long flags;
1815 unsigned int cnt=count;
1816 int stat, match;
1817
1818 /* count may include a LF at end of string */
1819 if (buf[cnt-1] == '\n')
1820 cnt--;
1821
1822 /* validate we have enough characters for WWPN */
1823 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1824 return -EINVAL;
1825
1826 stat = fc_parse_wwn(&buf[0], &wwpn);
1827 if (stat)
1828 return stat;
1829
1830 stat = fc_parse_wwn(&buf[17], &wwnn);
1831 if (stat)
1832 return stat;
1833
1834 spin_lock_irqsave(shost->host_lock, flags);
1835 match = 0;
1836 /* we only allow support on Channel 0 !!! */
1837 list_for_each_entry(vport, &fc_host->vports, peers) {
1838 if ((vport->channel == 0) &&
1839 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
Gal Rosen0d9dc7c2010-01-21 10:15:32 +02001840 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1841 break;
1842 vport->flags |= FC_VPORT_DELETING;
James Smarta53eb5e2007-04-27 12:41:09 -04001843 match = 1;
1844 break;
1845 }
1846 }
1847 spin_unlock_irqrestore(shost->host_lock, flags);
1848
1849 if (!match)
1850 return -ENODEV;
1851
1852 stat = fc_vport_terminate(vport);
1853 return stat ? stat : count;
1854}
Tony Jonesee959b02008-02-22 00:13:36 +01001855static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001856 store_fc_host_vport_delete);
1857
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859static int fc_host_match(struct attribute_container *cont,
1860 struct device *dev)
1861{
1862 struct Scsi_Host *shost;
1863 struct fc_internal *i;
1864
1865 if (!scsi_is_host_device(dev))
1866 return 0;
1867
1868 shost = dev_to_shost(dev);
1869 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1870 != &fc_host_class.class)
1871 return 0;
1872
1873 i = to_fc_internal(shost->transportt);
1874
1875 return &i->t.host_attrs.ac == cont;
1876}
1877
1878static int fc_target_match(struct attribute_container *cont,
1879 struct device *dev)
1880{
1881 struct Scsi_Host *shost;
1882 struct fc_internal *i;
1883
1884 if (!scsi_is_target_device(dev))
1885 return 0;
1886
1887 shost = dev_to_shost(dev->parent);
1888 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1889 != &fc_host_class.class)
1890 return 0;
1891
1892 i = to_fc_internal(shost->transportt);
1893
1894 return &i->t.target_attrs.ac == cont;
1895}
1896
1897static void fc_rport_dev_release(struct device *dev)
1898{
1899 struct fc_rport *rport = dev_to_rport(dev);
1900 put_device(dev->parent);
1901 kfree(rport);
1902}
1903
1904int scsi_is_fc_rport(const struct device *dev)
1905{
1906 return dev->release == fc_rport_dev_release;
1907}
1908EXPORT_SYMBOL(scsi_is_fc_rport);
1909
1910static int fc_rport_match(struct attribute_container *cont,
1911 struct device *dev)
1912{
1913 struct Scsi_Host *shost;
1914 struct fc_internal *i;
1915
1916 if (!scsi_is_fc_rport(dev))
1917 return 0;
1918
1919 shost = dev_to_shost(dev->parent);
1920 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1921 != &fc_host_class.class)
1922 return 0;
1923
1924 i = to_fc_internal(shost->transportt);
1925
1926 return &i->rport_attr_cont.ac == cont;
1927}
1928
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001929
James Smarta53eb5e2007-04-27 12:41:09 -04001930static void fc_vport_dev_release(struct device *dev)
1931{
1932 struct fc_vport *vport = dev_to_vport(dev);
1933 put_device(dev->parent); /* release kobj parent */
1934 kfree(vport);
1935}
1936
1937int scsi_is_fc_vport(const struct device *dev)
1938{
1939 return dev->release == fc_vport_dev_release;
1940}
1941EXPORT_SYMBOL(scsi_is_fc_vport);
1942
1943static int fc_vport_match(struct attribute_container *cont,
1944 struct device *dev)
1945{
1946 struct fc_vport *vport;
1947 struct Scsi_Host *shost;
1948 struct fc_internal *i;
1949
1950 if (!scsi_is_fc_vport(dev))
1951 return 0;
1952 vport = dev_to_vport(dev);
1953
1954 shost = vport_to_shost(vport);
1955 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1956 != &fc_host_class.class)
1957 return 0;
1958
1959 i = to_fc_internal(shost->transportt);
1960 return &i->vport_attr_cont.ac == cont;
1961}
1962
1963
James Smartc829c392006-03-13 08:28:57 -05001964/**
1965 * fc_timed_out - FC Transport I/O timeout intercept handler
James Smartc829c392006-03-13 08:28:57 -05001966 * @scmd: The SCSI command which timed out
1967 *
1968 * This routine protects against error handlers getting invoked while a
1969 * rport is in a blocked state, typically due to a temporarily loss of
1970 * connectivity. If the error handlers are allowed to proceed, requests
1971 * to abort i/o, reset the target, etc will likely fail as there is no way
1972 * to communicate with the device to perform the requested function. These
1973 * failures may result in the midlayer taking the device offline, requiring
1974 * manual intervention to restore operation.
1975 *
1976 * This routine, called whenever an i/o times out, validates the state of
1977 * the underlying rport. If the rport is blocked, it returns
1978 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1979 * Eventually, either the device will return, or devloss_tmo will fire,
1980 * and when the timeout then fires, it will be handled normally.
1981 * If the rport is not blocked, normal error handling continues.
1982 *
1983 * Notes:
1984 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05001985 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001986static enum blk_eh_timer_return
James Smartc829c392006-03-13 08:28:57 -05001987fc_timed_out(struct scsi_cmnd *scmd)
1988{
1989 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1990
1991 if (rport->port_state == FC_PORTSTATE_BLOCKED)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001992 return BLK_EH_RESET_TIMER;
James Smartc829c392006-03-13 08:28:57 -05001993
Jens Axboe242f9dc2008-09-14 05:55:09 -07001994 return BLK_EH_NOT_HANDLED;
James Smartc829c392006-03-13 08:28:57 -05001995}
1996
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001997/*
James Smartbda23252008-04-24 12:12:46 -04001998 * Called by fc_user_scan to locate an rport on the shost that
1999 * matches the channel and target id, and invoke scsi_scan_target()
2000 * on the rport.
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002001 */
James Smartbda23252008-04-24 12:12:46 -04002002static void
2003fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002004{
2005 struct fc_rport *rport;
James Smartbda23252008-04-24 12:12:46 -04002006 unsigned long flags;
2007
2008 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002009
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002010 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2011 if (rport->scsi_target_id == -1)
2012 continue;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002013
Hannes Reinecke0d2fcd92007-06-14 15:16:45 +02002014 if (rport->port_state != FC_PORTSTATE_ONLINE)
2015 continue;
2016
James Smartbda23252008-04-24 12:12:46 -04002017 if ((channel == rport->channel) &&
2018 (id == rport->scsi_target_id)) {
2019 spin_unlock_irqrestore(shost->host_lock, flags);
2020 scsi_scan_target(&rport->dev, channel, id, lun, 1);
2021 return;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002022 }
2023 }
2024
James Smartbda23252008-04-24 12:12:46 -04002025 spin_unlock_irqrestore(shost->host_lock, flags);
2026}
2027
2028/*
2029 * Called via sysfs scan routines. Necessary, as the FC transport
2030 * wants to place all target objects below the rport object. So this
2031 * routine must invoke the scsi_scan_target() routine with the rport
2032 * object as the parent.
2033 */
2034static int
2035fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2036{
2037 uint chlo, chhi;
2038 uint tgtlo, tgthi;
2039
2040 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2041 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2042 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2043 return -EINVAL;
2044
2045 if (channel == SCAN_WILD_CARD) {
2046 chlo = 0;
2047 chhi = shost->max_channel + 1;
2048 } else {
2049 chlo = channel;
2050 chhi = channel + 1;
2051 }
2052
2053 if (id == SCAN_WILD_CARD) {
2054 tgtlo = 0;
2055 tgthi = shost->max_id;
2056 } else {
2057 tgtlo = id;
2058 tgthi = id + 1;
2059 }
2060
2061 for ( ; chlo < chhi; chlo++)
2062 for ( ; tgtlo < tgthi; tgtlo++)
2063 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2064
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002065 return 0;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002066}
2067
FUJITA Tomonori75252362007-09-01 02:02:27 +09002068static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
2069 int result)
2070{
2071 struct fc_internal *i = to_fc_internal(shost->transportt);
2072 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
2073}
2074
2075static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
2076{
2077 struct fc_internal *i = to_fc_internal(shost->transportt);
2078 return i->f->it_nexus_response(shost, nexus, result);
2079}
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081struct scsi_transport_template *
2082fc_attach_transport(struct fc_function_template *ft)
2083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 int count;
Jes Sorensen24669f752006-01-16 10:31:18 -05002085 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2086 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 if (unlikely(!i))
2089 return NULL;
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2092 i->t.target_attrs.ac.class = &fc_transport_class.class;
2093 i->t.target_attrs.ac.match = fc_target_match;
2094 i->t.target_size = sizeof(struct fc_starget_attrs);
2095 transport_container_register(&i->t.target_attrs);
2096
2097 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2098 i->t.host_attrs.ac.class = &fc_host_class.class;
2099 i->t.host_attrs.ac.match = fc_host_match;
2100 i->t.host_size = sizeof(struct fc_host_attrs);
2101 if (ft->get_fc_host_stats)
2102 i->t.host_attrs.statistics = &fc_statistics_group;
2103 transport_container_register(&i->t.host_attrs);
2104
2105 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2106 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2107 i->rport_attr_cont.ac.match = fc_rport_match;
2108 transport_container_register(&i->rport_attr_cont);
2109
James Smarta53eb5e2007-04-27 12:41:09 -04002110 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2111 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2112 i->vport_attr_cont.ac.match = fc_vport_match;
2113 transport_container_register(&i->vport_attr_cont);
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 i->f = ft;
2116
2117 /* Transport uses the shost workq for scsi scanning */
2118 i->t.create_work_queue = 1;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002119
James Smartc829c392006-03-13 08:28:57 -05002120 i->t.eh_timed_out = fc_timed_out;
2121
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002122 i->t.user_scan = fc_user_scan;
James Smart9ef3e4a2007-05-24 19:04:44 -04002123
FUJITA Tomonori75252362007-09-01 02:02:27 +09002124 /* target-mode drivers' functions */
2125 i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2126 i->t.it_nexus_response = fc_it_nexus_response;
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /*
2129 * Setup SCSI Target Attributes.
2130 */
2131 count = 0;
2132 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2133 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2134 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2135
2136 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2137
2138 i->starget_attrs[count] = NULL;
2139
2140
2141 /*
2142 * Setup SCSI Host Attributes.
2143 */
2144 count=0;
2145 SETUP_HOST_ATTRIBUTE_RD(node_name);
2146 SETUP_HOST_ATTRIBUTE_RD(port_name);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01002147 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2149 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2151 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
James Smarta53eb5e2007-04-27 12:41:09 -04002152 if (ft->vport_create) {
2153 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2154 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2157
2158 SETUP_HOST_ATTRIBUTE_RD(port_id);
2159 SETUP_HOST_ATTRIBUTE_RD(port_type);
2160 SETUP_HOST_ATTRIBUTE_RD(port_state);
2161 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2162 SETUP_HOST_ATTRIBUTE_RD(speed);
2163 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
James Smart016131b2006-08-14 08:20:25 -04002164 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
James Smartb8d08212006-08-17 08:00:43 -04002165 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 /* Transport-managed attributes */
2168 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002169 if (ft->issue_fc_host_lip)
2170 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
James Smarta53eb5e2007-04-27 12:41:09 -04002171 if (ft->vport_create)
2172 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2173 if (ft->vport_delete)
2174 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 BUG_ON(count > FC_HOST_NUM_ATTRS);
2177
2178 i->host_attrs[count] = NULL;
2179
2180 /*
2181 * Setup Remote Port Attributes.
2182 */
2183 count=0;
2184 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2185 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2186 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2187 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2188 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2189 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2190 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2191 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2192 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
Mike Christiefff9d402008-08-19 18:45:23 -05002193 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2196
2197 i->rport_attrs[count] = NULL;
2198
James Smarta53eb5e2007-04-27 12:41:09 -04002199 /*
2200 * Setup Virtual Port Attributes.
2201 */
2202 count=0;
2203 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2204 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2205 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2206 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2207 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2208 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2209 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2210 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2211 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2212
2213 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2214
2215 i->vport_attrs[count] = NULL;
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return &i->t;
2218}
2219EXPORT_SYMBOL(fc_attach_transport);
2220
2221void fc_release_transport(struct scsi_transport_template *t)
2222{
2223 struct fc_internal *i = to_fc_internal(t);
2224
2225 transport_container_unregister(&i->t.target_attrs);
2226 transport_container_unregister(&i->t.host_attrs);
2227 transport_container_unregister(&i->rport_attr_cont);
James Smarta53eb5e2007-04-27 12:41:09 -04002228 transport_container_unregister(&i->vport_attr_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 kfree(i);
2231}
2232EXPORT_SYMBOL(fc_release_transport);
2233
James Smartaedf3492006-04-10 10:14:05 -04002234/**
2235 * fc_queue_work - Queue work to the fc_host workqueue.
2236 * @shost: Pointer to Scsi_Host bound to fc_host.
2237 * @work: Work to queue for execution.
2238 *
2239 * Return value:
James Smarta0785ed2006-05-11 13:27:09 -04002240 * 1 - work queued for execution
2241 * 0 - work is already queued
2242 * -EINVAL - work queue doesn't exist
Rob Landleyeb448202007-11-03 13:30:39 -05002243 */
James Smartaedf3492006-04-10 10:14:05 -04002244static int
2245fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2246{
2247 if (unlikely(!fc_host_work_q(shost))) {
2248 printk(KERN_ERR
2249 "ERROR: FC host '%s' attempted to queue work, "
2250 "when no workqueue created.\n", shost->hostt->name);
2251 dump_stack();
2252
2253 return -EINVAL;
2254 }
2255
2256 return queue_work(fc_host_work_q(shost), work);
2257}
2258
2259/**
2260 * fc_flush_work - Flush a fc_host's workqueue.
2261 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002262 */
James Smartaedf3492006-04-10 10:14:05 -04002263static void
2264fc_flush_work(struct Scsi_Host *shost)
2265{
2266 if (!fc_host_work_q(shost)) {
2267 printk(KERN_ERR
2268 "ERROR: FC host '%s' attempted to flush work, "
2269 "when no workqueue created.\n", shost->hostt->name);
2270 dump_stack();
2271 return;
2272 }
2273
2274 flush_workqueue(fc_host_work_q(shost));
2275}
2276
2277/**
2278 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2279 * @shost: Pointer to Scsi_Host bound to fc_host.
2280 * @work: Work to queue for execution.
2281 * @delay: jiffies to delay the work queuing
2282 *
2283 * Return value:
James Smart0f29b962006-08-18 17:33:29 -04002284 * 1 on success / 0 already queued / < 0 for error
Rob Landleyeb448202007-11-03 13:30:39 -05002285 */
James Smartaedf3492006-04-10 10:14:05 -04002286static int
David Howellsc4028952006-11-22 14:57:56 +00002287fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
James Smartaedf3492006-04-10 10:14:05 -04002288 unsigned long delay)
2289{
2290 if (unlikely(!fc_host_devloss_work_q(shost))) {
2291 printk(KERN_ERR
2292 "ERROR: FC host '%s' attempted to queue work, "
2293 "when no workqueue created.\n", shost->hostt->name);
2294 dump_stack();
2295
2296 return -EINVAL;
2297 }
2298
2299 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2300}
2301
2302/**
2303 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2304 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002305 */
James Smartaedf3492006-04-10 10:14:05 -04002306static void
2307fc_flush_devloss(struct Scsi_Host *shost)
2308{
2309 if (!fc_host_devloss_work_q(shost)) {
2310 printk(KERN_ERR
2311 "ERROR: FC host '%s' attempted to flush work, "
2312 "when no workqueue created.\n", shost->hostt->name);
2313 dump_stack();
2314 return;
2315 }
2316
2317 flush_workqueue(fc_host_devloss_work_q(shost));
2318}
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321/**
Rob Landleyeb448202007-11-03 13:30:39 -05002322 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2323 * @shost: Which &Scsi_Host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 *
2325 * This routine is expected to be called immediately preceeding the
2326 * a driver's call to scsi_remove_host().
2327 *
2328 * WARNING: A driver utilizing the fc_transport, which fails to call
Rob Landleyeb448202007-11-03 13:30:39 -05002329 * this routine prior to scsi_remove_host(), will leave dangling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 * objects in /sys/class/fc_remote_ports. Access to any of these
2331 * objects can result in a system crash !!!
2332 *
2333 * Notes:
2334 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336void
2337fc_remove_host(struct Scsi_Host *shost)
2338{
James Smarta53eb5e2007-04-27 12:41:09 -04002339 struct fc_vport *vport = NULL, *next_vport = NULL;
2340 struct fc_rport *rport = NULL, *next_rport = NULL;
James Smartaedf3492006-04-10 10:14:05 -04002341 struct workqueue_struct *work_q;
2342 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James Smarta53eb5e2007-04-27 12:41:09 -04002343 unsigned long flags;
James Smarta53eb5e2007-04-27 12:41:09 -04002344
2345 spin_lock_irqsave(shost->host_lock, flags);
2346
2347 /* Remove any vports */
James Smart9ef3e4a2007-05-24 19:04:44 -04002348 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2349 fc_queue_work(shost, &vport->vport_delete_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 /* Remove any remote ports */
2352 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002353 &fc_host->rports, peers) {
2354 list_del(&rport->peers);
2355 rport->port_state = FC_PORTSTATE_DELETED;
2356 fc_queue_work(shost, &rport->rport_delete_work);
2357 }
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002360 &fc_host->rport_bindings, peers) {
2361 list_del(&rport->peers);
2362 rport->port_state = FC_PORTSTATE_DELETED;
2363 fc_queue_work(shost, &rport->rport_delete_work);
2364 }
2365
James Smarta53eb5e2007-04-27 12:41:09 -04002366 spin_unlock_irqrestore(shost->host_lock, flags);
2367
James Smartaedf3492006-04-10 10:14:05 -04002368 /* flush all scan work items */
2369 scsi_flush_work(shost);
2370
2371 /* flush all stgt delete, and rport delete work items, then kill it */
2372 if (fc_host->work_q) {
2373 work_q = fc_host->work_q;
2374 fc_host->work_q = NULL;
2375 destroy_workqueue(work_q);
2376 }
2377
2378 /* flush all devloss work items, then kill it */
2379 if (fc_host->devloss_work_q) {
2380 work_q = fc_host->devloss_work_q;
2381 fc_host->devloss_work_q = NULL;
2382 destroy_workqueue(work_q);
2383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385EXPORT_SYMBOL(fc_remove_host);
2386
Mike Christiefff9d402008-08-19 18:45:23 -05002387static void fc_terminate_rport_io(struct fc_rport *rport)
2388{
2389 struct Scsi_Host *shost = rport_to_shost(rport);
2390 struct fc_internal *i = to_fc_internal(shost->transportt);
2391
2392 /* Involve the LLDD if possible to terminate all io on the rport. */
2393 if (i->f->terminate_rport_io)
2394 i->f->terminate_rport_io(rport);
2395
2396 /*
2397 * must unblock to flush queued IO. The caller will have set
2398 * the port_state or flags, so that fc_remote_port_chkready will
2399 * fail IO.
2400 */
2401 scsi_target_unblock(&rport->dev);
2402}
James Smartaedf3492006-04-10 10:14:05 -04002403
2404/**
2405 * fc_starget_delete - called to delete the scsi decendents of an rport
David Howellsc4028952006-11-22 14:57:56 +00002406 * @work: remote port to be operated on.
Rob Landleyeb448202007-11-03 13:30:39 -05002407 *
2408 * Deletes target and all sdevs.
2409 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002410static void
David Howellsc4028952006-11-22 14:57:56 +00002411fc_starget_delete(struct work_struct *work)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002412{
David Howellsc4028952006-11-22 14:57:56 +00002413 struct fc_rport *rport =
2414 container_of(work, struct fc_rport, stgt_delete_work);
James Smart0f29b962006-08-18 17:33:29 -04002415
Mike Christiefff9d402008-08-19 18:45:23 -05002416 fc_terminate_rport_io(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002417 scsi_remove_target(&rport->dev);
2418}
2419
James Smartaedf3492006-04-10 10:14:05 -04002420
2421/**
2422 * fc_rport_final_delete - finish rport termination and delete it.
David Howellsc4028952006-11-22 14:57:56 +00002423 * @work: remote port to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05002424 */
James Smartaedf3492006-04-10 10:14:05 -04002425static void
David Howellsc4028952006-11-22 14:57:56 +00002426fc_rport_final_delete(struct work_struct *work)
James Smartaedf3492006-04-10 10:14:05 -04002427{
David Howellsc4028952006-11-22 14:57:56 +00002428 struct fc_rport *rport =
2429 container_of(work, struct fc_rport, rport_delete_work);
James Smartaedf3492006-04-10 10:14:05 -04002430 struct device *dev = &rport->dev;
2431 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002432 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart92740b22007-04-27 11:53:17 -04002433 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05002434 int do_callback = 0;
James Smartaedf3492006-04-10 10:14:05 -04002435
2436 /*
James Smart9ef3e4a2007-05-24 19:04:44 -04002437 * if a scan is pending, flush the SCSI Host work_q so that
James Smartaedf3492006-04-10 10:14:05 -04002438 * that we can reclaim the rport scan work element.
2439 */
2440 if (rport->flags & FC_RPORT_SCAN_PENDING)
2441 scsi_flush_work(shost);
2442
Mike Christiefff9d402008-08-19 18:45:23 -05002443 fc_terminate_rport_io(rport);
James Smart9e4f5e22009-03-26 13:33:19 -04002444
James Smart92740b22007-04-27 11:53:17 -04002445 /*
2446 * Cancel any outstanding timers. These should really exist
2447 * only when rmmod'ing the LLDD and we're asking for
2448 * immediate termination of the rports
2449 */
2450 spin_lock_irqsave(shost->host_lock, flags);
2451 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2452 spin_unlock_irqrestore(shost->host_lock, flags);
2453 if (!cancel_delayed_work(&rport->fail_io_work))
2454 fc_flush_devloss(shost);
2455 if (!cancel_delayed_work(&rport->dev_loss_work))
2456 fc_flush_devloss(shost);
2457 spin_lock_irqsave(shost->host_lock, flags);
2458 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2459 }
2460 spin_unlock_irqrestore(shost->host_lock, flags);
2461
James Smart0f29b962006-08-18 17:33:29 -04002462 /* Delete SCSI target and sdevs */
2463 if (rport->scsi_target_id != -1)
David Howellsc4028952006-11-22 14:57:56 +00002464 fc_starget_delete(&rport->stgt_delete_work);
James Smart92740b22007-04-27 11:53:17 -04002465
2466 /*
2467 * Notify the driver that the rport is now dead. The LLDD will
2468 * also guarantee that any communication to the rport is terminated
James Smart4be98c02009-01-05 12:14:18 -05002469 *
2470 * Avoid this call if we already called it when we preserved the
2471 * rport for the binding.
James Smart92740b22007-04-27 11:53:17 -04002472 */
Michael Reed8798a692009-10-09 14:15:59 -05002473 spin_lock_irqsave(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05002474 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
Michael Reed8798a692009-10-09 14:15:59 -05002475 (i->f->dev_loss_tmo_callbk)) {
2476 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2477 do_callback = 1;
2478 }
2479 spin_unlock_irqrestore(shost->host_lock, flags);
2480
2481 if (do_callback)
James Smart0f29b962006-08-18 17:33:29 -04002482 i->f->dev_loss_tmo_callbk(rport);
James Smart0f29b962006-08-18 17:33:29 -04002483
James Smart9e4f5e22009-03-26 13:33:19 -04002484 fc_bsg_remove(rport->rqst_q);
2485
James Smartaedf3492006-04-10 10:14:05 -04002486 transport_remove_device(dev);
2487 device_del(dev);
2488 transport_destroy_device(dev);
James Smart3bdad7b2006-06-26 14:19:59 -04002489 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2490 put_device(dev); /* for self-reference */
James Smartaedf3492006-04-10 10:14:05 -04002491}
2492
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494/**
2495 * fc_rport_create - allocates and creates a remote FC port.
2496 * @shost: scsi host the remote port is connected to.
2497 * @channel: Channel on shost port connected to.
2498 * @ids: The world wide names, fc address, and FC4 port
2499 * roles for the remote port.
2500 *
2501 * Allocates and creates the remoter port structure, including the
2502 * class and sysfs creation.
2503 *
2504 * Notes:
2505 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002506 */
Adrian Bunk44818ef2007-07-09 11:59:59 -07002507static struct fc_rport *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508fc_rport_create(struct Scsi_Host *shost, int channel,
2509 struct fc_rport_identifiers *ids)
2510{
James Smartaedf3492006-04-10 10:14:05 -04002511 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 struct fc_internal *fci = to_fc_internal(shost->transportt);
2513 struct fc_rport *rport;
2514 struct device *dev;
2515 unsigned long flags;
2516 int error;
2517 size_t size;
2518
2519 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
Jes Sorensen24669f752006-01-16 10:31:18 -05002520 rport = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (unlikely(!rport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002522 printk(KERN_ERR "%s: allocation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return NULL;
2524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 rport->maxframe_size = -1;
2527 rport->supported_classes = FC_COS_UNSPECIFIED;
Mike Christieb8ef3202010-08-06 03:02:37 -05002528 if (fci->f->get_host_def_dev_loss_tmo) {
2529 fci->f->get_host_def_dev_loss_tmo(shost);
2530 rport->dev_loss_tmo = fc_host_def_dev_loss_tmo(shost);
2531 } else
2532 rport->dev_loss_tmo = fc_dev_loss_tmo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2534 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2535 rport->port_id = ids->port_id;
2536 rport->roles = ids->roles;
2537 rport->port_state = FC_PORTSTATE_ONLINE;
2538 if (fci->f->dd_fcrport_size)
2539 rport->dd_data = &rport[1];
2540 rport->channel = channel;
James Smart0f29b962006-08-18 17:33:29 -04002541 rport->fast_io_fail_tmo = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
David Howellsc4028952006-11-22 14:57:56 +00002543 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2544 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2545 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2546 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2547 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 spin_lock_irqsave(shost->host_lock, flags);
2550
2551 rport->number = fc_host->next_rport_number++;
James Smarta53eb5e2007-04-27 12:41:09 -04002552 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 rport->scsi_target_id = fc_host->next_target_id++;
2554 else
2555 rport->scsi_target_id = -1;
James Smartaedf3492006-04-10 10:14:05 -04002556 list_add_tail(&rport->peers, &fc_host->rports);
James Smart3bdad7b2006-06-26 14:19:59 -04002557 get_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 spin_unlock_irqrestore(shost->host_lock, flags);
2560
2561 dev = &rport->dev;
James Smart3bdad7b2006-06-26 14:19:59 -04002562 device_initialize(dev); /* takes self reference */
2563 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 dev->release = fc_rport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01002565 dev_set_name(dev, "rport-%d:%d-%d",
2566 shost->host_no, channel, rport->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 transport_setup_device(dev);
2568
2569 error = device_add(dev);
2570 if (error) {
2571 printk(KERN_ERR "FC Remote Port device_add failed\n");
2572 goto delete_rport;
2573 }
2574 transport_add_device(dev);
2575 transport_configure_device(dev);
2576
James Smart9e4f5e22009-03-26 13:33:19 -04002577 fc_bsg_rportadd(shost, rport);
2578 /* ignore any bsg add error - we just can't do sgio */
2579
James Smarta53eb5e2007-04-27 12:41:09 -04002580 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002582 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 return rport;
2587
2588delete_rport:
2589 transport_destroy_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 spin_lock_irqsave(shost->host_lock, flags);
2591 list_del(&rport->peers);
James Smart3bdad7b2006-06-26 14:19:59 -04002592 put_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 spin_unlock_irqrestore(shost->host_lock, flags);
2594 put_device(dev->parent);
2595 kfree(rport);
2596 return NULL;
2597}
2598
2599/**
Rob Landleyeb448202007-11-03 13:30:39 -05002600 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 * @shost: scsi host the remote port is connected to.
2602 * @channel: Channel on shost port connected to.
2603 * @ids: The world wide names, fc address, and FC4 port
2604 * roles for the remote port.
2605 *
2606 * The LLDD calls this routine to notify the transport of the existence
2607 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2608 * of the port, it's FC address (port_id), and the FC4 roles that are
2609 * active for the port.
2610 *
2611 * For ports that are FCP targets (aka scsi targets), the FC transport
2612 * maintains consistent target id bindings on behalf of the LLDD.
2613 * A consistent target id binding is an assignment of a target id to
2614 * a remote port identifier, which persists while the scsi host is
2615 * attached. The remote port can disappear, then later reappear, and
2616 * it's target id assignment remains the same. This allows for shifts
2617 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2618 * changes to the scsi subsystem which is based on scsi host number and
2619 * target id values. Bindings are only valid during the attachment of
2620 * the scsi host. If the host detaches, then later re-attaches, target
2621 * id bindings may change.
2622 *
2623 * This routine is responsible for returning a remote port structure.
2624 * The routine will search the list of remote ports it maintains
2625 * internally on behalf of consistent target id mappings. If found, the
2626 * remote port structure will be reused. Otherwise, a new remote port
2627 * structure will be allocated.
2628 *
2629 * Whenever a remote port is allocated, a new fc_remote_port class
2630 * device is created.
2631 *
2632 * Should not be called from interrupt context.
2633 *
2634 * Notes:
2635 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002636 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637struct fc_rport *
2638fc_remote_port_add(struct Scsi_Host *shost, int channel,
2639 struct fc_rport_identifiers *ids)
2640{
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002641 struct fc_internal *fci = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04002642 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 struct fc_rport *rport;
2644 unsigned long flags;
2645 int match = 0;
2646
James Smartaedf3492006-04-10 10:14:05 -04002647 /* ensure any stgt delete functions are done */
2648 fc_flush_work(shost);
2649
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002650 /*
2651 * Search the list of "active" rports, for an rport that has been
2652 * deleted, but we've held off the real delete while the target
2653 * is in a "blocked" state.
2654 */
2655 spin_lock_irqsave(shost->host_lock, flags);
2656
James Smartaedf3492006-04-10 10:14:05 -04002657 list_for_each_entry(rport, &fc_host->rports, peers) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002658
2659 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2660 (rport->channel == channel)) {
2661
James Smartaedf3492006-04-10 10:14:05 -04002662 switch (fc_host->tgtid_bind_type) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002663 case FC_TGTID_BIND_BY_WWPN:
2664 case FC_TGTID_BIND_NONE:
2665 if (rport->port_name == ids->port_name)
2666 match = 1;
2667 break;
2668 case FC_TGTID_BIND_BY_WWNN:
2669 if (rport->node_name == ids->node_name)
2670 match = 1;
2671 break;
2672 case FC_TGTID_BIND_BY_ID:
2673 if (rport->port_id == ids->port_id)
2674 match = 1;
2675 break;
2676 }
2677
2678 if (match) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002679
2680 memcpy(&rport->node_name, &ids->node_name,
2681 sizeof(rport->node_name));
2682 memcpy(&rport->port_name, &ids->port_name,
2683 sizeof(rport->port_name));
2684 rport->port_id = ids->port_id;
2685
2686 rport->port_state = FC_PORTSTATE_ONLINE;
2687 rport->roles = ids->roles;
2688
2689 spin_unlock_irqrestore(shost->host_lock, flags);
2690
2691 if (fci->f->dd_fcrport_size)
2692 memset(rport->dd_data, 0,
2693 fci->f->dd_fcrport_size);
2694
2695 /*
James Smart92740b22007-04-27 11:53:17 -04002696 * If we were not a target, cancel the
2697 * io terminate and rport timers, and
2698 * we're done.
2699 *
2700 * If we were a target, but our new role
2701 * doesn't indicate a target, leave the
2702 * timers running expecting the role to
2703 * change as the target fully logs in. If
2704 * it doesn't, the target will be torn down.
2705 *
2706 * If we were a target, and our role shows
2707 * we're still a target, cancel the timers
2708 * and kick off a scan.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002709 */
James Smart92740b22007-04-27 11:53:17 -04002710
2711 /* was a target, not in roles */
2712 if ((rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04002713 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002714 return rport;
2715
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002716 /*
James Smart92740b22007-04-27 11:53:17 -04002717 * Stop the fail io and dev_loss timers.
2718 * If they flush, the port_state will
2719 * be checked and will NOOP the function.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002720 */
James Smart0f29b962006-08-18 17:33:29 -04002721 if (!cancel_delayed_work(&rport->fail_io_work))
2722 fc_flush_devloss(shost);
James Smart92740b22007-04-27 11:53:17 -04002723 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002724 fc_flush_devloss(shost);
2725
2726 spin_lock_irqsave(shost->host_lock, flags);
2727
Mike Christiefff9d402008-08-19 18:45:23 -05002728 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
James Smart4be98c02009-01-05 12:14:18 -05002729 FC_RPORT_DEVLOSS_PENDING |
2730 FC_RPORT_DEVLOSS_CALLBK_DONE);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002731
James Smart92740b22007-04-27 11:53:17 -04002732 /* if target, initiate a scan */
2733 if (rport->scsi_target_id != -1) {
2734 rport->flags |= FC_RPORT_SCAN_PENDING;
2735 scsi_queue_work(shost,
2736 &rport->scan_work);
2737 spin_unlock_irqrestore(shost->host_lock,
2738 flags);
2739 scsi_target_unblock(&rport->dev);
2740 } else
2741 spin_unlock_irqrestore(shost->host_lock,
2742 flags);
James Smarta0785ed2006-05-11 13:27:09 -04002743
James Smart9e4f5e22009-03-26 13:33:19 -04002744 fc_bsg_goose_queue(rport);
2745
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002746 return rport;
2747 }
2748 }
2749 }
2750
James Smart92740b22007-04-27 11:53:17 -04002751 /*
2752 * Search the bindings array
2753 * Note: if never a FCP target, you won't be on this list
2754 */
James Smartaedf3492006-04-10 10:14:05 -04002755 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 /* search for a matching consistent binding */
2758
James Smartaedf3492006-04-10 10:14:05 -04002759 list_for_each_entry(rport, &fc_host->rport_bindings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 peers) {
2761 if (rport->channel != channel)
2762 continue;
2763
James Smartaedf3492006-04-10 10:14:05 -04002764 switch (fc_host->tgtid_bind_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 case FC_TGTID_BIND_BY_WWPN:
2766 if (rport->port_name == ids->port_name)
2767 match = 1;
2768 break;
2769 case FC_TGTID_BIND_BY_WWNN:
2770 if (rport->node_name == ids->node_name)
2771 match = 1;
2772 break;
2773 case FC_TGTID_BIND_BY_ID:
2774 if (rport->port_id == ids->port_id)
2775 match = 1;
2776 break;
2777 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2778 break;
2779 }
2780
2781 if (match) {
James Smartaedf3492006-04-10 10:14:05 -04002782 list_move_tail(&rport->peers, &fc_host->rports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 break;
2784 }
2785 }
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (match) {
2788 memcpy(&rport->node_name, &ids->node_name,
2789 sizeof(rport->node_name));
2790 memcpy(&rport->port_name, &ids->port_name,
2791 sizeof(rport->port_name));
2792 rport->port_id = ids->port_id;
2793 rport->roles = ids->roles;
2794 rport->port_state = FC_PORTSTATE_ONLINE;
Mike Christiefff9d402008-08-19 18:45:23 -05002795 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002797 if (fci->f->dd_fcrport_size)
2798 memset(rport->dd_data, 0,
2799 fci->f->dd_fcrport_size);
2800
James Smarta53eb5e2007-04-27 12:41:09 -04002801 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002803 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 scsi_queue_work(shost, &rport->scan_work);
James Smarta0785ed2006-05-11 13:27:09 -04002805 spin_unlock_irqrestore(shost->host_lock, flags);
2806 scsi_target_unblock(&rport->dev);
2807 } else
2808 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 return rport;
2811 }
2812 }
2813
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002814 spin_unlock_irqrestore(shost->host_lock, flags);
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 /* No consistent binding found - create new remote port entry */
2817 rport = fc_rport_create(shost, channel, ids);
2818
2819 return rport;
2820}
2821EXPORT_SYMBOL(fc_remote_port_add);
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824/**
Rob Landleyeb448202007-11-03 13:30:39 -05002825 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 * @rport: The remote port that no longer exists
2827 *
2828 * The LLDD calls this routine to notify the transport that a remote
2829 * port is no longer part of the topology. Note: Although a port
2830 * may no longer be part of the topology, it may persist in the remote
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002831 * ports displayed by the fc_host. We do this under 2 conditions:
Rob Landleyeb448202007-11-03 13:30:39 -05002832 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002833 * This allows the port to temporarily disappear, then reappear without
2834 * disrupting the SCSI device tree attached to it. During the "blocked"
2835 * period the port will still exist.
Rob Landleyeb448202007-11-03 13:30:39 -05002836 * 2) If the port was a scsi target and disappears for longer than we
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002837 * expect, we'll delete the port and the tear down the SCSI device tree
2838 * attached to it. However, we want to semi-persist the target id assigned
2839 * to that port if it eventually does exist. The port structure will
2840 * remain (although with minimal information) so that the target id
2841 * bindings remails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 *
2843 * If the remote port is not an FCP Target, it will be fully torn down
2844 * and deallocated, including the fc_remote_port class device.
2845 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002846 * If the remote port is an FCP Target, the port will be placed in a
2847 * temporary blocked state. From the LLDD's perspective, the rport no
2848 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2849 * exists, but all sdevs on it are blocked from further I/O. The following
Rob Landleyeb448202007-11-03 13:30:39 -05002850 * is then expected.
2851 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002852 * If the remote port does not return (signaled by a LLDD call to
2853 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2854 * scsi target is removed - killing all outstanding i/o and removing the
2855 * scsi devices attached ot it. The port structure will be marked Not
2856 * Present and be partially cleared, leaving only enough information to
2857 * recognize the remote port relative to the scsi target id binding if
2858 * it later appears. The port will remain as long as there is a valid
2859 * binding (e.g. until the user changes the binding type or unloads the
2860 * scsi host with the binding).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002862 * If the remote port returns within the dev_loss_tmo value (and matches
2863 * according to the target id binding type), the port structure will be
2864 * reused. If it is no longer a SCSI target, the target will be torn
2865 * down. If it continues to be a SCSI target, then the target will be
2866 * unblocked (allowing i/o to be resumed), and a scan will be activated
2867 * to ensure that all luns are detected.
2868 *
2869 * Called from normal process context only - cannot be called from interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 *
2871 * Notes:
2872 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002873 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874void
2875fc_remote_port_delete(struct fc_rport *rport)
2876{
James Smartaedf3492006-04-10 10:14:05 -04002877 struct Scsi_Host *shost = rport_to_shost(rport);
Hannes Reinecke36dd2882010-03-09 10:18:48 +01002878 unsigned long timeout = rport->dev_loss_tmo;
James Smartaedf3492006-04-10 10:14:05 -04002879 unsigned long flags;
2880
2881 /*
2882 * No need to flush the fc_host work_q's, as all adds are synchronous.
2883 *
2884 * We do need to reclaim the rport scan work element, so eventually
2885 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2886 * there's still a scan pending.
2887 */
2888
2889 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
James Smart92740b22007-04-27 11:53:17 -04002891 if (rport->port_state != FC_PORTSTATE_ONLINE) {
James Smartaedf3492006-04-10 10:14:05 -04002892 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return;
2894 }
2895
James Smart92740b22007-04-27 11:53:17 -04002896 /*
2897 * In the past, we if this was not an FCP-Target, we would
2898 * unconditionally just jump to deleting the rport.
2899 * However, rports can be used as node containers by the LLDD,
2900 * and its not appropriate to just terminate the rport at the
2901 * first sign of a loss in connectivity. The LLDD may want to
2902 * send ELS traffic to re-validate the login. If the rport is
2903 * immediately deleted, it makes it inappropriate for a node
2904 * container.
2905 * So... we now unconditionally wait dev_loss_tmo before
2906 * destroying an rport.
2907 */
2908
James Smartaedf3492006-04-10 10:14:05 -04002909 rport->port_state = FC_PORTSTATE_BLOCKED;
2910
2911 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2912
2913 spin_unlock_irqrestore(shost->host_lock, flags);
2914
FUJITA Tomonori75252362007-09-01 02:02:27 +09002915 if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
2916 shost->active_mode & MODE_TARGET)
2917 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
2918
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002919 scsi_target_block(&rport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
James Smart0f29b962006-08-18 17:33:29 -04002921 /* see if we need to kill io faster than waiting for device loss */
2922 if ((rport->fast_io_fail_tmo != -1) &&
Mike Christiefff9d402008-08-19 18:45:23 -05002923 (rport->fast_io_fail_tmo < timeout))
James Smart0f29b962006-08-18 17:33:29 -04002924 fc_queue_devloss_work(shost, &rport->fail_io_work,
2925 rport->fast_io_fail_tmo * HZ);
2926
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002927 /* cap the length the devices can be blocked until they are deleted */
James Smartaedf3492006-04-10 10:14:05 -04002928 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929}
2930EXPORT_SYMBOL(fc_remote_port_delete);
2931
2932/**
Rob Landleyeb448202007-11-03 13:30:39 -05002933 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 * @rport: The remote port that changed.
Rob Landleyeb448202007-11-03 13:30:39 -05002935 * @roles: New roles for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 *
Rob Landleyeb448202007-11-03 13:30:39 -05002937 * Description: The LLDD calls this routine to notify the transport that the
2938 * roles on a remote port may have changed. The largest effect of this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 * if a port now becomes a FCP Target, it must be allocated a
2940 * scsi target id. If the port is no longer a FCP target, any
2941 * scsi target id value assigned to it will persist in case the
2942 * role changes back to include FCP Target. No changes in the scsi
2943 * midlayer will be invoked if the role changes (in the expectation
2944 * that the role will be resumed. If it doesn't normal error processing
2945 * will take place).
2946 *
2947 * Should not be called from interrupt context.
2948 *
2949 * Notes:
2950 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952void
2953fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2954{
2955 struct Scsi_Host *shost = rport_to_shost(rport);
James Smartaedf3492006-04-10 10:14:05 -04002956 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 unsigned long flags;
2958 int create = 0;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002959 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 spin_lock_irqsave(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04002962 if (roles & FC_PORT_ROLE_FCP_TARGET) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002963 if (rport->scsi_target_id == -1) {
2964 rport->scsi_target_id = fc_host->next_target_id++;
2965 create = 1;
James Smarta53eb5e2007-04-27 12:41:09 -04002966 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002967 create = 1;
FUJITA Tomonori75252362007-09-01 02:02:27 +09002968 } else if (shost->active_mode & MODE_TARGET) {
2969 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
2970 (char *)&rport->node_name);
2971 if (ret)
2972 printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
2973 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002976 rport->roles = roles;
2977
James Smartaedf3492006-04-10 10:14:05 -04002978 spin_unlock_irqrestore(shost->host_lock, flags);
2979
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002980 if (create) {
2981 /*
2982 * There may have been a delete timer running on the
2983 * port. Ensure that it is cancelled as we now know
2984 * the port is an FCP Target.
2985 * Note: we know the rport is exists and in an online
2986 * state as the LLDD would not have had an rport
2987 * reference to pass us.
2988 *
2989 * Take no action on the del_timer failure as the state
2990 * machine state change will validate the
2991 * transaction.
2992 */
James Smart0f29b962006-08-18 17:33:29 -04002993 if (!cancel_delayed_work(&rport->fail_io_work))
2994 fc_flush_devloss(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002995 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002996 fc_flush_devloss(shost);
2997
2998 spin_lock_irqsave(shost->host_lock, flags);
Mike Christiefff9d402008-08-19 18:45:23 -05002999 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
3000 FC_RPORT_DEVLOSS_PENDING);
James Smartaedf3492006-04-10 10:14:05 -04003001 spin_unlock_irqrestore(shost->host_lock, flags);
3002
3003 /* ensure any stgt delete functions are done */
3004 fc_flush_work(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04003007 spin_lock_irqsave(shost->host_lock, flags);
3008 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003010 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04003011 scsi_target_unblock(&rport->dev);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014EXPORT_SYMBOL(fc_remote_port_rolechg);
3015
3016/**
Rob Landleyeb448202007-11-03 13:30:39 -05003017 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
James Smart92740b22007-04-27 11:53:17 -04003018 * @work: rport target that failed to reappear in the allotted time.
Rob Landleyeb448202007-11-03 13:30:39 -05003019 *
3020 * Description: An attempt to delete a remote port blocks, and if it fails
3021 * to return in the allotted time this gets called.
3022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023static void
David Howellsc4028952006-11-22 14:57:56 +00003024fc_timeout_deleted_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
David Howellsc4028952006-11-22 14:57:56 +00003026 struct fc_rport *rport =
3027 container_of(work, struct fc_rport, dev_loss_work.work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003028 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart4be98c02009-01-05 12:14:18 -05003029 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04003030 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003031 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05003032 int do_callback = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003034 spin_lock_irqsave(shost->host_lock, flags);
3035
James Smartaedf3492006-04-10 10:14:05 -04003036 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3037
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003038 /*
James Smart92740b22007-04-27 11:53:17 -04003039 * If the port is ONLINE, then it came back. If it was a SCSI
3040 * target, validate it still is. If not, tear down the
3041 * scsi_target on it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003042 */
James Smartaedf3492006-04-10 10:14:05 -04003043 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
James Smart92740b22007-04-27 11:53:17 -04003044 (rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04003045 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003046 dev_printk(KERN_ERR, &rport->dev,
James Smartaedf3492006-04-10 10:14:05 -04003047 "blocked FC remote port time out: no longer"
3048 " a FCP target, removing starget\n");
James Smartaedf3492006-04-10 10:14:05 -04003049 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta0785ed2006-05-11 13:27:09 -04003050 scsi_target_unblock(&rport->dev);
3051 fc_queue_work(shost, &rport->stgt_delete_work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003052 return;
3053 }
3054
James Smart92740b22007-04-27 11:53:17 -04003055 /* NOOP state - we're flushing workq's */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003056 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3057 spin_unlock_irqrestore(shost->host_lock, flags);
3058 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003059 "blocked FC remote port time out: leaving"
3060 " rport%s alone\n",
3061 (rport->scsi_target_id != -1) ? " and starget" : "");
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003062 return;
3063 }
3064
James Smart92740b22007-04-27 11:53:17 -04003065 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3066 (rport->scsi_target_id == -1)) {
James Smartaedf3492006-04-10 10:14:05 -04003067 list_del(&rport->peers);
3068 rport->port_state = FC_PORTSTATE_DELETED;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003069 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003070 "blocked FC remote port time out: removing"
3071 " rport%s\n",
3072 (rport->scsi_target_id != -1) ? " and starget" : "");
James Smartaedf3492006-04-10 10:14:05 -04003073 fc_queue_work(shost, &rport->rport_delete_work);
3074 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003075 return;
3076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 dev_printk(KERN_ERR, &rport->dev,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003079 "blocked FC remote port time out: removing target and "
3080 "saving binding\n");
3081
James Smartaedf3492006-04-10 10:14:05 -04003082 list_move_tail(&rport->peers, &fc_host->rport_bindings);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003083
3084 /*
3085 * Note: We do not remove or clear the hostdata area. This allows
3086 * host-specific target data to persist along with the
3087 * scsi_target_id. It's up to the host to manage it's hostdata area.
3088 */
3089
3090 /*
3091 * Reinitialize port attributes that may change if the port comes back.
3092 */
3093 rport->maxframe_size = -1;
3094 rport->supported_classes = FC_COS_UNSPECIFIED;
James Smarta53eb5e2007-04-27 12:41:09 -04003095 rport->roles = FC_PORT_ROLE_UNKNOWN;
James Smartaedf3492006-04-10 10:14:05 -04003096 rport->port_state = FC_PORTSTATE_NOTPRESENT;
Mike Christiefff9d402008-08-19 18:45:23 -05003097 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003098
James Smartf78badb2008-12-05 16:29:59 -06003099 /*
3100 * Pre-emptively kill I/O rather than waiting for the work queue
3101 * item to teardown the starget. (FCOE libFC folks prefer this
3102 * and to have the rport_port_id still set when it's done).
3103 */
3104 spin_unlock_irqrestore(shost->host_lock, flags);
3105 fc_terminate_rport_io(rport);
3106
Michael Reed8798a692009-10-09 14:15:59 -05003107 spin_lock_irqsave(shost->host_lock, flags);
James Smartf78badb2008-12-05 16:29:59 -06003108
Michael Reed8798a692009-10-09 14:15:59 -05003109 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3110
3111 /* remove the identifiers that aren't used in the consisting binding */
3112 switch (fc_host->tgtid_bind_type) {
3113 case FC_TGTID_BIND_BY_WWPN:
3114 rport->node_name = -1;
3115 rport->port_id = -1;
3116 break;
3117 case FC_TGTID_BIND_BY_WWNN:
3118 rport->port_name = -1;
3119 rport->port_id = -1;
3120 break;
3121 case FC_TGTID_BIND_BY_ID:
3122 rport->node_name = -1;
3123 rport->port_name = -1;
3124 break;
3125 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3126 break;
3127 }
3128
3129 /*
3130 * As this only occurs if the remote port (scsi target)
3131 * went away and didn't come back - we'll remove
3132 * all attached scsi devices.
3133 */
3134 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3135 fc_queue_work(shost, &rport->stgt_delete_work);
3136
3137 do_callback = 1;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003138 }
3139
Michael Reed8798a692009-10-09 14:15:59 -05003140 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05003141
3142 /*
3143 * Notify the driver that the rport is now dead. The LLDD will
3144 * also guarantee that any communication to the rport is terminated
3145 *
3146 * Note: we set the CALLBK_DONE flag above to correspond
3147 */
Michael Reed8798a692009-10-09 14:15:59 -05003148 if (do_callback && i->f->dev_loss_tmo_callbk)
James Smart4be98c02009-01-05 12:14:18 -05003149 i->f->dev_loss_tmo_callbk(rport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
James Smart4be98c02009-01-05 12:14:18 -05003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153/**
Rob Landleyeb448202007-11-03 13:30:39 -05003154 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
David Howellsc4028952006-11-22 14:57:56 +00003155 * @work: rport to terminate io on.
James Smart0f29b962006-08-18 17:33:29 -04003156 *
3157 * Notes: Only requests the failure of the io, not that all are flushed
3158 * prior to returning.
Rob Landleyeb448202007-11-03 13:30:39 -05003159 */
James Smart0f29b962006-08-18 17:33:29 -04003160static void
David Howellsc4028952006-11-22 14:57:56 +00003161fc_timeout_fail_rport_io(struct work_struct *work)
James Smart0f29b962006-08-18 17:33:29 -04003162{
David Howellsc4028952006-11-22 14:57:56 +00003163 struct fc_rport *rport =
3164 container_of(work, struct fc_rport, fail_io_work.work);
James Smart0f29b962006-08-18 17:33:29 -04003165
3166 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3167 return;
3168
Mike Christiefff9d402008-08-19 18:45:23 -05003169 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3170 fc_terminate_rport_io(rport);
James Smart0f29b962006-08-18 17:33:29 -04003171}
3172
3173/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
David Howellsc4028952006-11-22 14:57:56 +00003175 * @work: remote port to be scanned.
Rob Landleyeb448202007-11-03 13:30:39 -05003176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177static void
David Howellsc4028952006-11-22 14:57:56 +00003178fc_scsi_scan_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
David Howellsc4028952006-11-22 14:57:56 +00003180 struct fc_rport *rport =
3181 container_of(work, struct fc_rport, scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003182 struct Scsi_Host *shost = rport_to_shost(rport);
Christof Schmitt03f002f2007-08-28 09:31:21 +02003183 struct fc_internal *i = to_fc_internal(shost->transportt);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003184 unsigned long flags;
3185
James Smartaedf3492006-04-10 10:14:05 -04003186 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
Christof Schmitt03f002f2007-08-28 09:31:21 +02003187 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3188 !(i->f->disable_target_scan)) {
James Smartaedf3492006-04-10 10:14:05 -04003189 scsi_scan_target(&rport->dev, rport->channel,
3190 rport->scsi_target_id, SCAN_WILD_CARD, 1);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003191 }
James Smartaedf3492006-04-10 10:14:05 -04003192
3193 spin_lock_irqsave(shost->host_lock, flags);
3194 rport->flags &= ~FC_RPORT_SCAN_PENDING;
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003195 spin_unlock_irqrestore(shost->host_lock, flags);
3196}
3197
Christof Schmitt65d430f2009-10-30 17:59:29 +01003198/**
3199 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3200 * @cmnd: SCSI command that scsi_eh is trying to recover
3201 *
3202 * This routine can be called from a FC LLD scsi_eh callback. It
3203 * blocks the scsi_eh thread until the fc_rport leaves the
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003204 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3205 * necessary to avoid the scsi_eh failing recovery actions for blocked
3206 * rports which would lead to offlined SCSI devices.
3207 *
3208 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3209 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3210 * passed back to scsi_eh.
Christof Schmitt65d430f2009-10-30 17:59:29 +01003211 */
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003212int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
Christof Schmitt65d430f2009-10-30 17:59:29 +01003213{
3214 struct Scsi_Host *shost = cmnd->device->host;
3215 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3216 unsigned long flags;
3217
3218 spin_lock_irqsave(shost->host_lock, flags);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003219 while (rport->port_state == FC_PORTSTATE_BLOCKED &&
3220 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
Christof Schmitt65d430f2009-10-30 17:59:29 +01003221 spin_unlock_irqrestore(shost->host_lock, flags);
3222 msleep(1000);
3223 spin_lock_irqsave(shost->host_lock, flags);
3224 }
3225 spin_unlock_irqrestore(shost->host_lock, flags);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003226
3227 if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
3228 return FAST_IO_FAIL;
3229
3230 return 0;
Christof Schmitt65d430f2009-10-30 17:59:29 +01003231}
3232EXPORT_SYMBOL(fc_block_scsi_eh);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003233
James Smarta53eb5e2007-04-27 12:41:09 -04003234/**
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003235 * fc_vport_setup - allocates and creates a FC virtual port.
James Smarta53eb5e2007-04-27 12:41:09 -04003236 * @shost: scsi host the virtual port is connected to.
3237 * @channel: Channel on shost port connected to.
3238 * @pdev: parent device for vport
3239 * @ids: The world wide names, FC4 port roles, etc for
3240 * the virtual port.
3241 * @ret_vport: The pointer to the created vport.
3242 *
3243 * Allocates and creates the vport structure, calls the parent host
3244 * to instantiate the vport, the completes w/ class and sysfs creation.
3245 *
3246 * Notes:
3247 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003248 */
James Smarta53eb5e2007-04-27 12:41:09 -04003249static int
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003250fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
James Smarta53eb5e2007-04-27 12:41:09 -04003251 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3252{
3253 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3254 struct fc_internal *fci = to_fc_internal(shost->transportt);
3255 struct fc_vport *vport;
3256 struct device *dev;
3257 unsigned long flags;
3258 size_t size;
3259 int error;
3260
3261 *ret_vport = NULL;
3262
3263 if ( ! fci->f->vport_create)
3264 return -ENOENT;
3265
3266 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3267 vport = kzalloc(size, GFP_KERNEL);
3268 if (unlikely(!vport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003269 printk(KERN_ERR "%s: allocation failure\n", __func__);
James Smarta53eb5e2007-04-27 12:41:09 -04003270 return -ENOMEM;
3271 }
3272
3273 vport->vport_state = FC_VPORT_UNKNOWN;
3274 vport->vport_last_state = FC_VPORT_UNKNOWN;
3275 vport->node_name = ids->node_name;
3276 vport->port_name = ids->port_name;
3277 vport->roles = ids->roles;
3278 vport->vport_type = ids->vport_type;
3279 if (fci->f->dd_fcvport_size)
3280 vport->dd_data = &vport[1];
3281 vport->shost = shost;
3282 vport->channel = channel;
3283 vport->flags = FC_VPORT_CREATING;
James Smart9ef3e4a2007-05-24 19:04:44 -04003284 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
James Smarta53eb5e2007-04-27 12:41:09 -04003285
3286 spin_lock_irqsave(shost->host_lock, flags);
3287
3288 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3289 spin_unlock_irqrestore(shost->host_lock, flags);
3290 kfree(vport);
3291 return -ENOSPC;
3292 }
3293 fc_host->npiv_vports_inuse++;
3294 vport->number = fc_host->next_vport_number++;
3295 list_add_tail(&vport->peers, &fc_host->vports);
3296 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3297
3298 spin_unlock_irqrestore(shost->host_lock, flags);
3299
3300 dev = &vport->dev;
3301 device_initialize(dev); /* takes self reference */
3302 dev->parent = get_device(pdev); /* takes parent reference */
3303 dev->release = fc_vport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01003304 dev_set_name(dev, "vport-%d:%d-%d",
3305 shost->host_no, channel, vport->number);
James Smarta53eb5e2007-04-27 12:41:09 -04003306 transport_setup_device(dev);
3307
3308 error = device_add(dev);
3309 if (error) {
3310 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3311 goto delete_vport;
3312 }
3313 transport_add_device(dev);
3314 transport_configure_device(dev);
3315
3316 error = fci->f->vport_create(vport, ids->disable);
3317 if (error) {
3318 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3319 goto delete_vport_all;
3320 }
3321
3322 /*
3323 * if the parent isn't the physical adapter's Scsi_Host, ensure
3324 * the Scsi_Host at least contains ia symlink to the vport.
3325 */
3326 if (pdev != &shost->shost_gendev) {
3327 error = sysfs_create_link(&shost->shost_gendev.kobj,
Kay Sievers71610f52008-12-03 22:41:36 +01003328 &dev->kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003329 if (error)
3330 printk(KERN_ERR
3331 "%s: Cannot create vport symlinks for "
3332 "%s, err=%d\n",
Kay Sievers71610f52008-12-03 22:41:36 +01003333 __func__, dev_name(dev), error);
James Smarta53eb5e2007-04-27 12:41:09 -04003334 }
3335 spin_lock_irqsave(shost->host_lock, flags);
3336 vport->flags &= ~FC_VPORT_CREATING;
3337 spin_unlock_irqrestore(shost->host_lock, flags);
3338
3339 dev_printk(KERN_NOTICE, pdev,
Kay Sievers71610f52008-12-03 22:41:36 +01003340 "%s created via shost%d channel %d\n", dev_name(dev),
James Smarta53eb5e2007-04-27 12:41:09 -04003341 shost->host_no, channel);
3342
3343 *ret_vport = vport;
3344
3345 return 0;
3346
3347delete_vport_all:
3348 transport_remove_device(dev);
3349 device_del(dev);
3350delete_vport:
3351 transport_destroy_device(dev);
3352 spin_lock_irqsave(shost->host_lock, flags);
3353 list_del(&vport->peers);
3354 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3355 fc_host->npiv_vports_inuse--;
3356 spin_unlock_irqrestore(shost->host_lock, flags);
3357 put_device(dev->parent);
3358 kfree(vport);
3359
3360 return error;
3361}
3362
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003363/**
3364 * fc_vport_create - Admin App or LLDD requests creation of a vport
3365 * @shost: scsi host the virtual port is connected to.
3366 * @channel: channel on shost port connected to.
3367 * @ids: The world wide names, FC4 port roles, etc for
3368 * the virtual port.
3369 *
3370 * Notes:
3371 * This routine assumes no locks are held on entry.
3372 */
3373struct fc_vport *
3374fc_vport_create(struct Scsi_Host *shost, int channel,
3375 struct fc_vport_identifiers *ids)
3376{
3377 int stat;
3378 struct fc_vport *vport;
3379
3380 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3381 ids, &vport);
3382 return stat ? NULL : vport;
3383}
3384EXPORT_SYMBOL(fc_vport_create);
James Smarta53eb5e2007-04-27 12:41:09 -04003385
3386/**
3387 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3388 * @vport: fc_vport to be terminated
3389 *
3390 * Calls the LLDD vport_delete() function, then deallocates and removes
3391 * the vport from the shost and object tree.
3392 *
3393 * Notes:
3394 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003395 */
James Smarta53eb5e2007-04-27 12:41:09 -04003396int
3397fc_vport_terminate(struct fc_vport *vport)
3398{
3399 struct Scsi_Host *shost = vport_to_shost(vport);
3400 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3401 struct fc_internal *i = to_fc_internal(shost->transportt);
3402 struct device *dev = &vport->dev;
3403 unsigned long flags;
3404 int stat;
3405
James Smarta53eb5e2007-04-27 12:41:09 -04003406 if (i->f->vport_delete)
3407 stat = i->f->vport_delete(vport);
3408 else
3409 stat = -ENOENT;
3410
3411 spin_lock_irqsave(shost->host_lock, flags);
3412 vport->flags &= ~FC_VPORT_DELETING;
3413 if (!stat) {
3414 vport->flags |= FC_VPORT_DELETED;
3415 list_del(&vport->peers);
3416 fc_host->npiv_vports_inuse--;
3417 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3418 }
3419 spin_unlock_irqrestore(shost->host_lock, flags);
3420
3421 if (stat)
3422 return stat;
3423
3424 if (dev->parent != &shost->shost_gendev)
Kay Sievers71610f52008-12-03 22:41:36 +01003425 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003426 transport_remove_device(dev);
3427 device_del(dev);
3428 transport_destroy_device(dev);
3429
3430 /*
3431 * Removing our self-reference should mean our
3432 * release function gets called, which will drop the remaining
3433 * parent reference and free the data structure.
3434 */
3435 put_device(dev); /* for self-reference */
3436
3437 return 0; /* SUCCESS */
3438}
3439EXPORT_SYMBOL(fc_vport_terminate);
3440
James Smart9ef3e4a2007-05-24 19:04:44 -04003441/**
3442 * fc_vport_sched_delete - workq-based delete request for a vport
James Smart9ef3e4a2007-05-24 19:04:44 -04003443 * @work: vport to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05003444 */
James Smart9ef3e4a2007-05-24 19:04:44 -04003445static void
3446fc_vport_sched_delete(struct work_struct *work)
3447{
3448 struct fc_vport *vport =
3449 container_of(work, struct fc_vport, vport_delete_work);
3450 int stat;
James Smarta53eb5e2007-04-27 12:41:09 -04003451
James Smart9ef3e4a2007-05-24 19:04:44 -04003452 stat = fc_vport_terminate(vport);
3453 if (stat)
3454 dev_printk(KERN_ERR, vport->dev.parent,
3455 "%s: %s could not be deleted created via "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003456 "shost%d channel %d - error %d\n", __func__,
Kay Sievers71610f52008-12-03 22:41:36 +01003457 dev_name(&vport->dev), vport->shost->host_no,
James Smart9ef3e4a2007-05-24 19:04:44 -04003458 vport->channel, stat);
3459}
3460
3461
James Smart9e4f5e22009-03-26 13:33:19 -04003462/*
3463 * BSG support
3464 */
3465
3466
3467/**
3468 * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
3469 * @job: fc_bsg_job that is to be torn down
3470 */
3471static void
3472fc_destroy_bsgjob(struct fc_bsg_job *job)
3473{
3474 unsigned long flags;
3475
3476 spin_lock_irqsave(&job->job_lock, flags);
3477 if (job->ref_cnt) {
3478 spin_unlock_irqrestore(&job->job_lock, flags);
3479 return;
3480 }
3481 spin_unlock_irqrestore(&job->job_lock, flags);
3482
3483 put_device(job->dev); /* release reference for the request */
3484
3485 kfree(job->request_payload.sg_list);
3486 kfree(job->reply_payload.sg_list);
3487 kfree(job);
3488}
3489
James Smart9e4f5e22009-03-26 13:33:19 -04003490/**
3491 * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
3492 * completed
3493 * @job: fc_bsg_job that is complete
3494 */
3495static void
3496fc_bsg_jobdone(struct fc_bsg_job *job)
3497{
3498 struct request *req = job->req;
3499 struct request *rsp = req->next_rq;
James Smart9e4f5e22009-03-26 13:33:19 -04003500 int err;
3501
James Smart9e4f5e22009-03-26 13:33:19 -04003502 err = job->req->errors = job->reply->result;
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003503
James Smart9e4f5e22009-03-26 13:33:19 -04003504 if (err < 0)
3505 /* we're only returning the result field in the reply */
3506 job->req->sense_len = sizeof(uint32_t);
3507 else
3508 job->req->sense_len = job->reply_len;
3509
3510 /* we assume all request payload was transferred, residual == 0 */
3511 req->resid_len = 0;
3512
3513 if (rsp) {
3514 WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
3515
3516 /* set reply (bidi) residual */
3517 rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
3518 rsp->resid_len);
3519 }
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003520 blk_complete_request(req);
James Smart9e4f5e22009-03-26 13:33:19 -04003521}
3522
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003523/**
3524 * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
Randy Dunlapfe5d20c2009-07-04 13:10:41 -07003525 * @rq: BSG request that holds the job to be destroyed
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003526 */
3527static void fc_bsg_softirq_done(struct request *rq)
3528{
3529 struct fc_bsg_job *job = rq->special;
3530 unsigned long flags;
3531
3532 spin_lock_irqsave(&job->job_lock, flags);
3533 job->state_flags |= FC_RQST_STATE_DONE;
3534 job->ref_cnt--;
3535 spin_unlock_irqrestore(&job->job_lock, flags);
3536
3537 blk_end_request_all(rq, rq->errors);
3538 fc_destroy_bsgjob(job);
3539}
James Smart9e4f5e22009-03-26 13:33:19 -04003540
3541/**
3542 * fc_bsg_job_timeout - handler for when a bsg request timesout
3543 * @req: request that timed out
3544 */
3545static enum blk_eh_timer_return
3546fc_bsg_job_timeout(struct request *req)
3547{
3548 struct fc_bsg_job *job = (void *) req->special;
3549 struct Scsi_Host *shost = job->shost;
3550 struct fc_internal *i = to_fc_internal(shost->transportt);
3551 unsigned long flags;
3552 int err = 0, done = 0;
3553
3554 if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
3555 return BLK_EH_RESET_TIMER;
3556
3557 spin_lock_irqsave(&job->job_lock, flags);
3558 if (job->state_flags & FC_RQST_STATE_DONE)
3559 done = 1;
3560 else
3561 job->ref_cnt++;
3562 spin_unlock_irqrestore(&job->job_lock, flags);
3563
3564 if (!done && i->f->bsg_timeout) {
3565 /* call LLDD to abort the i/o as it has timed out */
3566 err = i->f->bsg_timeout(job);
Swen Schilligb8f08642010-01-14 17:19:00 +01003567 if (err == -EAGAIN) {
3568 job->ref_cnt--;
3569 return BLK_EH_RESET_TIMER;
3570 } else if (err)
James Smart9e4f5e22009-03-26 13:33:19 -04003571 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3572 "abort failed with status %d\n", err);
3573 }
3574
James Smart9e4f5e22009-03-26 13:33:19 -04003575 /* the blk_end_sync_io() doesn't check the error */
Giridhar Malavali47e7e892009-06-19 16:26:54 -07003576 if (done)
3577 return BLK_EH_NOT_HANDLED;
3578 else
3579 return BLK_EH_HANDLED;
James Smart9e4f5e22009-03-26 13:33:19 -04003580}
3581
James Smart9e4f5e22009-03-26 13:33:19 -04003582static int
3583fc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
3584{
3585 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
3586
3587 BUG_ON(!req->nr_phys_segments);
3588
3589 buf->sg_list = kzalloc(sz, GFP_KERNEL);
3590 if (!buf->sg_list)
3591 return -ENOMEM;
3592 sg_init_table(buf->sg_list, req->nr_phys_segments);
3593 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
3594 buf->payload_len = blk_rq_bytes(req);
3595 return 0;
3596}
3597
3598
3599/**
3600 * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
3601 * bsg request
3602 * @shost: SCSI Host corresponding to the bsg object
3603 * @rport: (optional) FC Remote Port corresponding to the bsg object
3604 * @req: BSG request that needs a job structure
3605 */
3606static int
3607fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
3608 struct request *req)
3609{
3610 struct fc_internal *i = to_fc_internal(shost->transportt);
3611 struct request *rsp = req->next_rq;
3612 struct fc_bsg_job *job;
3613 int ret;
3614
3615 BUG_ON(req->special);
3616
3617 job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
3618 GFP_KERNEL);
3619 if (!job)
3620 return -ENOMEM;
3621
3622 /*
3623 * Note: this is a bit silly.
3624 * The request gets formatted as a SGIO v4 ioctl request, which
3625 * then gets reformatted as a blk request, which then gets
3626 * reformatted as a fc bsg request. And on completion, we have
3627 * to wrap return results such that SGIO v4 thinks it was a scsi
3628 * status. I hope this was all worth it.
3629 */
3630
3631 req->special = job;
3632 job->shost = shost;
3633 job->rport = rport;
3634 job->req = req;
3635 if (i->f->dd_bsg_size)
3636 job->dd_data = (void *)&job[1];
3637 spin_lock_init(&job->job_lock);
3638 job->request = (struct fc_bsg_request *)req->cmd;
3639 job->request_len = req->cmd_len;
3640 job->reply = req->sense;
3641 job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
3642 * allocated */
3643 if (req->bio) {
3644 ret = fc_bsg_map_buffer(&job->request_payload, req);
3645 if (ret)
3646 goto failjob_rls_job;
3647 }
3648 if (rsp && rsp->bio) {
3649 ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
3650 if (ret)
3651 goto failjob_rls_rqst_payload;
3652 }
3653 job->job_done = fc_bsg_jobdone;
3654 if (rport)
3655 job->dev = &rport->dev;
3656 else
3657 job->dev = &shost->shost_gendev;
3658 get_device(job->dev); /* take a reference for the request */
3659
3660 job->ref_cnt = 1;
3661
3662 return 0;
3663
3664
3665failjob_rls_rqst_payload:
3666 kfree(job->request_payload.sg_list);
3667failjob_rls_job:
3668 kfree(job);
3669 return -ENOMEM;
3670}
3671
3672
3673enum fc_dispatch_result {
3674 FC_DISPATCH_BREAK, /* on return, q is locked, break from q loop */
3675 FC_DISPATCH_LOCKED, /* on return, q is locked, continue on */
3676 FC_DISPATCH_UNLOCKED, /* on return, q is unlocked, continue on */
3677};
3678
3679
3680/**
3681 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
Randy Dunlap54159072009-06-18 19:39:57 -07003682 * @q: fc host request queue
James Smart9e4f5e22009-03-26 13:33:19 -04003683 * @shost: scsi host rport attached to
3684 * @job: bsg job to be processed
3685 */
3686static enum fc_dispatch_result
3687fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3688 struct fc_bsg_job *job)
3689{
3690 struct fc_internal *i = to_fc_internal(shost->transportt);
3691 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3692 int ret;
3693
3694 /* Validate the host command */
3695 switch (job->request->msgcode) {
3696 case FC_BSG_HST_ADD_RPORT:
3697 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3698 break;
3699
3700 case FC_BSG_HST_DEL_RPORT:
3701 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3702 break;
3703
3704 case FC_BSG_HST_ELS_NOLOGIN:
3705 cmdlen += sizeof(struct fc_bsg_host_els);
3706 /* there better be a xmt and rcv payloads */
3707 if ((!job->request_payload.payload_len) ||
3708 (!job->reply_payload.payload_len)) {
3709 ret = -EINVAL;
3710 goto fail_host_msg;
3711 }
3712 break;
3713
3714 case FC_BSG_HST_CT:
3715 cmdlen += sizeof(struct fc_bsg_host_ct);
3716 /* there better be xmt and rcv payloads */
3717 if ((!job->request_payload.payload_len) ||
3718 (!job->reply_payload.payload_len)) {
3719 ret = -EINVAL;
3720 goto fail_host_msg;
3721 }
3722 break;
3723
3724 case FC_BSG_HST_VENDOR:
3725 cmdlen += sizeof(struct fc_bsg_host_vendor);
3726 if ((shost->hostt->vendor_id == 0L) ||
3727 (job->request->rqst_data.h_vendor.vendor_id !=
3728 shost->hostt->vendor_id)) {
3729 ret = -ESRCH;
3730 goto fail_host_msg;
3731 }
3732 break;
3733
3734 default:
3735 ret = -EBADR;
3736 goto fail_host_msg;
3737 }
3738
3739 /* check if we really have all the request data needed */
3740 if (job->request_len < cmdlen) {
3741 ret = -ENOMSG;
3742 goto fail_host_msg;
3743 }
3744
3745 ret = i->f->bsg_request(job);
3746 if (!ret)
3747 return FC_DISPATCH_UNLOCKED;
3748
3749fail_host_msg:
3750 /* return the errno failure code as the only status */
3751 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003752 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003753 job->reply->result = ret;
3754 job->reply_len = sizeof(uint32_t);
3755 fc_bsg_jobdone(job);
3756 return FC_DISPATCH_UNLOCKED;
3757}
3758
3759
3760/*
3761 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3762 * @rport: rport to be restarted
3763 */
3764static void
3765fc_bsg_goose_queue(struct fc_rport *rport)
3766{
3767 int flagset;
Christof Schmitt39562e72009-06-26 16:30:43 +02003768 unsigned long flags;
James Smart9e4f5e22009-03-26 13:33:19 -04003769
3770 if (!rport->rqst_q)
3771 return;
3772
3773 get_device(&rport->dev);
3774
Christof Schmitt39562e72009-06-26 16:30:43 +02003775 spin_lock_irqsave(rport->rqst_q->queue_lock, flags);
James Smart9e4f5e22009-03-26 13:33:19 -04003776 flagset = test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags) &&
3777 !test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags);
3778 if (flagset)
3779 queue_flag_set(QUEUE_FLAG_REENTER, rport->rqst_q);
3780 __blk_run_queue(rport->rqst_q);
3781 if (flagset)
3782 queue_flag_clear(QUEUE_FLAG_REENTER, rport->rqst_q);
Christof Schmitt39562e72009-06-26 16:30:43 +02003783 spin_unlock_irqrestore(rport->rqst_q->queue_lock, flags);
James Smart9e4f5e22009-03-26 13:33:19 -04003784
3785 put_device(&rport->dev);
3786}
3787
3788
3789/**
3790 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
Randy Dunlap54159072009-06-18 19:39:57 -07003791 * @q: rport request queue
James Smart9e4f5e22009-03-26 13:33:19 -04003792 * @shost: scsi host rport attached to
3793 * @rport: rport request destined to
3794 * @job: bsg job to be processed
3795 */
3796static enum fc_dispatch_result
3797fc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3798 struct fc_rport *rport, struct fc_bsg_job *job)
3799{
3800 struct fc_internal *i = to_fc_internal(shost->transportt);
3801 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3802 int ret;
3803
3804 /* Validate the rport command */
3805 switch (job->request->msgcode) {
3806 case FC_BSG_RPT_ELS:
3807 cmdlen += sizeof(struct fc_bsg_rport_els);
3808 goto check_bidi;
3809
3810 case FC_BSG_RPT_CT:
3811 cmdlen += sizeof(struct fc_bsg_rport_ct);
3812check_bidi:
3813 /* there better be xmt and rcv payloads */
3814 if ((!job->request_payload.payload_len) ||
3815 (!job->reply_payload.payload_len)) {
3816 ret = -EINVAL;
3817 goto fail_rport_msg;
3818 }
3819 break;
3820 default:
3821 ret = -EBADR;
3822 goto fail_rport_msg;
3823 }
3824
3825 /* check if we really have all the request data needed */
3826 if (job->request_len < cmdlen) {
3827 ret = -ENOMSG;
3828 goto fail_rport_msg;
3829 }
3830
3831 ret = i->f->bsg_request(job);
3832 if (!ret)
3833 return FC_DISPATCH_UNLOCKED;
3834
3835fail_rport_msg:
3836 /* return the errno failure code as the only status */
3837 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003838 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003839 job->reply->result = ret;
3840 job->reply_len = sizeof(uint32_t);
3841 fc_bsg_jobdone(job);
3842 return FC_DISPATCH_UNLOCKED;
3843}
3844
3845
3846/**
3847 * fc_bsg_request_handler - generic handler for bsg requests
3848 * @q: request queue to manage
3849 * @shost: Scsi_Host related to the bsg object
3850 * @rport: FC remote port related to the bsg object (optional)
3851 * @dev: device structure for bsg object
3852 */
3853static void
3854fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
3855 struct fc_rport *rport, struct device *dev)
3856{
3857 struct request *req;
3858 struct fc_bsg_job *job;
3859 enum fc_dispatch_result ret;
3860
3861 if (!get_device(dev))
3862 return;
3863
3864 while (!blk_queue_plugged(q)) {
Mike Christie2bc1c592009-11-05 11:18:09 -06003865 if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED) &&
3866 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
3867 break;
James Smart9e4f5e22009-03-26 13:33:19 -04003868
3869 req = blk_fetch_request(q);
3870 if (!req)
3871 break;
3872
3873 if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
3874 req->errors = -ENXIO;
3875 spin_unlock_irq(q->queue_lock);
Sarang Radked88a7142010-03-10 04:03:04 -06003876 blk_end_request_all(req, -ENXIO);
James Smart9e4f5e22009-03-26 13:33:19 -04003877 spin_lock_irq(q->queue_lock);
3878 continue;
3879 }
3880
3881 spin_unlock_irq(q->queue_lock);
3882
3883 ret = fc_req_to_bsgjob(shost, rport, req);
3884 if (ret) {
3885 req->errors = ret;
Sarang Radked88a7142010-03-10 04:03:04 -06003886 blk_end_request_all(req, ret);
James Smart9e4f5e22009-03-26 13:33:19 -04003887 spin_lock_irq(q->queue_lock);
3888 continue;
3889 }
3890
3891 job = req->special;
3892
3893 /* check if we have the msgcode value at least */
3894 if (job->request_len < sizeof(uint32_t)) {
3895 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003896 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003897 job->reply->result = -ENOMSG;
3898 job->reply_len = sizeof(uint32_t);
3899 fc_bsg_jobdone(job);
3900 spin_lock_irq(q->queue_lock);
3901 continue;
3902 }
3903
3904 /* the dispatch routines will unlock the queue_lock */
3905 if (rport)
3906 ret = fc_bsg_rport_dispatch(q, shost, rport, job);
3907 else
3908 ret = fc_bsg_host_dispatch(q, shost, job);
3909
3910 /* did dispatcher hit state that can't process any more */
3911 if (ret == FC_DISPATCH_BREAK)
3912 break;
3913
3914 /* did dispatcher had released the lock */
3915 if (ret == FC_DISPATCH_UNLOCKED)
3916 spin_lock_irq(q->queue_lock);
3917 }
3918
3919 spin_unlock_irq(q->queue_lock);
3920 put_device(dev);
3921 spin_lock_irq(q->queue_lock);
3922}
3923
3924
3925/**
3926 * fc_bsg_host_handler - handler for bsg requests for a fc host
3927 * @q: fc host request queue
3928 */
3929static void
3930fc_bsg_host_handler(struct request_queue *q)
3931{
3932 struct Scsi_Host *shost = q->queuedata;
3933
3934 fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
3935}
3936
3937
3938/**
3939 * fc_bsg_rport_handler - handler for bsg requests for a fc rport
3940 * @q: rport request queue
3941 */
3942static void
3943fc_bsg_rport_handler(struct request_queue *q)
3944{
3945 struct fc_rport *rport = q->queuedata;
3946 struct Scsi_Host *shost = rport_to_shost(rport);
3947
3948 fc_bsg_request_handler(q, shost, rport, &rport->dev);
3949}
3950
3951
3952/**
3953 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3954 * @shost: shost for fc_host
3955 * @fc_host: fc_host adding the structures to
3956 */
3957static int
3958fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
3959{
3960 struct device *dev = &shost->shost_gendev;
3961 struct fc_internal *i = to_fc_internal(shost->transportt);
3962 struct request_queue *q;
3963 int err;
James Bottomley3c559ea2009-06-21 12:11:43 -05003964 char bsg_name[20];
James Smart9e4f5e22009-03-26 13:33:19 -04003965
3966 fc_host->rqst_q = NULL;
3967
3968 if (!i->f->bsg_request)
3969 return -ENOTSUPP;
3970
3971 snprintf(bsg_name, sizeof(bsg_name),
3972 "fc_host%d", shost->host_no);
3973
3974 q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
3975 if (!q) {
3976 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3977 "initialize - no request queue\n",
3978 shost->host_no);
3979 return -ENOMEM;
3980 }
3981
3982 q->queuedata = shost;
3983 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003984 blk_queue_softirq_done(q, fc_bsg_softirq_done);
James Smart9e4f5e22009-03-26 13:33:19 -04003985 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
3986 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
3987
3988 err = bsg_register_queue(q, dev, bsg_name, NULL);
3989 if (err) {
3990 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3991 "initialize - register queue\n",
3992 shost->host_no);
3993 blk_cleanup_queue(q);
3994 return err;
3995 }
3996
3997 fc_host->rqst_q = q;
3998 return 0;
3999}
4000
4001
4002/**
4003 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
4004 * @shost: shost that rport is attached to
4005 * @rport: rport that the bsg hooks are being attached to
4006 */
4007static int
4008fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
4009{
4010 struct device *dev = &rport->dev;
4011 struct fc_internal *i = to_fc_internal(shost->transportt);
4012 struct request_queue *q;
4013 int err;
4014
4015 rport->rqst_q = NULL;
4016
4017 if (!i->f->bsg_request)
4018 return -ENOTSUPP;
4019
4020 q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
4021 if (!q) {
4022 printk(KERN_ERR "%s: bsg interface failed to "
4023 "initialize - no request queue\n",
4024 dev->kobj.name);
4025 return -ENOMEM;
4026 }
4027
4028 q->queuedata = rport;
4029 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07004030 blk_queue_softirq_done(q, fc_bsg_softirq_done);
James Smart9e4f5e22009-03-26 13:33:19 -04004031 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
4032 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
4033
4034 err = bsg_register_queue(q, dev, NULL, NULL);
4035 if (err) {
4036 printk(KERN_ERR "%s: bsg interface failed to "
4037 "initialize - register queue\n",
4038 dev->kobj.name);
4039 blk_cleanup_queue(q);
4040 return err;
4041 }
4042
4043 rport->rqst_q = q;
4044 return 0;
4045}
4046
4047
4048/**
4049 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
4050 * @q: the request_queue that is to be torn down.
James Smart78d16342010-08-31 22:27:31 -04004051 *
4052 * Notes:
4053 * Before unregistering the queue empty any requests that are blocked
4054 *
4055 *
James Smart9e4f5e22009-03-26 13:33:19 -04004056 */
4057static void
4058fc_bsg_remove(struct request_queue *q)
4059{
James Smart78d16342010-08-31 22:27:31 -04004060 struct request *req; /* block request */
4061 int counts; /* totals for request_list count and starved */
4062
James Smart9e4f5e22009-03-26 13:33:19 -04004063 if (q) {
James Smart78d16342010-08-31 22:27:31 -04004064 /* Stop taking in new requests */
4065 spin_lock_irq(q->queue_lock);
4066 blk_stop_queue(q);
4067
4068 /* drain all requests in the queue */
4069 while (1) {
4070 /* need the lock to fetch a request
4071 * this may fetch the same reqeust as the previous pass
4072 */
4073 req = blk_fetch_request(q);
4074 /* save requests in use and starved */
4075 counts = q->rq.count[0] + q->rq.count[1] +
4076 q->rq.starved[0] + q->rq.starved[1];
4077 spin_unlock_irq(q->queue_lock);
4078 /* any requests still outstanding? */
4079 if (counts == 0)
4080 break;
4081
4082 /* This may be the same req as the previous iteration,
4083 * always send the blk_end_request_all after a prefetch.
4084 * It is not okay to not end the request because the
4085 * prefetch started the request.
4086 */
4087 if (req) {
4088 /* return -ENXIO to indicate that this queue is
4089 * going away
4090 */
4091 req->errors = -ENXIO;
4092 blk_end_request_all(req, -ENXIO);
4093 }
4094
4095 msleep(200); /* allow bsg to possibly finish */
4096 spin_lock_irq(q->queue_lock);
4097 }
4098
James Smart9e4f5e22009-03-26 13:33:19 -04004099 bsg_unregister_queue(q);
4100 blk_cleanup_queue(q);
4101 }
4102}
4103
4104
James Smart9ef3e4a2007-05-24 19:04:44 -04004105/* Original Author: Martin Hicks */
4106MODULE_AUTHOR("James Smart");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107MODULE_DESCRIPTION("FC Transport Attributes");
4108MODULE_LICENSE("GPL");
4109
4110module_init(fc_transport_init);
4111module_exit(fc_transport_exit);