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