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