blob: 0e493236294941bf636f021f809dc60f06d60c76 [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
64static unsigned int ata_busy_sleep (struct ata_port *ap,
65 unsigned long tmout_pat,
66 unsigned long tmout);
Albert Lee59a10b12005-10-12 15:09:42 +080067static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ec2005-05-12 15:29:42 -040068static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void ata_set_mode(struct ata_port *ap);
70static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040071static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040073static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 u8 *xfer_mode_out,
75 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static unsigned int ata_unique_id = 1;
78static struct workqueue_struct *ata_wq;
79
Jeff Garzik1623c812005-08-30 03:37:42 -040080int atapi_enabled = 0;
81module_param(atapi_enabled, int, 0444);
82MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_AUTHOR("Jeff Garzik");
85MODULE_DESCRIPTION("Library module for ATA devices");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(DRV_VERSION);
88
89/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040090 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * @ap: Port to which output is sent
92 * @tf: ATA taskfile register set
93 *
94 * Outputs ATA taskfile to standard ATA host controller.
95 *
96 * LOCKING:
97 * Inherited from caller.
98 */
99
Jeff Garzik057ace52005-10-22 14:27:05 -0400100static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct ata_ioports *ioaddr = &ap->ioaddr;
103 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
104
105 if (tf->ctl != ap->last_ctl) {
106 outb(tf->ctl, ioaddr->ctl_addr);
107 ap->last_ctl = tf->ctl;
108 ata_wait_idle(ap);
109 }
110
111 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
112 outb(tf->hob_feature, ioaddr->feature_addr);
113 outb(tf->hob_nsect, ioaddr->nsect_addr);
114 outb(tf->hob_lbal, ioaddr->lbal_addr);
115 outb(tf->hob_lbam, ioaddr->lbam_addr);
116 outb(tf->hob_lbah, ioaddr->lbah_addr);
117 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
118 tf->hob_feature,
119 tf->hob_nsect,
120 tf->hob_lbal,
121 tf->hob_lbam,
122 tf->hob_lbah);
123 }
124
125 if (is_addr) {
126 outb(tf->feature, ioaddr->feature_addr);
127 outb(tf->nsect, ioaddr->nsect_addr);
128 outb(tf->lbal, ioaddr->lbal_addr);
129 outb(tf->lbam, ioaddr->lbam_addr);
130 outb(tf->lbah, ioaddr->lbah_addr);
131 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
132 tf->feature,
133 tf->nsect,
134 tf->lbal,
135 tf->lbam,
136 tf->lbah);
137 }
138
139 if (tf->flags & ATA_TFLAG_DEVICE) {
140 outb(tf->device, ioaddr->device_addr);
141 VPRINTK("device 0x%X\n", tf->device);
142 }
143
144 ata_wait_idle(ap);
145}
146
147/**
148 * ata_tf_load_mmio - send taskfile registers to host controller
149 * @ap: Port to which output is sent
150 * @tf: ATA taskfile register set
151 *
152 * Outputs ATA taskfile to standard ATA host controller using MMIO.
153 *
154 * LOCKING:
155 * Inherited from caller.
156 */
157
Jeff Garzik057ace52005-10-22 14:27:05 -0400158static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct ata_ioports *ioaddr = &ap->ioaddr;
161 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
162
163 if (tf->ctl != ap->last_ctl) {
164 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
165 ap->last_ctl = tf->ctl;
166 ata_wait_idle(ap);
167 }
168
169 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
170 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
171 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
172 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
173 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
174 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
175 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
176 tf->hob_feature,
177 tf->hob_nsect,
178 tf->hob_lbal,
179 tf->hob_lbam,
180 tf->hob_lbah);
181 }
182
183 if (is_addr) {
184 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
185 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
186 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
187 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
188 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
189 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
190 tf->feature,
191 tf->nsect,
192 tf->lbal,
193 tf->lbam,
194 tf->lbah);
195 }
196
197 if (tf->flags & ATA_TFLAG_DEVICE) {
198 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
199 VPRINTK("device 0x%X\n", tf->device);
200 }
201
202 ata_wait_idle(ap);
203}
204
Edward Falk0baab862005-06-02 18:17:13 -0400205
206/**
207 * ata_tf_load - send taskfile registers to host controller
208 * @ap: Port to which output is sent
209 * @tf: ATA taskfile register set
210 *
211 * Outputs ATA taskfile to standard ATA host controller using MMIO
212 * or PIO as indicated by the ATA_FLAG_MMIO flag.
213 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
214 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
215 * hob_lbal, hob_lbam, and hob_lbah.
216 *
217 * This function waits for idle (!BUSY and !DRQ) after writing
218 * registers. If the control register has a new value, this
219 * function also waits for idle after writing control and before
220 * writing the remaining registers.
221 *
222 * May be used as the tf_load() entry in ata_port_operations.
223 *
224 * LOCKING:
225 * Inherited from caller.
226 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400227void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 if (ap->flags & ATA_FLAG_MMIO)
230 ata_tf_load_mmio(ap, tf);
231 else
232 ata_tf_load_pio(ap, tf);
233}
234
235/**
Edward Falk0baab862005-06-02 18:17:13 -0400236 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * @ap: port to which command is being issued
238 * @tf: ATA taskfile register set
239 *
Edward Falk0baab862005-06-02 18:17:13 -0400240 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * synchronization with interrupt handler / other threads.
242 *
243 * LOCKING:
244 * spin_lock_irqsave(host_set lock)
245 */
246
Jeff Garzik057ace52005-10-22 14:27:05 -0400247static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
250
251 outb(tf->command, ap->ioaddr.command_addr);
252 ata_pause(ap);
253}
254
255
256/**
257 * ata_exec_command_mmio - issue ATA command to host controller
258 * @ap: port to which command is being issued
259 * @tf: ATA taskfile register set
260 *
261 * Issues MMIO write to ATA command register, with proper
262 * synchronization with interrupt handler / other threads.
263 *
264 * LOCKING:
265 * spin_lock_irqsave(host_set lock)
266 */
267
Jeff Garzik057ace52005-10-22 14:27:05 -0400268static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
271
272 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
273 ata_pause(ap);
274}
275
Edward Falk0baab862005-06-02 18:17:13 -0400276
277/**
278 * ata_exec_command - issue ATA command to host controller
279 * @ap: port to which command is being issued
280 * @tf: ATA taskfile register set
281 *
282 * Issues PIO/MMIO write to ATA command register, with proper
283 * synchronization with interrupt handler / other threads.
284 *
285 * LOCKING:
286 * spin_lock_irqsave(host_set lock)
287 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400288void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 if (ap->flags & ATA_FLAG_MMIO)
291 ata_exec_command_mmio(ap, tf);
292 else
293 ata_exec_command_pio(ap, tf);
294}
295
296/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * ata_tf_to_host - issue ATA taskfile to host controller
298 * @ap: port to which command is being issued
299 * @tf: ATA taskfile register set
300 *
301 * Issues ATA taskfile register set to ATA host controller,
302 * with proper synchronization with interrupt handler and
303 * other threads.
304 *
305 * LOCKING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * spin_lock_irqsave(host_set lock)
307 */
308
Jeff Garzike5338252005-10-30 21:37:17 -0500309static inline void ata_tf_to_host(struct ata_port *ap,
310 const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 ap->ops->tf_load(ap, tf);
313 ap->ops->exec_command(ap, tf);
314}
315
316/**
Edward Falk0baab862005-06-02 18:17:13 -0400317 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * @ap: Port from which input is read
319 * @tf: ATA taskfile register set for storing input
320 *
321 * Reads ATA taskfile registers for currently-selected device
322 * into @tf.
323 *
324 * LOCKING:
325 * Inherited from caller.
326 */
327
328static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
329{
330 struct ata_ioports *ioaddr = &ap->ioaddr;
331
Jeff Garzikac19bff2005-10-29 13:58:21 -0400332 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400333 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 tf->nsect = inb(ioaddr->nsect_addr);
335 tf->lbal = inb(ioaddr->lbal_addr);
336 tf->lbam = inb(ioaddr->lbam_addr);
337 tf->lbah = inb(ioaddr->lbah_addr);
338 tf->device = inb(ioaddr->device_addr);
339
340 if (tf->flags & ATA_TFLAG_LBA48) {
341 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
342 tf->hob_feature = inb(ioaddr->error_addr);
343 tf->hob_nsect = inb(ioaddr->nsect_addr);
344 tf->hob_lbal = inb(ioaddr->lbal_addr);
345 tf->hob_lbam = inb(ioaddr->lbam_addr);
346 tf->hob_lbah = inb(ioaddr->lbah_addr);
347 }
348}
349
350/**
351 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
352 * @ap: Port from which input is read
353 * @tf: ATA taskfile register set for storing input
354 *
355 * Reads ATA taskfile registers for currently-selected device
356 * into @tf via MMIO.
357 *
358 * LOCKING:
359 * Inherited from caller.
360 */
361
362static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
363{
364 struct ata_ioports *ioaddr = &ap->ioaddr;
365
Jeff Garzikac19bff2005-10-29 13:58:21 -0400366 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400367 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
369 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
370 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
371 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
372 tf->device = readb((void __iomem *)ioaddr->device_addr);
373
374 if (tf->flags & ATA_TFLAG_LBA48) {
375 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
376 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
377 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
378 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
379 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
380 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
381 }
382}
383
Edward Falk0baab862005-06-02 18:17:13 -0400384
385/**
386 * ata_tf_read - input device's ATA taskfile shadow registers
387 * @ap: Port from which input is read
388 * @tf: ATA taskfile register set for storing input
389 *
390 * Reads ATA taskfile registers for currently-selected device
391 * into @tf.
392 *
393 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
394 * is set, also reads the hob registers.
395 *
396 * May be used as the tf_read() entry in ata_port_operations.
397 *
398 * LOCKING:
399 * Inherited from caller.
400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
402{
403 if (ap->flags & ATA_FLAG_MMIO)
404 ata_tf_read_mmio(ap, tf);
405 else
406 ata_tf_read_pio(ap, tf);
407}
408
409/**
410 * ata_check_status_pio - Read device status reg & clear interrupt
411 * @ap: port where the device is
412 *
413 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400414 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * from this device
416 *
417 * LOCKING:
418 * Inherited from caller.
419 */
420static u8 ata_check_status_pio(struct ata_port *ap)
421{
422 return inb(ap->ioaddr.status_addr);
423}
424
425/**
426 * ata_check_status_mmio - Read device status reg & clear interrupt
427 * @ap: port where the device is
428 *
429 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400430 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * from this device
432 *
433 * LOCKING:
434 * Inherited from caller.
435 */
436static u8 ata_check_status_mmio(struct ata_port *ap)
437{
438 return readb((void __iomem *) ap->ioaddr.status_addr);
439}
440
Edward Falk0baab862005-06-02 18:17:13 -0400441
442/**
443 * ata_check_status - Read device status reg & clear interrupt
444 * @ap: port where the device is
445 *
446 * Reads ATA taskfile status register for currently-selected device
447 * and return its value. This also clears pending interrupts
448 * from this device
449 *
450 * May be used as the check_status() entry in ata_port_operations.
451 *
452 * LOCKING:
453 * Inherited from caller.
454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455u8 ata_check_status(struct ata_port *ap)
456{
457 if (ap->flags & ATA_FLAG_MMIO)
458 return ata_check_status_mmio(ap);
459 return ata_check_status_pio(ap);
460}
461
Edward Falk0baab862005-06-02 18:17:13 -0400462
463/**
464 * ata_altstatus - Read device alternate status reg
465 * @ap: port where the device is
466 *
467 * Reads ATA taskfile alternate status register for
468 * currently-selected device and return its value.
469 *
470 * Note: may NOT be used as the check_altstatus() entry in
471 * ata_port_operations.
472 *
473 * LOCKING:
474 * Inherited from caller.
475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476u8 ata_altstatus(struct ata_port *ap)
477{
478 if (ap->ops->check_altstatus)
479 return ap->ops->check_altstatus(ap);
480
481 if (ap->flags & ATA_FLAG_MMIO)
482 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
483 return inb(ap->ioaddr.altstatus_addr);
484}
485
Edward Falk0baab862005-06-02 18:17:13 -0400486
487/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
489 * @tf: Taskfile to convert
490 * @fis: Buffer into which data will output
491 * @pmp: Port multiplier port
492 *
493 * Converts a standard ATA taskfile to a Serial ATA
494 * FIS structure (Register - Host to Device).
495 *
496 * LOCKING:
497 * Inherited from caller.
498 */
499
Jeff Garzik057ace52005-10-22 14:27:05 -0400500void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 fis[0] = 0x27; /* Register - Host to Device FIS */
503 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
504 bit 7 indicates Command FIS */
505 fis[2] = tf->command;
506 fis[3] = tf->feature;
507
508 fis[4] = tf->lbal;
509 fis[5] = tf->lbam;
510 fis[6] = tf->lbah;
511 fis[7] = tf->device;
512
513 fis[8] = tf->hob_lbal;
514 fis[9] = tf->hob_lbam;
515 fis[10] = tf->hob_lbah;
516 fis[11] = tf->hob_feature;
517
518 fis[12] = tf->nsect;
519 fis[13] = tf->hob_nsect;
520 fis[14] = 0;
521 fis[15] = tf->ctl;
522
523 fis[16] = 0;
524 fis[17] = 0;
525 fis[18] = 0;
526 fis[19] = 0;
527}
528
529/**
530 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
531 * @fis: Buffer from which data will be input
532 * @tf: Taskfile to output
533 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500534 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 *
536 * LOCKING:
537 * Inherited from caller.
538 */
539
Jeff Garzik057ace52005-10-22 14:27:05 -0400540void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 tf->command = fis[2]; /* status */
543 tf->feature = fis[3]; /* error */
544
545 tf->lbal = fis[4];
546 tf->lbam = fis[5];
547 tf->lbah = fis[6];
548 tf->device = fis[7];
549
550 tf->hob_lbal = fis[8];
551 tf->hob_lbam = fis[9];
552 tf->hob_lbah = fis[10];
553
554 tf->nsect = fis[12];
555 tf->hob_nsect = fis[13];
556}
557
Albert Lee8cbd6df2005-10-12 15:06:27 +0800558static const u8 ata_rw_cmds[] = {
559 /* pio multi */
560 ATA_CMD_READ_MULTI,
561 ATA_CMD_WRITE_MULTI,
562 ATA_CMD_READ_MULTI_EXT,
563 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100564 0,
565 0,
566 0,
567 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800568 /* pio */
569 ATA_CMD_PIO_READ,
570 ATA_CMD_PIO_WRITE,
571 ATA_CMD_PIO_READ_EXT,
572 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100573 0,
574 0,
575 0,
576 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800577 /* dma */
578 ATA_CMD_READ,
579 ATA_CMD_WRITE,
580 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100581 ATA_CMD_WRITE_EXT,
582 0,
583 0,
584 0,
585 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800586};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800589 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
590 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800592 * Examine the device configuration and tf->flags to calculate
593 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 *
595 * LOCKING:
596 * caller.
597 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100598int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800600 struct ata_taskfile *tf = &qc->tf;
601 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100602 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100604 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800605
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100606 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800607 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
608 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Albert Lee8cbd6df2005-10-12 15:06:27 +0800610 if (dev->flags & ATA_DFLAG_PIO) {
611 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100612 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000613 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
614 /* Unable to use DMA due to host limitation */
615 tf->protocol = ATA_PROT_PIO;
616 index = dev->multi_count ? 0 : 4;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800617 } else {
618 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100619 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100622 cmd = ata_rw_cmds[index + fua + lba48 + write];
623 if (cmd) {
624 tf->command = cmd;
625 return 0;
626 }
627 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100630static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 "UDMA/16",
632 "UDMA/25",
633 "UDMA/33",
634 "UDMA/44",
635 "UDMA/66",
636 "UDMA/100",
637 "UDMA/133",
638 "UDMA7",
639 "MWDMA0",
640 "MWDMA1",
641 "MWDMA2",
642 "PIO0",
643 "PIO1",
644 "PIO2",
645 "PIO3",
646 "PIO4",
647};
648
649/**
650 * ata_udma_string - convert UDMA bit offset to string
651 * @mask: mask of bits supported; only highest bit counts.
652 *
653 * Determine string which represents the highest speed
654 * (highest bit in @udma_mask).
655 *
656 * LOCKING:
657 * None.
658 *
659 * RETURNS:
660 * Constant C string representing highest speed listed in
661 * @udma_mask, or the constant C string "<n/a>".
662 */
663
664static const char *ata_mode_string(unsigned int mask)
665{
666 int i;
667
668 for (i = 7; i >= 0; i--)
669 if (mask & (1 << i))
670 goto out;
671 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
672 if (mask & (1 << i))
673 goto out;
674 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
675 if (mask & (1 << i))
676 goto out;
677
678 return "<n/a>";
679
680out:
681 return xfer_mode_str[i];
682}
683
684/**
685 * ata_pio_devchk - PATA device presence detection
686 * @ap: ATA channel to examine
687 * @device: Device to examine (starting at zero)
688 *
689 * This technique was originally described in
690 * Hale Landis's ATADRVR (www.ata-atapi.com), and
691 * later found its way into the ATA/ATAPI spec.
692 *
693 * Write a pattern to the ATA shadow registers,
694 * and if a device is present, it will respond by
695 * correctly storing and echoing back the
696 * ATA shadow register contents.
697 *
698 * LOCKING:
699 * caller.
700 */
701
702static unsigned int ata_pio_devchk(struct ata_port *ap,
703 unsigned int device)
704{
705 struct ata_ioports *ioaddr = &ap->ioaddr;
706 u8 nsect, lbal;
707
708 ap->ops->dev_select(ap, device);
709
710 outb(0x55, ioaddr->nsect_addr);
711 outb(0xaa, ioaddr->lbal_addr);
712
713 outb(0xaa, ioaddr->nsect_addr);
714 outb(0x55, ioaddr->lbal_addr);
715
716 outb(0x55, ioaddr->nsect_addr);
717 outb(0xaa, ioaddr->lbal_addr);
718
719 nsect = inb(ioaddr->nsect_addr);
720 lbal = inb(ioaddr->lbal_addr);
721
722 if ((nsect == 0x55) && (lbal == 0xaa))
723 return 1; /* we found a device */
724
725 return 0; /* nothing found */
726}
727
728/**
729 * ata_mmio_devchk - PATA device presence detection
730 * @ap: ATA channel to examine
731 * @device: Device to examine (starting at zero)
732 *
733 * This technique was originally described in
734 * Hale Landis's ATADRVR (www.ata-atapi.com), and
735 * later found its way into the ATA/ATAPI spec.
736 *
737 * Write a pattern to the ATA shadow registers,
738 * and if a device is present, it will respond by
739 * correctly storing and echoing back the
740 * ATA shadow register contents.
741 *
742 * LOCKING:
743 * caller.
744 */
745
746static unsigned int ata_mmio_devchk(struct ata_port *ap,
747 unsigned int device)
748{
749 struct ata_ioports *ioaddr = &ap->ioaddr;
750 u8 nsect, lbal;
751
752 ap->ops->dev_select(ap, device);
753
754 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
755 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
756
757 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
758 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
759
760 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
761 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
762
763 nsect = readb((void __iomem *) ioaddr->nsect_addr);
764 lbal = readb((void __iomem *) ioaddr->lbal_addr);
765
766 if ((nsect == 0x55) && (lbal == 0xaa))
767 return 1; /* we found a device */
768
769 return 0; /* nothing found */
770}
771
772/**
773 * ata_devchk - PATA device presence detection
774 * @ap: ATA channel to examine
775 * @device: Device to examine (starting at zero)
776 *
777 * Dispatch ATA device presence detection, depending
778 * on whether we are using PIO or MMIO to talk to the
779 * ATA shadow registers.
780 *
781 * LOCKING:
782 * caller.
783 */
784
785static unsigned int ata_devchk(struct ata_port *ap,
786 unsigned int device)
787{
788 if (ap->flags & ATA_FLAG_MMIO)
789 return ata_mmio_devchk(ap, device);
790 return ata_pio_devchk(ap, device);
791}
792
793/**
794 * ata_dev_classify - determine device type based on ATA-spec signature
795 * @tf: ATA taskfile register set for device to be identified
796 *
797 * Determine from taskfile register contents whether a device is
798 * ATA or ATAPI, as per "Signature and persistence" section
799 * of ATA/PI spec (volume 1, sect 5.14).
800 *
801 * LOCKING:
802 * None.
803 *
804 * RETURNS:
805 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
806 * the event of failure.
807 */
808
Jeff Garzik057ace52005-10-22 14:27:05 -0400809unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 /* Apple's open source Darwin code hints that some devices only
812 * put a proper signature into the LBA mid/high registers,
813 * So, we only check those. It's sufficient for uniqueness.
814 */
815
816 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
817 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
818 DPRINTK("found ATA device by sig\n");
819 return ATA_DEV_ATA;
820 }
821
822 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
823 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
824 DPRINTK("found ATAPI device by sig\n");
825 return ATA_DEV_ATAPI;
826 }
827
828 DPRINTK("unknown device\n");
829 return ATA_DEV_UNKNOWN;
830}
831
832/**
833 * ata_dev_try_classify - Parse returned ATA device signature
834 * @ap: ATA channel to examine
835 * @device: Device to examine (starting at zero)
836 *
837 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
838 * an ATA/ATAPI-defined set of values is placed in the ATA
839 * shadow registers, indicating the results of device detection
840 * and diagnostics.
841 *
842 * Select the ATA device, and read the values from the ATA shadow
843 * registers. Then parse according to the Error register value,
844 * and the spec-defined values examined by ata_dev_classify().
845 *
846 * LOCKING:
847 * caller.
848 */
849
850static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
851{
852 struct ata_device *dev = &ap->device[device];
853 struct ata_taskfile tf;
854 unsigned int class;
855 u8 err;
856
857 ap->ops->dev_select(ap, device);
858
859 memset(&tf, 0, sizeof(tf));
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400862 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 dev->class = ATA_DEV_NONE;
865
866 /* see if device passed diags */
867 if (err == 1)
868 /* do nothing */ ;
869 else if ((device == 0) && (err == 0x81))
870 /* do nothing */ ;
871 else
872 return err;
873
874 /* determine if device if ATA or ATAPI */
875 class = ata_dev_classify(&tf);
876 if (class == ATA_DEV_UNKNOWN)
877 return err;
878 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
879 return err;
880
881 dev->class = class;
882
883 return err;
884}
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 Heoa2a7a662005-12-13 14:48:31 +09001075struct ata_exec_internal_arg {
1076 unsigned int err_mask;
1077 struct ata_taskfile *tf;
1078 struct completion *waiting;
1079};
1080
1081int ata_qc_complete_internal(struct ata_queued_cmd *qc)
1082{
1083 struct ata_exec_internal_arg *arg = qc->private_data;
1084 struct completion *waiting = arg->waiting;
1085
1086 if (!(qc->err_mask & ~AC_ERR_DEV))
1087 qc->ap->ops->tf_read(qc->ap, arg->tf);
1088 arg->err_mask = qc->err_mask;
1089 arg->waiting = NULL;
1090 complete(waiting);
1091
1092 return 0;
1093}
1094
1095/**
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;
1123 struct ata_exec_internal_arg arg;
1124
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;
1135 }
1136
1137 arg.waiting = &wait;
1138 arg.tf = tf;
1139 qc->private_data = &arg;
1140 qc->complete_fn = ata_qc_complete_internal;
1141
1142 if (ata_qc_issue(qc))
1143 goto issue_fail;
1144
1145 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1146
1147 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1148 spin_lock_irqsave(&ap->host_set->lock, flags);
1149
1150 /* We're racing with irq here. If we lose, the
1151 * following test prevents us from completing the qc
1152 * again. If completion irq occurs after here but
1153 * before the caller cleans up, it will result in a
1154 * spurious interrupt. We can live with that.
1155 */
1156 if (arg.waiting) {
1157 qc->err_mask = AC_ERR_OTHER;
1158 ata_qc_complete(qc);
1159 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1160 ap->id, command);
1161 }
1162
1163 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1164 }
1165
1166 return arg.err_mask;
1167
1168 issue_fail:
1169 ata_qc_free(qc);
1170 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1171 return AC_ERR_OTHER;
1172}
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001175 * ata_pio_need_iordy - check if iordy needed
1176 * @adev: ATA device
1177 *
1178 * Check if the current speed of the device requires IORDY. Used
1179 * by various controllers for chip configuration.
1180 */
1181
1182unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1183{
1184 int pio;
1185 int speed = adev->pio_mode - XFER_PIO_0;
1186
1187 if (speed < 2)
1188 return 0;
1189 if (speed > 2)
1190 return 1;
1191
1192 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1193
1194 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1195 pio = adev->id[ATA_ID_EIDE_PIO];
1196 /* Is the speed faster than the drive allows non IORDY ? */
1197 if (pio) {
1198 /* This is cycle times not frequency - watch the logic! */
1199 if (pio > 240) /* PIO2 is 240nS per cycle */
1200 return 1;
1201 return 0;
1202 }
1203 }
1204 return 0;
1205}
1206
1207/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1209 * @ap: port on which device we wish to probe resides
1210 * @device: device bus address, starting at zero
1211 *
1212 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1213 * command, and read back the 512-byte device information page.
1214 * The device information page is fed to us via the standard
1215 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1216 * using standard PIO-IN paths)
1217 *
1218 * After reading the device information page, we use several
1219 * bits of information from it to initialize data structures
1220 * that will be used during the lifetime of the ata_device.
1221 * Other data from the info page is used to disqualify certain
1222 * older ATA devices we do not wish to support.
1223 *
1224 * LOCKING:
1225 * Inherited from caller. Some functions called by this function
1226 * obtain the host_set lock.
1227 */
1228
1229static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1230{
1231 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001232 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 u16 tmp;
1234 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001236 struct ata_taskfile tf;
1237 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 int rc;
1239
1240 if (!ata_dev_present(dev)) {
1241 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1242 ap->id, device);
1243 return;
1244 }
1245
1246 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1247 using_edd = 0;
1248 else
1249 using_edd = 1;
1250
1251 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1252
1253 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1254 dev->class == ATA_DEV_NONE);
1255
1256 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001259 ata_tf_init(ap, &tf, device);
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001262 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 DPRINTK("do ATA identify\n");
1264 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001265 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 DPRINTK("do ATAPI identify\n");
1267 }
1268
Tejun Heoa0123702005-12-13 14:49:31 +09001269 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Tejun Heoa0123702005-12-13 14:49:31 +09001271 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1272 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Tejun Heoa0123702005-12-13 14:49:31 +09001274 if (err_mask) {
1275 if (err_mask & ~AC_ERR_DEV)
1276 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /*
1279 * arg! EDD works for all test cases, but seems to return
1280 * the ATA signature for some ATAPI devices. Until the
1281 * reason for this is found and fixed, we fix up the mess
1282 * here. If IDENTIFY DEVICE returns command aborted
1283 * (as ATAPI devices do), then we issue an
1284 * IDENTIFY PACKET DEVICE.
1285 *
1286 * ATA software reset (SRST, the default) does not appear
1287 * to have this problem.
1288 */
Albert Lee7c398332005-11-09 13:03:30 +08001289 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001290 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (err & ATA_ABORTED) {
1292 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 goto retry;
1294 }
1295 }
1296 goto err_out;
1297 }
1298
1299 swap_buf_le16(dev->id, ATA_ID_WORDS);
1300
1301 /* print device capabilities */
1302 printk(KERN_DEBUG "ata%u: dev %u cfg "
1303 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1304 ap->id, device, dev->id[49],
1305 dev->id[82], dev->id[83], dev->id[84],
1306 dev->id[85], dev->id[86], dev->id[87],
1307 dev->id[88]);
1308
1309 /*
1310 * common ATA, ATAPI feature tests
1311 */
1312
Albert Lee8bf62ec2005-05-12 15:29:42 -04001313 /* we require DMA support (bits 8 of word 49) */
1314 if (!ata_id_has_dma(dev->id)) {
1315 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 goto err_out_nosup;
1317 }
1318
1319 /* quick-n-dirty find max transfer mode; for printk only */
1320 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1321 if (!xfer_modes)
1322 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001323 if (!xfer_modes)
1324 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 ata_dump_id(dev);
1327
1328 /* ATA-specific feature tests */
1329 if (dev->class == ATA_DEV_ATA) {
1330 if (!ata_id_is_ata(dev->id)) /* sanity check */
1331 goto err_out_nosup;
1332
Albert Lee8bf62ec2005-05-12 15:29:42 -04001333 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001335 for (major_version = 14; major_version >= 1; major_version--)
1336 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 break;
1338
Albert Lee8bf62ec2005-05-12 15:29:42 -04001339 /*
1340 * The exact sequence expected by certain pre-ATA4 drives is:
1341 * SRST RESET
1342 * IDENTIFY
1343 * INITIALIZE DEVICE PARAMETERS
1344 * anything else..
1345 * Some drives were very specific about that exact sequence.
1346 */
Albert Lee59a10b12005-10-12 15:09:42 +08001347 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001348 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Albert Lee59a10b12005-10-12 15:09:42 +08001350 /* current CHS translation info (id[53-58]) might be
1351 * changed. reread the identify device info.
1352 */
1353 ata_dev_reread_id(ap, dev);
1354 }
1355
Albert Lee8bf62ec2005-05-12 15:29:42 -04001356 if (ata_id_has_lba(dev->id)) {
1357 dev->flags |= ATA_DFLAG_LBA;
1358
1359 if (ata_id_has_lba48(dev->id)) {
1360 dev->flags |= ATA_DFLAG_LBA48;
1361 dev->n_sectors = ata_id_u64(dev->id, 100);
1362 } else {
1363 dev->n_sectors = ata_id_u32(dev->id, 60);
1364 }
1365
1366 /* print device info to dmesg */
1367 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1368 ap->id, device,
1369 major_version,
1370 ata_mode_string(xfer_modes),
1371 (unsigned long long)dev->n_sectors,
1372 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1373 } else {
1374 /* CHS */
1375
1376 /* Default translation */
1377 dev->cylinders = dev->id[1];
1378 dev->heads = dev->id[3];
1379 dev->sectors = dev->id[6];
1380 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1381
1382 if (ata_id_current_chs_valid(dev->id)) {
1383 /* Current CHS translation is valid. */
1384 dev->cylinders = dev->id[54];
1385 dev->heads = dev->id[55];
1386 dev->sectors = dev->id[56];
1387
1388 dev->n_sectors = ata_id_u32(dev->id, 57);
1389 }
1390
1391 /* print device info to dmesg */
1392 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1393 ap->id, device,
1394 major_version,
1395 ata_mode_string(xfer_modes),
1396 (unsigned long long)dev->n_sectors,
1397 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
1401 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
1404 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001405 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (ata_id_is_ata(dev->id)) /* sanity check */
1407 goto err_out_nosup;
1408
1409 rc = atapi_cdb_len(dev->id);
1410 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1411 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1412 goto err_out_nosup;
1413 }
1414 ap->cdb_len = (unsigned int) rc;
1415 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1416
1417 /* print device info to dmesg */
1418 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1419 ap->id, device,
1420 ata_mode_string(xfer_modes));
1421 }
1422
1423 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1424 return;
1425
1426err_out_nosup:
1427 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1428 ap->id, device);
1429err_out:
1430 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1431 DPRINTK("EXIT, err\n");
1432}
1433
Brad Campbell6f2f3812005-05-12 15:07:47 -04001434
Jeff Garzik057ace52005-10-22 14:27:05 -04001435static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001436{
1437 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1438}
1439
1440/**
1441 * ata_dev_config - Run device specific handlers and check for
1442 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001443 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001444 * @i: Device
1445 *
1446 * LOCKING:
1447 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001448
Brad Campbell6f2f3812005-05-12 15:07:47 -04001449void ata_dev_config(struct ata_port *ap, unsigned int i)
1450{
1451 /* limit bridge transfers to udma5, 200 sectors */
1452 if (ata_dev_knobble(ap)) {
1453 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1454 ap->id, ap->device->devno);
1455 ap->udma_mask &= ATA_UDMA5;
1456 ap->host->max_sectors = ATA_MAX_SECTORS;
1457 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001458 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001459 }
1460
1461 if (ap->ops->dev_config)
1462 ap->ops->dev_config(ap, &ap->device[i]);
1463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/**
1466 * ata_bus_probe - Reset and probe ATA bus
1467 * @ap: Bus to probe
1468 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001469 * Master ATA bus probing function. Initiates a hardware-dependent
1470 * bus reset, then attempts to identify any devices found on
1471 * the bus.
1472 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001474 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 *
1476 * RETURNS:
1477 * Zero on success, non-zero on error.
1478 */
1479
1480static int ata_bus_probe(struct ata_port *ap)
1481{
1482 unsigned int i, found = 0;
1483
1484 ap->ops->phy_reset(ap);
1485 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1486 goto err_out;
1487
1488 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1489 ata_dev_identify(ap, i);
1490 if (ata_dev_present(&ap->device[i])) {
1491 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001492 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494 }
1495
1496 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1497 goto err_out_disable;
1498
1499 ata_set_mode(ap);
1500 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1501 goto err_out_disable;
1502
1503 return 0;
1504
1505err_out_disable:
1506 ap->ops->port_disable(ap);
1507err_out:
1508 return -1;
1509}
1510
1511/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001512 * ata_port_probe - Mark port as enabled
1513 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001515 * Modify @ap data structure such that the system
1516 * thinks that the entire port is enabled.
1517 *
1518 * LOCKING: host_set lock, or some other form of
1519 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 */
1521
1522void ata_port_probe(struct ata_port *ap)
1523{
1524 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1525}
1526
1527/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001528 * sata_print_link_status - Print SATA link status
1529 * @ap: SATA port to printk link status about
1530 *
1531 * This function prints link speed and status of a SATA link.
1532 *
1533 * LOCKING:
1534 * None.
1535 */
1536static void sata_print_link_status(struct ata_port *ap)
1537{
1538 u32 sstatus, tmp;
1539 const char *speed;
1540
1541 if (!ap->ops->scr_read)
1542 return;
1543
1544 sstatus = scr_read(ap, SCR_STATUS);
1545
1546 if (sata_dev_present(ap)) {
1547 tmp = (sstatus >> 4) & 0xf;
1548 if (tmp & (1 << 0))
1549 speed = "1.5";
1550 else if (tmp & (1 << 1))
1551 speed = "3.0";
1552 else
1553 speed = "<unknown>";
1554 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1555 ap->id, speed, sstatus);
1556 } else {
1557 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1558 ap->id, sstatus);
1559 }
1560}
1561
1562/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001563 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1564 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001566 * This function issues commands to standard SATA Sxxx
1567 * PHY registers, to wake up the phy (and device), and
1568 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 *
1570 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001571 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 *
1573 */
1574void __sata_phy_reset(struct ata_port *ap)
1575{
1576 u32 sstatus;
1577 unsigned long timeout = jiffies + (HZ * 5);
1578
1579 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001580 /* issue phy wake/reset */
1581 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001582 /* Couldn't find anything in SATA I/II specs, but
1583 * AHCI-1.1 10.4.2 says at least 1 ms. */
1584 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
Brett Russcdcca892005-03-28 15:10:27 -05001586 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /* wait for phy to become ready, if necessary */
1589 do {
1590 msleep(200);
1591 sstatus = scr_read(ap, SCR_STATUS);
1592 if ((sstatus & 0xf) != 1)
1593 break;
1594 } while (time_before(jiffies, timeout));
1595
Tejun Heo3be680b2005-12-19 22:35:02 +09001596 /* print link status */
1597 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001598
Tejun Heo3be680b2005-12-19 22:35:02 +09001599 /* TODO: phy layer with polling, timeouts, etc. */
1600 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001602 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1606 return;
1607
1608 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1609 ata_port_disable(ap);
1610 return;
1611 }
1612
1613 ap->cbl = ATA_CBL_SATA;
1614}
1615
1616/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001617 * sata_phy_reset - Reset SATA bus.
1618 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001620 * This function resets the SATA bus, and then probes
1621 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 *
1623 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001624 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 *
1626 */
1627void sata_phy_reset(struct ata_port *ap)
1628{
1629 __sata_phy_reset(ap);
1630 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1631 return;
1632 ata_bus_reset(ap);
1633}
1634
1635/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001636 * ata_port_disable - Disable port.
1637 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001639 * Modify @ap data structure such that the system
1640 * thinks that the entire port is disabled, and should
1641 * never attempt to probe or communicate with devices
1642 * on this port.
1643 *
1644 * LOCKING: host_set lock, or some other form of
1645 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
1647
1648void ata_port_disable(struct ata_port *ap)
1649{
1650 ap->device[0].class = ATA_DEV_NONE;
1651 ap->device[1].class = ATA_DEV_NONE;
1652 ap->flags |= ATA_FLAG_PORT_DISABLED;
1653}
1654
Alan Cox452503f2005-10-21 19:01:32 -04001655/*
1656 * This mode timing computation functionality is ported over from
1657 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1658 */
1659/*
1660 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1661 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1662 * for PIO 5, which is a nonstandard extension and UDMA6, which
1663 * is currently supported only by Maxtor drives.
1664 */
1665
1666static const struct ata_timing ata_timing[] = {
1667
1668 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1669 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1670 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1671 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1672
1673 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1674 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1675 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1676
1677/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1678
1679 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1680 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1681 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1682
1683 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1684 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1685 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1686
1687/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1688 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1689 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1690
1691 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1692 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1693 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1694
1695/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1696
1697 { 0xFF }
1698};
1699
1700#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1701#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1702
1703static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1704{
1705 q->setup = EZ(t->setup * 1000, T);
1706 q->act8b = EZ(t->act8b * 1000, T);
1707 q->rec8b = EZ(t->rec8b * 1000, T);
1708 q->cyc8b = EZ(t->cyc8b * 1000, T);
1709 q->active = EZ(t->active * 1000, T);
1710 q->recover = EZ(t->recover * 1000, T);
1711 q->cycle = EZ(t->cycle * 1000, T);
1712 q->udma = EZ(t->udma * 1000, UT);
1713}
1714
1715void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1716 struct ata_timing *m, unsigned int what)
1717{
1718 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1719 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1720 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1721 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1722 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1723 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1724 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1725 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1726}
1727
1728static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1729{
1730 const struct ata_timing *t;
1731
1732 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001733 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001734 return NULL;
1735 return t;
1736}
1737
1738int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1739 struct ata_timing *t, int T, int UT)
1740{
1741 const struct ata_timing *s;
1742 struct ata_timing p;
1743
1744 /*
1745 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001746 */
Alan Cox452503f2005-10-21 19:01:32 -04001747
1748 if (!(s = ata_timing_find_mode(speed)))
1749 return -EINVAL;
1750
Albert Lee75b1f2f2005-11-16 17:06:18 +08001751 memcpy(t, s, sizeof(*s));
1752
Alan Cox452503f2005-10-21 19:01:32 -04001753 /*
1754 * If the drive is an EIDE drive, it can tell us it needs extended
1755 * PIO/MW_DMA cycle timing.
1756 */
1757
1758 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1759 memset(&p, 0, sizeof(p));
1760 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1761 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1762 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1763 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1764 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1765 }
1766 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1767 }
1768
1769 /*
1770 * Convert the timing to bus clock counts.
1771 */
1772
Albert Lee75b1f2f2005-11-16 17:06:18 +08001773 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001774
1775 /*
1776 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1777 * and some other commands. We have to ensure that the DMA cycle timing is
1778 * slower/equal than the fastest PIO timing.
1779 */
1780
1781 if (speed > XFER_PIO_4) {
1782 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1783 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1784 }
1785
1786 /*
1787 * Lenghten active & recovery time so that cycle time is correct.
1788 */
1789
1790 if (t->act8b + t->rec8b < t->cyc8b) {
1791 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1792 t->rec8b = t->cyc8b - t->act8b;
1793 }
1794
1795 if (t->active + t->recover < t->cycle) {
1796 t->active += (t->cycle - (t->active + t->recover)) / 2;
1797 t->recover = t->cycle - t->active;
1798 }
1799
1800 return 0;
1801}
1802
Jeff Garzik057ace52005-10-22 14:27:05 -04001803static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 unsigned int shift;
1805 u8 base;
1806} xfer_mode_classes[] = {
1807 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1808 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1809 { ATA_SHIFT_PIO, XFER_PIO_0 },
1810};
1811
Arjan van de Ven858119e2006-01-14 13:20:43 -08001812static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 int i;
1815
1816 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1817 if (xfer_mode_classes[i].shift == shift)
1818 return xfer_mode_classes[i].base;
1819
1820 return 0xff;
1821}
1822
1823static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1824{
1825 int ofs, idx;
1826 u8 base;
1827
1828 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1829 return;
1830
1831 if (dev->xfer_shift == ATA_SHIFT_PIO)
1832 dev->flags |= ATA_DFLAG_PIO;
1833
1834 ata_dev_set_xfermode(ap, dev);
1835
1836 base = base_from_shift(dev->xfer_shift);
1837 ofs = dev->xfer_mode - base;
1838 idx = ofs + dev->xfer_shift;
1839 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1840
1841 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1842 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1843
1844 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1845 ap->id, dev->devno, xfer_mode_str[idx]);
1846}
1847
1848static int ata_host_set_pio(struct ata_port *ap)
1849{
1850 unsigned int mask;
1851 int x, i;
1852 u8 base, xfer_mode;
1853
1854 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1855 x = fgb(mask);
1856 if (x < 0) {
1857 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1858 return -1;
1859 }
1860
1861 base = base_from_shift(ATA_SHIFT_PIO);
1862 xfer_mode = base + x;
1863
1864 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1865 (int)base, (int)xfer_mode, mask, x);
1866
1867 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1868 struct ata_device *dev = &ap->device[i];
1869 if (ata_dev_present(dev)) {
1870 dev->pio_mode = xfer_mode;
1871 dev->xfer_mode = xfer_mode;
1872 dev->xfer_shift = ATA_SHIFT_PIO;
1873 if (ap->ops->set_piomode)
1874 ap->ops->set_piomode(ap, dev);
1875 }
1876 }
1877
1878 return 0;
1879}
1880
1881static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1882 unsigned int xfer_shift)
1883{
1884 int i;
1885
1886 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1887 struct ata_device *dev = &ap->device[i];
1888 if (ata_dev_present(dev)) {
1889 dev->dma_mode = xfer_mode;
1890 dev->xfer_mode = xfer_mode;
1891 dev->xfer_shift = xfer_shift;
1892 if (ap->ops->set_dmamode)
1893 ap->ops->set_dmamode(ap, dev);
1894 }
1895 }
1896}
1897
1898/**
1899 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1900 * @ap: port on which timings will be programmed
1901 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001902 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1903 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001905 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 *
1907 */
1908static void ata_set_mode(struct ata_port *ap)
1909{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001910 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 u8 xfer_mode;
1912 int rc;
1913
1914 /* step 1: always set host PIO timings */
1915 rc = ata_host_set_pio(ap);
1916 if (rc)
1917 goto err_out;
1918
1919 /* step 2: choose the best data xfer mode */
1920 xfer_mode = xfer_shift = 0;
1921 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1922 if (rc)
1923 goto err_out;
1924
1925 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1926 if (xfer_shift != ATA_SHIFT_PIO)
1927 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1928
1929 /* step 4: update devices' xfer mode */
1930 ata_dev_set_mode(ap, &ap->device[0]);
1931 ata_dev_set_mode(ap, &ap->device[1]);
1932
1933 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1934 return;
1935
1936 if (ap->ops->post_set_mode)
1937 ap->ops->post_set_mode(ap);
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return;
1940
1941err_out:
1942 ata_port_disable(ap);
1943}
1944
1945/**
1946 * ata_busy_sleep - sleep until BSY clears, or timeout
1947 * @ap: port containing status register to be polled
1948 * @tmout_pat: impatience timeout
1949 * @tmout: overall timeout
1950 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001951 * Sleep until ATA Status register bit BSY clears,
1952 * or a timeout occurs.
1953 *
1954 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 *
1956 */
1957
1958static unsigned int ata_busy_sleep (struct ata_port *ap,
1959 unsigned long tmout_pat,
1960 unsigned long tmout)
1961{
1962 unsigned long timer_start, timeout;
1963 u8 status;
1964
1965 status = ata_busy_wait(ap, ATA_BUSY, 300);
1966 timer_start = jiffies;
1967 timeout = timer_start + tmout_pat;
1968 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1969 msleep(50);
1970 status = ata_busy_wait(ap, ATA_BUSY, 3);
1971 }
1972
1973 if (status & ATA_BUSY)
1974 printk(KERN_WARNING "ata%u is slow to respond, "
1975 "please be patient\n", ap->id);
1976
1977 timeout = timer_start + tmout;
1978 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1979 msleep(50);
1980 status = ata_chk_status(ap);
1981 }
1982
1983 if (status & ATA_BUSY) {
1984 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1985 ap->id, tmout / HZ);
1986 return 1;
1987 }
1988
1989 return 0;
1990}
1991
1992static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1993{
1994 struct ata_ioports *ioaddr = &ap->ioaddr;
1995 unsigned int dev0 = devmask & (1 << 0);
1996 unsigned int dev1 = devmask & (1 << 1);
1997 unsigned long timeout;
1998
1999 /* if device 0 was found in ata_devchk, wait for its
2000 * BSY bit to clear
2001 */
2002 if (dev0)
2003 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2004
2005 /* if device 1 was found in ata_devchk, wait for
2006 * register access, then wait for BSY to clear
2007 */
2008 timeout = jiffies + ATA_TMOUT_BOOT;
2009 while (dev1) {
2010 u8 nsect, lbal;
2011
2012 ap->ops->dev_select(ap, 1);
2013 if (ap->flags & ATA_FLAG_MMIO) {
2014 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2015 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2016 } else {
2017 nsect = inb(ioaddr->nsect_addr);
2018 lbal = inb(ioaddr->lbal_addr);
2019 }
2020 if ((nsect == 1) && (lbal == 1))
2021 break;
2022 if (time_after(jiffies, timeout)) {
2023 dev1 = 0;
2024 break;
2025 }
2026 msleep(50); /* give drive a breather */
2027 }
2028 if (dev1)
2029 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2030
2031 /* is all this really necessary? */
2032 ap->ops->dev_select(ap, 0);
2033 if (dev1)
2034 ap->ops->dev_select(ap, 1);
2035 if (dev0)
2036 ap->ops->dev_select(ap, 0);
2037}
2038
2039/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002040 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2041 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002043 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2044 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 *
2046 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002047 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05002048 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 *
2050 */
2051
2052static unsigned int ata_bus_edd(struct ata_port *ap)
2053{
2054 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05002055 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 /* set up execute-device-diag (bus reset) taskfile */
2058 /* also, take interrupts to a known state (disabled) */
2059 DPRINTK("execute-device-diag\n");
2060 ata_tf_init(ap, &tf, 0);
2061 tf.ctl |= ATA_NIEN;
2062 tf.command = ATA_CMD_EDD;
2063 tf.protocol = ATA_PROT_NODATA;
2064
2065 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002066 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002068 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 /* spec says at least 2ms. but who knows with those
2071 * crazy ATAPI devices...
2072 */
2073 msleep(150);
2074
2075 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2076}
2077
2078static unsigned int ata_bus_softreset(struct ata_port *ap,
2079 unsigned int devmask)
2080{
2081 struct ata_ioports *ioaddr = &ap->ioaddr;
2082
2083 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2084
2085 /* software reset. causes dev0 to be selected */
2086 if (ap->flags & ATA_FLAG_MMIO) {
2087 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2088 udelay(20); /* FIXME: flush */
2089 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2090 udelay(20); /* FIXME: flush */
2091 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2092 } else {
2093 outb(ap->ctl, ioaddr->ctl_addr);
2094 udelay(10);
2095 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2096 udelay(10);
2097 outb(ap->ctl, ioaddr->ctl_addr);
2098 }
2099
2100 /* spec mandates ">= 2ms" before checking status.
2101 * We wait 150ms, because that was the magic delay used for
2102 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2103 * between when the ATA command register is written, and then
2104 * status is checked. Because waiting for "a while" before
2105 * checking status is fine, post SRST, we perform this magic
2106 * delay here as well.
2107 */
2108 msleep(150);
2109
2110 ata_bus_post_reset(ap, devmask);
2111
2112 return 0;
2113}
2114
2115/**
2116 * ata_bus_reset - reset host port and associated ATA channel
2117 * @ap: port to reset
2118 *
2119 * This is typically the first time we actually start issuing
2120 * commands to the ATA channel. We wait for BSY to clear, then
2121 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2122 * result. Determine what devices, if any, are on the channel
2123 * by looking at the device 0/1 error register. Look at the signature
2124 * stored in each device's taskfile registers, to determine if
2125 * the device is ATA or ATAPI.
2126 *
2127 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002128 * PCI/etc. bus probe sem.
2129 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 *
2131 * SIDE EFFECTS:
2132 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2133 */
2134
2135void ata_bus_reset(struct ata_port *ap)
2136{
2137 struct ata_ioports *ioaddr = &ap->ioaddr;
2138 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2139 u8 err;
2140 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2141
2142 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2143
2144 /* determine if device 0/1 are present */
2145 if (ap->flags & ATA_FLAG_SATA_RESET)
2146 dev0 = 1;
2147 else {
2148 dev0 = ata_devchk(ap, 0);
2149 if (slave_possible)
2150 dev1 = ata_devchk(ap, 1);
2151 }
2152
2153 if (dev0)
2154 devmask |= (1 << 0);
2155 if (dev1)
2156 devmask |= (1 << 1);
2157
2158 /* select device 0 again */
2159 ap->ops->dev_select(ap, 0);
2160
2161 /* issue bus reset */
2162 if (ap->flags & ATA_FLAG_SRST)
2163 rc = ata_bus_softreset(ap, devmask);
2164 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2165 /* set up device control */
2166 if (ap->flags & ATA_FLAG_MMIO)
2167 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2168 else
2169 outb(ap->ctl, ioaddr->ctl_addr);
2170 rc = ata_bus_edd(ap);
2171 }
2172
2173 if (rc)
2174 goto err_out;
2175
2176 /*
2177 * determine by signature whether we have ATA or ATAPI devices
2178 */
2179 err = ata_dev_try_classify(ap, 0);
2180 if ((slave_possible) && (err != 0x81))
2181 ata_dev_try_classify(ap, 1);
2182
2183 /* re-enable interrupts */
2184 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2185 ata_irq_on(ap);
2186
2187 /* is double-select really necessary? */
2188 if (ap->device[1].class != ATA_DEV_NONE)
2189 ap->ops->dev_select(ap, 1);
2190 if (ap->device[0].class != ATA_DEV_NONE)
2191 ap->ops->dev_select(ap, 0);
2192
2193 /* if no devices were detected, disable this port */
2194 if ((ap->device[0].class == ATA_DEV_NONE) &&
2195 (ap->device[1].class == ATA_DEV_NONE))
2196 goto err_out;
2197
2198 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2199 /* set up device control for ATA_FLAG_SATA_RESET */
2200 if (ap->flags & ATA_FLAG_MMIO)
2201 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2202 else
2203 outb(ap->ctl, ioaddr->ctl_addr);
2204 }
2205
2206 DPRINTK("EXIT\n");
2207 return;
2208
2209err_out:
2210 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2211 ap->ops->port_disable(ap);
2212
2213 DPRINTK("EXIT\n");
2214}
2215
Jeff Garzik057ace52005-10-22 14:27:05 -04002216static void ata_pr_blacklisted(const struct ata_port *ap,
2217 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2220 ap->id, dev->devno);
2221}
2222
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002223static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 "WDC AC11000H",
2225 "WDC AC22100H",
2226 "WDC AC32500H",
2227 "WDC AC33100H",
2228 "WDC AC31600H",
2229 "WDC AC32100H",
2230 "WDC AC23200L",
2231 "Compaq CRD-8241B",
2232 "CRD-8400B",
2233 "CRD-8480B",
2234 "CRD-8482B",
2235 "CRD-84",
2236 "SanDisk SDP3B",
2237 "SanDisk SDP3B-64",
2238 "SANYO CD-ROM CRD",
2239 "HITACHI CDR-8",
2240 "HITACHI CDR-8335",
2241 "HITACHI CDR-8435",
2242 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002243 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 "CD-532E-A",
2245 "E-IDE CD-ROM CR-840",
2246 "CD-ROM Drive/F5A",
2247 "WPI CDD-820",
2248 "SAMSUNG CD-ROM SC-148C",
2249 "SAMSUNG CD-ROM SC",
2250 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2252 "_NEC DV5800A",
2253};
2254
Jeff Garzik057ace52005-10-22 14:27:05 -04002255static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 unsigned char model_num[40];
2258 char *s;
2259 unsigned int len;
2260 int i;
2261
2262 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2263 sizeof(model_num));
2264 s = &model_num[0];
2265 len = strnlen(s, sizeof(model_num));
2266
2267 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2268 while ((len > 0) && (s[len - 1] == ' ')) {
2269 len--;
2270 s[len] = 0;
2271 }
2272
2273 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2274 if (!strncmp(ata_dma_blacklist[i], s, len))
2275 return 1;
2276
2277 return 0;
2278}
2279
Jeff Garzik057ace52005-10-22 14:27:05 -04002280static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Jeff Garzik057ace52005-10-22 14:27:05 -04002282 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 unsigned int mask;
2284
2285 master = &ap->device[0];
2286 slave = &ap->device[1];
2287
2288 assert (ata_dev_present(master) || ata_dev_present(slave));
2289
2290 if (shift == ATA_SHIFT_UDMA) {
2291 mask = ap->udma_mask;
2292 if (ata_dev_present(master)) {
2293 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002294 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 mask = 0;
2296 ata_pr_blacklisted(ap, master);
2297 }
2298 }
2299 if (ata_dev_present(slave)) {
2300 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002301 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 mask = 0;
2303 ata_pr_blacklisted(ap, slave);
2304 }
2305 }
2306 }
2307 else if (shift == ATA_SHIFT_MWDMA) {
2308 mask = ap->mwdma_mask;
2309 if (ata_dev_present(master)) {
2310 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002311 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 mask = 0;
2313 ata_pr_blacklisted(ap, master);
2314 }
2315 }
2316 if (ata_dev_present(slave)) {
2317 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002318 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 mask = 0;
2320 ata_pr_blacklisted(ap, slave);
2321 }
2322 }
2323 }
2324 else if (shift == ATA_SHIFT_PIO) {
2325 mask = ap->pio_mask;
2326 if (ata_dev_present(master)) {
2327 /* spec doesn't return explicit support for
2328 * PIO0-2, so we fake it
2329 */
2330 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2331 tmp_mode <<= 3;
2332 tmp_mode |= 0x7;
2333 mask &= tmp_mode;
2334 }
2335 if (ata_dev_present(slave)) {
2336 /* spec doesn't return explicit support for
2337 * PIO0-2, so we fake it
2338 */
2339 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2340 tmp_mode <<= 3;
2341 tmp_mode |= 0x7;
2342 mask &= tmp_mode;
2343 }
2344 }
2345 else {
2346 mask = 0xffffffff; /* shut up compiler warning */
2347 BUG();
2348 }
2349
2350 return mask;
2351}
2352
2353/* find greatest bit */
2354static int fgb(u32 bitmap)
2355{
2356 unsigned int i;
2357 int x = -1;
2358
2359 for (i = 0; i < 32; i++)
2360 if (bitmap & (1 << i))
2361 x = i;
2362
2363 return x;
2364}
2365
2366/**
2367 * ata_choose_xfer_mode - attempt to find best transfer mode
2368 * @ap: Port for which an xfer mode will be selected
2369 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2370 * @xfer_shift_out: (output) bit shift that selects this mode
2371 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002372 * Based on host and device capabilities, determine the
2373 * maximum transfer mode that is amenable to all.
2374 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002376 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 *
2378 * RETURNS:
2379 * Zero on success, negative on error.
2380 */
2381
Jeff Garzik057ace52005-10-22 14:27:05 -04002382static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 u8 *xfer_mode_out,
2384 unsigned int *xfer_shift_out)
2385{
2386 unsigned int mask, shift;
2387 int x, i;
2388
2389 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2390 shift = xfer_mode_classes[i].shift;
2391 mask = ata_get_mode_mask(ap, shift);
2392
2393 x = fgb(mask);
2394 if (x >= 0) {
2395 *xfer_mode_out = xfer_mode_classes[i].base + x;
2396 *xfer_shift_out = shift;
2397 return 0;
2398 }
2399 }
2400
2401 return -1;
2402}
2403
2404/**
2405 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2406 * @ap: Port associated with device @dev
2407 * @dev: Device to which command will be sent
2408 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002409 * Issue SET FEATURES - XFER MODE command to device @dev
2410 * on port @ap.
2411 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002413 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 */
2415
2416static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2417{
Tejun Heoa0123702005-12-13 14:49:31 +09002418 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 /* set up set-features taskfile */
2421 DPRINTK("set features - xfer mode\n");
2422
Tejun Heoa0123702005-12-13 14:49:31 +09002423 ata_tf_init(ap, &tf, dev->devno);
2424 tf.command = ATA_CMD_SET_FEATURES;
2425 tf.feature = SETFEATURES_XFER;
2426 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2427 tf.protocol = ATA_PROT_NODATA;
2428 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Tejun Heoa0123702005-12-13 14:49:31 +09002430 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2431 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2432 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 DPRINTK("EXIT\n");
2437}
2438
2439/**
Albert Lee59a10b12005-10-12 15:09:42 +08002440 * ata_dev_reread_id - Reread the device identify device info
2441 * @ap: port where the device is
2442 * @dev: device to reread the identify device info
2443 *
2444 * LOCKING:
2445 */
2446
2447static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2448{
Tejun Heoa0123702005-12-13 14:49:31 +09002449 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002450
Tejun Heoa0123702005-12-13 14:49:31 +09002451 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002452
2453 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002454 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002455 DPRINTK("do ATA identify\n");
2456 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002457 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002458 DPRINTK("do ATAPI identify\n");
2459 }
2460
Tejun Heoa0123702005-12-13 14:49:31 +09002461 tf.flags |= ATA_TFLAG_DEVICE;
2462 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002463
Tejun Heoa0123702005-12-13 14:49:31 +09002464 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2465 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002466 goto err_out;
2467
Albert Lee59a10b12005-10-12 15:09:42 +08002468 swap_buf_le16(dev->id, ATA_ID_WORDS);
2469
2470 ata_dump_id(dev);
2471
2472 DPRINTK("EXIT\n");
2473
2474 return;
2475err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002476 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002477 ata_port_disable(ap);
2478}
2479
2480/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002481 * ata_dev_init_params - Issue INIT DEV PARAMS command
2482 * @ap: Port associated with device @dev
2483 * @dev: Device to which command will be sent
2484 *
2485 * LOCKING:
2486 */
2487
2488static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2489{
Tejun Heoa0123702005-12-13 14:49:31 +09002490 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002491 u16 sectors = dev->id[6];
2492 u16 heads = dev->id[3];
2493
2494 /* Number of sectors per track 1-255. Number of heads 1-16 */
2495 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2496 return;
2497
2498 /* set up init dev params taskfile */
2499 DPRINTK("init dev params \n");
2500
Tejun Heoa0123702005-12-13 14:49:31 +09002501 ata_tf_init(ap, &tf, dev->devno);
2502 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2503 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2504 tf.protocol = ATA_PROT_NODATA;
2505 tf.nsect = sectors;
2506 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002507
Tejun Heoa0123702005-12-13 14:49:31 +09002508 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2509 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2510 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002511 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002512 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002513
2514 DPRINTK("EXIT\n");
2515}
2516
2517/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002518 * ata_sg_clean - Unmap DMA memory associated with command
2519 * @qc: Command containing DMA memory to be released
2520 *
2521 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 *
2523 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002524 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 */
2526
2527static void ata_sg_clean(struct ata_queued_cmd *qc)
2528{
2529 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002530 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002532 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2535 assert(sg != NULL);
2536
2537 if (qc->flags & ATA_QCFLAG_SINGLE)
2538 assert(qc->n_elem == 1);
2539
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002540 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002542 /* if we padded the buffer out to 32-bit bound, and data
2543 * xfer direction is from-device, we must copy from the
2544 * pad buffer back into the supplied buffer
2545 */
2546 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2547 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2548
2549 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002550 if (qc->n_elem)
2551 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002552 /* restore last sg */
2553 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2554 if (pad_buf) {
2555 struct scatterlist *psg = &qc->pad_sgent;
2556 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2557 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002558 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002559 }
2560 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002561 if (sg_dma_len(&sg[0]) > 0)
2562 dma_unmap_single(ap->host_set->dev,
2563 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2564 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002565 /* restore sg */
2566 sg->length += qc->pad_len;
2567 if (pad_buf)
2568 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2569 pad_buf, qc->pad_len);
2570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002573 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
2576/**
2577 * ata_fill_sg - Fill PCI IDE PRD table
2578 * @qc: Metadata associated with taskfile to be transferred
2579 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002580 * Fill PCI IDE PRD (scatter-gather) table with segments
2581 * associated with the current disk command.
2582 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002584 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 *
2586 */
2587static void ata_fill_sg(struct ata_queued_cmd *qc)
2588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002590 struct scatterlist *sg;
2591 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002593 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 assert(qc->n_elem > 0);
2595
2596 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002597 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 u32 addr, offset;
2599 u32 sg_len, len;
2600
2601 /* determine if physical DMA addr spans 64K boundary.
2602 * Note h/w doesn't support 64-bit, so we unconditionally
2603 * truncate dma_addr_t to u32.
2604 */
2605 addr = (u32) sg_dma_address(sg);
2606 sg_len = sg_dma_len(sg);
2607
2608 while (sg_len) {
2609 offset = addr & 0xffff;
2610 len = sg_len;
2611 if ((offset + sg_len) > 0x10000)
2612 len = 0x10000 - offset;
2613
2614 ap->prd[idx].addr = cpu_to_le32(addr);
2615 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2616 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2617
2618 idx++;
2619 sg_len -= len;
2620 addr += len;
2621 }
2622 }
2623
2624 if (idx)
2625 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2626}
2627/**
2628 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2629 * @qc: Metadata associated with taskfile to check
2630 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002631 * Allow low-level driver to filter ATA PACKET commands, returning
2632 * a status indicating whether or not it is OK to use DMA for the
2633 * supplied PACKET command.
2634 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002636 * spin_lock_irqsave(host_set lock)
2637 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 * RETURNS: 0 when ATAPI DMA can be used
2639 * nonzero otherwise
2640 */
2641int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2642{
2643 struct ata_port *ap = qc->ap;
2644 int rc = 0; /* Assume ATAPI DMA is OK by default */
2645
2646 if (ap->ops->check_atapi_dma)
2647 rc = ap->ops->check_atapi_dma(qc);
2648
2649 return rc;
2650}
2651/**
2652 * ata_qc_prep - Prepare taskfile for submission
2653 * @qc: Metadata associated with taskfile to be prepared
2654 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002655 * Prepare ATA taskfile for submission.
2656 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 * LOCKING:
2658 * spin_lock_irqsave(host_set lock)
2659 */
2660void ata_qc_prep(struct ata_queued_cmd *qc)
2661{
2662 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2663 return;
2664
2665 ata_fill_sg(qc);
2666}
2667
Jeff Garzik0cba6322005-05-30 19:49:12 -04002668/**
2669 * ata_sg_init_one - Associate command with memory buffer
2670 * @qc: Command to be associated
2671 * @buf: Memory buffer
2672 * @buflen: Length of memory buffer, in bytes.
2673 *
2674 * Initialize the data-related elements of queued_cmd @qc
2675 * to point to a single memory buffer, @buf of byte length @buflen.
2676 *
2677 * LOCKING:
2678 * spin_lock_irqsave(host_set lock)
2679 */
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2682{
2683 struct scatterlist *sg;
2684
2685 qc->flags |= ATA_QCFLAG_SINGLE;
2686
2687 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002688 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002690 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 qc->buf_virt = buf;
2692
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002693 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002694 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
Jeff Garzik0cba6322005-05-30 19:49:12 -04002697/**
2698 * ata_sg_init - Associate command with scatter-gather table.
2699 * @qc: Command to be associated
2700 * @sg: Scatter-gather table.
2701 * @n_elem: Number of elements in s/g table.
2702 *
2703 * Initialize the data-related elements of queued_cmd @qc
2704 * to point to a scatter-gather table @sg, containing @n_elem
2705 * elements.
2706 *
2707 * LOCKING:
2708 * spin_lock_irqsave(host_set lock)
2709 */
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2712 unsigned int n_elem)
2713{
2714 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002715 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002717 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
2720/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002721 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2722 * @qc: Command with memory buffer to be mapped.
2723 *
2724 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 *
2726 * LOCKING:
2727 * spin_lock_irqsave(host_set lock)
2728 *
2729 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002730 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 */
2732
2733static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2734{
2735 struct ata_port *ap = qc->ap;
2736 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002737 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 dma_addr_t dma_address;
2739
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002740 /* we must lengthen transfers to end on a 32-bit boundary */
2741 qc->pad_len = sg->length & 3;
2742 if (qc->pad_len) {
2743 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2744 struct scatterlist *psg = &qc->pad_sgent;
2745
2746 assert(qc->dev->class == ATA_DEV_ATAPI);
2747
2748 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2749
2750 if (qc->tf.flags & ATA_TFLAG_WRITE)
2751 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2752 qc->pad_len);
2753
2754 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2755 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2756 /* trim sg */
2757 sg->length -= qc->pad_len;
2758
2759 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2760 sg->length, qc->pad_len);
2761 }
2762
Jeff Garzike1410f22005-11-14 14:06:26 -05002763 if (!sg->length) {
2764 sg_dma_address(sg) = 0;
2765 goto skip_map;
2766 }
2767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002769 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002770 if (dma_mapping_error(dma_address)) {
2771 /* restore sg */
2772 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002777skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002778 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2781 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2782
2783 return 0;
2784}
2785
2786/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002787 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2788 * @qc: Command with scatter-gather table to be mapped.
2789 *
2790 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 *
2792 * LOCKING:
2793 * spin_lock_irqsave(host_set lock)
2794 *
2795 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002796 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 *
2798 */
2799
2800static int ata_sg_setup(struct ata_queued_cmd *qc)
2801{
2802 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002803 struct scatterlist *sg = qc->__sg;
2804 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002805 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 VPRINTK("ENTER, ata%u\n", ap->id);
2808 assert(qc->flags & ATA_QCFLAG_SG);
2809
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002810 /* we must lengthen transfers to end on a 32-bit boundary */
2811 qc->pad_len = lsg->length & 3;
2812 if (qc->pad_len) {
2813 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2814 struct scatterlist *psg = &qc->pad_sgent;
2815 unsigned int offset;
2816
2817 assert(qc->dev->class == ATA_DEV_ATAPI);
2818
2819 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2820
2821 /*
2822 * psg->page/offset are used to copy to-be-written
2823 * data in this function or read data in ata_sg_clean.
2824 */
2825 offset = lsg->offset + lsg->length - qc->pad_len;
2826 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2827 psg->offset = offset_in_page(offset);
2828
2829 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2830 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2831 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002832 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002833 }
2834
2835 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2836 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2837 /* trim last sg */
2838 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002839 if (lsg->length == 0)
2840 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002841
2842 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2843 qc->n_elem - 1, lsg->length, qc->pad_len);
2844 }
2845
Jeff Garzike1410f22005-11-14 14:06:26 -05002846 pre_n_elem = qc->n_elem;
2847 if (trim_sg && pre_n_elem)
2848 pre_n_elem--;
2849
2850 if (!pre_n_elem) {
2851 n_elem = 0;
2852 goto skip_map;
2853 }
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002856 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002857 if (n_elem < 1) {
2858 /* restore last sg */
2859 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 DPRINTK("%d sg elements mapped\n", n_elem);
2864
Jeff Garzike1410f22005-11-14 14:06:26 -05002865skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 qc->n_elem = n_elem;
2867
2868 return 0;
2869}
2870
2871/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002872 * ata_poll_qc_complete - turn irq back on and finish qc
2873 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002874 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002875 *
2876 * LOCKING:
2877 * None. (grabs host lock)
2878 */
2879
Albert Leea22e2eb2005-12-05 15:38:02 +08002880void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002881{
2882 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002883 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002884
Jeff Garzikb8f61532005-08-25 22:01:20 -04002885 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002886 ap->flags &= ~ATA_FLAG_NOINTR;
2887 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002888 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002889 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002890}
2891
2892/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002894 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 *
2896 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002897 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 *
2899 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002900 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 */
2902
2903static unsigned long ata_pio_poll(struct ata_port *ap)
2904{
Albert Leec14b8332005-12-05 15:36:08 +08002905 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002907 unsigned int poll_state = HSM_ST_UNKNOWN;
2908 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Albert Leec14b8332005-12-05 15:36:08 +08002910 qc = ata_qc_from_tag(ap, ap->active_tag);
2911 assert(qc != NULL);
2912
Albert Lee14be71f2005-09-27 17:36:35 +08002913 switch (ap->hsm_task_state) {
2914 case HSM_ST:
2915 case HSM_ST_POLL:
2916 poll_state = HSM_ST_POLL;
2917 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002919 case HSM_ST_LAST:
2920 case HSM_ST_LAST_POLL:
2921 poll_state = HSM_ST_LAST_POLL;
2922 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 break;
2924 default:
2925 BUG();
2926 break;
2927 }
2928
2929 status = ata_chk_status(ap);
2930 if (status & ATA_BUSY) {
2931 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee1c848982005-12-05 15:40:15 +08002932 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee7c398332005-11-09 13:03:30 +08002933 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return 0;
2935 }
Albert Lee14be71f2005-09-27 17:36:35 +08002936 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return ATA_SHORT_PAUSE;
2938 }
2939
Albert Lee14be71f2005-09-27 17:36:35 +08002940 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return 0;
2942}
2943
2944/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002945 * ata_pio_complete - check if drive is busy or idle
2946 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 *
2948 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002949 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002950 *
2951 * RETURNS:
2952 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 */
2954
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002955static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 struct ata_queued_cmd *qc;
2958 u8 drv_stat;
2959
2960 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002961 * This is purely heuristic. This is a fast path. Sometimes when
2962 * we enter, BSY will be cleared in a chk-status or two. If not,
2963 * the drive is probably seeking or something. Snooze for a couple
2964 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002965 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 */
Albert Leefe79e682005-12-06 11:34:59 +08002967 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2968 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002970 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2971 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002972 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
2976 }
2977
Albert Leec14b8332005-12-05 15:36:08 +08002978 qc = ata_qc_from_tag(ap, ap->active_tag);
2979 assert(qc != NULL);
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 drv_stat = ata_wait_idle(ap);
2982 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002983 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002984 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987
Albert Lee14be71f2005-09-27 17:36:35 +08002988 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Albert Leea22e2eb2005-12-05 15:38:02 +08002990 assert(qc->err_mask == 0);
2991 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002992
2993 /* another command may start at this point */
2994
2995 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
2997
Edward Falk0baab862005-06-02 18:17:13 -04002998
2999/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003000 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04003001 * @buf: Buffer to swap
3002 * @buf_words: Number of 16-bit words in buffer.
3003 *
3004 * Swap halves of 16-bit words if needed to convert from
3005 * little-endian byte order to native cpu byte order, or
3006 * vice-versa.
3007 *
3008 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003009 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011void swap_buf_le16(u16 *buf, unsigned int buf_words)
3012{
3013#ifdef __BIG_ENDIAN
3014 unsigned int i;
3015
3016 for (i = 0; i < buf_words; i++)
3017 buf[i] = le16_to_cpu(buf[i]);
3018#endif /* __BIG_ENDIAN */
3019}
3020
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003021/**
3022 * ata_mmio_data_xfer - Transfer data by MMIO
3023 * @ap: port to read/write
3024 * @buf: data buffer
3025 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003026 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003027 *
3028 * Transfer data from/to the device data register by MMIO.
3029 *
3030 * LOCKING:
3031 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003032 */
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3035 unsigned int buflen, int write_data)
3036{
3037 unsigned int i;
3038 unsigned int words = buflen >> 1;
3039 u16 *buf16 = (u16 *) buf;
3040 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3041
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003042 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 if (write_data) {
3044 for (i = 0; i < words; i++)
3045 writew(le16_to_cpu(buf16[i]), mmio);
3046 } else {
3047 for (i = 0; i < words; i++)
3048 buf16[i] = cpu_to_le16(readw(mmio));
3049 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003050
3051 /* Transfer trailing 1 byte, if any. */
3052 if (unlikely(buflen & 0x01)) {
3053 u16 align_buf[1] = { 0 };
3054 unsigned char *trailing_buf = buf + buflen - 1;
3055
3056 if (write_data) {
3057 memcpy(align_buf, trailing_buf, 1);
3058 writew(le16_to_cpu(align_buf[0]), mmio);
3059 } else {
3060 align_buf[0] = cpu_to_le16(readw(mmio));
3061 memcpy(trailing_buf, align_buf, 1);
3062 }
3063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003066/**
3067 * ata_pio_data_xfer - Transfer data by PIO
3068 * @ap: port to read/write
3069 * @buf: data buffer
3070 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003071 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003072 *
3073 * Transfer data from/to the device data register by PIO.
3074 *
3075 * LOCKING:
3076 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003077 */
3078
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3080 unsigned int buflen, int write_data)
3081{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003082 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003084 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003086 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003088 insw(ap->ioaddr.data_addr, buf, words);
3089
3090 /* Transfer trailing 1 byte, if any. */
3091 if (unlikely(buflen & 0x01)) {
3092 u16 align_buf[1] = { 0 };
3093 unsigned char *trailing_buf = buf + buflen - 1;
3094
3095 if (write_data) {
3096 memcpy(align_buf, trailing_buf, 1);
3097 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3098 } else {
3099 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3100 memcpy(trailing_buf, align_buf, 1);
3101 }
3102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
3104
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003105/**
3106 * ata_data_xfer - Transfer data from/to the data register.
3107 * @ap: port to read/write
3108 * @buf: data buffer
3109 * @buflen: buffer length
3110 * @do_write: read/write
3111 *
3112 * Transfer data from/to the device data register.
3113 *
3114 * LOCKING:
3115 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003116 */
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3119 unsigned int buflen, int do_write)
3120{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003121 /* Make the crap hardware pay the costs not the good stuff */
3122 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3123 unsigned long flags;
3124 local_irq_save(flags);
3125 if (ap->flags & ATA_FLAG_MMIO)
3126 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3127 else
3128 ata_pio_data_xfer(ap, buf, buflen, do_write);
3129 local_irq_restore(flags);
3130 } else {
3131 if (ap->flags & ATA_FLAG_MMIO)
3132 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3133 else
3134 ata_pio_data_xfer(ap, buf, buflen, do_write);
3135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003138/**
3139 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3140 * @qc: Command on going
3141 *
3142 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3143 *
3144 * LOCKING:
3145 * Inherited from caller.
3146 */
3147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148static void ata_pio_sector(struct ata_queued_cmd *qc)
3149{
3150 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003151 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 struct ata_port *ap = qc->ap;
3153 struct page *page;
3154 unsigned int offset;
3155 unsigned char *buf;
3156
3157 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003158 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 page = sg[qc->cursg].page;
3161 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3162
3163 /* get the current page and offset */
3164 page = nth_page(page, (offset >> PAGE_SHIFT));
3165 offset %= PAGE_SIZE;
3166
3167 buf = kmap(page) + offset;
3168
3169 qc->cursect++;
3170 qc->cursg_ofs++;
3171
Albert Lee32529e02005-05-26 03:49:42 -04003172 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 qc->cursg++;
3174 qc->cursg_ofs = 0;
3175 }
3176
3177 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3178
3179 /* do the actual data transfer */
3180 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3181 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3182
3183 kunmap(page);
3184}
3185
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003186/**
3187 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3188 * @qc: Command on going
3189 * @bytes: number of bytes
3190 *
3191 * Transfer Transfer data from/to the ATAPI device.
3192 *
3193 * LOCKING:
3194 * Inherited from caller.
3195 *
3196 */
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3199{
3200 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003201 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 struct ata_port *ap = qc->ap;
3203 struct page *page;
3204 unsigned char *buf;
3205 unsigned int offset, count;
3206
Albert Lee563a6e12005-08-12 14:17:50 +08003207 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003208 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003211 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003212 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003213 * The end of qc->sg is reached and the device expects
3214 * more data to transfer. In order not to overrun qc->sg
3215 * and fulfill length specified in the byte count register,
3216 * - for read case, discard trailing data from the device
3217 * - for write case, padding zero data to the device
3218 */
3219 u16 pad_buf[1] = { 0 };
3220 unsigned int words = bytes >> 1;
3221 unsigned int i;
3222
3223 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003224 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003225 ap->id, bytes);
3226
3227 for (i = 0; i < words; i++)
3228 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3229
Albert Lee14be71f2005-09-27 17:36:35 +08003230 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003231 return;
3232 }
3233
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003234 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 page = sg->page;
3237 offset = sg->offset + qc->cursg_ofs;
3238
3239 /* get the current page and offset */
3240 page = nth_page(page, (offset >> PAGE_SHIFT));
3241 offset %= PAGE_SIZE;
3242
Albert Lee6952df02005-06-06 15:56:03 +08003243 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003244 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 /* don't cross page boundaries */
3247 count = min(count, (unsigned int)PAGE_SIZE - offset);
3248
3249 buf = kmap(page) + offset;
3250
3251 bytes -= count;
3252 qc->curbytes += count;
3253 qc->cursg_ofs += count;
3254
Albert Lee32529e02005-05-26 03:49:42 -04003255 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 qc->cursg++;
3257 qc->cursg_ofs = 0;
3258 }
3259
3260 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3261
3262 /* do the actual data transfer */
3263 ata_data_xfer(ap, buf, count, do_write);
3264
3265 kunmap(page);
3266
Albert Lee563a6e12005-08-12 14:17:50 +08003267 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
3270
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003271/**
3272 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3273 * @qc: Command on going
3274 *
3275 * Transfer Transfer data from/to the ATAPI device.
3276 *
3277 * LOCKING:
3278 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003279 */
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3282{
3283 struct ata_port *ap = qc->ap;
3284 struct ata_device *dev = qc->dev;
3285 unsigned int ireason, bc_lo, bc_hi, bytes;
3286 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3287
3288 ap->ops->tf_read(ap, &qc->tf);
3289 ireason = qc->tf.nsect;
3290 bc_lo = qc->tf.lbam;
3291 bc_hi = qc->tf.lbah;
3292 bytes = (bc_hi << 8) | bc_lo;
3293
3294 /* shall be cleared to zero, indicating xfer of data */
3295 if (ireason & (1 << 0))
3296 goto err_out;
3297
3298 /* make sure transfer direction matches expected */
3299 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3300 if (do_write != i_write)
3301 goto err_out;
3302
3303 __atapi_pio_bytes(qc, bytes);
3304
3305 return;
3306
3307err_out:
3308 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3309 ap->id, dev->devno);
Albert Lee1c848982005-12-05 15:40:15 +08003310 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003311 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
3314/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003315 * ata_pio_block - start PIO on a block
3316 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 *
3318 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003319 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 */
3321
3322static void ata_pio_block(struct ata_port *ap)
3323{
3324 struct ata_queued_cmd *qc;
3325 u8 status;
3326
3327 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003328 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 * Sometimes when we enter, BSY will be cleared in
3330 * a chk-status or two. If not, the drive is probably seeking
3331 * or something. Snooze for a couple msecs, then
3332 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003333 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 */
3335 status = ata_busy_wait(ap, ATA_BUSY, 5);
3336 if (status & ATA_BUSY) {
3337 msleep(2);
3338 status = ata_busy_wait(ap, ATA_BUSY, 10);
3339 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003340 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3342 return;
3343 }
3344 }
3345
3346 qc = ata_qc_from_tag(ap, ap->active_tag);
3347 assert(qc != NULL);
3348
Albert Leefe79e682005-12-06 11:34:59 +08003349 /* check error */
3350 if (status & (ATA_ERR | ATA_DF)) {
3351 qc->err_mask |= AC_ERR_DEV;
3352 ap->hsm_task_state = HSM_ST_ERR;
3353 return;
3354 }
3355
3356 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003358 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003360 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return;
3362 }
3363
3364 atapi_pio_bytes(qc);
3365 } else {
3366 /* handle BSY=0, DRQ=0 as error */
3367 if ((status & ATA_DRQ) == 0) {
Albert Lee1c848982005-12-05 15:40:15 +08003368 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003369 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 return;
3371 }
3372
3373 ata_pio_sector(qc);
3374 }
3375}
3376
3377static void ata_pio_error(struct ata_port *ap)
3378{
3379 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003380
3381 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
3383 qc = ata_qc_from_tag(ap, ap->active_tag);
3384 assert(qc != NULL);
3385
Albert Lee1c848982005-12-05 15:40:15 +08003386 /* make sure qc->err_mask is available to
3387 * know what's wrong and recover
3388 */
3389 assert(qc->err_mask);
3390
Albert Lee14be71f2005-09-27 17:36:35 +08003391 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Albert Leea22e2eb2005-12-05 15:38:02 +08003393 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394}
3395
3396static void ata_pio_task(void *_data)
3397{
3398 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003399 unsigned long timeout;
3400 int qc_completed;
3401
3402fsm_start:
3403 timeout = 0;
3404 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
Albert Lee14be71f2005-09-27 17:36:35 +08003406 switch (ap->hsm_task_state) {
3407 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return;
3409
Albert Lee14be71f2005-09-27 17:36:35 +08003410 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 ata_pio_block(ap);
3412 break;
3413
Albert Lee14be71f2005-09-27 17:36:35 +08003414 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003415 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 break;
3417
Albert Lee14be71f2005-09-27 17:36:35 +08003418 case HSM_ST_POLL:
3419 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 timeout = ata_pio_poll(ap);
3421 break;
3422
Albert Lee14be71f2005-09-27 17:36:35 +08003423 case HSM_ST_TMOUT:
3424 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 ata_pio_error(ap);
3426 return;
3427 }
3428
3429 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003430 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3431 else if (!qc_completed)
3432 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433}
3434
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435/**
3436 * ata_qc_timeout - Handle timeout of queued command
3437 * @qc: Command that timed out
3438 *
3439 * Some part of the kernel (currently, only the SCSI layer)
3440 * has noticed that the active command on port @ap has not
3441 * completed after a specified length of time. Handle this
3442 * condition by disabling DMA (if necessary) and completing
3443 * transactions, with error if necessary.
3444 *
3445 * This also handles the case of the "lost interrupt", where
3446 * for some reason (possibly hardware bug, possibly driver bug)
3447 * an interrupt was not delivered to the driver, even though the
3448 * transaction completed successfully.
3449 *
3450 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003451 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 */
3453
3454static void ata_qc_timeout(struct ata_queued_cmd *qc)
3455{
3456 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003457 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003459 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 DPRINTK("ENTER\n");
3462
Jeff Garzikb8f61532005-08-25 22:01:20 -04003463 spin_lock_irqsave(&host_set->lock, flags);
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 /* hack alert! We cannot use the supplied completion
3466 * function from inside the ->eh_strategy_handler() thread.
3467 * libata is the only user of ->eh_strategy_handler() in
3468 * any kernel, so the default scsi_done() assumes it is
3469 * not being called from the SCSI EH.
3470 */
3471 qc->scsidone = scsi_finish_command;
3472
3473 switch (qc->tf.protocol) {
3474
3475 case ATA_PROT_DMA:
3476 case ATA_PROT_ATAPI_DMA:
3477 host_stat = ap->ops->bmdma_status(ap);
3478
3479 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003480 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
3482 /* fall through */
3483
3484 default:
3485 ata_altstatus(ap);
3486 drv_stat = ata_chk_status(ap);
3487
3488 /* ack bmdma irq events */
3489 ap->ops->irq_clear(ap);
3490
3491 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3492 ap->id, qc->tf.command, drv_stat, host_stat);
3493
3494 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003495 qc->err_mask |= ac_err_mask(drv_stat);
3496 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 break;
3498 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003499
3500 spin_unlock_irqrestore(&host_set->lock, flags);
3501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 DPRINTK("EXIT\n");
3503}
3504
3505/**
3506 * ata_eng_timeout - Handle timeout of queued command
3507 * @ap: Port on which timed-out command is active
3508 *
3509 * Some part of the kernel (currently, only the SCSI layer)
3510 * has noticed that the active command on port @ap has not
3511 * completed after a specified length of time. Handle this
3512 * condition by disabling DMA (if necessary) and completing
3513 * transactions, with error if necessary.
3514 *
3515 * This also handles the case of the "lost interrupt", where
3516 * for some reason (possibly hardware bug, possibly driver bug)
3517 * an interrupt was not delivered to the driver, even though the
3518 * transaction completed successfully.
3519 *
3520 * LOCKING:
3521 * Inherited from SCSI layer (none, can sleep)
3522 */
3523
3524void ata_eng_timeout(struct ata_port *ap)
3525{
3526 struct ata_queued_cmd *qc;
3527
3528 DPRINTK("ENTER\n");
3529
3530 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003531 if (qc)
3532 ata_qc_timeout(qc);
3533 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3535 ap->id);
3536 goto out;
3537 }
3538
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539out:
3540 DPRINTK("EXIT\n");
3541}
3542
3543/**
3544 * ata_qc_new - Request an available ATA command, for queueing
3545 * @ap: Port associated with device @dev
3546 * @dev: Device from whom we request an available command structure
3547 *
3548 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003549 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 */
3551
3552static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3553{
3554 struct ata_queued_cmd *qc = NULL;
3555 unsigned int i;
3556
3557 for (i = 0; i < ATA_MAX_QUEUE; i++)
3558 if (!test_and_set_bit(i, &ap->qactive)) {
3559 qc = ata_qc_from_tag(ap, i);
3560 break;
3561 }
3562
3563 if (qc)
3564 qc->tag = i;
3565
3566 return qc;
3567}
3568
3569/**
3570 * ata_qc_new_init - Request an available ATA command, and initialize it
3571 * @ap: Port associated with device @dev
3572 * @dev: Device from whom we request an available command structure
3573 *
3574 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003575 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 */
3577
3578struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3579 struct ata_device *dev)
3580{
3581 struct ata_queued_cmd *qc;
3582
3583 qc = ata_qc_new(ap);
3584 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 qc->scsicmd = NULL;
3586 qc->ap = ap;
3587 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003589 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 }
3591
3592 return qc;
3593}
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595/**
3596 * ata_qc_free - free unused ata_queued_cmd
3597 * @qc: Command to complete
3598 *
3599 * Designed to free unused ata_queued_cmd object
3600 * in case something prevents using it.
3601 *
3602 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003603 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 */
3605void ata_qc_free(struct ata_queued_cmd *qc)
3606{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003607 struct ata_port *ap = qc->ap;
3608 unsigned int tag;
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Tejun Heo4ba946e2006-01-23 13:09:36 +09003612 qc->flags = 0;
3613 tag = qc->tag;
3614 if (likely(ata_tag_valid(tag))) {
3615 if (tag == ap->active_tag)
3616 ap->active_tag = ATA_TAG_POISON;
3617 qc->tag = ATA_TAG_POISON;
3618 clear_bit(tag, &ap->qactive);
3619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
3621
3622/**
3623 * ata_qc_complete - Complete an active ATA command
3624 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003625 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003626 *
3627 * Indicate to the mid and upper layers that an ATA
3628 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 *
3630 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003631 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 */
3633
Albert Leea22e2eb2005-12-05 15:38:02 +08003634void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
3636 int rc;
3637
3638 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3639 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3640
3641 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3642 ata_sg_clean(qc);
3643
Albert Lee3f3791d2005-08-16 14:25:38 +08003644 /* atapi: mark qc as inactive to prevent the interrupt handler
3645 * from completing the command twice later, before the error handler
3646 * is called. (when rc != 0 and atapi request sense is needed)
3647 */
3648 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 /* call completion callback */
Albert Leea22e2eb2005-12-05 15:38:02 +08003651 rc = qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
3653 /* if callback indicates not to complete command (non-zero),
3654 * return immediately
3655 */
3656 if (rc != 0)
3657 return;
3658
Tejun Heo4ba946e2006-01-23 13:09:36 +09003659 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
3661 VPRINTK("EXIT\n");
3662}
3663
3664static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3665{
3666 struct ata_port *ap = qc->ap;
3667
3668 switch (qc->tf.protocol) {
3669 case ATA_PROT_DMA:
3670 case ATA_PROT_ATAPI_DMA:
3671 return 1;
3672
3673 case ATA_PROT_ATAPI:
3674 case ATA_PROT_PIO:
3675 case ATA_PROT_PIO_MULT:
3676 if (ap->flags & ATA_FLAG_PIO_DMA)
3677 return 1;
3678
3679 /* fall through */
3680
3681 default:
3682 return 0;
3683 }
3684
3685 /* never reached */
3686}
3687
3688/**
3689 * ata_qc_issue - issue taskfile to device
3690 * @qc: command to issue to device
3691 *
3692 * Prepare an ATA command to submission to device.
3693 * This includes mapping the data into a DMA-able
3694 * area, filling in the S/G table, and finally
3695 * writing the taskfile to hardware, starting the command.
3696 *
3697 * LOCKING:
3698 * spin_lock_irqsave(host_set lock)
3699 *
3700 * RETURNS:
3701 * Zero on success, negative on error.
3702 */
3703
3704int ata_qc_issue(struct ata_queued_cmd *qc)
3705{
3706 struct ata_port *ap = qc->ap;
3707
3708 if (ata_should_dma_map(qc)) {
3709 if (qc->flags & ATA_QCFLAG_SG) {
3710 if (ata_sg_setup(qc))
3711 goto err_out;
3712 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3713 if (ata_sg_setup_one(qc))
3714 goto err_out;
3715 }
3716 } else {
3717 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3718 }
3719
3720 ap->ops->qc_prep(qc);
3721
3722 qc->ap->active_tag = qc->tag;
3723 qc->flags |= ATA_QCFLAG_ACTIVE;
3724
3725 return ap->ops->qc_issue(qc);
3726
3727err_out:
3728 return -1;
3729}
3730
Edward Falk0baab862005-06-02 18:17:13 -04003731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732/**
3733 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3734 * @qc: command to issue to device
3735 *
3736 * Using various libata functions and hooks, this function
3737 * starts an ATA command. ATA commands are grouped into
3738 * classes called "protocols", and issuing each type of protocol
3739 * is slightly different.
3740 *
Edward Falk0baab862005-06-02 18:17:13 -04003741 * May be used as the qc_issue() entry in ata_port_operations.
3742 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 * LOCKING:
3744 * spin_lock_irqsave(host_set lock)
3745 *
3746 * RETURNS:
3747 * Zero on success, negative on error.
3748 */
3749
3750int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3751{
3752 struct ata_port *ap = qc->ap;
3753
3754 ata_dev_select(ap, qc->dev->devno, 1, 0);
3755
3756 switch (qc->tf.protocol) {
3757 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003758 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 break;
3760
3761 case ATA_PROT_DMA:
3762 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3763 ap->ops->bmdma_setup(qc); /* set up bmdma */
3764 ap->ops->bmdma_start(qc); /* initiate bmdma */
3765 break;
3766
3767 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3768 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003769 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003770 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 queue_work(ata_wq, &ap->pio_task);
3772 break;
3773
3774 case ATA_PROT_ATAPI:
3775 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003776 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 queue_work(ata_wq, &ap->packet_task);
3778 break;
3779
3780 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003781 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003782 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 queue_work(ata_wq, &ap->packet_task);
3784 break;
3785
3786 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003787 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3789 ap->ops->bmdma_setup(qc); /* set up bmdma */
3790 queue_work(ata_wq, &ap->packet_task);
3791 break;
3792
3793 default:
3794 WARN_ON(1);
3795 return -1;
3796 }
3797
3798 return 0;
3799}
3800
3801/**
Edward Falk0baab862005-06-02 18:17:13 -04003802 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 * @qc: Info associated with this ATA transaction.
3804 *
3805 * LOCKING:
3806 * spin_lock_irqsave(host_set lock)
3807 */
3808
3809static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3810{
3811 struct ata_port *ap = qc->ap;
3812 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3813 u8 dmactl;
3814 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3815
3816 /* load PRD table addr. */
3817 mb(); /* make sure PRD table writes are visible to controller */
3818 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3819
3820 /* specify data direction, triple-check start bit is clear */
3821 dmactl = readb(mmio + ATA_DMA_CMD);
3822 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3823 if (!rw)
3824 dmactl |= ATA_DMA_WR;
3825 writeb(dmactl, mmio + ATA_DMA_CMD);
3826
3827 /* issue r/w command */
3828 ap->ops->exec_command(ap, &qc->tf);
3829}
3830
3831/**
Alan Coxb73fc892005-08-26 16:03:19 +01003832 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 * @qc: Info associated with this ATA transaction.
3834 *
3835 * LOCKING:
3836 * spin_lock_irqsave(host_set lock)
3837 */
3838
3839static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3840{
3841 struct ata_port *ap = qc->ap;
3842 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3843 u8 dmactl;
3844
3845 /* start host DMA transaction */
3846 dmactl = readb(mmio + ATA_DMA_CMD);
3847 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3848
3849 /* Strictly, one may wish to issue a readb() here, to
3850 * flush the mmio write. However, control also passes
3851 * to the hardware at this point, and it will interrupt
3852 * us when we are to resume control. So, in effect,
3853 * we don't care when the mmio write flushes.
3854 * Further, a read of the DMA status register _immediately_
3855 * following the write may not be what certain flaky hardware
3856 * is expected, so I think it is best to not add a readb()
3857 * without first all the MMIO ATA cards/mobos.
3858 * Or maybe I'm just being paranoid.
3859 */
3860}
3861
3862/**
3863 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3864 * @qc: Info associated with this ATA transaction.
3865 *
3866 * LOCKING:
3867 * spin_lock_irqsave(host_set lock)
3868 */
3869
3870static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3871{
3872 struct ata_port *ap = qc->ap;
3873 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3874 u8 dmactl;
3875
3876 /* load PRD table addr. */
3877 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3878
3879 /* specify data direction, triple-check start bit is clear */
3880 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3881 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3882 if (!rw)
3883 dmactl |= ATA_DMA_WR;
3884 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3885
3886 /* issue r/w command */
3887 ap->ops->exec_command(ap, &qc->tf);
3888}
3889
3890/**
3891 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3892 * @qc: Info associated with this ATA transaction.
3893 *
3894 * LOCKING:
3895 * spin_lock_irqsave(host_set lock)
3896 */
3897
3898static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3899{
3900 struct ata_port *ap = qc->ap;
3901 u8 dmactl;
3902
3903 /* start host DMA transaction */
3904 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3905 outb(dmactl | ATA_DMA_START,
3906 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3907}
3908
Edward Falk0baab862005-06-02 18:17:13 -04003909
3910/**
3911 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3912 * @qc: Info associated with this ATA transaction.
3913 *
3914 * Writes the ATA_DMA_START flag to the DMA command register.
3915 *
3916 * May be used as the bmdma_start() entry in ata_port_operations.
3917 *
3918 * LOCKING:
3919 * spin_lock_irqsave(host_set lock)
3920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921void ata_bmdma_start(struct ata_queued_cmd *qc)
3922{
3923 if (qc->ap->flags & ATA_FLAG_MMIO)
3924 ata_bmdma_start_mmio(qc);
3925 else
3926 ata_bmdma_start_pio(qc);
3927}
3928
Edward Falk0baab862005-06-02 18:17:13 -04003929
3930/**
3931 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3932 * @qc: Info associated with this ATA transaction.
3933 *
3934 * Writes address of PRD table to device's PRD Table Address
3935 * register, sets the DMA control register, and calls
3936 * ops->exec_command() to start the transfer.
3937 *
3938 * May be used as the bmdma_setup() entry in ata_port_operations.
3939 *
3940 * LOCKING:
3941 * spin_lock_irqsave(host_set lock)
3942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943void ata_bmdma_setup(struct ata_queued_cmd *qc)
3944{
3945 if (qc->ap->flags & ATA_FLAG_MMIO)
3946 ata_bmdma_setup_mmio(qc);
3947 else
3948 ata_bmdma_setup_pio(qc);
3949}
3950
Edward Falk0baab862005-06-02 18:17:13 -04003951
3952/**
3953 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003954 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003955 *
3956 * Clear interrupt and error flags in DMA status register.
3957 *
3958 * May be used as the irq_clear() entry in ata_port_operations.
3959 *
3960 * LOCKING:
3961 * spin_lock_irqsave(host_set lock)
3962 */
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964void ata_bmdma_irq_clear(struct ata_port *ap)
3965{
3966 if (ap->flags & ATA_FLAG_MMIO) {
3967 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3968 writeb(readb(mmio), mmio);
3969 } else {
3970 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3971 outb(inb(addr), addr);
3972 }
3973
3974}
3975
Edward Falk0baab862005-06-02 18:17:13 -04003976
3977/**
3978 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003979 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003980 *
3981 * Read and return BMDMA status register.
3982 *
3983 * May be used as the bmdma_status() entry in ata_port_operations.
3984 *
3985 * LOCKING:
3986 * spin_lock_irqsave(host_set lock)
3987 */
3988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989u8 ata_bmdma_status(struct ata_port *ap)
3990{
3991 u8 host_stat;
3992 if (ap->flags & ATA_FLAG_MMIO) {
3993 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3994 host_stat = readb(mmio + ATA_DMA_STATUS);
3995 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003996 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 return host_stat;
3998}
3999
Edward Falk0baab862005-06-02 18:17:13 -04004000
4001/**
4002 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004003 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004004 *
4005 * Clears the ATA_DMA_START flag in the dma control register
4006 *
4007 * May be used as the bmdma_stop() entry in ata_port_operations.
4008 *
4009 * LOCKING:
4010 * spin_lock_irqsave(host_set lock)
4011 */
4012
Alan Coxb73fc892005-08-26 16:03:19 +01004013void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
Alan Coxb73fc892005-08-26 16:03:19 +01004015 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 if (ap->flags & ATA_FLAG_MMIO) {
4017 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4018
4019 /* clear start/stop bit */
4020 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4021 mmio + ATA_DMA_CMD);
4022 } else {
4023 /* clear start/stop bit */
4024 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4025 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4026 }
4027
4028 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4029 ata_altstatus(ap); /* dummy read */
4030}
4031
4032/**
4033 * ata_host_intr - Handle host interrupt for given (port, task)
4034 * @ap: Port on which interrupt arrived (possibly...)
4035 * @qc: Taskfile currently active in engine
4036 *
4037 * Handle host interrupt for given queued command. Currently,
4038 * only DMA interrupts are handled. All other commands are
4039 * handled via polling with interrupts disabled (nIEN bit).
4040 *
4041 * LOCKING:
4042 * spin_lock_irqsave(host_set lock)
4043 *
4044 * RETURNS:
4045 * One if interrupt was handled, zero if not (shared irq).
4046 */
4047
4048inline unsigned int ata_host_intr (struct ata_port *ap,
4049 struct ata_queued_cmd *qc)
4050{
4051 u8 status, host_stat;
4052
4053 switch (qc->tf.protocol) {
4054
4055 case ATA_PROT_DMA:
4056 case ATA_PROT_ATAPI_DMA:
4057 case ATA_PROT_ATAPI:
4058 /* check status of DMA engine */
4059 host_stat = ap->ops->bmdma_status(ap);
4060 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4061
4062 /* if it's not our irq... */
4063 if (!(host_stat & ATA_DMA_INTR))
4064 goto idle_irq;
4065
4066 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004067 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
4069 /* fall through */
4070
4071 case ATA_PROT_ATAPI_NODATA:
4072 case ATA_PROT_NODATA:
4073 /* check altstatus */
4074 status = ata_altstatus(ap);
4075 if (status & ATA_BUSY)
4076 goto idle_irq;
4077
4078 /* check main status, clearing INTRQ */
4079 status = ata_chk_status(ap);
4080 if (unlikely(status & ATA_BUSY))
4081 goto idle_irq;
4082 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4083 ap->id, qc->tf.protocol, status);
4084
4085 /* ack bmdma irq events */
4086 ap->ops->irq_clear(ap);
4087
4088 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004089 qc->err_mask |= ac_err_mask(status);
4090 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 break;
4092
4093 default:
4094 goto idle_irq;
4095 }
4096
4097 return 1; /* irq handled */
4098
4099idle_irq:
4100 ap->stats.idle_irq++;
4101
4102#ifdef ATA_IRQ_TRAP
4103 if ((ap->stats.idle_irq % 1000) == 0) {
4104 handled = 1;
4105 ata_irq_ack(ap, 0); /* debug trap */
4106 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4107 }
4108#endif
4109 return 0; /* irq not handled */
4110}
4111
4112/**
4113 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004114 * @irq: irq line (unused)
4115 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 * @regs: unused
4117 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004118 * Default interrupt handler for PCI IDE devices. Calls
4119 * ata_host_intr() for each port that is not disabled.
4120 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004122 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 *
4124 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004125 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 */
4127
4128irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4129{
4130 struct ata_host_set *host_set = dev_instance;
4131 unsigned int i;
4132 unsigned int handled = 0;
4133 unsigned long flags;
4134
4135 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4136 spin_lock_irqsave(&host_set->lock, flags);
4137
4138 for (i = 0; i < host_set->n_ports; i++) {
4139 struct ata_port *ap;
4140
4141 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004142 if (ap &&
4143 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 struct ata_queued_cmd *qc;
4145
4146 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004147 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4148 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 handled |= ata_host_intr(ap, qc);
4150 }
4151 }
4152
4153 spin_unlock_irqrestore(&host_set->lock, flags);
4154
4155 return IRQ_RETVAL(handled);
4156}
4157
4158/**
4159 * atapi_packet_task - Write CDB bytes to hardware
4160 * @_data: Port to which ATAPI device is attached.
4161 *
4162 * When device has indicated its readiness to accept
4163 * a CDB, this function is called. Send the CDB.
4164 * If DMA is to be performed, exit immediately.
4165 * Otherwise, we are in polling mode, so poll
4166 * status under operation succeeds or fails.
4167 *
4168 * LOCKING:
4169 * Kernel thread context (may sleep)
4170 */
4171
4172static void atapi_packet_task(void *_data)
4173{
4174 struct ata_port *ap = _data;
4175 struct ata_queued_cmd *qc;
4176 u8 status;
4177
4178 qc = ata_qc_from_tag(ap, ap->active_tag);
4179 assert(qc != NULL);
4180 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4181
4182 /* sleep-wait for BSY to clear */
4183 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004184 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4185 qc->err_mask |= AC_ERR_ATA_BUS;
4186 goto err_out;
4187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 /* make sure DRQ is set */
4190 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004191 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4192 qc->err_mask |= AC_ERR_ATA_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196 /* send SCSI cdb */
4197 DPRINTK("send cdb\n");
4198 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
Tejun Heoc1389502005-08-22 14:59:24 +09004200 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4201 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4202 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Tejun Heoc1389502005-08-22 14:59:24 +09004204 /* Once we're done issuing command and kicking bmdma,
4205 * irq handler takes over. To not lose irq, we need
4206 * to clear NOINTR flag before sending cdb, but
4207 * interrupt handler shouldn't be invoked before we're
4208 * finished. Hence, the following locking.
4209 */
4210 spin_lock_irqsave(&ap->host_set->lock, flags);
4211 ap->flags &= ~ATA_FLAG_NOINTR;
4212 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4213 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4214 ap->ops->bmdma_start(qc); /* initiate bmdma */
4215 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4216 } else {
4217 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Tejun Heoc1389502005-08-22 14:59:24 +09004219 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004220 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 queue_work(ata_wq, &ap->pio_task);
4222 }
4223
4224 return;
4225
4226err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004227 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
Edward Falk0baab862005-06-02 18:17:13 -04004230
4231/**
4232 * ata_port_start - Set port up for dma.
4233 * @ap: Port to initialize
4234 *
4235 * Called just after data structures for each port are
4236 * initialized. Allocates space for PRD table.
4237 *
4238 * May be used as the port_start() entry in ata_port_operations.
4239 *
4240 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004241 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004242 */
4243
Jens Axboe9b847542006-01-06 09:28:07 +01004244/*
4245 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4246 * without filling any other registers
4247 */
4248static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4249 u8 cmd)
4250{
4251 struct ata_taskfile tf;
4252 int err;
4253
4254 ata_tf_init(ap, &tf, dev->devno);
4255
4256 tf.command = cmd;
4257 tf.flags |= ATA_TFLAG_DEVICE;
4258 tf.protocol = ATA_PROT_NODATA;
4259
4260 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4261 if (err)
4262 printk(KERN_ERR "%s: ata command failed: %d\n",
4263 __FUNCTION__, err);
4264
4265 return err;
4266}
4267
4268static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4269{
4270 u8 cmd;
4271
4272 if (!ata_try_flush_cache(dev))
4273 return 0;
4274
4275 if (ata_id_has_flush_ext(dev->id))
4276 cmd = ATA_CMD_FLUSH_EXT;
4277 else
4278 cmd = ATA_CMD_FLUSH;
4279
4280 return ata_do_simple_cmd(ap, dev, cmd);
4281}
4282
4283static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4284{
4285 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4286}
4287
4288static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4289{
4290 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4291}
4292
4293/**
4294 * ata_device_resume - wakeup a previously suspended devices
4295 *
4296 * Kick the drive back into action, by sending it an idle immediate
4297 * command and making sure its transfer mode matches between drive
4298 * and host.
4299 *
4300 */
4301int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4302{
4303 if (ap->flags & ATA_FLAG_SUSPENDED) {
4304 ap->flags &= ~ATA_FLAG_SUSPENDED;
4305 ata_set_mode(ap);
4306 }
4307 if (!ata_dev_present(dev))
4308 return 0;
4309 if (dev->class == ATA_DEV_ATA)
4310 ata_start_drive(ap, dev);
4311
4312 return 0;
4313}
4314
4315/**
4316 * ata_device_suspend - prepare a device for suspend
4317 *
4318 * Flush the cache on the drive, if appropriate, then issue a
4319 * standbynow command.
4320 *
4321 */
4322int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4323{
4324 if (!ata_dev_present(dev))
4325 return 0;
4326 if (dev->class == ATA_DEV_ATA)
4327 ata_flush_cache(ap, dev);
4328
4329 ata_standby_drive(ap, dev);
4330 ap->flags |= ATA_FLAG_SUSPENDED;
4331 return 0;
4332}
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334int ata_port_start (struct ata_port *ap)
4335{
4336 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004337 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
4339 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4340 if (!ap->prd)
4341 return -ENOMEM;
4342
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004343 rc = ata_pad_alloc(ap, dev);
4344 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004345 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004346 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004347 }
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4350
4351 return 0;
4352}
4353
Edward Falk0baab862005-06-02 18:17:13 -04004354
4355/**
4356 * ata_port_stop - Undo ata_port_start()
4357 * @ap: Port to shut down
4358 *
4359 * Frees the PRD table.
4360 *
4361 * May be used as the port_stop() entry in ata_port_operations.
4362 *
4363 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004364 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004365 */
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367void ata_port_stop (struct ata_port *ap)
4368{
4369 struct device *dev = ap->host_set->dev;
4370
4371 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004372 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373}
4374
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004375void ata_host_stop (struct ata_host_set *host_set)
4376{
4377 if (host_set->mmio_base)
4378 iounmap(host_set->mmio_base);
4379}
4380
4381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382/**
4383 * ata_host_remove - Unregister SCSI host structure with upper layers
4384 * @ap: Port to unregister
4385 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4386 *
4387 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004388 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 */
4390
4391static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4392{
4393 struct Scsi_Host *sh = ap->host;
4394
4395 DPRINTK("ENTER\n");
4396
4397 if (do_unregister)
4398 scsi_remove_host(sh);
4399
4400 ap->ops->port_stop(ap);
4401}
4402
4403/**
4404 * ata_host_init - Initialize an ata_port structure
4405 * @ap: Structure to initialize
4406 * @host: associated SCSI mid-layer structure
4407 * @host_set: Collection of hosts to which @ap belongs
4408 * @ent: Probe information provided by low-level driver
4409 * @port_no: Port number associated with this ata_port
4410 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004411 * Initialize a new ata_port structure, and its associated
4412 * scsi_host.
4413 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004415 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 */
4417
4418static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4419 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004420 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421{
4422 unsigned int i;
4423
4424 host->max_id = 16;
4425 host->max_lun = 1;
4426 host->max_channel = 1;
4427 host->unique_id = ata_unique_id++;
4428 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 ap->flags = ATA_FLAG_PORT_DISABLED;
4431 ap->id = host->unique_id;
4432 ap->host = host;
4433 ap->ctl = ATA_DEVCTL_OBS;
4434 ap->host_set = host_set;
4435 ap->port_no = port_no;
4436 ap->hard_port_no =
4437 ent->legacy_mode ? ent->hard_port_no : port_no;
4438 ap->pio_mask = ent->pio_mask;
4439 ap->mwdma_mask = ent->mwdma_mask;
4440 ap->udma_mask = ent->udma_mask;
4441 ap->flags |= ent->host_flags;
4442 ap->ops = ent->port_ops;
4443 ap->cbl = ATA_CBL_NONE;
4444 ap->active_tag = ATA_TAG_POISON;
4445 ap->last_ctl = 0xFF;
4446
4447 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4448 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4449
4450 for (i = 0; i < ATA_MAX_DEVICES; i++)
4451 ap->device[i].devno = i;
4452
4453#ifdef ATA_IRQ_TRAP
4454 ap->stats.unhandled_irq = 1;
4455 ap->stats.idle_irq = 1;
4456#endif
4457
4458 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4459}
4460
4461/**
4462 * ata_host_add - Attach low-level ATA driver to system
4463 * @ent: Information provided by low-level driver
4464 * @host_set: Collections of ports to which we add
4465 * @port_no: Port number associated with this host
4466 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004467 * Attach low-level ATA driver to system.
4468 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004470 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 *
4472 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004473 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 */
4475
Jeff Garzik057ace52005-10-22 14:27:05 -04004476static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 struct ata_host_set *host_set,
4478 unsigned int port_no)
4479{
4480 struct Scsi_Host *host;
4481 struct ata_port *ap;
4482 int rc;
4483
4484 DPRINTK("ENTER\n");
4485 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4486 if (!host)
4487 return NULL;
4488
4489 ap = (struct ata_port *) &host->hostdata[0];
4490
4491 ata_host_init(ap, host, host_set, ent, port_no);
4492
4493 rc = ap->ops->port_start(ap);
4494 if (rc)
4495 goto err_out;
4496
4497 return ap;
4498
4499err_out:
4500 scsi_host_put(host);
4501 return NULL;
4502}
4503
4504/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004505 * ata_device_add - Register hardware device with ATA and SCSI layers
4506 * @ent: Probe information describing hardware device to be registered
4507 *
4508 * This function processes the information provided in the probe
4509 * information struct @ent, allocates the necessary ATA and SCSI
4510 * host information structures, initializes them, and registers
4511 * everything with requisite kernel subsystems.
4512 *
4513 * This function requests irqs, probes the ATA bus, and probes
4514 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 *
4516 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004517 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 *
4519 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004520 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 */
4522
Jeff Garzik057ace52005-10-22 14:27:05 -04004523int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
4525 unsigned int count = 0, i;
4526 struct device *dev = ent->dev;
4527 struct ata_host_set *host_set;
4528
4529 DPRINTK("ENTER\n");
4530 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004531 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4533 if (!host_set)
4534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 spin_lock_init(&host_set->lock);
4536
4537 host_set->dev = dev;
4538 host_set->n_ports = ent->n_ports;
4539 host_set->irq = ent->irq;
4540 host_set->mmio_base = ent->mmio_base;
4541 host_set->private_data = ent->private_data;
4542 host_set->ops = ent->port_ops;
4543
4544 /* register each port bound to this device */
4545 for (i = 0; i < ent->n_ports; i++) {
4546 struct ata_port *ap;
4547 unsigned long xfer_mode_mask;
4548
4549 ap = ata_host_add(ent, host_set, i);
4550 if (!ap)
4551 goto err_out;
4552
4553 host_set->ports[i] = ap;
4554 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4555 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4556 (ap->pio_mask << ATA_SHIFT_PIO);
4557
4558 /* print per-port info to dmesg */
4559 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4560 "bmdma 0x%lX irq %lu\n",
4561 ap->id,
4562 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4563 ata_mode_string(xfer_mode_mask),
4564 ap->ioaddr.cmd_addr,
4565 ap->ioaddr.ctl_addr,
4566 ap->ioaddr.bmdma_addr,
4567 ent->irq);
4568
4569 ata_chk_status(ap);
4570 host_set->ops->irq_clear(ap);
4571 count++;
4572 }
4573
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004574 if (!count)
4575 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
4577 /* obtain irq, that is shared between channels */
4578 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4579 DRV_NAME, host_set))
4580 goto err_out;
4581
4582 /* perform each probe synchronously */
4583 DPRINTK("probe begin\n");
4584 for (i = 0; i < count; i++) {
4585 struct ata_port *ap;
4586 int rc;
4587
4588 ap = host_set->ports[i];
4589
4590 DPRINTK("ata%u: probe begin\n", ap->id);
4591 rc = ata_bus_probe(ap);
4592 DPRINTK("ata%u: probe end\n", ap->id);
4593
4594 if (rc) {
4595 /* FIXME: do something useful here?
4596 * Current libata behavior will
4597 * tear down everything when
4598 * the module is removed
4599 * or the h/w is unplugged.
4600 */
4601 }
4602
4603 rc = scsi_add_host(ap->host, dev);
4604 if (rc) {
4605 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4606 ap->id);
4607 /* FIXME: do something useful here */
4608 /* FIXME: handle unconditional calls to
4609 * scsi_scan_host and ata_host_remove, below,
4610 * at the very least
4611 */
4612 }
4613 }
4614
4615 /* probes are done, now scan each port's disk(s) */
4616 DPRINTK("probe begin\n");
4617 for (i = 0; i < count; i++) {
4618 struct ata_port *ap = host_set->ports[i];
4619
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004620 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 }
4622
4623 dev_set_drvdata(dev, host_set);
4624
4625 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4626 return ent->n_ports; /* success */
4627
4628err_out:
4629 for (i = 0; i < count; i++) {
4630 ata_host_remove(host_set->ports[i], 1);
4631 scsi_host_put(host_set->ports[i]->host);
4632 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004633err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 kfree(host_set);
4635 VPRINTK("EXIT, returning 0\n");
4636 return 0;
4637}
4638
4639/**
Alan Cox17b14452005-09-15 15:44:00 +01004640 * ata_host_set_remove - PCI layer callback for device removal
4641 * @host_set: ATA host set that was removed
4642 *
4643 * Unregister all objects associated with this host set. Free those
4644 * objects.
4645 *
4646 * LOCKING:
4647 * Inherited from calling layer (may sleep).
4648 */
4649
Alan Cox17b14452005-09-15 15:44:00 +01004650void ata_host_set_remove(struct ata_host_set *host_set)
4651{
4652 struct ata_port *ap;
4653 unsigned int i;
4654
4655 for (i = 0; i < host_set->n_ports; i++) {
4656 ap = host_set->ports[i];
4657 scsi_remove_host(ap->host);
4658 }
4659
4660 free_irq(host_set->irq, host_set);
4661
4662 for (i = 0; i < host_set->n_ports; i++) {
4663 ap = host_set->ports[i];
4664
4665 ata_scsi_release(ap->host);
4666
4667 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4668 struct ata_ioports *ioaddr = &ap->ioaddr;
4669
4670 if (ioaddr->cmd_addr == 0x1f0)
4671 release_region(0x1f0, 8);
4672 else if (ioaddr->cmd_addr == 0x170)
4673 release_region(0x170, 8);
4674 }
4675
4676 scsi_host_put(ap->host);
4677 }
4678
4679 if (host_set->ops->host_stop)
4680 host_set->ops->host_stop(host_set);
4681
4682 kfree(host_set);
4683}
4684
4685/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 * ata_scsi_release - SCSI layer callback hook for host unload
4687 * @host: libata host to be unloaded
4688 *
4689 * Performs all duties necessary to shut down a libata port...
4690 * Kill port kthread, disable port, and release resources.
4691 *
4692 * LOCKING:
4693 * Inherited from SCSI layer.
4694 *
4695 * RETURNS:
4696 * One.
4697 */
4698
4699int ata_scsi_release(struct Scsi_Host *host)
4700{
4701 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4702
4703 DPRINTK("ENTER\n");
4704
4705 ap->ops->port_disable(ap);
4706 ata_host_remove(ap, 0);
4707
4708 DPRINTK("EXIT\n");
4709 return 1;
4710}
4711
4712/**
4713 * ata_std_ports - initialize ioaddr with standard port offsets.
4714 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004715 *
4716 * Utility function which initializes data_addr, error_addr,
4717 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4718 * device_addr, status_addr, and command_addr to standard offsets
4719 * relative to cmd_addr.
4720 *
4721 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 */
Edward Falk0baab862005-06-02 18:17:13 -04004723
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724void ata_std_ports(struct ata_ioports *ioaddr)
4725{
4726 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4727 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4728 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4729 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4730 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4731 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4732 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4733 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4734 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4735 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4736}
4737
4738static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004739ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
4741 struct ata_probe_ent *probe_ent;
4742
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004743 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 if (!probe_ent) {
4745 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4746 kobject_name(&(dev->kobj)));
4747 return NULL;
4748 }
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 INIT_LIST_HEAD(&probe_ent->node);
4751 probe_ent->dev = dev;
4752
4753 probe_ent->sht = port->sht;
4754 probe_ent->host_flags = port->host_flags;
4755 probe_ent->pio_mask = port->pio_mask;
4756 probe_ent->mwdma_mask = port->mwdma_mask;
4757 probe_ent->udma_mask = port->udma_mask;
4758 probe_ent->port_ops = port->port_ops;
4759
4760 return probe_ent;
4761}
4762
Edward Falk0baab862005-06-02 18:17:13 -04004763
4764
Jeff Garzik374b1872005-08-30 05:42:52 -04004765#ifdef CONFIG_PCI
4766
4767void ata_pci_host_stop (struct ata_host_set *host_set)
4768{
4769 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4770
4771 pci_iounmap(pdev, host_set->mmio_base);
4772}
4773
Edward Falk0baab862005-06-02 18:17:13 -04004774/**
4775 * ata_pci_init_native_mode - Initialize native-mode driver
4776 * @pdev: pci device to be initialized
4777 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004778 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004779 *
4780 * Utility function which allocates and initializes an
4781 * ata_probe_ent structure for a standard dual-port
4782 * PIO-based IDE controller. The returned ata_probe_ent
4783 * structure can be passed to ata_device_add(). The returned
4784 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004785 *
4786 * The caller need only pass the address of the primary port, the
4787 * secondary will be deduced automatically. If the device has non
4788 * standard secondary port mappings this function can be called twice,
4789 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004790 */
4791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004793ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
4795 struct ata_probe_ent *probe_ent =
4796 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004797 int p = 0;
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 if (!probe_ent)
4800 return NULL;
4801
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 probe_ent->irq = pdev->irq;
4803 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004804 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
Alan Cox47a86592005-10-04 08:09:19 -04004806 if (ports & ATA_PORT_PRIMARY) {
4807 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4808 probe_ent->port[p].altstatus_addr =
4809 probe_ent->port[p].ctl_addr =
4810 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4811 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4812 ata_std_ports(&probe_ent->port[p]);
4813 p++;
4814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Alan Cox47a86592005-10-04 08:09:19 -04004816 if (ports & ATA_PORT_SECONDARY) {
4817 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4818 probe_ent->port[p].altstatus_addr =
4819 probe_ent->port[p].ctl_addr =
4820 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4821 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4822 ata_std_ports(&probe_ent->port[p]);
4823 p++;
4824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Alan Cox47a86592005-10-04 08:09:19 -04004826 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 return probe_ent;
4828}
4829
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004830static 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 -07004831{
Alan Cox47a86592005-10-04 08:09:19 -04004832 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004834 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 if (!probe_ent)
4836 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004839 probe_ent->n_ports = 1;
4840 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004841 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Alan Cox47a86592005-10-04 08:09:19 -04004843 switch(port_num)
4844 {
4845 case 0:
4846 probe_ent->irq = 14;
4847 probe_ent->port[0].cmd_addr = 0x1f0;
4848 probe_ent->port[0].altstatus_addr =
4849 probe_ent->port[0].ctl_addr = 0x3f6;
4850 break;
4851 case 1:
4852 probe_ent->irq = 15;
4853 probe_ent->port[0].cmd_addr = 0x170;
4854 probe_ent->port[0].altstatus_addr =
4855 probe_ent->port[0].ctl_addr = 0x376;
4856 break;
4857 }
4858 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 return probe_ent;
4861}
4862
4863/**
4864 * ata_pci_init_one - Initialize/register PCI IDE host controller
4865 * @pdev: Controller to be initialized
4866 * @port_info: Information from low-level host driver
4867 * @n_ports: Number of ports attached to host controller
4868 *
Edward Falk0baab862005-06-02 18:17:13 -04004869 * This is a helper function which can be called from a driver's
4870 * xxx_init_one() probe function if the hardware uses traditional
4871 * IDE taskfile registers.
4872 *
4873 * This function calls pci_enable_device(), reserves its register
4874 * regions, sets the dma mask, enables bus master mode, and calls
4875 * ata_device_add()
4876 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 * LOCKING:
4878 * Inherited from PCI layer (may sleep).
4879 *
4880 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004881 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 */
4883
4884int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4885 unsigned int n_ports)
4886{
Alan Cox47a86592005-10-04 08:09:19 -04004887 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 struct ata_port_info *port[2];
4889 u8 tmp8, mask;
4890 unsigned int legacy_mode = 0;
4891 int disable_dev_on_err = 1;
4892 int rc;
4893
4894 DPRINTK("ENTER\n");
4895
4896 port[0] = port_info[0];
4897 if (n_ports > 1)
4898 port[1] = port_info[1];
4899 else
4900 port[1] = port[0];
4901
4902 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4903 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004904 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4906 mask = (1 << 2) | (1 << 0);
4907 if ((tmp8 & mask) != mask)
4908 legacy_mode = (1 << 3);
4909 }
4910
4911 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004912 if ((!legacy_mode) && (n_ports > 2)) {
4913 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4914 n_ports = 2;
4915 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 }
4917
Alan Cox47a86592005-10-04 08:09:19 -04004918 /* FIXME: Really for ATA it isn't safe because the device may be
4919 multi-purpose and we want to leave it alone if it was already
4920 enabled. Secondly for shared use as Arjan says we want refcounting
4921
4922 Checking dev->is_enabled is insufficient as this is not set at
4923 boot for the primary video which is BIOS enabled
4924 */
4925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 rc = pci_enable_device(pdev);
4927 if (rc)
4928 return rc;
4929
4930 rc = pci_request_regions(pdev, DRV_NAME);
4931 if (rc) {
4932 disable_dev_on_err = 0;
4933 goto err_out;
4934 }
4935
Alan Cox47a86592005-10-04 08:09:19 -04004936 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 if (legacy_mode) {
4938 if (!request_region(0x1f0, 8, "libata")) {
4939 struct resource *conflict, res;
4940 res.start = 0x1f0;
4941 res.end = 0x1f0 + 8 - 1;
4942 conflict = ____request_resource(&ioport_resource, &res);
4943 if (!strcmp(conflict->name, "libata"))
4944 legacy_mode |= (1 << 0);
4945 else {
4946 disable_dev_on_err = 0;
4947 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4948 }
4949 } else
4950 legacy_mode |= (1 << 0);
4951
4952 if (!request_region(0x170, 8, "libata")) {
4953 struct resource *conflict, res;
4954 res.start = 0x170;
4955 res.end = 0x170 + 8 - 1;
4956 conflict = ____request_resource(&ioport_resource, &res);
4957 if (!strcmp(conflict->name, "libata"))
4958 legacy_mode |= (1 << 1);
4959 else {
4960 disable_dev_on_err = 0;
4961 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4962 }
4963 } else
4964 legacy_mode |= (1 << 1);
4965 }
4966
4967 /* we have legacy mode, but all ports are unavailable */
4968 if (legacy_mode == (1 << 3)) {
4969 rc = -EBUSY;
4970 goto err_out_regions;
4971 }
4972
4973 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4974 if (rc)
4975 goto err_out_regions;
4976 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4977 if (rc)
4978 goto err_out_regions;
4979
4980 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004981 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004982 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004983 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004984 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004985 } else {
4986 if (n_ports == 2)
4987 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4988 else
4989 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4990 }
4991 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 rc = -ENOMEM;
4993 goto err_out_regions;
4994 }
4995
4996 pci_set_master(pdev);
4997
4998 /* FIXME: check ata_device_add return */
4999 if (legacy_mode) {
5000 if (legacy_mode & (1 << 0))
5001 ata_device_add(probe_ent);
5002 if (legacy_mode & (1 << 1))
5003 ata_device_add(probe_ent2);
5004 } else
5005 ata_device_add(probe_ent);
5006
5007 kfree(probe_ent);
5008 kfree(probe_ent2);
5009
5010 return 0;
5011
5012err_out_regions:
5013 if (legacy_mode & (1 << 0))
5014 release_region(0x1f0, 8);
5015 if (legacy_mode & (1 << 1))
5016 release_region(0x170, 8);
5017 pci_release_regions(pdev);
5018err_out:
5019 if (disable_dev_on_err)
5020 pci_disable_device(pdev);
5021 return rc;
5022}
5023
5024/**
5025 * ata_pci_remove_one - PCI layer callback for device removal
5026 * @pdev: PCI device that was removed
5027 *
5028 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005029 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 * Handle this by unregistering all objects associated
5031 * with this PCI device. Free those objects. Then finally
5032 * release PCI resources and disable device.
5033 *
5034 * LOCKING:
5035 * Inherited from PCI layer (may sleep).
5036 */
5037
5038void ata_pci_remove_one (struct pci_dev *pdev)
5039{
5040 struct device *dev = pci_dev_to_dev(pdev);
5041 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
Alan Cox17b14452005-09-15 15:44:00 +01005043 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 pci_release_regions(pdev);
5045 pci_disable_device(pdev);
5046 dev_set_drvdata(dev, NULL);
5047}
5048
5049/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005050int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051{
5052 unsigned long tmp = 0;
5053
5054 switch (bits->width) {
5055 case 1: {
5056 u8 tmp8 = 0;
5057 pci_read_config_byte(pdev, bits->reg, &tmp8);
5058 tmp = tmp8;
5059 break;
5060 }
5061 case 2: {
5062 u16 tmp16 = 0;
5063 pci_read_config_word(pdev, bits->reg, &tmp16);
5064 tmp = tmp16;
5065 break;
5066 }
5067 case 4: {
5068 u32 tmp32 = 0;
5069 pci_read_config_dword(pdev, bits->reg, &tmp32);
5070 tmp = tmp32;
5071 break;
5072 }
5073
5074 default:
5075 return -EINVAL;
5076 }
5077
5078 tmp &= bits->mask;
5079
5080 return (tmp == bits->val) ? 1 : 0;
5081}
Jens Axboe9b847542006-01-06 09:28:07 +01005082
5083int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5084{
5085 pci_save_state(pdev);
5086 pci_disable_device(pdev);
5087 pci_set_power_state(pdev, PCI_D3hot);
5088 return 0;
5089}
5090
5091int ata_pci_device_resume(struct pci_dev *pdev)
5092{
5093 pci_set_power_state(pdev, PCI_D0);
5094 pci_restore_state(pdev);
5095 pci_enable_device(pdev);
5096 pci_set_master(pdev);
5097 return 0;
5098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099#endif /* CONFIG_PCI */
5100
5101
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102static int __init ata_init(void)
5103{
5104 ata_wq = create_workqueue("ata");
5105 if (!ata_wq)
5106 return -ENOMEM;
5107
5108 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5109 return 0;
5110}
5111
5112static void __exit ata_exit(void)
5113{
5114 destroy_workqueue(ata_wq);
5115}
5116
5117module_init(ata_init);
5118module_exit(ata_exit);
5119
Jeff Garzik67846b32005-10-05 02:58:32 -04005120static unsigned long ratelimit_time;
5121static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5122
5123int ata_ratelimit(void)
5124{
5125 int rc;
5126 unsigned long flags;
5127
5128 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5129
5130 if (time_after(jiffies, ratelimit_time)) {
5131 rc = 1;
5132 ratelimit_time = jiffies + (HZ/5);
5133 } else
5134 rc = 0;
5135
5136 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5137
5138 return rc;
5139}
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141/*
5142 * libata is essentially a library of internal helper functions for
5143 * low-level ATA host controller drivers. As such, the API/ABI is
5144 * likely to change as new drivers are added and updated.
5145 * Do not depend on ABI/API stability.
5146 */
5147
5148EXPORT_SYMBOL_GPL(ata_std_bios_param);
5149EXPORT_SYMBOL_GPL(ata_std_ports);
5150EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005151EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152EXPORT_SYMBOL_GPL(ata_sg_init);
5153EXPORT_SYMBOL_GPL(ata_sg_init_one);
5154EXPORT_SYMBOL_GPL(ata_qc_complete);
5155EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5156EXPORT_SYMBOL_GPL(ata_eng_timeout);
5157EXPORT_SYMBOL_GPL(ata_tf_load);
5158EXPORT_SYMBOL_GPL(ata_tf_read);
5159EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5160EXPORT_SYMBOL_GPL(ata_std_dev_select);
5161EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5162EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5163EXPORT_SYMBOL_GPL(ata_check_status);
5164EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165EXPORT_SYMBOL_GPL(ata_exec_command);
5166EXPORT_SYMBOL_GPL(ata_port_start);
5167EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005168EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169EXPORT_SYMBOL_GPL(ata_interrupt);
5170EXPORT_SYMBOL_GPL(ata_qc_prep);
5171EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5172EXPORT_SYMBOL_GPL(ata_bmdma_start);
5173EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5174EXPORT_SYMBOL_GPL(ata_bmdma_status);
5175EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5176EXPORT_SYMBOL_GPL(ata_port_probe);
5177EXPORT_SYMBOL_GPL(sata_phy_reset);
5178EXPORT_SYMBOL_GPL(__sata_phy_reset);
5179EXPORT_SYMBOL_GPL(ata_bus_reset);
5180EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005181EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5183EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5184EXPORT_SYMBOL_GPL(ata_scsi_error);
5185EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5186EXPORT_SYMBOL_GPL(ata_scsi_release);
5187EXPORT_SYMBOL_GPL(ata_host_intr);
5188EXPORT_SYMBOL_GPL(ata_dev_classify);
5189EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005190EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5192
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005193EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005194EXPORT_SYMBOL_GPL(ata_timing_compute);
5195EXPORT_SYMBOL_GPL(ata_timing_merge);
5196
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197#ifdef CONFIG_PCI
5198EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005199EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5201EXPORT_SYMBOL_GPL(ata_pci_init_one);
5202EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005203EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5204EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005206
5207EXPORT_SYMBOL_GPL(ata_device_suspend);
5208EXPORT_SYMBOL_GPL(ata_device_resume);
5209EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5210EXPORT_SYMBOL_GPL(ata_scsi_device_resume);