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