blob: 15df633521d006d164192e1c38a729d58ebe73de [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 Heo77853bf2006-01-23 13:09:36 +09001075void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001076{
Tejun Heo77853bf2006-01-23 13:09:36 +09001077 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001078
Tejun Heo77853bf2006-01-23 13:09:36 +09001079 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001080 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001081}
1082
1083/**
1084 * ata_exec_internal - execute libata internal command
1085 * @ap: Port to which the command is sent
1086 * @dev: Device to which the command is sent
1087 * @tf: Taskfile registers for the command and the result
1088 * @dma_dir: Data tranfer direction of the command
1089 * @buf: Data buffer of the command
1090 * @buflen: Length of data buffer
1091 *
1092 * Executes libata internal command with timeout. @tf contains
1093 * command on entry and result on return. Timeout and error
1094 * conditions are reported via return value. No recovery action
1095 * is taken after a command times out. It's caller's duty to
1096 * clean up after timeout.
1097 *
1098 * LOCKING:
1099 * None. Should be called with kernel context, might sleep.
1100 */
1101
1102static unsigned
1103ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1104 struct ata_taskfile *tf,
1105 int dma_dir, void *buf, unsigned int buflen)
1106{
1107 u8 command = tf->command;
1108 struct ata_queued_cmd *qc;
1109 DECLARE_COMPLETION(wait);
1110 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001111 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001112
1113 spin_lock_irqsave(&ap->host_set->lock, flags);
1114
1115 qc = ata_qc_new_init(ap, dev);
1116 BUG_ON(qc == NULL);
1117
1118 qc->tf = *tf;
1119 qc->dma_dir = dma_dir;
1120 if (dma_dir != DMA_NONE) {
1121 ata_sg_init_one(qc, buf, buflen);
1122 qc->nsect = buflen / ATA_SECT_SIZE;
1123 }
1124
Tejun Heo77853bf2006-01-23 13:09:36 +09001125 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001126 qc->complete_fn = ata_qc_complete_internal;
1127
1128 if (ata_qc_issue(qc))
1129 goto issue_fail;
1130
1131 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1132
1133 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1134 spin_lock_irqsave(&ap->host_set->lock, flags);
1135
1136 /* We're racing with irq here. If we lose, the
1137 * following test prevents us from completing the qc
1138 * again. If completion irq occurs after here but
1139 * before the caller cleans up, it will result in a
1140 * spurious interrupt. We can live with that.
1141 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001142 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heoa2a7a662005-12-13 14:48:31 +09001143 qc->err_mask = AC_ERR_OTHER;
1144 ata_qc_complete(qc);
1145 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1146 ap->id, command);
1147 }
1148
1149 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1150 }
1151
Tejun Heo77853bf2006-01-23 13:09:36 +09001152 *tf = qc->tf;
1153 err_mask = qc->err_mask;
1154
1155 ata_qc_free(qc);
1156
1157 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001158
1159 issue_fail:
1160 ata_qc_free(qc);
1161 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1162 return AC_ERR_OTHER;
1163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001166 * ata_pio_need_iordy - check if iordy needed
1167 * @adev: ATA device
1168 *
1169 * Check if the current speed of the device requires IORDY. Used
1170 * by various controllers for chip configuration.
1171 */
1172
1173unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1174{
1175 int pio;
1176 int speed = adev->pio_mode - XFER_PIO_0;
1177
1178 if (speed < 2)
1179 return 0;
1180 if (speed > 2)
1181 return 1;
1182
1183 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1184
1185 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1186 pio = adev->id[ATA_ID_EIDE_PIO];
1187 /* Is the speed faster than the drive allows non IORDY ? */
1188 if (pio) {
1189 /* This is cycle times not frequency - watch the logic! */
1190 if (pio > 240) /* PIO2 is 240nS per cycle */
1191 return 1;
1192 return 0;
1193 }
1194 }
1195 return 0;
1196}
1197
1198/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1200 * @ap: port on which device we wish to probe resides
1201 * @device: device bus address, starting at zero
1202 *
1203 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1204 * command, and read back the 512-byte device information page.
1205 * The device information page is fed to us via the standard
1206 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1207 * using standard PIO-IN paths)
1208 *
1209 * After reading the device information page, we use several
1210 * bits of information from it to initialize data structures
1211 * that will be used during the lifetime of the ata_device.
1212 * Other data from the info page is used to disqualify certain
1213 * older ATA devices we do not wish to support.
1214 *
1215 * LOCKING:
1216 * Inherited from caller. Some functions called by this function
1217 * obtain the host_set lock.
1218 */
1219
1220static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1221{
1222 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001223 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 u16 tmp;
1225 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001227 struct ata_taskfile tf;
1228 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int rc;
1230
1231 if (!ata_dev_present(dev)) {
1232 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1233 ap->id, device);
1234 return;
1235 }
1236
1237 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1238 using_edd = 0;
1239 else
1240 using_edd = 1;
1241
1242 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1243
1244 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1245 dev->class == ATA_DEV_NONE);
1246
1247 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001250 ata_tf_init(ap, &tf, device);
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001253 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 DPRINTK("do ATA identify\n");
1255 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001256 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 DPRINTK("do ATAPI identify\n");
1258 }
1259
Tejun Heoa0123702005-12-13 14:49:31 +09001260 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Tejun Heoa0123702005-12-13 14:49:31 +09001262 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1263 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Tejun Heoa0123702005-12-13 14:49:31 +09001265 if (err_mask) {
1266 if (err_mask & ~AC_ERR_DEV)
1267 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /*
1270 * arg! EDD works for all test cases, but seems to return
1271 * the ATA signature for some ATAPI devices. Until the
1272 * reason for this is found and fixed, we fix up the mess
1273 * here. If IDENTIFY DEVICE returns command aborted
1274 * (as ATAPI devices do), then we issue an
1275 * IDENTIFY PACKET DEVICE.
1276 *
1277 * ATA software reset (SRST, the default) does not appear
1278 * to have this problem.
1279 */
Albert Lee7c398332005-11-09 13:03:30 +08001280 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001281 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (err & ATA_ABORTED) {
1283 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto retry;
1285 }
1286 }
1287 goto err_out;
1288 }
1289
1290 swap_buf_le16(dev->id, ATA_ID_WORDS);
1291
1292 /* print device capabilities */
1293 printk(KERN_DEBUG "ata%u: dev %u cfg "
1294 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1295 ap->id, device, dev->id[49],
1296 dev->id[82], dev->id[83], dev->id[84],
1297 dev->id[85], dev->id[86], dev->id[87],
1298 dev->id[88]);
1299
1300 /*
1301 * common ATA, ATAPI feature tests
1302 */
1303
Albert Lee8bf62ec2005-05-12 15:29:42 -04001304 /* we require DMA support (bits 8 of word 49) */
1305 if (!ata_id_has_dma(dev->id)) {
1306 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 goto err_out_nosup;
1308 }
1309
1310 /* quick-n-dirty find max transfer mode; for printk only */
1311 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1312 if (!xfer_modes)
1313 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001314 if (!xfer_modes)
1315 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 ata_dump_id(dev);
1318
1319 /* ATA-specific feature tests */
1320 if (dev->class == ATA_DEV_ATA) {
1321 if (!ata_id_is_ata(dev->id)) /* sanity check */
1322 goto err_out_nosup;
1323
Albert Lee8bf62ec2005-05-12 15:29:42 -04001324 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001326 for (major_version = 14; major_version >= 1; major_version--)
1327 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 break;
1329
Albert Lee8bf62ec2005-05-12 15:29:42 -04001330 /*
1331 * The exact sequence expected by certain pre-ATA4 drives is:
1332 * SRST RESET
1333 * IDENTIFY
1334 * INITIALIZE DEVICE PARAMETERS
1335 * anything else..
1336 * Some drives were very specific about that exact sequence.
1337 */
Albert Lee59a10b12005-10-12 15:09:42 +08001338 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001339 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Albert Lee59a10b12005-10-12 15:09:42 +08001341 /* current CHS translation info (id[53-58]) might be
1342 * changed. reread the identify device info.
1343 */
1344 ata_dev_reread_id(ap, dev);
1345 }
1346
Albert Lee8bf62ec2005-05-12 15:29:42 -04001347 if (ata_id_has_lba(dev->id)) {
1348 dev->flags |= ATA_DFLAG_LBA;
1349
1350 if (ata_id_has_lba48(dev->id)) {
1351 dev->flags |= ATA_DFLAG_LBA48;
1352 dev->n_sectors = ata_id_u64(dev->id, 100);
1353 } else {
1354 dev->n_sectors = ata_id_u32(dev->id, 60);
1355 }
1356
1357 /* print device info to dmesg */
1358 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1359 ap->id, device,
1360 major_version,
1361 ata_mode_string(xfer_modes),
1362 (unsigned long long)dev->n_sectors,
1363 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1364 } else {
1365 /* CHS */
1366
1367 /* Default translation */
1368 dev->cylinders = dev->id[1];
1369 dev->heads = dev->id[3];
1370 dev->sectors = dev->id[6];
1371 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1372
1373 if (ata_id_current_chs_valid(dev->id)) {
1374 /* Current CHS translation is valid. */
1375 dev->cylinders = dev->id[54];
1376 dev->heads = dev->id[55];
1377 dev->sectors = dev->id[56];
1378
1379 dev->n_sectors = ata_id_u32(dev->id, 57);
1380 }
1381
1382 /* print device info to dmesg */
1383 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1384 ap->id, device,
1385 major_version,
1386 ata_mode_string(xfer_modes),
1387 (unsigned long long)dev->n_sectors,
1388 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
1392 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394
1395 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001396 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (ata_id_is_ata(dev->id)) /* sanity check */
1398 goto err_out_nosup;
1399
1400 rc = atapi_cdb_len(dev->id);
1401 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1402 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1403 goto err_out_nosup;
1404 }
1405 ap->cdb_len = (unsigned int) rc;
1406 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1407
1408 /* print device info to dmesg */
1409 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1410 ap->id, device,
1411 ata_mode_string(xfer_modes));
1412 }
1413
1414 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1415 return;
1416
1417err_out_nosup:
1418 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1419 ap->id, device);
1420err_out:
1421 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1422 DPRINTK("EXIT, err\n");
1423}
1424
Brad Campbell6f2f3812005-05-12 15:07:47 -04001425
Jeff Garzik057ace52005-10-22 14:27:05 -04001426static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001427{
1428 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1429}
1430
1431/**
1432 * ata_dev_config - Run device specific handlers and check for
1433 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001434 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001435 * @i: Device
1436 *
1437 * LOCKING:
1438 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001439
Brad Campbell6f2f3812005-05-12 15:07:47 -04001440void ata_dev_config(struct ata_port *ap, unsigned int i)
1441{
1442 /* limit bridge transfers to udma5, 200 sectors */
1443 if (ata_dev_knobble(ap)) {
1444 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1445 ap->id, ap->device->devno);
1446 ap->udma_mask &= ATA_UDMA5;
1447 ap->host->max_sectors = ATA_MAX_SECTORS;
1448 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001449 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001450 }
1451
1452 if (ap->ops->dev_config)
1453 ap->ops->dev_config(ap, &ap->device[i]);
1454}
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456/**
1457 * ata_bus_probe - Reset and probe ATA bus
1458 * @ap: Bus to probe
1459 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001460 * Master ATA bus probing function. Initiates a hardware-dependent
1461 * bus reset, then attempts to identify any devices found on
1462 * the bus.
1463 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001465 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
1467 * RETURNS:
1468 * Zero on success, non-zero on error.
1469 */
1470
1471static int ata_bus_probe(struct ata_port *ap)
1472{
1473 unsigned int i, found = 0;
1474
1475 ap->ops->phy_reset(ap);
1476 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1477 goto err_out;
1478
1479 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1480 ata_dev_identify(ap, i);
1481 if (ata_dev_present(&ap->device[i])) {
1482 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001483 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485 }
1486
1487 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1488 goto err_out_disable;
1489
1490 ata_set_mode(ap);
1491 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1492 goto err_out_disable;
1493
1494 return 0;
1495
1496err_out_disable:
1497 ap->ops->port_disable(ap);
1498err_out:
1499 return -1;
1500}
1501
1502/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001503 * ata_port_probe - Mark port as enabled
1504 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001506 * Modify @ap data structure such that the system
1507 * thinks that the entire port is enabled.
1508 *
1509 * LOCKING: host_set lock, or some other form of
1510 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
1512
1513void ata_port_probe(struct ata_port *ap)
1514{
1515 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1516}
1517
1518/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001519 * sata_print_link_status - Print SATA link status
1520 * @ap: SATA port to printk link status about
1521 *
1522 * This function prints link speed and status of a SATA link.
1523 *
1524 * LOCKING:
1525 * None.
1526 */
1527static void sata_print_link_status(struct ata_port *ap)
1528{
1529 u32 sstatus, tmp;
1530 const char *speed;
1531
1532 if (!ap->ops->scr_read)
1533 return;
1534
1535 sstatus = scr_read(ap, SCR_STATUS);
1536
1537 if (sata_dev_present(ap)) {
1538 tmp = (sstatus >> 4) & 0xf;
1539 if (tmp & (1 << 0))
1540 speed = "1.5";
1541 else if (tmp & (1 << 1))
1542 speed = "3.0";
1543 else
1544 speed = "<unknown>";
1545 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1546 ap->id, speed, sstatus);
1547 } else {
1548 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1549 ap->id, sstatus);
1550 }
1551}
1552
1553/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001554 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1555 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001557 * This function issues commands to standard SATA Sxxx
1558 * PHY registers, to wake up the phy (and device), and
1559 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 *
1561 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001562 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 *
1564 */
1565void __sata_phy_reset(struct ata_port *ap)
1566{
1567 u32 sstatus;
1568 unsigned long timeout = jiffies + (HZ * 5);
1569
1570 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001571 /* issue phy wake/reset */
1572 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001573 /* Couldn't find anything in SATA I/II specs, but
1574 * AHCI-1.1 10.4.2 says at least 1 ms. */
1575 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
Brett Russcdcca892005-03-28 15:10:27 -05001577 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 /* wait for phy to become ready, if necessary */
1580 do {
1581 msleep(200);
1582 sstatus = scr_read(ap, SCR_STATUS);
1583 if ((sstatus & 0xf) != 1)
1584 break;
1585 } while (time_before(jiffies, timeout));
1586
Tejun Heo3be680b2005-12-19 22:35:02 +09001587 /* print link status */
1588 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001589
Tejun Heo3be680b2005-12-19 22:35:02 +09001590 /* TODO: phy layer with polling, timeouts, etc. */
1591 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001593 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1597 return;
1598
1599 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1600 ata_port_disable(ap);
1601 return;
1602 }
1603
1604 ap->cbl = ATA_CBL_SATA;
1605}
1606
1607/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001608 * sata_phy_reset - Reset SATA bus.
1609 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001611 * This function resets the SATA bus, and then probes
1612 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 *
1614 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001615 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 *
1617 */
1618void sata_phy_reset(struct ata_port *ap)
1619{
1620 __sata_phy_reset(ap);
1621 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1622 return;
1623 ata_bus_reset(ap);
1624}
1625
1626/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001627 * ata_port_disable - Disable port.
1628 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001630 * Modify @ap data structure such that the system
1631 * thinks that the entire port is disabled, and should
1632 * never attempt to probe or communicate with devices
1633 * on this port.
1634 *
1635 * LOCKING: host_set lock, or some other form of
1636 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 */
1638
1639void ata_port_disable(struct ata_port *ap)
1640{
1641 ap->device[0].class = ATA_DEV_NONE;
1642 ap->device[1].class = ATA_DEV_NONE;
1643 ap->flags |= ATA_FLAG_PORT_DISABLED;
1644}
1645
Alan Cox452503f2005-10-21 19:01:32 -04001646/*
1647 * This mode timing computation functionality is ported over from
1648 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1649 */
1650/*
1651 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1652 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1653 * for PIO 5, which is a nonstandard extension and UDMA6, which
1654 * is currently supported only by Maxtor drives.
1655 */
1656
1657static const struct ata_timing ata_timing[] = {
1658
1659 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1660 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1661 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1662 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1663
1664 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1665 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1666 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1667
1668/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1669
1670 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1671 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1672 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1673
1674 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1675 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1676 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1677
1678/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1679 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1680 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1681
1682 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1683 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1684 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1685
1686/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1687
1688 { 0xFF }
1689};
1690
1691#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1692#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1693
1694static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1695{
1696 q->setup = EZ(t->setup * 1000, T);
1697 q->act8b = EZ(t->act8b * 1000, T);
1698 q->rec8b = EZ(t->rec8b * 1000, T);
1699 q->cyc8b = EZ(t->cyc8b * 1000, T);
1700 q->active = EZ(t->active * 1000, T);
1701 q->recover = EZ(t->recover * 1000, T);
1702 q->cycle = EZ(t->cycle * 1000, T);
1703 q->udma = EZ(t->udma * 1000, UT);
1704}
1705
1706void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1707 struct ata_timing *m, unsigned int what)
1708{
1709 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1710 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1711 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1712 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1713 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1714 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1715 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1716 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1717}
1718
1719static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1720{
1721 const struct ata_timing *t;
1722
1723 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001724 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001725 return NULL;
1726 return t;
1727}
1728
1729int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1730 struct ata_timing *t, int T, int UT)
1731{
1732 const struct ata_timing *s;
1733 struct ata_timing p;
1734
1735 /*
1736 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001737 */
Alan Cox452503f2005-10-21 19:01:32 -04001738
1739 if (!(s = ata_timing_find_mode(speed)))
1740 return -EINVAL;
1741
Albert Lee75b1f2f2005-11-16 17:06:18 +08001742 memcpy(t, s, sizeof(*s));
1743
Alan Cox452503f2005-10-21 19:01:32 -04001744 /*
1745 * If the drive is an EIDE drive, it can tell us it needs extended
1746 * PIO/MW_DMA cycle timing.
1747 */
1748
1749 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1750 memset(&p, 0, sizeof(p));
1751 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1752 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1753 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1754 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1755 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1756 }
1757 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1758 }
1759
1760 /*
1761 * Convert the timing to bus clock counts.
1762 */
1763
Albert Lee75b1f2f2005-11-16 17:06:18 +08001764 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001765
1766 /*
1767 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1768 * and some other commands. We have to ensure that the DMA cycle timing is
1769 * slower/equal than the fastest PIO timing.
1770 */
1771
1772 if (speed > XFER_PIO_4) {
1773 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1774 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1775 }
1776
1777 /*
1778 * Lenghten active & recovery time so that cycle time is correct.
1779 */
1780
1781 if (t->act8b + t->rec8b < t->cyc8b) {
1782 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1783 t->rec8b = t->cyc8b - t->act8b;
1784 }
1785
1786 if (t->active + t->recover < t->cycle) {
1787 t->active += (t->cycle - (t->active + t->recover)) / 2;
1788 t->recover = t->cycle - t->active;
1789 }
1790
1791 return 0;
1792}
1793
Jeff Garzik057ace52005-10-22 14:27:05 -04001794static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 unsigned int shift;
1796 u8 base;
1797} xfer_mode_classes[] = {
1798 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1799 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1800 { ATA_SHIFT_PIO, XFER_PIO_0 },
1801};
1802
Arjan van de Ven858119e2006-01-14 13:20:43 -08001803static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
1805 int i;
1806
1807 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1808 if (xfer_mode_classes[i].shift == shift)
1809 return xfer_mode_classes[i].base;
1810
1811 return 0xff;
1812}
1813
1814static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1815{
1816 int ofs, idx;
1817 u8 base;
1818
1819 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1820 return;
1821
1822 if (dev->xfer_shift == ATA_SHIFT_PIO)
1823 dev->flags |= ATA_DFLAG_PIO;
1824
1825 ata_dev_set_xfermode(ap, dev);
1826
1827 base = base_from_shift(dev->xfer_shift);
1828 ofs = dev->xfer_mode - base;
1829 idx = ofs + dev->xfer_shift;
1830 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1831
1832 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1833 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1834
1835 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1836 ap->id, dev->devno, xfer_mode_str[idx]);
1837}
1838
1839static int ata_host_set_pio(struct ata_port *ap)
1840{
1841 unsigned int mask;
1842 int x, i;
1843 u8 base, xfer_mode;
1844
1845 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1846 x = fgb(mask);
1847 if (x < 0) {
1848 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1849 return -1;
1850 }
1851
1852 base = base_from_shift(ATA_SHIFT_PIO);
1853 xfer_mode = base + x;
1854
1855 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1856 (int)base, (int)xfer_mode, mask, x);
1857
1858 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1859 struct ata_device *dev = &ap->device[i];
1860 if (ata_dev_present(dev)) {
1861 dev->pio_mode = xfer_mode;
1862 dev->xfer_mode = xfer_mode;
1863 dev->xfer_shift = ATA_SHIFT_PIO;
1864 if (ap->ops->set_piomode)
1865 ap->ops->set_piomode(ap, dev);
1866 }
1867 }
1868
1869 return 0;
1870}
1871
1872static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1873 unsigned int xfer_shift)
1874{
1875 int i;
1876
1877 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1878 struct ata_device *dev = &ap->device[i];
1879 if (ata_dev_present(dev)) {
1880 dev->dma_mode = xfer_mode;
1881 dev->xfer_mode = xfer_mode;
1882 dev->xfer_shift = xfer_shift;
1883 if (ap->ops->set_dmamode)
1884 ap->ops->set_dmamode(ap, dev);
1885 }
1886 }
1887}
1888
1889/**
1890 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1891 * @ap: port on which timings will be programmed
1892 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001893 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1894 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001896 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 *
1898 */
1899static void ata_set_mode(struct ata_port *ap)
1900{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001901 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 u8 xfer_mode;
1903 int rc;
1904
1905 /* step 1: always set host PIO timings */
1906 rc = ata_host_set_pio(ap);
1907 if (rc)
1908 goto err_out;
1909
1910 /* step 2: choose the best data xfer mode */
1911 xfer_mode = xfer_shift = 0;
1912 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1913 if (rc)
1914 goto err_out;
1915
1916 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1917 if (xfer_shift != ATA_SHIFT_PIO)
1918 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1919
1920 /* step 4: update devices' xfer mode */
1921 ata_dev_set_mode(ap, &ap->device[0]);
1922 ata_dev_set_mode(ap, &ap->device[1]);
1923
1924 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1925 return;
1926
1927 if (ap->ops->post_set_mode)
1928 ap->ops->post_set_mode(ap);
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return;
1931
1932err_out:
1933 ata_port_disable(ap);
1934}
1935
1936/**
1937 * ata_busy_sleep - sleep until BSY clears, or timeout
1938 * @ap: port containing status register to be polled
1939 * @tmout_pat: impatience timeout
1940 * @tmout: overall timeout
1941 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001942 * Sleep until ATA Status register bit BSY clears,
1943 * or a timeout occurs.
1944 *
1945 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 *
1947 */
1948
1949static unsigned int ata_busy_sleep (struct ata_port *ap,
1950 unsigned long tmout_pat,
1951 unsigned long tmout)
1952{
1953 unsigned long timer_start, timeout;
1954 u8 status;
1955
1956 status = ata_busy_wait(ap, ATA_BUSY, 300);
1957 timer_start = jiffies;
1958 timeout = timer_start + tmout_pat;
1959 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1960 msleep(50);
1961 status = ata_busy_wait(ap, ATA_BUSY, 3);
1962 }
1963
1964 if (status & ATA_BUSY)
1965 printk(KERN_WARNING "ata%u is slow to respond, "
1966 "please be patient\n", ap->id);
1967
1968 timeout = timer_start + tmout;
1969 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1970 msleep(50);
1971 status = ata_chk_status(ap);
1972 }
1973
1974 if (status & ATA_BUSY) {
1975 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1976 ap->id, tmout / HZ);
1977 return 1;
1978 }
1979
1980 return 0;
1981}
1982
1983static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1984{
1985 struct ata_ioports *ioaddr = &ap->ioaddr;
1986 unsigned int dev0 = devmask & (1 << 0);
1987 unsigned int dev1 = devmask & (1 << 1);
1988 unsigned long timeout;
1989
1990 /* if device 0 was found in ata_devchk, wait for its
1991 * BSY bit to clear
1992 */
1993 if (dev0)
1994 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1995
1996 /* if device 1 was found in ata_devchk, wait for
1997 * register access, then wait for BSY to clear
1998 */
1999 timeout = jiffies + ATA_TMOUT_BOOT;
2000 while (dev1) {
2001 u8 nsect, lbal;
2002
2003 ap->ops->dev_select(ap, 1);
2004 if (ap->flags & ATA_FLAG_MMIO) {
2005 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2006 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2007 } else {
2008 nsect = inb(ioaddr->nsect_addr);
2009 lbal = inb(ioaddr->lbal_addr);
2010 }
2011 if ((nsect == 1) && (lbal == 1))
2012 break;
2013 if (time_after(jiffies, timeout)) {
2014 dev1 = 0;
2015 break;
2016 }
2017 msleep(50); /* give drive a breather */
2018 }
2019 if (dev1)
2020 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2021
2022 /* is all this really necessary? */
2023 ap->ops->dev_select(ap, 0);
2024 if (dev1)
2025 ap->ops->dev_select(ap, 1);
2026 if (dev0)
2027 ap->ops->dev_select(ap, 0);
2028}
2029
2030/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002031 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2032 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002034 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2035 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 *
2037 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002038 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05002039 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 *
2041 */
2042
2043static unsigned int ata_bus_edd(struct ata_port *ap)
2044{
2045 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05002046 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* set up execute-device-diag (bus reset) taskfile */
2049 /* also, take interrupts to a known state (disabled) */
2050 DPRINTK("execute-device-diag\n");
2051 ata_tf_init(ap, &tf, 0);
2052 tf.ctl |= ATA_NIEN;
2053 tf.command = ATA_CMD_EDD;
2054 tf.protocol = ATA_PROT_NODATA;
2055
2056 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002057 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002059 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 /* spec says at least 2ms. but who knows with those
2062 * crazy ATAPI devices...
2063 */
2064 msleep(150);
2065
2066 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2067}
2068
2069static unsigned int ata_bus_softreset(struct ata_port *ap,
2070 unsigned int devmask)
2071{
2072 struct ata_ioports *ioaddr = &ap->ioaddr;
2073
2074 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2075
2076 /* software reset. causes dev0 to be selected */
2077 if (ap->flags & ATA_FLAG_MMIO) {
2078 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2079 udelay(20); /* FIXME: flush */
2080 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2081 udelay(20); /* FIXME: flush */
2082 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2083 } else {
2084 outb(ap->ctl, ioaddr->ctl_addr);
2085 udelay(10);
2086 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2087 udelay(10);
2088 outb(ap->ctl, ioaddr->ctl_addr);
2089 }
2090
2091 /* spec mandates ">= 2ms" before checking status.
2092 * We wait 150ms, because that was the magic delay used for
2093 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2094 * between when the ATA command register is written, and then
2095 * status is checked. Because waiting for "a while" before
2096 * checking status is fine, post SRST, we perform this magic
2097 * delay here as well.
2098 */
2099 msleep(150);
2100
2101 ata_bus_post_reset(ap, devmask);
2102
2103 return 0;
2104}
2105
2106/**
2107 * ata_bus_reset - reset host port and associated ATA channel
2108 * @ap: port to reset
2109 *
2110 * This is typically the first time we actually start issuing
2111 * commands to the ATA channel. We wait for BSY to clear, then
2112 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2113 * result. Determine what devices, if any, are on the channel
2114 * by looking at the device 0/1 error register. Look at the signature
2115 * stored in each device's taskfile registers, to determine if
2116 * the device is ATA or ATAPI.
2117 *
2118 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002119 * PCI/etc. bus probe sem.
2120 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 *
2122 * SIDE EFFECTS:
2123 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2124 */
2125
2126void ata_bus_reset(struct ata_port *ap)
2127{
2128 struct ata_ioports *ioaddr = &ap->ioaddr;
2129 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2130 u8 err;
2131 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2132
2133 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2134
2135 /* determine if device 0/1 are present */
2136 if (ap->flags & ATA_FLAG_SATA_RESET)
2137 dev0 = 1;
2138 else {
2139 dev0 = ata_devchk(ap, 0);
2140 if (slave_possible)
2141 dev1 = ata_devchk(ap, 1);
2142 }
2143
2144 if (dev0)
2145 devmask |= (1 << 0);
2146 if (dev1)
2147 devmask |= (1 << 1);
2148
2149 /* select device 0 again */
2150 ap->ops->dev_select(ap, 0);
2151
2152 /* issue bus reset */
2153 if (ap->flags & ATA_FLAG_SRST)
2154 rc = ata_bus_softreset(ap, devmask);
2155 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2156 /* set up device control */
2157 if (ap->flags & ATA_FLAG_MMIO)
2158 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2159 else
2160 outb(ap->ctl, ioaddr->ctl_addr);
2161 rc = ata_bus_edd(ap);
2162 }
2163
2164 if (rc)
2165 goto err_out;
2166
2167 /*
2168 * determine by signature whether we have ATA or ATAPI devices
2169 */
2170 err = ata_dev_try_classify(ap, 0);
2171 if ((slave_possible) && (err != 0x81))
2172 ata_dev_try_classify(ap, 1);
2173
2174 /* re-enable interrupts */
2175 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2176 ata_irq_on(ap);
2177
2178 /* is double-select really necessary? */
2179 if (ap->device[1].class != ATA_DEV_NONE)
2180 ap->ops->dev_select(ap, 1);
2181 if (ap->device[0].class != ATA_DEV_NONE)
2182 ap->ops->dev_select(ap, 0);
2183
2184 /* if no devices were detected, disable this port */
2185 if ((ap->device[0].class == ATA_DEV_NONE) &&
2186 (ap->device[1].class == ATA_DEV_NONE))
2187 goto err_out;
2188
2189 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2190 /* set up device control for ATA_FLAG_SATA_RESET */
2191 if (ap->flags & ATA_FLAG_MMIO)
2192 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2193 else
2194 outb(ap->ctl, ioaddr->ctl_addr);
2195 }
2196
2197 DPRINTK("EXIT\n");
2198 return;
2199
2200err_out:
2201 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2202 ap->ops->port_disable(ap);
2203
2204 DPRINTK("EXIT\n");
2205}
2206
Jeff Garzik057ace52005-10-22 14:27:05 -04002207static void ata_pr_blacklisted(const struct ata_port *ap,
2208 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2211 ap->id, dev->devno);
2212}
2213
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002214static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 "WDC AC11000H",
2216 "WDC AC22100H",
2217 "WDC AC32500H",
2218 "WDC AC33100H",
2219 "WDC AC31600H",
2220 "WDC AC32100H",
2221 "WDC AC23200L",
2222 "Compaq CRD-8241B",
2223 "CRD-8400B",
2224 "CRD-8480B",
2225 "CRD-8482B",
2226 "CRD-84",
2227 "SanDisk SDP3B",
2228 "SanDisk SDP3B-64",
2229 "SANYO CD-ROM CRD",
2230 "HITACHI CDR-8",
2231 "HITACHI CDR-8335",
2232 "HITACHI CDR-8435",
2233 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002234 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 "CD-532E-A",
2236 "E-IDE CD-ROM CR-840",
2237 "CD-ROM Drive/F5A",
2238 "WPI CDD-820",
2239 "SAMSUNG CD-ROM SC-148C",
2240 "SAMSUNG CD-ROM SC",
2241 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2243 "_NEC DV5800A",
2244};
2245
Jeff Garzik057ace52005-10-22 14:27:05 -04002246static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 unsigned char model_num[40];
2249 char *s;
2250 unsigned int len;
2251 int i;
2252
2253 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2254 sizeof(model_num));
2255 s = &model_num[0];
2256 len = strnlen(s, sizeof(model_num));
2257
2258 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2259 while ((len > 0) && (s[len - 1] == ' ')) {
2260 len--;
2261 s[len] = 0;
2262 }
2263
2264 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2265 if (!strncmp(ata_dma_blacklist[i], s, len))
2266 return 1;
2267
2268 return 0;
2269}
2270
Jeff Garzik057ace52005-10-22 14:27:05 -04002271static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Jeff Garzik057ace52005-10-22 14:27:05 -04002273 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 unsigned int mask;
2275
2276 master = &ap->device[0];
2277 slave = &ap->device[1];
2278
2279 assert (ata_dev_present(master) || ata_dev_present(slave));
2280
2281 if (shift == ATA_SHIFT_UDMA) {
2282 mask = ap->udma_mask;
2283 if (ata_dev_present(master)) {
2284 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002285 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 mask = 0;
2287 ata_pr_blacklisted(ap, master);
2288 }
2289 }
2290 if (ata_dev_present(slave)) {
2291 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002292 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 mask = 0;
2294 ata_pr_blacklisted(ap, slave);
2295 }
2296 }
2297 }
2298 else if (shift == ATA_SHIFT_MWDMA) {
2299 mask = ap->mwdma_mask;
2300 if (ata_dev_present(master)) {
2301 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002302 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 mask = 0;
2304 ata_pr_blacklisted(ap, master);
2305 }
2306 }
2307 if (ata_dev_present(slave)) {
2308 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002309 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 mask = 0;
2311 ata_pr_blacklisted(ap, slave);
2312 }
2313 }
2314 }
2315 else if (shift == ATA_SHIFT_PIO) {
2316 mask = ap->pio_mask;
2317 if (ata_dev_present(master)) {
2318 /* spec doesn't return explicit support for
2319 * PIO0-2, so we fake it
2320 */
2321 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2322 tmp_mode <<= 3;
2323 tmp_mode |= 0x7;
2324 mask &= tmp_mode;
2325 }
2326 if (ata_dev_present(slave)) {
2327 /* spec doesn't return explicit support for
2328 * PIO0-2, so we fake it
2329 */
2330 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2331 tmp_mode <<= 3;
2332 tmp_mode |= 0x7;
2333 mask &= tmp_mode;
2334 }
2335 }
2336 else {
2337 mask = 0xffffffff; /* shut up compiler warning */
2338 BUG();
2339 }
2340
2341 return mask;
2342}
2343
2344/* find greatest bit */
2345static int fgb(u32 bitmap)
2346{
2347 unsigned int i;
2348 int x = -1;
2349
2350 for (i = 0; i < 32; i++)
2351 if (bitmap & (1 << i))
2352 x = i;
2353
2354 return x;
2355}
2356
2357/**
2358 * ata_choose_xfer_mode - attempt to find best transfer mode
2359 * @ap: Port for which an xfer mode will be selected
2360 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2361 * @xfer_shift_out: (output) bit shift that selects this mode
2362 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002363 * Based on host and device capabilities, determine the
2364 * maximum transfer mode that is amenable to all.
2365 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002367 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 *
2369 * RETURNS:
2370 * Zero on success, negative on error.
2371 */
2372
Jeff Garzik057ace52005-10-22 14:27:05 -04002373static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 u8 *xfer_mode_out,
2375 unsigned int *xfer_shift_out)
2376{
2377 unsigned int mask, shift;
2378 int x, i;
2379
2380 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2381 shift = xfer_mode_classes[i].shift;
2382 mask = ata_get_mode_mask(ap, shift);
2383
2384 x = fgb(mask);
2385 if (x >= 0) {
2386 *xfer_mode_out = xfer_mode_classes[i].base + x;
2387 *xfer_shift_out = shift;
2388 return 0;
2389 }
2390 }
2391
2392 return -1;
2393}
2394
2395/**
2396 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2397 * @ap: Port associated with device @dev
2398 * @dev: Device to which command will be sent
2399 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002400 * Issue SET FEATURES - XFER MODE command to device @dev
2401 * on port @ap.
2402 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002404 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 */
2406
2407static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2408{
Tejun Heoa0123702005-12-13 14:49:31 +09002409 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 /* set up set-features taskfile */
2412 DPRINTK("set features - xfer mode\n");
2413
Tejun Heoa0123702005-12-13 14:49:31 +09002414 ata_tf_init(ap, &tf, dev->devno);
2415 tf.command = ATA_CMD_SET_FEATURES;
2416 tf.feature = SETFEATURES_XFER;
2417 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2418 tf.protocol = ATA_PROT_NODATA;
2419 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Tejun Heoa0123702005-12-13 14:49:31 +09002421 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2422 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2423 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 DPRINTK("EXIT\n");
2428}
2429
2430/**
Albert Lee59a10b12005-10-12 15:09:42 +08002431 * ata_dev_reread_id - Reread the device identify device info
2432 * @ap: port where the device is
2433 * @dev: device to reread the identify device info
2434 *
2435 * LOCKING:
2436 */
2437
2438static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2439{
Tejun Heoa0123702005-12-13 14:49:31 +09002440 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002441
Tejun Heoa0123702005-12-13 14:49:31 +09002442 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002443
2444 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002445 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002446 DPRINTK("do ATA identify\n");
2447 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002448 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002449 DPRINTK("do ATAPI identify\n");
2450 }
2451
Tejun Heoa0123702005-12-13 14:49:31 +09002452 tf.flags |= ATA_TFLAG_DEVICE;
2453 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002454
Tejun Heoa0123702005-12-13 14:49:31 +09002455 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2456 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002457 goto err_out;
2458
Albert Lee59a10b12005-10-12 15:09:42 +08002459 swap_buf_le16(dev->id, ATA_ID_WORDS);
2460
2461 ata_dump_id(dev);
2462
2463 DPRINTK("EXIT\n");
2464
2465 return;
2466err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002467 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002468 ata_port_disable(ap);
2469}
2470
2471/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002472 * ata_dev_init_params - Issue INIT DEV PARAMS command
2473 * @ap: Port associated with device @dev
2474 * @dev: Device to which command will be sent
2475 *
2476 * LOCKING:
2477 */
2478
2479static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2480{
Tejun Heoa0123702005-12-13 14:49:31 +09002481 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002482 u16 sectors = dev->id[6];
2483 u16 heads = dev->id[3];
2484
2485 /* Number of sectors per track 1-255. Number of heads 1-16 */
2486 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2487 return;
2488
2489 /* set up init dev params taskfile */
2490 DPRINTK("init dev params \n");
2491
Tejun Heoa0123702005-12-13 14:49:31 +09002492 ata_tf_init(ap, &tf, dev->devno);
2493 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2494 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2495 tf.protocol = ATA_PROT_NODATA;
2496 tf.nsect = sectors;
2497 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002498
Tejun Heoa0123702005-12-13 14:49:31 +09002499 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2500 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2501 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002502 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002503 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002504
2505 DPRINTK("EXIT\n");
2506}
2507
2508/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002509 * ata_sg_clean - Unmap DMA memory associated with command
2510 * @qc: Command containing DMA memory to be released
2511 *
2512 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 *
2514 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002515 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 */
2517
2518static void ata_sg_clean(struct ata_queued_cmd *qc)
2519{
2520 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002521 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002523 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2526 assert(sg != NULL);
2527
2528 if (qc->flags & ATA_QCFLAG_SINGLE)
2529 assert(qc->n_elem == 1);
2530
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002531 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002533 /* if we padded the buffer out to 32-bit bound, and data
2534 * xfer direction is from-device, we must copy from the
2535 * pad buffer back into the supplied buffer
2536 */
2537 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2538 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2539
2540 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002541 if (qc->n_elem)
2542 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002543 /* restore last sg */
2544 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2545 if (pad_buf) {
2546 struct scatterlist *psg = &qc->pad_sgent;
2547 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2548 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002549 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002550 }
2551 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002552 if (sg_dma_len(&sg[0]) > 0)
2553 dma_unmap_single(ap->host_set->dev,
2554 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2555 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002556 /* restore sg */
2557 sg->length += qc->pad_len;
2558 if (pad_buf)
2559 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2560 pad_buf, qc->pad_len);
2561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002564 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
2567/**
2568 * ata_fill_sg - Fill PCI IDE PRD table
2569 * @qc: Metadata associated with taskfile to be transferred
2570 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002571 * Fill PCI IDE PRD (scatter-gather) table with segments
2572 * associated with the current disk command.
2573 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002575 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 *
2577 */
2578static void ata_fill_sg(struct ata_queued_cmd *qc)
2579{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002581 struct scatterlist *sg;
2582 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002584 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 assert(qc->n_elem > 0);
2586
2587 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002588 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 u32 addr, offset;
2590 u32 sg_len, len;
2591
2592 /* determine if physical DMA addr spans 64K boundary.
2593 * Note h/w doesn't support 64-bit, so we unconditionally
2594 * truncate dma_addr_t to u32.
2595 */
2596 addr = (u32) sg_dma_address(sg);
2597 sg_len = sg_dma_len(sg);
2598
2599 while (sg_len) {
2600 offset = addr & 0xffff;
2601 len = sg_len;
2602 if ((offset + sg_len) > 0x10000)
2603 len = 0x10000 - offset;
2604
2605 ap->prd[idx].addr = cpu_to_le32(addr);
2606 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2607 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2608
2609 idx++;
2610 sg_len -= len;
2611 addr += len;
2612 }
2613 }
2614
2615 if (idx)
2616 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2617}
2618/**
2619 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2620 * @qc: Metadata associated with taskfile to check
2621 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002622 * Allow low-level driver to filter ATA PACKET commands, returning
2623 * a status indicating whether or not it is OK to use DMA for the
2624 * supplied PACKET command.
2625 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002627 * spin_lock_irqsave(host_set lock)
2628 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 * RETURNS: 0 when ATAPI DMA can be used
2630 * nonzero otherwise
2631 */
2632int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2633{
2634 struct ata_port *ap = qc->ap;
2635 int rc = 0; /* Assume ATAPI DMA is OK by default */
2636
2637 if (ap->ops->check_atapi_dma)
2638 rc = ap->ops->check_atapi_dma(qc);
2639
2640 return rc;
2641}
2642/**
2643 * ata_qc_prep - Prepare taskfile for submission
2644 * @qc: Metadata associated with taskfile to be prepared
2645 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002646 * Prepare ATA taskfile for submission.
2647 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 * LOCKING:
2649 * spin_lock_irqsave(host_set lock)
2650 */
2651void ata_qc_prep(struct ata_queued_cmd *qc)
2652{
2653 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2654 return;
2655
2656 ata_fill_sg(qc);
2657}
2658
Jeff Garzik0cba6322005-05-30 19:49:12 -04002659/**
2660 * ata_sg_init_one - Associate command with memory buffer
2661 * @qc: Command to be associated
2662 * @buf: Memory buffer
2663 * @buflen: Length of memory buffer, in bytes.
2664 *
2665 * Initialize the data-related elements of queued_cmd @qc
2666 * to point to a single memory buffer, @buf of byte length @buflen.
2667 *
2668 * LOCKING:
2669 * spin_lock_irqsave(host_set lock)
2670 */
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2673{
2674 struct scatterlist *sg;
2675
2676 qc->flags |= ATA_QCFLAG_SINGLE;
2677
2678 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002679 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002681 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 qc->buf_virt = buf;
2683
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002684 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002685 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}
2687
Jeff Garzik0cba6322005-05-30 19:49:12 -04002688/**
2689 * ata_sg_init - Associate command with scatter-gather table.
2690 * @qc: Command to be associated
2691 * @sg: Scatter-gather table.
2692 * @n_elem: Number of elements in s/g table.
2693 *
2694 * Initialize the data-related elements of queued_cmd @qc
2695 * to point to a scatter-gather table @sg, containing @n_elem
2696 * elements.
2697 *
2698 * LOCKING:
2699 * spin_lock_irqsave(host_set lock)
2700 */
2701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2703 unsigned int n_elem)
2704{
2705 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002706 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002708 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709}
2710
2711/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002712 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2713 * @qc: Command with memory buffer to be mapped.
2714 *
2715 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 *
2717 * LOCKING:
2718 * spin_lock_irqsave(host_set lock)
2719 *
2720 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002721 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 */
2723
2724static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2725{
2726 struct ata_port *ap = qc->ap;
2727 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002728 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 dma_addr_t dma_address;
2730
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002731 /* we must lengthen transfers to end on a 32-bit boundary */
2732 qc->pad_len = sg->length & 3;
2733 if (qc->pad_len) {
2734 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2735 struct scatterlist *psg = &qc->pad_sgent;
2736
2737 assert(qc->dev->class == ATA_DEV_ATAPI);
2738
2739 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2740
2741 if (qc->tf.flags & ATA_TFLAG_WRITE)
2742 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2743 qc->pad_len);
2744
2745 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2746 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2747 /* trim sg */
2748 sg->length -= qc->pad_len;
2749
2750 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2751 sg->length, qc->pad_len);
2752 }
2753
Jeff Garzike1410f22005-11-14 14:06:26 -05002754 if (!sg->length) {
2755 sg_dma_address(sg) = 0;
2756 goto skip_map;
2757 }
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002760 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002761 if (dma_mapping_error(dma_address)) {
2762 /* restore sg */
2763 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002768skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002769 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2772 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2773
2774 return 0;
2775}
2776
2777/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002778 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2779 * @qc: Command with scatter-gather table to be mapped.
2780 *
2781 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 *
2783 * LOCKING:
2784 * spin_lock_irqsave(host_set lock)
2785 *
2786 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002787 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 *
2789 */
2790
2791static int ata_sg_setup(struct ata_queued_cmd *qc)
2792{
2793 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002794 struct scatterlist *sg = qc->__sg;
2795 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002796 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 VPRINTK("ENTER, ata%u\n", ap->id);
2799 assert(qc->flags & ATA_QCFLAG_SG);
2800
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002801 /* we must lengthen transfers to end on a 32-bit boundary */
2802 qc->pad_len = lsg->length & 3;
2803 if (qc->pad_len) {
2804 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2805 struct scatterlist *psg = &qc->pad_sgent;
2806 unsigned int offset;
2807
2808 assert(qc->dev->class == ATA_DEV_ATAPI);
2809
2810 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2811
2812 /*
2813 * psg->page/offset are used to copy to-be-written
2814 * data in this function or read data in ata_sg_clean.
2815 */
2816 offset = lsg->offset + lsg->length - qc->pad_len;
2817 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2818 psg->offset = offset_in_page(offset);
2819
2820 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2821 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2822 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002823 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002824 }
2825
2826 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2827 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2828 /* trim last sg */
2829 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002830 if (lsg->length == 0)
2831 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002832
2833 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2834 qc->n_elem - 1, lsg->length, qc->pad_len);
2835 }
2836
Jeff Garzike1410f22005-11-14 14:06:26 -05002837 pre_n_elem = qc->n_elem;
2838 if (trim_sg && pre_n_elem)
2839 pre_n_elem--;
2840
2841 if (!pre_n_elem) {
2842 n_elem = 0;
2843 goto skip_map;
2844 }
2845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002847 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002848 if (n_elem < 1) {
2849 /* restore last sg */
2850 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 DPRINTK("%d sg elements mapped\n", n_elem);
2855
Jeff Garzike1410f22005-11-14 14:06:26 -05002856skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 qc->n_elem = n_elem;
2858
2859 return 0;
2860}
2861
2862/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002863 * ata_poll_qc_complete - turn irq back on and finish qc
2864 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002865 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002866 *
2867 * LOCKING:
2868 * None. (grabs host lock)
2869 */
2870
Albert Leea22e2eb2005-12-05 15:38:02 +08002871void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002872{
2873 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002874 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002875
Jeff Garzikb8f61532005-08-25 22:01:20 -04002876 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002877 ap->flags &= ~ATA_FLAG_NOINTR;
2878 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002879 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002880 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002881}
2882
2883/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002885 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 *
2887 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002888 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 *
2890 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002891 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 */
2893
2894static unsigned long ata_pio_poll(struct ata_port *ap)
2895{
Albert Leec14b8332005-12-05 15:36:08 +08002896 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002898 unsigned int poll_state = HSM_ST_UNKNOWN;
2899 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Albert Leec14b8332005-12-05 15:36:08 +08002901 qc = ata_qc_from_tag(ap, ap->active_tag);
2902 assert(qc != NULL);
2903
Albert Lee14be71f2005-09-27 17:36:35 +08002904 switch (ap->hsm_task_state) {
2905 case HSM_ST:
2906 case HSM_ST_POLL:
2907 poll_state = HSM_ST_POLL;
2908 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002910 case HSM_ST_LAST:
2911 case HSM_ST_LAST_POLL:
2912 poll_state = HSM_ST_LAST_POLL;
2913 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 break;
2915 default:
2916 BUG();
2917 break;
2918 }
2919
2920 status = ata_chk_status(ap);
2921 if (status & ATA_BUSY) {
2922 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee1c848982005-12-05 15:40:15 +08002923 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee7c398332005-11-09 13:03:30 +08002924 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return 0;
2926 }
Albert Lee14be71f2005-09-27 17:36:35 +08002927 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return ATA_SHORT_PAUSE;
2929 }
2930
Albert Lee14be71f2005-09-27 17:36:35 +08002931 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return 0;
2933}
2934
2935/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002936 * ata_pio_complete - check if drive is busy or idle
2937 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 *
2939 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002940 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002941 *
2942 * RETURNS:
2943 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 */
2945
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002946static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 struct ata_queued_cmd *qc;
2949 u8 drv_stat;
2950
2951 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002952 * This is purely heuristic. This is a fast path. Sometimes when
2953 * we enter, BSY will be cleared in a chk-status or two. If not,
2954 * the drive is probably seeking or something. Snooze for a couple
2955 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002956 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 */
Albert Leefe79e682005-12-06 11:34:59 +08002958 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2959 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002961 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2962 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002963 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
2967 }
2968
Albert Leec14b8332005-12-05 15:36:08 +08002969 qc = ata_qc_from_tag(ap, ap->active_tag);
2970 assert(qc != NULL);
2971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 drv_stat = ata_wait_idle(ap);
2973 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002974 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002975 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
2978
Albert Lee14be71f2005-09-27 17:36:35 +08002979 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Albert Leea22e2eb2005-12-05 15:38:02 +08002981 assert(qc->err_mask == 0);
2982 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002983
2984 /* another command may start at this point */
2985
2986 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
2988
Edward Falk0baab862005-06-02 18:17:13 -04002989
2990/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002991 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002992 * @buf: Buffer to swap
2993 * @buf_words: Number of 16-bit words in buffer.
2994 *
2995 * Swap halves of 16-bit words if needed to convert from
2996 * little-endian byte order to native cpu byte order, or
2997 * vice-versa.
2998 *
2999 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003000 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002void swap_buf_le16(u16 *buf, unsigned int buf_words)
3003{
3004#ifdef __BIG_ENDIAN
3005 unsigned int i;
3006
3007 for (i = 0; i < buf_words; i++)
3008 buf[i] = le16_to_cpu(buf[i]);
3009#endif /* __BIG_ENDIAN */
3010}
3011
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003012/**
3013 * ata_mmio_data_xfer - Transfer data by MMIO
3014 * @ap: port to read/write
3015 * @buf: data buffer
3016 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003017 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003018 *
3019 * Transfer data from/to the device data register by MMIO.
3020 *
3021 * LOCKING:
3022 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003023 */
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3026 unsigned int buflen, int write_data)
3027{
3028 unsigned int i;
3029 unsigned int words = buflen >> 1;
3030 u16 *buf16 = (u16 *) buf;
3031 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3032
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003033 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 if (write_data) {
3035 for (i = 0; i < words; i++)
3036 writew(le16_to_cpu(buf16[i]), mmio);
3037 } else {
3038 for (i = 0; i < words; i++)
3039 buf16[i] = cpu_to_le16(readw(mmio));
3040 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003041
3042 /* Transfer trailing 1 byte, if any. */
3043 if (unlikely(buflen & 0x01)) {
3044 u16 align_buf[1] = { 0 };
3045 unsigned char *trailing_buf = buf + buflen - 1;
3046
3047 if (write_data) {
3048 memcpy(align_buf, trailing_buf, 1);
3049 writew(le16_to_cpu(align_buf[0]), mmio);
3050 } else {
3051 align_buf[0] = cpu_to_le16(readw(mmio));
3052 memcpy(trailing_buf, align_buf, 1);
3053 }
3054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055}
3056
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003057/**
3058 * ata_pio_data_xfer - Transfer data by PIO
3059 * @ap: port to read/write
3060 * @buf: data buffer
3061 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003062 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003063 *
3064 * Transfer data from/to the device data register by PIO.
3065 *
3066 * LOCKING:
3067 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003068 */
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3071 unsigned int buflen, int write_data)
3072{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003073 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003075 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003077 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003079 insw(ap->ioaddr.data_addr, buf, words);
3080
3081 /* Transfer trailing 1 byte, if any. */
3082 if (unlikely(buflen & 0x01)) {
3083 u16 align_buf[1] = { 0 };
3084 unsigned char *trailing_buf = buf + buflen - 1;
3085
3086 if (write_data) {
3087 memcpy(align_buf, trailing_buf, 1);
3088 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3089 } else {
3090 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3091 memcpy(trailing_buf, align_buf, 1);
3092 }
3093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003096/**
3097 * ata_data_xfer - Transfer data from/to the data register.
3098 * @ap: port to read/write
3099 * @buf: data buffer
3100 * @buflen: buffer length
3101 * @do_write: read/write
3102 *
3103 * Transfer data from/to the device data register.
3104 *
3105 * LOCKING:
3106 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003107 */
3108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3110 unsigned int buflen, int do_write)
3111{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003112 /* Make the crap hardware pay the costs not the good stuff */
3113 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3114 unsigned long flags;
3115 local_irq_save(flags);
3116 if (ap->flags & ATA_FLAG_MMIO)
3117 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3118 else
3119 ata_pio_data_xfer(ap, buf, buflen, do_write);
3120 local_irq_restore(flags);
3121 } else {
3122 if (ap->flags & ATA_FLAG_MMIO)
3123 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3124 else
3125 ata_pio_data_xfer(ap, buf, buflen, do_write);
3126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003129/**
3130 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3131 * @qc: Command on going
3132 *
3133 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3134 *
3135 * LOCKING:
3136 * Inherited from caller.
3137 */
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139static void ata_pio_sector(struct ata_queued_cmd *qc)
3140{
3141 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003142 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 struct ata_port *ap = qc->ap;
3144 struct page *page;
3145 unsigned int offset;
3146 unsigned char *buf;
3147
3148 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003149 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
3151 page = sg[qc->cursg].page;
3152 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3153
3154 /* get the current page and offset */
3155 page = nth_page(page, (offset >> PAGE_SHIFT));
3156 offset %= PAGE_SIZE;
3157
3158 buf = kmap(page) + offset;
3159
3160 qc->cursect++;
3161 qc->cursg_ofs++;
3162
Albert Lee32529e02005-05-26 03:49:42 -04003163 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 qc->cursg++;
3165 qc->cursg_ofs = 0;
3166 }
3167
3168 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3169
3170 /* do the actual data transfer */
3171 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3172 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3173
3174 kunmap(page);
3175}
3176
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003177/**
3178 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3179 * @qc: Command on going
3180 * @bytes: number of bytes
3181 *
3182 * Transfer Transfer data from/to the ATAPI device.
3183 *
3184 * LOCKING:
3185 * Inherited from caller.
3186 *
3187 */
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3190{
3191 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003192 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 struct ata_port *ap = qc->ap;
3194 struct page *page;
3195 unsigned char *buf;
3196 unsigned int offset, count;
3197
Albert Lee563a6e12005-08-12 14:17:50 +08003198 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003199 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003202 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003203 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003204 * The end of qc->sg is reached and the device expects
3205 * more data to transfer. In order not to overrun qc->sg
3206 * and fulfill length specified in the byte count register,
3207 * - for read case, discard trailing data from the device
3208 * - for write case, padding zero data to the device
3209 */
3210 u16 pad_buf[1] = { 0 };
3211 unsigned int words = bytes >> 1;
3212 unsigned int i;
3213
3214 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003215 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003216 ap->id, bytes);
3217
3218 for (i = 0; i < words; i++)
3219 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3220
Albert Lee14be71f2005-09-27 17:36:35 +08003221 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003222 return;
3223 }
3224
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003225 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 page = sg->page;
3228 offset = sg->offset + qc->cursg_ofs;
3229
3230 /* get the current page and offset */
3231 page = nth_page(page, (offset >> PAGE_SHIFT));
3232 offset %= PAGE_SIZE;
3233
Albert Lee6952df02005-06-06 15:56:03 +08003234 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003235 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237 /* don't cross page boundaries */
3238 count = min(count, (unsigned int)PAGE_SIZE - offset);
3239
3240 buf = kmap(page) + offset;
3241
3242 bytes -= count;
3243 qc->curbytes += count;
3244 qc->cursg_ofs += count;
3245
Albert Lee32529e02005-05-26 03:49:42 -04003246 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 qc->cursg++;
3248 qc->cursg_ofs = 0;
3249 }
3250
3251 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3252
3253 /* do the actual data transfer */
3254 ata_data_xfer(ap, buf, count, do_write);
3255
3256 kunmap(page);
3257
Albert Lee563a6e12005-08-12 14:17:50 +08003258 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003262/**
3263 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3264 * @qc: Command on going
3265 *
3266 * Transfer Transfer data from/to the ATAPI device.
3267 *
3268 * LOCKING:
3269 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003270 */
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3273{
3274 struct ata_port *ap = qc->ap;
3275 struct ata_device *dev = qc->dev;
3276 unsigned int ireason, bc_lo, bc_hi, bytes;
3277 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3278
3279 ap->ops->tf_read(ap, &qc->tf);
3280 ireason = qc->tf.nsect;
3281 bc_lo = qc->tf.lbam;
3282 bc_hi = qc->tf.lbah;
3283 bytes = (bc_hi << 8) | bc_lo;
3284
3285 /* shall be cleared to zero, indicating xfer of data */
3286 if (ireason & (1 << 0))
3287 goto err_out;
3288
3289 /* make sure transfer direction matches expected */
3290 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3291 if (do_write != i_write)
3292 goto err_out;
3293
3294 __atapi_pio_bytes(qc, bytes);
3295
3296 return;
3297
3298err_out:
3299 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3300 ap->id, dev->devno);
Albert Lee1c848982005-12-05 15:40:15 +08003301 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003302 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
3305/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003306 * ata_pio_block - start PIO on a block
3307 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 *
3309 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003310 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 */
3312
3313static void ata_pio_block(struct ata_port *ap)
3314{
3315 struct ata_queued_cmd *qc;
3316 u8 status;
3317
3318 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003319 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 * Sometimes when we enter, BSY will be cleared in
3321 * a chk-status or two. If not, the drive is probably seeking
3322 * or something. Snooze for a couple msecs, then
3323 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003324 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 */
3326 status = ata_busy_wait(ap, ATA_BUSY, 5);
3327 if (status & ATA_BUSY) {
3328 msleep(2);
3329 status = ata_busy_wait(ap, ATA_BUSY, 10);
3330 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003331 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3333 return;
3334 }
3335 }
3336
3337 qc = ata_qc_from_tag(ap, ap->active_tag);
3338 assert(qc != NULL);
3339
Albert Leefe79e682005-12-06 11:34:59 +08003340 /* check error */
3341 if (status & (ATA_ERR | ATA_DF)) {
3342 qc->err_mask |= AC_ERR_DEV;
3343 ap->hsm_task_state = HSM_ST_ERR;
3344 return;
3345 }
3346
3347 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003349 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003351 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 return;
3353 }
3354
3355 atapi_pio_bytes(qc);
3356 } else {
3357 /* handle BSY=0, DRQ=0 as error */
3358 if ((status & ATA_DRQ) == 0) {
Albert Lee1c848982005-12-05 15:40:15 +08003359 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003360 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return;
3362 }
3363
3364 ata_pio_sector(qc);
3365 }
3366}
3367
3368static void ata_pio_error(struct ata_port *ap)
3369{
3370 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003371
3372 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
3374 qc = ata_qc_from_tag(ap, ap->active_tag);
3375 assert(qc != NULL);
3376
Albert Lee1c848982005-12-05 15:40:15 +08003377 /* make sure qc->err_mask is available to
3378 * know what's wrong and recover
3379 */
3380 assert(qc->err_mask);
3381
Albert Lee14be71f2005-09-27 17:36:35 +08003382 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Albert Leea22e2eb2005-12-05 15:38:02 +08003384 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385}
3386
3387static void ata_pio_task(void *_data)
3388{
3389 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003390 unsigned long timeout;
3391 int qc_completed;
3392
3393fsm_start:
3394 timeout = 0;
3395 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Albert Lee14be71f2005-09-27 17:36:35 +08003397 switch (ap->hsm_task_state) {
3398 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 return;
3400
Albert Lee14be71f2005-09-27 17:36:35 +08003401 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 ata_pio_block(ap);
3403 break;
3404
Albert Lee14be71f2005-09-27 17:36:35 +08003405 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003406 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 break;
3408
Albert Lee14be71f2005-09-27 17:36:35 +08003409 case HSM_ST_POLL:
3410 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 timeout = ata_pio_poll(ap);
3412 break;
3413
Albert Lee14be71f2005-09-27 17:36:35 +08003414 case HSM_ST_TMOUT:
3415 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 ata_pio_error(ap);
3417 return;
3418 }
3419
3420 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003421 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3422 else if (!qc_completed)
3423 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426/**
3427 * ata_qc_timeout - Handle timeout of queued command
3428 * @qc: Command that timed out
3429 *
3430 * Some part of the kernel (currently, only the SCSI layer)
3431 * has noticed that the active command on port @ap has not
3432 * completed after a specified length of time. Handle this
3433 * condition by disabling DMA (if necessary) and completing
3434 * transactions, with error if necessary.
3435 *
3436 * This also handles the case of the "lost interrupt", where
3437 * for some reason (possibly hardware bug, possibly driver bug)
3438 * an interrupt was not delivered to the driver, even though the
3439 * transaction completed successfully.
3440 *
3441 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003442 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 */
3444
3445static void ata_qc_timeout(struct ata_queued_cmd *qc)
3446{
3447 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003448 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003450 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
3452 DPRINTK("ENTER\n");
3453
Jeff Garzikb8f61532005-08-25 22:01:20 -04003454 spin_lock_irqsave(&host_set->lock, flags);
3455
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 /* hack alert! We cannot use the supplied completion
3457 * function from inside the ->eh_strategy_handler() thread.
3458 * libata is the only user of ->eh_strategy_handler() in
3459 * any kernel, so the default scsi_done() assumes it is
3460 * not being called from the SCSI EH.
3461 */
3462 qc->scsidone = scsi_finish_command;
3463
3464 switch (qc->tf.protocol) {
3465
3466 case ATA_PROT_DMA:
3467 case ATA_PROT_ATAPI_DMA:
3468 host_stat = ap->ops->bmdma_status(ap);
3469
3470 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003471 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
3473 /* fall through */
3474
3475 default:
3476 ata_altstatus(ap);
3477 drv_stat = ata_chk_status(ap);
3478
3479 /* ack bmdma irq events */
3480 ap->ops->irq_clear(ap);
3481
3482 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3483 ap->id, qc->tf.command, drv_stat, host_stat);
3484
3485 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003486 qc->err_mask |= ac_err_mask(drv_stat);
3487 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 break;
3489 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003490
3491 spin_unlock_irqrestore(&host_set->lock, flags);
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 DPRINTK("EXIT\n");
3494}
3495
3496/**
3497 * ata_eng_timeout - Handle timeout of queued command
3498 * @ap: Port on which timed-out command is active
3499 *
3500 * Some part of the kernel (currently, only the SCSI layer)
3501 * has noticed that the active command on port @ap has not
3502 * completed after a specified length of time. Handle this
3503 * condition by disabling DMA (if necessary) and completing
3504 * transactions, with error if necessary.
3505 *
3506 * This also handles the case of the "lost interrupt", where
3507 * for some reason (possibly hardware bug, possibly driver bug)
3508 * an interrupt was not delivered to the driver, even though the
3509 * transaction completed successfully.
3510 *
3511 * LOCKING:
3512 * Inherited from SCSI layer (none, can sleep)
3513 */
3514
3515void ata_eng_timeout(struct ata_port *ap)
3516{
3517 struct ata_queued_cmd *qc;
3518
3519 DPRINTK("ENTER\n");
3520
3521 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003522 if (qc)
3523 ata_qc_timeout(qc);
3524 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3526 ap->id);
3527 goto out;
3528 }
3529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530out:
3531 DPRINTK("EXIT\n");
3532}
3533
3534/**
3535 * ata_qc_new - Request an available ATA command, for queueing
3536 * @ap: Port associated with device @dev
3537 * @dev: Device from whom we request an available command structure
3538 *
3539 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003540 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 */
3542
3543static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3544{
3545 struct ata_queued_cmd *qc = NULL;
3546 unsigned int i;
3547
3548 for (i = 0; i < ATA_MAX_QUEUE; i++)
3549 if (!test_and_set_bit(i, &ap->qactive)) {
3550 qc = ata_qc_from_tag(ap, i);
3551 break;
3552 }
3553
3554 if (qc)
3555 qc->tag = i;
3556
3557 return qc;
3558}
3559
3560/**
3561 * ata_qc_new_init - Request an available ATA command, and initialize it
3562 * @ap: Port associated with device @dev
3563 * @dev: Device from whom we request an available command structure
3564 *
3565 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003566 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 */
3568
3569struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3570 struct ata_device *dev)
3571{
3572 struct ata_queued_cmd *qc;
3573
3574 qc = ata_qc_new(ap);
3575 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 qc->scsicmd = NULL;
3577 qc->ap = ap;
3578 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003580 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 }
3582
3583 return qc;
3584}
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586/**
3587 * ata_qc_free - free unused ata_queued_cmd
3588 * @qc: Command to complete
3589 *
3590 * Designed to free unused ata_queued_cmd object
3591 * in case something prevents using it.
3592 *
3593 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003594 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 */
3596void ata_qc_free(struct ata_queued_cmd *qc)
3597{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003598 struct ata_port *ap = qc->ap;
3599 unsigned int tag;
3600
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Tejun Heo4ba946e2006-01-23 13:09:36 +09003603 qc->flags = 0;
3604 tag = qc->tag;
3605 if (likely(ata_tag_valid(tag))) {
3606 if (tag == ap->active_tag)
3607 ap->active_tag = ATA_TAG_POISON;
3608 qc->tag = ATA_TAG_POISON;
3609 clear_bit(tag, &ap->qactive);
3610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
3613/**
3614 * ata_qc_complete - Complete an active ATA command
3615 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003616 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003617 *
3618 * Indicate to the mid and upper layers that an ATA
3619 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 *
3621 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003622 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 */
3624
Albert Leea22e2eb2005-12-05 15:38:02 +08003625void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3628 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3629
3630 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3631 ata_sg_clean(qc);
3632
Albert Lee3f3791d2005-08-16 14:25:38 +08003633 /* atapi: mark qc as inactive to prevent the interrupt handler
3634 * from completing the command twice later, before the error handler
3635 * is called. (when rc != 0 and atapi request sense is needed)
3636 */
3637 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003640 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641}
3642
3643static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3644{
3645 struct ata_port *ap = qc->ap;
3646
3647 switch (qc->tf.protocol) {
3648 case ATA_PROT_DMA:
3649 case ATA_PROT_ATAPI_DMA:
3650 return 1;
3651
3652 case ATA_PROT_ATAPI:
3653 case ATA_PROT_PIO:
3654 case ATA_PROT_PIO_MULT:
3655 if (ap->flags & ATA_FLAG_PIO_DMA)
3656 return 1;
3657
3658 /* fall through */
3659
3660 default:
3661 return 0;
3662 }
3663
3664 /* never reached */
3665}
3666
3667/**
3668 * ata_qc_issue - issue taskfile to device
3669 * @qc: command to issue to device
3670 *
3671 * Prepare an ATA command to submission to device.
3672 * This includes mapping the data into a DMA-able
3673 * area, filling in the S/G table, and finally
3674 * writing the taskfile to hardware, starting the command.
3675 *
3676 * LOCKING:
3677 * spin_lock_irqsave(host_set lock)
3678 *
3679 * RETURNS:
3680 * Zero on success, negative on error.
3681 */
3682
3683int ata_qc_issue(struct ata_queued_cmd *qc)
3684{
3685 struct ata_port *ap = qc->ap;
3686
3687 if (ata_should_dma_map(qc)) {
3688 if (qc->flags & ATA_QCFLAG_SG) {
3689 if (ata_sg_setup(qc))
3690 goto err_out;
3691 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3692 if (ata_sg_setup_one(qc))
3693 goto err_out;
3694 }
3695 } else {
3696 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3697 }
3698
3699 ap->ops->qc_prep(qc);
3700
3701 qc->ap->active_tag = qc->tag;
3702 qc->flags |= ATA_QCFLAG_ACTIVE;
3703
3704 return ap->ops->qc_issue(qc);
3705
3706err_out:
3707 return -1;
3708}
3709
Edward Falk0baab862005-06-02 18:17:13 -04003710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711/**
3712 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3713 * @qc: command to issue to device
3714 *
3715 * Using various libata functions and hooks, this function
3716 * starts an ATA command. ATA commands are grouped into
3717 * classes called "protocols", and issuing each type of protocol
3718 * is slightly different.
3719 *
Edward Falk0baab862005-06-02 18:17:13 -04003720 * May be used as the qc_issue() entry in ata_port_operations.
3721 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 * LOCKING:
3723 * spin_lock_irqsave(host_set lock)
3724 *
3725 * RETURNS:
3726 * Zero on success, negative on error.
3727 */
3728
3729int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3730{
3731 struct ata_port *ap = qc->ap;
3732
3733 ata_dev_select(ap, qc->dev->devno, 1, 0);
3734
3735 switch (qc->tf.protocol) {
3736 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003737 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 break;
3739
3740 case ATA_PROT_DMA:
3741 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3742 ap->ops->bmdma_setup(qc); /* set up bmdma */
3743 ap->ops->bmdma_start(qc); /* initiate bmdma */
3744 break;
3745
3746 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3747 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003748 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003749 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 queue_work(ata_wq, &ap->pio_task);
3751 break;
3752
3753 case ATA_PROT_ATAPI:
3754 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003755 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 queue_work(ata_wq, &ap->packet_task);
3757 break;
3758
3759 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003760 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003761 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 queue_work(ata_wq, &ap->packet_task);
3763 break;
3764
3765 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003766 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3768 ap->ops->bmdma_setup(qc); /* set up bmdma */
3769 queue_work(ata_wq, &ap->packet_task);
3770 break;
3771
3772 default:
3773 WARN_ON(1);
3774 return -1;
3775 }
3776
3777 return 0;
3778}
3779
3780/**
Edward Falk0baab862005-06-02 18:17:13 -04003781 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 * @qc: Info associated with this ATA transaction.
3783 *
3784 * LOCKING:
3785 * spin_lock_irqsave(host_set lock)
3786 */
3787
3788static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3789{
3790 struct ata_port *ap = qc->ap;
3791 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3792 u8 dmactl;
3793 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3794
3795 /* load PRD table addr. */
3796 mb(); /* make sure PRD table writes are visible to controller */
3797 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3798
3799 /* specify data direction, triple-check start bit is clear */
3800 dmactl = readb(mmio + ATA_DMA_CMD);
3801 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3802 if (!rw)
3803 dmactl |= ATA_DMA_WR;
3804 writeb(dmactl, mmio + ATA_DMA_CMD);
3805
3806 /* issue r/w command */
3807 ap->ops->exec_command(ap, &qc->tf);
3808}
3809
3810/**
Alan Coxb73fc892005-08-26 16:03:19 +01003811 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 * @qc: Info associated with this ATA transaction.
3813 *
3814 * LOCKING:
3815 * spin_lock_irqsave(host_set lock)
3816 */
3817
3818static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3819{
3820 struct ata_port *ap = qc->ap;
3821 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3822 u8 dmactl;
3823
3824 /* start host DMA transaction */
3825 dmactl = readb(mmio + ATA_DMA_CMD);
3826 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3827
3828 /* Strictly, one may wish to issue a readb() here, to
3829 * flush the mmio write. However, control also passes
3830 * to the hardware at this point, and it will interrupt
3831 * us when we are to resume control. So, in effect,
3832 * we don't care when the mmio write flushes.
3833 * Further, a read of the DMA status register _immediately_
3834 * following the write may not be what certain flaky hardware
3835 * is expected, so I think it is best to not add a readb()
3836 * without first all the MMIO ATA cards/mobos.
3837 * Or maybe I'm just being paranoid.
3838 */
3839}
3840
3841/**
3842 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3843 * @qc: Info associated with this ATA transaction.
3844 *
3845 * LOCKING:
3846 * spin_lock_irqsave(host_set lock)
3847 */
3848
3849static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3850{
3851 struct ata_port *ap = qc->ap;
3852 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3853 u8 dmactl;
3854
3855 /* load PRD table addr. */
3856 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3857
3858 /* specify data direction, triple-check start bit is clear */
3859 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3860 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3861 if (!rw)
3862 dmactl |= ATA_DMA_WR;
3863 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3864
3865 /* issue r/w command */
3866 ap->ops->exec_command(ap, &qc->tf);
3867}
3868
3869/**
3870 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3871 * @qc: Info associated with this ATA transaction.
3872 *
3873 * LOCKING:
3874 * spin_lock_irqsave(host_set lock)
3875 */
3876
3877static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3878{
3879 struct ata_port *ap = qc->ap;
3880 u8 dmactl;
3881
3882 /* start host DMA transaction */
3883 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3884 outb(dmactl | ATA_DMA_START,
3885 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3886}
3887
Edward Falk0baab862005-06-02 18:17:13 -04003888
3889/**
3890 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3891 * @qc: Info associated with this ATA transaction.
3892 *
3893 * Writes the ATA_DMA_START flag to the DMA command register.
3894 *
3895 * May be used as the bmdma_start() entry in ata_port_operations.
3896 *
3897 * LOCKING:
3898 * spin_lock_irqsave(host_set lock)
3899 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900void ata_bmdma_start(struct ata_queued_cmd *qc)
3901{
3902 if (qc->ap->flags & ATA_FLAG_MMIO)
3903 ata_bmdma_start_mmio(qc);
3904 else
3905 ata_bmdma_start_pio(qc);
3906}
3907
Edward Falk0baab862005-06-02 18:17:13 -04003908
3909/**
3910 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3911 * @qc: Info associated with this ATA transaction.
3912 *
3913 * Writes address of PRD table to device's PRD Table Address
3914 * register, sets the DMA control register, and calls
3915 * ops->exec_command() to start the transfer.
3916 *
3917 * May be used as the bmdma_setup() entry in ata_port_operations.
3918 *
3919 * LOCKING:
3920 * spin_lock_irqsave(host_set lock)
3921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922void ata_bmdma_setup(struct ata_queued_cmd *qc)
3923{
3924 if (qc->ap->flags & ATA_FLAG_MMIO)
3925 ata_bmdma_setup_mmio(qc);
3926 else
3927 ata_bmdma_setup_pio(qc);
3928}
3929
Edward Falk0baab862005-06-02 18:17:13 -04003930
3931/**
3932 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003933 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003934 *
3935 * Clear interrupt and error flags in DMA status register.
3936 *
3937 * May be used as the irq_clear() entry in ata_port_operations.
3938 *
3939 * LOCKING:
3940 * spin_lock_irqsave(host_set lock)
3941 */
3942
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943void ata_bmdma_irq_clear(struct ata_port *ap)
3944{
3945 if (ap->flags & ATA_FLAG_MMIO) {
3946 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3947 writeb(readb(mmio), mmio);
3948 } else {
3949 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3950 outb(inb(addr), addr);
3951 }
3952
3953}
3954
Edward Falk0baab862005-06-02 18:17:13 -04003955
3956/**
3957 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003958 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003959 *
3960 * Read and return BMDMA status register.
3961 *
3962 * May be used as the bmdma_status() entry in ata_port_operations.
3963 *
3964 * LOCKING:
3965 * spin_lock_irqsave(host_set lock)
3966 */
3967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968u8 ata_bmdma_status(struct ata_port *ap)
3969{
3970 u8 host_stat;
3971 if (ap->flags & ATA_FLAG_MMIO) {
3972 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3973 host_stat = readb(mmio + ATA_DMA_STATUS);
3974 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003975 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return host_stat;
3977}
3978
Edward Falk0baab862005-06-02 18:17:13 -04003979
3980/**
3981 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003982 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003983 *
3984 * Clears the ATA_DMA_START flag in the dma control register
3985 *
3986 * May be used as the bmdma_stop() entry in ata_port_operations.
3987 *
3988 * LOCKING:
3989 * spin_lock_irqsave(host_set lock)
3990 */
3991
Alan Coxb73fc892005-08-26 16:03:19 +01003992void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
Alan Coxb73fc892005-08-26 16:03:19 +01003994 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 if (ap->flags & ATA_FLAG_MMIO) {
3996 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3997
3998 /* clear start/stop bit */
3999 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4000 mmio + ATA_DMA_CMD);
4001 } else {
4002 /* clear start/stop bit */
4003 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4004 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4005 }
4006
4007 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4008 ata_altstatus(ap); /* dummy read */
4009}
4010
4011/**
4012 * ata_host_intr - Handle host interrupt for given (port, task)
4013 * @ap: Port on which interrupt arrived (possibly...)
4014 * @qc: Taskfile currently active in engine
4015 *
4016 * Handle host interrupt for given queued command. Currently,
4017 * only DMA interrupts are handled. All other commands are
4018 * handled via polling with interrupts disabled (nIEN bit).
4019 *
4020 * LOCKING:
4021 * spin_lock_irqsave(host_set lock)
4022 *
4023 * RETURNS:
4024 * One if interrupt was handled, zero if not (shared irq).
4025 */
4026
4027inline unsigned int ata_host_intr (struct ata_port *ap,
4028 struct ata_queued_cmd *qc)
4029{
4030 u8 status, host_stat;
4031
4032 switch (qc->tf.protocol) {
4033
4034 case ATA_PROT_DMA:
4035 case ATA_PROT_ATAPI_DMA:
4036 case ATA_PROT_ATAPI:
4037 /* check status of DMA engine */
4038 host_stat = ap->ops->bmdma_status(ap);
4039 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4040
4041 /* if it's not our irq... */
4042 if (!(host_stat & ATA_DMA_INTR))
4043 goto idle_irq;
4044
4045 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004046 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
4048 /* fall through */
4049
4050 case ATA_PROT_ATAPI_NODATA:
4051 case ATA_PROT_NODATA:
4052 /* check altstatus */
4053 status = ata_altstatus(ap);
4054 if (status & ATA_BUSY)
4055 goto idle_irq;
4056
4057 /* check main status, clearing INTRQ */
4058 status = ata_chk_status(ap);
4059 if (unlikely(status & ATA_BUSY))
4060 goto idle_irq;
4061 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4062 ap->id, qc->tf.protocol, status);
4063
4064 /* ack bmdma irq events */
4065 ap->ops->irq_clear(ap);
4066
4067 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004068 qc->err_mask |= ac_err_mask(status);
4069 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 break;
4071
4072 default:
4073 goto idle_irq;
4074 }
4075
4076 return 1; /* irq handled */
4077
4078idle_irq:
4079 ap->stats.idle_irq++;
4080
4081#ifdef ATA_IRQ_TRAP
4082 if ((ap->stats.idle_irq % 1000) == 0) {
4083 handled = 1;
4084 ata_irq_ack(ap, 0); /* debug trap */
4085 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4086 }
4087#endif
4088 return 0; /* irq not handled */
4089}
4090
4091/**
4092 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004093 * @irq: irq line (unused)
4094 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 * @regs: unused
4096 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004097 * Default interrupt handler for PCI IDE devices. Calls
4098 * ata_host_intr() for each port that is not disabled.
4099 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004101 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 *
4103 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004104 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 */
4106
4107irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4108{
4109 struct ata_host_set *host_set = dev_instance;
4110 unsigned int i;
4111 unsigned int handled = 0;
4112 unsigned long flags;
4113
4114 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4115 spin_lock_irqsave(&host_set->lock, flags);
4116
4117 for (i = 0; i < host_set->n_ports; i++) {
4118 struct ata_port *ap;
4119
4120 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004121 if (ap &&
4122 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 struct ata_queued_cmd *qc;
4124
4125 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004126 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4127 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 handled |= ata_host_intr(ap, qc);
4129 }
4130 }
4131
4132 spin_unlock_irqrestore(&host_set->lock, flags);
4133
4134 return IRQ_RETVAL(handled);
4135}
4136
4137/**
4138 * atapi_packet_task - Write CDB bytes to hardware
4139 * @_data: Port to which ATAPI device is attached.
4140 *
4141 * When device has indicated its readiness to accept
4142 * a CDB, this function is called. Send the CDB.
4143 * If DMA is to be performed, exit immediately.
4144 * Otherwise, we are in polling mode, so poll
4145 * status under operation succeeds or fails.
4146 *
4147 * LOCKING:
4148 * Kernel thread context (may sleep)
4149 */
4150
4151static void atapi_packet_task(void *_data)
4152{
4153 struct ata_port *ap = _data;
4154 struct ata_queued_cmd *qc;
4155 u8 status;
4156
4157 qc = ata_qc_from_tag(ap, ap->active_tag);
4158 assert(qc != NULL);
4159 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4160
4161 /* sleep-wait for BSY to clear */
4162 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004163 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4164 qc->err_mask |= AC_ERR_ATA_BUS;
4165 goto err_out;
4166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
4168 /* make sure DRQ is set */
4169 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004170 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4171 qc->err_mask |= AC_ERR_ATA_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175 /* send SCSI cdb */
4176 DPRINTK("send cdb\n");
4177 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Tejun Heoc1389502005-08-22 14:59:24 +09004179 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4180 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4181 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Tejun Heoc1389502005-08-22 14:59:24 +09004183 /* Once we're done issuing command and kicking bmdma,
4184 * irq handler takes over. To not lose irq, we need
4185 * to clear NOINTR flag before sending cdb, but
4186 * interrupt handler shouldn't be invoked before we're
4187 * finished. Hence, the following locking.
4188 */
4189 spin_lock_irqsave(&ap->host_set->lock, flags);
4190 ap->flags &= ~ATA_FLAG_NOINTR;
4191 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4192 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4193 ap->ops->bmdma_start(qc); /* initiate bmdma */
4194 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4195 } else {
4196 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Tejun Heoc1389502005-08-22 14:59:24 +09004198 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004199 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 queue_work(ata_wq, &ap->pio_task);
4201 }
4202
4203 return;
4204
4205err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004206 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207}
4208
Edward Falk0baab862005-06-02 18:17:13 -04004209
4210/**
4211 * ata_port_start - Set port up for dma.
4212 * @ap: Port to initialize
4213 *
4214 * Called just after data structures for each port are
4215 * initialized. Allocates space for PRD table.
4216 *
4217 * May be used as the port_start() entry in ata_port_operations.
4218 *
4219 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004220 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004221 */
4222
Jens Axboe9b847542006-01-06 09:28:07 +01004223/*
4224 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4225 * without filling any other registers
4226 */
4227static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4228 u8 cmd)
4229{
4230 struct ata_taskfile tf;
4231 int err;
4232
4233 ata_tf_init(ap, &tf, dev->devno);
4234
4235 tf.command = cmd;
4236 tf.flags |= ATA_TFLAG_DEVICE;
4237 tf.protocol = ATA_PROT_NODATA;
4238
4239 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4240 if (err)
4241 printk(KERN_ERR "%s: ata command failed: %d\n",
4242 __FUNCTION__, err);
4243
4244 return err;
4245}
4246
4247static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4248{
4249 u8 cmd;
4250
4251 if (!ata_try_flush_cache(dev))
4252 return 0;
4253
4254 if (ata_id_has_flush_ext(dev->id))
4255 cmd = ATA_CMD_FLUSH_EXT;
4256 else
4257 cmd = ATA_CMD_FLUSH;
4258
4259 return ata_do_simple_cmd(ap, dev, cmd);
4260}
4261
4262static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4263{
4264 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4265}
4266
4267static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4268{
4269 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4270}
4271
4272/**
4273 * ata_device_resume - wakeup a previously suspended devices
4274 *
4275 * Kick the drive back into action, by sending it an idle immediate
4276 * command and making sure its transfer mode matches between drive
4277 * and host.
4278 *
4279 */
4280int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4281{
4282 if (ap->flags & ATA_FLAG_SUSPENDED) {
4283 ap->flags &= ~ATA_FLAG_SUSPENDED;
4284 ata_set_mode(ap);
4285 }
4286 if (!ata_dev_present(dev))
4287 return 0;
4288 if (dev->class == ATA_DEV_ATA)
4289 ata_start_drive(ap, dev);
4290
4291 return 0;
4292}
4293
4294/**
4295 * ata_device_suspend - prepare a device for suspend
4296 *
4297 * Flush the cache on the drive, if appropriate, then issue a
4298 * standbynow command.
4299 *
4300 */
4301int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4302{
4303 if (!ata_dev_present(dev))
4304 return 0;
4305 if (dev->class == ATA_DEV_ATA)
4306 ata_flush_cache(ap, dev);
4307
4308 ata_standby_drive(ap, dev);
4309 ap->flags |= ATA_FLAG_SUSPENDED;
4310 return 0;
4311}
4312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313int ata_port_start (struct ata_port *ap)
4314{
4315 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004316 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
4318 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4319 if (!ap->prd)
4320 return -ENOMEM;
4321
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004322 rc = ata_pad_alloc(ap, dev);
4323 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004324 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004325 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004326 }
4327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4329
4330 return 0;
4331}
4332
Edward Falk0baab862005-06-02 18:17:13 -04004333
4334/**
4335 * ata_port_stop - Undo ata_port_start()
4336 * @ap: Port to shut down
4337 *
4338 * Frees the PRD table.
4339 *
4340 * May be used as the port_stop() entry in ata_port_operations.
4341 *
4342 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004343 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004344 */
4345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346void ata_port_stop (struct ata_port *ap)
4347{
4348 struct device *dev = ap->host_set->dev;
4349
4350 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004351 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352}
4353
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004354void ata_host_stop (struct ata_host_set *host_set)
4355{
4356 if (host_set->mmio_base)
4357 iounmap(host_set->mmio_base);
4358}
4359
4360
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361/**
4362 * ata_host_remove - Unregister SCSI host structure with upper layers
4363 * @ap: Port to unregister
4364 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4365 *
4366 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004367 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 */
4369
4370static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4371{
4372 struct Scsi_Host *sh = ap->host;
4373
4374 DPRINTK("ENTER\n");
4375
4376 if (do_unregister)
4377 scsi_remove_host(sh);
4378
4379 ap->ops->port_stop(ap);
4380}
4381
4382/**
4383 * ata_host_init - Initialize an ata_port structure
4384 * @ap: Structure to initialize
4385 * @host: associated SCSI mid-layer structure
4386 * @host_set: Collection of hosts to which @ap belongs
4387 * @ent: Probe information provided by low-level driver
4388 * @port_no: Port number associated with this ata_port
4389 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004390 * Initialize a new ata_port structure, and its associated
4391 * scsi_host.
4392 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004394 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 */
4396
4397static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4398 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004399 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
4401 unsigned int i;
4402
4403 host->max_id = 16;
4404 host->max_lun = 1;
4405 host->max_channel = 1;
4406 host->unique_id = ata_unique_id++;
4407 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 ap->flags = ATA_FLAG_PORT_DISABLED;
4410 ap->id = host->unique_id;
4411 ap->host = host;
4412 ap->ctl = ATA_DEVCTL_OBS;
4413 ap->host_set = host_set;
4414 ap->port_no = port_no;
4415 ap->hard_port_no =
4416 ent->legacy_mode ? ent->hard_port_no : port_no;
4417 ap->pio_mask = ent->pio_mask;
4418 ap->mwdma_mask = ent->mwdma_mask;
4419 ap->udma_mask = ent->udma_mask;
4420 ap->flags |= ent->host_flags;
4421 ap->ops = ent->port_ops;
4422 ap->cbl = ATA_CBL_NONE;
4423 ap->active_tag = ATA_TAG_POISON;
4424 ap->last_ctl = 0xFF;
4425
4426 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4427 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4428
4429 for (i = 0; i < ATA_MAX_DEVICES; i++)
4430 ap->device[i].devno = i;
4431
4432#ifdef ATA_IRQ_TRAP
4433 ap->stats.unhandled_irq = 1;
4434 ap->stats.idle_irq = 1;
4435#endif
4436
4437 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4438}
4439
4440/**
4441 * ata_host_add - Attach low-level ATA driver to system
4442 * @ent: Information provided by low-level driver
4443 * @host_set: Collections of ports to which we add
4444 * @port_no: Port number associated with this host
4445 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004446 * Attach low-level ATA driver to system.
4447 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004449 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 *
4451 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004452 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 */
4454
Jeff Garzik057ace52005-10-22 14:27:05 -04004455static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 struct ata_host_set *host_set,
4457 unsigned int port_no)
4458{
4459 struct Scsi_Host *host;
4460 struct ata_port *ap;
4461 int rc;
4462
4463 DPRINTK("ENTER\n");
4464 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4465 if (!host)
4466 return NULL;
4467
4468 ap = (struct ata_port *) &host->hostdata[0];
4469
4470 ata_host_init(ap, host, host_set, ent, port_no);
4471
4472 rc = ap->ops->port_start(ap);
4473 if (rc)
4474 goto err_out;
4475
4476 return ap;
4477
4478err_out:
4479 scsi_host_put(host);
4480 return NULL;
4481}
4482
4483/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004484 * ata_device_add - Register hardware device with ATA and SCSI layers
4485 * @ent: Probe information describing hardware device to be registered
4486 *
4487 * This function processes the information provided in the probe
4488 * information struct @ent, allocates the necessary ATA and SCSI
4489 * host information structures, initializes them, and registers
4490 * everything with requisite kernel subsystems.
4491 *
4492 * This function requests irqs, probes the ATA bus, and probes
4493 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 *
4495 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004496 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 *
4498 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004499 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 */
4501
Jeff Garzik057ace52005-10-22 14:27:05 -04004502int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503{
4504 unsigned int count = 0, i;
4505 struct device *dev = ent->dev;
4506 struct ata_host_set *host_set;
4507
4508 DPRINTK("ENTER\n");
4509 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004510 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4512 if (!host_set)
4513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 spin_lock_init(&host_set->lock);
4515
4516 host_set->dev = dev;
4517 host_set->n_ports = ent->n_ports;
4518 host_set->irq = ent->irq;
4519 host_set->mmio_base = ent->mmio_base;
4520 host_set->private_data = ent->private_data;
4521 host_set->ops = ent->port_ops;
4522
4523 /* register each port bound to this device */
4524 for (i = 0; i < ent->n_ports; i++) {
4525 struct ata_port *ap;
4526 unsigned long xfer_mode_mask;
4527
4528 ap = ata_host_add(ent, host_set, i);
4529 if (!ap)
4530 goto err_out;
4531
4532 host_set->ports[i] = ap;
4533 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4534 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4535 (ap->pio_mask << ATA_SHIFT_PIO);
4536
4537 /* print per-port info to dmesg */
4538 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4539 "bmdma 0x%lX irq %lu\n",
4540 ap->id,
4541 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4542 ata_mode_string(xfer_mode_mask),
4543 ap->ioaddr.cmd_addr,
4544 ap->ioaddr.ctl_addr,
4545 ap->ioaddr.bmdma_addr,
4546 ent->irq);
4547
4548 ata_chk_status(ap);
4549 host_set->ops->irq_clear(ap);
4550 count++;
4551 }
4552
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004553 if (!count)
4554 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
4556 /* obtain irq, that is shared between channels */
4557 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4558 DRV_NAME, host_set))
4559 goto err_out;
4560
4561 /* perform each probe synchronously */
4562 DPRINTK("probe begin\n");
4563 for (i = 0; i < count; i++) {
4564 struct ata_port *ap;
4565 int rc;
4566
4567 ap = host_set->ports[i];
4568
4569 DPRINTK("ata%u: probe begin\n", ap->id);
4570 rc = ata_bus_probe(ap);
4571 DPRINTK("ata%u: probe end\n", ap->id);
4572
4573 if (rc) {
4574 /* FIXME: do something useful here?
4575 * Current libata behavior will
4576 * tear down everything when
4577 * the module is removed
4578 * or the h/w is unplugged.
4579 */
4580 }
4581
4582 rc = scsi_add_host(ap->host, dev);
4583 if (rc) {
4584 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4585 ap->id);
4586 /* FIXME: do something useful here */
4587 /* FIXME: handle unconditional calls to
4588 * scsi_scan_host and ata_host_remove, below,
4589 * at the very least
4590 */
4591 }
4592 }
4593
4594 /* probes are done, now scan each port's disk(s) */
4595 DPRINTK("probe begin\n");
4596 for (i = 0; i < count; i++) {
4597 struct ata_port *ap = host_set->ports[i];
4598
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004599 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 }
4601
4602 dev_set_drvdata(dev, host_set);
4603
4604 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4605 return ent->n_ports; /* success */
4606
4607err_out:
4608 for (i = 0; i < count; i++) {
4609 ata_host_remove(host_set->ports[i], 1);
4610 scsi_host_put(host_set->ports[i]->host);
4611 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004612err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 kfree(host_set);
4614 VPRINTK("EXIT, returning 0\n");
4615 return 0;
4616}
4617
4618/**
Alan Cox17b14452005-09-15 15:44:00 +01004619 * ata_host_set_remove - PCI layer callback for device removal
4620 * @host_set: ATA host set that was removed
4621 *
4622 * Unregister all objects associated with this host set. Free those
4623 * objects.
4624 *
4625 * LOCKING:
4626 * Inherited from calling layer (may sleep).
4627 */
4628
Alan Cox17b14452005-09-15 15:44:00 +01004629void ata_host_set_remove(struct ata_host_set *host_set)
4630{
4631 struct ata_port *ap;
4632 unsigned int i;
4633
4634 for (i = 0; i < host_set->n_ports; i++) {
4635 ap = host_set->ports[i];
4636 scsi_remove_host(ap->host);
4637 }
4638
4639 free_irq(host_set->irq, host_set);
4640
4641 for (i = 0; i < host_set->n_ports; i++) {
4642 ap = host_set->ports[i];
4643
4644 ata_scsi_release(ap->host);
4645
4646 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4647 struct ata_ioports *ioaddr = &ap->ioaddr;
4648
4649 if (ioaddr->cmd_addr == 0x1f0)
4650 release_region(0x1f0, 8);
4651 else if (ioaddr->cmd_addr == 0x170)
4652 release_region(0x170, 8);
4653 }
4654
4655 scsi_host_put(ap->host);
4656 }
4657
4658 if (host_set->ops->host_stop)
4659 host_set->ops->host_stop(host_set);
4660
4661 kfree(host_set);
4662}
4663
4664/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 * ata_scsi_release - SCSI layer callback hook for host unload
4666 * @host: libata host to be unloaded
4667 *
4668 * Performs all duties necessary to shut down a libata port...
4669 * Kill port kthread, disable port, and release resources.
4670 *
4671 * LOCKING:
4672 * Inherited from SCSI layer.
4673 *
4674 * RETURNS:
4675 * One.
4676 */
4677
4678int ata_scsi_release(struct Scsi_Host *host)
4679{
4680 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4681
4682 DPRINTK("ENTER\n");
4683
4684 ap->ops->port_disable(ap);
4685 ata_host_remove(ap, 0);
4686
4687 DPRINTK("EXIT\n");
4688 return 1;
4689}
4690
4691/**
4692 * ata_std_ports - initialize ioaddr with standard port offsets.
4693 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004694 *
4695 * Utility function which initializes data_addr, error_addr,
4696 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4697 * device_addr, status_addr, and command_addr to standard offsets
4698 * relative to cmd_addr.
4699 *
4700 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 */
Edward Falk0baab862005-06-02 18:17:13 -04004702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703void ata_std_ports(struct ata_ioports *ioaddr)
4704{
4705 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4706 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4707 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4708 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4709 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4710 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4711 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4712 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4713 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4714 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4715}
4716
4717static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004718ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719{
4720 struct ata_probe_ent *probe_ent;
4721
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004722 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 if (!probe_ent) {
4724 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4725 kobject_name(&(dev->kobj)));
4726 return NULL;
4727 }
4728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 INIT_LIST_HEAD(&probe_ent->node);
4730 probe_ent->dev = dev;
4731
4732 probe_ent->sht = port->sht;
4733 probe_ent->host_flags = port->host_flags;
4734 probe_ent->pio_mask = port->pio_mask;
4735 probe_ent->mwdma_mask = port->mwdma_mask;
4736 probe_ent->udma_mask = port->udma_mask;
4737 probe_ent->port_ops = port->port_ops;
4738
4739 return probe_ent;
4740}
4741
Edward Falk0baab862005-06-02 18:17:13 -04004742
4743
Jeff Garzik374b1872005-08-30 05:42:52 -04004744#ifdef CONFIG_PCI
4745
4746void ata_pci_host_stop (struct ata_host_set *host_set)
4747{
4748 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4749
4750 pci_iounmap(pdev, host_set->mmio_base);
4751}
4752
Edward Falk0baab862005-06-02 18:17:13 -04004753/**
4754 * ata_pci_init_native_mode - Initialize native-mode driver
4755 * @pdev: pci device to be initialized
4756 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004757 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004758 *
4759 * Utility function which allocates and initializes an
4760 * ata_probe_ent structure for a standard dual-port
4761 * PIO-based IDE controller. The returned ata_probe_ent
4762 * structure can be passed to ata_device_add(). The returned
4763 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004764 *
4765 * The caller need only pass the address of the primary port, the
4766 * secondary will be deduced automatically. If the device has non
4767 * standard secondary port mappings this function can be called twice,
4768 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004769 */
4770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004772ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773{
4774 struct ata_probe_ent *probe_ent =
4775 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004776 int p = 0;
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 if (!probe_ent)
4779 return NULL;
4780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 probe_ent->irq = pdev->irq;
4782 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004783 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Alan Cox47a86592005-10-04 08:09:19 -04004785 if (ports & ATA_PORT_PRIMARY) {
4786 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4787 probe_ent->port[p].altstatus_addr =
4788 probe_ent->port[p].ctl_addr =
4789 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4790 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4791 ata_std_ports(&probe_ent->port[p]);
4792 p++;
4793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Alan Cox47a86592005-10-04 08:09:19 -04004795 if (ports & ATA_PORT_SECONDARY) {
4796 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4797 probe_ent->port[p].altstatus_addr =
4798 probe_ent->port[p].ctl_addr =
4799 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4800 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4801 ata_std_ports(&probe_ent->port[p]);
4802 p++;
4803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Alan Cox47a86592005-10-04 08:09:19 -04004805 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 return probe_ent;
4807}
4808
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004809static 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 -07004810{
Alan Cox47a86592005-10-04 08:09:19 -04004811 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004813 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 if (!probe_ent)
4815 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004818 probe_ent->n_ports = 1;
4819 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004820 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Alan Cox47a86592005-10-04 08:09:19 -04004822 switch(port_num)
4823 {
4824 case 0:
4825 probe_ent->irq = 14;
4826 probe_ent->port[0].cmd_addr = 0x1f0;
4827 probe_ent->port[0].altstatus_addr =
4828 probe_ent->port[0].ctl_addr = 0x3f6;
4829 break;
4830 case 1:
4831 probe_ent->irq = 15;
4832 probe_ent->port[0].cmd_addr = 0x170;
4833 probe_ent->port[0].altstatus_addr =
4834 probe_ent->port[0].ctl_addr = 0x376;
4835 break;
4836 }
4837 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 return probe_ent;
4840}
4841
4842/**
4843 * ata_pci_init_one - Initialize/register PCI IDE host controller
4844 * @pdev: Controller to be initialized
4845 * @port_info: Information from low-level host driver
4846 * @n_ports: Number of ports attached to host controller
4847 *
Edward Falk0baab862005-06-02 18:17:13 -04004848 * This is a helper function which can be called from a driver's
4849 * xxx_init_one() probe function if the hardware uses traditional
4850 * IDE taskfile registers.
4851 *
4852 * This function calls pci_enable_device(), reserves its register
4853 * regions, sets the dma mask, enables bus master mode, and calls
4854 * ata_device_add()
4855 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 * LOCKING:
4857 * Inherited from PCI layer (may sleep).
4858 *
4859 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004860 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 */
4862
4863int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4864 unsigned int n_ports)
4865{
Alan Cox47a86592005-10-04 08:09:19 -04004866 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 struct ata_port_info *port[2];
4868 u8 tmp8, mask;
4869 unsigned int legacy_mode = 0;
4870 int disable_dev_on_err = 1;
4871 int rc;
4872
4873 DPRINTK("ENTER\n");
4874
4875 port[0] = port_info[0];
4876 if (n_ports > 1)
4877 port[1] = port_info[1];
4878 else
4879 port[1] = port[0];
4880
4881 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4882 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004883 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4885 mask = (1 << 2) | (1 << 0);
4886 if ((tmp8 & mask) != mask)
4887 legacy_mode = (1 << 3);
4888 }
4889
4890 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004891 if ((!legacy_mode) && (n_ports > 2)) {
4892 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4893 n_ports = 2;
4894 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 }
4896
Alan Cox47a86592005-10-04 08:09:19 -04004897 /* FIXME: Really for ATA it isn't safe because the device may be
4898 multi-purpose and we want to leave it alone if it was already
4899 enabled. Secondly for shared use as Arjan says we want refcounting
4900
4901 Checking dev->is_enabled is insufficient as this is not set at
4902 boot for the primary video which is BIOS enabled
4903 */
4904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 rc = pci_enable_device(pdev);
4906 if (rc)
4907 return rc;
4908
4909 rc = pci_request_regions(pdev, DRV_NAME);
4910 if (rc) {
4911 disable_dev_on_err = 0;
4912 goto err_out;
4913 }
4914
Alan Cox47a86592005-10-04 08:09:19 -04004915 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 if (legacy_mode) {
4917 if (!request_region(0x1f0, 8, "libata")) {
4918 struct resource *conflict, res;
4919 res.start = 0x1f0;
4920 res.end = 0x1f0 + 8 - 1;
4921 conflict = ____request_resource(&ioport_resource, &res);
4922 if (!strcmp(conflict->name, "libata"))
4923 legacy_mode |= (1 << 0);
4924 else {
4925 disable_dev_on_err = 0;
4926 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4927 }
4928 } else
4929 legacy_mode |= (1 << 0);
4930
4931 if (!request_region(0x170, 8, "libata")) {
4932 struct resource *conflict, res;
4933 res.start = 0x170;
4934 res.end = 0x170 + 8 - 1;
4935 conflict = ____request_resource(&ioport_resource, &res);
4936 if (!strcmp(conflict->name, "libata"))
4937 legacy_mode |= (1 << 1);
4938 else {
4939 disable_dev_on_err = 0;
4940 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4941 }
4942 } else
4943 legacy_mode |= (1 << 1);
4944 }
4945
4946 /* we have legacy mode, but all ports are unavailable */
4947 if (legacy_mode == (1 << 3)) {
4948 rc = -EBUSY;
4949 goto err_out_regions;
4950 }
4951
4952 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4953 if (rc)
4954 goto err_out_regions;
4955 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4956 if (rc)
4957 goto err_out_regions;
4958
4959 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004960 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004961 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004962 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004963 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004964 } else {
4965 if (n_ports == 2)
4966 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4967 else
4968 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4969 }
4970 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 rc = -ENOMEM;
4972 goto err_out_regions;
4973 }
4974
4975 pci_set_master(pdev);
4976
4977 /* FIXME: check ata_device_add return */
4978 if (legacy_mode) {
4979 if (legacy_mode & (1 << 0))
4980 ata_device_add(probe_ent);
4981 if (legacy_mode & (1 << 1))
4982 ata_device_add(probe_ent2);
4983 } else
4984 ata_device_add(probe_ent);
4985
4986 kfree(probe_ent);
4987 kfree(probe_ent2);
4988
4989 return 0;
4990
4991err_out_regions:
4992 if (legacy_mode & (1 << 0))
4993 release_region(0x1f0, 8);
4994 if (legacy_mode & (1 << 1))
4995 release_region(0x170, 8);
4996 pci_release_regions(pdev);
4997err_out:
4998 if (disable_dev_on_err)
4999 pci_disable_device(pdev);
5000 return rc;
5001}
5002
5003/**
5004 * ata_pci_remove_one - PCI layer callback for device removal
5005 * @pdev: PCI device that was removed
5006 *
5007 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005008 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 * Handle this by unregistering all objects associated
5010 * with this PCI device. Free those objects. Then finally
5011 * release PCI resources and disable device.
5012 *
5013 * LOCKING:
5014 * Inherited from PCI layer (may sleep).
5015 */
5016
5017void ata_pci_remove_one (struct pci_dev *pdev)
5018{
5019 struct device *dev = pci_dev_to_dev(pdev);
5020 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Alan Cox17b14452005-09-15 15:44:00 +01005022 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 pci_release_regions(pdev);
5024 pci_disable_device(pdev);
5025 dev_set_drvdata(dev, NULL);
5026}
5027
5028/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005029int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030{
5031 unsigned long tmp = 0;
5032
5033 switch (bits->width) {
5034 case 1: {
5035 u8 tmp8 = 0;
5036 pci_read_config_byte(pdev, bits->reg, &tmp8);
5037 tmp = tmp8;
5038 break;
5039 }
5040 case 2: {
5041 u16 tmp16 = 0;
5042 pci_read_config_word(pdev, bits->reg, &tmp16);
5043 tmp = tmp16;
5044 break;
5045 }
5046 case 4: {
5047 u32 tmp32 = 0;
5048 pci_read_config_dword(pdev, bits->reg, &tmp32);
5049 tmp = tmp32;
5050 break;
5051 }
5052
5053 default:
5054 return -EINVAL;
5055 }
5056
5057 tmp &= bits->mask;
5058
5059 return (tmp == bits->val) ? 1 : 0;
5060}
Jens Axboe9b847542006-01-06 09:28:07 +01005061
5062int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5063{
5064 pci_save_state(pdev);
5065 pci_disable_device(pdev);
5066 pci_set_power_state(pdev, PCI_D3hot);
5067 return 0;
5068}
5069
5070int ata_pci_device_resume(struct pci_dev *pdev)
5071{
5072 pci_set_power_state(pdev, PCI_D0);
5073 pci_restore_state(pdev);
5074 pci_enable_device(pdev);
5075 pci_set_master(pdev);
5076 return 0;
5077}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078#endif /* CONFIG_PCI */
5079
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081static int __init ata_init(void)
5082{
5083 ata_wq = create_workqueue("ata");
5084 if (!ata_wq)
5085 return -ENOMEM;
5086
5087 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5088 return 0;
5089}
5090
5091static void __exit ata_exit(void)
5092{
5093 destroy_workqueue(ata_wq);
5094}
5095
5096module_init(ata_init);
5097module_exit(ata_exit);
5098
Jeff Garzik67846b32005-10-05 02:58:32 -04005099static unsigned long ratelimit_time;
5100static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5101
5102int ata_ratelimit(void)
5103{
5104 int rc;
5105 unsigned long flags;
5106
5107 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5108
5109 if (time_after(jiffies, ratelimit_time)) {
5110 rc = 1;
5111 ratelimit_time = jiffies + (HZ/5);
5112 } else
5113 rc = 0;
5114
5115 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5116
5117 return rc;
5118}
5119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120/*
5121 * libata is essentially a library of internal helper functions for
5122 * low-level ATA host controller drivers. As such, the API/ABI is
5123 * likely to change as new drivers are added and updated.
5124 * Do not depend on ABI/API stability.
5125 */
5126
5127EXPORT_SYMBOL_GPL(ata_std_bios_param);
5128EXPORT_SYMBOL_GPL(ata_std_ports);
5129EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005130EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131EXPORT_SYMBOL_GPL(ata_sg_init);
5132EXPORT_SYMBOL_GPL(ata_sg_init_one);
5133EXPORT_SYMBOL_GPL(ata_qc_complete);
5134EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5135EXPORT_SYMBOL_GPL(ata_eng_timeout);
5136EXPORT_SYMBOL_GPL(ata_tf_load);
5137EXPORT_SYMBOL_GPL(ata_tf_read);
5138EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5139EXPORT_SYMBOL_GPL(ata_std_dev_select);
5140EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5141EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5142EXPORT_SYMBOL_GPL(ata_check_status);
5143EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144EXPORT_SYMBOL_GPL(ata_exec_command);
5145EXPORT_SYMBOL_GPL(ata_port_start);
5146EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005147EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148EXPORT_SYMBOL_GPL(ata_interrupt);
5149EXPORT_SYMBOL_GPL(ata_qc_prep);
5150EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5151EXPORT_SYMBOL_GPL(ata_bmdma_start);
5152EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5153EXPORT_SYMBOL_GPL(ata_bmdma_status);
5154EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5155EXPORT_SYMBOL_GPL(ata_port_probe);
5156EXPORT_SYMBOL_GPL(sata_phy_reset);
5157EXPORT_SYMBOL_GPL(__sata_phy_reset);
5158EXPORT_SYMBOL_GPL(ata_bus_reset);
5159EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005160EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5162EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5163EXPORT_SYMBOL_GPL(ata_scsi_error);
5164EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5165EXPORT_SYMBOL_GPL(ata_scsi_release);
5166EXPORT_SYMBOL_GPL(ata_host_intr);
5167EXPORT_SYMBOL_GPL(ata_dev_classify);
5168EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005169EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5171
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005172EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005173EXPORT_SYMBOL_GPL(ata_timing_compute);
5174EXPORT_SYMBOL_GPL(ata_timing_merge);
5175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176#ifdef CONFIG_PCI
5177EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005178EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5180EXPORT_SYMBOL_GPL(ata_pci_init_one);
5181EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005182EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5183EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005185
5186EXPORT_SYMBOL_GPL(ata_device_suspend);
5187EXPORT_SYMBOL_GPL(ata_device_resume);
5188EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5189EXPORT_SYMBOL_GPL(ata_scsi_device_resume);