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