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