blob: e106c276aa00275ce1564ed5cccba739e62d6804 [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 <net/netlink.h>
39#include <scsi/scsi_netlink_fc.h>
James Smart9e4f5e22009-03-26 13:33:19 -040040#include <scsi/scsi_bsg_fc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "scsi_priv.h"
FUJITA Tomonori75252362007-09-01 02:02:27 +090042#include "scsi_transport_fc_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
James Smartaedf3492006-04-10 10:14:05 -040044static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
James Smart9ef3e4a2007-05-24 19:04:44 -040045static void fc_vport_sched_delete(struct work_struct *work);
Andrew Vasqueza30c3f62008-07-18 08:32:52 -070046static int fc_vport_setup(struct Scsi_Host *shost, int channel,
James Smarta53eb5e2007-04-27 12:41:09 -040047 struct device *pdev, struct fc_vport_identifiers *ids,
48 struct fc_vport **vport);
James Smart9e4f5e22009-03-26 13:33:19 -040049static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
50static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
51static void fc_bsg_remove(struct request_queue *);
52static void fc_bsg_goose_queue(struct fc_rport *);
James Smarta53eb5e2007-04-27 12:41:09 -040053
54/*
Mike Christie43ca9102010-09-15 16:52:32 -050055 * Module Parameters
56 */
57
58/*
59 * dev_loss_tmo: the default number of seconds that the FC transport
60 * should insulate the loss of a remote port.
61 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
62 */
63static unsigned int fc_dev_loss_tmo = 60; /* seconds */
64
65module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
66MODULE_PARM_DESC(dev_loss_tmo,
67 "Maximum number of seconds that the FC transport should"
68 " insulate the loss of a remote port. Once this value is"
69 " exceeded, the scsi target is removed. Value should be"
70 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
71 " fast_io_fail_tmo is not set.");
72
73/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * Redefine so that we can have same named attributes in the
75 * sdev/starget/host objects.
76 */
Tony Jonesee959b02008-02-22 00:13:36 +010077#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
78struct device_attribute device_attr_##_prefix##_##_name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 __ATTR(_name,_mode,_show,_store)
80
81#define fc_enum_name_search(title, table_type, table) \
82static const char *get_fc_##title##_name(enum table_type table_key) \
83{ \
84 int i; \
85 char *name = NULL; \
86 \
Tobias Klauser6391a112006-06-08 22:23:48 -070087 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (table[i].value == table_key) { \
89 name = table[i].name; \
90 break; \
91 } \
92 } \
93 return name; \
94}
95
96#define fc_enum_name_match(title, table_type, table) \
97static int get_fc_##title##_match(const char *table_key, \
98 enum table_type *value) \
99{ \
100 int i; \
101 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700102 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (strncmp(table_key, table[i].name, \
104 table[i].matchlen) == 0) { \
105 *value = table[i].value; \
106 return 0; /* success */ \
107 } \
108 } \
109 return 1; /* failure */ \
110}
111
112
113/* Convert fc_port_type values to ascii string name */
114static struct {
115 enum fc_port_type value;
116 char *name;
117} fc_port_type_names[] = {
118 { FC_PORTTYPE_UNKNOWN, "Unknown" },
119 { FC_PORTTYPE_OTHER, "Other" },
120 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
121 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
122 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
123 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
Christof Schmitt951948a2009-01-15 16:51:48 +0100124 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
James Smarta53eb5e2007-04-27 12:41:09 -0400125 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
128#define FC_PORTTYPE_MAX_NAMELEN 50
129
James Smarta53eb5e2007-04-27 12:41:09 -0400130/* Reuse fc_port_type enum function for vport_type */
131#define get_fc_vport_type_name get_fc_port_type_name
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
James Smart84314fd2006-08-18 17:30:09 -0400134/* Convert fc_host_event_code values to ascii string name */
135static const struct {
136 enum fc_host_event_code value;
137 char *name;
138} fc_host_event_code_names[] = {
139 { FCH_EVT_LIP, "lip" },
140 { FCH_EVT_LINKUP, "link_up" },
141 { FCH_EVT_LINKDOWN, "link_down" },
142 { FCH_EVT_LIPRESET, "lip_reset" },
143 { FCH_EVT_RSCN, "rscn" },
144 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
145 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
146 { FCH_EVT_PORT_ONLINE, "port_online" },
147 { FCH_EVT_PORT_OFFLINE, "port_offline" },
148 { FCH_EVT_PORT_FABRIC, "port_fabric" },
149 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
150 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
151};
152fc_enum_name_search(host_event_code, fc_host_event_code,
153 fc_host_event_code_names)
154#define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
155
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Convert fc_port_state values to ascii string name */
158static struct {
159 enum fc_port_state value;
160 char *name;
161} fc_port_state_names[] = {
162 { FC_PORTSTATE_UNKNOWN, "Unknown" },
163 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
164 { FC_PORTSTATE_ONLINE, "Online" },
165 { FC_PORTSTATE_OFFLINE, "Offline" },
166 { FC_PORTSTATE_BLOCKED, "Blocked" },
167 { FC_PORTSTATE_BYPASSED, "Bypassed" },
168 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
169 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
170 { FC_PORTSTATE_ERROR, "Error" },
171 { FC_PORTSTATE_LOOPBACK, "Loopback" },
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500172 { FC_PORTSTATE_DELETED, "Deleted" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173};
174fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
175#define FC_PORTSTATE_MAX_NAMELEN 20
176
177
James Smarta53eb5e2007-04-27 12:41:09 -0400178/* Convert fc_vport_state values to ascii string name */
179static struct {
180 enum fc_vport_state value;
181 char *name;
182} fc_vport_state_names[] = {
183 { FC_VPORT_UNKNOWN, "Unknown" },
184 { FC_VPORT_ACTIVE, "Active" },
185 { FC_VPORT_DISABLED, "Disabled" },
186 { FC_VPORT_LINKDOWN, "Linkdown" },
187 { FC_VPORT_INITIALIZING, "Initializing" },
188 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
189 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
190 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
191 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
192 { FC_VPORT_FAILED, "VPort Failed" },
193};
194fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
195#define FC_VPORTSTATE_MAX_NAMELEN 24
196
197/* Reuse fc_vport_state enum function for vport_last_state */
198#define get_fc_vport_last_state_name get_fc_vport_state_name
199
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/* Convert fc_tgtid_binding_type values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100202static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 enum fc_tgtid_binding_type value;
204 char *name;
205 int matchlen;
206} fc_tgtid_binding_type_names[] = {
207 { FC_TGTID_BIND_NONE, "none", 4 },
208 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
209 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
210 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
211};
212fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
213 fc_tgtid_binding_type_names)
214fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
215 fc_tgtid_binding_type_names)
216#define FC_BINDTYPE_MAX_NAMELEN 30
217
218
219#define fc_bitfield_name_search(title, table) \
220static ssize_t \
221get_fc_##title##_names(u32 table_key, char *buf) \
222{ \
223 char *prefix = ""; \
224 ssize_t len = 0; \
225 int i; \
226 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700227 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (table[i].value & table_key) { \
229 len += sprintf(buf + len, "%s%s", \
230 prefix, table[i].name); \
231 prefix = ", "; \
232 } \
233 } \
234 len += sprintf(buf + len, "\n"); \
235 return len; \
236}
237
238
239/* Convert FC_COS bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100240static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 u32 value;
242 char *name;
243} fc_cos_names[] = {
244 { FC_COS_CLASS1, "Class 1" },
245 { FC_COS_CLASS2, "Class 2" },
246 { FC_COS_CLASS3, "Class 3" },
247 { FC_COS_CLASS4, "Class 4" },
248 { FC_COS_CLASS6, "Class 6" },
249};
250fc_bitfield_name_search(cos, fc_cos_names)
251
252
253/* Convert FC_PORTSPEED bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100254static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 u32 value;
256 char *name;
257} fc_port_speed_names[] = {
258 { FC_PORTSPEED_1GBIT, "1 Gbit" },
259 { FC_PORTSPEED_2GBIT, "2 Gbit" },
260 { FC_PORTSPEED_4GBIT, "4 Gbit" },
261 { FC_PORTSPEED_10GBIT, "10 Gbit" },
James Smartc3d23502007-03-12 14:16:35 -0500262 { FC_PORTSPEED_8GBIT, "8 Gbit" },
263 { FC_PORTSPEED_16GBIT, "16 Gbit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
265};
266fc_bitfield_name_search(port_speed, fc_port_speed_names)
267
268
269static int
270show_fc_fc4s (char *buf, u8 *fc4_list)
271{
272 int i, len=0;
273
274 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
275 len += sprintf(buf + len , "0x%02x ", *fc4_list);
276 len += sprintf(buf + len, "\n");
277 return len;
278}
279
280
James Smarta53eb5e2007-04-27 12:41:09 -0400281/* Convert FC_PORT_ROLE bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100282static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 u32 value;
284 char *name;
James Smarta53eb5e2007-04-27 12:41:09 -0400285} fc_port_role_names[] = {
286 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
287 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
288 { FC_PORT_ROLE_IP_PORT, "IP Port" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289};
James Smarta53eb5e2007-04-27 12:41:09 -0400290fc_bitfield_name_search(port_roles, fc_port_role_names)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/*
293 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
294 */
295#define FC_WELLKNOWN_PORTID_MASK 0xfffff0
296#define FC_WELLKNOWN_ROLE_MASK 0x00000f
297#define FC_FPORT_PORTID 0x00000e
298#define FC_FABCTLR_PORTID 0x00000d
299#define FC_DIRSRVR_PORTID 0x00000c
300#define FC_TIMESRVR_PORTID 0x00000b
301#define FC_MGMTSRVR_PORTID 0x00000a
302
303
David Howellsc4028952006-11-22 14:57:56 +0000304static void fc_timeout_deleted_rport(struct work_struct *work);
305static void fc_timeout_fail_rport_io(struct work_struct *work);
306static void fc_scsi_scan_rport(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308/*
309 * Attribute counts pre object type...
310 * Increase these values if you add attributes
311 */
312#define FC_STARGET_NUM_ATTRS 3
James Smart0f29b962006-08-18 17:33:29 -0400313#define FC_RPORT_NUM_ATTRS 10
James Smarta53eb5e2007-04-27 12:41:09 -0400314#define FC_VPORT_NUM_ATTRS 9
Neerav Parikhbb8ef582012-01-22 17:29:50 -0800315#define FC_HOST_NUM_ATTRS 29
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317struct fc_internal {
318 struct scsi_transport_template t;
319 struct fc_function_template *f;
320
321 /*
322 * For attributes : each object has :
323 * An array of the actual attributes structures
324 * An array of null-terminated pointers to the attribute
325 * structures - used for mid-layer interaction.
326 *
327 * The attribute containers for the starget and host are are
328 * part of the midlayer. As the remote port is specific to the
329 * fc transport, we must provide the attribute container.
330 */
Tony Jonesee959b02008-02-22 00:13:36 +0100331 struct device_attribute private_starget_attrs[
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 FC_STARGET_NUM_ATTRS];
Tony Jonesee959b02008-02-22 00:13:36 +0100333 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Tony Jonesee959b02008-02-22 00:13:36 +0100335 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
336 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 struct transport_container rport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100339 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
340 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
James Smarta53eb5e2007-04-27 12:41:09 -0400341
342 struct transport_container vport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100343 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
344 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345};
346
347#define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
348
James Bottomleyd0a7e572005-08-14 17:09:01 -0500349static int fc_target_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100350 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct scsi_target *starget = to_scsi_target(dev);
353 struct fc_rport *rport = starget_to_rport(starget);
354
355 /*
356 * if parent is remote port, use values from remote port.
357 * Otherwise, this host uses the fc_transport, but not the
358 * remote port interface. As such, initialize to known non-values.
359 */
360 if (rport) {
361 fc_starget_node_name(starget) = rport->node_name;
362 fc_starget_port_name(starget) = rport->port_name;
363 fc_starget_port_id(starget) = rport->port_id;
364 } else {
365 fc_starget_node_name(starget) = -1;
366 fc_starget_port_name(starget) = -1;
367 fc_starget_port_id(starget) = -1;
368 }
369
370 return 0;
371}
372
373static DECLARE_TRANSPORT_CLASS(fc_transport_class,
374 "fc_transport",
375 fc_target_setup,
376 NULL,
377 NULL);
378
James Bottomleyd0a7e572005-08-14 17:09:01 -0500379static int fc_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100380 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct Scsi_Host *shost = dev_to_shost(dev);
James Smartaedf3492006-04-10 10:14:05 -0400383 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
James Smart9ef3e4a2007-05-24 19:04:44 -0400385 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * Set default values easily detected by the midlayer as
387 * failure cases. The scsi lldd is responsible for initializing
388 * all transport attributes to valid values per host.
389 */
James Smartaedf3492006-04-10 10:14:05 -0400390 fc_host->node_name = -1;
391 fc_host->port_name = -1;
392 fc_host->permanent_port_name = -1;
393 fc_host->supported_classes = FC_COS_UNSPECIFIED;
394 memset(fc_host->supported_fc4s, 0,
395 sizeof(fc_host->supported_fc4s));
James Smartaedf3492006-04-10 10:14:05 -0400396 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
397 fc_host->maxframe_size = -1;
James Smarta53eb5e2007-04-27 12:41:09 -0400398 fc_host->max_npiv_vports = 0;
James Smartaedf3492006-04-10 10:14:05 -0400399 memset(fc_host->serial_number, 0,
400 sizeof(fc_host->serial_number));
Neerav Parikhbb8ef582012-01-22 17:29:50 -0800401 memset(fc_host->manufacturer, 0,
402 sizeof(fc_host->manufacturer));
403 memset(fc_host->model, 0,
404 sizeof(fc_host->model));
405 memset(fc_host->model_description, 0,
406 sizeof(fc_host->model_description));
407 memset(fc_host->hardware_version, 0,
408 sizeof(fc_host->hardware_version));
409 memset(fc_host->driver_version, 0,
410 sizeof(fc_host->driver_version));
411 memset(fc_host->firmware_version, 0,
412 sizeof(fc_host->firmware_version));
413 memset(fc_host->optionrom_version, 0,
414 sizeof(fc_host->optionrom_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
James Smartaedf3492006-04-10 10:14:05 -0400416 fc_host->port_id = -1;
417 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
418 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
419 memset(fc_host->active_fc4s, 0,
420 sizeof(fc_host->active_fc4s));
421 fc_host->speed = FC_PORTSPEED_UNKNOWN;
422 fc_host->fabric_name = -1;
James Smartb8d08212006-08-17 08:00:43 -0400423 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
424 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
James Smartaedf3492006-04-10 10:14:05 -0400426 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
James Smartaedf3492006-04-10 10:14:05 -0400428 INIT_LIST_HEAD(&fc_host->rports);
429 INIT_LIST_HEAD(&fc_host->rport_bindings);
James Smarta53eb5e2007-04-27 12:41:09 -0400430 INIT_LIST_HEAD(&fc_host->vports);
James Smartaedf3492006-04-10 10:14:05 -0400431 fc_host->next_rport_number = 0;
432 fc_host->next_target_id = 0;
James Smarta53eb5e2007-04-27 12:41:09 -0400433 fc_host->next_vport_number = 0;
434 fc_host->npiv_vports_inuse = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Kay Sieversaab0de22008-05-02 06:02:41 +0200436 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
437 "fc_wq_%d", shost->host_no);
Nithin Nayak Sujir112f6612011-04-25 12:30:06 -0700438 fc_host->work_q = alloc_workqueue(fc_host->work_q_name, 0, 0);
James Smartaedf3492006-04-10 10:14:05 -0400439 if (!fc_host->work_q)
440 return -ENOMEM;
441
Mike Christie43ca9102010-09-15 16:52:32 -0500442 fc_host->dev_loss_tmo = fc_dev_loss_tmo;
Kay Sieversaab0de22008-05-02 06:02:41 +0200443 snprintf(fc_host->devloss_work_q_name,
444 sizeof(fc_host->devloss_work_q_name),
445 "fc_dl_%d", shost->host_no);
Nithin Nayak Sujir112f6612011-04-25 12:30:06 -0700446 fc_host->devloss_work_q =
447 alloc_workqueue(fc_host->devloss_work_q_name, 0, 0);
James Smartaedf3492006-04-10 10:14:05 -0400448 if (!fc_host->devloss_work_q) {
449 destroy_workqueue(fc_host->work_q);
450 fc_host->work_q = NULL;
451 return -ENOMEM;
452 }
453
James Smart9e4f5e22009-03-26 13:33:19 -0400454 fc_bsg_hostadd(shost, fc_host);
455 /* ignore any bsg add error - we just can't do sgio */
456
457 return 0;
458}
459
460static int fc_host_remove(struct transport_container *tc, struct device *dev,
461 struct device *cdev)
462{
463 struct Scsi_Host *shost = dev_to_shost(dev);
464 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
465
466 fc_bsg_remove(fc_host->rqst_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0;
468}
469
470static DECLARE_TRANSPORT_CLASS(fc_host_class,
471 "fc_host",
472 fc_host_setup,
James Smart9e4f5e22009-03-26 13:33:19 -0400473 fc_host_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 NULL);
475
476/*
477 * Setup and Remove actions for remote ports are handled
478 * in the service functions below.
479 */
480static DECLARE_TRANSPORT_CLASS(fc_rport_class,
481 "fc_remote_ports",
482 NULL,
483 NULL,
484 NULL);
485
486/*
James Smarta53eb5e2007-04-27 12:41:09 -0400487 * Setup and Remove actions for virtual ports are handled
488 * in the service functions below.
489 */
490static DECLARE_TRANSPORT_CLASS(fc_vport_class,
491 "fc_vports",
492 NULL,
493 NULL,
494 NULL);
495
496/*
James Smart84314fd2006-08-18 17:30:09 -0400497 * Netlink Infrastructure
Rob Landleyeb448202007-11-03 13:30:39 -0500498 */
James Smart84314fd2006-08-18 17:30:09 -0400499
500static atomic_t fc_event_seq;
501
502/**
503 * fc_get_event_number - Obtain the next sequential FC event number
504 *
505 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -0500506 * We could have inlined this, but it would have required fc_event_seq to
James Smart84314fd2006-08-18 17:30:09 -0400507 * be exposed. For now, live with the subroutine call.
508 * Atomic used to avoid lock/unlock...
Rob Landleyeb448202007-11-03 13:30:39 -0500509 */
James Smart84314fd2006-08-18 17:30:09 -0400510u32
511fc_get_event_number(void)
512{
513 return atomic_add_return(1, &fc_event_seq);
514}
515EXPORT_SYMBOL(fc_get_event_number);
516
517
518/**
519 * fc_host_post_event - called to post an even on an fc_host.
James Smart84314fd2006-08-18 17:30:09 -0400520 * @shost: host the event occurred on
521 * @event_number: fc event number obtained from get_fc_event_number()
522 * @event_code: fc_host event being posted
523 * @event_data: 32bits of data for the event being posted
524 *
525 * Notes:
526 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500527 */
James Smart84314fd2006-08-18 17:30:09 -0400528void
529fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
530 enum fc_host_event_code event_code, u32 event_data)
531{
532 struct sk_buff *skb;
533 struct nlmsghdr *nlh;
534 struct fc_nl_event *event;
535 const char *name;
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000536 u32 len;
James Smart84314fd2006-08-18 17:30:09 -0400537 int err;
538
539 if (!scsi_nl_sock) {
540 err = -ENOENT;
541 goto send_fail;
542 }
543
544 len = FC_NL_MSGALIGN(sizeof(*event));
James Smart84314fd2006-08-18 17:30:09 -0400545
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000546 skb = nlmsg_new(len, GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400547 if (!skb) {
548 err = -ENOBUFS;
549 goto send_fail;
550 }
551
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000552 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
James Smart84314fd2006-08-18 17:30:09 -0400553 if (!nlh) {
554 err = -ENOBUFS;
555 goto send_fail_skb;
556 }
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000557 event = nlmsg_data(nlh);
James Smart84314fd2006-08-18 17:30:09 -0400558
559 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
560 FC_NL_ASYNC_EVENT, len);
561 event->seconds = get_seconds();
562 event->vendor_id = 0;
563 event->host_no = shost->host_no;
564 event->event_datalen = sizeof(u32); /* bytes */
565 event->event_num = event_number;
566 event->event_code = event_code;
567 event->event_data = event_data;
568
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800569 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
570 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400571 return;
572
573send_fail_skb:
574 kfree_skb(skb);
575send_fail:
576 name = get_fc_host_event_code_name(event_code);
577 printk(KERN_WARNING
578 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700579 __func__, shost->host_no,
James Smart84314fd2006-08-18 17:30:09 -0400580 (name) ? name : "<unknown>", event_data, err);
581 return;
582}
583EXPORT_SYMBOL(fc_host_post_event);
584
585
586/**
Rob Landleyeb448202007-11-03 13:30:39 -0500587 * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
James Smart84314fd2006-08-18 17:30:09 -0400588 * @shost: host the event occurred on
589 * @event_number: fc event number obtained from get_fc_event_number()
590 * @data_len: amount, in bytes, of vendor unique data
591 * @data_buf: pointer to vendor unique data
Rob Landleyeb448202007-11-03 13:30:39 -0500592 * @vendor_id: Vendor id
James Smart84314fd2006-08-18 17:30:09 -0400593 *
594 * Notes:
595 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500596 */
James Smart84314fd2006-08-18 17:30:09 -0400597void
598fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
James Smartf14e2e22006-08-22 09:55:23 -0400599 u32 data_len, char * data_buf, u64 vendor_id)
James Smart84314fd2006-08-18 17:30:09 -0400600{
601 struct sk_buff *skb;
602 struct nlmsghdr *nlh;
603 struct fc_nl_event *event;
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000604 u32 len;
James Smart84314fd2006-08-18 17:30:09 -0400605 int err;
606
607 if (!scsi_nl_sock) {
608 err = -ENOENT;
609 goto send_vendor_fail;
610 }
611
612 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
James Smart84314fd2006-08-18 17:30:09 -0400613
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000614 skb = nlmsg_new(len, GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400615 if (!skb) {
616 err = -ENOBUFS;
617 goto send_vendor_fail;
618 }
619
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000620 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
James Smart84314fd2006-08-18 17:30:09 -0400621 if (!nlh) {
622 err = -ENOBUFS;
623 goto send_vendor_fail_skb;
624 }
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000625 event = nlmsg_data(nlh);
James Smart84314fd2006-08-18 17:30:09 -0400626
627 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
628 FC_NL_ASYNC_EVENT, len);
629 event->seconds = get_seconds();
630 event->vendor_id = vendor_id;
631 event->host_no = shost->host_no;
632 event->event_datalen = data_len; /* bytes */
633 event->event_num = event_number;
634 event->event_code = FCH_EVT_VENDOR_UNIQUE;
635 memcpy(&event->event_data, data_buf, data_len);
636
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800637 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
638 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400639 return;
640
641send_vendor_fail_skb:
642 kfree_skb(skb);
643send_vendor_fail:
644 printk(KERN_WARNING
645 "%s: Dropped Event : host %d vendor_unique - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700646 __func__, shost->host_no, err);
James Smart84314fd2006-08-18 17:30:09 -0400647 return;
648}
649EXPORT_SYMBOL(fc_host_post_vendor_event);
650
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653static __init int fc_transport_init(void)
654{
James Smart84314fd2006-08-18 17:30:09 -0400655 int error;
656
657 atomic_set(&fc_event_seq, 0);
658
659 error = transport_class_register(&fc_host_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (error)
661 return error;
James Smarta53eb5e2007-04-27 12:41:09 -0400662 error = transport_class_register(&fc_vport_class);
663 if (error)
Mike Christie48de68a2009-11-17 21:25:16 -0600664 goto unreg_host_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 error = transport_class_register(&fc_rport_class);
666 if (error)
Mike Christie48de68a2009-11-17 21:25:16 -0600667 goto unreg_vport_class;
668 error = transport_class_register(&fc_transport_class);
669 if (error)
670 goto unreg_rport_class;
671 return 0;
672
673unreg_rport_class:
674 transport_class_unregister(&fc_rport_class);
675unreg_vport_class:
676 transport_class_unregister(&fc_vport_class);
677unreg_host_class:
678 transport_class_unregister(&fc_host_class);
679 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682static void __exit fc_transport_exit(void)
683{
684 transport_class_unregister(&fc_transport_class);
685 transport_class_unregister(&fc_rport_class);
686 transport_class_unregister(&fc_host_class);
James Smarta53eb5e2007-04-27 12:41:09 -0400687 transport_class_unregister(&fc_vport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/*
691 * FC Remote Port Attribute Management
692 */
693
694#define fc_rport_show_function(field, format_string, sz, cast) \
695static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100696show_fc_rport_##field (struct device *dev, \
697 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100699 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct Scsi_Host *shost = rport_to_shost(rport); \
701 struct fc_internal *i = to_fc_internal(shost->transportt); \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400702 if ((i->f->get_rport_##field) && \
703 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500704 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400705 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 i->f->get_rport_##field(rport); \
707 return snprintf(buf, sz, format_string, cast rport->field); \
708}
709
710#define fc_rport_store_function(field) \
711static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100712store_fc_rport_##field(struct device *dev, \
713 struct device_attribute *attr, \
714 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{ \
716 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100717 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct Scsi_Host *shost = rport_to_shost(rport); \
719 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -0400720 char *cp; \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400721 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500722 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400723 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
724 return -EBUSY; \
James Smart0f29b962006-08-18 17:33:29 -0400725 val = simple_strtoul(buf, &cp, 0); \
726 if (*cp && (*cp != '\n')) \
727 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 i->f->set_rport_##field(rport, val); \
729 return count; \
730}
731
732#define fc_rport_rd_attr(field, format_string, sz) \
733 fc_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100734static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 show_fc_rport_##field, NULL)
736
737#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
738 fc_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100739static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 show_fc_rport_##field, NULL)
741
742#define fc_rport_rw_attr(field, format_string, sz) \
743 fc_rport_show_function(field, format_string, sz, ) \
744 fc_rport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100745static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 show_fc_rport_##field, \
747 store_fc_rport_##field)
748
749
750#define fc_private_rport_show_function(field, format_string, sz, cast) \
751static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100752show_fc_rport_##field (struct device *dev, \
753 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100755 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return snprintf(buf, sz, format_string, cast rport->field); \
757}
758
759#define fc_private_rport_rd_attr(field, format_string, sz) \
760 fc_private_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +0100761static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 show_fc_rport_##field, NULL)
763
764#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
765 fc_private_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100766static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 show_fc_rport_##field, NULL)
768
769
770#define fc_private_rport_rd_enum_attr(title, maxlen) \
771static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100772show_fc_rport_##title (struct device *dev, \
773 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100775 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 const char *name; \
777 name = get_fc_##title##_name(rport->title); \
778 if (!name) \
779 return -EINVAL; \
780 return snprintf(buf, maxlen, "%s\n", name); \
781} \
Tony Jonesee959b02008-02-22 00:13:36 +0100782static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 show_fc_rport_##title, NULL)
784
785
786#define SETUP_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100787 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
789 i->private_rport_attrs[count].store = NULL; \
790 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
791 if (i->f->show_rport_##field) \
792 count++
793
794#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100795 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
797 i->private_rport_attrs[count].store = NULL; \
798 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
799 count++
800
801#define SETUP_RPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100802 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (!i->f->set_rport_##field) { \
804 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
805 i->private_rport_attrs[count].store = NULL; \
806 } \
807 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
808 if (i->f->show_rport_##field) \
809 count++
810
James Smart0f29b962006-08-18 17:33:29 -0400811#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
812{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100813 i->private_rport_attrs[count] = device_attr_rport_##field; \
James Smart0f29b962006-08-18 17:33:29 -0400814 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
815 count++; \
816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819/* The FC Transport Remote Port Attributes: */
820
821/* Fixed Remote Port Attributes */
822
823fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
824
825static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100826show_fc_rport_supported_classes (struct device *dev,
827 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Tony Jonesee959b02008-02-22 00:13:36 +0100829 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (rport->supported_classes == FC_COS_UNSPECIFIED)
831 return snprintf(buf, 20, "unspecified\n");
832 return get_fc_cos_names(rport->supported_classes, buf);
833}
Tony Jonesee959b02008-02-22 00:13:36 +0100834static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 show_fc_rport_supported_classes, NULL);
836
837/* Dynamic Remote Port Attributes */
838
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400839/*
840 * dev_loss_tmo attribute
841 */
Mike Christie43ca9102010-09-15 16:52:32 -0500842static int fc_str_to_dev_loss(const char *buf, unsigned long *val)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400843{
Mike Christie43ca9102010-09-15 16:52:32 -0500844 char *cp;
845
846 *val = simple_strtoul(buf, &cp, 0);
847 if ((*cp && (*cp != '\n')) || (*val < 0))
848 return -EINVAL;
849 /*
850 * Check for overflow; dev_loss_tmo is u32
851 */
852 if (*val > UINT_MAX)
853 return -EINVAL;
854
855 return 0;
856}
857
858static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
859 unsigned long val)
860{
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400861 struct Scsi_Host *shost = rport_to_shost(rport);
862 struct fc_internal *i = to_fc_internal(shost->transportt);
Mike Christie43ca9102010-09-15 16:52:32 -0500863
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400864 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500865 (rport->port_state == FC_PORTSTATE_DELETED) ||
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400866 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
867 return -EBUSY;
Hannes Reineckef28186632009-12-15 09:26:06 +0100868 /*
Hannes Reinecke36dd2882010-03-09 10:18:48 +0100869 * Check for overflow; dev_loss_tmo is u32
870 */
871 if (val > UINT_MAX)
872 return -EINVAL;
873
874 /*
Hannes Reineckef28186632009-12-15 09:26:06 +0100875 * If fast_io_fail is off we have to cap
876 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
877 */
878 if (rport->fast_io_fail_tmo == -1 &&
879 val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
880 return -EINVAL;
881
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400882 i->f->set_rport_dev_loss_tmo(rport, val);
Mike Christie43ca9102010-09-15 16:52:32 -0500883 return 0;
884}
885
886fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
887static ssize_t
888store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
889 const char *buf, size_t count)
890{
891 struct fc_rport *rport = transport_class_to_rport(dev);
892 unsigned long val;
893 int rc;
894
895 rc = fc_str_to_dev_loss(buf, &val);
896 if (rc)
897 return rc;
898
899 rc = fc_rport_set_dev_loss_tmo(rport, val);
900 if (rc)
901 return rc;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400902 return count;
903}
Tony Jonesee959b02008-02-22 00:13:36 +0100904static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400905 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907
908/* Private Remote Port Attributes */
909
910fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
911fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
912fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
913
914static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100915show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
916 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Tony Jonesee959b02008-02-22 00:13:36 +0100918 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /* identify any roles that are port_id specific */
921 if ((rport->port_id != -1) &&
922 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
923 FC_WELLKNOWN_PORTID_MASK) {
924 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
925 case FC_FPORT_PORTID:
926 return snprintf(buf, 30, "Fabric Port\n");
927 case FC_FABCTLR_PORTID:
928 return snprintf(buf, 30, "Fabric Controller\n");
929 case FC_DIRSRVR_PORTID:
930 return snprintf(buf, 30, "Directory Server\n");
931 case FC_TIMESRVR_PORTID:
932 return snprintf(buf, 30, "Time Server\n");
933 case FC_MGMTSRVR_PORTID:
934 return snprintf(buf, 30, "Management Server\n");
935 default:
936 return snprintf(buf, 30, "Unknown Fabric Entity\n");
937 }
938 } else {
James Smarta53eb5e2007-04-27 12:41:09 -0400939 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return snprintf(buf, 20, "unknown\n");
James Smarta53eb5e2007-04-27 12:41:09 -0400941 return get_fc_port_roles_names(rport->roles, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943}
Tony Jonesee959b02008-02-22 00:13:36 +0100944static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 show_fc_rport_roles, NULL);
946
947fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
948fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
949
James Smart0f29b962006-08-18 17:33:29 -0400950/*
951 * fast_io_fail_tmo attribute
952 */
953static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100954show_fc_rport_fast_io_fail_tmo (struct device *dev,
955 struct device_attribute *attr, char *buf)
James Smart0f29b962006-08-18 17:33:29 -0400956{
Tony Jonesee959b02008-02-22 00:13:36 +0100957 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400958
959 if (rport->fast_io_fail_tmo == -1)
960 return snprintf(buf, 5, "off\n");
961 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
962}
963
964static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100965store_fc_rport_fast_io_fail_tmo(struct device *dev,
966 struct device_attribute *attr, const char *buf,
967 size_t count)
James Smart0f29b962006-08-18 17:33:29 -0400968{
969 int val;
970 char *cp;
Tony Jonesee959b02008-02-22 00:13:36 +0100971 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -0400972
973 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
974 (rport->port_state == FC_PORTSTATE_DELETED) ||
975 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
976 return -EBUSY;
977 if (strncmp(buf, "off", 3) == 0)
978 rport->fast_io_fail_tmo = -1;
979 else {
980 val = simple_strtoul(buf, &cp, 0);
Hannes Reineckef28186632009-12-15 09:26:06 +0100981 if ((*cp && (*cp != '\n')) || (val < 0))
James Smart0f29b962006-08-18 17:33:29 -0400982 return -EINVAL;
Hannes Reineckef28186632009-12-15 09:26:06 +0100983 /*
984 * Cap fast_io_fail by dev_loss_tmo or
985 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
986 */
987 if ((val >= rport->dev_loss_tmo) ||
988 (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
989 return -EINVAL;
990
James Smart0f29b962006-08-18 17:33:29 -0400991 rport->fast_io_fail_tmo = val;
992 }
993 return count;
994}
Tony Jonesee959b02008-02-22 00:13:36 +0100995static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
James Smart0f29b962006-08-18 17:33:29 -0400996 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998
999/*
1000 * FC SCSI Target Attribute Management
1001 */
1002
1003/*
1004 * Note: in the target show function we recognize when the remote
Uwe Kleine-König732bee72010-06-11 12:16:59 +02001005 * port is in the hierarchy and do not allow the driver to get
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * involved in sysfs functions. The driver only gets involved if
1007 * it's the "old" style that doesn't use rports.
1008 */
1009#define fc_starget_show_function(field, format_string, sz, cast) \
1010static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001011show_fc_starget_##field (struct device *dev, \
1012 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001014 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
1016 struct fc_internal *i = to_fc_internal(shost->transportt); \
1017 struct fc_rport *rport = starget_to_rport(starget); \
1018 if (rport) \
1019 fc_starget_##field(starget) = rport->field; \
1020 else if (i->f->get_starget_##field) \
1021 i->f->get_starget_##field(starget); \
1022 return snprintf(buf, sz, format_string, \
1023 cast fc_starget_##field(starget)); \
1024}
1025
1026#define fc_starget_rd_attr(field, format_string, sz) \
1027 fc_starget_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001028static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 show_fc_starget_##field, NULL)
1030
1031#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
1032 fc_starget_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001033static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 show_fc_starget_##field, NULL)
1035
1036#define SETUP_STARGET_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001037 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1039 i->private_starget_attrs[count].store = NULL; \
1040 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1041 if (i->f->show_starget_##field) \
1042 count++
1043
1044#define SETUP_STARGET_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001045 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (!i->f->set_starget_##field) { \
1047 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1048 i->private_starget_attrs[count].store = NULL; \
1049 } \
1050 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1051 if (i->f->show_starget_##field) \
1052 count++
1053
1054/* The FC Transport SCSI Target Attributes: */
1055fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1056fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1057fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1058
1059
1060/*
James Smarta53eb5e2007-04-27 12:41:09 -04001061 * FC Virtual Port Attribute Management
1062 */
1063
1064#define fc_vport_show_function(field, format_string, sz, cast) \
1065static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001066show_fc_vport_##field (struct device *dev, \
1067 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001068{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001069 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001070 struct Scsi_Host *shost = vport_to_shost(vport); \
1071 struct fc_internal *i = to_fc_internal(shost->transportt); \
1072 if ((i->f->get_vport_##field) && \
1073 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1074 i->f->get_vport_##field(vport); \
1075 return snprintf(buf, sz, format_string, cast vport->field); \
1076}
1077
1078#define fc_vport_store_function(field) \
1079static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001080store_fc_vport_##field(struct device *dev, \
1081 struct device_attribute *attr, \
1082 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001083{ \
1084 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001085 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001086 struct Scsi_Host *shost = vport_to_shost(vport); \
1087 struct fc_internal *i = to_fc_internal(shost->transportt); \
1088 char *cp; \
1089 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1090 return -EBUSY; \
1091 val = simple_strtoul(buf, &cp, 0); \
1092 if (*cp && (*cp != '\n')) \
1093 return -EINVAL; \
1094 i->f->set_vport_##field(vport, val); \
1095 return count; \
1096}
1097
1098#define fc_vport_store_str_function(field, slen) \
1099static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001100store_fc_vport_##field(struct device *dev, \
1101 struct device_attribute *attr, \
1102 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001103{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001104 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001105 struct Scsi_Host *shost = vport_to_shost(vport); \
1106 struct fc_internal *i = to_fc_internal(shost->transportt); \
1107 unsigned int cnt=count; \
1108 \
1109 /* count may include a LF at end of string */ \
1110 if (buf[cnt-1] == '\n') \
1111 cnt--; \
1112 if (cnt > ((slen) - 1)) \
1113 return -EINVAL; \
1114 memcpy(vport->field, buf, cnt); \
1115 i->f->set_vport_##field(vport); \
1116 return count; \
1117}
1118
1119#define fc_vport_rd_attr(field, format_string, sz) \
1120 fc_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001121static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001122 show_fc_vport_##field, NULL)
1123
1124#define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1125 fc_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001126static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001127 show_fc_vport_##field, NULL)
1128
1129#define fc_vport_rw_attr(field, format_string, sz) \
1130 fc_vport_show_function(field, format_string, sz, ) \
1131 fc_vport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001132static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001133 show_fc_vport_##field, \
1134 store_fc_vport_##field)
1135
1136#define fc_private_vport_show_function(field, format_string, sz, cast) \
1137static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001138show_fc_vport_##field (struct device *dev, \
1139 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001140{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001141 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001142 return snprintf(buf, sz, format_string, cast vport->field); \
1143}
1144
1145#define fc_private_vport_store_u32_function(field) \
1146static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001147store_fc_vport_##field(struct device *dev, \
1148 struct device_attribute *attr, \
1149 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001150{ \
1151 u32 val; \
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 char *cp; \
1154 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1155 return -EBUSY; \
1156 val = simple_strtoul(buf, &cp, 0); \
1157 if (*cp && (*cp != '\n')) \
1158 return -EINVAL; \
1159 vport->field = val; \
1160 return count; \
1161}
1162
1163
1164#define fc_private_vport_rd_attr(field, format_string, sz) \
1165 fc_private_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001166static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001167 show_fc_vport_##field, NULL)
1168
1169#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1170 fc_private_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001171static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001172 show_fc_vport_##field, NULL)
1173
1174#define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1175 fc_private_vport_show_function(field, format_string, sz, ) \
1176 fc_private_vport_store_u32_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001177static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001178 show_fc_vport_##field, \
1179 store_fc_vport_##field)
1180
1181
1182#define fc_private_vport_rd_enum_attr(title, maxlen) \
1183static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001184show_fc_vport_##title (struct device *dev, \
1185 struct device_attribute *attr, \
1186 char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001187{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001188 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001189 const char *name; \
1190 name = get_fc_##title##_name(vport->title); \
1191 if (!name) \
1192 return -EINVAL; \
1193 return snprintf(buf, maxlen, "%s\n", name); \
1194} \
Tony Jonesee959b02008-02-22 00:13:36 +01001195static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001196 show_fc_vport_##title, NULL)
1197
1198
1199#define SETUP_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001200 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001201 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1202 i->private_vport_attrs[count].store = NULL; \
1203 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1204 if (i->f->get_##field) \
1205 count++
1206 /* NOTE: Above MACRO differs: checks function not show bit */
1207
1208#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001209 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001210 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1211 i->private_vport_attrs[count].store = NULL; \
1212 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1213 count++
1214
1215#define SETUP_VPORT_ATTRIBUTE_WR(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001216 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001217 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1218 if (i->f->field) \
1219 count++
1220 /* NOTE: Above MACRO differs: checks function */
1221
1222#define SETUP_VPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001223 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001224 if (!i->f->set_vport_##field) { \
1225 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1226 i->private_vport_attrs[count].store = NULL; \
1227 } \
1228 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1229 count++
1230 /* NOTE: Above MACRO differs: does not check show bit */
1231
1232#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1233{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001234 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001235 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1236 count++; \
1237}
1238
1239
1240/* The FC Transport Virtual Port Attributes: */
1241
1242/* Fixed Virtual Port Attributes */
1243
1244/* Dynamic Virtual Port Attributes */
1245
1246/* Private Virtual Port Attributes */
1247
1248fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1249fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1250fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1251fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1252
1253static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001254show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1255 char *buf)
James Smarta53eb5e2007-04-27 12:41:09 -04001256{
Tony Jonesee959b02008-02-22 00:13:36 +01001257 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001258
1259 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1260 return snprintf(buf, 20, "unknown\n");
1261 return get_fc_port_roles_names(vport->roles, buf);
1262}
Tony Jonesee959b02008-02-22 00:13:36 +01001263static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
James Smarta53eb5e2007-04-27 12:41:09 -04001264
1265fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1266
1267fc_private_vport_show_function(symbolic_name, "%s\n",
1268 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1269fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
Tony Jonesee959b02008-02-22 00:13:36 +01001270static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001271 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1272
1273static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001274store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1275 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001276{
Tony Jonesee959b02008-02-22 00:13:36 +01001277 struct fc_vport *vport = transport_class_to_vport(dev);
James Smart9ef3e4a2007-05-24 19:04:44 -04001278 struct Scsi_Host *shost = vport_to_shost(vport);
Gal Rosen0d9dc7c2010-01-21 10:15:32 +02001279 unsigned long flags;
1280
1281 spin_lock_irqsave(shost->host_lock, flags);
1282 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
1283 spin_unlock_irqrestore(shost->host_lock, flags);
1284 return -EBUSY;
1285 }
1286 vport->flags |= FC_VPORT_DELETING;
1287 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04001288
James Smart9ef3e4a2007-05-24 19:04:44 -04001289 fc_queue_work(shost, &vport->vport_delete_work);
James Smarta53eb5e2007-04-27 12:41:09 -04001290 return count;
1291}
Tony Jonesee959b02008-02-22 00:13:36 +01001292static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001293 NULL, store_fc_vport_delete);
1294
1295
1296/*
1297 * Enable/Disable vport
1298 * Write "1" to disable, write "0" to enable
1299 */
1300static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001301store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1302 const char *buf,
James Smarta53eb5e2007-04-27 12:41:09 -04001303 size_t count)
1304{
Tony Jonesee959b02008-02-22 00:13:36 +01001305 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001306 struct Scsi_Host *shost = vport_to_shost(vport);
1307 struct fc_internal *i = to_fc_internal(shost->transportt);
1308 int stat;
1309
1310 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1311 return -EBUSY;
1312
1313 if (*buf == '0') {
1314 if (vport->vport_state != FC_VPORT_DISABLED)
1315 return -EALREADY;
1316 } else if (*buf == '1') {
1317 if (vport->vport_state == FC_VPORT_DISABLED)
1318 return -EALREADY;
1319 } else
1320 return -EINVAL;
1321
1322 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1323 return stat ? stat : count;
1324}
Tony Jonesee959b02008-02-22 00:13:36 +01001325static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001326 NULL, store_fc_vport_disable);
1327
1328
1329/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 * Host Attribute Management
1331 */
1332
1333#define fc_host_show_function(field, format_string, sz, cast) \
1334static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001335show_fc_host_##field (struct device *dev, \
1336 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001338 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct fc_internal *i = to_fc_internal(shost->transportt); \
1340 if (i->f->get_host_##field) \
1341 i->f->get_host_##field(shost); \
1342 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1343}
1344
1345#define fc_host_store_function(field) \
1346static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001347store_fc_host_##field(struct device *dev, \
1348 struct device_attribute *attr, \
1349 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{ \
1351 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001352 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -04001354 char *cp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 \
James Smart0f29b962006-08-18 17:33:29 -04001356 val = simple_strtoul(buf, &cp, 0); \
1357 if (*cp && (*cp != '\n')) \
1358 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 i->f->set_host_##field(shost, val); \
1360 return count; \
1361}
1362
James Smartb8d08212006-08-17 08:00:43 -04001363#define fc_host_store_str_function(field, slen) \
1364static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001365store_fc_host_##field(struct device *dev, \
1366 struct device_attribute *attr, \
1367 const char *buf, size_t count) \
James Smartb8d08212006-08-17 08:00:43 -04001368{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001369 struct Scsi_Host *shost = transport_class_to_shost(dev); \
James Smartb8d08212006-08-17 08:00:43 -04001370 struct fc_internal *i = to_fc_internal(shost->transportt); \
1371 unsigned int cnt=count; \
1372 \
1373 /* count may include a LF at end of string */ \
1374 if (buf[cnt-1] == '\n') \
1375 cnt--; \
1376 if (cnt > ((slen) - 1)) \
1377 return -EINVAL; \
1378 memcpy(fc_host_##field(shost), buf, cnt); \
1379 i->f->set_host_##field(shost); \
1380 return count; \
1381}
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383#define fc_host_rd_attr(field, format_string, sz) \
1384 fc_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001385static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 show_fc_host_##field, NULL)
1387
1388#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1389 fc_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001390static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 show_fc_host_##field, NULL)
1392
1393#define fc_host_rw_attr(field, format_string, sz) \
1394 fc_host_show_function(field, format_string, sz, ) \
1395 fc_host_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001396static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 show_fc_host_##field, \
1398 store_fc_host_##field)
1399
1400#define fc_host_rd_enum_attr(title, maxlen) \
1401static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001402show_fc_host_##title (struct device *dev, \
1403 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001405 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 struct fc_internal *i = to_fc_internal(shost->transportt); \
1407 const char *name; \
1408 if (i->f->get_host_##title) \
1409 i->f->get_host_##title(shost); \
1410 name = get_fc_##title##_name(fc_host_##title(shost)); \
1411 if (!name) \
1412 return -EINVAL; \
1413 return snprintf(buf, maxlen, "%s\n", name); \
1414} \
Tony Jonesee959b02008-02-22 00:13:36 +01001415static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417#define SETUP_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001418 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1420 i->private_host_attrs[count].store = NULL; \
1421 i->host_attrs[count] = &i->private_host_attrs[count]; \
1422 if (i->f->show_host_##field) \
1423 count++
1424
James Smarta53eb5e2007-04-27 12:41:09 -04001425#define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001426 i->private_host_attrs[count] = device_attr_host_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001427 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1428 i->private_host_attrs[count].store = NULL; \
1429 i->host_attrs[count] = &i->private_host_attrs[count]; \
1430 count++
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432#define SETUP_HOST_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001433 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (!i->f->set_host_##field) { \
1435 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1436 i->private_host_attrs[count].store = NULL; \
1437 } \
1438 i->host_attrs[count] = &i->private_host_attrs[count]; \
1439 if (i->f->show_host_##field) \
1440 count++
1441
1442
1443#define fc_private_host_show_function(field, format_string, sz, cast) \
1444static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001445show_fc_host_##field (struct device *dev, \
1446 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001448 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1450}
1451
1452#define fc_private_host_rd_attr(field, format_string, sz) \
1453 fc_private_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001454static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 show_fc_host_##field, NULL)
1456
1457#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1458 fc_private_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001459static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 show_fc_host_##field, NULL)
1461
1462#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001463 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1465 i->private_host_attrs[count].store = NULL; \
1466 i->host_attrs[count] = &i->private_host_attrs[count]; \
1467 count++
1468
1469#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001470{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001471 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 i->host_attrs[count] = &i->private_host_attrs[count]; \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001473 count++; \
1474}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476
1477/* Fixed Host Attributes */
1478
1479static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001480show_fc_host_supported_classes (struct device *dev,
1481 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Tony Jonesee959b02008-02-22 00:13:36 +01001483 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1486 return snprintf(buf, 20, "unspecified\n");
1487
1488 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1489}
Tony Jonesee959b02008-02-22 00:13:36 +01001490static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 show_fc_host_supported_classes, NULL);
1492
1493static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001494show_fc_host_supported_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 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1499}
Tony Jonesee959b02008-02-22 00:13:36 +01001500static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 show_fc_host_supported_fc4s, NULL);
1502
1503static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001504show_fc_host_supported_speeds (struct device *dev,
1505 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Tony Jonesee959b02008-02-22 00:13:36 +01001507 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1510 return snprintf(buf, 20, "unknown\n");
1511
1512 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1513}
Tony Jonesee959b02008-02-22 00:13:36 +01001514static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 show_fc_host_supported_speeds, NULL);
1516
1517
1518fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1519fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01001520fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1521 unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
James Smarta53eb5e2007-04-27 12:41:09 -04001523fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
Neerav Parikhbb8ef582012-01-22 17:29:50 -08001525fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
1526fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1527fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1528fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1529fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1530fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1531fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533
1534/* Dynamic Host Attributes */
1535
1536static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001537show_fc_host_active_fc4s (struct device *dev,
1538 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Tony Jonesee959b02008-02-22 00:13:36 +01001540 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 struct fc_internal *i = to_fc_internal(shost->transportt);
1542
1543 if (i->f->get_host_active_fc4s)
1544 i->f->get_host_active_fc4s(shost);
1545
1546 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1547}
Tony Jonesee959b02008-02-22 00:13:36 +01001548static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 show_fc_host_active_fc4s, NULL);
1550
1551static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001552show_fc_host_speed (struct device *dev,
1553 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Tony Jonesee959b02008-02-22 00:13:36 +01001555 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct fc_internal *i = to_fc_internal(shost->transportt);
1557
1558 if (i->f->get_host_speed)
1559 i->f->get_host_speed(shost);
1560
1561 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1562 return snprintf(buf, 20, "unknown\n");
1563
1564 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1565}
Tony Jonesee959b02008-02-22 00:13:36 +01001566static FC_DEVICE_ATTR(host, speed, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 show_fc_host_speed, NULL);
1568
1569
1570fc_host_rd_attr(port_id, "0x%06x\n", 20);
1571fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1572fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1573fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
James Smart016131b2006-08-14 08:20:25 -04001574fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
James Smartb8d08212006-08-17 08:00:43 -04001576fc_private_host_show_function(system_hostname, "%s\n",
1577 FC_SYMBOLIC_NAME_SIZE + 1, )
1578fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
Tony Jonesee959b02008-02-22 00:13:36 +01001579static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
James Smartb8d08212006-08-17 08:00:43 -04001580 show_fc_host_system_hostname, store_fc_host_system_hostname);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583/* Private Host Attributes */
1584
1585static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001586show_fc_private_host_tgtid_bind_type(struct device *dev,
1587 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Tony Jonesee959b02008-02-22 00:13:36 +01001589 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 const char *name;
1591
1592 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1593 if (!name)
1594 return -EINVAL;
1595 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1596}
1597
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001598#define get_list_head_entry(pos, head, member) \
1599 pos = list_entry((head)->next, typeof(*pos), member)
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001602store_fc_private_host_tgtid_bind_type(struct device *dev,
1603 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Tony Jonesee959b02008-02-22 00:13:36 +01001605 struct Scsi_Host *shost = transport_class_to_shost(dev);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001606 struct fc_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 enum fc_tgtid_binding_type val;
1608 unsigned long flags;
1609
1610 if (get_fc_tgtid_bind_type_match(buf, &val))
1611 return -EINVAL;
1612
1613 /* if changing bind type, purge all unused consistent bindings */
1614 if (val != fc_host_tgtid_bind_type(shost)) {
1615 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001616 while (!list_empty(&fc_host_rport_bindings(shost))) {
1617 get_list_head_entry(rport,
1618 &fc_host_rport_bindings(shost), peers);
James Smartaedf3492006-04-10 10:14:05 -04001619 list_del(&rport->peers);
1620 rport->port_state = FC_PORTSTATE_DELETED;
1621 fc_queue_work(shost, &rport->rport_delete_work);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04001622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 spin_unlock_irqrestore(shost->host_lock, flags);
1624 }
1625
1626 fc_host_tgtid_bind_type(shost) = val;
1627 return count;
1628}
1629
Tony Jonesee959b02008-02-22 00:13:36 +01001630static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 show_fc_private_host_tgtid_bind_type,
1632 store_fc_private_host_tgtid_bind_type);
1633
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001634static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001635store_fc_private_host_issue_lip(struct device *dev,
1636 struct device_attribute *attr, const char *buf, size_t count)
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001637{
Tony Jonesee959b02008-02-22 00:13:36 +01001638 struct Scsi_Host *shost = transport_class_to_shost(dev);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001639 struct fc_internal *i = to_fc_internal(shost->transportt);
1640 int ret;
1641
1642 /* ignore any data value written to the attribute */
1643 if (i->f->issue_fc_host_lip) {
1644 ret = i->f->issue_fc_host_lip(shost);
1645 return ret ? ret: count;
1646 }
1647
1648 return -ENOENT;
1649}
1650
Tony Jonesee959b02008-02-22 00:13:36 +01001651static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001652 store_fc_private_host_issue_lip);
1653
Mike Christie43ca9102010-09-15 16:52:32 -05001654static ssize_t
1655store_fc_private_host_dev_loss_tmo(struct device *dev,
1656 struct device_attribute *attr,
1657 const char *buf, size_t count)
1658{
1659 struct Scsi_Host *shost = transport_class_to_shost(dev);
1660 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1661 struct fc_rport *rport;
1662 unsigned long val, flags;
1663 int rc;
James Smarta53eb5e2007-04-27 12:41:09 -04001664
Mike Christie43ca9102010-09-15 16:52:32 -05001665 rc = fc_str_to_dev_loss(buf, &val);
1666 if (rc)
1667 return rc;
1668
1669 fc_host_dev_loss_tmo(shost) = val;
1670 spin_lock_irqsave(shost->host_lock, flags);
1671 list_for_each_entry(rport, &fc_host->rports, peers)
1672 fc_rport_set_dev_loss_tmo(rport, val);
1673 spin_unlock_irqrestore(shost->host_lock, flags);
1674 return count;
1675}
1676
1677fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
1678static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
1679 show_fc_host_dev_loss_tmo,
1680 store_fc_private_host_dev_loss_tmo);
1681
1682fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
James Smarta53eb5e2007-04-27 12:41:09 -04001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684/*
1685 * Host Statistics Management
1686 */
1687
1688/* Show a given an attribute in the statistics group */
1689static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001690fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Tony Jonesee959b02008-02-22 00:13:36 +01001692 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 struct fc_internal *i = to_fc_internal(shost->transportt);
1694 struct fc_host_statistics *stats;
1695 ssize_t ret = -ENOENT;
1696
1697 if (offset > sizeof(struct fc_host_statistics) ||
1698 offset % sizeof(u64) != 0)
1699 WARN_ON(1);
1700
1701 if (i->f->get_fc_host_stats) {
1702 stats = (i->f->get_fc_host_stats)(shost);
1703 if (stats)
1704 ret = snprintf(buf, 20, "0x%llx\n",
1705 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1706 }
1707 return ret;
1708}
1709
1710
1711/* generate a read-only statistics attribute */
1712#define fc_host_statistic(name) \
Tony Jonesee959b02008-02-22 00:13:36 +01001713static ssize_t show_fcstat_##name(struct device *cd, \
1714 struct device_attribute *attr, \
1715 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{ \
1717 return fc_stat_show(cd, buf, \
1718 offsetof(struct fc_host_statistics, name)); \
1719} \
Tony Jonesee959b02008-02-22 00:13:36 +01001720static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722fc_host_statistic(seconds_since_last_reset);
1723fc_host_statistic(tx_frames);
1724fc_host_statistic(tx_words);
1725fc_host_statistic(rx_frames);
1726fc_host_statistic(rx_words);
1727fc_host_statistic(lip_count);
1728fc_host_statistic(nos_count);
1729fc_host_statistic(error_frames);
1730fc_host_statistic(dumped_frames);
1731fc_host_statistic(link_failure_count);
1732fc_host_statistic(loss_of_sync_count);
1733fc_host_statistic(loss_of_signal_count);
1734fc_host_statistic(prim_seq_protocol_err_count);
1735fc_host_statistic(invalid_tx_word_count);
1736fc_host_statistic(invalid_crc_count);
1737fc_host_statistic(fcp_input_requests);
1738fc_host_statistic(fcp_output_requests);
1739fc_host_statistic(fcp_control_requests);
1740fc_host_statistic(fcp_input_megabytes);
1741fc_host_statistic(fcp_output_megabytes);
Vasu Deve58abb02012-05-25 10:26:38 -07001742fc_host_statistic(fcp_packet_alloc_failures);
1743fc_host_statistic(fcp_packet_aborts);
1744fc_host_statistic(fcp_frame_alloc_failures);
1745fc_host_statistic(fc_no_free_exch);
1746fc_host_statistic(fc_no_free_exch_xid);
1747fc_host_statistic(fc_xid_not_found);
1748fc_host_statistic(fc_xid_busy);
1749fc_host_statistic(fc_seq_not_found);
1750fc_host_statistic(fc_non_bls_resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001753fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1754 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Tony Jonesee959b02008-02-22 00:13:36 +01001756 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 struct fc_internal *i = to_fc_internal(shost->transportt);
1758
1759 /* ignore any data value written to the attribute */
1760 if (i->f->reset_fc_host_stats) {
1761 i->f->reset_fc_host_stats(shost);
1762 return count;
1763 }
1764
1765 return -ENOENT;
1766}
Tony Jonesee959b02008-02-22 00:13:36 +01001767static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 fc_reset_statistics);
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770static struct attribute *fc_statistics_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001771 &device_attr_host_seconds_since_last_reset.attr,
1772 &device_attr_host_tx_frames.attr,
1773 &device_attr_host_tx_words.attr,
1774 &device_attr_host_rx_frames.attr,
1775 &device_attr_host_rx_words.attr,
1776 &device_attr_host_lip_count.attr,
1777 &device_attr_host_nos_count.attr,
1778 &device_attr_host_error_frames.attr,
1779 &device_attr_host_dumped_frames.attr,
1780 &device_attr_host_link_failure_count.attr,
1781 &device_attr_host_loss_of_sync_count.attr,
1782 &device_attr_host_loss_of_signal_count.attr,
1783 &device_attr_host_prim_seq_protocol_err_count.attr,
1784 &device_attr_host_invalid_tx_word_count.attr,
1785 &device_attr_host_invalid_crc_count.attr,
1786 &device_attr_host_fcp_input_requests.attr,
1787 &device_attr_host_fcp_output_requests.attr,
1788 &device_attr_host_fcp_control_requests.attr,
1789 &device_attr_host_fcp_input_megabytes.attr,
1790 &device_attr_host_fcp_output_megabytes.attr,
Vasu Deve58abb02012-05-25 10:26:38 -07001791 &device_attr_host_fcp_packet_alloc_failures.attr,
1792 &device_attr_host_fcp_packet_aborts.attr,
1793 &device_attr_host_fcp_frame_alloc_failures.attr,
1794 &device_attr_host_fc_no_free_exch.attr,
1795 &device_attr_host_fc_no_free_exch_xid.attr,
1796 &device_attr_host_fc_xid_not_found.attr,
1797 &device_attr_host_fc_xid_busy.attr,
1798 &device_attr_host_fc_seq_not_found.attr,
1799 &device_attr_host_fc_non_bls_resp.attr,
Tony Jonesee959b02008-02-22 00:13:36 +01001800 &device_attr_host_reset_statistics.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 NULL
1802};
1803
1804static struct attribute_group fc_statistics_group = {
1805 .name = "statistics",
1806 .attrs = fc_statistics_attrs,
1807};
1808
James Smarta53eb5e2007-04-27 12:41:09 -04001809
1810/* Host Vport Attributes */
1811
1812static int
1813fc_parse_wwn(const char *ns, u64 *nm)
1814{
1815 unsigned int i, j;
1816 u8 wwn[8];
1817
1818 memset(wwn, 0, sizeof(wwn));
1819
1820 /* Validate and store the new name */
1821 for (i=0, j=0; i < 16; i++) {
Andy Shevchenkoecc30992010-08-10 18:01:27 -07001822 int value;
1823
1824 value = hex_to_bin(*ns++);
1825 if (value >= 0)
1826 j = (j << 4) | value;
James Smarta53eb5e2007-04-27 12:41:09 -04001827 else
1828 return -EINVAL;
1829 if (i % 2) {
1830 wwn[i/2] = j & 0xff;
1831 j = 0;
1832 }
1833 }
1834
1835 *nm = wwn_to_u64(wwn);
1836
1837 return 0;
1838}
1839
1840
1841/*
1842 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1843 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1844 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1845 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1846 */
1847static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001848store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1849 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001850{
Tony Jonesee959b02008-02-22 00:13:36 +01001851 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001852 struct fc_vport_identifiers vid;
1853 struct fc_vport *vport;
1854 unsigned int cnt=count;
1855 int stat;
1856
1857 memset(&vid, 0, sizeof(vid));
1858
1859 /* count may include a LF at end of string */
1860 if (buf[cnt-1] == '\n')
1861 cnt--;
1862
1863 /* validate we have enough characters for WWPN */
1864 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1865 return -EINVAL;
1866
1867 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1868 if (stat)
1869 return stat;
1870
1871 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1872 if (stat)
1873 return stat;
1874
1875 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1876 vid.vport_type = FC_PORTTYPE_NPIV;
1877 /* vid.symbolic_name is already zero/NULL's */
1878 vid.disable = false; /* always enabled */
1879
1880 /* we only allow support on Channel 0 !!! */
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07001881 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
James Smarta53eb5e2007-04-27 12:41:09 -04001882 return stat ? stat : count;
1883}
Tony Jonesee959b02008-02-22 00:13:36 +01001884static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001885 store_fc_host_vport_create);
1886
1887
1888/*
1889 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1890 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1891 * The WWNs are specified as hex characters, and may *not* contain
1892 * any prefixes (e.g. 0x, x, etc)
1893 */
1894static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001895store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1896 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001897{
Tony Jonesee959b02008-02-22 00:13:36 +01001898 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001899 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1900 struct fc_vport *vport;
1901 u64 wwpn, wwnn;
1902 unsigned long flags;
1903 unsigned int cnt=count;
1904 int stat, match;
1905
1906 /* count may include a LF at end of string */
1907 if (buf[cnt-1] == '\n')
1908 cnt--;
1909
1910 /* validate we have enough characters for WWPN */
1911 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1912 return -EINVAL;
1913
1914 stat = fc_parse_wwn(&buf[0], &wwpn);
1915 if (stat)
1916 return stat;
1917
1918 stat = fc_parse_wwn(&buf[17], &wwnn);
1919 if (stat)
1920 return stat;
1921
1922 spin_lock_irqsave(shost->host_lock, flags);
1923 match = 0;
1924 /* we only allow support on Channel 0 !!! */
1925 list_for_each_entry(vport, &fc_host->vports, peers) {
1926 if ((vport->channel == 0) &&
1927 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
Gal Rosen0d9dc7c2010-01-21 10:15:32 +02001928 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1929 break;
1930 vport->flags |= FC_VPORT_DELETING;
James Smarta53eb5e2007-04-27 12:41:09 -04001931 match = 1;
1932 break;
1933 }
1934 }
1935 spin_unlock_irqrestore(shost->host_lock, flags);
1936
1937 if (!match)
1938 return -ENODEV;
1939
1940 stat = fc_vport_terminate(vport);
1941 return stat ? stat : count;
1942}
Tony Jonesee959b02008-02-22 00:13:36 +01001943static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04001944 store_fc_host_vport_delete);
1945
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947static int fc_host_match(struct attribute_container *cont,
1948 struct device *dev)
1949{
1950 struct Scsi_Host *shost;
1951 struct fc_internal *i;
1952
1953 if (!scsi_is_host_device(dev))
1954 return 0;
1955
1956 shost = dev_to_shost(dev);
1957 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1958 != &fc_host_class.class)
1959 return 0;
1960
1961 i = to_fc_internal(shost->transportt);
1962
1963 return &i->t.host_attrs.ac == cont;
1964}
1965
1966static int fc_target_match(struct attribute_container *cont,
1967 struct device *dev)
1968{
1969 struct Scsi_Host *shost;
1970 struct fc_internal *i;
1971
1972 if (!scsi_is_target_device(dev))
1973 return 0;
1974
1975 shost = dev_to_shost(dev->parent);
1976 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1977 != &fc_host_class.class)
1978 return 0;
1979
1980 i = to_fc_internal(shost->transportt);
1981
1982 return &i->t.target_attrs.ac == cont;
1983}
1984
1985static void fc_rport_dev_release(struct device *dev)
1986{
1987 struct fc_rport *rport = dev_to_rport(dev);
1988 put_device(dev->parent);
1989 kfree(rport);
1990}
1991
1992int scsi_is_fc_rport(const struct device *dev)
1993{
1994 return dev->release == fc_rport_dev_release;
1995}
1996EXPORT_SYMBOL(scsi_is_fc_rport);
1997
1998static int fc_rport_match(struct attribute_container *cont,
1999 struct device *dev)
2000{
2001 struct Scsi_Host *shost;
2002 struct fc_internal *i;
2003
2004 if (!scsi_is_fc_rport(dev))
2005 return 0;
2006
2007 shost = dev_to_shost(dev->parent);
2008 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2009 != &fc_host_class.class)
2010 return 0;
2011
2012 i = to_fc_internal(shost->transportt);
2013
2014 return &i->rport_attr_cont.ac == cont;
2015}
2016
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002017
James Smarta53eb5e2007-04-27 12:41:09 -04002018static void fc_vport_dev_release(struct device *dev)
2019{
2020 struct fc_vport *vport = dev_to_vport(dev);
2021 put_device(dev->parent); /* release kobj parent */
2022 kfree(vport);
2023}
2024
2025int scsi_is_fc_vport(const struct device *dev)
2026{
2027 return dev->release == fc_vport_dev_release;
2028}
2029EXPORT_SYMBOL(scsi_is_fc_vport);
2030
2031static int fc_vport_match(struct attribute_container *cont,
2032 struct device *dev)
2033{
2034 struct fc_vport *vport;
2035 struct Scsi_Host *shost;
2036 struct fc_internal *i;
2037
2038 if (!scsi_is_fc_vport(dev))
2039 return 0;
2040 vport = dev_to_vport(dev);
2041
2042 shost = vport_to_shost(vport);
2043 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2044 != &fc_host_class.class)
2045 return 0;
2046
2047 i = to_fc_internal(shost->transportt);
2048 return &i->vport_attr_cont.ac == cont;
2049}
2050
2051
James Smartc829c392006-03-13 08:28:57 -05002052/**
2053 * fc_timed_out - FC Transport I/O timeout intercept handler
James Smartc829c392006-03-13 08:28:57 -05002054 * @scmd: The SCSI command which timed out
2055 *
2056 * This routine protects against error handlers getting invoked while a
2057 * rport is in a blocked state, typically due to a temporarily loss of
2058 * connectivity. If the error handlers are allowed to proceed, requests
2059 * to abort i/o, reset the target, etc will likely fail as there is no way
2060 * to communicate with the device to perform the requested function. These
2061 * failures may result in the midlayer taking the device offline, requiring
2062 * manual intervention to restore operation.
2063 *
2064 * This routine, called whenever an i/o times out, validates the state of
2065 * the underlying rport. If the rport is blocked, it returns
2066 * EH_RESET_TIMER, which will continue to reschedule the timeout.
2067 * Eventually, either the device will return, or devloss_tmo will fire,
2068 * and when the timeout then fires, it will be handled normally.
2069 * If the rport is not blocked, normal error handling continues.
2070 *
2071 * Notes:
2072 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002073 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07002074static enum blk_eh_timer_return
James Smartc829c392006-03-13 08:28:57 -05002075fc_timed_out(struct scsi_cmnd *scmd)
2076{
2077 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
2078
2079 if (rport->port_state == FC_PORTSTATE_BLOCKED)
Jens Axboe242f9dc2008-09-14 05:55:09 -07002080 return BLK_EH_RESET_TIMER;
James Smartc829c392006-03-13 08:28:57 -05002081
Jens Axboe242f9dc2008-09-14 05:55:09 -07002082 return BLK_EH_NOT_HANDLED;
James Smartc829c392006-03-13 08:28:57 -05002083}
2084
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002085/*
James Smartbda23252008-04-24 12:12:46 -04002086 * Called by fc_user_scan to locate an rport on the shost that
2087 * matches the channel and target id, and invoke scsi_scan_target()
2088 * on the rport.
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002089 */
James Smartbda23252008-04-24 12:12:46 -04002090static void
2091fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002092{
2093 struct fc_rport *rport;
James Smartbda23252008-04-24 12:12:46 -04002094 unsigned long flags;
2095
2096 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002097
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002098 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2099 if (rport->scsi_target_id == -1)
2100 continue;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002101
Hannes Reinecke0d2fcd92007-06-14 15:16:45 +02002102 if (rport->port_state != FC_PORTSTATE_ONLINE)
2103 continue;
2104
James Smartbda23252008-04-24 12:12:46 -04002105 if ((channel == rport->channel) &&
2106 (id == rport->scsi_target_id)) {
2107 spin_unlock_irqrestore(shost->host_lock, flags);
2108 scsi_scan_target(&rport->dev, channel, id, lun, 1);
2109 return;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002110 }
2111 }
2112
James Smartbda23252008-04-24 12:12:46 -04002113 spin_unlock_irqrestore(shost->host_lock, flags);
2114}
2115
2116/*
2117 * Called via sysfs scan routines. Necessary, as the FC transport
2118 * wants to place all target objects below the rport object. So this
2119 * routine must invoke the scsi_scan_target() routine with the rport
2120 * object as the parent.
2121 */
2122static int
2123fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2124{
2125 uint chlo, chhi;
2126 uint tgtlo, tgthi;
2127
2128 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2129 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2130 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2131 return -EINVAL;
2132
2133 if (channel == SCAN_WILD_CARD) {
2134 chlo = 0;
2135 chhi = shost->max_channel + 1;
2136 } else {
2137 chlo = channel;
2138 chhi = channel + 1;
2139 }
2140
2141 if (id == SCAN_WILD_CARD) {
2142 tgtlo = 0;
2143 tgthi = shost->max_id;
2144 } else {
2145 tgtlo = id;
2146 tgthi = id + 1;
2147 }
2148
2149 for ( ; chlo < chhi; chlo++)
2150 for ( ; tgtlo < tgthi; tgtlo++)
2151 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2152
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002153 return 0;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002154}
2155
FUJITA Tomonori75252362007-09-01 02:02:27 +09002156static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
2157 int result)
2158{
2159 struct fc_internal *i = to_fc_internal(shost->transportt);
2160 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
2161}
2162
2163static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
2164{
2165 struct fc_internal *i = to_fc_internal(shost->transportt);
2166 return i->f->it_nexus_response(shost, nexus, result);
2167}
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169struct scsi_transport_template *
2170fc_attach_transport(struct fc_function_template *ft)
2171{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 int count;
Jes Sorensen24669f752006-01-16 10:31:18 -05002173 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2174 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 if (unlikely(!i))
2177 return NULL;
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2180 i->t.target_attrs.ac.class = &fc_transport_class.class;
2181 i->t.target_attrs.ac.match = fc_target_match;
2182 i->t.target_size = sizeof(struct fc_starget_attrs);
2183 transport_container_register(&i->t.target_attrs);
2184
2185 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2186 i->t.host_attrs.ac.class = &fc_host_class.class;
2187 i->t.host_attrs.ac.match = fc_host_match;
2188 i->t.host_size = sizeof(struct fc_host_attrs);
2189 if (ft->get_fc_host_stats)
2190 i->t.host_attrs.statistics = &fc_statistics_group;
2191 transport_container_register(&i->t.host_attrs);
2192
2193 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2194 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2195 i->rport_attr_cont.ac.match = fc_rport_match;
2196 transport_container_register(&i->rport_attr_cont);
2197
James Smarta53eb5e2007-04-27 12:41:09 -04002198 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2199 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2200 i->vport_attr_cont.ac.match = fc_vport_match;
2201 transport_container_register(&i->vport_attr_cont);
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 i->f = ft;
2204
2205 /* Transport uses the shost workq for scsi scanning */
2206 i->t.create_work_queue = 1;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002207
James Smartc829c392006-03-13 08:28:57 -05002208 i->t.eh_timed_out = fc_timed_out;
2209
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002210 i->t.user_scan = fc_user_scan;
James Smart9ef3e4a2007-05-24 19:04:44 -04002211
FUJITA Tomonori75252362007-09-01 02:02:27 +09002212 /* target-mode drivers' functions */
2213 i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2214 i->t.it_nexus_response = fc_it_nexus_response;
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /*
2217 * Setup SCSI Target Attributes.
2218 */
2219 count = 0;
2220 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2221 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2222 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2223
2224 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2225
2226 i->starget_attrs[count] = NULL;
2227
2228
2229 /*
2230 * Setup SCSI Host Attributes.
2231 */
2232 count=0;
2233 SETUP_HOST_ATTRIBUTE_RD(node_name);
2234 SETUP_HOST_ATTRIBUTE_RD(port_name);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01002235 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2237 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2239 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
James Smarta53eb5e2007-04-27 12:41:09 -04002240 if (ft->vport_create) {
2241 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2242 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 SETUP_HOST_ATTRIBUTE_RD(serial_number);
Neerav Parikhbb8ef582012-01-22 17:29:50 -08002245 SETUP_HOST_ATTRIBUTE_RD(manufacturer);
2246 SETUP_HOST_ATTRIBUTE_RD(model);
2247 SETUP_HOST_ATTRIBUTE_RD(model_description);
2248 SETUP_HOST_ATTRIBUTE_RD(hardware_version);
2249 SETUP_HOST_ATTRIBUTE_RD(driver_version);
2250 SETUP_HOST_ATTRIBUTE_RD(firmware_version);
2251 SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 SETUP_HOST_ATTRIBUTE_RD(port_id);
2254 SETUP_HOST_ATTRIBUTE_RD(port_type);
2255 SETUP_HOST_ATTRIBUTE_RD(port_state);
2256 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2257 SETUP_HOST_ATTRIBUTE_RD(speed);
2258 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
James Smart016131b2006-08-14 08:20:25 -04002259 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
James Smartb8d08212006-08-17 08:00:43 -04002260 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 /* Transport-managed attributes */
Mike Christie43ca9102010-09-15 16:52:32 -05002263 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002265 if (ft->issue_fc_host_lip)
2266 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
James Smarta53eb5e2007-04-27 12:41:09 -04002267 if (ft->vport_create)
2268 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2269 if (ft->vport_delete)
2270 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 BUG_ON(count > FC_HOST_NUM_ATTRS);
2273
2274 i->host_attrs[count] = NULL;
2275
2276 /*
2277 * Setup Remote Port Attributes.
2278 */
2279 count=0;
2280 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2281 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2282 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2283 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2284 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2285 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2286 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2287 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2288 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
Mike Christiefff9d402008-08-19 18:45:23 -05002289 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2292
2293 i->rport_attrs[count] = NULL;
2294
James Smarta53eb5e2007-04-27 12:41:09 -04002295 /*
2296 * Setup Virtual Port Attributes.
2297 */
2298 count=0;
2299 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2300 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2301 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2302 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2303 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2304 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2305 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2306 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2307 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2308
2309 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2310
2311 i->vport_attrs[count] = NULL;
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return &i->t;
2314}
2315EXPORT_SYMBOL(fc_attach_transport);
2316
2317void fc_release_transport(struct scsi_transport_template *t)
2318{
2319 struct fc_internal *i = to_fc_internal(t);
2320
2321 transport_container_unregister(&i->t.target_attrs);
2322 transport_container_unregister(&i->t.host_attrs);
2323 transport_container_unregister(&i->rport_attr_cont);
James Smarta53eb5e2007-04-27 12:41:09 -04002324 transport_container_unregister(&i->vport_attr_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 kfree(i);
2327}
2328EXPORT_SYMBOL(fc_release_transport);
2329
James Smartaedf3492006-04-10 10:14:05 -04002330/**
2331 * fc_queue_work - Queue work to the fc_host workqueue.
2332 * @shost: Pointer to Scsi_Host bound to fc_host.
2333 * @work: Work to queue for execution.
2334 *
2335 * Return value:
James Smarta0785ed2006-05-11 13:27:09 -04002336 * 1 - work queued for execution
2337 * 0 - work is already queued
2338 * -EINVAL - work queue doesn't exist
Rob Landleyeb448202007-11-03 13:30:39 -05002339 */
James Smartaedf3492006-04-10 10:14:05 -04002340static int
2341fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2342{
2343 if (unlikely(!fc_host_work_q(shost))) {
2344 printk(KERN_ERR
2345 "ERROR: FC host '%s' attempted to queue work, "
2346 "when no workqueue created.\n", shost->hostt->name);
2347 dump_stack();
2348
2349 return -EINVAL;
2350 }
2351
2352 return queue_work(fc_host_work_q(shost), work);
2353}
2354
2355/**
2356 * fc_flush_work - Flush a fc_host's workqueue.
2357 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002358 */
James Smartaedf3492006-04-10 10:14:05 -04002359static void
2360fc_flush_work(struct Scsi_Host *shost)
2361{
2362 if (!fc_host_work_q(shost)) {
2363 printk(KERN_ERR
2364 "ERROR: FC host '%s' attempted to flush work, "
2365 "when no workqueue created.\n", shost->hostt->name);
2366 dump_stack();
2367 return;
2368 }
2369
2370 flush_workqueue(fc_host_work_q(shost));
2371}
2372
2373/**
2374 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2375 * @shost: Pointer to Scsi_Host bound to fc_host.
2376 * @work: Work to queue for execution.
2377 * @delay: jiffies to delay the work queuing
2378 *
2379 * Return value:
James Smart0f29b962006-08-18 17:33:29 -04002380 * 1 on success / 0 already queued / < 0 for error
Rob Landleyeb448202007-11-03 13:30:39 -05002381 */
James Smartaedf3492006-04-10 10:14:05 -04002382static int
David Howellsc4028952006-11-22 14:57:56 +00002383fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
James Smartaedf3492006-04-10 10:14:05 -04002384 unsigned long delay)
2385{
2386 if (unlikely(!fc_host_devloss_work_q(shost))) {
2387 printk(KERN_ERR
2388 "ERROR: FC host '%s' attempted to queue work, "
2389 "when no workqueue created.\n", shost->hostt->name);
2390 dump_stack();
2391
2392 return -EINVAL;
2393 }
2394
2395 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2396}
2397
2398/**
2399 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2400 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002401 */
James Smartaedf3492006-04-10 10:14:05 -04002402static void
2403fc_flush_devloss(struct Scsi_Host *shost)
2404{
2405 if (!fc_host_devloss_work_q(shost)) {
2406 printk(KERN_ERR
2407 "ERROR: FC host '%s' attempted to flush work, "
2408 "when no workqueue created.\n", shost->hostt->name);
2409 dump_stack();
2410 return;
2411 }
2412
2413 flush_workqueue(fc_host_devloss_work_q(shost));
2414}
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417/**
Rob Landleyeb448202007-11-03 13:30:39 -05002418 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2419 * @shost: Which &Scsi_Host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002421 * This routine is expected to be called immediately preceding the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 * a driver's call to scsi_remove_host().
2423 *
2424 * WARNING: A driver utilizing the fc_transport, which fails to call
Rob Landleyeb448202007-11-03 13:30:39 -05002425 * this routine prior to scsi_remove_host(), will leave dangling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 * objects in /sys/class/fc_remote_ports. Access to any of these
2427 * objects can result in a system crash !!!
2428 *
2429 * Notes:
2430 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002431 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432void
2433fc_remove_host(struct Scsi_Host *shost)
2434{
James Smarta53eb5e2007-04-27 12:41:09 -04002435 struct fc_vport *vport = NULL, *next_vport = NULL;
2436 struct fc_rport *rport = NULL, *next_rport = NULL;
James Smartaedf3492006-04-10 10:14:05 -04002437 struct workqueue_struct *work_q;
2438 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James Smarta53eb5e2007-04-27 12:41:09 -04002439 unsigned long flags;
James Smarta53eb5e2007-04-27 12:41:09 -04002440
2441 spin_lock_irqsave(shost->host_lock, flags);
2442
2443 /* Remove any vports */
James Smart9ef3e4a2007-05-24 19:04:44 -04002444 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2445 fc_queue_work(shost, &vport->vport_delete_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 /* Remove any remote ports */
2448 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002449 &fc_host->rports, peers) {
2450 list_del(&rport->peers);
2451 rport->port_state = FC_PORTSTATE_DELETED;
2452 fc_queue_work(shost, &rport->rport_delete_work);
2453 }
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002456 &fc_host->rport_bindings, peers) {
2457 list_del(&rport->peers);
2458 rport->port_state = FC_PORTSTATE_DELETED;
2459 fc_queue_work(shost, &rport->rport_delete_work);
2460 }
2461
James Smarta53eb5e2007-04-27 12:41:09 -04002462 spin_unlock_irqrestore(shost->host_lock, flags);
2463
James Smartaedf3492006-04-10 10:14:05 -04002464 /* flush all scan work items */
2465 scsi_flush_work(shost);
2466
2467 /* flush all stgt delete, and rport delete work items, then kill it */
2468 if (fc_host->work_q) {
2469 work_q = fc_host->work_q;
2470 fc_host->work_q = NULL;
2471 destroy_workqueue(work_q);
2472 }
2473
2474 /* flush all devloss work items, then kill it */
2475 if (fc_host->devloss_work_q) {
2476 work_q = fc_host->devloss_work_q;
2477 fc_host->devloss_work_q = NULL;
2478 destroy_workqueue(work_q);
2479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481EXPORT_SYMBOL(fc_remove_host);
2482
Mike Christiefff9d402008-08-19 18:45:23 -05002483static void fc_terminate_rport_io(struct fc_rport *rport)
2484{
2485 struct Scsi_Host *shost = rport_to_shost(rport);
2486 struct fc_internal *i = to_fc_internal(shost->transportt);
2487
2488 /* Involve the LLDD if possible to terminate all io on the rport. */
2489 if (i->f->terminate_rport_io)
2490 i->f->terminate_rport_io(rport);
2491
2492 /*
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002493 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
Mike Christiefff9d402008-08-19 18:45:23 -05002494 */
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002495 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
Mike Christiefff9d402008-08-19 18:45:23 -05002496}
James Smartaedf3492006-04-10 10:14:05 -04002497
2498/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002499 * fc_starget_delete - called to delete the scsi descendants of an rport
David Howellsc4028952006-11-22 14:57:56 +00002500 * @work: remote port to be operated on.
Rob Landleyeb448202007-11-03 13:30:39 -05002501 *
2502 * Deletes target and all sdevs.
2503 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002504static void
David Howellsc4028952006-11-22 14:57:56 +00002505fc_starget_delete(struct work_struct *work)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002506{
David Howellsc4028952006-11-22 14:57:56 +00002507 struct fc_rport *rport =
2508 container_of(work, struct fc_rport, stgt_delete_work);
James Smart0f29b962006-08-18 17:33:29 -04002509
Mike Christiefff9d402008-08-19 18:45:23 -05002510 fc_terminate_rport_io(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002511 scsi_remove_target(&rport->dev);
2512}
2513
James Smartaedf3492006-04-10 10:14:05 -04002514
2515/**
2516 * fc_rport_final_delete - finish rport termination and delete it.
David Howellsc4028952006-11-22 14:57:56 +00002517 * @work: remote port to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05002518 */
James Smartaedf3492006-04-10 10:14:05 -04002519static void
David Howellsc4028952006-11-22 14:57:56 +00002520fc_rport_final_delete(struct work_struct *work)
James Smartaedf3492006-04-10 10:14:05 -04002521{
David Howellsc4028952006-11-22 14:57:56 +00002522 struct fc_rport *rport =
2523 container_of(work, struct fc_rport, rport_delete_work);
James Smartaedf3492006-04-10 10:14:05 -04002524 struct device *dev = &rport->dev;
2525 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002526 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart92740b22007-04-27 11:53:17 -04002527 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05002528 int do_callback = 0;
James Smartaedf3492006-04-10 10:14:05 -04002529
Nithin Nayak Sujir112f6612011-04-25 12:30:06 -07002530 fc_terminate_rport_io(rport);
2531
James Smartaedf3492006-04-10 10:14:05 -04002532 /*
James Smart9ef3e4a2007-05-24 19:04:44 -04002533 * if a scan is pending, flush the SCSI Host work_q so that
James Smartaedf3492006-04-10 10:14:05 -04002534 * that we can reclaim the rport scan work element.
2535 */
2536 if (rport->flags & FC_RPORT_SCAN_PENDING)
2537 scsi_flush_work(shost);
2538
James Smart92740b22007-04-27 11:53:17 -04002539 /*
2540 * Cancel any outstanding timers. These should really exist
2541 * only when rmmod'ing the LLDD and we're asking for
2542 * immediate termination of the rports
2543 */
2544 spin_lock_irqsave(shost->host_lock, flags);
2545 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2546 spin_unlock_irqrestore(shost->host_lock, flags);
2547 if (!cancel_delayed_work(&rport->fail_io_work))
2548 fc_flush_devloss(shost);
2549 if (!cancel_delayed_work(&rport->dev_loss_work))
2550 fc_flush_devloss(shost);
2551 spin_lock_irqsave(shost->host_lock, flags);
2552 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2553 }
2554 spin_unlock_irqrestore(shost->host_lock, flags);
2555
James Smart0f29b962006-08-18 17:33:29 -04002556 /* Delete SCSI target and sdevs */
2557 if (rport->scsi_target_id != -1)
David Howellsc4028952006-11-22 14:57:56 +00002558 fc_starget_delete(&rport->stgt_delete_work);
James Smart92740b22007-04-27 11:53:17 -04002559
2560 /*
2561 * Notify the driver that the rport is now dead. The LLDD will
2562 * also guarantee that any communication to the rport is terminated
James Smart4be98c02009-01-05 12:14:18 -05002563 *
2564 * Avoid this call if we already called it when we preserved the
2565 * rport for the binding.
James Smart92740b22007-04-27 11:53:17 -04002566 */
Michael Reed8798a692009-10-09 14:15:59 -05002567 spin_lock_irqsave(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05002568 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
Michael Reed8798a692009-10-09 14:15:59 -05002569 (i->f->dev_loss_tmo_callbk)) {
2570 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2571 do_callback = 1;
2572 }
2573 spin_unlock_irqrestore(shost->host_lock, flags);
2574
2575 if (do_callback)
James Smart0f29b962006-08-18 17:33:29 -04002576 i->f->dev_loss_tmo_callbk(rport);
James Smart0f29b962006-08-18 17:33:29 -04002577
James Smart9e4f5e22009-03-26 13:33:19 -04002578 fc_bsg_remove(rport->rqst_q);
2579
James Smartaedf3492006-04-10 10:14:05 -04002580 transport_remove_device(dev);
2581 device_del(dev);
2582 transport_destroy_device(dev);
James Smart3bdad7b2006-06-26 14:19:59 -04002583 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2584 put_device(dev); /* for self-reference */
James Smartaedf3492006-04-10 10:14:05 -04002585}
2586
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588/**
2589 * fc_rport_create - allocates and creates a remote FC port.
2590 * @shost: scsi host the remote port is connected to.
2591 * @channel: Channel on shost port connected to.
2592 * @ids: The world wide names, fc address, and FC4 port
2593 * roles for the remote port.
2594 *
2595 * Allocates and creates the remoter port structure, including the
2596 * class and sysfs creation.
2597 *
2598 * Notes:
2599 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002600 */
Adrian Bunk44818ef2007-07-09 11:59:59 -07002601static struct fc_rport *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602fc_rport_create(struct Scsi_Host *shost, int channel,
2603 struct fc_rport_identifiers *ids)
2604{
James Smartaedf3492006-04-10 10:14:05 -04002605 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct fc_internal *fci = to_fc_internal(shost->transportt);
2607 struct fc_rport *rport;
2608 struct device *dev;
2609 unsigned long flags;
2610 int error;
2611 size_t size;
2612
2613 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
Jes Sorensen24669f752006-01-16 10:31:18 -05002614 rport = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (unlikely(!rport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002616 printk(KERN_ERR "%s: allocation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return NULL;
2618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620 rport->maxframe_size = -1;
2621 rport->supported_classes = FC_COS_UNSPECIFIED;
Mike Christie43ca9102010-09-15 16:52:32 -05002622 rport->dev_loss_tmo = fc_host->dev_loss_tmo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2624 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2625 rport->port_id = ids->port_id;
2626 rport->roles = ids->roles;
2627 rport->port_state = FC_PORTSTATE_ONLINE;
2628 if (fci->f->dd_fcrport_size)
2629 rport->dd_data = &rport[1];
2630 rport->channel = channel;
James Smart0f29b962006-08-18 17:33:29 -04002631 rport->fast_io_fail_tmo = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
David Howellsc4028952006-11-22 14:57:56 +00002633 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2634 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2635 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2636 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2637 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 spin_lock_irqsave(shost->host_lock, flags);
2640
2641 rport->number = fc_host->next_rport_number++;
James Smarta53eb5e2007-04-27 12:41:09 -04002642 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 rport->scsi_target_id = fc_host->next_target_id++;
2644 else
2645 rport->scsi_target_id = -1;
James Smartaedf3492006-04-10 10:14:05 -04002646 list_add_tail(&rport->peers, &fc_host->rports);
James Smart3bdad7b2006-06-26 14:19:59 -04002647 get_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 spin_unlock_irqrestore(shost->host_lock, flags);
2650
2651 dev = &rport->dev;
James Smart3bdad7b2006-06-26 14:19:59 -04002652 device_initialize(dev); /* takes self reference */
2653 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 dev->release = fc_rport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01002655 dev_set_name(dev, "rport-%d:%d-%d",
2656 shost->host_no, channel, rport->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 transport_setup_device(dev);
2658
2659 error = device_add(dev);
2660 if (error) {
2661 printk(KERN_ERR "FC Remote Port device_add failed\n");
2662 goto delete_rport;
2663 }
2664 transport_add_device(dev);
2665 transport_configure_device(dev);
2666
James Smart9e4f5e22009-03-26 13:33:19 -04002667 fc_bsg_rportadd(shost, rport);
2668 /* ignore any bsg add error - we just can't do sgio */
2669
James Smarta53eb5e2007-04-27 12:41:09 -04002670 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04002672 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04002674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676 return rport;
2677
2678delete_rport:
2679 transport_destroy_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 spin_lock_irqsave(shost->host_lock, flags);
2681 list_del(&rport->peers);
James Smart3bdad7b2006-06-26 14:19:59 -04002682 put_device(&shost->shost_gendev); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 spin_unlock_irqrestore(shost->host_lock, flags);
2684 put_device(dev->parent);
2685 kfree(rport);
2686 return NULL;
2687}
2688
2689/**
Rob Landleyeb448202007-11-03 13:30:39 -05002690 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 * @shost: scsi host the remote port is connected to.
2692 * @channel: Channel on shost port connected to.
2693 * @ids: The world wide names, fc address, and FC4 port
2694 * roles for the remote port.
2695 *
2696 * The LLDD calls this routine to notify the transport of the existence
2697 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2698 * of the port, it's FC address (port_id), and the FC4 roles that are
2699 * active for the port.
2700 *
2701 * For ports that are FCP targets (aka scsi targets), the FC transport
2702 * maintains consistent target id bindings on behalf of the LLDD.
2703 * A consistent target id binding is an assignment of a target id to
2704 * a remote port identifier, which persists while the scsi host is
2705 * attached. The remote port can disappear, then later reappear, and
2706 * it's target id assignment remains the same. This allows for shifts
2707 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2708 * changes to the scsi subsystem which is based on scsi host number and
2709 * target id values. Bindings are only valid during the attachment of
2710 * the scsi host. If the host detaches, then later re-attaches, target
2711 * id bindings may change.
2712 *
2713 * This routine is responsible for returning a remote port structure.
2714 * The routine will search the list of remote ports it maintains
2715 * internally on behalf of consistent target id mappings. If found, the
2716 * remote port structure will be reused. Otherwise, a new remote port
2717 * structure will be allocated.
2718 *
2719 * Whenever a remote port is allocated, a new fc_remote_port class
2720 * device is created.
2721 *
2722 * Should not be called from interrupt context.
2723 *
2724 * Notes:
2725 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727struct fc_rport *
2728fc_remote_port_add(struct Scsi_Host *shost, int channel,
2729 struct fc_rport_identifiers *ids)
2730{
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002731 struct fc_internal *fci = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04002732 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 struct fc_rport *rport;
2734 unsigned long flags;
2735 int match = 0;
2736
James Smartaedf3492006-04-10 10:14:05 -04002737 /* ensure any stgt delete functions are done */
2738 fc_flush_work(shost);
2739
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002740 /*
2741 * Search the list of "active" rports, for an rport that has been
2742 * deleted, but we've held off the real delete while the target
2743 * is in a "blocked" state.
2744 */
2745 spin_lock_irqsave(shost->host_lock, flags);
2746
James Smartaedf3492006-04-10 10:14:05 -04002747 list_for_each_entry(rport, &fc_host->rports, peers) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002748
2749 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2750 (rport->channel == channel)) {
2751
James Smartaedf3492006-04-10 10:14:05 -04002752 switch (fc_host->tgtid_bind_type) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002753 case FC_TGTID_BIND_BY_WWPN:
2754 case FC_TGTID_BIND_NONE:
2755 if (rport->port_name == ids->port_name)
2756 match = 1;
2757 break;
2758 case FC_TGTID_BIND_BY_WWNN:
2759 if (rport->node_name == ids->node_name)
2760 match = 1;
2761 break;
2762 case FC_TGTID_BIND_BY_ID:
2763 if (rport->port_id == ids->port_id)
2764 match = 1;
2765 break;
2766 }
2767
2768 if (match) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002769
2770 memcpy(&rport->node_name, &ids->node_name,
2771 sizeof(rport->node_name));
2772 memcpy(&rport->port_name, &ids->port_name,
2773 sizeof(rport->port_name));
2774 rport->port_id = ids->port_id;
2775
2776 rport->port_state = FC_PORTSTATE_ONLINE;
2777 rport->roles = ids->roles;
2778
2779 spin_unlock_irqrestore(shost->host_lock, flags);
2780
2781 if (fci->f->dd_fcrport_size)
2782 memset(rport->dd_data, 0,
2783 fci->f->dd_fcrport_size);
2784
2785 /*
James Smart92740b22007-04-27 11:53:17 -04002786 * If we were not a target, cancel the
2787 * io terminate and rport timers, and
2788 * we're done.
2789 *
2790 * If we were a target, but our new role
2791 * doesn't indicate a target, leave the
2792 * timers running expecting the role to
2793 * change as the target fully logs in. If
2794 * it doesn't, the target will be torn down.
2795 *
2796 * If we were a target, and our role shows
2797 * we're still a target, cancel the timers
2798 * and kick off a scan.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002799 */
James Smart92740b22007-04-27 11:53:17 -04002800
2801 /* was a target, not in roles */
2802 if ((rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04002803 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002804 return rport;
2805
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002806 /*
James Smart92740b22007-04-27 11:53:17 -04002807 * Stop the fail io and dev_loss timers.
2808 * If they flush, the port_state will
2809 * be checked and will NOOP the function.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002810 */
James Smart0f29b962006-08-18 17:33:29 -04002811 if (!cancel_delayed_work(&rport->fail_io_work))
2812 fc_flush_devloss(shost);
James Smart92740b22007-04-27 11:53:17 -04002813 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04002814 fc_flush_devloss(shost);
2815
2816 spin_lock_irqsave(shost->host_lock, flags);
2817
Mike Christiefff9d402008-08-19 18:45:23 -05002818 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
James Smart4be98c02009-01-05 12:14:18 -05002819 FC_RPORT_DEVLOSS_PENDING |
2820 FC_RPORT_DEVLOSS_CALLBK_DONE);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002821
Mike Christie8fb2ef82012-04-15 21:47:50 -05002822 spin_unlock_irqrestore(shost->host_lock, flags);
2823
James Smart92740b22007-04-27 11:53:17 -04002824 /* if target, initiate a scan */
2825 if (rport->scsi_target_id != -1) {
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002826 scsi_target_unblock(&rport->dev,
2827 SDEV_RUNNING);
Mike Christie8fb2ef82012-04-15 21:47:50 -05002828 spin_lock_irqsave(shost->host_lock,
2829 flags);
James Smart92740b22007-04-27 11:53:17 -04002830 rport->flags |= FC_RPORT_SCAN_PENDING;
2831 scsi_queue_work(shost,
2832 &rport->scan_work);
2833 spin_unlock_irqrestore(shost->host_lock,
2834 flags);
Mike Christie8fb2ef82012-04-15 21:47:50 -05002835 }
James Smarta0785ed2006-05-11 13:27:09 -04002836
James Smart9e4f5e22009-03-26 13:33:19 -04002837 fc_bsg_goose_queue(rport);
2838
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002839 return rport;
2840 }
2841 }
2842 }
2843
James Smart92740b22007-04-27 11:53:17 -04002844 /*
2845 * Search the bindings array
2846 * Note: if never a FCP target, you won't be on this list
2847 */
James Smartaedf3492006-04-10 10:14:05 -04002848 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 /* search for a matching consistent binding */
2851
James Smartaedf3492006-04-10 10:14:05 -04002852 list_for_each_entry(rport, &fc_host->rport_bindings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 peers) {
2854 if (rport->channel != channel)
2855 continue;
2856
James Smartaedf3492006-04-10 10:14:05 -04002857 switch (fc_host->tgtid_bind_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 case FC_TGTID_BIND_BY_WWPN:
2859 if (rport->port_name == ids->port_name)
2860 match = 1;
2861 break;
2862 case FC_TGTID_BIND_BY_WWNN:
2863 if (rport->node_name == ids->node_name)
2864 match = 1;
2865 break;
2866 case FC_TGTID_BIND_BY_ID:
2867 if (rport->port_id == ids->port_id)
2868 match = 1;
2869 break;
2870 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2871 break;
2872 }
2873
2874 if (match) {
James Smartaedf3492006-04-10 10:14:05 -04002875 list_move_tail(&rport->peers, &fc_host->rports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 break;
2877 }
2878 }
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (match) {
2881 memcpy(&rport->node_name, &ids->node_name,
2882 sizeof(rport->node_name));
2883 memcpy(&rport->port_name, &ids->port_name,
2884 sizeof(rport->port_name));
2885 rport->port_id = ids->port_id;
2886 rport->roles = ids->roles;
2887 rport->port_state = FC_PORTSTATE_ONLINE;
Mike Christiefff9d402008-08-19 18:45:23 -05002888 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002890 if (fci->f->dd_fcrport_size)
2891 memset(rport->dd_data, 0,
2892 fci->f->dd_fcrport_size);
Mike Christie8fb2ef82012-04-15 21:47:50 -05002893 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002894
Mike Christie8fb2ef82012-04-15 21:47:50 -05002895 if (ids->roles & FC_PORT_ROLE_FCP_TARGET) {
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002896 scsi_target_unblock(&rport->dev, SDEV_RUNNING);
Mike Christie8fb2ef82012-04-15 21:47:50 -05002897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 /* initiate a scan of the target */
Mike Christie8fb2ef82012-04-15 21:47:50 -05002899 spin_lock_irqsave(shost->host_lock, flags);
James Smartaedf3492006-04-10 10:14:05 -04002900 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 scsi_queue_work(shost, &rport->scan_work);
James Smarta0785ed2006-05-11 13:27:09 -04002902 spin_unlock_irqrestore(shost->host_lock, flags);
Mike Christie8fb2ef82012-04-15 21:47:50 -05002903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return rport;
2905 }
2906 }
2907
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002908 spin_unlock_irqrestore(shost->host_lock, flags);
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 /* No consistent binding found - create new remote port entry */
2911 rport = fc_rport_create(shost, channel, ids);
2912
2913 return rport;
2914}
2915EXPORT_SYMBOL(fc_remote_port_add);
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918/**
Rob Landleyeb448202007-11-03 13:30:39 -05002919 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 * @rport: The remote port that no longer exists
2921 *
2922 * The LLDD calls this routine to notify the transport that a remote
2923 * port is no longer part of the topology. Note: Although a port
2924 * may no longer be part of the topology, it may persist in the remote
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002925 * ports displayed by the fc_host. We do this under 2 conditions:
Rob Landleyeb448202007-11-03 13:30:39 -05002926 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002927 * This allows the port to temporarily disappear, then reappear without
2928 * disrupting the SCSI device tree attached to it. During the "blocked"
2929 * period the port will still exist.
Rob Landleyeb448202007-11-03 13:30:39 -05002930 * 2) If the port was a scsi target and disappears for longer than we
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002931 * expect, we'll delete the port and the tear down the SCSI device tree
2932 * attached to it. However, we want to semi-persist the target id assigned
2933 * to that port if it eventually does exist. The port structure will
2934 * remain (although with minimal information) so that the target id
2935 * bindings remails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 *
2937 * If the remote port is not an FCP Target, it will be fully torn down
2938 * and deallocated, including the fc_remote_port class device.
2939 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002940 * If the remote port is an FCP Target, the port will be placed in a
2941 * temporary blocked state. From the LLDD's perspective, the rport no
2942 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2943 * exists, but all sdevs on it are blocked from further I/O. The following
Rob Landleyeb448202007-11-03 13:30:39 -05002944 * is then expected.
2945 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002946 * If the remote port does not return (signaled by a LLDD call to
2947 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2948 * scsi target is removed - killing all outstanding i/o and removing the
2949 * scsi devices attached ot it. The port structure will be marked Not
2950 * Present and be partially cleared, leaving only enough information to
2951 * recognize the remote port relative to the scsi target id binding if
2952 * it later appears. The port will remain as long as there is a valid
2953 * binding (e.g. until the user changes the binding type or unloads the
2954 * scsi host with the binding).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002956 * If the remote port returns within the dev_loss_tmo value (and matches
2957 * according to the target id binding type), the port structure will be
2958 * reused. If it is no longer a SCSI target, the target will be torn
2959 * down. If it continues to be a SCSI target, then the target will be
2960 * unblocked (allowing i/o to be resumed), and a scan will be activated
2961 * to ensure that all luns are detected.
2962 *
2963 * Called from normal process context only - cannot be called from interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 *
2965 * Notes:
2966 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002967 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968void
2969fc_remote_port_delete(struct fc_rport *rport)
2970{
James Smartaedf3492006-04-10 10:14:05 -04002971 struct Scsi_Host *shost = rport_to_shost(rport);
Hannes Reinecke36dd2882010-03-09 10:18:48 +01002972 unsigned long timeout = rport->dev_loss_tmo;
James Smartaedf3492006-04-10 10:14:05 -04002973 unsigned long flags;
2974
2975 /*
2976 * No need to flush the fc_host work_q's, as all adds are synchronous.
2977 *
2978 * We do need to reclaim the rport scan work element, so eventually
2979 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2980 * there's still a scan pending.
2981 */
2982
2983 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
James Smart92740b22007-04-27 11:53:17 -04002985 if (rport->port_state != FC_PORTSTATE_ONLINE) {
James Smartaedf3492006-04-10 10:14:05 -04002986 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return;
2988 }
2989
James Smart92740b22007-04-27 11:53:17 -04002990 /*
2991 * In the past, we if this was not an FCP-Target, we would
2992 * unconditionally just jump to deleting the rport.
2993 * However, rports can be used as node containers by the LLDD,
2994 * and its not appropriate to just terminate the rport at the
2995 * first sign of a loss in connectivity. The LLDD may want to
2996 * send ELS traffic to re-validate the login. If the rport is
2997 * immediately deleted, it makes it inappropriate for a node
2998 * container.
2999 * So... we now unconditionally wait dev_loss_tmo before
3000 * destroying an rport.
3001 */
3002
James Smartaedf3492006-04-10 10:14:05 -04003003 rport->port_state = FC_PORTSTATE_BLOCKED;
3004
3005 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
3006
3007 spin_unlock_irqrestore(shost->host_lock, flags);
3008
FUJITA Tomonori75252362007-09-01 02:02:27 +09003009 if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
3010 shost->active_mode & MODE_TARGET)
3011 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
3012
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003013 scsi_target_block(&rport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
James Smart0f29b962006-08-18 17:33:29 -04003015 /* see if we need to kill io faster than waiting for device loss */
3016 if ((rport->fast_io_fail_tmo != -1) &&
Mike Christiefff9d402008-08-19 18:45:23 -05003017 (rport->fast_io_fail_tmo < timeout))
James Smart0f29b962006-08-18 17:33:29 -04003018 fc_queue_devloss_work(shost, &rport->fail_io_work,
3019 rport->fast_io_fail_tmo * HZ);
3020
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003021 /* cap the length the devices can be blocked until they are deleted */
James Smartaedf3492006-04-10 10:14:05 -04003022 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024EXPORT_SYMBOL(fc_remote_port_delete);
3025
3026/**
Rob Landleyeb448202007-11-03 13:30:39 -05003027 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 * @rport: The remote port that changed.
Rob Landleyeb448202007-11-03 13:30:39 -05003029 * @roles: New roles for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 *
Rob Landleyeb448202007-11-03 13:30:39 -05003031 * Description: The LLDD calls this routine to notify the transport that the
3032 * roles on a remote port may have changed. The largest effect of this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 * if a port now becomes a FCP Target, it must be allocated a
3034 * scsi target id. If the port is no longer a FCP target, any
3035 * scsi target id value assigned to it will persist in case the
3036 * role changes back to include FCP Target. No changes in the scsi
3037 * midlayer will be invoked if the role changes (in the expectation
3038 * that the role will be resumed. If it doesn't normal error processing
3039 * will take place).
3040 *
3041 * Should not be called from interrupt context.
3042 *
3043 * Notes:
3044 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046void
3047fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
3048{
3049 struct Scsi_Host *shost = rport_to_shost(rport);
James Smartaedf3492006-04-10 10:14:05 -04003050 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 unsigned long flags;
3052 int create = 0;
FUJITA Tomonori75252362007-09-01 02:02:27 +09003053 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 spin_lock_irqsave(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04003056 if (roles & FC_PORT_ROLE_FCP_TARGET) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003057 if (rport->scsi_target_id == -1) {
3058 rport->scsi_target_id = fc_host->next_target_id++;
3059 create = 1;
James Smarta53eb5e2007-04-27 12:41:09 -04003060 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003061 create = 1;
FUJITA Tomonori75252362007-09-01 02:02:27 +09003062 } else if (shost->active_mode & MODE_TARGET) {
3063 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
3064 (char *)&rport->node_name);
3065 if (ret)
3066 printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
3067 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003070 rport->roles = roles;
3071
James Smartaedf3492006-04-10 10:14:05 -04003072 spin_unlock_irqrestore(shost->host_lock, flags);
3073
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003074 if (create) {
3075 /*
3076 * There may have been a delete timer running on the
3077 * port. Ensure that it is cancelled as we now know
3078 * the port is an FCP Target.
3079 * Note: we know the rport is exists and in an online
3080 * state as the LLDD would not have had an rport
3081 * reference to pass us.
3082 *
3083 * Take no action on the del_timer failure as the state
3084 * machine state change will validate the
3085 * transaction.
3086 */
James Smart0f29b962006-08-18 17:33:29 -04003087 if (!cancel_delayed_work(&rport->fail_io_work))
3088 fc_flush_devloss(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003089 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04003090 fc_flush_devloss(shost);
3091
3092 spin_lock_irqsave(shost->host_lock, flags);
Mike Christiefff9d402008-08-19 18:45:23 -05003093 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
James Smart0eecee42011-11-03 11:10:40 -04003094 FC_RPORT_DEVLOSS_PENDING |
3095 FC_RPORT_DEVLOSS_CALLBK_DONE);
James Smartaedf3492006-04-10 10:14:05 -04003096 spin_unlock_irqrestore(shost->host_lock, flags);
3097
3098 /* ensure any stgt delete functions are done */
3099 fc_flush_work(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003100
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003101 scsi_target_unblock(&rport->dev, SDEV_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04003103 spin_lock_irqsave(shost->host_lock, flags);
3104 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003106 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109EXPORT_SYMBOL(fc_remote_port_rolechg);
3110
3111/**
Rob Landleyeb448202007-11-03 13:30:39 -05003112 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
James Smart92740b22007-04-27 11:53:17 -04003113 * @work: rport target that failed to reappear in the allotted time.
Rob Landleyeb448202007-11-03 13:30:39 -05003114 *
3115 * Description: An attempt to delete a remote port blocks, and if it fails
3116 * to return in the allotted time this gets called.
3117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118static void
David Howellsc4028952006-11-22 14:57:56 +00003119fc_timeout_deleted_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
David Howellsc4028952006-11-22 14:57:56 +00003121 struct fc_rport *rport =
3122 container_of(work, struct fc_rport, dev_loss_work.work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003123 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart4be98c02009-01-05 12:14:18 -05003124 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04003125 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003126 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05003127 int do_callback = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003129 spin_lock_irqsave(shost->host_lock, flags);
3130
James Smartaedf3492006-04-10 10:14:05 -04003131 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3132
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003133 /*
James Smart92740b22007-04-27 11:53:17 -04003134 * If the port is ONLINE, then it came back. If it was a SCSI
3135 * target, validate it still is. If not, tear down the
3136 * scsi_target on it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003137 */
James Smartaedf3492006-04-10 10:14:05 -04003138 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
James Smart92740b22007-04-27 11:53:17 -04003139 (rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04003140 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003141 dev_printk(KERN_ERR, &rport->dev,
James Smartaedf3492006-04-10 10:14:05 -04003142 "blocked FC remote port time out: no longer"
3143 " a FCP target, removing starget\n");
James Smartaedf3492006-04-10 10:14:05 -04003144 spin_unlock_irqrestore(shost->host_lock, flags);
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003145 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
James Smarta0785ed2006-05-11 13:27:09 -04003146 fc_queue_work(shost, &rport->stgt_delete_work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003147 return;
3148 }
3149
James Smart92740b22007-04-27 11:53:17 -04003150 /* NOOP state - we're flushing workq's */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003151 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3152 spin_unlock_irqrestore(shost->host_lock, flags);
3153 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003154 "blocked FC remote port time out: leaving"
3155 " rport%s alone\n",
3156 (rport->scsi_target_id != -1) ? " and starget" : "");
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003157 return;
3158 }
3159
James Smart92740b22007-04-27 11:53:17 -04003160 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3161 (rport->scsi_target_id == -1)) {
James Smartaedf3492006-04-10 10:14:05 -04003162 list_del(&rport->peers);
3163 rport->port_state = FC_PORTSTATE_DELETED;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003164 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003165 "blocked FC remote port time out: removing"
3166 " rport%s\n",
3167 (rport->scsi_target_id != -1) ? " and starget" : "");
James Smartaedf3492006-04-10 10:14:05 -04003168 fc_queue_work(shost, &rport->rport_delete_work);
3169 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003170 return;
3171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 dev_printk(KERN_ERR, &rport->dev,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003174 "blocked FC remote port time out: removing target and "
3175 "saving binding\n");
3176
James Smartaedf3492006-04-10 10:14:05 -04003177 list_move_tail(&rport->peers, &fc_host->rport_bindings);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003178
3179 /*
3180 * Note: We do not remove or clear the hostdata area. This allows
3181 * host-specific target data to persist along with the
3182 * scsi_target_id. It's up to the host to manage it's hostdata area.
3183 */
3184
3185 /*
3186 * Reinitialize port attributes that may change if the port comes back.
3187 */
3188 rport->maxframe_size = -1;
3189 rport->supported_classes = FC_COS_UNSPECIFIED;
James Smarta53eb5e2007-04-27 12:41:09 -04003190 rport->roles = FC_PORT_ROLE_UNKNOWN;
James Smartaedf3492006-04-10 10:14:05 -04003191 rport->port_state = FC_PORTSTATE_NOTPRESENT;
Mike Christiefff9d402008-08-19 18:45:23 -05003192 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003193
James Smartf78badb2008-12-05 16:29:59 -06003194 /*
3195 * Pre-emptively kill I/O rather than waiting for the work queue
3196 * item to teardown the starget. (FCOE libFC folks prefer this
3197 * and to have the rport_port_id still set when it's done).
3198 */
3199 spin_unlock_irqrestore(shost->host_lock, flags);
3200 fc_terminate_rport_io(rport);
3201
Michael Reed8798a692009-10-09 14:15:59 -05003202 spin_lock_irqsave(shost->host_lock, flags);
James Smartf78badb2008-12-05 16:29:59 -06003203
Michael Reed8798a692009-10-09 14:15:59 -05003204 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3205
3206 /* remove the identifiers that aren't used in the consisting binding */
3207 switch (fc_host->tgtid_bind_type) {
3208 case FC_TGTID_BIND_BY_WWPN:
3209 rport->node_name = -1;
3210 rport->port_id = -1;
3211 break;
3212 case FC_TGTID_BIND_BY_WWNN:
3213 rport->port_name = -1;
3214 rport->port_id = -1;
3215 break;
3216 case FC_TGTID_BIND_BY_ID:
3217 rport->node_name = -1;
3218 rport->port_name = -1;
3219 break;
3220 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3221 break;
3222 }
3223
3224 /*
3225 * As this only occurs if the remote port (scsi target)
3226 * went away and didn't come back - we'll remove
3227 * all attached scsi devices.
3228 */
3229 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3230 fc_queue_work(shost, &rport->stgt_delete_work);
3231
3232 do_callback = 1;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003233 }
3234
Michael Reed8798a692009-10-09 14:15:59 -05003235 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05003236
3237 /*
3238 * Notify the driver that the rport is now dead. The LLDD will
3239 * also guarantee that any communication to the rport is terminated
3240 *
3241 * Note: we set the CALLBK_DONE flag above to correspond
3242 */
Michael Reed8798a692009-10-09 14:15:59 -05003243 if (do_callback && i->f->dev_loss_tmo_callbk)
James Smart4be98c02009-01-05 12:14:18 -05003244 i->f->dev_loss_tmo_callbk(rport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
James Smart4be98c02009-01-05 12:14:18 -05003247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248/**
Rob Landleyeb448202007-11-03 13:30:39 -05003249 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
David Howellsc4028952006-11-22 14:57:56 +00003250 * @work: rport to terminate io on.
James Smart0f29b962006-08-18 17:33:29 -04003251 *
3252 * Notes: Only requests the failure of the io, not that all are flushed
3253 * prior to returning.
Rob Landleyeb448202007-11-03 13:30:39 -05003254 */
James Smart0f29b962006-08-18 17:33:29 -04003255static void
David Howellsc4028952006-11-22 14:57:56 +00003256fc_timeout_fail_rport_io(struct work_struct *work)
James Smart0f29b962006-08-18 17:33:29 -04003257{
David Howellsc4028952006-11-22 14:57:56 +00003258 struct fc_rport *rport =
3259 container_of(work, struct fc_rport, fail_io_work.work);
James Smart0f29b962006-08-18 17:33:29 -04003260
3261 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3262 return;
3263
Mike Christiefff9d402008-08-19 18:45:23 -05003264 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3265 fc_terminate_rport_io(rport);
James Smart0f29b962006-08-18 17:33:29 -04003266}
3267
3268/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
David Howellsc4028952006-11-22 14:57:56 +00003270 * @work: remote port to be scanned.
Rob Landleyeb448202007-11-03 13:30:39 -05003271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272static void
David Howellsc4028952006-11-22 14:57:56 +00003273fc_scsi_scan_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
David Howellsc4028952006-11-22 14:57:56 +00003275 struct fc_rport *rport =
3276 container_of(work, struct fc_rport, scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003277 struct Scsi_Host *shost = rport_to_shost(rport);
Christof Schmitt03f002f2007-08-28 09:31:21 +02003278 struct fc_internal *i = to_fc_internal(shost->transportt);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003279 unsigned long flags;
3280
James Smartaedf3492006-04-10 10:14:05 -04003281 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
Christof Schmitt03f002f2007-08-28 09:31:21 +02003282 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3283 !(i->f->disable_target_scan)) {
James Smartaedf3492006-04-10 10:14:05 -04003284 scsi_scan_target(&rport->dev, rport->channel,
3285 rport->scsi_target_id, SCAN_WILD_CARD, 1);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003286 }
James Smartaedf3492006-04-10 10:14:05 -04003287
3288 spin_lock_irqsave(shost->host_lock, flags);
3289 rport->flags &= ~FC_RPORT_SCAN_PENDING;
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003290 spin_unlock_irqrestore(shost->host_lock, flags);
3291}
3292
Christof Schmitt65d430f2009-10-30 17:59:29 +01003293/**
3294 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3295 * @cmnd: SCSI command that scsi_eh is trying to recover
3296 *
3297 * This routine can be called from a FC LLD scsi_eh callback. It
3298 * blocks the scsi_eh thread until the fc_rport leaves the
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003299 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3300 * necessary to avoid the scsi_eh failing recovery actions for blocked
3301 * rports which would lead to offlined SCSI devices.
3302 *
3303 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3304 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3305 * passed back to scsi_eh.
Christof Schmitt65d430f2009-10-30 17:59:29 +01003306 */
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003307int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
Christof Schmitt65d430f2009-10-30 17:59:29 +01003308{
3309 struct Scsi_Host *shost = cmnd->device->host;
3310 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3311 unsigned long flags;
3312
3313 spin_lock_irqsave(shost->host_lock, flags);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003314 while (rport->port_state == FC_PORTSTATE_BLOCKED &&
3315 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
Christof Schmitt65d430f2009-10-30 17:59:29 +01003316 spin_unlock_irqrestore(shost->host_lock, flags);
3317 msleep(1000);
3318 spin_lock_irqsave(shost->host_lock, flags);
3319 }
3320 spin_unlock_irqrestore(shost->host_lock, flags);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003321
3322 if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
3323 return FAST_IO_FAIL;
3324
3325 return 0;
Christof Schmitt65d430f2009-10-30 17:59:29 +01003326}
3327EXPORT_SYMBOL(fc_block_scsi_eh);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003328
James Smarta53eb5e2007-04-27 12:41:09 -04003329/**
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003330 * fc_vport_setup - allocates and creates a FC virtual port.
James Smarta53eb5e2007-04-27 12:41:09 -04003331 * @shost: scsi host the virtual port is connected to.
3332 * @channel: Channel on shost port connected to.
3333 * @pdev: parent device for vport
3334 * @ids: The world wide names, FC4 port roles, etc for
3335 * the virtual port.
3336 * @ret_vport: The pointer to the created vport.
3337 *
3338 * Allocates and creates the vport structure, calls the parent host
3339 * to instantiate the vport, the completes w/ class and sysfs creation.
3340 *
3341 * Notes:
3342 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003343 */
James Smarta53eb5e2007-04-27 12:41:09 -04003344static int
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003345fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
James Smarta53eb5e2007-04-27 12:41:09 -04003346 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3347{
3348 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3349 struct fc_internal *fci = to_fc_internal(shost->transportt);
3350 struct fc_vport *vport;
3351 struct device *dev;
3352 unsigned long flags;
3353 size_t size;
3354 int error;
3355
3356 *ret_vport = NULL;
3357
3358 if ( ! fci->f->vport_create)
3359 return -ENOENT;
3360
3361 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3362 vport = kzalloc(size, GFP_KERNEL);
3363 if (unlikely(!vport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003364 printk(KERN_ERR "%s: allocation failure\n", __func__);
James Smarta53eb5e2007-04-27 12:41:09 -04003365 return -ENOMEM;
3366 }
3367
3368 vport->vport_state = FC_VPORT_UNKNOWN;
3369 vport->vport_last_state = FC_VPORT_UNKNOWN;
3370 vport->node_name = ids->node_name;
3371 vport->port_name = ids->port_name;
3372 vport->roles = ids->roles;
3373 vport->vport_type = ids->vport_type;
3374 if (fci->f->dd_fcvport_size)
3375 vport->dd_data = &vport[1];
3376 vport->shost = shost;
3377 vport->channel = channel;
3378 vport->flags = FC_VPORT_CREATING;
James Smart9ef3e4a2007-05-24 19:04:44 -04003379 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
James Smarta53eb5e2007-04-27 12:41:09 -04003380
3381 spin_lock_irqsave(shost->host_lock, flags);
3382
3383 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3384 spin_unlock_irqrestore(shost->host_lock, flags);
3385 kfree(vport);
3386 return -ENOSPC;
3387 }
3388 fc_host->npiv_vports_inuse++;
3389 vport->number = fc_host->next_vport_number++;
3390 list_add_tail(&vport->peers, &fc_host->vports);
3391 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3392
3393 spin_unlock_irqrestore(shost->host_lock, flags);
3394
3395 dev = &vport->dev;
3396 device_initialize(dev); /* takes self reference */
3397 dev->parent = get_device(pdev); /* takes parent reference */
3398 dev->release = fc_vport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01003399 dev_set_name(dev, "vport-%d:%d-%d",
3400 shost->host_no, channel, vport->number);
James Smarta53eb5e2007-04-27 12:41:09 -04003401 transport_setup_device(dev);
3402
3403 error = device_add(dev);
3404 if (error) {
3405 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3406 goto delete_vport;
3407 }
3408 transport_add_device(dev);
3409 transport_configure_device(dev);
3410
3411 error = fci->f->vport_create(vport, ids->disable);
3412 if (error) {
3413 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3414 goto delete_vport_all;
3415 }
3416
3417 /*
3418 * if the parent isn't the physical adapter's Scsi_Host, ensure
3419 * the Scsi_Host at least contains ia symlink to the vport.
3420 */
3421 if (pdev != &shost->shost_gendev) {
3422 error = sysfs_create_link(&shost->shost_gendev.kobj,
Kay Sievers71610f52008-12-03 22:41:36 +01003423 &dev->kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003424 if (error)
3425 printk(KERN_ERR
3426 "%s: Cannot create vport symlinks for "
3427 "%s, err=%d\n",
Kay Sievers71610f52008-12-03 22:41:36 +01003428 __func__, dev_name(dev), error);
James Smarta53eb5e2007-04-27 12:41:09 -04003429 }
3430 spin_lock_irqsave(shost->host_lock, flags);
3431 vport->flags &= ~FC_VPORT_CREATING;
3432 spin_unlock_irqrestore(shost->host_lock, flags);
3433
3434 dev_printk(KERN_NOTICE, pdev,
Kay Sievers71610f52008-12-03 22:41:36 +01003435 "%s created via shost%d channel %d\n", dev_name(dev),
James Smarta53eb5e2007-04-27 12:41:09 -04003436 shost->host_no, channel);
3437
3438 *ret_vport = vport;
3439
3440 return 0;
3441
3442delete_vport_all:
3443 transport_remove_device(dev);
3444 device_del(dev);
3445delete_vport:
3446 transport_destroy_device(dev);
3447 spin_lock_irqsave(shost->host_lock, flags);
3448 list_del(&vport->peers);
3449 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3450 fc_host->npiv_vports_inuse--;
3451 spin_unlock_irqrestore(shost->host_lock, flags);
3452 put_device(dev->parent);
3453 kfree(vport);
3454
3455 return error;
3456}
3457
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003458/**
3459 * fc_vport_create - Admin App or LLDD requests creation of a vport
3460 * @shost: scsi host the virtual port is connected to.
3461 * @channel: channel on shost port connected to.
3462 * @ids: The world wide names, FC4 port roles, etc for
3463 * the virtual port.
3464 *
3465 * Notes:
3466 * This routine assumes no locks are held on entry.
3467 */
3468struct fc_vport *
3469fc_vport_create(struct Scsi_Host *shost, int channel,
3470 struct fc_vport_identifiers *ids)
3471{
3472 int stat;
3473 struct fc_vport *vport;
3474
3475 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3476 ids, &vport);
3477 return stat ? NULL : vport;
3478}
3479EXPORT_SYMBOL(fc_vport_create);
James Smarta53eb5e2007-04-27 12:41:09 -04003480
3481/**
3482 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3483 * @vport: fc_vport to be terminated
3484 *
3485 * Calls the LLDD vport_delete() function, then deallocates and removes
3486 * the vport from the shost and object tree.
3487 *
3488 * Notes:
3489 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003490 */
James Smarta53eb5e2007-04-27 12:41:09 -04003491int
3492fc_vport_terminate(struct fc_vport *vport)
3493{
3494 struct Scsi_Host *shost = vport_to_shost(vport);
3495 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3496 struct fc_internal *i = to_fc_internal(shost->transportt);
3497 struct device *dev = &vport->dev;
3498 unsigned long flags;
3499 int stat;
3500
James Smarta53eb5e2007-04-27 12:41:09 -04003501 if (i->f->vport_delete)
3502 stat = i->f->vport_delete(vport);
3503 else
3504 stat = -ENOENT;
3505
3506 spin_lock_irqsave(shost->host_lock, flags);
3507 vport->flags &= ~FC_VPORT_DELETING;
3508 if (!stat) {
3509 vport->flags |= FC_VPORT_DELETED;
3510 list_del(&vport->peers);
3511 fc_host->npiv_vports_inuse--;
3512 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3513 }
3514 spin_unlock_irqrestore(shost->host_lock, flags);
3515
3516 if (stat)
3517 return stat;
3518
3519 if (dev->parent != &shost->shost_gendev)
Kay Sievers71610f52008-12-03 22:41:36 +01003520 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003521 transport_remove_device(dev);
3522 device_del(dev);
3523 transport_destroy_device(dev);
3524
3525 /*
3526 * Removing our self-reference should mean our
3527 * release function gets called, which will drop the remaining
3528 * parent reference and free the data structure.
3529 */
3530 put_device(dev); /* for self-reference */
3531
3532 return 0; /* SUCCESS */
3533}
3534EXPORT_SYMBOL(fc_vport_terminate);
3535
James Smart9ef3e4a2007-05-24 19:04:44 -04003536/**
3537 * fc_vport_sched_delete - workq-based delete request for a vport
James Smart9ef3e4a2007-05-24 19:04:44 -04003538 * @work: vport to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05003539 */
James Smart9ef3e4a2007-05-24 19:04:44 -04003540static void
3541fc_vport_sched_delete(struct work_struct *work)
3542{
3543 struct fc_vport *vport =
3544 container_of(work, struct fc_vport, vport_delete_work);
3545 int stat;
James Smarta53eb5e2007-04-27 12:41:09 -04003546
James Smart9ef3e4a2007-05-24 19:04:44 -04003547 stat = fc_vport_terminate(vport);
3548 if (stat)
3549 dev_printk(KERN_ERR, vport->dev.parent,
3550 "%s: %s could not be deleted created via "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003551 "shost%d channel %d - error %d\n", __func__,
Kay Sievers71610f52008-12-03 22:41:36 +01003552 dev_name(&vport->dev), vport->shost->host_no,
James Smart9ef3e4a2007-05-24 19:04:44 -04003553 vport->channel, stat);
3554}
3555
3556
James Smart9e4f5e22009-03-26 13:33:19 -04003557/*
3558 * BSG support
3559 */
3560
3561
3562/**
3563 * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
3564 * @job: fc_bsg_job that is to be torn down
3565 */
3566static void
3567fc_destroy_bsgjob(struct fc_bsg_job *job)
3568{
3569 unsigned long flags;
3570
3571 spin_lock_irqsave(&job->job_lock, flags);
3572 if (job->ref_cnt) {
3573 spin_unlock_irqrestore(&job->job_lock, flags);
3574 return;
3575 }
3576 spin_unlock_irqrestore(&job->job_lock, flags);
3577
3578 put_device(job->dev); /* release reference for the request */
3579
3580 kfree(job->request_payload.sg_list);
3581 kfree(job->reply_payload.sg_list);
3582 kfree(job);
3583}
3584
James Smart9e4f5e22009-03-26 13:33:19 -04003585/**
3586 * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
3587 * completed
3588 * @job: fc_bsg_job that is complete
3589 */
3590static void
3591fc_bsg_jobdone(struct fc_bsg_job *job)
3592{
3593 struct request *req = job->req;
3594 struct request *rsp = req->next_rq;
James Smart9e4f5e22009-03-26 13:33:19 -04003595 int err;
3596
James Smart9e4f5e22009-03-26 13:33:19 -04003597 err = job->req->errors = job->reply->result;
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003598
James Smart9e4f5e22009-03-26 13:33:19 -04003599 if (err < 0)
3600 /* we're only returning the result field in the reply */
3601 job->req->sense_len = sizeof(uint32_t);
3602 else
3603 job->req->sense_len = job->reply_len;
3604
3605 /* we assume all request payload was transferred, residual == 0 */
3606 req->resid_len = 0;
3607
3608 if (rsp) {
3609 WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
3610
3611 /* set reply (bidi) residual */
3612 rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
3613 rsp->resid_len);
3614 }
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003615 blk_complete_request(req);
James Smart9e4f5e22009-03-26 13:33:19 -04003616}
3617
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003618/**
3619 * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
Randy Dunlapfe5d20c2009-07-04 13:10:41 -07003620 * @rq: BSG request that holds the job to be destroyed
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07003621 */
3622static void fc_bsg_softirq_done(struct request *rq)
3623{
3624 struct fc_bsg_job *job = rq->special;
3625 unsigned long flags;
3626
3627 spin_lock_irqsave(&job->job_lock, flags);
3628 job->state_flags |= FC_RQST_STATE_DONE;
3629 job->ref_cnt--;
3630 spin_unlock_irqrestore(&job->job_lock, flags);
3631
3632 blk_end_request_all(rq, rq->errors);
3633 fc_destroy_bsgjob(job);
3634}
James Smart9e4f5e22009-03-26 13:33:19 -04003635
3636/**
3637 * fc_bsg_job_timeout - handler for when a bsg request timesout
3638 * @req: request that timed out
3639 */
3640static enum blk_eh_timer_return
3641fc_bsg_job_timeout(struct request *req)
3642{
3643 struct fc_bsg_job *job = (void *) req->special;
3644 struct Scsi_Host *shost = job->shost;
3645 struct fc_internal *i = to_fc_internal(shost->transportt);
3646 unsigned long flags;
3647 int err = 0, done = 0;
3648
3649 if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
3650 return BLK_EH_RESET_TIMER;
3651
3652 spin_lock_irqsave(&job->job_lock, flags);
3653 if (job->state_flags & FC_RQST_STATE_DONE)
3654 done = 1;
3655 else
3656 job->ref_cnt++;
3657 spin_unlock_irqrestore(&job->job_lock, flags);
3658
3659 if (!done && i->f->bsg_timeout) {
3660 /* call LLDD to abort the i/o as it has timed out */
3661 err = i->f->bsg_timeout(job);
Swen Schilligb8f08642010-01-14 17:19:00 +01003662 if (err == -EAGAIN) {
3663 job->ref_cnt--;
3664 return BLK_EH_RESET_TIMER;
3665 } else if (err)
James Smart9e4f5e22009-03-26 13:33:19 -04003666 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3667 "abort failed with status %d\n", err);
3668 }
3669
James Smart9e4f5e22009-03-26 13:33:19 -04003670 /* the blk_end_sync_io() doesn't check the error */
Giridhar Malavali47e7e892009-06-19 16:26:54 -07003671 if (done)
3672 return BLK_EH_NOT_HANDLED;
3673 else
3674 return BLK_EH_HANDLED;
James Smart9e4f5e22009-03-26 13:33:19 -04003675}
3676
James Smart9e4f5e22009-03-26 13:33:19 -04003677static int
3678fc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
3679{
3680 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
3681
3682 BUG_ON(!req->nr_phys_segments);
3683
3684 buf->sg_list = kzalloc(sz, GFP_KERNEL);
3685 if (!buf->sg_list)
3686 return -ENOMEM;
3687 sg_init_table(buf->sg_list, req->nr_phys_segments);
3688 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
3689 buf->payload_len = blk_rq_bytes(req);
3690 return 0;
3691}
3692
3693
3694/**
3695 * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
3696 * bsg request
3697 * @shost: SCSI Host corresponding to the bsg object
3698 * @rport: (optional) FC Remote Port corresponding to the bsg object
3699 * @req: BSG request that needs a job structure
3700 */
3701static int
3702fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
3703 struct request *req)
3704{
3705 struct fc_internal *i = to_fc_internal(shost->transportt);
3706 struct request *rsp = req->next_rq;
3707 struct fc_bsg_job *job;
3708 int ret;
3709
3710 BUG_ON(req->special);
3711
3712 job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
3713 GFP_KERNEL);
3714 if (!job)
3715 return -ENOMEM;
3716
3717 /*
3718 * Note: this is a bit silly.
3719 * The request gets formatted as a SGIO v4 ioctl request, which
3720 * then gets reformatted as a blk request, which then gets
3721 * reformatted as a fc bsg request. And on completion, we have
3722 * to wrap return results such that SGIO v4 thinks it was a scsi
3723 * status. I hope this was all worth it.
3724 */
3725
3726 req->special = job;
3727 job->shost = shost;
3728 job->rport = rport;
3729 job->req = req;
3730 if (i->f->dd_bsg_size)
3731 job->dd_data = (void *)&job[1];
3732 spin_lock_init(&job->job_lock);
3733 job->request = (struct fc_bsg_request *)req->cmd;
3734 job->request_len = req->cmd_len;
3735 job->reply = req->sense;
3736 job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
3737 * allocated */
3738 if (req->bio) {
3739 ret = fc_bsg_map_buffer(&job->request_payload, req);
3740 if (ret)
3741 goto failjob_rls_job;
3742 }
3743 if (rsp && rsp->bio) {
3744 ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
3745 if (ret)
3746 goto failjob_rls_rqst_payload;
3747 }
3748 job->job_done = fc_bsg_jobdone;
3749 if (rport)
3750 job->dev = &rport->dev;
3751 else
3752 job->dev = &shost->shost_gendev;
3753 get_device(job->dev); /* take a reference for the request */
3754
3755 job->ref_cnt = 1;
3756
3757 return 0;
3758
3759
3760failjob_rls_rqst_payload:
3761 kfree(job->request_payload.sg_list);
3762failjob_rls_job:
3763 kfree(job);
3764 return -ENOMEM;
3765}
3766
3767
3768enum fc_dispatch_result {
3769 FC_DISPATCH_BREAK, /* on return, q is locked, break from q loop */
3770 FC_DISPATCH_LOCKED, /* on return, q is locked, continue on */
3771 FC_DISPATCH_UNLOCKED, /* on return, q is unlocked, continue on */
3772};
3773
3774
3775/**
3776 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
Randy Dunlap54159072009-06-18 19:39:57 -07003777 * @q: fc host request queue
James Smart9e4f5e22009-03-26 13:33:19 -04003778 * @shost: scsi host rport attached to
3779 * @job: bsg job to be processed
3780 */
3781static enum fc_dispatch_result
3782fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3783 struct fc_bsg_job *job)
3784{
3785 struct fc_internal *i = to_fc_internal(shost->transportt);
3786 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3787 int ret;
3788
3789 /* Validate the host command */
3790 switch (job->request->msgcode) {
3791 case FC_BSG_HST_ADD_RPORT:
3792 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3793 break;
3794
3795 case FC_BSG_HST_DEL_RPORT:
3796 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3797 break;
3798
3799 case FC_BSG_HST_ELS_NOLOGIN:
3800 cmdlen += sizeof(struct fc_bsg_host_els);
3801 /* there better be a xmt and rcv payloads */
3802 if ((!job->request_payload.payload_len) ||
3803 (!job->reply_payload.payload_len)) {
3804 ret = -EINVAL;
3805 goto fail_host_msg;
3806 }
3807 break;
3808
3809 case FC_BSG_HST_CT:
3810 cmdlen += sizeof(struct fc_bsg_host_ct);
3811 /* there better be xmt and rcv payloads */
3812 if ((!job->request_payload.payload_len) ||
3813 (!job->reply_payload.payload_len)) {
3814 ret = -EINVAL;
3815 goto fail_host_msg;
3816 }
3817 break;
3818
3819 case FC_BSG_HST_VENDOR:
3820 cmdlen += sizeof(struct fc_bsg_host_vendor);
3821 if ((shost->hostt->vendor_id == 0L) ||
3822 (job->request->rqst_data.h_vendor.vendor_id !=
3823 shost->hostt->vendor_id)) {
3824 ret = -ESRCH;
3825 goto fail_host_msg;
3826 }
3827 break;
3828
3829 default:
3830 ret = -EBADR;
3831 goto fail_host_msg;
3832 }
3833
3834 /* check if we really have all the request data needed */
3835 if (job->request_len < cmdlen) {
3836 ret = -ENOMSG;
3837 goto fail_host_msg;
3838 }
3839
3840 ret = i->f->bsg_request(job);
3841 if (!ret)
3842 return FC_DISPATCH_UNLOCKED;
3843
3844fail_host_msg:
3845 /* return the errno failure code as the only status */
3846 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003847 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003848 job->reply->result = ret;
3849 job->reply_len = sizeof(uint32_t);
3850 fc_bsg_jobdone(job);
3851 return FC_DISPATCH_UNLOCKED;
3852}
3853
3854
3855/*
3856 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3857 * @rport: rport to be restarted
3858 */
3859static void
3860fc_bsg_goose_queue(struct fc_rport *rport)
3861{
James Smart9e4f5e22009-03-26 13:33:19 -04003862 if (!rport->rqst_q)
3863 return;
3864
Jens Axboec21e6be2011-04-19 13:32:46 +02003865 /*
3866 * This get/put dance makes no sense
3867 */
James Smart9e4f5e22009-03-26 13:33:19 -04003868 get_device(&rport->dev);
Jens Axboec21e6be2011-04-19 13:32:46 +02003869 blk_run_queue_async(rport->rqst_q);
James Smart9e4f5e22009-03-26 13:33:19 -04003870 put_device(&rport->dev);
3871}
3872
James Smart9e4f5e22009-03-26 13:33:19 -04003873/**
3874 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
Randy Dunlap54159072009-06-18 19:39:57 -07003875 * @q: rport request queue
James Smart9e4f5e22009-03-26 13:33:19 -04003876 * @shost: scsi host rport attached to
3877 * @rport: rport request destined to
3878 * @job: bsg job to be processed
3879 */
3880static enum fc_dispatch_result
3881fc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3882 struct fc_rport *rport, struct fc_bsg_job *job)
3883{
3884 struct fc_internal *i = to_fc_internal(shost->transportt);
3885 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3886 int ret;
3887
3888 /* Validate the rport command */
3889 switch (job->request->msgcode) {
3890 case FC_BSG_RPT_ELS:
3891 cmdlen += sizeof(struct fc_bsg_rport_els);
3892 goto check_bidi;
3893
3894 case FC_BSG_RPT_CT:
3895 cmdlen += sizeof(struct fc_bsg_rport_ct);
3896check_bidi:
3897 /* there better be xmt and rcv payloads */
3898 if ((!job->request_payload.payload_len) ||
3899 (!job->reply_payload.payload_len)) {
3900 ret = -EINVAL;
3901 goto fail_rport_msg;
3902 }
3903 break;
3904 default:
3905 ret = -EBADR;
3906 goto fail_rport_msg;
3907 }
3908
3909 /* check if we really have all the request data needed */
3910 if (job->request_len < cmdlen) {
3911 ret = -ENOMSG;
3912 goto fail_rport_msg;
3913 }
3914
3915 ret = i->f->bsg_request(job);
3916 if (!ret)
3917 return FC_DISPATCH_UNLOCKED;
3918
3919fail_rport_msg:
3920 /* return the errno failure code as the only status */
3921 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003922 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003923 job->reply->result = ret;
3924 job->reply_len = sizeof(uint32_t);
3925 fc_bsg_jobdone(job);
3926 return FC_DISPATCH_UNLOCKED;
3927}
3928
3929
3930/**
3931 * fc_bsg_request_handler - generic handler for bsg requests
3932 * @q: request queue to manage
3933 * @shost: Scsi_Host related to the bsg object
3934 * @rport: FC remote port related to the bsg object (optional)
3935 * @dev: device structure for bsg object
3936 */
3937static void
3938fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
3939 struct fc_rport *rport, struct device *dev)
3940{
3941 struct request *req;
3942 struct fc_bsg_job *job;
3943 enum fc_dispatch_result ret;
3944
3945 if (!get_device(dev))
3946 return;
3947
Jens Axboe7eaceac2011-03-10 08:52:07 +01003948 while (1) {
Mike Christie2bc1c592009-11-05 11:18:09 -06003949 if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED) &&
3950 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
3951 break;
James Smart9e4f5e22009-03-26 13:33:19 -04003952
3953 req = blk_fetch_request(q);
3954 if (!req)
3955 break;
3956
3957 if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
3958 req->errors = -ENXIO;
3959 spin_unlock_irq(q->queue_lock);
Sarang Radked88a7142010-03-10 04:03:04 -06003960 blk_end_request_all(req, -ENXIO);
James Smart9e4f5e22009-03-26 13:33:19 -04003961 spin_lock_irq(q->queue_lock);
3962 continue;
3963 }
3964
3965 spin_unlock_irq(q->queue_lock);
3966
3967 ret = fc_req_to_bsgjob(shost, rport, req);
3968 if (ret) {
3969 req->errors = ret;
Sarang Radked88a7142010-03-10 04:03:04 -06003970 blk_end_request_all(req, ret);
James Smart9e4f5e22009-03-26 13:33:19 -04003971 spin_lock_irq(q->queue_lock);
3972 continue;
3973 }
3974
3975 job = req->special;
3976
3977 /* check if we have the msgcode value at least */
3978 if (job->request_len < sizeof(uint32_t)) {
3979 BUG_ON(job->reply_len < sizeof(uint32_t));
Brian King61ec33e2009-10-19 10:53:36 -05003980 job->reply->reply_payload_rcv_len = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04003981 job->reply->result = -ENOMSG;
3982 job->reply_len = sizeof(uint32_t);
3983 fc_bsg_jobdone(job);
3984 spin_lock_irq(q->queue_lock);
3985 continue;
3986 }
3987
3988 /* the dispatch routines will unlock the queue_lock */
3989 if (rport)
3990 ret = fc_bsg_rport_dispatch(q, shost, rport, job);
3991 else
3992 ret = fc_bsg_host_dispatch(q, shost, job);
3993
3994 /* did dispatcher hit state that can't process any more */
3995 if (ret == FC_DISPATCH_BREAK)
3996 break;
3997
3998 /* did dispatcher had released the lock */
3999 if (ret == FC_DISPATCH_UNLOCKED)
4000 spin_lock_irq(q->queue_lock);
4001 }
4002
4003 spin_unlock_irq(q->queue_lock);
4004 put_device(dev);
4005 spin_lock_irq(q->queue_lock);
4006}
4007
4008
4009/**
4010 * fc_bsg_host_handler - handler for bsg requests for a fc host
4011 * @q: fc host request queue
4012 */
4013static void
4014fc_bsg_host_handler(struct request_queue *q)
4015{
4016 struct Scsi_Host *shost = q->queuedata;
4017
4018 fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
4019}
4020
4021
4022/**
4023 * fc_bsg_rport_handler - handler for bsg requests for a fc rport
4024 * @q: rport request queue
4025 */
4026static void
4027fc_bsg_rport_handler(struct request_queue *q)
4028{
4029 struct fc_rport *rport = q->queuedata;
4030 struct Scsi_Host *shost = rport_to_shost(rport);
4031
4032 fc_bsg_request_handler(q, shost, rport, &rport->dev);
4033}
4034
4035
4036/**
4037 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
4038 * @shost: shost for fc_host
4039 * @fc_host: fc_host adding the structures to
4040 */
4041static int
4042fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
4043{
4044 struct device *dev = &shost->shost_gendev;
4045 struct fc_internal *i = to_fc_internal(shost->transportt);
4046 struct request_queue *q;
4047 int err;
James Bottomley3c559ea2009-06-21 12:11:43 -05004048 char bsg_name[20];
James Smart9e4f5e22009-03-26 13:33:19 -04004049
4050 fc_host->rqst_q = NULL;
4051
4052 if (!i->f->bsg_request)
4053 return -ENOTSUPP;
4054
4055 snprintf(bsg_name, sizeof(bsg_name),
4056 "fc_host%d", shost->host_no);
4057
4058 q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
4059 if (!q) {
4060 printk(KERN_ERR "fc_host%d: bsg interface failed to "
4061 "initialize - no request queue\n",
4062 shost->host_no);
4063 return -ENOMEM;
4064 }
4065
4066 q->queuedata = shost;
4067 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07004068 blk_queue_softirq_done(q, fc_bsg_softirq_done);
James Smart9e4f5e22009-03-26 13:33:19 -04004069 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
4070 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
4071
4072 err = bsg_register_queue(q, dev, bsg_name, NULL);
4073 if (err) {
4074 printk(KERN_ERR "fc_host%d: bsg interface failed to "
4075 "initialize - register queue\n",
4076 shost->host_no);
4077 blk_cleanup_queue(q);
4078 return err;
4079 }
4080
4081 fc_host->rqst_q = q;
4082 return 0;
4083}
4084
4085
4086/**
4087 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
4088 * @shost: shost that rport is attached to
4089 * @rport: rport that the bsg hooks are being attached to
4090 */
4091static int
4092fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
4093{
4094 struct device *dev = &rport->dev;
4095 struct fc_internal *i = to_fc_internal(shost->transportt);
4096 struct request_queue *q;
4097 int err;
4098
4099 rport->rqst_q = NULL;
4100
4101 if (!i->f->bsg_request)
4102 return -ENOTSUPP;
4103
4104 q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
4105 if (!q) {
4106 printk(KERN_ERR "%s: bsg interface failed to "
4107 "initialize - no request queue\n",
4108 dev->kobj.name);
4109 return -ENOMEM;
4110 }
4111
4112 q->queuedata = rport;
4113 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Giridhar Malavalib5c6f772009-06-19 16:26:53 -07004114 blk_queue_softirq_done(q, fc_bsg_softirq_done);
James Smart9e4f5e22009-03-26 13:33:19 -04004115 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
4116 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
4117
4118 err = bsg_register_queue(q, dev, NULL, NULL);
4119 if (err) {
4120 printk(KERN_ERR "%s: bsg interface failed to "
4121 "initialize - register queue\n",
4122 dev->kobj.name);
4123 blk_cleanup_queue(q);
4124 return err;
4125 }
4126
4127 rport->rqst_q = q;
4128 return 0;
4129}
4130
4131
4132/**
4133 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
4134 * @q: the request_queue that is to be torn down.
James Smart78d16342010-08-31 22:27:31 -04004135 *
4136 * Notes:
4137 * Before unregistering the queue empty any requests that are blocked
4138 *
4139 *
James Smart9e4f5e22009-03-26 13:33:19 -04004140 */
4141static void
4142fc_bsg_remove(struct request_queue *q)
4143{
4144 if (q) {
4145 bsg_unregister_queue(q);
4146 blk_cleanup_queue(q);
4147 }
4148}
4149
4150
James Smart9ef3e4a2007-05-24 19:04:44 -04004151/* Original Author: Martin Hicks */
4152MODULE_AUTHOR("James Smart");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153MODULE_DESCRIPTION("FC Transport Attributes");
4154MODULE_LICENSE("GPL");
4155
4156module_init(fc_transport_init);
4157module_exit(fc_transport_exit);