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