blob: 41f76b9bf99291a069b870a0a70d5b1c85e9edbd [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 Lee8bf62ece2005-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 -070076static void __ata_qc_complete(struct ata_queued_cmd *qc);
Jeff Garzike33b9df2005-10-09 09:51:46 -040077static void ata_pio_error(struct ata_port *ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static unsigned int ata_unique_id = 1;
80static struct workqueue_struct *ata_wq;
81
Jeff Garzik1623c812005-08-30 03:37:42 -040082int atapi_enabled = 0;
83module_param(atapi_enabled, int, 0444);
84MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_AUTHOR("Jeff Garzik");
87MODULE_DESCRIPTION("Library module for ATA devices");
88MODULE_LICENSE("GPL");
89MODULE_VERSION(DRV_VERSION);
90
91/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040092 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * @ap: Port to which output is sent
94 * @tf: ATA taskfile register set
95 *
96 * Outputs ATA taskfile to standard ATA host controller.
97 *
98 * LOCKING:
99 * Inherited from caller.
100 */
101
Jeff Garzik057ace52005-10-22 14:27:05 -0400102static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct ata_ioports *ioaddr = &ap->ioaddr;
105 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
106
107 if (tf->ctl != ap->last_ctl) {
108 outb(tf->ctl, ioaddr->ctl_addr);
109 ap->last_ctl = tf->ctl;
110 ata_wait_idle(ap);
111 }
112
113 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
114 outb(tf->hob_feature, ioaddr->feature_addr);
115 outb(tf->hob_nsect, ioaddr->nsect_addr);
116 outb(tf->hob_lbal, ioaddr->lbal_addr);
117 outb(tf->hob_lbam, ioaddr->lbam_addr);
118 outb(tf->hob_lbah, ioaddr->lbah_addr);
119 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
120 tf->hob_feature,
121 tf->hob_nsect,
122 tf->hob_lbal,
123 tf->hob_lbam,
124 tf->hob_lbah);
125 }
126
127 if (is_addr) {
128 outb(tf->feature, ioaddr->feature_addr);
129 outb(tf->nsect, ioaddr->nsect_addr);
130 outb(tf->lbal, ioaddr->lbal_addr);
131 outb(tf->lbam, ioaddr->lbam_addr);
132 outb(tf->lbah, ioaddr->lbah_addr);
133 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
134 tf->feature,
135 tf->nsect,
136 tf->lbal,
137 tf->lbam,
138 tf->lbah);
139 }
140
141 if (tf->flags & ATA_TFLAG_DEVICE) {
142 outb(tf->device, ioaddr->device_addr);
143 VPRINTK("device 0x%X\n", tf->device);
144 }
145
146 ata_wait_idle(ap);
147}
148
149/**
150 * ata_tf_load_mmio - send taskfile registers to host controller
151 * @ap: Port to which output is sent
152 * @tf: ATA taskfile register set
153 *
154 * Outputs ATA taskfile to standard ATA host controller using MMIO.
155 *
156 * LOCKING:
157 * Inherited from caller.
158 */
159
Jeff Garzik057ace52005-10-22 14:27:05 -0400160static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct ata_ioports *ioaddr = &ap->ioaddr;
163 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
164
165 if (tf->ctl != ap->last_ctl) {
166 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
167 ap->last_ctl = tf->ctl;
168 ata_wait_idle(ap);
169 }
170
171 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
172 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
173 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
174 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
175 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
176 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
177 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
178 tf->hob_feature,
179 tf->hob_nsect,
180 tf->hob_lbal,
181 tf->hob_lbam,
182 tf->hob_lbah);
183 }
184
185 if (is_addr) {
186 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
187 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
188 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
189 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
190 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
191 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
192 tf->feature,
193 tf->nsect,
194 tf->lbal,
195 tf->lbam,
196 tf->lbah);
197 }
198
199 if (tf->flags & ATA_TFLAG_DEVICE) {
200 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
201 VPRINTK("device 0x%X\n", tf->device);
202 }
203
204 ata_wait_idle(ap);
205}
206
Edward Falk0baab862005-06-02 18:17:13 -0400207
208/**
209 * ata_tf_load - send taskfile registers to host controller
210 * @ap: Port to which output is sent
211 * @tf: ATA taskfile register set
212 *
213 * Outputs ATA taskfile to standard ATA host controller using MMIO
214 * or PIO as indicated by the ATA_FLAG_MMIO flag.
215 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
216 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
217 * hob_lbal, hob_lbam, and hob_lbah.
218 *
219 * This function waits for idle (!BUSY and !DRQ) after writing
220 * registers. If the control register has a new value, this
221 * function also waits for idle after writing control and before
222 * writing the remaining registers.
223 *
224 * May be used as the tf_load() entry in ata_port_operations.
225 *
226 * LOCKING:
227 * Inherited from caller.
228 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400229void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 if (ap->flags & ATA_FLAG_MMIO)
232 ata_tf_load_mmio(ap, tf);
233 else
234 ata_tf_load_pio(ap, tf);
235}
236
237/**
Edward Falk0baab862005-06-02 18:17:13 -0400238 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * @ap: port to which command is being issued
240 * @tf: ATA taskfile register set
241 *
Edward Falk0baab862005-06-02 18:17:13 -0400242 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * synchronization with interrupt handler / other threads.
244 *
245 * LOCKING:
246 * spin_lock_irqsave(host_set lock)
247 */
248
Jeff Garzik057ace52005-10-22 14:27:05 -0400249static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
252
253 outb(tf->command, ap->ioaddr.command_addr);
254 ata_pause(ap);
255}
256
257
258/**
259 * ata_exec_command_mmio - issue ATA command to host controller
260 * @ap: port to which command is being issued
261 * @tf: ATA taskfile register set
262 *
263 * Issues MMIO write to ATA command register, with proper
264 * synchronization with interrupt handler / other threads.
265 *
266 * LOCKING:
267 * spin_lock_irqsave(host_set lock)
268 */
269
Jeff Garzik057ace52005-10-22 14:27:05 -0400270static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
273
274 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
275 ata_pause(ap);
276}
277
Edward Falk0baab862005-06-02 18:17:13 -0400278
279/**
280 * ata_exec_command - issue ATA command to host controller
281 * @ap: port to which command is being issued
282 * @tf: ATA taskfile register set
283 *
284 * Issues PIO/MMIO write to ATA command register, with proper
285 * synchronization with interrupt handler / other threads.
286 *
287 * LOCKING:
288 * spin_lock_irqsave(host_set lock)
289 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400290void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 if (ap->flags & ATA_FLAG_MMIO)
293 ata_exec_command_mmio(ap, tf);
294 else
295 ata_exec_command_pio(ap, tf);
296}
297
298/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * ata_tf_to_host - issue ATA taskfile to host controller
300 * @ap: port to which command is being issued
301 * @tf: ATA taskfile register set
302 *
303 * Issues ATA taskfile register set to ATA host controller,
304 * with proper synchronization with interrupt handler and
305 * other threads.
306 *
307 * LOCKING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * spin_lock_irqsave(host_set lock)
309 */
310
Jeff Garzike5338252005-10-30 21:37:17 -0500311static inline void ata_tf_to_host(struct ata_port *ap,
312 const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 ap->ops->tf_load(ap, tf);
315 ap->ops->exec_command(ap, tf);
316}
317
318/**
Edward Falk0baab862005-06-02 18:17:13 -0400319 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * @ap: Port from which input is read
321 * @tf: ATA taskfile register set for storing input
322 *
323 * Reads ATA taskfile registers for currently-selected device
324 * into @tf.
325 *
326 * LOCKING:
327 * Inherited from caller.
328 */
329
330static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
331{
332 struct ata_ioports *ioaddr = &ap->ioaddr;
333
Jeff Garzikac19bff2005-10-29 13:58:21 -0400334 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400335 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 tf->nsect = inb(ioaddr->nsect_addr);
337 tf->lbal = inb(ioaddr->lbal_addr);
338 tf->lbam = inb(ioaddr->lbam_addr);
339 tf->lbah = inb(ioaddr->lbah_addr);
340 tf->device = inb(ioaddr->device_addr);
341
342 if (tf->flags & ATA_TFLAG_LBA48) {
343 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
344 tf->hob_feature = inb(ioaddr->error_addr);
345 tf->hob_nsect = inb(ioaddr->nsect_addr);
346 tf->hob_lbal = inb(ioaddr->lbal_addr);
347 tf->hob_lbam = inb(ioaddr->lbam_addr);
348 tf->hob_lbah = inb(ioaddr->lbah_addr);
349 }
350}
351
352/**
353 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
354 * @ap: Port from which input is read
355 * @tf: ATA taskfile register set for storing input
356 *
357 * Reads ATA taskfile registers for currently-selected device
358 * into @tf via MMIO.
359 *
360 * LOCKING:
361 * Inherited from caller.
362 */
363
364static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
365{
366 struct ata_ioports *ioaddr = &ap->ioaddr;
367
Jeff Garzikac19bff2005-10-29 13:58:21 -0400368 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400369 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
371 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
372 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
373 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
374 tf->device = readb((void __iomem *)ioaddr->device_addr);
375
376 if (tf->flags & ATA_TFLAG_LBA48) {
377 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
378 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
379 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
380 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
381 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
382 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
383 }
384}
385
Edward Falk0baab862005-06-02 18:17:13 -0400386
387/**
388 * ata_tf_read - input device's ATA taskfile shadow registers
389 * @ap: Port from which input is read
390 * @tf: ATA taskfile register set for storing input
391 *
392 * Reads ATA taskfile registers for currently-selected device
393 * into @tf.
394 *
395 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
396 * is set, also reads the hob registers.
397 *
398 * May be used as the tf_read() entry in ata_port_operations.
399 *
400 * LOCKING:
401 * Inherited from caller.
402 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
404{
405 if (ap->flags & ATA_FLAG_MMIO)
406 ata_tf_read_mmio(ap, tf);
407 else
408 ata_tf_read_pio(ap, tf);
409}
410
411/**
412 * ata_check_status_pio - Read device status reg & clear interrupt
413 * @ap: port where the device is
414 *
415 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400416 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * from this device
418 *
419 * LOCKING:
420 * Inherited from caller.
421 */
422static u8 ata_check_status_pio(struct ata_port *ap)
423{
424 return inb(ap->ioaddr.status_addr);
425}
426
427/**
428 * ata_check_status_mmio - Read device status reg & clear interrupt
429 * @ap: port where the device is
430 *
431 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400432 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * from this device
434 *
435 * LOCKING:
436 * Inherited from caller.
437 */
438static u8 ata_check_status_mmio(struct ata_port *ap)
439{
440 return readb((void __iomem *) ap->ioaddr.status_addr);
441}
442
Edward Falk0baab862005-06-02 18:17:13 -0400443
444/**
445 * ata_check_status - Read device status reg & clear interrupt
446 * @ap: port where the device is
447 *
448 * Reads ATA taskfile status register for currently-selected device
449 * and return its value. This also clears pending interrupts
450 * from this device
451 *
452 * May be used as the check_status() entry in ata_port_operations.
453 *
454 * LOCKING:
455 * Inherited from caller.
456 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457u8 ata_check_status(struct ata_port *ap)
458{
459 if (ap->flags & ATA_FLAG_MMIO)
460 return ata_check_status_mmio(ap);
461 return ata_check_status_pio(ap);
462}
463
Edward Falk0baab862005-06-02 18:17:13 -0400464
465/**
466 * ata_altstatus - Read device alternate status reg
467 * @ap: port where the device is
468 *
469 * Reads ATA taskfile alternate status register for
470 * currently-selected device and return its value.
471 *
472 * Note: may NOT be used as the check_altstatus() entry in
473 * ata_port_operations.
474 *
475 * LOCKING:
476 * Inherited from caller.
477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478u8 ata_altstatus(struct ata_port *ap)
479{
480 if (ap->ops->check_altstatus)
481 return ap->ops->check_altstatus(ap);
482
483 if (ap->flags & ATA_FLAG_MMIO)
484 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
485 return inb(ap->ioaddr.altstatus_addr);
486}
487
Edward Falk0baab862005-06-02 18:17:13 -0400488
489/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
491 * @tf: Taskfile to convert
492 * @fis: Buffer into which data will output
493 * @pmp: Port multiplier port
494 *
495 * Converts a standard ATA taskfile to a Serial ATA
496 * FIS structure (Register - Host to Device).
497 *
498 * LOCKING:
499 * Inherited from caller.
500 */
501
Jeff Garzik057ace52005-10-22 14:27:05 -0400502void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 fis[0] = 0x27; /* Register - Host to Device FIS */
505 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
506 bit 7 indicates Command FIS */
507 fis[2] = tf->command;
508 fis[3] = tf->feature;
509
510 fis[4] = tf->lbal;
511 fis[5] = tf->lbam;
512 fis[6] = tf->lbah;
513 fis[7] = tf->device;
514
515 fis[8] = tf->hob_lbal;
516 fis[9] = tf->hob_lbam;
517 fis[10] = tf->hob_lbah;
518 fis[11] = tf->hob_feature;
519
520 fis[12] = tf->nsect;
521 fis[13] = tf->hob_nsect;
522 fis[14] = 0;
523 fis[15] = tf->ctl;
524
525 fis[16] = 0;
526 fis[17] = 0;
527 fis[18] = 0;
528 fis[19] = 0;
529}
530
531/**
532 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
533 * @fis: Buffer from which data will be input
534 * @tf: Taskfile to output
535 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500536 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 *
538 * LOCKING:
539 * Inherited from caller.
540 */
541
Jeff Garzik057ace52005-10-22 14:27:05 -0400542void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 tf->command = fis[2]; /* status */
545 tf->feature = fis[3]; /* error */
546
547 tf->lbal = fis[4];
548 tf->lbam = fis[5];
549 tf->lbah = fis[6];
550 tf->device = fis[7];
551
552 tf->hob_lbal = fis[8];
553 tf->hob_lbam = fis[9];
554 tf->hob_lbah = fis[10];
555
556 tf->nsect = fis[12];
557 tf->hob_nsect = fis[13];
558}
559
Albert Lee8cbd6df2005-10-12 15:06:27 +0800560static const u8 ata_rw_cmds[] = {
561 /* pio multi */
562 ATA_CMD_READ_MULTI,
563 ATA_CMD_WRITE_MULTI,
564 ATA_CMD_READ_MULTI_EXT,
565 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100566 0,
567 0,
568 0,
569 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800570 /* pio */
571 ATA_CMD_PIO_READ,
572 ATA_CMD_PIO_WRITE,
573 ATA_CMD_PIO_READ_EXT,
574 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100575 0,
576 0,
577 0,
578 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800579 /* dma */
580 ATA_CMD_READ,
581 ATA_CMD_WRITE,
582 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100583 ATA_CMD_WRITE_EXT,
584 0,
585 0,
586 0,
587 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800588};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800591 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
592 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800594 * Examine the device configuration and tf->flags to calculate
595 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
597 * LOCKING:
598 * caller.
599 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100600int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800602 struct ata_taskfile *tf = &qc->tf;
603 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100604 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100606 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800607
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100608 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800609 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
610 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Albert Lee8cbd6df2005-10-12 15:06:27 +0800612 if (dev->flags & ATA_DFLAG_PIO) {
613 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100614 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800615 } else {
616 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100617 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100620 cmd = ata_rw_cmds[index + fua + lba48 + write];
621 if (cmd) {
622 tf->command = cmd;
623 return 0;
624 }
625 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100628static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 "UDMA/16",
630 "UDMA/25",
631 "UDMA/33",
632 "UDMA/44",
633 "UDMA/66",
634 "UDMA/100",
635 "UDMA/133",
636 "UDMA7",
637 "MWDMA0",
638 "MWDMA1",
639 "MWDMA2",
640 "PIO0",
641 "PIO1",
642 "PIO2",
643 "PIO3",
644 "PIO4",
645};
646
647/**
648 * ata_udma_string - convert UDMA bit offset to string
649 * @mask: mask of bits supported; only highest bit counts.
650 *
651 * Determine string which represents the highest speed
652 * (highest bit in @udma_mask).
653 *
654 * LOCKING:
655 * None.
656 *
657 * RETURNS:
658 * Constant C string representing highest speed listed in
659 * @udma_mask, or the constant C string "<n/a>".
660 */
661
662static const char *ata_mode_string(unsigned int mask)
663{
664 int i;
665
666 for (i = 7; i >= 0; i--)
667 if (mask & (1 << i))
668 goto out;
669 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
670 if (mask & (1 << i))
671 goto out;
672 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
673 if (mask & (1 << i))
674 goto out;
675
676 return "<n/a>";
677
678out:
679 return xfer_mode_str[i];
680}
681
682/**
683 * ata_pio_devchk - PATA device presence detection
684 * @ap: ATA channel to examine
685 * @device: Device to examine (starting at zero)
686 *
687 * This technique was originally described in
688 * Hale Landis's ATADRVR (www.ata-atapi.com), and
689 * later found its way into the ATA/ATAPI spec.
690 *
691 * Write a pattern to the ATA shadow registers,
692 * and if a device is present, it will respond by
693 * correctly storing and echoing back the
694 * ATA shadow register contents.
695 *
696 * LOCKING:
697 * caller.
698 */
699
700static unsigned int ata_pio_devchk(struct ata_port *ap,
701 unsigned int device)
702{
703 struct ata_ioports *ioaddr = &ap->ioaddr;
704 u8 nsect, lbal;
705
706 ap->ops->dev_select(ap, device);
707
708 outb(0x55, ioaddr->nsect_addr);
709 outb(0xaa, ioaddr->lbal_addr);
710
711 outb(0xaa, ioaddr->nsect_addr);
712 outb(0x55, ioaddr->lbal_addr);
713
714 outb(0x55, ioaddr->nsect_addr);
715 outb(0xaa, ioaddr->lbal_addr);
716
717 nsect = inb(ioaddr->nsect_addr);
718 lbal = inb(ioaddr->lbal_addr);
719
720 if ((nsect == 0x55) && (lbal == 0xaa))
721 return 1; /* we found a device */
722
723 return 0; /* nothing found */
724}
725
726/**
727 * ata_mmio_devchk - PATA device presence detection
728 * @ap: ATA channel to examine
729 * @device: Device to examine (starting at zero)
730 *
731 * This technique was originally described in
732 * Hale Landis's ATADRVR (www.ata-atapi.com), and
733 * later found its way into the ATA/ATAPI spec.
734 *
735 * Write a pattern to the ATA shadow registers,
736 * and if a device is present, it will respond by
737 * correctly storing and echoing back the
738 * ATA shadow register contents.
739 *
740 * LOCKING:
741 * caller.
742 */
743
744static unsigned int ata_mmio_devchk(struct ata_port *ap,
745 unsigned int device)
746{
747 struct ata_ioports *ioaddr = &ap->ioaddr;
748 u8 nsect, lbal;
749
750 ap->ops->dev_select(ap, device);
751
752 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
753 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
754
755 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
756 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
757
758 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
759 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
760
761 nsect = readb((void __iomem *) ioaddr->nsect_addr);
762 lbal = readb((void __iomem *) ioaddr->lbal_addr);
763
764 if ((nsect == 0x55) && (lbal == 0xaa))
765 return 1; /* we found a device */
766
767 return 0; /* nothing found */
768}
769
770/**
771 * ata_devchk - PATA device presence detection
772 * @ap: ATA channel to examine
773 * @device: Device to examine (starting at zero)
774 *
775 * Dispatch ATA device presence detection, depending
776 * on whether we are using PIO or MMIO to talk to the
777 * ATA shadow registers.
778 *
779 * LOCKING:
780 * caller.
781 */
782
783static unsigned int ata_devchk(struct ata_port *ap,
784 unsigned int device)
785{
786 if (ap->flags & ATA_FLAG_MMIO)
787 return ata_mmio_devchk(ap, device);
788 return ata_pio_devchk(ap, device);
789}
790
791/**
792 * ata_dev_classify - determine device type based on ATA-spec signature
793 * @tf: ATA taskfile register set for device to be identified
794 *
795 * Determine from taskfile register contents whether a device is
796 * ATA or ATAPI, as per "Signature and persistence" section
797 * of ATA/PI spec (volume 1, sect 5.14).
798 *
799 * LOCKING:
800 * None.
801 *
802 * RETURNS:
803 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
804 * the event of failure.
805 */
806
Jeff Garzik057ace52005-10-22 14:27:05 -0400807unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 /* Apple's open source Darwin code hints that some devices only
810 * put a proper signature into the LBA mid/high registers,
811 * So, we only check those. It's sufficient for uniqueness.
812 */
813
814 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
815 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
816 DPRINTK("found ATA device by sig\n");
817 return ATA_DEV_ATA;
818 }
819
820 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
821 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
822 DPRINTK("found ATAPI device by sig\n");
823 return ATA_DEV_ATAPI;
824 }
825
826 DPRINTK("unknown device\n");
827 return ATA_DEV_UNKNOWN;
828}
829
830/**
831 * ata_dev_try_classify - Parse returned ATA device signature
832 * @ap: ATA channel to examine
833 * @device: Device to examine (starting at zero)
834 *
835 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
836 * an ATA/ATAPI-defined set of values is placed in the ATA
837 * shadow registers, indicating the results of device detection
838 * and diagnostics.
839 *
840 * Select the ATA device, and read the values from the ATA shadow
841 * registers. Then parse according to the Error register value,
842 * and the spec-defined values examined by ata_dev_classify().
843 *
844 * LOCKING:
845 * caller.
846 */
847
848static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
849{
850 struct ata_device *dev = &ap->device[device];
851 struct ata_taskfile tf;
852 unsigned int class;
853 u8 err;
854
855 ap->ops->dev_select(ap, device);
856
857 memset(&tf, 0, sizeof(tf));
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400860 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 dev->class = ATA_DEV_NONE;
863
864 /* see if device passed diags */
865 if (err == 1)
866 /* do nothing */ ;
867 else if ((device == 0) && (err == 0x81))
868 /* do nothing */ ;
869 else
870 return err;
871
872 /* determine if device if ATA or ATAPI */
873 class = ata_dev_classify(&tf);
874 if (class == ATA_DEV_UNKNOWN)
875 return err;
876 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
877 return err;
878
879 dev->class = class;
880
881 return err;
882}
883
884/**
885 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
886 * @id: IDENTIFY DEVICE results we will examine
887 * @s: string into which data is output
888 * @ofs: offset into identify device page
889 * @len: length of string to return. must be an even number.
890 *
891 * The strings in the IDENTIFY DEVICE page are broken up into
892 * 16-bit chunks. Run through the string, and output each
893 * 8-bit chunk linearly, regardless of platform.
894 *
895 * LOCKING:
896 * caller.
897 */
898
Jeff Garzik057ace52005-10-22 14:27:05 -0400899void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 unsigned int ofs, unsigned int len)
901{
902 unsigned int c;
903
904 while (len > 0) {
905 c = id[ofs] >> 8;
906 *s = c;
907 s++;
908
909 c = id[ofs] & 0xff;
910 *s = c;
911 s++;
912
913 ofs++;
914 len -= 2;
915 }
916}
917
Edward Falk0baab862005-06-02 18:17:13 -0400918
919/**
920 * ata_noop_dev_select - Select device 0/1 on ATA bus
921 * @ap: ATA channel to manipulate
922 * @device: ATA device (numbered from zero) to select
923 *
924 * This function performs no actual function.
925 *
926 * May be used as the dev_select() entry in ata_port_operations.
927 *
928 * LOCKING:
929 * caller.
930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
932{
933}
934
Edward Falk0baab862005-06-02 18:17:13 -0400935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/**
937 * ata_std_dev_select - Select device 0/1 on ATA bus
938 * @ap: ATA channel to manipulate
939 * @device: ATA device (numbered from zero) to select
940 *
941 * Use the method defined in the ATA specification to
942 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400943 * ATA channel. Works with both PIO and MMIO.
944 *
945 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 *
947 * LOCKING:
948 * caller.
949 */
950
951void ata_std_dev_select (struct ata_port *ap, unsigned int device)
952{
953 u8 tmp;
954
955 if (device == 0)
956 tmp = ATA_DEVICE_OBS;
957 else
958 tmp = ATA_DEVICE_OBS | ATA_DEV1;
959
960 if (ap->flags & ATA_FLAG_MMIO) {
961 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
962 } else {
963 outb(tmp, ap->ioaddr.device_addr);
964 }
965 ata_pause(ap); /* needed; also flushes, for mmio */
966}
967
968/**
969 * ata_dev_select - Select device 0/1 on ATA bus
970 * @ap: ATA channel to manipulate
971 * @device: ATA device (numbered from zero) to select
972 * @wait: non-zero to wait for Status register BSY bit to clear
973 * @can_sleep: non-zero if context allows sleeping
974 *
975 * Use the method defined in the ATA specification to
976 * make either device 0, or device 1, active on the
977 * ATA channel.
978 *
979 * This is a high-level version of ata_std_dev_select(),
980 * which additionally provides the services of inserting
981 * the proper pauses and status polling, where needed.
982 *
983 * LOCKING:
984 * caller.
985 */
986
987void ata_dev_select(struct ata_port *ap, unsigned int device,
988 unsigned int wait, unsigned int can_sleep)
989{
990 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
991 ap->id, device, wait);
992
993 if (wait)
994 ata_wait_idle(ap);
995
996 ap->ops->dev_select(ap, device);
997
998 if (wait) {
999 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1000 msleep(150);
1001 ata_wait_idle(ap);
1002 }
1003}
1004
1005/**
1006 * ata_dump_id - IDENTIFY DEVICE info debugging output
1007 * @dev: Device whose IDENTIFY DEVICE page we will dump
1008 *
1009 * Dump selected 16-bit words from a detected device's
1010 * IDENTIFY PAGE page.
1011 *
1012 * LOCKING:
1013 * caller.
1014 */
1015
Jeff Garzik057ace52005-10-22 14:27:05 -04001016static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 DPRINTK("49==0x%04x "
1019 "53==0x%04x "
1020 "63==0x%04x "
1021 "64==0x%04x "
1022 "75==0x%04x \n",
1023 dev->id[49],
1024 dev->id[53],
1025 dev->id[63],
1026 dev->id[64],
1027 dev->id[75]);
1028 DPRINTK("80==0x%04x "
1029 "81==0x%04x "
1030 "82==0x%04x "
1031 "83==0x%04x "
1032 "84==0x%04x \n",
1033 dev->id[80],
1034 dev->id[81],
1035 dev->id[82],
1036 dev->id[83],
1037 dev->id[84]);
1038 DPRINTK("88==0x%04x "
1039 "93==0x%04x\n",
1040 dev->id[88],
1041 dev->id[93]);
1042}
1043
Alan Cox11e29e22005-10-21 18:46:32 -04001044/*
1045 * Compute the PIO modes available for this device. This is not as
1046 * trivial as it seems if we must consider early devices correctly.
1047 *
1048 * FIXME: pre IDE drive timing (do we care ?).
1049 */
1050
Jeff Garzik057ace52005-10-22 14:27:05 -04001051static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001052{
1053 u16 modes;
1054
Alan Coxffa29452006-01-09 17:14:40 +00001055 /* Usual case. Word 53 indicates word 64 is valid */
1056 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -04001057 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1058 modes <<= 3;
1059 modes |= 0x7;
1060 return modes;
1061 }
1062
Alan Coxffa29452006-01-09 17:14:40 +00001063 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
1064 number for the maximum. Turn it into a mask and return it */
1065 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -04001066 return modes;
Alan Coxffa29452006-01-09 17:14:40 +00001067 /* But wait.. there's more. Design your standards by committee and
1068 you too can get a free iordy field to process. However its the
1069 speeds not the modes that are supported... Note drivers using the
1070 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -04001071}
1072
Tejun Heoa2a7a662005-12-13 14:48:31 +09001073struct ata_exec_internal_arg {
1074 unsigned int err_mask;
1075 struct ata_taskfile *tf;
1076 struct completion *waiting;
1077};
1078
1079int ata_qc_complete_internal(struct ata_queued_cmd *qc)
Jeff Garzik64f043d2005-11-17 10:50:01 -05001080{
Tejun Heoa2a7a662005-12-13 14:48:31 +09001081 struct ata_exec_internal_arg *arg = qc->private_data;
1082 struct completion *waiting = arg->waiting;
Jeff Garzik64f043d2005-11-17 10:50:01 -05001083
Tejun Heoa2a7a662005-12-13 14:48:31 +09001084 if (!(qc->err_mask & ~AC_ERR_DEV))
1085 qc->ap->ops->tf_read(qc->ap, arg->tf);
1086 arg->err_mask = qc->err_mask;
1087 arg->waiting = NULL;
1088 complete(waiting);
Jeff Garzik64f043d2005-11-17 10:50:01 -05001089
Tejun Heoa2a7a662005-12-13 14:48:31 +09001090 return 0;
1091}
Jeff Garzik64f043d2005-11-17 10:50:01 -05001092
Tejun Heoa2a7a662005-12-13 14:48:31 +09001093/**
1094 * ata_exec_internal - execute libata internal command
1095 * @ap: Port to which the command is sent
1096 * @dev: Device to which the command is sent
1097 * @tf: Taskfile registers for the command and the result
1098 * @dma_dir: Data tranfer direction of the command
1099 * @buf: Data buffer of the command
1100 * @buflen: Length of data buffer
1101 *
1102 * Executes libata internal command with timeout. @tf contains
1103 * command on entry and result on return. Timeout and error
1104 * conditions are reported via return value. No recovery action
1105 * is taken after a command times out. It's caller's duty to
1106 * clean up after timeout.
1107 *
1108 * LOCKING:
1109 * None. Should be called with kernel context, might sleep.
1110 */
1111
1112static unsigned
1113ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1114 struct ata_taskfile *tf,
1115 int dma_dir, void *buf, unsigned int buflen)
1116{
1117 u8 command = tf->command;
1118 struct ata_queued_cmd *qc;
1119 DECLARE_COMPLETION(wait);
1120 unsigned long flags;
1121 struct ata_exec_internal_arg arg;
1122
1123 spin_lock_irqsave(&ap->host_set->lock, flags);
1124
1125 qc = ata_qc_new_init(ap, dev);
1126 BUG_ON(qc == NULL);
1127
1128 qc->tf = *tf;
1129 qc->dma_dir = dma_dir;
1130 if (dma_dir != DMA_NONE) {
1131 ata_sg_init_one(qc, buf, buflen);
1132 qc->nsect = buflen / ATA_SECT_SIZE;
Jeff Garzik64f043d2005-11-17 10:50:01 -05001133 }
1134
Tejun Heoa2a7a662005-12-13 14:48:31 +09001135 arg.waiting = &wait;
1136 arg.tf = tf;
1137 qc->private_data = &arg;
1138 qc->complete_fn = ata_qc_complete_internal;
1139
1140 if (ata_qc_issue(qc))
1141 goto issue_fail;
1142
1143 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1144
1145 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1146 spin_lock_irqsave(&ap->host_set->lock, flags);
1147
1148 /* We're racing with irq here. If we lose, the
1149 * following test prevents us from completing the qc
1150 * again. If completion irq occurs after here but
1151 * before the caller cleans up, it will result in a
1152 * spurious interrupt. We can live with that.
1153 */
1154 if (arg.waiting) {
1155 qc->err_mask = AC_ERR_OTHER;
1156 ata_qc_complete(qc);
1157 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1158 ap->id, command);
1159 }
1160
1161 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1162 }
1163
1164 return arg.err_mask;
1165
1166 issue_fail:
1167 ata_qc_free(qc);
1168 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1169 return AC_ERR_OTHER;
Jeff Garzik64f043d2005-11-17 10:50:01 -05001170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001173 * ata_pio_need_iordy - check if iordy needed
1174 * @adev: ATA device
1175 *
1176 * Check if the current speed of the device requires IORDY. Used
1177 * by various controllers for chip configuration.
1178 */
1179
1180unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1181{
1182 int pio;
1183 int speed = adev->pio_mode - XFER_PIO_0;
1184
1185 if (speed < 2)
1186 return 0;
1187 if (speed > 2)
1188 return 1;
1189
1190 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1191
1192 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1193 pio = adev->id[ATA_ID_EIDE_PIO];
1194 /* Is the speed faster than the drive allows non IORDY ? */
1195 if (pio) {
1196 /* This is cycle times not frequency - watch the logic! */
1197 if (pio > 240) /* PIO2 is 240nS per cycle */
1198 return 1;
1199 return 0;
1200 }
1201 }
1202 return 0;
1203}
1204
1205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1207 * @ap: port on which device we wish to probe resides
1208 * @device: device bus address, starting at zero
1209 *
1210 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1211 * command, and read back the 512-byte device information page.
1212 * The device information page is fed to us via the standard
1213 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1214 * using standard PIO-IN paths)
1215 *
1216 * After reading the device information page, we use several
1217 * bits of information from it to initialize data structures
1218 * that will be used during the lifetime of the ata_device.
1219 * Other data from the info page is used to disqualify certain
1220 * older ATA devices we do not wish to support.
1221 *
1222 * LOCKING:
1223 * Inherited from caller. Some functions called by this function
1224 * obtain the host_set lock.
1225 */
1226
1227static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1228{
1229 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001230 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 u16 tmp;
1232 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001234 struct ata_taskfile tf;
1235 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int rc;
1237
1238 if (!ata_dev_present(dev)) {
1239 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1240 ap->id, device);
1241 return;
1242 }
1243
1244 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1245 using_edd = 0;
1246 else
1247 using_edd = 1;
1248
1249 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1250
1251 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1252 dev->class == ATA_DEV_NONE);
1253
1254 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001257 ata_tf_init(ap, &tf, device);
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001260 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 DPRINTK("do ATA identify\n");
1262 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001263 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 DPRINTK("do ATAPI identify\n");
1265 }
1266
Tejun Heoa0123702005-12-13 14:49:31 +09001267 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Tejun Heoa0123702005-12-13 14:49:31 +09001269 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1270 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Tejun Heoa0123702005-12-13 14:49:31 +09001272 if (err_mask) {
1273 if (err_mask & ~AC_ERR_DEV)
1274 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
1277 * arg! EDD works for all test cases, but seems to return
1278 * the ATA signature for some ATAPI devices. Until the
1279 * reason for this is found and fixed, we fix up the mess
1280 * here. If IDENTIFY DEVICE returns command aborted
1281 * (as ATAPI devices do), then we issue an
1282 * IDENTIFY PACKET DEVICE.
1283 *
1284 * ATA software reset (SRST, the default) does not appear
1285 * to have this problem.
1286 */
Albert Lee7c398332005-11-09 13:03:30 +08001287 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001288 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (err & ATA_ABORTED) {
1290 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto retry;
1292 }
1293 }
1294 goto err_out;
1295 }
1296
1297 swap_buf_le16(dev->id, ATA_ID_WORDS);
1298
1299 /* print device capabilities */
1300 printk(KERN_DEBUG "ata%u: dev %u cfg "
1301 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1302 ap->id, device, dev->id[49],
1303 dev->id[82], dev->id[83], dev->id[84],
1304 dev->id[85], dev->id[86], dev->id[87],
1305 dev->id[88]);
1306
1307 /*
1308 * common ATA, ATAPI feature tests
1309 */
1310
Albert Lee8bf62ece2005-05-12 15:29:42 -04001311 /* we require DMA support (bits 8 of word 49) */
1312 if (!ata_id_has_dma(dev->id)) {
1313 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 goto err_out_nosup;
1315 }
1316
1317 /* quick-n-dirty find max transfer mode; for printk only */
1318 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1319 if (!xfer_modes)
1320 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001321 if (!xfer_modes)
1322 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 ata_dump_id(dev);
1325
1326 /* ATA-specific feature tests */
1327 if (dev->class == ATA_DEV_ATA) {
1328 if (!ata_id_is_ata(dev->id)) /* sanity check */
1329 goto err_out_nosup;
1330
Albert Lee8bf62ece2005-05-12 15:29:42 -04001331 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001333 for (major_version = 14; major_version >= 1; major_version--)
1334 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 break;
1336
Albert Lee8bf62ece2005-05-12 15:29:42 -04001337 /*
1338 * The exact sequence expected by certain pre-ATA4 drives is:
1339 * SRST RESET
1340 * IDENTIFY
1341 * INITIALIZE DEVICE PARAMETERS
1342 * anything else..
1343 * Some drives were very specific about that exact sequence.
1344 */
Albert Lee59a10b12005-10-12 15:09:42 +08001345 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001346 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Albert Lee59a10b12005-10-12 15:09:42 +08001348 /* current CHS translation info (id[53-58]) might be
1349 * changed. reread the identify device info.
1350 */
1351 ata_dev_reread_id(ap, dev);
1352 }
1353
Albert Lee8bf62ece2005-05-12 15:29:42 -04001354 if (ata_id_has_lba(dev->id)) {
1355 dev->flags |= ATA_DFLAG_LBA;
1356
1357 if (ata_id_has_lba48(dev->id)) {
1358 dev->flags |= ATA_DFLAG_LBA48;
1359 dev->n_sectors = ata_id_u64(dev->id, 100);
1360 } else {
1361 dev->n_sectors = ata_id_u32(dev->id, 60);
1362 }
1363
1364 /* print device info to dmesg */
1365 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1366 ap->id, device,
1367 major_version,
1368 ata_mode_string(xfer_modes),
1369 (unsigned long long)dev->n_sectors,
1370 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1371 } else {
1372 /* CHS */
1373
1374 /* Default translation */
1375 dev->cylinders = dev->id[1];
1376 dev->heads = dev->id[3];
1377 dev->sectors = dev->id[6];
1378 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1379
1380 if (ata_id_current_chs_valid(dev->id)) {
1381 /* Current CHS translation is valid. */
1382 dev->cylinders = dev->id[54];
1383 dev->heads = dev->id[55];
1384 dev->sectors = dev->id[56];
1385
1386 dev->n_sectors = ata_id_u32(dev->id, 57);
1387 }
1388
1389 /* print device info to dmesg */
1390 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1391 ap->id, device,
1392 major_version,
1393 ata_mode_string(xfer_modes),
1394 (unsigned long long)dev->n_sectors,
1395 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
Albert Lee07f6f7d2005-11-01 19:33:20 +08001399 if (dev->id[59] & 0x100) {
1400 dev->multi_count = dev->id[59] & 0xff;
1401 DPRINTK("ata%u: dev %u multi count %u\n",
1402 ap->id, device, dev->multi_count);
1403 }
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
1408 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001409 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (ata_id_is_ata(dev->id)) /* sanity check */
1411 goto err_out_nosup;
1412
1413 rc = atapi_cdb_len(dev->id);
1414 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1415 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1416 goto err_out_nosup;
1417 }
1418 ap->cdb_len = (unsigned int) rc;
1419 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1420
Albert Lee312f7da2005-09-27 17:38:03 +08001421 if (ata_id_cdb_intr(dev->id))
1422 dev->flags |= ATA_DFLAG_CDB_INTR;
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /* print device info to dmesg */
1425 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1426 ap->id, device,
1427 ata_mode_string(xfer_modes));
1428 }
1429
1430 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1431 return;
1432
1433err_out_nosup:
1434 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1435 ap->id, device);
1436err_out:
1437 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1438 DPRINTK("EXIT, err\n");
1439}
1440
Brad Campbell6f2f3812005-05-12 15:07:47 -04001441
Jeff Garzik057ace52005-10-22 14:27:05 -04001442static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001443{
1444 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1445}
1446
1447/**
1448 * ata_dev_config - Run device specific handlers and check for
1449 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001450 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001451 * @i: Device
1452 *
1453 * LOCKING:
1454 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001455
Brad Campbell6f2f3812005-05-12 15:07:47 -04001456void ata_dev_config(struct ata_port *ap, unsigned int i)
1457{
1458 /* limit bridge transfers to udma5, 200 sectors */
1459 if (ata_dev_knobble(ap)) {
1460 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1461 ap->id, ap->device->devno);
1462 ap->udma_mask &= ATA_UDMA5;
1463 ap->host->max_sectors = ATA_MAX_SECTORS;
1464 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1465 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1466 }
1467
1468 if (ap->ops->dev_config)
1469 ap->ops->dev_config(ap, &ap->device[i]);
1470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/**
1473 * ata_bus_probe - Reset and probe ATA bus
1474 * @ap: Bus to probe
1475 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001476 * Master ATA bus probing function. Initiates a hardware-dependent
1477 * bus reset, then attempts to identify any devices found on
1478 * the bus.
1479 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001481 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 *
1483 * RETURNS:
1484 * Zero on success, non-zero on error.
1485 */
1486
1487static int ata_bus_probe(struct ata_port *ap)
1488{
1489 unsigned int i, found = 0;
1490
1491 ap->ops->phy_reset(ap);
1492 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1493 goto err_out;
1494
1495 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1496 ata_dev_identify(ap, i);
1497 if (ata_dev_present(&ap->device[i])) {
1498 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001499 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 }
1502
1503 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1504 goto err_out_disable;
1505
1506 ata_set_mode(ap);
1507 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1508 goto err_out_disable;
1509
1510 return 0;
1511
1512err_out_disable:
1513 ap->ops->port_disable(ap);
1514err_out:
1515 return -1;
1516}
1517
1518/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001519 * ata_port_probe - Mark port as enabled
1520 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001522 * Modify @ap data structure such that the system
1523 * thinks that the entire port is enabled.
1524 *
1525 * LOCKING: host_set lock, or some other form of
1526 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 */
1528
1529void ata_port_probe(struct ata_port *ap)
1530{
1531 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1532}
1533
1534/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001535 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1536 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001538 * This function issues commands to standard SATA Sxxx
1539 * PHY registers, to wake up the phy (and device), and
1540 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 *
1542 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001543 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 *
1545 */
1546void __sata_phy_reset(struct ata_port *ap)
1547{
1548 u32 sstatus;
1549 unsigned long timeout = jiffies + (HZ * 5);
1550
1551 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001552 /* issue phy wake/reset */
1553 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001554 /* Couldn't find anything in SATA I/II specs, but
1555 * AHCI-1.1 10.4.2 says at least 1 ms. */
1556 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001558 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 /* wait for phy to become ready, if necessary */
1561 do {
1562 msleep(200);
1563 sstatus = scr_read(ap, SCR_STATUS);
1564 if ((sstatus & 0xf) != 1)
1565 break;
1566 } while (time_before(jiffies, timeout));
1567
1568 /* TODO: phy layer with polling, timeouts, etc. */
Jeff Garzik656563e2005-11-20 03:36:45 -05001569 sstatus = scr_read(ap, SCR_STATUS);
1570 if (sata_dev_present(ap)) {
1571 const char *speed;
1572 u32 tmp;
1573
1574 tmp = (sstatus >> 4) & 0xf;
1575 if (tmp & (1 << 0))
1576 speed = "1.5";
1577 else if (tmp & (1 << 1))
1578 speed = "3.0";
1579 else
1580 speed = "<unknown>";
1581 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1582 ap->id, speed, sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 ata_port_probe(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001584 } else {
1585 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 ap->id, sstatus);
1587 ata_port_disable(ap);
1588 }
1589
1590 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1591 return;
1592
1593 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1594 ata_port_disable(ap);
1595 return;
1596 }
1597
1598 ap->cbl = ATA_CBL_SATA;
1599}
1600
1601/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001602 * sata_phy_reset - Reset SATA bus.
1603 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001605 * This function resets the SATA bus, and then probes
1606 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 *
1608 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001609 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 *
1611 */
1612void sata_phy_reset(struct ata_port *ap)
1613{
1614 __sata_phy_reset(ap);
1615 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1616 return;
1617 ata_bus_reset(ap);
1618}
1619
1620/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001621 * ata_port_disable - Disable port.
1622 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001624 * Modify @ap data structure such that the system
1625 * thinks that the entire port is disabled, and should
1626 * never attempt to probe or communicate with devices
1627 * on this port.
1628 *
1629 * LOCKING: host_set lock, or some other form of
1630 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
1632
1633void ata_port_disable(struct ata_port *ap)
1634{
1635 ap->device[0].class = ATA_DEV_NONE;
1636 ap->device[1].class = ATA_DEV_NONE;
1637 ap->flags |= ATA_FLAG_PORT_DISABLED;
1638}
1639
Alan Cox452503f2005-10-21 19:01:32 -04001640/*
1641 * This mode timing computation functionality is ported over from
1642 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1643 */
1644/*
1645 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1646 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1647 * for PIO 5, which is a nonstandard extension and UDMA6, which
1648 * is currently supported only by Maxtor drives.
1649 */
1650
1651static const struct ata_timing ata_timing[] = {
1652
1653 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1654 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1655 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1656 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1657
1658 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1659 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1660 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1661
1662/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1663
1664 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1665 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1666 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1667
1668 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1669 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1670 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1671
1672/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1673 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1674 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1675
1676 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1677 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1678 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1679
1680/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1681
1682 { 0xFF }
1683};
1684
1685#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1686#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1687
1688static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1689{
1690 q->setup = EZ(t->setup * 1000, T);
1691 q->act8b = EZ(t->act8b * 1000, T);
1692 q->rec8b = EZ(t->rec8b * 1000, T);
1693 q->cyc8b = EZ(t->cyc8b * 1000, T);
1694 q->active = EZ(t->active * 1000, T);
1695 q->recover = EZ(t->recover * 1000, T);
1696 q->cycle = EZ(t->cycle * 1000, T);
1697 q->udma = EZ(t->udma * 1000, UT);
1698}
1699
1700void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1701 struct ata_timing *m, unsigned int what)
1702{
1703 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1704 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1705 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1706 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1707 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1708 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1709 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1710 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1711}
1712
1713static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1714{
1715 const struct ata_timing *t;
1716
1717 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001718 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001719 return NULL;
1720 return t;
1721}
1722
1723int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1724 struct ata_timing *t, int T, int UT)
1725{
1726 const struct ata_timing *s;
1727 struct ata_timing p;
1728
1729 /*
1730 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001731 */
Alan Cox452503f2005-10-21 19:01:32 -04001732
1733 if (!(s = ata_timing_find_mode(speed)))
1734 return -EINVAL;
1735
Albert Lee75b1f2f2005-11-16 17:06:18 +08001736 memcpy(t, s, sizeof(*s));
1737
Alan Cox452503f2005-10-21 19:01:32 -04001738 /*
1739 * If the drive is an EIDE drive, it can tell us it needs extended
1740 * PIO/MW_DMA cycle timing.
1741 */
1742
1743 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1744 memset(&p, 0, sizeof(p));
1745 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1746 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1747 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1748 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1749 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1750 }
1751 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1752 }
1753
1754 /*
1755 * Convert the timing to bus clock counts.
1756 */
1757
Albert Lee75b1f2f2005-11-16 17:06:18 +08001758 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001759
1760 /*
1761 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1762 * and some other commands. We have to ensure that the DMA cycle timing is
1763 * slower/equal than the fastest PIO timing.
1764 */
1765
1766 if (speed > XFER_PIO_4) {
1767 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1768 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1769 }
1770
1771 /*
1772 * Lenghten active & recovery time so that cycle time is correct.
1773 */
1774
1775 if (t->act8b + t->rec8b < t->cyc8b) {
1776 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1777 t->rec8b = t->cyc8b - t->act8b;
1778 }
1779
1780 if (t->active + t->recover < t->cycle) {
1781 t->active += (t->cycle - (t->active + t->recover)) / 2;
1782 t->recover = t->cycle - t->active;
1783 }
1784
1785 return 0;
1786}
1787
Jeff Garzik057ace52005-10-22 14:27:05 -04001788static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 unsigned int shift;
1790 u8 base;
1791} xfer_mode_classes[] = {
1792 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1793 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1794 { ATA_SHIFT_PIO, XFER_PIO_0 },
1795};
1796
Arjan van de Ven858119e2006-01-14 13:20:43 -08001797static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 int i;
1800
1801 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1802 if (xfer_mode_classes[i].shift == shift)
1803 return xfer_mode_classes[i].base;
1804
1805 return 0xff;
1806}
1807
1808static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1809{
1810 int ofs, idx;
1811 u8 base;
1812
1813 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1814 return;
1815
1816 if (dev->xfer_shift == ATA_SHIFT_PIO)
1817 dev->flags |= ATA_DFLAG_PIO;
1818
1819 ata_dev_set_xfermode(ap, dev);
1820
1821 base = base_from_shift(dev->xfer_shift);
1822 ofs = dev->xfer_mode - base;
1823 idx = ofs + dev->xfer_shift;
1824 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1825
1826 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1827 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1828
1829 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1830 ap->id, dev->devno, xfer_mode_str[idx]);
1831}
1832
1833static int ata_host_set_pio(struct ata_port *ap)
1834{
1835 unsigned int mask;
1836 int x, i;
1837 u8 base, xfer_mode;
1838
1839 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1840 x = fgb(mask);
1841 if (x < 0) {
1842 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1843 return -1;
1844 }
1845
1846 base = base_from_shift(ATA_SHIFT_PIO);
1847 xfer_mode = base + x;
1848
1849 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1850 (int)base, (int)xfer_mode, mask, x);
1851
1852 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1853 struct ata_device *dev = &ap->device[i];
1854 if (ata_dev_present(dev)) {
1855 dev->pio_mode = xfer_mode;
1856 dev->xfer_mode = xfer_mode;
1857 dev->xfer_shift = ATA_SHIFT_PIO;
1858 if (ap->ops->set_piomode)
1859 ap->ops->set_piomode(ap, dev);
1860 }
1861 }
1862
1863 return 0;
1864}
1865
1866static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1867 unsigned int xfer_shift)
1868{
1869 int i;
1870
1871 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1872 struct ata_device *dev = &ap->device[i];
1873 if (ata_dev_present(dev)) {
1874 dev->dma_mode = xfer_mode;
1875 dev->xfer_mode = xfer_mode;
1876 dev->xfer_shift = xfer_shift;
1877 if (ap->ops->set_dmamode)
1878 ap->ops->set_dmamode(ap, dev);
1879 }
1880 }
1881}
1882
1883/**
1884 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1885 * @ap: port on which timings will be programmed
1886 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001887 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1888 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001890 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 *
1892 */
1893static void ata_set_mode(struct ata_port *ap)
1894{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001895 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 u8 xfer_mode;
1897 int rc;
1898
1899 /* step 1: always set host PIO timings */
1900 rc = ata_host_set_pio(ap);
1901 if (rc)
1902 goto err_out;
1903
1904 /* step 2: choose the best data xfer mode */
1905 xfer_mode = xfer_shift = 0;
1906 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1907 if (rc)
1908 goto err_out;
1909
1910 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1911 if (xfer_shift != ATA_SHIFT_PIO)
1912 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1913
1914 /* step 4: update devices' xfer mode */
1915 ata_dev_set_mode(ap, &ap->device[0]);
1916 ata_dev_set_mode(ap, &ap->device[1]);
1917
1918 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1919 return;
1920
1921 if (ap->ops->post_set_mode)
1922 ap->ops->post_set_mode(ap);
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return;
1925
1926err_out:
1927 ata_port_disable(ap);
1928}
1929
1930/**
1931 * ata_busy_sleep - sleep until BSY clears, or timeout
1932 * @ap: port containing status register to be polled
1933 * @tmout_pat: impatience timeout
1934 * @tmout: overall timeout
1935 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001936 * Sleep until ATA Status register bit BSY clears,
1937 * or a timeout occurs.
1938 *
1939 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 *
1941 */
1942
1943static unsigned int ata_busy_sleep (struct ata_port *ap,
1944 unsigned long tmout_pat,
1945 unsigned long tmout)
1946{
1947 unsigned long timer_start, timeout;
1948 u8 status;
1949
1950 status = ata_busy_wait(ap, ATA_BUSY, 300);
1951 timer_start = jiffies;
1952 timeout = timer_start + tmout_pat;
1953 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1954 msleep(50);
1955 status = ata_busy_wait(ap, ATA_BUSY, 3);
1956 }
1957
1958 if (status & ATA_BUSY)
1959 printk(KERN_WARNING "ata%u is slow to respond, "
1960 "please be patient\n", ap->id);
1961
1962 timeout = timer_start + tmout;
1963 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1964 msleep(50);
1965 status = ata_chk_status(ap);
1966 }
1967
1968 if (status & ATA_BUSY) {
1969 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1970 ap->id, tmout / HZ);
1971 return 1;
1972 }
1973
1974 return 0;
1975}
1976
1977static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1978{
1979 struct ata_ioports *ioaddr = &ap->ioaddr;
1980 unsigned int dev0 = devmask & (1 << 0);
1981 unsigned int dev1 = devmask & (1 << 1);
1982 unsigned long timeout;
1983
1984 /* if device 0 was found in ata_devchk, wait for its
1985 * BSY bit to clear
1986 */
1987 if (dev0)
1988 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1989
1990 /* if device 1 was found in ata_devchk, wait for
1991 * register access, then wait for BSY to clear
1992 */
1993 timeout = jiffies + ATA_TMOUT_BOOT;
1994 while (dev1) {
1995 u8 nsect, lbal;
1996
1997 ap->ops->dev_select(ap, 1);
1998 if (ap->flags & ATA_FLAG_MMIO) {
1999 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2000 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2001 } else {
2002 nsect = inb(ioaddr->nsect_addr);
2003 lbal = inb(ioaddr->lbal_addr);
2004 }
2005 if ((nsect == 1) && (lbal == 1))
2006 break;
2007 if (time_after(jiffies, timeout)) {
2008 dev1 = 0;
2009 break;
2010 }
2011 msleep(50); /* give drive a breather */
2012 }
2013 if (dev1)
2014 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2015
2016 /* is all this really necessary? */
2017 ap->ops->dev_select(ap, 0);
2018 if (dev1)
2019 ap->ops->dev_select(ap, 1);
2020 if (dev0)
2021 ap->ops->dev_select(ap, 0);
2022}
2023
2024/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002025 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2026 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002028 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2029 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 *
2031 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002032 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05002033 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 *
2035 */
2036
2037static unsigned int ata_bus_edd(struct ata_port *ap)
2038{
2039 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05002040 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /* set up execute-device-diag (bus reset) taskfile */
2043 /* also, take interrupts to a known state (disabled) */
2044 DPRINTK("execute-device-diag\n");
2045 ata_tf_init(ap, &tf, 0);
2046 tf.ctl |= ATA_NIEN;
2047 tf.command = ATA_CMD_EDD;
2048 tf.protocol = ATA_PROT_NODATA;
2049
2050 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002051 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002053 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 /* spec says at least 2ms. but who knows with those
2056 * crazy ATAPI devices...
2057 */
2058 msleep(150);
2059
2060 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2061}
2062
2063static unsigned int ata_bus_softreset(struct ata_port *ap,
2064 unsigned int devmask)
2065{
2066 struct ata_ioports *ioaddr = &ap->ioaddr;
2067
2068 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2069
2070 /* software reset. causes dev0 to be selected */
2071 if (ap->flags & ATA_FLAG_MMIO) {
2072 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2073 udelay(20); /* FIXME: flush */
2074 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2075 udelay(20); /* FIXME: flush */
2076 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2077 } else {
2078 outb(ap->ctl, ioaddr->ctl_addr);
2079 udelay(10);
2080 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2081 udelay(10);
2082 outb(ap->ctl, ioaddr->ctl_addr);
2083 }
2084
2085 /* spec mandates ">= 2ms" before checking status.
2086 * We wait 150ms, because that was the magic delay used for
2087 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2088 * between when the ATA command register is written, and then
2089 * status is checked. Because waiting for "a while" before
2090 * checking status is fine, post SRST, we perform this magic
2091 * delay here as well.
2092 */
2093 msleep(150);
2094
2095 ata_bus_post_reset(ap, devmask);
2096
2097 return 0;
2098}
2099
2100/**
2101 * ata_bus_reset - reset host port and associated ATA channel
2102 * @ap: port to reset
2103 *
2104 * This is typically the first time we actually start issuing
2105 * commands to the ATA channel. We wait for BSY to clear, then
2106 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2107 * result. Determine what devices, if any, are on the channel
2108 * by looking at the device 0/1 error register. Look at the signature
2109 * stored in each device's taskfile registers, to determine if
2110 * the device is ATA or ATAPI.
2111 *
2112 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002113 * PCI/etc. bus probe sem.
2114 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 *
2116 * SIDE EFFECTS:
2117 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2118 */
2119
2120void ata_bus_reset(struct ata_port *ap)
2121{
2122 struct ata_ioports *ioaddr = &ap->ioaddr;
2123 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2124 u8 err;
2125 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2126
2127 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2128
2129 /* determine if device 0/1 are present */
2130 if (ap->flags & ATA_FLAG_SATA_RESET)
2131 dev0 = 1;
2132 else {
2133 dev0 = ata_devchk(ap, 0);
2134 if (slave_possible)
2135 dev1 = ata_devchk(ap, 1);
2136 }
2137
2138 if (dev0)
2139 devmask |= (1 << 0);
2140 if (dev1)
2141 devmask |= (1 << 1);
2142
2143 /* select device 0 again */
2144 ap->ops->dev_select(ap, 0);
2145
2146 /* issue bus reset */
2147 if (ap->flags & ATA_FLAG_SRST)
2148 rc = ata_bus_softreset(ap, devmask);
2149 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2150 /* set up device control */
2151 if (ap->flags & ATA_FLAG_MMIO)
2152 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2153 else
2154 outb(ap->ctl, ioaddr->ctl_addr);
2155 rc = ata_bus_edd(ap);
2156 }
2157
2158 if (rc)
2159 goto err_out;
2160
2161 /*
2162 * determine by signature whether we have ATA or ATAPI devices
2163 */
2164 err = ata_dev_try_classify(ap, 0);
2165 if ((slave_possible) && (err != 0x81))
2166 ata_dev_try_classify(ap, 1);
2167
2168 /* re-enable interrupts */
2169 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2170 ata_irq_on(ap);
2171
2172 /* is double-select really necessary? */
2173 if (ap->device[1].class != ATA_DEV_NONE)
2174 ap->ops->dev_select(ap, 1);
2175 if (ap->device[0].class != ATA_DEV_NONE)
2176 ap->ops->dev_select(ap, 0);
2177
2178 /* if no devices were detected, disable this port */
2179 if ((ap->device[0].class == ATA_DEV_NONE) &&
2180 (ap->device[1].class == ATA_DEV_NONE))
2181 goto err_out;
2182
2183 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2184 /* set up device control for ATA_FLAG_SATA_RESET */
2185 if (ap->flags & ATA_FLAG_MMIO)
2186 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2187 else
2188 outb(ap->ctl, ioaddr->ctl_addr);
2189 }
2190
2191 DPRINTK("EXIT\n");
2192 return;
2193
2194err_out:
2195 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2196 ap->ops->port_disable(ap);
2197
2198 DPRINTK("EXIT\n");
2199}
2200
Jeff Garzik057ace52005-10-22 14:27:05 -04002201static void ata_pr_blacklisted(const struct ata_port *ap,
2202 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2205 ap->id, dev->devno);
2206}
2207
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002208static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 "WDC AC11000H",
2210 "WDC AC22100H",
2211 "WDC AC32500H",
2212 "WDC AC33100H",
2213 "WDC AC31600H",
2214 "WDC AC32100H",
2215 "WDC AC23200L",
2216 "Compaq CRD-8241B",
2217 "CRD-8400B",
2218 "CRD-8480B",
2219 "CRD-8482B",
2220 "CRD-84",
2221 "SanDisk SDP3B",
2222 "SanDisk SDP3B-64",
2223 "SANYO CD-ROM CRD",
2224 "HITACHI CDR-8",
2225 "HITACHI CDR-8335",
2226 "HITACHI CDR-8435",
2227 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002228 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 "CD-532E-A",
2230 "E-IDE CD-ROM CR-840",
2231 "CD-ROM Drive/F5A",
2232 "WPI CDD-820",
2233 "SAMSUNG CD-ROM SC-148C",
2234 "SAMSUNG CD-ROM SC",
2235 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2237 "_NEC DV5800A",
2238};
2239
Jeff Garzik057ace52005-10-22 14:27:05 -04002240static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
2242 unsigned char model_num[40];
2243 char *s;
2244 unsigned int len;
2245 int i;
2246
2247 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2248 sizeof(model_num));
2249 s = &model_num[0];
2250 len = strnlen(s, sizeof(model_num));
2251
2252 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2253 while ((len > 0) && (s[len - 1] == ' ')) {
2254 len--;
2255 s[len] = 0;
2256 }
2257
2258 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2259 if (!strncmp(ata_dma_blacklist[i], s, len))
2260 return 1;
2261
2262 return 0;
2263}
2264
Jeff Garzik057ace52005-10-22 14:27:05 -04002265static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
Jeff Garzik057ace52005-10-22 14:27:05 -04002267 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 unsigned int mask;
2269
2270 master = &ap->device[0];
2271 slave = &ap->device[1];
2272
2273 assert (ata_dev_present(master) || ata_dev_present(slave));
2274
2275 if (shift == ATA_SHIFT_UDMA) {
2276 mask = ap->udma_mask;
2277 if (ata_dev_present(master)) {
2278 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002279 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 mask = 0;
2281 ata_pr_blacklisted(ap, master);
2282 }
2283 }
2284 if (ata_dev_present(slave)) {
2285 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002286 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 mask = 0;
2288 ata_pr_blacklisted(ap, slave);
2289 }
2290 }
2291 }
2292 else if (shift == ATA_SHIFT_MWDMA) {
2293 mask = ap->mwdma_mask;
2294 if (ata_dev_present(master)) {
2295 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002296 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 mask = 0;
2298 ata_pr_blacklisted(ap, master);
2299 }
2300 }
2301 if (ata_dev_present(slave)) {
2302 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002303 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 mask = 0;
2305 ata_pr_blacklisted(ap, slave);
2306 }
2307 }
2308 }
2309 else if (shift == ATA_SHIFT_PIO) {
2310 mask = ap->pio_mask;
2311 if (ata_dev_present(master)) {
2312 /* spec doesn't return explicit support for
2313 * PIO0-2, so we fake it
2314 */
2315 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2316 tmp_mode <<= 3;
2317 tmp_mode |= 0x7;
2318 mask &= tmp_mode;
2319 }
2320 if (ata_dev_present(slave)) {
2321 /* spec doesn't return explicit support for
2322 * PIO0-2, so we fake it
2323 */
2324 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2325 tmp_mode <<= 3;
2326 tmp_mode |= 0x7;
2327 mask &= tmp_mode;
2328 }
2329 }
2330 else {
2331 mask = 0xffffffff; /* shut up compiler warning */
2332 BUG();
2333 }
2334
2335 return mask;
2336}
2337
2338/* find greatest bit */
2339static int fgb(u32 bitmap)
2340{
2341 unsigned int i;
2342 int x = -1;
2343
2344 for (i = 0; i < 32; i++)
2345 if (bitmap & (1 << i))
2346 x = i;
2347
2348 return x;
2349}
2350
2351/**
2352 * ata_choose_xfer_mode - attempt to find best transfer mode
2353 * @ap: Port for which an xfer mode will be selected
2354 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2355 * @xfer_shift_out: (output) bit shift that selects this mode
2356 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002357 * Based on host and device capabilities, determine the
2358 * maximum transfer mode that is amenable to all.
2359 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002361 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 *
2363 * RETURNS:
2364 * Zero on success, negative on error.
2365 */
2366
Jeff Garzik057ace52005-10-22 14:27:05 -04002367static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 u8 *xfer_mode_out,
2369 unsigned int *xfer_shift_out)
2370{
2371 unsigned int mask, shift;
2372 int x, i;
2373
2374 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2375 shift = xfer_mode_classes[i].shift;
2376 mask = ata_get_mode_mask(ap, shift);
2377
2378 x = fgb(mask);
2379 if (x >= 0) {
2380 *xfer_mode_out = xfer_mode_classes[i].base + x;
2381 *xfer_shift_out = shift;
2382 return 0;
2383 }
2384 }
2385
2386 return -1;
2387}
2388
2389/**
2390 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2391 * @ap: Port associated with device @dev
2392 * @dev: Device to which command will be sent
2393 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002394 * Issue SET FEATURES - XFER MODE command to device @dev
2395 * on port @ap.
2396 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002398 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 */
2400
2401static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2402{
Tejun Heoa0123702005-12-13 14:49:31 +09002403 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 /* set up set-features taskfile */
2406 DPRINTK("set features - xfer mode\n");
2407
Tejun Heoa0123702005-12-13 14:49:31 +09002408 ata_tf_init(ap, &tf, dev->devno);
2409 tf.command = ATA_CMD_SET_FEATURES;
2410 tf.feature = SETFEATURES_XFER;
2411 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2412 tf.protocol = ATA_PROT_NODATA;
2413 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Tejun Heoa0123702005-12-13 14:49:31 +09002415 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2416 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2417 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 DPRINTK("EXIT\n");
2422}
2423
2424/**
Albert Lee59a10b12005-10-12 15:09:42 +08002425 * ata_dev_reread_id - Reread the device identify device info
2426 * @ap: port where the device is
2427 * @dev: device to reread the identify device info
2428 *
2429 * LOCKING:
2430 */
2431
2432static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2433{
Tejun Heoa0123702005-12-13 14:49:31 +09002434 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002435
Tejun Heoa0123702005-12-13 14:49:31 +09002436 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002437
2438 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002439 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002440 DPRINTK("do ATA identify\n");
2441 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002442 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002443 DPRINTK("do ATAPI identify\n");
2444 }
2445
Tejun Heoa0123702005-12-13 14:49:31 +09002446 tf.flags |= ATA_TFLAG_DEVICE;
2447 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002448
Tejun Heoa0123702005-12-13 14:49:31 +09002449 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2450 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002451 goto err_out;
2452
Albert Lee59a10b12005-10-12 15:09:42 +08002453 swap_buf_le16(dev->id, ATA_ID_WORDS);
2454
2455 ata_dump_id(dev);
2456
2457 DPRINTK("EXIT\n");
2458
2459 return;
2460err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002461 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002462 ata_port_disable(ap);
2463}
2464
2465/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002466 * ata_dev_init_params - Issue INIT DEV PARAMS command
2467 * @ap: Port associated with device @dev
2468 * @dev: Device to which command will be sent
2469 *
2470 * LOCKING:
2471 */
2472
2473static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2474{
Tejun Heoa0123702005-12-13 14:49:31 +09002475 struct ata_taskfile tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002476 u16 sectors = dev->id[6];
2477 u16 heads = dev->id[3];
2478
2479 /* Number of sectors per track 1-255. Number of heads 1-16 */
2480 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2481 return;
2482
2483 /* set up init dev params taskfile */
2484 DPRINTK("init dev params \n");
2485
Tejun Heoa0123702005-12-13 14:49:31 +09002486 ata_tf_init(ap, &tf, dev->devno);
2487 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2488 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2489 tf.protocol = ATA_PROT_NODATA;
2490 tf.nsect = sectors;
2491 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04002492
Tejun Heoa0123702005-12-13 14:49:31 +09002493 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2494 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2495 ap->id);
Albert Lee8bf62ece2005-05-12 15:29:42 -04002496 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002497 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04002498
2499 DPRINTK("EXIT\n");
2500}
2501
2502/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002503 * ata_sg_clean - Unmap DMA memory associated with command
2504 * @qc: Command containing DMA memory to be released
2505 *
2506 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 *
2508 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002509 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 */
2511
2512static void ata_sg_clean(struct ata_queued_cmd *qc)
2513{
2514 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002515 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002517 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2520 assert(sg != NULL);
2521
2522 if (qc->flags & ATA_QCFLAG_SINGLE)
2523 assert(qc->n_elem == 1);
2524
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002525 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002527 /* if we padded the buffer out to 32-bit bound, and data
2528 * xfer direction is from-device, we must copy from the
2529 * pad buffer back into the supplied buffer
2530 */
2531 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2532 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2533
2534 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002535 if (qc->n_elem)
2536 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002537 /* restore last sg */
2538 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2539 if (pad_buf) {
2540 struct scatterlist *psg = &qc->pad_sgent;
2541 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2542 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002543 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002544 }
2545 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002546 if (sg_dma_len(&sg[0]) > 0)
2547 dma_unmap_single(ap->host_set->dev,
2548 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2549 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002550 /* restore sg */
2551 sg->length += qc->pad_len;
2552 if (pad_buf)
2553 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2554 pad_buf, qc->pad_len);
2555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002558 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
2561/**
2562 * ata_fill_sg - Fill PCI IDE PRD table
2563 * @qc: Metadata associated with taskfile to be transferred
2564 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002565 * Fill PCI IDE PRD (scatter-gather) table with segments
2566 * associated with the current disk command.
2567 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002569 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 *
2571 */
2572static void ata_fill_sg(struct ata_queued_cmd *qc)
2573{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002575 struct scatterlist *sg;
2576 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002578 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 assert(qc->n_elem > 0);
2580
2581 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002582 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 u32 addr, offset;
2584 u32 sg_len, len;
2585
2586 /* determine if physical DMA addr spans 64K boundary.
2587 * Note h/w doesn't support 64-bit, so we unconditionally
2588 * truncate dma_addr_t to u32.
2589 */
2590 addr = (u32) sg_dma_address(sg);
2591 sg_len = sg_dma_len(sg);
2592
2593 while (sg_len) {
2594 offset = addr & 0xffff;
2595 len = sg_len;
2596 if ((offset + sg_len) > 0x10000)
2597 len = 0x10000 - offset;
2598
2599 ap->prd[idx].addr = cpu_to_le32(addr);
2600 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2601 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2602
2603 idx++;
2604 sg_len -= len;
2605 addr += len;
2606 }
2607 }
2608
2609 if (idx)
2610 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2611}
2612/**
2613 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2614 * @qc: Metadata associated with taskfile to check
2615 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002616 * Allow low-level driver to filter ATA PACKET commands, returning
2617 * a status indicating whether or not it is OK to use DMA for the
2618 * supplied PACKET command.
2619 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002621 * spin_lock_irqsave(host_set lock)
2622 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 * RETURNS: 0 when ATAPI DMA can be used
2624 * nonzero otherwise
2625 */
2626int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2627{
2628 struct ata_port *ap = qc->ap;
2629 int rc = 0; /* Assume ATAPI DMA is OK by default */
2630
2631 if (ap->ops->check_atapi_dma)
2632 rc = ap->ops->check_atapi_dma(qc);
2633
2634 return rc;
2635}
2636/**
2637 * ata_qc_prep - Prepare taskfile for submission
2638 * @qc: Metadata associated with taskfile to be prepared
2639 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002640 * Prepare ATA taskfile for submission.
2641 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 * LOCKING:
2643 * spin_lock_irqsave(host_set lock)
2644 */
2645void ata_qc_prep(struct ata_queued_cmd *qc)
2646{
2647 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2648 return;
2649
2650 ata_fill_sg(qc);
2651}
2652
Jeff Garzik0cba6322005-05-30 19:49:12 -04002653/**
2654 * ata_sg_init_one - Associate command with memory buffer
2655 * @qc: Command to be associated
2656 * @buf: Memory buffer
2657 * @buflen: Length of memory buffer, in bytes.
2658 *
2659 * Initialize the data-related elements of queued_cmd @qc
2660 * to point to a single memory buffer, @buf of byte length @buflen.
2661 *
2662 * LOCKING:
2663 * spin_lock_irqsave(host_set lock)
2664 */
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2667{
2668 struct scatterlist *sg;
2669
2670 qc->flags |= ATA_QCFLAG_SINGLE;
2671
2672 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002673 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002675 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 qc->buf_virt = buf;
2677
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002678 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002679 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Jeff Garzik0cba6322005-05-30 19:49:12 -04002682/**
2683 * ata_sg_init - Associate command with scatter-gather table.
2684 * @qc: Command to be associated
2685 * @sg: Scatter-gather table.
2686 * @n_elem: Number of elements in s/g table.
2687 *
2688 * Initialize the data-related elements of queued_cmd @qc
2689 * to point to a scatter-gather table @sg, containing @n_elem
2690 * elements.
2691 *
2692 * LOCKING:
2693 * spin_lock_irqsave(host_set lock)
2694 */
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2697 unsigned int n_elem)
2698{
2699 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002700 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002702 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
2705/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002706 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2707 * @qc: Command with memory buffer to be mapped.
2708 *
2709 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 *
2711 * LOCKING:
2712 * spin_lock_irqsave(host_set lock)
2713 *
2714 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002715 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 */
2717
2718static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2719{
2720 struct ata_port *ap = qc->ap;
2721 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002722 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 dma_addr_t dma_address;
2724
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002725 /* we must lengthen transfers to end on a 32-bit boundary */
2726 qc->pad_len = sg->length & 3;
2727 if (qc->pad_len) {
2728 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2729 struct scatterlist *psg = &qc->pad_sgent;
2730
2731 assert(qc->dev->class == ATA_DEV_ATAPI);
2732
2733 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2734
2735 if (qc->tf.flags & ATA_TFLAG_WRITE)
2736 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2737 qc->pad_len);
2738
2739 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2740 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2741 /* trim sg */
2742 sg->length -= qc->pad_len;
2743
2744 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2745 sg->length, qc->pad_len);
2746 }
2747
Jeff Garzike1410f22005-11-14 14:06:26 -05002748 if (!sg->length) {
2749 sg_dma_address(sg) = 0;
2750 goto skip_map;
2751 }
2752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002754 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002755 if (dma_mapping_error(dma_address)) {
2756 /* restore sg */
2757 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002762skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002763 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2766 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2767
2768 return 0;
2769}
2770
2771/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002772 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2773 * @qc: Command with scatter-gather table to be mapped.
2774 *
2775 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 *
2777 * LOCKING:
2778 * spin_lock_irqsave(host_set lock)
2779 *
2780 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002781 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 *
2783 */
2784
2785static int ata_sg_setup(struct ata_queued_cmd *qc)
2786{
2787 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002788 struct scatterlist *sg = qc->__sg;
2789 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002790 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
2792 VPRINTK("ENTER, ata%u\n", ap->id);
2793 assert(qc->flags & ATA_QCFLAG_SG);
2794
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002795 /* we must lengthen transfers to end on a 32-bit boundary */
2796 qc->pad_len = lsg->length & 3;
2797 if (qc->pad_len) {
2798 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2799 struct scatterlist *psg = &qc->pad_sgent;
2800 unsigned int offset;
2801
2802 assert(qc->dev->class == ATA_DEV_ATAPI);
2803
2804 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2805
2806 /*
2807 * psg->page/offset are used to copy to-be-written
2808 * data in this function or read data in ata_sg_clean.
2809 */
2810 offset = lsg->offset + lsg->length - qc->pad_len;
2811 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2812 psg->offset = offset_in_page(offset);
2813
2814 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2815 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2816 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002817 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002818 }
2819
2820 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2821 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2822 /* trim last sg */
2823 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002824 if (lsg->length == 0)
2825 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002826
2827 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2828 qc->n_elem - 1, lsg->length, qc->pad_len);
2829 }
2830
Jeff Garzike1410f22005-11-14 14:06:26 -05002831 pre_n_elem = qc->n_elem;
2832 if (trim_sg && pre_n_elem)
2833 pre_n_elem--;
2834
2835 if (!pre_n_elem) {
2836 n_elem = 0;
2837 goto skip_map;
2838 }
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002841 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002842 if (n_elem < 1) {
2843 /* restore last sg */
2844 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 DPRINTK("%d sg elements mapped\n", n_elem);
2849
Jeff Garzike1410f22005-11-14 14:06:26 -05002850skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 qc->n_elem = n_elem;
2852
2853 return 0;
2854}
2855
2856/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002857 * ata_poll_qc_complete - turn irq back on and finish qc
2858 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002859 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002860 *
2861 * LOCKING:
2862 * None. (grabs host lock)
2863 */
2864
Albert Leea22e2eb2005-12-05 15:38:02 +08002865void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002866{
2867 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002868 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002869
Jeff Garzikb8f61532005-08-25 22:01:20 -04002870 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002871 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002872 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002873 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002874}
2875
2876/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002878 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 *
2880 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002881 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 *
2883 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002884 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 */
2886
2887static unsigned long ata_pio_poll(struct ata_port *ap)
2888{
Albert Leec14b8332005-12-05 15:36:08 +08002889 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002891 unsigned int poll_state = HSM_ST_UNKNOWN;
2892 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Albert Leec14b8332005-12-05 15:36:08 +08002894 qc = ata_qc_from_tag(ap, ap->active_tag);
2895 assert(qc != NULL);
2896
Albert Lee14be71f2005-09-27 17:36:35 +08002897 switch (ap->hsm_task_state) {
2898 case HSM_ST:
2899 case HSM_ST_POLL:
2900 poll_state = HSM_ST_POLL;
2901 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002903 case HSM_ST_LAST:
2904 case HSM_ST_LAST_POLL:
2905 poll_state = HSM_ST_LAST_POLL;
2906 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 break;
2908 default:
2909 BUG();
2910 break;
2911 }
2912
2913 status = ata_chk_status(ap);
2914 if (status & ATA_BUSY) {
2915 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee1c848982005-12-05 15:40:15 +08002916 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee7c398332005-11-09 13:03:30 +08002917 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return 0;
2919 }
Albert Lee14be71f2005-09-27 17:36:35 +08002920 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return ATA_SHORT_PAUSE;
2922 }
2923
Albert Lee14be71f2005-09-27 17:36:35 +08002924 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return 0;
2926}
2927
2928/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002929 * ata_pio_complete - check if drive is busy or idle
2930 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 *
2932 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002933 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002934 *
2935 * RETURNS:
Albert Leefbcdd802005-11-01 19:30:05 +08002936 * Zero if qc completed.
2937 * Non-zero if has next.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 */
2939
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002940static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
2942 struct ata_queued_cmd *qc;
2943 u8 drv_stat;
2944
2945 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002946 * This is purely heuristic. This is a fast path. Sometimes when
2947 * we enter, BSY will be cleared in a chk-status or two. If not,
2948 * the drive is probably seeking or something. Snooze for a couple
2949 * msecs, then chk-status again. If still busy, fall back to
Albert Lee07f6f7d2005-11-01 19:33:20 +08002950 * HSM_ST_LAST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 */
Albert Leefe79e682005-12-06 11:34:59 +08002952 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2953 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002955 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2956 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002957 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Albert Leefbcdd802005-11-01 19:30:05 +08002959 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
2961 }
2962
Albert Leec14b8332005-12-05 15:36:08 +08002963 qc = ata_qc_from_tag(ap, ap->active_tag);
2964 assert(qc != NULL);
2965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 drv_stat = ata_wait_idle(ap);
2967 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002968 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002969 ap->hsm_task_state = HSM_ST_ERR;
Albert Leefbcdd802005-11-01 19:30:05 +08002970 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
2972
Albert Lee14be71f2005-09-27 17:36:35 +08002973 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Albert Leea22e2eb2005-12-05 15:38:02 +08002975 assert(qc->err_mask == 0);
2976 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002977
2978 /* another command may start at this point */
2979
Albert Leefbcdd802005-11-01 19:30:05 +08002980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
Edward Falk0baab862005-06-02 18:17:13 -04002983
2984/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002985 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002986 * @buf: Buffer to swap
2987 * @buf_words: Number of 16-bit words in buffer.
2988 *
2989 * Swap halves of 16-bit words if needed to convert from
2990 * little-endian byte order to native cpu byte order, or
2991 * vice-versa.
2992 *
2993 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002994 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002995 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996void swap_buf_le16(u16 *buf, unsigned int buf_words)
2997{
2998#ifdef __BIG_ENDIAN
2999 unsigned int i;
3000
3001 for (i = 0; i < buf_words; i++)
3002 buf[i] = le16_to_cpu(buf[i]);
3003#endif /* __BIG_ENDIAN */
3004}
3005
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003006/**
3007 * ata_mmio_data_xfer - Transfer data by MMIO
3008 * @ap: port to read/write
3009 * @buf: data buffer
3010 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003011 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003012 *
3013 * Transfer data from/to the device data register by MMIO.
3014 *
3015 * LOCKING:
3016 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003017 */
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3020 unsigned int buflen, int write_data)
3021{
3022 unsigned int i;
3023 unsigned int words = buflen >> 1;
3024 u16 *buf16 = (u16 *) buf;
3025 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3026
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003027 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 if (write_data) {
3029 for (i = 0; i < words; i++)
3030 writew(le16_to_cpu(buf16[i]), mmio);
3031 } else {
3032 for (i = 0; i < words; i++)
3033 buf16[i] = cpu_to_le16(readw(mmio));
3034 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003035
3036 /* Transfer trailing 1 byte, if any. */
3037 if (unlikely(buflen & 0x01)) {
3038 u16 align_buf[1] = { 0 };
3039 unsigned char *trailing_buf = buf + buflen - 1;
3040
3041 if (write_data) {
3042 memcpy(align_buf, trailing_buf, 1);
3043 writew(le16_to_cpu(align_buf[0]), mmio);
3044 } else {
3045 align_buf[0] = cpu_to_le16(readw(mmio));
3046 memcpy(trailing_buf, align_buf, 1);
3047 }
3048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003051/**
3052 * ata_pio_data_xfer - Transfer data by PIO
3053 * @ap: port to read/write
3054 * @buf: data buffer
3055 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003056 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003057 *
3058 * Transfer data from/to the device data register by PIO.
3059 *
3060 * LOCKING:
3061 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003062 */
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3065 unsigned int buflen, int write_data)
3066{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003067 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003069 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003071 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003073 insw(ap->ioaddr.data_addr, buf, words);
3074
3075 /* Transfer trailing 1 byte, if any. */
3076 if (unlikely(buflen & 0x01)) {
3077 u16 align_buf[1] = { 0 };
3078 unsigned char *trailing_buf = buf + buflen - 1;
3079
3080 if (write_data) {
3081 memcpy(align_buf, trailing_buf, 1);
3082 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3083 } else {
3084 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3085 memcpy(trailing_buf, align_buf, 1);
3086 }
3087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003090/**
3091 * ata_data_xfer - Transfer data from/to the data register.
3092 * @ap: port to read/write
3093 * @buf: data buffer
3094 * @buflen: buffer length
3095 * @do_write: read/write
3096 *
3097 * Transfer data from/to the device data register.
3098 *
3099 * LOCKING:
3100 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003101 */
3102
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3104 unsigned int buflen, int do_write)
3105{
3106 if (ap->flags & ATA_FLAG_MMIO)
3107 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3108 else
3109 ata_pio_data_xfer(ap, buf, buflen, do_write);
3110}
3111
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003112/**
3113 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3114 * @qc: Command on going
3115 *
3116 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3117 *
3118 * LOCKING:
3119 * Inherited from caller.
3120 */
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122static void ata_pio_sector(struct ata_queued_cmd *qc)
3123{
3124 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003125 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 struct ata_port *ap = qc->ap;
3127 struct page *page;
3128 unsigned int offset;
3129 unsigned char *buf;
3130
3131 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003132 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 page = sg[qc->cursg].page;
3135 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3136
3137 /* get the current page and offset */
3138 page = nth_page(page, (offset >> PAGE_SHIFT));
3139 offset %= PAGE_SIZE;
3140
Albert Lee7282aa42005-10-09 09:46:07 -04003141 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3142
Albert Lee91b8b312005-10-09 09:48:44 -04003143 if (PageHighMem(page)) {
3144 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003145
Albert Lee91b8b312005-10-09 09:48:44 -04003146 local_irq_save(flags);
3147 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003148
Albert Lee91b8b312005-10-09 09:48:44 -04003149 /* do the actual data transfer */
3150 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3151
3152 kunmap_atomic(buf, KM_IRQ0);
3153 local_irq_restore(flags);
3154 } else {
3155 buf = page_address(page);
3156 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3157 }
Albert Lee7282aa42005-10-09 09:46:07 -04003158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 qc->cursect++;
3160 qc->cursg_ofs++;
3161
Albert Lee32529e02005-05-26 03:49:42 -04003162 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 qc->cursg++;
3164 qc->cursg_ofs = 0;
3165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003168/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003169 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3170 * @qc: Command on going
3171 *
3172 * Transfer one or many ATA_SECT_SIZE of data from/to the
3173 * ATA device for the DRQ request.
3174 *
3175 * LOCKING:
3176 * Inherited from caller.
3177 */
3178
3179static void ata_pio_sectors(struct ata_queued_cmd *qc)
3180{
3181 if (is_multi_taskfile(&qc->tf)) {
3182 /* READ/WRITE MULTIPLE */
3183 unsigned int nsect;
3184
3185 assert(qc->dev->multi_count);
3186
3187 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3188 while (nsect--)
3189 ata_pio_sector(qc);
3190 } else
3191 ata_pio_sector(qc);
3192}
3193
3194/**
Albert Leec71c1852005-10-04 06:03:45 -04003195 * atapi_send_cdb - Write CDB bytes to hardware
3196 * @ap: Port to which ATAPI device is attached.
3197 * @qc: Taskfile currently active
3198 *
3199 * When device has indicated its readiness to accept
3200 * a CDB, this function is called. Send the CDB.
3201 *
3202 * LOCKING:
3203 * caller.
3204 */
3205
3206static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3207{
3208 /* send SCSI cdb */
3209 DPRINTK("send cdb\n");
3210 assert(ap->cdb_len >= 12);
3211
3212 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3213 ata_altstatus(ap); /* flush */
3214
3215 switch (qc->tf.protocol) {
3216 case ATA_PROT_ATAPI:
3217 ap->hsm_task_state = HSM_ST;
3218 break;
3219 case ATA_PROT_ATAPI_NODATA:
3220 ap->hsm_task_state = HSM_ST_LAST;
3221 break;
3222 case ATA_PROT_ATAPI_DMA:
3223 ap->hsm_task_state = HSM_ST_LAST;
3224 /* initiate bmdma */
3225 ap->ops->bmdma_start(qc);
3226 break;
3227 }
3228}
3229
3230/**
Albert Leee27486d2005-11-01 19:24:49 +08003231 * ata_pio_first_block - Write first data block to hardware
3232 * @ap: Port to which ATA/ATAPI device is attached.
Albert Leec71c1852005-10-04 06:03:45 -04003233 *
3234 * When device has indicated its readiness to accept
3235 * the data, this function sends out the CDB or
3236 * the first data block by PIO.
3237 * After this,
3238 * - If polling, ata_pio_task() handles the rest.
3239 * - Otherwise, interrupt handler takes over.
3240 *
3241 * LOCKING:
3242 * Kernel thread context (may sleep)
Albert Leefbcdd802005-11-01 19:30:05 +08003243 *
3244 * RETURNS:
3245 * Zero if irq handler takes over
3246 * Non-zero if has next (polling).
Albert Leec71c1852005-10-04 06:03:45 -04003247 */
3248
Albert Leefbcdd802005-11-01 19:30:05 +08003249static int ata_pio_first_block(struct ata_port *ap)
Albert Leec71c1852005-10-04 06:03:45 -04003250{
Albert Leec71c1852005-10-04 06:03:45 -04003251 struct ata_queued_cmd *qc;
3252 u8 status;
3253 unsigned long flags;
Albert Leefbcdd802005-11-01 19:30:05 +08003254 int has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003255
3256 qc = ata_qc_from_tag(ap, ap->active_tag);
3257 assert(qc != NULL);
3258 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3259
Albert Leefbcdd802005-11-01 19:30:05 +08003260 /* if polling, we will stay in the work queue after sending the data.
3261 * otherwise, interrupt handler takes over after sending the data.
3262 */
3263 has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3264
Albert Leec71c1852005-10-04 06:03:45 -04003265 /* sleep-wait for BSY to clear */
3266 DPRINTK("busy wait\n");
Albert Leefbcdd802005-11-01 19:30:05 +08003267 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
Albert Leea4f16612005-12-26 16:40:53 +08003268 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Leefbcdd802005-11-01 19:30:05 +08003269 ap->hsm_task_state = HSM_ST_TMOUT;
Albert Leec71c1852005-10-04 06:03:45 -04003270 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003271 }
Albert Leec71c1852005-10-04 06:03:45 -04003272
3273 /* make sure DRQ is set */
3274 status = ata_chk_status(ap);
Albert Leefbcdd802005-11-01 19:30:05 +08003275 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3276 /* device status error */
Albert Leea4f16612005-12-26 16:40:53 +08003277 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Leefbcdd802005-11-01 19:30:05 +08003278 ap->hsm_task_state = HSM_ST_ERR;
Albert Leec71c1852005-10-04 06:03:45 -04003279 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003280 }
Albert Leec71c1852005-10-04 06:03:45 -04003281
3282 /* Send the CDB (atapi) or the first data block (ata pio out).
3283 * During the state transition, interrupt handler shouldn't
3284 * be invoked before the data transfer is complete and
3285 * hsm_task_state is changed. Hence, the following locking.
3286 */
3287 spin_lock_irqsave(&ap->host_set->lock, flags);
3288
3289 if (qc->tf.protocol == ATA_PROT_PIO) {
3290 /* PIO data out protocol.
3291 * send first data block.
3292 */
3293
Albert Lee07f6f7d2005-11-01 19:33:20 +08003294 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3295 * so, the state is changed here before ata_pio_sectors().
Albert Leec71c1852005-10-04 06:03:45 -04003296 */
3297 ap->hsm_task_state = HSM_ST;
Albert Lee07f6f7d2005-11-01 19:33:20 +08003298 ata_pio_sectors(qc);
Albert Leec71c1852005-10-04 06:03:45 -04003299 ata_altstatus(ap); /* flush */
3300 } else
3301 /* send CDB */
3302 atapi_send_cdb(ap, qc);
3303
Albert Leefbcdd802005-11-01 19:30:05 +08003304 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3305
Albert Leec71c1852005-10-04 06:03:45 -04003306 /* if polling, ata_pio_task() handles the rest.
3307 * otherwise, interrupt handler takes over from here.
3308 */
Albert Leefbcdd802005-11-01 19:30:05 +08003309 return has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003310
3311err_out:
Albert Leefbcdd802005-11-01 19:30:05 +08003312 return 1; /* has next */
Albert Leec71c1852005-10-04 06:03:45 -04003313}
3314
3315/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003316 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3317 * @qc: Command on going
3318 * @bytes: number of bytes
3319 *
3320 * Transfer Transfer data from/to the ATAPI device.
3321 *
3322 * LOCKING:
3323 * Inherited from caller.
3324 *
3325 */
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3328{
3329 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003330 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 struct ata_port *ap = qc->ap;
3332 struct page *page;
3333 unsigned char *buf;
3334 unsigned int offset, count;
3335
Albert Lee563a6e12005-08-12 14:17:50 +08003336 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003337 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
3339next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003340 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003341 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003342 * The end of qc->sg is reached and the device expects
3343 * more data to transfer. In order not to overrun qc->sg
3344 * and fulfill length specified in the byte count register,
3345 * - for read case, discard trailing data from the device
3346 * - for write case, padding zero data to the device
3347 */
3348 u16 pad_buf[1] = { 0 };
3349 unsigned int words = bytes >> 1;
3350 unsigned int i;
3351
3352 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003353 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003354 ap->id, bytes);
3355
3356 for (i = 0; i < words; i++)
3357 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3358
Albert Lee14be71f2005-09-27 17:36:35 +08003359 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003360 return;
3361 }
3362
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003363 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 page = sg->page;
3366 offset = sg->offset + qc->cursg_ofs;
3367
3368 /* get the current page and offset */
3369 page = nth_page(page, (offset >> PAGE_SHIFT));
3370 offset %= PAGE_SIZE;
3371
Albert Lee6952df02005-06-06 15:56:03 +08003372 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003373 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
3375 /* don't cross page boundaries */
3376 count = min(count, (unsigned int)PAGE_SIZE - offset);
3377
Albert Lee7282aa42005-10-09 09:46:07 -04003378 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3379
Albert Lee91b8b312005-10-09 09:48:44 -04003380 if (PageHighMem(page)) {
3381 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003382
Albert Lee91b8b312005-10-09 09:48:44 -04003383 local_irq_save(flags);
3384 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003385
Albert Lee91b8b312005-10-09 09:48:44 -04003386 /* do the actual data transfer */
3387 ata_data_xfer(ap, buf + offset, count, do_write);
3388
3389 kunmap_atomic(buf, KM_IRQ0);
3390 local_irq_restore(flags);
3391 } else {
3392 buf = page_address(page);
3393 ata_data_xfer(ap, buf + offset, count, do_write);
3394 }
Albert Lee7282aa42005-10-09 09:46:07 -04003395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 bytes -= count;
3397 qc->curbytes += count;
3398 qc->cursg_ofs += count;
3399
Albert Lee32529e02005-05-26 03:49:42 -04003400 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 qc->cursg++;
3402 qc->cursg_ofs = 0;
3403 }
3404
Albert Lee563a6e12005-08-12 14:17:50 +08003405 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003409/**
3410 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3411 * @qc: Command on going
3412 *
3413 * Transfer Transfer data from/to the ATAPI device.
3414 *
3415 * LOCKING:
3416 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003417 */
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3420{
3421 struct ata_port *ap = qc->ap;
3422 struct ata_device *dev = qc->dev;
3423 unsigned int ireason, bc_lo, bc_hi, bytes;
3424 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3425
3426 ap->ops->tf_read(ap, &qc->tf);
3427 ireason = qc->tf.nsect;
3428 bc_lo = qc->tf.lbam;
3429 bc_hi = qc->tf.lbah;
3430 bytes = (bc_hi << 8) | bc_lo;
3431
3432 /* shall be cleared to zero, indicating xfer of data */
3433 if (ireason & (1 << 0))
3434 goto err_out;
3435
3436 /* make sure transfer direction matches expected */
3437 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3438 if (do_write != i_write)
3439 goto err_out;
3440
Albert Lee312f7da2005-09-27 17:38:03 +08003441 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 __atapi_pio_bytes(qc, bytes);
3444
3445 return;
3446
3447err_out:
3448 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3449 ap->id, dev->devno);
Albert Lee1c848982005-12-05 15:40:15 +08003450 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003451 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
3454/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003455 * ata_pio_block - start PIO on a block
3456 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 *
3458 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003459 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 */
3461
3462static void ata_pio_block(struct ata_port *ap)
3463{
3464 struct ata_queued_cmd *qc;
3465 u8 status;
3466
3467 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003468 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 * Sometimes when we enter, BSY will be cleared in
3470 * a chk-status or two. If not, the drive is probably seeking
3471 * or something. Snooze for a couple msecs, then
3472 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003473 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 */
3475 status = ata_busy_wait(ap, ATA_BUSY, 5);
3476 if (status & ATA_BUSY) {
3477 msleep(2);
3478 status = ata_busy_wait(ap, ATA_BUSY, 10);
3479 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003480 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3482 return;
3483 }
3484 }
3485
3486 qc = ata_qc_from_tag(ap, ap->active_tag);
3487 assert(qc != NULL);
3488
Albert Leefe79e682005-12-06 11:34:59 +08003489 /* check error */
3490 if (status & (ATA_ERR | ATA_DF)) {
3491 qc->err_mask |= AC_ERR_DEV;
3492 ap->hsm_task_state = HSM_ST_ERR;
3493 return;
3494 }
3495
3496 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003498 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003500 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 return;
3502 }
3503
3504 atapi_pio_bytes(qc);
3505 } else {
3506 /* handle BSY=0, DRQ=0 as error */
3507 if ((status & ATA_DRQ) == 0) {
Albert Lee1c848982005-12-05 15:40:15 +08003508 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003509 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 return;
3511 }
3512
Albert Lee07f6f7d2005-11-01 19:33:20 +08003513 ata_pio_sectors(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 }
Albert Lee467b16d2005-11-01 19:19:01 +08003515
3516 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
3519static void ata_pio_error(struct ata_port *ap)
3520{
3521 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003522
3523 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 qc = ata_qc_from_tag(ap, ap->active_tag);
3526 assert(qc != NULL);
3527
Albert Lee1c848982005-12-05 15:40:15 +08003528 /* make sure qc->err_mask is available to
3529 * know what's wrong and recover
3530 */
3531 assert(qc->err_mask);
3532
Albert Lee14be71f2005-09-27 17:36:35 +08003533 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Albert Leea22e2eb2005-12-05 15:38:02 +08003535 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536}
3537
3538static void ata_pio_task(void *_data)
3539{
3540 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003541 unsigned long timeout;
Albert Leefbcdd802005-11-01 19:30:05 +08003542 int has_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003543
3544fsm_start:
3545 timeout = 0;
Albert Leefbcdd802005-11-01 19:30:05 +08003546 has_next = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Albert Lee14be71f2005-09-27 17:36:35 +08003548 switch (ap->hsm_task_state) {
Albert Leee27486d2005-11-01 19:24:49 +08003549 case HSM_ST_FIRST:
Albert Leefbcdd802005-11-01 19:30:05 +08003550 has_next = ata_pio_first_block(ap);
3551 break;
Albert Leee27486d2005-11-01 19:24:49 +08003552
Albert Lee14be71f2005-09-27 17:36:35 +08003553 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 ata_pio_block(ap);
3555 break;
3556
Albert Lee14be71f2005-09-27 17:36:35 +08003557 case HSM_ST_LAST:
Albert Leefbcdd802005-11-01 19:30:05 +08003558 has_next = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 break;
3560
Albert Lee14be71f2005-09-27 17:36:35 +08003561 case HSM_ST_POLL:
3562 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 timeout = ata_pio_poll(ap);
3564 break;
3565
Albert Lee14be71f2005-09-27 17:36:35 +08003566 case HSM_ST_TMOUT:
3567 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 ata_pio_error(ap);
3569 return;
Albert Lee467b16d2005-11-01 19:19:01 +08003570
3571 default:
3572 BUG();
3573 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 }
3575
3576 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003577 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
Albert Leefbcdd802005-11-01 19:30:05 +08003578 else if (has_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003579 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582/**
3583 * ata_qc_timeout - Handle timeout of queued command
3584 * @qc: Command that timed out
3585 *
3586 * Some part of the kernel (currently, only the SCSI layer)
3587 * has noticed that the active command on port @ap has not
3588 * completed after a specified length of time. Handle this
3589 * condition by disabling DMA (if necessary) and completing
3590 * transactions, with error if necessary.
3591 *
3592 * This also handles the case of the "lost interrupt", where
3593 * for some reason (possibly hardware bug, possibly driver bug)
3594 * an interrupt was not delivered to the driver, even though the
3595 * transaction completed successfully.
3596 *
3597 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003598 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 */
3600
3601static void ata_qc_timeout(struct ata_queued_cmd *qc)
3602{
3603 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003604 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003606 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
3608 DPRINTK("ENTER\n");
3609
Jeff Garzikb8f61532005-08-25 22:01:20 -04003610 spin_lock_irqsave(&host_set->lock, flags);
3611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 /* hack alert! We cannot use the supplied completion
3613 * function from inside the ->eh_strategy_handler() thread.
3614 * libata is the only user of ->eh_strategy_handler() in
3615 * any kernel, so the default scsi_done() assumes it is
3616 * not being called from the SCSI EH.
3617 */
3618 qc->scsidone = scsi_finish_command;
3619
3620 switch (qc->tf.protocol) {
3621
3622 case ATA_PROT_DMA:
3623 case ATA_PROT_ATAPI_DMA:
3624 host_stat = ap->ops->bmdma_status(ap);
3625
3626 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003627 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
3629 /* fall through */
3630
3631 default:
3632 ata_altstatus(ap);
3633 drv_stat = ata_chk_status(ap);
3634
3635 /* ack bmdma irq events */
3636 ap->ops->irq_clear(ap);
3637
3638 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3639 ap->id, qc->tf.command, drv_stat, host_stat);
3640
Albert Lee312f7da2005-09-27 17:38:03 +08003641 ap->hsm_task_state = HSM_ST_IDLE;
3642
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003644 qc->err_mask |= ac_err_mask(drv_stat);
3645 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 break;
3647 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003648
3649 spin_unlock_irqrestore(&host_set->lock, flags);
3650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 DPRINTK("EXIT\n");
3652}
3653
3654/**
3655 * ata_eng_timeout - Handle timeout of queued command
3656 * @ap: Port on which timed-out command is active
3657 *
3658 * Some part of the kernel (currently, only the SCSI layer)
3659 * has noticed that the active command on port @ap has not
3660 * completed after a specified length of time. Handle this
3661 * condition by disabling DMA (if necessary) and completing
3662 * transactions, with error if necessary.
3663 *
3664 * This also handles the case of the "lost interrupt", where
3665 * for some reason (possibly hardware bug, possibly driver bug)
3666 * an interrupt was not delivered to the driver, even though the
3667 * transaction completed successfully.
3668 *
3669 * LOCKING:
3670 * Inherited from SCSI layer (none, can sleep)
3671 */
3672
3673void ata_eng_timeout(struct ata_port *ap)
3674{
3675 struct ata_queued_cmd *qc;
3676
3677 DPRINTK("ENTER\n");
3678
3679 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003680 if (qc)
3681 ata_qc_timeout(qc);
3682 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3684 ap->id);
3685 goto out;
3686 }
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688out:
3689 DPRINTK("EXIT\n");
3690}
3691
3692/**
3693 * ata_qc_new - Request an available ATA command, for queueing
3694 * @ap: Port associated with device @dev
3695 * @dev: Device from whom we request an available command structure
3696 *
3697 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003698 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 */
3700
3701static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3702{
3703 struct ata_queued_cmd *qc = NULL;
3704 unsigned int i;
3705
3706 for (i = 0; i < ATA_MAX_QUEUE; i++)
3707 if (!test_and_set_bit(i, &ap->qactive)) {
3708 qc = ata_qc_from_tag(ap, i);
3709 break;
3710 }
3711
3712 if (qc)
3713 qc->tag = i;
3714
3715 return qc;
3716}
3717
3718/**
3719 * ata_qc_new_init - Request an available ATA command, and initialize it
3720 * @ap: Port associated with device @dev
3721 * @dev: Device from whom we request an available command structure
3722 *
3723 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003724 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 */
3726
3727struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3728 struct ata_device *dev)
3729{
3730 struct ata_queued_cmd *qc;
3731
3732 qc = ata_qc_new(ap);
3733 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 qc->scsicmd = NULL;
3735 qc->ap = ap;
3736 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003738 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 }
3740
3741 return qc;
3742}
3743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744static void __ata_qc_complete(struct ata_queued_cmd *qc)
3745{
3746 struct ata_port *ap = qc->ap;
Tejun Heob5632302005-12-13 14:51:25 +09003747 unsigned int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
3749 qc->flags = 0;
3750 tag = qc->tag;
3751 if (likely(ata_tag_valid(tag))) {
3752 if (tag == ap->active_tag)
3753 ap->active_tag = ATA_TAG_POISON;
3754 qc->tag = ATA_TAG_POISON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 clear_bit(tag, &ap->qactive);
Tejun Heob5632302005-12-13 14:51:25 +09003756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
3758
3759/**
3760 * ata_qc_free - free unused ata_queued_cmd
3761 * @qc: Command to complete
3762 *
3763 * Designed to free unused ata_queued_cmd object
3764 * in case something prevents using it.
3765 *
3766 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003767 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 */
3769void ata_qc_free(struct ata_queued_cmd *qc)
3770{
3771 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
3773 __ata_qc_complete(qc);
3774}
3775
3776/**
3777 * ata_qc_complete - Complete an active ATA command
3778 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003779 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003780 *
3781 * Indicate to the mid and upper layers that an ATA
3782 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 *
3784 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003785 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 */
3787
Albert Leea22e2eb2005-12-05 15:38:02 +08003788void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
3790 int rc;
3791
3792 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3793 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3794
3795 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3796 ata_sg_clean(qc);
3797
Albert Lee3f3791d2005-08-16 14:25:38 +08003798 /* atapi: mark qc as inactive to prevent the interrupt handler
3799 * from completing the command twice later, before the error handler
3800 * is called. (when rc != 0 and atapi request sense is needed)
3801 */
3802 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3803
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 /* call completion callback */
Albert Leea22e2eb2005-12-05 15:38:02 +08003805 rc = qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807 /* if callback indicates not to complete command (non-zero),
3808 * return immediately
3809 */
3810 if (rc != 0)
3811 return;
3812
3813 __ata_qc_complete(qc);
3814
3815 VPRINTK("EXIT\n");
3816}
3817
3818static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3819{
3820 struct ata_port *ap = qc->ap;
3821
3822 switch (qc->tf.protocol) {
3823 case ATA_PROT_DMA:
3824 case ATA_PROT_ATAPI_DMA:
3825 return 1;
3826
3827 case ATA_PROT_ATAPI:
3828 case ATA_PROT_PIO:
3829 case ATA_PROT_PIO_MULT:
3830 if (ap->flags & ATA_FLAG_PIO_DMA)
3831 return 1;
3832
3833 /* fall through */
3834
3835 default:
3836 return 0;
3837 }
3838
3839 /* never reached */
3840}
3841
3842/**
3843 * ata_qc_issue - issue taskfile to device
3844 * @qc: command to issue to device
3845 *
3846 * Prepare an ATA command to submission to device.
3847 * This includes mapping the data into a DMA-able
3848 * area, filling in the S/G table, and finally
3849 * writing the taskfile to hardware, starting the command.
3850 *
3851 * LOCKING:
3852 * spin_lock_irqsave(host_set lock)
3853 *
3854 * RETURNS:
3855 * Zero on success, negative on error.
3856 */
3857
3858int ata_qc_issue(struct ata_queued_cmd *qc)
3859{
3860 struct ata_port *ap = qc->ap;
3861
3862 if (ata_should_dma_map(qc)) {
3863 if (qc->flags & ATA_QCFLAG_SG) {
3864 if (ata_sg_setup(qc))
3865 goto err_out;
3866 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3867 if (ata_sg_setup_one(qc))
3868 goto err_out;
3869 }
3870 } else {
3871 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3872 }
3873
3874 ap->ops->qc_prep(qc);
3875
3876 qc->ap->active_tag = qc->tag;
3877 qc->flags |= ATA_QCFLAG_ACTIVE;
3878
3879 return ap->ops->qc_issue(qc);
3880
3881err_out:
3882 return -1;
3883}
3884
Edward Falk0baab862005-06-02 18:17:13 -04003885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886/**
3887 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3888 * @qc: command to issue to device
3889 *
3890 * Using various libata functions and hooks, this function
3891 * starts an ATA command. ATA commands are grouped into
3892 * classes called "protocols", and issuing each type of protocol
3893 * is slightly different.
3894 *
Edward Falk0baab862005-06-02 18:17:13 -04003895 * May be used as the qc_issue() entry in ata_port_operations.
3896 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 * LOCKING:
3898 * spin_lock_irqsave(host_set lock)
3899 *
3900 * RETURNS:
3901 * Zero on success, negative on error.
3902 */
3903
3904int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3905{
3906 struct ata_port *ap = qc->ap;
3907
Albert Leee50362e2005-09-27 17:39:50 +08003908 /* Use polling pio if the LLD doesn't handle
3909 * interrupt driven pio and atapi CDB interrupt.
3910 */
3911 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3912 switch (qc->tf.protocol) {
3913 case ATA_PROT_PIO:
3914 case ATA_PROT_ATAPI:
3915 case ATA_PROT_ATAPI_NODATA:
3916 qc->tf.flags |= ATA_TFLAG_POLLING;
3917 break;
3918 case ATA_PROT_ATAPI_DMA:
3919 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3920 BUG();
3921 break;
3922 default:
3923 break;
3924 }
3925 }
3926
Albert Lee312f7da2005-09-27 17:38:03 +08003927 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 ata_dev_select(ap, qc->dev->devno, 1, 0);
3929
Albert Lee312f7da2005-09-27 17:38:03 +08003930 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 switch (qc->tf.protocol) {
3932 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003933 if (qc->tf.flags & ATA_TFLAG_POLLING)
3934 ata_qc_set_polling(qc);
3935
Jeff Garzike5338252005-10-30 21:37:17 -05003936 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003937 ap->hsm_task_state = HSM_ST_LAST;
3938
3939 if (qc->tf.flags & ATA_TFLAG_POLLING)
3940 queue_work(ata_wq, &ap->pio_task);
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 break;
3943
3944 case ATA_PROT_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003945 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3946
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3948 ap->ops->bmdma_setup(qc); /* set up bmdma */
3949 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003950 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 break;
3952
Albert Lee312f7da2005-09-27 17:38:03 +08003953 case ATA_PROT_PIO:
3954 if (qc->tf.flags & ATA_TFLAG_POLLING)
3955 ata_qc_set_polling(qc);
3956
Jeff Garzike5338252005-10-30 21:37:17 -05003957 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003958
Albert Lee54f00382005-09-30 19:14:19 +08003959 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3960 /* PIO data out protocol */
3961 ap->hsm_task_state = HSM_ST_FIRST;
Albert Leee27486d2005-11-01 19:24:49 +08003962 queue_work(ata_wq, &ap->pio_task);
Albert Lee54f00382005-09-30 19:14:19 +08003963
3964 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08003965 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08003966 */
Albert Lee312f7da2005-09-27 17:38:03 +08003967 } else {
Albert Lee54f00382005-09-30 19:14:19 +08003968 /* PIO data in protocol */
3969 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08003970
Albert Lee54f00382005-09-30 19:14:19 +08003971 if (qc->tf.flags & ATA_TFLAG_POLLING)
3972 queue_work(ata_wq, &ap->pio_task);
Albert Lee312f7da2005-09-27 17:38:03 +08003973
Albert Lee54f00382005-09-30 19:14:19 +08003974 /* if polling, ata_pio_task() handles the rest.
3975 * otherwise, interrupt handler takes over from here.
3976 */
Albert Lee312f7da2005-09-27 17:38:03 +08003977 }
3978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 break;
3980
3981 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003983 if (qc->tf.flags & ATA_TFLAG_POLLING)
3984 ata_qc_set_polling(qc);
3985
Jeff Garzike5338252005-10-30 21:37:17 -05003986 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003987 ap->hsm_task_state = HSM_ST_FIRST;
3988
3989 /* send cdb by polling if no cdb interrupt */
3990 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3991 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Leee27486d2005-11-01 19:24:49 +08003992 queue_work(ata_wq, &ap->pio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 break;
3994
3995 case ATA_PROT_ATAPI_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003996 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3997
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3999 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004000 ap->hsm_task_state = HSM_ST_FIRST;
4001
4002 /* send cdb by polling if no cdb interrupt */
4003 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Leee27486d2005-11-01 19:24:49 +08004004 queue_work(ata_wq, &ap->pio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 break;
4006
4007 default:
4008 WARN_ON(1);
4009 return -1;
4010 }
4011
4012 return 0;
4013}
4014
4015/**
Edward Falk0baab862005-06-02 18:17:13 -04004016 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 * @qc: Info associated with this ATA transaction.
4018 *
4019 * LOCKING:
4020 * spin_lock_irqsave(host_set lock)
4021 */
4022
4023static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4024{
4025 struct ata_port *ap = qc->ap;
4026 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4027 u8 dmactl;
4028 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4029
4030 /* load PRD table addr. */
4031 mb(); /* make sure PRD table writes are visible to controller */
4032 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4033
4034 /* specify data direction, triple-check start bit is clear */
4035 dmactl = readb(mmio + ATA_DMA_CMD);
4036 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4037 if (!rw)
4038 dmactl |= ATA_DMA_WR;
4039 writeb(dmactl, mmio + ATA_DMA_CMD);
4040
4041 /* issue r/w command */
4042 ap->ops->exec_command(ap, &qc->tf);
4043}
4044
4045/**
Alan Coxb73fc892005-08-26 16:03:19 +01004046 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 * @qc: Info associated with this ATA transaction.
4048 *
4049 * LOCKING:
4050 * spin_lock_irqsave(host_set lock)
4051 */
4052
4053static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4054{
4055 struct ata_port *ap = qc->ap;
4056 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4057 u8 dmactl;
4058
4059 /* start host DMA transaction */
4060 dmactl = readb(mmio + ATA_DMA_CMD);
4061 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4062
4063 /* Strictly, one may wish to issue a readb() here, to
4064 * flush the mmio write. However, control also passes
4065 * to the hardware at this point, and it will interrupt
4066 * us when we are to resume control. So, in effect,
4067 * we don't care when the mmio write flushes.
4068 * Further, a read of the DMA status register _immediately_
4069 * following the write may not be what certain flaky hardware
4070 * is expected, so I think it is best to not add a readb()
4071 * without first all the MMIO ATA cards/mobos.
4072 * Or maybe I'm just being paranoid.
4073 */
4074}
4075
4076/**
4077 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4078 * @qc: Info associated with this ATA transaction.
4079 *
4080 * LOCKING:
4081 * spin_lock_irqsave(host_set lock)
4082 */
4083
4084static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4085{
4086 struct ata_port *ap = qc->ap;
4087 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4088 u8 dmactl;
4089
4090 /* load PRD table addr. */
4091 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4092
4093 /* specify data direction, triple-check start bit is clear */
4094 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4095 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4096 if (!rw)
4097 dmactl |= ATA_DMA_WR;
4098 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4099
4100 /* issue r/w command */
4101 ap->ops->exec_command(ap, &qc->tf);
4102}
4103
4104/**
4105 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4106 * @qc: Info associated with this ATA transaction.
4107 *
4108 * LOCKING:
4109 * spin_lock_irqsave(host_set lock)
4110 */
4111
4112static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4113{
4114 struct ata_port *ap = qc->ap;
4115 u8 dmactl;
4116
4117 /* start host DMA transaction */
4118 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4119 outb(dmactl | ATA_DMA_START,
4120 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4121}
4122
Edward Falk0baab862005-06-02 18:17:13 -04004123
4124/**
4125 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4126 * @qc: Info associated with this ATA transaction.
4127 *
4128 * Writes the ATA_DMA_START flag to the DMA command register.
4129 *
4130 * May be used as the bmdma_start() entry in ata_port_operations.
4131 *
4132 * LOCKING:
4133 * spin_lock_irqsave(host_set lock)
4134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135void ata_bmdma_start(struct ata_queued_cmd *qc)
4136{
4137 if (qc->ap->flags & ATA_FLAG_MMIO)
4138 ata_bmdma_start_mmio(qc);
4139 else
4140 ata_bmdma_start_pio(qc);
4141}
4142
Edward Falk0baab862005-06-02 18:17:13 -04004143
4144/**
4145 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4146 * @qc: Info associated with this ATA transaction.
4147 *
4148 * Writes address of PRD table to device's PRD Table Address
4149 * register, sets the DMA control register, and calls
4150 * ops->exec_command() to start the transfer.
4151 *
4152 * May be used as the bmdma_setup() entry in ata_port_operations.
4153 *
4154 * LOCKING:
4155 * spin_lock_irqsave(host_set lock)
4156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157void ata_bmdma_setup(struct ata_queued_cmd *qc)
4158{
4159 if (qc->ap->flags & ATA_FLAG_MMIO)
4160 ata_bmdma_setup_mmio(qc);
4161 else
4162 ata_bmdma_setup_pio(qc);
4163}
4164
Edward Falk0baab862005-06-02 18:17:13 -04004165
4166/**
4167 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004168 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004169 *
4170 * Clear interrupt and error flags in DMA status register.
4171 *
4172 * May be used as the irq_clear() entry in ata_port_operations.
4173 *
4174 * LOCKING:
4175 * spin_lock_irqsave(host_set lock)
4176 */
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178void ata_bmdma_irq_clear(struct ata_port *ap)
4179{
4180 if (ap->flags & ATA_FLAG_MMIO) {
4181 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4182 writeb(readb(mmio), mmio);
4183 } else {
4184 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4185 outb(inb(addr), addr);
4186 }
4187
4188}
4189
Edward Falk0baab862005-06-02 18:17:13 -04004190
4191/**
4192 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004193 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004194 *
4195 * Read and return BMDMA status register.
4196 *
4197 * May be used as the bmdma_status() entry in ata_port_operations.
4198 *
4199 * LOCKING:
4200 * spin_lock_irqsave(host_set lock)
4201 */
4202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203u8 ata_bmdma_status(struct ata_port *ap)
4204{
4205 u8 host_stat;
4206 if (ap->flags & ATA_FLAG_MMIO) {
4207 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4208 host_stat = readb(mmio + ATA_DMA_STATUS);
4209 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004210 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 return host_stat;
4212}
4213
Edward Falk0baab862005-06-02 18:17:13 -04004214
4215/**
4216 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004217 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004218 *
4219 * Clears the ATA_DMA_START flag in the dma control register
4220 *
4221 * May be used as the bmdma_stop() entry in ata_port_operations.
4222 *
4223 * LOCKING:
4224 * spin_lock_irqsave(host_set lock)
4225 */
4226
Alan Coxb73fc892005-08-26 16:03:19 +01004227void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228{
Alan Coxb73fc892005-08-26 16:03:19 +01004229 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 if (ap->flags & ATA_FLAG_MMIO) {
4231 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4232
4233 /* clear start/stop bit */
4234 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4235 mmio + ATA_DMA_CMD);
4236 } else {
4237 /* clear start/stop bit */
4238 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4239 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4240 }
4241
4242 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4243 ata_altstatus(ap); /* dummy read */
4244}
4245
4246/**
4247 * ata_host_intr - Handle host interrupt for given (port, task)
4248 * @ap: Port on which interrupt arrived (possibly...)
4249 * @qc: Taskfile currently active in engine
4250 *
4251 * Handle host interrupt for given queued command. Currently,
4252 * only DMA interrupts are handled. All other commands are
4253 * handled via polling with interrupts disabled (nIEN bit).
4254 *
4255 * LOCKING:
4256 * spin_lock_irqsave(host_set lock)
4257 *
4258 * RETURNS:
4259 * One if interrupt was handled, zero if not (shared irq).
4260 */
4261
4262inline unsigned int ata_host_intr (struct ata_port *ap,
4263 struct ata_queued_cmd *qc)
4264{
Albert Lee312f7da2005-09-27 17:38:03 +08004265 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Albert Lee312f7da2005-09-27 17:38:03 +08004267 VPRINTK("ata%u: protocol %d task_state %d\n",
4268 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Albert Lee312f7da2005-09-27 17:38:03 +08004270 /* Check whether we are expecting interrupt in this state */
4271 switch (ap->hsm_task_state) {
4272 case HSM_ST_FIRST:
4273 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4274 * The flag was turned on only for atapi devices.
4275 * No need to check is_atapi_taskfile(&qc->tf) again.
4276 */
4277 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08004279 break;
4280 case HSM_ST_LAST:
4281 if (qc->tf.protocol == ATA_PROT_DMA ||
4282 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4283 /* check status of DMA engine */
4284 host_stat = ap->ops->bmdma_status(ap);
4285 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Albert Lee312f7da2005-09-27 17:38:03 +08004287 /* if it's not our irq... */
4288 if (!(host_stat & ATA_DMA_INTR))
4289 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Albert Lee312f7da2005-09-27 17:38:03 +08004291 /* before we do anything else, clear DMA-Start bit */
4292 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08004293
4294 if (unlikely(host_stat & ATA_DMA_ERR)) {
4295 /* error when transfering data to/from memory */
4296 qc->err_mask |= AC_ERR_HOST_BUS;
4297 ap->hsm_task_state = HSM_ST_ERR;
4298 }
Albert Lee312f7da2005-09-27 17:38:03 +08004299 }
4300 break;
4301 case HSM_ST:
4302 break;
4303 default:
4304 goto idle_irq;
4305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Albert Lee312f7da2005-09-27 17:38:03 +08004307 /* check altstatus */
4308 status = ata_altstatus(ap);
4309 if (status & ATA_BUSY)
4310 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Albert Lee312f7da2005-09-27 17:38:03 +08004312 /* check main status, clearing INTRQ */
4313 status = ata_chk_status(ap);
4314 if (unlikely(status & ATA_BUSY))
4315 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
Albert Lee312f7da2005-09-27 17:38:03 +08004317 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4318 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4319
4320 /* ack bmdma irq events */
4321 ap->ops->irq_clear(ap);
4322
4323 /* check error */
Albert Leea4f16612005-12-26 16:40:53 +08004324 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4325 qc->err_mask |= AC_ERR_DEV;
Albert Lee312f7da2005-09-27 17:38:03 +08004326 ap->hsm_task_state = HSM_ST_ERR;
Albert Leea4f16612005-12-26 16:40:53 +08004327 }
Albert Lee312f7da2005-09-27 17:38:03 +08004328
4329fsm_start:
4330 switch (ap->hsm_task_state) {
4331 case HSM_ST_FIRST:
4332 /* Some pre-ATAPI-4 devices assert INTRQ
4333 * at this state when ready to receive CDB.
4334 */
4335
4336 /* check device status */
4337 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4338 /* Wrong status. Let EH handle this */
Albert Leea4f16612005-12-26 16:40:53 +08004339 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee312f7da2005-09-27 17:38:03 +08004340 ap->hsm_task_state = HSM_ST_ERR;
4341 goto fsm_start;
4342 }
4343
4344 atapi_send_cdb(ap, qc);
4345
4346 break;
4347
4348 case HSM_ST:
4349 /* complete command or read/write the data register */
4350 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4351 /* ATAPI PIO protocol */
4352 if ((status & ATA_DRQ) == 0) {
4353 /* no more data to transfer */
4354 ap->hsm_task_state = HSM_ST_LAST;
4355 goto fsm_start;
4356 }
4357
4358 atapi_pio_bytes(qc);
4359
4360 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4361 /* bad ireason reported by device */
4362 goto fsm_start;
4363
4364 } else {
4365 /* ATA PIO protocol */
4366 if (unlikely((status & ATA_DRQ) == 0)) {
4367 /* handle BSY=0, DRQ=0 as error */
Albert Leea4f16612005-12-26 16:40:53 +08004368 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee312f7da2005-09-27 17:38:03 +08004369 ap->hsm_task_state = HSM_ST_ERR;
4370 goto fsm_start;
4371 }
4372
Albert Lee07f6f7d2005-11-01 19:33:20 +08004373 ata_pio_sectors(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004374
4375 if (ap->hsm_task_state == HSM_ST_LAST &&
4376 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4377 /* all data read */
4378 ata_altstatus(ap);
4379 status = ata_chk_status(ap);
4380 goto fsm_start;
4381 }
4382 }
4383
4384 ata_altstatus(ap); /* flush */
4385 break;
4386
4387 case HSM_ST_LAST:
4388 if (unlikely(status & ATA_DRQ)) {
4389 /* handle DRQ=1 as error */
Albert Leea4f16612005-12-26 16:40:53 +08004390 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee312f7da2005-09-27 17:38:03 +08004391 ap->hsm_task_state = HSM_ST_ERR;
4392 goto fsm_start;
4393 }
4394
4395 /* no more data to transfer */
4396 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4397 ap->id, status);
4398
4399 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004402 qc->err_mask |= ac_err_mask(status);
4403 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 break;
4405
Albert Lee312f7da2005-09-27 17:38:03 +08004406 case HSM_ST_ERR:
4407 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4408 ap->id, status, host_stat);
4409
Albert Leea4f16612005-12-26 16:40:53 +08004410 /* make sure qc->err_mask is available to
4411 * know what's wrong and recover
4412 */
4413 assert(qc->err_mask);
4414
Albert Lee312f7da2005-09-27 17:38:03 +08004415 ap->hsm_task_state = HSM_ST_IDLE;
Jeff Garzik278efe92005-12-06 05:01:27 -05004416 ata_qc_complete(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 default:
4419 goto idle_irq;
4420 }
4421
4422 return 1; /* irq handled */
4423
4424idle_irq:
4425 ap->stats.idle_irq++;
4426
4427#ifdef ATA_IRQ_TRAP
4428 if ((ap->stats.idle_irq % 1000) == 0) {
4429 handled = 1;
4430 ata_irq_ack(ap, 0); /* debug trap */
4431 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4432 }
4433#endif
4434 return 0; /* irq not handled */
4435}
4436
4437/**
4438 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004439 * @irq: irq line (unused)
4440 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 * @regs: unused
4442 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004443 * Default interrupt handler for PCI IDE devices. Calls
4444 * ata_host_intr() for each port that is not disabled.
4445 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004447 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 *
4449 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004450 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 */
4452
4453irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4454{
4455 struct ata_host_set *host_set = dev_instance;
4456 unsigned int i;
4457 unsigned int handled = 0;
4458 unsigned long flags;
4459
4460 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4461 spin_lock_irqsave(&host_set->lock, flags);
4462
4463 for (i = 0; i < host_set->n_ports; i++) {
4464 struct ata_port *ap;
4465
4466 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004467 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08004468 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 struct ata_queued_cmd *qc;
4470
4471 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004472 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004473 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 handled |= ata_host_intr(ap, qc);
4475 }
4476 }
4477
4478 spin_unlock_irqrestore(&host_set->lock, flags);
4479
4480 return IRQ_RETVAL(handled);
4481}
4482
4483/**
Edward Falk0baab862005-06-02 18:17:13 -04004484 * ata_port_start - Set port up for dma.
4485 * @ap: Port to initialize
4486 *
4487 * Called just after data structures for each port are
4488 * initialized. Allocates space for PRD table.
4489 *
4490 * May be used as the port_start() entry in ata_port_operations.
4491 *
4492 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004493 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004494 */
4495
Jens Axboe9b847542006-01-06 09:28:07 +01004496/*
4497 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4498 * without filling any other registers
4499 */
4500static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4501 u8 cmd)
4502{
4503 struct ata_taskfile tf;
4504 int err;
4505
4506 ata_tf_init(ap, &tf, dev->devno);
4507
4508 tf.command = cmd;
4509 tf.flags |= ATA_TFLAG_DEVICE;
4510 tf.protocol = ATA_PROT_NODATA;
4511
4512 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4513 if (err)
4514 printk(KERN_ERR "%s: ata command failed: %d\n",
4515 __FUNCTION__, err);
4516
4517 return err;
4518}
4519
4520static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4521{
4522 u8 cmd;
4523
4524 if (!ata_try_flush_cache(dev))
4525 return 0;
4526
4527 if (ata_id_has_flush_ext(dev->id))
4528 cmd = ATA_CMD_FLUSH_EXT;
4529 else
4530 cmd = ATA_CMD_FLUSH;
4531
4532 return ata_do_simple_cmd(ap, dev, cmd);
4533}
4534
4535static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4536{
4537 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4538}
4539
4540static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4541{
4542 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4543}
4544
4545/**
4546 * ata_device_resume - wakeup a previously suspended devices
4547 *
4548 * Kick the drive back into action, by sending it an idle immediate
4549 * command and making sure its transfer mode matches between drive
4550 * and host.
4551 *
4552 */
4553int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4554{
4555 if (ap->flags & ATA_FLAG_SUSPENDED) {
4556 ap->flags &= ~ATA_FLAG_SUSPENDED;
4557 ata_set_mode(ap);
4558 }
4559 if (!ata_dev_present(dev))
4560 return 0;
4561 if (dev->class == ATA_DEV_ATA)
4562 ata_start_drive(ap, dev);
4563
4564 return 0;
4565}
4566
4567/**
4568 * ata_device_suspend - prepare a device for suspend
4569 *
4570 * Flush the cache on the drive, if appropriate, then issue a
4571 * standbynow command.
4572 *
4573 */
4574int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4575{
4576 if (!ata_dev_present(dev))
4577 return 0;
4578 if (dev->class == ATA_DEV_ATA)
4579 ata_flush_cache(ap, dev);
4580
4581 ata_standby_drive(ap, dev);
4582 ap->flags |= ATA_FLAG_SUSPENDED;
4583 return 0;
4584}
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586int ata_port_start (struct ata_port *ap)
4587{
4588 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004589 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
4591 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4592 if (!ap->prd)
4593 return -ENOMEM;
4594
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004595 rc = ata_pad_alloc(ap, dev);
4596 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004597 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004598 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004599 }
4600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4602
4603 return 0;
4604}
4605
Edward Falk0baab862005-06-02 18:17:13 -04004606
4607/**
4608 * ata_port_stop - Undo ata_port_start()
4609 * @ap: Port to shut down
4610 *
4611 * Frees the PRD table.
4612 *
4613 * May be used as the port_stop() entry in ata_port_operations.
4614 *
4615 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004616 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004617 */
4618
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619void ata_port_stop (struct ata_port *ap)
4620{
4621 struct device *dev = ap->host_set->dev;
4622
4623 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004624 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625}
4626
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004627void ata_host_stop (struct ata_host_set *host_set)
4628{
4629 if (host_set->mmio_base)
4630 iounmap(host_set->mmio_base);
4631}
4632
4633
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634/**
4635 * ata_host_remove - Unregister SCSI host structure with upper layers
4636 * @ap: Port to unregister
4637 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4638 *
4639 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004640 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 */
4642
4643static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4644{
4645 struct Scsi_Host *sh = ap->host;
4646
4647 DPRINTK("ENTER\n");
4648
4649 if (do_unregister)
4650 scsi_remove_host(sh);
4651
4652 ap->ops->port_stop(ap);
4653}
4654
4655/**
4656 * ata_host_init - Initialize an ata_port structure
4657 * @ap: Structure to initialize
4658 * @host: associated SCSI mid-layer structure
4659 * @host_set: Collection of hosts to which @ap belongs
4660 * @ent: Probe information provided by low-level driver
4661 * @port_no: Port number associated with this ata_port
4662 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004663 * Initialize a new ata_port structure, and its associated
4664 * scsi_host.
4665 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004667 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 */
4669
4670static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4671 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004672 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
4674 unsigned int i;
4675
4676 host->max_id = 16;
4677 host->max_lun = 1;
4678 host->max_channel = 1;
4679 host->unique_id = ata_unique_id++;
4680 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004681
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 ap->flags = ATA_FLAG_PORT_DISABLED;
4683 ap->id = host->unique_id;
4684 ap->host = host;
4685 ap->ctl = ATA_DEVCTL_OBS;
4686 ap->host_set = host_set;
4687 ap->port_no = port_no;
4688 ap->hard_port_no =
4689 ent->legacy_mode ? ent->hard_port_no : port_no;
4690 ap->pio_mask = ent->pio_mask;
4691 ap->mwdma_mask = ent->mwdma_mask;
4692 ap->udma_mask = ent->udma_mask;
4693 ap->flags |= ent->host_flags;
4694 ap->ops = ent->port_ops;
4695 ap->cbl = ATA_CBL_NONE;
4696 ap->active_tag = ATA_TAG_POISON;
4697 ap->last_ctl = 0xFF;
4698
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4700
4701 for (i = 0; i < ATA_MAX_DEVICES; i++)
4702 ap->device[i].devno = i;
4703
4704#ifdef ATA_IRQ_TRAP
4705 ap->stats.unhandled_irq = 1;
4706 ap->stats.idle_irq = 1;
4707#endif
4708
4709 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4710}
4711
4712/**
4713 * ata_host_add - Attach low-level ATA driver to system
4714 * @ent: Information provided by low-level driver
4715 * @host_set: Collections of ports to which we add
4716 * @port_no: Port number associated with this host
4717 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004718 * Attach low-level ATA driver to system.
4719 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004721 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 *
4723 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004724 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 */
4726
Jeff Garzik057ace52005-10-22 14:27:05 -04004727static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 struct ata_host_set *host_set,
4729 unsigned int port_no)
4730{
4731 struct Scsi_Host *host;
4732 struct ata_port *ap;
4733 int rc;
4734
4735 DPRINTK("ENTER\n");
4736 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4737 if (!host)
4738 return NULL;
4739
4740 ap = (struct ata_port *) &host->hostdata[0];
4741
4742 ata_host_init(ap, host, host_set, ent, port_no);
4743
4744 rc = ap->ops->port_start(ap);
4745 if (rc)
4746 goto err_out;
4747
4748 return ap;
4749
4750err_out:
4751 scsi_host_put(host);
4752 return NULL;
4753}
4754
4755/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004756 * ata_device_add - Register hardware device with ATA and SCSI layers
4757 * @ent: Probe information describing hardware device to be registered
4758 *
4759 * This function processes the information provided in the probe
4760 * information struct @ent, allocates the necessary ATA and SCSI
4761 * host information structures, initializes them, and registers
4762 * everything with requisite kernel subsystems.
4763 *
4764 * This function requests irqs, probes the ATA bus, and probes
4765 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 *
4767 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004768 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 *
4770 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004771 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 */
4773
Jeff Garzik057ace52005-10-22 14:27:05 -04004774int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
4776 unsigned int count = 0, i;
4777 struct device *dev = ent->dev;
4778 struct ata_host_set *host_set;
4779
4780 DPRINTK("ENTER\n");
4781 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004782 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4784 if (!host_set)
4785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 spin_lock_init(&host_set->lock);
4787
4788 host_set->dev = dev;
4789 host_set->n_ports = ent->n_ports;
4790 host_set->irq = ent->irq;
4791 host_set->mmio_base = ent->mmio_base;
4792 host_set->private_data = ent->private_data;
4793 host_set->ops = ent->port_ops;
4794
4795 /* register each port bound to this device */
4796 for (i = 0; i < ent->n_ports; i++) {
4797 struct ata_port *ap;
4798 unsigned long xfer_mode_mask;
4799
4800 ap = ata_host_add(ent, host_set, i);
4801 if (!ap)
4802 goto err_out;
4803
4804 host_set->ports[i] = ap;
4805 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4806 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4807 (ap->pio_mask << ATA_SHIFT_PIO);
4808
4809 /* print per-port info to dmesg */
4810 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4811 "bmdma 0x%lX irq %lu\n",
4812 ap->id,
4813 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4814 ata_mode_string(xfer_mode_mask),
4815 ap->ioaddr.cmd_addr,
4816 ap->ioaddr.ctl_addr,
4817 ap->ioaddr.bmdma_addr,
4818 ent->irq);
4819
4820 ata_chk_status(ap);
4821 host_set->ops->irq_clear(ap);
4822 count++;
4823 }
4824
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004825 if (!count)
4826 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 /* obtain irq, that is shared between channels */
4829 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4830 DRV_NAME, host_set))
4831 goto err_out;
4832
4833 /* perform each probe synchronously */
4834 DPRINTK("probe begin\n");
4835 for (i = 0; i < count; i++) {
4836 struct ata_port *ap;
4837 int rc;
4838
4839 ap = host_set->ports[i];
4840
4841 DPRINTK("ata%u: probe begin\n", ap->id);
4842 rc = ata_bus_probe(ap);
4843 DPRINTK("ata%u: probe end\n", ap->id);
4844
4845 if (rc) {
4846 /* FIXME: do something useful here?
4847 * Current libata behavior will
4848 * tear down everything when
4849 * the module is removed
4850 * or the h/w is unplugged.
4851 */
4852 }
4853
4854 rc = scsi_add_host(ap->host, dev);
4855 if (rc) {
4856 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4857 ap->id);
4858 /* FIXME: do something useful here */
4859 /* FIXME: handle unconditional calls to
4860 * scsi_scan_host and ata_host_remove, below,
4861 * at the very least
4862 */
4863 }
4864 }
4865
4866 /* probes are done, now scan each port's disk(s) */
4867 DPRINTK("probe begin\n");
4868 for (i = 0; i < count; i++) {
4869 struct ata_port *ap = host_set->ports[i];
4870
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004871 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 }
4873
4874 dev_set_drvdata(dev, host_set);
4875
4876 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4877 return ent->n_ports; /* success */
4878
4879err_out:
4880 for (i = 0; i < count; i++) {
4881 ata_host_remove(host_set->ports[i], 1);
4882 scsi_host_put(host_set->ports[i]->host);
4883 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004884err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 kfree(host_set);
4886 VPRINTK("EXIT, returning 0\n");
4887 return 0;
4888}
4889
4890/**
Alan Cox17b14452005-09-15 15:44:00 +01004891 * ata_host_set_remove - PCI layer callback for device removal
4892 * @host_set: ATA host set that was removed
4893 *
4894 * Unregister all objects associated with this host set. Free those
4895 * objects.
4896 *
4897 * LOCKING:
4898 * Inherited from calling layer (may sleep).
4899 */
4900
Alan Cox17b14452005-09-15 15:44:00 +01004901void ata_host_set_remove(struct ata_host_set *host_set)
4902{
4903 struct ata_port *ap;
4904 unsigned int i;
4905
4906 for (i = 0; i < host_set->n_ports; i++) {
4907 ap = host_set->ports[i];
4908 scsi_remove_host(ap->host);
4909 }
4910
4911 free_irq(host_set->irq, host_set);
4912
4913 for (i = 0; i < host_set->n_ports; i++) {
4914 ap = host_set->ports[i];
4915
4916 ata_scsi_release(ap->host);
4917
4918 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4919 struct ata_ioports *ioaddr = &ap->ioaddr;
4920
4921 if (ioaddr->cmd_addr == 0x1f0)
4922 release_region(0x1f0, 8);
4923 else if (ioaddr->cmd_addr == 0x170)
4924 release_region(0x170, 8);
4925 }
4926
4927 scsi_host_put(ap->host);
4928 }
4929
4930 if (host_set->ops->host_stop)
4931 host_set->ops->host_stop(host_set);
4932
4933 kfree(host_set);
4934}
4935
4936/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 * ata_scsi_release - SCSI layer callback hook for host unload
4938 * @host: libata host to be unloaded
4939 *
4940 * Performs all duties necessary to shut down a libata port...
4941 * Kill port kthread, disable port, and release resources.
4942 *
4943 * LOCKING:
4944 * Inherited from SCSI layer.
4945 *
4946 * RETURNS:
4947 * One.
4948 */
4949
4950int ata_scsi_release(struct Scsi_Host *host)
4951{
4952 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4953
4954 DPRINTK("ENTER\n");
4955
4956 ap->ops->port_disable(ap);
4957 ata_host_remove(ap, 0);
4958
4959 DPRINTK("EXIT\n");
4960 return 1;
4961}
4962
4963/**
4964 * ata_std_ports - initialize ioaddr with standard port offsets.
4965 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004966 *
4967 * Utility function which initializes data_addr, error_addr,
4968 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4969 * device_addr, status_addr, and command_addr to standard offsets
4970 * relative to cmd_addr.
4971 *
4972 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 */
Edward Falk0baab862005-06-02 18:17:13 -04004974
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975void ata_std_ports(struct ata_ioports *ioaddr)
4976{
4977 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4978 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4979 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4980 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4981 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4982 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4983 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4984 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4985 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4986 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4987}
4988
4989static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004990ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
4992 struct ata_probe_ent *probe_ent;
4993
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004994 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 if (!probe_ent) {
4996 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4997 kobject_name(&(dev->kobj)));
4998 return NULL;
4999 }
5000
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 INIT_LIST_HEAD(&probe_ent->node);
5002 probe_ent->dev = dev;
5003
5004 probe_ent->sht = port->sht;
5005 probe_ent->host_flags = port->host_flags;
5006 probe_ent->pio_mask = port->pio_mask;
5007 probe_ent->mwdma_mask = port->mwdma_mask;
5008 probe_ent->udma_mask = port->udma_mask;
5009 probe_ent->port_ops = port->port_ops;
5010
5011 return probe_ent;
5012}
5013
Edward Falk0baab862005-06-02 18:17:13 -04005014
5015
Jeff Garzik374b1872005-08-30 05:42:52 -04005016#ifdef CONFIG_PCI
5017
5018void ata_pci_host_stop (struct ata_host_set *host_set)
5019{
5020 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5021
5022 pci_iounmap(pdev, host_set->mmio_base);
5023}
5024
Edward Falk0baab862005-06-02 18:17:13 -04005025/**
5026 * ata_pci_init_native_mode - Initialize native-mode driver
5027 * @pdev: pci device to be initialized
5028 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04005029 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04005030 *
5031 * Utility function which allocates and initializes an
5032 * ata_probe_ent structure for a standard dual-port
5033 * PIO-based IDE controller. The returned ata_probe_ent
5034 * structure can be passed to ata_device_add(). The returned
5035 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04005036 *
5037 * The caller need only pass the address of the primary port, the
5038 * secondary will be deduced automatically. If the device has non
5039 * standard secondary port mappings this function can be called twice,
5040 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04005041 */
5042
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04005044ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045{
5046 struct ata_probe_ent *probe_ent =
5047 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04005048 int p = 0;
5049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 if (!probe_ent)
5051 return NULL;
5052
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 probe_ent->irq = pdev->irq;
5054 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00005055 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056
Alan Cox47a86592005-10-04 08:09:19 -04005057 if (ports & ATA_PORT_PRIMARY) {
5058 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
5059 probe_ent->port[p].altstatus_addr =
5060 probe_ent->port[p].ctl_addr =
5061 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
5062 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
5063 ata_std_ports(&probe_ent->port[p]);
5064 p++;
5065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Alan Cox47a86592005-10-04 08:09:19 -04005067 if (ports & ATA_PORT_SECONDARY) {
5068 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
5069 probe_ent->port[p].altstatus_addr =
5070 probe_ent->port[p].ctl_addr =
5071 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
5072 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
5073 ata_std_ports(&probe_ent->port[p]);
5074 p++;
5075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
Alan Cox47a86592005-10-04 08:09:19 -04005077 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return probe_ent;
5079}
5080
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005081static 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 -07005082{
Alan Cox47a86592005-10-04 08:09:19 -04005083 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005085 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 if (!probe_ent)
5087 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04005090 probe_ent->n_ports = 1;
5091 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00005092 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
Alan Cox47a86592005-10-04 08:09:19 -04005094 switch(port_num)
5095 {
5096 case 0:
5097 probe_ent->irq = 14;
5098 probe_ent->port[0].cmd_addr = 0x1f0;
5099 probe_ent->port[0].altstatus_addr =
5100 probe_ent->port[0].ctl_addr = 0x3f6;
5101 break;
5102 case 1:
5103 probe_ent->irq = 15;
5104 probe_ent->port[0].cmd_addr = 0x170;
5105 probe_ent->port[0].altstatus_addr =
5106 probe_ent->port[0].ctl_addr = 0x376;
5107 break;
5108 }
5109 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 return probe_ent;
5112}
5113
5114/**
5115 * ata_pci_init_one - Initialize/register PCI IDE host controller
5116 * @pdev: Controller to be initialized
5117 * @port_info: Information from low-level host driver
5118 * @n_ports: Number of ports attached to host controller
5119 *
Edward Falk0baab862005-06-02 18:17:13 -04005120 * This is a helper function which can be called from a driver's
5121 * xxx_init_one() probe function if the hardware uses traditional
5122 * IDE taskfile registers.
5123 *
5124 * This function calls pci_enable_device(), reserves its register
5125 * regions, sets the dma mask, enables bus master mode, and calls
5126 * ata_device_add()
5127 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 * LOCKING:
5129 * Inherited from PCI layer (may sleep).
5130 *
5131 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005132 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 */
5134
5135int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
5136 unsigned int n_ports)
5137{
Alan Cox47a86592005-10-04 08:09:19 -04005138 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 struct ata_port_info *port[2];
5140 u8 tmp8, mask;
5141 unsigned int legacy_mode = 0;
5142 int disable_dev_on_err = 1;
5143 int rc;
5144
5145 DPRINTK("ENTER\n");
5146
5147 port[0] = port_info[0];
5148 if (n_ports > 1)
5149 port[1] = port_info[1];
5150 else
5151 port[1] = port[0];
5152
5153 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
5154 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04005155 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
5157 mask = (1 << 2) | (1 << 0);
5158 if ((tmp8 & mask) != mask)
5159 legacy_mode = (1 << 3);
5160 }
5161
5162 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04005163 if ((!legacy_mode) && (n_ports > 2)) {
5164 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
5165 n_ports = 2;
5166 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 }
5168
Alan Cox47a86592005-10-04 08:09:19 -04005169 /* FIXME: Really for ATA it isn't safe because the device may be
5170 multi-purpose and we want to leave it alone if it was already
5171 enabled. Secondly for shared use as Arjan says we want refcounting
5172
5173 Checking dev->is_enabled is insufficient as this is not set at
5174 boot for the primary video which is BIOS enabled
5175 */
5176
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 rc = pci_enable_device(pdev);
5178 if (rc)
5179 return rc;
5180
5181 rc = pci_request_regions(pdev, DRV_NAME);
5182 if (rc) {
5183 disable_dev_on_err = 0;
5184 goto err_out;
5185 }
5186
Alan Cox47a86592005-10-04 08:09:19 -04005187 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 if (legacy_mode) {
5189 if (!request_region(0x1f0, 8, "libata")) {
5190 struct resource *conflict, res;
5191 res.start = 0x1f0;
5192 res.end = 0x1f0 + 8 - 1;
5193 conflict = ____request_resource(&ioport_resource, &res);
5194 if (!strcmp(conflict->name, "libata"))
5195 legacy_mode |= (1 << 0);
5196 else {
5197 disable_dev_on_err = 0;
5198 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
5199 }
5200 } else
5201 legacy_mode |= (1 << 0);
5202
5203 if (!request_region(0x170, 8, "libata")) {
5204 struct resource *conflict, res;
5205 res.start = 0x170;
5206 res.end = 0x170 + 8 - 1;
5207 conflict = ____request_resource(&ioport_resource, &res);
5208 if (!strcmp(conflict->name, "libata"))
5209 legacy_mode |= (1 << 1);
5210 else {
5211 disable_dev_on_err = 0;
5212 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
5213 }
5214 } else
5215 legacy_mode |= (1 << 1);
5216 }
5217
5218 /* we have legacy mode, but all ports are unavailable */
5219 if (legacy_mode == (1 << 3)) {
5220 rc = -EBUSY;
5221 goto err_out_regions;
5222 }
5223
5224 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
5225 if (rc)
5226 goto err_out_regions;
5227 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
5228 if (rc)
5229 goto err_out_regions;
5230
5231 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04005232 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005233 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04005234 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005235 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04005236 } else {
5237 if (n_ports == 2)
5238 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
5239 else
5240 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
5241 }
5242 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 rc = -ENOMEM;
5244 goto err_out_regions;
5245 }
5246
5247 pci_set_master(pdev);
5248
5249 /* FIXME: check ata_device_add return */
5250 if (legacy_mode) {
5251 if (legacy_mode & (1 << 0))
5252 ata_device_add(probe_ent);
5253 if (legacy_mode & (1 << 1))
5254 ata_device_add(probe_ent2);
5255 } else
5256 ata_device_add(probe_ent);
5257
5258 kfree(probe_ent);
5259 kfree(probe_ent2);
5260
5261 return 0;
5262
5263err_out_regions:
5264 if (legacy_mode & (1 << 0))
5265 release_region(0x1f0, 8);
5266 if (legacy_mode & (1 << 1))
5267 release_region(0x170, 8);
5268 pci_release_regions(pdev);
5269err_out:
5270 if (disable_dev_on_err)
5271 pci_disable_device(pdev);
5272 return rc;
5273}
5274
5275/**
5276 * ata_pci_remove_one - PCI layer callback for device removal
5277 * @pdev: PCI device that was removed
5278 *
5279 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005280 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 * Handle this by unregistering all objects associated
5282 * with this PCI device. Free those objects. Then finally
5283 * release PCI resources and disable device.
5284 *
5285 * LOCKING:
5286 * Inherited from PCI layer (may sleep).
5287 */
5288
5289void ata_pci_remove_one (struct pci_dev *pdev)
5290{
5291 struct device *dev = pci_dev_to_dev(pdev);
5292 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
Alan Cox17b14452005-09-15 15:44:00 +01005294 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 pci_release_regions(pdev);
5296 pci_disable_device(pdev);
5297 dev_set_drvdata(dev, NULL);
5298}
5299
5300/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005301int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302{
5303 unsigned long tmp = 0;
5304
5305 switch (bits->width) {
5306 case 1: {
5307 u8 tmp8 = 0;
5308 pci_read_config_byte(pdev, bits->reg, &tmp8);
5309 tmp = tmp8;
5310 break;
5311 }
5312 case 2: {
5313 u16 tmp16 = 0;
5314 pci_read_config_word(pdev, bits->reg, &tmp16);
5315 tmp = tmp16;
5316 break;
5317 }
5318 case 4: {
5319 u32 tmp32 = 0;
5320 pci_read_config_dword(pdev, bits->reg, &tmp32);
5321 tmp = tmp32;
5322 break;
5323 }
5324
5325 default:
5326 return -EINVAL;
5327 }
5328
5329 tmp &= bits->mask;
5330
5331 return (tmp == bits->val) ? 1 : 0;
5332}
Jens Axboe9b847542006-01-06 09:28:07 +01005333
5334int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5335{
5336 pci_save_state(pdev);
5337 pci_disable_device(pdev);
5338 pci_set_power_state(pdev, PCI_D3hot);
5339 return 0;
5340}
5341
5342int ata_pci_device_resume(struct pci_dev *pdev)
5343{
5344 pci_set_power_state(pdev, PCI_D0);
5345 pci_restore_state(pdev);
5346 pci_enable_device(pdev);
5347 pci_set_master(pdev);
5348 return 0;
5349}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350#endif /* CONFIG_PCI */
5351
5352
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353static int __init ata_init(void)
5354{
5355 ata_wq = create_workqueue("ata");
5356 if (!ata_wq)
5357 return -ENOMEM;
5358
5359 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5360 return 0;
5361}
5362
5363static void __exit ata_exit(void)
5364{
5365 destroy_workqueue(ata_wq);
5366}
5367
5368module_init(ata_init);
5369module_exit(ata_exit);
5370
Jeff Garzik67846b32005-10-05 02:58:32 -04005371static unsigned long ratelimit_time;
5372static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5373
5374int ata_ratelimit(void)
5375{
5376 int rc;
5377 unsigned long flags;
5378
5379 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5380
5381 if (time_after(jiffies, ratelimit_time)) {
5382 rc = 1;
5383 ratelimit_time = jiffies + (HZ/5);
5384 } else
5385 rc = 0;
5386
5387 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5388
5389 return rc;
5390}
5391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392/*
5393 * libata is essentially a library of internal helper functions for
5394 * low-level ATA host controller drivers. As such, the API/ABI is
5395 * likely to change as new drivers are added and updated.
5396 * Do not depend on ABI/API stability.
5397 */
5398
5399EXPORT_SYMBOL_GPL(ata_std_bios_param);
5400EXPORT_SYMBOL_GPL(ata_std_ports);
5401EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005402EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403EXPORT_SYMBOL_GPL(ata_sg_init);
5404EXPORT_SYMBOL_GPL(ata_sg_init_one);
5405EXPORT_SYMBOL_GPL(ata_qc_complete);
5406EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5407EXPORT_SYMBOL_GPL(ata_eng_timeout);
5408EXPORT_SYMBOL_GPL(ata_tf_load);
5409EXPORT_SYMBOL_GPL(ata_tf_read);
5410EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5411EXPORT_SYMBOL_GPL(ata_std_dev_select);
5412EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5413EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5414EXPORT_SYMBOL_GPL(ata_check_status);
5415EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416EXPORT_SYMBOL_GPL(ata_exec_command);
5417EXPORT_SYMBOL_GPL(ata_port_start);
5418EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005419EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420EXPORT_SYMBOL_GPL(ata_interrupt);
5421EXPORT_SYMBOL_GPL(ata_qc_prep);
5422EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5423EXPORT_SYMBOL_GPL(ata_bmdma_start);
5424EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5425EXPORT_SYMBOL_GPL(ata_bmdma_status);
5426EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5427EXPORT_SYMBOL_GPL(ata_port_probe);
5428EXPORT_SYMBOL_GPL(sata_phy_reset);
5429EXPORT_SYMBOL_GPL(__sata_phy_reset);
5430EXPORT_SYMBOL_GPL(ata_bus_reset);
5431EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005432EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5434EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5435EXPORT_SYMBOL_GPL(ata_scsi_error);
5436EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5437EXPORT_SYMBOL_GPL(ata_scsi_release);
5438EXPORT_SYMBOL_GPL(ata_host_intr);
5439EXPORT_SYMBOL_GPL(ata_dev_classify);
5440EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005441EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5443
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005444EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005445EXPORT_SYMBOL_GPL(ata_timing_compute);
5446EXPORT_SYMBOL_GPL(ata_timing_merge);
5447
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448#ifdef CONFIG_PCI
5449EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005450EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5452EXPORT_SYMBOL_GPL(ata_pci_init_one);
5453EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005454EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5455EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005457
5458EXPORT_SYMBOL_GPL(ata_device_suspend);
5459EXPORT_SYMBOL_GPL(ata_device_resume);
5460EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5461EXPORT_SYMBOL_GPL(ata_scsi_device_resume);