blob: 75217828dfe3021c307fd4d141c415c74249e908 [file] [log] [blame]
Thomas Gleixnerc82ee6d2019-05-19 15:51:48 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002/*
Dave Jonesf3a03b02007-07-16 11:23:03 -04003 * libata-sff.c - helper library for PCI IDE BMDMA
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05004 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05005 * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2006 Jeff Garzik
7 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05008 * libata documentation is available via 'make {ps|pdf}docs',
Mauro Carvalho Chehab9bb9a392017-05-16 09:16:37 -03009 * as Documentation/driver-api/libata.rst
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050010 *
11 * Hardware documentation available from http://www.t13.org/ and
12 * http://www.sata-io.org/
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050013 */
14
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050015#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050017#include <linux/pci.h>
Paul Gortmakerbff78322011-07-03 13:41:29 -040018#include <linux/module.h>
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050019#include <linux/libata.h>
Tejun Heo624d5c52008-03-25 22:16:41 +090020#include <linux/highmem.h>
Hannes Reineckec206a382021-12-21 08:20:31 +010021#include <trace/events/libata.h>
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050022#include "libata.h"
23
Tejun Heoc4291372010-05-10 21:41:38 +020024static struct workqueue_struct *ata_sff_wq;
25
Tejun Heo624d5c52008-03-25 22:16:41 +090026const struct ata_port_operations ata_sff_port_ops = {
27 .inherits = &ata_base_port_ops,
28
Tejun Heof47451c2010-05-10 21:41:40 +020029 .qc_prep = ata_noop_qc_prep,
Tejun Heo9363c382008-04-07 22:47:16 +090030 .qc_issue = ata_sff_qc_issue,
Tejun Heo4c9bf4e2008-04-07 22:47:20 +090031 .qc_fill_rtf = ata_sff_qc_fill_rtf,
Tejun Heo624d5c52008-03-25 22:16:41 +090032
Tejun Heo9363c382008-04-07 22:47:16 +090033 .freeze = ata_sff_freeze,
34 .thaw = ata_sff_thaw,
Tejun Heo0aa11132008-04-07 22:47:18 +090035 .prereset = ata_sff_prereset,
Tejun Heo9363c382008-04-07 22:47:16 +090036 .softreset = ata_sff_softreset,
Tejun Heo57c9efd2008-04-07 22:47:19 +090037 .hardreset = sata_sff_hardreset,
Tejun Heo203c75b2008-04-07 22:47:18 +090038 .postreset = ata_sff_postreset,
Tejun Heo9363c382008-04-07 22:47:16 +090039 .error_handler = ata_sff_error_handler,
Tejun Heo624d5c52008-03-25 22:16:41 +090040
Tejun Heo5682ed32008-04-07 22:47:16 +090041 .sff_dev_select = ata_sff_dev_select,
42 .sff_check_status = ata_sff_check_status,
43 .sff_tf_load = ata_sff_tf_load,
44 .sff_tf_read = ata_sff_tf_read,
45 .sff_exec_command = ata_sff_exec_command,
46 .sff_data_xfer = ata_sff_data_xfer,
Tejun Heo8244cd02010-05-10 21:41:36 +020047 .sff_drain_fifo = ata_sff_drain_fifo,
Tejun Heo624d5c52008-03-25 22:16:41 +090048
Alan Coxc96f1732009-03-24 10:23:46 +000049 .lost_interrupt = ata_sff_lost_interrupt,
Tejun Heo624d5c52008-03-25 22:16:41 +090050};
Alan Cox0fe40ff2009-01-05 14:16:13 +000051EXPORT_SYMBOL_GPL(ata_sff_port_ops);
Tejun Heo624d5c52008-03-25 22:16:41 +090052
Tejun Heo624d5c52008-03-25 22:16:41 +090053/**
Tejun Heo9363c382008-04-07 22:47:16 +090054 * ata_sff_check_status - Read device status reg & clear interrupt
Tejun Heo272f7882008-03-25 22:16:40 +090055 * @ap: port where the device is
56 *
57 * Reads ATA taskfile status register for currently-selected device
58 * and return its value. This also clears pending interrupts
59 * from this device
60 *
61 * LOCKING:
62 * Inherited from caller.
63 */
Tejun Heo9363c382008-04-07 22:47:16 +090064u8 ata_sff_check_status(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +090065{
66 return ioread8(ap->ioaddr.status_addr);
67}
Alan Cox0fe40ff2009-01-05 14:16:13 +000068EXPORT_SYMBOL_GPL(ata_sff_check_status);
Tejun Heo272f7882008-03-25 22:16:40 +090069
70/**
Tejun Heo9363c382008-04-07 22:47:16 +090071 * ata_sff_altstatus - Read device alternate status reg
Tejun Heo272f7882008-03-25 22:16:40 +090072 * @ap: port where the device is
73 *
74 * Reads ATA taskfile alternate status register for
75 * currently-selected device and return its value.
76 *
77 * Note: may NOT be used as the check_altstatus() entry in
78 * ata_port_operations.
79 *
80 * LOCKING:
81 * Inherited from caller.
82 */
Alan Coxa57c1ba2008-05-29 22:10:58 +010083static u8 ata_sff_altstatus(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +090084{
Tejun Heo5682ed32008-04-07 22:47:16 +090085 if (ap->ops->sff_check_altstatus)
86 return ap->ops->sff_check_altstatus(ap);
Tejun Heo272f7882008-03-25 22:16:40 +090087
88 return ioread8(ap->ioaddr.altstatus_addr);
89}
90
91/**
Alan Coxa57c1ba2008-05-29 22:10:58 +010092 * ata_sff_irq_status - Check if the device is busy
93 * @ap: port where the device is
94 *
95 * Determine if the port is currently busy. Uses altstatus
96 * if available in order to avoid clearing shared IRQ status
97 * when finding an IRQ source. Non ctl capable devices don't
98 * share interrupt lines fortunately for us.
99 *
100 * LOCKING:
101 * Inherited from caller.
102 */
103static u8 ata_sff_irq_status(struct ata_port *ap)
104{
105 u8 status;
106
107 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
108 status = ata_sff_altstatus(ap);
109 /* Not us: We are busy */
110 if (status & ATA_BUSY)
Alan Cox0fe40ff2009-01-05 14:16:13 +0000111 return status;
Alan Coxa57c1ba2008-05-29 22:10:58 +0100112 }
113 /* Clear INTRQ latch */
Hugh Dickins6311c902008-06-05 14:44:39 +0100114 status = ap->ops->sff_check_status(ap);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100115 return status;
116}
117
118/**
119 * ata_sff_sync - Flush writes
120 * @ap: Port to wait for.
121 *
122 * CAUTION:
123 * If we have an mmio device with no ctl and no altstatus
124 * method this will fail. No such devices are known to exist.
125 *
126 * LOCKING:
127 * Inherited from caller.
128 */
129
130static void ata_sff_sync(struct ata_port *ap)
131{
132 if (ap->ops->sff_check_altstatus)
133 ap->ops->sff_check_altstatus(ap);
134 else if (ap->ioaddr.altstatus_addr)
135 ioread8(ap->ioaddr.altstatus_addr);
136}
137
138/**
139 * ata_sff_pause - Flush writes and wait 400nS
140 * @ap: Port to pause for.
141 *
142 * CAUTION:
143 * If we have an mmio device with no ctl and no altstatus
144 * method this will fail. No such devices are known to exist.
145 *
146 * LOCKING:
147 * Inherited from caller.
148 */
149
150void ata_sff_pause(struct ata_port *ap)
151{
152 ata_sff_sync(ap);
153 ndelay(400);
154}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000155EXPORT_SYMBOL_GPL(ata_sff_pause);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100156
157/**
158 * ata_sff_dma_pause - Pause before commencing DMA
159 * @ap: Port to pause for.
160 *
161 * Perform I/O fencing and ensure sufficient cycle delays occur
162 * for the HDMA1:0 transition
163 */
Alan Cox0fe40ff2009-01-05 14:16:13 +0000164
Alan Coxa57c1ba2008-05-29 22:10:58 +0100165void ata_sff_dma_pause(struct ata_port *ap)
166{
167 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
168 /* An altstatus read will cause the needed delay without
169 messing up the IRQ status */
170 ata_sff_altstatus(ap);
171 return;
172 }
173 /* There are no DMA controllers without ctl. BUG here to ensure
174 we never violate the HDMA1:0 transition timing and risk
175 corruption. */
176 BUG();
177}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000178EXPORT_SYMBOL_GPL(ata_sff_dma_pause);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100179
180/**
Tejun Heo9363c382008-04-07 22:47:16 +0900181 * ata_sff_busy_sleep - sleep until BSY clears, or timeout
Tejun Heo624d5c52008-03-25 22:16:41 +0900182 * @ap: port containing status register to be polled
Tejun Heo341c2c92008-05-20 02:17:51 +0900183 * @tmout_pat: impatience timeout in msecs
184 * @tmout: overall timeout in msecs
Tejun Heo624d5c52008-03-25 22:16:41 +0900185 *
186 * Sleep until ATA Status register bit BSY clears,
187 * or a timeout occurs.
188 *
189 * LOCKING:
190 * Kernel thread context (may sleep).
191 *
192 * RETURNS:
193 * 0 on success, -errno otherwise.
194 */
Tejun Heo9363c382008-04-07 22:47:16 +0900195int ata_sff_busy_sleep(struct ata_port *ap,
196 unsigned long tmout_pat, unsigned long tmout)
Tejun Heo624d5c52008-03-25 22:16:41 +0900197{
198 unsigned long timer_start, timeout;
199 u8 status;
200
Tejun Heo9363c382008-04-07 22:47:16 +0900201 status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
Tejun Heo624d5c52008-03-25 22:16:41 +0900202 timer_start = jiffies;
Tejun Heo341c2c92008-05-20 02:17:51 +0900203 timeout = ata_deadline(timer_start, tmout_pat);
Tejun Heo624d5c52008-03-25 22:16:41 +0900204 while (status != 0xff && (status & ATA_BUSY) &&
205 time_before(jiffies, timeout)) {
Tejun Heo97750ce2010-09-06 17:56:29 +0200206 ata_msleep(ap, 50);
Tejun Heo9363c382008-04-07 22:47:16 +0900207 status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
Tejun Heo624d5c52008-03-25 22:16:41 +0900208 }
209
210 if (status != 0xff && (status & ATA_BUSY))
Joe Perchesa9a79df2011-04-15 15:51:59 -0700211 ata_port_warn(ap,
212 "port is slow to respond, please be patient (Status 0x%x)\n",
213 status);
Tejun Heo624d5c52008-03-25 22:16:41 +0900214
Tejun Heo341c2c92008-05-20 02:17:51 +0900215 timeout = ata_deadline(timer_start, tmout);
Tejun Heo624d5c52008-03-25 22:16:41 +0900216 while (status != 0xff && (status & ATA_BUSY) &&
217 time_before(jiffies, timeout)) {
Tejun Heo97750ce2010-09-06 17:56:29 +0200218 ata_msleep(ap, 50);
Tejun Heo5682ed32008-04-07 22:47:16 +0900219 status = ap->ops->sff_check_status(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900220 }
221
222 if (status == 0xff)
223 return -ENODEV;
224
225 if (status & ATA_BUSY) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700226 ata_port_err(ap,
227 "port failed to respond (%lu secs, Status 0x%x)\n",
228 DIV_ROUND_UP(tmout, 1000), status);
Tejun Heo624d5c52008-03-25 22:16:41 +0900229 return -EBUSY;
230 }
231
232 return 0;
233}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000234EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
Tejun Heo624d5c52008-03-25 22:16:41 +0900235
Tejun Heoaa2731a2008-04-07 22:47:19 +0900236static int ata_sff_check_ready(struct ata_link *link)
237{
238 u8 status = link->ap->ops->sff_check_status(link->ap);
239
Tejun Heo78ab88f2008-05-01 23:41:41 +0900240 return ata_check_ready(status);
Tejun Heoaa2731a2008-04-07 22:47:19 +0900241}
242
Tejun Heo624d5c52008-03-25 22:16:41 +0900243/**
Tejun Heo9363c382008-04-07 22:47:16 +0900244 * ata_sff_wait_ready - sleep until BSY clears, or timeout
Tejun Heo705e76b2008-04-07 22:47:19 +0900245 * @link: SFF link to wait ready status for
Tejun Heo624d5c52008-03-25 22:16:41 +0900246 * @deadline: deadline jiffies for the operation
247 *
248 * Sleep until ATA Status register bit BSY clears, or timeout
249 * occurs.
250 *
251 * LOCKING:
252 * Kernel thread context (may sleep).
253 *
254 * RETURNS:
255 * 0 on success, -errno otherwise.
256 */
Tejun Heo705e76b2008-04-07 22:47:19 +0900257int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +0900258{
Tejun Heoaa2731a2008-04-07 22:47:19 +0900259 return ata_wait_ready(link, deadline, ata_sff_check_ready);
Tejun Heo624d5c52008-03-25 22:16:41 +0900260}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000261EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
Tejun Heo624d5c52008-03-25 22:16:41 +0900262
263/**
Sergei Shtylyov41dec292010-05-07 22:47:50 +0400264 * ata_sff_set_devctl - Write device control reg
265 * @ap: port where the device is
266 * @ctl: value to write
267 *
268 * Writes ATA taskfile device control register.
269 *
270 * Note: may NOT be used as the sff_set_devctl() entry in
271 * ata_port_operations.
272 *
273 * LOCKING:
274 * Inherited from caller.
275 */
276static void ata_sff_set_devctl(struct ata_port *ap, u8 ctl)
277{
278 if (ap->ops->sff_set_devctl)
279 ap->ops->sff_set_devctl(ap, ctl);
280 else
281 iowrite8(ctl, ap->ioaddr.ctl_addr);
282}
283
284/**
Tejun Heo9363c382008-04-07 22:47:16 +0900285 * ata_sff_dev_select - Select device 0/1 on ATA bus
Tejun Heo624d5c52008-03-25 22:16:41 +0900286 * @ap: ATA channel to manipulate
287 * @device: ATA device (numbered from zero) to select
288 *
289 * Use the method defined in the ATA specification to
290 * make either device 0, or device 1, active on the
291 * ATA channel. Works with both PIO and MMIO.
292 *
293 * May be used as the dev_select() entry in ata_port_operations.
294 *
295 * LOCKING:
296 * caller.
297 */
Tejun Heo9363c382008-04-07 22:47:16 +0900298void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
Tejun Heo624d5c52008-03-25 22:16:41 +0900299{
300 u8 tmp;
301
302 if (device == 0)
303 tmp = ATA_DEVICE_OBS;
304 else
305 tmp = ATA_DEVICE_OBS | ATA_DEV1;
306
307 iowrite8(tmp, ap->ioaddr.device_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900308 ata_sff_pause(ap); /* needed; also flushes, for mmio */
Tejun Heo624d5c52008-03-25 22:16:41 +0900309}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000310EXPORT_SYMBOL_GPL(ata_sff_dev_select);
Tejun Heo624d5c52008-03-25 22:16:41 +0900311
312/**
313 * ata_dev_select - Select device 0/1 on ATA bus
314 * @ap: ATA channel to manipulate
315 * @device: ATA device (numbered from zero) to select
316 * @wait: non-zero to wait for Status register BSY bit to clear
317 * @can_sleep: non-zero if context allows sleeping
318 *
319 * Use the method defined in the ATA specification to
320 * make either device 0, or device 1, active on the
321 * ATA channel.
322 *
Tejun Heo9363c382008-04-07 22:47:16 +0900323 * This is a high-level version of ata_sff_dev_select(), which
324 * additionally provides the services of inserting the proper
325 * pauses and status polling, where needed.
Tejun Heo624d5c52008-03-25 22:16:41 +0900326 *
327 * LOCKING:
328 * caller.
329 */
Tejun Heoc7a82092010-05-10 21:41:29 +0200330static void ata_dev_select(struct ata_port *ap, unsigned int device,
Tejun Heo624d5c52008-03-25 22:16:41 +0900331 unsigned int wait, unsigned int can_sleep)
332{
Tejun Heo624d5c52008-03-25 22:16:41 +0900333 if (wait)
334 ata_wait_idle(ap);
335
Tejun Heo5682ed32008-04-07 22:47:16 +0900336 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +0900337
338 if (wait) {
339 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
Tejun Heo97750ce2010-09-06 17:56:29 +0200340 ata_msleep(ap, 150);
Tejun Heo624d5c52008-03-25 22:16:41 +0900341 ata_wait_idle(ap);
342 }
343}
344
345/**
Tejun Heo9363c382008-04-07 22:47:16 +0900346 * ata_sff_irq_on - Enable interrupts on a port.
Tejun Heo90088bb2006-10-09 11:10:26 +0900347 * @ap: Port on which interrupts are enabled.
348 *
349 * Enable interrupts on a legacy IDE device using MMIO or PIO,
350 * wait for idle, clear any pending interrupts.
351 *
Sergei Shtylyove42a5422010-05-07 22:49:02 +0400352 * Note: may NOT be used as the sff_irq_on() entry in
353 * ata_port_operations.
354 *
Tejun Heo90088bb2006-10-09 11:10:26 +0900355 * LOCKING:
356 * Inherited from caller.
357 */
Sergei Shtylyove42a5422010-05-07 22:49:02 +0400358void ata_sff_irq_on(struct ata_port *ap)
Tejun Heo90088bb2006-10-09 11:10:26 +0900359{
360 struct ata_ioports *ioaddr = &ap->ioaddr;
Sergei Shtylyove42a5422010-05-07 22:49:02 +0400361
362 if (ap->ops->sff_irq_on) {
363 ap->ops->sff_irq_on(ap);
364 return;
365 }
Tejun Heo90088bb2006-10-09 11:10:26 +0900366
367 ap->ctl &= ~ATA_NIEN;
368 ap->last_ctl = ap->ctl;
369
Sergei Shtylyove42a5422010-05-07 22:49:02 +0400370 if (ap->ops->sff_set_devctl || ioaddr->ctl_addr)
371 ata_sff_set_devctl(ap, ap->ctl);
372 ata_wait_idle(ap);
Tejun Heo90088bb2006-10-09 11:10:26 +0900373
Tejun Heo37f65b82010-05-19 22:10:20 +0200374 if (ap->ops->sff_irq_clear)
375 ap->ops->sff_irq_clear(ap);
Tejun Heo90088bb2006-10-09 11:10:26 +0900376}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000377EXPORT_SYMBOL_GPL(ata_sff_irq_on);
Tejun Heo90088bb2006-10-09 11:10:26 +0900378
379/**
Tejun Heo9363c382008-04-07 22:47:16 +0900380 * ata_sff_tf_load - send taskfile registers to host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500381 * @ap: Port to which output is sent
382 * @tf: ATA taskfile register set
383 *
384 * Outputs ATA taskfile to standard ATA host controller.
385 *
386 * LOCKING:
387 * Inherited from caller.
388 */
Tejun Heo9363c382008-04-07 22:47:16 +0900389void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500390{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900391 struct ata_ioports *ioaddr = &ap->ioaddr;
392 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
393
394 if (tf->ctl != ap->last_ctl) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900395 if (ioaddr->ctl_addr)
396 iowrite8(tf->ctl, ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900397 ap->last_ctl = tf->ctl;
Tejun Heo40c60232010-09-09 17:13:31 +0200398 ata_wait_idle(ap);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900399 }
400
401 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900402 WARN_ON_ONCE(!ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900403 iowrite8(tf->hob_feature, ioaddr->feature_addr);
404 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
405 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
406 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
407 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900408 }
409
410 if (is_addr) {
411 iowrite8(tf->feature, ioaddr->feature_addr);
412 iowrite8(tf->nsect, ioaddr->nsect_addr);
413 iowrite8(tf->lbal, ioaddr->lbal_addr);
414 iowrite8(tf->lbam, ioaddr->lbam_addr);
415 iowrite8(tf->lbah, ioaddr->lbah_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900416 }
417
Hannes Reineckec206a382021-12-21 08:20:31 +0100418 if (tf->flags & ATA_TFLAG_DEVICE)
Tejun Heo0d5ff562007-02-01 15:06:36 +0900419 iowrite8(tf->device, ioaddr->device_addr);
Tejun Heo40c60232010-09-09 17:13:31 +0200420
421 ata_wait_idle(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500422}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000423EXPORT_SYMBOL_GPL(ata_sff_tf_load);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500424
425/**
Tejun Heo9363c382008-04-07 22:47:16 +0900426 * ata_sff_tf_read - input device's ATA taskfile shadow registers
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500427 * @ap: Port from which input is read
428 * @tf: ATA taskfile register set for storing input
429 *
430 * Reads ATA taskfile registers for currently-selected device
Alan Cox76548ed2007-11-19 14:34:56 +0000431 * into @tf. Assumes the device has a fully SFF compliant task file
432 * layout and behaviour. If you device does not (eg has a different
433 * status method) then you will need to provide a replacement tf_read
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500434 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500435 * LOCKING:
436 * Inherited from caller.
437 */
Tejun Heo9363c382008-04-07 22:47:16 +0900438void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500439{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900440 struct ata_ioports *ioaddr = &ap->ioaddr;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500441
Tejun Heo9363c382008-04-07 22:47:16 +0900442 tf->command = ata_sff_check_status(ap);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900443 tf->feature = ioread8(ioaddr->error_addr);
444 tf->nsect = ioread8(ioaddr->nsect_addr);
445 tf->lbal = ioread8(ioaddr->lbal_addr);
446 tf->lbam = ioread8(ioaddr->lbam_addr);
447 tf->lbah = ioread8(ioaddr->lbah_addr);
448 tf->device = ioread8(ioaddr->device_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500449
Tejun Heo0d5ff562007-02-01 15:06:36 +0900450 if (tf->flags & ATA_TFLAG_LBA48) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900451 if (likely(ioaddr->ctl_addr)) {
452 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
453 tf->hob_feature = ioread8(ioaddr->error_addr);
454 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
455 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
456 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
457 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
458 iowrite8(tf->ctl, ioaddr->ctl_addr);
459 ap->last_ctl = tf->ctl;
460 } else
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900461 WARN_ON_ONCE(1);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900462 }
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500463}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000464EXPORT_SYMBOL_GPL(ata_sff_tf_read);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500465
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500466/**
Tejun Heo9363c382008-04-07 22:47:16 +0900467 * ata_sff_exec_command - issue ATA command to host controller
Tejun Heo272f7882008-03-25 22:16:40 +0900468 * @ap: port to which command is being issued
469 * @tf: ATA taskfile register set
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500470 *
Tejun Heo272f7882008-03-25 22:16:40 +0900471 * Issues ATA command, with proper synchronization with interrupt
472 * handler / other threads.
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500473 *
474 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400475 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500476 */
Tejun Heo9363c382008-04-07 22:47:16 +0900477void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500478{
Tejun Heo272f7882008-03-25 22:16:40 +0900479 iowrite8(tf->command, ap->ioaddr.command_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900480 ata_sff_pause(ap);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500481}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000482EXPORT_SYMBOL_GPL(ata_sff_exec_command);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500483
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900484/**
Tejun Heo624d5c52008-03-25 22:16:41 +0900485 * ata_tf_to_host - issue ATA taskfile to host controller
486 * @ap: port to which command is being issued
487 * @tf: ATA taskfile register set
Hannes Reineckec206a382021-12-21 08:20:31 +0100488 * @tag: tag of the associated command
Tejun Heo624d5c52008-03-25 22:16:41 +0900489 *
490 * Issues ATA taskfile register set to ATA host controller,
491 * with proper synchronization with interrupt handler and
492 * other threads.
493 *
494 * LOCKING:
495 * spin_lock_irqsave(host lock)
496 */
497static inline void ata_tf_to_host(struct ata_port *ap,
Hannes Reineckec206a382021-12-21 08:20:31 +0100498 const struct ata_taskfile *tf,
499 unsigned int tag)
Tejun Heo624d5c52008-03-25 22:16:41 +0900500{
Hannes Reineckec206a382021-12-21 08:20:31 +0100501 trace_ata_tf_load(ap, tf);
Tejun Heo5682ed32008-04-07 22:47:16 +0900502 ap->ops->sff_tf_load(ap, tf);
Hannes Reineckec206a382021-12-21 08:20:31 +0100503 trace_ata_exec_command(ap, tf, tag);
Tejun Heo5682ed32008-04-07 22:47:16 +0900504 ap->ops->sff_exec_command(ap, tf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900505}
506
507/**
Tejun Heo9363c382008-04-07 22:47:16 +0900508 * ata_sff_data_xfer - Transfer data by PIO
Bartlomiej Zolnierkiewicz989e0aa2016-12-30 15:01:17 +0100509 * @qc: queued command
Tejun Heo624d5c52008-03-25 22:16:41 +0900510 * @buf: data buffer
511 * @buflen: buffer length
512 * @rw: read/write
513 *
514 * Transfer data from/to the device data register by PIO.
515 *
516 * LOCKING:
517 * Inherited from caller.
518 *
519 * RETURNS:
520 * Bytes consumed.
521 */
Bartlomiej Zolnierkiewicz989e0aa2016-12-30 15:01:17 +0100522unsigned int ata_sff_data_xfer(struct ata_queued_cmd *qc, unsigned char *buf,
Tejun Heo9363c382008-04-07 22:47:16 +0900523 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900524{
Bartlomiej Zolnierkiewicz989e0aa2016-12-30 15:01:17 +0100525 struct ata_port *ap = qc->dev->link->ap;
Tejun Heo624d5c52008-03-25 22:16:41 +0900526 void __iomem *data_addr = ap->ioaddr.data_addr;
527 unsigned int words = buflen >> 1;
528
529 /* Transfer multiple of 2 bytes */
530 if (rw == READ)
531 ioread16_rep(data_addr, buf, words);
532 else
533 iowrite16_rep(data_addr, buf, words);
534
Sergei Shtylyov2102d742009-02-15 23:30:38 +0400535 /* Transfer trailing byte, if any. */
Tejun Heo624d5c52008-03-25 22:16:41 +0900536 if (unlikely(buflen & 0x01)) {
Tejun Heo21dba242011-09-06 13:09:05 +0900537 unsigned char pad[2] = { };
Tejun Heo624d5c52008-03-25 22:16:41 +0900538
Sergei Shtylyov2102d742009-02-15 23:30:38 +0400539 /* Point buf to the tail of buffer */
540 buf += buflen - 1;
541
542 /*
543 * Use io*16_rep() accessors here as well to avoid pointlessly
Krzysztof Halasa972b94ff2009-11-11 00:55:27 +0100544 * swapping bytes to and from on the big endian machines...
Sergei Shtylyov2102d742009-02-15 23:30:38 +0400545 */
Tejun Heo624d5c52008-03-25 22:16:41 +0900546 if (rw == READ) {
Sergei Shtylyov2102d742009-02-15 23:30:38 +0400547 ioread16_rep(data_addr, pad, 1);
548 *buf = pad[0];
Tejun Heo624d5c52008-03-25 22:16:41 +0900549 } else {
Sergei Shtylyov2102d742009-02-15 23:30:38 +0400550 pad[0] = *buf;
551 iowrite16_rep(data_addr, pad, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +0900552 }
553 words++;
554 }
555
556 return words << 1;
557}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000558EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
Tejun Heo624d5c52008-03-25 22:16:41 +0900559
560/**
Alan Cox871af122009-01-05 14:16:39 +0000561 * ata_sff_data_xfer32 - Transfer data by PIO
Bartlomiej Zolnierkiewicz989e0aa2016-12-30 15:01:17 +0100562 * @qc: queued command
Alan Cox871af122009-01-05 14:16:39 +0000563 * @buf: data buffer
564 * @buflen: buffer length
565 * @rw: read/write
566 *
567 * Transfer data from/to the device data register by PIO using 32bit
568 * I/O operations.
569 *
570 * LOCKING:
571 * Inherited from caller.
572 *
573 * RETURNS:
574 * Bytes consumed.
575 */
576
Bartlomiej Zolnierkiewicz989e0aa2016-12-30 15:01:17 +0100577unsigned int ata_sff_data_xfer32(struct ata_queued_cmd *qc, unsigned char *buf,
Alan Cox871af122009-01-05 14:16:39 +0000578 unsigned int buflen, int rw)
579{
Bartlomiej Zolnierkiewicz989e0aa2016-12-30 15:01:17 +0100580 struct ata_device *dev = qc->dev;
Alan Cox871af122009-01-05 14:16:39 +0000581 struct ata_port *ap = dev->link->ap;
582 void __iomem *data_addr = ap->ioaddr.data_addr;
583 unsigned int words = buflen >> 2;
584 int slop = buflen & 3;
Krzysztof Halasa972b94ff2009-11-11 00:55:27 +0100585
Alan Coxe3cf95d2009-04-09 17:31:17 +0100586 if (!(ap->pflags & ATA_PFLAG_PIO32))
Bartlomiej Zolnierkiewicz989e0aa2016-12-30 15:01:17 +0100587 return ata_sff_data_xfer(qc, buf, buflen, rw);
Alan Cox871af122009-01-05 14:16:39 +0000588
589 /* Transfer multiple of 4 bytes */
590 if (rw == READ)
591 ioread32_rep(data_addr, buf, words);
592 else
593 iowrite32_rep(data_addr, buf, words);
594
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400595 /* Transfer trailing bytes, if any */
Alan Cox871af122009-01-05 14:16:39 +0000596 if (unlikely(slop)) {
Tejun Heo21dba242011-09-06 13:09:05 +0900597 unsigned char pad[4] = { };
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400598
599 /* Point buf to the tail of buffer */
600 buf += buflen - slop;
601
602 /*
603 * Use io*_rep() accessors here as well to avoid pointlessly
Krzysztof Halasa972b94ff2009-11-11 00:55:27 +0100604 * swapping bytes to and from on the big endian machines...
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400605 */
Alan Cox871af122009-01-05 14:16:39 +0000606 if (rw == READ) {
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400607 if (slop < 3)
608 ioread16_rep(data_addr, pad, 1);
609 else
610 ioread32_rep(data_addr, pad, 1);
611 memcpy(buf, pad, slop);
Alan Cox871af122009-01-05 14:16:39 +0000612 } else {
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400613 memcpy(pad, buf, slop);
614 if (slop < 3)
615 iowrite16_rep(data_addr, pad, 1);
616 else
617 iowrite32_rep(data_addr, pad, 1);
Alan Cox871af122009-01-05 14:16:39 +0000618 }
Alan Cox871af122009-01-05 14:16:39 +0000619 }
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400620 return (buflen + 1) & ~1;
Alan Cox871af122009-01-05 14:16:39 +0000621}
622EXPORT_SYMBOL_GPL(ata_sff_data_xfer32);
623
Christoph Hellwigecef6a92021-07-09 15:02:37 +0200624static void ata_pio_xfer(struct ata_queued_cmd *qc, struct page *page,
625 unsigned int offset, size_t xfer_size)
626{
627 bool do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
628 unsigned char *buf;
629
630 buf = kmap_atomic(page);
631 qc->ap->ops->sff_data_xfer(qc, buf + offset, xfer_size, do_write);
632 kunmap_atomic(buf);
633
634 if (!do_write && !PageSlab(page))
635 flush_dcache_page(page);
636}
637
Alan Cox871af122009-01-05 14:16:39 +0000638/**
Tejun Heo624d5c52008-03-25 22:16:41 +0900639 * ata_pio_sector - Transfer a sector of data.
640 * @qc: Command on going
641 *
642 * Transfer qc->sect_size bytes of data from/to the ATA device.
643 *
644 * LOCKING:
645 * Inherited from caller.
646 */
647static void ata_pio_sector(struct ata_queued_cmd *qc)
648{
Tejun Heo624d5c52008-03-25 22:16:41 +0900649 struct ata_port *ap = qc->ap;
650 struct page *page;
651 unsigned int offset;
Tejun Heo624d5c52008-03-25 22:16:41 +0900652
Jens Axboe752ead42019-08-07 12:23:57 -0600653 if (!qc->cursg) {
654 qc->curbytes = qc->nbytes;
655 return;
656 }
Tejun Heo624d5c52008-03-25 22:16:41 +0900657 if (qc->curbytes == qc->nbytes - qc->sect_size)
658 ap->hsm_task_state = HSM_ST_LAST;
659
660 page = sg_page(qc->cursg);
661 offset = qc->cursg->offset + qc->cursg_ofs;
662
663 /* get the current page and offset */
664 page = nth_page(page, (offset >> PAGE_SHIFT));
665 offset %= PAGE_SIZE;
666
Hannes Reinecke7fad6ad2021-12-21 08:20:32 +0100667 trace_ata_sff_pio_transfer_data(qc, offset, qc->sect_size);
Tejun Heo624d5c52008-03-25 22:16:41 +0900668
Christoph Hellwigecef6a92021-07-09 15:02:37 +0200669 /*
670 * Split the transfer when it splits a page boundary. Note that the
671 * split still has to be dword aligned like all ATA data transfers.
672 */
673 WARN_ON_ONCE(offset % 4);
674 if (offset + qc->sect_size > PAGE_SIZE) {
675 unsigned int split_len = PAGE_SIZE - offset;
Tejun Heo624d5c52008-03-25 22:16:41 +0900676
Christoph Hellwigecef6a92021-07-09 15:02:37 +0200677 ata_pio_xfer(qc, page, offset, split_len);
678 ata_pio_xfer(qc, nth_page(page, 1), 0,
679 qc->sect_size - split_len);
680 } else {
681 ata_pio_xfer(qc, page, offset, qc->sect_size);
682 }
Catalin Marinas2d68b7f2010-02-04 01:04:50 -0500683
Tejun Heo624d5c52008-03-25 22:16:41 +0900684 qc->curbytes += qc->sect_size;
685 qc->cursg_ofs += qc->sect_size;
686
687 if (qc->cursg_ofs == qc->cursg->length) {
688 qc->cursg = sg_next(qc->cursg);
Jens Axboe752ead42019-08-07 12:23:57 -0600689 if (!qc->cursg)
690 ap->hsm_task_state = HSM_ST_LAST;
Tejun Heo624d5c52008-03-25 22:16:41 +0900691 qc->cursg_ofs = 0;
692 }
693}
694
695/**
696 * ata_pio_sectors - Transfer one or many sectors.
697 * @qc: Command on going
698 *
699 * Transfer one or many sectors of data from/to the
700 * ATA device for the DRQ request.
701 *
702 * LOCKING:
703 * Inherited from caller.
704 */
705static void ata_pio_sectors(struct ata_queued_cmd *qc)
706{
707 if (is_multi_taskfile(&qc->tf)) {
708 /* READ/WRITE MULTIPLE */
709 unsigned int nsect;
710
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900711 WARN_ON_ONCE(qc->dev->multi_count == 0);
Tejun Heo624d5c52008-03-25 22:16:41 +0900712
713 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
714 qc->dev->multi_count);
715 while (nsect--)
716 ata_pio_sector(qc);
717 } else
718 ata_pio_sector(qc);
719
Alan Coxa57c1ba2008-05-29 22:10:58 +0100720 ata_sff_sync(qc->ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900721}
722
723/**
724 * atapi_send_cdb - Write CDB bytes to hardware
725 * @ap: Port to which ATAPI device is attached.
726 * @qc: Taskfile currently active
727 *
728 * When device has indicated its readiness to accept
729 * a CDB, this function is called. Send the CDB.
730 *
731 * LOCKING:
732 * caller.
733 */
734static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
735{
736 /* send SCSI cdb */
Hannes Reinecke7fad6ad2021-12-21 08:20:32 +0100737 trace_atapi_send_cdb(qc, 0, qc->dev->cdb_len);
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900738 WARN_ON_ONCE(qc->dev->cdb_len < 12);
Tejun Heo624d5c52008-03-25 22:16:41 +0900739
Bartlomiej Zolnierkiewicz989e0aa2016-12-30 15:01:17 +0100740 ap->ops->sff_data_xfer(qc, qc->cdb, qc->dev->cdb_len, 1);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100741 ata_sff_sync(ap);
742 /* FIXME: If the CDB is for DMA do we need to do the transition delay
743 or is bmdma_start guaranteed to do it ? */
Tejun Heo624d5c52008-03-25 22:16:41 +0900744 switch (qc->tf.protocol) {
745 case ATAPI_PROT_PIO:
746 ap->hsm_task_state = HSM_ST;
747 break;
748 case ATAPI_PROT_NODATA:
749 ap->hsm_task_state = HSM_ST_LAST;
750 break;
Tejun Heo9a7780c2010-05-19 22:10:24 +0200751#ifdef CONFIG_ATA_BMDMA
Tejun Heo624d5c52008-03-25 22:16:41 +0900752 case ATAPI_PROT_DMA:
753 ap->hsm_task_state = HSM_ST_LAST;
754 /* initiate bmdma */
Hannes Reineckec206a382021-12-21 08:20:31 +0100755 trace_ata_bmdma_start(ap, &qc->tf, qc->tag);
Tejun Heo624d5c52008-03-25 22:16:41 +0900756 ap->ops->bmdma_start(qc);
757 break;
Tejun Heo9a7780c2010-05-19 22:10:24 +0200758#endif /* CONFIG_ATA_BMDMA */
759 default:
760 BUG();
Tejun Heo624d5c52008-03-25 22:16:41 +0900761 }
762}
763
764/**
765 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
766 * @qc: Command on going
767 * @bytes: number of bytes
768 *
769 * Transfer Transfer data from/to the ATAPI device.
770 *
771 * LOCKING:
772 * Inherited from caller.
773 *
774 */
775static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
776{
777 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
778 struct ata_port *ap = qc->ap;
779 struct ata_device *dev = qc->dev;
780 struct ata_eh_info *ehi = &dev->link->eh_info;
781 struct scatterlist *sg;
782 struct page *page;
783 unsigned char *buf;
784 unsigned int offset, count, consumed;
785
786next_sg:
787 sg = qc->cursg;
788 if (unlikely(!sg)) {
789 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
790 "buf=%u cur=%u bytes=%u",
791 qc->nbytes, qc->curbytes, bytes);
792 return -1;
793 }
794
795 page = sg_page(sg);
796 offset = sg->offset + qc->cursg_ofs;
797
798 /* get the current page and offset */
799 page = nth_page(page, (offset >> PAGE_SHIFT));
800 offset %= PAGE_SIZE;
801
802 /* don't overrun current sg */
803 count = min(sg->length - qc->cursg_ofs, bytes);
804
805 /* don't cross page boundaries */
806 count = min(count, (unsigned int)PAGE_SIZE - offset);
807
Hannes Reinecke7fad6ad2021-12-21 08:20:32 +0100808 trace_atapi_pio_transfer_data(qc, offset, count);
Tejun Heo624d5c52008-03-25 22:16:41 +0900809
Tycho Andersen5d7a2882017-05-04 16:15:51 -0600810 /* do the actual data transfer */
811 buf = kmap_atomic(page);
812 consumed = ap->ops->sff_data_xfer(qc, buf + offset, count, rw);
813 kunmap_atomic(buf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900814
815 bytes -= min(bytes, consumed);
816 qc->curbytes += count;
817 qc->cursg_ofs += count;
818
819 if (qc->cursg_ofs == sg->length) {
820 qc->cursg = sg_next(qc->cursg);
821 qc->cursg_ofs = 0;
822 }
823
Christian Borntraegera0f79f72009-01-13 10:38:36 +0100824 /*
825 * There used to be a WARN_ON_ONCE(qc->cursg && count != consumed);
826 * Unfortunately __atapi_pio_bytes doesn't know enough to do the WARN
827 * check correctly as it doesn't know if it is the last request being
828 * made. Somebody should implement a proper sanity check.
829 */
Tejun Heo624d5c52008-03-25 22:16:41 +0900830 if (bytes)
831 goto next_sg;
832 return 0;
833}
834
835/**
836 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
837 * @qc: Command on going
838 *
839 * Transfer Transfer data from/to the ATAPI device.
840 *
841 * LOCKING:
842 * Inherited from caller.
843 */
844static void atapi_pio_bytes(struct ata_queued_cmd *qc)
845{
846 struct ata_port *ap = qc->ap;
847 struct ata_device *dev = qc->dev;
848 struct ata_eh_info *ehi = &dev->link->eh_info;
849 unsigned int ireason, bc_lo, bc_hi, bytes;
850 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
851
852 /* Abuse qc->result_tf for temp storage of intermediate TF
853 * here to save some kernel stack usage.
854 * For normal completion, qc->result_tf is not relevant. For
855 * error, qc->result_tf is later overwritten by ata_qc_complete().
856 * So, the correctness of qc->result_tf is not affected.
857 */
Tejun Heo5682ed32008-04-07 22:47:16 +0900858 ap->ops->sff_tf_read(ap, &qc->result_tf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900859 ireason = qc->result_tf.nsect;
860 bc_lo = qc->result_tf.lbam;
861 bc_hi = qc->result_tf.lbah;
862 bytes = (bc_hi << 8) | bc_lo;
863
864 /* shall be cleared to zero, indicating xfer of data */
Sergei Shtylyov002ae082011-12-02 18:39:53 +0300865 if (unlikely(ireason & ATAPI_COD))
Tejun Heo624d5c52008-03-25 22:16:41 +0900866 goto atapi_check;
867
868 /* make sure transfer direction matches expected */
Sergei Shtylyov002ae082011-12-02 18:39:53 +0300869 i_write = ((ireason & ATAPI_IO) == 0) ? 1 : 0;
Tejun Heo624d5c52008-03-25 22:16:41 +0900870 if (unlikely(do_write != i_write))
871 goto atapi_check;
872
873 if (unlikely(!bytes))
874 goto atapi_check;
875
Tejun Heo624d5c52008-03-25 22:16:41 +0900876 if (unlikely(__atapi_pio_bytes(qc, bytes)))
877 goto err_out;
Alan Coxa57c1ba2008-05-29 22:10:58 +0100878 ata_sff_sync(ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900879
880 return;
881
882 atapi_check:
883 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
884 ireason, bytes);
885 err_out:
886 qc->err_mask |= AC_ERR_HSM;
887 ap->hsm_task_state = HSM_ST_ERR;
888}
889
890/**
891 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
892 * @ap: the target ata_port
893 * @qc: qc on going
894 *
895 * RETURNS:
896 * 1 if ok in workqueue, 0 otherwise.
897 */
Alan Cox0fe40ff2009-01-05 14:16:13 +0000898static inline int ata_hsm_ok_in_wq(struct ata_port *ap,
899 struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900900{
901 if (qc->tf.flags & ATA_TFLAG_POLLING)
902 return 1;
903
904 if (ap->hsm_task_state == HSM_ST_FIRST) {
905 if (qc->tf.protocol == ATA_PROT_PIO &&
Alan Cox0fe40ff2009-01-05 14:16:13 +0000906 (qc->tf.flags & ATA_TFLAG_WRITE))
Tejun Heo624d5c52008-03-25 22:16:41 +0900907 return 1;
908
909 if (ata_is_atapi(qc->tf.protocol) &&
Alan Cox0fe40ff2009-01-05 14:16:13 +0000910 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Tejun Heo624d5c52008-03-25 22:16:41 +0900911 return 1;
912 }
913
914 return 0;
915}
916
917/**
918 * ata_hsm_qc_complete - finish a qc running on standard HSM
919 * @qc: Command to complete
920 * @in_wq: 1 if called from workqueue, 0 otherwise
921 *
922 * Finish @qc which is running on standard HSM.
923 *
924 * LOCKING:
925 * If @in_wq is zero, spin_lock_irqsave(host lock).
926 * Otherwise, none on entry and grabs host lock.
927 */
928static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
929{
930 struct ata_port *ap = qc->ap;
Tejun Heo624d5c52008-03-25 22:16:41 +0900931
932 if (ap->ops->error_handler) {
933 if (in_wq) {
Tejun Heo624d5c52008-03-25 22:16:41 +0900934 /* EH might have kicked in while host lock is
935 * released.
936 */
937 qc = ata_qc_from_tag(ap, qc->tag);
938 if (qc) {
939 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Sergei Shtylyove42a5422010-05-07 22:49:02 +0400940 ata_sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900941 ata_qc_complete(qc);
942 } else
943 ata_port_freeze(ap);
944 }
Tejun Heo624d5c52008-03-25 22:16:41 +0900945 } else {
946 if (likely(!(qc->err_mask & AC_ERR_HSM)))
947 ata_qc_complete(qc);
948 else
949 ata_port_freeze(ap);
950 }
951 } else {
952 if (in_wq) {
Sergei Shtylyove42a5422010-05-07 22:49:02 +0400953 ata_sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900954 ata_qc_complete(qc);
Tejun Heo624d5c52008-03-25 22:16:41 +0900955 } else
956 ata_qc_complete(qc);
957 }
958}
959
960/**
Tejun Heo9363c382008-04-07 22:47:16 +0900961 * ata_sff_hsm_move - move the HSM to the next state.
Tejun Heo624d5c52008-03-25 22:16:41 +0900962 * @ap: the target ata_port
963 * @qc: qc on going
964 * @status: current device status
965 * @in_wq: 1 if called from workqueue, 0 otherwise
966 *
967 * RETURNS:
968 * 1 when poll next status needed, 0 otherwise.
969 */
Tejun Heo9363c382008-04-07 22:47:16 +0900970int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
971 u8 status, int in_wq)
Tejun Heo624d5c52008-03-25 22:16:41 +0900972{
Gwendal Grignouea3c6452010-08-31 16:20:36 -0700973 struct ata_link *link = qc->dev->link;
974 struct ata_eh_info *ehi = &link->eh_info;
Tejun Heo624d5c52008-03-25 22:16:41 +0900975 int poll_next;
976
Tejun Heo8eee1d32016-02-01 11:33:21 -0500977 lockdep_assert_held(ap->lock);
978
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900979 WARN_ON_ONCE((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Tejun Heo624d5c52008-03-25 22:16:41 +0900980
Tejun Heo9363c382008-04-07 22:47:16 +0900981 /* Make sure ata_sff_qc_issue() does not throw things
Tejun Heo624d5c52008-03-25 22:16:41 +0900982 * like DMA polling into the workqueue. Notice that
983 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
984 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900985 WARN_ON_ONCE(in_wq != ata_hsm_ok_in_wq(ap, qc));
Tejun Heo624d5c52008-03-25 22:16:41 +0900986
987fsm_start:
Hannes Reinecke7fad6ad2021-12-21 08:20:32 +0100988 trace_ata_sff_hsm_state(qc, status);
Tejun Heo624d5c52008-03-25 22:16:41 +0900989
990 switch (ap->hsm_task_state) {
991 case HSM_ST_FIRST:
992 /* Send first data block or PACKET CDB */
993
994 /* If polling, we will stay in the work queue after
995 * sending the data. Otherwise, interrupt handler
996 * takes over after sending the data.
997 */
998 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
999
1000 /* check device status */
1001 if (unlikely((status & ATA_DRQ) == 0)) {
1002 /* handle BSY=0, DRQ=0 as error */
1003 if (likely(status & (ATA_ERR | ATA_DF)))
1004 /* device stops HSM for abort/error */
1005 qc->err_mask |= AC_ERR_DEV;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001006 else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001007 /* HSM violation. Let EH handle this */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001008 ata_ehi_push_desc(ehi,
1009 "ST_FIRST: !(DRQ|ERR|DF)");
Tejun Heo624d5c52008-03-25 22:16:41 +09001010 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001011 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001012
1013 ap->hsm_task_state = HSM_ST_ERR;
1014 goto fsm_start;
1015 }
1016
1017 /* Device should not ask for data transfer (DRQ=1)
1018 * when it finds something wrong.
1019 * We ignore DRQ here and stop the HSM by
1020 * changing hsm_task_state to HSM_ST_ERR and
1021 * let the EH abort the command or reset the device.
1022 */
1023 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1024 /* Some ATAPI tape drives forget to clear the ERR bit
1025 * when doing the next command (mostly request sense).
1026 * We ignore ERR here to workaround and proceed sending
1027 * the CDB.
1028 */
1029 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001030 ata_ehi_push_desc(ehi, "ST_FIRST: "
1031 "DRQ=1 with device error, "
1032 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001033 qc->err_mask |= AC_ERR_HSM;
1034 ap->hsm_task_state = HSM_ST_ERR;
1035 goto fsm_start;
1036 }
1037 }
1038
Tejun Heo624d5c52008-03-25 22:16:41 +09001039 if (qc->tf.protocol == ATA_PROT_PIO) {
1040 /* PIO data out protocol.
1041 * send first data block.
1042 */
1043
1044 /* ata_pio_sectors() might change the state
1045 * to HSM_ST_LAST. so, the state is changed here
1046 * before ata_pio_sectors().
1047 */
1048 ap->hsm_task_state = HSM_ST;
1049 ata_pio_sectors(qc);
1050 } else
1051 /* send CDB */
1052 atapi_send_cdb(ap, qc);
1053
Tejun Heoc4291372010-05-10 21:41:38 +02001054 /* if polling, ata_sff_pio_task() handles the rest.
Tejun Heo624d5c52008-03-25 22:16:41 +09001055 * otherwise, interrupt handler takes over from here.
1056 */
1057 break;
1058
1059 case HSM_ST:
1060 /* complete command or read/write the data register */
1061 if (qc->tf.protocol == ATAPI_PROT_PIO) {
1062 /* ATAPI PIO protocol */
1063 if ((status & ATA_DRQ) == 0) {
1064 /* No more data to transfer or device error.
1065 * Device error will be tagged in HSM_ST_LAST.
1066 */
1067 ap->hsm_task_state = HSM_ST_LAST;
1068 goto fsm_start;
1069 }
1070
1071 /* Device should not ask for data transfer (DRQ=1)
1072 * when it finds something wrong.
1073 * We ignore DRQ here and stop the HSM by
1074 * changing hsm_task_state to HSM_ST_ERR and
1075 * let the EH abort the command or reset the device.
1076 */
1077 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001078 ata_ehi_push_desc(ehi, "ST-ATAPI: "
1079 "DRQ=1 with device error, "
1080 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001081 qc->err_mask |= AC_ERR_HSM;
1082 ap->hsm_task_state = HSM_ST_ERR;
1083 goto fsm_start;
1084 }
1085
1086 atapi_pio_bytes(qc);
1087
1088 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
1089 /* bad ireason reported by device */
1090 goto fsm_start;
1091
1092 } else {
1093 /* ATA PIO protocol */
1094 if (unlikely((status & ATA_DRQ) == 0)) {
1095 /* handle BSY=0, DRQ=0 as error */
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001096 if (likely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo624d5c52008-03-25 22:16:41 +09001097 /* device stops HSM for abort/error */
1098 qc->err_mask |= AC_ERR_DEV;
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001099
1100 /* If diagnostic failed and this is
1101 * IDENTIFY, it's likely a phantom
1102 * device. Mark hint.
1103 */
1104 if (qc->dev->horkage &
1105 ATA_HORKAGE_DIAGNOSTIC)
1106 qc->err_mask |=
1107 AC_ERR_NODEV_HINT;
1108 } else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001109 /* HSM violation. Let EH handle this.
1110 * Phantom devices also trigger this
1111 * condition. Mark hint.
1112 */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001113 ata_ehi_push_desc(ehi, "ST-ATA: "
Tejun Heo80ee6f52009-01-23 14:12:59 +09001114 "DRQ=0 without device error, "
Tejun Heoa836d3e2008-06-28 01:39:43 +09001115 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001116 qc->err_mask |= AC_ERR_HSM |
1117 AC_ERR_NODEV_HINT;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001118 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001119
1120 ap->hsm_task_state = HSM_ST_ERR;
1121 goto fsm_start;
1122 }
1123
1124 /* For PIO reads, some devices may ask for
1125 * data transfer (DRQ=1) alone with ERR=1.
1126 * We respect DRQ here and transfer one
1127 * block of junk data before changing the
1128 * hsm_task_state to HSM_ST_ERR.
1129 *
1130 * For PIO writes, ERR=1 DRQ=1 doesn't make
1131 * sense since the data block has been
1132 * transferred to the device.
1133 */
1134 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1135 /* data might be corrputed */
1136 qc->err_mask |= AC_ERR_DEV;
1137
1138 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
1139 ata_pio_sectors(qc);
1140 status = ata_wait_idle(ap);
1141 }
1142
Tejun Heoa836d3e2008-06-28 01:39:43 +09001143 if (status & (ATA_BUSY | ATA_DRQ)) {
1144 ata_ehi_push_desc(ehi, "ST-ATA: "
1145 "BUSY|DRQ persists on ERR|DF, "
1146 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001147 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001148 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001149
Tejun Heob9199302009-01-25 10:26:00 +09001150 /* There are oddball controllers with
1151 * status register stuck at 0x7f and
1152 * lbal/m/h at zero which makes it
1153 * pass all other presence detection
1154 * mechanisms we have. Set NODEV_HINT
1155 * for it. Kernel bz#7241.
1156 */
1157 if (status == 0x7f)
1158 qc->err_mask |= AC_ERR_NODEV_HINT;
1159
Tejun Heo624d5c52008-03-25 22:16:41 +09001160 /* ata_pio_sectors() might change the
1161 * state to HSM_ST_LAST. so, the state
1162 * is changed after ata_pio_sectors().
1163 */
1164 ap->hsm_task_state = HSM_ST_ERR;
1165 goto fsm_start;
1166 }
1167
1168 ata_pio_sectors(qc);
1169
1170 if (ap->hsm_task_state == HSM_ST_LAST &&
1171 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
1172 /* all data read */
1173 status = ata_wait_idle(ap);
1174 goto fsm_start;
1175 }
1176 }
1177
1178 poll_next = 1;
1179 break;
1180
1181 case HSM_ST_LAST:
1182 if (unlikely(!ata_ok(status))) {
1183 qc->err_mask |= __ac_err_mask(status);
1184 ap->hsm_task_state = HSM_ST_ERR;
1185 goto fsm_start;
1186 }
1187
1188 /* no more data to transfer */
Hannes Reinecke7fad6ad2021-12-21 08:20:32 +01001189 trace_ata_sff_hsm_command_complete(qc, status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001190
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001191 WARN_ON_ONCE(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
Tejun Heo624d5c52008-03-25 22:16:41 +09001192
1193 ap->hsm_task_state = HSM_ST_IDLE;
1194
1195 /* complete taskfile transaction */
1196 ata_hsm_qc_complete(qc, in_wq);
1197
1198 poll_next = 0;
1199 break;
1200
1201 case HSM_ST_ERR:
Tejun Heo624d5c52008-03-25 22:16:41 +09001202 ap->hsm_task_state = HSM_ST_IDLE;
1203
1204 /* complete taskfile transaction */
1205 ata_hsm_qc_complete(qc, in_wq);
1206
1207 poll_next = 0;
1208 break;
1209 default:
1210 poll_next = 0;
Tejun Heoa588afc2016-01-29 07:06:53 -05001211 WARN(true, "ata%d: SFF host state machine in invalid state %d",
1212 ap->print_id, ap->hsm_task_state);
Tejun Heo624d5c52008-03-25 22:16:41 +09001213 }
1214
1215 return poll_next;
1216}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001217EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
Tejun Heo624d5c52008-03-25 22:16:41 +09001218
Viresh Kumar64b97592011-02-22 14:32:38 +05301219void ata_sff_queue_work(struct work_struct *work)
1220{
1221 queue_work(ata_sff_wq, work);
1222}
1223EXPORT_SYMBOL_GPL(ata_sff_queue_work);
1224
1225void ata_sff_queue_delayed_work(struct delayed_work *dwork, unsigned long delay)
1226{
1227 queue_delayed_work(ata_sff_wq, dwork, delay);
1228}
1229EXPORT_SYMBOL_GPL(ata_sff_queue_delayed_work);
1230
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001231void ata_sff_queue_pio_task(struct ata_link *link, unsigned long delay)
Tejun Heoc4291372010-05-10 21:41:38 +02001232{
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001233 struct ata_port *ap = link->ap;
1234
1235 WARN_ON((ap->sff_pio_task_link != NULL) &&
1236 (ap->sff_pio_task_link != link));
1237 ap->sff_pio_task_link = link;
1238
Tejun Heoc4291372010-05-10 21:41:38 +02001239 /* may fail if ata_sff_flush_pio_task() in progress */
Viresh Kumar64b97592011-02-22 14:32:38 +05301240 ata_sff_queue_delayed_work(&ap->sff_pio_task, msecs_to_jiffies(delay));
Tejun Heoc4291372010-05-10 21:41:38 +02001241}
1242EXPORT_SYMBOL_GPL(ata_sff_queue_pio_task);
1243
1244void ata_sff_flush_pio_task(struct ata_port *ap)
1245{
Hannes Reinecke7fad6ad2021-12-21 08:20:32 +01001246 trace_ata_sff_flush_pio_task(ap);
Tejun Heoc4291372010-05-10 21:41:38 +02001247
Tejun Heoafe2c512010-12-14 16:21:17 +01001248 cancel_delayed_work_sync(&ap->sff_pio_task);
David Jefferyce751452015-01-19 13:03:25 -06001249
1250 /*
1251 * We wanna reset the HSM state to IDLE. If we do so without
1252 * grabbing the port lock, critical sections protected by it which
1253 * expect the HSM state to stay stable may get surprised. For
1254 * example, we may set IDLE in between the time
1255 * __ata_sff_port_intr() checks for HSM_ST_IDLE and before it calls
1256 * ata_sff_hsm_move() causing ata_sff_hsm_move() to BUG().
1257 */
1258 spin_lock_irq(ap->lock);
Tejun Heoc4291372010-05-10 21:41:38 +02001259 ap->hsm_task_state = HSM_ST_IDLE;
David Jefferyce751452015-01-19 13:03:25 -06001260 spin_unlock_irq(ap->lock);
1261
Gwendal Grignoud4d8eaf2011-07-22 13:48:32 -07001262 ap->sff_pio_task_link = NULL;
Tejun Heoc4291372010-05-10 21:41:38 +02001263}
1264
1265static void ata_sff_pio_task(struct work_struct *work)
Tejun Heo624d5c52008-03-25 22:16:41 +09001266{
1267 struct ata_port *ap =
Tejun Heoc4291372010-05-10 21:41:38 +02001268 container_of(work, struct ata_port, sff_pio_task.work);
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001269 struct ata_link *link = ap->sff_pio_task_link;
Tejun Heoc4291372010-05-10 21:41:38 +02001270 struct ata_queued_cmd *qc;
Tejun Heo624d5c52008-03-25 22:16:41 +09001271 u8 status;
1272 int poll_next;
1273
Tejun Heo8eee1d32016-02-01 11:33:21 -05001274 spin_lock_irq(ap->lock);
1275
Jeff Garzik4fca3772011-02-15 01:13:24 -05001276 BUG_ON(ap->sff_pio_task_link == NULL);
Tejun Heoc4291372010-05-10 21:41:38 +02001277 /* qc can be NULL if timeout occurred */
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001278 qc = ata_qc_from_tag(ap, link->active_tag);
1279 if (!qc) {
1280 ap->sff_pio_task_link = NULL;
Tejun Heo8eee1d32016-02-01 11:33:21 -05001281 goto out_unlock;
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001282 }
Tejun Heoc4291372010-05-10 21:41:38 +02001283
Tejun Heo624d5c52008-03-25 22:16:41 +09001284fsm_start:
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001285 WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE);
Tejun Heo624d5c52008-03-25 22:16:41 +09001286
1287 /*
1288 * This is purely heuristic. This is a fast path.
1289 * Sometimes when we enter, BSY will be cleared in
1290 * a chk-status or two. If not, the drive is probably seeking
1291 * or something. Snooze for a couple msecs, then
1292 * chk-status again. If still busy, queue delayed work.
1293 */
Tejun Heo9363c382008-04-07 22:47:16 +09001294 status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
Tejun Heo624d5c52008-03-25 22:16:41 +09001295 if (status & ATA_BUSY) {
Tejun Heo8eee1d32016-02-01 11:33:21 -05001296 spin_unlock_irq(ap->lock);
Tejun Heo97750ce2010-09-06 17:56:29 +02001297 ata_msleep(ap, 2);
Tejun Heo8eee1d32016-02-01 11:33:21 -05001298 spin_lock_irq(ap->lock);
1299
Tejun Heo9363c382008-04-07 22:47:16 +09001300 status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
Tejun Heo624d5c52008-03-25 22:16:41 +09001301 if (status & ATA_BUSY) {
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001302 ata_sff_queue_pio_task(link, ATA_SHORT_PAUSE);
Tejun Heo8eee1d32016-02-01 11:33:21 -05001303 goto out_unlock;
Tejun Heo624d5c52008-03-25 22:16:41 +09001304 }
1305 }
1306
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001307 /*
1308 * hsm_move() may trigger another command to be processed.
1309 * clean the link beforehand.
1310 */
1311 ap->sff_pio_task_link = NULL;
Tejun Heo624d5c52008-03-25 22:16:41 +09001312 /* move the HSM */
Tejun Heo9363c382008-04-07 22:47:16 +09001313 poll_next = ata_sff_hsm_move(ap, qc, status, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001314
1315 /* another command or interrupt handler
1316 * may be running at this point.
1317 */
1318 if (poll_next)
1319 goto fsm_start;
Tejun Heo8eee1d32016-02-01 11:33:21 -05001320out_unlock:
1321 spin_unlock_irq(ap->lock);
Tejun Heo624d5c52008-03-25 22:16:41 +09001322}
1323
1324/**
Tejun Heo360ff782010-05-10 21:41:42 +02001325 * ata_sff_qc_issue - issue taskfile to a SFF controller
Tejun Heo624d5c52008-03-25 22:16:41 +09001326 * @qc: command to issue to device
1327 *
Tejun Heo360ff782010-05-10 21:41:42 +02001328 * This function issues a PIO or NODATA command to a SFF
1329 * controller.
Tejun Heo624d5c52008-03-25 22:16:41 +09001330 *
1331 * LOCKING:
1332 * spin_lock_irqsave(host lock)
1333 *
1334 * RETURNS:
1335 * Zero on success, AC_ERR_* mask on failure
1336 */
Tejun Heo9363c382008-04-07 22:47:16 +09001337unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001338{
1339 struct ata_port *ap = qc->ap;
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001340 struct ata_link *link = qc->dev->link;
Tejun Heo624d5c52008-03-25 22:16:41 +09001341
1342 /* Use polling pio if the LLD doesn't handle
1343 * interrupt driven pio and atapi CDB interrupt.
1344 */
Tejun Heo360ff782010-05-10 21:41:42 +02001345 if (ap->flags & ATA_FLAG_PIO_POLLING)
1346 qc->tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo624d5c52008-03-25 22:16:41 +09001347
1348 /* select the device */
1349 ata_dev_select(ap, qc->dev->devno, 1, 0);
1350
1351 /* start the command */
1352 switch (qc->tf.protocol) {
1353 case ATA_PROT_NODATA:
1354 if (qc->tf.flags & ATA_TFLAG_POLLING)
1355 ata_qc_set_polling(qc);
1356
Hannes Reineckec206a382021-12-21 08:20:31 +01001357 ata_tf_to_host(ap, &qc->tf, qc->tag);
Tejun Heo624d5c52008-03-25 22:16:41 +09001358 ap->hsm_task_state = HSM_ST_LAST;
1359
1360 if (qc->tf.flags & ATA_TFLAG_POLLING)
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001361 ata_sff_queue_pio_task(link, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001362
1363 break;
1364
Tejun Heo624d5c52008-03-25 22:16:41 +09001365 case ATA_PROT_PIO:
1366 if (qc->tf.flags & ATA_TFLAG_POLLING)
1367 ata_qc_set_polling(qc);
1368
Hannes Reineckec206a382021-12-21 08:20:31 +01001369 ata_tf_to_host(ap, &qc->tf, qc->tag);
Tejun Heo624d5c52008-03-25 22:16:41 +09001370
1371 if (qc->tf.flags & ATA_TFLAG_WRITE) {
1372 /* PIO data out protocol */
1373 ap->hsm_task_state = HSM_ST_FIRST;
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001374 ata_sff_queue_pio_task(link, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001375
Tejun Heoc4291372010-05-10 21:41:38 +02001376 /* always send first data block using the
1377 * ata_sff_pio_task() codepath.
Tejun Heo624d5c52008-03-25 22:16:41 +09001378 */
1379 } else {
1380 /* PIO data in protocol */
1381 ap->hsm_task_state = HSM_ST;
1382
1383 if (qc->tf.flags & ATA_TFLAG_POLLING)
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001384 ata_sff_queue_pio_task(link, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001385
Tejun Heoc4291372010-05-10 21:41:38 +02001386 /* if polling, ata_sff_pio_task() handles the
1387 * rest. otherwise, interrupt handler takes
1388 * over from here.
Tejun Heo624d5c52008-03-25 22:16:41 +09001389 */
1390 }
1391
1392 break;
1393
1394 case ATAPI_PROT_PIO:
1395 case ATAPI_PROT_NODATA:
1396 if (qc->tf.flags & ATA_TFLAG_POLLING)
1397 ata_qc_set_polling(qc);
1398
Hannes Reineckec206a382021-12-21 08:20:31 +01001399 ata_tf_to_host(ap, &qc->tf, qc->tag);
Tejun Heo624d5c52008-03-25 22:16:41 +09001400
1401 ap->hsm_task_state = HSM_ST_FIRST;
1402
1403 /* send cdb by polling if no cdb interrupt */
1404 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
1405 (qc->tf.flags & ATA_TFLAG_POLLING))
Gwendal Grignouea3c6452010-08-31 16:20:36 -07001406 ata_sff_queue_pio_task(link, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001407 break;
1408
Tejun Heo624d5c52008-03-25 22:16:41 +09001409 default:
Tejun Heo624d5c52008-03-25 22:16:41 +09001410 return AC_ERR_SYSTEM;
1411 }
1412
1413 return 0;
1414}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001415EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
Tejun Heo624d5c52008-03-25 22:16:41 +09001416
1417/**
Tejun Heo22183bf2008-04-07 22:47:20 +09001418 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1419 * @qc: qc to fill result TF for
1420 *
1421 * @qc is finished and result TF needs to be filled. Fill it
1422 * using ->sff_tf_read.
1423 *
1424 * LOCKING:
1425 * spin_lock_irqsave(host lock)
1426 *
1427 * RETURNS:
1428 * true indicating that result TF is successfully filled.
1429 */
1430bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
1431{
1432 qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
1433 return true;
1434}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001435EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
Tejun Heo22183bf2008-04-07 22:47:20 +09001436
Tejun Heoc3b28892010-05-19 22:10:21 +02001437static unsigned int ata_sff_idle_irq(struct ata_port *ap)
Tejun Heo624d5c52008-03-25 22:16:41 +09001438{
Tejun Heoc3b28892010-05-19 22:10:21 +02001439 ap->stats.idle_irq++;
1440
1441#ifdef ATA_IRQ_TRAP
1442 if ((ap->stats.idle_irq % 1000) == 0) {
1443 ap->ops->sff_check_status(ap);
1444 if (ap->ops->sff_irq_clear)
1445 ap->ops->sff_irq_clear(ap);
Joe Perchesa9a79df2011-04-15 15:51:59 -07001446 ata_port_warn(ap, "irq trap\n");
Tejun Heoc3b28892010-05-19 22:10:21 +02001447 return 1;
1448 }
1449#endif
1450 return 0; /* irq not handled */
1451}
1452
1453static unsigned int __ata_sff_port_intr(struct ata_port *ap,
1454 struct ata_queued_cmd *qc,
1455 bool hsmv_on_idle)
1456{
1457 u8 status;
Tejun Heo624d5c52008-03-25 22:16:41 +09001458
Hannes Reinecke7fad6ad2021-12-21 08:20:32 +01001459 trace_ata_sff_port_intr(qc, hsmv_on_idle);
Tejun Heo624d5c52008-03-25 22:16:41 +09001460
1461 /* Check whether we are expecting interrupt in this state */
1462 switch (ap->hsm_task_state) {
1463 case HSM_ST_FIRST:
1464 /* Some pre-ATAPI-4 devices assert INTRQ
1465 * at this state when ready to receive CDB.
1466 */
1467
1468 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1469 * The flag was turned on only for atapi devices. No
1470 * need to check ata_is_atapi(qc->tf.protocol) again.
1471 */
1472 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Tejun Heoc3b28892010-05-19 22:10:21 +02001473 return ata_sff_idle_irq(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001474 break;
Tejun Heo687a9932010-12-03 15:19:13 +01001475 case HSM_ST_IDLE:
Tejun Heoc3b28892010-05-19 22:10:21 +02001476 return ata_sff_idle_irq(ap);
Tejun Heo687a9932010-12-03 15:19:13 +01001477 default:
1478 break;
Tejun Heo624d5c52008-03-25 22:16:41 +09001479 }
1480
Alan Coxa57c1ba2008-05-29 22:10:58 +01001481 /* check main status, clearing INTRQ if needed */
1482 status = ata_sff_irq_status(ap);
Tejun Heo332ac7f2010-03-23 12:24:08 +09001483 if (status & ATA_BUSY) {
Tejun Heoc3b28892010-05-19 22:10:21 +02001484 if (hsmv_on_idle) {
Tejun Heo332ac7f2010-03-23 12:24:08 +09001485 /* BMDMA engine is already stopped, we're screwed */
1486 qc->err_mask |= AC_ERR_HSM;
1487 ap->hsm_task_state = HSM_ST_ERR;
1488 } else
Tejun Heoc3b28892010-05-19 22:10:21 +02001489 return ata_sff_idle_irq(ap);
Tejun Heo332ac7f2010-03-23 12:24:08 +09001490 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001491
Tejun Heo9f2f7212010-05-10 21:41:32 +02001492 /* clear irq events */
Tejun Heo37f65b82010-05-19 22:10:20 +02001493 if (ap->ops->sff_irq_clear)
1494 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001495
Tejun Heo9363c382008-04-07 22:47:16 +09001496 ata_sff_hsm_move(ap, qc, status, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001497
Tejun Heo624d5c52008-03-25 22:16:41 +09001498 return 1; /* irq handled */
Tejun Heo624d5c52008-03-25 22:16:41 +09001499}
1500
1501/**
Tejun Heoc3b28892010-05-19 22:10:21 +02001502 * ata_sff_port_intr - Handle SFF port interrupt
1503 * @ap: Port on which interrupt arrived (possibly...)
1504 * @qc: Taskfile currently active in engine
Tejun Heo624d5c52008-03-25 22:16:41 +09001505 *
Tejun Heoc3b28892010-05-19 22:10:21 +02001506 * Handle port interrupt for given queued command.
Tejun Heo624d5c52008-03-25 22:16:41 +09001507 *
1508 * LOCKING:
Tejun Heoc3b28892010-05-19 22:10:21 +02001509 * spin_lock_irqsave(host lock)
Tejun Heo624d5c52008-03-25 22:16:41 +09001510 *
1511 * RETURNS:
Tejun Heoc3b28892010-05-19 22:10:21 +02001512 * One if interrupt was handled, zero if not (shared irq).
Tejun Heo624d5c52008-03-25 22:16:41 +09001513 */
Tejun Heoc3b28892010-05-19 22:10:21 +02001514unsigned int ata_sff_port_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
1515{
1516 return __ata_sff_port_intr(ap, qc, false);
1517}
1518EXPORT_SYMBOL_GPL(ata_sff_port_intr);
1519
1520static inline irqreturn_t __ata_sff_interrupt(int irq, void *dev_instance,
1521 unsigned int (*port_intr)(struct ata_port *, struct ata_queued_cmd *))
Tejun Heo624d5c52008-03-25 22:16:41 +09001522{
1523 struct ata_host *host = dev_instance;
Tejun Heo332ac7f2010-03-23 12:24:08 +09001524 bool retried = false;
Tejun Heo624d5c52008-03-25 22:16:41 +09001525 unsigned int i;
Tejun Heo332ac7f2010-03-23 12:24:08 +09001526 unsigned int handled, idle, polling;
Tejun Heo624d5c52008-03-25 22:16:41 +09001527 unsigned long flags;
1528
1529 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1530 spin_lock_irqsave(&host->lock, flags);
1531
Tejun Heo332ac7f2010-03-23 12:24:08 +09001532retry:
1533 handled = idle = polling = 0;
Tejun Heo624d5c52008-03-25 22:16:41 +09001534 for (i = 0; i < host->n_ports; i++) {
Tejun Heod88ec2e2010-01-19 10:46:32 +09001535 struct ata_port *ap = host->ports[i];
1536 struct ata_queued_cmd *qc;
Tejun Heo624d5c52008-03-25 22:16:41 +09001537
Tejun Heod88ec2e2010-01-19 10:46:32 +09001538 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo27943622010-01-19 10:49:19 +09001539 if (qc) {
1540 if (!(qc->tf.flags & ATA_TFLAG_POLLING))
Tejun Heoc3b28892010-05-19 22:10:21 +02001541 handled |= port_intr(ap, qc);
Tejun Heo27943622010-01-19 10:49:19 +09001542 else
1543 polling |= 1 << i;
Tejun Heo332ac7f2010-03-23 12:24:08 +09001544 } else
1545 idle |= 1 << i;
Tejun Heo27943622010-01-19 10:49:19 +09001546 }
1547
1548 /*
1549 * If no port was expecting IRQ but the controller is actually
1550 * asserting IRQ line, nobody cared will ensue. Check IRQ
1551 * pending status if available and clear spurious IRQ.
1552 */
Tejun Heo332ac7f2010-03-23 12:24:08 +09001553 if (!handled && !retried) {
1554 bool retry = false;
1555
Tejun Heo27943622010-01-19 10:49:19 +09001556 for (i = 0; i < host->n_ports; i++) {
1557 struct ata_port *ap = host->ports[i];
1558
1559 if (polling & (1 << i))
1560 continue;
1561
1562 if (!ap->ops->sff_irq_check ||
1563 !ap->ops->sff_irq_check(ap))
1564 continue;
1565
Tejun Heo332ac7f2010-03-23 12:24:08 +09001566 if (idle & (1 << i)) {
1567 ap->ops->sff_check_status(ap);
Tejun Heo37f65b82010-05-19 22:10:20 +02001568 if (ap->ops->sff_irq_clear)
1569 ap->ops->sff_irq_clear(ap);
Tejun Heo332ac7f2010-03-23 12:24:08 +09001570 } else {
1571 /* clear INTRQ and check if BUSY cleared */
1572 if (!(ap->ops->sff_check_status(ap) & ATA_BUSY))
1573 retry |= true;
1574 /*
1575 * With command in flight, we can't do
1576 * sff_irq_clear() w/o racing with completion.
1577 */
1578 }
1579 }
1580
1581 if (retry) {
1582 retried = true;
1583 goto retry;
Tejun Heo27943622010-01-19 10:49:19 +09001584 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001585 }
1586
1587 spin_unlock_irqrestore(&host->lock, flags);
1588
1589 return IRQ_RETVAL(handled);
1590}
Tejun Heoc3b28892010-05-19 22:10:21 +02001591
1592/**
1593 * ata_sff_interrupt - Default SFF ATA host interrupt handler
1594 * @irq: irq line (unused)
1595 * @dev_instance: pointer to our ata_host information structure
1596 *
1597 * Default interrupt handler for PCI IDE devices. Calls
1598 * ata_sff_port_intr() for each port that is not disabled.
1599 *
1600 * LOCKING:
1601 * Obtains host lock during operation.
1602 *
1603 * RETURNS:
1604 * IRQ_NONE or IRQ_HANDLED.
1605 */
1606irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
1607{
1608 return __ata_sff_interrupt(irq, dev_instance, ata_sff_port_intr);
1609}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001610EXPORT_SYMBOL_GPL(ata_sff_interrupt);
Tejun Heo624d5c52008-03-25 22:16:41 +09001611
1612/**
Alan Coxc96f1732009-03-24 10:23:46 +00001613 * ata_sff_lost_interrupt - Check for an apparent lost interrupt
1614 * @ap: port that appears to have timed out
1615 *
1616 * Called from the libata error handlers when the core code suspects
1617 * an interrupt has been lost. If it has complete anything we can and
1618 * then return. Interface must support altstatus for this faster
1619 * recovery to occur.
1620 *
1621 * Locking:
1622 * Caller holds host lock
1623 */
1624
1625void ata_sff_lost_interrupt(struct ata_port *ap)
1626{
1627 u8 status;
1628 struct ata_queued_cmd *qc;
1629
1630 /* Only one outstanding command per SFF channel */
1631 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo3e4ec342010-05-10 21:41:30 +02001632 /* We cannot lose an interrupt on a non-existent or polled command */
1633 if (!qc || qc->tf.flags & ATA_TFLAG_POLLING)
Alan Coxc96f1732009-03-24 10:23:46 +00001634 return;
1635 /* See if the controller thinks it is still busy - if so the command
1636 isn't a lost IRQ but is still in progress */
1637 status = ata_sff_altstatus(ap);
1638 if (status & ATA_BUSY)
1639 return;
1640
1641 /* There was a command running, we are no longer busy and we have
1642 no interrupt. */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001643 ata_port_warn(ap, "lost interrupt (Status 0x%x)\n",
Alan Coxc96f1732009-03-24 10:23:46 +00001644 status);
1645 /* Run the host interrupt logic as if the interrupt had not been
1646 lost */
Tejun Heoc3b28892010-05-19 22:10:21 +02001647 ata_sff_port_intr(ap, qc);
Alan Coxc96f1732009-03-24 10:23:46 +00001648}
1649EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt);
1650
1651/**
Tejun Heo9363c382008-04-07 22:47:16 +09001652 * ata_sff_freeze - Freeze SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001653 * @ap: port to freeze
1654 *
Tejun Heo9f2f7212010-05-10 21:41:32 +02001655 * Freeze SFF controller port.
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001656 *
1657 * LOCKING:
1658 * Inherited from caller.
1659 */
Tejun Heo9363c382008-04-07 22:47:16 +09001660void ata_sff_freeze(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001661{
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001662 ap->ctl |= ATA_NIEN;
1663 ap->last_ctl = ap->ctl;
1664
Sergei Shtylyov41dec292010-05-07 22:47:50 +04001665 if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr)
1666 ata_sff_set_devctl(ap, ap->ctl);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001667
1668 /* Under certain circumstances, some controllers raise IRQ on
1669 * ATA_NIEN manipulation. Also, many controllers fail to mask
1670 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1671 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001672 ap->ops->sff_check_status(ap);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001673
Tejun Heo37f65b82010-05-19 22:10:20 +02001674 if (ap->ops->sff_irq_clear)
1675 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001676}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001677EXPORT_SYMBOL_GPL(ata_sff_freeze);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001678
1679/**
Tejun Heo9363c382008-04-07 22:47:16 +09001680 * ata_sff_thaw - Thaw SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001681 * @ap: port to thaw
1682 *
Tejun Heo9363c382008-04-07 22:47:16 +09001683 * Thaw SFF controller port.
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001684 *
1685 * LOCKING:
1686 * Inherited from caller.
1687 */
Tejun Heo9363c382008-04-07 22:47:16 +09001688void ata_sff_thaw(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001689{
1690 /* clear & re-enable interrupts */
Tejun Heo5682ed32008-04-07 22:47:16 +09001691 ap->ops->sff_check_status(ap);
Tejun Heo37f65b82010-05-19 22:10:20 +02001692 if (ap->ops->sff_irq_clear)
1693 ap->ops->sff_irq_clear(ap);
Sergei Shtylyove42a5422010-05-07 22:49:02 +04001694 ata_sff_irq_on(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001695}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001696EXPORT_SYMBOL_GPL(ata_sff_thaw);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001697
1698/**
Tejun Heo0aa11132008-04-07 22:47:18 +09001699 * ata_sff_prereset - prepare SFF link for reset
1700 * @link: SFF link to be reset
1701 * @deadline: deadline jiffies for the operation
1702 *
1703 * SFF link @link is about to be reset. Initialize it. It first
1704 * calls ata_std_prereset() and wait for !BSY if the port is
1705 * being softreset.
1706 *
1707 * LOCKING:
1708 * Kernel thread context (may sleep)
1709 *
1710 * RETURNS:
1711 * 0 on success, -errno otherwise.
1712 */
1713int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
1714{
Tejun Heo0aa11132008-04-07 22:47:18 +09001715 struct ata_eh_context *ehc = &link->eh_context;
1716 int rc;
1717
1718 rc = ata_std_prereset(link, deadline);
1719 if (rc)
1720 return rc;
1721
1722 /* if we're about to do hardreset, nothing more to do */
1723 if (ehc->i.action & ATA_EH_HARDRESET)
1724 return 0;
1725
1726 /* wait for !BSY if we don't know that no device is attached */
1727 if (!ata_link_offline(link)) {
Tejun Heo705e76b2008-04-07 22:47:19 +09001728 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo0aa11132008-04-07 22:47:18 +09001729 if (rc && rc != -ENODEV) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001730 ata_link_warn(link,
1731 "device not ready (errno=%d), forcing hardreset\n",
1732 rc);
Tejun Heo0aa11132008-04-07 22:47:18 +09001733 ehc->i.action |= ATA_EH_HARDRESET;
1734 }
1735 }
1736
1737 return 0;
1738}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001739EXPORT_SYMBOL_GPL(ata_sff_prereset);
Tejun Heo0aa11132008-04-07 22:47:18 +09001740
1741/**
Tejun Heo624d5c52008-03-25 22:16:41 +09001742 * ata_devchk - PATA device presence detection
1743 * @ap: ATA channel to examine
1744 * @device: Device to examine (starting at zero)
1745 *
1746 * This technique was originally described in
1747 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1748 * later found its way into the ATA/ATAPI spec.
1749 *
1750 * Write a pattern to the ATA shadow registers,
1751 * and if a device is present, it will respond by
1752 * correctly storing and echoing back the
1753 * ATA shadow register contents.
1754 *
1755 * LOCKING:
1756 * caller.
1757 */
1758static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1759{
1760 struct ata_ioports *ioaddr = &ap->ioaddr;
1761 u8 nsect, lbal;
1762
Tejun Heo5682ed32008-04-07 22:47:16 +09001763 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +09001764
1765 iowrite8(0x55, ioaddr->nsect_addr);
1766 iowrite8(0xaa, ioaddr->lbal_addr);
1767
1768 iowrite8(0xaa, ioaddr->nsect_addr);
1769 iowrite8(0x55, ioaddr->lbal_addr);
1770
1771 iowrite8(0x55, ioaddr->nsect_addr);
1772 iowrite8(0xaa, ioaddr->lbal_addr);
1773
1774 nsect = ioread8(ioaddr->nsect_addr);
1775 lbal = ioread8(ioaddr->lbal_addr);
1776
1777 if ((nsect == 0x55) && (lbal == 0xaa))
1778 return 1; /* we found a device */
1779
1780 return 0; /* nothing found */
1781}
1782
1783/**
Tejun Heo9363c382008-04-07 22:47:16 +09001784 * ata_sff_dev_classify - Parse returned ATA device signature
Tejun Heo624d5c52008-03-25 22:16:41 +09001785 * @dev: ATA device to classify (starting at zero)
1786 * @present: device seems present
1787 * @r_err: Value of error register on completion
1788 *
1789 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1790 * an ATA/ATAPI-defined set of values is placed in the ATA
1791 * shadow registers, indicating the results of device detection
1792 * and diagnostics.
1793 *
1794 * Select the ATA device, and read the values from the ATA shadow
1795 * registers. Then parse according to the Error register value,
1796 * and the spec-defined values examined by ata_dev_classify().
1797 *
1798 * LOCKING:
1799 * caller.
1800 *
1801 * RETURNS:
1802 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1803 */
Tejun Heo9363c382008-04-07 22:47:16 +09001804unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
Tejun Heo624d5c52008-03-25 22:16:41 +09001805 u8 *r_err)
1806{
1807 struct ata_port *ap = dev->link->ap;
1808 struct ata_taskfile tf;
1809 unsigned int class;
1810 u8 err;
1811
Tejun Heo5682ed32008-04-07 22:47:16 +09001812 ap->ops->sff_dev_select(ap, dev->devno);
Tejun Heo624d5c52008-03-25 22:16:41 +09001813
1814 memset(&tf, 0, sizeof(tf));
1815
Tejun Heo5682ed32008-04-07 22:47:16 +09001816 ap->ops->sff_tf_read(ap, &tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001817 err = tf.feature;
1818 if (r_err)
1819 *r_err = err;
1820
1821 /* see if device passed diags: continue and warn later */
1822 if (err == 0)
1823 /* diagnostic fail : do nothing _YET_ */
1824 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1825 else if (err == 1)
1826 /* do nothing */ ;
1827 else if ((dev->devno == 0) && (err == 0x81))
1828 /* do nothing */ ;
1829 else
1830 return ATA_DEV_NONE;
1831
1832 /* determine if device is ATA or ATAPI */
Hannes Reinecke6c952a02021-12-21 08:20:26 +01001833 class = ata_port_classify(ap, &tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001834
1835 if (class == ATA_DEV_UNKNOWN) {
1836 /* If the device failed diagnostic, it's likely to
1837 * have reported incorrect device signature too.
1838 * Assume ATA device if the device seems present but
1839 * device signature is invalid with diagnostic
1840 * failure.
1841 */
1842 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1843 class = ATA_DEV_ATA;
1844 else
1845 class = ATA_DEV_NONE;
Tejun Heo5682ed32008-04-07 22:47:16 +09001846 } else if ((class == ATA_DEV_ATA) &&
1847 (ap->ops->sff_check_status(ap) == 0))
Tejun Heo624d5c52008-03-25 22:16:41 +09001848 class = ATA_DEV_NONE;
1849
1850 return class;
1851}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001852EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
Tejun Heo624d5c52008-03-25 22:16:41 +09001853
Tejun Heo705e76b2008-04-07 22:47:19 +09001854/**
1855 * ata_sff_wait_after_reset - wait for devices to become ready after reset
1856 * @link: SFF link which is just reset
1857 * @devmask: mask of present devices
1858 * @deadline: deadline jiffies for the operation
1859 *
1860 * Wait devices attached to SFF @link to become ready after
1861 * reset. It contains preceding 150ms wait to avoid accessing TF
1862 * status register too early.
1863 *
1864 * LOCKING:
1865 * Kernel thread context (may sleep).
1866 *
1867 * RETURNS:
1868 * 0 on success, -ENODEV if some or all of devices in @devmask
1869 * don't seem to exist. -errno on other errors.
1870 */
1871int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
1872 unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +09001873{
Tejun Heo705e76b2008-04-07 22:47:19 +09001874 struct ata_port *ap = link->ap;
Tejun Heo624d5c52008-03-25 22:16:41 +09001875 struct ata_ioports *ioaddr = &ap->ioaddr;
1876 unsigned int dev0 = devmask & (1 << 0);
1877 unsigned int dev1 = devmask & (1 << 1);
1878 int rc, ret = 0;
1879
Tejun Heo97750ce2010-09-06 17:56:29 +02001880 ata_msleep(ap, ATA_WAIT_AFTER_RESET);
Tejun Heo705e76b2008-04-07 22:47:19 +09001881
1882 /* always check readiness of the master device */
1883 rc = ata_sff_wait_ready(link, deadline);
1884 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
1885 * and TF status is 0xff, bail out on it too.
Tejun Heo624d5c52008-03-25 22:16:41 +09001886 */
Tejun Heo705e76b2008-04-07 22:47:19 +09001887 if (rc)
1888 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09001889
1890 /* if device 1 was found in ata_devchk, wait for register
1891 * access briefly, then wait for BSY to clear.
1892 */
1893 if (dev1) {
1894 int i;
1895
Tejun Heo5682ed32008-04-07 22:47:16 +09001896 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001897
1898 /* Wait for register access. Some ATAPI devices fail
1899 * to set nsect/lbal after reset, so don't waste too
1900 * much time on it. We're gonna wait for !BSY anyway.
1901 */
1902 for (i = 0; i < 2; i++) {
1903 u8 nsect, lbal;
1904
1905 nsect = ioread8(ioaddr->nsect_addr);
1906 lbal = ioread8(ioaddr->lbal_addr);
1907 if ((nsect == 1) && (lbal == 1))
1908 break;
Tejun Heo97750ce2010-09-06 17:56:29 +02001909 ata_msleep(ap, 50); /* give drive a breather */
Tejun Heo624d5c52008-03-25 22:16:41 +09001910 }
1911
Tejun Heo705e76b2008-04-07 22:47:19 +09001912 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09001913 if (rc) {
1914 if (rc != -ENODEV)
1915 return rc;
1916 ret = rc;
1917 }
1918 }
1919
1920 /* is all this really necessary? */
Tejun Heo5682ed32008-04-07 22:47:16 +09001921 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001922 if (dev1)
Tejun Heo5682ed32008-04-07 22:47:16 +09001923 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001924 if (dev0)
Tejun Heo5682ed32008-04-07 22:47:16 +09001925 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001926
1927 return ret;
1928}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001929EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
Tejun Heo624d5c52008-03-25 22:16:41 +09001930
Tejun Heo624d5c52008-03-25 22:16:41 +09001931static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
1932 unsigned long deadline)
1933{
1934 struct ata_ioports *ioaddr = &ap->ioaddr;
1935
Ondrej Zary6d8ca282014-09-27 00:04:46 +02001936 if (ap->ioaddr.ctl_addr) {
1937 /* software reset. causes dev0 to be selected */
1938 iowrite8(ap->ctl, ioaddr->ctl_addr);
1939 udelay(20); /* FIXME: flush */
1940 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1941 udelay(20); /* FIXME: flush */
1942 iowrite8(ap->ctl, ioaddr->ctl_addr);
1943 ap->last_ctl = ap->ctl;
1944 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001945
Tejun Heo705e76b2008-04-07 22:47:19 +09001946 /* wait the port to become ready */
1947 return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09001948}
1949
1950/**
Tejun Heo9363c382008-04-07 22:47:16 +09001951 * ata_sff_softreset - reset host port via ATA SRST
Tejun Heo624d5c52008-03-25 22:16:41 +09001952 * @link: ATA link to reset
1953 * @classes: resulting classes of attached devices
1954 * @deadline: deadline jiffies for the operation
1955 *
1956 * Reset host port using ATA SRST.
1957 *
1958 * LOCKING:
1959 * Kernel thread context (may sleep)
1960 *
1961 * RETURNS:
1962 * 0 on success, -errno otherwise.
1963 */
Tejun Heo9363c382008-04-07 22:47:16 +09001964int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heo624d5c52008-03-25 22:16:41 +09001965 unsigned long deadline)
1966{
1967 struct ata_port *ap = link->ap;
1968 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1969 unsigned int devmask = 0;
1970 int rc;
1971 u8 err;
1972
Tejun Heo624d5c52008-03-25 22:16:41 +09001973 /* determine if device 0/1 are present */
1974 if (ata_devchk(ap, 0))
1975 devmask |= (1 << 0);
1976 if (slave_possible && ata_devchk(ap, 1))
1977 devmask |= (1 << 1);
1978
1979 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09001980 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001981
1982 /* issue bus reset */
Tejun Heo624d5c52008-03-25 22:16:41 +09001983 rc = ata_bus_softreset(ap, devmask, deadline);
1984 /* if link is occupied, -ENODEV too is an error */
1985 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001986 ata_link_err(link, "SRST failed (errno=%d)\n", rc);
Tejun Heo624d5c52008-03-25 22:16:41 +09001987 return rc;
1988 }
1989
1990 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo9363c382008-04-07 22:47:16 +09001991 classes[0] = ata_sff_dev_classify(&link->device[0],
Tejun Heo624d5c52008-03-25 22:16:41 +09001992 devmask & (1 << 0), &err);
1993 if (slave_possible && err != 0x81)
Tejun Heo9363c382008-04-07 22:47:16 +09001994 classes[1] = ata_sff_dev_classify(&link->device[1],
Tejun Heo624d5c52008-03-25 22:16:41 +09001995 devmask & (1 << 1), &err);
1996
Tejun Heo624d5c52008-03-25 22:16:41 +09001997 return 0;
1998}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001999EXPORT_SYMBOL_GPL(ata_sff_softreset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002000
2001/**
Tejun Heo9363c382008-04-07 22:47:16 +09002002 * sata_sff_hardreset - reset host port via SATA phy reset
Tejun Heo624d5c52008-03-25 22:16:41 +09002003 * @link: link to reset
2004 * @class: resulting class of attached device
2005 * @deadline: deadline jiffies for the operation
2006 *
2007 * SATA phy-reset host port using DET bits of SControl register,
2008 * wait for !BSY and classify the attached device.
2009 *
2010 * LOCKING:
2011 * Kernel thread context (may sleep)
2012 *
2013 * RETURNS:
2014 * 0 on success, -errno otherwise.
2015 */
Tejun Heo9363c382008-04-07 22:47:16 +09002016int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heo624d5c52008-03-25 22:16:41 +09002017 unsigned long deadline)
2018{
Tejun Heo9dadd452008-04-07 22:47:19 +09002019 struct ata_eh_context *ehc = &link->eh_context;
2020 const unsigned long *timing = sata_ehc_deb_timing(ehc);
2021 bool online;
Tejun Heo624d5c52008-03-25 22:16:41 +09002022 int rc;
2023
Tejun Heo9dadd452008-04-07 22:47:19 +09002024 rc = sata_link_hardreset(link, timing, deadline, &online,
2025 ata_sff_check_ready);
Tejun Heo9dadd452008-04-07 22:47:19 +09002026 if (online)
2027 *class = ata_sff_dev_classify(link->device, 1, NULL);
Tejun Heo624d5c52008-03-25 22:16:41 +09002028
Tejun Heo9dadd452008-04-07 22:47:19 +09002029 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09002030}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002031EXPORT_SYMBOL_GPL(sata_sff_hardreset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002032
2033/**
Tejun Heo203c75b2008-04-07 22:47:18 +09002034 * ata_sff_postreset - SFF postreset callback
2035 * @link: the target SFF ata_link
2036 * @classes: classes of attached devices
2037 *
2038 * This function is invoked after a successful reset. It first
2039 * calls ata_std_postreset() and performs SFF specific postreset
2040 * processing.
2041 *
2042 * LOCKING:
2043 * Kernel thread context (may sleep)
2044 */
2045void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
2046{
2047 struct ata_port *ap = link->ap;
2048
2049 ata_std_postreset(link, classes);
2050
2051 /* is double-select really necessary? */
2052 if (classes[0] != ATA_DEV_NONE)
2053 ap->ops->sff_dev_select(ap, 1);
2054 if (classes[1] != ATA_DEV_NONE)
2055 ap->ops->sff_dev_select(ap, 0);
2056
2057 /* bail out if no device is present */
Hannes Reineckef8ec26d2021-12-21 08:20:29 +01002058 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE)
Tejun Heo203c75b2008-04-07 22:47:18 +09002059 return;
Tejun Heo203c75b2008-04-07 22:47:18 +09002060
2061 /* set up device control */
Sergei Shtylyov41dec292010-05-07 22:47:50 +04002062 if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr) {
2063 ata_sff_set_devctl(ap, ap->ctl);
Stuart MENEFYe3e43852009-03-10 11:38:13 +00002064 ap->last_ctl = ap->ctl;
2065 }
Tejun Heo203c75b2008-04-07 22:47:18 +09002066}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002067EXPORT_SYMBOL_GPL(ata_sff_postreset);
Tejun Heo203c75b2008-04-07 22:47:18 +09002068
2069/**
Alan Cox3d47aa82009-03-24 10:23:19 +00002070 * ata_sff_drain_fifo - Stock FIFO drain logic for SFF controllers
2071 * @qc: command
2072 *
2073 * Drain the FIFO and device of any stuck data following a command
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08002074 * failing to complete. In some cases this is necessary before a
Alan Cox3d47aa82009-03-24 10:23:19 +00002075 * reset will recover the device.
2076 *
2077 */
2078
2079void ata_sff_drain_fifo(struct ata_queued_cmd *qc)
2080{
2081 int count;
2082 struct ata_port *ap;
2083
2084 /* We only need to flush incoming data when a command was running */
2085 if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
2086 return;
2087
2088 ap = qc->ap;
2089 /* Drain up to 64K of data before we give up this recovery method */
2090 for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ)
Robert Hancock9a8fd682009-12-08 20:48:10 -06002091 && count < 65536; count += 2)
Alan Cox3d47aa82009-03-24 10:23:19 +00002092 ioread16(ap->ioaddr.data_addr);
2093
Alan Cox3d47aa82009-03-24 10:23:19 +00002094 if (count)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002095 ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
Alan Cox3d47aa82009-03-24 10:23:19 +00002096
2097}
2098EXPORT_SYMBOL_GPL(ata_sff_drain_fifo);
2099
2100/**
Tejun Heofe06e5f2010-05-10 21:41:39 +02002101 * ata_sff_error_handler - Stock error handler for SFF controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002102 * @ap: port to handle error for
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002103 *
Tejun Heo9363c382008-04-07 22:47:16 +09002104 * Stock error handler for SFF controller. It can handle both
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002105 * PATA and SATA controllers. Many controllers should be able to
2106 * use this EH as-is or with some added handling before and
2107 * after.
2108 *
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002109 * LOCKING:
2110 * Kernel thread context (may sleep)
2111 */
Tejun Heo9363c382008-04-07 22:47:16 +09002112void ata_sff_error_handler(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002113{
Tejun Heoa1efdab2008-03-25 12:22:50 +09002114 ata_reset_fn_t softreset = ap->ops->softreset;
2115 ata_reset_fn_t hardreset = ap->ops->hardreset;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002116 struct ata_queued_cmd *qc;
2117 unsigned long flags;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002118
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002119 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002120 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2121 qc = NULL;
2122
Jeff Garzikba6a1302006-06-22 23:46:10 -04002123 spin_lock_irqsave(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002124
Tejun Heofe06e5f2010-05-10 21:41:39 +02002125 /*
2126 * We *MUST* do FIFO draining before we issue a reset as
2127 * several devices helpfully clear their internal state and
2128 * will lock solid if we touch the data port post reset. Pass
2129 * qc in case anyone wants to do different PIO/DMA recovery or
2130 * has per command fixups
Alan Cox3d47aa82009-03-24 10:23:19 +00002131 */
Tejun Heo8244cd02010-05-10 21:41:36 +02002132 if (ap->ops->sff_drain_fifo)
2133 ap->ops->sff_drain_fifo(qc);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002134
Jeff Garzikba6a1302006-06-22 23:46:10 -04002135 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002136
Tejun Heofe06e5f2010-05-10 21:41:39 +02002137 /* ignore built-in hardresets if SCR access is not available */
2138 if ((hardreset == sata_std_hardreset ||
2139 hardreset == sata_sff_hardreset) && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09002140 hardreset = NULL;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002141
Tejun Heoa1efdab2008-03-25 12:22:50 +09002142 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
2143 ap->ops->postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002144}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002145EXPORT_SYMBOL_GPL(ata_sff_error_handler);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002146
2147/**
Tejun Heo9363c382008-04-07 22:47:16 +09002148 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
Tejun Heo624d5c52008-03-25 22:16:41 +09002149 * @ioaddr: IO address structure to be initialized
2150 *
2151 * Utility function which initializes data_addr, error_addr,
2152 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2153 * device_addr, status_addr, and command_addr to standard offsets
2154 * relative to cmd_addr.
2155 *
2156 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2157 */
Tejun Heo9363c382008-04-07 22:47:16 +09002158void ata_sff_std_ports(struct ata_ioports *ioaddr)
Tejun Heo624d5c52008-03-25 22:16:41 +09002159{
2160 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
2161 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
2162 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
2163 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
2164 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
2165 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
2166 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
2167 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
2168 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
2169 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
2170}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002171EXPORT_SYMBOL_GPL(ata_sff_std_ports);
Tejun Heo624d5c52008-03-25 22:16:41 +09002172
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002173#ifdef CONFIG_PCI
Alan4112e162007-01-08 12:10:05 +00002174
Tejun Heo272f7882008-03-25 22:16:40 +09002175static int ata_resources_present(struct pci_dev *pdev, int port)
2176{
2177 int i;
2178
2179 /* Check the PCI resources for this channel are enabled */
2180 port = port * 2;
Alan Cox0fe40ff2009-01-05 14:16:13 +00002181 for (i = 0; i < 2; i++) {
Tejun Heo272f7882008-03-25 22:16:40 +09002182 if (pci_resource_start(pdev, port + i) == 0 ||
2183 pci_resource_len(pdev, port + i) == 0)
2184 return 0;
2185 }
2186 return 1;
2187}
2188
Tejun Heod491b272007-04-17 23:44:07 +09002189/**
Tejun Heo9363c382008-04-07 22:47:16 +09002190 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
Tejun Heod491b272007-04-17 23:44:07 +09002191 * @host: target ATA host
Tejun Heod491b272007-04-17 23:44:07 +09002192 *
Tejun Heo1626aeb2007-05-04 12:43:58 +02002193 * Acquire native PCI ATA resources for @host and initialize the
2194 * first two ports of @host accordingly. Ports marked dummy are
2195 * skipped and allocation failure makes the port dummy.
Tejun Heod491b272007-04-17 23:44:07 +09002196 *
Tejun Heod583bc12007-07-04 18:02:07 +09002197 * Note that native PCI resources are valid even for legacy hosts
2198 * as we fix up pdev resources array early in boot, so this
2199 * function can be used for both native and legacy SFF hosts.
2200 *
Tejun Heod491b272007-04-17 23:44:07 +09002201 * LOCKING:
2202 * Inherited from calling layer (may sleep).
2203 *
2204 * RETURNS:
Tejun Heo1626aeb2007-05-04 12:43:58 +02002205 * 0 if at least one port is initialized, -ENODEV if no port is
2206 * available.
Tejun Heod491b272007-04-17 23:44:07 +09002207 */
Tejun Heo9363c382008-04-07 22:47:16 +09002208int ata_pci_sff_init_host(struct ata_host *host)
Tejun Heod491b272007-04-17 23:44:07 +09002209{
2210 struct device *gdev = host->dev;
2211 struct pci_dev *pdev = to_pci_dev(gdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002212 unsigned int mask = 0;
Tejun Heod491b272007-04-17 23:44:07 +09002213 int i, rc;
2214
Tejun Heod491b272007-04-17 23:44:07 +09002215 /* request, iomap BARs and init port addresses accordingly */
2216 for (i = 0; i < 2; i++) {
2217 struct ata_port *ap = host->ports[i];
2218 int base = i * 2;
2219 void __iomem * const *iomap;
2220
Tejun Heo1626aeb2007-05-04 12:43:58 +02002221 if (ata_port_is_dummy(ap))
Tejun Heod491b272007-04-17 23:44:07 +09002222 continue;
2223
Tejun Heo1626aeb2007-05-04 12:43:58 +02002224 /* Discard disabled ports. Some controllers show
2225 * their unused channels this way. Disabled ports are
2226 * made dummy.
2227 */
2228 if (!ata_resources_present(pdev, i)) {
2229 ap->ops = &ata_dummy_port_ops;
2230 continue;
2231 }
2232
Tejun Heo35a10a82008-01-04 18:42:21 +09002233 rc = pcim_iomap_regions(pdev, 0x3 << base,
2234 dev_driver_string(gdev));
Tejun Heod491b272007-04-17 23:44:07 +09002235 if (rc) {
Joe Perchesa44fec12011-04-15 15:51:58 -07002236 dev_warn(gdev,
2237 "failed to request/iomap BARs for port %d (errno=%d)\n",
2238 i, rc);
Tejun Heod491b272007-04-17 23:44:07 +09002239 if (rc == -EBUSY)
2240 pcim_pin_device(pdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002241 ap->ops = &ata_dummy_port_ops;
2242 continue;
Tejun Heod491b272007-04-17 23:44:07 +09002243 }
2244 host->iomap = iomap = pcim_iomap_table(pdev);
2245
2246 ap->ioaddr.cmd_addr = iomap[base];
2247 ap->ioaddr.altstatus_addr =
2248 ap->ioaddr.ctl_addr = (void __iomem *)
2249 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
Tejun Heo9363c382008-04-07 22:47:16 +09002250 ata_sff_std_ports(&ap->ioaddr);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002251
Tejun Heocbcdd872007-08-18 13:14:55 +09002252 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
2253 (unsigned long long)pci_resource_start(pdev, base),
2254 (unsigned long long)pci_resource_start(pdev, base + 1));
2255
Tejun Heo1626aeb2007-05-04 12:43:58 +02002256 mask |= 1 << i;
2257 }
2258
2259 if (!mask) {
Joe Perchesa44fec12011-04-15 15:51:58 -07002260 dev_err(gdev, "no available native port\n");
Tejun Heo1626aeb2007-05-04 12:43:58 +02002261 return -ENODEV;
Tejun Heod491b272007-04-17 23:44:07 +09002262 }
2263
2264 return 0;
2265}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002266EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
Tejun Heod491b272007-04-17 23:44:07 +09002267
Tejun Heo21b0ad42007-04-17 23:44:07 +09002268/**
Tejun Heo1c5afdf2010-05-19 22:10:22 +02002269 * ata_pci_sff_prepare_host - helper to prepare PCI PIO-only SFF ATA host
Tejun Heo21b0ad42007-04-17 23:44:07 +09002270 * @pdev: target PCI device
Tejun Heo1626aeb2007-05-04 12:43:58 +02002271 * @ppi: array of port_info, must be enough for two ports
Tejun Heo21b0ad42007-04-17 23:44:07 +09002272 * @r_host: out argument for the initialized ATA host
2273 *
Tejun Heo1c5afdf2010-05-19 22:10:22 +02002274 * Helper to allocate PIO-only SFF ATA host for @pdev, acquire
2275 * all PCI resources and initialize it accordingly in one go.
Tejun Heo21b0ad42007-04-17 23:44:07 +09002276 *
2277 * LOCKING:
2278 * Inherited from calling layer (may sleep).
2279 *
2280 * RETURNS:
2281 * 0 on success, -errno otherwise.
2282 */
Tejun Heo9363c382008-04-07 22:47:16 +09002283int ata_pci_sff_prepare_host(struct pci_dev *pdev,
Alan Cox0fe40ff2009-01-05 14:16:13 +00002284 const struct ata_port_info * const *ppi,
Tejun Heod583bc12007-07-04 18:02:07 +09002285 struct ata_host **r_host)
Tejun Heo21b0ad42007-04-17 23:44:07 +09002286{
2287 struct ata_host *host;
Tejun Heo21b0ad42007-04-17 23:44:07 +09002288 int rc;
2289
2290 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
2291 return -ENOMEM;
2292
2293 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
2294 if (!host) {
Joe Perchesa44fec12011-04-15 15:51:58 -07002295 dev_err(&pdev->dev, "failed to allocate ATA host\n");
Tejun Heo21b0ad42007-04-17 23:44:07 +09002296 rc = -ENOMEM;
2297 goto err_out;
2298 }
2299
Tejun Heo9363c382008-04-07 22:47:16 +09002300 rc = ata_pci_sff_init_host(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002301 if (rc)
2302 goto err_out;
2303
Tejun Heo21b0ad42007-04-17 23:44:07 +09002304 devres_remove_group(&pdev->dev, NULL);
2305 *r_host = host;
2306 return 0;
2307
Alan Cox0fe40ff2009-01-05 14:16:13 +00002308err_out:
Tejun Heo21b0ad42007-04-17 23:44:07 +09002309 devres_release_group(&pdev->dev, NULL);
2310 return rc;
2311}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002312EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002313
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002314/**
Tejun Heo9363c382008-04-07 22:47:16 +09002315 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002316 * @host: target SFF ATA host
2317 * @irq_handler: irq_handler used when requesting IRQ(s)
2318 * @sht: scsi_host_template to use when registering the host
2319 *
2320 * This is the counterpart of ata_host_activate() for SFF ATA
2321 * hosts. This separate helper is necessary because SFF hosts
2322 * use two separate interrupts in legacy mode.
2323 *
2324 * LOCKING:
2325 * Inherited from calling layer (may sleep).
2326 *
2327 * RETURNS:
2328 * 0 on success, -errno otherwise.
2329 */
Tejun Heo9363c382008-04-07 22:47:16 +09002330int ata_pci_sff_activate_host(struct ata_host *host,
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002331 irq_handler_t irq_handler,
2332 struct scsi_host_template *sht)
2333{
2334 struct device *dev = host->dev;
2335 struct pci_dev *pdev = to_pci_dev(dev);
2336 const char *drv_name = dev_driver_string(host->dev);
2337 int legacy_mode = 0, rc;
2338
2339 rc = ata_host_start(host);
2340 if (rc)
2341 return rc;
2342
2343 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Darren Stevens589d5722017-01-23 14:33:36 -05002344 u8 tmp8, mask = 0;
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002345
Darren Stevens589d5722017-01-23 14:33:36 -05002346 /*
2347 * ATA spec says we should use legacy mode when one
2348 * port is in legacy mode, but disabled ports on some
2349 * PCI hosts appear as fixed legacy ports, e.g SB600/700
2350 * on which the secondary port is not wired, so
2351 * ignore ports that are marked as 'dummy' during
2352 * this check
2353 */
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002354 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
Darren Stevens589d5722017-01-23 14:33:36 -05002355 if (!ata_port_is_dummy(host->ports[0]))
2356 mask |= (1 << 0);
2357 if (!ata_port_is_dummy(host->ports[1]))
2358 mask |= (1 << 2);
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002359 if ((tmp8 & mask) != mask)
2360 legacy_mode = 1;
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002361 }
2362
2363 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2364 return -ENOMEM;
2365
2366 if (!legacy_mode && pdev->irq) {
James Bottomleyaf649a12011-04-24 14:31:33 -05002367 int i;
2368
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002369 rc = devm_request_irq(dev, pdev->irq, irq_handler,
2370 IRQF_SHARED, drv_name, host);
2371 if (rc)
2372 goto out;
2373
James Bottomleyaf649a12011-04-24 14:31:33 -05002374 for (i = 0; i < 2; i++) {
2375 if (ata_port_is_dummy(host->ports[i]))
2376 continue;
2377 ata_port_desc(host->ports[i], "irq %d", pdev->irq);
2378 }
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002379 } else if (legacy_mode) {
2380 if (!ata_port_is_dummy(host->ports[0])) {
2381 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
2382 irq_handler, IRQF_SHARED,
2383 drv_name, host);
2384 if (rc)
2385 goto out;
2386
2387 ata_port_desc(host->ports[0], "irq %d",
2388 ATA_PRIMARY_IRQ(pdev));
2389 }
2390
2391 if (!ata_port_is_dummy(host->ports[1])) {
2392 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
2393 irq_handler, IRQF_SHARED,
2394 drv_name, host);
2395 if (rc)
2396 goto out;
2397
2398 ata_port_desc(host->ports[1], "irq %d",
2399 ATA_SECONDARY_IRQ(pdev));
2400 }
2401 }
2402
2403 rc = ata_host_register(host, sht);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002404out:
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002405 if (rc == 0)
2406 devres_remove_group(dev, NULL);
2407 else
2408 devres_release_group(dev, NULL);
2409
2410 return rc;
2411}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002412EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002413
Tejun Heo1c5afdf2010-05-19 22:10:22 +02002414static const struct ata_port_info *ata_sff_find_valid_pi(
2415 const struct ata_port_info * const *ppi)
2416{
2417 int i;
2418
2419 /* look up the first valid port_info */
2420 for (i = 0; i < 2 && ppi[i]; i++)
2421 if (ppi[i]->port_ops != &ata_dummy_port_ops)
2422 return ppi[i];
2423
2424 return NULL;
2425}
2426
Bartlomiej Zolnierkiewiczc2036032011-10-11 19:22:16 +02002427static int ata_pci_init_one(struct pci_dev *pdev,
2428 const struct ata_port_info * const *ppi,
2429 struct scsi_host_template *sht, void *host_priv,
2430 int hflags, bool bmdma)
2431{
2432 struct device *dev = &pdev->dev;
2433 const struct ata_port_info *pi;
2434 struct ata_host *host = NULL;
2435 int rc;
2436
Bartlomiej Zolnierkiewiczc2036032011-10-11 19:22:16 +02002437 pi = ata_sff_find_valid_pi(ppi);
2438 if (!pi) {
2439 dev_err(&pdev->dev, "no valid port_info specified\n");
2440 return -EINVAL;
2441 }
2442
2443 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2444 return -ENOMEM;
2445
2446 rc = pcim_enable_device(pdev);
2447 if (rc)
2448 goto out;
2449
Alexander Beregalovaab94402011-11-13 01:30:56 +04002450#ifdef CONFIG_ATA_BMDMA
Bartlomiej Zolnierkiewiczc2036032011-10-11 19:22:16 +02002451 if (bmdma)
2452 /* prepare and activate BMDMA host */
2453 rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
2454 else
Alexander Beregalovaab94402011-11-13 01:30:56 +04002455#endif
Bartlomiej Zolnierkiewiczc2036032011-10-11 19:22:16 +02002456 /* prepare and activate SFF host */
2457 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
2458 if (rc)
2459 goto out;
2460 host->private_data = host_priv;
2461 host->flags |= hflags;
2462
Alexander Beregalovaab94402011-11-13 01:30:56 +04002463#ifdef CONFIG_ATA_BMDMA
Bartlomiej Zolnierkiewiczc2036032011-10-11 19:22:16 +02002464 if (bmdma) {
2465 pci_set_master(pdev);
2466 rc = ata_pci_sff_activate_host(host, ata_bmdma_interrupt, sht);
2467 } else
Alexander Beregalovaab94402011-11-13 01:30:56 +04002468#endif
Bartlomiej Zolnierkiewiczc2036032011-10-11 19:22:16 +02002469 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
2470out:
2471 if (rc == 0)
2472 devres_remove_group(&pdev->dev, NULL);
2473 else
2474 devres_release_group(&pdev->dev, NULL);
2475
2476 return rc;
2477}
2478
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002479/**
Tejun Heo1c5afdf2010-05-19 22:10:22 +02002480 * ata_pci_sff_init_one - Initialize/register PIO-only PCI IDE controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002481 * @pdev: Controller to be initialized
Tejun Heo1626aeb2007-05-04 12:43:58 +02002482 * @ppi: array of port_info, must be enough for two ports
Tejun Heo1bd5b7152008-03-25 12:22:49 +09002483 * @sht: scsi_host_template to use when registering the host
Tejun Heo887125e2008-03-25 12:22:49 +09002484 * @host_priv: host private_data
Alan Cox16ea0fc2010-02-23 02:26:06 -05002485 * @hflag: host flags
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002486 *
2487 * This is a helper function which can be called from a driver's
2488 * xxx_init_one() probe function if the hardware uses traditional
Tejun Heo1c5afdf2010-05-19 22:10:22 +02002489 * IDE taskfile registers and is PIO only.
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002490 *
Alan Cox2ec7df02006-08-10 16:59:10 +09002491 * ASSUMPTION:
2492 * Nobody makes a single channel controller that appears solely as
2493 * the secondary legacy port on PCI.
2494 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002495 * LOCKING:
2496 * Inherited from PCI layer (may sleep).
2497 *
2498 * RETURNS:
2499 * Zero on success, negative on errno-based value on error.
2500 */
Tejun Heo9363c382008-04-07 22:47:16 +09002501int ata_pci_sff_init_one(struct pci_dev *pdev,
Alan Cox16ea0fc2010-02-23 02:26:06 -05002502 const struct ata_port_info * const *ppi,
2503 struct scsi_host_template *sht, void *host_priv, int hflag)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002504{
Bartlomiej Zolnierkiewiczc2036032011-10-11 19:22:16 +02002505 return ata_pci_init_one(pdev, ppi, sht, host_priv, hflag, 0);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002506}
Tejun Heo9363c382008-04-07 22:47:16 +09002507EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002508
Tejun Heo624d5c52008-03-25 22:16:41 +09002509#endif /* CONFIG_PCI */
Tejun Heo9f2f7212010-05-10 21:41:32 +02002510
Tejun Heo9a7780c2010-05-19 22:10:24 +02002511/*
2512 * BMDMA support
2513 */
2514
2515#ifdef CONFIG_ATA_BMDMA
2516
Tejun Heo9f2f7212010-05-10 21:41:32 +02002517const struct ata_port_operations ata_bmdma_port_ops = {
2518 .inherits = &ata_sff_port_ops,
2519
Tejun Heofe06e5f2010-05-10 21:41:39 +02002520 .error_handler = ata_bmdma_error_handler,
2521 .post_internal_cmd = ata_bmdma_post_internal_cmd,
2522
Tejun Heof47451c2010-05-10 21:41:40 +02002523 .qc_prep = ata_bmdma_qc_prep,
Tejun Heo360ff782010-05-10 21:41:42 +02002524 .qc_issue = ata_bmdma_qc_issue,
Tejun Heof47451c2010-05-10 21:41:40 +02002525
Tejun Heo37f65b82010-05-19 22:10:20 +02002526 .sff_irq_clear = ata_bmdma_irq_clear,
Tejun Heo9f2f7212010-05-10 21:41:32 +02002527 .bmdma_setup = ata_bmdma_setup,
2528 .bmdma_start = ata_bmdma_start,
2529 .bmdma_stop = ata_bmdma_stop,
2530 .bmdma_status = ata_bmdma_status,
Tejun Heoc7087652010-05-10 21:41:34 +02002531
2532 .port_start = ata_bmdma_port_start,
Tejun Heo9f2f7212010-05-10 21:41:32 +02002533};
2534EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
2535
2536const struct ata_port_operations ata_bmdma32_port_ops = {
2537 .inherits = &ata_bmdma_port_ops,
2538
2539 .sff_data_xfer = ata_sff_data_xfer32,
Tejun Heoc7087652010-05-10 21:41:34 +02002540 .port_start = ata_bmdma_port_start32,
Tejun Heo9f2f7212010-05-10 21:41:32 +02002541};
2542EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
2543
Tejun Heo9f2f7212010-05-10 21:41:32 +02002544/**
Tejun Heof47451c2010-05-10 21:41:40 +02002545 * ata_bmdma_fill_sg - Fill PCI IDE PRD table
2546 * @qc: Metadata associated with taskfile to be transferred
2547 *
2548 * Fill PCI IDE PRD (scatter-gather) table with segments
2549 * associated with the current disk command.
2550 *
2551 * LOCKING:
2552 * spin_lock_irqsave(host lock)
2553 *
2554 */
2555static void ata_bmdma_fill_sg(struct ata_queued_cmd *qc)
2556{
2557 struct ata_port *ap = qc->ap;
Tejun Heof60d7012010-05-10 21:41:41 +02002558 struct ata_bmdma_prd *prd = ap->bmdma_prd;
Tejun Heof47451c2010-05-10 21:41:40 +02002559 struct scatterlist *sg;
2560 unsigned int si, pi;
2561
2562 pi = 0;
2563 for_each_sg(qc->sg, sg, qc->n_elem, si) {
2564 u32 addr, offset;
2565 u32 sg_len, len;
2566
2567 /* determine if physical DMA addr spans 64K boundary.
2568 * Note h/w doesn't support 64-bit, so we unconditionally
2569 * truncate dma_addr_t to u32.
2570 */
2571 addr = (u32) sg_dma_address(sg);
2572 sg_len = sg_dma_len(sg);
2573
2574 while (sg_len) {
2575 offset = addr & 0xffff;
2576 len = sg_len;
2577 if ((offset + sg_len) > 0x10000)
2578 len = 0x10000 - offset;
2579
Tejun Heof60d7012010-05-10 21:41:41 +02002580 prd[pi].addr = cpu_to_le32(addr);
2581 prd[pi].flags_len = cpu_to_le32(len & 0xffff);
Tejun Heof47451c2010-05-10 21:41:40 +02002582
2583 pi++;
2584 sg_len -= len;
2585 addr += len;
2586 }
2587 }
2588
Tejun Heof60d7012010-05-10 21:41:41 +02002589 prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
Tejun Heof47451c2010-05-10 21:41:40 +02002590}
2591
2592/**
2593 * ata_bmdma_fill_sg_dumb - Fill PCI IDE PRD table
2594 * @qc: Metadata associated with taskfile to be transferred
2595 *
2596 * Fill PCI IDE PRD (scatter-gather) table with segments
2597 * associated with the current disk command. Perform the fill
2598 * so that we avoid writing any length 64K records for
2599 * controllers that don't follow the spec.
2600 *
2601 * LOCKING:
2602 * spin_lock_irqsave(host lock)
2603 *
2604 */
2605static void ata_bmdma_fill_sg_dumb(struct ata_queued_cmd *qc)
2606{
2607 struct ata_port *ap = qc->ap;
Tejun Heof60d7012010-05-10 21:41:41 +02002608 struct ata_bmdma_prd *prd = ap->bmdma_prd;
Tejun Heof47451c2010-05-10 21:41:40 +02002609 struct scatterlist *sg;
2610 unsigned int si, pi;
2611
2612 pi = 0;
2613 for_each_sg(qc->sg, sg, qc->n_elem, si) {
2614 u32 addr, offset;
2615 u32 sg_len, len, blen;
2616
2617 /* determine if physical DMA addr spans 64K boundary.
2618 * Note h/w doesn't support 64-bit, so we unconditionally
2619 * truncate dma_addr_t to u32.
2620 */
2621 addr = (u32) sg_dma_address(sg);
2622 sg_len = sg_dma_len(sg);
2623
2624 while (sg_len) {
2625 offset = addr & 0xffff;
2626 len = sg_len;
2627 if ((offset + sg_len) > 0x10000)
2628 len = 0x10000 - offset;
2629
2630 blen = len & 0xffff;
Tejun Heof60d7012010-05-10 21:41:41 +02002631 prd[pi].addr = cpu_to_le32(addr);
Tejun Heof47451c2010-05-10 21:41:40 +02002632 if (blen == 0) {
2633 /* Some PATA chipsets like the CS5530 can't
2634 cope with 0x0000 meaning 64K as the spec
2635 says */
Tejun Heof60d7012010-05-10 21:41:41 +02002636 prd[pi].flags_len = cpu_to_le32(0x8000);
Tejun Heof47451c2010-05-10 21:41:40 +02002637 blen = 0x8000;
Tejun Heof60d7012010-05-10 21:41:41 +02002638 prd[++pi].addr = cpu_to_le32(addr + 0x8000);
Tejun Heof47451c2010-05-10 21:41:40 +02002639 }
Tejun Heof60d7012010-05-10 21:41:41 +02002640 prd[pi].flags_len = cpu_to_le32(blen);
Tejun Heof47451c2010-05-10 21:41:40 +02002641
2642 pi++;
2643 sg_len -= len;
2644 addr += len;
2645 }
2646 }
2647
Tejun Heof60d7012010-05-10 21:41:41 +02002648 prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
Tejun Heof47451c2010-05-10 21:41:40 +02002649}
2650
2651/**
2652 * ata_bmdma_qc_prep - Prepare taskfile for submission
2653 * @qc: Metadata associated with taskfile to be prepared
2654 *
2655 * Prepare ATA taskfile for submission.
2656 *
2657 * LOCKING:
2658 * spin_lock_irqsave(host lock)
2659 */
Jiri Slaby95364f32019-10-31 10:59:45 +01002660enum ata_completion_errors ata_bmdma_qc_prep(struct ata_queued_cmd *qc)
Tejun Heof47451c2010-05-10 21:41:40 +02002661{
2662 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
Jiri Slaby95364f32019-10-31 10:59:45 +01002663 return AC_ERR_OK;
Tejun Heof47451c2010-05-10 21:41:40 +02002664
2665 ata_bmdma_fill_sg(qc);
Jiri Slaby95364f32019-10-31 10:59:45 +01002666
2667 return AC_ERR_OK;
Tejun Heof47451c2010-05-10 21:41:40 +02002668}
2669EXPORT_SYMBOL_GPL(ata_bmdma_qc_prep);
2670
2671/**
2672 * ata_bmdma_dumb_qc_prep - Prepare taskfile for submission
2673 * @qc: Metadata associated with taskfile to be prepared
2674 *
2675 * Prepare ATA taskfile for submission.
2676 *
2677 * LOCKING:
2678 * spin_lock_irqsave(host lock)
2679 */
Jiri Slaby95364f32019-10-31 10:59:45 +01002680enum ata_completion_errors ata_bmdma_dumb_qc_prep(struct ata_queued_cmd *qc)
Tejun Heof47451c2010-05-10 21:41:40 +02002681{
2682 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
Jiri Slaby95364f32019-10-31 10:59:45 +01002683 return AC_ERR_OK;
Tejun Heof47451c2010-05-10 21:41:40 +02002684
2685 ata_bmdma_fill_sg_dumb(qc);
Jiri Slaby95364f32019-10-31 10:59:45 +01002686
2687 return AC_ERR_OK;
Tejun Heof47451c2010-05-10 21:41:40 +02002688}
2689EXPORT_SYMBOL_GPL(ata_bmdma_dumb_qc_prep);
2690
2691/**
Tejun Heo360ff782010-05-10 21:41:42 +02002692 * ata_bmdma_qc_issue - issue taskfile to a BMDMA controller
2693 * @qc: command to issue to device
2694 *
2695 * This function issues a PIO, NODATA or DMA command to a
2696 * SFF/BMDMA controller. PIO and NODATA are handled by
2697 * ata_sff_qc_issue().
2698 *
2699 * LOCKING:
2700 * spin_lock_irqsave(host lock)
2701 *
2702 * RETURNS:
2703 * Zero on success, AC_ERR_* mask on failure
2704 */
2705unsigned int ata_bmdma_qc_issue(struct ata_queued_cmd *qc)
2706{
2707 struct ata_port *ap = qc->ap;
Gwendal Grignouea3c6452010-08-31 16:20:36 -07002708 struct ata_link *link = qc->dev->link;
Tejun Heo360ff782010-05-10 21:41:42 +02002709
Tejun Heo360ff782010-05-10 21:41:42 +02002710 /* defer PIO handling to sff_qc_issue */
2711 if (!ata_is_dma(qc->tf.protocol))
2712 return ata_sff_qc_issue(qc);
2713
2714 /* select the device */
2715 ata_dev_select(ap, qc->dev->devno, 1, 0);
2716
2717 /* start the command */
2718 switch (qc->tf.protocol) {
2719 case ATA_PROT_DMA:
2720 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
2721
Hannes Reineckec206a382021-12-21 08:20:31 +01002722 trace_ata_tf_load(ap, &qc->tf);
Tejun Heo360ff782010-05-10 21:41:42 +02002723 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Hannes Reineckec206a382021-12-21 08:20:31 +01002724 trace_ata_bmdma_setup(ap, &qc->tf, qc->tag);
Tejun Heo360ff782010-05-10 21:41:42 +02002725 ap->ops->bmdma_setup(qc); /* set up bmdma */
Hannes Reineckec206a382021-12-21 08:20:31 +01002726 trace_ata_bmdma_start(ap, &qc->tf, qc->tag);
Tejun Heo360ff782010-05-10 21:41:42 +02002727 ap->ops->bmdma_start(qc); /* initiate bmdma */
2728 ap->hsm_task_state = HSM_ST_LAST;
2729 break;
2730
2731 case ATAPI_PROT_DMA:
2732 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
2733
Hannes Reineckec206a382021-12-21 08:20:31 +01002734 trace_ata_tf_load(ap, &qc->tf);
Tejun Heo360ff782010-05-10 21:41:42 +02002735 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Hannes Reineckec206a382021-12-21 08:20:31 +01002736 trace_ata_bmdma_setup(ap, &qc->tf, qc->tag);
Tejun Heo360ff782010-05-10 21:41:42 +02002737 ap->ops->bmdma_setup(qc); /* set up bmdma */
2738 ap->hsm_task_state = HSM_ST_FIRST;
2739
2740 /* send cdb by polling if no cdb interrupt */
2741 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Gwendal Grignouea3c6452010-08-31 16:20:36 -07002742 ata_sff_queue_pio_task(link, 0);
Tejun Heo360ff782010-05-10 21:41:42 +02002743 break;
2744
2745 default:
2746 WARN_ON(1);
2747 return AC_ERR_SYSTEM;
2748 }
2749
2750 return 0;
2751}
2752EXPORT_SYMBOL_GPL(ata_bmdma_qc_issue);
2753
2754/**
Tejun Heoc3b28892010-05-19 22:10:21 +02002755 * ata_bmdma_port_intr - Handle BMDMA port interrupt
2756 * @ap: Port on which interrupt arrived (possibly...)
2757 * @qc: Taskfile currently active in engine
2758 *
2759 * Handle port interrupt for given queued command.
2760 *
2761 * LOCKING:
2762 * spin_lock_irqsave(host lock)
2763 *
2764 * RETURNS:
2765 * One if interrupt was handled, zero if not (shared irq).
2766 */
2767unsigned int ata_bmdma_port_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
2768{
2769 struct ata_eh_info *ehi = &ap->link.eh_info;
2770 u8 host_stat = 0;
2771 bool bmdma_stopped = false;
2772 unsigned int handled;
2773
2774 if (ap->hsm_task_state == HSM_ST_LAST && ata_is_dma(qc->tf.protocol)) {
2775 /* check status of DMA engine */
2776 host_stat = ap->ops->bmdma_status(ap);
Hannes Reineckec206a382021-12-21 08:20:31 +01002777 trace_ata_bmdma_status(ap, host_stat);
Tejun Heoc3b28892010-05-19 22:10:21 +02002778
2779 /* if it's not our irq... */
2780 if (!(host_stat & ATA_DMA_INTR))
2781 return ata_sff_idle_irq(ap);
2782
2783 /* before we do anything else, clear DMA-Start bit */
Hannes Reineckec206a382021-12-21 08:20:31 +01002784 trace_ata_bmdma_stop(ap, &qc->tf, qc->tag);
Tejun Heoc3b28892010-05-19 22:10:21 +02002785 ap->ops->bmdma_stop(qc);
2786 bmdma_stopped = true;
2787
2788 if (unlikely(host_stat & ATA_DMA_ERR)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002789 /* error when transferring data to/from memory */
Tejun Heoc3b28892010-05-19 22:10:21 +02002790 qc->err_mask |= AC_ERR_HOST_BUS;
2791 ap->hsm_task_state = HSM_ST_ERR;
2792 }
2793 }
2794
2795 handled = __ata_sff_port_intr(ap, qc, bmdma_stopped);
2796
2797 if (unlikely(qc->err_mask) && ata_is_dma(qc->tf.protocol))
2798 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
2799
2800 return handled;
2801}
2802EXPORT_SYMBOL_GPL(ata_bmdma_port_intr);
2803
2804/**
2805 * ata_bmdma_interrupt - Default BMDMA ATA host interrupt handler
2806 * @irq: irq line (unused)
2807 * @dev_instance: pointer to our ata_host information structure
2808 *
2809 * Default interrupt handler for PCI IDE devices. Calls
2810 * ata_bmdma_port_intr() for each port that is not disabled.
2811 *
2812 * LOCKING:
2813 * Obtains host lock during operation.
2814 *
2815 * RETURNS:
2816 * IRQ_NONE or IRQ_HANDLED.
2817 */
2818irqreturn_t ata_bmdma_interrupt(int irq, void *dev_instance)
2819{
2820 return __ata_sff_interrupt(irq, dev_instance, ata_bmdma_port_intr);
2821}
2822EXPORT_SYMBOL_GPL(ata_bmdma_interrupt);
2823
2824/**
Tejun Heofe06e5f2010-05-10 21:41:39 +02002825 * ata_bmdma_error_handler - Stock error handler for BMDMA controller
2826 * @ap: port to handle error for
2827 *
2828 * Stock error handler for BMDMA controller. It can handle both
2829 * PATA and SATA controllers. Most BMDMA controllers should be
2830 * able to use this EH as-is or with some added handling before
2831 * and after.
2832 *
2833 * LOCKING:
2834 * Kernel thread context (may sleep)
2835 */
2836void ata_bmdma_error_handler(struct ata_port *ap)
2837{
2838 struct ata_queued_cmd *qc;
2839 unsigned long flags;
2840 bool thaw = false;
2841
2842 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
2843 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2844 qc = NULL;
2845
2846 /* reset PIO HSM and stop DMA engine */
2847 spin_lock_irqsave(ap->lock, flags);
2848
2849 if (qc && ata_is_dma(qc->tf.protocol)) {
2850 u8 host_stat;
2851
2852 host_stat = ap->ops->bmdma_status(ap);
Hannes Reineckec206a382021-12-21 08:20:31 +01002853 trace_ata_bmdma_status(ap, host_stat);
Tejun Heofe06e5f2010-05-10 21:41:39 +02002854
2855 /* BMDMA controllers indicate host bus error by
2856 * setting DMA_ERR bit and timing out. As it wasn't
2857 * really a timeout event, adjust error mask and
2858 * cancel frozen state.
2859 */
2860 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
2861 qc->err_mask = AC_ERR_HOST_BUS;
2862 thaw = true;
2863 }
2864
Hannes Reineckec206a382021-12-21 08:20:31 +01002865 trace_ata_bmdma_stop(ap, &qc->tf, qc->tag);
Tejun Heofe06e5f2010-05-10 21:41:39 +02002866 ap->ops->bmdma_stop(qc);
2867
2868 /* if we're gonna thaw, make sure IRQ is clear */
2869 if (thaw) {
2870 ap->ops->sff_check_status(ap);
Tejun Heo37f65b82010-05-19 22:10:20 +02002871 if (ap->ops->sff_irq_clear)
2872 ap->ops->sff_irq_clear(ap);
Tejun Heofe06e5f2010-05-10 21:41:39 +02002873 }
2874 }
2875
2876 spin_unlock_irqrestore(ap->lock, flags);
2877
2878 if (thaw)
2879 ata_eh_thaw_port(ap);
2880
2881 ata_sff_error_handler(ap);
2882}
2883EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
2884
2885/**
2886 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for BMDMA
2887 * @qc: internal command to clean up
2888 *
2889 * LOCKING:
2890 * Kernel thread context (may sleep)
2891 */
2892void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
2893{
2894 struct ata_port *ap = qc->ap;
2895 unsigned long flags;
2896
2897 if (ata_is_dma(qc->tf.protocol)) {
2898 spin_lock_irqsave(ap->lock, flags);
Hannes Reineckec206a382021-12-21 08:20:31 +01002899 trace_ata_bmdma_stop(ap, &qc->tf, qc->tag);
Tejun Heofe06e5f2010-05-10 21:41:39 +02002900 ap->ops->bmdma_stop(qc);
2901 spin_unlock_irqrestore(ap->lock, flags);
2902 }
2903}
2904EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
2905
2906/**
Tejun Heo37f65b82010-05-19 22:10:20 +02002907 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
2908 * @ap: Port associated with this ATA transaction.
2909 *
2910 * Clear interrupt and error flags in DMA status register.
2911 *
2912 * May be used as the irq_clear() entry in ata_port_operations.
2913 *
2914 * LOCKING:
2915 * spin_lock_irqsave(host lock)
2916 */
2917void ata_bmdma_irq_clear(struct ata_port *ap)
2918{
2919 void __iomem *mmio = ap->ioaddr.bmdma_addr;
2920
2921 if (!mmio)
2922 return;
2923
2924 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
2925}
2926EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
2927
2928/**
Tejun Heo9f2f7212010-05-10 21:41:32 +02002929 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2930 * @qc: Info associated with this ATA transaction.
2931 *
2932 * LOCKING:
2933 * spin_lock_irqsave(host lock)
2934 */
2935void ata_bmdma_setup(struct ata_queued_cmd *qc)
2936{
2937 struct ata_port *ap = qc->ap;
2938 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2939 u8 dmactl;
2940
2941 /* load PRD table addr. */
2942 mb(); /* make sure PRD table writes are visible to controller */
Tejun Heof60d7012010-05-10 21:41:41 +02002943 iowrite32(ap->bmdma_prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
Tejun Heo9f2f7212010-05-10 21:41:32 +02002944
2945 /* specify data direction, triple-check start bit is clear */
2946 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2947 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2948 if (!rw)
2949 dmactl |= ATA_DMA_WR;
2950 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2951
2952 /* issue r/w command */
2953 ap->ops->sff_exec_command(ap, &qc->tf);
2954}
2955EXPORT_SYMBOL_GPL(ata_bmdma_setup);
2956
2957/**
2958 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2959 * @qc: Info associated with this ATA transaction.
2960 *
2961 * LOCKING:
2962 * spin_lock_irqsave(host lock)
2963 */
2964void ata_bmdma_start(struct ata_queued_cmd *qc)
2965{
2966 struct ata_port *ap = qc->ap;
2967 u8 dmactl;
2968
2969 /* start host DMA transaction */
2970 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2971 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2972
2973 /* Strictly, one may wish to issue an ioread8() here, to
2974 * flush the mmio write. However, control also passes
2975 * to the hardware at this point, and it will interrupt
2976 * us when we are to resume control. So, in effect,
2977 * we don't care when the mmio write flushes.
2978 * Further, a read of the DMA status register _immediately_
2979 * following the write may not be what certain flaky hardware
2980 * is expected, so I think it is best to not add a readb()
2981 * without first all the MMIO ATA cards/mobos.
2982 * Or maybe I'm just being paranoid.
2983 *
2984 * FIXME: The posting of this write means I/O starts are
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002985 * unnecessarily delayed for MMIO
Tejun Heo9f2f7212010-05-10 21:41:32 +02002986 */
2987}
2988EXPORT_SYMBOL_GPL(ata_bmdma_start);
2989
2990/**
2991 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2992 * @qc: Command we are ending DMA for
2993 *
2994 * Clears the ATA_DMA_START flag in the dma control register
2995 *
2996 * May be used as the bmdma_stop() entry in ata_port_operations.
2997 *
2998 * LOCKING:
2999 * spin_lock_irqsave(host lock)
3000 */
3001void ata_bmdma_stop(struct ata_queued_cmd *qc)
3002{
3003 struct ata_port *ap = qc->ap;
3004 void __iomem *mmio = ap->ioaddr.bmdma_addr;
3005
3006 /* clear start/stop bit */
3007 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3008 mmio + ATA_DMA_CMD);
3009
3010 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3011 ata_sff_dma_pause(ap);
3012}
3013EXPORT_SYMBOL_GPL(ata_bmdma_stop);
3014
3015/**
3016 * ata_bmdma_status - Read PCI IDE BMDMA status
3017 * @ap: Port associated with this ATA transaction.
3018 *
3019 * Read and return BMDMA status register.
3020 *
3021 * May be used as the bmdma_status() entry in ata_port_operations.
3022 *
3023 * LOCKING:
3024 * spin_lock_irqsave(host lock)
3025 */
3026u8 ata_bmdma_status(struct ata_port *ap)
3027{
3028 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3029}
3030EXPORT_SYMBOL_GPL(ata_bmdma_status);
3031
Tejun Heoc7087652010-05-10 21:41:34 +02003032
3033/**
3034 * ata_bmdma_port_start - Set port up for bmdma.
3035 * @ap: Port to initialize
3036 *
3037 * Called just after data structures for each port are
3038 * initialized. Allocates space for PRD table.
3039 *
3040 * May be used as the port_start() entry in ata_port_operations.
3041 *
3042 * LOCKING:
3043 * Inherited from caller.
3044 */
3045int ata_bmdma_port_start(struct ata_port *ap)
3046{
3047 if (ap->mwdma_mask || ap->udma_mask) {
Tejun Heof60d7012010-05-10 21:41:41 +02003048 ap->bmdma_prd =
3049 dmam_alloc_coherent(ap->host->dev, ATA_PRD_TBL_SZ,
3050 &ap->bmdma_prd_dma, GFP_KERNEL);
3051 if (!ap->bmdma_prd)
Tejun Heoc7087652010-05-10 21:41:34 +02003052 return -ENOMEM;
3053 }
3054
3055 return 0;
3056}
3057EXPORT_SYMBOL_GPL(ata_bmdma_port_start);
3058
3059/**
3060 * ata_bmdma_port_start32 - Set port up for dma.
3061 * @ap: Port to initialize
3062 *
3063 * Called just after data structures for each port are
3064 * initialized. Enables 32bit PIO and allocates space for PRD
3065 * table.
3066 *
3067 * May be used as the port_start() entry in ata_port_operations for
3068 * devices that are capable of 32bit PIO.
3069 *
3070 * LOCKING:
3071 * Inherited from caller.
3072 */
3073int ata_bmdma_port_start32(struct ata_port *ap)
3074{
3075 ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE;
3076 return ata_bmdma_port_start(ap);
3077}
3078EXPORT_SYMBOL_GPL(ata_bmdma_port_start32);
3079
Tejun Heo9f2f7212010-05-10 21:41:32 +02003080#ifdef CONFIG_PCI
3081
3082/**
3083 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
3084 * @pdev: PCI device
3085 *
3086 * Some PCI ATA devices report simplex mode but in fact can be told to
3087 * enter non simplex mode. This implements the necessary logic to
3088 * perform the task on such devices. Calling it on other devices will
3089 * have -undefined- behaviour.
3090 */
3091int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
3092{
3093 unsigned long bmdma = pci_resource_start(pdev, 4);
3094 u8 simplex;
3095
3096 if (bmdma == 0)
3097 return -ENOENT;
3098
3099 simplex = inb(bmdma + 0x02);
3100 outb(simplex & 0x60, bmdma + 0x02);
3101 simplex = inb(bmdma + 0x02);
3102 if (simplex & 0x80)
3103 return -EOPNOTSUPP;
3104 return 0;
3105}
3106EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
3107
Tejun Heoc7087652010-05-10 21:41:34 +02003108static void ata_bmdma_nodma(struct ata_host *host, const char *reason)
3109{
3110 int i;
3111
Joe Perchesa44fec12011-04-15 15:51:58 -07003112 dev_err(host->dev, "BMDMA: %s, falling back to PIO\n", reason);
Tejun Heoc7087652010-05-10 21:41:34 +02003113
3114 for (i = 0; i < 2; i++) {
3115 host->ports[i]->mwdma_mask = 0;
3116 host->ports[i]->udma_mask = 0;
3117 }
3118}
3119
Tejun Heo9f2f7212010-05-10 21:41:32 +02003120/**
3121 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
3122 * @host: target ATA host
3123 *
3124 * Acquire PCI BMDMA resources and initialize @host accordingly.
3125 *
3126 * LOCKING:
3127 * Inherited from calling layer (may sleep).
Tejun Heo9f2f7212010-05-10 21:41:32 +02003128 */
Tejun Heoc7087652010-05-10 21:41:34 +02003129void ata_pci_bmdma_init(struct ata_host *host)
Tejun Heo9f2f7212010-05-10 21:41:32 +02003130{
3131 struct device *gdev = host->dev;
3132 struct pci_dev *pdev = to_pci_dev(gdev);
3133 int i, rc;
3134
3135 /* No BAR4 allocation: No DMA */
Tejun Heoc7087652010-05-10 21:41:34 +02003136 if (pci_resource_start(pdev, 4) == 0) {
3137 ata_bmdma_nodma(host, "BAR4 is zero");
3138 return;
3139 }
Tejun Heo9f2f7212010-05-10 21:41:32 +02003140
Tejun Heoc7087652010-05-10 21:41:34 +02003141 /*
3142 * Some controllers require BMDMA region to be initialized
3143 * even if DMA is not in use to clear IRQ status via
3144 * ->sff_irq_clear method. Try to initialize bmdma_addr
3145 * regardless of dma masks.
3146 */
Christoph Hellwigb5e55552019-08-26 12:57:25 +02003147 rc = dma_set_mask_and_coherent(&pdev->dev, ATA_DMA_MASK);
Tejun Heo9f2f7212010-05-10 21:41:32 +02003148 if (rc)
Tejun Heoc7087652010-05-10 21:41:34 +02003149 ata_bmdma_nodma(host, "failed to set dma mask");
Tejun Heo9f2f7212010-05-10 21:41:32 +02003150
3151 /* request and iomap DMA region */
3152 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
3153 if (rc) {
Tejun Heoc7087652010-05-10 21:41:34 +02003154 ata_bmdma_nodma(host, "failed to request/iomap BAR4");
3155 return;
Tejun Heo9f2f7212010-05-10 21:41:32 +02003156 }
3157 host->iomap = pcim_iomap_table(pdev);
3158
3159 for (i = 0; i < 2; i++) {
3160 struct ata_port *ap = host->ports[i];
3161 void __iomem *bmdma = host->iomap[4] + 8 * i;
3162
3163 if (ata_port_is_dummy(ap))
3164 continue;
3165
3166 ap->ioaddr.bmdma_addr = bmdma;
3167 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
3168 (ioread8(bmdma + 2) & 0x80))
3169 host->flags |= ATA_HOST_SIMPLEX;
3170
3171 ata_port_desc(ap, "bmdma 0x%llx",
3172 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
3173 }
Tejun Heo9f2f7212010-05-10 21:41:32 +02003174}
3175EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
3176
Tejun Heo1c5afdf2010-05-19 22:10:22 +02003177/**
3178 * ata_pci_bmdma_prepare_host - helper to prepare PCI BMDMA ATA host
3179 * @pdev: target PCI device
3180 * @ppi: array of port_info, must be enough for two ports
3181 * @r_host: out argument for the initialized ATA host
3182 *
3183 * Helper to allocate BMDMA ATA host for @pdev, acquire all PCI
3184 * resources and initialize it accordingly in one go.
3185 *
3186 * LOCKING:
3187 * Inherited from calling layer (may sleep).
3188 *
3189 * RETURNS:
3190 * 0 on success, -errno otherwise.
3191 */
3192int ata_pci_bmdma_prepare_host(struct pci_dev *pdev,
3193 const struct ata_port_info * const * ppi,
3194 struct ata_host **r_host)
3195{
3196 int rc;
3197
3198 rc = ata_pci_sff_prepare_host(pdev, ppi, r_host);
3199 if (rc)
3200 return rc;
3201
3202 ata_pci_bmdma_init(*r_host);
3203 return 0;
3204}
3205EXPORT_SYMBOL_GPL(ata_pci_bmdma_prepare_host);
3206
3207/**
3208 * ata_pci_bmdma_init_one - Initialize/register BMDMA PCI IDE controller
3209 * @pdev: Controller to be initialized
3210 * @ppi: array of port_info, must be enough for two ports
3211 * @sht: scsi_host_template to use when registering the host
3212 * @host_priv: host private_data
3213 * @hflags: host flags
3214 *
3215 * This function is similar to ata_pci_sff_init_one() but also
3216 * takes care of BMDMA initialization.
3217 *
3218 * LOCKING:
3219 * Inherited from PCI layer (may sleep).
3220 *
3221 * RETURNS:
3222 * Zero on success, negative on errno-based value on error.
3223 */
3224int ata_pci_bmdma_init_one(struct pci_dev *pdev,
3225 const struct ata_port_info * const * ppi,
3226 struct scsi_host_template *sht, void *host_priv,
3227 int hflags)
3228{
Bartlomiej Zolnierkiewiczc2036032011-10-11 19:22:16 +02003229 return ata_pci_init_one(pdev, ppi, sht, host_priv, hflags, 1);
Tejun Heo1c5afdf2010-05-19 22:10:22 +02003230}
3231EXPORT_SYMBOL_GPL(ata_pci_bmdma_init_one);
3232
Tejun Heo9f2f7212010-05-10 21:41:32 +02003233#endif /* CONFIG_PCI */
Tejun Heo9a7780c2010-05-19 22:10:24 +02003234#endif /* CONFIG_ATA_BMDMA */
Tejun Heo270390e2010-05-10 21:41:35 +02003235
3236/**
3237 * ata_sff_port_init - Initialize SFF/BMDMA ATA port
3238 * @ap: Port to initialize
3239 *
3240 * Called on port allocation to initialize SFF/BMDMA specific
3241 * fields.
3242 *
3243 * LOCKING:
3244 * None.
3245 */
3246void ata_sff_port_init(struct ata_port *ap)
3247{
Tejun Heoc4291372010-05-10 21:41:38 +02003248 INIT_DELAYED_WORK(&ap->sff_pio_task, ata_sff_pio_task);
Tejun Heo5fe74542010-05-10 21:41:37 +02003249 ap->ctl = ATA_DEVCTL_OBS;
3250 ap->last_ctl = 0xFF;
Tejun Heo270390e2010-05-10 21:41:35 +02003251}
3252
3253int __init ata_sff_init(void)
3254{
Tejun Heo6370a6a2010-10-11 15:12:27 +02003255 ata_sff_wq = alloc_workqueue("ata_sff", WQ_MEM_RECLAIM, WQ_MAX_ACTIVE);
Tejun Heoc4291372010-05-10 21:41:38 +02003256 if (!ata_sff_wq)
3257 return -ENOMEM;
3258
Tejun Heo270390e2010-05-10 21:41:35 +02003259 return 0;
3260}
3261
Luck, Tonyc43d5592010-08-23 13:18:02 -07003262void ata_sff_exit(void)
Tejun Heo270390e2010-05-10 21:41:35 +02003263{
Tejun Heoc4291372010-05-10 21:41:38 +02003264 destroy_workqueue(ata_sff_wq);
Tejun Heo270390e2010-05-10 21:41:35 +02003265}