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