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