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