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