1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * libata.h - helper library for ATA
4 *
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
7 *
8 * libata documentation is available via 'make {ps|pdf}docs',
9 * as Documentation/driver-api/libata.rst
10 */
11
12 #ifndef __LIBATA_H__
13 #define __LIBATA_H__
14
15 #define DRV_NAME "libata"
16 #define DRV_VERSION "3.00" /* must be exactly four chars */
17
18 /* libata-core.c */
19 enum {
20 /* flags for ata_dev_read_id() */
21 ATA_READID_POSTRESET = (1 << 0), /* reading ID after reset */
22
23 /* selector for ata_down_xfermask_limit() */
24 ATA_DNXFER_PIO = 0, /* speed down PIO */
25 ATA_DNXFER_DMA = 1, /* speed down DMA */
26 ATA_DNXFER_40C = 2, /* apply 40c cable limit */
27 ATA_DNXFER_FORCE_PIO = 3, /* force PIO */
28 ATA_DNXFER_FORCE_PIO0 = 4, /* force PIO0 */
29
30 ATA_DNXFER_QUIET = (1 << 31),
31 };
32
33 #define ATA_PORT_TYPE_NAME "ata_port"
34
35 extern int atapi_passthru16;
36 extern int libata_fua;
37 extern int libata_noacpi;
38 extern int libata_allow_tpm;
39 extern const struct device_type ata_port_type;
40 extern struct ata_link *ata_dev_phys_link(struct ata_device *dev);
41
ata_sstatus_online(u32 sstatus)42 static inline bool ata_sstatus_online(u32 sstatus)
43 {
44 return (sstatus & 0xf) == 0x3;
45 }
46
ata_dev_is_zac(struct ata_device * dev)47 static inline bool ata_dev_is_zac(struct ata_device *dev)
48 {
49 /* Host managed device or host aware device */
50 return dev->class == ATA_DEV_ZAC ||
51 ata_id_zoned_cap(dev->id) == 0x01;
52 }
53
ata_port_eh_scheduled(struct ata_port * ap)54 static inline bool ata_port_eh_scheduled(struct ata_port *ap)
55 {
56 return ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS);
57 }
58
59 #ifdef CONFIG_ATA_FORCE
60 extern void ata_force_cbl(struct ata_port *ap);
61 #else
ata_force_cbl(struct ata_port * ap)62 static inline void ata_force_cbl(struct ata_port *ap) { }
63 #endif
64 extern u64 ata_tf_to_lba(const struct ata_taskfile *tf);
65 extern u64 ata_tf_to_lba48(const struct ata_taskfile *tf);
66 extern int ata_build_rw_tf(struct ata_queued_cmd *qc, u64 block, u32 n_block,
67 unsigned int tf_flags, int dld, int class);
68 extern u64 ata_tf_read_block(const struct ata_taskfile *tf,
69 struct ata_device *dev);
70 unsigned int ata_exec_internal(struct ata_device *dev, struct ata_taskfile *tf,
71 const u8 *cdb, enum dma_data_direction dma_dir,
72 void *buf, unsigned int buflen,
73 unsigned int timeout);
74 extern int ata_wait_ready(struct ata_link *link, unsigned long deadline,
75 int (*check_ready)(struct ata_link *link));
76 extern int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
77 unsigned int flags, u16 *id);
78 extern int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags);
79 extern int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
80 unsigned int readid_flags);
81 extern int ata_dev_configure(struct ata_device *dev);
82 extern bool ata_dev_power_init_tf(struct ata_device *dev,
83 struct ata_taskfile *tf, bool set_active);
84 extern void ata_dev_power_set_standby(struct ata_device *dev);
85 extern void ata_dev_power_set_active(struct ata_device *dev);
86 void ata_dev_free_resources(struct ata_device *dev);
87 extern int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel);
88 extern unsigned int ata_dev_set_feature(struct ata_device *dev,
89 u8 subcmd, u8 action);
90 extern void ata_qc_free(struct ata_queued_cmd *qc);
91 extern void ata_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
92 __must_hold(ap->lock);
93 extern void __ata_qc_complete(struct ata_queued_cmd *qc);
94 extern int atapi_check_dma(struct ata_queued_cmd *qc);
95 extern void swap_buf_le16(u16 *buf, unsigned int buf_words);
96 extern bool ata_phys_link_online(struct ata_link *link);
97 extern bool ata_phys_link_offline(struct ata_link *link);
98 bool ata_adapter_is_online(struct ata_port *ap);
99 extern void ata_dev_init(struct ata_device *dev);
100 extern void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp);
101 extern int sata_link_init_spd(struct ata_link *link);
102 extern int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg);
103 extern int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg);
104 extern const char *sata_spd_string(unsigned int spd);
105 extern unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
106 u8 page, void *buf, unsigned int sectors);
107
108 #define to_ata_port(d) container_of(d, struct ata_port, tdev)
109
110 /* libata-sata.c */
111 #ifdef CONFIG_SATA_HOST
112 int sata_down_spd_limit(struct ata_link *link, u32 spd_limit);
113 int ata_eh_get_ncq_success_sense(struct ata_link *link);
114 #else
sata_down_spd_limit(struct ata_link * link,u32 spd_limit)115 static inline int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
116 {
117 return -EOPNOTSUPP;
118 }
ata_eh_get_ncq_success_sense(struct ata_link * link)119 static inline int ata_eh_get_ncq_success_sense(struct ata_link *link)
120 {
121 return -EOPNOTSUPP;
122 }
123 #endif
124
125 /* libata-acpi.c */
126 #ifdef CONFIG_ATA_ACPI
127 extern unsigned int ata_acpi_gtf_filter;
128 extern void ata_acpi_dissociate(struct ata_host *host);
129 extern void ata_acpi_on_resume(struct ata_port *ap);
130 extern int ata_acpi_on_devcfg(struct ata_device *dev);
131 extern void ata_acpi_on_disable(struct ata_device *dev);
132 extern void ata_acpi_set_state(struct ata_port *ap, pm_message_t state);
133 extern void ata_acpi_bind_port(struct ata_port *ap);
134 extern void ata_acpi_bind_dev(struct ata_device *dev);
135 extern void ata_acpi_port_power_on(struct ata_port *ap);
136 extern bool ata_acpi_dev_manage_restart(struct ata_device *dev);
137 extern acpi_handle ata_dev_acpi_handle(struct ata_device *dev);
138 #else
ata_acpi_dissociate(struct ata_host * host)139 static inline void ata_acpi_dissociate(struct ata_host *host) { }
ata_acpi_on_resume(struct ata_port * ap)140 static inline void ata_acpi_on_resume(struct ata_port *ap) { }
ata_acpi_on_devcfg(struct ata_device * dev)141 static inline int ata_acpi_on_devcfg(struct ata_device *dev) { return 0; }
ata_acpi_on_disable(struct ata_device * dev)142 static inline void ata_acpi_on_disable(struct ata_device *dev) { }
ata_acpi_set_state(struct ata_port * ap,pm_message_t state)143 static inline void ata_acpi_set_state(struct ata_port *ap,
144 pm_message_t state) { }
ata_acpi_bind_port(struct ata_port * ap)145 static inline void ata_acpi_bind_port(struct ata_port *ap) {}
ata_acpi_bind_dev(struct ata_device * dev)146 static inline void ata_acpi_bind_dev(struct ata_device *dev) {}
ata_acpi_port_power_on(struct ata_port * ap)147 static inline void ata_acpi_port_power_on(struct ata_port *ap) {}
ata_acpi_dev_manage_restart(struct ata_device * dev)148 static inline bool ata_acpi_dev_manage_restart(struct ata_device *dev) { return 0; }
149 #endif
150
151 /* libata-scsi.c */
152 #define ATA_SCSI_RBUF_SIZE 2048
153
154 /*
155 * Maximum number of concurrent positioning ranges (CPR) supported. The ACS
156 * specifications allow up to 255, but we limit this to the number of CPR
157 * descriptors that fit in the rbuf buffer used to emit VPD page B9h.
158 */
159 #define ATA_DEV_MAX_CPR min(255, ((ATA_SCSI_RBUF_SIZE - 64) / 32))
160
161 extern struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
162 const struct scsi_device *scsidev);
163 extern int ata_scsi_add_hosts(struct ata_host *host,
164 const struct scsi_host_template *sht);
165 extern void ata_scsi_scan_host(struct ata_port *ap, int sync);
166 extern bool ata_scsi_offline_dev(struct ata_device *dev);
167 extern bool ata_scsi_sense_is_valid(u8 sk, u8 asc, u8 ascq);
168 extern void ata_scsi_set_sense(struct ata_device *dev,
169 struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq);
170 extern void ata_scsi_media_change_notify(struct ata_device *dev);
171 extern void ata_scsi_hotplug(struct work_struct *work);
172 extern void ata_scsi_dev_rescan(struct work_struct *work);
173 extern int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
174 unsigned int id, u64 lun);
175 void ata_scsi_sdev_config(struct scsi_device *sdev);
176 int ata_scsi_dev_config(struct scsi_device *sdev, struct queue_limits *lim,
177 struct ata_device *dev);
178 enum scsi_qc_status __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
179 struct ata_device *dev,
180 struct ata_port *ap)
181 __must_hold(ap->lock);
182 void ata_scsi_deferred_qc_work(struct work_struct *work);
183 void ata_scsi_requeue_deferred_qc(struct ata_port *ap);
184
185 /* libata-eh.c */
186 extern unsigned int ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd);
187 extern void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd);
188 extern void ata_eh_acquire(struct ata_port *ap)
189 __acquires(&ap->host->eh_mutex);
190 extern void ata_eh_release(struct ata_port *ap)
191 __releases(&ap->host->eh_mutex);
192 extern void ata_scsi_error(struct Scsi_Host *host);
193 extern void ata_eh_fastdrain_timerfn(struct timer_list *t);
194 extern void ata_qc_schedule_eh(struct ata_queued_cmd *qc);
195 extern void ata_dev_disable(struct ata_device *dev);
196 extern void ata_eh_detach_dev(struct ata_device *dev);
197 extern void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
198 unsigned int action);
199 extern void ata_eh_done(struct ata_link *link, struct ata_device *dev,
200 unsigned int action);
201 extern void ata_eh_autopsy(struct ata_port *ap);
202 const char *ata_get_cmd_name(u8 command);
203 extern void ata_eh_report(struct ata_port *ap);
204 extern int ata_eh_reset(struct ata_port *ap, struct ata_link *link,
205 int classify, struct ata_reset_operations *reset_ops)
206 __must_hold(&ap->host->eh_mutex);
207 extern int ata_eh_recover(struct ata_port *ap,
208 struct ata_reset_operations *reset_ops,
209 struct ata_link **r_failed_disk)
210 __must_hold(&ap->host->eh_mutex);
211 extern void ata_eh_finish(struct ata_port *ap);
212 extern int ata_ering_map(struct ata_ering *ering,
213 int (*map_fn)(struct ata_ering_entry *, void *),
214 void *arg);
215 enum scsi_disposition ata_eh_decide_disposition(struct ata_queued_cmd *qc);
216 extern unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key);
217 extern unsigned int atapi_eh_request_sense(struct ata_device *dev,
218 u8 *sense_buf, u8 dfl_sense_key);
219
220 /* libata-pmp.c */
221 #ifdef CONFIG_SATA_PMP
222 extern int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *val);
223 extern int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val);
224 extern int sata_pmp_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
225 unsigned hints);
226 extern int sata_pmp_attach(struct ata_device *dev);
227 #else /* CONFIG_SATA_PMP */
sata_pmp_scr_read(struct ata_link * link,int reg,u32 * val)228 static inline int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *val)
229 {
230 return -EINVAL;
231 }
232
sata_pmp_scr_write(struct ata_link * link,int reg,u32 val)233 static inline int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
234 {
235 return -EINVAL;
236 }
237
sata_pmp_set_lpm(struct ata_link * link,enum ata_lpm_policy policy,unsigned hints)238 static inline int sata_pmp_set_lpm(struct ata_link *link,
239 enum ata_lpm_policy policy, unsigned hints)
240 {
241 return -EINVAL;
242 }
243
sata_pmp_attach(struct ata_device * dev)244 static inline int sata_pmp_attach(struct ata_device *dev)
245 {
246 return -EINVAL;
247 }
248 #endif /* CONFIG_SATA_PMP */
249
250 /* libata-sff.c */
251 #ifdef CONFIG_ATA_SFF
252 extern void ata_sff_flush_pio_task(struct ata_port *ap);
253 extern void ata_sff_port_init(struct ata_port *ap);
254 extern int ata_sff_init(void);
255 extern void ata_sff_exit(void);
256 #else /* CONFIG_ATA_SFF */
ata_sff_flush_pio_task(struct ata_port * ap)257 static inline void ata_sff_flush_pio_task(struct ata_port *ap)
258 { }
ata_sff_port_init(struct ata_port * ap)259 static inline void ata_sff_port_init(struct ata_port *ap)
260 { }
ata_sff_init(void)261 static inline int ata_sff_init(void)
262 { return 0; }
ata_sff_exit(void)263 static inline void ata_sff_exit(void)
264 { }
265 #endif /* CONFIG_ATA_SFF */
266
267 /* libata-zpodd.c */
268 #ifdef CONFIG_SATA_ZPODD
269 void zpodd_init(struct ata_device *dev);
270 void zpodd_exit(struct ata_device *dev);
zpodd_dev_enabled(struct ata_device * dev)271 static inline bool zpodd_dev_enabled(struct ata_device *dev)
272 {
273 return dev->zpodd != NULL;
274 }
275 void zpodd_on_suspend(struct ata_device *dev);
276 bool zpodd_zpready(struct ata_device *dev);
277 void zpodd_enable_run_wake(struct ata_device *dev);
278 void zpodd_disable_run_wake(struct ata_device *dev);
279 void zpodd_post_poweron(struct ata_device *dev);
280 #else /* CONFIG_SATA_ZPODD */
zpodd_init(struct ata_device * dev)281 static inline void zpodd_init(struct ata_device *dev) {}
zpodd_exit(struct ata_device * dev)282 static inline void zpodd_exit(struct ata_device *dev) {}
zpodd_dev_enabled(struct ata_device * dev)283 static inline bool zpodd_dev_enabled(struct ata_device *dev) { return false; }
zpodd_on_suspend(struct ata_device * dev)284 static inline void zpodd_on_suspend(struct ata_device *dev) {}
zpodd_zpready(struct ata_device * dev)285 static inline bool zpodd_zpready(struct ata_device *dev) { return false; }
zpodd_enable_run_wake(struct ata_device * dev)286 static inline void zpodd_enable_run_wake(struct ata_device *dev) {}
zpodd_disable_run_wake(struct ata_device * dev)287 static inline void zpodd_disable_run_wake(struct ata_device *dev) {}
zpodd_post_poweron(struct ata_device * dev)288 static inline void zpodd_post_poweron(struct ata_device *dev) {}
289 #endif /* CONFIG_SATA_ZPODD */
290
291 #endif /* __LIBATA_H__ */
292