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