12908d778SJames Bottomley /* 22908d778SJames Bottomley * SAS host prototypes and structures header file 32908d778SJames Bottomley * 42908d778SJames Bottomley * Copyright (C) 2005 Adaptec, Inc. All rights reserved. 52908d778SJames Bottomley * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com> 62908d778SJames Bottomley * 72908d778SJames Bottomley * This file is licensed under GPLv2. 82908d778SJames Bottomley * 92908d778SJames Bottomley * This program is free software; you can redistribute it and/or 102908d778SJames Bottomley * modify it under the terms of the GNU General Public License as 112908d778SJames Bottomley * published by the Free Software Foundation; either version 2 of the 122908d778SJames Bottomley * License, or (at your option) any later version. 132908d778SJames Bottomley * 142908d778SJames Bottomley * This program is distributed in the hope that it will be useful, but 152908d778SJames Bottomley * WITHOUT ANY WARRANTY; without even the implied warranty of 162908d778SJames Bottomley * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 172908d778SJames Bottomley * General Public License for more details. 182908d778SJames Bottomley * 192908d778SJames Bottomley * You should have received a copy of the GNU General Public License 202908d778SJames Bottomley * along with this program; if not, write to the Free Software 212908d778SJames Bottomley * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 222908d778SJames Bottomley * USA 232908d778SJames Bottomley * 242908d778SJames Bottomley */ 252908d778SJames Bottomley 262908d778SJames Bottomley #ifndef _LIBSAS_H_ 272908d778SJames Bottomley #define _LIBSAS_H_ 282908d778SJames Bottomley 292908d778SJames Bottomley 302908d778SJames Bottomley #include <linux/timer.h> 312908d778SJames Bottomley #include <linux/pci.h> 322908d778SJames Bottomley #include <scsi/sas.h> 33fa1c1e8fSDarrick J. Wong #include <linux/libata.h> 342908d778SJames Bottomley #include <linux/list.h> 352908d778SJames Bottomley #include <scsi/scsi_device.h> 362908d778SJames Bottomley #include <scsi/scsi_cmnd.h> 372908d778SJames Bottomley #include <scsi/scsi_transport_sas.h> 3887ae9afdSAdrian Bunk #include <linux/scatterlist.h> 395a0e3ad6STejun Heo #include <linux/slab.h> 402908d778SJames Bottomley 412908d778SJames Bottomley struct block_device; 422908d778SJames Bottomley 432908d778SJames Bottomley enum sas_class { 442908d778SJames Bottomley SAS, 452908d778SJames Bottomley EXPANDER 462908d778SJames Bottomley }; 472908d778SJames Bottomley 482908d778SJames Bottomley enum sas_phy_role { 492908d778SJames Bottomley PHY_ROLE_NONE = 0, 502908d778SJames Bottomley PHY_ROLE_TARGET = 0x40, 512908d778SJames Bottomley PHY_ROLE_INITIATOR = 0x80, 522908d778SJames Bottomley }; 532908d778SJames Bottomley 542908d778SJames Bottomley enum sas_phy_type { 552908d778SJames Bottomley PHY_TYPE_PHYSICAL, 562908d778SJames Bottomley PHY_TYPE_VIRTUAL 572908d778SJames Bottomley }; 582908d778SJames Bottomley 592908d778SJames Bottomley /* The events are mnemonically described in sas_dump.c 602908d778SJames Bottomley * so when updating/adding events here, please also 612908d778SJames Bottomley * update the other file too. 622908d778SJames Bottomley */ 632908d778SJames Bottomley enum ha_event { 642908d778SJames Bottomley HAE_RESET = 0U, 652908d778SJames Bottomley HA_NUM_EVENTS = 1, 662908d778SJames Bottomley }; 672908d778SJames Bottomley 682908d778SJames Bottomley enum port_event { 692908d778SJames Bottomley PORTE_BYTES_DMAED = 0U, 702908d778SJames Bottomley PORTE_BROADCAST_RCVD = 1, 712908d778SJames Bottomley PORTE_LINK_RESET_ERR = 2, 722908d778SJames Bottomley PORTE_TIMER_EVENT = 3, 732908d778SJames Bottomley PORTE_HARD_RESET = 4, 742908d778SJames Bottomley PORT_NUM_EVENTS = 5, 752908d778SJames Bottomley }; 762908d778SJames Bottomley 772908d778SJames Bottomley enum phy_event { 782908d778SJames Bottomley PHYE_LOSS_OF_SIGNAL = 0U, 792908d778SJames Bottomley PHYE_OOB_DONE = 1, 802908d778SJames Bottomley PHYE_OOB_ERROR = 2, 812908d778SJames Bottomley PHYE_SPINUP_HOLD = 3, /* hot plug SATA, no COMWAKE sent */ 82303694eeSDan Williams PHYE_RESUME_TIMEOUT = 4, 83303694eeSDan Williams PHY_NUM_EVENTS = 5, 842908d778SJames Bottomley }; 852908d778SJames Bottomley 862908d778SJames Bottomley enum discover_event { 872908d778SJames Bottomley DISCE_DISCOVER_DOMAIN = 0U, 882908d778SJames Bottomley DISCE_REVALIDATE_DOMAIN = 1, 892908d778SJames Bottomley DISCE_PORT_GONE = 2, 9087c8331fSDan Williams DISCE_PROBE = 3, 91303694eeSDan Williams DISCE_SUSPEND = 4, 92303694eeSDan Williams DISCE_RESUME = 5, 93303694eeSDan Williams DISCE_DESTRUCT = 6, 94303694eeSDan Williams DISC_NUM_EVENTS = 7, 952908d778SJames Bottomley }; 962908d778SJames Bottomley 972908d778SJames Bottomley /* ---------- Expander Devices ---------- */ 982908d778SJames Bottomley 992908d778SJames Bottomley #define to_dom_device(_obj) container_of(_obj, struct domain_device, dev_obj) 1002908d778SJames Bottomley #define to_dev_attr(_attr) container_of(_attr, struct domain_dev_attribute,\ 1012908d778SJames Bottomley attr) 1022908d778SJames Bottomley 1032908d778SJames Bottomley enum routing_attribute { 1042908d778SJames Bottomley DIRECT_ROUTING, 1052908d778SJames Bottomley SUBTRACTIVE_ROUTING, 1062908d778SJames Bottomley TABLE_ROUTING, 1072908d778SJames Bottomley }; 1082908d778SJames Bottomley 1092908d778SJames Bottomley enum ex_phy_state { 1102908d778SJames Bottomley PHY_EMPTY, 1112908d778SJames Bottomley PHY_VACANT, 1122908d778SJames Bottomley PHY_NOT_PRESENT, 1132908d778SJames Bottomley PHY_DEVICE_DISCOVERED 1142908d778SJames Bottomley }; 1152908d778SJames Bottomley 1162908d778SJames Bottomley struct ex_phy { 1172908d778SJames Bottomley int phy_id; 1182908d778SJames Bottomley 1192908d778SJames Bottomley enum ex_phy_state phy_state; 1202908d778SJames Bottomley 121aa9f8328SJames Bottomley enum sas_device_type attached_dev_type; 12288edf746SJames Bottomley enum sas_linkrate linkrate; 1232908d778SJames Bottomley 1242908d778SJames Bottomley u8 attached_sata_host:1; 1252908d778SJames Bottomley u8 attached_sata_dev:1; 1262908d778SJames Bottomley u8 attached_sata_ps:1; 1272908d778SJames Bottomley 1285929faf3SDarrick J. Wong enum sas_protocol attached_tproto; 1295929faf3SDarrick J. Wong enum sas_protocol attached_iproto; 1302908d778SJames Bottomley 1312908d778SJames Bottomley u8 attached_sas_addr[SAS_ADDR_SIZE]; 1322908d778SJames Bottomley u8 attached_phy_id; 1332908d778SJames Bottomley 134303694eeSDan Williams int phy_change_count; 1352908d778SJames Bottomley enum routing_attribute routing_attr; 1362908d778SJames Bottomley u8 virtual:1; 1372908d778SJames Bottomley 1382908d778SJames Bottomley int last_da_index; 1392908d778SJames Bottomley 1402908d778SJames Bottomley struct sas_phy *phy; 1412908d778SJames Bottomley struct sas_port *port; 1422908d778SJames Bottomley }; 1432908d778SJames Bottomley 1442908d778SJames Bottomley struct expander_device { 1452908d778SJames Bottomley struct list_head children; 1462908d778SJames Bottomley 147303694eeSDan Williams int ex_change_count; 1482908d778SJames Bottomley u16 max_route_indexes; 1492908d778SJames Bottomley u8 num_phys; 150ffaac8f4SLuben Tuikov 151ffaac8f4SLuben Tuikov u8 t2t_supp:1; 1522908d778SJames Bottomley u8 configuring:1; 1532908d778SJames Bottomley u8 conf_route_table:1; 154ffaac8f4SLuben Tuikov 1552908d778SJames Bottomley u8 enclosure_logical_id[8]; 1562908d778SJames Bottomley 1572908d778SJames Bottomley struct ex_phy *ex_phy; 1582908d778SJames Bottomley struct sas_port *parent_port; 15989d3cf6aSJeff Skirvin 16089d3cf6aSJeff Skirvin struct mutex cmd_mutex; 1612908d778SJames Bottomley }; 1622908d778SJames Bottomley 1632908d778SJames Bottomley /* ---------- SATA device ---------- */ 1646ef1b512SDan Williams #define ATA_RESP_FIS_SIZE 24 1656ef1b512SDan Williams 1662908d778SJames Bottomley struct sata_device { 1671cbd772dSHannes Reinecke unsigned int class; 1682908d778SJames Bottomley struct smp_resp rps_resp; /* report_phy_sata_resp */ 1692908d778SJames Bottomley u8 port_no; /* port number, if this is a PM (Port) */ 170fa1c1e8fSDarrick J. Wong 171fa1c1e8fSDarrick J. Wong struct ata_port *ap; 172fa1c1e8fSDarrick J. Wong struct ata_host ata_host; 1736ef1b512SDan Williams u8 fis[ATA_RESP_FIS_SIZE]; 1742908d778SJames Bottomley }; 1752908d778SJames Bottomley 1765db45bdcSDan Williams struct ssp_device { 1775db45bdcSDan Williams struct list_head eh_list_node; /* pending a user requested eh action */ 1785db45bdcSDan Williams struct scsi_lun reset_lun; 1795db45bdcSDan Williams }; 1805db45bdcSDan Williams 181e139942dSDan Williams enum { 182e139942dSDan Williams SAS_DEV_GONE, 183303694eeSDan Williams SAS_DEV_FOUND, /* device notified to lldd */ 18487c8331fSDan Williams SAS_DEV_DESTROY, 185e4a9c373SDan Williams SAS_DEV_EH_PENDING, 1865db45bdcSDan Williams SAS_DEV_LU_RESET, 1875db45bdcSDan Williams SAS_DEV_RESET, 188e139942dSDan Williams }; 189e139942dSDan Williams 1902908d778SJames Bottomley struct domain_device { 1919095a64aSDan Williams spinlock_t done_lock; 192aa9f8328SJames Bottomley enum sas_device_type dev_type; 1932908d778SJames Bottomley 19488edf746SJames Bottomley enum sas_linkrate linkrate; 19588edf746SJames Bottomley enum sas_linkrate min_linkrate; 19688edf746SJames Bottomley enum sas_linkrate max_linkrate; 1972908d778SJames Bottomley 1982908d778SJames Bottomley int pathways; 1992908d778SJames Bottomley 2002908d778SJames Bottomley struct domain_device *parent; 2012908d778SJames Bottomley struct list_head siblings; /* devices on the same level */ 2022908d778SJames Bottomley struct asd_sas_port *port; /* shortcut to root of the tree */ 203f41a0c44SDan Williams struct sas_phy *phy; 2042908d778SJames Bottomley 2052908d778SJames Bottomley struct list_head dev_list_node; 20687c8331fSDan Williams struct list_head disco_list_node; /* awaiting probe or destruct */ 2072908d778SJames Bottomley 2085929faf3SDarrick J. Wong enum sas_protocol iproto; 2095929faf3SDarrick J. Wong enum sas_protocol tproto; 2102908d778SJames Bottomley 2112908d778SJames Bottomley struct sas_rphy *rphy; 2122908d778SJames Bottomley 2132908d778SJames Bottomley u8 sas_addr[SAS_ADDR_SIZE]; 2142908d778SJames Bottomley u8 hashed_sas_addr[HASHED_SAS_ADDR_SIZE]; 2152908d778SJames Bottomley 2162908d778SJames Bottomley u8 frame_rcvd[32]; 2172908d778SJames Bottomley 2182908d778SJames Bottomley union { 2192908d778SJames Bottomley struct expander_device ex_dev; 2202908d778SJames Bottomley struct sata_device sata_dev; /* STP & directly attached */ 2215db45bdcSDan Williams struct ssp_device ssp_dev; 2222908d778SJames Bottomley }; 2232908d778SJames Bottomley 2242908d778SJames Bottomley void *lldd_dev; 225e139942dSDan Williams unsigned long state; 226735f7d2fSDan Williams struct kref kref; 2272908d778SJames Bottomley }; 2282908d778SJames Bottomley 22922b9153fSDan Williams struct sas_work { 23022b9153fSDan Williams struct list_head drain_node; 231c4028958SDavid Howells struct work_struct work; 23222b9153fSDan Williams }; 23322b9153fSDan Williams 23422b9153fSDan Williams static inline void INIT_SAS_WORK(struct sas_work *sw, void (*fn)(struct work_struct *)) 23522b9153fSDan Williams { 23622b9153fSDan Williams INIT_WORK(&sw->work, fn); 23722b9153fSDan Williams INIT_LIST_HEAD(&sw->drain_node); 23822b9153fSDan Williams } 23922b9153fSDan Williams 24022b9153fSDan Williams struct sas_discovery_event { 24122b9153fSDan Williams struct sas_work work; 242c4028958SDavid Howells struct asd_sas_port *port; 243c4028958SDavid Howells }; 244c4028958SDavid Howells 24522b9153fSDan Williams static inline struct sas_discovery_event *to_sas_discovery_event(struct work_struct *work) 24622b9153fSDan Williams { 24722b9153fSDan Williams struct sas_discovery_event *ev = container_of(work, typeof(*ev), work.work); 24822b9153fSDan Williams 24922b9153fSDan Williams return ev; 25022b9153fSDan Williams } 25122b9153fSDan Williams 2522908d778SJames Bottomley struct sas_discovery { 253c4028958SDavid Howells struct sas_discovery_event disc_work[DISC_NUM_EVENTS]; 2542908d778SJames Bottomley unsigned long pending; 2552908d778SJames Bottomley u8 fanout_sas_addr[8]; 2562908d778SJames Bottomley u8 eeds_a[8]; 2572908d778SJames Bottomley u8 eeds_b[8]; 2582908d778SJames Bottomley int max_level; 2592908d778SJames Bottomley }; 2602908d778SJames Bottomley 2612908d778SJames Bottomley /* The port struct is Class:RW, driver:RO */ 2622908d778SJames Bottomley struct asd_sas_port { 2632908d778SJames Bottomley /* private: */ 2642908d778SJames Bottomley struct completion port_gone_completion; 2652908d778SJames Bottomley 2662908d778SJames Bottomley struct sas_discovery disc; 2672908d778SJames Bottomley struct domain_device *port_dev; 2682908d778SJames Bottomley spinlock_t dev_list_lock; 2692908d778SJames Bottomley struct list_head dev_list; 27087c8331fSDan Williams struct list_head disco_list; 27187c8331fSDan Williams struct list_head destroy_list; 27288edf746SJames Bottomley enum sas_linkrate linkrate; 2732908d778SJames Bottomley 27422b9153fSDan Williams struct sas_work work; 275303694eeSDan Williams int suspended; 2762908d778SJames Bottomley 2772908d778SJames Bottomley /* public: */ 2782908d778SJames Bottomley int id; 2792908d778SJames Bottomley 2802908d778SJames Bottomley enum sas_class class; 2812908d778SJames Bottomley u8 sas_addr[SAS_ADDR_SIZE]; 2822908d778SJames Bottomley u8 attached_sas_addr[SAS_ADDR_SIZE]; 2835929faf3SDarrick J. Wong enum sas_protocol iproto; 2845929faf3SDarrick J. Wong enum sas_protocol tproto; 2852908d778SJames Bottomley 2862908d778SJames Bottomley enum sas_oob_mode oob_mode; 2872908d778SJames Bottomley 2882908d778SJames Bottomley spinlock_t phy_list_lock; 2892908d778SJames Bottomley struct list_head phy_list; 2902908d778SJames Bottomley int num_phys; 2912908d778SJames Bottomley u32 phy_mask; 2922908d778SJames Bottomley 2932908d778SJames Bottomley struct sas_ha_struct *ha; 2942908d778SJames Bottomley 2952908d778SJames Bottomley struct sas_port *port; 2962908d778SJames Bottomley 2972908d778SJames Bottomley void *lldd_port; /* not touched by the sas class code */ 2982908d778SJames Bottomley }; 2992908d778SJames Bottomley 300c4028958SDavid Howells struct asd_sas_event { 30122b9153fSDan Williams struct sas_work work; 302c4028958SDavid Howells struct asd_sas_phy *phy; 303c4028958SDavid Howells }; 304c4028958SDavid Howells 30522b9153fSDan Williams static inline struct asd_sas_event *to_asd_sas_event(struct work_struct *work) 30622b9153fSDan Williams { 30722b9153fSDan Williams struct asd_sas_event *ev = container_of(work, typeof(*ev), work.work); 30822b9153fSDan Williams 30922b9153fSDan Williams return ev; 31022b9153fSDan Williams } 31122b9153fSDan Williams 3122908d778SJames Bottomley /* The phy pretty much is controlled by the LLDD. 3132908d778SJames Bottomley * The class only reads those fields. 3142908d778SJames Bottomley */ 3152908d778SJames Bottomley struct asd_sas_phy { 3162908d778SJames Bottomley /* private: */ 317c4028958SDavid Howells struct asd_sas_event port_events[PORT_NUM_EVENTS]; 318c4028958SDavid Howells struct asd_sas_event phy_events[PHY_NUM_EVENTS]; 3192908d778SJames Bottomley 3202908d778SJames Bottomley unsigned long port_events_pending; 3212908d778SJames Bottomley unsigned long phy_events_pending; 3222908d778SJames Bottomley 3232908d778SJames Bottomley int error; 324303694eeSDan Williams int suspended; 3252908d778SJames Bottomley 3262908d778SJames Bottomley struct sas_phy *phy; 3272908d778SJames Bottomley 3282908d778SJames Bottomley /* public: */ 3292908d778SJames Bottomley /* The following are class:RO, driver:R/W */ 3302908d778SJames Bottomley int enabled; /* must be set */ 3312908d778SJames Bottomley 3322908d778SJames Bottomley int id; /* must be set */ 3332908d778SJames Bottomley enum sas_class class; 3345929faf3SDarrick J. Wong enum sas_protocol iproto; 3355929faf3SDarrick J. Wong enum sas_protocol tproto; 3362908d778SJames Bottomley 3372908d778SJames Bottomley enum sas_phy_type type; 3382908d778SJames Bottomley enum sas_phy_role role; 3392908d778SJames Bottomley enum sas_oob_mode oob_mode; 34088edf746SJames Bottomley enum sas_linkrate linkrate; 3412908d778SJames Bottomley 3422908d778SJames Bottomley u8 *sas_addr; /* must be set */ 3432908d778SJames Bottomley u8 attached_sas_addr[SAS_ADDR_SIZE]; /* class:RO, driver: R/W */ 3442908d778SJames Bottomley 3452908d778SJames Bottomley spinlock_t frame_rcvd_lock; 3462908d778SJames Bottomley u8 *frame_rcvd; /* must be set */ 3472908d778SJames Bottomley int frame_rcvd_size; 3482908d778SJames Bottomley 3492908d778SJames Bottomley spinlock_t sas_prim_lock; 3502908d778SJames Bottomley u32 sas_prim; 3512908d778SJames Bottomley 3522908d778SJames Bottomley struct list_head port_phy_el; /* driver:RO */ 3532908d778SJames Bottomley struct asd_sas_port *port; /* Class:RW, driver: RO */ 3542908d778SJames Bottomley 3552908d778SJames Bottomley struct sas_ha_struct *ha; /* may be set; the class sets it anyway */ 3562908d778SJames Bottomley 3572908d778SJames Bottomley void *lldd_phy; /* not touched by the sas_class_code */ 3582908d778SJames Bottomley }; 3592908d778SJames Bottomley 3602908d778SJames Bottomley struct scsi_core { 3612908d778SJames Bottomley struct Scsi_Host *shost; 3622908d778SJames Bottomley 3632908d778SJames Bottomley }; 3642908d778SJames Bottomley 365c4028958SDavid Howells struct sas_ha_event { 36622b9153fSDan Williams struct sas_work work; 367c4028958SDavid Howells struct sas_ha_struct *ha; 368c4028958SDavid Howells }; 369c4028958SDavid Howells 37022b9153fSDan Williams static inline struct sas_ha_event *to_sas_ha_event(struct work_struct *work) 37122b9153fSDan Williams { 37222b9153fSDan Williams struct sas_ha_event *ev = container_of(work, typeof(*ev), work.work); 37322b9153fSDan Williams 37422b9153fSDan Williams return ev; 37522b9153fSDan Williams } 37622b9153fSDan Williams 3776b0efb85SDarrick J. Wong enum sas_ha_state { 3786b0efb85SDarrick J. Wong SAS_HA_REGISTERED, 379b1124cd3SDan Williams SAS_HA_DRAINING, 38087c8331fSDan Williams SAS_HA_ATA_EH_ACTIVE, 3819095a64aSDan Williams SAS_HA_FROZEN, 3826b0efb85SDarrick J. Wong }; 3836b0efb85SDarrick J. Wong 3842908d778SJames Bottomley struct sas_ha_struct { 3852908d778SJames Bottomley /* private: */ 386c4028958SDavid Howells struct sas_ha_event ha_events[HA_NUM_EVENTS]; 3872908d778SJames Bottomley unsigned long pending; 3882908d778SJames Bottomley 389b1124cd3SDan Williams struct list_head defer_q; /* work queued while draining */ 390b1124cd3SDan Williams struct mutex drain_mutex; 391f8daa6e6SDan Williams unsigned long state; 392e4a9c373SDan Williams spinlock_t lock; 393e4a9c373SDan Williams int eh_active; 3945db45bdcSDan Williams wait_queue_head_t eh_wait_q; 3955db45bdcSDan Williams struct list_head eh_dev_q; 3966b0efb85SDarrick J. Wong 39787c8331fSDan Williams struct mutex disco_mutex; 39887c8331fSDan Williams 3992908d778SJames Bottomley struct scsi_core core; 4002908d778SJames Bottomley 4012908d778SJames Bottomley /* public: */ 4022908d778SJames Bottomley char *sas_ha_name; 4031d1bbee6SJeff Garzik struct device *dev; /* should be set */ 4042908d778SJames Bottomley struct module *lldd_module; /* should be set */ 4052908d778SJames Bottomley 4062908d778SJames Bottomley u8 *sas_addr; /* must be set */ 4072908d778SJames Bottomley u8 hashed_sas_addr[HASHED_SAS_ADDR_SIZE]; 4082908d778SJames Bottomley 4092908d778SJames Bottomley spinlock_t phy_port_lock; 4102908d778SJames Bottomley struct asd_sas_phy **sas_phy; /* array of valid pointers, must be set */ 4112908d778SJames Bottomley struct asd_sas_port **sas_port; /* array of valid pointers, must be set */ 4122908d778SJames Bottomley int num_phys; /* must be set, gt 0, static */ 4132908d778SJames Bottomley 41400f0254eSDan Williams int strict_wide_ports; /* both sas_addr and attached_sas_addr must match 41500f0254eSDan Williams * their siblings when forming wide ports */ 4162908d778SJames Bottomley 4172908d778SJames Bottomley /* LLDD calls these to notify the class of an event. */ 4186d311fa7SJohannes Thumshirn int (*notify_ha_event)(struct sas_ha_struct *, enum ha_event); 4196d311fa7SJohannes Thumshirn int (*notify_port_event)(struct asd_sas_phy *, enum port_event); 4206d311fa7SJohannes Thumshirn int (*notify_phy_event)(struct asd_sas_phy *, enum phy_event); 4212908d778SJames Bottomley 4222908d778SJames Bottomley void *lldd_ha; /* not touched by sas class code */ 423f456393eSDarrick J. Wong 4243944f509SDan Williams struct list_head eh_done_q; /* complete via scsi_eh_flush_done_q */ 4253944f509SDan Williams struct list_head eh_ata_q; /* scmds to promote from sas to ata eh */ 4262908d778SJames Bottomley }; 4272908d778SJames Bottomley 4282908d778SJames Bottomley #define SHOST_TO_SAS_HA(_shost) (*(struct sas_ha_struct **)(_shost)->hostdata) 4292908d778SJames Bottomley 4302908d778SJames Bottomley static inline struct domain_device * 4312908d778SJames Bottomley starget_to_domain_dev(struct scsi_target *starget) { 4322908d778SJames Bottomley return starget->hostdata; 4332908d778SJames Bottomley } 4342908d778SJames Bottomley 4352908d778SJames Bottomley static inline struct domain_device * 4362908d778SJames Bottomley sdev_to_domain_dev(struct scsi_device *sdev) { 4372908d778SJames Bottomley return starget_to_domain_dev(sdev->sdev_target); 4382908d778SJames Bottomley } 4392908d778SJames Bottomley 440b50102d3SDan Williams static inline struct ata_device *sas_to_ata_dev(struct domain_device *dev) 441b50102d3SDan Williams { 442b50102d3SDan Williams return &dev->sata_dev.ap->link.device[0]; 443b50102d3SDan Williams } 444b50102d3SDan Williams 4452908d778SJames Bottomley static inline struct domain_device * 4462908d778SJames Bottomley cmd_to_domain_dev(struct scsi_cmnd *cmd) 4472908d778SJames Bottomley { 4482908d778SJames Bottomley return sdev_to_domain_dev(cmd->device); 4492908d778SJames Bottomley } 4502908d778SJames Bottomley 4512908d778SJames Bottomley void sas_hash_addr(u8 *hashed, const u8 *sas_addr); 4522908d778SJames Bottomley 4532908d778SJames Bottomley /* Before calling a notify event, LLDD should use this function 4542908d778SJames Bottomley * when the link is severed (possibly from its tasklet). 4552908d778SJames Bottomley * The idea is that the Class only reads those, while the LLDD, 4562908d778SJames Bottomley * can R/W these (thus avoiding a race). 4572908d778SJames Bottomley */ 4582908d778SJames Bottomley static inline void sas_phy_disconnected(struct asd_sas_phy *phy) 4592908d778SJames Bottomley { 4602908d778SJames Bottomley phy->oob_mode = OOB_NOT_CONNECTED; 46188edf746SJames Bottomley phy->linkrate = SAS_LINK_RATE_UNKNOWN; 4622908d778SJames Bottomley } 4632908d778SJames Bottomley 4648ec6552fSDan Williams static inline unsigned int to_sas_gpio_od(int device, int bit) 4658ec6552fSDan Williams { 4668ec6552fSDan Williams return 3 * device + bit; 4678ec6552fSDan Williams } 4688ec6552fSDan Williams 469f41a0c44SDan Williams static inline void sas_put_local_phy(struct sas_phy *phy) 470f41a0c44SDan Williams { 471f41a0c44SDan Williams put_device(&phy->dev); 472f41a0c44SDan Williams } 473f41a0c44SDan Williams 474d962480eSDan Williams #ifdef CONFIG_SCSI_SAS_HOST_SMP 4758ec6552fSDan Williams int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count); 476d962480eSDan Williams #else 477d962480eSDan Williams static inline int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count) 478d962480eSDan Williams { 479d962480eSDan Williams return -1; 480d962480eSDan Williams } 481d962480eSDan Williams #endif 4828ec6552fSDan Williams 4832908d778SJames Bottomley /* ---------- Tasks ---------- */ 4842908d778SJames Bottomley /* 4852908d778SJames Bottomley service_response | SAS_TASK_COMPLETE | SAS_TASK_UNDELIVERED | 4862908d778SJames Bottomley exec_status | | | 4872908d778SJames Bottomley ---------------------+---------------------+-----------------------+ 4882908d778SJames Bottomley SAM_... | X | | 4892908d778SJames Bottomley DEV_NO_RESPONSE | X | X | 4902908d778SJames Bottomley INTERRUPTED | X | | 4912908d778SJames Bottomley QUEUE_FULL | | X | 4922908d778SJames Bottomley DEVICE_UNKNOWN | | X | 4932908d778SJames Bottomley SG_ERR | | X | 4942908d778SJames Bottomley ---------------------+---------------------+-----------------------+ 4952908d778SJames Bottomley */ 4962908d778SJames Bottomley 4972908d778SJames Bottomley enum service_response { 4982908d778SJames Bottomley SAS_TASK_COMPLETE, 4992908d778SJames Bottomley SAS_TASK_UNDELIVERED = -1, 5002908d778SJames Bottomley }; 5012908d778SJames Bottomley 5022908d778SJames Bottomley enum exec_status { 503a3a14252SDan Williams /* The SAM_STAT_.. codes fit in the lower 6 bits, alias some of 504a3a14252SDan Williams * them here to silence 'case value not in enumerated type' warnings 505a3a14252SDan Williams */ 506a3a14252SDan Williams __SAM_STAT_CHECK_CONDITION = SAM_STAT_CHECK_CONDITION, 5072908d778SJames Bottomley 5082908d778SJames Bottomley SAS_DEV_NO_RESPONSE = 0x80, 5092908d778SJames Bottomley SAS_DATA_UNDERRUN, 5102908d778SJames Bottomley SAS_DATA_OVERRUN, 5112908d778SJames Bottomley SAS_INTERRUPTED, 5122908d778SJames Bottomley SAS_QUEUE_FULL, 5132908d778SJames Bottomley SAS_DEVICE_UNKNOWN, 5142908d778SJames Bottomley SAS_SG_ERR, 5152908d778SJames Bottomley SAS_OPEN_REJECT, 5162908d778SJames Bottomley SAS_OPEN_TO, 5172908d778SJames Bottomley SAS_PROTO_RESPONSE, 5182908d778SJames Bottomley SAS_PHY_DOWN, 5192908d778SJames Bottomley SAS_NAK_R_ERR, 5202908d778SJames Bottomley SAS_PENDING, 5212908d778SJames Bottomley SAS_ABORTED_TASK, 5222908d778SJames Bottomley }; 5232908d778SJames Bottomley 5242908d778SJames Bottomley /* When a task finishes with a response, the LLDD examines the 5252908d778SJames Bottomley * response: 5262908d778SJames Bottomley * - For an ATA task task_status_struct::stat is set to 5272908d778SJames Bottomley * SAS_PROTO_RESPONSE, and the task_status_struct::buf is set to the 5282908d778SJames Bottomley * contents of struct ata_task_resp. 5292908d778SJames Bottomley * - For SSP tasks, if no data is present or status/TMF response 5302908d778SJames Bottomley * is valid, task_status_struct::stat is set. If data is present 5312908d778SJames Bottomley * (SENSE data), the LLDD copies up to SAS_STATUS_BUF_SIZE, sets 5322908d778SJames Bottomley * task_status_struct::buf_valid_size, and task_status_struct::stat is 5332908d778SJames Bottomley * set to SAM_CHECK_COND. 5342908d778SJames Bottomley * 5352908d778SJames Bottomley * "buf" has format SCSI Sense for SSP task, or struct ata_task_resp 5362908d778SJames Bottomley * for ATA task. 5372908d778SJames Bottomley * 5382908d778SJames Bottomley * "frame_len" is the total frame length, which could be more or less 5392908d778SJames Bottomley * than actually copied. 5402908d778SJames Bottomley * 5412908d778SJames Bottomley * Tasks ending with response, always set the residual field. 5422908d778SJames Bottomley */ 5432908d778SJames Bottomley struct ata_task_resp { 5442908d778SJames Bottomley u16 frame_len; 5456ef1b512SDan Williams u8 ending_fis[ATA_RESP_FIS_SIZE]; /* dev to host or data-in */ 5462908d778SJames Bottomley }; 5472908d778SJames Bottomley 5482908d778SJames Bottomley #define SAS_STATUS_BUF_SIZE 96 5492908d778SJames Bottomley 5502908d778SJames Bottomley struct task_status_struct { 5512908d778SJames Bottomley enum service_response resp; 5522908d778SJames Bottomley enum exec_status stat; 5532908d778SJames Bottomley int buf_valid_size; 5542908d778SJames Bottomley 5552908d778SJames Bottomley u8 buf[SAS_STATUS_BUF_SIZE]; 5562908d778SJames Bottomley 5572908d778SJames Bottomley u32 residual; 5582908d778SJames Bottomley enum sas_open_rej_reason open_rej_reason; 5592908d778SJames Bottomley }; 5602908d778SJames Bottomley 5612908d778SJames Bottomley /* ATA and ATAPI task queuable to a SAS LLDD. 5622908d778SJames Bottomley */ 5632908d778SJames Bottomley struct sas_ata_task { 5642908d778SJames Bottomley struct host_to_dev_fis fis; 5652908d778SJames Bottomley u8 atapi_packet[16]; /* 0 if not ATAPI task */ 5662908d778SJames Bottomley 5672908d778SJames Bottomley u8 retry_count; /* hardware retry, should be > 0 */ 5682908d778SJames Bottomley 5692908d778SJames Bottomley u8 dma_xfer:1; /* PIO:0 or DMA:1 */ 5702908d778SJames Bottomley u8 use_ncq:1; 5712908d778SJames Bottomley u8 set_affil_pol:1; 5722908d778SJames Bottomley u8 stp_affil_pol:1; 5732908d778SJames Bottomley 5742908d778SJames Bottomley u8 device_control_reg_update:1; 5752908d778SJames Bottomley }; 5762908d778SJames Bottomley 5772908d778SJames Bottomley struct sas_smp_task { 5782908d778SJames Bottomley struct scatterlist smp_req; 5792908d778SJames Bottomley struct scatterlist smp_resp; 5802908d778SJames Bottomley }; 5812908d778SJames Bottomley 5822908d778SJames Bottomley enum task_attribute { 5832908d778SJames Bottomley TASK_ATTR_SIMPLE = 0, 5842908d778SJames Bottomley TASK_ATTR_HOQ = 1, 5852908d778SJames Bottomley TASK_ATTR_ORDERED= 2, 5862908d778SJames Bottomley TASK_ATTR_ACA = 4, 5872908d778SJames Bottomley }; 5882908d778SJames Bottomley 5892908d778SJames Bottomley struct sas_ssp_task { 5902908d778SJames Bottomley u8 retry_count; /* hardware retry, should be > 0 */ 5912908d778SJames Bottomley 5922908d778SJames Bottomley u8 LUN[8]; 5932908d778SJames Bottomley u8 enable_first_burst:1; 5942908d778SJames Bottomley enum task_attribute task_attr; 5952908d778SJames Bottomley u8 task_prio; 596e73823f7SJames Bottomley struct scsi_cmnd *cmd; 5972908d778SJames Bottomley }; 5982908d778SJames Bottomley 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; 6112908d778SJames Bottomley }; 6122908d778SJames Bottomley 6132908d778SJames Bottomley struct scatterlist *scatter; 6142908d778SJames Bottomley int num_scatter; 6152908d778SJames Bottomley u32 total_xfer_len; 6162908d778SJames Bottomley u8 data_dir:2; /* Use PCI_DMA_... */ 6172908d778SJames Bottomley 6182908d778SJames Bottomley struct task_status_struct task_status; 6192908d778SJames Bottomley void (*task_done)(struct sas_task *); 6202908d778SJames Bottomley 6212908d778SJames Bottomley void *lldd_task; /* for use by LLDDs */ 6222908d778SJames Bottomley void *uldd_task; 623f0bf750cSDan Williams struct sas_task_slow *slow_task; 624f0bf750cSDan Williams }; 62579a5eb60SDarrick J. Wong 626f0bf750cSDan Williams struct sas_task_slow { 627f0bf750cSDan Williams /* standard/extra infrastructure for slow path commands (SMP and 628f0bf750cSDan Williams * internal lldd commands 629f0bf750cSDan Williams */ 630f0bf750cSDan Williams struct timer_list timer; 631f0bf750cSDan Williams struct completion completion; 6322908d778SJames Bottomley }; 6332908d778SJames Bottomley 6342908d778SJames Bottomley #define SAS_TASK_STATE_PENDING 1 6352908d778SJames Bottomley #define SAS_TASK_STATE_DONE 2 6362908d778SJames Bottomley #define SAS_TASK_STATE_ABORTED 4 6373ebf6922SDarrick J. Wong #define SAS_TASK_NEED_DEV_RESET 8 638b218a0d8SDarrick J. Wong #define SAS_TASK_AT_INITIATOR 16 6392908d778SJames Bottomley 6404fcf812cSDan Williams extern struct sas_task *sas_alloc_task(gfp_t flags); 641f0bf750cSDan Williams extern struct sas_task *sas_alloc_slow_task(gfp_t flags); 6424fcf812cSDan Williams extern void sas_free_task(struct sas_task *task); 6432908d778SJames Bottomley 6442908d778SJames Bottomley struct sas_domain_function_template { 6452908d778SJames Bottomley /* The class calls these to notify the LLDD of an event. */ 6462908d778SJames Bottomley void (*lldd_port_formed)(struct asd_sas_phy *); 6472908d778SJames Bottomley void (*lldd_port_deformed)(struct asd_sas_phy *); 6482908d778SJames Bottomley 6492908d778SJames Bottomley /* The class calls these when a device is found or gone. */ 6502908d778SJames Bottomley int (*lldd_dev_found)(struct domain_device *); 6512908d778SJames Bottomley void (*lldd_dev_gone)(struct domain_device *); 6522908d778SJames Bottomley 65379855d17SChristoph Hellwig int (*lldd_execute_task)(struct sas_task *, gfp_t gfp_flags); 6542908d778SJames Bottomley 6552908d778SJames Bottomley /* Task Management Functions. Must be called from process context. */ 6562908d778SJames Bottomley int (*lldd_abort_task)(struct sas_task *); 6572908d778SJames Bottomley int (*lldd_abort_task_set)(struct domain_device *, u8 *lun); 6582908d778SJames Bottomley int (*lldd_clear_aca)(struct domain_device *, u8 *lun); 6592908d778SJames Bottomley int (*lldd_clear_task_set)(struct domain_device *, u8 *lun); 6602908d778SJames Bottomley int (*lldd_I_T_nexus_reset)(struct domain_device *); 66136a39947SDan Williams int (*lldd_ata_check_ready)(struct domain_device *); 662b91bb296SDan Williams void (*lldd_ata_set_dmamode)(struct domain_device *); 6632908d778SJames Bottomley int (*lldd_lu_reset)(struct domain_device *, u8 *lun); 6642908d778SJames Bottomley int (*lldd_query_task)(struct sas_task *); 6652908d778SJames Bottomley 6662908d778SJames Bottomley /* Port and Adapter management */ 6672908d778SJames Bottomley int (*lldd_clear_nexus_port)(struct asd_sas_port *); 6682908d778SJames Bottomley int (*lldd_clear_nexus_ha)(struct sas_ha_struct *); 6692908d778SJames Bottomley 6702908d778SJames Bottomley /* Phy management */ 671a01e70e5SJames Bottomley int (*lldd_control_phy)(struct asd_sas_phy *, enum phy_func, void *); 6728ec6552fSDan Williams 6738ec6552fSDan Williams /* GPIO support */ 6748ec6552fSDan Williams int (*lldd_write_gpio)(struct sas_ha_struct *, u8 reg_type, 6758ec6552fSDan Williams u8 reg_index, u8 reg_count, u8 *write_data); 6762908d778SJames Bottomley }; 6772908d778SJames Bottomley 6782908d778SJames Bottomley extern int sas_register_ha(struct sas_ha_struct *); 6792908d778SJames Bottomley extern int sas_unregister_ha(struct sas_ha_struct *); 680303694eeSDan Williams extern void sas_prep_resume_ha(struct sas_ha_struct *sas_ha); 681303694eeSDan Williams extern void sas_resume_ha(struct sas_ha_struct *sas_ha); 682303694eeSDan Williams extern void sas_suspend_ha(struct sas_ha_struct *sas_ha); 6832908d778SJames Bottomley 684acbf167dSDarrick J. Wong int sas_set_phy_speed(struct sas_phy *phy, 685acbf167dSDarrick J. Wong struct sas_phy_linkrates *rates); 686dea22214SDarrick J. Wong int sas_phy_reset(struct sas_phy *phy, int hard_reset); 687f281233dSJeff Garzik extern int sas_queuecommand(struct Scsi_Host * ,struct scsi_cmnd *); 6882908d778SJames Bottomley extern int sas_target_alloc(struct scsi_target *); 6892908d778SJames Bottomley extern int sas_slave_configure(struct scsi_device *); 690db5ed4dfSChristoph Hellwig extern int sas_change_queue_depth(struct scsi_device *, int new_depth); 6912908d778SJames Bottomley extern int sas_bios_param(struct scsi_device *, 6922908d778SJames Bottomley struct block_device *, 6932908d778SJames Bottomley sector_t capacity, int *hsc); 6942908d778SJames Bottomley extern struct scsi_transport_template * 6952908d778SJames Bottomley sas_domain_attach_transport(struct sas_domain_function_template *); 6962908d778SJames Bottomley 6972908d778SJames Bottomley int sas_discover_root_expander(struct domain_device *); 6982908d778SJames Bottomley 6992908d778SJames Bottomley void sas_init_ex_attr(void); 7002908d778SJames Bottomley 7012908d778SJames Bottomley int sas_ex_revalidate_domain(struct domain_device *); 7022908d778SJames Bottomley 7037d05919aSDan Williams void sas_unregister_domain_devices(struct asd_sas_port *port, int gone); 7042908d778SJames Bottomley void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *); 7052908d778SJames Bottomley int sas_discover_event(struct asd_sas_port *, enum discover_event ev); 7062908d778SJames Bottomley 7072908d778SJames Bottomley int sas_discover_sata(struct domain_device *); 7082908d778SJames Bottomley int sas_discover_end_dev(struct domain_device *); 7092908d778SJames Bottomley 7101a34c064SDan Williams void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *); 7112908d778SJames Bottomley 7122908d778SJames Bottomley void sas_init_dev(struct domain_device *); 7132908d778SJames Bottomley 714396819fbSDarrick J. Wong void sas_task_abort(struct sas_task *); 7159524c682SDan Williams int sas_eh_abort_handler(struct scsi_cmnd *cmd); 716ad689233SDarrick J. Wong int sas_eh_device_reset_handler(struct scsi_cmnd *cmd); 717cc199e78SHannes Reinecke int sas_eh_target_reset_handler(struct scsi_cmnd *cmd); 71879a5eb60SDarrick J. Wong 719fa1c1e8fSDarrick J. Wong extern void sas_target_destroy(struct scsi_target *); 720fa1c1e8fSDarrick J. Wong extern int sas_slave_alloc(struct scsi_device *); 721fa1c1e8fSDarrick J. Wong extern int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg); 722b1124cd3SDan Williams extern int sas_drain_work(struct sas_ha_struct *ha); 723fa1c1e8fSDarrick J. Wong 724ba1fc175SFUJITA Tomonori extern int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy, 725ba1fc175SFUJITA Tomonori struct request *req); 726366ca51fSJames Bottomley 727366ca51fSJames Bottomley extern void sas_ssp_task_response(struct device *dev, struct sas_task *task, 728366ca51fSJames Bottomley struct ssp_response_iu *iu); 729f41a0c44SDan Williams struct sas_phy *sas_get_local_phy(struct domain_device *dev); 730366ca51fSJames Bottomley 73145e6cdf4SDarrick J. Wong int sas_request_addr(struct Scsi_Host *shost, u8 *addr); 73245e6cdf4SDarrick J. Wong 7332908d778SJames Bottomley #endif /* _SASLIB_H_ */ 734