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