1/*
2 *  FiberChannel transport specific attributes exported to sysfs.
3 *
4 *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
5 *
6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 *
20 *  ========
21 *
22 *  Copyright (C) 2004-2005   James Smart, Emulex Corporation
23 *    Rewrite for host, target, device, and remote port attributes,
24 *    statistics, and service functions...
25 *
26 */
27#ifndef SCSI_TRANSPORT_FC_H
28#define SCSI_TRANSPORT_FC_H
29
30#include <linux/sched.h>
31#include <scsi/scsi.h>
32#include <scsi/scsi_netlink.h>
33
34struct scsi_transport_template;
35
36
37/*
38 * FC Port definitions - Following FC HBAAPI guidelines
39 *
40 * Note: Not all binary values for the different fields match HBAAPI.
41 *  Instead, we use densely packed ordinal values or enums.
42 *  We get away with this as we never present the actual binary values
43 *  externally. For sysfs, we always present the string that describes
44 *  the value. Thus, an admin doesn't need a magic HBAAPI decoder ring
45 *  to understand the values. The HBAAPI user-space library is free to
46 *  convert the strings into the HBAAPI-specified binary values.
47 *
48 * Note: Not all HBAAPI-defined values are contained in the definitions
49 *  below. Those not appropriate to an fc_host (e.g. FCP initiator) have
50 *  been removed.
51 */
52
53/*
54 * fc_port_type: If you alter this, you also need to alter scsi_transport_fc.c
55 * (for the ascii descriptions).
56 */
57enum fc_port_type {
58	FC_PORTTYPE_UNKNOWN,
59	FC_PORTTYPE_OTHER,
60	FC_PORTTYPE_NOTPRESENT,
61	FC_PORTTYPE_NPORT,		/* Attached to FPort */
62	FC_PORTTYPE_NLPORT,		/* (Public) Loop w/ FLPort */
63	FC_PORTTYPE_LPORT,		/* (Private) Loop w/o FLPort */
64	FC_PORTTYPE_PTP,		/* Point to Point w/ another NPort */
65};
66
67/*
68 * fc_port_state: If you alter this, you also need to alter scsi_transport_fc.c
69 * (for the ascii descriptions).
70 */
71enum fc_port_state {
72	FC_PORTSTATE_UNKNOWN,
73	FC_PORTSTATE_NOTPRESENT,
74	FC_PORTSTATE_ONLINE,
75	FC_PORTSTATE_OFFLINE,		/* User has taken Port Offline */
76	FC_PORTSTATE_BLOCKED,
77	FC_PORTSTATE_BYPASSED,
78	FC_PORTSTATE_DIAGNOSTICS,
79	FC_PORTSTATE_LINKDOWN,
80	FC_PORTSTATE_ERROR,
81	FC_PORTSTATE_LOOPBACK,
82	FC_PORTSTATE_DELETED,
83};
84
85
86/*
87 * FC Classes of Service
88 * Note: values are not enumerated, as they can be "or'd" together
89 * for reporting (e.g. report supported_classes). If you alter this list,
90 * you also need to alter scsi_transport_fc.c (for the ascii descriptions).
91 */
92#define FC_COS_UNSPECIFIED		0
93#define FC_COS_CLASS1			2
94#define FC_COS_CLASS2			4
95#define FC_COS_CLASS3			8
96#define FC_COS_CLASS4			0x10
97#define FC_COS_CLASS6			0x40
98
99/*
100 * FC Port Speeds
101 * Note: values are not enumerated, as they can be "or'd" together
102 * for reporting (e.g. report supported_speeds). If you alter this list,
103 * you also need to alter scsi_transport_fc.c (for the ascii descriptions).
104 */
105#define FC_PORTSPEED_UNKNOWN		0 /* Unknown - transceiver
106					     incapable of reporting */
107#define FC_PORTSPEED_1GBIT		1
108#define FC_PORTSPEED_2GBIT		2
109#define FC_PORTSPEED_4GBIT		4
110#define FC_PORTSPEED_10GBIT		8
111#define FC_PORTSPEED_8GBIT		0x10
112#define FC_PORTSPEED_16GBIT		0x20
113#define FC_PORTSPEED_NOT_NEGOTIATED	(1 << 15) /* Speed not established */
114
115/*
116 * fc_tgtid_binding_type: If you alter this, you also need to alter
117 * scsi_transport_fc.c (for the ascii descriptions).
118 */
119enum fc_tgtid_binding_type  {
120	FC_TGTID_BIND_NONE,
121	FC_TGTID_BIND_BY_WWPN,
122	FC_TGTID_BIND_BY_WWNN,
123	FC_TGTID_BIND_BY_ID,
124};
125
126/*
127 * FC Remote Port Roles
128 * Note: values are not enumerated, as they can be "or'd" together
129 * for reporting (e.g. report roles). If you alter this list,
130 * you also need to alter scsi_transport_fc.c (for the ascii descriptions).
131 */
132#define FC_RPORT_ROLE_UNKNOWN			0x00
133#define FC_RPORT_ROLE_FCP_TARGET		0x01
134#define FC_RPORT_ROLE_FCP_INITIATOR		0x02
135#define FC_RPORT_ROLE_IP_PORT			0x04
136
137
138/*
139 * fc_rport_identifiers: This set of data contains all elements
140 * to uniquely identify a remote FC port. The driver uses this data
141 * to report the existence of a remote FC port in the topology. Internally,
142 * the transport uses this data for attributes and to manage consistent
143 * target id bindings.
144 */
145struct fc_rport_identifiers {
146	u64 node_name;
147	u64 port_name;
148	u32 port_id;
149	u32 roles;
150};
151
152/* Macro for use in defining Remote Port attributes */
153#define FC_RPORT_ATTR(_name,_mode,_show,_store)				\
154struct class_device_attribute class_device_attr_rport_##_name = 	\
155	__ATTR(_name,_mode,_show,_store)
156
157
158/*
159 * FC Remote Port Attributes
160 *
161 * This structure exists for each remote FC port that a LLDD notifies
162 * the subsystem of.  A remote FC port may or may not be a SCSI Target,
163 * also be a SCSI initiator, IP endpoint, etc. As such, the remote
164 * port is considered a separate entity, independent of "role" (such
165 * as scsi target).
166 *
167 * --
168 *
169 * Attributes are based on HBAAPI V2.0 definitions. Only those
170 * attributes that are determinable by the local port (aka Host)
171 * are contained.
172 *
173 * Fixed attributes are not expected to change. The driver is
174 * expected to set these values after successfully calling
175 * fc_remote_port_add(). The transport fully manages all get functions
176 * w/o driver interaction.
177 *
178 * Dynamic attributes are expected to change. The driver participates
179 * in all get/set operations via functions provided by the driver.
180 *
181 * Private attributes are transport-managed values. They are fully
182 * managed by the transport w/o driver interaction.
183 */
184
185struct fc_rport {	/* aka fc_starget_attrs */
186	/* Fixed Attributes */
187	u32 maxframe_size;
188	u32 supported_classes;
189
190	/* Dynamic Attributes */
191	u32 dev_loss_tmo;	/* Remote Port loss timeout in seconds. */
192
193	/* Private (Transport-managed) Attributes */
194	u64 node_name;
195	u64 port_name;
196	u32 port_id;
197	u32 roles;
198	enum fc_port_state port_state;	/* Will only be ONLINE or UNKNOWN */
199	u32 scsi_target_id;
200	u32 fast_io_fail_tmo;
201
202	/* exported data */
203	void *dd_data;			/* Used for driver-specific storage */
204
205	/* internal data */
206	unsigned int channel;
207	u32 number;
208	u8 flags;
209	struct list_head peers;
210	struct device dev;
211 	struct delayed_work dev_loss_work;
212 	struct work_struct scan_work;
213 	struct delayed_work fail_io_work;
214 	struct work_struct stgt_delete_work;
215	struct work_struct rport_delete_work;
216} __attribute__((aligned(sizeof(unsigned long))));
217
218/* bit field values for struct fc_rport "flags" field: */
219#define FC_RPORT_DEVLOSS_PENDING	0x01
220#define FC_RPORT_SCAN_PENDING		0x02
221
222#define	dev_to_rport(d)				\
223	container_of(d, struct fc_rport, dev)
224#define transport_class_to_rport(classdev)	\
225	dev_to_rport(classdev->dev)
226#define rport_to_shost(r)			\
227	dev_to_shost(r->dev.parent)
228
229/*
230 * FC SCSI Target Attributes
231 *
232 * The SCSI Target is considered an extention of a remote port (as
233 * a remote port can be more than a SCSI Target). Within the scsi
234 * subsystem, we leave the Target as a separate entity. Doing so
235 * provides backward compatibility with prior FC transport api's,
236 * and lets remote ports be handled entirely within the FC transport
237 * and independently from the scsi subsystem. The drawback is that
238 * some data will be duplicated.
239 */
240
241struct fc_starget_attrs {	/* aka fc_target_attrs */
242	/* Dynamic Attributes */
243	u64 node_name;
244	u64 port_name;
245	u32 port_id;
246};
247
248#define fc_starget_node_name(x) \
249	(((struct fc_starget_attrs *)&(x)->starget_data)->node_name)
250#define fc_starget_port_name(x)	\
251	(((struct fc_starget_attrs *)&(x)->starget_data)->port_name)
252#define fc_starget_port_id(x) \
253	(((struct fc_starget_attrs *)&(x)->starget_data)->port_id)
254
255#define starget_to_rport(s)			\
256	scsi_is_fc_rport(s->dev.parent) ? dev_to_rport(s->dev.parent) : NULL
257
258
259/*
260 * FC Local Port (Host) Statistics
261 */
262
263/* FC Statistics - Following FC HBAAPI v2.0 guidelines */
264struct fc_host_statistics {
265	/* port statistics */
266	u64 seconds_since_last_reset;
267	u64 tx_frames;
268	u64 tx_words;
269	u64 rx_frames;
270	u64 rx_words;
271	u64 lip_count;
272	u64 nos_count;
273	u64 error_frames;
274	u64 dumped_frames;
275	u64 link_failure_count;
276	u64 loss_of_sync_count;
277	u64 loss_of_signal_count;
278	u64 prim_seq_protocol_err_count;
279	u64 invalid_tx_word_count;
280	u64 invalid_crc_count;
281
282	/* fc4 statistics  (only FCP supported currently) */
283	u64 fcp_input_requests;
284	u64 fcp_output_requests;
285	u64 fcp_control_requests;
286	u64 fcp_input_megabytes;
287	u64 fcp_output_megabytes;
288};
289
290
291/*
292 * FC Event Codes - Polled and Async, following FC HBAAPI v2.0 guidelines
293 */
294
295/*
296 * fc_host_event_code: If you alter this, you also need to alter
297 * scsi_transport_fc.c (for the ascii descriptions).
298 */
299enum fc_host_event_code  {
300	FCH_EVT_LIP			= 0x1,
301	FCH_EVT_LINKUP			= 0x2,
302	FCH_EVT_LINKDOWN		= 0x3,
303	FCH_EVT_LIPRESET		= 0x4,
304	FCH_EVT_RSCN			= 0x5,
305	FCH_EVT_ADAPTER_CHANGE		= 0x103,
306	FCH_EVT_PORT_UNKNOWN		= 0x200,
307	FCH_EVT_PORT_OFFLINE		= 0x201,
308	FCH_EVT_PORT_ONLINE		= 0x202,
309	FCH_EVT_PORT_FABRIC		= 0x204,
310	FCH_EVT_LINK_UNKNOWN		= 0x500,
311	FCH_EVT_VENDOR_UNIQUE		= 0xffff,
312};
313
314
315/*
316 * FC Local Port (Host) Attributes
317 *
318 * Attributes are based on HBAAPI V2.0 definitions.
319 * Note: OSDeviceName is determined by user-space library
320 *
321 * Fixed attributes are not expected to change. The driver is
322 * expected to set these values after successfully calling scsi_add_host().
323 * The transport fully manages all get functions w/o driver interaction.
324 *
325 * Dynamic attributes are expected to change. The driver participates
326 * in all get/set operations via functions provided by the driver.
327 *
328 * Private attributes are transport-managed values. They are fully
329 * managed by the transport w/o driver interaction.
330 */
331
332#define FC_FC4_LIST_SIZE		32
333#define FC_SYMBOLIC_NAME_SIZE		256
334#define FC_VERSION_STRING_SIZE		64
335#define FC_SERIAL_NUMBER_SIZE		80
336
337struct fc_host_attrs {
338	/* Fixed Attributes */
339	u64 node_name;
340	u64 port_name;
341	u64 permanent_port_name;
342	u32 supported_classes;
343	u8  supported_fc4s[FC_FC4_LIST_SIZE];
344	u32 supported_speeds;
345	u32 maxframe_size;
346	char serial_number[FC_SERIAL_NUMBER_SIZE];
347
348	/* Dynamic Attributes */
349	u32 port_id;
350	enum fc_port_type port_type;
351	enum fc_port_state port_state;
352	u8  active_fc4s[FC_FC4_LIST_SIZE];
353	u32 speed;
354	u64 fabric_name;
355	char symbolic_name[FC_SYMBOLIC_NAME_SIZE];
356	char system_hostname[FC_SYMBOLIC_NAME_SIZE];
357
358	/* Private (Transport-managed) Attributes */
359	enum fc_tgtid_binding_type  tgtid_bind_type;
360
361	/* internal data */
362	struct list_head rports;
363	struct list_head rport_bindings;
364	u32 next_rport_number;
365	u32 next_target_id;
366
367	/* work queues for rport state manipulation */
368	char work_q_name[KOBJ_NAME_LEN];
369	struct workqueue_struct *work_q;
370	char devloss_work_q_name[KOBJ_NAME_LEN];
371	struct workqueue_struct *devloss_work_q;
372};
373
374#define shost_to_fc_host(x) \
375	((struct fc_host_attrs *)(x)->shost_data)
376
377#define fc_host_node_name(x) \
378	(((struct fc_host_attrs *)(x)->shost_data)->node_name)
379#define fc_host_port_name(x)	\
380	(((struct fc_host_attrs *)(x)->shost_data)->port_name)
381#define fc_host_permanent_port_name(x)	\
382	(((struct fc_host_attrs *)(x)->shost_data)->permanent_port_name)
383#define fc_host_supported_classes(x)	\
384	(((struct fc_host_attrs *)(x)->shost_data)->supported_classes)
385#define fc_host_supported_fc4s(x)	\
386	(((struct fc_host_attrs *)(x)->shost_data)->supported_fc4s)
387#define fc_host_supported_speeds(x)	\
388	(((struct fc_host_attrs *)(x)->shost_data)->supported_speeds)
389#define fc_host_maxframe_size(x)	\
390	(((struct fc_host_attrs *)(x)->shost_data)->maxframe_size)
391#define fc_host_serial_number(x)	\
392	(((struct fc_host_attrs *)(x)->shost_data)->serial_number)
393#define fc_host_port_id(x)	\
394	(((struct fc_host_attrs *)(x)->shost_data)->port_id)
395#define fc_host_port_type(x)	\
396	(((struct fc_host_attrs *)(x)->shost_data)->port_type)
397#define fc_host_port_state(x)	\
398	(((struct fc_host_attrs *)(x)->shost_data)->port_state)
399#define fc_host_active_fc4s(x)	\
400	(((struct fc_host_attrs *)(x)->shost_data)->active_fc4s)
401#define fc_host_speed(x)	\
402	(((struct fc_host_attrs *)(x)->shost_data)->speed)
403#define fc_host_fabric_name(x)	\
404	(((struct fc_host_attrs *)(x)->shost_data)->fabric_name)
405#define fc_host_symbolic_name(x)	\
406	(((struct fc_host_attrs *)(x)->shost_data)->symbolic_name)
407#define fc_host_system_hostname(x)	\
408	(((struct fc_host_attrs *)(x)->shost_data)->system_hostname)
409#define fc_host_tgtid_bind_type(x) \
410	(((struct fc_host_attrs *)(x)->shost_data)->tgtid_bind_type)
411#define fc_host_rports(x) \
412	(((struct fc_host_attrs *)(x)->shost_data)->rports)
413#define fc_host_rport_bindings(x) \
414	(((struct fc_host_attrs *)(x)->shost_data)->rport_bindings)
415#define fc_host_next_rport_number(x) \
416	(((struct fc_host_attrs *)(x)->shost_data)->next_rport_number)
417#define fc_host_next_target_id(x) \
418	(((struct fc_host_attrs *)(x)->shost_data)->next_target_id)
419#define fc_host_work_q_name(x) \
420	(((struct fc_host_attrs *)(x)->shost_data)->work_q_name)
421#define fc_host_work_q(x) \
422	(((struct fc_host_attrs *)(x)->shost_data)->work_q)
423#define fc_host_devloss_work_q_name(x) \
424	(((struct fc_host_attrs *)(x)->shost_data)->devloss_work_q_name)
425#define fc_host_devloss_work_q(x) \
426	(((struct fc_host_attrs *)(x)->shost_data)->devloss_work_q)
427
428
429/* The functions by which the transport class and the driver communicate */
430struct fc_function_template {
431	void    (*get_rport_dev_loss_tmo)(struct fc_rport *);
432	void	(*set_rport_dev_loss_tmo)(struct fc_rport *, u32);
433
434	void	(*get_starget_node_name)(struct scsi_target *);
435	void	(*get_starget_port_name)(struct scsi_target *);
436	void 	(*get_starget_port_id)(struct scsi_target *);
437
438	void 	(*get_host_port_id)(struct Scsi_Host *);
439	void	(*get_host_port_type)(struct Scsi_Host *);
440	void	(*get_host_port_state)(struct Scsi_Host *);
441	void	(*get_host_active_fc4s)(struct Scsi_Host *);
442	void	(*get_host_speed)(struct Scsi_Host *);
443	void	(*get_host_fabric_name)(struct Scsi_Host *);
444	void	(*get_host_symbolic_name)(struct Scsi_Host *);
445	void	(*set_host_system_hostname)(struct Scsi_Host *);
446
447	struct fc_host_statistics * (*get_fc_host_stats)(struct Scsi_Host *);
448	void	(*reset_fc_host_stats)(struct Scsi_Host *);
449
450	int	(*issue_fc_host_lip)(struct Scsi_Host *);
451
452	void    (*dev_loss_tmo_callbk)(struct fc_rport *);
453	void	(*terminate_rport_io)(struct fc_rport *);
454
455	/* allocation lengths for host-specific data */
456	u32	 			dd_fcrport_size;
457
458	/*
459	 * The driver sets these to tell the transport class it
460	 * wants the attributes displayed in sysfs.  If the show_ flag
461	 * is not set, the attribute will be private to the transport
462	 * class
463	 */
464
465	/* remote port fixed attributes */
466	unsigned long	show_rport_maxframe_size:1;
467	unsigned long	show_rport_supported_classes:1;
468	unsigned long   show_rport_dev_loss_tmo:1;
469
470	/*
471	 * target dynamic attributes
472	 * These should all be "1" if the driver uses the remote port
473	 * add/delete functions (so attributes reflect rport values).
474	 */
475	unsigned long	show_starget_node_name:1;
476	unsigned long	show_starget_port_name:1;
477	unsigned long	show_starget_port_id:1;
478
479	/* host fixed attributes */
480	unsigned long	show_host_node_name:1;
481	unsigned long	show_host_port_name:1;
482	unsigned long	show_host_permanent_port_name:1;
483	unsigned long	show_host_supported_classes:1;
484	unsigned long	show_host_supported_fc4s:1;
485	unsigned long	show_host_supported_speeds:1;
486	unsigned long	show_host_maxframe_size:1;
487	unsigned long	show_host_serial_number:1;
488	/* host dynamic attributes */
489	unsigned long	show_host_port_id:1;
490	unsigned long	show_host_port_type:1;
491	unsigned long	show_host_port_state:1;
492	unsigned long	show_host_active_fc4s:1;
493	unsigned long	show_host_speed:1;
494	unsigned long	show_host_fabric_name:1;
495	unsigned long	show_host_symbolic_name:1;
496	unsigned long	show_host_system_hostname:1;
497};
498
499
500/**
501 * fc_remote_port_chkready - called to validate the remote port state
502 *   prior to initiating io to the port.
503 *
504 * Returns a scsi result code that can be returned by the LLDD.
505 *
506 * @rport:	remote port to be checked
507 **/
508static inline int
509fc_remote_port_chkready(struct fc_rport *rport)
510{
511	int result;
512
513	switch (rport->port_state) {
514	case FC_PORTSTATE_ONLINE:
515		if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
516			result = 0;
517		else if (rport->flags & FC_RPORT_DEVLOSS_PENDING)
518			result = DID_IMM_RETRY << 16;
519		else
520			result = DID_NO_CONNECT << 16;
521		break;
522	case FC_PORTSTATE_BLOCKED:
523		result = DID_IMM_RETRY << 16;
524		break;
525	default:
526		result = DID_NO_CONNECT << 16;
527		break;
528	}
529	return result;
530}
531
532static inline u64 wwn_to_u64(u8 *wwn)
533{
534	return (u64)wwn[0] << 56 | (u64)wwn[1] << 48 |
535	    (u64)wwn[2] << 40 | (u64)wwn[3] << 32 |
536	    (u64)wwn[4] << 24 | (u64)wwn[5] << 16 |
537	    (u64)wwn[6] <<  8 | (u64)wwn[7];
538}
539
540static inline void u64_to_wwn(u64 inm, u8 *wwn)
541{
542	wwn[0] = (inm >> 56) & 0xff;
543	wwn[1] = (inm >> 48) & 0xff;
544	wwn[2] = (inm >> 40) & 0xff;
545	wwn[3] = (inm >> 32) & 0xff;
546	wwn[4] = (inm >> 24) & 0xff;
547	wwn[5] = (inm >> 16) & 0xff;
548	wwn[6] = (inm >> 8) & 0xff;
549	wwn[7] = inm & 0xff;
550}
551
552struct scsi_transport_template *fc_attach_transport(
553			struct fc_function_template *);
554void fc_release_transport(struct scsi_transport_template *);
555void fc_remove_host(struct Scsi_Host *);
556struct fc_rport *fc_remote_port_add(struct Scsi_Host *shost,
557			int channel, struct fc_rport_identifiers  *ids);
558void fc_remote_port_delete(struct fc_rport  *rport);
559void fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles);
560int scsi_is_fc_rport(const struct device *);
561u32 fc_get_event_number(void);
562void fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
563		enum fc_host_event_code event_code, u32 event_data);
564void fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
565		u32 data_len, char * data_buf, u64 vendor_id);
566	/* Note: when specifying vendor_id to fc_host_post_vendor_event()
567	 *   be sure to read the Vendor Type and ID formatting requirements
568	 *   specified in scsi_netlink.h
569	 */
570
571#endif /* SCSI_TRANSPORT_FC_H */
572