xref: /openbmc/linux/include/scsi/libsas.h (revision 2d08f329)
1ba6d10abSLinus Torvalds /* SPDX-License-Identifier: GPL-2.0-only */
22908d778SJames Bottomley /*
32908d778SJames Bottomley  * SAS host prototypes and structures header file
42908d778SJames Bottomley  *
52908d778SJames Bottomley  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
62908d778SJames Bottomley  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
72908d778SJames Bottomley  */
82908d778SJames Bottomley 
92908d778SJames Bottomley #ifndef _LIBSAS_H_
102908d778SJames Bottomley #define _LIBSAS_H_
112908d778SJames Bottomley 
122908d778SJames Bottomley 
132908d778SJames Bottomley #include <linux/timer.h>
142908d778SJames Bottomley #include <linux/pci.h>
152908d778SJames Bottomley #include <scsi/sas.h>
16fa1c1e8fSDarrick J. Wong #include <linux/libata.h>
172908d778SJames Bottomley #include <linux/list.h>
182908d778SJames Bottomley #include <scsi/scsi_device.h>
192908d778SJames Bottomley #include <scsi/scsi_cmnd.h>
202908d778SJames Bottomley #include <scsi/scsi_transport_sas.h>
2187ae9afdSAdrian Bunk #include <linux/scatterlist.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
232908d778SJames Bottomley 
242908d778SJames Bottomley struct block_device;
252908d778SJames Bottomley 
262908d778SJames Bottomley enum sas_class {
272908d778SJames Bottomley 	SAS,
282908d778SJames Bottomley 	EXPANDER
292908d778SJames Bottomley };
302908d778SJames Bottomley 
312908d778SJames Bottomley enum sas_phy_role {
322908d778SJames Bottomley 	PHY_ROLE_NONE = 0,
332908d778SJames Bottomley 	PHY_ROLE_TARGET = 0x40,
342908d778SJames Bottomley 	PHY_ROLE_INITIATOR = 0x80,
352908d778SJames Bottomley };
362908d778SJames Bottomley 
372908d778SJames Bottomley enum sas_phy_type {
382908d778SJames Bottomley 	PHY_TYPE_PHYSICAL,
392908d778SJames Bottomley 	PHY_TYPE_VIRTUAL
402908d778SJames Bottomley };
412908d778SJames Bottomley 
422908d778SJames Bottomley /* The events are mnemonically described in sas_dump.c
432908d778SJames Bottomley  * so when updating/adding events here, please also
442908d778SJames Bottomley  * update the other file too.
452908d778SJames Bottomley  */
462908d778SJames Bottomley enum port_event {
472908d778SJames Bottomley 	PORTE_BYTES_DMAED     = 0U,
480d78f969SJason Yan 	PORTE_BROADCAST_RCVD,
490d78f969SJason Yan 	PORTE_LINK_RESET_ERR,
500d78f969SJason Yan 	PORTE_TIMER_EVENT,
510d78f969SJason Yan 	PORTE_HARD_RESET,
520d78f969SJason Yan 	PORT_NUM_EVENTS,
532908d778SJames Bottomley };
542908d778SJames Bottomley 
552908d778SJames Bottomley enum phy_event {
562908d778SJames Bottomley 	PHYE_LOSS_OF_SIGNAL   = 0U,
570d78f969SJason Yan 	PHYE_OOB_DONE,
580d78f969SJason Yan 	PHYE_OOB_ERROR,
590d78f969SJason Yan 	PHYE_SPINUP_HOLD,             /* hot plug SATA, no COMWAKE sent */
600d78f969SJason Yan 	PHYE_RESUME_TIMEOUT,
61f12486e0SJason Yan 	PHYE_SHUTDOWN,
620d78f969SJason Yan 	PHY_NUM_EVENTS,
632908d778SJames Bottomley };
642908d778SJames Bottomley 
652908d778SJames Bottomley enum discover_event {
662908d778SJames Bottomley 	DISCE_DISCOVER_DOMAIN   = 0U,
670d78f969SJason Yan 	DISCE_REVALIDATE_DOMAIN,
680d78f969SJason Yan 	DISCE_SUSPEND,
690d78f969SJason Yan 	DISCE_RESUME,
700d78f969SJason Yan 	DISC_NUM_EVENTS,
712908d778SJames Bottomley };
722908d778SJames Bottomley 
732908d778SJames Bottomley /* ---------- Expander Devices ---------- */
742908d778SJames Bottomley 
752908d778SJames Bottomley #define to_dom_device(_obj) container_of(_obj, struct domain_device, dev_obj)
762908d778SJames Bottomley #define to_dev_attr(_attr)  container_of(_attr, struct domain_dev_attribute,\
772908d778SJames Bottomley 					 attr)
782908d778SJames Bottomley 
792908d778SJames Bottomley enum routing_attribute {
802908d778SJames Bottomley 	DIRECT_ROUTING,
812908d778SJames Bottomley 	SUBTRACTIVE_ROUTING,
822908d778SJames Bottomley 	TABLE_ROUTING,
832908d778SJames Bottomley };
842908d778SJames Bottomley 
852908d778SJames Bottomley enum ex_phy_state {
862908d778SJames Bottomley 	PHY_EMPTY,
872908d778SJames Bottomley 	PHY_VACANT,
882908d778SJames Bottomley 	PHY_NOT_PRESENT,
892908d778SJames Bottomley 	PHY_DEVICE_DISCOVERED
902908d778SJames Bottomley };
912908d778SJames Bottomley 
922908d778SJames Bottomley struct ex_phy {
932908d778SJames Bottomley 	int    phy_id;
942908d778SJames Bottomley 
952908d778SJames Bottomley 	enum ex_phy_state phy_state;
962908d778SJames Bottomley 
97aa9f8328SJames Bottomley 	enum sas_device_type attached_dev_type;
9888edf746SJames Bottomley 	enum sas_linkrate linkrate;
992908d778SJames Bottomley 
1002908d778SJames Bottomley 	u8   attached_sata_host:1;
1012908d778SJames Bottomley 	u8   attached_sata_dev:1;
1022908d778SJames Bottomley 	u8   attached_sata_ps:1;
1032908d778SJames Bottomley 
1045929faf3SDarrick J. Wong 	enum sas_protocol attached_tproto;
1055929faf3SDarrick J. Wong 	enum sas_protocol attached_iproto;
1062908d778SJames Bottomley 
1072908d778SJames Bottomley 	u8   attached_sas_addr[SAS_ADDR_SIZE];
1082908d778SJames Bottomley 	u8   attached_phy_id;
1092908d778SJames Bottomley 
110303694eeSDan Williams 	int phy_change_count;
1112908d778SJames Bottomley 	enum routing_attribute routing_attr;
1122908d778SJames Bottomley 	u8   virtual:1;
1132908d778SJames Bottomley 
1142908d778SJames Bottomley 	int  last_da_index;
1152908d778SJames Bottomley 
1162908d778SJames Bottomley 	struct sas_phy *phy;
1172908d778SJames Bottomley 	struct sas_port *port;
1182908d778SJames Bottomley };
1192908d778SJames Bottomley 
1202908d778SJames Bottomley struct expander_device {
1212908d778SJames Bottomley 	struct list_head children;
1222908d778SJames Bottomley 
123303694eeSDan Williams 	int    ex_change_count;
1242908d778SJames Bottomley 	u16    max_route_indexes;
1252908d778SJames Bottomley 	u8     num_phys;
126ffaac8f4SLuben Tuikov 
127ffaac8f4SLuben Tuikov 	u8     t2t_supp:1;
1282908d778SJames Bottomley 	u8     configuring:1;
1292908d778SJames Bottomley 	u8     conf_route_table:1;
130ffaac8f4SLuben Tuikov 
1312908d778SJames Bottomley 	u8     enclosure_logical_id[8];
1322908d778SJames Bottomley 
1332908d778SJames Bottomley 	struct ex_phy *ex_phy;
1342908d778SJames Bottomley 	struct sas_port *parent_port;
13589d3cf6aSJeff Skirvin 
13689d3cf6aSJeff Skirvin 	struct mutex cmd_mutex;
1372908d778SJames Bottomley };
1382908d778SJames Bottomley 
1392908d778SJames Bottomley /* ---------- SATA device ---------- */
1406ef1b512SDan Williams #define ATA_RESP_FIS_SIZE 24
1416ef1b512SDan Williams 
1422908d778SJames Bottomley struct sata_device {
1431cbd772dSHannes Reinecke 	unsigned int class;
1442908d778SJames Bottomley 	u8     port_no;        /* port number, if this is a PM (Port) */
145fa1c1e8fSDarrick J. Wong 
146fa1c1e8fSDarrick J. Wong 	struct ata_port *ap;
1472fa4a326SJason Yan 	struct ata_host *ata_host;
1483dafe064SDamien Le Moal 	struct smp_rps_resp rps_resp ____cacheline_aligned; /* report_phy_sata_resp */
1496ef1b512SDan Williams 	u8     fis[ATA_RESP_FIS_SIZE];
1502908d778SJames Bottomley };
1512908d778SJames Bottomley 
1525db45bdcSDan Williams struct ssp_device {
1535db45bdcSDan Williams 	struct list_head eh_list_node; /* pending a user requested eh action */
1545db45bdcSDan Williams 	struct scsi_lun reset_lun;
1555db45bdcSDan Williams };
1565db45bdcSDan Williams 
157e139942dSDan Williams enum {
158e139942dSDan Williams 	SAS_DEV_GONE,
159303694eeSDan Williams 	SAS_DEV_FOUND, /* device notified to lldd */
16087c8331fSDan Williams 	SAS_DEV_DESTROY,
161e4a9c373SDan Williams 	SAS_DEV_EH_PENDING,
1625db45bdcSDan Williams 	SAS_DEV_LU_RESET,
1635db45bdcSDan Williams 	SAS_DEV_RESET,
164e139942dSDan Williams };
165e139942dSDan Williams 
1662908d778SJames Bottomley struct domain_device {
1679095a64aSDan Williams 	spinlock_t done_lock;
168aa9f8328SJames Bottomley 	enum sas_device_type dev_type;
1692908d778SJames Bottomley 
17088edf746SJames Bottomley 	enum sas_linkrate linkrate;
17188edf746SJames Bottomley 	enum sas_linkrate min_linkrate;
17288edf746SJames Bottomley 	enum sas_linkrate max_linkrate;
1732908d778SJames Bottomley 
1742908d778SJames Bottomley 	int  pathways;
1752908d778SJames Bottomley 
1762908d778SJames Bottomley 	struct domain_device *parent;
1772908d778SJames Bottomley 	struct list_head siblings; /* devices on the same level */
1782908d778SJames Bottomley 	struct asd_sas_port *port;        /* shortcut to root of the tree */
179f41a0c44SDan Williams 	struct sas_phy *phy;
1802908d778SJames Bottomley 
1812908d778SJames Bottomley 	struct list_head dev_list_node;
18287c8331fSDan Williams 	struct list_head disco_list_node; /* awaiting probe or destruct */
1832908d778SJames Bottomley 
1845929faf3SDarrick J. Wong 	enum sas_protocol    iproto;
1855929faf3SDarrick J. Wong 	enum sas_protocol    tproto;
1862908d778SJames Bottomley 
1872908d778SJames Bottomley 	struct sas_rphy *rphy;
1882908d778SJames Bottomley 
1892908d778SJames Bottomley 	u8  sas_addr[SAS_ADDR_SIZE];
1902908d778SJames Bottomley 	u8  hashed_sas_addr[HASHED_SAS_ADDR_SIZE];
1912908d778SJames Bottomley 
1922908d778SJames Bottomley 	u8  frame_rcvd[32];
1932908d778SJames Bottomley 
1942908d778SJames Bottomley 	union {
1952908d778SJames Bottomley 		struct expander_device ex_dev;
1962908d778SJames Bottomley 		struct sata_device     sata_dev; /* STP & directly attached */
1975db45bdcSDan Williams 		struct ssp_device      ssp_dev;
1982908d778SJames Bottomley 	};
1992908d778SJames Bottomley 
2002908d778SJames Bottomley 	void *lldd_dev;
201e139942dSDan Williams 	unsigned long state;
202735f7d2fSDan Williams 	struct kref kref;
2032908d778SJames Bottomley };
2042908d778SJames Bottomley 
20522b9153fSDan Williams struct sas_work {
20622b9153fSDan Williams 	struct list_head drain_node;
207c4028958SDavid Howells 	struct work_struct work;
20822b9153fSDan Williams };
20922b9153fSDan Williams 
210924a3541SJohn Garry static inline bool dev_is_expander(enum sas_device_type type)
211085f104aSJohn Garry {
212085f104aSJohn Garry 	return type == SAS_EDGE_EXPANDER_DEVICE ||
213085f104aSJohn Garry 	       type == SAS_FANOUT_EXPANDER_DEVICE;
214085f104aSJohn Garry }
215085f104aSJohn Garry 
21622b9153fSDan Williams static inline void INIT_SAS_WORK(struct sas_work *sw, void (*fn)(struct work_struct *))
21722b9153fSDan Williams {
21822b9153fSDan Williams 	INIT_WORK(&sw->work, fn);
21922b9153fSDan Williams 	INIT_LIST_HEAD(&sw->drain_node);
22022b9153fSDan Williams }
22122b9153fSDan Williams 
22222b9153fSDan Williams struct sas_discovery_event {
22322b9153fSDan Williams 	struct sas_work work;
224c4028958SDavid Howells 	struct asd_sas_port *port;
225c4028958SDavid Howells };
226c4028958SDavid Howells 
22722b9153fSDan Williams static inline struct sas_discovery_event *to_sas_discovery_event(struct work_struct *work)
22822b9153fSDan Williams {
22922b9153fSDan Williams 	struct sas_discovery_event *ev = container_of(work, typeof(*ev), work.work);
23022b9153fSDan Williams 
23122b9153fSDan Williams 	return ev;
23222b9153fSDan Williams }
23322b9153fSDan Williams 
2342908d778SJames Bottomley struct sas_discovery {
235c4028958SDavid Howells 	struct sas_discovery_event disc_work[DISC_NUM_EVENTS];
2362908d778SJames Bottomley 	unsigned long    pending;
2377b27c5feSJohn Garry 	u8     fanout_sas_addr[SAS_ADDR_SIZE];
2387b27c5feSJohn Garry 	u8     eeds_a[SAS_ADDR_SIZE];
2397b27c5feSJohn Garry 	u8     eeds_b[SAS_ADDR_SIZE];
2402908d778SJames Bottomley 	int    max_level;
2412908d778SJames Bottomley };
2422908d778SJames Bottomley 
2432908d778SJames Bottomley /* The port struct is Class:RW, driver:RO */
2442908d778SJames Bottomley struct asd_sas_port {
2452908d778SJames Bottomley /* private: */
2462908d778SJames Bottomley 	struct sas_discovery disc;
2472908d778SJames Bottomley 	struct domain_device *port_dev;
2482908d778SJames Bottomley 	spinlock_t dev_list_lock;
2492908d778SJames Bottomley 	struct list_head dev_list;
25087c8331fSDan Williams 	struct list_head disco_list;
25187c8331fSDan Williams 	struct list_head destroy_list;
2520558f33cSJason Yan 	struct list_head sas_port_del_list;
25388edf746SJames Bottomley 	enum   sas_linkrate linkrate;
2542908d778SJames Bottomley 
25522b9153fSDan Williams 	struct sas_work work;
256303694eeSDan Williams 	int suspended;
2572908d778SJames Bottomley 
2582908d778SJames Bottomley /* public: */
2592908d778SJames Bottomley 	int id;
2602908d778SJames Bottomley 
2612908d778SJames Bottomley 	enum sas_class   class;
2622908d778SJames Bottomley 	u8               sas_addr[SAS_ADDR_SIZE];
2632908d778SJames Bottomley 	u8               attached_sas_addr[SAS_ADDR_SIZE];
2645929faf3SDarrick J. Wong 	enum sas_protocol   iproto;
2655929faf3SDarrick J. Wong 	enum sas_protocol   tproto;
2662908d778SJames Bottomley 
2672908d778SJames Bottomley 	enum sas_oob_mode oob_mode;
2682908d778SJames Bottomley 
2692908d778SJames Bottomley 	spinlock_t       phy_list_lock;
2702908d778SJames Bottomley 	struct list_head phy_list;
2712908d778SJames Bottomley 	int              num_phys;
2722908d778SJames Bottomley 	u32              phy_mask;
2732908d778SJames Bottomley 
2742908d778SJames Bottomley 	struct sas_ha_struct *ha;
2752908d778SJames Bottomley 
2762908d778SJames Bottomley 	struct sas_port	*port;
2772908d778SJames Bottomley 
2782908d778SJames Bottomley 	void *lldd_port;	  /* not touched by the sas class code */
2792908d778SJames Bottomley };
2802908d778SJames Bottomley 
281c4028958SDavid Howells struct asd_sas_event {
28222b9153fSDan Williams 	struct sas_work work;
283c4028958SDavid Howells 	struct asd_sas_phy *phy;
2841c393b97SJason Yan 	int event;
285c4028958SDavid Howells };
286c4028958SDavid Howells 
28722b9153fSDan Williams static inline struct asd_sas_event *to_asd_sas_event(struct work_struct *work)
28822b9153fSDan Williams {
28922b9153fSDan Williams 	struct asd_sas_event *ev = container_of(work, typeof(*ev), work.work);
29022b9153fSDan Williams 
29122b9153fSDan Williams 	return ev;
29222b9153fSDan Williams }
29322b9153fSDan Williams 
2941c393b97SJason Yan static inline void INIT_SAS_EVENT(struct asd_sas_event *ev,
2951c393b97SJason Yan 		void (*fn)(struct work_struct *),
2961c393b97SJason Yan 		struct asd_sas_phy *phy, int event)
2971c393b97SJason Yan {
2981c393b97SJason Yan 	INIT_SAS_WORK(&ev->work, fn);
2991c393b97SJason Yan 	ev->phy = phy;
3001c393b97SJason Yan 	ev->event = event;
3011c393b97SJason Yan }
3021c393b97SJason Yan 
303f12486e0SJason Yan #define SAS_PHY_SHUTDOWN_THRES   1024
3041c393b97SJason Yan 
3052908d778SJames Bottomley /* The phy pretty much is controlled by the LLDD.
3062908d778SJames Bottomley  * The class only reads those fields.
3072908d778SJames Bottomley  */
3082908d778SJames Bottomley struct asd_sas_phy {
3092908d778SJames Bottomley /* private: */
310f12486e0SJason Yan 	atomic_t event_nr;
311f12486e0SJason Yan 	int in_shutdown;
3122908d778SJames Bottomley 	int error;
313303694eeSDan Williams 	int suspended;
3142908d778SJames Bottomley 
3152908d778SJames Bottomley 	struct sas_phy *phy;
3162908d778SJames Bottomley 
3172908d778SJames Bottomley /* public: */
3182908d778SJames Bottomley 	/* The following are class:RO, driver:R/W */
3192908d778SJames Bottomley 	int            enabled;	  /* must be set */
3202908d778SJames Bottomley 
3212908d778SJames Bottomley 	int            id;	  /* must be set */
3222908d778SJames Bottomley 	enum sas_class class;
3235929faf3SDarrick J. Wong 	enum sas_protocol iproto;
3245929faf3SDarrick J. Wong 	enum sas_protocol tproto;
3252908d778SJames Bottomley 
3262908d778SJames Bottomley 	enum sas_phy_type  type;
3272908d778SJames Bottomley 	enum sas_phy_role  role;
3282908d778SJames Bottomley 	enum sas_oob_mode  oob_mode;
32988edf746SJames Bottomley 	enum sas_linkrate linkrate;
3302908d778SJames Bottomley 
3312908d778SJames Bottomley 	u8   *sas_addr;		  /* must be set */
3322908d778SJames Bottomley 	u8   attached_sas_addr[SAS_ADDR_SIZE]; /* class:RO, driver: R/W */
3332908d778SJames Bottomley 
3342908d778SJames Bottomley 	spinlock_t     frame_rcvd_lock;
3352908d778SJames Bottomley 	u8             *frame_rcvd; /* must be set */
3362908d778SJames Bottomley 	int            frame_rcvd_size;
3372908d778SJames Bottomley 
3382908d778SJames Bottomley 	spinlock_t     sas_prim_lock;
3392908d778SJames Bottomley 	u32            sas_prim;
3402908d778SJames Bottomley 
3412908d778SJames Bottomley 	struct list_head port_phy_el; /* driver:RO */
3422908d778SJames Bottomley 	struct asd_sas_port      *port; /* Class:RW, driver: RO */
3432908d778SJames Bottomley 
3442908d778SJames Bottomley 	struct sas_ha_struct *ha; /* may be set; the class sets it anyway */
3452908d778SJames Bottomley 
3462908d778SJames Bottomley 	void *lldd_phy;		  /* not touched by the sas_class_code */
3472908d778SJames Bottomley };
3482908d778SJames Bottomley 
3492908d778SJames Bottomley struct scsi_core {
3502908d778SJames Bottomley 	struct Scsi_Host *shost;
3512908d778SJames Bottomley 
3522908d778SJames Bottomley };
3532908d778SJames Bottomley 
3546b0efb85SDarrick J. Wong enum sas_ha_state {
3556b0efb85SDarrick J. Wong 	SAS_HA_REGISTERED,
356b1124cd3SDan Williams 	SAS_HA_DRAINING,
35787c8331fSDan Williams 	SAS_HA_ATA_EH_ACTIVE,
3589095a64aSDan Williams 	SAS_HA_FROZEN,
3594ea775abSXiang Chen 	SAS_HA_RESUMING,
3606b0efb85SDarrick J. Wong };
3616b0efb85SDarrick J. Wong 
3622908d778SJames Bottomley struct sas_ha_struct {
3632908d778SJames Bottomley /* private: */
364b1124cd3SDan Williams 	struct list_head  defer_q; /* work queued while draining */
365b1124cd3SDan Williams 	struct mutex	  drain_mutex;
366f8daa6e6SDan Williams 	unsigned long	  state;
367e4a9c373SDan Williams 	spinlock_t	  lock;
368e4a9c373SDan Williams 	int		  eh_active;
3695db45bdcSDan Williams 	wait_queue_head_t eh_wait_q;
3705db45bdcSDan Williams 	struct list_head  eh_dev_q;
3716b0efb85SDarrick J. Wong 
37287c8331fSDan Williams 	struct mutex disco_mutex;
37387c8331fSDan Williams 
3742908d778SJames Bottomley 	struct scsi_core core;
3752908d778SJames Bottomley 
3762908d778SJames Bottomley /* public: */
3772908d778SJames Bottomley 	char *sas_ha_name;
3781d1bbee6SJeff Garzik 	struct device *dev;	  /* should be set */
3792908d778SJames Bottomley 	struct module *lldd_module; /* should be set */
3802908d778SJames Bottomley 
38193bdbd06SJason Yan 	struct workqueue_struct *event_q;
38293bdbd06SJason Yan 	struct workqueue_struct *disco_q;
38393bdbd06SJason Yan 
3842908d778SJames Bottomley 	u8 *sas_addr;		  /* must be set */
3852908d778SJames Bottomley 	u8 hashed_sas_addr[HASHED_SAS_ADDR_SIZE];
3862908d778SJames Bottomley 
3872908d778SJames Bottomley 	spinlock_t      phy_port_lock;
3882908d778SJames Bottomley 	struct asd_sas_phy  **sas_phy; /* array of valid pointers, must be set */
3892908d778SJames Bottomley 	struct asd_sas_port **sas_port; /* array of valid pointers, must be set */
3902908d778SJames Bottomley 	int             num_phys; /* must be set, gt 0, static */
3912908d778SJames Bottomley 
39200f0254eSDan Williams 	int strict_wide_ports; /* both sas_addr and attached_sas_addr must match
39300f0254eSDan Williams 				* their siblings when forming wide ports */
3942908d778SJames Bottomley 
3952908d778SJames Bottomley 	void *lldd_ha;		  /* not touched by sas class code */
396f456393eSDarrick J. Wong 
3973944f509SDan Williams 	struct list_head eh_done_q;  /* complete via scsi_eh_flush_done_q */
3983944f509SDan Williams 	struct list_head eh_ata_q; /* scmds to promote from sas to ata eh */
399f12486e0SJason Yan 
400f12486e0SJason Yan 	int event_thres;
4012908d778SJames Bottomley };
4022908d778SJames Bottomley 
4032908d778SJames Bottomley #define SHOST_TO_SAS_HA(_shost) (*(struct sas_ha_struct **)(_shost)->hostdata)
4042908d778SJames Bottomley 
4052908d778SJames Bottomley static inline struct domain_device *
4062908d778SJames Bottomley starget_to_domain_dev(struct scsi_target *starget) {
4072908d778SJames Bottomley 	return starget->hostdata;
4082908d778SJames Bottomley }
4092908d778SJames Bottomley 
4102908d778SJames Bottomley static inline struct domain_device *
4112908d778SJames Bottomley sdev_to_domain_dev(struct scsi_device *sdev) {
4122908d778SJames Bottomley 	return starget_to_domain_dev(sdev->sdev_target);
4132908d778SJames Bottomley }
4142908d778SJames Bottomley 
415b50102d3SDan Williams static inline struct ata_device *sas_to_ata_dev(struct domain_device *dev)
416b50102d3SDan Williams {
417b50102d3SDan Williams 	return &dev->sata_dev.ap->link.device[0];
418b50102d3SDan Williams }
419b50102d3SDan Williams 
4202908d778SJames Bottomley static inline struct domain_device *
4212908d778SJames Bottomley cmd_to_domain_dev(struct scsi_cmnd *cmd)
4222908d778SJames Bottomley {
4232908d778SJames Bottomley 	return sdev_to_domain_dev(cmd->device);
4242908d778SJames Bottomley }
4252908d778SJames Bottomley 
4262908d778SJames Bottomley void sas_hash_addr(u8 *hashed, const u8 *sas_addr);
4272908d778SJames Bottomley 
4282908d778SJames Bottomley /* Before calling a notify event, LLDD should use this function
4292908d778SJames Bottomley  * when the link is severed (possibly from its tasklet).
4302908d778SJames Bottomley  * The idea is that the Class only reads those, while the LLDD,
4312908d778SJames Bottomley  * can R/W these (thus avoiding a race).
4322908d778SJames Bottomley  */
4332908d778SJames Bottomley static inline void sas_phy_disconnected(struct asd_sas_phy *phy)
4342908d778SJames Bottomley {
4352908d778SJames Bottomley 	phy->oob_mode = OOB_NOT_CONNECTED;
43688edf746SJames Bottomley 	phy->linkrate = SAS_LINK_RATE_UNKNOWN;
4372908d778SJames Bottomley }
4382908d778SJames Bottomley 
4398ec6552fSDan Williams static inline unsigned int to_sas_gpio_od(int device, int bit)
4408ec6552fSDan Williams {
4418ec6552fSDan Williams 	return 3 * device + bit;
4428ec6552fSDan Williams }
4438ec6552fSDan Williams 
444f41a0c44SDan Williams static inline void sas_put_local_phy(struct sas_phy *phy)
445f41a0c44SDan Williams {
446f41a0c44SDan Williams 	put_device(&phy->dev);
447f41a0c44SDan Williams }
448f41a0c44SDan Williams 
449d962480eSDan Williams #ifdef CONFIG_SCSI_SAS_HOST_SMP
4508ec6552fSDan Williams int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count);
451d962480eSDan Williams #else
452d962480eSDan Williams static inline int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count)
453d962480eSDan Williams {
454d962480eSDan Williams 	return -1;
455d962480eSDan Williams }
456d962480eSDan Williams #endif
4578ec6552fSDan Williams 
4582908d778SJames Bottomley /* ---------- Tasks ---------- */
4592908d778SJames Bottomley /*
4602908d778SJames Bottomley       service_response |  SAS_TASK_COMPLETE  |  SAS_TASK_UNDELIVERED |
4612908d778SJames Bottomley   exec_status          |                     |                       |
4622908d778SJames Bottomley   ---------------------+---------------------+-----------------------+
4632908d778SJames Bottomley        SAM_...         |         X           |                       |
4642908d778SJames Bottomley        DEV_NO_RESPONSE |         X           |           X           |
4652908d778SJames Bottomley        INTERRUPTED     |         X           |                       |
4662908d778SJames Bottomley        QUEUE_FULL      |                     |           X           |
4672908d778SJames Bottomley        DEVICE_UNKNOWN  |                     |           X           |
4682908d778SJames Bottomley        SG_ERR          |                     |           X           |
4692908d778SJames Bottomley   ---------------------+---------------------+-----------------------+
4702908d778SJames Bottomley  */
4712908d778SJames Bottomley 
4722908d778SJames Bottomley enum service_response {
4732908d778SJames Bottomley 	SAS_TASK_COMPLETE,
4742908d778SJames Bottomley 	SAS_TASK_UNDELIVERED = -1,
4752908d778SJames Bottomley };
4762908d778SJames Bottomley 
4772908d778SJames Bottomley enum exec_status {
478d377f415SBart Van Assche 	/*
479d377f415SBart Van Assche 	 * Values 0..0x7f are used to return the SAM_STAT_* codes.  To avoid
480d377f415SBart Van Assche 	 * 'case value not in enumerated type' compiler warnings every value
481d377f415SBart Van Assche 	 * returned through the exec_status enum needs an alias with the SAS_
482d377f415SBart Van Assche 	 * prefix here.
483a3a14252SDan Williams 	 */
484d377f415SBart Van Assche 	SAS_SAM_STAT_GOOD = SAM_STAT_GOOD,
485d377f415SBart Van Assche 	SAS_SAM_STAT_BUSY = SAM_STAT_BUSY,
486d377f415SBart Van Assche 	SAS_SAM_STAT_TASK_ABORTED = SAM_STAT_TASK_ABORTED,
487d377f415SBart Van Assche 	SAS_SAM_STAT_CHECK_CONDITION = SAM_STAT_CHECK_CONDITION,
4882908d778SJames Bottomley 
4892908d778SJames Bottomley 	SAS_DEV_NO_RESPONSE = 0x80,
4902908d778SJames Bottomley 	SAS_DATA_UNDERRUN,
4912908d778SJames Bottomley 	SAS_DATA_OVERRUN,
4922908d778SJames Bottomley 	SAS_INTERRUPTED,
4932908d778SJames Bottomley 	SAS_QUEUE_FULL,
4942908d778SJames Bottomley 	SAS_DEVICE_UNKNOWN,
4952908d778SJames Bottomley 	SAS_OPEN_REJECT,
4962908d778SJames Bottomley 	SAS_OPEN_TO,
4972908d778SJames Bottomley 	SAS_PROTO_RESPONSE,
4982908d778SJames Bottomley 	SAS_PHY_DOWN,
4992908d778SJames Bottomley 	SAS_NAK_R_ERR,
5002908d778SJames Bottomley 	SAS_PENDING,
5012908d778SJames Bottomley 	SAS_ABORTED_TASK,
5022908d778SJames Bottomley };
5032908d778SJames Bottomley 
5042908d778SJames Bottomley /* When a task finishes with a response, the LLDD examines the
5052908d778SJames Bottomley  * response:
5062908d778SJames Bottomley  *	- For an ATA task task_status_struct::stat is set to
5072908d778SJames Bottomley  * SAS_PROTO_RESPONSE, and the task_status_struct::buf is set to the
5082908d778SJames Bottomley  * contents of struct ata_task_resp.
5092908d778SJames Bottomley  *	- For SSP tasks, if no data is present or status/TMF response
5102908d778SJames Bottomley  * is valid, task_status_struct::stat is set.  If data is present
5112908d778SJames Bottomley  * (SENSE data), the LLDD copies up to SAS_STATUS_BUF_SIZE, sets
5122908d778SJames Bottomley  * task_status_struct::buf_valid_size, and task_status_struct::stat is
5132908d778SJames Bottomley  * set to SAM_CHECK_COND.
5142908d778SJames Bottomley  *
5152908d778SJames Bottomley  * "buf" has format SCSI Sense for SSP task, or struct ata_task_resp
5162908d778SJames Bottomley  * for ATA task.
5172908d778SJames Bottomley  *
5182908d778SJames Bottomley  * "frame_len" is the total frame length, which could be more or less
5192908d778SJames Bottomley  * than actually copied.
5202908d778SJames Bottomley  *
5212908d778SJames Bottomley  * Tasks ending with response, always set the residual field.
5222908d778SJames Bottomley  */
5232908d778SJames Bottomley struct ata_task_resp {
5242908d778SJames Bottomley 	u16  frame_len;
5256ef1b512SDan Williams 	u8   ending_fis[ATA_RESP_FIS_SIZE];	  /* dev to host or data-in */
5262908d778SJames Bottomley };
5272908d778SJames Bottomley 
5282908d778SJames Bottomley #define SAS_STATUS_BUF_SIZE 96
5292908d778SJames Bottomley 
5302908d778SJames Bottomley struct task_status_struct {
5312908d778SJames Bottomley 	enum service_response resp;
5322908d778SJames Bottomley 	enum exec_status      stat;
5332908d778SJames Bottomley 	int  buf_valid_size;
5342908d778SJames Bottomley 
5352908d778SJames Bottomley 	u8   buf[SAS_STATUS_BUF_SIZE];
5362908d778SJames Bottomley 
5372908d778SJames Bottomley 	u32  residual;
5382908d778SJames Bottomley 	enum sas_open_rej_reason open_rej_reason;
5392908d778SJames Bottomley };
5402908d778SJames Bottomley 
5412908d778SJames Bottomley /* ATA and ATAPI task queuable to a SAS LLDD.
5422908d778SJames Bottomley  */
5432908d778SJames Bottomley struct sas_ata_task {
5442908d778SJames Bottomley 	struct host_to_dev_fis fis;
5452908d778SJames Bottomley 	u8     atapi_packet[16];  /* 0 if not ATAPI task */
5462908d778SJames Bottomley 
5472908d778SJames Bottomley 	u8     retry_count;	  /* hardware retry, should be > 0 */
5482908d778SJames Bottomley 
5492908d778SJames Bottomley 	u8     dma_xfer:1;	  /* PIO:0 or DMA:1 */
5502908d778SJames Bottomley 	u8     use_ncq:1;
5512908d778SJames Bottomley 	u8     set_affil_pol:1;
5522908d778SJames Bottomley 	u8     stp_affil_pol:1;
5532908d778SJames Bottomley 
5542908d778SJames Bottomley 	u8     device_control_reg_update:1;
5553f2e252eSJohn Garry 
5563f2e252eSJohn Garry 	bool   force_phy;
5573f2e252eSJohn Garry 	int    force_phy_id;
5582908d778SJames Bottomley };
5592908d778SJames Bottomley 
5605c9bf363SJohn Garry /* LLDDs rely on these values */
5615c9bf363SJohn Garry enum sas_internal_abort {
5625c9bf363SJohn Garry 	SAS_INTERNAL_ABORT_SINGLE	= 0,
5636a91c3e3SJohn Garry 	SAS_INTERNAL_ABORT_DEV		= 1,
5645c9bf363SJohn Garry };
5655c9bf363SJohn Garry 
5665c9bf363SJohn Garry struct sas_internal_abort_task {
5675c9bf363SJohn Garry 	enum sas_internal_abort type;
568095478a6SJohn Garry 	unsigned int qid;
5695c9bf363SJohn Garry 	u16 tag;
5705c9bf363SJohn Garry };
5715c9bf363SJohn Garry 
5722908d778SJames Bottomley struct sas_smp_task {
5732908d778SJames Bottomley 	struct scatterlist smp_req;
5742908d778SJames Bottomley 	struct scatterlist smp_resp;
5752908d778SJames Bottomley };
5762908d778SJames Bottomley 
5772908d778SJames Bottomley enum task_attribute {
5782908d778SJames Bottomley 	TASK_ATTR_SIMPLE = 0,
5792908d778SJames Bottomley 	TASK_ATTR_HOQ    = 1,
5802908d778SJames Bottomley 	TASK_ATTR_ORDERED= 2,
5812908d778SJames Bottomley 	TASK_ATTR_ACA    = 4,
5822908d778SJames Bottomley };
5832908d778SJames Bottomley 
5842908d778SJames Bottomley struct sas_ssp_task {
5852908d778SJames Bottomley 	u8     retry_count;	  /* hardware retry, should be > 0 */
5862908d778SJames Bottomley 
5872908d778SJames Bottomley 	u8     LUN[8];
5882908d778SJames Bottomley 	u8     enable_first_burst:1;
5892908d778SJames Bottomley 	enum   task_attribute task_attr;
5902908d778SJames Bottomley 	u8     task_prio;
591e73823f7SJames Bottomley 	struct scsi_cmnd *cmd;
5922908d778SJames Bottomley };
5932908d778SJames Bottomley 
594bbfe82cdSJohn Garry struct sas_tmf_task {
595bbfe82cdSJohn Garry 	u8 tmf;
596bbfe82cdSJohn Garry 	u16 tag_of_task_to_be_managed;
597bbfe82cdSJohn Garry };
598bbfe82cdSJohn Garry 
5992908d778SJames Bottomley struct sas_task {
6002908d778SJames Bottomley 	struct domain_device *dev;
6012908d778SJames Bottomley 
6022908d778SJames Bottomley 	spinlock_t   task_state_lock;
6032908d778SJames Bottomley 	unsigned     task_state_flags;
6042908d778SJames Bottomley 
6055929faf3SDarrick J. Wong 	enum   sas_protocol      task_proto;
6062908d778SJames Bottomley 
6072908d778SJames Bottomley 	union {
6082908d778SJames Bottomley 		struct sas_ata_task ata_task;
6092908d778SJames Bottomley 		struct sas_smp_task smp_task;
6102908d778SJames Bottomley 		struct sas_ssp_task ssp_task;
6115c9bf363SJohn Garry 		struct sas_internal_abort_task abort_task;
6122908d778SJames Bottomley 	};
6132908d778SJames Bottomley 
6142908d778SJames Bottomley 	struct scatterlist *scatter;
6152908d778SJames Bottomley 	int    num_scatter;
6162908d778SJames Bottomley 	u32    total_xfer_len;
6172908d778SJames Bottomley 	u8     data_dir:2;	  /* Use PCI_DMA_... */
6182908d778SJames Bottomley 
6192908d778SJames Bottomley 	struct task_status_struct task_status;
6202908d778SJames Bottomley 	void   (*task_done)(struct sas_task *);
6212908d778SJames Bottomley 
6222908d778SJames Bottomley 	void   *lldd_task;	  /* for use by LLDDs */
6232908d778SJames Bottomley 	void   *uldd_task;
624f0bf750cSDan Williams 	struct sas_task_slow *slow_task;
62596e54376SJohn Garry 	struct sas_tmf_task *tmf;
626f0bf750cSDan Williams };
62779a5eb60SDarrick J. Wong 
628f0bf750cSDan Williams struct sas_task_slow {
629f0bf750cSDan Williams 	/* standard/extra infrastructure for slow path commands (SMP and
630f0bf750cSDan Williams 	 * internal lldd commands
631f0bf750cSDan Williams 	 */
632f0bf750cSDan Williams 	struct timer_list     timer;
633f0bf750cSDan Williams 	struct completion     completion;
63477570eedSKees Cook 	struct sas_task       *task;
6352908d778SJames Bottomley };
6362908d778SJames Bottomley 
6372908d778SJames Bottomley #define SAS_TASK_STATE_PENDING      1
6382908d778SJames Bottomley #define SAS_TASK_STATE_DONE         2
6392908d778SJames Bottomley #define SAS_TASK_STATE_ABORTED      4
6403ebf6922SDarrick J. Wong #define SAS_TASK_NEED_DEV_RESET     8
6412908d778SJames Bottomley 
6426a91c3e3SJohn Garry static inline bool sas_is_internal_abort(struct sas_task *task)
6436a91c3e3SJohn Garry {
6446a91c3e3SJohn Garry 	return task->task_proto == SAS_PROTOCOL_INTERNAL_ABORT;
6456a91c3e3SJohn Garry }
6466a91c3e3SJohn Garry 
6472908d778SJames Bottomley struct sas_domain_function_template {
6482908d778SJames Bottomley 	/* The class calls these to notify the LLDD of an event. */
6492908d778SJames Bottomley 	void (*lldd_port_formed)(struct asd_sas_phy *);
6502908d778SJames Bottomley 	void (*lldd_port_deformed)(struct asd_sas_phy *);
6512908d778SJames Bottomley 
6522908d778SJames Bottomley 	/* The class calls these when a device is found or gone. */
6532908d778SJames Bottomley 	int  (*lldd_dev_found)(struct domain_device *);
6542908d778SJames Bottomley 	void (*lldd_dev_gone)(struct domain_device *);
6552908d778SJames Bottomley 
65679855d17SChristoph Hellwig 	int (*lldd_execute_task)(struct sas_task *, gfp_t gfp_flags);
6572908d778SJames Bottomley 
6582908d778SJames Bottomley 	/* Task Management Functions. Must be called from process context. */
6592908d778SJames Bottomley 	int (*lldd_abort_task)(struct sas_task *);
6602908d778SJames Bottomley 	int (*lldd_abort_task_set)(struct domain_device *, u8 *lun);
6612908d778SJames Bottomley 	int (*lldd_clear_task_set)(struct domain_device *, u8 *lun);
6622908d778SJames Bottomley 	int (*lldd_I_T_nexus_reset)(struct domain_device *);
66336a39947SDan Williams 	int (*lldd_ata_check_ready)(struct domain_device *);
664b91bb296SDan Williams 	void (*lldd_ata_set_dmamode)(struct domain_device *);
6652908d778SJames Bottomley 	int (*lldd_lu_reset)(struct domain_device *, u8 *lun);
6662908d778SJames Bottomley 	int (*lldd_query_task)(struct sas_task *);
6672908d778SJames Bottomley 
6682037a340SJohn Garry 	/* Special TMF callbacks */
6692037a340SJohn Garry 	void (*lldd_tmf_exec_complete)(struct domain_device *dev);
670693e66a0SJohn Garry 	void (*lldd_tmf_aborted)(struct sas_task *task);
671095478a6SJohn Garry 	bool (*lldd_abort_timeout)(struct sas_task *task, void *data);
6722037a340SJohn Garry 
6732908d778SJames Bottomley 	/* Port and Adapter management */
6742908d778SJames Bottomley 	int (*lldd_clear_nexus_port)(struct asd_sas_port *);
6752908d778SJames Bottomley 	int (*lldd_clear_nexus_ha)(struct sas_ha_struct *);
6762908d778SJames Bottomley 
6772908d778SJames Bottomley 	/* Phy management */
678a01e70e5SJames Bottomley 	int (*lldd_control_phy)(struct asd_sas_phy *, enum phy_func, void *);
6798ec6552fSDan Williams 
6808ec6552fSDan Williams 	/* GPIO support */
6818ec6552fSDan Williams 	int (*lldd_write_gpio)(struct sas_ha_struct *, u8 reg_type,
6828ec6552fSDan Williams 			       u8 reg_index, u8 reg_count, u8 *write_data);
6832908d778SJames Bottomley };
6842908d778SJames Bottomley 
6852908d778SJames Bottomley extern int sas_register_ha(struct sas_ha_struct *);
6862908d778SJames Bottomley extern int sas_unregister_ha(struct sas_ha_struct *);
687303694eeSDan Williams extern void sas_prep_resume_ha(struct sas_ha_struct *sas_ha);
688303694eeSDan Williams extern void sas_resume_ha(struct sas_ha_struct *sas_ha);
689fbefe228SJohn Garry extern void sas_resume_ha_no_sync(struct sas_ha_struct *sas_ha);
690303694eeSDan Williams extern void sas_suspend_ha(struct sas_ha_struct *sas_ha);
6912908d778SJames Bottomley 
692ff525b6eSJohn Garry int sas_set_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates);
693dea22214SDarrick J. Wong int sas_phy_reset(struct sas_phy *phy, int hard_reset);
69400aeaf32SLuo Jiaxing int sas_phy_enable(struct sas_phy *phy, int enable);
695f281233dSJeff Garzik extern int sas_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
6962908d778SJames Bottomley extern int sas_target_alloc(struct scsi_target *);
6972908d778SJames Bottomley extern int sas_slave_configure(struct scsi_device *);
698db5ed4dfSChristoph Hellwig extern int sas_change_queue_depth(struct scsi_device *, int new_depth);
699ff525b6eSJohn Garry extern int sas_bios_param(struct scsi_device *, struct block_device *,
7002908d778SJames Bottomley 			  sector_t capacity, int *hsc);
7015c9bf363SJohn Garry int sas_execute_internal_abort_single(struct domain_device *device,
7025c9bf363SJohn Garry 				      u16 tag, unsigned int qid,
7035c9bf363SJohn Garry 				      void *data);
7046a91c3e3SJohn Garry int sas_execute_internal_abort_dev(struct domain_device *device,
7056a91c3e3SJohn Garry 				   unsigned int qid, void *data);
7062908d778SJames Bottomley extern struct scsi_transport_template *
7072908d778SJames Bottomley sas_domain_attach_transport(struct sas_domain_function_template *);
7088eea9dd8SJason Yan extern struct device_attribute dev_attr_phy_event_threshold;
7092908d778SJames Bottomley 
7102908d778SJames Bottomley int  sas_discover_root_expander(struct domain_device *);
7112908d778SJames Bottomley 
7122908d778SJames Bottomley void sas_init_ex_attr(void);
7132908d778SJames Bottomley 
7142908d778SJames Bottomley int  sas_ex_revalidate_domain(struct domain_device *);
7152908d778SJames Bottomley 
7167d05919aSDan Williams void sas_unregister_domain_devices(struct asd_sas_port *port, int gone);
7172908d778SJames Bottomley void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *);
71826d4a969SXiang Chen void sas_discover_event(struct asd_sas_port *, enum discover_event ev);
7192908d778SJames Bottomley 
7202908d778SJames Bottomley int  sas_discover_sata(struct domain_device *);
7212908d778SJames Bottomley int  sas_discover_end_dev(struct domain_device *);
7222908d778SJames Bottomley 
7231a34c064SDan Williams void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *);
7242908d778SJames Bottomley 
7252908d778SJames Bottomley void sas_init_dev(struct domain_device *);
7262908d778SJames Bottomley 
727396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *);
7289524c682SDan Williams int sas_eh_abort_handler(struct scsi_cmnd *cmd);
729ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd);
730cc199e78SHannes Reinecke int sas_eh_target_reset_handler(struct scsi_cmnd *cmd);
73179a5eb60SDarrick J. Wong 
732fa1c1e8fSDarrick J. Wong extern void sas_target_destroy(struct scsi_target *);
733fa1c1e8fSDarrick J. Wong extern int sas_slave_alloc(struct scsi_device *);
7346f4e626fSNathan Chancellor extern int sas_ioctl(struct scsi_device *sdev, unsigned int cmd,
7356f4e626fSNathan Chancellor 		     void __user *arg);
736b1124cd3SDan Williams extern int sas_drain_work(struct sas_ha_struct *ha);
737fa1c1e8fSDarrick J. Wong 
738366ca51fSJames Bottomley extern void sas_ssp_task_response(struct device *dev, struct sas_task *task,
739366ca51fSJames Bottomley 				  struct ssp_response_iu *iu);
740f41a0c44SDan Williams struct sas_phy *sas_get_local_phy(struct domain_device *dev);
741366ca51fSJames Bottomley 
74245e6cdf4SDarrick J. Wong int sas_request_addr(struct Scsi_Host *shost, u8 *addr);
74345e6cdf4SDarrick J. Wong 
74469b80a0eSJohn Garry int sas_abort_task_set(struct domain_device *dev, u8 *lun);
745e8585452SJohn Garry int sas_clear_task_set(struct domain_device *dev, u8 *lun);
74629d77690SJohn Garry int sas_lu_reset(struct domain_device *dev, u8 *lun);
74772f8810eSJohn Garry int sas_query_task(struct sas_task *task, u16 tag);
7484fea759eSJohn Garry int sas_abort_task(struct sas_task *task, u16 tag);
749*2d08f329SJason Yan int sas_find_attached_phy_id(struct expander_device *ex_dev,
750*2d08f329SJason Yan 			     struct domain_device *dev);
75169b80a0eSJohn Garry 
752f1834fd1SJohn Garry void sas_notify_port_event(struct asd_sas_phy *phy, enum port_event event,
7535d6a75a1SAhmed S. Darwish 			   gfp_t gfp_flags);
754f1834fd1SJohn Garry void sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event,
7555d6a75a1SAhmed S. Darwish 			   gfp_t gfp_flags);
756121181f3SJohn Garry 
7572908d778SJames Bottomley #endif /* _SASLIB_H_ */
758