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  */
10 #ifndef SCSI_TRANSPORT_FC_H
11 #define SCSI_TRANSPORT_FC_H
12 
13 #include <linux/sched.h>
14 #include <linux/bsg-lib.h>
15 #include <asm/unaligned.h>
16 #include <scsi/scsi.h>
17 #include <scsi/scsi_netlink.h>
18 #include <scsi/scsi_host.h>
19 
20 struct scsi_transport_template;
21 
22 /*
23  * FC Port definitions - Following FC HBAAPI guidelines
24  *
25  * Note: Not all binary values for the different fields match HBAAPI.
26  *  Instead, we use densely packed ordinal values or enums.
27  *  We get away with this as we never present the actual binary values
28  *  externally. For sysfs, we always present the string that describes
29  *  the value. Thus, an admin doesn't need a magic HBAAPI decoder ring
30  *  to understand the values. The HBAAPI user-space library is free to
31  *  convert the strings into the HBAAPI-specified binary values.
32  *
33  * Note: Not all HBAAPI-defined values are contained in the definitions
34  *  below. Those not appropriate to an fc_host (e.g. FCP initiator) have
35  *  been removed.
36  */
37 
38 /*
39  * fc_port_type: If you alter this, you also need to alter scsi_transport_fc.c
40  * (for the ascii descriptions).
41  */
42 enum fc_port_type {
43 	FC_PORTTYPE_UNKNOWN,
44 	FC_PORTTYPE_OTHER,
45 	FC_PORTTYPE_NOTPRESENT,
46 	FC_PORTTYPE_NPORT,		/* Attached to FPort */
47 	FC_PORTTYPE_NLPORT,		/* (Public) Loop w/ FLPort */
48 	FC_PORTTYPE_LPORT,		/* (Private) Loop w/o FLPort */
49 	FC_PORTTYPE_PTP,		/* Point to Point w/ another NPort */
50 	FC_PORTTYPE_NPIV,		/* VPORT based on NPIV */
51 };
52 
53 
54 /*
55  * fc_port_state: If you alter this, you also need to alter scsi_transport_fc.c
56  * (for the ascii descriptions).
57  */
58 enum fc_port_state {
59 	FC_PORTSTATE_UNKNOWN,
60 	FC_PORTSTATE_NOTPRESENT,
61 	FC_PORTSTATE_ONLINE,
62 	FC_PORTSTATE_OFFLINE,		/* User has taken Port Offline */
63 	FC_PORTSTATE_BLOCKED,
64 	FC_PORTSTATE_BYPASSED,
65 	FC_PORTSTATE_DIAGNOSTICS,
66 	FC_PORTSTATE_LINKDOWN,
67 	FC_PORTSTATE_ERROR,
68 	FC_PORTSTATE_LOOPBACK,
69 	FC_PORTSTATE_DELETED,
70 	FC_PORTSTATE_MARGINAL,
71 };
72 
73 
74 /*
75  * fc_vport_state: If you alter this, you also need to alter
76  * scsi_transport_fc.c (for the ascii descriptions).
77  */
78 enum fc_vport_state {
79 	FC_VPORT_UNKNOWN,
80 	FC_VPORT_ACTIVE,
81 	FC_VPORT_DISABLED,
82 	FC_VPORT_LINKDOWN,
83 	FC_VPORT_INITIALIZING,
84 	FC_VPORT_NO_FABRIC_SUPP,
85 	FC_VPORT_NO_FABRIC_RSCS,
86 	FC_VPORT_FABRIC_LOGOUT,
87 	FC_VPORT_FABRIC_REJ_WWN,
88 	FC_VPORT_FAILED,
89 };
90 
91 
92 
93 /*
94  * FC Classes of Service
95  * Note: values are not enumerated, as they can be "or'd" together
96  * for reporting (e.g. report supported_classes). If you alter this list,
97  * you also need to alter scsi_transport_fc.c (for the ascii descriptions).
98  */
99 #define FC_COS_UNSPECIFIED		0
100 #define FC_COS_CLASS1			2
101 #define FC_COS_CLASS2			4
102 #define FC_COS_CLASS3			8
103 #define FC_COS_CLASS4			0x10
104 #define FC_COS_CLASS6			0x40
105 
106 /*
107  * FC Port Speeds
108  * Note: values are not enumerated, as they can be "or'd" together
109  * for reporting (e.g. report supported_speeds). If you alter this list,
110  * you also need to alter scsi_transport_fc.c (for the ascii descriptions).
111  */
112 #define FC_PORTSPEED_UNKNOWN		0 /* Unknown - transceiver
113 					     incapable of reporting */
114 #define FC_PORTSPEED_1GBIT		1
115 #define FC_PORTSPEED_2GBIT		2
116 #define FC_PORTSPEED_10GBIT		4
117 #define FC_PORTSPEED_4GBIT		8
118 #define FC_PORTSPEED_8GBIT		0x10
119 #define FC_PORTSPEED_16GBIT		0x20
120 #define FC_PORTSPEED_32GBIT		0x40
121 #define FC_PORTSPEED_20GBIT		0x80
122 #define FC_PORTSPEED_40GBIT		0x100
123 #define FC_PORTSPEED_50GBIT		0x200
124 #define FC_PORTSPEED_100GBIT		0x400
125 #define FC_PORTSPEED_25GBIT		0x800
126 #define FC_PORTSPEED_64GBIT		0x1000
127 #define FC_PORTSPEED_128GBIT		0x2000
128 #define FC_PORTSPEED_256GBIT		0x4000
129 #define FC_PORTSPEED_NOT_NEGOTIATED	(1 << 15) /* Speed not established */
130 
131 /*
132  * fc_tgtid_binding_type: If you alter this, you also need to alter
133  * scsi_transport_fc.c (for the ascii descriptions).
134  */
135 enum fc_tgtid_binding_type  {
136 	FC_TGTID_BIND_NONE,
137 	FC_TGTID_BIND_BY_WWPN,
138 	FC_TGTID_BIND_BY_WWNN,
139 	FC_TGTID_BIND_BY_ID,
140 };
141 
142 /*
143  * FC Port Roles
144  * Note: values are not enumerated, as they can be "or'd" together
145  * for reporting (e.g. report roles). If you alter this list,
146  * you also need to alter scsi_transport_fc.c (for the ascii descriptions).
147  */
148 #define FC_PORT_ROLE_UNKNOWN			0x00
149 #define FC_PORT_ROLE_FCP_TARGET			0x01
150 #define FC_PORT_ROLE_FCP_INITIATOR		0x02
151 #define FC_PORT_ROLE_IP_PORT			0x04
152 #define FC_PORT_ROLE_FCP_DUMMY_INITIATOR	0x08
153 #define FC_PORT_ROLE_NVME_INITIATOR		0x10
154 #define FC_PORT_ROLE_NVME_TARGET		0x20
155 #define FC_PORT_ROLE_NVME_DISCOVERY		0x40
156 
157 /* The following are for compatibility */
158 #define FC_RPORT_ROLE_UNKNOWN			FC_PORT_ROLE_UNKNOWN
159 #define FC_RPORT_ROLE_FCP_TARGET		FC_PORT_ROLE_FCP_TARGET
160 #define FC_RPORT_ROLE_FCP_INITIATOR		FC_PORT_ROLE_FCP_INITIATOR
161 #define FC_RPORT_ROLE_IP_PORT			FC_PORT_ROLE_IP_PORT
162 
163 
164 /* Macro for use in defining Virtual Port attributes */
165 #define FC_VPORT_ATTR(_name,_mode,_show,_store)		\
166 struct device_attribute dev_attr_vport_##_name = 	\
167 	__ATTR(_name,_mode,_show,_store)
168 
169 /*
170  * fc_vport_identifiers: This set of data contains all elements
171  * to uniquely identify and instantiate a FC virtual port.
172  *
173  * Notes:
174  *   symbolic_name: The driver is to append the symbolic_name string data
175  *      to the symbolic_node_name data that it generates by default.
176  *      the resulting combination should then be registered with the switch.
177  *      It is expected that things like Xen may stuff a VM title into
178  *      this field.
179  */
180 #define FC_VPORT_SYMBOLIC_NAMELEN		64
181 struct fc_vport_identifiers {
182 	u64 node_name;
183 	u64 port_name;
184 	u32 roles;
185 	bool disable;
186 	enum fc_port_type vport_type;	/* only FC_PORTTYPE_NPIV allowed */
187 	char symbolic_name[FC_VPORT_SYMBOLIC_NAMELEN];
188 };
189 
190 /*
191  * FC Virtual Port Attributes
192  *
193  * This structure exists for each FC port is a virtual FC port. Virtual
194  * ports share the physical link with the Physical port. Each virtual
195  * ports has a unique presence on the SAN, and may be instantiated via
196  * NPIV, Virtual Fabrics, or via additional ALPAs. As the vport is a
197  * unique presence, each vport has it's own view of the fabric,
198  * authentication privilege, and priorities.
199  *
200  * A virtual port may support 1 or more FC4 roles. Typically it is a
201  * FCP Initiator. It could be a FCP Target, or exist sole for an IP over FC
202  * roles. FC port attributes for the vport will be reported on any
203  * fc_host class object allocated for an FCP Initiator.
204  *
205  * --
206  *
207  * Fixed attributes are not expected to change. The driver is
208  * expected to set these values after receiving the fc_vport structure
209  * via the vport_create() call from the transport.
210  * The transport fully manages all get functions w/o driver interaction.
211  *
212  * Dynamic attributes are expected to change. The driver participates
213  * in all get/set operations via functions provided by the driver.
214  *
215  * Private attributes are transport-managed values. They are fully
216  * managed by the transport w/o driver interaction.
217  */
218 
219 struct fc_vport {
220 	/* Fixed Attributes */
221 
222 	/* Dynamic Attributes */
223 
224 	/* Private (Transport-managed) Attributes */
225 	enum fc_vport_state vport_state;
226 	enum fc_vport_state vport_last_state;
227 	u64 node_name;
228 	u64 port_name;
229 	u32 roles;
230 	u32 vport_id;		/* Admin Identifier for the vport */
231 	enum fc_port_type vport_type;
232 	char symbolic_name[FC_VPORT_SYMBOLIC_NAMELEN];
233 
234 	/* exported data */
235 	void *dd_data;			/* Used for driver-specific storage */
236 
237 	/* internal data */
238 	struct Scsi_Host *shost;	/* Physical Port Parent */
239 	unsigned int channel;
240 	u32 number;
241 	u8 flags;
242 	struct list_head peers;
243 	struct device dev;
244 	struct work_struct vport_delete_work;
245 } __attribute__((aligned(sizeof(unsigned long))));
246 
247 /* bit field values for struct fc_vport "flags" field: */
248 #define FC_VPORT_CREATING		0x01
249 #define FC_VPORT_DELETING		0x02
250 #define FC_VPORT_DELETED		0x04
251 #define FC_VPORT_DEL			0x06	/* Any DELETE state */
252 
253 #define	dev_to_vport(d)				\
254 	container_of(d, struct fc_vport, dev)
255 #define transport_class_to_vport(dev)		\
256 	dev_to_vport(dev->parent)
257 #define vport_to_shost(v)			\
258 	(v->shost)
259 #define vport_to_shost_channel(v)		\
260 	(v->channel)
261 #define vport_to_parent(v)			\
262 	(v->dev.parent)
263 
264 
265 /* Error return codes for vport_create() callback */
266 #define VPCERR_UNSUPPORTED		-ENOSYS		/* no driver/adapter
267 							   support */
268 #define VPCERR_BAD_WWN			-ENOTUNIQ	/* driver validation
269 							   of WWNs failed */
270 #define VPCERR_NO_FABRIC_SUPP		-EOPNOTSUPP	/* Fabric connection
271 							   is loop or the
272 							   Fabric Port does
273 							   not support NPIV */
274 
275 /*
276  * fc_rport_identifiers: This set of data contains all elements
277  * to uniquely identify a remote FC port. The driver uses this data
278  * to report the existence of a remote FC port in the topology. Internally,
279  * the transport uses this data for attributes and to manage consistent
280  * target id bindings.
281  */
282 struct fc_rport_identifiers {
283 	u64 node_name;
284 	u64 port_name;
285 	u32 port_id;
286 	u32 roles;
287 };
288 
289 /*
290  * Fabric Performance Impact Notification Statistics
291  */
292 struct fc_fpin_stats {
293 	/* Delivery */
294 	u64 dn;
295 	u64 dn_unknown;
296 	u64 dn_timeout;
297 	u64 dn_unable_to_route;
298 	u64 dn_device_specific;
299 
300 	/* Link Integrity */
301 	u64 li;
302 	u64 li_failure_unknown;
303 	u64 li_link_failure_count;
304 	u64 li_loss_of_sync_count;
305 	u64 li_loss_of_signals_count;
306 	u64 li_prim_seq_err_count;
307 	u64 li_invalid_tx_word_count;
308 	u64 li_invalid_crc_count;
309 	u64 li_device_specific;
310 
311 	/* Congestion/Peer Congestion */
312 	u64 cn;
313 	u64 cn_clear;
314 	u64 cn_lost_credit;
315 	u64 cn_credit_stall;
316 	u64 cn_oversubscription;
317 	u64 cn_device_specific;
318 };
319 
320 /* Macro for use in defining Remote Port attributes */
321 #define FC_RPORT_ATTR(_name,_mode,_show,_store)				\
322 struct device_attribute dev_attr_rport_##_name = 	\
323 	__ATTR(_name,_mode,_show,_store)
324 
325 
326 /*
327  * FC Remote Port Attributes
328  *
329  * This structure exists for each remote FC port that a LLDD notifies
330  * the subsystem of.  A remote FC port may or may not be a SCSI Target,
331  * also be a SCSI initiator, IP endpoint, etc. As such, the remote
332  * port is considered a separate entity, independent of "role" (such
333  * as scsi target).
334  *
335  * --
336  *
337  * Attributes are based on HBAAPI V2.0 definitions. Only those
338  * attributes that are determinable by the local port (aka Host)
339  * are contained.
340  *
341  * Fixed attributes are not expected to change. The driver is
342  * expected to set these values after successfully calling
343  * fc_remote_port_add(). The transport fully manages all get functions
344  * w/o driver interaction.
345  *
346  * Dynamic attributes are expected to change. The driver participates
347  * in all get/set operations via functions provided by the driver.
348  *
349  * Private attributes are transport-managed values. They are fully
350  * managed by the transport w/o driver interaction.
351  */
352 
353 struct fc_rport {	/* aka fc_starget_attrs */
354 	/* Fixed Attributes */
355 	u32 maxframe_size;
356 	u32 supported_classes;
357 
358 	/* Dynamic Attributes */
359 	u32 dev_loss_tmo;	/* Remote Port loss timeout in seconds. */
360 	struct fc_fpin_stats fpin_stats;
361 
362 	/* Private (Transport-managed) Attributes */
363 	u64 node_name;
364 	u64 port_name;
365 	u32 port_id;
366 	u32 roles;
367 	enum fc_port_state port_state;	/* Will only be ONLINE or UNKNOWN */
368 	u32 scsi_target_id;
369 	u32 fast_io_fail_tmo;
370 
371 	/* exported data */
372 	void *dd_data;			/* Used for driver-specific storage */
373 
374 	/* internal data */
375 	unsigned int channel;
376 	u32 number;
377 	u8 flags;
378 	struct list_head peers;
379 	struct device dev;
380  	struct delayed_work dev_loss_work;
381  	struct work_struct scan_work;
382  	struct delayed_work fail_io_work;
383  	struct work_struct stgt_delete_work;
384 	struct work_struct rport_delete_work;
385 	struct request_queue *rqst_q;	/* bsg support */
386 } __attribute__((aligned(sizeof(unsigned long))));
387 
388 /* bit field values for struct fc_rport "flags" field: */
389 #define FC_RPORT_DEVLOSS_PENDING	0x01
390 #define FC_RPORT_SCAN_PENDING		0x02
391 #define FC_RPORT_FAST_FAIL_TIMEDOUT	0x04
392 #define FC_RPORT_DEVLOSS_CALLBK_DONE	0x08
393 
394 #define	dev_to_rport(d)				\
395 	container_of(d, struct fc_rport, dev)
396 #define transport_class_to_rport(dev)	\
397 	dev_to_rport(dev->parent)
398 #define rport_to_shost(r)			\
399 	dev_to_shost(r->dev.parent)
400 
401 /*
402  * FC SCSI Target Attributes
403  *
404  * The SCSI Target is considered an extension of a remote port (as
405  * a remote port can be more than a SCSI Target). Within the scsi
406  * subsystem, we leave the Target as a separate entity. Doing so
407  * provides backward compatibility with prior FC transport api's,
408  * and lets remote ports be handled entirely within the FC transport
409  * and independently from the scsi subsystem. The drawback is that
410  * some data will be duplicated.
411  */
412 
413 struct fc_starget_attrs {	/* aka fc_target_attrs */
414 	/* Dynamic Attributes */
415 	u64 node_name;
416 	u64 port_name;
417 	u32 port_id;
418 };
419 
420 #define fc_starget_node_name(x) \
421 	(((struct fc_starget_attrs *)&(x)->starget_data)->node_name)
422 #define fc_starget_port_name(x)	\
423 	(((struct fc_starget_attrs *)&(x)->starget_data)->port_name)
424 #define fc_starget_port_id(x) \
425 	(((struct fc_starget_attrs *)&(x)->starget_data)->port_id)
426 
427 #define starget_to_rport(s)			\
428 	scsi_is_fc_rport(s->dev.parent) ? dev_to_rport(s->dev.parent) : NULL
429 
430 
431 /*
432  * FC Local Port (Host) Statistics
433  */
434 
435 /* FC Statistics - Following FC HBAAPI v2.0 guidelines */
436 struct fc_host_statistics {
437 	/* port statistics */
438 	u64 seconds_since_last_reset;
439 	u64 tx_frames;
440 	u64 tx_words;
441 	u64 rx_frames;
442 	u64 rx_words;
443 	u64 lip_count;
444 	u64 nos_count;
445 	u64 error_frames;
446 	u64 dumped_frames;
447 	u64 link_failure_count;
448 	u64 loss_of_sync_count;
449 	u64 loss_of_signal_count;
450 	u64 prim_seq_protocol_err_count;
451 	u64 invalid_tx_word_count;
452 	u64 invalid_crc_count;
453 
454 	/* fc4 statistics  (only FCP supported currently) */
455 	u64 fcp_input_requests;
456 	u64 fcp_output_requests;
457 	u64 fcp_control_requests;
458 	u64 fcp_input_megabytes;
459 	u64 fcp_output_megabytes;
460 	u64 fcp_packet_alloc_failures;	/* fcp packet allocation failures */
461 	u64 fcp_packet_aborts;		/* fcp packet aborted */
462 	u64 fcp_frame_alloc_failures;	/* fcp frame allocation failures */
463 
464 	/* fc exches statistics */
465 	u64 fc_no_free_exch;		/* no free exch memory */
466 	u64 fc_no_free_exch_xid;	/* no free exch id */
467 	u64 fc_xid_not_found;		/* exch not found for a response */
468 	u64 fc_xid_busy;		/* exch exist for new a request */
469 	u64 fc_seq_not_found;		/* seq is not found for exchange */
470 	u64 fc_non_bls_resp;		/* a non BLS response frame with
471 					   a sequence responder in new exch */
472 	/* Host Congestion Signals */
473 	u64 cn_sig_warn;
474 	u64 cn_sig_alarm;
475 };
476 
477 
478 /*
479  * FC Event Codes - Polled and Async, following FC HBAAPI v2.0 guidelines
480  */
481 
482 /*
483  * fc_host_event_code: If you alter this, you also need to alter
484  * scsi_transport_fc.c (for the ascii descriptions).
485  */
486 enum fc_host_event_code  {
487 	FCH_EVT_LIP			= 0x1,
488 	FCH_EVT_LINKUP			= 0x2,
489 	FCH_EVT_LINKDOWN		= 0x3,
490 	FCH_EVT_LIPRESET		= 0x4,
491 	FCH_EVT_RSCN			= 0x5,
492 	FCH_EVT_ADAPTER_CHANGE		= 0x103,
493 	FCH_EVT_PORT_UNKNOWN		= 0x200,
494 	FCH_EVT_PORT_OFFLINE		= 0x201,
495 	FCH_EVT_PORT_ONLINE		= 0x202,
496 	FCH_EVT_PORT_FABRIC		= 0x204,
497 	FCH_EVT_LINK_UNKNOWN		= 0x500,
498 	FCH_EVT_LINK_FPIN		= 0x501,
499 	FCH_EVT_VENDOR_UNIQUE		= 0xffff,
500 };
501 
502 
503 /*
504  * FC Local Port (Host) Attributes
505  *
506  * Attributes are based on HBAAPI V2.0 definitions.
507  * Note: OSDeviceName is determined by user-space library
508  *
509  * Fixed attributes are not expected to change. The driver is
510  * expected to set these values after successfully calling scsi_add_host().
511  * The transport fully manages all get functions w/o driver interaction.
512  *
513  * Dynamic attributes are expected to change. The driver participates
514  * in all get/set operations via functions provided by the driver.
515  *
516  * Private attributes are transport-managed values. They are fully
517  * managed by the transport w/o driver interaction.
518  */
519 
520 #define FC_FC4_LIST_SIZE		32
521 #define FC_SYMBOLIC_NAME_SIZE		256
522 #define FC_VERSION_STRING_SIZE		64
523 #define FC_SERIAL_NUMBER_SIZE		80
524 
525 struct fc_host_attrs {
526 	/* Fixed Attributes */
527 	u64 node_name;
528 	u64 port_name;
529 	u64 permanent_port_name;
530 	u32 supported_classes;
531 	u8  supported_fc4s[FC_FC4_LIST_SIZE];
532 	u32 supported_speeds;
533 	u32 maxframe_size;
534 	u16 max_npiv_vports;
535 	char serial_number[FC_SERIAL_NUMBER_SIZE];
536 	char manufacturer[FC_SERIAL_NUMBER_SIZE];
537 	char model[FC_SYMBOLIC_NAME_SIZE];
538 	char model_description[FC_SYMBOLIC_NAME_SIZE];
539 	char hardware_version[FC_VERSION_STRING_SIZE];
540 	char driver_version[FC_VERSION_STRING_SIZE];
541 	char firmware_version[FC_VERSION_STRING_SIZE];
542 	char optionrom_version[FC_VERSION_STRING_SIZE];
543 
544 	/* Dynamic Attributes */
545 	u32 port_id;
546 	enum fc_port_type port_type;
547 	enum fc_port_state port_state;
548 	u8  active_fc4s[FC_FC4_LIST_SIZE];
549 	u32 speed;
550 	u64 fabric_name;
551 	char symbolic_name[FC_SYMBOLIC_NAME_SIZE];
552 	char system_hostname[FC_SYMBOLIC_NAME_SIZE];
553 	u32 dev_loss_tmo;
554 	struct fc_fpin_stats fpin_stats;
555 
556 	/* Private (Transport-managed) Attributes */
557 	enum fc_tgtid_binding_type  tgtid_bind_type;
558 
559 	/* internal data */
560 	struct list_head rports;
561 	struct list_head rport_bindings;
562 	struct list_head vports;
563 	u32 next_rport_number;
564 	u32 next_target_id;
565 	u32 next_vport_number;
566 	u16 npiv_vports_inuse;
567 
568 	/* work queues for rport state manipulation */
569 	char work_q_name[20];
570 	struct workqueue_struct *work_q;
571 	char devloss_work_q_name[20];
572 	struct workqueue_struct *devloss_work_q;
573 
574 	/* bsg support */
575 	struct request_queue *rqst_q;
576 };
577 
578 #define shost_to_fc_host(x) \
579 	((struct fc_host_attrs *)(x)->shost_data)
580 
581 #define fc_host_node_name(x) \
582 	(((struct fc_host_attrs *)(x)->shost_data)->node_name)
583 #define fc_host_port_name(x)	\
584 	(((struct fc_host_attrs *)(x)->shost_data)->port_name)
585 #define fc_host_permanent_port_name(x)	\
586 	(((struct fc_host_attrs *)(x)->shost_data)->permanent_port_name)
587 #define fc_host_supported_classes(x)	\
588 	(((struct fc_host_attrs *)(x)->shost_data)->supported_classes)
589 #define fc_host_supported_fc4s(x)	\
590 	(((struct fc_host_attrs *)(x)->shost_data)->supported_fc4s)
591 #define fc_host_supported_speeds(x)	\
592 	(((struct fc_host_attrs *)(x)->shost_data)->supported_speeds)
593 #define fc_host_maxframe_size(x)	\
594 	(((struct fc_host_attrs *)(x)->shost_data)->maxframe_size)
595 #define fc_host_max_npiv_vports(x)	\
596 	(((struct fc_host_attrs *)(x)->shost_data)->max_npiv_vports)
597 #define fc_host_serial_number(x)	\
598 	(((struct fc_host_attrs *)(x)->shost_data)->serial_number)
599 #define fc_host_manufacturer(x)	\
600 	(((struct fc_host_attrs *)(x)->shost_data)->manufacturer)
601 #define fc_host_model(x)	\
602 	(((struct fc_host_attrs *)(x)->shost_data)->model)
603 #define fc_host_model_description(x)	\
604 	(((struct fc_host_attrs *)(x)->shost_data)->model_description)
605 #define fc_host_hardware_version(x)	\
606 	(((struct fc_host_attrs *)(x)->shost_data)->hardware_version)
607 #define fc_host_driver_version(x)	\
608 	(((struct fc_host_attrs *)(x)->shost_data)->driver_version)
609 #define fc_host_firmware_version(x)	\
610 	(((struct fc_host_attrs *)(x)->shost_data)->firmware_version)
611 #define fc_host_optionrom_version(x)	\
612 	(((struct fc_host_attrs *)(x)->shost_data)->optionrom_version)
613 #define fc_host_port_id(x)	\
614 	(((struct fc_host_attrs *)(x)->shost_data)->port_id)
615 #define fc_host_port_type(x)	\
616 	(((struct fc_host_attrs *)(x)->shost_data)->port_type)
617 #define fc_host_port_state(x)	\
618 	(((struct fc_host_attrs *)(x)->shost_data)->port_state)
619 #define fc_host_active_fc4s(x)	\
620 	(((struct fc_host_attrs *)(x)->shost_data)->active_fc4s)
621 #define fc_host_speed(x)	\
622 	(((struct fc_host_attrs *)(x)->shost_data)->speed)
623 #define fc_host_fabric_name(x)	\
624 	(((struct fc_host_attrs *)(x)->shost_data)->fabric_name)
625 #define fc_host_symbolic_name(x)	\
626 	(((struct fc_host_attrs *)(x)->shost_data)->symbolic_name)
627 #define fc_host_system_hostname(x)	\
628 	(((struct fc_host_attrs *)(x)->shost_data)->system_hostname)
629 #define fc_host_tgtid_bind_type(x) \
630 	(((struct fc_host_attrs *)(x)->shost_data)->tgtid_bind_type)
631 #define fc_host_rports(x) \
632 	(((struct fc_host_attrs *)(x)->shost_data)->rports)
633 #define fc_host_rport_bindings(x) \
634 	(((struct fc_host_attrs *)(x)->shost_data)->rport_bindings)
635 #define fc_host_vports(x) \
636 	(((struct fc_host_attrs *)(x)->shost_data)->vports)
637 #define fc_host_next_rport_number(x) \
638 	(((struct fc_host_attrs *)(x)->shost_data)->next_rport_number)
639 #define fc_host_next_target_id(x) \
640 	(((struct fc_host_attrs *)(x)->shost_data)->next_target_id)
641 #define fc_host_next_vport_number(x) \
642 	(((struct fc_host_attrs *)(x)->shost_data)->next_vport_number)
643 #define fc_host_npiv_vports_inuse(x)	\
644 	(((struct fc_host_attrs *)(x)->shost_data)->npiv_vports_inuse)
645 #define fc_host_work_q_name(x) \
646 	(((struct fc_host_attrs *)(x)->shost_data)->work_q_name)
647 #define fc_host_work_q(x) \
648 	(((struct fc_host_attrs *)(x)->shost_data)->work_q)
649 #define fc_host_devloss_work_q_name(x) \
650 	(((struct fc_host_attrs *)(x)->shost_data)->devloss_work_q_name)
651 #define fc_host_devloss_work_q(x) \
652 	(((struct fc_host_attrs *)(x)->shost_data)->devloss_work_q)
653 #define fc_host_dev_loss_tmo(x) \
654 	(((struct fc_host_attrs *)(x)->shost_data)->dev_loss_tmo)
655 
656 /* The functions by which the transport class and the driver communicate */
657 struct fc_function_template {
658 	void    (*get_rport_dev_loss_tmo)(struct fc_rport *);
659 	void	(*set_rport_dev_loss_tmo)(struct fc_rport *, u32);
660 
661 	void	(*get_starget_node_name)(struct scsi_target *);
662 	void	(*get_starget_port_name)(struct scsi_target *);
663 	void 	(*get_starget_port_id)(struct scsi_target *);
664 
665 	void 	(*get_host_port_id)(struct Scsi_Host *);
666 	void	(*get_host_port_type)(struct Scsi_Host *);
667 	void	(*get_host_port_state)(struct Scsi_Host *);
668 	void	(*get_host_active_fc4s)(struct Scsi_Host *);
669 	void	(*get_host_speed)(struct Scsi_Host *);
670 	void	(*get_host_fabric_name)(struct Scsi_Host *);
671 	void	(*get_host_symbolic_name)(struct Scsi_Host *);
672 	void	(*set_host_system_hostname)(struct Scsi_Host *);
673 
674 	struct fc_host_statistics * (*get_fc_host_stats)(struct Scsi_Host *);
675 	void	(*reset_fc_host_stats)(struct Scsi_Host *);
676 
677 	int	(*issue_fc_host_lip)(struct Scsi_Host *);
678 
679 	void    (*dev_loss_tmo_callbk)(struct fc_rport *);
680 	void	(*terminate_rport_io)(struct fc_rport *);
681 
682 	void	(*set_vport_symbolic_name)(struct fc_vport *);
683 	int  	(*vport_create)(struct fc_vport *, bool);
684 	int	(*vport_disable)(struct fc_vport *, bool);
685 	int  	(*vport_delete)(struct fc_vport *);
686 
687 	/* bsg support */
688 	int	(*bsg_request)(struct bsg_job *);
689 	int	(*bsg_timeout)(struct bsg_job *);
690 
691 	/* allocation lengths for host-specific data */
692 	u32	 			dd_fcrport_size;
693 	u32	 			dd_fcvport_size;
694 	u32				dd_bsg_size;
695 
696 	/*
697 	 * The driver sets these to tell the transport class it
698 	 * wants the attributes displayed in sysfs.  If the show_ flag
699 	 * is not set, the attribute will be private to the transport
700 	 * class
701 	 */
702 
703 	/* remote port fixed attributes */
704 	unsigned long	show_rport_maxframe_size:1;
705 	unsigned long	show_rport_supported_classes:1;
706 	unsigned long   show_rport_dev_loss_tmo:1;
707 
708 	/*
709 	 * target dynamic attributes
710 	 * These should all be "1" if the driver uses the remote port
711 	 * add/delete functions (so attributes reflect rport values).
712 	 */
713 	unsigned long	show_starget_node_name:1;
714 	unsigned long	show_starget_port_name:1;
715 	unsigned long	show_starget_port_id:1;
716 
717 	/* host fixed attributes */
718 	unsigned long	show_host_node_name:1;
719 	unsigned long	show_host_port_name:1;
720 	unsigned long	show_host_permanent_port_name:1;
721 	unsigned long	show_host_supported_classes:1;
722 	unsigned long	show_host_supported_fc4s:1;
723 	unsigned long	show_host_supported_speeds:1;
724 	unsigned long	show_host_maxframe_size:1;
725 	unsigned long	show_host_serial_number:1;
726 	unsigned long	show_host_manufacturer:1;
727 	unsigned long	show_host_model:1;
728 	unsigned long	show_host_model_description:1;
729 	unsigned long	show_host_hardware_version:1;
730 	unsigned long	show_host_driver_version:1;
731 	unsigned long	show_host_firmware_version:1;
732 	unsigned long	show_host_optionrom_version:1;
733 	/* host dynamic attributes */
734 	unsigned long	show_host_port_id:1;
735 	unsigned long	show_host_port_type:1;
736 	unsigned long	show_host_port_state:1;
737 	unsigned long	show_host_active_fc4s:1;
738 	unsigned long	show_host_speed:1;
739 	unsigned long	show_host_fabric_name:1;
740 	unsigned long	show_host_symbolic_name:1;
741 	unsigned long	show_host_system_hostname:1;
742 
743 	unsigned long	disable_target_scan:1;
744 };
745 
746 /**
747  * fc_remote_port_chkready - called to validate the remote port state
748  *   prior to initiating io to the port.
749  *
750  * Returns a scsi result code that can be returned by the LLDD.
751  *
752  * @rport:	remote port to be checked
753  **/
754 static inline int
755 fc_remote_port_chkready(struct fc_rport *rport)
756 {
757 	int result;
758 
759 	switch (rport->port_state) {
760 	case FC_PORTSTATE_ONLINE:
761 	case FC_PORTSTATE_MARGINAL:
762 		if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
763 			result = 0;
764 		else if (rport->flags & FC_RPORT_DEVLOSS_PENDING)
765 			result = DID_IMM_RETRY << 16;
766 		else
767 			result = DID_NO_CONNECT << 16;
768 		break;
769 	case FC_PORTSTATE_BLOCKED:
770 		if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
771 			result = DID_TRANSPORT_FAILFAST << 16;
772 		else
773 			result = DID_IMM_RETRY << 16;
774 		break;
775 	default:
776 		result = DID_NO_CONNECT << 16;
777 		break;
778 	}
779 	return result;
780 }
781 
782 static inline u64 wwn_to_u64(const u8 *wwn)
783 {
784 	return get_unaligned_be64(wwn);
785 }
786 
787 static inline void u64_to_wwn(u64 inm, u8 *wwn)
788 {
789 	put_unaligned_be64(inm, wwn);
790 }
791 
792 /**
793  * fc_vport_set_state() - called to set a vport's state. Saves the old state,
794  *   excepting the transitory states of initializing and sending the ELS
795  *   traffic to instantiate the vport on the link.
796  *
797  * Assumes the driver has surrounded this with the proper locking to ensure
798  * a coherent state change.
799  *
800  * @vport:	virtual port whose state is changing
801  * @new_state:  new state
802  **/
803 static inline void
804 fc_vport_set_state(struct fc_vport *vport, enum fc_vport_state new_state)
805 {
806 	if ((new_state != FC_VPORT_UNKNOWN) &&
807 	    (new_state != FC_VPORT_INITIALIZING))
808 		vport->vport_last_state = vport->vport_state;
809 	vport->vport_state = new_state;
810 }
811 
812 struct scsi_transport_template *fc_attach_transport(
813 			struct fc_function_template *);
814 void fc_release_transport(struct scsi_transport_template *);
815 void fc_remove_host(struct Scsi_Host *);
816 struct fc_rport *fc_remote_port_add(struct Scsi_Host *shost,
817 			int channel, struct fc_rport_identifiers  *ids);
818 void fc_remote_port_delete(struct fc_rport  *rport);
819 void fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles);
820 int scsi_is_fc_rport(const struct device *);
821 u32 fc_get_event_number(void);
822 void fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
823 		enum fc_host_event_code event_code, u32 event_data);
824 void fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
825 		u32 data_len, char *data_buf, u64 vendor_id);
826 struct fc_rport *fc_find_rport_by_wwpn(struct Scsi_Host *shost, u64 wwpn);
827 void fc_host_post_fc_event(struct Scsi_Host *shost, u32 event_number,
828 		enum fc_host_event_code event_code,
829 		u32 data_len, char *data_buf, u64 vendor_id);
830 	/* Note: when specifying vendor_id to fc_host_post_vendor_event()
831 	 *   or fc_host_post_fc_event(), be sure to read the Vendor Type
832 	 *   and ID formatting requirements specified in scsi_netlink.h
833 	 * Note: when calling fc_host_post_fc_event(), vendor_id may be
834 	 *   specified as 0.
835 	 */
836 void fc_host_fpin_rcv(struct Scsi_Host *shost, u32 fpin_len, char *fpin_buf);
837 struct fc_vport *fc_vport_create(struct Scsi_Host *shost, int channel,
838 		struct fc_vport_identifiers *);
839 int fc_vport_terminate(struct fc_vport *vport);
840 int fc_block_rport(struct fc_rport *rport);
841 int fc_block_scsi_eh(struct scsi_cmnd *cmnd);
842 enum blk_eh_timer_return fc_eh_timed_out(struct scsi_cmnd *scmd);
843 bool fc_eh_should_retry_cmd(struct scsi_cmnd *scmd);
844 
845 static inline struct Scsi_Host *fc_bsg_to_shost(struct bsg_job *job)
846 {
847 	if (scsi_is_host_device(job->dev))
848 		return dev_to_shost(job->dev);
849 	return rport_to_shost(dev_to_rport(job->dev));
850 }
851 
852 static inline struct fc_rport *fc_bsg_to_rport(struct bsg_job *job)
853 {
854 	if (scsi_is_fc_rport(job->dev))
855 		return dev_to_rport(job->dev);
856 	return NULL;
857 }
858 
859 #endif /* SCSI_TRANSPORT_FC_H */
860