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