Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm...
[linux-2.6-block.git] / drivers / scsi / scsi_transport_fc.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
9ef3e4a4 2/*
1da177e4
LT
3 * FiberChannel transport specific attributes exported to sysfs.
4 *
5 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
6 *
1da177e4
LT
7 * ========
8 *
a53eb5e0 9 * Copyright (C) 2004-2007 James Smart, Emulex Corporation
1da177e4
LT
10 * Rewrite for host, target, device, and remote port attributes,
11 * statistics, and service functions...
9ef3e4a4 12 * Add vports, etc
1da177e4
LT
13 */
14#include <linux/module.h>
15#include <linux/init.h>
5a0e3ad6 16#include <linux/slab.h>
65d430fa 17#include <linux/delay.h>
ecc30990 18#include <linux/kernel.h>
c00da4c9 19#include <linux/bsg-lib.h>
1da177e4
LT
20#include <scsi/scsi_device.h>
21#include <scsi/scsi_host.h>
22#include <scsi/scsi_transport.h>
23#include <scsi/scsi_transport_fc.h>
c829c394 24#include <scsi/scsi_cmnd.h>
84314fd4
JS
25#include <net/netlink.h>
26#include <scsi/scsi_netlink_fc.h>
9e4f5e29 27#include <scsi/scsi_bsg_fc.h>
1da177e4
LT
28#include "scsi_priv.h"
29
aedf3497 30static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
9ef3e4a4 31static void fc_vport_sched_delete(struct work_struct *work);
a30c3f69 32static int fc_vport_setup(struct Scsi_Host *shost, int channel,
a53eb5e0
JS
33 struct device *pdev, struct fc_vport_identifiers *ids,
34 struct fc_vport **vport);
9e4f5e29
JS
35static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
36static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
37static void fc_bsg_remove(struct request_queue *);
38static void fc_bsg_goose_queue(struct fc_rport *);
a53eb5e0 39
43ca910a
MC
40/*
41 * Module Parameters
42 */
43
44/*
45 * dev_loss_tmo: the default number of seconds that the FC transport
46 * should insulate the loss of a remote port.
47 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
48 */
49static unsigned int fc_dev_loss_tmo = 60; /* seconds */
50
51module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
52MODULE_PARM_DESC(dev_loss_tmo,
53 "Maximum number of seconds that the FC transport should"
54 " insulate the loss of a remote port. Once this value is"
55 " exceeded, the scsi target is removed. Value should be"
56 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
57 " fast_io_fail_tmo is not set.");
58
1da177e4
LT
59/*
60 * Redefine so that we can have same named attributes in the
61 * sdev/starget/host objects.
62 */
ee959b00
TJ
63#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
64struct device_attribute device_attr_##_prefix##_##_name = \
1da177e4
LT
65 __ATTR(_name,_mode,_show,_store)
66
67#define fc_enum_name_search(title, table_type, table) \
68static const char *get_fc_##title##_name(enum table_type table_key) \
69{ \
70 int i; \
71 char *name = NULL; \
72 \
6391a113 73 for (i = 0; i < ARRAY_SIZE(table); i++) { \
1da177e4
LT
74 if (table[i].value == table_key) { \
75 name = table[i].name; \
76 break; \
77 } \
78 } \
79 return name; \
80}
81
82#define fc_enum_name_match(title, table_type, table) \
83static int get_fc_##title##_match(const char *table_key, \
84 enum table_type *value) \
85{ \
86 int i; \
87 \
6391a113 88 for (i = 0; i < ARRAY_SIZE(table); i++) { \
1da177e4
LT
89 if (strncmp(table_key, table[i].name, \
90 table[i].matchlen) == 0) { \
91 *value = table[i].value; \
92 return 0; /* success */ \
93 } \
94 } \
95 return 1; /* failure */ \
96}
97
98
99/* Convert fc_port_type values to ascii string name */
100static struct {
101 enum fc_port_type value;
102 char *name;
103} fc_port_type_names[] = {
104 { FC_PORTTYPE_UNKNOWN, "Unknown" },
105 { FC_PORTTYPE_OTHER, "Other" },
106 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
107 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
108 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
109 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
951948a3 110 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
a53eb5e0 111 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
1da177e4
LT
112};
113fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
114#define FC_PORTTYPE_MAX_NAMELEN 50
115
a53eb5e0
JS
116/* Reuse fc_port_type enum function for vport_type */
117#define get_fc_vport_type_name get_fc_port_type_name
118
1da177e4 119
84314fd4
JS
120/* Convert fc_host_event_code values to ascii string name */
121static const struct {
122 enum fc_host_event_code value;
123 char *name;
124} fc_host_event_code_names[] = {
125 { FCH_EVT_LIP, "lip" },
126 { FCH_EVT_LINKUP, "link_up" },
127 { FCH_EVT_LINKDOWN, "link_down" },
128 { FCH_EVT_LIPRESET, "lip_reset" },
129 { FCH_EVT_RSCN, "rscn" },
130 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
131 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
132 { FCH_EVT_PORT_ONLINE, "port_online" },
133 { FCH_EVT_PORT_OFFLINE, "port_offline" },
134 { FCH_EVT_PORT_FABRIC, "port_fabric" },
135 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
c39e0af6 136 { FCH_EVT_LINK_FPIN, "link_FPIN" },
84314fd4
JS
137 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
138};
139fc_enum_name_search(host_event_code, fc_host_event_code,
140 fc_host_event_code_names)
141#define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
142
143
1da177e4
LT
144/* Convert fc_port_state values to ascii string name */
145static struct {
146 enum fc_port_state value;
147 char *name;
148} fc_port_state_names[] = {
149 { FC_PORTSTATE_UNKNOWN, "Unknown" },
150 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
151 { FC_PORTSTATE_ONLINE, "Online" },
152 { FC_PORTSTATE_OFFLINE, "Offline" },
153 { FC_PORTSTATE_BLOCKED, "Blocked" },
154 { FC_PORTSTATE_BYPASSED, "Bypassed" },
155 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
156 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
157 { FC_PORTSTATE_ERROR, "Error" },
158 { FC_PORTSTATE_LOOPBACK, "Loopback" },
42e33148 159 { FC_PORTSTATE_DELETED, "Deleted" },
1da177e4
LT
160};
161fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
162#define FC_PORTSTATE_MAX_NAMELEN 20
163
164
a53eb5e0
JS
165/* Convert fc_vport_state values to ascii string name */
166static struct {
167 enum fc_vport_state value;
168 char *name;
169} fc_vport_state_names[] = {
170 { FC_VPORT_UNKNOWN, "Unknown" },
171 { FC_VPORT_ACTIVE, "Active" },
172 { FC_VPORT_DISABLED, "Disabled" },
173 { FC_VPORT_LINKDOWN, "Linkdown" },
174 { FC_VPORT_INITIALIZING, "Initializing" },
175 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
176 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
177 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
178 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
179 { FC_VPORT_FAILED, "VPort Failed" },
180};
181fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
182#define FC_VPORTSTATE_MAX_NAMELEN 24
183
184/* Reuse fc_vport_state enum function for vport_last_state */
185#define get_fc_vport_last_state_name get_fc_vport_state_name
186
187
1da177e4 188/* Convert fc_tgtid_binding_type values to ascii string name */
0ad78200 189static const struct {
1da177e4
LT
190 enum fc_tgtid_binding_type value;
191 char *name;
192 int matchlen;
193} fc_tgtid_binding_type_names[] = {
194 { FC_TGTID_BIND_NONE, "none", 4 },
195 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
196 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
197 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
198};
199fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
200 fc_tgtid_binding_type_names)
201fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
202 fc_tgtid_binding_type_names)
203#define FC_BINDTYPE_MAX_NAMELEN 30
204
205
206#define fc_bitfield_name_search(title, table) \
207static ssize_t \
208get_fc_##title##_names(u32 table_key, char *buf) \
209{ \
210 char *prefix = ""; \
211 ssize_t len = 0; \
212 int i; \
213 \
6391a113 214 for (i = 0; i < ARRAY_SIZE(table); i++) { \
1da177e4
LT
215 if (table[i].value & table_key) { \
216 len += sprintf(buf + len, "%s%s", \
217 prefix, table[i].name); \
218 prefix = ", "; \
219 } \
220 } \
221 len += sprintf(buf + len, "\n"); \
222 return len; \
223}
224
225
226/* Convert FC_COS bit values to ascii string name */
0ad78200 227static const struct {
1da177e4
LT
228 u32 value;
229 char *name;
230} fc_cos_names[] = {
231 { FC_COS_CLASS1, "Class 1" },
232 { FC_COS_CLASS2, "Class 2" },
233 { FC_COS_CLASS3, "Class 3" },
234 { FC_COS_CLASS4, "Class 4" },
235 { FC_COS_CLASS6, "Class 6" },
236};
237fc_bitfield_name_search(cos, fc_cos_names)
238
239
240/* Convert FC_PORTSPEED bit values to ascii string name */
0ad78200 241static const struct {
1da177e4
LT
242 u32 value;
243 char *name;
244} fc_port_speed_names[] = {
245 { FC_PORTSPEED_1GBIT, "1 Gbit" },
246 { FC_PORTSPEED_2GBIT, "2 Gbit" },
247 { FC_PORTSPEED_4GBIT, "4 Gbit" },
248 { FC_PORTSPEED_10GBIT, "10 Gbit" },
c3d2350a
JS
249 { FC_PORTSPEED_8GBIT, "8 Gbit" },
250 { FC_PORTSPEED_16GBIT, "16 Gbit" },
624f28be 251 { FC_PORTSPEED_32GBIT, "32 Gbit" },
c21a2c1a
DK
252 { FC_PORTSPEED_20GBIT, "20 Gbit" },
253 { FC_PORTSPEED_40GBIT, "40 Gbit" },
254 { FC_PORTSPEED_50GBIT, "50 Gbit" },
255 { FC_PORTSPEED_100GBIT, "100 Gbit" },
c749e6bc 256 { FC_PORTSPEED_25GBIT, "25 Gbit" },
cc019a5a
JS
257 { FC_PORTSPEED_64GBIT, "64 Gbit" },
258 { FC_PORTSPEED_128GBIT, "128 Gbit" },
1da177e4
LT
259 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
260};
261fc_bitfield_name_search(port_speed, fc_port_speed_names)
262
263
264static int
265show_fc_fc4s (char *buf, u8 *fc4_list)
266{
267 int i, len=0;
268
269 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
270 len += sprintf(buf + len , "0x%02x ", *fc4_list);
271 len += sprintf(buf + len, "\n");
272 return len;
273}
274
275
a53eb5e0 276/* Convert FC_PORT_ROLE bit values to ascii string name */
0ad78200 277static const struct {
1da177e4
LT
278 u32 value;
279 char *name;
a53eb5e0 280} fc_port_role_names[] = {
0c3ae266
CA
281 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
282 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
283 { FC_PORT_ROLE_IP_PORT, "IP Port" },
284 { FC_PORT_ROLE_FCP_DUMMY_INITIATOR, "FCP Dummy Initiator" },
a6a6d058
HR
285 { FC_PORT_ROLE_NVME_INITIATOR, "NVMe Initiator" },
286 { FC_PORT_ROLE_NVME_TARGET, "NVMe Target" },
287 { FC_PORT_ROLE_NVME_DISCOVERY, "NVMe Discovery" },
1da177e4 288};
a53eb5e0 289fc_bitfield_name_search(port_roles, fc_port_role_names)
1da177e4
LT
290
291/*
292 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
293 */
294#define FC_WELLKNOWN_PORTID_MASK 0xfffff0
295#define FC_WELLKNOWN_ROLE_MASK 0x00000f
296#define FC_FPORT_PORTID 0x00000e
297#define FC_FABCTLR_PORTID 0x00000d
298#define FC_DIRSRVR_PORTID 0x00000c
299#define FC_TIMESRVR_PORTID 0x00000b
300#define FC_MGMTSRVR_PORTID 0x00000a
301
302
c4028958
DH
303static void fc_timeout_deleted_rport(struct work_struct *work);
304static void fc_timeout_fail_rport_io(struct work_struct *work);
305static void fc_scsi_scan_rport(struct work_struct *work);
1da177e4
LT
306
307/*
308 * Attribute counts pre object type...
309 * Increase these values if you add attributes
310 */
311#define FC_STARGET_NUM_ATTRS 3
0f29b966 312#define FC_RPORT_NUM_ATTRS 10
a53eb5e0 313#define FC_VPORT_NUM_ATTRS 9
bb8ef587 314#define FC_HOST_NUM_ATTRS 29
1da177e4
LT
315
316struct fc_internal {
317 struct scsi_transport_template t;
318 struct fc_function_template *f;
319
320 /*
321 * For attributes : each object has :
322 * An array of the actual attributes structures
323 * An array of null-terminated pointers to the attribute
324 * structures - used for mid-layer interaction.
325 *
326 * The attribute containers for the starget and host are are
327 * part of the midlayer. As the remote port is specific to the
328 * fc transport, we must provide the attribute container.
329 */
ee959b00 330 struct device_attribute private_starget_attrs[
1da177e4 331 FC_STARGET_NUM_ATTRS];
ee959b00 332 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
1da177e4 333
ee959b00
TJ
334 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
335 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
1da177e4
LT
336
337 struct transport_container rport_attr_cont;
ee959b00
TJ
338 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
339 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
a53eb5e0
JS
340
341 struct transport_container vport_attr_cont;
ee959b00
TJ
342 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
343 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
1da177e4
LT
344};
345
346#define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
347
d0a7e574 348static int fc_target_setup(struct transport_container *tc, struct device *dev,
ee959b00 349 struct device *cdev)
1da177e4
LT
350{
351 struct scsi_target *starget = to_scsi_target(dev);
352 struct fc_rport *rport = starget_to_rport(starget);
353
354 /*
355 * if parent is remote port, use values from remote port.
356 * Otherwise, this host uses the fc_transport, but not the
357 * remote port interface. As such, initialize to known non-values.
358 */
359 if (rport) {
360 fc_starget_node_name(starget) = rport->node_name;
361 fc_starget_port_name(starget) = rport->port_name;
362 fc_starget_port_id(starget) = rport->port_id;
363 } else {
364 fc_starget_node_name(starget) = -1;
365 fc_starget_port_name(starget) = -1;
366 fc_starget_port_id(starget) = -1;
367 }
368
369 return 0;
370}
371
372static DECLARE_TRANSPORT_CLASS(fc_transport_class,
373 "fc_transport",
374 fc_target_setup,
375 NULL,
376 NULL);
377
d0a7e574 378static int fc_host_setup(struct transport_container *tc, struct device *dev,
ee959b00 379 struct device *cdev)
1da177e4
LT
380{
381 struct Scsi_Host *shost = dev_to_shost(dev);
aedf3497 382 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4 383
9ef3e4a4 384 /*
1da177e4
LT
385 * Set default values easily detected by the midlayer as
386 * failure cases. The scsi lldd is responsible for initializing
387 * all transport attributes to valid values per host.
388 */
aedf3497
JS
389 fc_host->node_name = -1;
390 fc_host->port_name = -1;
391 fc_host->permanent_port_name = -1;
392 fc_host->supported_classes = FC_COS_UNSPECIFIED;
393 memset(fc_host->supported_fc4s, 0,
394 sizeof(fc_host->supported_fc4s));
aedf3497
JS
395 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
396 fc_host->maxframe_size = -1;
a53eb5e0 397 fc_host->max_npiv_vports = 0;
aedf3497
JS
398 memset(fc_host->serial_number, 0,
399 sizeof(fc_host->serial_number));
bb8ef587
NP
400 memset(fc_host->manufacturer, 0,
401 sizeof(fc_host->manufacturer));
402 memset(fc_host->model, 0,
403 sizeof(fc_host->model));
404 memset(fc_host->model_description, 0,
405 sizeof(fc_host->model_description));
406 memset(fc_host->hardware_version, 0,
407 sizeof(fc_host->hardware_version));
408 memset(fc_host->driver_version, 0,
409 sizeof(fc_host->driver_version));
410 memset(fc_host->firmware_version, 0,
411 sizeof(fc_host->firmware_version));
412 memset(fc_host->optionrom_version, 0,
413 sizeof(fc_host->optionrom_version));
aedf3497
JS
414
415 fc_host->port_id = -1;
416 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
417 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
418 memset(fc_host->active_fc4s, 0,
419 sizeof(fc_host->active_fc4s));
420 fc_host->speed = FC_PORTSPEED_UNKNOWN;
421 fc_host->fabric_name = -1;
b8d08210
JS
422 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
423 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
aedf3497
JS
424
425 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
426
427 INIT_LIST_HEAD(&fc_host->rports);
428 INIT_LIST_HEAD(&fc_host->rport_bindings);
a53eb5e0 429 INIT_LIST_HEAD(&fc_host->vports);
aedf3497
JS
430 fc_host->next_rport_number = 0;
431 fc_host->next_target_id = 0;
a53eb5e0
JS
432 fc_host->next_vport_number = 0;
433 fc_host->npiv_vports_inuse = 0;
aedf3497 434
aab0de24
KS
435 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
436 "fc_wq_%d", shost->host_no);
d8537548 437 fc_host->work_q = alloc_workqueue("%s", 0, 0, fc_host->work_q_name);
aedf3497
JS
438 if (!fc_host->work_q)
439 return -ENOMEM;
440
43ca910a 441 fc_host->dev_loss_tmo = fc_dev_loss_tmo;
aab0de24
KS
442 snprintf(fc_host->devloss_work_q_name,
443 sizeof(fc_host->devloss_work_q_name),
444 "fc_dl_%d", shost->host_no);
d8537548
KC
445 fc_host->devloss_work_q = alloc_workqueue("%s", 0, 0,
446 fc_host->devloss_work_q_name);
aedf3497
JS
447 if (!fc_host->devloss_work_q) {
448 destroy_workqueue(fc_host->work_q);
449 fc_host->work_q = NULL;
450 return -ENOMEM;
451 }
452
9e4f5e29
JS
453 fc_bsg_hostadd(shost, fc_host);
454 /* ignore any bsg add error - we just can't do sgio */
455
456 return 0;
457}
458
459static int fc_host_remove(struct transport_container *tc, struct device *dev,
460 struct device *cdev)
461{
462 struct Scsi_Host *shost = dev_to_shost(dev);
463 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
464
465 fc_bsg_remove(fc_host->rqst_q);
1da177e4
LT
466 return 0;
467}
468
469static DECLARE_TRANSPORT_CLASS(fc_host_class,
470 "fc_host",
471 fc_host_setup,
9e4f5e29 472 fc_host_remove,
1da177e4
LT
473 NULL);
474
475/*
476 * Setup and Remove actions for remote ports are handled
477 * in the service functions below.
478 */
479static DECLARE_TRANSPORT_CLASS(fc_rport_class,
480 "fc_remote_ports",
481 NULL,
482 NULL,
483 NULL);
484
a53eb5e0
JS
485/*
486 * Setup and Remove actions for virtual ports are handled
487 * in the service functions below.
488 */
489static DECLARE_TRANSPORT_CLASS(fc_vport_class,
490 "fc_vports",
491 NULL,
492 NULL,
493 NULL);
494
eb44820c 495/*
84314fd4 496 * Netlink Infrastructure
eb44820c 497 */
84314fd4
JS
498
499static atomic_t fc_event_seq;
500
501/**
502 * fc_get_event_number - Obtain the next sequential FC event number
503 *
504 * Notes:
eb44820c 505 * We could have inlined this, but it would have required fc_event_seq to
84314fd4
JS
506 * be exposed. For now, live with the subroutine call.
507 * Atomic used to avoid lock/unlock...
eb44820c 508 */
84314fd4
JS
509u32
510fc_get_event_number(void)
511{
512 return atomic_add_return(1, &fc_event_seq);
513}
514EXPORT_SYMBOL(fc_get_event_number);
515
84314fd4 516/**
2b1be558
JS
517 * fc_host_post_fc_event - routine to do the work of posting an event
518 * on an fc_host.
84314fd4
JS
519 * @shost: host the event occurred on
520 * @event_number: fc event number obtained from get_fc_event_number()
521 * @event_code: fc_host event being posted
2b1be558
JS
522 * @data_len: amount, in bytes, of event data
523 * @data_buf: pointer to event data
524 * @vendor_id: value for Vendor id
84314fd4
JS
525 *
526 * Notes:
527 * This routine assumes no locks are held on entry.
eb44820c 528 */
84314fd4 529void
2b1be558
JS
530fc_host_post_fc_event(struct Scsi_Host *shost, u32 event_number,
531 enum fc_host_event_code event_code,
532 u32 data_len, char *data_buf, u64 vendor_id)
84314fd4
JS
533{
534 struct sk_buff *skb;
535 struct nlmsghdr *nlh;
536 struct fc_nl_event *event;
537 const char *name;
e07ebea0 538 u32 len;
84314fd4
JS
539 int err;
540
2b1be558
JS
541 if (!data_buf || data_len < 4)
542 data_len = 0;
543
84314fd4
JS
544 if (!scsi_nl_sock) {
545 err = -ENOENT;
546 goto send_fail;
547 }
548
2b1be558 549 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
84314fd4 550
e07ebea0 551 skb = nlmsg_new(len, GFP_KERNEL);
84314fd4
JS
552 if (!skb) {
553 err = -ENOBUFS;
554 goto send_fail;
555 }
556
e07ebea0 557 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
84314fd4
JS
558 if (!nlh) {
559 err = -ENOBUFS;
560 goto send_fail_skb;
561 }
e07ebea0 562 event = nlmsg_data(nlh);
84314fd4
JS
563
564 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
565 FC_NL_ASYNC_EVENT, len);
fe7f4e5d 566 event->seconds = ktime_get_real_seconds();
2b1be558 567 event->vendor_id = vendor_id;
84314fd4 568 event->host_no = shost->host_no;
2b1be558 569 event->event_datalen = data_len; /* bytes */
84314fd4
JS
570 event->event_num = event_number;
571 event->event_code = event_code;
2b1be558
JS
572 if (data_len)
573 memcpy(&event->event_data, data_buf, data_len);
84314fd4 574
ff491a73
PNA
575 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
576 GFP_KERNEL);
84314fd4
JS
577 return;
578
579send_fail_skb:
580 kfree_skb(skb);
581send_fail:
582 name = get_fc_host_event_code_name(event_code);
583 printk(KERN_WARNING
584 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
cadbd4a5 585 __func__, shost->host_no,
2b1be558
JS
586 (name) ? name : "<unknown>",
587 (data_len) ? *((u32 *)data_buf) : 0xFFFFFFFF, err);
84314fd4
JS
588 return;
589}
2b1be558
JS
590EXPORT_SYMBOL(fc_host_post_fc_event);
591
592/**
593 * fc_host_post_event - called to post an even on an fc_host.
594 * @shost: host the event occurred on
595 * @event_number: fc event number obtained from get_fc_event_number()
596 * @event_code: fc_host event being posted
597 * @event_data: 32bits of data for the event being posted
598 *
599 * Notes:
600 * This routine assumes no locks are held on entry.
601 */
602void
603fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
604 enum fc_host_event_code event_code, u32 event_data)
605{
606 fc_host_post_fc_event(shost, event_number, event_code,
607 (u32)sizeof(u32), (char *)&event_data, 0);
608}
84314fd4
JS
609EXPORT_SYMBOL(fc_host_post_event);
610
611
612/**
2b1be558
JS
613 * fc_host_post_vendor_event - called to post a vendor unique event
614 * on an fc_host
84314fd4
JS
615 * @shost: host the event occurred on
616 * @event_number: fc event number obtained from get_fc_event_number()
617 * @data_len: amount, in bytes, of vendor unique data
618 * @data_buf: pointer to vendor unique data
eb44820c 619 * @vendor_id: Vendor id
84314fd4
JS
620 *
621 * Notes:
622 * This routine assumes no locks are held on entry.
eb44820c 623 */
84314fd4
JS
624void
625fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
f14e2e29 626 u32 data_len, char * data_buf, u64 vendor_id)
84314fd4 627{
2b1be558
JS
628 fc_host_post_fc_event(shost, event_number, FCH_EVT_VENDOR_UNIQUE,
629 data_len, data_buf, vendor_id);
84314fd4
JS
630}
631EXPORT_SYMBOL(fc_host_post_vendor_event);
632
c39e0af6
JS
633/**
634 * fc_host_rcv_fpin - routine to process a received FPIN.
635 * @shost: host the FPIN was received on
636 * @fpin_len: length of FPIN payload, in bytes
637 * @fpin_buf: pointer to FPIN payload
638 *
639 * Notes:
640 * This routine assumes no locks are held on entry.
641 */
642void
643fc_host_fpin_rcv(struct Scsi_Host *shost, u32 fpin_len, char *fpin_buf)
644{
645 fc_host_post_fc_event(shost, fc_get_event_number(),
646 FCH_EVT_LINK_FPIN, fpin_len, fpin_buf, 0);
647}
648EXPORT_SYMBOL(fc_host_fpin_rcv);
649
84314fd4 650
1da177e4
LT
651static __init int fc_transport_init(void)
652{
84314fd4
JS
653 int error;
654
655 atomic_set(&fc_event_seq, 0);
656
657 error = transport_class_register(&fc_host_class);
a53eb5e0
JS
658 if (error)
659 return error;
660 error = transport_class_register(&fc_vport_class);
1da177e4 661 if (error)
48de68a4 662 goto unreg_host_class;
1da177e4
LT
663 error = transport_class_register(&fc_rport_class);
664 if (error)
48de68a4
MC
665 goto unreg_vport_class;
666 error = transport_class_register(&fc_transport_class);
667 if (error)
668 goto unreg_rport_class;
669 return 0;
670
671unreg_rport_class:
672 transport_class_unregister(&fc_rport_class);
673unreg_vport_class:
674 transport_class_unregister(&fc_vport_class);
675unreg_host_class:
676 transport_class_unregister(&fc_host_class);
677 return error;
1da177e4
LT
678}
679
680static void __exit fc_transport_exit(void)
681{
682 transport_class_unregister(&fc_transport_class);
683 transport_class_unregister(&fc_rport_class);
684 transport_class_unregister(&fc_host_class);
a53eb5e0 685 transport_class_unregister(&fc_vport_class);
1da177e4
LT
686}
687
688/*
689 * FC Remote Port Attribute Management
690 */
691
692#define fc_rport_show_function(field, format_string, sz, cast) \
693static ssize_t \
ee959b00
TJ
694show_fc_rport_##field (struct device *dev, \
695 struct device_attribute *attr, char *buf) \
1da177e4 696{ \
ee959b00 697 struct fc_rport *rport = transport_class_to_rport(dev); \
1da177e4
LT
698 struct Scsi_Host *shost = rport_to_shost(rport); \
699 struct fc_internal *i = to_fc_internal(shost->transportt); \
19a7b4ae
JSEC
700 if ((i->f->get_rport_##field) && \
701 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
42e33148 702 (rport->port_state == FC_PORTSTATE_DELETED) || \
19a7b4ae 703 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
1da177e4
LT
704 i->f->get_rport_##field(rport); \
705 return snprintf(buf, sz, format_string, cast rport->field); \
706}
707
708#define fc_rport_store_function(field) \
709static ssize_t \
ee959b00
TJ
710store_fc_rport_##field(struct device *dev, \
711 struct device_attribute *attr, \
712 const char *buf, size_t count) \
1da177e4
LT
713{ \
714 int val; \
ee959b00 715 struct fc_rport *rport = transport_class_to_rport(dev); \
1da177e4
LT
716 struct Scsi_Host *shost = rport_to_shost(rport); \
717 struct fc_internal *i = to_fc_internal(shost->transportt); \
0f29b966 718 char *cp; \
19a7b4ae 719 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
42e33148 720 (rport->port_state == FC_PORTSTATE_DELETED) || \
19a7b4ae
JSEC
721 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
722 return -EBUSY; \
0f29b966
JS
723 val = simple_strtoul(buf, &cp, 0); \
724 if (*cp && (*cp != '\n')) \
725 return -EINVAL; \
1da177e4
LT
726 i->f->set_rport_##field(rport, val); \
727 return count; \
728}
729
730#define fc_rport_rd_attr(field, format_string, sz) \
731 fc_rport_show_function(field, format_string, sz, ) \
ee959b00 732static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
1da177e4
LT
733 show_fc_rport_##field, NULL)
734
735#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
736 fc_rport_show_function(field, format_string, sz, (cast)) \
ee959b00 737static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
1da177e4
LT
738 show_fc_rport_##field, NULL)
739
740#define fc_rport_rw_attr(field, format_string, sz) \
741 fc_rport_show_function(field, format_string, sz, ) \
742 fc_rport_store_function(field) \
ee959b00 743static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
1da177e4
LT
744 show_fc_rport_##field, \
745 store_fc_rport_##field)
746
747
748#define fc_private_rport_show_function(field, format_string, sz, cast) \
749static ssize_t \
ee959b00
TJ
750show_fc_rport_##field (struct device *dev, \
751 struct device_attribute *attr, char *buf) \
1da177e4 752{ \
ee959b00 753 struct fc_rport *rport = transport_class_to_rport(dev); \
1da177e4
LT
754 return snprintf(buf, sz, format_string, cast rport->field); \
755}
756
757#define fc_private_rport_rd_attr(field, format_string, sz) \
758 fc_private_rport_show_function(field, format_string, sz, ) \
ee959b00 759static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
1da177e4
LT
760 show_fc_rport_##field, NULL)
761
762#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
763 fc_private_rport_show_function(field, format_string, sz, (cast)) \
ee959b00 764static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
1da177e4
LT
765 show_fc_rport_##field, NULL)
766
767
768#define fc_private_rport_rd_enum_attr(title, maxlen) \
769static ssize_t \
ee959b00
TJ
770show_fc_rport_##title (struct device *dev, \
771 struct device_attribute *attr, char *buf) \
1da177e4 772{ \
ee959b00 773 struct fc_rport *rport = transport_class_to_rport(dev); \
1da177e4
LT
774 const char *name; \
775 name = get_fc_##title##_name(rport->title); \
776 if (!name) \
777 return -EINVAL; \
778 return snprintf(buf, maxlen, "%s\n", name); \
779} \
ee959b00 780static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
1da177e4
LT
781 show_fc_rport_##title, NULL)
782
783
784#define SETUP_RPORT_ATTRIBUTE_RD(field) \
ee959b00 785 i->private_rport_attrs[count] = device_attr_rport_##field; \
1da177e4
LT
786 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
787 i->private_rport_attrs[count].store = NULL; \
788 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
789 if (i->f->show_rport_##field) \
790 count++
791
792#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
ee959b00 793 i->private_rport_attrs[count] = device_attr_rport_##field; \
1da177e4
LT
794 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
795 i->private_rport_attrs[count].store = NULL; \
796 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
797 count++
798
799#define SETUP_RPORT_ATTRIBUTE_RW(field) \
ee959b00 800 i->private_rport_attrs[count] = device_attr_rport_##field; \
1da177e4
LT
801 if (!i->f->set_rport_##field) { \
802 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
803 i->private_rport_attrs[count].store = NULL; \
804 } \
805 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
806 if (i->f->show_rport_##field) \
807 count++
808
0f29b966
JS
809#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
810{ \
ee959b00 811 i->private_rport_attrs[count] = device_attr_rport_##field; \
0f29b966
JS
812 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
813 count++; \
814}
815
1da177e4
LT
816
817/* The FC Transport Remote Port Attributes: */
818
819/* Fixed Remote Port Attributes */
820
821fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
822
823static ssize_t
ee959b00
TJ
824show_fc_rport_supported_classes (struct device *dev,
825 struct device_attribute *attr, char *buf)
1da177e4 826{
ee959b00 827 struct fc_rport *rport = transport_class_to_rport(dev);
1da177e4
LT
828 if (rport->supported_classes == FC_COS_UNSPECIFIED)
829 return snprintf(buf, 20, "unspecified\n");
830 return get_fc_cos_names(rport->supported_classes, buf);
831}
ee959b00 832static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
1da177e4
LT
833 show_fc_rport_supported_classes, NULL);
834
835/* Dynamic Remote Port Attributes */
836
19a7b4ae
JSEC
837/*
838 * dev_loss_tmo attribute
839 */
43ca910a
MC
840static int fc_str_to_dev_loss(const char *buf, unsigned long *val)
841{
842 char *cp;
843
844 *val = simple_strtoul(buf, &cp, 0);
eea42270 845 if (*cp && (*cp != '\n'))
43ca910a
MC
846 return -EINVAL;
847 /*
848 * Check for overflow; dev_loss_tmo is u32
849 */
850 if (*val > UINT_MAX)
851 return -EINVAL;
852
853 return 0;
854}
855
856static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
857 unsigned long val)
19a7b4ae 858{
19a7b4ae
JSEC
859 struct Scsi_Host *shost = rport_to_shost(rport);
860 struct fc_internal *i = to_fc_internal(shost->transportt);
43ca910a 861
19a7b4ae 862 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
42e33148 863 (rport->port_state == FC_PORTSTATE_DELETED) ||
19a7b4ae
JSEC
864 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
865 return -EBUSY;
36dd288f
HR
866 /*
867 * Check for overflow; dev_loss_tmo is u32
868 */
869 if (val > UINT_MAX)
870 return -EINVAL;
871
f2818663
HR
872 /*
873 * If fast_io_fail is off we have to cap
874 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
875 */
876 if (rport->fast_io_fail_tmo == -1 &&
877 val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
878 return -EINVAL;
879
19a7b4ae 880 i->f->set_rport_dev_loss_tmo(rport, val);
43ca910a
MC
881 return 0;
882}
883
884fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
885static ssize_t
886store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
887 const char *buf, size_t count)
888{
889 struct fc_rport *rport = transport_class_to_rport(dev);
890 unsigned long val;
891 int rc;
892
893 rc = fc_str_to_dev_loss(buf, &val);
894 if (rc)
895 return rc;
896
897 rc = fc_rport_set_dev_loss_tmo(rport, val);
898 if (rc)
899 return rc;
19a7b4ae
JSEC
900 return count;
901}
ee959b00 902static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
19a7b4ae 903 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
1da177e4
LT
904
905
906/* Private Remote Port Attributes */
907
908fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
909fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
910fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
911
912static ssize_t
ee959b00
TJ
913show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
914 char *buf)
1da177e4 915{
ee959b00 916 struct fc_rport *rport = transport_class_to_rport(dev);
1da177e4
LT
917
918 /* identify any roles that are port_id specific */
919 if ((rport->port_id != -1) &&
920 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
921 FC_WELLKNOWN_PORTID_MASK) {
922 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
923 case FC_FPORT_PORTID:
924 return snprintf(buf, 30, "Fabric Port\n");
925 case FC_FABCTLR_PORTID:
926 return snprintf(buf, 30, "Fabric Controller\n");
927 case FC_DIRSRVR_PORTID:
928 return snprintf(buf, 30, "Directory Server\n");
929 case FC_TIMESRVR_PORTID:
930 return snprintf(buf, 30, "Time Server\n");
931 case FC_MGMTSRVR_PORTID:
932 return snprintf(buf, 30, "Management Server\n");
933 default:
934 return snprintf(buf, 30, "Unknown Fabric Entity\n");
935 }
936 } else {
a53eb5e0 937 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
1da177e4 938 return snprintf(buf, 20, "unknown\n");
a53eb5e0 939 return get_fc_port_roles_names(rport->roles, buf);
1da177e4
LT
940 }
941}
ee959b00 942static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
1da177e4
LT
943 show_fc_rport_roles, NULL);
944
945fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
946fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
947
0f29b966
JS
948/*
949 * fast_io_fail_tmo attribute
950 */
951static ssize_t
ee959b00
TJ
952show_fc_rport_fast_io_fail_tmo (struct device *dev,
953 struct device_attribute *attr, char *buf)
0f29b966 954{
ee959b00 955 struct fc_rport *rport = transport_class_to_rport(dev);
0f29b966
JS
956
957 if (rport->fast_io_fail_tmo == -1)
958 return snprintf(buf, 5, "off\n");
959 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
960}
961
962static ssize_t
ee959b00
TJ
963store_fc_rport_fast_io_fail_tmo(struct device *dev,
964 struct device_attribute *attr, const char *buf,
965 size_t count)
0f29b966
JS
966{
967 int val;
968 char *cp;
ee959b00 969 struct fc_rport *rport = transport_class_to_rport(dev);
0f29b966
JS
970
971 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
972 (rport->port_state == FC_PORTSTATE_DELETED) ||
973 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
974 return -EBUSY;
975 if (strncmp(buf, "off", 3) == 0)
976 rport->fast_io_fail_tmo = -1;
977 else {
978 val = simple_strtoul(buf, &cp, 0);
f2818663
HR
979 if ((*cp && (*cp != '\n')) || (val < 0))
980 return -EINVAL;
981 /*
982 * Cap fast_io_fail by dev_loss_tmo or
983 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
984 */
985 if ((val >= rport->dev_loss_tmo) ||
986 (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
0f29b966 987 return -EINVAL;
f2818663 988
0f29b966
JS
989 rport->fast_io_fail_tmo = val;
990 }
991 return count;
992}
ee959b00 993static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
0f29b966 994 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
1da177e4
LT
995
996
997/*
998 * FC SCSI Target Attribute Management
999 */
1000
1001/*
1002 * Note: in the target show function we recognize when the remote
732bee7a 1003 * port is in the hierarchy and do not allow the driver to get
1da177e4
LT
1004 * involved in sysfs functions. The driver only gets involved if
1005 * it's the "old" style that doesn't use rports.
1006 */
1007#define fc_starget_show_function(field, format_string, sz, cast) \
1008static ssize_t \
ee959b00
TJ
1009show_fc_starget_##field (struct device *dev, \
1010 struct device_attribute *attr, char *buf) \
1da177e4 1011{ \
ee959b00 1012 struct scsi_target *starget = transport_class_to_starget(dev); \
1da177e4
LT
1013 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
1014 struct fc_internal *i = to_fc_internal(shost->transportt); \
1015 struct fc_rport *rport = starget_to_rport(starget); \
1016 if (rport) \
1017 fc_starget_##field(starget) = rport->field; \
1018 else if (i->f->get_starget_##field) \
1019 i->f->get_starget_##field(starget); \
1020 return snprintf(buf, sz, format_string, \
1021 cast fc_starget_##field(starget)); \
1022}
1023
1024#define fc_starget_rd_attr(field, format_string, sz) \
1025 fc_starget_show_function(field, format_string, sz, ) \
ee959b00 1026static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
1da177e4
LT
1027 show_fc_starget_##field, NULL)
1028
1029#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
1030 fc_starget_show_function(field, format_string, sz, (cast)) \
ee959b00 1031static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
1da177e4
LT
1032 show_fc_starget_##field, NULL)
1033
1034#define SETUP_STARGET_ATTRIBUTE_RD(field) \
ee959b00 1035 i->private_starget_attrs[count] = device_attr_starget_##field; \
1da177e4
LT
1036 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1037 i->private_starget_attrs[count].store = NULL; \
1038 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1039 if (i->f->show_starget_##field) \
1040 count++
1041
1042#define SETUP_STARGET_ATTRIBUTE_RW(field) \
ee959b00 1043 i->private_starget_attrs[count] = device_attr_starget_##field; \
1da177e4
LT
1044 if (!i->f->set_starget_##field) { \
1045 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1046 i->private_starget_attrs[count].store = NULL; \
1047 } \
1048 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1049 if (i->f->show_starget_##field) \
1050 count++
1051
1052/* The FC Transport SCSI Target Attributes: */
1053fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1054fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1055fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1056
1057
a53eb5e0
JS
1058/*
1059 * FC Virtual Port Attribute Management
1060 */
1061
1062#define fc_vport_show_function(field, format_string, sz, cast) \
1063static ssize_t \
ee959b00
TJ
1064show_fc_vport_##field (struct device *dev, \
1065 struct device_attribute *attr, char *buf) \
a53eb5e0 1066{ \
ee959b00 1067 struct fc_vport *vport = transport_class_to_vport(dev); \
a53eb5e0
JS
1068 struct Scsi_Host *shost = vport_to_shost(vport); \
1069 struct fc_internal *i = to_fc_internal(shost->transportt); \
1070 if ((i->f->get_vport_##field) && \
1071 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1072 i->f->get_vport_##field(vport); \
1073 return snprintf(buf, sz, format_string, cast vport->field); \
1074}
1075
1076#define fc_vport_store_function(field) \
1077static ssize_t \
ee959b00
TJ
1078store_fc_vport_##field(struct device *dev, \
1079 struct device_attribute *attr, \
1080 const char *buf, size_t count) \
a53eb5e0
JS
1081{ \
1082 int val; \
ee959b00 1083 struct fc_vport *vport = transport_class_to_vport(dev); \
a53eb5e0
JS
1084 struct Scsi_Host *shost = vport_to_shost(vport); \
1085 struct fc_internal *i = to_fc_internal(shost->transportt); \
1086 char *cp; \
1087 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1088 return -EBUSY; \
1089 val = simple_strtoul(buf, &cp, 0); \
1090 if (*cp && (*cp != '\n')) \
1091 return -EINVAL; \
1092 i->f->set_vport_##field(vport, val); \
1093 return count; \
1094}
1095
1096#define fc_vport_store_str_function(field, slen) \
1097static ssize_t \
ee959b00
TJ
1098store_fc_vport_##field(struct device *dev, \
1099 struct device_attribute *attr, \
1100 const char *buf, size_t count) \
a53eb5e0 1101{ \
ee959b00 1102 struct fc_vport *vport = transport_class_to_vport(dev); \
a53eb5e0
JS
1103 struct Scsi_Host *shost = vport_to_shost(vport); \
1104 struct fc_internal *i = to_fc_internal(shost->transportt); \
1105 unsigned int cnt=count; \
1106 \
1107 /* count may include a LF at end of string */ \
1108 if (buf[cnt-1] == '\n') \
1109 cnt--; \
1110 if (cnt > ((slen) - 1)) \
1111 return -EINVAL; \
1112 memcpy(vport->field, buf, cnt); \
1113 i->f->set_vport_##field(vport); \
1114 return count; \
1115}
1116
1117#define fc_vport_rd_attr(field, format_string, sz) \
1118 fc_vport_show_function(field, format_string, sz, ) \
ee959b00 1119static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
a53eb5e0
JS
1120 show_fc_vport_##field, NULL)
1121
1122#define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1123 fc_vport_show_function(field, format_string, sz, (cast)) \
ee959b00 1124static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
a53eb5e0
JS
1125 show_fc_vport_##field, NULL)
1126
1127#define fc_vport_rw_attr(field, format_string, sz) \
1128 fc_vport_show_function(field, format_string, sz, ) \
1129 fc_vport_store_function(field) \
ee959b00 1130static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
a53eb5e0
JS
1131 show_fc_vport_##field, \
1132 store_fc_vport_##field)
1133
1134#define fc_private_vport_show_function(field, format_string, sz, cast) \
1135static ssize_t \
ee959b00
TJ
1136show_fc_vport_##field (struct device *dev, \
1137 struct device_attribute *attr, char *buf) \
a53eb5e0 1138{ \
ee959b00 1139 struct fc_vport *vport = transport_class_to_vport(dev); \
a53eb5e0
JS
1140 return snprintf(buf, sz, format_string, cast vport->field); \
1141}
1142
1143#define fc_private_vport_store_u32_function(field) \
1144static ssize_t \
ee959b00
TJ
1145store_fc_vport_##field(struct device *dev, \
1146 struct device_attribute *attr, \
1147 const char *buf, size_t count) \
a53eb5e0
JS
1148{ \
1149 u32 val; \
ee959b00 1150 struct fc_vport *vport = transport_class_to_vport(dev); \
a53eb5e0
JS
1151 char *cp; \
1152 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1153 return -EBUSY; \
1154 val = simple_strtoul(buf, &cp, 0); \
1155 if (*cp && (*cp != '\n')) \
1156 return -EINVAL; \
1157 vport->field = val; \
1158 return count; \
1159}
1160
1161
1162#define fc_private_vport_rd_attr(field, format_string, sz) \
1163 fc_private_vport_show_function(field, format_string, sz, ) \
ee959b00 1164static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
a53eb5e0
JS
1165 show_fc_vport_##field, NULL)
1166
1167#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1168 fc_private_vport_show_function(field, format_string, sz, (cast)) \
ee959b00 1169static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
a53eb5e0
JS
1170 show_fc_vport_##field, NULL)
1171
1172#define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1173 fc_private_vport_show_function(field, format_string, sz, ) \
1174 fc_private_vport_store_u32_function(field) \
ee959b00 1175static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
a53eb5e0
JS
1176 show_fc_vport_##field, \
1177 store_fc_vport_##field)
1178
1179
1180#define fc_private_vport_rd_enum_attr(title, maxlen) \
1181static ssize_t \
ee959b00
TJ
1182show_fc_vport_##title (struct device *dev, \
1183 struct device_attribute *attr, \
1184 char *buf) \
a53eb5e0 1185{ \
ee959b00 1186 struct fc_vport *vport = transport_class_to_vport(dev); \
a53eb5e0
JS
1187 const char *name; \
1188 name = get_fc_##title##_name(vport->title); \
1189 if (!name) \
1190 return -EINVAL; \
1191 return snprintf(buf, maxlen, "%s\n", name); \
1192} \
ee959b00 1193static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
a53eb5e0
JS
1194 show_fc_vport_##title, NULL)
1195
1196
1197#define SETUP_VPORT_ATTRIBUTE_RD(field) \
ee959b00 1198 i->private_vport_attrs[count] = device_attr_vport_##field; \
a53eb5e0
JS
1199 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1200 i->private_vport_attrs[count].store = NULL; \
1201 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1202 if (i->f->get_##field) \
1203 count++
1204 /* NOTE: Above MACRO differs: checks function not show bit */
1205
1206#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
ee959b00 1207 i->private_vport_attrs[count] = device_attr_vport_##field; \
a53eb5e0
JS
1208 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1209 i->private_vport_attrs[count].store = NULL; \
1210 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1211 count++
1212
1213#define SETUP_VPORT_ATTRIBUTE_WR(field) \
ee959b00 1214 i->private_vport_attrs[count] = device_attr_vport_##field; \
a53eb5e0
JS
1215 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1216 if (i->f->field) \
1217 count++
1218 /* NOTE: Above MACRO differs: checks function */
1219
1220#define SETUP_VPORT_ATTRIBUTE_RW(field) \
ee959b00 1221 i->private_vport_attrs[count] = device_attr_vport_##field; \
a53eb5e0
JS
1222 if (!i->f->set_vport_##field) { \
1223 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1224 i->private_vport_attrs[count].store = NULL; \
1225 } \
1226 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1227 count++
1228 /* NOTE: Above MACRO differs: does not check show bit */
1229
1230#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1231{ \
ee959b00 1232 i->private_vport_attrs[count] = device_attr_vport_##field; \
a53eb5e0
JS
1233 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1234 count++; \
1235}
1236
1237
1238/* The FC Transport Virtual Port Attributes: */
1239
1240/* Fixed Virtual Port Attributes */
1241
1242/* Dynamic Virtual Port Attributes */
1243
1244/* Private Virtual Port Attributes */
1245
1246fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1247fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1248fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1249fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1250
1251static ssize_t
ee959b00
TJ
1252show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1253 char *buf)
a53eb5e0 1254{
ee959b00 1255 struct fc_vport *vport = transport_class_to_vport(dev);
a53eb5e0
JS
1256
1257 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1258 return snprintf(buf, 20, "unknown\n");
1259 return get_fc_port_roles_names(vport->roles, buf);
1260}
ee959b00 1261static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
a53eb5e0
JS
1262
1263fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1264
1265fc_private_vport_show_function(symbolic_name, "%s\n",
1266 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1267fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
ee959b00 1268static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
a53eb5e0
JS
1269 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1270
1271static ssize_t
ee959b00
TJ
1272store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1273 const char *buf, size_t count)
a53eb5e0 1274{
ee959b00 1275 struct fc_vport *vport = transport_class_to_vport(dev);
9ef3e4a4 1276 struct Scsi_Host *shost = vport_to_shost(vport);
0d9dc7c8
GR
1277 unsigned long flags;
1278
1279 spin_lock_irqsave(shost->host_lock, flags);
260f4aed 1280 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING | FC_VPORT_DELETING)) {
0d9dc7c8
GR
1281 spin_unlock_irqrestore(shost->host_lock, flags);
1282 return -EBUSY;
1283 }
1284 vport->flags |= FC_VPORT_DELETING;
1285 spin_unlock_irqrestore(shost->host_lock, flags);
a53eb5e0 1286
9ef3e4a4 1287 fc_queue_work(shost, &vport->vport_delete_work);
a53eb5e0
JS
1288 return count;
1289}
ee959b00 1290static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
a53eb5e0
JS
1291 NULL, store_fc_vport_delete);
1292
1293
1294/*
1295 * Enable/Disable vport
1296 * Write "1" to disable, write "0" to enable
1297 */
1298static ssize_t
ee959b00
TJ
1299store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1300 const char *buf,
a53eb5e0
JS
1301 size_t count)
1302{
ee959b00 1303 struct fc_vport *vport = transport_class_to_vport(dev);
a53eb5e0
JS
1304 struct Scsi_Host *shost = vport_to_shost(vport);
1305 struct fc_internal *i = to_fc_internal(shost->transportt);
1306 int stat;
1307
1308 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1309 return -EBUSY;
1310
1311 if (*buf == '0') {
1312 if (vport->vport_state != FC_VPORT_DISABLED)
1313 return -EALREADY;
1314 } else if (*buf == '1') {
1315 if (vport->vport_state == FC_VPORT_DISABLED)
1316 return -EALREADY;
1317 } else
1318 return -EINVAL;
1319
1320 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1321 return stat ? stat : count;
1322}
ee959b00 1323static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
a53eb5e0
JS
1324 NULL, store_fc_vport_disable);
1325
1326
1da177e4
LT
1327/*
1328 * Host Attribute Management
1329 */
1330
1331#define fc_host_show_function(field, format_string, sz, cast) \
1332static ssize_t \
ee959b00
TJ
1333show_fc_host_##field (struct device *dev, \
1334 struct device_attribute *attr, char *buf) \
1da177e4 1335{ \
ee959b00 1336 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1da177e4
LT
1337 struct fc_internal *i = to_fc_internal(shost->transportt); \
1338 if (i->f->get_host_##field) \
1339 i->f->get_host_##field(shost); \
1340 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1341}
1342
1343#define fc_host_store_function(field) \
1344static ssize_t \
ee959b00
TJ
1345store_fc_host_##field(struct device *dev, \
1346 struct device_attribute *attr, \
1347 const char *buf, size_t count) \
1da177e4
LT
1348{ \
1349 int val; \
ee959b00 1350 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1da177e4 1351 struct fc_internal *i = to_fc_internal(shost->transportt); \
0f29b966 1352 char *cp; \
1da177e4 1353 \
0f29b966
JS
1354 val = simple_strtoul(buf, &cp, 0); \
1355 if (*cp && (*cp != '\n')) \
1356 return -EINVAL; \
1da177e4
LT
1357 i->f->set_host_##field(shost, val); \
1358 return count; \
1359}
1360
b8d08210
JS
1361#define fc_host_store_str_function(field, slen) \
1362static ssize_t \
ee959b00
TJ
1363store_fc_host_##field(struct device *dev, \
1364 struct device_attribute *attr, \
1365 const char *buf, size_t count) \
b8d08210 1366{ \
ee959b00 1367 struct Scsi_Host *shost = transport_class_to_shost(dev); \
b8d08210
JS
1368 struct fc_internal *i = to_fc_internal(shost->transportt); \
1369 unsigned int cnt=count; \
1370 \
1371 /* count may include a LF at end of string */ \
1372 if (buf[cnt-1] == '\n') \
1373 cnt--; \
1374 if (cnt > ((slen) - 1)) \
1375 return -EINVAL; \
1376 memcpy(fc_host_##field(shost), buf, cnt); \
1377 i->f->set_host_##field(shost); \
1378 return count; \
1379}
1380
1da177e4
LT
1381#define fc_host_rd_attr(field, format_string, sz) \
1382 fc_host_show_function(field, format_string, sz, ) \
ee959b00 1383static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1da177e4
LT
1384 show_fc_host_##field, NULL)
1385
1386#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1387 fc_host_show_function(field, format_string, sz, (cast)) \
ee959b00 1388static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1da177e4
LT
1389 show_fc_host_##field, NULL)
1390
1391#define fc_host_rw_attr(field, format_string, sz) \
1392 fc_host_show_function(field, format_string, sz, ) \
1393 fc_host_store_function(field) \
ee959b00 1394static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
1da177e4
LT
1395 show_fc_host_##field, \
1396 store_fc_host_##field)
1397
1398#define fc_host_rd_enum_attr(title, maxlen) \
1399static ssize_t \
ee959b00
TJ
1400show_fc_host_##title (struct device *dev, \
1401 struct device_attribute *attr, char *buf) \
1da177e4 1402{ \
ee959b00 1403 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1da177e4
LT
1404 struct fc_internal *i = to_fc_internal(shost->transportt); \
1405 const char *name; \
1406 if (i->f->get_host_##title) \
1407 i->f->get_host_##title(shost); \
1408 name = get_fc_##title##_name(fc_host_##title(shost)); \
1409 if (!name) \
1410 return -EINVAL; \
1411 return snprintf(buf, maxlen, "%s\n", name); \
1412} \
ee959b00 1413static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1da177e4
LT
1414
1415#define SETUP_HOST_ATTRIBUTE_RD(field) \
ee959b00 1416 i->private_host_attrs[count] = device_attr_host_##field; \
1da177e4
LT
1417 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1418 i->private_host_attrs[count].store = NULL; \
1419 i->host_attrs[count] = &i->private_host_attrs[count]; \
1420 if (i->f->show_host_##field) \
1421 count++
1422
a53eb5e0 1423#define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
ee959b00 1424 i->private_host_attrs[count] = device_attr_host_##field; \
a53eb5e0
JS
1425 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1426 i->private_host_attrs[count].store = NULL; \
1427 i->host_attrs[count] = &i->private_host_attrs[count]; \
1428 count++
1429
1da177e4 1430#define SETUP_HOST_ATTRIBUTE_RW(field) \
ee959b00 1431 i->private_host_attrs[count] = device_attr_host_##field; \
1da177e4
LT
1432 if (!i->f->set_host_##field) { \
1433 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1434 i->private_host_attrs[count].store = NULL; \
1435 } \
1436 i->host_attrs[count] = &i->private_host_attrs[count]; \
1437 if (i->f->show_host_##field) \
1438 count++
1439
1440
1441#define fc_private_host_show_function(field, format_string, sz, cast) \
1442static ssize_t \
ee959b00
TJ
1443show_fc_host_##field (struct device *dev, \
1444 struct device_attribute *attr, char *buf) \
1da177e4 1445{ \
ee959b00 1446 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1da177e4
LT
1447 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1448}
1449
1450#define fc_private_host_rd_attr(field, format_string, sz) \
1451 fc_private_host_show_function(field, format_string, sz, ) \
ee959b00 1452static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1da177e4
LT
1453 show_fc_host_##field, NULL)
1454
1455#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1456 fc_private_host_show_function(field, format_string, sz, (cast)) \
ee959b00 1457static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1da177e4
LT
1458 show_fc_host_##field, NULL)
1459
1460#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
ee959b00 1461 i->private_host_attrs[count] = device_attr_host_##field; \
1da177e4
LT
1462 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1463 i->private_host_attrs[count].store = NULL; \
1464 i->host_attrs[count] = &i->private_host_attrs[count]; \
1465 count++
1466
1467#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
91ca7b01 1468{ \
ee959b00 1469 i->private_host_attrs[count] = device_attr_host_##field; \
1da177e4 1470 i->host_attrs[count] = &i->private_host_attrs[count]; \
91ca7b01
AV
1471 count++; \
1472}
1da177e4
LT
1473
1474
1475/* Fixed Host Attributes */
1476
1477static ssize_t
ee959b00
TJ
1478show_fc_host_supported_classes (struct device *dev,
1479 struct device_attribute *attr, char *buf)
1da177e4 1480{
ee959b00 1481 struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4
LT
1482
1483 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1484 return snprintf(buf, 20, "unspecified\n");
1485
1486 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1487}
ee959b00 1488static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1da177e4
LT
1489 show_fc_host_supported_classes, NULL);
1490
1491static ssize_t
ee959b00
TJ
1492show_fc_host_supported_fc4s (struct device *dev,
1493 struct device_attribute *attr, char *buf)
1da177e4 1494{
ee959b00 1495 struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4
LT
1496 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1497}
ee959b00 1498static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1da177e4
LT
1499 show_fc_host_supported_fc4s, NULL);
1500
1501static ssize_t
ee959b00
TJ
1502show_fc_host_supported_speeds (struct device *dev,
1503 struct device_attribute *attr, char *buf)
1da177e4 1504{
ee959b00 1505 struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4
LT
1506
1507 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1508 return snprintf(buf, 20, "unknown\n");
1509
1510 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1511}
ee959b00 1512static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1da177e4
LT
1513 show_fc_host_supported_speeds, NULL);
1514
1515
1516fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1517fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
6b7281d0
AH
1518fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1519 unsigned long long);
1da177e4 1520fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
a53eb5e0 1521fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1da177e4 1522fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
bb8ef587
NP
1523fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
1524fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1525fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1526fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1527fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1528fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1529fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1da177e4
LT
1530
1531
1532/* Dynamic Host Attributes */
1533
1534static ssize_t
ee959b00
TJ
1535show_fc_host_active_fc4s (struct device *dev,
1536 struct device_attribute *attr, char *buf)
1da177e4 1537{
ee959b00 1538 struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4
LT
1539 struct fc_internal *i = to_fc_internal(shost->transportt);
1540
1541 if (i->f->get_host_active_fc4s)
1542 i->f->get_host_active_fc4s(shost);
1543
1544 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1545}
ee959b00 1546static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1da177e4
LT
1547 show_fc_host_active_fc4s, NULL);
1548
1549static ssize_t
ee959b00
TJ
1550show_fc_host_speed (struct device *dev,
1551 struct device_attribute *attr, char *buf)
1da177e4 1552{
ee959b00 1553 struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4
LT
1554 struct fc_internal *i = to_fc_internal(shost->transportt);
1555
1556 if (i->f->get_host_speed)
1557 i->f->get_host_speed(shost);
1558
1559 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1560 return snprintf(buf, 20, "unknown\n");
1561
1562 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1563}
ee959b00 1564static FC_DEVICE_ATTR(host, speed, S_IRUGO,
1da177e4
LT
1565 show_fc_host_speed, NULL);
1566
1567
1568fc_host_rd_attr(port_id, "0x%06x\n", 20);
1569fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1570fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1571fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
016131b8 1572fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1da177e4 1573
b8d08210
JS
1574fc_private_host_show_function(system_hostname, "%s\n",
1575 FC_SYMBOLIC_NAME_SIZE + 1, )
1576fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
ee959b00 1577static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
b8d08210
JS
1578 show_fc_host_system_hostname, store_fc_host_system_hostname);
1579
1da177e4
LT
1580
1581/* Private Host Attributes */
1582
1583static ssize_t
ee959b00
TJ
1584show_fc_private_host_tgtid_bind_type(struct device *dev,
1585 struct device_attribute *attr, char *buf)
1da177e4 1586{
ee959b00 1587 struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4
LT
1588 const char *name;
1589
1590 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1591 if (!name)
1592 return -EINVAL;
1593 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1594}
1595
d16794f6
JSEC
1596#define get_list_head_entry(pos, head, member) \
1597 pos = list_entry((head)->next, typeof(*pos), member)
1598
1da177e4 1599static ssize_t
ee959b00
TJ
1600store_fc_private_host_tgtid_bind_type(struct device *dev,
1601 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1602{
ee959b00 1603 struct Scsi_Host *shost = transport_class_to_shost(dev);
d16794f6 1604 struct fc_rport *rport;
1da177e4
LT
1605 enum fc_tgtid_binding_type val;
1606 unsigned long flags;
1607
1608 if (get_fc_tgtid_bind_type_match(buf, &val))
1609 return -EINVAL;
1610
1611 /* if changing bind type, purge all unused consistent bindings */
1612 if (val != fc_host_tgtid_bind_type(shost)) {
1613 spin_lock_irqsave(shost->host_lock, flags);
d16794f6
JSEC
1614 while (!list_empty(&fc_host_rport_bindings(shost))) {
1615 get_list_head_entry(rport,
1616 &fc_host_rport_bindings(shost), peers);
aedf3497
JS
1617 list_del(&rport->peers);
1618 rport->port_state = FC_PORTSTATE_DELETED;
1619 fc_queue_work(shost, &rport->rport_delete_work);
d16794f6 1620 }
1da177e4
LT
1621 spin_unlock_irqrestore(shost->host_lock, flags);
1622 }
1623
1624 fc_host_tgtid_bind_type(shost) = val;
1625 return count;
1626}
1627
ee959b00 1628static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1da177e4
LT
1629 show_fc_private_host_tgtid_bind_type,
1630 store_fc_private_host_tgtid_bind_type);
1631
91ca7b01 1632static ssize_t
ee959b00
TJ
1633store_fc_private_host_issue_lip(struct device *dev,
1634 struct device_attribute *attr, const char *buf, size_t count)
91ca7b01 1635{
ee959b00 1636 struct Scsi_Host *shost = transport_class_to_shost(dev);
91ca7b01
AV
1637 struct fc_internal *i = to_fc_internal(shost->transportt);
1638 int ret;
1639
1640 /* ignore any data value written to the attribute */
1641 if (i->f->issue_fc_host_lip) {
1642 ret = i->f->issue_fc_host_lip(shost);
1643 return ret ? ret: count;
1644 }
1645
1646 return -ENOENT;
1647}
1648
ee959b00 1649static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
91ca7b01
AV
1650 store_fc_private_host_issue_lip);
1651
43ca910a
MC
1652static ssize_t
1653store_fc_private_host_dev_loss_tmo(struct device *dev,
1654 struct device_attribute *attr,
1655 const char *buf, size_t count)
1656{
1657 struct Scsi_Host *shost = transport_class_to_shost(dev);
1658 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1659 struct fc_rport *rport;
1660 unsigned long val, flags;
1661 int rc;
1662
1663 rc = fc_str_to_dev_loss(buf, &val);
1664 if (rc)
1665 return rc;
1666
1667 fc_host_dev_loss_tmo(shost) = val;
1668 spin_lock_irqsave(shost->host_lock, flags);
1669 list_for_each_entry(rport, &fc_host->rports, peers)
1670 fc_rport_set_dev_loss_tmo(rport, val);
1671 spin_unlock_irqrestore(shost->host_lock, flags);
1672 return count;
1673}
a53eb5e0 1674
43ca910a
MC
1675fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
1676static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
1677 show_fc_host_dev_loss_tmo,
1678 store_fc_private_host_dev_loss_tmo);
1679
1680fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
a53eb5e0 1681
1da177e4
LT
1682/*
1683 * Host Statistics Management
1684 */
1685
134699d2 1686/* Show a given attribute in the statistics group */
1da177e4 1687static ssize_t
ee959b00 1688fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
1da177e4 1689{
ee959b00 1690 struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4
LT
1691 struct fc_internal *i = to_fc_internal(shost->transportt);
1692 struct fc_host_statistics *stats;
1693 ssize_t ret = -ENOENT;
1694
1695 if (offset > sizeof(struct fc_host_statistics) ||
1696 offset % sizeof(u64) != 0)
1697 WARN_ON(1);
1698
1699 if (i->f->get_fc_host_stats) {
1700 stats = (i->f->get_fc_host_stats)(shost);
1701 if (stats)
1702 ret = snprintf(buf, 20, "0x%llx\n",
1703 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1704 }
1705 return ret;
1706}
1707
1708
1709/* generate a read-only statistics attribute */
1710#define fc_host_statistic(name) \
ee959b00
TJ
1711static ssize_t show_fcstat_##name(struct device *cd, \
1712 struct device_attribute *attr, \
1713 char *buf) \
1da177e4
LT
1714{ \
1715 return fc_stat_show(cd, buf, \
1716 offsetof(struct fc_host_statistics, name)); \
1717} \
ee959b00 1718static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1da177e4
LT
1719
1720fc_host_statistic(seconds_since_last_reset);
1721fc_host_statistic(tx_frames);
1722fc_host_statistic(tx_words);
1723fc_host_statistic(rx_frames);
1724fc_host_statistic(rx_words);
1725fc_host_statistic(lip_count);
1726fc_host_statistic(nos_count);
1727fc_host_statistic(error_frames);
1728fc_host_statistic(dumped_frames);
1729fc_host_statistic(link_failure_count);
1730fc_host_statistic(loss_of_sync_count);
1731fc_host_statistic(loss_of_signal_count);
1732fc_host_statistic(prim_seq_protocol_err_count);
1733fc_host_statistic(invalid_tx_word_count);
1734fc_host_statistic(invalid_crc_count);
1735fc_host_statistic(fcp_input_requests);
1736fc_host_statistic(fcp_output_requests);
1737fc_host_statistic(fcp_control_requests);
1738fc_host_statistic(fcp_input_megabytes);
1739fc_host_statistic(fcp_output_megabytes);
e58abb0c
VD
1740fc_host_statistic(fcp_packet_alloc_failures);
1741fc_host_statistic(fcp_packet_aborts);
1742fc_host_statistic(fcp_frame_alloc_failures);
1743fc_host_statistic(fc_no_free_exch);
1744fc_host_statistic(fc_no_free_exch_xid);
1745fc_host_statistic(fc_xid_not_found);
1746fc_host_statistic(fc_xid_busy);
1747fc_host_statistic(fc_seq_not_found);
1748fc_host_statistic(fc_non_bls_resp);
1da177e4
LT
1749
1750static ssize_t
ee959b00
TJ
1751fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1752 const char *buf, size_t count)
1da177e4 1753{
ee959b00 1754 struct Scsi_Host *shost = transport_class_to_shost(dev);
1da177e4
LT
1755 struct fc_internal *i = to_fc_internal(shost->transportt);
1756
1757 /* ignore any data value written to the attribute */
1758 if (i->f->reset_fc_host_stats) {
1759 i->f->reset_fc_host_stats(shost);
1760 return count;
1761 }
1762
1763 return -ENOENT;
1764}
ee959b00 1765static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1da177e4
LT
1766 fc_reset_statistics);
1767
1da177e4 1768static struct attribute *fc_statistics_attrs[] = {
ee959b00
TJ
1769 &device_attr_host_seconds_since_last_reset.attr,
1770 &device_attr_host_tx_frames.attr,
1771 &device_attr_host_tx_words.attr,
1772 &device_attr_host_rx_frames.attr,
1773 &device_attr_host_rx_words.attr,
1774 &device_attr_host_lip_count.attr,
1775 &device_attr_host_nos_count.attr,
1776 &device_attr_host_error_frames.attr,
1777 &device_attr_host_dumped_frames.attr,
1778 &device_attr_host_link_failure_count.attr,
1779 &device_attr_host_loss_of_sync_count.attr,
1780 &device_attr_host_loss_of_signal_count.attr,
1781 &device_attr_host_prim_seq_protocol_err_count.attr,
1782 &device_attr_host_invalid_tx_word_count.attr,
1783 &device_attr_host_invalid_crc_count.attr,
1784 &device_attr_host_fcp_input_requests.attr,
1785 &device_attr_host_fcp_output_requests.attr,
1786 &device_attr_host_fcp_control_requests.attr,
1787 &device_attr_host_fcp_input_megabytes.attr,
1788 &device_attr_host_fcp_output_megabytes.attr,
e58abb0c
VD
1789 &device_attr_host_fcp_packet_alloc_failures.attr,
1790 &device_attr_host_fcp_packet_aborts.attr,
1791 &device_attr_host_fcp_frame_alloc_failures.attr,
1792 &device_attr_host_fc_no_free_exch.attr,
1793 &device_attr_host_fc_no_free_exch_xid.attr,
1794 &device_attr_host_fc_xid_not_found.attr,
1795 &device_attr_host_fc_xid_busy.attr,
1796 &device_attr_host_fc_seq_not_found.attr,
1797 &device_attr_host_fc_non_bls_resp.attr,
ee959b00 1798 &device_attr_host_reset_statistics.attr,
1da177e4
LT
1799 NULL
1800};
1801
1802static struct attribute_group fc_statistics_group = {
1803 .name = "statistics",
1804 .attrs = fc_statistics_attrs,
1805};
1806
a53eb5e0
JS
1807
1808/* Host Vport Attributes */
1809
1810static int
1811fc_parse_wwn(const char *ns, u64 *nm)
1812{
1813 unsigned int i, j;
1814 u8 wwn[8];
1815
1816 memset(wwn, 0, sizeof(wwn));
1817
1818 /* Validate and store the new name */
1819 for (i=0, j=0; i < 16; i++) {
ecc30990
AS
1820 int value;
1821
1822 value = hex_to_bin(*ns++);
1823 if (value >= 0)
1824 j = (j << 4) | value;
a53eb5e0
JS
1825 else
1826 return -EINVAL;
1827 if (i % 2) {
1828 wwn[i/2] = j & 0xff;
1829 j = 0;
1830 }
1831 }
1832
1833 *nm = wwn_to_u64(wwn);
1834
1835 return 0;
1836}
1837
1838
1839/*
1840 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1841 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1842 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1843 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1844 */
1845static ssize_t
ee959b00
TJ
1846store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1847 const char *buf, size_t count)
a53eb5e0 1848{
ee959b00 1849 struct Scsi_Host *shost = transport_class_to_shost(dev);
a53eb5e0
JS
1850 struct fc_vport_identifiers vid;
1851 struct fc_vport *vport;
1852 unsigned int cnt=count;
1853 int stat;
1854
1855 memset(&vid, 0, sizeof(vid));
1856
1857 /* count may include a LF at end of string */
1858 if (buf[cnt-1] == '\n')
1859 cnt--;
1860
1861 /* validate we have enough characters for WWPN */
1862 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1863 return -EINVAL;
1864
1865 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1866 if (stat)
1867 return stat;
1868
1869 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1870 if (stat)
1871 return stat;
1872
1873 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1874 vid.vport_type = FC_PORTTYPE_NPIV;
1875 /* vid.symbolic_name is already zero/NULL's */
1876 vid.disable = false; /* always enabled */
1877
1878 /* we only allow support on Channel 0 !!! */
a30c3f69 1879 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
a53eb5e0
JS
1880 return stat ? stat : count;
1881}
ee959b00 1882static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
a53eb5e0
JS
1883 store_fc_host_vport_create);
1884
1885
1886/*
1887 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1888 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1889 * The WWNs are specified as hex characters, and may *not* contain
1890 * any prefixes (e.g. 0x, x, etc)
1891 */
1892static ssize_t
ee959b00
TJ
1893store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1894 const char *buf, size_t count)
a53eb5e0 1895{
ee959b00 1896 struct Scsi_Host *shost = transport_class_to_shost(dev);
a53eb5e0
JS
1897 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1898 struct fc_vport *vport;
1899 u64 wwpn, wwnn;
1900 unsigned long flags;
1901 unsigned int cnt=count;
1902 int stat, match;
1903
1904 /* count may include a LF at end of string */
1905 if (buf[cnt-1] == '\n')
1906 cnt--;
1907
1908 /* validate we have enough characters for WWPN */
1909 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1910 return -EINVAL;
1911
1912 stat = fc_parse_wwn(&buf[0], &wwpn);
1913 if (stat)
1914 return stat;
1915
1916 stat = fc_parse_wwn(&buf[17], &wwnn);
1917 if (stat)
1918 return stat;
1919
1920 spin_lock_irqsave(shost->host_lock, flags);
1921 match = 0;
1922 /* we only allow support on Channel 0 !!! */
1923 list_for_each_entry(vport, &fc_host->vports, peers) {
1924 if ((vport->channel == 0) &&
1925 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
0d9dc7c8
GR
1926 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1927 break;
1928 vport->flags |= FC_VPORT_DELETING;
a53eb5e0
JS
1929 match = 1;
1930 break;
1931 }
1932 }
1933 spin_unlock_irqrestore(shost->host_lock, flags);
1934
1935 if (!match)
1936 return -ENODEV;
1937
1938 stat = fc_vport_terminate(vport);
1939 return stat ? stat : count;
1940}
ee959b00 1941static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
a53eb5e0
JS
1942 store_fc_host_vport_delete);
1943
1944
1da177e4
LT
1945static int fc_host_match(struct attribute_container *cont,
1946 struct device *dev)
1947{
1948 struct Scsi_Host *shost;
1949 struct fc_internal *i;
1950
1951 if (!scsi_is_host_device(dev))
1952 return 0;
1953
1954 shost = dev_to_shost(dev);
1955 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1956 != &fc_host_class.class)
1957 return 0;
1958
1959 i = to_fc_internal(shost->transportt);
1960
1961 return &i->t.host_attrs.ac == cont;
1962}
1963
1964static int fc_target_match(struct attribute_container *cont,
1965 struct device *dev)
1966{
1967 struct Scsi_Host *shost;
1968 struct fc_internal *i;
1969
1970 if (!scsi_is_target_device(dev))
1971 return 0;
1972
1973 shost = dev_to_shost(dev->parent);
1974 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1975 != &fc_host_class.class)
1976 return 0;
1977
1978 i = to_fc_internal(shost->transportt);
1979
1980 return &i->t.target_attrs.ac == cont;
1981}
1982
1983static void fc_rport_dev_release(struct device *dev)
1984{
1985 struct fc_rport *rport = dev_to_rport(dev);
1986 put_device(dev->parent);
1987 kfree(rport);
1988}
1989
1990int scsi_is_fc_rport(const struct device *dev)
1991{
1992 return dev->release == fc_rport_dev_release;
1993}
1994EXPORT_SYMBOL(scsi_is_fc_rport);
1995
1996static int fc_rport_match(struct attribute_container *cont,
1997 struct device *dev)
1998{
1999 struct Scsi_Host *shost;
2000 struct fc_internal *i;
2001
2002 if (!scsi_is_fc_rport(dev))
2003 return 0;
2004
2005 shost = dev_to_shost(dev->parent);
2006 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2007 != &fc_host_class.class)
2008 return 0;
2009
2010 i = to_fc_internal(shost->transportt);
2011
2012 return &i->rport_attr_cont.ac == cont;
2013}
2014
5c44cd2a 2015
a53eb5e0
JS
2016static void fc_vport_dev_release(struct device *dev)
2017{
2018 struct fc_vport *vport = dev_to_vport(dev);
2019 put_device(dev->parent); /* release kobj parent */
2020 kfree(vport);
2021}
2022
62055172 2023static int scsi_is_fc_vport(const struct device *dev)
a53eb5e0
JS
2024{
2025 return dev->release == fc_vport_dev_release;
2026}
a53eb5e0
JS
2027
2028static int fc_vport_match(struct attribute_container *cont,
2029 struct device *dev)
2030{
2031 struct fc_vport *vport;
2032 struct Scsi_Host *shost;
2033 struct fc_internal *i;
2034
2035 if (!scsi_is_fc_vport(dev))
2036 return 0;
2037 vport = dev_to_vport(dev);
2038
2039 shost = vport_to_shost(vport);
2040 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2041 != &fc_host_class.class)
2042 return 0;
2043
2044 i = to_fc_internal(shost->transportt);
2045 return &i->vport_attr_cont.ac == cont;
2046}
2047
2048
c829c394 2049/**
b6a05c82 2050 * fc_eh_timed_out - FC Transport I/O timeout intercept handler
c829c394
JS
2051 * @scmd: The SCSI command which timed out
2052 *
2053 * This routine protects against error handlers getting invoked while a
2054 * rport is in a blocked state, typically due to a temporarily loss of
2055 * connectivity. If the error handlers are allowed to proceed, requests
2056 * to abort i/o, reset the target, etc will likely fail as there is no way
2057 * to communicate with the device to perform the requested function. These
2058 * failures may result in the midlayer taking the device offline, requiring
2059 * manual intervention to restore operation.
2060 *
2061 * This routine, called whenever an i/o times out, validates the state of
2062 * the underlying rport. If the rport is blocked, it returns
2063 * EH_RESET_TIMER, which will continue to reschedule the timeout.
2064 * Eventually, either the device will return, or devloss_tmo will fire,
2065 * and when the timeout then fires, it will be handled normally.
2066 * If the rport is not blocked, normal error handling continues.
2067 *
2068 * Notes:
2069 * This routine assumes no locks are held on entry.
eb44820c 2070 */
b6a05c82
CH
2071enum blk_eh_timer_return
2072fc_eh_timed_out(struct scsi_cmnd *scmd)
c829c394
JS
2073{
2074 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
2075
2076 if (rport->port_state == FC_PORTSTATE_BLOCKED)
242f9dcb 2077 return BLK_EH_RESET_TIMER;
c829c394 2078
6600593c 2079 return BLK_EH_DONE;
c829c394 2080}
b6a05c82 2081EXPORT_SYMBOL(fc_eh_timed_out);
c829c394 2082
5c44cd2a 2083/*
bda23253
JS
2084 * Called by fc_user_scan to locate an rport on the shost that
2085 * matches the channel and target id, and invoke scsi_scan_target()
2086 * on the rport.
5c44cd2a 2087 */
bda23253 2088static void
9cb78c16 2089fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
5c44cd2a
JSEC
2090{
2091 struct fc_rport *rport;
bda23253
JS
2092 unsigned long flags;
2093
2094 spin_lock_irqsave(shost->host_lock, flags);
5c44cd2a 2095
e02f3f59
CH
2096 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2097 if (rport->scsi_target_id == -1)
2098 continue;
5c44cd2a 2099
0d2fcd9f
HR
2100 if (rport->port_state != FC_PORTSTATE_ONLINE)
2101 continue;
2102
bda23253
JS
2103 if ((channel == rport->channel) &&
2104 (id == rport->scsi_target_id)) {
2105 spin_unlock_irqrestore(shost->host_lock, flags);
1d645088
HR
2106 scsi_scan_target(&rport->dev, channel, id, lun,
2107 SCSI_SCAN_MANUAL);
bda23253 2108 return;
e02f3f59
CH
2109 }
2110 }
2111
bda23253
JS
2112 spin_unlock_irqrestore(shost->host_lock, flags);
2113}
2114
2115/*
2116 * Called via sysfs scan routines. Necessary, as the FC transport
2117 * wants to place all target objects below the rport object. So this
2118 * routine must invoke the scsi_scan_target() routine with the rport
2119 * object as the parent.
2120 */
2121static int
9cb78c16 2122fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
bda23253
JS
2123{
2124 uint chlo, chhi;
2125 uint tgtlo, tgthi;
2126
2127 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2128 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2129 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2130 return -EINVAL;
2131
2132 if (channel == SCAN_WILD_CARD) {
2133 chlo = 0;
2134 chhi = shost->max_channel + 1;
2135 } else {
2136 chlo = channel;
2137 chhi = channel + 1;
2138 }
2139
2140 if (id == SCAN_WILD_CARD) {
2141 tgtlo = 0;
2142 tgthi = shost->max_id;
2143 } else {
2144 tgtlo = id;
2145 tgthi = id + 1;
2146 }
2147
2148 for ( ; chlo < chhi; chlo++)
2149 for ( ; tgtlo < tgthi; tgtlo++)
2150 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2151
e02f3f59 2152 return 0;
5c44cd2a
JSEC
2153}
2154
1da177e4
LT
2155struct scsi_transport_template *
2156fc_attach_transport(struct fc_function_template *ft)
2157{
1da177e4 2158 int count;
24669f75
JS
2159 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2160 GFP_KERNEL);
1da177e4
LT
2161
2162 if (unlikely(!i))
2163 return NULL;
2164
1da177e4
LT
2165 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2166 i->t.target_attrs.ac.class = &fc_transport_class.class;
2167 i->t.target_attrs.ac.match = fc_target_match;
2168 i->t.target_size = sizeof(struct fc_starget_attrs);
2169 transport_container_register(&i->t.target_attrs);
2170
2171 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2172 i->t.host_attrs.ac.class = &fc_host_class.class;
2173 i->t.host_attrs.ac.match = fc_host_match;
2174 i->t.host_size = sizeof(struct fc_host_attrs);
2175 if (ft->get_fc_host_stats)
2176 i->t.host_attrs.statistics = &fc_statistics_group;
2177 transport_container_register(&i->t.host_attrs);
2178
2179 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2180 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2181 i->rport_attr_cont.ac.match = fc_rport_match;
2182 transport_container_register(&i->rport_attr_cont);
2183
a53eb5e0
JS
2184 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2185 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2186 i->vport_attr_cont.ac.match = fc_vport_match;
2187 transport_container_register(&i->vport_attr_cont);
2188
1da177e4
LT
2189 i->f = ft;
2190
2191 /* Transport uses the shost workq for scsi scanning */
2192 i->t.create_work_queue = 1;
5c44cd2a 2193
e02f3f59 2194 i->t.user_scan = fc_user_scan;
9ef3e4a4 2195
1da177e4
LT
2196 /*
2197 * Setup SCSI Target Attributes.
2198 */
2199 count = 0;
2200 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2201 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2202 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2203
2204 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2205
2206 i->starget_attrs[count] = NULL;
2207
2208
2209 /*
2210 * Setup SCSI Host Attributes.
2211 */
2212 count=0;
2213 SETUP_HOST_ATTRIBUTE_RD(node_name);
2214 SETUP_HOST_ATTRIBUTE_RD(port_name);
6b7281d0 2215 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
1da177e4
LT
2216 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2217 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1da177e4
LT
2218 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2219 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
a53eb5e0
JS
2220 if (ft->vport_create) {
2221 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2222 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2223 }
1da177e4 2224 SETUP_HOST_ATTRIBUTE_RD(serial_number);
bb8ef587
NP
2225 SETUP_HOST_ATTRIBUTE_RD(manufacturer);
2226 SETUP_HOST_ATTRIBUTE_RD(model);
2227 SETUP_HOST_ATTRIBUTE_RD(model_description);
2228 SETUP_HOST_ATTRIBUTE_RD(hardware_version);
2229 SETUP_HOST_ATTRIBUTE_RD(driver_version);
2230 SETUP_HOST_ATTRIBUTE_RD(firmware_version);
2231 SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
1da177e4
LT
2232
2233 SETUP_HOST_ATTRIBUTE_RD(port_id);
2234 SETUP_HOST_ATTRIBUTE_RD(port_type);
2235 SETUP_HOST_ATTRIBUTE_RD(port_state);
2236 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2237 SETUP_HOST_ATTRIBUTE_RD(speed);
2238 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
016131b8 2239 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
b8d08210 2240 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
1da177e4
LT
2241
2242 /* Transport-managed attributes */
43ca910a 2243 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
1da177e4 2244 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
91ca7b01
AV
2245 if (ft->issue_fc_host_lip)
2246 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
a53eb5e0
JS
2247 if (ft->vport_create)
2248 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2249 if (ft->vport_delete)
2250 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
1da177e4
LT
2251
2252 BUG_ON(count > FC_HOST_NUM_ATTRS);
2253
2254 i->host_attrs[count] = NULL;
2255
2256 /*
2257 * Setup Remote Port Attributes.
2258 */
2259 count=0;
2260 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2261 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2262 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2263 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2264 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2265 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2266 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2267 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2268 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
fff9d40c 2269 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
1da177e4
LT
2270
2271 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2272
2273 i->rport_attrs[count] = NULL;
2274
a53eb5e0
JS
2275 /*
2276 * Setup Virtual Port Attributes.
2277 */
2278 count=0;
2279 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2280 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2281 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2282 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2283 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2284 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2285 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2286 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2287 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2288
2289 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2290
2291 i->vport_attrs[count] = NULL;
2292
1da177e4
LT
2293 return &i->t;
2294}
2295EXPORT_SYMBOL(fc_attach_transport);
2296
2297void fc_release_transport(struct scsi_transport_template *t)
2298{
2299 struct fc_internal *i = to_fc_internal(t);
2300
2301 transport_container_unregister(&i->t.target_attrs);
2302 transport_container_unregister(&i->t.host_attrs);
2303 transport_container_unregister(&i->rport_attr_cont);
a53eb5e0 2304 transport_container_unregister(&i->vport_attr_cont);
1da177e4
LT
2305
2306 kfree(i);
2307}
2308EXPORT_SYMBOL(fc_release_transport);
2309
aedf3497
JS
2310/**
2311 * fc_queue_work - Queue work to the fc_host workqueue.
2312 * @shost: Pointer to Scsi_Host bound to fc_host.
2313 * @work: Work to queue for execution.
2314 *
2315 * Return value:
a0785edf
JS
2316 * 1 - work queued for execution
2317 * 0 - work is already queued
2318 * -EINVAL - work queue doesn't exist
eb44820c 2319 */
aedf3497
JS
2320static int
2321fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2322{
2323 if (unlikely(!fc_host_work_q(shost))) {
2324 printk(KERN_ERR
2325 "ERROR: FC host '%s' attempted to queue work, "
2326 "when no workqueue created.\n", shost->hostt->name);
2327 dump_stack();
2328
2329 return -EINVAL;
2330 }
2331
2332 return queue_work(fc_host_work_q(shost), work);
2333}
2334
2335/**
2336 * fc_flush_work - Flush a fc_host's workqueue.
2337 * @shost: Pointer to Scsi_Host bound to fc_host.
eb44820c 2338 */
aedf3497
JS
2339static void
2340fc_flush_work(struct Scsi_Host *shost)
2341{
2342 if (!fc_host_work_q(shost)) {
2343 printk(KERN_ERR
2344 "ERROR: FC host '%s' attempted to flush work, "
2345 "when no workqueue created.\n", shost->hostt->name);
2346 dump_stack();
2347 return;
2348 }
2349
2350 flush_workqueue(fc_host_work_q(shost));
2351}
2352
2353/**
2354 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2355 * @shost: Pointer to Scsi_Host bound to fc_host.
2356 * @work: Work to queue for execution.
2357 * @delay: jiffies to delay the work queuing
2358 *
2359 * Return value:
0f29b966 2360 * 1 on success / 0 already queued / < 0 for error
eb44820c 2361 */
aedf3497 2362static int
c4028958 2363fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
aedf3497
JS
2364 unsigned long delay)
2365{
2366 if (unlikely(!fc_host_devloss_work_q(shost))) {
2367 printk(KERN_ERR
2368 "ERROR: FC host '%s' attempted to queue work, "
2369 "when no workqueue created.\n", shost->hostt->name);
2370 dump_stack();
2371
2372 return -EINVAL;
2373 }
2374
2375 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2376}
2377
2378/**
2379 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2380 * @shost: Pointer to Scsi_Host bound to fc_host.
eb44820c 2381 */
aedf3497
JS
2382static void
2383fc_flush_devloss(struct Scsi_Host *shost)
2384{
2385 if (!fc_host_devloss_work_q(shost)) {
2386 printk(KERN_ERR
2387 "ERROR: FC host '%s' attempted to flush work, "
2388 "when no workqueue created.\n", shost->hostt->name);
2389 dump_stack();
2390 return;
2391 }
2392
2393 flush_workqueue(fc_host_devloss_work_q(shost));
2394}
2395
1da177e4
LT
2396
2397/**
eb44820c
RL
2398 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2399 * @shost: Which &Scsi_Host
1da177e4 2400 *
25985edc 2401 * This routine is expected to be called immediately preceding the
1da177e4
LT
2402 * a driver's call to scsi_remove_host().
2403 *
2404 * WARNING: A driver utilizing the fc_transport, which fails to call
eb44820c 2405 * this routine prior to scsi_remove_host(), will leave dangling
1da177e4
LT
2406 * objects in /sys/class/fc_remote_ports. Access to any of these
2407 * objects can result in a system crash !!!
2408 *
2409 * Notes:
2410 * This routine assumes no locks are held on entry.
eb44820c 2411 */
1da177e4
LT
2412void
2413fc_remove_host(struct Scsi_Host *shost)
2414{
a53eb5e0
JS
2415 struct fc_vport *vport = NULL, *next_vport = NULL;
2416 struct fc_rport *rport = NULL, *next_rport = NULL;
aedf3497
JS
2417 struct workqueue_struct *work_q;
2418 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
a53eb5e0 2419 unsigned long flags;
a53eb5e0
JS
2420
2421 spin_lock_irqsave(shost->host_lock, flags);
2422
2423 /* Remove any vports */
260f4aed
HR
2424 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2425 vport->flags |= FC_VPORT_DELETING;
9ef3e4a4 2426 fc_queue_work(shost, &vport->vport_delete_work);
260f4aed 2427 }
1da177e4
LT
2428
2429 /* Remove any remote ports */
2430 list_for_each_entry_safe(rport, next_rport,
aedf3497
JS
2431 &fc_host->rports, peers) {
2432 list_del(&rport->peers);
2433 rport->port_state = FC_PORTSTATE_DELETED;
2434 fc_queue_work(shost, &rport->rport_delete_work);
2435 }
2436
1da177e4 2437 list_for_each_entry_safe(rport, next_rport,
aedf3497
JS
2438 &fc_host->rport_bindings, peers) {
2439 list_del(&rport->peers);
2440 rport->port_state = FC_PORTSTATE_DELETED;
2441 fc_queue_work(shost, &rport->rport_delete_work);
2442 }
2443
a53eb5e0
JS
2444 spin_unlock_irqrestore(shost->host_lock, flags);
2445
aedf3497
JS
2446 /* flush all scan work items */
2447 scsi_flush_work(shost);
2448
2449 /* flush all stgt delete, and rport delete work items, then kill it */
2450 if (fc_host->work_q) {
2451 work_q = fc_host->work_q;
2452 fc_host->work_q = NULL;
2453 destroy_workqueue(work_q);
2454 }
2455
2456 /* flush all devloss work items, then kill it */
2457 if (fc_host->devloss_work_q) {
2458 work_q = fc_host->devloss_work_q;
2459 fc_host->devloss_work_q = NULL;
2460 destroy_workqueue(work_q);
2461 }
1da177e4
LT
2462}
2463EXPORT_SYMBOL(fc_remove_host);
2464
fff9d40c
MC
2465static void fc_terminate_rport_io(struct fc_rport *rport)
2466{
2467 struct Scsi_Host *shost = rport_to_shost(rport);
2468 struct fc_internal *i = to_fc_internal(shost->transportt);
2469
2470 /* Involve the LLDD if possible to terminate all io on the rport. */
2471 if (i->f->terminate_rport_io)
2472 i->f->terminate_rport_io(rport);
2473
2474 /*
5d9fb5cc 2475 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
fff9d40c 2476 */
5d9fb5cc 2477 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
fff9d40c 2478}
aedf3497
JS
2479
2480/**
25985edc 2481 * fc_starget_delete - called to delete the scsi descendants of an rport
c4028958 2482 * @work: remote port to be operated on.
eb44820c
RL
2483 *
2484 * Deletes target and all sdevs.
2485 */
19a7b4ae 2486static void
c4028958 2487fc_starget_delete(struct work_struct *work)
19a7b4ae 2488{
c4028958
DH
2489 struct fc_rport *rport =
2490 container_of(work, struct fc_rport, stgt_delete_work);
19a7b4ae 2491
fff9d40c 2492 fc_terminate_rport_io(rport);
19a7b4ae
JSEC
2493 scsi_remove_target(&rport->dev);
2494}
2495
aedf3497
JS
2496
2497/**
2498 * fc_rport_final_delete - finish rport termination and delete it.
c4028958 2499 * @work: remote port to be deleted.
eb44820c 2500 */
aedf3497 2501static void
c4028958 2502fc_rport_final_delete(struct work_struct *work)
aedf3497 2503{
c4028958
DH
2504 struct fc_rport *rport =
2505 container_of(work, struct fc_rport, rport_delete_work);
aedf3497
JS
2506 struct device *dev = &rport->dev;
2507 struct Scsi_Host *shost = rport_to_shost(rport);
0f29b966 2508 struct fc_internal *i = to_fc_internal(shost->transportt);
92740b24 2509 unsigned long flags;
8798a694 2510 int do_callback = 0;
aedf3497 2511
112f661d
NNS
2512 fc_terminate_rport_io(rport);
2513
aedf3497 2514 /*
9ef3e4a4 2515 * if a scan is pending, flush the SCSI Host work_q so that
aedf3497
JS
2516 * that we can reclaim the rport scan work element.
2517 */
2518 if (rport->flags & FC_RPORT_SCAN_PENDING)
2519 scsi_flush_work(shost);
2520
92740b24
JS
2521 /*
2522 * Cancel any outstanding timers. These should really exist
2523 * only when rmmod'ing the LLDD and we're asking for
2524 * immediate termination of the rports
2525 */
2526 spin_lock_irqsave(shost->host_lock, flags);
2527 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2528 spin_unlock_irqrestore(shost->host_lock, flags);
2529 if (!cancel_delayed_work(&rport->fail_io_work))
2530 fc_flush_devloss(shost);
2531 if (!cancel_delayed_work(&rport->dev_loss_work))
2532 fc_flush_devloss(shost);
0353e085 2533 cancel_work_sync(&rport->scan_work);
92740b24
JS
2534 spin_lock_irqsave(shost->host_lock, flags);
2535 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2536 }
2537 spin_unlock_irqrestore(shost->host_lock, flags);
2538
0f29b966
JS
2539 /* Delete SCSI target and sdevs */
2540 if (rport->scsi_target_id != -1)
c4028958 2541 fc_starget_delete(&rport->stgt_delete_work);
92740b24
JS
2542
2543 /*
2544 * Notify the driver that the rport is now dead. The LLDD will
2545 * also guarantee that any communication to the rport is terminated
4be98c0c
JS
2546 *
2547 * Avoid this call if we already called it when we preserved the
2548 * rport for the binding.
92740b24 2549 */
8798a694 2550 spin_lock_irqsave(shost->host_lock, flags);
4be98c0c 2551 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
8798a694
MR
2552 (i->f->dev_loss_tmo_callbk)) {
2553 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2554 do_callback = 1;
2555 }
2556 spin_unlock_irqrestore(shost->host_lock, flags);
2557
2558 if (do_callback)
0f29b966 2559 i->f->dev_loss_tmo_callbk(rport);
0f29b966 2560
9e4f5e29
JS
2561 fc_bsg_remove(rport->rqst_q);
2562
aedf3497
JS
2563 transport_remove_device(dev);
2564 device_del(dev);
2565 transport_destroy_device(dev);
4cd38e38 2566 scsi_host_put(shost); /* for fc_host->rport list */
3bdad7bd 2567 put_device(dev); /* for self-reference */
aedf3497
JS
2568}
2569
2570
1da177e4 2571/**
558b88fe 2572 * fc_remote_port_create - allocates and creates a remote FC port.
1da177e4
LT
2573 * @shost: scsi host the remote port is connected to.
2574 * @channel: Channel on shost port connected to.
2575 * @ids: The world wide names, fc address, and FC4 port
2576 * roles for the remote port.
2577 *
2578 * Allocates and creates the remoter port structure, including the
2579 * class and sysfs creation.
2580 *
2581 * Notes:
2582 * This routine assumes no locks are held on entry.
eb44820c 2583 */
44818efb 2584static struct fc_rport *
558b88fe
HR
2585fc_remote_port_create(struct Scsi_Host *shost, int channel,
2586 struct fc_rport_identifiers *ids)
1da177e4 2587{
aedf3497 2588 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4
LT
2589 struct fc_internal *fci = to_fc_internal(shost->transportt);
2590 struct fc_rport *rport;
2591 struct device *dev;
2592 unsigned long flags;
2593 int error;
2594 size_t size;
2595
2596 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
24669f75 2597 rport = kzalloc(size, GFP_KERNEL);
1da177e4 2598 if (unlikely(!rport)) {
cadbd4a5 2599 printk(KERN_ERR "%s: allocation failure\n", __func__);
1da177e4
LT
2600 return NULL;
2601 }
1da177e4
LT
2602
2603 rport->maxframe_size = -1;
2604 rport->supported_classes = FC_COS_UNSPECIFIED;
43ca910a 2605 rport->dev_loss_tmo = fc_host->dev_loss_tmo;
1da177e4
LT
2606 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2607 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2608 rport->port_id = ids->port_id;
2609 rport->roles = ids->roles;
2610 rport->port_state = FC_PORTSTATE_ONLINE;
2611 if (fci->f->dd_fcrport_size)
2612 rport->dd_data = &rport[1];
2613 rport->channel = channel;
0f29b966 2614 rport->fast_io_fail_tmo = -1;
1da177e4 2615
c4028958
DH
2616 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2617 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2618 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2619 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2620 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
1da177e4
LT
2621
2622 spin_lock_irqsave(shost->host_lock, flags);
2623
2624 rport->number = fc_host->next_rport_number++;
0c3ae266
CA
2625 if ((rport->roles & FC_PORT_ROLE_FCP_TARGET) ||
2626 (rport->roles & FC_PORT_ROLE_FCP_DUMMY_INITIATOR))
1da177e4
LT
2627 rport->scsi_target_id = fc_host->next_target_id++;
2628 else
2629 rport->scsi_target_id = -1;
aedf3497 2630 list_add_tail(&rport->peers, &fc_host->rports);
4cd38e38 2631 scsi_host_get(shost); /* for fc_host->rport list */
1da177e4
LT
2632
2633 spin_unlock_irqrestore(shost->host_lock, flags);
2634
2635 dev = &rport->dev;
3bdad7bd
JS
2636 device_initialize(dev); /* takes self reference */
2637 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
1da177e4 2638 dev->release = fc_rport_dev_release;
71610f55
KS
2639 dev_set_name(dev, "rport-%d:%d-%d",
2640 shost->host_no, channel, rport->number);
1da177e4
LT
2641 transport_setup_device(dev);
2642
2643 error = device_add(dev);
2644 if (error) {
2645 printk(KERN_ERR "FC Remote Port device_add failed\n");
2646 goto delete_rport;
2647 }
2648 transport_add_device(dev);
2649 transport_configure_device(dev);
2650
9e4f5e29
JS
2651 fc_bsg_rportadd(shost, rport);
2652 /* ignore any bsg add error - we just can't do sgio */
2653
a53eb5e0 2654 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
1da177e4 2655 /* initiate a scan of the target */
aedf3497 2656 rport->flags |= FC_RPORT_SCAN_PENDING;
1da177e4 2657 scsi_queue_work(shost, &rport->scan_work);
aedf3497 2658 }
1da177e4
LT
2659
2660 return rport;
2661
2662delete_rport:
2663 transport_destroy_device(dev);
1da177e4
LT
2664 spin_lock_irqsave(shost->host_lock, flags);
2665 list_del(&rport->peers);
4cd38e38 2666 scsi_host_put(shost); /* for fc_host->rport list */
1da177e4
LT
2667 spin_unlock_irqrestore(shost->host_lock, flags);
2668 put_device(dev->parent);
2669 kfree(rport);
2670 return NULL;
2671}
2672
2673/**
eb44820c 2674 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
1da177e4
LT
2675 * @shost: scsi host the remote port is connected to.
2676 * @channel: Channel on shost port connected to.
2677 * @ids: The world wide names, fc address, and FC4 port
2678 * roles for the remote port.
2679 *
2680 * The LLDD calls this routine to notify the transport of the existence
2681 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2682 * of the port, it's FC address (port_id), and the FC4 roles that are
2683 * active for the port.
2684 *
2685 * For ports that are FCP targets (aka scsi targets), the FC transport
2686 * maintains consistent target id bindings on behalf of the LLDD.
2687 * A consistent target id binding is an assignment of a target id to
2688 * a remote port identifier, which persists while the scsi host is
2689 * attached. The remote port can disappear, then later reappear, and
2690 * it's target id assignment remains the same. This allows for shifts
2691 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2692 * changes to the scsi subsystem which is based on scsi host number and
2693 * target id values. Bindings are only valid during the attachment of
2694 * the scsi host. If the host detaches, then later re-attaches, target
2695 * id bindings may change.
2696 *
2697 * This routine is responsible for returning a remote port structure.
2698 * The routine will search the list of remote ports it maintains
2699 * internally on behalf of consistent target id mappings. If found, the
2700 * remote port structure will be reused. Otherwise, a new remote port
2701 * structure will be allocated.
2702 *
2703 * Whenever a remote port is allocated, a new fc_remote_port class
2704 * device is created.
2705 *
2706 * Should not be called from interrupt context.
2707 *
2708 * Notes:
2709 * This routine assumes no locks are held on entry.
eb44820c 2710 */
1da177e4
LT
2711struct fc_rport *
2712fc_remote_port_add(struct Scsi_Host *shost, int channel,
2713 struct fc_rport_identifiers *ids)
2714{
19a7b4ae 2715 struct fc_internal *fci = to_fc_internal(shost->transportt);
aedf3497 2716 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4
LT
2717 struct fc_rport *rport;
2718 unsigned long flags;
2719 int match = 0;
2720
aedf3497
JS
2721 /* ensure any stgt delete functions are done */
2722 fc_flush_work(shost);
2723
19a7b4ae
JSEC
2724 /*
2725 * Search the list of "active" rports, for an rport that has been
2726 * deleted, but we've held off the real delete while the target
2727 * is in a "blocked" state.
2728 */
2729 spin_lock_irqsave(shost->host_lock, flags);
2730
aedf3497 2731 list_for_each_entry(rport, &fc_host->rports, peers) {
19a7b4ae 2732
f091fb8c
HR
2733 if ((rport->port_state == FC_PORTSTATE_BLOCKED ||
2734 rport->port_state == FC_PORTSTATE_NOTPRESENT) &&
19a7b4ae
JSEC
2735 (rport->channel == channel)) {
2736
aedf3497 2737 switch (fc_host->tgtid_bind_type) {
19a7b4ae
JSEC
2738 case FC_TGTID_BIND_BY_WWPN:
2739 case FC_TGTID_BIND_NONE:
2740 if (rport->port_name == ids->port_name)
2741 match = 1;
2742 break;
2743 case FC_TGTID_BIND_BY_WWNN:
2744 if (rport->node_name == ids->node_name)
2745 match = 1;
2746 break;
2747 case FC_TGTID_BIND_BY_ID:
2748 if (rport->port_id == ids->port_id)
2749 match = 1;
2750 break;
2751 }
2752
2753 if (match) {
19a7b4ae
JSEC
2754
2755 memcpy(&rport->node_name, &ids->node_name,
2756 sizeof(rport->node_name));
2757 memcpy(&rport->port_name, &ids->port_name,
2758 sizeof(rport->port_name));
2759 rport->port_id = ids->port_id;
2760
2761 rport->port_state = FC_PORTSTATE_ONLINE;
2762 rport->roles = ids->roles;
2763
2764 spin_unlock_irqrestore(shost->host_lock, flags);
2765
2766 if (fci->f->dd_fcrport_size)
2767 memset(rport->dd_data, 0,
2768 fci->f->dd_fcrport_size);
2769
2770 /*
92740b24
JS
2771 * If we were not a target, cancel the
2772 * io terminate and rport timers, and
2773 * we're done.
2774 *
2775 * If we were a target, but our new role
2776 * doesn't indicate a target, leave the
2777 * timers running expecting the role to
2778 * change as the target fully logs in. If
2779 * it doesn't, the target will be torn down.
2780 *
2781 * If we were a target, and our role shows
2782 * we're still a target, cancel the timers
2783 * and kick off a scan.
19a7b4ae 2784 */
19a7b4ae 2785
92740b24
JS
2786 /* was a target, not in roles */
2787 if ((rport->scsi_target_id != -1) &&
a53eb5e0 2788 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
92740b24 2789 return rport;
19a7b4ae
JSEC
2790
2791 /*
92740b24
JS
2792 * Stop the fail io and dev_loss timers.
2793 * If they flush, the port_state will
2794 * be checked and will NOOP the function.
19a7b4ae 2795 */
0f29b966
JS
2796 if (!cancel_delayed_work(&rport->fail_io_work))
2797 fc_flush_devloss(shost);
92740b24 2798 if (!cancel_delayed_work(&rport->dev_loss_work))
aedf3497
JS
2799 fc_flush_devloss(shost);
2800
2801 spin_lock_irqsave(shost->host_lock, flags);
2802
fff9d40c 2803 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
4be98c0c
JS
2804 FC_RPORT_DEVLOSS_PENDING |
2805 FC_RPORT_DEVLOSS_CALLBK_DONE);
19a7b4ae 2806
8fb2ef89
MC
2807 spin_unlock_irqrestore(shost->host_lock, flags);
2808
92740b24
JS
2809 /* if target, initiate a scan */
2810 if (rport->scsi_target_id != -1) {
5d9fb5cc
MC
2811 scsi_target_unblock(&rport->dev,
2812 SDEV_RUNNING);
8fb2ef89
MC
2813 spin_lock_irqsave(shost->host_lock,
2814 flags);
92740b24
JS
2815 rport->flags |= FC_RPORT_SCAN_PENDING;
2816 scsi_queue_work(shost,
2817 &rport->scan_work);
2818 spin_unlock_irqrestore(shost->host_lock,
2819 flags);
8fb2ef89 2820 }
a0785edf 2821
9e4f5e29
JS
2822 fc_bsg_goose_queue(rport);
2823
19a7b4ae
JSEC
2824 return rport;
2825 }
2826 }
2827 }
2828
92740b24
JS
2829 /*
2830 * Search the bindings array
2831 * Note: if never a FCP target, you won't be on this list
2832 */
aedf3497 2833 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
1da177e4
LT
2834
2835 /* search for a matching consistent binding */
2836
aedf3497 2837 list_for_each_entry(rport, &fc_host->rport_bindings,
1da177e4
LT
2838 peers) {
2839 if (rport->channel != channel)
2840 continue;
2841
aedf3497 2842 switch (fc_host->tgtid_bind_type) {
1da177e4
LT
2843 case FC_TGTID_BIND_BY_WWPN:
2844 if (rport->port_name == ids->port_name)
2845 match = 1;
2846 break;
2847 case FC_TGTID_BIND_BY_WWNN:
2848 if (rport->node_name == ids->node_name)
2849 match = 1;
2850 break;
2851 case FC_TGTID_BIND_BY_ID:
2852 if (rport->port_id == ids->port_id)
2853 match = 1;
2854 break;
2855 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2856 break;
2857 }
2858
2859 if (match) {
aedf3497 2860 list_move_tail(&rport->peers, &fc_host->rports);
1da177e4
LT
2861 break;
2862 }
2863 }
2864
1da177e4
LT
2865 if (match) {
2866 memcpy(&rport->node_name, &ids->node_name,
2867 sizeof(rport->node_name));
2868 memcpy(&rport->port_name, &ids->port_name,
2869 sizeof(rport->port_name));
2870 rport->port_id = ids->port_id;
1da177e4 2871 rport->port_state = FC_PORTSTATE_ONLINE;
fff9d40c 2872 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
1da177e4 2873
19a7b4ae
JSEC
2874 if (fci->f->dd_fcrport_size)
2875 memset(rport->dd_data, 0,
2876 fci->f->dd_fcrport_size);
8fb2ef89
MC
2877 spin_unlock_irqrestore(shost->host_lock, flags);
2878
675195d0 2879 fc_remote_port_rolechg(rport, ids->roles);
1da177e4
LT
2880 return rport;
2881 }
2882 }
2883
19a7b4ae
JSEC
2884 spin_unlock_irqrestore(shost->host_lock, flags);
2885
1da177e4 2886 /* No consistent binding found - create new remote port entry */
558b88fe 2887 rport = fc_remote_port_create(shost, channel, ids);
1da177e4
LT
2888
2889 return rport;
2890}
2891EXPORT_SYMBOL(fc_remote_port_add);
2892
1da177e4
LT
2893
2894/**
eb44820c 2895 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
1da177e4
LT
2896 * @rport: The remote port that no longer exists
2897 *
2898 * The LLDD calls this routine to notify the transport that a remote
2899 * port is no longer part of the topology. Note: Although a port
2900 * may no longer be part of the topology, it may persist in the remote
19a7b4ae 2901 * ports displayed by the fc_host. We do this under 2 conditions:
739aca06 2902 *
eb44820c 2903 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
739aca06
MCC
2904 * This allows the port to temporarily disappear, then reappear without
2905 * disrupting the SCSI device tree attached to it. During the "blocked"
2906 * period the port will still exist.
2907 *
eb44820c 2908 * 2) If the port was a scsi target and disappears for longer than we
739aca06
MCC
2909 * expect, we'll delete the port and the tear down the SCSI device tree
2910 * attached to it. However, we want to semi-persist the target id assigned
2911 * to that port if it eventually does exist. The port structure will
2912 * remain (although with minimal information) so that the target id
90311148 2913 * bindings also remain.
1da177e4
LT
2914 *
2915 * If the remote port is not an FCP Target, it will be fully torn down
2916 * and deallocated, including the fc_remote_port class device.
2917 *
19a7b4ae
JSEC
2918 * If the remote port is an FCP Target, the port will be placed in a
2919 * temporary blocked state. From the LLDD's perspective, the rport no
2920 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2921 * exists, but all sdevs on it are blocked from further I/O. The following
eb44820c
RL
2922 * is then expected.
2923 *
19a7b4ae
JSEC
2924 * If the remote port does not return (signaled by a LLDD call to
2925 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2926 * scsi target is removed - killing all outstanding i/o and removing the
134699d2 2927 * scsi devices attached to it. The port structure will be marked Not
19a7b4ae
JSEC
2928 * Present and be partially cleared, leaving only enough information to
2929 * recognize the remote port relative to the scsi target id binding if
2930 * it later appears. The port will remain as long as there is a valid
2931 * binding (e.g. until the user changes the binding type or unloads the
2932 * scsi host with the binding).
1da177e4 2933 *
19a7b4ae
JSEC
2934 * If the remote port returns within the dev_loss_tmo value (and matches
2935 * according to the target id binding type), the port structure will be
2936 * reused. If it is no longer a SCSI target, the target will be torn
2937 * down. If it continues to be a SCSI target, then the target will be
2938 * unblocked (allowing i/o to be resumed), and a scan will be activated
2939 * to ensure that all luns are detected.
2940 *
2941 * Called from normal process context only - cannot be called from interrupt.
1da177e4
LT
2942 *
2943 * Notes:
2944 * This routine assumes no locks are held on entry.
eb44820c 2945 */
1da177e4
LT
2946void
2947fc_remote_port_delete(struct fc_rport *rport)
2948{
aedf3497 2949 struct Scsi_Host *shost = rport_to_shost(rport);
36dd288f 2950 unsigned long timeout = rport->dev_loss_tmo;
aedf3497
JS
2951 unsigned long flags;
2952
2953 /*
2954 * No need to flush the fc_host work_q's, as all adds are synchronous.
2955 *
2956 * We do need to reclaim the rport scan work element, so eventually
2957 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2958 * there's still a scan pending.
2959 */
2960
2961 spin_lock_irqsave(shost->host_lock, flags);
1da177e4 2962
92740b24 2963 if (rport->port_state != FC_PORTSTATE_ONLINE) {
aedf3497 2964 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4
LT
2965 return;
2966 }
2967
92740b24
JS
2968 /*
2969 * In the past, we if this was not an FCP-Target, we would
2970 * unconditionally just jump to deleting the rport.
2971 * However, rports can be used as node containers by the LLDD,
2972 * and its not appropriate to just terminate the rport at the
2973 * first sign of a loss in connectivity. The LLDD may want to
2974 * send ELS traffic to re-validate the login. If the rport is
2975 * immediately deleted, it makes it inappropriate for a node
2976 * container.
2977 * So... we now unconditionally wait dev_loss_tmo before
2978 * destroying an rport.
2979 */
2980
aedf3497
JS
2981 rport->port_state = FC_PORTSTATE_BLOCKED;
2982
2983 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2984
2985 spin_unlock_irqrestore(shost->host_lock, flags);
2986
19a7b4ae 2987 scsi_target_block(&rport->dev);
1da177e4 2988
0f29b966
JS
2989 /* see if we need to kill io faster than waiting for device loss */
2990 if ((rport->fast_io_fail_tmo != -1) &&
fff9d40c 2991 (rport->fast_io_fail_tmo < timeout))
0f29b966
JS
2992 fc_queue_devloss_work(shost, &rport->fail_io_work,
2993 rport->fast_io_fail_tmo * HZ);
2994
19a7b4ae 2995 /* cap the length the devices can be blocked until they are deleted */
aedf3497 2996 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
1da177e4
LT
2997}
2998EXPORT_SYMBOL(fc_remote_port_delete);
2999
3000/**
eb44820c 3001 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
1da177e4 3002 * @rport: The remote port that changed.
eb44820c 3003 * @roles: New roles for this port.
1da177e4 3004 *
eb44820c
RL
3005 * Description: The LLDD calls this routine to notify the transport that the
3006 * roles on a remote port may have changed. The largest effect of this is
1da177e4
LT
3007 * if a port now becomes a FCP Target, it must be allocated a
3008 * scsi target id. If the port is no longer a FCP target, any
3009 * scsi target id value assigned to it will persist in case the
3010 * role changes back to include FCP Target. No changes in the scsi
3011 * midlayer will be invoked if the role changes (in the expectation
3012 * that the role will be resumed. If it doesn't normal error processing
3013 * will take place).
3014 *
3015 * Should not be called from interrupt context.
3016 *
3017 * Notes:
3018 * This routine assumes no locks are held on entry.
eb44820c 3019 */
1da177e4
LT
3020void
3021fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
3022{
3023 struct Scsi_Host *shost = rport_to_shost(rport);
aedf3497 3024 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4
LT
3025 unsigned long flags;
3026 int create = 0;
3027
1da177e4 3028 spin_lock_irqsave(shost->host_lock, flags);
a53eb5e0 3029 if (roles & FC_PORT_ROLE_FCP_TARGET) {
19a7b4ae
JSEC
3030 if (rport->scsi_target_id == -1) {
3031 rport->scsi_target_id = fc_host->next_target_id++;
3032 create = 1;
a53eb5e0 3033 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
19a7b4ae 3034 create = 1;
1da177e4 3035 }
1da177e4 3036
19a7b4ae
JSEC
3037 rport->roles = roles;
3038
aedf3497
JS
3039 spin_unlock_irqrestore(shost->host_lock, flags);
3040
19a7b4ae
JSEC
3041 if (create) {
3042 /*
3043 * There may have been a delete timer running on the
3044 * port. Ensure that it is cancelled as we now know
3045 * the port is an FCP Target.
134699d2 3046 * Note: we know the rport exists and is in an online
19a7b4ae
JSEC
3047 * state as the LLDD would not have had an rport
3048 * reference to pass us.
3049 *
3050 * Take no action on the del_timer failure as the state
3051 * machine state change will validate the
3052 * transaction.
3053 */
0f29b966
JS
3054 if (!cancel_delayed_work(&rport->fail_io_work))
3055 fc_flush_devloss(shost);
19a7b4ae 3056 if (!cancel_delayed_work(&rport->dev_loss_work))
aedf3497
JS
3057 fc_flush_devloss(shost);
3058
3059 spin_lock_irqsave(shost->host_lock, flags);
fff9d40c 3060 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
0eecee41
JS
3061 FC_RPORT_DEVLOSS_PENDING |
3062 FC_RPORT_DEVLOSS_CALLBK_DONE);
aedf3497
JS
3063 spin_unlock_irqrestore(shost->host_lock, flags);
3064
3065 /* ensure any stgt delete functions are done */
3066 fc_flush_work(shost);
19a7b4ae 3067
5d9fb5cc 3068 scsi_target_unblock(&rport->dev, SDEV_RUNNING);
1da177e4 3069 /* initiate a scan of the target */
aedf3497
JS
3070 spin_lock_irqsave(shost->host_lock, flags);
3071 rport->flags |= FC_RPORT_SCAN_PENDING;
1da177e4 3072 scsi_queue_work(shost, &rport->scan_work);
aedf3497 3073 spin_unlock_irqrestore(shost->host_lock, flags);
19a7b4ae 3074 }
1da177e4
LT
3075}
3076EXPORT_SYMBOL(fc_remote_port_rolechg);
3077
3078/**
eb44820c 3079 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
92740b24 3080 * @work: rport target that failed to reappear in the allotted time.
eb44820c
RL
3081 *
3082 * Description: An attempt to delete a remote port blocks, and if it fails
3083 * to return in the allotted time this gets called.
3084 */
1da177e4 3085static void
c4028958 3086fc_timeout_deleted_rport(struct work_struct *work)
1da177e4 3087{
c4028958
DH
3088 struct fc_rport *rport =
3089 container_of(work, struct fc_rport, dev_loss_work.work);
19a7b4ae 3090 struct Scsi_Host *shost = rport_to_shost(rport);
4be98c0c 3091 struct fc_internal *i = to_fc_internal(shost->transportt);
aedf3497 3092 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
19a7b4ae 3093 unsigned long flags;
8798a694 3094 int do_callback = 0;
1da177e4 3095
19a7b4ae 3096 spin_lock_irqsave(shost->host_lock, flags);
1da177e4 3097
aedf3497
JS
3098 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3099
1da177e4 3100 /*
92740b24
JS
3101 * If the port is ONLINE, then it came back. If it was a SCSI
3102 * target, validate it still is. If not, tear down the
3103 * scsi_target on it.
1da177e4 3104 */
aedf3497 3105 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
92740b24 3106 (rport->scsi_target_id != -1) &&
a53eb5e0 3107 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
19a7b4ae 3108 dev_printk(KERN_ERR, &rport->dev,
aedf3497
JS
3109 "blocked FC remote port time out: no longer"
3110 " a FCP target, removing starget\n");
aedf3497 3111 spin_unlock_irqrestore(shost->host_lock, flags);
5d9fb5cc 3112 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
a0785edf 3113 fc_queue_work(shost, &rport->stgt_delete_work);
19a7b4ae
JSEC
3114 return;
3115 }
1da177e4 3116
92740b24 3117 /* NOOP state - we're flushing workq's */
19a7b4ae
JSEC
3118 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3119 spin_unlock_irqrestore(shost->host_lock, flags);
3120 dev_printk(KERN_ERR, &rport->dev,
92740b24
JS
3121 "blocked FC remote port time out: leaving"
3122 " rport%s alone\n",
3123 (rport->scsi_target_id != -1) ? " and starget" : "");
19a7b4ae
JSEC
3124 return;
3125 }
1da177e4 3126
92740b24
JS
3127 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3128 (rport->scsi_target_id == -1)) {
aedf3497
JS
3129 list_del(&rport->peers);
3130 rport->port_state = FC_PORTSTATE_DELETED;
19a7b4ae 3131 dev_printk(KERN_ERR, &rport->dev,
92740b24
JS
3132 "blocked FC remote port time out: removing"
3133 " rport%s\n",
3134 (rport->scsi_target_id != -1) ? " and starget" : "");
aedf3497
JS
3135 fc_queue_work(shost, &rport->rport_delete_work);
3136 spin_unlock_irqrestore(shost->host_lock, flags);
19a7b4ae
JSEC
3137 return;
3138 }
1da177e4 3139
19a7b4ae
JSEC
3140 dev_printk(KERN_ERR, &rport->dev,
3141 "blocked FC remote port time out: removing target and "
3142 "saving binding\n");
3143
aedf3497 3144 list_move_tail(&rport->peers, &fc_host->rport_bindings);
1da177e4
LT
3145
3146 /*
19a7b4ae
JSEC
3147 * Note: We do not remove or clear the hostdata area. This allows
3148 * host-specific target data to persist along with the
3149 * scsi_target_id. It's up to the host to manage it's hostdata area.
1da177e4 3150 */
1da177e4 3151
19a7b4ae
JSEC
3152 /*
3153 * Reinitialize port attributes that may change if the port comes back.
3154 */
3155 rport->maxframe_size = -1;
3156 rport->supported_classes = FC_COS_UNSPECIFIED;
a53eb5e0 3157 rport->roles = FC_PORT_ROLE_UNKNOWN;
aedf3497 3158 rport->port_state = FC_PORTSTATE_NOTPRESENT;
fff9d40c 3159 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
1da177e4 3160
f78badb1
JS
3161 /*
3162 * Pre-emptively kill I/O rather than waiting for the work queue
3163 * item to teardown the starget. (FCOE libFC folks prefer this
3164 * and to have the rport_port_id still set when it's done).
3165 */
3166 spin_unlock_irqrestore(shost->host_lock, flags);
3167 fc_terminate_rport_io(rport);
3168
8798a694
MR
3169 spin_lock_irqsave(shost->host_lock, flags);
3170
3171 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
f78badb1 3172
8798a694
MR
3173 /* remove the identifiers that aren't used in the consisting binding */
3174 switch (fc_host->tgtid_bind_type) {
3175 case FC_TGTID_BIND_BY_WWPN:
3176 rport->node_name = -1;
3177 rport->port_id = -1;
3178 break;
3179 case FC_TGTID_BIND_BY_WWNN:
3180 rport->port_name = -1;
3181 rport->port_id = -1;
3182 break;
3183 case FC_TGTID_BIND_BY_ID:
3184 rport->node_name = -1;
3185 rport->port_name = -1;
3186 break;
3187 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3188 break;
3189 }
3190
3191 /*
3192 * As this only occurs if the remote port (scsi target)
3193 * went away and didn't come back - we'll remove
3194 * all attached scsi devices.
3195 */
3196 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3197 fc_queue_work(shost, &rport->stgt_delete_work);
3198
3199 do_callback = 1;
19a7b4ae
JSEC
3200 }
3201
8798a694 3202 spin_unlock_irqrestore(shost->host_lock, flags);
4be98c0c
JS
3203
3204 /*
3205 * Notify the driver that the rport is now dead. The LLDD will
3206 * also guarantee that any communication to the rport is terminated
3207 *
3208 * Note: we set the CALLBK_DONE flag above to correspond
3209 */
8798a694 3210 if (do_callback && i->f->dev_loss_tmo_callbk)
4be98c0c 3211 i->f->dev_loss_tmo_callbk(rport);
1da177e4 3212}
1da177e4 3213
4be98c0c 3214
0f29b966 3215/**
eb44820c 3216 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
c4028958 3217 * @work: rport to terminate io on.
0f29b966
JS
3218 *
3219 * Notes: Only requests the failure of the io, not that all are flushed
3220 * prior to returning.
eb44820c 3221 */
0f29b966 3222static void
c4028958 3223fc_timeout_fail_rport_io(struct work_struct *work)
0f29b966 3224{
c4028958
DH
3225 struct fc_rport *rport =
3226 container_of(work, struct fc_rport, fail_io_work.work);
0f29b966
JS
3227
3228 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3229 return;
3230
fff9d40c
MC
3231 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3232 fc_terminate_rport_io(rport);
0f29b966
JS
3233}
3234
1da177e4
LT
3235/**
3236 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
c4028958 3237 * @work: remote port to be scanned.
eb44820c 3238 */
1da177e4 3239static void
c4028958 3240fc_scsi_scan_rport(struct work_struct *work)
1da177e4 3241{
c4028958
DH
3242 struct fc_rport *rport =
3243 container_of(work, struct fc_rport, scan_work);
aedf3497 3244 struct Scsi_Host *shost = rport_to_shost(rport);
03f002f7 3245 struct fc_internal *i = to_fc_internal(shost->transportt);
42e33148
JSEC
3246 unsigned long flags;
3247
aedf3497 3248 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
03f002f7
CS
3249 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3250 !(i->f->disable_target_scan)) {
aedf3497 3251 scsi_scan_target(&rport->dev, rport->channel,
1d645088
HR
3252 rport->scsi_target_id, SCAN_WILD_CARD,
3253 SCSI_SCAN_RESCAN);
42e33148 3254 }
aedf3497
JS
3255
3256 spin_lock_irqsave(shost->host_lock, flags);
3257 rport->flags &= ~FC_RPORT_SCAN_PENDING;
42e33148
JSEC
3258 spin_unlock_irqrestore(shost->host_lock, flags);
3259}
3260
65d430fa 3261/**
67b46525
SM
3262 * fc_block_rport() - Block SCSI eh thread for blocked fc_rport.
3263 * @rport: Remote port that scsi_eh is trying to recover.
65d430fa
CS
3264 *
3265 * This routine can be called from a FC LLD scsi_eh callback. It
3266 * blocks the scsi_eh thread until the fc_rport leaves the
2f2eb587
CS
3267 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3268 * necessary to avoid the scsi_eh failing recovery actions for blocked
3269 * rports which would lead to offlined SCSI devices.
3270 *
3271 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3272 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3273 * passed back to scsi_eh.
65d430fa 3274 */
67b46525 3275int fc_block_rport(struct fc_rport *rport)
65d430fa 3276{
67b46525 3277 struct Scsi_Host *shost = rport_to_shost(rport);
65d430fa
CS
3278 unsigned long flags;
3279
3280 spin_lock_irqsave(shost->host_lock, flags);
2f2eb587
CS
3281 while (rport->port_state == FC_PORTSTATE_BLOCKED &&
3282 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
65d430fa
CS
3283 spin_unlock_irqrestore(shost->host_lock, flags);
3284 msleep(1000);
3285 spin_lock_irqsave(shost->host_lock, flags);
3286 }
3287 spin_unlock_irqrestore(shost->host_lock, flags);
2f2eb587
CS
3288
3289 if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
3290 return FAST_IO_FAIL;
3291
3292 return 0;
65d430fa 3293}
67b46525
SM
3294EXPORT_SYMBOL(fc_block_rport);
3295
3296/**
3297 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3298 * @cmnd: SCSI command that scsi_eh is trying to recover
3299 *
3300 * This routine can be called from a FC LLD scsi_eh callback. It
3301 * blocks the scsi_eh thread until the fc_rport leaves the
3302 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3303 * necessary to avoid the scsi_eh failing recovery actions for blocked
3304 * rports which would lead to offlined SCSI devices.
3305 *
3306 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3307 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3308 * passed back to scsi_eh.
3309 */
3310int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3311{
3312 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3313
8d30371f
JT
3314 if (WARN_ON_ONCE(!rport))
3315 return FAST_IO_FAIL;
3316
67b46525
SM
3317 return fc_block_rport(rport);
3318}
65d430fa 3319EXPORT_SYMBOL(fc_block_scsi_eh);
42e33148 3320
a53eb5e0 3321/**
a30c3f69 3322 * fc_vport_setup - allocates and creates a FC virtual port.
a53eb5e0
JS
3323 * @shost: scsi host the virtual port is connected to.
3324 * @channel: Channel on shost port connected to.
3325 * @pdev: parent device for vport
3326 * @ids: The world wide names, FC4 port roles, etc for
3327 * the virtual port.
3328 * @ret_vport: The pointer to the created vport.
3329 *
3330 * Allocates and creates the vport structure, calls the parent host
134699d2 3331 * to instantiate the vport, this completes w/ class and sysfs creation.
a53eb5e0
JS
3332 *
3333 * Notes:
3334 * This routine assumes no locks are held on entry.
eb44820c 3335 */
a53eb5e0 3336static int
a30c3f69 3337fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
a53eb5e0
JS
3338 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3339{
3340 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3341 struct fc_internal *fci = to_fc_internal(shost->transportt);
3342 struct fc_vport *vport;
3343 struct device *dev;
3344 unsigned long flags;
3345 size_t size;
3346 int error;
3347
3348 *ret_vport = NULL;
3349
3350 if ( ! fci->f->vport_create)
3351 return -ENOENT;
3352
3353 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3354 vport = kzalloc(size, GFP_KERNEL);
3355 if (unlikely(!vport)) {
cadbd4a5 3356 printk(KERN_ERR "%s: allocation failure\n", __func__);
a53eb5e0
JS
3357 return -ENOMEM;
3358 }
3359
3360 vport->vport_state = FC_VPORT_UNKNOWN;
3361 vport->vport_last_state = FC_VPORT_UNKNOWN;
3362 vport->node_name = ids->node_name;
3363 vport->port_name = ids->port_name;
3364 vport->roles = ids->roles;
3365 vport->vport_type = ids->vport_type;
3366 if (fci->f->dd_fcvport_size)
3367 vport->dd_data = &vport[1];
3368 vport->shost = shost;
3369 vport->channel = channel;
3370 vport->flags = FC_VPORT_CREATING;
9ef3e4a4 3371 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
a53eb5e0
JS
3372
3373 spin_lock_irqsave(shost->host_lock, flags);
3374
3375 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3376 spin_unlock_irqrestore(shost->host_lock, flags);
3377 kfree(vport);
3378 return -ENOSPC;
3379 }
3380 fc_host->npiv_vports_inuse++;
3381 vport->number = fc_host->next_vport_number++;
3382 list_add_tail(&vport->peers, &fc_host->vports);
4cd38e38 3383 scsi_host_get(shost); /* for fc_host->vport list */
a53eb5e0
JS
3384
3385 spin_unlock_irqrestore(shost->host_lock, flags);
3386
3387 dev = &vport->dev;
3388 device_initialize(dev); /* takes self reference */
3389 dev->parent = get_device(pdev); /* takes parent reference */
3390 dev->release = fc_vport_dev_release;
71610f55
KS
3391 dev_set_name(dev, "vport-%d:%d-%d",
3392 shost->host_no, channel, vport->number);
a53eb5e0
JS
3393 transport_setup_device(dev);
3394
3395 error = device_add(dev);
3396 if (error) {
3397 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3398 goto delete_vport;
3399 }
3400 transport_add_device(dev);
3401 transport_configure_device(dev);
3402
3403 error = fci->f->vport_create(vport, ids->disable);
3404 if (error) {
3405 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3406 goto delete_vport_all;
3407 }
3408
3409 /*
3410 * if the parent isn't the physical adapter's Scsi_Host, ensure
134699d2 3411 * the Scsi_Host at least contains a symlink to the vport.
a53eb5e0
JS
3412 */
3413 if (pdev != &shost->shost_gendev) {
3414 error = sysfs_create_link(&shost->shost_gendev.kobj,
71610f55 3415 &dev->kobj, dev_name(dev));
a53eb5e0
JS
3416 if (error)
3417 printk(KERN_ERR
3418 "%s: Cannot create vport symlinks for "
3419 "%s, err=%d\n",
71610f55 3420 __func__, dev_name(dev), error);
a53eb5e0
JS
3421 }
3422 spin_lock_irqsave(shost->host_lock, flags);
3423 vport->flags &= ~FC_VPORT_CREATING;
3424 spin_unlock_irqrestore(shost->host_lock, flags);
3425
3426 dev_printk(KERN_NOTICE, pdev,
71610f55 3427 "%s created via shost%d channel %d\n", dev_name(dev),
a53eb5e0
JS
3428 shost->host_no, channel);
3429
3430 *ret_vport = vport;
3431
3432 return 0;
3433
3434delete_vport_all:
3435 transport_remove_device(dev);
3436 device_del(dev);
3437delete_vport:
3438 transport_destroy_device(dev);
3439 spin_lock_irqsave(shost->host_lock, flags);
3440 list_del(&vport->peers);
4cd38e38 3441 scsi_host_put(shost); /* for fc_host->vport list */
a53eb5e0
JS
3442 fc_host->npiv_vports_inuse--;
3443 spin_unlock_irqrestore(shost->host_lock, flags);
3444 put_device(dev->parent);
3445 kfree(vport);
3446
3447 return error;
3448}
3449
a30c3f69
AV
3450/**
3451 * fc_vport_create - Admin App or LLDD requests creation of a vport
3452 * @shost: scsi host the virtual port is connected to.
3453 * @channel: channel on shost port connected to.
3454 * @ids: The world wide names, FC4 port roles, etc for
3455 * the virtual port.
3456 *
3457 * Notes:
3458 * This routine assumes no locks are held on entry.
3459 */
3460struct fc_vport *
3461fc_vport_create(struct Scsi_Host *shost, int channel,
3462 struct fc_vport_identifiers *ids)
3463{
3464 int stat;
3465 struct fc_vport *vport;
3466
3467 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3468 ids, &vport);
3469 return stat ? NULL : vport;
3470}
3471EXPORT_SYMBOL(fc_vport_create);
a53eb5e0
JS
3472
3473/**
3474 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3475 * @vport: fc_vport to be terminated
3476 *
3477 * Calls the LLDD vport_delete() function, then deallocates and removes
3478 * the vport from the shost and object tree.
3479 *
3480 * Notes:
3481 * This routine assumes no locks are held on entry.
eb44820c 3482 */
a53eb5e0
JS
3483int
3484fc_vport_terminate(struct fc_vport *vport)
3485{
3486 struct Scsi_Host *shost = vport_to_shost(vport);
3487 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3488 struct fc_internal *i = to_fc_internal(shost->transportt);
3489 struct device *dev = &vport->dev;
3490 unsigned long flags;
3491 int stat;
3492
a53eb5e0
JS
3493 if (i->f->vport_delete)
3494 stat = i->f->vport_delete(vport);
3495 else
3496 stat = -ENOENT;
3497
3498 spin_lock_irqsave(shost->host_lock, flags);
3499 vport->flags &= ~FC_VPORT_DELETING;
3500 if (!stat) {
3501 vport->flags |= FC_VPORT_DELETED;
3502 list_del(&vport->peers);
3503 fc_host->npiv_vports_inuse--;
4cd38e38 3504 scsi_host_put(shost); /* for fc_host->vport list */
a53eb5e0
JS
3505 }
3506 spin_unlock_irqrestore(shost->host_lock, flags);
3507
3508 if (stat)
3509 return stat;
3510
3511 if (dev->parent != &shost->shost_gendev)
71610f55 3512 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
a53eb5e0
JS
3513 transport_remove_device(dev);
3514 device_del(dev);
3515 transport_destroy_device(dev);
3516
3517 /*
3518 * Removing our self-reference should mean our
3519 * release function gets called, which will drop the remaining
3520 * parent reference and free the data structure.
3521 */
3522 put_device(dev); /* for self-reference */
3523
3524 return 0; /* SUCCESS */
3525}
3526EXPORT_SYMBOL(fc_vport_terminate);
3527
9ef3e4a4
JS
3528/**
3529 * fc_vport_sched_delete - workq-based delete request for a vport
9ef3e4a4 3530 * @work: vport to be deleted.
eb44820c 3531 */
9ef3e4a4
JS
3532static void
3533fc_vport_sched_delete(struct work_struct *work)
3534{
3535 struct fc_vport *vport =
3536 container_of(work, struct fc_vport, vport_delete_work);
3537 int stat;
3538
3539 stat = fc_vport_terminate(vport);
3540 if (stat)
3541 dev_printk(KERN_ERR, vport->dev.parent,
3542 "%s: %s could not be deleted created via "
cadbd4a5 3543 "shost%d channel %d - error %d\n", __func__,
71610f55 3544 dev_name(&vport->dev), vport->shost->host_no,
9ef3e4a4
JS
3545 vport->channel, stat);
3546}
3547
a53eb5e0 3548
9e4f5e29
JS
3549/*
3550 * BSG support
3551 */
3552
9e4f5e29
JS
3553/**
3554 * fc_bsg_job_timeout - handler for when a bsg request timesout
3555 * @req: request that timed out
3556 */
3557static enum blk_eh_timer_return
3558fc_bsg_job_timeout(struct request *req)
3559{
b468b6a4 3560 struct bsg_job *job = blk_mq_rq_to_pdu(req);
cd21c605 3561 struct Scsi_Host *shost = fc_bsg_to_shost(job);
1d69b122 3562 struct fc_rport *rport = fc_bsg_to_rport(job);
9e4f5e29 3563 struct fc_internal *i = to_fc_internal(shost->transportt);
ad7660cc 3564 int err = 0, inflight = 0;
9e4f5e29 3565
1d69b122 3566 if (rport && rport->port_state == FC_PORTSTATE_BLOCKED)
9e4f5e29
JS
3567 return BLK_EH_RESET_TIMER;
3568
fb6f7c8d 3569 inflight = bsg_job_get(job);
9e4f5e29 3570
ad7660cc 3571 if (inflight && i->f->bsg_timeout) {
9e4f5e29
JS
3572 /* call LLDD to abort the i/o as it has timed out */
3573 err = i->f->bsg_timeout(job);
b8f08645 3574 if (err == -EAGAIN) {
fb6f7c8d 3575 bsg_job_put(job);
b8f08645
SS
3576 return BLK_EH_RESET_TIMER;
3577 } else if (err)
9e4f5e29
JS
3578 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3579 "abort failed with status %d\n", err);
3580 }
3581
9e4f5e29 3582 /* the blk_end_sync_io() doesn't check the error */
1fc2b62e 3583 if (inflight)
cd2f076f 3584 blk_mq_end_request(req, BLK_STS_IOERR);
1fc2b62e 3585 return BLK_EH_DONE;
9e4f5e29
JS
3586}
3587
9e4f5e29
JS
3588/**
3589 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
3590 * @shost: scsi host rport attached to
3591 * @job: bsg job to be processed
3592 */
a0f4bd7f 3593static int fc_bsg_host_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
9e4f5e29
JS
3594{
3595 struct fc_internal *i = to_fc_internal(shost->transportt);
01e0e15c
JT
3596 struct fc_bsg_request *bsg_request = job->request;
3597 struct fc_bsg_reply *bsg_reply = job->reply;
9e4f5e29
JS
3598 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3599 int ret;
3600
01e0e15c
JT
3601 /* check if we really have all the request data needed */
3602 if (job->request_len < cmdlen) {
3603 ret = -ENOMSG;
3604 goto fail_host_msg;
3605 }
3606
9e4f5e29 3607 /* Validate the host command */
01e0e15c 3608 switch (bsg_request->msgcode) {
9e4f5e29
JS
3609 case FC_BSG_HST_ADD_RPORT:
3610 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3611 break;
3612
3613 case FC_BSG_HST_DEL_RPORT:
3614 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3615 break;
3616
3617 case FC_BSG_HST_ELS_NOLOGIN:
3618 cmdlen += sizeof(struct fc_bsg_host_els);
3619 /* there better be a xmt and rcv payloads */
3620 if ((!job->request_payload.payload_len) ||
3621 (!job->reply_payload.payload_len)) {
3622 ret = -EINVAL;
3623 goto fail_host_msg;
3624 }
3625 break;
3626
3627 case FC_BSG_HST_CT:
3628 cmdlen += sizeof(struct fc_bsg_host_ct);
3629 /* there better be xmt and rcv payloads */
3630 if ((!job->request_payload.payload_len) ||
3631 (!job->reply_payload.payload_len)) {
3632 ret = -EINVAL;
3633 goto fail_host_msg;
3634 }
3635 break;
3636
3637 case FC_BSG_HST_VENDOR:
3638 cmdlen += sizeof(struct fc_bsg_host_vendor);
3639 if ((shost->hostt->vendor_id == 0L) ||
01e0e15c 3640 (bsg_request->rqst_data.h_vendor.vendor_id !=
9e4f5e29
JS
3641 shost->hostt->vendor_id)) {
3642 ret = -ESRCH;
3643 goto fail_host_msg;
3644 }
3645 break;
3646
3647 default:
3648 ret = -EBADR;
3649 goto fail_host_msg;
3650 }
3651
9e4f5e29
JS
3652 ret = i->f->bsg_request(job);
3653 if (!ret)
a0f4bd7f 3654 return 0;
9e4f5e29
JS
3655
3656fail_host_msg:
3657 /* return the errno failure code as the only status */
3658 BUG_ON(job->reply_len < sizeof(uint32_t));
01e0e15c
JT
3659 bsg_reply->reply_payload_rcv_len = 0;
3660 bsg_reply->result = ret;
9e4f5e29 3661 job->reply_len = sizeof(uint32_t);
06548160 3662 bsg_job_done(job, bsg_reply->result,
1abaede7 3663 bsg_reply->reply_payload_rcv_len);
a0f4bd7f 3664 return 0;
9e4f5e29
JS
3665}
3666
3667
3668/*
3669 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3670 * @rport: rport to be restarted
3671 */
3672static void
3673fc_bsg_goose_queue(struct fc_rport *rport)
3674{
378eeade 3675 struct request_queue *q = rport->rqst_q;
9e4f5e29 3676
cd2f076f
JA
3677 if (q)
3678 blk_mq_run_hw_queues(q, true);
9e4f5e29
JS
3679}
3680
9e4f5e29
JS
3681/**
3682 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
3683 * @shost: scsi host rport attached to
9e4f5e29
JS
3684 * @job: bsg job to be processed
3685 */
a0f4bd7f 3686static int fc_bsg_rport_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
9e4f5e29
JS
3687{
3688 struct fc_internal *i = to_fc_internal(shost->transportt);
01e0e15c
JT
3689 struct fc_bsg_request *bsg_request = job->request;
3690 struct fc_bsg_reply *bsg_reply = job->reply;
9e4f5e29
JS
3691 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3692 int ret;
3693
01e0e15c
JT
3694 /* check if we really have all the request data needed */
3695 if (job->request_len < cmdlen) {
3696 ret = -ENOMSG;
3697 goto fail_rport_msg;
3698 }
3699
9e4f5e29 3700 /* Validate the rport command */
01e0e15c 3701 switch (bsg_request->msgcode) {
9e4f5e29
JS
3702 case FC_BSG_RPT_ELS:
3703 cmdlen += sizeof(struct fc_bsg_rport_els);
3704 goto check_bidi;
3705
3706 case FC_BSG_RPT_CT:
3707 cmdlen += sizeof(struct fc_bsg_rport_ct);
3708check_bidi:
3709 /* there better be xmt and rcv payloads */
3710 if ((!job->request_payload.payload_len) ||
3711 (!job->reply_payload.payload_len)) {
3712 ret = -EINVAL;
3713 goto fail_rport_msg;
3714 }
3715 break;
3716 default:
3717 ret = -EBADR;
3718 goto fail_rport_msg;
3719 }
3720
9e4f5e29
JS
3721 ret = i->f->bsg_request(job);
3722 if (!ret)
a0f4bd7f 3723 return 0;
9e4f5e29
JS
3724
3725fail_rport_msg:
3726 /* return the errno failure code as the only status */
3727 BUG_ON(job->reply_len < sizeof(uint32_t));
01e0e15c
JT
3728 bsg_reply->reply_payload_rcv_len = 0;
3729 bsg_reply->result = ret;
9e4f5e29 3730 job->reply_len = sizeof(uint32_t);
06548160 3731 bsg_job_done(job, bsg_reply->result,
1abaede7 3732 bsg_reply->reply_payload_rcv_len);
a0f4bd7f 3733 return 0;
9e4f5e29
JS
3734}
3735
a0f4bd7f 3736static int fc_bsg_dispatch(struct bsg_job *job)
9e4f5e29 3737{
a0f4bd7f 3738 struct Scsi_Host *shost = fc_bsg_to_shost(job);
9e4f5e29 3739
a0f4bd7f
JT
3740 if (scsi_is_fc_rport(job->dev))
3741 return fc_bsg_rport_dispatch(shost, job);
3742 else
3743 return fc_bsg_host_dispatch(shost, job);
9e4f5e29
JS
3744}
3745
cd2f076f
JA
3746static blk_status_t fc_bsg_rport_prep(struct fc_rport *rport)
3747{
3748 if (rport->port_state == FC_PORTSTATE_BLOCKED &&
3749 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
3750 return BLK_STS_RESOURCE;
3751
3752 if (rport->port_state != FC_PORTSTATE_ONLINE)
3753 return BLK_STS_IOERR;
3754
3755 return BLK_STS_OK;
3756}
3757
3758
3759static int fc_bsg_dispatch_prep(struct bsg_job *job)
3760{
3761 struct fc_rport *rport = fc_bsg_to_rport(job);
3762 blk_status_t ret;
3763
3764 ret = fc_bsg_rport_prep(rport);
3765 switch (ret) {
3766 case BLK_STS_OK:
3767 break;
3768 case BLK_STS_RESOURCE:
3769 return -EAGAIN;
3770 default:
3771 return -EIO;
3772 }
3773
3774 return fc_bsg_dispatch(job);
3775}
3776
9e4f5e29
JS
3777/**
3778 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3779 * @shost: shost for fc_host
3780 * @fc_host: fc_host adding the structures to
3781 */
3782static int
3783fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
3784{
3785 struct device *dev = &shost->shost_gendev;
3786 struct fc_internal *i = to_fc_internal(shost->transportt);
3787 struct request_queue *q;
3c559ea8 3788 char bsg_name[20];
9e4f5e29
JS
3789
3790 fc_host->rqst_q = NULL;
3791
3792 if (!i->f->bsg_request)
3793 return -ENOTSUPP;
3794
3795 snprintf(bsg_name, sizeof(bsg_name),
3796 "fc_host%d", shost->host_no);
3797
aae3b069
JA
3798 q = bsg_setup_queue(dev, bsg_name, fc_bsg_dispatch, fc_bsg_job_timeout,
3799 i->f->dd_bsg_size);
8ae94eb6 3800 if (IS_ERR(q)) {
a0f4bd7f
JT
3801 dev_err(dev,
3802 "fc_host%d: bsg interface failed to initialize - setup queue\n",
3803 shost->host_no);
8ae94eb6 3804 return PTR_ERR(q);
9e4f5e29 3805 }
8ae94eb6 3806 __scsi_init_queue(shost, q);
a0f4bd7f 3807 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
9e4f5e29
JS
3808 fc_host->rqst_q = q;
3809 return 0;
3810}
3811
9e4f5e29
JS
3812/**
3813 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3814 * @shost: shost that rport is attached to
3815 * @rport: rport that the bsg hooks are being attached to
3816 */
3817static int
3818fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
3819{
3820 struct device *dev = &rport->dev;
3821 struct fc_internal *i = to_fc_internal(shost->transportt);
3822 struct request_queue *q;
9e4f5e29
JS
3823
3824 rport->rqst_q = NULL;
3825
3826 if (!i->f->bsg_request)
3827 return -ENOTSUPP;
3828
cd2f076f 3829 q = bsg_setup_queue(dev, dev_name(dev), fc_bsg_dispatch_prep,
aae3b069 3830 fc_bsg_job_timeout, i->f->dd_bsg_size);
8ae94eb6 3831 if (IS_ERR(q)) {
a0f4bd7f 3832 dev_err(dev, "failed to setup bsg queue\n");
8ae94eb6 3833 return PTR_ERR(q);
9e4f5e29 3834 }
8ae94eb6 3835 __scsi_init_queue(shost, q);
a0f4bd7f 3836 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
9e4f5e29
JS
3837 rport->rqst_q = q;
3838 return 0;
3839}
3840
3841
3842/**
3843 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
3844 * @q: the request_queue that is to be torn down.
78d16341
JS
3845 *
3846 * Notes:
3847 * Before unregistering the queue empty any requests that are blocked
3848 *
3849 *
9e4f5e29
JS
3850 */
3851static void
3852fc_bsg_remove(struct request_queue *q)
3853{
5e28b8d8 3854 bsg_remove_queue(q);
9e4f5e29
JS
3855}
3856
3857
9ef3e4a4
JS
3858/* Original Author: Martin Hicks */
3859MODULE_AUTHOR("James Smart");
1da177e4
LT
3860MODULE_DESCRIPTION("FC Transport Attributes");
3861MODULE_LICENSE("GPL");
3862
3863module_init(fc_transport_init);
3864module_exit(fc_transport_exit);