Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[cascardo/linux.git] / include / scsi / libsas.h
1 /*
2  * SAS host prototypes and structures header file
3  *
4  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
5  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6  *
7  * This file is licensed under GPLv2.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  */
25
26 #ifndef _LIBSAS_H_
27 #define _LIBSAS_H_
28
29
30 #include <linux/timer.h>
31 #include <linux/pci.h>
32 #include <scsi/sas.h>
33 #include <linux/libata.h>
34 #include <linux/list.h>
35 #include <scsi/scsi_device.h>
36 #include <scsi/scsi_cmnd.h>
37 #include <scsi/scsi_transport_sas.h>
38 #include <linux/scatterlist.h>
39 #include <linux/slab.h>
40
41 struct block_device;
42
43 enum sas_class {
44         SAS,
45         EXPANDER
46 };
47
48 enum sas_phy_role {
49         PHY_ROLE_NONE = 0,
50         PHY_ROLE_TARGET = 0x40,
51         PHY_ROLE_INITIATOR = 0x80,
52 };
53
54 enum sas_phy_type {
55         PHY_TYPE_PHYSICAL,
56         PHY_TYPE_VIRTUAL
57 };
58
59 /* The events are mnemonically described in sas_dump.c
60  * so when updating/adding events here, please also
61  * update the other file too.
62  */
63 enum ha_event {
64         HAE_RESET             = 0U,
65         HA_NUM_EVENTS         = 1,
66 };
67
68 enum port_event {
69         PORTE_BYTES_DMAED     = 0U,
70         PORTE_BROADCAST_RCVD  = 1,
71         PORTE_LINK_RESET_ERR  = 2,
72         PORTE_TIMER_EVENT     = 3,
73         PORTE_HARD_RESET      = 4,
74         PORT_NUM_EVENTS       = 5,
75 };
76
77 enum phy_event {
78         PHYE_LOSS_OF_SIGNAL   = 0U,
79         PHYE_OOB_DONE         = 1,
80         PHYE_OOB_ERROR        = 2,
81         PHYE_SPINUP_HOLD      = 3, /* hot plug SATA, no COMWAKE sent */
82         PHY_NUM_EVENTS        = 4,
83 };
84
85 enum discover_event {
86         DISCE_DISCOVER_DOMAIN   = 0U,
87         DISCE_REVALIDATE_DOMAIN = 1,
88         DISCE_PORT_GONE         = 2,
89         DISCE_PROBE             = 3,
90         DISCE_DESTRUCT          = 4,
91         DISC_NUM_EVENTS         = 5,
92 };
93
94 /* ---------- Expander Devices ---------- */
95
96 #define to_dom_device(_obj) container_of(_obj, struct domain_device, dev_obj)
97 #define to_dev_attr(_attr)  container_of(_attr, struct domain_dev_attribute,\
98                                          attr)
99
100 enum routing_attribute {
101         DIRECT_ROUTING,
102         SUBTRACTIVE_ROUTING,
103         TABLE_ROUTING,
104 };
105
106 enum ex_phy_state {
107         PHY_EMPTY,
108         PHY_VACANT,
109         PHY_NOT_PRESENT,
110         PHY_DEVICE_DISCOVERED
111 };
112
113 struct ex_phy {
114         int    phy_id;
115
116         enum ex_phy_state phy_state;
117
118         enum sas_dev_type attached_dev_type;
119         enum sas_linkrate linkrate;
120
121         u8   attached_sata_host:1;
122         u8   attached_sata_dev:1;
123         u8   attached_sata_ps:1;
124
125         enum sas_protocol attached_tproto;
126         enum sas_protocol attached_iproto;
127
128         u8   attached_sas_addr[SAS_ADDR_SIZE];
129         u8   attached_phy_id;
130
131         u8   phy_change_count;
132         enum routing_attribute routing_attr;
133         u8   virtual:1;
134
135         int  last_da_index;
136
137         struct sas_phy *phy;
138         struct sas_port *port;
139 };
140
141 struct expander_device {
142         struct list_head children;
143
144         u16    ex_change_count;
145         u16    max_route_indexes;
146         u8     num_phys;
147
148         u8     t2t_supp:1;
149         u8     configuring:1;
150         u8     conf_route_table:1;
151
152         u8     enclosure_logical_id[8];
153
154         struct ex_phy *ex_phy;
155         struct sas_port *parent_port;
156
157         struct mutex cmd_mutex;
158 };
159
160 /* ---------- SATA device ---------- */
161 enum ata_command_set {
162         ATA_COMMAND_SET   = 0,
163         ATAPI_COMMAND_SET = 1,
164 };
165
166 struct sata_device {
167         enum   ata_command_set command_set;
168         struct smp_resp        rps_resp; /* report_phy_sata_resp */
169         u8     port_no;        /* port number, if this is a PM (Port) */
170         struct list_head children; /* PM Ports if this is a PM */
171
172         struct ata_port *ap;
173         struct ata_host ata_host;
174         struct ata_taskfile tf;
175 };
176
177 enum {
178         SAS_DEV_GONE,
179         SAS_DEV_DESTROY,
180 };
181
182 struct domain_device {
183         spinlock_t done_lock;
184         enum sas_dev_type dev_type;
185
186         enum sas_linkrate linkrate;
187         enum sas_linkrate min_linkrate;
188         enum sas_linkrate max_linkrate;
189
190         int  pathways;
191
192         struct domain_device *parent;
193         struct list_head siblings; /* devices on the same level */
194         struct asd_sas_port *port;        /* shortcut to root of the tree */
195         struct sas_phy *phy;
196
197         struct list_head dev_list_node;
198         struct list_head disco_list_node; /* awaiting probe or destruct */
199
200         enum sas_protocol    iproto;
201         enum sas_protocol    tproto;
202
203         struct sas_rphy *rphy;
204
205         u8  sas_addr[SAS_ADDR_SIZE];
206         u8  hashed_sas_addr[HASHED_SAS_ADDR_SIZE];
207
208         u8  frame_rcvd[32];
209
210         union {
211                 struct expander_device ex_dev;
212                 struct sata_device     sata_dev; /* STP & directly attached */
213         };
214
215         void *lldd_dev;
216         unsigned long state;
217         struct kref kref;
218 };
219
220 struct sas_discovery_event {
221         struct work_struct work;
222         struct asd_sas_port *port;
223 };
224
225 struct sas_discovery {
226         struct sas_discovery_event disc_work[DISC_NUM_EVENTS];
227         unsigned long    pending;
228         u8     fanout_sas_addr[8];
229         u8     eeds_a[8];
230         u8     eeds_b[8];
231         int    max_level;
232 };
233
234 /* The port struct is Class:RW, driver:RO */
235 struct asd_sas_port {
236 /* private: */
237         struct completion port_gone_completion;
238
239         struct sas_discovery disc;
240         struct domain_device *port_dev;
241         spinlock_t dev_list_lock;
242         struct list_head dev_list;
243         struct list_head disco_list;
244         struct list_head destroy_list;
245         enum   sas_linkrate linkrate;
246
247         struct work_struct work;
248
249 /* public: */
250         int id;
251
252         enum sas_class   class;
253         u8               sas_addr[SAS_ADDR_SIZE];
254         u8               attached_sas_addr[SAS_ADDR_SIZE];
255         enum sas_protocol   iproto;
256         enum sas_protocol   tproto;
257
258         enum sas_oob_mode oob_mode;
259
260         spinlock_t       phy_list_lock;
261         struct list_head phy_list;
262         int              num_phys;
263         u32              phy_mask;
264
265         struct sas_ha_struct *ha;
266
267         struct sas_port *port;
268
269         void *lldd_port;          /* not touched by the sas class code */
270 };
271
272 struct asd_sas_event {
273         struct work_struct work;
274         struct asd_sas_phy *phy;
275 };
276
277 /* The phy pretty much is controlled by the LLDD.
278  * The class only reads those fields.
279  */
280 struct asd_sas_phy {
281 /* private: */
282         struct asd_sas_event   port_events[PORT_NUM_EVENTS];
283         struct asd_sas_event   phy_events[PHY_NUM_EVENTS];
284
285         unsigned long port_events_pending;
286         unsigned long phy_events_pending;
287
288         int error;
289
290         struct sas_phy *phy;
291
292 /* public: */
293         /* The following are class:RO, driver:R/W */
294         int            enabled;   /* must be set */
295
296         int            id;        /* must be set */
297         enum sas_class class;
298         enum sas_protocol iproto;
299         enum sas_protocol tproto;
300
301         enum sas_phy_type  type;
302         enum sas_phy_role  role;
303         enum sas_oob_mode  oob_mode;
304         enum sas_linkrate linkrate;
305
306         u8   *sas_addr;           /* must be set */
307         u8   attached_sas_addr[SAS_ADDR_SIZE]; /* class:RO, driver: R/W */
308
309         spinlock_t     frame_rcvd_lock;
310         u8             *frame_rcvd; /* must be set */
311         int            frame_rcvd_size;
312
313         spinlock_t     sas_prim_lock;
314         u32            sas_prim;
315
316         struct list_head port_phy_el; /* driver:RO */
317         struct asd_sas_port      *port; /* Class:RW, driver: RO */
318
319         struct sas_ha_struct *ha; /* may be set; the class sets it anyway */
320
321         void *lldd_phy;           /* not touched by the sas_class_code */
322 };
323
324 struct scsi_core {
325         struct Scsi_Host *shost;
326
327         struct mutex      task_queue_flush;
328         spinlock_t        task_queue_lock;
329         struct list_head  task_queue;
330         int               task_queue_size;
331
332         struct task_struct *queue_thread;
333 };
334
335 struct sas_ha_event {
336         struct work_struct work;
337         struct sas_ha_struct *ha;
338 };
339
340 enum sas_ha_state {
341         SAS_HA_REGISTERED,
342         SAS_HA_DRAINING,
343         SAS_HA_ATA_EH_ACTIVE,
344         SAS_HA_FROZEN,
345 };
346
347 struct sas_ha_struct {
348 /* private: */
349         struct sas_ha_event ha_events[HA_NUM_EVENTS];
350         unsigned long    pending;
351
352         struct list_head  defer_q; /* work queued while draining */
353         struct mutex      drain_mutex;
354         unsigned long     state;
355         spinlock_t        state_lock;
356
357         struct mutex disco_mutex;
358
359         struct scsi_core core;
360
361 /* public: */
362         char *sas_ha_name;
363         struct device *dev;       /* should be set */
364         struct module *lldd_module; /* should be set */
365
366         u8 *sas_addr;             /* must be set */
367         u8 hashed_sas_addr[HASHED_SAS_ADDR_SIZE];
368
369         spinlock_t      phy_port_lock;
370         struct asd_sas_phy  **sas_phy; /* array of valid pointers, must be set */
371         struct asd_sas_port **sas_port; /* array of valid pointers, must be set */
372         int             num_phys; /* must be set, gt 0, static */
373
374         /* The class calls this to send a task for execution. */
375         int lldd_max_execute_num;
376         int lldd_queue_size;
377         int strict_wide_ports; /* both sas_addr and attached_sas_addr must match
378                                 * their siblings when forming wide ports */
379
380         /* LLDD calls these to notify the class of an event. */
381         void (*notify_ha_event)(struct sas_ha_struct *, enum ha_event);
382         void (*notify_port_event)(struct asd_sas_phy *, enum port_event);
383         void (*notify_phy_event)(struct asd_sas_phy *, enum phy_event);
384
385         void *lldd_ha;            /* not touched by sas class code */
386
387         struct list_head eh_done_q;  /* complete via scsi_eh_flush_done_q */
388         struct list_head eh_ata_q; /* scmds to promote from sas to ata eh */
389 };
390
391 #define SHOST_TO_SAS_HA(_shost) (*(struct sas_ha_struct **)(_shost)->hostdata)
392
393 static inline struct domain_device *
394 starget_to_domain_dev(struct scsi_target *starget) {
395         return starget->hostdata;
396 }
397
398 static inline struct domain_device *
399 sdev_to_domain_dev(struct scsi_device *sdev) {
400         return starget_to_domain_dev(sdev->sdev_target);
401 }
402
403 static inline struct ata_device *sas_to_ata_dev(struct domain_device *dev)
404 {
405         return &dev->sata_dev.ap->link.device[0];
406 }
407
408 static inline struct domain_device *
409 cmd_to_domain_dev(struct scsi_cmnd *cmd)
410 {
411         return sdev_to_domain_dev(cmd->device);
412 }
413
414 void sas_hash_addr(u8 *hashed, const u8 *sas_addr);
415
416 /* Before calling a notify event, LLDD should use this function
417  * when the link is severed (possibly from its tasklet).
418  * The idea is that the Class only reads those, while the LLDD,
419  * can R/W these (thus avoiding a race).
420  */
421 static inline void sas_phy_disconnected(struct asd_sas_phy *phy)
422 {
423         phy->oob_mode = OOB_NOT_CONNECTED;
424         phy->linkrate = SAS_LINK_RATE_UNKNOWN;
425 }
426
427 static inline unsigned int to_sas_gpio_od(int device, int bit)
428 {
429         return 3 * device + bit;
430 }
431
432 static inline void sas_put_local_phy(struct sas_phy *phy)
433 {
434         put_device(&phy->dev);
435 }
436
437 #ifdef CONFIG_SCSI_SAS_HOST_SMP
438 int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count);
439 #else
440 static inline int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count)
441 {
442         return -1;
443 }
444 #endif
445
446 /* ---------- Tasks ---------- */
447 /*
448       service_response |  SAS_TASK_COMPLETE  |  SAS_TASK_UNDELIVERED |
449   exec_status          |                     |                       |
450   ---------------------+---------------------+-----------------------+
451        SAM_...         |         X           |                       |
452        DEV_NO_RESPONSE |         X           |           X           |
453        INTERRUPTED     |         X           |                       |
454        QUEUE_FULL      |                     |           X           |
455        DEVICE_UNKNOWN  |                     |           X           |
456        SG_ERR          |                     |           X           |
457   ---------------------+---------------------+-----------------------+
458  */
459
460 enum service_response {
461         SAS_TASK_COMPLETE,
462         SAS_TASK_UNDELIVERED = -1,
463 };
464
465 enum exec_status {
466         /* The SAM_STAT_.. codes fit in the lower 6 bits, alias some of
467          * them here to silence 'case value not in enumerated type' warnings
468          */
469         __SAM_STAT_CHECK_CONDITION = SAM_STAT_CHECK_CONDITION,
470
471         SAS_DEV_NO_RESPONSE = 0x80,
472         SAS_DATA_UNDERRUN,
473         SAS_DATA_OVERRUN,
474         SAS_INTERRUPTED,
475         SAS_QUEUE_FULL,
476         SAS_DEVICE_UNKNOWN,
477         SAS_SG_ERR,
478         SAS_OPEN_REJECT,
479         SAS_OPEN_TO,
480         SAS_PROTO_RESPONSE,
481         SAS_PHY_DOWN,
482         SAS_NAK_R_ERR,
483         SAS_PENDING,
484         SAS_ABORTED_TASK,
485 };
486
487 /* When a task finishes with a response, the LLDD examines the
488  * response:
489  *      - For an ATA task task_status_struct::stat is set to
490  * SAS_PROTO_RESPONSE, and the task_status_struct::buf is set to the
491  * contents of struct ata_task_resp.
492  *      - For SSP tasks, if no data is present or status/TMF response
493  * is valid, task_status_struct::stat is set.  If data is present
494  * (SENSE data), the LLDD copies up to SAS_STATUS_BUF_SIZE, sets
495  * task_status_struct::buf_valid_size, and task_status_struct::stat is
496  * set to SAM_CHECK_COND.
497  *
498  * "buf" has format SCSI Sense for SSP task, or struct ata_task_resp
499  * for ATA task.
500  *
501  * "frame_len" is the total frame length, which could be more or less
502  * than actually copied.
503  *
504  * Tasks ending with response, always set the residual field.
505  */
506 struct ata_task_resp {
507         u16  frame_len;
508         u8   ending_fis[24];      /* dev to host or data-in */
509 };
510
511 #define SAS_STATUS_BUF_SIZE 96
512
513 struct task_status_struct {
514         enum service_response resp;
515         enum exec_status      stat;
516         int  buf_valid_size;
517
518         u8   buf[SAS_STATUS_BUF_SIZE];
519
520         u32  residual;
521         enum sas_open_rej_reason open_rej_reason;
522 };
523
524 /* ATA and ATAPI task queuable to a SAS LLDD.
525  */
526 struct sas_ata_task {
527         struct host_to_dev_fis fis;
528         u8     atapi_packet[16];  /* 0 if not ATAPI task */
529
530         u8     retry_count;       /* hardware retry, should be > 0 */
531
532         u8     dma_xfer:1;        /* PIO:0 or DMA:1 */
533         u8     use_ncq:1;
534         u8     set_affil_pol:1;
535         u8     stp_affil_pol:1;
536
537         u8     device_control_reg_update:1;
538 };
539
540 struct sas_smp_task {
541         struct scatterlist smp_req;
542         struct scatterlist smp_resp;
543 };
544
545 enum task_attribute {
546         TASK_ATTR_SIMPLE = 0,
547         TASK_ATTR_HOQ    = 1,
548         TASK_ATTR_ORDERED= 2,
549         TASK_ATTR_ACA    = 4,
550 };
551
552 struct sas_ssp_task {
553         u8     retry_count;       /* hardware retry, should be > 0 */
554
555         u8     LUN[8];
556         u8     enable_first_burst:1;
557         enum   task_attribute task_attr;
558         u8     task_prio;
559         u8     cdb[16];
560 };
561
562 struct sas_task {
563         struct domain_device *dev;
564         struct list_head      list;
565
566         spinlock_t   task_state_lock;
567         unsigned     task_state_flags;
568
569         enum   sas_protocol      task_proto;
570
571         /* Used by the discovery code. */
572         struct timer_list     timer;
573         struct completion     completion;
574
575         union {
576                 struct sas_ata_task ata_task;
577                 struct sas_smp_task smp_task;
578                 struct sas_ssp_task ssp_task;
579         };
580
581         struct scatterlist *scatter;
582         int    num_scatter;
583         u32    total_xfer_len;
584         u8     data_dir:2;        /* Use PCI_DMA_... */
585
586         struct task_status_struct task_status;
587         void   (*task_done)(struct sas_task *);
588
589         void   *lldd_task;        /* for use by LLDDs */
590         void   *uldd_task;
591
592         struct work_struct abort_work;
593 };
594
595 #define SAS_TASK_STATE_PENDING      1
596 #define SAS_TASK_STATE_DONE         2
597 #define SAS_TASK_STATE_ABORTED      4
598 #define SAS_TASK_NEED_DEV_RESET     8
599 #define SAS_TASK_AT_INITIATOR       16
600
601 extern struct sas_task *sas_alloc_task(gfp_t flags);
602 extern void sas_free_task(struct sas_task *task);
603
604 struct sas_domain_function_template {
605         /* The class calls these to notify the LLDD of an event. */
606         void (*lldd_port_formed)(struct asd_sas_phy *);
607         void (*lldd_port_deformed)(struct asd_sas_phy *);
608
609         /* The class calls these when a device is found or gone. */
610         int  (*lldd_dev_found)(struct domain_device *);
611         void (*lldd_dev_gone)(struct domain_device *);
612
613         int (*lldd_execute_task)(struct sas_task *, int num,
614                                  gfp_t gfp_flags);
615
616         /* Task Management Functions. Must be called from process context. */
617         int (*lldd_abort_task)(struct sas_task *);
618         int (*lldd_abort_task_set)(struct domain_device *, u8 *lun);
619         int (*lldd_clear_aca)(struct domain_device *, u8 *lun);
620         int (*lldd_clear_task_set)(struct domain_device *, u8 *lun);
621         int (*lldd_I_T_nexus_reset)(struct domain_device *);
622         int (*lldd_ata_check_ready)(struct domain_device *);
623         void (*lldd_ata_set_dmamode)(struct domain_device *);
624         int (*lldd_lu_reset)(struct domain_device *, u8 *lun);
625         int (*lldd_query_task)(struct sas_task *);
626
627         /* Port and Adapter management */
628         int (*lldd_clear_nexus_port)(struct asd_sas_port *);
629         int (*lldd_clear_nexus_ha)(struct sas_ha_struct *);
630
631         /* Phy management */
632         int (*lldd_control_phy)(struct asd_sas_phy *, enum phy_func, void *);
633
634         /* GPIO support */
635         int (*lldd_write_gpio)(struct sas_ha_struct *, u8 reg_type,
636                                u8 reg_index, u8 reg_count, u8 *write_data);
637 };
638
639 extern int sas_register_ha(struct sas_ha_struct *);
640 extern int sas_unregister_ha(struct sas_ha_struct *);
641
642 int sas_set_phy_speed(struct sas_phy *phy,
643                       struct sas_phy_linkrates *rates);
644 int sas_phy_reset(struct sas_phy *phy, int hard_reset);
645 int sas_queue_up(struct sas_task *task);
646 extern int sas_queuecommand(struct Scsi_Host * ,struct scsi_cmnd *);
647 extern int sas_target_alloc(struct scsi_target *);
648 extern int sas_slave_configure(struct scsi_device *);
649 extern int sas_change_queue_depth(struct scsi_device *, int new_depth,
650                                   int reason);
651 extern int sas_change_queue_type(struct scsi_device *, int qt);
652 extern int sas_bios_param(struct scsi_device *,
653                           struct block_device *,
654                           sector_t capacity, int *hsc);
655 extern struct scsi_transport_template *
656 sas_domain_attach_transport(struct sas_domain_function_template *);
657 extern void sas_domain_release_transport(struct scsi_transport_template *);
658
659 int  sas_discover_root_expander(struct domain_device *);
660
661 void sas_init_ex_attr(void);
662
663 int  sas_ex_revalidate_domain(struct domain_device *);
664
665 void sas_unregister_domain_devices(struct asd_sas_port *port, int gone);
666 void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *);
667 int  sas_discover_event(struct asd_sas_port *, enum discover_event ev);
668
669 int  sas_discover_sata(struct domain_device *);
670 int  sas_discover_end_dev(struct domain_device *);
671
672 void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *);
673
674 void sas_init_dev(struct domain_device *);
675
676 void sas_task_abort(struct sas_task *);
677 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd);
678 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd);
679
680 extern void sas_target_destroy(struct scsi_target *);
681 extern int sas_slave_alloc(struct scsi_device *);
682 extern int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg);
683 extern int sas_drain_work(struct sas_ha_struct *ha);
684
685 extern int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
686                            struct request *req);
687
688 extern void sas_ssp_task_response(struct device *dev, struct sas_task *task,
689                                   struct ssp_response_iu *iu);
690 struct sas_phy *sas_get_local_phy(struct domain_device *dev);
691
692 int sas_request_addr(struct Scsi_Host *shost, u8 *addr);
693
694 #endif /* _SASLIB_H_ */