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