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_phy_role {
272908d778SJames Bottomley PHY_ROLE_NONE = 0,
282908d778SJames Bottomley PHY_ROLE_TARGET = 0x40,
292908d778SJames Bottomley PHY_ROLE_INITIATOR = 0x80,
302908d778SJames Bottomley };
312908d778SJames Bottomley
322908d778SJames Bottomley /* The events are mnemonically described in sas_dump.c
332908d778SJames Bottomley * so when updating/adding events here, please also
342908d778SJames Bottomley * update the other file too.
352908d778SJames Bottomley */
362908d778SJames Bottomley enum port_event {
372908d778SJames Bottomley PORTE_BYTES_DMAED = 0U,
380d78f969SJason Yan PORTE_BROADCAST_RCVD,
390d78f969SJason Yan PORTE_LINK_RESET_ERR,
400d78f969SJason Yan PORTE_TIMER_EVENT,
410d78f969SJason Yan PORTE_HARD_RESET,
420d78f969SJason Yan PORT_NUM_EVENTS,
432908d778SJames Bottomley };
442908d778SJames Bottomley
452908d778SJames Bottomley enum phy_event {
462908d778SJames Bottomley PHYE_LOSS_OF_SIGNAL = 0U,
470d78f969SJason Yan PHYE_OOB_DONE,
480d78f969SJason Yan PHYE_OOB_ERROR,
490d78f969SJason Yan PHYE_SPINUP_HOLD, /* hot plug SATA, no COMWAKE sent */
500d78f969SJason Yan PHYE_RESUME_TIMEOUT,
51f12486e0SJason Yan PHYE_SHUTDOWN,
520d78f969SJason Yan PHY_NUM_EVENTS,
532908d778SJames Bottomley };
542908d778SJames Bottomley
552908d778SJames Bottomley enum discover_event {
562908d778SJames Bottomley DISCE_DISCOVER_DOMAIN = 0U,
570d78f969SJason Yan DISCE_REVALIDATE_DOMAIN,
580d78f969SJason Yan DISCE_SUSPEND,
590d78f969SJason Yan DISCE_RESUME,
600d78f969SJason Yan DISC_NUM_EVENTS,
612908d778SJames Bottomley };
622908d778SJames Bottomley
632908d778SJames Bottomley /* ---------- Expander Devices ---------- */
642908d778SJames Bottomley
652908d778SJames Bottomley #define to_dom_device(_obj) container_of(_obj, struct domain_device, dev_obj)
662908d778SJames Bottomley #define to_dev_attr(_attr) container_of(_attr, struct domain_dev_attribute,\
672908d778SJames Bottomley attr)
682908d778SJames Bottomley
692908d778SJames Bottomley enum routing_attribute {
702908d778SJames Bottomley DIRECT_ROUTING,
712908d778SJames Bottomley SUBTRACTIVE_ROUTING,
722908d778SJames Bottomley TABLE_ROUTING,
732908d778SJames Bottomley };
742908d778SJames Bottomley
752908d778SJames Bottomley enum ex_phy_state {
762908d778SJames Bottomley PHY_EMPTY,
772908d778SJames Bottomley PHY_VACANT,
782908d778SJames Bottomley PHY_NOT_PRESENT,
792908d778SJames Bottomley PHY_DEVICE_DISCOVERED
802908d778SJames Bottomley };
812908d778SJames Bottomley
822908d778SJames Bottomley struct ex_phy {
832908d778SJames Bottomley int phy_id;
842908d778SJames Bottomley
852908d778SJames Bottomley enum ex_phy_state phy_state;
862908d778SJames Bottomley
87aa9f8328SJames Bottomley enum sas_device_type attached_dev_type;
8888edf746SJames Bottomley enum sas_linkrate linkrate;
892908d778SJames Bottomley
902908d778SJames Bottomley u8 attached_sata_host:1;
912908d778SJames Bottomley u8 attached_sata_dev:1;
922908d778SJames Bottomley u8 attached_sata_ps:1;
932908d778SJames Bottomley
945929faf3SDarrick J. Wong enum sas_protocol attached_tproto;
955929faf3SDarrick J. Wong enum sas_protocol attached_iproto;
962908d778SJames Bottomley
972908d778SJames Bottomley u8 attached_sas_addr[SAS_ADDR_SIZE];
982908d778SJames Bottomley u8 attached_phy_id;
992908d778SJames Bottomley
100303694eeSDan Williams int phy_change_count;
1012908d778SJames Bottomley enum routing_attribute routing_attr;
1022908d778SJames Bottomley u8 virtual:1;
1032908d778SJames Bottomley
1042908d778SJames Bottomley int last_da_index;
1052908d778SJames Bottomley
1062908d778SJames Bottomley struct sas_phy *phy;
1072908d778SJames Bottomley struct sas_port *port;
1082908d778SJames Bottomley };
1092908d778SJames Bottomley
1102908d778SJames Bottomley struct expander_device {
1112908d778SJames Bottomley struct list_head children;
1122908d778SJames Bottomley
113303694eeSDan Williams int ex_change_count;
1142908d778SJames Bottomley u16 max_route_indexes;
1152908d778SJames Bottomley u8 num_phys;
116ffaac8f4SLuben Tuikov
117ffaac8f4SLuben Tuikov u8 t2t_supp:1;
1182908d778SJames Bottomley u8 configuring:1;
1192908d778SJames Bottomley u8 conf_route_table:1;
120ffaac8f4SLuben Tuikov
1212908d778SJames Bottomley u8 enclosure_logical_id[8];
1222908d778SJames Bottomley
1232908d778SJames Bottomley struct ex_phy *ex_phy;
1242908d778SJames Bottomley struct sas_port *parent_port;
12589d3cf6aSJeff Skirvin
12689d3cf6aSJeff Skirvin struct mutex cmd_mutex;
1272908d778SJames Bottomley };
1282908d778SJames Bottomley
1292908d778SJames Bottomley /* ---------- SATA device ---------- */
1306ef1b512SDan Williams #define ATA_RESP_FIS_SIZE 24
1316ef1b512SDan Williams
1322908d778SJames Bottomley struct sata_device {
1331cbd772dSHannes Reinecke unsigned int class;
1342908d778SJames Bottomley u8 port_no; /* port number, if this is a PM (Port) */
135fa1c1e8fSDarrick J. Wong
136fa1c1e8fSDarrick J. Wong struct ata_port *ap;
1372fa4a326SJason Yan struct ata_host *ata_host;
1383dafe064SDamien Le Moal struct smp_rps_resp rps_resp ____cacheline_aligned; /* report_phy_sata_resp */
1396ef1b512SDan Williams u8 fis[ATA_RESP_FIS_SIZE];
1402908d778SJames Bottomley };
1412908d778SJames Bottomley
1425db45bdcSDan Williams struct ssp_device {
1435db45bdcSDan Williams struct list_head eh_list_node; /* pending a user requested eh action */
1445db45bdcSDan Williams struct scsi_lun reset_lun;
1455db45bdcSDan Williams };
1465db45bdcSDan Williams
147e139942dSDan Williams enum {
148e139942dSDan Williams SAS_DEV_GONE,
149303694eeSDan Williams SAS_DEV_FOUND, /* device notified to lldd */
15087c8331fSDan Williams SAS_DEV_DESTROY,
151e4a9c373SDan Williams SAS_DEV_EH_PENDING,
1525db45bdcSDan Williams SAS_DEV_LU_RESET,
1535db45bdcSDan Williams SAS_DEV_RESET,
154e139942dSDan Williams };
155e139942dSDan Williams
1562908d778SJames Bottomley struct domain_device {
1579095a64aSDan Williams spinlock_t done_lock;
158aa9f8328SJames Bottomley enum sas_device_type dev_type;
1592908d778SJames Bottomley
16088edf746SJames Bottomley enum sas_linkrate linkrate;
16188edf746SJames Bottomley enum sas_linkrate min_linkrate;
16288edf746SJames Bottomley enum sas_linkrate max_linkrate;
1632908d778SJames Bottomley
1642908d778SJames Bottomley int pathways;
1652908d778SJames Bottomley
1662908d778SJames Bottomley struct domain_device *parent;
1672908d778SJames Bottomley struct list_head siblings; /* devices on the same level */
1682908d778SJames Bottomley struct asd_sas_port *port; /* shortcut to root of the tree */
169f41a0c44SDan Williams struct sas_phy *phy;
1702908d778SJames Bottomley
1712908d778SJames Bottomley struct list_head dev_list_node;
17287c8331fSDan Williams struct list_head disco_list_node; /* awaiting probe or destruct */
1732908d778SJames Bottomley
1745929faf3SDarrick J. Wong enum sas_protocol iproto;
1755929faf3SDarrick J. Wong enum sas_protocol tproto;
1762908d778SJames Bottomley
1772908d778SJames Bottomley struct sas_rphy *rphy;
1782908d778SJames Bottomley
1792908d778SJames Bottomley u8 sas_addr[SAS_ADDR_SIZE];
1802908d778SJames Bottomley u8 hashed_sas_addr[HASHED_SAS_ADDR_SIZE];
1812908d778SJames Bottomley
1822908d778SJames Bottomley u8 frame_rcvd[32];
1832908d778SJames Bottomley
1842908d778SJames Bottomley union {
1852908d778SJames Bottomley struct expander_device ex_dev;
1862908d778SJames Bottomley struct sata_device sata_dev; /* STP & directly attached */
1875db45bdcSDan Williams struct ssp_device ssp_dev;
1882908d778SJames Bottomley };
1892908d778SJames Bottomley
1902908d778SJames Bottomley void *lldd_dev;
191e139942dSDan Williams unsigned long state;
192735f7d2fSDan Williams struct kref kref;
1932908d778SJames Bottomley };
1942908d778SJames Bottomley
19522b9153fSDan Williams struct sas_work {
19622b9153fSDan Williams struct list_head drain_node;
197c4028958SDavid Howells struct work_struct work;
19822b9153fSDan Williams };
19922b9153fSDan Williams
dev_is_expander(enum sas_device_type type)200924a3541SJohn Garry static inline bool dev_is_expander(enum sas_device_type type)
201085f104aSJohn Garry {
202085f104aSJohn Garry return type == SAS_EDGE_EXPANDER_DEVICE ||
203085f104aSJohn Garry type == SAS_FANOUT_EXPANDER_DEVICE;
204085f104aSJohn Garry }
205085f104aSJohn Garry
INIT_SAS_WORK(struct sas_work * sw,void (* fn)(struct work_struct *))20622b9153fSDan Williams static inline void INIT_SAS_WORK(struct sas_work *sw, void (*fn)(struct work_struct *))
20722b9153fSDan Williams {
20822b9153fSDan Williams INIT_WORK(&sw->work, fn);
20922b9153fSDan Williams INIT_LIST_HEAD(&sw->drain_node);
21022b9153fSDan Williams }
21122b9153fSDan Williams
21222b9153fSDan Williams struct sas_discovery_event {
21322b9153fSDan Williams struct sas_work work;
214c4028958SDavid Howells struct asd_sas_port *port;
215c4028958SDavid Howells };
216c4028958SDavid Howells
to_sas_discovery_event(struct work_struct * work)21722b9153fSDan Williams static inline struct sas_discovery_event *to_sas_discovery_event(struct work_struct *work)
21822b9153fSDan Williams {
21922b9153fSDan Williams struct sas_discovery_event *ev = container_of(work, typeof(*ev), work.work);
22022b9153fSDan Williams
22122b9153fSDan Williams return ev;
22222b9153fSDan Williams }
22322b9153fSDan Williams
2242908d778SJames Bottomley struct sas_discovery {
225c4028958SDavid Howells struct sas_discovery_event disc_work[DISC_NUM_EVENTS];
2262908d778SJames Bottomley unsigned long pending;
2277b27c5feSJohn Garry u8 fanout_sas_addr[SAS_ADDR_SIZE];
2287b27c5feSJohn Garry u8 eeds_a[SAS_ADDR_SIZE];
2297b27c5feSJohn Garry u8 eeds_b[SAS_ADDR_SIZE];
2302908d778SJames Bottomley int max_level;
2312908d778SJames Bottomley };
2322908d778SJames Bottomley
2332908d778SJames Bottomley /* The port struct is Class:RW, driver:RO */
2342908d778SJames Bottomley struct asd_sas_port {
2352908d778SJames Bottomley /* private: */
2362908d778SJames Bottomley struct sas_discovery disc;
2372908d778SJames Bottomley struct domain_device *port_dev;
2382908d778SJames Bottomley spinlock_t dev_list_lock;
2392908d778SJames Bottomley struct list_head dev_list;
24087c8331fSDan Williams struct list_head disco_list;
24187c8331fSDan Williams struct list_head destroy_list;
2420558f33cSJason Yan struct list_head sas_port_del_list;
24388edf746SJames Bottomley enum sas_linkrate linkrate;
2442908d778SJames Bottomley
24522b9153fSDan Williams struct sas_work work;
246303694eeSDan Williams int suspended;
2472908d778SJames Bottomley
2482908d778SJames Bottomley /* public: */
2492908d778SJames Bottomley int id;
2502908d778SJames Bottomley
2512908d778SJames Bottomley u8 sas_addr[SAS_ADDR_SIZE];
2522908d778SJames Bottomley u8 attached_sas_addr[SAS_ADDR_SIZE];
2535929faf3SDarrick J. Wong enum sas_protocol iproto;
2545929faf3SDarrick J. Wong enum sas_protocol tproto;
2552908d778SJames Bottomley
2562908d778SJames Bottomley enum sas_oob_mode oob_mode;
2572908d778SJames Bottomley
2582908d778SJames Bottomley spinlock_t phy_list_lock;
2592908d778SJames Bottomley struct list_head phy_list;
2602908d778SJames Bottomley int num_phys;
2612908d778SJames Bottomley u32 phy_mask;
2622908d778SJames Bottomley
2632908d778SJames Bottomley struct sas_ha_struct *ha;
2642908d778SJames Bottomley
2652908d778SJames Bottomley struct sas_port *port;
2662908d778SJames Bottomley
2672908d778SJames Bottomley void *lldd_port; /* not touched by the sas class code */
2682908d778SJames Bottomley };
2692908d778SJames Bottomley
270c4028958SDavid Howells struct asd_sas_event {
27122b9153fSDan Williams struct sas_work work;
272c4028958SDavid Howells struct asd_sas_phy *phy;
2731c393b97SJason Yan int event;
274c4028958SDavid Howells };
275c4028958SDavid Howells
to_asd_sas_event(struct work_struct * work)27622b9153fSDan Williams static inline struct asd_sas_event *to_asd_sas_event(struct work_struct *work)
27722b9153fSDan Williams {
27822b9153fSDan Williams struct asd_sas_event *ev = container_of(work, typeof(*ev), work.work);
27922b9153fSDan Williams
28022b9153fSDan Williams return ev;
28122b9153fSDan Williams }
28222b9153fSDan Williams
INIT_SAS_EVENT(struct asd_sas_event * ev,void (* fn)(struct work_struct *),struct asd_sas_phy * phy,int event)2831c393b97SJason Yan static inline void INIT_SAS_EVENT(struct asd_sas_event *ev,
2841c393b97SJason Yan void (*fn)(struct work_struct *),
2851c393b97SJason Yan struct asd_sas_phy *phy, int event)
2861c393b97SJason Yan {
2871c393b97SJason Yan INIT_SAS_WORK(&ev->work, fn);
2881c393b97SJason Yan ev->phy = phy;
2891c393b97SJason Yan ev->event = event;
2901c393b97SJason Yan }
2911c393b97SJason Yan
292f12486e0SJason Yan #define SAS_PHY_SHUTDOWN_THRES 1024
2931c393b97SJason Yan
2942908d778SJames Bottomley /* The phy pretty much is controlled by the LLDD.
2952908d778SJames Bottomley * The class only reads those fields.
2962908d778SJames Bottomley */
2972908d778SJames Bottomley struct asd_sas_phy {
2982908d778SJames Bottomley /* private: */
299f12486e0SJason Yan atomic_t event_nr;
300f12486e0SJason Yan int in_shutdown;
3012908d778SJames Bottomley int error;
302303694eeSDan Williams int suspended;
3032908d778SJames Bottomley
3042908d778SJames Bottomley struct sas_phy *phy;
3052908d778SJames Bottomley
3062908d778SJames Bottomley /* public: */
3072908d778SJames Bottomley /* The following are class:RO, driver:R/W */
3082908d778SJames Bottomley int enabled; /* must be set */
3092908d778SJames Bottomley
3102908d778SJames Bottomley int id; /* must be set */
3115929faf3SDarrick J. Wong enum sas_protocol iproto;
3125929faf3SDarrick J. Wong enum sas_protocol tproto;
3132908d778SJames Bottomley
3142908d778SJames Bottomley enum sas_phy_role role;
3152908d778SJames Bottomley enum sas_oob_mode oob_mode;
31688edf746SJames Bottomley enum sas_linkrate linkrate;
3172908d778SJames Bottomley
3182908d778SJames Bottomley u8 *sas_addr; /* must be set */
3192908d778SJames Bottomley u8 attached_sas_addr[SAS_ADDR_SIZE]; /* class:RO, driver: R/W */
3202908d778SJames Bottomley
3212908d778SJames Bottomley spinlock_t frame_rcvd_lock;
3222908d778SJames Bottomley u8 *frame_rcvd; /* must be set */
3232908d778SJames Bottomley int frame_rcvd_size;
3242908d778SJames Bottomley
3252908d778SJames Bottomley spinlock_t sas_prim_lock;
3262908d778SJames Bottomley u32 sas_prim;
3272908d778SJames Bottomley
3282908d778SJames Bottomley struct list_head port_phy_el; /* driver:RO */
3292908d778SJames Bottomley struct asd_sas_port *port; /* Class:RW, driver: RO */
3302908d778SJames Bottomley
3312908d778SJames Bottomley struct sas_ha_struct *ha; /* may be set; the class sets it anyway */
3322908d778SJames Bottomley
3332908d778SJames Bottomley void *lldd_phy; /* not touched by the sas_class_code */
3342908d778SJames Bottomley };
3352908d778SJames Bottomley
3366b0efb85SDarrick J. Wong enum sas_ha_state {
3376b0efb85SDarrick J. Wong SAS_HA_REGISTERED,
338b1124cd3SDan Williams SAS_HA_DRAINING,
33987c8331fSDan Williams SAS_HA_ATA_EH_ACTIVE,
3409095a64aSDan Williams SAS_HA_FROZEN,
3414ea775abSXiang Chen SAS_HA_RESUMING,
3426b0efb85SDarrick J. Wong };
3436b0efb85SDarrick J. Wong
3442908d778SJames Bottomley struct sas_ha_struct {
3452908d778SJames Bottomley /* private: */
346b1124cd3SDan Williams struct list_head defer_q; /* work queued while draining */
347b1124cd3SDan Williams struct mutex drain_mutex;
348f8daa6e6SDan Williams unsigned long state;
349e4a9c373SDan Williams spinlock_t lock;
350e4a9c373SDan Williams int eh_active;
3515db45bdcSDan Williams wait_queue_head_t eh_wait_q;
3525db45bdcSDan Williams struct list_head eh_dev_q;
3536b0efb85SDarrick J. Wong
35487c8331fSDan Williams struct mutex disco_mutex;
35587c8331fSDan Williams
356*1136a022SJohn Garry struct Scsi_Host *shost;
3572908d778SJames Bottomley
3582908d778SJames Bottomley /* public: */
3592908d778SJames Bottomley char *sas_ha_name;
3601d1bbee6SJeff Garzik struct device *dev; /* should be set */
3612908d778SJames Bottomley
36293bdbd06SJason Yan struct workqueue_struct *event_q;
36393bdbd06SJason Yan struct workqueue_struct *disco_q;
36493bdbd06SJason Yan
3652908d778SJames Bottomley u8 *sas_addr; /* must be set */
3662908d778SJames Bottomley u8 hashed_sas_addr[HASHED_SAS_ADDR_SIZE];
3672908d778SJames Bottomley
3682908d778SJames Bottomley spinlock_t phy_port_lock;
3692908d778SJames Bottomley struct asd_sas_phy **sas_phy; /* array of valid pointers, must be set */
3702908d778SJames Bottomley struct asd_sas_port **sas_port; /* array of valid pointers, must be set */
3712908d778SJames Bottomley int num_phys; /* must be set, gt 0, static */
3722908d778SJames Bottomley
37300f0254eSDan Williams int strict_wide_ports; /* both sas_addr and attached_sas_addr must match
37400f0254eSDan Williams * their siblings when forming wide ports */
3752908d778SJames Bottomley
3762908d778SJames Bottomley void *lldd_ha; /* not touched by sas class code */
377f456393eSDarrick J. Wong
3783944f509SDan Williams struct list_head eh_done_q; /* complete via scsi_eh_flush_done_q */
3793944f509SDan Williams struct list_head eh_ata_q; /* scmds to promote from sas to ata eh */
380f12486e0SJason Yan
381f12486e0SJason Yan int event_thres;
3822908d778SJames Bottomley };
3832908d778SJames Bottomley
3842908d778SJames Bottomley #define SHOST_TO_SAS_HA(_shost) (*(struct sas_ha_struct **)(_shost)->hostdata)
3852908d778SJames Bottomley
3862908d778SJames Bottomley static inline struct domain_device *
starget_to_domain_dev(struct scsi_target * starget)3872908d778SJames Bottomley starget_to_domain_dev(struct scsi_target *starget) {
3882908d778SJames Bottomley return starget->hostdata;
3892908d778SJames Bottomley }
3902908d778SJames Bottomley
3912908d778SJames Bottomley static inline struct domain_device *
sdev_to_domain_dev(struct scsi_device * sdev)3922908d778SJames Bottomley sdev_to_domain_dev(struct scsi_device *sdev) {
3932908d778SJames Bottomley return starget_to_domain_dev(sdev->sdev_target);
3942908d778SJames Bottomley }
3952908d778SJames Bottomley
sas_to_ata_dev(struct domain_device * dev)396b50102d3SDan Williams static inline struct ata_device *sas_to_ata_dev(struct domain_device *dev)
397b50102d3SDan Williams {
398b50102d3SDan Williams return &dev->sata_dev.ap->link.device[0];
399b50102d3SDan Williams }
400b50102d3SDan Williams
4012908d778SJames Bottomley static inline struct domain_device *
cmd_to_domain_dev(struct scsi_cmnd * cmd)4022908d778SJames Bottomley cmd_to_domain_dev(struct scsi_cmnd *cmd)
4032908d778SJames Bottomley {
4042908d778SJames Bottomley return sdev_to_domain_dev(cmd->device);
4052908d778SJames Bottomley }
4062908d778SJames Bottomley
4072908d778SJames Bottomley void sas_hash_addr(u8 *hashed, const u8 *sas_addr);
4082908d778SJames Bottomley
4092908d778SJames Bottomley /* Before calling a notify event, LLDD should use this function
4102908d778SJames Bottomley * when the link is severed (possibly from its tasklet).
4112908d778SJames Bottomley * The idea is that the Class only reads those, while the LLDD,
4122908d778SJames Bottomley * can R/W these (thus avoiding a race).
4132908d778SJames Bottomley */
sas_phy_disconnected(struct asd_sas_phy * phy)4142908d778SJames Bottomley static inline void sas_phy_disconnected(struct asd_sas_phy *phy)
4152908d778SJames Bottomley {
4162908d778SJames Bottomley phy->oob_mode = OOB_NOT_CONNECTED;
41788edf746SJames Bottomley phy->linkrate = SAS_LINK_RATE_UNKNOWN;
4182908d778SJames Bottomley }
4192908d778SJames Bottomley
to_sas_gpio_od(int device,int bit)4208ec6552fSDan Williams static inline unsigned int to_sas_gpio_od(int device, int bit)
4218ec6552fSDan Williams {
4228ec6552fSDan Williams return 3 * device + bit;
4238ec6552fSDan Williams }
4248ec6552fSDan Williams
sas_put_local_phy(struct sas_phy * phy)425f41a0c44SDan Williams static inline void sas_put_local_phy(struct sas_phy *phy)
426f41a0c44SDan Williams {
427f41a0c44SDan Williams put_device(&phy->dev);
428f41a0c44SDan Williams }
429f41a0c44SDan Williams
430d962480eSDan Williams #ifdef CONFIG_SCSI_SAS_HOST_SMP
4318ec6552fSDan Williams int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count);
432d962480eSDan Williams #else
try_test_sas_gpio_gp_bit(unsigned int od,u8 * data,u8 index,u8 count)433d962480eSDan Williams static inline int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count)
434d962480eSDan Williams {
435d962480eSDan Williams return -1;
436d962480eSDan Williams }
437d962480eSDan Williams #endif
4388ec6552fSDan Williams
4392908d778SJames Bottomley /* ---------- Tasks ---------- */
4402908d778SJames Bottomley /*
4412908d778SJames Bottomley service_response | SAS_TASK_COMPLETE | SAS_TASK_UNDELIVERED |
4422908d778SJames Bottomley exec_status | | |
4432908d778SJames Bottomley ---------------------+---------------------+-----------------------+
4442908d778SJames Bottomley SAM_... | X | |
4452908d778SJames Bottomley DEV_NO_RESPONSE | X | X |
4462908d778SJames Bottomley INTERRUPTED | X | |
4472908d778SJames Bottomley QUEUE_FULL | | X |
4482908d778SJames Bottomley DEVICE_UNKNOWN | | X |
4492908d778SJames Bottomley SG_ERR | | X |
4502908d778SJames Bottomley ---------------------+---------------------+-----------------------+
4512908d778SJames Bottomley */
4522908d778SJames Bottomley
4532908d778SJames Bottomley enum service_response {
4542908d778SJames Bottomley SAS_TASK_COMPLETE,
4552908d778SJames Bottomley SAS_TASK_UNDELIVERED = -1,
4562908d778SJames Bottomley };
4572908d778SJames Bottomley
4582908d778SJames Bottomley enum exec_status {
459d377f415SBart Van Assche /*
460d377f415SBart Van Assche * Values 0..0x7f are used to return the SAM_STAT_* codes. To avoid
461d377f415SBart Van Assche * 'case value not in enumerated type' compiler warnings every value
462d377f415SBart Van Assche * returned through the exec_status enum needs an alias with the SAS_
463d377f415SBart Van Assche * prefix here.
464a3a14252SDan Williams */
465d377f415SBart Van Assche SAS_SAM_STAT_GOOD = SAM_STAT_GOOD,
466d377f415SBart Van Assche SAS_SAM_STAT_BUSY = SAM_STAT_BUSY,
467d377f415SBart Van Assche SAS_SAM_STAT_TASK_ABORTED = SAM_STAT_TASK_ABORTED,
468d377f415SBart Van Assche SAS_SAM_STAT_CHECK_CONDITION = SAM_STAT_CHECK_CONDITION,
4692908d778SJames Bottomley
4702908d778SJames Bottomley SAS_DEV_NO_RESPONSE = 0x80,
4712908d778SJames Bottomley SAS_DATA_UNDERRUN,
4722908d778SJames Bottomley SAS_DATA_OVERRUN,
4732908d778SJames Bottomley SAS_INTERRUPTED,
4742908d778SJames Bottomley SAS_QUEUE_FULL,
4752908d778SJames Bottomley SAS_DEVICE_UNKNOWN,
4762908d778SJames Bottomley SAS_OPEN_REJECT,
4772908d778SJames Bottomley SAS_OPEN_TO,
4782908d778SJames Bottomley SAS_PROTO_RESPONSE,
4792908d778SJames Bottomley SAS_PHY_DOWN,
4802908d778SJames Bottomley SAS_NAK_R_ERR,
4812908d778SJames Bottomley SAS_PENDING,
4822908d778SJames Bottomley SAS_ABORTED_TASK,
4832908d778SJames Bottomley };
4842908d778SJames Bottomley
4852908d778SJames Bottomley /* When a task finishes with a response, the LLDD examines the
4862908d778SJames Bottomley * response:
4872908d778SJames Bottomley * - For an ATA task task_status_struct::stat is set to
4882908d778SJames Bottomley * SAS_PROTO_RESPONSE, and the task_status_struct::buf is set to the
4892908d778SJames Bottomley * contents of struct ata_task_resp.
4902908d778SJames Bottomley * - For SSP tasks, if no data is present or status/TMF response
4912908d778SJames Bottomley * is valid, task_status_struct::stat is set. If data is present
4922908d778SJames Bottomley * (SENSE data), the LLDD copies up to SAS_STATUS_BUF_SIZE, sets
4932908d778SJames Bottomley * task_status_struct::buf_valid_size, and task_status_struct::stat is
4942908d778SJames Bottomley * set to SAM_CHECK_COND.
4952908d778SJames Bottomley *
4962908d778SJames Bottomley * "buf" has format SCSI Sense for SSP task, or struct ata_task_resp
4972908d778SJames Bottomley * for ATA task.
4982908d778SJames Bottomley *
4992908d778SJames Bottomley * "frame_len" is the total frame length, which could be more or less
5002908d778SJames Bottomley * than actually copied.
5012908d778SJames Bottomley *
5022908d778SJames Bottomley * Tasks ending with response, always set the residual field.
5032908d778SJames Bottomley */
5042908d778SJames Bottomley struct ata_task_resp {
5052908d778SJames Bottomley u16 frame_len;
5066ef1b512SDan Williams u8 ending_fis[ATA_RESP_FIS_SIZE]; /* dev to host or data-in */
5072908d778SJames Bottomley };
5082908d778SJames Bottomley
5092908d778SJames Bottomley #define SAS_STATUS_BUF_SIZE 96
5102908d778SJames Bottomley
5112908d778SJames Bottomley struct task_status_struct {
5122908d778SJames Bottomley enum service_response resp;
5132908d778SJames Bottomley enum exec_status stat;
5142908d778SJames Bottomley int buf_valid_size;
5152908d778SJames Bottomley
5162908d778SJames Bottomley u8 buf[SAS_STATUS_BUF_SIZE];
5172908d778SJames Bottomley
5182908d778SJames Bottomley u32 residual;
5192908d778SJames Bottomley enum sas_open_rej_reason open_rej_reason;
5202908d778SJames Bottomley };
5212908d778SJames Bottomley
5222908d778SJames Bottomley /* ATA and ATAPI task queuable to a SAS LLDD.
5232908d778SJames Bottomley */
5242908d778SJames Bottomley struct sas_ata_task {
5252908d778SJames Bottomley struct host_to_dev_fis fis;
5262908d778SJames Bottomley u8 atapi_packet[16]; /* 0 if not ATAPI task */
5272908d778SJames Bottomley
5282908d778SJames Bottomley u8 dma_xfer:1; /* PIO:0 or DMA:1 */
5292908d778SJames Bottomley u8 use_ncq:1;
5302908d778SJames Bottomley u8 return_fis_on_success:1;
5312908d778SJames Bottomley
5323f2e252eSJohn Garry u8 device_control_reg_update:1;
5333f2e252eSJohn Garry
5343f2e252eSJohn Garry bool force_phy;
5352908d778SJames Bottomley int force_phy_id;
5362908d778SJames Bottomley };
5375c9bf363SJohn Garry
5385c9bf363SJohn Garry /* LLDDs rely on these values */
5395c9bf363SJohn Garry enum sas_internal_abort {
5406a91c3e3SJohn Garry SAS_INTERNAL_ABORT_SINGLE = 0,
5415c9bf363SJohn Garry SAS_INTERNAL_ABORT_DEV = 1,
5425c9bf363SJohn Garry };
5435c9bf363SJohn Garry
5445c9bf363SJohn Garry struct sas_internal_abort_task {
545095478a6SJohn Garry enum sas_internal_abort type;
5465c9bf363SJohn Garry unsigned int qid;
5475c9bf363SJohn Garry u16 tag;
5485c9bf363SJohn Garry };
5492908d778SJames Bottomley
5502908d778SJames Bottomley struct sas_smp_task {
5512908d778SJames Bottomley struct scatterlist smp_req;
5522908d778SJames Bottomley struct scatterlist smp_resp;
5532908d778SJames Bottomley };
5542908d778SJames Bottomley
5552908d778SJames Bottomley enum task_attribute {
5562908d778SJames Bottomley TASK_ATTR_SIMPLE = 0,
5572908d778SJames Bottomley TASK_ATTR_HOQ = 1,
5582908d778SJames Bottomley TASK_ATTR_ORDERED= 2,
5592908d778SJames Bottomley TASK_ATTR_ACA = 4,
5602908d778SJames Bottomley };
5612908d778SJames Bottomley
5622908d778SJames Bottomley struct sas_ssp_task {
5632908d778SJames Bottomley u8 LUN[8];
564e73823f7SJames Bottomley enum task_attribute task_attr;
5652908d778SJames Bottomley struct scsi_cmnd *cmd;
5662908d778SJames Bottomley };
567bbfe82cdSJohn Garry
568bbfe82cdSJohn Garry struct sas_tmf_task {
569bbfe82cdSJohn Garry u8 tmf;
570bbfe82cdSJohn Garry u16 tag_of_task_to_be_managed;
571bbfe82cdSJohn Garry };
5722908d778SJames Bottomley
5732908d778SJames Bottomley struct sas_task {
5742908d778SJames Bottomley struct domain_device *dev;
5752908d778SJames Bottomley
5762908d778SJames Bottomley spinlock_t task_state_lock;
5772908d778SJames Bottomley unsigned task_state_flags;
5785929faf3SDarrick J. Wong
5792908d778SJames Bottomley enum sas_protocol task_proto;
5802908d778SJames Bottomley
5812908d778SJames Bottomley union {
5822908d778SJames Bottomley struct sas_ata_task ata_task;
5832908d778SJames Bottomley struct sas_smp_task smp_task;
5845c9bf363SJohn Garry struct sas_ssp_task ssp_task;
5852908d778SJames Bottomley struct sas_internal_abort_task abort_task;
5862908d778SJames Bottomley };
5872908d778SJames Bottomley
5882908d778SJames Bottomley struct scatterlist *scatter;
5892908d778SJames Bottomley int num_scatter;
5902908d778SJames Bottomley u32 total_xfer_len;
5912908d778SJames Bottomley u8 data_dir:2; /* Use PCI_DMA_... */
5922908d778SJames Bottomley
5932908d778SJames Bottomley struct task_status_struct task_status;
5942908d778SJames Bottomley void (*task_done)(struct sas_task *);
5952908d778SJames Bottomley
5962908d778SJames Bottomley void *lldd_task; /* for use by LLDDs */
597f0bf750cSDan Williams void *uldd_task;
59896e54376SJohn Garry struct sas_task_slow *slow_task;
599f0bf750cSDan Williams struct sas_tmf_task *tmf;
60079a5eb60SDarrick J. Wong };
601f0bf750cSDan Williams
602f0bf750cSDan Williams struct sas_task_slow {
603f0bf750cSDan Williams /* standard/extra infrastructure for slow path commands (SMP and
604f0bf750cSDan Williams * internal lldd commands
605f0bf750cSDan Williams */
606f0bf750cSDan Williams struct timer_list timer;
60777570eedSKees Cook struct completion completion;
6082908d778SJames Bottomley struct sas_task *task;
6092908d778SJames Bottomley };
6102908d778SJames Bottomley
6112908d778SJames Bottomley #define SAS_TASK_STATE_PENDING 1
6122908d778SJames Bottomley #define SAS_TASK_STATE_DONE 2
6133ebf6922SDarrick J. Wong #define SAS_TASK_STATE_ABORTED 4
6142908d778SJames Bottomley #define SAS_TASK_NEED_DEV_RESET 8
6156a91c3e3SJohn Garry
sas_is_internal_abort(struct sas_task * task)6166a91c3e3SJohn Garry static inline bool sas_is_internal_abort(struct sas_task *task)
6176a91c3e3SJohn Garry {
6186a91c3e3SJohn Garry return task->task_proto == SAS_PROTOCOL_INTERNAL_ABORT;
6196a91c3e3SJohn Garry }
620a9ee3f84SJohn Garry
sas_task_find_rq(struct sas_task * task)621a9ee3f84SJohn Garry static inline struct request *sas_task_find_rq(struct sas_task *task)
622a9ee3f84SJohn Garry {
623a9ee3f84SJohn Garry struct scsi_cmnd *scmd;
624a9ee3f84SJohn Garry
625a9ee3f84SJohn Garry if (task->task_proto & SAS_PROTOCOL_STP_ALL) {
626a9ee3f84SJohn Garry struct ata_queued_cmd *qc = task->uldd_task;
627a9ee3f84SJohn Garry
628a9ee3f84SJohn Garry scmd = qc ? qc->scsicmd : NULL;
629a9ee3f84SJohn Garry } else {
630a9ee3f84SJohn Garry scmd = task->uldd_task;
631a9ee3f84SJohn Garry }
632a9ee3f84SJohn Garry
633a9ee3f84SJohn Garry if (!scmd)
634a9ee3f84SJohn Garry return NULL;
635a9ee3f84SJohn Garry
636a9ee3f84SJohn Garry return scsi_cmd_to_rq(scmd);
637a9ee3f84SJohn Garry }
6382908d778SJames Bottomley
6392908d778SJames Bottomley struct sas_domain_function_template {
6402908d778SJames Bottomley /* The class calls these to notify the LLDD of an event. */
6412908d778SJames Bottomley void (*lldd_port_formed)(struct asd_sas_phy *);
6422908d778SJames Bottomley void (*lldd_port_deformed)(struct asd_sas_phy *);
6432908d778SJames Bottomley
6442908d778SJames Bottomley /* The class calls these when a device is found or gone. */
6452908d778SJames Bottomley int (*lldd_dev_found)(struct domain_device *);
6462908d778SJames Bottomley void (*lldd_dev_gone)(struct domain_device *);
64779855d17SChristoph Hellwig
6482908d778SJames Bottomley int (*lldd_execute_task)(struct sas_task *, gfp_t gfp_flags);
6492908d778SJames Bottomley
6502908d778SJames Bottomley /* Task Management Functions. Must be called from process context. */
6512908d778SJames Bottomley int (*lldd_abort_task)(struct sas_task *);
6522908d778SJames Bottomley int (*lldd_abort_task_set)(struct domain_device *, u8 *lun);
6532908d778SJames Bottomley int (*lldd_clear_task_set)(struct domain_device *, u8 *lun);
65436a39947SDan Williams int (*lldd_I_T_nexus_reset)(struct domain_device *);
655b91bb296SDan Williams int (*lldd_ata_check_ready)(struct domain_device *);
6562908d778SJames Bottomley void (*lldd_ata_set_dmamode)(struct domain_device *);
6572908d778SJames Bottomley int (*lldd_lu_reset)(struct domain_device *, u8 *lun);
6582908d778SJames Bottomley int (*lldd_query_task)(struct sas_task *);
6592037a340SJohn Garry
6602037a340SJohn Garry /* Special TMF callbacks */
661693e66a0SJohn Garry void (*lldd_tmf_exec_complete)(struct domain_device *dev);
662095478a6SJohn Garry void (*lldd_tmf_aborted)(struct sas_task *task);
6632037a340SJohn Garry bool (*lldd_abort_timeout)(struct sas_task *task, void *data);
6642908d778SJames Bottomley
6652908d778SJames Bottomley /* Port and Adapter management */
6662908d778SJames Bottomley int (*lldd_clear_nexus_port)(struct asd_sas_port *);
6672908d778SJames Bottomley int (*lldd_clear_nexus_ha)(struct sas_ha_struct *);
6682908d778SJames Bottomley
669a01e70e5SJames Bottomley /* Phy management */
6708ec6552fSDan Williams int (*lldd_control_phy)(struct asd_sas_phy *, enum phy_func, void *);
6718ec6552fSDan Williams
6728ec6552fSDan Williams /* GPIO support */
6738ec6552fSDan Williams int (*lldd_write_gpio)(struct sas_ha_struct *, u8 reg_type,
6742908d778SJames Bottomley u8 reg_index, u8 reg_count, u8 *write_data);
6752908d778SJames Bottomley };
6762908d778SJames Bottomley
6772908d778SJames Bottomley extern int sas_register_ha(struct sas_ha_struct *);
678303694eeSDan Williams extern int sas_unregister_ha(struct sas_ha_struct *);
679303694eeSDan Williams extern void sas_prep_resume_ha(struct sas_ha_struct *sas_ha);
680fbefe228SJohn Garry extern void sas_resume_ha(struct sas_ha_struct *sas_ha);
681303694eeSDan Williams extern void sas_resume_ha_no_sync(struct sas_ha_struct *sas_ha);
6822908d778SJames Bottomley extern void sas_suspend_ha(struct sas_ha_struct *sas_ha);
683ff525b6eSJohn Garry
684dea22214SDarrick J. Wong int sas_set_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates);
68500aeaf32SLuo Jiaxing int sas_phy_reset(struct sas_phy *phy, int hard_reset);
686f281233dSJeff Garzik int sas_phy_enable(struct sas_phy *phy, int enable);
6872908d778SJames Bottomley extern int sas_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
6882908d778SJames Bottomley extern int sas_target_alloc(struct scsi_target *);
689db5ed4dfSChristoph Hellwig extern int sas_slave_configure(struct scsi_device *);
690ff525b6eSJohn Garry extern int sas_change_queue_depth(struct scsi_device *, int new_depth);
6912908d778SJames Bottomley extern int sas_bios_param(struct scsi_device *, struct block_device *,
6925c9bf363SJohn Garry sector_t capacity, int *hsc);
6935c9bf363SJohn Garry int sas_execute_internal_abort_single(struct domain_device *device,
6945c9bf363SJohn Garry u16 tag, unsigned int qid,
6956a91c3e3SJohn Garry void *data);
6966a91c3e3SJohn Garry int sas_execute_internal_abort_dev(struct domain_device *device,
6972908d778SJames Bottomley unsigned int qid, void *data);
6982908d778SJames Bottomley extern struct scsi_transport_template *
6998eea9dd8SJason Yan sas_domain_attach_transport(struct sas_domain_function_template *);
7002908d778SJames Bottomley extern struct device_attribute dev_attr_phy_event_threshold;
7012908d778SJames Bottomley
7022908d778SJames Bottomley int sas_discover_root_expander(struct domain_device *);
7032908d778SJames Bottomley
7042908d778SJames Bottomley int sas_ex_revalidate_domain(struct domain_device *);
7052908d778SJames Bottomley
7062908d778SJames Bottomley void sas_unregister_domain_devices(struct asd_sas_port *port, int gone);
7077d05919aSDan Williams void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *);
7082908d778SJames Bottomley void sas_discover_event(struct asd_sas_port *, enum discover_event ev);
70926d4a969SXiang Chen
7102908d778SJames Bottomley int sas_discover_end_dev(struct domain_device *);
7112908d778SJames Bottomley
7122908d778SJames Bottomley void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *);
7131a34c064SDan Williams
7142908d778SJames Bottomley void sas_init_dev(struct domain_device *);
7152908d778SJames Bottomley
7162908d778SJames Bottomley void sas_task_abort(struct sas_task *);
717396819fbSDarrick J. Wong int sas_eh_abort_handler(struct scsi_cmnd *cmd);
7189524c682SDan Williams int sas_eh_device_reset_handler(struct scsi_cmnd *cmd);
719ad689233SDarrick J. Wong int sas_eh_target_reset_handler(struct scsi_cmnd *cmd);
720cc199e78SHannes Reinecke
72179a5eb60SDarrick J. Wong extern void sas_target_destroy(struct scsi_target *);
722fa1c1e8fSDarrick J. Wong extern int sas_slave_alloc(struct scsi_device *);
723fa1c1e8fSDarrick J. Wong extern int sas_ioctl(struct scsi_device *sdev, unsigned int cmd,
7246f4e626fSNathan Chancellor void __user *arg);
7256f4e626fSNathan Chancellor extern int sas_drain_work(struct sas_ha_struct *ha);
726b1124cd3SDan Williams
727fa1c1e8fSDarrick J. Wong extern void sas_ssp_task_response(struct device *dev, struct sas_task *task,
728366ca51fSJames Bottomley struct ssp_response_iu *iu);
729366ca51fSJames Bottomley struct sas_phy *sas_get_local_phy(struct domain_device *dev);
730f41a0c44SDan Williams
731366ca51fSJames Bottomley int sas_request_addr(struct Scsi_Host *shost, u8 *addr);
73245e6cdf4SDarrick J. Wong
73345e6cdf4SDarrick J. Wong int sas_abort_task_set(struct domain_device *dev, u8 *lun);
73469b80a0eSJohn Garry int sas_clear_task_set(struct domain_device *dev, u8 *lun);
735e8585452SJohn Garry int sas_lu_reset(struct domain_device *dev, u8 *lun);
73629d77690SJohn Garry int sas_query_task(struct sas_task *task, u16 tag);
73772f8810eSJohn Garry int sas_abort_task(struct sas_task *task, u16 tag);
7384fea759eSJohn Garry int sas_find_attached_phy_id(struct expander_device *ex_dev,
7392d08f329SJason Yan struct domain_device *dev);
7402d08f329SJason Yan
74169b80a0eSJohn Garry void sas_notify_port_event(struct asd_sas_phy *phy, enum port_event event,
742f1834fd1SJohn Garry gfp_t gfp_flags);
7435d6a75a1SAhmed S. Darwish void sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event,
744f1834fd1SJohn Garry gfp_t gfp_flags);
7455d6a75a1SAhmed S. Darwish
746121181f3SJohn Garry #endif /* _SASLIB_H_ */
7472908d778SJames Bottomley