blob: 415c64f81145c08b9702c6c33e55ac1dcaae9879 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040051#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100052#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "scsi_priv.h"
Jeff Garzik193515d2005-11-07 00:59:37 -050055#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
Albert Lee59a10b12005-10-12 15:09:42 +080064static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ec2005-05-12 15:29:42 -040065static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ata_set_mode(struct ata_port *ap);
67static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040068static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040070static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u8 *xfer_mode_out,
72 unsigned int *xfer_shift_out);
Jeff Garzike33b9df2005-10-09 09:51:46 -040073static void ata_pio_error(struct ata_port *ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static unsigned int ata_unique_id = 1;
76static struct workqueue_struct *ata_wq;
77
Jeff Garzik1623c812005-08-30 03:37:42 -040078int atapi_enabled = 0;
79module_param(atapi_enabled, int, 0444);
80MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082MODULE_AUTHOR("Jeff Garzik");
83MODULE_DESCRIPTION("Library module for ATA devices");
84MODULE_LICENSE("GPL");
85MODULE_VERSION(DRV_VERSION);
86
87/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040088 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * @ap: Port to which output is sent
90 * @tf: ATA taskfile register set
91 *
92 * Outputs ATA taskfile to standard ATA host controller.
93 *
94 * LOCKING:
95 * Inherited from caller.
96 */
97
Jeff Garzik057ace52005-10-22 14:27:05 -040098static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct ata_ioports *ioaddr = &ap->ioaddr;
101 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
102
103 if (tf->ctl != ap->last_ctl) {
104 outb(tf->ctl, ioaddr->ctl_addr);
105 ap->last_ctl = tf->ctl;
106 ata_wait_idle(ap);
107 }
108
109 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
110 outb(tf->hob_feature, ioaddr->feature_addr);
111 outb(tf->hob_nsect, ioaddr->nsect_addr);
112 outb(tf->hob_lbal, ioaddr->lbal_addr);
113 outb(tf->hob_lbam, ioaddr->lbam_addr);
114 outb(tf->hob_lbah, ioaddr->lbah_addr);
115 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
116 tf->hob_feature,
117 tf->hob_nsect,
118 tf->hob_lbal,
119 tf->hob_lbam,
120 tf->hob_lbah);
121 }
122
123 if (is_addr) {
124 outb(tf->feature, ioaddr->feature_addr);
125 outb(tf->nsect, ioaddr->nsect_addr);
126 outb(tf->lbal, ioaddr->lbal_addr);
127 outb(tf->lbam, ioaddr->lbam_addr);
128 outb(tf->lbah, ioaddr->lbah_addr);
129 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
130 tf->feature,
131 tf->nsect,
132 tf->lbal,
133 tf->lbam,
134 tf->lbah);
135 }
136
137 if (tf->flags & ATA_TFLAG_DEVICE) {
138 outb(tf->device, ioaddr->device_addr);
139 VPRINTK("device 0x%X\n", tf->device);
140 }
141
142 ata_wait_idle(ap);
143}
144
145/**
146 * ata_tf_load_mmio - send taskfile registers to host controller
147 * @ap: Port to which output is sent
148 * @tf: ATA taskfile register set
149 *
150 * Outputs ATA taskfile to standard ATA host controller using MMIO.
151 *
152 * LOCKING:
153 * Inherited from caller.
154 */
155
Jeff Garzik057ace52005-10-22 14:27:05 -0400156static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct ata_ioports *ioaddr = &ap->ioaddr;
159 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
160
161 if (tf->ctl != ap->last_ctl) {
162 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
163 ap->last_ctl = tf->ctl;
164 ata_wait_idle(ap);
165 }
166
167 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
168 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
169 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
170 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
171 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
172 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
173 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
174 tf->hob_feature,
175 tf->hob_nsect,
176 tf->hob_lbal,
177 tf->hob_lbam,
178 tf->hob_lbah);
179 }
180
181 if (is_addr) {
182 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
183 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
184 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
185 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
186 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
187 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
188 tf->feature,
189 tf->nsect,
190 tf->lbal,
191 tf->lbam,
192 tf->lbah);
193 }
194
195 if (tf->flags & ATA_TFLAG_DEVICE) {
196 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
197 VPRINTK("device 0x%X\n", tf->device);
198 }
199
200 ata_wait_idle(ap);
201}
202
Edward Falk0baab862005-06-02 18:17:13 -0400203
204/**
205 * ata_tf_load - send taskfile registers to host controller
206 * @ap: Port to which output is sent
207 * @tf: ATA taskfile register set
208 *
209 * Outputs ATA taskfile to standard ATA host controller using MMIO
210 * or PIO as indicated by the ATA_FLAG_MMIO flag.
211 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
212 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
213 * hob_lbal, hob_lbam, and hob_lbah.
214 *
215 * This function waits for idle (!BUSY and !DRQ) after writing
216 * registers. If the control register has a new value, this
217 * function also waits for idle after writing control and before
218 * writing the remaining registers.
219 *
220 * May be used as the tf_load() entry in ata_port_operations.
221 *
222 * LOCKING:
223 * Inherited from caller.
224 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400225void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 if (ap->flags & ATA_FLAG_MMIO)
228 ata_tf_load_mmio(ap, tf);
229 else
230 ata_tf_load_pio(ap, tf);
231}
232
233/**
Edward Falk0baab862005-06-02 18:17:13 -0400234 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * @ap: port to which command is being issued
236 * @tf: ATA taskfile register set
237 *
Edward Falk0baab862005-06-02 18:17:13 -0400238 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * synchronization with interrupt handler / other threads.
240 *
241 * LOCKING:
242 * spin_lock_irqsave(host_set lock)
243 */
244
Jeff Garzik057ace52005-10-22 14:27:05 -0400245static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
248
249 outb(tf->command, ap->ioaddr.command_addr);
250 ata_pause(ap);
251}
252
253
254/**
255 * ata_exec_command_mmio - issue ATA command to host controller
256 * @ap: port to which command is being issued
257 * @tf: ATA taskfile register set
258 *
259 * Issues MMIO write to ATA command register, with proper
260 * synchronization with interrupt handler / other threads.
261 *
262 * LOCKING:
263 * spin_lock_irqsave(host_set lock)
264 */
265
Jeff Garzik057ace52005-10-22 14:27:05 -0400266static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
269
270 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
271 ata_pause(ap);
272}
273
Edward Falk0baab862005-06-02 18:17:13 -0400274
275/**
276 * ata_exec_command - issue ATA command to host controller
277 * @ap: port to which command is being issued
278 * @tf: ATA taskfile register set
279 *
280 * Issues PIO/MMIO write to ATA command register, with proper
281 * synchronization with interrupt handler / other threads.
282 *
283 * LOCKING:
284 * spin_lock_irqsave(host_set lock)
285 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400286void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 if (ap->flags & ATA_FLAG_MMIO)
289 ata_exec_command_mmio(ap, tf);
290 else
291 ata_exec_command_pio(ap, tf);
292}
293
294/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * ata_tf_to_host - issue ATA taskfile to host controller
296 * @ap: port to which command is being issued
297 * @tf: ATA taskfile register set
298 *
299 * Issues ATA taskfile register set to ATA host controller,
300 * with proper synchronization with interrupt handler and
301 * other threads.
302 *
303 * LOCKING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * spin_lock_irqsave(host_set lock)
305 */
306
Jeff Garzike5338252005-10-30 21:37:17 -0500307static inline void ata_tf_to_host(struct ata_port *ap,
308 const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 ap->ops->tf_load(ap, tf);
311 ap->ops->exec_command(ap, tf);
312}
313
314/**
Edward Falk0baab862005-06-02 18:17:13 -0400315 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * @ap: Port from which input is read
317 * @tf: ATA taskfile register set for storing input
318 *
319 * Reads ATA taskfile registers for currently-selected device
320 * into @tf.
321 *
322 * LOCKING:
323 * Inherited from caller.
324 */
325
326static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
327{
328 struct ata_ioports *ioaddr = &ap->ioaddr;
329
Jeff Garzikac19bff2005-10-29 13:58:21 -0400330 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400331 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 tf->nsect = inb(ioaddr->nsect_addr);
333 tf->lbal = inb(ioaddr->lbal_addr);
334 tf->lbam = inb(ioaddr->lbam_addr);
335 tf->lbah = inb(ioaddr->lbah_addr);
336 tf->device = inb(ioaddr->device_addr);
337
338 if (tf->flags & ATA_TFLAG_LBA48) {
339 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
340 tf->hob_feature = inb(ioaddr->error_addr);
341 tf->hob_nsect = inb(ioaddr->nsect_addr);
342 tf->hob_lbal = inb(ioaddr->lbal_addr);
343 tf->hob_lbam = inb(ioaddr->lbam_addr);
344 tf->hob_lbah = inb(ioaddr->lbah_addr);
345 }
346}
347
348/**
349 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
350 * @ap: Port from which input is read
351 * @tf: ATA taskfile register set for storing input
352 *
353 * Reads ATA taskfile registers for currently-selected device
354 * into @tf via MMIO.
355 *
356 * LOCKING:
357 * Inherited from caller.
358 */
359
360static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
361{
362 struct ata_ioports *ioaddr = &ap->ioaddr;
363
Jeff Garzikac19bff2005-10-29 13:58:21 -0400364 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400365 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
367 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
368 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
369 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
370 tf->device = readb((void __iomem *)ioaddr->device_addr);
371
372 if (tf->flags & ATA_TFLAG_LBA48) {
373 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
374 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
375 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
376 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
377 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
378 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
379 }
380}
381
Edward Falk0baab862005-06-02 18:17:13 -0400382
383/**
384 * ata_tf_read - input device's ATA taskfile shadow registers
385 * @ap: Port from which input is read
386 * @tf: ATA taskfile register set for storing input
387 *
388 * Reads ATA taskfile registers for currently-selected device
389 * into @tf.
390 *
391 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
392 * is set, also reads the hob registers.
393 *
394 * May be used as the tf_read() entry in ata_port_operations.
395 *
396 * LOCKING:
397 * Inherited from caller.
398 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
400{
401 if (ap->flags & ATA_FLAG_MMIO)
402 ata_tf_read_mmio(ap, tf);
403 else
404 ata_tf_read_pio(ap, tf);
405}
406
407/**
408 * ata_check_status_pio - Read device status reg & clear interrupt
409 * @ap: port where the device is
410 *
411 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400412 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * from this device
414 *
415 * LOCKING:
416 * Inherited from caller.
417 */
418static u8 ata_check_status_pio(struct ata_port *ap)
419{
420 return inb(ap->ioaddr.status_addr);
421}
422
423/**
424 * ata_check_status_mmio - Read device status reg & clear interrupt
425 * @ap: port where the device is
426 *
427 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400428 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * from this device
430 *
431 * LOCKING:
432 * Inherited from caller.
433 */
434static u8 ata_check_status_mmio(struct ata_port *ap)
435{
436 return readb((void __iomem *) ap->ioaddr.status_addr);
437}
438
Edward Falk0baab862005-06-02 18:17:13 -0400439
440/**
441 * ata_check_status - Read device status reg & clear interrupt
442 * @ap: port where the device is
443 *
444 * Reads ATA taskfile status register for currently-selected device
445 * and return its value. This also clears pending interrupts
446 * from this device
447 *
448 * May be used as the check_status() entry in ata_port_operations.
449 *
450 * LOCKING:
451 * Inherited from caller.
452 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453u8 ata_check_status(struct ata_port *ap)
454{
455 if (ap->flags & ATA_FLAG_MMIO)
456 return ata_check_status_mmio(ap);
457 return ata_check_status_pio(ap);
458}
459
Edward Falk0baab862005-06-02 18:17:13 -0400460
461/**
462 * ata_altstatus - Read device alternate status reg
463 * @ap: port where the device is
464 *
465 * Reads ATA taskfile alternate status register for
466 * currently-selected device and return its value.
467 *
468 * Note: may NOT be used as the check_altstatus() entry in
469 * ata_port_operations.
470 *
471 * LOCKING:
472 * Inherited from caller.
473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474u8 ata_altstatus(struct ata_port *ap)
475{
476 if (ap->ops->check_altstatus)
477 return ap->ops->check_altstatus(ap);
478
479 if (ap->flags & ATA_FLAG_MMIO)
480 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
481 return inb(ap->ioaddr.altstatus_addr);
482}
483
Edward Falk0baab862005-06-02 18:17:13 -0400484
485/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
487 * @tf: Taskfile to convert
488 * @fis: Buffer into which data will output
489 * @pmp: Port multiplier port
490 *
491 * Converts a standard ATA taskfile to a Serial ATA
492 * FIS structure (Register - Host to Device).
493 *
494 * LOCKING:
495 * Inherited from caller.
496 */
497
Jeff Garzik057ace52005-10-22 14:27:05 -0400498void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 fis[0] = 0x27; /* Register - Host to Device FIS */
501 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
502 bit 7 indicates Command FIS */
503 fis[2] = tf->command;
504 fis[3] = tf->feature;
505
506 fis[4] = tf->lbal;
507 fis[5] = tf->lbam;
508 fis[6] = tf->lbah;
509 fis[7] = tf->device;
510
511 fis[8] = tf->hob_lbal;
512 fis[9] = tf->hob_lbam;
513 fis[10] = tf->hob_lbah;
514 fis[11] = tf->hob_feature;
515
516 fis[12] = tf->nsect;
517 fis[13] = tf->hob_nsect;
518 fis[14] = 0;
519 fis[15] = tf->ctl;
520
521 fis[16] = 0;
522 fis[17] = 0;
523 fis[18] = 0;
524 fis[19] = 0;
525}
526
527/**
528 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
529 * @fis: Buffer from which data will be input
530 * @tf: Taskfile to output
531 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500532 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 *
534 * LOCKING:
535 * Inherited from caller.
536 */
537
Jeff Garzik057ace52005-10-22 14:27:05 -0400538void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 tf->command = fis[2]; /* status */
541 tf->feature = fis[3]; /* error */
542
543 tf->lbal = fis[4];
544 tf->lbam = fis[5];
545 tf->lbah = fis[6];
546 tf->device = fis[7];
547
548 tf->hob_lbal = fis[8];
549 tf->hob_lbam = fis[9];
550 tf->hob_lbah = fis[10];
551
552 tf->nsect = fis[12];
553 tf->hob_nsect = fis[13];
554}
555
Albert Lee8cbd6df2005-10-12 15:06:27 +0800556static const u8 ata_rw_cmds[] = {
557 /* pio multi */
558 ATA_CMD_READ_MULTI,
559 ATA_CMD_WRITE_MULTI,
560 ATA_CMD_READ_MULTI_EXT,
561 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100562 0,
563 0,
564 0,
565 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800566 /* pio */
567 ATA_CMD_PIO_READ,
568 ATA_CMD_PIO_WRITE,
569 ATA_CMD_PIO_READ_EXT,
570 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100571 0,
572 0,
573 0,
574 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800575 /* dma */
576 ATA_CMD_READ,
577 ATA_CMD_WRITE,
578 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100579 ATA_CMD_WRITE_EXT,
580 0,
581 0,
582 0,
583 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800584};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800587 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
588 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800590 * Examine the device configuration and tf->flags to calculate
591 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 *
593 * LOCKING:
594 * caller.
595 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100596int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800598 struct ata_taskfile *tf = &qc->tf;
599 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100600 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100602 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800603
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100604 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800605 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
606 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Albert Lee8cbd6df2005-10-12 15:06:27 +0800608 if (dev->flags & ATA_DFLAG_PIO) {
609 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100610 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000611 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
612 /* Unable to use DMA due to host limitation */
613 tf->protocol = ATA_PROT_PIO;
Albert Leeaef9d532006-02-08 16:37:43 +0800614 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800615 } else {
616 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100617 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100620 cmd = ata_rw_cmds[index + fua + lba48 + write];
621 if (cmd) {
622 tf->command = cmd;
623 return 0;
624 }
625 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100628static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 "UDMA/16",
630 "UDMA/25",
631 "UDMA/33",
632 "UDMA/44",
633 "UDMA/66",
634 "UDMA/100",
635 "UDMA/133",
636 "UDMA7",
637 "MWDMA0",
638 "MWDMA1",
639 "MWDMA2",
640 "PIO0",
641 "PIO1",
642 "PIO2",
643 "PIO3",
644 "PIO4",
645};
646
647/**
648 * ata_udma_string - convert UDMA bit offset to string
649 * @mask: mask of bits supported; only highest bit counts.
650 *
651 * Determine string which represents the highest speed
652 * (highest bit in @udma_mask).
653 *
654 * LOCKING:
655 * None.
656 *
657 * RETURNS:
658 * Constant C string representing highest speed listed in
659 * @udma_mask, or the constant C string "<n/a>".
660 */
661
662static const char *ata_mode_string(unsigned int mask)
663{
664 int i;
665
666 for (i = 7; i >= 0; i--)
667 if (mask & (1 << i))
668 goto out;
669 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
670 if (mask & (1 << i))
671 goto out;
672 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
673 if (mask & (1 << i))
674 goto out;
675
676 return "<n/a>";
677
678out:
679 return xfer_mode_str[i];
680}
681
682/**
683 * ata_pio_devchk - PATA device presence detection
684 * @ap: ATA channel to examine
685 * @device: Device to examine (starting at zero)
686 *
687 * This technique was originally described in
688 * Hale Landis's ATADRVR (www.ata-atapi.com), and
689 * later found its way into the ATA/ATAPI spec.
690 *
691 * Write a pattern to the ATA shadow registers,
692 * and if a device is present, it will respond by
693 * correctly storing and echoing back the
694 * ATA shadow register contents.
695 *
696 * LOCKING:
697 * caller.
698 */
699
700static unsigned int ata_pio_devchk(struct ata_port *ap,
701 unsigned int device)
702{
703 struct ata_ioports *ioaddr = &ap->ioaddr;
704 u8 nsect, lbal;
705
706 ap->ops->dev_select(ap, device);
707
708 outb(0x55, ioaddr->nsect_addr);
709 outb(0xaa, ioaddr->lbal_addr);
710
711 outb(0xaa, ioaddr->nsect_addr);
712 outb(0x55, ioaddr->lbal_addr);
713
714 outb(0x55, ioaddr->nsect_addr);
715 outb(0xaa, ioaddr->lbal_addr);
716
717 nsect = inb(ioaddr->nsect_addr);
718 lbal = inb(ioaddr->lbal_addr);
719
720 if ((nsect == 0x55) && (lbal == 0xaa))
721 return 1; /* we found a device */
722
723 return 0; /* nothing found */
724}
725
726/**
727 * ata_mmio_devchk - PATA device presence detection
728 * @ap: ATA channel to examine
729 * @device: Device to examine (starting at zero)
730 *
731 * This technique was originally described in
732 * Hale Landis's ATADRVR (www.ata-atapi.com), and
733 * later found its way into the ATA/ATAPI spec.
734 *
735 * Write a pattern to the ATA shadow registers,
736 * and if a device is present, it will respond by
737 * correctly storing and echoing back the
738 * ATA shadow register contents.
739 *
740 * LOCKING:
741 * caller.
742 */
743
744static unsigned int ata_mmio_devchk(struct ata_port *ap,
745 unsigned int device)
746{
747 struct ata_ioports *ioaddr = &ap->ioaddr;
748 u8 nsect, lbal;
749
750 ap->ops->dev_select(ap, device);
751
752 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
753 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
754
755 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
756 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
757
758 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
759 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
760
761 nsect = readb((void __iomem *) ioaddr->nsect_addr);
762 lbal = readb((void __iomem *) ioaddr->lbal_addr);
763
764 if ((nsect == 0x55) && (lbal == 0xaa))
765 return 1; /* we found a device */
766
767 return 0; /* nothing found */
768}
769
770/**
771 * ata_devchk - PATA device presence detection
772 * @ap: ATA channel to examine
773 * @device: Device to examine (starting at zero)
774 *
775 * Dispatch ATA device presence detection, depending
776 * on whether we are using PIO or MMIO to talk to the
777 * ATA shadow registers.
778 *
779 * LOCKING:
780 * caller.
781 */
782
783static unsigned int ata_devchk(struct ata_port *ap,
784 unsigned int device)
785{
786 if (ap->flags & ATA_FLAG_MMIO)
787 return ata_mmio_devchk(ap, device);
788 return ata_pio_devchk(ap, device);
789}
790
791/**
792 * ata_dev_classify - determine device type based on ATA-spec signature
793 * @tf: ATA taskfile register set for device to be identified
794 *
795 * Determine from taskfile register contents whether a device is
796 * ATA or ATAPI, as per "Signature and persistence" section
797 * of ATA/PI spec (volume 1, sect 5.14).
798 *
799 * LOCKING:
800 * None.
801 *
802 * RETURNS:
803 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
804 * the event of failure.
805 */
806
Jeff Garzik057ace52005-10-22 14:27:05 -0400807unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 /* Apple's open source Darwin code hints that some devices only
810 * put a proper signature into the LBA mid/high registers,
811 * So, we only check those. It's sufficient for uniqueness.
812 */
813
814 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
815 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
816 DPRINTK("found ATA device by sig\n");
817 return ATA_DEV_ATA;
818 }
819
820 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
821 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
822 DPRINTK("found ATAPI device by sig\n");
823 return ATA_DEV_ATAPI;
824 }
825
826 DPRINTK("unknown device\n");
827 return ATA_DEV_UNKNOWN;
828}
829
830/**
831 * ata_dev_try_classify - Parse returned ATA device signature
832 * @ap: ATA channel to examine
833 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900834 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 *
836 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
837 * an ATA/ATAPI-defined set of values is placed in the ATA
838 * shadow registers, indicating the results of device detection
839 * and diagnostics.
840 *
841 * Select the ATA device, and read the values from the ATA shadow
842 * registers. Then parse according to the Error register value,
843 * and the spec-defined values examined by ata_dev_classify().
844 *
845 * LOCKING:
846 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900847 *
848 * RETURNS:
849 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
851
Tejun Heob4dc7622006-01-24 17:05:22 +0900852static unsigned int
853ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct ata_taskfile tf;
856 unsigned int class;
857 u8 err;
858
859 ap->ops->dev_select(ap, device);
860
861 memset(&tf, 0, sizeof(tf));
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400864 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900865 if (r_err)
866 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 /* see if device passed diags */
869 if (err == 1)
870 /* do nothing */ ;
871 else if ((device == 0) && (err == 0x81))
872 /* do nothing */ ;
873 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900874 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Tejun Heob4dc7622006-01-24 17:05:22 +0900876 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900880 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900882 return ATA_DEV_NONE;
883 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886/**
887 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
888 * @id: IDENTIFY DEVICE results we will examine
889 * @s: string into which data is output
890 * @ofs: offset into identify device page
891 * @len: length of string to return. must be an even number.
892 *
893 * The strings in the IDENTIFY DEVICE page are broken up into
894 * 16-bit chunks. Run through the string, and output each
895 * 8-bit chunk linearly, regardless of platform.
896 *
897 * LOCKING:
898 * caller.
899 */
900
Jeff Garzik057ace52005-10-22 14:27:05 -0400901void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 unsigned int ofs, unsigned int len)
903{
904 unsigned int c;
905
906 while (len > 0) {
907 c = id[ofs] >> 8;
908 *s = c;
909 s++;
910
911 c = id[ofs] & 0xff;
912 *s = c;
913 s++;
914
915 ofs++;
916 len -= 2;
917 }
918}
919
Edward Falk0baab862005-06-02 18:17:13 -0400920
921/**
922 * ata_noop_dev_select - Select device 0/1 on ATA bus
923 * @ap: ATA channel to manipulate
924 * @device: ATA device (numbered from zero) to select
925 *
926 * This function performs no actual function.
927 *
928 * May be used as the dev_select() entry in ata_port_operations.
929 *
930 * LOCKING:
931 * caller.
932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
934{
935}
936
Edward Falk0baab862005-06-02 18:17:13 -0400937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/**
939 * ata_std_dev_select - Select device 0/1 on ATA bus
940 * @ap: ATA channel to manipulate
941 * @device: ATA device (numbered from zero) to select
942 *
943 * Use the method defined in the ATA specification to
944 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400945 * ATA channel. Works with both PIO and MMIO.
946 *
947 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 *
949 * LOCKING:
950 * caller.
951 */
952
953void ata_std_dev_select (struct ata_port *ap, unsigned int device)
954{
955 u8 tmp;
956
957 if (device == 0)
958 tmp = ATA_DEVICE_OBS;
959 else
960 tmp = ATA_DEVICE_OBS | ATA_DEV1;
961
962 if (ap->flags & ATA_FLAG_MMIO) {
963 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
964 } else {
965 outb(tmp, ap->ioaddr.device_addr);
966 }
967 ata_pause(ap); /* needed; also flushes, for mmio */
968}
969
970/**
971 * ata_dev_select - Select device 0/1 on ATA bus
972 * @ap: ATA channel to manipulate
973 * @device: ATA device (numbered from zero) to select
974 * @wait: non-zero to wait for Status register BSY bit to clear
975 * @can_sleep: non-zero if context allows sleeping
976 *
977 * Use the method defined in the ATA specification to
978 * make either device 0, or device 1, active on the
979 * ATA channel.
980 *
981 * This is a high-level version of ata_std_dev_select(),
982 * which additionally provides the services of inserting
983 * the proper pauses and status polling, where needed.
984 *
985 * LOCKING:
986 * caller.
987 */
988
989void ata_dev_select(struct ata_port *ap, unsigned int device,
990 unsigned int wait, unsigned int can_sleep)
991{
992 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
993 ap->id, device, wait);
994
995 if (wait)
996 ata_wait_idle(ap);
997
998 ap->ops->dev_select(ap, device);
999
1000 if (wait) {
1001 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1002 msleep(150);
1003 ata_wait_idle(ap);
1004 }
1005}
1006
1007/**
1008 * ata_dump_id - IDENTIFY DEVICE info debugging output
1009 * @dev: Device whose IDENTIFY DEVICE page we will dump
1010 *
1011 * Dump selected 16-bit words from a detected device's
1012 * IDENTIFY PAGE page.
1013 *
1014 * LOCKING:
1015 * caller.
1016 */
1017
Jeff Garzik057ace52005-10-22 14:27:05 -04001018static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 DPRINTK("49==0x%04x "
1021 "53==0x%04x "
1022 "63==0x%04x "
1023 "64==0x%04x "
1024 "75==0x%04x \n",
1025 dev->id[49],
1026 dev->id[53],
1027 dev->id[63],
1028 dev->id[64],
1029 dev->id[75]);
1030 DPRINTK("80==0x%04x "
1031 "81==0x%04x "
1032 "82==0x%04x "
1033 "83==0x%04x "
1034 "84==0x%04x \n",
1035 dev->id[80],
1036 dev->id[81],
1037 dev->id[82],
1038 dev->id[83],
1039 dev->id[84]);
1040 DPRINTK("88==0x%04x "
1041 "93==0x%04x\n",
1042 dev->id[88],
1043 dev->id[93]);
1044}
1045
Alan Cox11e29e22005-10-21 18:46:32 -04001046/*
1047 * Compute the PIO modes available for this device. This is not as
1048 * trivial as it seems if we must consider early devices correctly.
1049 *
1050 * FIXME: pre IDE drive timing (do we care ?).
1051 */
1052
Jeff Garzik057ace52005-10-22 14:27:05 -04001053static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001054{
1055 u16 modes;
1056
Alan Coxffa29452006-01-09 17:14:40 +00001057 /* Usual case. Word 53 indicates word 64 is valid */
1058 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -04001059 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1060 modes <<= 3;
1061 modes |= 0x7;
1062 return modes;
1063 }
1064
Alan Coxffa29452006-01-09 17:14:40 +00001065 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
1066 number for the maximum. Turn it into a mask and return it */
1067 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -04001068 return modes;
Alan Coxffa29452006-01-09 17:14:40 +00001069 /* But wait.. there's more. Design your standards by committee and
1070 you too can get a free iordy field to process. However its the
1071 speeds not the modes that are supported... Note drivers using the
1072 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -04001073}
1074
Tejun Heo95064372006-01-23 13:09:37 +09001075static inline void
Tejun Heo95064372006-01-23 13:09:37 +09001076ata_queue_pio_task(struct ata_port *ap)
1077{
1078 queue_work(ata_wq, &ap->pio_task);
1079}
1080
1081static inline void
1082ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
1083{
1084 queue_delayed_work(ata_wq, &ap->pio_task, delay);
1085}
1086
Tejun Heo77853bf2006-01-23 13:09:36 +09001087void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Jeff Garzik64f043d2005-11-17 10:50:01 -05001088{
Tejun Heo77853bf2006-01-23 13:09:36 +09001089 struct completion *waiting = qc->private_data;
Jeff Garzik64f043d2005-11-17 10:50:01 -05001090
Tejun Heo77853bf2006-01-23 13:09:36 +09001091 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001092 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001093}
Jeff Garzik64f043d2005-11-17 10:50:01 -05001094
Tejun Heoa2a7a662005-12-13 14:48:31 +09001095/**
1096 * ata_exec_internal - execute libata internal command
1097 * @ap: Port to which the command is sent
1098 * @dev: Device to which the command is sent
1099 * @tf: Taskfile registers for the command and the result
1100 * @dma_dir: Data tranfer direction of the command
1101 * @buf: Data buffer of the command
1102 * @buflen: Length of data buffer
1103 *
1104 * Executes libata internal command with timeout. @tf contains
1105 * command on entry and result on return. Timeout and error
1106 * conditions are reported via return value. No recovery action
1107 * is taken after a command times out. It's caller's duty to
1108 * clean up after timeout.
1109 *
1110 * LOCKING:
1111 * None. Should be called with kernel context, might sleep.
1112 */
1113
1114static unsigned
1115ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1116 struct ata_taskfile *tf,
1117 int dma_dir, void *buf, unsigned int buflen)
1118{
1119 u8 command = tf->command;
1120 struct ata_queued_cmd *qc;
1121 DECLARE_COMPLETION(wait);
1122 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001123 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001124
1125 spin_lock_irqsave(&ap->host_set->lock, flags);
1126
1127 qc = ata_qc_new_init(ap, dev);
1128 BUG_ON(qc == NULL);
1129
1130 qc->tf = *tf;
1131 qc->dma_dir = dma_dir;
1132 if (dma_dir != DMA_NONE) {
1133 ata_sg_init_one(qc, buf, buflen);
1134 qc->nsect = buflen / ATA_SECT_SIZE;
Jeff Garzik64f043d2005-11-17 10:50:01 -05001135 }
1136
Tejun Heo77853bf2006-01-23 13:09:36 +09001137 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001138 qc->complete_fn = ata_qc_complete_internal;
1139
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09001140 qc->err_mask = ata_qc_issue(qc);
1141 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +09001142 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001143
1144 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1145
1146 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1147 spin_lock_irqsave(&ap->host_set->lock, flags);
1148
1149 /* We're racing with irq here. If we lose, the
1150 * following test prevents us from completing the qc
1151 * again. If completion irq occurs after here but
1152 * before the caller cleans up, it will result in a
1153 * spurious interrupt. We can live with that.
1154 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001155 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001156 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001157 ata_qc_complete(qc);
1158 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1159 ap->id, command);
1160 }
1161
1162 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1163 }
1164
Tejun Heo77853bf2006-01-23 13:09:36 +09001165 *tf = qc->tf;
1166 err_mask = qc->err_mask;
1167
1168 ata_qc_free(qc);
1169
1170 return err_mask;
Jeff Garzik64f043d2005-11-17 10:50:01 -05001171}
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001174 * ata_pio_need_iordy - check if iordy needed
1175 * @adev: ATA device
1176 *
1177 * Check if the current speed of the device requires IORDY. Used
1178 * by various controllers for chip configuration.
1179 */
1180
1181unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1182{
1183 int pio;
1184 int speed = adev->pio_mode - XFER_PIO_0;
1185
1186 if (speed < 2)
1187 return 0;
1188 if (speed > 2)
1189 return 1;
1190
1191 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1192
1193 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1194 pio = adev->id[ATA_ID_EIDE_PIO];
1195 /* Is the speed faster than the drive allows non IORDY ? */
1196 if (pio) {
1197 /* This is cycle times not frequency - watch the logic! */
1198 if (pio > 240) /* PIO2 is 240nS per cycle */
1199 return 1;
1200 return 0;
1201 }
1202 }
1203 return 0;
1204}
1205
1206/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1208 * @ap: port on which device we wish to probe resides
1209 * @device: device bus address, starting at zero
1210 *
1211 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1212 * command, and read back the 512-byte device information page.
1213 * The device information page is fed to us via the standard
1214 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1215 * using standard PIO-IN paths)
1216 *
1217 * After reading the device information page, we use several
1218 * bits of information from it to initialize data structures
1219 * that will be used during the lifetime of the ata_device.
1220 * Other data from the info page is used to disqualify certain
1221 * older ATA devices we do not wish to support.
1222 *
1223 * LOCKING:
1224 * Inherited from caller. Some functions called by this function
1225 * obtain the host_set lock.
1226 */
1227
1228static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1229{
1230 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001231 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 u16 tmp;
1233 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001235 struct ata_taskfile tf;
1236 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int rc;
1238
1239 if (!ata_dev_present(dev)) {
1240 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1241 ap->id, device);
1242 return;
1243 }
1244
1245 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1246 using_edd = 0;
1247 else
1248 using_edd = 1;
1249
1250 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1251
1252 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1253 dev->class == ATA_DEV_NONE);
1254
1255 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001258 ata_tf_init(ap, &tf, device);
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001261 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 DPRINTK("do ATA identify\n");
1263 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001264 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 DPRINTK("do ATAPI identify\n");
1266 }
1267
Tejun Heoa0123702005-12-13 14:49:31 +09001268 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Tejun Heoa0123702005-12-13 14:49:31 +09001270 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1271 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Tejun Heoa0123702005-12-13 14:49:31 +09001273 if (err_mask) {
1274 if (err_mask & ~AC_ERR_DEV)
1275 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /*
1278 * arg! EDD works for all test cases, but seems to return
1279 * the ATA signature for some ATAPI devices. Until the
1280 * reason for this is found and fixed, we fix up the mess
1281 * here. If IDENTIFY DEVICE returns command aborted
1282 * (as ATAPI devices do), then we issue an
1283 * IDENTIFY PACKET DEVICE.
1284 *
1285 * ATA software reset (SRST, the default) does not appear
1286 * to have this problem.
1287 */
Albert Lee7c398332005-11-09 13:03:30 +08001288 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001289 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (err & ATA_ABORTED) {
1291 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto retry;
1293 }
1294 }
1295 goto err_out;
1296 }
1297
1298 swap_buf_le16(dev->id, ATA_ID_WORDS);
1299
1300 /* print device capabilities */
1301 printk(KERN_DEBUG "ata%u: dev %u cfg "
1302 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1303 ap->id, device, dev->id[49],
1304 dev->id[82], dev->id[83], dev->id[84],
1305 dev->id[85], dev->id[86], dev->id[87],
1306 dev->id[88]);
1307
1308 /*
1309 * common ATA, ATAPI feature tests
1310 */
1311
Albert Lee8bf62ec2005-05-12 15:29:42 -04001312 /* we require DMA support (bits 8 of word 49) */
1313 if (!ata_id_has_dma(dev->id)) {
1314 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 goto err_out_nosup;
1316 }
1317
1318 /* quick-n-dirty find max transfer mode; for printk only */
1319 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1320 if (!xfer_modes)
1321 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001322 if (!xfer_modes)
1323 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 ata_dump_id(dev);
1326
1327 /* ATA-specific feature tests */
1328 if (dev->class == ATA_DEV_ATA) {
1329 if (!ata_id_is_ata(dev->id)) /* sanity check */
1330 goto err_out_nosup;
1331
Albert Lee8bf62ec2005-05-12 15:29:42 -04001332 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001334 for (major_version = 14; major_version >= 1; major_version--)
1335 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 break;
1337
Albert Lee8bf62ec2005-05-12 15:29:42 -04001338 /*
1339 * The exact sequence expected by certain pre-ATA4 drives is:
1340 * SRST RESET
1341 * IDENTIFY
1342 * INITIALIZE DEVICE PARAMETERS
1343 * anything else..
1344 * Some drives were very specific about that exact sequence.
1345 */
Albert Lee59a10b12005-10-12 15:09:42 +08001346 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001347 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Albert Lee59a10b12005-10-12 15:09:42 +08001349 /* current CHS translation info (id[53-58]) might be
1350 * changed. reread the identify device info.
1351 */
1352 ata_dev_reread_id(ap, dev);
1353 }
1354
Albert Lee8bf62ec2005-05-12 15:29:42 -04001355 if (ata_id_has_lba(dev->id)) {
1356 dev->flags |= ATA_DFLAG_LBA;
1357
1358 if (ata_id_has_lba48(dev->id)) {
1359 dev->flags |= ATA_DFLAG_LBA48;
1360 dev->n_sectors = ata_id_u64(dev->id, 100);
1361 } else {
1362 dev->n_sectors = ata_id_u32(dev->id, 60);
1363 }
1364
1365 /* print device info to dmesg */
1366 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1367 ap->id, device,
1368 major_version,
1369 ata_mode_string(xfer_modes),
1370 (unsigned long long)dev->n_sectors,
1371 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1372 } else {
1373 /* CHS */
1374
1375 /* Default translation */
1376 dev->cylinders = dev->id[1];
1377 dev->heads = dev->id[3];
1378 dev->sectors = dev->id[6];
1379 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1380
1381 if (ata_id_current_chs_valid(dev->id)) {
1382 /* Current CHS translation is valid. */
1383 dev->cylinders = dev->id[54];
1384 dev->heads = dev->id[55];
1385 dev->sectors = dev->id[56];
1386
1387 dev->n_sectors = ata_id_u32(dev->id, 57);
1388 }
1389
1390 /* print device info to dmesg */
1391 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1392 ap->id, device,
1393 major_version,
1394 ata_mode_string(xfer_modes),
1395 (unsigned long long)dev->n_sectors,
1396 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
Albert Lee07f6f7d2005-11-01 19:33:20 +08001400 if (dev->id[59] & 0x100) {
1401 dev->multi_count = dev->id[59] & 0xff;
1402 DPRINTK("ata%u: dev %u multi count %u\n",
1403 ap->id, device, dev->multi_count);
1404 }
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
1409 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001410 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (ata_id_is_ata(dev->id)) /* sanity check */
1412 goto err_out_nosup;
1413
1414 rc = atapi_cdb_len(dev->id);
1415 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1416 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1417 goto err_out_nosup;
1418 }
1419 ap->cdb_len = (unsigned int) rc;
1420 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1421
Albert Lee312f7da2005-09-27 17:38:03 +08001422 if (ata_id_cdb_intr(dev->id))
1423 dev->flags |= ATA_DFLAG_CDB_INTR;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 /* print device info to dmesg */
1426 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1427 ap->id, device,
1428 ata_mode_string(xfer_modes));
1429 }
1430
1431 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1432 return;
1433
1434err_out_nosup:
1435 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1436 ap->id, device);
1437err_out:
1438 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1439 DPRINTK("EXIT, err\n");
1440}
1441
Brad Campbell6f2f3812005-05-12 15:07:47 -04001442
Jeff Garzik057ace52005-10-22 14:27:05 -04001443static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001444{
1445 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1446}
1447
1448/**
1449 * ata_dev_config - Run device specific handlers and check for
1450 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001451 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001452 * @i: Device
1453 *
1454 * LOCKING:
1455 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001456
Brad Campbell6f2f3812005-05-12 15:07:47 -04001457void ata_dev_config(struct ata_port *ap, unsigned int i)
1458{
1459 /* limit bridge transfers to udma5, 200 sectors */
1460 if (ata_dev_knobble(ap)) {
1461 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1462 ap->id, ap->device->devno);
1463 ap->udma_mask &= ATA_UDMA5;
1464 ap->host->max_sectors = ATA_MAX_SECTORS;
1465 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001466 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001467 }
1468
1469 if (ap->ops->dev_config)
1470 ap->ops->dev_config(ap, &ap->device[i]);
1471}
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473/**
1474 * ata_bus_probe - Reset and probe ATA bus
1475 * @ap: Bus to probe
1476 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001477 * Master ATA bus probing function. Initiates a hardware-dependent
1478 * bus reset, then attempts to identify any devices found on
1479 * the bus.
1480 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001482 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 *
1484 * RETURNS:
1485 * Zero on success, non-zero on error.
1486 */
1487
1488static int ata_bus_probe(struct ata_port *ap)
1489{
1490 unsigned int i, found = 0;
1491
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001492 if (ap->ops->probe_reset) {
1493 unsigned int classes[ATA_MAX_DEVICES];
1494 int rc;
1495
1496 ata_port_probe(ap);
1497
1498 rc = ap->ops->probe_reset(ap, classes);
1499 if (rc == 0) {
1500 for (i = 0; i < ATA_MAX_DEVICES; i++)
1501 ap->device[i].class = classes[i];
1502 } else {
1503 printk(KERN_ERR "ata%u: probe reset failed, "
1504 "disabling port\n", ap->id);
1505 ata_port_disable(ap);
1506 }
1507 } else
1508 ap->ops->phy_reset(ap);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1511 goto err_out;
1512
1513 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1514 ata_dev_identify(ap, i);
1515 if (ata_dev_present(&ap->device[i])) {
1516 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001517 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 }
1520
1521 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1522 goto err_out_disable;
1523
1524 ata_set_mode(ap);
1525 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1526 goto err_out_disable;
1527
1528 return 0;
1529
1530err_out_disable:
1531 ap->ops->port_disable(ap);
1532err_out:
1533 return -1;
1534}
1535
1536/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001537 * ata_port_probe - Mark port as enabled
1538 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001540 * Modify @ap data structure such that the system
1541 * thinks that the entire port is enabled.
1542 *
1543 * LOCKING: host_set lock, or some other form of
1544 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 */
1546
1547void ata_port_probe(struct ata_port *ap)
1548{
1549 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1550}
1551
1552/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001553 * sata_print_link_status - Print SATA link status
1554 * @ap: SATA port to printk link status about
1555 *
1556 * This function prints link speed and status of a SATA link.
1557 *
1558 * LOCKING:
1559 * None.
1560 */
1561static void sata_print_link_status(struct ata_port *ap)
1562{
1563 u32 sstatus, tmp;
1564 const char *speed;
1565
1566 if (!ap->ops->scr_read)
1567 return;
1568
1569 sstatus = scr_read(ap, SCR_STATUS);
1570
1571 if (sata_dev_present(ap)) {
1572 tmp = (sstatus >> 4) & 0xf;
1573 if (tmp & (1 << 0))
1574 speed = "1.5";
1575 else if (tmp & (1 << 1))
1576 speed = "3.0";
1577 else
1578 speed = "<unknown>";
1579 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1580 ap->id, speed, sstatus);
1581 } else {
1582 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1583 ap->id, sstatus);
1584 }
1585}
1586
1587/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001588 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1589 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001591 * This function issues commands to standard SATA Sxxx
1592 * PHY registers, to wake up the phy (and device), and
1593 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 *
1595 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001596 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 *
1598 */
1599void __sata_phy_reset(struct ata_port *ap)
1600{
1601 u32 sstatus;
1602 unsigned long timeout = jiffies + (HZ * 5);
1603
1604 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001605 /* issue phy wake/reset */
1606 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001607 /* Couldn't find anything in SATA I/II specs, but
1608 * AHCI-1.1 10.4.2 says at least 1 ms. */
1609 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
Brett Russcdcca892005-03-28 15:10:27 -05001611 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 /* wait for phy to become ready, if necessary */
1614 do {
1615 msleep(200);
1616 sstatus = scr_read(ap, SCR_STATUS);
1617 if ((sstatus & 0xf) != 1)
1618 break;
1619 } while (time_before(jiffies, timeout));
1620
Tejun Heo3be680b2005-12-19 22:35:02 +09001621 /* print link status */
1622 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001623
Tejun Heo3be680b2005-12-19 22:35:02 +09001624 /* TODO: phy layer with polling, timeouts, etc. */
1625 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001627 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1631 return;
1632
1633 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1634 ata_port_disable(ap);
1635 return;
1636 }
1637
1638 ap->cbl = ATA_CBL_SATA;
1639}
1640
1641/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001642 * sata_phy_reset - Reset SATA bus.
1643 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001645 * This function resets the SATA bus, and then probes
1646 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 *
1648 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001649 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 *
1651 */
1652void sata_phy_reset(struct ata_port *ap)
1653{
1654 __sata_phy_reset(ap);
1655 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1656 return;
1657 ata_bus_reset(ap);
1658}
1659
1660/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001661 * ata_port_disable - Disable port.
1662 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001664 * Modify @ap data structure such that the system
1665 * thinks that the entire port is disabled, and should
1666 * never attempt to probe or communicate with devices
1667 * on this port.
1668 *
1669 * LOCKING: host_set lock, or some other form of
1670 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 */
1672
1673void ata_port_disable(struct ata_port *ap)
1674{
1675 ap->device[0].class = ATA_DEV_NONE;
1676 ap->device[1].class = ATA_DEV_NONE;
1677 ap->flags |= ATA_FLAG_PORT_DISABLED;
1678}
1679
Alan Cox452503f2005-10-21 19:01:32 -04001680/*
1681 * This mode timing computation functionality is ported over from
1682 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1683 */
1684/*
1685 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1686 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1687 * for PIO 5, which is a nonstandard extension and UDMA6, which
1688 * is currently supported only by Maxtor drives.
1689 */
1690
1691static const struct ata_timing ata_timing[] = {
1692
1693 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1694 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1695 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1696 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1697
1698 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1699 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1700 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1701
1702/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1703
1704 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1705 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1706 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1707
1708 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1709 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1710 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1711
1712/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1713 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1714 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1715
1716 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1717 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1718 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1719
1720/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1721
1722 { 0xFF }
1723};
1724
1725#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1726#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1727
1728static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1729{
1730 q->setup = EZ(t->setup * 1000, T);
1731 q->act8b = EZ(t->act8b * 1000, T);
1732 q->rec8b = EZ(t->rec8b * 1000, T);
1733 q->cyc8b = EZ(t->cyc8b * 1000, T);
1734 q->active = EZ(t->active * 1000, T);
1735 q->recover = EZ(t->recover * 1000, T);
1736 q->cycle = EZ(t->cycle * 1000, T);
1737 q->udma = EZ(t->udma * 1000, UT);
1738}
1739
1740void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1741 struct ata_timing *m, unsigned int what)
1742{
1743 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1744 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1745 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1746 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1747 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1748 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1749 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1750 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1751}
1752
1753static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1754{
1755 const struct ata_timing *t;
1756
1757 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001758 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001759 return NULL;
1760 return t;
1761}
1762
1763int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1764 struct ata_timing *t, int T, int UT)
1765{
1766 const struct ata_timing *s;
1767 struct ata_timing p;
1768
1769 /*
1770 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001771 */
Alan Cox452503f2005-10-21 19:01:32 -04001772
1773 if (!(s = ata_timing_find_mode(speed)))
1774 return -EINVAL;
1775
Albert Lee75b1f2f2005-11-16 17:06:18 +08001776 memcpy(t, s, sizeof(*s));
1777
Alan Cox452503f2005-10-21 19:01:32 -04001778 /*
1779 * If the drive is an EIDE drive, it can tell us it needs extended
1780 * PIO/MW_DMA cycle timing.
1781 */
1782
1783 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1784 memset(&p, 0, sizeof(p));
1785 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1786 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1787 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1788 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1789 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1790 }
1791 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1792 }
1793
1794 /*
1795 * Convert the timing to bus clock counts.
1796 */
1797
Albert Lee75b1f2f2005-11-16 17:06:18 +08001798 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001799
1800 /*
1801 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1802 * and some other commands. We have to ensure that the DMA cycle timing is
1803 * slower/equal than the fastest PIO timing.
1804 */
1805
1806 if (speed > XFER_PIO_4) {
1807 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1808 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1809 }
1810
1811 /*
1812 * Lenghten active & recovery time so that cycle time is correct.
1813 */
1814
1815 if (t->act8b + t->rec8b < t->cyc8b) {
1816 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1817 t->rec8b = t->cyc8b - t->act8b;
1818 }
1819
1820 if (t->active + t->recover < t->cycle) {
1821 t->active += (t->cycle - (t->active + t->recover)) / 2;
1822 t->recover = t->cycle - t->active;
1823 }
1824
1825 return 0;
1826}
1827
Jeff Garzik057ace52005-10-22 14:27:05 -04001828static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 unsigned int shift;
1830 u8 base;
1831} xfer_mode_classes[] = {
1832 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1833 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1834 { ATA_SHIFT_PIO, XFER_PIO_0 },
1835};
1836
Arjan van de Ven858119e2006-01-14 13:20:43 -08001837static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 int i;
1840
1841 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1842 if (xfer_mode_classes[i].shift == shift)
1843 return xfer_mode_classes[i].base;
1844
1845 return 0xff;
1846}
1847
1848static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1849{
1850 int ofs, idx;
1851 u8 base;
1852
1853 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1854 return;
1855
1856 if (dev->xfer_shift == ATA_SHIFT_PIO)
1857 dev->flags |= ATA_DFLAG_PIO;
1858
1859 ata_dev_set_xfermode(ap, dev);
1860
1861 base = base_from_shift(dev->xfer_shift);
1862 ofs = dev->xfer_mode - base;
1863 idx = ofs + dev->xfer_shift;
1864 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1865
1866 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1867 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1868
1869 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1870 ap->id, dev->devno, xfer_mode_str[idx]);
1871}
1872
1873static int ata_host_set_pio(struct ata_port *ap)
1874{
1875 unsigned int mask;
1876 int x, i;
1877 u8 base, xfer_mode;
1878
1879 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1880 x = fgb(mask);
1881 if (x < 0) {
1882 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1883 return -1;
1884 }
1885
1886 base = base_from_shift(ATA_SHIFT_PIO);
1887 xfer_mode = base + x;
1888
1889 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1890 (int)base, (int)xfer_mode, mask, x);
1891
1892 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1893 struct ata_device *dev = &ap->device[i];
1894 if (ata_dev_present(dev)) {
1895 dev->pio_mode = xfer_mode;
1896 dev->xfer_mode = xfer_mode;
1897 dev->xfer_shift = ATA_SHIFT_PIO;
1898 if (ap->ops->set_piomode)
1899 ap->ops->set_piomode(ap, dev);
1900 }
1901 }
1902
1903 return 0;
1904}
1905
1906static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1907 unsigned int xfer_shift)
1908{
1909 int i;
1910
1911 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1912 struct ata_device *dev = &ap->device[i];
1913 if (ata_dev_present(dev)) {
1914 dev->dma_mode = xfer_mode;
1915 dev->xfer_mode = xfer_mode;
1916 dev->xfer_shift = xfer_shift;
1917 if (ap->ops->set_dmamode)
1918 ap->ops->set_dmamode(ap, dev);
1919 }
1920 }
1921}
1922
1923/**
1924 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1925 * @ap: port on which timings will be programmed
1926 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001927 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1928 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001930 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 *
1932 */
1933static void ata_set_mode(struct ata_port *ap)
1934{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001935 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 u8 xfer_mode;
1937 int rc;
1938
1939 /* step 1: always set host PIO timings */
1940 rc = ata_host_set_pio(ap);
1941 if (rc)
1942 goto err_out;
1943
1944 /* step 2: choose the best data xfer mode */
1945 xfer_mode = xfer_shift = 0;
1946 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1947 if (rc)
1948 goto err_out;
1949
1950 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1951 if (xfer_shift != ATA_SHIFT_PIO)
1952 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1953
1954 /* step 4: update devices' xfer mode */
1955 ata_dev_set_mode(ap, &ap->device[0]);
1956 ata_dev_set_mode(ap, &ap->device[1]);
1957
1958 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1959 return;
1960
1961 if (ap->ops->post_set_mode)
1962 ap->ops->post_set_mode(ap);
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return;
1965
1966err_out:
1967 ata_port_disable(ap);
1968}
1969
1970/**
1971 * ata_busy_sleep - sleep until BSY clears, or timeout
1972 * @ap: port containing status register to be polled
1973 * @tmout_pat: impatience timeout
1974 * @tmout: overall timeout
1975 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001976 * Sleep until ATA Status register bit BSY clears,
1977 * or a timeout occurs.
1978 *
1979 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 *
1981 */
1982
Tejun Heo6f8b9952006-01-24 17:05:21 +09001983unsigned int ata_busy_sleep (struct ata_port *ap,
1984 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 unsigned long timer_start, timeout;
1987 u8 status;
1988
1989 status = ata_busy_wait(ap, ATA_BUSY, 300);
1990 timer_start = jiffies;
1991 timeout = timer_start + tmout_pat;
1992 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1993 msleep(50);
1994 status = ata_busy_wait(ap, ATA_BUSY, 3);
1995 }
1996
1997 if (status & ATA_BUSY)
1998 printk(KERN_WARNING "ata%u is slow to respond, "
1999 "please be patient\n", ap->id);
2000
2001 timeout = timer_start + tmout;
2002 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2003 msleep(50);
2004 status = ata_chk_status(ap);
2005 }
2006
2007 if (status & ATA_BUSY) {
2008 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
2009 ap->id, tmout / HZ);
2010 return 1;
2011 }
2012
2013 return 0;
2014}
2015
2016static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2017{
2018 struct ata_ioports *ioaddr = &ap->ioaddr;
2019 unsigned int dev0 = devmask & (1 << 0);
2020 unsigned int dev1 = devmask & (1 << 1);
2021 unsigned long timeout;
2022
2023 /* if device 0 was found in ata_devchk, wait for its
2024 * BSY bit to clear
2025 */
2026 if (dev0)
2027 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2028
2029 /* if device 1 was found in ata_devchk, wait for
2030 * register access, then wait for BSY to clear
2031 */
2032 timeout = jiffies + ATA_TMOUT_BOOT;
2033 while (dev1) {
2034 u8 nsect, lbal;
2035
2036 ap->ops->dev_select(ap, 1);
2037 if (ap->flags & ATA_FLAG_MMIO) {
2038 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2039 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2040 } else {
2041 nsect = inb(ioaddr->nsect_addr);
2042 lbal = inb(ioaddr->lbal_addr);
2043 }
2044 if ((nsect == 1) && (lbal == 1))
2045 break;
2046 if (time_after(jiffies, timeout)) {
2047 dev1 = 0;
2048 break;
2049 }
2050 msleep(50); /* give drive a breather */
2051 }
2052 if (dev1)
2053 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2054
2055 /* is all this really necessary? */
2056 ap->ops->dev_select(ap, 0);
2057 if (dev1)
2058 ap->ops->dev_select(ap, 1);
2059 if (dev0)
2060 ap->ops->dev_select(ap, 0);
2061}
2062
2063/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002064 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2065 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002067 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2068 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 *
2070 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002071 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05002072 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 *
2074 */
2075
2076static unsigned int ata_bus_edd(struct ata_port *ap)
2077{
2078 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05002079 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 /* set up execute-device-diag (bus reset) taskfile */
2082 /* also, take interrupts to a known state (disabled) */
2083 DPRINTK("execute-device-diag\n");
2084 ata_tf_init(ap, &tf, 0);
2085 tf.ctl |= ATA_NIEN;
2086 tf.command = ATA_CMD_EDD;
2087 tf.protocol = ATA_PROT_NODATA;
2088
2089 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002090 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002092 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 /* spec says at least 2ms. but who knows with those
2095 * crazy ATAPI devices...
2096 */
2097 msleep(150);
2098
2099 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2100}
2101
2102static unsigned int ata_bus_softreset(struct ata_port *ap,
2103 unsigned int devmask)
2104{
2105 struct ata_ioports *ioaddr = &ap->ioaddr;
2106
2107 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2108
2109 /* software reset. causes dev0 to be selected */
2110 if (ap->flags & ATA_FLAG_MMIO) {
2111 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2112 udelay(20); /* FIXME: flush */
2113 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2114 udelay(20); /* FIXME: flush */
2115 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2116 } else {
2117 outb(ap->ctl, ioaddr->ctl_addr);
2118 udelay(10);
2119 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2120 udelay(10);
2121 outb(ap->ctl, ioaddr->ctl_addr);
2122 }
2123
2124 /* spec mandates ">= 2ms" before checking status.
2125 * We wait 150ms, because that was the magic delay used for
2126 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2127 * between when the ATA command register is written, and then
2128 * status is checked. Because waiting for "a while" before
2129 * checking status is fine, post SRST, we perform this magic
2130 * delay here as well.
2131 */
2132 msleep(150);
2133
2134 ata_bus_post_reset(ap, devmask);
2135
2136 return 0;
2137}
2138
2139/**
2140 * ata_bus_reset - reset host port and associated ATA channel
2141 * @ap: port to reset
2142 *
2143 * This is typically the first time we actually start issuing
2144 * commands to the ATA channel. We wait for BSY to clear, then
2145 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2146 * result. Determine what devices, if any, are on the channel
2147 * by looking at the device 0/1 error register. Look at the signature
2148 * stored in each device's taskfile registers, to determine if
2149 * the device is ATA or ATAPI.
2150 *
2151 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002152 * PCI/etc. bus probe sem.
2153 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 *
2155 * SIDE EFFECTS:
2156 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2157 */
2158
2159void ata_bus_reset(struct ata_port *ap)
2160{
2161 struct ata_ioports *ioaddr = &ap->ioaddr;
2162 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2163 u8 err;
2164 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2165
2166 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2167
2168 /* determine if device 0/1 are present */
2169 if (ap->flags & ATA_FLAG_SATA_RESET)
2170 dev0 = 1;
2171 else {
2172 dev0 = ata_devchk(ap, 0);
2173 if (slave_possible)
2174 dev1 = ata_devchk(ap, 1);
2175 }
2176
2177 if (dev0)
2178 devmask |= (1 << 0);
2179 if (dev1)
2180 devmask |= (1 << 1);
2181
2182 /* select device 0 again */
2183 ap->ops->dev_select(ap, 0);
2184
2185 /* issue bus reset */
2186 if (ap->flags & ATA_FLAG_SRST)
2187 rc = ata_bus_softreset(ap, devmask);
2188 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2189 /* set up device control */
2190 if (ap->flags & ATA_FLAG_MMIO)
2191 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2192 else
2193 outb(ap->ctl, ioaddr->ctl_addr);
2194 rc = ata_bus_edd(ap);
2195 }
2196
2197 if (rc)
2198 goto err_out;
2199
2200 /*
2201 * determine by signature whether we have ATA or ATAPI devices
2202 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002203 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002205 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 /* re-enable interrupts */
2208 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2209 ata_irq_on(ap);
2210
2211 /* is double-select really necessary? */
2212 if (ap->device[1].class != ATA_DEV_NONE)
2213 ap->ops->dev_select(ap, 1);
2214 if (ap->device[0].class != ATA_DEV_NONE)
2215 ap->ops->dev_select(ap, 0);
2216
2217 /* if no devices were detected, disable this port */
2218 if ((ap->device[0].class == ATA_DEV_NONE) &&
2219 (ap->device[1].class == ATA_DEV_NONE))
2220 goto err_out;
2221
2222 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2223 /* set up device control for ATA_FLAG_SATA_RESET */
2224 if (ap->flags & ATA_FLAG_MMIO)
2225 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2226 else
2227 outb(ap->ctl, ioaddr->ctl_addr);
2228 }
2229
2230 DPRINTK("EXIT\n");
2231 return;
2232
2233err_out:
2234 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2235 ap->ops->port_disable(ap);
2236
2237 DPRINTK("EXIT\n");
2238}
2239
Jeff Garzik057ace52005-10-22 14:27:05 -04002240static void ata_pr_blacklisted(const struct ata_port *ap,
2241 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2244 ap->id, dev->devno);
2245}
2246
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002247static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 "WDC AC11000H",
2249 "WDC AC22100H",
2250 "WDC AC32500H",
2251 "WDC AC33100H",
2252 "WDC AC31600H",
2253 "WDC AC32100H",
2254 "WDC AC23200L",
2255 "Compaq CRD-8241B",
2256 "CRD-8400B",
2257 "CRD-8480B",
2258 "CRD-8482B",
2259 "CRD-84",
2260 "SanDisk SDP3B",
2261 "SanDisk SDP3B-64",
2262 "SANYO CD-ROM CRD",
2263 "HITACHI CDR-8",
2264 "HITACHI CDR-8335",
2265 "HITACHI CDR-8435",
2266 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002267 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 "CD-532E-A",
2269 "E-IDE CD-ROM CR-840",
2270 "CD-ROM Drive/F5A",
2271 "WPI CDD-820",
2272 "SAMSUNG CD-ROM SC-148C",
2273 "SAMSUNG CD-ROM SC",
2274 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2276 "_NEC DV5800A",
2277};
2278
Jeff Garzik057ace52005-10-22 14:27:05 -04002279static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 unsigned char model_num[40];
2282 char *s;
2283 unsigned int len;
2284 int i;
2285
2286 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2287 sizeof(model_num));
2288 s = &model_num[0];
2289 len = strnlen(s, sizeof(model_num));
2290
2291 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2292 while ((len > 0) && (s[len - 1] == ' ')) {
2293 len--;
2294 s[len] = 0;
2295 }
2296
2297 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2298 if (!strncmp(ata_dma_blacklist[i], s, len))
2299 return 1;
2300
2301 return 0;
2302}
2303
Jeff Garzik057ace52005-10-22 14:27:05 -04002304static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
Jeff Garzik057ace52005-10-22 14:27:05 -04002306 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 unsigned int mask;
2308
2309 master = &ap->device[0];
2310 slave = &ap->device[1];
2311
2312 assert (ata_dev_present(master) || ata_dev_present(slave));
2313
2314 if (shift == ATA_SHIFT_UDMA) {
2315 mask = ap->udma_mask;
2316 if (ata_dev_present(master)) {
2317 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002318 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 mask = 0;
2320 ata_pr_blacklisted(ap, master);
2321 }
2322 }
2323 if (ata_dev_present(slave)) {
2324 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002325 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 mask = 0;
2327 ata_pr_blacklisted(ap, slave);
2328 }
2329 }
2330 }
2331 else if (shift == ATA_SHIFT_MWDMA) {
2332 mask = ap->mwdma_mask;
2333 if (ata_dev_present(master)) {
2334 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002335 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 mask = 0;
2337 ata_pr_blacklisted(ap, master);
2338 }
2339 }
2340 if (ata_dev_present(slave)) {
2341 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002342 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 mask = 0;
2344 ata_pr_blacklisted(ap, slave);
2345 }
2346 }
2347 }
2348 else if (shift == ATA_SHIFT_PIO) {
2349 mask = ap->pio_mask;
2350 if (ata_dev_present(master)) {
2351 /* spec doesn't return explicit support for
2352 * PIO0-2, so we fake it
2353 */
2354 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2355 tmp_mode <<= 3;
2356 tmp_mode |= 0x7;
2357 mask &= tmp_mode;
2358 }
2359 if (ata_dev_present(slave)) {
2360 /* spec doesn't return explicit support for
2361 * PIO0-2, so we fake it
2362 */
2363 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2364 tmp_mode <<= 3;
2365 tmp_mode |= 0x7;
2366 mask &= tmp_mode;
2367 }
2368 }
2369 else {
2370 mask = 0xffffffff; /* shut up compiler warning */
2371 BUG();
2372 }
2373
2374 return mask;
2375}
2376
2377/* find greatest bit */
2378static int fgb(u32 bitmap)
2379{
2380 unsigned int i;
2381 int x = -1;
2382
2383 for (i = 0; i < 32; i++)
2384 if (bitmap & (1 << i))
2385 x = i;
2386
2387 return x;
2388}
2389
2390/**
2391 * ata_choose_xfer_mode - attempt to find best transfer mode
2392 * @ap: Port for which an xfer mode will be selected
2393 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2394 * @xfer_shift_out: (output) bit shift that selects this mode
2395 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002396 * Based on host and device capabilities, determine the
2397 * maximum transfer mode that is amenable to all.
2398 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002400 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 *
2402 * RETURNS:
2403 * Zero on success, negative on error.
2404 */
2405
Jeff Garzik057ace52005-10-22 14:27:05 -04002406static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 u8 *xfer_mode_out,
2408 unsigned int *xfer_shift_out)
2409{
2410 unsigned int mask, shift;
2411 int x, i;
2412
2413 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2414 shift = xfer_mode_classes[i].shift;
2415 mask = ata_get_mode_mask(ap, shift);
2416
2417 x = fgb(mask);
2418 if (x >= 0) {
2419 *xfer_mode_out = xfer_mode_classes[i].base + x;
2420 *xfer_shift_out = shift;
2421 return 0;
2422 }
2423 }
2424
2425 return -1;
2426}
2427
2428/**
2429 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2430 * @ap: Port associated with device @dev
2431 * @dev: Device to which command will be sent
2432 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002433 * Issue SET FEATURES - XFER MODE command to device @dev
2434 * on port @ap.
2435 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002437 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 */
2439
2440static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2441{
Tejun Heoa0123702005-12-13 14:49:31 +09002442 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 /* set up set-features taskfile */
2445 DPRINTK("set features - xfer mode\n");
2446
Tejun Heoa0123702005-12-13 14:49:31 +09002447 ata_tf_init(ap, &tf, dev->devno);
2448 tf.command = ATA_CMD_SET_FEATURES;
2449 tf.feature = SETFEATURES_XFER;
2450 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2451 tf.protocol = ATA_PROT_NODATA;
2452 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Tejun Heoa0123702005-12-13 14:49:31 +09002454 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2455 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2456 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 DPRINTK("EXIT\n");
2461}
2462
2463/**
Albert Lee59a10b12005-10-12 15:09:42 +08002464 * ata_dev_reread_id - Reread the device identify device info
2465 * @ap: port where the device is
2466 * @dev: device to reread the identify device info
2467 *
2468 * LOCKING:
2469 */
2470
2471static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2472{
Tejun Heoa0123702005-12-13 14:49:31 +09002473 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002474
Tejun Heoa0123702005-12-13 14:49:31 +09002475 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002476
2477 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002478 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002479 DPRINTK("do ATA identify\n");
2480 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002481 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002482 DPRINTK("do ATAPI identify\n");
2483 }
2484
Tejun Heoa0123702005-12-13 14:49:31 +09002485 tf.flags |= ATA_TFLAG_DEVICE;
2486 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002487
Tejun Heoa0123702005-12-13 14:49:31 +09002488 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2489 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002490 goto err_out;
2491
Albert Lee59a10b12005-10-12 15:09:42 +08002492 swap_buf_le16(dev->id, ATA_ID_WORDS);
2493
2494 ata_dump_id(dev);
2495
2496 DPRINTK("EXIT\n");
2497
2498 return;
2499err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002500 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002501 ata_port_disable(ap);
2502}
2503
2504/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002505 * ata_dev_init_params - Issue INIT DEV PARAMS command
2506 * @ap: Port associated with device @dev
2507 * @dev: Device to which command will be sent
2508 *
2509 * LOCKING:
2510 */
2511
2512static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2513{
Tejun Heoa0123702005-12-13 14:49:31 +09002514 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002515 u16 sectors = dev->id[6];
2516 u16 heads = dev->id[3];
2517
2518 /* Number of sectors per track 1-255. Number of heads 1-16 */
2519 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2520 return;
2521
2522 /* set up init dev params taskfile */
2523 DPRINTK("init dev params \n");
2524
Tejun Heoa0123702005-12-13 14:49:31 +09002525 ata_tf_init(ap, &tf, dev->devno);
2526 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2527 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2528 tf.protocol = ATA_PROT_NODATA;
2529 tf.nsect = sectors;
2530 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002531
Tejun Heoa0123702005-12-13 14:49:31 +09002532 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2533 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2534 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002535 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002536 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002537
2538 DPRINTK("EXIT\n");
2539}
2540
2541/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002542 * ata_sg_clean - Unmap DMA memory associated with command
2543 * @qc: Command containing DMA memory to be released
2544 *
2545 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 *
2547 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002548 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 */
2550
2551static void ata_sg_clean(struct ata_queued_cmd *qc)
2552{
2553 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002554 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002556 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
2558 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2559 assert(sg != NULL);
2560
2561 if (qc->flags & ATA_QCFLAG_SINGLE)
2562 assert(qc->n_elem == 1);
2563
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002564 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002566 /* if we padded the buffer out to 32-bit bound, and data
2567 * xfer direction is from-device, we must copy from the
2568 * pad buffer back into the supplied buffer
2569 */
2570 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2571 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2572
2573 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002574 if (qc->n_elem)
2575 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002576 /* restore last sg */
2577 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2578 if (pad_buf) {
2579 struct scatterlist *psg = &qc->pad_sgent;
2580 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2581 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002582 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002583 }
2584 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002585 if (sg_dma_len(&sg[0]) > 0)
2586 dma_unmap_single(ap->host_set->dev,
2587 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2588 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002589 /* restore sg */
2590 sg->length += qc->pad_len;
2591 if (pad_buf)
2592 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2593 pad_buf, qc->pad_len);
2594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002597 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
2600/**
2601 * ata_fill_sg - Fill PCI IDE PRD table
2602 * @qc: Metadata associated with taskfile to be transferred
2603 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002604 * Fill PCI IDE PRD (scatter-gather) table with segments
2605 * associated with the current disk command.
2606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002608 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 *
2610 */
2611static void ata_fill_sg(struct ata_queued_cmd *qc)
2612{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002614 struct scatterlist *sg;
2615 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002617 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 assert(qc->n_elem > 0);
2619
2620 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002621 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 u32 addr, offset;
2623 u32 sg_len, len;
2624
2625 /* determine if physical DMA addr spans 64K boundary.
2626 * Note h/w doesn't support 64-bit, so we unconditionally
2627 * truncate dma_addr_t to u32.
2628 */
2629 addr = (u32) sg_dma_address(sg);
2630 sg_len = sg_dma_len(sg);
2631
2632 while (sg_len) {
2633 offset = addr & 0xffff;
2634 len = sg_len;
2635 if ((offset + sg_len) > 0x10000)
2636 len = 0x10000 - offset;
2637
2638 ap->prd[idx].addr = cpu_to_le32(addr);
2639 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2640 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2641
2642 idx++;
2643 sg_len -= len;
2644 addr += len;
2645 }
2646 }
2647
2648 if (idx)
2649 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2650}
2651/**
2652 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2653 * @qc: Metadata associated with taskfile to check
2654 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002655 * Allow low-level driver to filter ATA PACKET commands, returning
2656 * a status indicating whether or not it is OK to use DMA for the
2657 * supplied PACKET command.
2658 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002660 * spin_lock_irqsave(host_set lock)
2661 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 * RETURNS: 0 when ATAPI DMA can be used
2663 * nonzero otherwise
2664 */
2665int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2666{
2667 struct ata_port *ap = qc->ap;
2668 int rc = 0; /* Assume ATAPI DMA is OK by default */
2669
2670 if (ap->ops->check_atapi_dma)
2671 rc = ap->ops->check_atapi_dma(qc);
2672
2673 return rc;
2674}
2675/**
2676 * ata_qc_prep - Prepare taskfile for submission
2677 * @qc: Metadata associated with taskfile to be prepared
2678 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002679 * Prepare ATA taskfile for submission.
2680 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 * LOCKING:
2682 * spin_lock_irqsave(host_set lock)
2683 */
2684void ata_qc_prep(struct ata_queued_cmd *qc)
2685{
2686 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2687 return;
2688
2689 ata_fill_sg(qc);
2690}
2691
Jeff Garzik0cba6322005-05-30 19:49:12 -04002692/**
2693 * ata_sg_init_one - Associate command with memory buffer
2694 * @qc: Command to be associated
2695 * @buf: Memory buffer
2696 * @buflen: Length of memory buffer, in bytes.
2697 *
2698 * Initialize the data-related elements of queued_cmd @qc
2699 * to point to a single memory buffer, @buf of byte length @buflen.
2700 *
2701 * LOCKING:
2702 * spin_lock_irqsave(host_set lock)
2703 */
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2706{
2707 struct scatterlist *sg;
2708
2709 qc->flags |= ATA_QCFLAG_SINGLE;
2710
2711 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002712 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002714 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 qc->buf_virt = buf;
2716
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002717 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002718 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
Jeff Garzik0cba6322005-05-30 19:49:12 -04002721/**
2722 * ata_sg_init - Associate command with scatter-gather table.
2723 * @qc: Command to be associated
2724 * @sg: Scatter-gather table.
2725 * @n_elem: Number of elements in s/g table.
2726 *
2727 * Initialize the data-related elements of queued_cmd @qc
2728 * to point to a scatter-gather table @sg, containing @n_elem
2729 * elements.
2730 *
2731 * LOCKING:
2732 * spin_lock_irqsave(host_set lock)
2733 */
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2736 unsigned int n_elem)
2737{
2738 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002739 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002741 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
2744/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002745 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2746 * @qc: Command with memory buffer to be mapped.
2747 *
2748 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 *
2750 * LOCKING:
2751 * spin_lock_irqsave(host_set lock)
2752 *
2753 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002754 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 */
2756
2757static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2758{
2759 struct ata_port *ap = qc->ap;
2760 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002761 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 dma_addr_t dma_address;
2763
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002764 /* we must lengthen transfers to end on a 32-bit boundary */
2765 qc->pad_len = sg->length & 3;
2766 if (qc->pad_len) {
2767 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2768 struct scatterlist *psg = &qc->pad_sgent;
2769
2770 assert(qc->dev->class == ATA_DEV_ATAPI);
2771
2772 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2773
2774 if (qc->tf.flags & ATA_TFLAG_WRITE)
2775 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2776 qc->pad_len);
2777
2778 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2779 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2780 /* trim sg */
2781 sg->length -= qc->pad_len;
2782
2783 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2784 sg->length, qc->pad_len);
2785 }
2786
Jeff Garzike1410f22005-11-14 14:06:26 -05002787 if (!sg->length) {
2788 sg_dma_address(sg) = 0;
2789 goto skip_map;
2790 }
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002793 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002794 if (dma_mapping_error(dma_address)) {
2795 /* restore sg */
2796 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002801skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002802 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2805 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2806
2807 return 0;
2808}
2809
2810/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002811 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2812 * @qc: Command with scatter-gather table to be mapped.
2813 *
2814 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 *
2816 * LOCKING:
2817 * spin_lock_irqsave(host_set lock)
2818 *
2819 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002820 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 *
2822 */
2823
2824static int ata_sg_setup(struct ata_queued_cmd *qc)
2825{
2826 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002827 struct scatterlist *sg = qc->__sg;
2828 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002829 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 VPRINTK("ENTER, ata%u\n", ap->id);
2832 assert(qc->flags & ATA_QCFLAG_SG);
2833
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002834 /* we must lengthen transfers to end on a 32-bit boundary */
2835 qc->pad_len = lsg->length & 3;
2836 if (qc->pad_len) {
2837 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2838 struct scatterlist *psg = &qc->pad_sgent;
2839 unsigned int offset;
2840
2841 assert(qc->dev->class == ATA_DEV_ATAPI);
2842
2843 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2844
2845 /*
2846 * psg->page/offset are used to copy to-be-written
2847 * data in this function or read data in ata_sg_clean.
2848 */
2849 offset = lsg->offset + lsg->length - qc->pad_len;
2850 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2851 psg->offset = offset_in_page(offset);
2852
2853 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2854 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2855 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002856 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002857 }
2858
2859 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2860 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2861 /* trim last sg */
2862 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002863 if (lsg->length == 0)
2864 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002865
2866 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2867 qc->n_elem - 1, lsg->length, qc->pad_len);
2868 }
2869
Jeff Garzike1410f22005-11-14 14:06:26 -05002870 pre_n_elem = qc->n_elem;
2871 if (trim_sg && pre_n_elem)
2872 pre_n_elem--;
2873
2874 if (!pre_n_elem) {
2875 n_elem = 0;
2876 goto skip_map;
2877 }
2878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002880 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002881 if (n_elem < 1) {
2882 /* restore last sg */
2883 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
2887 DPRINTK("%d sg elements mapped\n", n_elem);
2888
Jeff Garzike1410f22005-11-14 14:06:26 -05002889skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 qc->n_elem = n_elem;
2891
2892 return 0;
2893}
2894
2895/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002896 * ata_poll_qc_complete - turn irq back on and finish qc
2897 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002898 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002899 *
2900 * LOCKING:
2901 * None. (grabs host lock)
2902 */
2903
Albert Leea22e2eb2005-12-05 15:38:02 +08002904void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002905{
2906 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002907 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002908
Jeff Garzikb8f61532005-08-25 22:01:20 -04002909 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002910 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002911 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002912 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002913}
2914
2915/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002917 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 *
2919 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002920 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 *
2922 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002923 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 */
2925
2926static unsigned long ata_pio_poll(struct ata_port *ap)
2927{
Albert Leec14b8332005-12-05 15:36:08 +08002928 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002930 unsigned int poll_state = HSM_ST_UNKNOWN;
2931 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Albert Leec14b8332005-12-05 15:36:08 +08002933 qc = ata_qc_from_tag(ap, ap->active_tag);
2934 assert(qc != NULL);
2935
Albert Lee14be71f2005-09-27 17:36:35 +08002936 switch (ap->hsm_task_state) {
2937 case HSM_ST:
2938 case HSM_ST_POLL:
2939 poll_state = HSM_ST_POLL;
2940 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002942 case HSM_ST_LAST:
2943 case HSM_ST_LAST_POLL:
2944 poll_state = HSM_ST_LAST_POLL;
2945 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 break;
2947 default:
2948 BUG();
2949 break;
2950 }
2951
2952 status = ata_chk_status(ap);
2953 if (status & ATA_BUSY) {
2954 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002955 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002956 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return 0;
2958 }
Albert Lee14be71f2005-09-27 17:36:35 +08002959 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return ATA_SHORT_PAUSE;
2961 }
2962
Albert Lee14be71f2005-09-27 17:36:35 +08002963 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return 0;
2965}
2966
2967/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002968 * ata_pio_complete - check if drive is busy or idle
2969 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 *
2971 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002972 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002973 *
2974 * RETURNS:
Albert Leefbcdd802005-11-01 19:30:05 +08002975 * Zero if qc completed.
2976 * Non-zero if has next.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 */
2978
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002979static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
2981 struct ata_queued_cmd *qc;
2982 u8 drv_stat;
2983
2984 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002985 * This is purely heuristic. This is a fast path. Sometimes when
2986 * we enter, BSY will be cleared in a chk-status or two. If not,
2987 * the drive is probably seeking or something. Snooze for a couple
2988 * msecs, then chk-status again. If still busy, fall back to
Albert Lee07f6f7d2005-11-01 19:33:20 +08002989 * HSM_ST_LAST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 */
Albert Leefe79e682005-12-06 11:34:59 +08002991 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2992 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002994 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2995 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002996 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Albert Leefbcdd802005-11-01 19:30:05 +08002998 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 }
3000 }
3001
Albert Leec14b8332005-12-05 15:36:08 +08003002 qc = ata_qc_from_tag(ap, ap->active_tag);
3003 assert(qc != NULL);
3004
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 drv_stat = ata_wait_idle(ap);
3006 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003007 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003008 ap->hsm_task_state = HSM_ST_ERR;
Albert Leefbcdd802005-11-01 19:30:05 +08003009 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
3011
Albert Lee14be71f2005-09-27 17:36:35 +08003012 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Albert Leea22e2eb2005-12-05 15:38:02 +08003014 assert(qc->err_mask == 0);
3015 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003016
3017 /* another command may start at this point */
3018
Albert Leefbcdd802005-11-01 19:30:05 +08003019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
Edward Falk0baab862005-06-02 18:17:13 -04003022
3023/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003024 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04003025 * @buf: Buffer to swap
3026 * @buf_words: Number of 16-bit words in buffer.
3027 *
3028 * Swap halves of 16-bit words if needed to convert from
3029 * little-endian byte order to native cpu byte order, or
3030 * vice-versa.
3031 *
3032 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003033 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035void swap_buf_le16(u16 *buf, unsigned int buf_words)
3036{
3037#ifdef __BIG_ENDIAN
3038 unsigned int i;
3039
3040 for (i = 0; i < buf_words; i++)
3041 buf[i] = le16_to_cpu(buf[i]);
3042#endif /* __BIG_ENDIAN */
3043}
3044
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003045/**
3046 * ata_mmio_data_xfer - Transfer data by MMIO
3047 * @ap: port to read/write
3048 * @buf: data buffer
3049 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003050 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003051 *
3052 * Transfer data from/to the device data register by MMIO.
3053 *
3054 * LOCKING:
3055 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003056 */
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3059 unsigned int buflen, int write_data)
3060{
3061 unsigned int i;
3062 unsigned int words = buflen >> 1;
3063 u16 *buf16 = (u16 *) buf;
3064 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3065
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003066 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 if (write_data) {
3068 for (i = 0; i < words; i++)
3069 writew(le16_to_cpu(buf16[i]), mmio);
3070 } else {
3071 for (i = 0; i < words; i++)
3072 buf16[i] = cpu_to_le16(readw(mmio));
3073 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003074
3075 /* Transfer trailing 1 byte, if any. */
3076 if (unlikely(buflen & 0x01)) {
3077 u16 align_buf[1] = { 0 };
3078 unsigned char *trailing_buf = buf + buflen - 1;
3079
3080 if (write_data) {
3081 memcpy(align_buf, trailing_buf, 1);
3082 writew(le16_to_cpu(align_buf[0]), mmio);
3083 } else {
3084 align_buf[0] = cpu_to_le16(readw(mmio));
3085 memcpy(trailing_buf, align_buf, 1);
3086 }
3087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003090/**
3091 * ata_pio_data_xfer - Transfer data by PIO
3092 * @ap: port to read/write
3093 * @buf: data buffer
3094 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003095 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003096 *
3097 * Transfer data from/to the device data register by PIO.
3098 *
3099 * LOCKING:
3100 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003101 */
3102
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3104 unsigned int buflen, int write_data)
3105{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003106 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003108 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003110 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003112 insw(ap->ioaddr.data_addr, buf, words);
3113
3114 /* Transfer trailing 1 byte, if any. */
3115 if (unlikely(buflen & 0x01)) {
3116 u16 align_buf[1] = { 0 };
3117 unsigned char *trailing_buf = buf + buflen - 1;
3118
3119 if (write_data) {
3120 memcpy(align_buf, trailing_buf, 1);
3121 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3122 } else {
3123 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3124 memcpy(trailing_buf, align_buf, 1);
3125 }
3126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003129/**
3130 * ata_data_xfer - Transfer data from/to the data register.
3131 * @ap: port to read/write
3132 * @buf: data buffer
3133 * @buflen: buffer length
3134 * @do_write: read/write
3135 *
3136 * Transfer data from/to the device data register.
3137 *
3138 * LOCKING:
3139 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003140 */
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3143 unsigned int buflen, int do_write)
3144{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003145 /* Make the crap hardware pay the costs not the good stuff */
3146 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3147 unsigned long flags;
3148 local_irq_save(flags);
3149 if (ap->flags & ATA_FLAG_MMIO)
3150 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3151 else
3152 ata_pio_data_xfer(ap, buf, buflen, do_write);
3153 local_irq_restore(flags);
3154 } else {
3155 if (ap->flags & ATA_FLAG_MMIO)
3156 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3157 else
3158 ata_pio_data_xfer(ap, buf, buflen, do_write);
3159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160}
3161
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003162/**
3163 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3164 * @qc: Command on going
3165 *
3166 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3167 *
3168 * LOCKING:
3169 * Inherited from caller.
3170 */
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172static void ata_pio_sector(struct ata_queued_cmd *qc)
3173{
3174 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003175 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 struct ata_port *ap = qc->ap;
3177 struct page *page;
3178 unsigned int offset;
3179 unsigned char *buf;
3180
3181 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003182 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 page = sg[qc->cursg].page;
3185 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3186
3187 /* get the current page and offset */
3188 page = nth_page(page, (offset >> PAGE_SHIFT));
3189 offset %= PAGE_SIZE;
3190
Albert Lee7282aa42005-10-09 09:46:07 -04003191 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3192
Albert Lee91b8b312005-10-09 09:48:44 -04003193 if (PageHighMem(page)) {
3194 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003195
Albert Lee91b8b312005-10-09 09:48:44 -04003196 local_irq_save(flags);
3197 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003198
Albert Lee91b8b312005-10-09 09:48:44 -04003199 /* do the actual data transfer */
3200 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3201
3202 kunmap_atomic(buf, KM_IRQ0);
3203 local_irq_restore(flags);
3204 } else {
3205 buf = page_address(page);
3206 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3207 }
Albert Lee7282aa42005-10-09 09:46:07 -04003208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 qc->cursect++;
3210 qc->cursg_ofs++;
3211
Albert Lee32529e02005-05-26 03:49:42 -04003212 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 qc->cursg++;
3214 qc->cursg_ofs = 0;
3215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216}
3217
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003218/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003219 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3220 * @qc: Command on going
3221 *
3222 * Transfer one or many ATA_SECT_SIZE of data from/to the
3223 * ATA device for the DRQ request.
3224 *
3225 * LOCKING:
3226 * Inherited from caller.
3227 */
3228
3229static void ata_pio_sectors(struct ata_queued_cmd *qc)
3230{
3231 if (is_multi_taskfile(&qc->tf)) {
3232 /* READ/WRITE MULTIPLE */
3233 unsigned int nsect;
3234
3235 assert(qc->dev->multi_count);
3236
3237 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3238 while (nsect--)
3239 ata_pio_sector(qc);
3240 } else
3241 ata_pio_sector(qc);
3242}
3243
3244/**
Albert Leec71c1852005-10-04 06:03:45 -04003245 * atapi_send_cdb - Write CDB bytes to hardware
3246 * @ap: Port to which ATAPI device is attached.
3247 * @qc: Taskfile currently active
3248 *
3249 * When device has indicated its readiness to accept
3250 * a CDB, this function is called. Send the CDB.
3251 *
3252 * LOCKING:
3253 * caller.
3254 */
3255
3256static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3257{
3258 /* send SCSI cdb */
3259 DPRINTK("send cdb\n");
3260 assert(ap->cdb_len >= 12);
3261
3262 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3263 ata_altstatus(ap); /* flush */
3264
3265 switch (qc->tf.protocol) {
3266 case ATA_PROT_ATAPI:
3267 ap->hsm_task_state = HSM_ST;
3268 break;
3269 case ATA_PROT_ATAPI_NODATA:
3270 ap->hsm_task_state = HSM_ST_LAST;
3271 break;
3272 case ATA_PROT_ATAPI_DMA:
3273 ap->hsm_task_state = HSM_ST_LAST;
3274 /* initiate bmdma */
3275 ap->ops->bmdma_start(qc);
3276 break;
3277 }
3278}
3279
3280/**
Albert Leee27486d2005-11-01 19:24:49 +08003281 * ata_pio_first_block - Write first data block to hardware
3282 * @ap: Port to which ATA/ATAPI device is attached.
Albert Leec71c1852005-10-04 06:03:45 -04003283 *
3284 * When device has indicated its readiness to accept
3285 * the data, this function sends out the CDB or
3286 * the first data block by PIO.
3287 * After this,
3288 * - If polling, ata_pio_task() handles the rest.
3289 * - Otherwise, interrupt handler takes over.
3290 *
3291 * LOCKING:
3292 * Kernel thread context (may sleep)
Albert Leefbcdd802005-11-01 19:30:05 +08003293 *
3294 * RETURNS:
3295 * Zero if irq handler takes over
3296 * Non-zero if has next (polling).
Albert Leec71c1852005-10-04 06:03:45 -04003297 */
3298
Albert Leefbcdd802005-11-01 19:30:05 +08003299static int ata_pio_first_block(struct ata_port *ap)
Albert Leec71c1852005-10-04 06:03:45 -04003300{
Albert Leec71c1852005-10-04 06:03:45 -04003301 struct ata_queued_cmd *qc;
3302 u8 status;
3303 unsigned long flags;
Albert Leefbcdd802005-11-01 19:30:05 +08003304 int has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003305
3306 qc = ata_qc_from_tag(ap, ap->active_tag);
3307 assert(qc != NULL);
3308 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3309
Albert Leefbcdd802005-11-01 19:30:05 +08003310 /* if polling, we will stay in the work queue after sending the data.
3311 * otherwise, interrupt handler takes over after sending the data.
3312 */
3313 has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3314
Albert Leec71c1852005-10-04 06:03:45 -04003315 /* sleep-wait for BSY to clear */
3316 DPRINTK("busy wait\n");
Albert Leefbcdd802005-11-01 19:30:05 +08003317 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
Albert Lee555a8962006-02-08 16:50:29 +08003318 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leefbcdd802005-11-01 19:30:05 +08003319 ap->hsm_task_state = HSM_ST_TMOUT;
Albert Leec71c1852005-10-04 06:03:45 -04003320 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003321 }
Albert Leec71c1852005-10-04 06:03:45 -04003322
3323 /* make sure DRQ is set */
3324 status = ata_chk_status(ap);
Albert Leefbcdd802005-11-01 19:30:05 +08003325 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3326 /* device status error */
Albert Lee555a8962006-02-08 16:50:29 +08003327 qc->err_mask |= AC_ERR_HSM;
Albert Leefbcdd802005-11-01 19:30:05 +08003328 ap->hsm_task_state = HSM_ST_ERR;
Albert Leec71c1852005-10-04 06:03:45 -04003329 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003330 }
Albert Leec71c1852005-10-04 06:03:45 -04003331
3332 /* Send the CDB (atapi) or the first data block (ata pio out).
3333 * During the state transition, interrupt handler shouldn't
3334 * be invoked before the data transfer is complete and
3335 * hsm_task_state is changed. Hence, the following locking.
3336 */
3337 spin_lock_irqsave(&ap->host_set->lock, flags);
3338
3339 if (qc->tf.protocol == ATA_PROT_PIO) {
3340 /* PIO data out protocol.
3341 * send first data block.
3342 */
3343
Albert Lee07f6f7d2005-11-01 19:33:20 +08003344 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3345 * so, the state is changed here before ata_pio_sectors().
Albert Leec71c1852005-10-04 06:03:45 -04003346 */
3347 ap->hsm_task_state = HSM_ST;
Albert Lee07f6f7d2005-11-01 19:33:20 +08003348 ata_pio_sectors(qc);
Albert Leec71c1852005-10-04 06:03:45 -04003349 ata_altstatus(ap); /* flush */
3350 } else
3351 /* send CDB */
3352 atapi_send_cdb(ap, qc);
3353
Albert Leefbcdd802005-11-01 19:30:05 +08003354 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3355
Albert Leec71c1852005-10-04 06:03:45 -04003356 /* if polling, ata_pio_task() handles the rest.
3357 * otherwise, interrupt handler takes over from here.
3358 */
Albert Leefbcdd802005-11-01 19:30:05 +08003359 return has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003360
3361err_out:
Albert Leefbcdd802005-11-01 19:30:05 +08003362 return 1; /* has next */
Albert Leec71c1852005-10-04 06:03:45 -04003363}
3364
3365/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003366 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3367 * @qc: Command on going
3368 * @bytes: number of bytes
3369 *
3370 * Transfer Transfer data from/to the ATAPI device.
3371 *
3372 * LOCKING:
3373 * Inherited from caller.
3374 *
3375 */
3376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3378{
3379 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003380 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 struct ata_port *ap = qc->ap;
3382 struct page *page;
3383 unsigned char *buf;
3384 unsigned int offset, count;
3385
Albert Lee563a6e12005-08-12 14:17:50 +08003386 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003387 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
3389next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003390 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003391 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003392 * The end of qc->sg is reached and the device expects
3393 * more data to transfer. In order not to overrun qc->sg
3394 * and fulfill length specified in the byte count register,
3395 * - for read case, discard trailing data from the device
3396 * - for write case, padding zero data to the device
3397 */
3398 u16 pad_buf[1] = { 0 };
3399 unsigned int words = bytes >> 1;
3400 unsigned int i;
3401
3402 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003403 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003404 ap->id, bytes);
3405
3406 for (i = 0; i < words; i++)
3407 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3408
Albert Lee14be71f2005-09-27 17:36:35 +08003409 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003410 return;
3411 }
3412
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003413 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 page = sg->page;
3416 offset = sg->offset + qc->cursg_ofs;
3417
3418 /* get the current page and offset */
3419 page = nth_page(page, (offset >> PAGE_SHIFT));
3420 offset %= PAGE_SIZE;
3421
Albert Lee6952df02005-06-06 15:56:03 +08003422 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003423 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
3425 /* don't cross page boundaries */
3426 count = min(count, (unsigned int)PAGE_SIZE - offset);
3427
Albert Lee7282aa42005-10-09 09:46:07 -04003428 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3429
Albert Lee91b8b312005-10-09 09:48:44 -04003430 if (PageHighMem(page)) {
3431 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003432
Albert Lee91b8b312005-10-09 09:48:44 -04003433 local_irq_save(flags);
3434 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003435
Albert Lee91b8b312005-10-09 09:48:44 -04003436 /* do the actual data transfer */
3437 ata_data_xfer(ap, buf + offset, count, do_write);
3438
3439 kunmap_atomic(buf, KM_IRQ0);
3440 local_irq_restore(flags);
3441 } else {
3442 buf = page_address(page);
3443 ata_data_xfer(ap, buf + offset, count, do_write);
3444 }
Albert Lee7282aa42005-10-09 09:46:07 -04003445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 bytes -= count;
3447 qc->curbytes += count;
3448 qc->cursg_ofs += count;
3449
Albert Lee32529e02005-05-26 03:49:42 -04003450 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 qc->cursg++;
3452 qc->cursg_ofs = 0;
3453 }
3454
Albert Lee563a6e12005-08-12 14:17:50 +08003455 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003459/**
3460 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3461 * @qc: Command on going
3462 *
3463 * Transfer Transfer data from/to the ATAPI device.
3464 *
3465 * LOCKING:
3466 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003467 */
3468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3470{
3471 struct ata_port *ap = qc->ap;
3472 struct ata_device *dev = qc->dev;
3473 unsigned int ireason, bc_lo, bc_hi, bytes;
3474 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3475
3476 ap->ops->tf_read(ap, &qc->tf);
3477 ireason = qc->tf.nsect;
3478 bc_lo = qc->tf.lbam;
3479 bc_hi = qc->tf.lbah;
3480 bytes = (bc_hi << 8) | bc_lo;
3481
3482 /* shall be cleared to zero, indicating xfer of data */
3483 if (ireason & (1 << 0))
3484 goto err_out;
3485
3486 /* make sure transfer direction matches expected */
3487 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3488 if (do_write != i_write)
3489 goto err_out;
3490
Albert Lee312f7da2005-09-27 17:38:03 +08003491 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 __atapi_pio_bytes(qc, bytes);
3494
3495 return;
3496
3497err_out:
3498 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3499 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003500 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003501 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
3504/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003505 * ata_pio_block - start PIO on a block
3506 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 *
3508 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003509 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 */
3511
3512static void ata_pio_block(struct ata_port *ap)
3513{
3514 struct ata_queued_cmd *qc;
3515 u8 status;
3516
3517 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003518 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 * Sometimes when we enter, BSY will be cleared in
3520 * a chk-status or two. If not, the drive is probably seeking
3521 * or something. Snooze for a couple msecs, then
3522 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003523 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 */
3525 status = ata_busy_wait(ap, ATA_BUSY, 5);
3526 if (status & ATA_BUSY) {
3527 msleep(2);
3528 status = ata_busy_wait(ap, ATA_BUSY, 10);
3529 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003530 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3532 return;
3533 }
3534 }
3535
3536 qc = ata_qc_from_tag(ap, ap->active_tag);
3537 assert(qc != NULL);
3538
Albert Leefe79e682005-12-06 11:34:59 +08003539 /* check error */
3540 if (status & (ATA_ERR | ATA_DF)) {
3541 qc->err_mask |= AC_ERR_DEV;
3542 ap->hsm_task_state = HSM_ST_ERR;
3543 return;
3544 }
3545
3546 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003548 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003550 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 return;
3552 }
3553
3554 atapi_pio_bytes(qc);
3555 } else {
3556 /* handle BSY=0, DRQ=0 as error */
3557 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003558 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003559 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return;
3561 }
3562
Albert Lee07f6f7d2005-11-01 19:33:20 +08003563 ata_pio_sectors(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
Albert Lee467b16d2005-11-01 19:19:01 +08003565
3566 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
3569static void ata_pio_error(struct ata_port *ap)
3570{
3571 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003572
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 qc = ata_qc_from_tag(ap, ap->active_tag);
3574 assert(qc != NULL);
3575
Albert Lee000080c2005-12-26 16:48:00 +08003576 if (qc->tf.command != ATA_CMD_PACKET)
3577 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3578
Albert Lee1c848982005-12-05 15:40:15 +08003579 /* make sure qc->err_mask is available to
3580 * know what's wrong and recover
3581 */
3582 assert(qc->err_mask);
3583
Albert Lee14be71f2005-09-27 17:36:35 +08003584 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Albert Leea22e2eb2005-12-05 15:38:02 +08003586 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
3589static void ata_pio_task(void *_data)
3590{
3591 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003592 unsigned long timeout;
Albert Leefbcdd802005-11-01 19:30:05 +08003593 int has_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003594
3595fsm_start:
3596 timeout = 0;
Albert Leefbcdd802005-11-01 19:30:05 +08003597 has_next = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Albert Lee14be71f2005-09-27 17:36:35 +08003599 switch (ap->hsm_task_state) {
Albert Leee27486d2005-11-01 19:24:49 +08003600 case HSM_ST_FIRST:
Albert Leefbcdd802005-11-01 19:30:05 +08003601 has_next = ata_pio_first_block(ap);
3602 break;
Albert Leee27486d2005-11-01 19:24:49 +08003603
Albert Lee14be71f2005-09-27 17:36:35 +08003604 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 ata_pio_block(ap);
3606 break;
3607
Albert Lee14be71f2005-09-27 17:36:35 +08003608 case HSM_ST_LAST:
Albert Leefbcdd802005-11-01 19:30:05 +08003609 has_next = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 break;
3611
Albert Lee14be71f2005-09-27 17:36:35 +08003612 case HSM_ST_POLL:
3613 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 timeout = ata_pio_poll(ap);
3615 break;
3616
Albert Lee14be71f2005-09-27 17:36:35 +08003617 case HSM_ST_TMOUT:
3618 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 ata_pio_error(ap);
3620 return;
Albert Lee467b16d2005-11-01 19:19:01 +08003621
3622 default:
3623 BUG();
3624 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
3626
3627 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003628 ata_queue_delayed_pio_task(ap, timeout);
Albert Leefbcdd802005-11-01 19:30:05 +08003629 else if (has_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003630 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633/**
3634 * ata_qc_timeout - Handle timeout of queued command
3635 * @qc: Command that timed out
3636 *
3637 * Some part of the kernel (currently, only the SCSI layer)
3638 * has noticed that the active command on port @ap has not
3639 * completed after a specified length of time. Handle this
3640 * condition by disabling DMA (if necessary) and completing
3641 * transactions, with error if necessary.
3642 *
3643 * This also handles the case of the "lost interrupt", where
3644 * for some reason (possibly hardware bug, possibly driver bug)
3645 * an interrupt was not delivered to the driver, even though the
3646 * transaction completed successfully.
3647 *
3648 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003649 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 */
3651
3652static void ata_qc_timeout(struct ata_queued_cmd *qc)
3653{
3654 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003655 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003657 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
3659 DPRINTK("ENTER\n");
3660
Jeff Garzikb8f61532005-08-25 22:01:20 -04003661 spin_lock_irqsave(&host_set->lock, flags);
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 switch (qc->tf.protocol) {
3664
3665 case ATA_PROT_DMA:
3666 case ATA_PROT_ATAPI_DMA:
3667 host_stat = ap->ops->bmdma_status(ap);
3668
3669 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003670 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
3672 /* fall through */
3673
3674 default:
3675 ata_altstatus(ap);
3676 drv_stat = ata_chk_status(ap);
3677
3678 /* ack bmdma irq events */
3679 ap->ops->irq_clear(ap);
3680
3681 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3682 ap->id, qc->tf.command, drv_stat, host_stat);
3683
Albert Lee312f7da2005-09-27 17:38:03 +08003684 ap->hsm_task_state = HSM_ST_IDLE;
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 /* complete taskfile transaction */
Albert Lee555a8962006-02-08 16:50:29 +08003687 qc->err_mask |= AC_ERR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 break;
3689 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003690
3691 spin_unlock_irqrestore(&host_set->lock, flags);
3692
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003693 ata_eh_qc_complete(qc);
3694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 DPRINTK("EXIT\n");
3696}
3697
3698/**
3699 * ata_eng_timeout - Handle timeout of queued command
3700 * @ap: Port on which timed-out command is active
3701 *
3702 * Some part of the kernel (currently, only the SCSI layer)
3703 * has noticed that the active command on port @ap has not
3704 * completed after a specified length of time. Handle this
3705 * condition by disabling DMA (if necessary) and completing
3706 * transactions, with error if necessary.
3707 *
3708 * This also handles the case of the "lost interrupt", where
3709 * for some reason (possibly hardware bug, possibly driver bug)
3710 * an interrupt was not delivered to the driver, even though the
3711 * transaction completed successfully.
3712 *
3713 * LOCKING:
3714 * Inherited from SCSI layer (none, can sleep)
3715 */
3716
3717void ata_eng_timeout(struct ata_port *ap)
3718{
3719 struct ata_queued_cmd *qc;
3720
3721 DPRINTK("ENTER\n");
3722
3723 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003724 if (qc)
3725 ata_qc_timeout(qc);
3726 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3728 ap->id);
3729 goto out;
3730 }
3731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732out:
3733 DPRINTK("EXIT\n");
3734}
3735
3736/**
3737 * ata_qc_new - Request an available ATA command, for queueing
3738 * @ap: Port associated with device @dev
3739 * @dev: Device from whom we request an available command structure
3740 *
3741 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003742 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 */
3744
3745static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3746{
3747 struct ata_queued_cmd *qc = NULL;
3748 unsigned int i;
3749
3750 for (i = 0; i < ATA_MAX_QUEUE; i++)
3751 if (!test_and_set_bit(i, &ap->qactive)) {
3752 qc = ata_qc_from_tag(ap, i);
3753 break;
3754 }
3755
3756 if (qc)
3757 qc->tag = i;
3758
3759 return qc;
3760}
3761
3762/**
3763 * ata_qc_new_init - Request an available ATA command, and initialize it
3764 * @ap: Port associated with device @dev
3765 * @dev: Device from whom we request an available command structure
3766 *
3767 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003768 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 */
3770
3771struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3772 struct ata_device *dev)
3773{
3774 struct ata_queued_cmd *qc;
3775
3776 qc = ata_qc_new(ap);
3777 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 qc->scsicmd = NULL;
3779 qc->ap = ap;
3780 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003782 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 }
3784
3785 return qc;
3786}
3787
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788/**
3789 * ata_qc_free - free unused ata_queued_cmd
3790 * @qc: Command to complete
3791 *
3792 * Designed to free unused ata_queued_cmd object
3793 * in case something prevents using it.
3794 *
3795 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003796 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 */
3798void ata_qc_free(struct ata_queued_cmd *qc)
3799{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003800 struct ata_port *ap = qc->ap;
3801 unsigned int tag;
3802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Tejun Heo4ba946e2006-01-23 13:09:36 +09003805 qc->flags = 0;
3806 tag = qc->tag;
3807 if (likely(ata_tag_valid(tag))) {
3808 if (tag == ap->active_tag)
3809 ap->active_tag = ATA_TAG_POISON;
3810 qc->tag = ATA_TAG_POISON;
3811 clear_bit(tag, &ap->qactive);
3812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813}
3814
3815/**
3816 * ata_qc_complete - Complete an active ATA command
3817 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003818 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003819 *
3820 * Indicate to the mid and upper layers that an ATA
3821 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 *
3823 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003824 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 */
3826
Albert Leea22e2eb2005-12-05 15:38:02 +08003827void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3830 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3831
3832 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3833 ata_sg_clean(qc);
3834
Albert Lee3f3791d2005-08-16 14:25:38 +08003835 /* atapi: mark qc as inactive to prevent the interrupt handler
3836 * from completing the command twice later, before the error handler
3837 * is called. (when rc != 0 and atapi request sense is needed)
3838 */
3839 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3840
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003842 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843}
3844
3845static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3846{
3847 struct ata_port *ap = qc->ap;
3848
3849 switch (qc->tf.protocol) {
3850 case ATA_PROT_DMA:
3851 case ATA_PROT_ATAPI_DMA:
3852 return 1;
3853
3854 case ATA_PROT_ATAPI:
3855 case ATA_PROT_PIO:
3856 case ATA_PROT_PIO_MULT:
3857 if (ap->flags & ATA_FLAG_PIO_DMA)
3858 return 1;
3859
3860 /* fall through */
3861
3862 default:
3863 return 0;
3864 }
3865
3866 /* never reached */
3867}
3868
3869/**
3870 * ata_qc_issue - issue taskfile to device
3871 * @qc: command to issue to device
3872 *
3873 * Prepare an ATA command to submission to device.
3874 * This includes mapping the data into a DMA-able
3875 * area, filling in the S/G table, and finally
3876 * writing the taskfile to hardware, starting the command.
3877 *
3878 * LOCKING:
3879 * spin_lock_irqsave(host_set lock)
3880 *
3881 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003882 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 */
3884
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003885unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
3887 struct ata_port *ap = qc->ap;
3888
3889 if (ata_should_dma_map(qc)) {
3890 if (qc->flags & ATA_QCFLAG_SG) {
3891 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003892 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3894 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003895 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
3897 } else {
3898 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3899 }
3900
3901 ap->ops->qc_prep(qc);
3902
3903 qc->ap->active_tag = qc->tag;
3904 qc->flags |= ATA_QCFLAG_ACTIVE;
3905
3906 return ap->ops->qc_issue(qc);
3907
Tejun Heo8e436af2006-01-23 13:09:36 +09003908sg_err:
3909 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003910 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911}
3912
Edward Falk0baab862005-06-02 18:17:13 -04003913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914/**
3915 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3916 * @qc: command to issue to device
3917 *
3918 * Using various libata functions and hooks, this function
3919 * starts an ATA command. ATA commands are grouped into
3920 * classes called "protocols", and issuing each type of protocol
3921 * is slightly different.
3922 *
Edward Falk0baab862005-06-02 18:17:13 -04003923 * May be used as the qc_issue() entry in ata_port_operations.
3924 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 * LOCKING:
3926 * spin_lock_irqsave(host_set lock)
3927 *
3928 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003929 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 */
3931
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003932unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
3934 struct ata_port *ap = qc->ap;
3935
Albert Leee50362e2005-09-27 17:39:50 +08003936 /* Use polling pio if the LLD doesn't handle
3937 * interrupt driven pio and atapi CDB interrupt.
3938 */
3939 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3940 switch (qc->tf.protocol) {
3941 case ATA_PROT_PIO:
3942 case ATA_PROT_ATAPI:
3943 case ATA_PROT_ATAPI_NODATA:
3944 qc->tf.flags |= ATA_TFLAG_POLLING;
3945 break;
3946 case ATA_PROT_ATAPI_DMA:
3947 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3948 BUG();
3949 break;
3950 default:
3951 break;
3952 }
3953 }
3954
Albert Lee312f7da2005-09-27 17:38:03 +08003955 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 ata_dev_select(ap, qc->dev->devno, 1, 0);
3957
Albert Lee312f7da2005-09-27 17:38:03 +08003958 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 switch (qc->tf.protocol) {
3960 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003961 if (qc->tf.flags & ATA_TFLAG_POLLING)
3962 ata_qc_set_polling(qc);
3963
Jeff Garzike5338252005-10-30 21:37:17 -05003964 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003965 ap->hsm_task_state = HSM_ST_LAST;
3966
3967 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee20ea0792006-02-08 16:48:49 +08003968 ata_queue_pio_task(ap);
Albert Lee312f7da2005-09-27 17:38:03 +08003969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 break;
3971
3972 case ATA_PROT_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003973 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3974
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3976 ap->ops->bmdma_setup(qc); /* set up bmdma */
3977 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003978 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 break;
3980
Albert Lee312f7da2005-09-27 17:38:03 +08003981 case ATA_PROT_PIO:
3982 if (qc->tf.flags & ATA_TFLAG_POLLING)
3983 ata_qc_set_polling(qc);
3984
Jeff Garzike5338252005-10-30 21:37:17 -05003985 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003986
Albert Lee54f00382005-09-30 19:14:19 +08003987 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3988 /* PIO data out protocol */
3989 ap->hsm_task_state = HSM_ST_FIRST;
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05003990 ata_queue_pio_task(ap);
Albert Lee54f00382005-09-30 19:14:19 +08003991
3992 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08003993 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08003994 */
Albert Lee312f7da2005-09-27 17:38:03 +08003995 } else {
Albert Lee54f00382005-09-30 19:14:19 +08003996 /* PIO data in protocol */
3997 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08003998
Albert Lee54f00382005-09-30 19:14:19 +08003999 if (qc->tf.flags & ATA_TFLAG_POLLING)
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004000 ata_queue_pio_task(ap);
Albert Lee312f7da2005-09-27 17:38:03 +08004001
Albert Lee54f00382005-09-30 19:14:19 +08004002 /* if polling, ata_pio_task() handles the rest.
4003 * otherwise, interrupt handler takes over from here.
4004 */
Albert Lee312f7da2005-09-27 17:38:03 +08004005 }
4006
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 break;
4008
4009 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004011 if (qc->tf.flags & ATA_TFLAG_POLLING)
4012 ata_qc_set_polling(qc);
4013
Jeff Garzike5338252005-10-30 21:37:17 -05004014 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004015
Albert Lee312f7da2005-09-27 17:38:03 +08004016 ap->hsm_task_state = HSM_ST_FIRST;
4017
4018 /* send cdb by polling if no cdb interrupt */
4019 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4020 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee20ea0792006-02-08 16:48:49 +08004021 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 break;
4023
4024 case ATA_PROT_ATAPI_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08004025 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
4026
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4028 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004029 ap->hsm_task_state = HSM_ST_FIRST;
4030
4031 /* send cdb by polling if no cdb interrupt */
4032 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee20ea0792006-02-08 16:48:49 +08004033 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 break;
4035
4036 default:
4037 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004038 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 }
4040
4041 return 0;
4042}
4043
4044/**
Edward Falk0baab862005-06-02 18:17:13 -04004045 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 * @qc: Info associated with this ATA transaction.
4047 *
4048 * LOCKING:
4049 * spin_lock_irqsave(host_set lock)
4050 */
4051
4052static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4053{
4054 struct ata_port *ap = qc->ap;
4055 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4056 u8 dmactl;
4057 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4058
4059 /* load PRD table addr. */
4060 mb(); /* make sure PRD table writes are visible to controller */
4061 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4062
4063 /* specify data direction, triple-check start bit is clear */
4064 dmactl = readb(mmio + ATA_DMA_CMD);
4065 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4066 if (!rw)
4067 dmactl |= ATA_DMA_WR;
4068 writeb(dmactl, mmio + ATA_DMA_CMD);
4069
4070 /* issue r/w command */
4071 ap->ops->exec_command(ap, &qc->tf);
4072}
4073
4074/**
Alan Coxb73fc892005-08-26 16:03:19 +01004075 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 * @qc: Info associated with this ATA transaction.
4077 *
4078 * LOCKING:
4079 * spin_lock_irqsave(host_set lock)
4080 */
4081
4082static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4083{
4084 struct ata_port *ap = qc->ap;
4085 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4086 u8 dmactl;
4087
4088 /* start host DMA transaction */
4089 dmactl = readb(mmio + ATA_DMA_CMD);
4090 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4091
4092 /* Strictly, one may wish to issue a readb() here, to
4093 * flush the mmio write. However, control also passes
4094 * to the hardware at this point, and it will interrupt
4095 * us when we are to resume control. So, in effect,
4096 * we don't care when the mmio write flushes.
4097 * Further, a read of the DMA status register _immediately_
4098 * following the write may not be what certain flaky hardware
4099 * is expected, so I think it is best to not add a readb()
4100 * without first all the MMIO ATA cards/mobos.
4101 * Or maybe I'm just being paranoid.
4102 */
4103}
4104
4105/**
4106 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4107 * @qc: Info associated with this ATA transaction.
4108 *
4109 * LOCKING:
4110 * spin_lock_irqsave(host_set lock)
4111 */
4112
4113static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4114{
4115 struct ata_port *ap = qc->ap;
4116 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4117 u8 dmactl;
4118
4119 /* load PRD table addr. */
4120 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4121
4122 /* specify data direction, triple-check start bit is clear */
4123 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4124 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4125 if (!rw)
4126 dmactl |= ATA_DMA_WR;
4127 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4128
4129 /* issue r/w command */
4130 ap->ops->exec_command(ap, &qc->tf);
4131}
4132
4133/**
4134 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4135 * @qc: Info associated with this ATA transaction.
4136 *
4137 * LOCKING:
4138 * spin_lock_irqsave(host_set lock)
4139 */
4140
4141static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4142{
4143 struct ata_port *ap = qc->ap;
4144 u8 dmactl;
4145
4146 /* start host DMA transaction */
4147 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4148 outb(dmactl | ATA_DMA_START,
4149 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4150}
4151
Edward Falk0baab862005-06-02 18:17:13 -04004152
4153/**
4154 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4155 * @qc: Info associated with this ATA transaction.
4156 *
4157 * Writes the ATA_DMA_START flag to the DMA command register.
4158 *
4159 * May be used as the bmdma_start() entry in ata_port_operations.
4160 *
4161 * LOCKING:
4162 * spin_lock_irqsave(host_set lock)
4163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164void ata_bmdma_start(struct ata_queued_cmd *qc)
4165{
4166 if (qc->ap->flags & ATA_FLAG_MMIO)
4167 ata_bmdma_start_mmio(qc);
4168 else
4169 ata_bmdma_start_pio(qc);
4170}
4171
Edward Falk0baab862005-06-02 18:17:13 -04004172
4173/**
4174 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4175 * @qc: Info associated with this ATA transaction.
4176 *
4177 * Writes address of PRD table to device's PRD Table Address
4178 * register, sets the DMA control register, and calls
4179 * ops->exec_command() to start the transfer.
4180 *
4181 * May be used as the bmdma_setup() entry in ata_port_operations.
4182 *
4183 * LOCKING:
4184 * spin_lock_irqsave(host_set lock)
4185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186void ata_bmdma_setup(struct ata_queued_cmd *qc)
4187{
4188 if (qc->ap->flags & ATA_FLAG_MMIO)
4189 ata_bmdma_setup_mmio(qc);
4190 else
4191 ata_bmdma_setup_pio(qc);
4192}
4193
Edward Falk0baab862005-06-02 18:17:13 -04004194
4195/**
4196 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004197 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004198 *
4199 * Clear interrupt and error flags in DMA status register.
4200 *
4201 * May be used as the irq_clear() entry in ata_port_operations.
4202 *
4203 * LOCKING:
4204 * spin_lock_irqsave(host_set lock)
4205 */
4206
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207void ata_bmdma_irq_clear(struct ata_port *ap)
4208{
4209 if (ap->flags & ATA_FLAG_MMIO) {
4210 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4211 writeb(readb(mmio), mmio);
4212 } else {
4213 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4214 outb(inb(addr), addr);
4215 }
4216
4217}
4218
Edward Falk0baab862005-06-02 18:17:13 -04004219
4220/**
4221 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004222 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004223 *
4224 * Read and return BMDMA status register.
4225 *
4226 * May be used as the bmdma_status() entry in ata_port_operations.
4227 *
4228 * LOCKING:
4229 * spin_lock_irqsave(host_set lock)
4230 */
4231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232u8 ata_bmdma_status(struct ata_port *ap)
4233{
4234 u8 host_stat;
4235 if (ap->flags & ATA_FLAG_MMIO) {
4236 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4237 host_stat = readb(mmio + ATA_DMA_STATUS);
4238 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004239 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return host_stat;
4241}
4242
Edward Falk0baab862005-06-02 18:17:13 -04004243
4244/**
4245 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004246 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004247 *
4248 * Clears the ATA_DMA_START flag in the dma control register
4249 *
4250 * May be used as the bmdma_stop() entry in ata_port_operations.
4251 *
4252 * LOCKING:
4253 * spin_lock_irqsave(host_set lock)
4254 */
4255
Alan Coxb73fc892005-08-26 16:03:19 +01004256void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257{
Alan Coxb73fc892005-08-26 16:03:19 +01004258 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 if (ap->flags & ATA_FLAG_MMIO) {
4260 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4261
4262 /* clear start/stop bit */
4263 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4264 mmio + ATA_DMA_CMD);
4265 } else {
4266 /* clear start/stop bit */
4267 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4268 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4269 }
4270
4271 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4272 ata_altstatus(ap); /* dummy read */
4273}
4274
4275/**
4276 * ata_host_intr - Handle host interrupt for given (port, task)
4277 * @ap: Port on which interrupt arrived (possibly...)
4278 * @qc: Taskfile currently active in engine
4279 *
4280 * Handle host interrupt for given queued command. Currently,
4281 * only DMA interrupts are handled. All other commands are
4282 * handled via polling with interrupts disabled (nIEN bit).
4283 *
4284 * LOCKING:
4285 * spin_lock_irqsave(host_set lock)
4286 *
4287 * RETURNS:
4288 * One if interrupt was handled, zero if not (shared irq).
4289 */
4290
4291inline unsigned int ata_host_intr (struct ata_port *ap,
4292 struct ata_queued_cmd *qc)
4293{
Albert Lee312f7da2005-09-27 17:38:03 +08004294 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Albert Lee312f7da2005-09-27 17:38:03 +08004296 VPRINTK("ata%u: protocol %d task_state %d\n",
4297 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Albert Lee312f7da2005-09-27 17:38:03 +08004299 /* Check whether we are expecting interrupt in this state */
4300 switch (ap->hsm_task_state) {
4301 case HSM_ST_FIRST:
4302 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4303 * The flag was turned on only for atapi devices.
4304 * No need to check is_atapi_taskfile(&qc->tf) again.
4305 */
4306 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08004308 break;
4309 case HSM_ST_LAST:
4310 if (qc->tf.protocol == ATA_PROT_DMA ||
4311 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4312 /* check status of DMA engine */
4313 host_stat = ap->ops->bmdma_status(ap);
4314 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Albert Lee312f7da2005-09-27 17:38:03 +08004316 /* if it's not our irq... */
4317 if (!(host_stat & ATA_DMA_INTR))
4318 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
Albert Lee312f7da2005-09-27 17:38:03 +08004320 /* before we do anything else, clear DMA-Start bit */
4321 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08004322
4323 if (unlikely(host_stat & ATA_DMA_ERR)) {
4324 /* error when transfering data to/from memory */
4325 qc->err_mask |= AC_ERR_HOST_BUS;
4326 ap->hsm_task_state = HSM_ST_ERR;
4327 }
Albert Lee312f7da2005-09-27 17:38:03 +08004328 }
4329 break;
4330 case HSM_ST:
4331 break;
4332 default:
4333 goto idle_irq;
4334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
Albert Lee312f7da2005-09-27 17:38:03 +08004336 /* check altstatus */
4337 status = ata_altstatus(ap);
4338 if (status & ATA_BUSY)
4339 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
Albert Lee312f7da2005-09-27 17:38:03 +08004341 /* check main status, clearing INTRQ */
4342 status = ata_chk_status(ap);
4343 if (unlikely(status & ATA_BUSY))
4344 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Albert Lee312f7da2005-09-27 17:38:03 +08004346 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4347 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4348
4349 /* ack bmdma irq events */
4350 ap->ops->irq_clear(ap);
4351
4352 /* check error */
Albert Leea4f16612005-12-26 16:40:53 +08004353 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4354 qc->err_mask |= AC_ERR_DEV;
Albert Lee312f7da2005-09-27 17:38:03 +08004355 ap->hsm_task_state = HSM_ST_ERR;
Albert Leea4f16612005-12-26 16:40:53 +08004356 }
Albert Lee312f7da2005-09-27 17:38:03 +08004357
4358fsm_start:
4359 switch (ap->hsm_task_state) {
4360 case HSM_ST_FIRST:
4361 /* Some pre-ATAPI-4 devices assert INTRQ
4362 * at this state when ready to receive CDB.
4363 */
4364
4365 /* check device status */
4366 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4367 /* Wrong status. Let EH handle this */
Albert Lee555a8962006-02-08 16:50:29 +08004368 qc->err_mask |= AC_ERR_HSM;
Albert Lee312f7da2005-09-27 17:38:03 +08004369 ap->hsm_task_state = HSM_ST_ERR;
4370 goto fsm_start;
4371 }
4372
4373 atapi_send_cdb(ap, qc);
4374
4375 break;
4376
4377 case HSM_ST:
4378 /* complete command or read/write the data register */
4379 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4380 /* ATAPI PIO protocol */
4381 if ((status & ATA_DRQ) == 0) {
4382 /* no more data to transfer */
4383 ap->hsm_task_state = HSM_ST_LAST;
4384 goto fsm_start;
4385 }
4386
4387 atapi_pio_bytes(qc);
4388
4389 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4390 /* bad ireason reported by device */
4391 goto fsm_start;
4392
4393 } else {
4394 /* ATA PIO protocol */
4395 if (unlikely((status & ATA_DRQ) == 0)) {
4396 /* handle BSY=0, DRQ=0 as error */
Albert Lee555a8962006-02-08 16:50:29 +08004397 qc->err_mask |= AC_ERR_HSM;
Albert Lee312f7da2005-09-27 17:38:03 +08004398 ap->hsm_task_state = HSM_ST_ERR;
4399 goto fsm_start;
4400 }
4401
Albert Lee07f6f7d2005-11-01 19:33:20 +08004402 ata_pio_sectors(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004403
4404 if (ap->hsm_task_state == HSM_ST_LAST &&
4405 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4406 /* all data read */
4407 ata_altstatus(ap);
4408 status = ata_chk_status(ap);
4409 goto fsm_start;
4410 }
4411 }
4412
4413 ata_altstatus(ap); /* flush */
4414 break;
4415
4416 case HSM_ST_LAST:
4417 if (unlikely(status & ATA_DRQ)) {
4418 /* handle DRQ=1 as error */
Albert Lee555a8962006-02-08 16:50:29 +08004419 qc->err_mask |= AC_ERR_HSM;
Albert Lee312f7da2005-09-27 17:38:03 +08004420 ap->hsm_task_state = HSM_ST_ERR;
4421 goto fsm_start;
4422 }
4423
4424 /* no more data to transfer */
4425 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4426 ap->id, status);
4427
4428 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
4430 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004431 qc->err_mask |= ac_err_mask(status);
4432 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 break;
4434
Albert Lee312f7da2005-09-27 17:38:03 +08004435 case HSM_ST_ERR:
Albert Lee000080c2005-12-26 16:48:00 +08004436 if (qc->tf.command != ATA_CMD_PACKET)
4437 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4438 ap->id, status, host_stat);
Albert Lee312f7da2005-09-27 17:38:03 +08004439
Albert Leea4f16612005-12-26 16:40:53 +08004440 /* make sure qc->err_mask is available to
4441 * know what's wrong and recover
4442 */
4443 assert(qc->err_mask);
4444
Albert Lee312f7da2005-09-27 17:38:03 +08004445 ap->hsm_task_state = HSM_ST_IDLE;
Jeff Garzik278efe92005-12-06 05:01:27 -05004446 ata_qc_complete(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 default:
4449 goto idle_irq;
4450 }
4451
4452 return 1; /* irq handled */
4453
4454idle_irq:
4455 ap->stats.idle_irq++;
4456
4457#ifdef ATA_IRQ_TRAP
4458 if ((ap->stats.idle_irq % 1000) == 0) {
4459 handled = 1;
4460 ata_irq_ack(ap, 0); /* debug trap */
4461 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4462 }
4463#endif
4464 return 0; /* irq not handled */
4465}
4466
4467/**
4468 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004469 * @irq: irq line (unused)
4470 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 * @regs: unused
4472 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004473 * Default interrupt handler for PCI IDE devices. Calls
4474 * ata_host_intr() for each port that is not disabled.
4475 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004477 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 *
4479 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004480 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 */
4482
4483irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4484{
4485 struct ata_host_set *host_set = dev_instance;
4486 unsigned int i;
4487 unsigned int handled = 0;
4488 unsigned long flags;
4489
4490 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4491 spin_lock_irqsave(&host_set->lock, flags);
4492
4493 for (i = 0; i < host_set->n_ports; i++) {
4494 struct ata_port *ap;
4495
4496 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004497 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08004498 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 struct ata_queued_cmd *qc;
4500
4501 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004502 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004503 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 handled |= ata_host_intr(ap, qc);
4505 }
4506 }
4507
4508 spin_unlock_irqrestore(&host_set->lock, flags);
4509
4510 return IRQ_RETVAL(handled);
4511}
4512
4513/**
Edward Falk0baab862005-06-02 18:17:13 -04004514 * ata_port_start - Set port up for dma.
4515 * @ap: Port to initialize
4516 *
4517 * Called just after data structures for each port are
4518 * initialized. Allocates space for PRD table.
4519 *
4520 * May be used as the port_start() entry in ata_port_operations.
4521 *
4522 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004523 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004524 */
4525
Jens Axboe9b847542006-01-06 09:28:07 +01004526/*
4527 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4528 * without filling any other registers
4529 */
4530static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4531 u8 cmd)
4532{
4533 struct ata_taskfile tf;
4534 int err;
4535
4536 ata_tf_init(ap, &tf, dev->devno);
4537
4538 tf.command = cmd;
4539 tf.flags |= ATA_TFLAG_DEVICE;
4540 tf.protocol = ATA_PROT_NODATA;
4541
4542 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4543 if (err)
4544 printk(KERN_ERR "%s: ata command failed: %d\n",
4545 __FUNCTION__, err);
4546
4547 return err;
4548}
4549
4550static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4551{
4552 u8 cmd;
4553
4554 if (!ata_try_flush_cache(dev))
4555 return 0;
4556
4557 if (ata_id_has_flush_ext(dev->id))
4558 cmd = ATA_CMD_FLUSH_EXT;
4559 else
4560 cmd = ATA_CMD_FLUSH;
4561
4562 return ata_do_simple_cmd(ap, dev, cmd);
4563}
4564
4565static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4566{
4567 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4568}
4569
4570static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4571{
4572 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4573}
4574
4575/**
4576 * ata_device_resume - wakeup a previously suspended devices
4577 *
4578 * Kick the drive back into action, by sending it an idle immediate
4579 * command and making sure its transfer mode matches between drive
4580 * and host.
4581 *
4582 */
4583int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4584{
4585 if (ap->flags & ATA_FLAG_SUSPENDED) {
4586 ap->flags &= ~ATA_FLAG_SUSPENDED;
4587 ata_set_mode(ap);
4588 }
4589 if (!ata_dev_present(dev))
4590 return 0;
4591 if (dev->class == ATA_DEV_ATA)
4592 ata_start_drive(ap, dev);
4593
4594 return 0;
4595}
4596
4597/**
4598 * ata_device_suspend - prepare a device for suspend
4599 *
4600 * Flush the cache on the drive, if appropriate, then issue a
4601 * standbynow command.
4602 *
4603 */
4604int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4605{
4606 if (!ata_dev_present(dev))
4607 return 0;
4608 if (dev->class == ATA_DEV_ATA)
4609 ata_flush_cache(ap, dev);
4610
4611 ata_standby_drive(ap, dev);
4612 ap->flags |= ATA_FLAG_SUSPENDED;
4613 return 0;
4614}
4615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616int ata_port_start (struct ata_port *ap)
4617{
4618 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004619 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4622 if (!ap->prd)
4623 return -ENOMEM;
4624
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004625 rc = ata_pad_alloc(ap, dev);
4626 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004627 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004628 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004629 }
4630
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4632
4633 return 0;
4634}
4635
Edward Falk0baab862005-06-02 18:17:13 -04004636
4637/**
4638 * ata_port_stop - Undo ata_port_start()
4639 * @ap: Port to shut down
4640 *
4641 * Frees the PRD table.
4642 *
4643 * May be used as the port_stop() entry in ata_port_operations.
4644 *
4645 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004646 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004647 */
4648
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649void ata_port_stop (struct ata_port *ap)
4650{
4651 struct device *dev = ap->host_set->dev;
4652
4653 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004654 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655}
4656
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004657void ata_host_stop (struct ata_host_set *host_set)
4658{
4659 if (host_set->mmio_base)
4660 iounmap(host_set->mmio_base);
4661}
4662
4663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664/**
4665 * ata_host_remove - Unregister SCSI host structure with upper layers
4666 * @ap: Port to unregister
4667 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4668 *
4669 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004670 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 */
4672
4673static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4674{
4675 struct Scsi_Host *sh = ap->host;
4676
4677 DPRINTK("ENTER\n");
4678
4679 if (do_unregister)
4680 scsi_remove_host(sh);
4681
4682 ap->ops->port_stop(ap);
4683}
4684
4685/**
4686 * ata_host_init - Initialize an ata_port structure
4687 * @ap: Structure to initialize
4688 * @host: associated SCSI mid-layer structure
4689 * @host_set: Collection of hosts to which @ap belongs
4690 * @ent: Probe information provided by low-level driver
4691 * @port_no: Port number associated with this ata_port
4692 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004693 * Initialize a new ata_port structure, and its associated
4694 * scsi_host.
4695 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004697 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 */
4699
4700static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4701 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004702 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703{
4704 unsigned int i;
4705
4706 host->max_id = 16;
4707 host->max_lun = 1;
4708 host->max_channel = 1;
4709 host->unique_id = ata_unique_id++;
4710 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 ap->flags = ATA_FLAG_PORT_DISABLED;
4713 ap->id = host->unique_id;
4714 ap->host = host;
4715 ap->ctl = ATA_DEVCTL_OBS;
4716 ap->host_set = host_set;
4717 ap->port_no = port_no;
4718 ap->hard_port_no =
4719 ent->legacy_mode ? ent->hard_port_no : port_no;
4720 ap->pio_mask = ent->pio_mask;
4721 ap->mwdma_mask = ent->mwdma_mask;
4722 ap->udma_mask = ent->udma_mask;
4723 ap->flags |= ent->host_flags;
4724 ap->ops = ent->port_ops;
4725 ap->cbl = ATA_CBL_NONE;
4726 ap->active_tag = ATA_TAG_POISON;
4727 ap->last_ctl = 0xFF;
4728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004730 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
4732 for (i = 0; i < ATA_MAX_DEVICES; i++)
4733 ap->device[i].devno = i;
4734
4735#ifdef ATA_IRQ_TRAP
4736 ap->stats.unhandled_irq = 1;
4737 ap->stats.idle_irq = 1;
4738#endif
4739
4740 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4741}
4742
4743/**
4744 * ata_host_add - Attach low-level ATA driver to system
4745 * @ent: Information provided by low-level driver
4746 * @host_set: Collections of ports to which we add
4747 * @port_no: Port number associated with this host
4748 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004749 * Attach low-level ATA driver to system.
4750 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004752 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 *
4754 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004755 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 */
4757
Jeff Garzik057ace52005-10-22 14:27:05 -04004758static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 struct ata_host_set *host_set,
4760 unsigned int port_no)
4761{
4762 struct Scsi_Host *host;
4763 struct ata_port *ap;
4764 int rc;
4765
4766 DPRINTK("ENTER\n");
4767 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4768 if (!host)
4769 return NULL;
4770
4771 ap = (struct ata_port *) &host->hostdata[0];
4772
4773 ata_host_init(ap, host, host_set, ent, port_no);
4774
4775 rc = ap->ops->port_start(ap);
4776 if (rc)
4777 goto err_out;
4778
4779 return ap;
4780
4781err_out:
4782 scsi_host_put(host);
4783 return NULL;
4784}
4785
4786/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004787 * ata_device_add - Register hardware device with ATA and SCSI layers
4788 * @ent: Probe information describing hardware device to be registered
4789 *
4790 * This function processes the information provided in the probe
4791 * information struct @ent, allocates the necessary ATA and SCSI
4792 * host information structures, initializes them, and registers
4793 * everything with requisite kernel subsystems.
4794 *
4795 * This function requests irqs, probes the ATA bus, and probes
4796 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 *
4798 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004799 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 *
4801 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004802 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 */
4804
Jeff Garzik057ace52005-10-22 14:27:05 -04004805int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
4807 unsigned int count = 0, i;
4808 struct device *dev = ent->dev;
4809 struct ata_host_set *host_set;
4810
4811 DPRINTK("ENTER\n");
4812 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004813 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4815 if (!host_set)
4816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 spin_lock_init(&host_set->lock);
4818
4819 host_set->dev = dev;
4820 host_set->n_ports = ent->n_ports;
4821 host_set->irq = ent->irq;
4822 host_set->mmio_base = ent->mmio_base;
4823 host_set->private_data = ent->private_data;
4824 host_set->ops = ent->port_ops;
4825
4826 /* register each port bound to this device */
4827 for (i = 0; i < ent->n_ports; i++) {
4828 struct ata_port *ap;
4829 unsigned long xfer_mode_mask;
4830
4831 ap = ata_host_add(ent, host_set, i);
4832 if (!ap)
4833 goto err_out;
4834
4835 host_set->ports[i] = ap;
4836 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4837 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4838 (ap->pio_mask << ATA_SHIFT_PIO);
4839
4840 /* print per-port info to dmesg */
4841 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4842 "bmdma 0x%lX irq %lu\n",
4843 ap->id,
4844 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4845 ata_mode_string(xfer_mode_mask),
4846 ap->ioaddr.cmd_addr,
4847 ap->ioaddr.ctl_addr,
4848 ap->ioaddr.bmdma_addr,
4849 ent->irq);
4850
4851 ata_chk_status(ap);
4852 host_set->ops->irq_clear(ap);
4853 count++;
4854 }
4855
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004856 if (!count)
4857 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
4859 /* obtain irq, that is shared between channels */
4860 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4861 DRV_NAME, host_set))
4862 goto err_out;
4863
4864 /* perform each probe synchronously */
4865 DPRINTK("probe begin\n");
4866 for (i = 0; i < count; i++) {
4867 struct ata_port *ap;
4868 int rc;
4869
4870 ap = host_set->ports[i];
4871
4872 DPRINTK("ata%u: probe begin\n", ap->id);
4873 rc = ata_bus_probe(ap);
4874 DPRINTK("ata%u: probe end\n", ap->id);
4875
4876 if (rc) {
4877 /* FIXME: do something useful here?
4878 * Current libata behavior will
4879 * tear down everything when
4880 * the module is removed
4881 * or the h/w is unplugged.
4882 */
4883 }
4884
4885 rc = scsi_add_host(ap->host, dev);
4886 if (rc) {
4887 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4888 ap->id);
4889 /* FIXME: do something useful here */
4890 /* FIXME: handle unconditional calls to
4891 * scsi_scan_host and ata_host_remove, below,
4892 * at the very least
4893 */
4894 }
4895 }
4896
4897 /* probes are done, now scan each port's disk(s) */
4898 DPRINTK("probe begin\n");
4899 for (i = 0; i < count; i++) {
4900 struct ata_port *ap = host_set->ports[i];
4901
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004902 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 }
4904
4905 dev_set_drvdata(dev, host_set);
4906
4907 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4908 return ent->n_ports; /* success */
4909
4910err_out:
4911 for (i = 0; i < count; i++) {
4912 ata_host_remove(host_set->ports[i], 1);
4913 scsi_host_put(host_set->ports[i]->host);
4914 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004915err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 kfree(host_set);
4917 VPRINTK("EXIT, returning 0\n");
4918 return 0;
4919}
4920
4921/**
Alan Cox17b14452005-09-15 15:44:00 +01004922 * ata_host_set_remove - PCI layer callback for device removal
4923 * @host_set: ATA host set that was removed
4924 *
4925 * Unregister all objects associated with this host set. Free those
4926 * objects.
4927 *
4928 * LOCKING:
4929 * Inherited from calling layer (may sleep).
4930 */
4931
Alan Cox17b14452005-09-15 15:44:00 +01004932void ata_host_set_remove(struct ata_host_set *host_set)
4933{
4934 struct ata_port *ap;
4935 unsigned int i;
4936
4937 for (i = 0; i < host_set->n_ports; i++) {
4938 ap = host_set->ports[i];
4939 scsi_remove_host(ap->host);
4940 }
4941
4942 free_irq(host_set->irq, host_set);
4943
4944 for (i = 0; i < host_set->n_ports; i++) {
4945 ap = host_set->ports[i];
4946
4947 ata_scsi_release(ap->host);
4948
4949 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4950 struct ata_ioports *ioaddr = &ap->ioaddr;
4951
4952 if (ioaddr->cmd_addr == 0x1f0)
4953 release_region(0x1f0, 8);
4954 else if (ioaddr->cmd_addr == 0x170)
4955 release_region(0x170, 8);
4956 }
4957
4958 scsi_host_put(ap->host);
4959 }
4960
4961 if (host_set->ops->host_stop)
4962 host_set->ops->host_stop(host_set);
4963
4964 kfree(host_set);
4965}
4966
4967/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 * ata_scsi_release - SCSI layer callback hook for host unload
4969 * @host: libata host to be unloaded
4970 *
4971 * Performs all duties necessary to shut down a libata port...
4972 * Kill port kthread, disable port, and release resources.
4973 *
4974 * LOCKING:
4975 * Inherited from SCSI layer.
4976 *
4977 * RETURNS:
4978 * One.
4979 */
4980
4981int ata_scsi_release(struct Scsi_Host *host)
4982{
4983 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4984
4985 DPRINTK("ENTER\n");
4986
4987 ap->ops->port_disable(ap);
4988 ata_host_remove(ap, 0);
4989
4990 DPRINTK("EXIT\n");
4991 return 1;
4992}
4993
4994/**
4995 * ata_std_ports - initialize ioaddr with standard port offsets.
4996 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004997 *
4998 * Utility function which initializes data_addr, error_addr,
4999 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5000 * device_addr, status_addr, and command_addr to standard offsets
5001 * relative to cmd_addr.
5002 *
5003 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 */
Edward Falk0baab862005-06-02 18:17:13 -04005005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006void ata_std_ports(struct ata_ioports *ioaddr)
5007{
5008 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5009 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5010 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5011 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5012 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5013 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5014 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5015 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5016 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5017 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5018}
5019
5020static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04005021ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
5023 struct ata_probe_ent *probe_ent;
5024
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005025 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 if (!probe_ent) {
5027 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
5028 kobject_name(&(dev->kobj)));
5029 return NULL;
5030 }
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 INIT_LIST_HEAD(&probe_ent->node);
5033 probe_ent->dev = dev;
5034
5035 probe_ent->sht = port->sht;
5036 probe_ent->host_flags = port->host_flags;
5037 probe_ent->pio_mask = port->pio_mask;
5038 probe_ent->mwdma_mask = port->mwdma_mask;
5039 probe_ent->udma_mask = port->udma_mask;
5040 probe_ent->port_ops = port->port_ops;
5041
5042 return probe_ent;
5043}
5044
Edward Falk0baab862005-06-02 18:17:13 -04005045
5046
Jeff Garzik374b1872005-08-30 05:42:52 -04005047#ifdef CONFIG_PCI
5048
5049void ata_pci_host_stop (struct ata_host_set *host_set)
5050{
5051 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5052
5053 pci_iounmap(pdev, host_set->mmio_base);
5054}
5055
Edward Falk0baab862005-06-02 18:17:13 -04005056/**
5057 * ata_pci_init_native_mode - Initialize native-mode driver
5058 * @pdev: pci device to be initialized
5059 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04005060 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04005061 *
5062 * Utility function which allocates and initializes an
5063 * ata_probe_ent structure for a standard dual-port
5064 * PIO-based IDE controller. The returned ata_probe_ent
5065 * structure can be passed to ata_device_add(). The returned
5066 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04005067 *
5068 * The caller need only pass the address of the primary port, the
5069 * secondary will be deduced automatically. If the device has non
5070 * standard secondary port mappings this function can be called twice,
5071 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04005072 */
5073
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04005075ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076{
5077 struct ata_probe_ent *probe_ent =
5078 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04005079 int p = 0;
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 if (!probe_ent)
5082 return NULL;
5083
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 probe_ent->irq = pdev->irq;
5085 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00005086 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Alan Cox47a86592005-10-04 08:09:19 -04005088 if (ports & ATA_PORT_PRIMARY) {
5089 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
5090 probe_ent->port[p].altstatus_addr =
5091 probe_ent->port[p].ctl_addr =
5092 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
5093 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
5094 ata_std_ports(&probe_ent->port[p]);
5095 p++;
5096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
Alan Cox47a86592005-10-04 08:09:19 -04005098 if (ports & ATA_PORT_SECONDARY) {
5099 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
5100 probe_ent->port[p].altstatus_addr =
5101 probe_ent->port[p].ctl_addr =
5102 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
5103 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
5104 ata_std_ports(&probe_ent->port[p]);
5105 p++;
5106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Alan Cox47a86592005-10-04 08:09:19 -04005108 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 return probe_ent;
5110}
5111
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005112static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113{
Alan Cox47a86592005-10-04 08:09:19 -04005114 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005116 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 if (!probe_ent)
5118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04005121 probe_ent->n_ports = 1;
5122 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00005123 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
Alan Cox47a86592005-10-04 08:09:19 -04005125 switch(port_num)
5126 {
5127 case 0:
5128 probe_ent->irq = 14;
5129 probe_ent->port[0].cmd_addr = 0x1f0;
5130 probe_ent->port[0].altstatus_addr =
5131 probe_ent->port[0].ctl_addr = 0x3f6;
5132 break;
5133 case 1:
5134 probe_ent->irq = 15;
5135 probe_ent->port[0].cmd_addr = 0x170;
5136 probe_ent->port[0].altstatus_addr =
5137 probe_ent->port[0].ctl_addr = 0x376;
5138 break;
5139 }
5140 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return probe_ent;
5143}
5144
5145/**
5146 * ata_pci_init_one - Initialize/register PCI IDE host controller
5147 * @pdev: Controller to be initialized
5148 * @port_info: Information from low-level host driver
5149 * @n_ports: Number of ports attached to host controller
5150 *
Edward Falk0baab862005-06-02 18:17:13 -04005151 * This is a helper function which can be called from a driver's
5152 * xxx_init_one() probe function if the hardware uses traditional
5153 * IDE taskfile registers.
5154 *
5155 * This function calls pci_enable_device(), reserves its register
5156 * regions, sets the dma mask, enables bus master mode, and calls
5157 * ata_device_add()
5158 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 * LOCKING:
5160 * Inherited from PCI layer (may sleep).
5161 *
5162 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005163 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 */
5165
5166int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
5167 unsigned int n_ports)
5168{
Alan Cox47a86592005-10-04 08:09:19 -04005169 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 struct ata_port_info *port[2];
5171 u8 tmp8, mask;
5172 unsigned int legacy_mode = 0;
5173 int disable_dev_on_err = 1;
5174 int rc;
5175
5176 DPRINTK("ENTER\n");
5177
5178 port[0] = port_info[0];
5179 if (n_ports > 1)
5180 port[1] = port_info[1];
5181 else
5182 port[1] = port[0];
5183
5184 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
5185 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04005186 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
5188 mask = (1 << 2) | (1 << 0);
5189 if ((tmp8 & mask) != mask)
5190 legacy_mode = (1 << 3);
5191 }
5192
5193 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04005194 if ((!legacy_mode) && (n_ports > 2)) {
5195 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
5196 n_ports = 2;
5197 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 }
5199
Alan Cox47a86592005-10-04 08:09:19 -04005200 /* FIXME: Really for ATA it isn't safe because the device may be
5201 multi-purpose and we want to leave it alone if it was already
5202 enabled. Secondly for shared use as Arjan says we want refcounting
5203
5204 Checking dev->is_enabled is insufficient as this is not set at
5205 boot for the primary video which is BIOS enabled
5206 */
5207
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 rc = pci_enable_device(pdev);
5209 if (rc)
5210 return rc;
5211
5212 rc = pci_request_regions(pdev, DRV_NAME);
5213 if (rc) {
5214 disable_dev_on_err = 0;
5215 goto err_out;
5216 }
5217
Alan Cox47a86592005-10-04 08:09:19 -04005218 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 if (legacy_mode) {
5220 if (!request_region(0x1f0, 8, "libata")) {
5221 struct resource *conflict, res;
5222 res.start = 0x1f0;
5223 res.end = 0x1f0 + 8 - 1;
5224 conflict = ____request_resource(&ioport_resource, &res);
5225 if (!strcmp(conflict->name, "libata"))
5226 legacy_mode |= (1 << 0);
5227 else {
5228 disable_dev_on_err = 0;
5229 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
5230 }
5231 } else
5232 legacy_mode |= (1 << 0);
5233
5234 if (!request_region(0x170, 8, "libata")) {
5235 struct resource *conflict, res;
5236 res.start = 0x170;
5237 res.end = 0x170 + 8 - 1;
5238 conflict = ____request_resource(&ioport_resource, &res);
5239 if (!strcmp(conflict->name, "libata"))
5240 legacy_mode |= (1 << 1);
5241 else {
5242 disable_dev_on_err = 0;
5243 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
5244 }
5245 } else
5246 legacy_mode |= (1 << 1);
5247 }
5248
5249 /* we have legacy mode, but all ports are unavailable */
5250 if (legacy_mode == (1 << 3)) {
5251 rc = -EBUSY;
5252 goto err_out_regions;
5253 }
5254
5255 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
5256 if (rc)
5257 goto err_out_regions;
5258 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
5259 if (rc)
5260 goto err_out_regions;
5261
5262 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04005263 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005264 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04005265 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005266 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04005267 } else {
5268 if (n_ports == 2)
5269 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
5270 else
5271 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
5272 }
5273 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 rc = -ENOMEM;
5275 goto err_out_regions;
5276 }
5277
5278 pci_set_master(pdev);
5279
5280 /* FIXME: check ata_device_add return */
5281 if (legacy_mode) {
5282 if (legacy_mode & (1 << 0))
5283 ata_device_add(probe_ent);
5284 if (legacy_mode & (1 << 1))
5285 ata_device_add(probe_ent2);
5286 } else
5287 ata_device_add(probe_ent);
5288
5289 kfree(probe_ent);
5290 kfree(probe_ent2);
5291
5292 return 0;
5293
5294err_out_regions:
5295 if (legacy_mode & (1 << 0))
5296 release_region(0x1f0, 8);
5297 if (legacy_mode & (1 << 1))
5298 release_region(0x170, 8);
5299 pci_release_regions(pdev);
5300err_out:
5301 if (disable_dev_on_err)
5302 pci_disable_device(pdev);
5303 return rc;
5304}
5305
5306/**
5307 * ata_pci_remove_one - PCI layer callback for device removal
5308 * @pdev: PCI device that was removed
5309 *
5310 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005311 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 * Handle this by unregistering all objects associated
5313 * with this PCI device. Free those objects. Then finally
5314 * release PCI resources and disable device.
5315 *
5316 * LOCKING:
5317 * Inherited from PCI layer (may sleep).
5318 */
5319
5320void ata_pci_remove_one (struct pci_dev *pdev)
5321{
5322 struct device *dev = pci_dev_to_dev(pdev);
5323 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
Alan Cox17b14452005-09-15 15:44:00 +01005325 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 pci_release_regions(pdev);
5327 pci_disable_device(pdev);
5328 dev_set_drvdata(dev, NULL);
5329}
5330
5331/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005332int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333{
5334 unsigned long tmp = 0;
5335
5336 switch (bits->width) {
5337 case 1: {
5338 u8 tmp8 = 0;
5339 pci_read_config_byte(pdev, bits->reg, &tmp8);
5340 tmp = tmp8;
5341 break;
5342 }
5343 case 2: {
5344 u16 tmp16 = 0;
5345 pci_read_config_word(pdev, bits->reg, &tmp16);
5346 tmp = tmp16;
5347 break;
5348 }
5349 case 4: {
5350 u32 tmp32 = 0;
5351 pci_read_config_dword(pdev, bits->reg, &tmp32);
5352 tmp = tmp32;
5353 break;
5354 }
5355
5356 default:
5357 return -EINVAL;
5358 }
5359
5360 tmp &= bits->mask;
5361
5362 return (tmp == bits->val) ? 1 : 0;
5363}
Jens Axboe9b847542006-01-06 09:28:07 +01005364
5365int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5366{
5367 pci_save_state(pdev);
5368 pci_disable_device(pdev);
5369 pci_set_power_state(pdev, PCI_D3hot);
5370 return 0;
5371}
5372
5373int ata_pci_device_resume(struct pci_dev *pdev)
5374{
5375 pci_set_power_state(pdev, PCI_D0);
5376 pci_restore_state(pdev);
5377 pci_enable_device(pdev);
5378 pci_set_master(pdev);
5379 return 0;
5380}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381#endif /* CONFIG_PCI */
5382
5383
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384static int __init ata_init(void)
5385{
5386 ata_wq = create_workqueue("ata");
5387 if (!ata_wq)
5388 return -ENOMEM;
5389
5390 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5391 return 0;
5392}
5393
5394static void __exit ata_exit(void)
5395{
5396 destroy_workqueue(ata_wq);
5397}
5398
5399module_init(ata_init);
5400module_exit(ata_exit);
5401
Jeff Garzik67846b32005-10-05 02:58:32 -04005402static unsigned long ratelimit_time;
5403static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5404
5405int ata_ratelimit(void)
5406{
5407 int rc;
5408 unsigned long flags;
5409
5410 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5411
5412 if (time_after(jiffies, ratelimit_time)) {
5413 rc = 1;
5414 ratelimit_time = jiffies + (HZ/5);
5415 } else
5416 rc = 0;
5417
5418 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5419
5420 return rc;
5421}
5422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423/*
5424 * libata is essentially a library of internal helper functions for
5425 * low-level ATA host controller drivers. As such, the API/ABI is
5426 * likely to change as new drivers are added and updated.
5427 * Do not depend on ABI/API stability.
5428 */
5429
5430EXPORT_SYMBOL_GPL(ata_std_bios_param);
5431EXPORT_SYMBOL_GPL(ata_std_ports);
5432EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005433EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434EXPORT_SYMBOL_GPL(ata_sg_init);
5435EXPORT_SYMBOL_GPL(ata_sg_init_one);
5436EXPORT_SYMBOL_GPL(ata_qc_complete);
5437EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5438EXPORT_SYMBOL_GPL(ata_eng_timeout);
5439EXPORT_SYMBOL_GPL(ata_tf_load);
5440EXPORT_SYMBOL_GPL(ata_tf_read);
5441EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5442EXPORT_SYMBOL_GPL(ata_std_dev_select);
5443EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5444EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5445EXPORT_SYMBOL_GPL(ata_check_status);
5446EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447EXPORT_SYMBOL_GPL(ata_exec_command);
5448EXPORT_SYMBOL_GPL(ata_port_start);
5449EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005450EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451EXPORT_SYMBOL_GPL(ata_interrupt);
5452EXPORT_SYMBOL_GPL(ata_qc_prep);
5453EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5454EXPORT_SYMBOL_GPL(ata_bmdma_start);
5455EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5456EXPORT_SYMBOL_GPL(ata_bmdma_status);
5457EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5458EXPORT_SYMBOL_GPL(ata_port_probe);
5459EXPORT_SYMBOL_GPL(sata_phy_reset);
5460EXPORT_SYMBOL_GPL(__sata_phy_reset);
5461EXPORT_SYMBOL_GPL(ata_bus_reset);
5462EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005463EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005464EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5466EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5467EXPORT_SYMBOL_GPL(ata_scsi_error);
5468EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5469EXPORT_SYMBOL_GPL(ata_scsi_release);
5470EXPORT_SYMBOL_GPL(ata_host_intr);
5471EXPORT_SYMBOL_GPL(ata_dev_classify);
5472EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005473EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005475EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5476EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005478EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005479EXPORT_SYMBOL_GPL(ata_timing_compute);
5480EXPORT_SYMBOL_GPL(ata_timing_merge);
5481
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482#ifdef CONFIG_PCI
5483EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005484EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5486EXPORT_SYMBOL_GPL(ata_pci_init_one);
5487EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005488EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5489EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005491
5492EXPORT_SYMBOL_GPL(ata_device_suspend);
5493EXPORT_SYMBOL_GPL(ata_device_resume);
5494EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5495EXPORT_SYMBOL_GPL(ata_scsi_device_resume);