blob: 59a4a26bc13fdb495b8e138ab7c5813eeb270391 [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 *
536 * Converts a standard ATA taskfile to a Serial ATA
537 * FIS structure (Register - Host to Device).
538 *
539 * LOCKING:
540 * Inherited from caller.
541 */
542
Jeff Garzik057ace52005-10-22 14:27:05 -0400543void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 tf->command = fis[2]; /* status */
546 tf->feature = fis[3]; /* error */
547
548 tf->lbal = fis[4];
549 tf->lbam = fis[5];
550 tf->lbah = fis[6];
551 tf->device = fis[7];
552
553 tf->hob_lbal = fis[8];
554 tf->hob_lbam = fis[9];
555 tf->hob_lbah = fis[10];
556
557 tf->nsect = fis[12];
558 tf->hob_nsect = fis[13];
559}
560
Albert Lee8cbd6df2005-10-12 15:06:27 +0800561static const u8 ata_rw_cmds[] = {
562 /* pio multi */
563 ATA_CMD_READ_MULTI,
564 ATA_CMD_WRITE_MULTI,
565 ATA_CMD_READ_MULTI_EXT,
566 ATA_CMD_WRITE_MULTI_EXT,
567 /* pio */
568 ATA_CMD_PIO_READ,
569 ATA_CMD_PIO_WRITE,
570 ATA_CMD_PIO_READ_EXT,
571 ATA_CMD_PIO_WRITE_EXT,
572 /* dma */
573 ATA_CMD_READ,
574 ATA_CMD_WRITE,
575 ATA_CMD_READ_EXT,
576 ATA_CMD_WRITE_EXT
577};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800580 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
581 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800583 * Examine the device configuration and tf->flags to calculate
584 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
586 * LOCKING:
587 * caller.
588 */
Albert Lee8cbd6df2005-10-12 15:06:27 +0800589void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800591 struct ata_taskfile *tf = &qc->tf;
592 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Albert Lee8cbd6df2005-10-12 15:06:27 +0800594 int index, lba48, write;
595
596 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
597 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Albert Lee8cbd6df2005-10-12 15:06:27 +0800599 if (dev->flags & ATA_DFLAG_PIO) {
600 tf->protocol = ATA_PROT_PIO;
601 index = dev->multi_count ? 0 : 4;
602 } else {
603 tf->protocol = ATA_PROT_DMA;
604 index = 8;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Albert Lee8cbd6df2005-10-12 15:06:27 +0800607 tf->command = ata_rw_cmds[index + lba48 + write];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610static const char * xfer_mode_str[] = {
611 "UDMA/16",
612 "UDMA/25",
613 "UDMA/33",
614 "UDMA/44",
615 "UDMA/66",
616 "UDMA/100",
617 "UDMA/133",
618 "UDMA7",
619 "MWDMA0",
620 "MWDMA1",
621 "MWDMA2",
622 "PIO0",
623 "PIO1",
624 "PIO2",
625 "PIO3",
626 "PIO4",
627};
628
629/**
630 * ata_udma_string - convert UDMA bit offset to string
631 * @mask: mask of bits supported; only highest bit counts.
632 *
633 * Determine string which represents the highest speed
634 * (highest bit in @udma_mask).
635 *
636 * LOCKING:
637 * None.
638 *
639 * RETURNS:
640 * Constant C string representing highest speed listed in
641 * @udma_mask, or the constant C string "<n/a>".
642 */
643
644static const char *ata_mode_string(unsigned int mask)
645{
646 int i;
647
648 for (i = 7; i >= 0; i--)
649 if (mask & (1 << i))
650 goto out;
651 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
652 if (mask & (1 << i))
653 goto out;
654 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
655 if (mask & (1 << i))
656 goto out;
657
658 return "<n/a>";
659
660out:
661 return xfer_mode_str[i];
662}
663
664/**
665 * ata_pio_devchk - PATA device presence detection
666 * @ap: ATA channel to examine
667 * @device: Device to examine (starting at zero)
668 *
669 * This technique was originally described in
670 * Hale Landis's ATADRVR (www.ata-atapi.com), and
671 * later found its way into the ATA/ATAPI spec.
672 *
673 * Write a pattern to the ATA shadow registers,
674 * and if a device is present, it will respond by
675 * correctly storing and echoing back the
676 * ATA shadow register contents.
677 *
678 * LOCKING:
679 * caller.
680 */
681
682static unsigned int ata_pio_devchk(struct ata_port *ap,
683 unsigned int device)
684{
685 struct ata_ioports *ioaddr = &ap->ioaddr;
686 u8 nsect, lbal;
687
688 ap->ops->dev_select(ap, device);
689
690 outb(0x55, ioaddr->nsect_addr);
691 outb(0xaa, ioaddr->lbal_addr);
692
693 outb(0xaa, ioaddr->nsect_addr);
694 outb(0x55, ioaddr->lbal_addr);
695
696 outb(0x55, ioaddr->nsect_addr);
697 outb(0xaa, ioaddr->lbal_addr);
698
699 nsect = inb(ioaddr->nsect_addr);
700 lbal = inb(ioaddr->lbal_addr);
701
702 if ((nsect == 0x55) && (lbal == 0xaa))
703 return 1; /* we found a device */
704
705 return 0; /* nothing found */
706}
707
708/**
709 * ata_mmio_devchk - PATA device presence detection
710 * @ap: ATA channel to examine
711 * @device: Device to examine (starting at zero)
712 *
713 * This technique was originally described in
714 * Hale Landis's ATADRVR (www.ata-atapi.com), and
715 * later found its way into the ATA/ATAPI spec.
716 *
717 * Write a pattern to the ATA shadow registers,
718 * and if a device is present, it will respond by
719 * correctly storing and echoing back the
720 * ATA shadow register contents.
721 *
722 * LOCKING:
723 * caller.
724 */
725
726static unsigned int ata_mmio_devchk(struct ata_port *ap,
727 unsigned int device)
728{
729 struct ata_ioports *ioaddr = &ap->ioaddr;
730 u8 nsect, lbal;
731
732 ap->ops->dev_select(ap, device);
733
734 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
735 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
736
737 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
738 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
739
740 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
741 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
742
743 nsect = readb((void __iomem *) ioaddr->nsect_addr);
744 lbal = readb((void __iomem *) ioaddr->lbal_addr);
745
746 if ((nsect == 0x55) && (lbal == 0xaa))
747 return 1; /* we found a device */
748
749 return 0; /* nothing found */
750}
751
752/**
753 * ata_devchk - PATA device presence detection
754 * @ap: ATA channel to examine
755 * @device: Device to examine (starting at zero)
756 *
757 * Dispatch ATA device presence detection, depending
758 * on whether we are using PIO or MMIO to talk to the
759 * ATA shadow registers.
760 *
761 * LOCKING:
762 * caller.
763 */
764
765static unsigned int ata_devchk(struct ata_port *ap,
766 unsigned int device)
767{
768 if (ap->flags & ATA_FLAG_MMIO)
769 return ata_mmio_devchk(ap, device);
770 return ata_pio_devchk(ap, device);
771}
772
773/**
774 * ata_dev_classify - determine device type based on ATA-spec signature
775 * @tf: ATA taskfile register set for device to be identified
776 *
777 * Determine from taskfile register contents whether a device is
778 * ATA or ATAPI, as per "Signature and persistence" section
779 * of ATA/PI spec (volume 1, sect 5.14).
780 *
781 * LOCKING:
782 * None.
783 *
784 * RETURNS:
785 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
786 * the event of failure.
787 */
788
Jeff Garzik057ace52005-10-22 14:27:05 -0400789unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 /* Apple's open source Darwin code hints that some devices only
792 * put a proper signature into the LBA mid/high registers,
793 * So, we only check those. It's sufficient for uniqueness.
794 */
795
796 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
797 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
798 DPRINTK("found ATA device by sig\n");
799 return ATA_DEV_ATA;
800 }
801
802 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
803 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
804 DPRINTK("found ATAPI device by sig\n");
805 return ATA_DEV_ATAPI;
806 }
807
808 DPRINTK("unknown device\n");
809 return ATA_DEV_UNKNOWN;
810}
811
812/**
813 * ata_dev_try_classify - Parse returned ATA device signature
814 * @ap: ATA channel to examine
815 * @device: Device to examine (starting at zero)
816 *
817 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
818 * an ATA/ATAPI-defined set of values is placed in the ATA
819 * shadow registers, indicating the results of device detection
820 * and diagnostics.
821 *
822 * Select the ATA device, and read the values from the ATA shadow
823 * registers. Then parse according to the Error register value,
824 * and the spec-defined values examined by ata_dev_classify().
825 *
826 * LOCKING:
827 * caller.
828 */
829
830static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
831{
832 struct ata_device *dev = &ap->device[device];
833 struct ata_taskfile tf;
834 unsigned int class;
835 u8 err;
836
837 ap->ops->dev_select(ap, device);
838
839 memset(&tf, 0, sizeof(tf));
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400842 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 dev->class = ATA_DEV_NONE;
845
846 /* see if device passed diags */
847 if (err == 1)
848 /* do nothing */ ;
849 else if ((device == 0) && (err == 0x81))
850 /* do nothing */ ;
851 else
852 return err;
853
854 /* determine if device if ATA or ATAPI */
855 class = ata_dev_classify(&tf);
856 if (class == ATA_DEV_UNKNOWN)
857 return err;
858 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
859 return err;
860
861 dev->class = class;
862
863 return err;
864}
865
866/**
867 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
868 * @id: IDENTIFY DEVICE results we will examine
869 * @s: string into which data is output
870 * @ofs: offset into identify device page
871 * @len: length of string to return. must be an even number.
872 *
873 * The strings in the IDENTIFY DEVICE page are broken up into
874 * 16-bit chunks. Run through the string, and output each
875 * 8-bit chunk linearly, regardless of platform.
876 *
877 * LOCKING:
878 * caller.
879 */
880
Jeff Garzik057ace52005-10-22 14:27:05 -0400881void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 unsigned int ofs, unsigned int len)
883{
884 unsigned int c;
885
886 while (len > 0) {
887 c = id[ofs] >> 8;
888 *s = c;
889 s++;
890
891 c = id[ofs] & 0xff;
892 *s = c;
893 s++;
894
895 ofs++;
896 len -= 2;
897 }
898}
899
Edward Falk0baab862005-06-02 18:17:13 -0400900
901/**
902 * ata_noop_dev_select - Select device 0/1 on ATA bus
903 * @ap: ATA channel to manipulate
904 * @device: ATA device (numbered from zero) to select
905 *
906 * This function performs no actual function.
907 *
908 * May be used as the dev_select() entry in ata_port_operations.
909 *
910 * LOCKING:
911 * caller.
912 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
914{
915}
916
Edward Falk0baab862005-06-02 18:17:13 -0400917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/**
919 * ata_std_dev_select - Select device 0/1 on ATA bus
920 * @ap: ATA channel to manipulate
921 * @device: ATA device (numbered from zero) to select
922 *
923 * Use the method defined in the ATA specification to
924 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400925 * ATA channel. Works with both PIO and MMIO.
926 *
927 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 *
929 * LOCKING:
930 * caller.
931 */
932
933void ata_std_dev_select (struct ata_port *ap, unsigned int device)
934{
935 u8 tmp;
936
937 if (device == 0)
938 tmp = ATA_DEVICE_OBS;
939 else
940 tmp = ATA_DEVICE_OBS | ATA_DEV1;
941
942 if (ap->flags & ATA_FLAG_MMIO) {
943 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
944 } else {
945 outb(tmp, ap->ioaddr.device_addr);
946 }
947 ata_pause(ap); /* needed; also flushes, for mmio */
948}
949
950/**
951 * ata_dev_select - Select device 0/1 on ATA bus
952 * @ap: ATA channel to manipulate
953 * @device: ATA device (numbered from zero) to select
954 * @wait: non-zero to wait for Status register BSY bit to clear
955 * @can_sleep: non-zero if context allows sleeping
956 *
957 * Use the method defined in the ATA specification to
958 * make either device 0, or device 1, active on the
959 * ATA channel.
960 *
961 * This is a high-level version of ata_std_dev_select(),
962 * which additionally provides the services of inserting
963 * the proper pauses and status polling, where needed.
964 *
965 * LOCKING:
966 * caller.
967 */
968
969void ata_dev_select(struct ata_port *ap, unsigned int device,
970 unsigned int wait, unsigned int can_sleep)
971{
972 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
973 ap->id, device, wait);
974
975 if (wait)
976 ata_wait_idle(ap);
977
978 ap->ops->dev_select(ap, device);
979
980 if (wait) {
981 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
982 msleep(150);
983 ata_wait_idle(ap);
984 }
985}
986
987/**
988 * ata_dump_id - IDENTIFY DEVICE info debugging output
989 * @dev: Device whose IDENTIFY DEVICE page we will dump
990 *
991 * Dump selected 16-bit words from a detected device's
992 * IDENTIFY PAGE page.
993 *
994 * LOCKING:
995 * caller.
996 */
997
Jeff Garzik057ace52005-10-22 14:27:05 -0400998static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 DPRINTK("49==0x%04x "
1001 "53==0x%04x "
1002 "63==0x%04x "
1003 "64==0x%04x "
1004 "75==0x%04x \n",
1005 dev->id[49],
1006 dev->id[53],
1007 dev->id[63],
1008 dev->id[64],
1009 dev->id[75]);
1010 DPRINTK("80==0x%04x "
1011 "81==0x%04x "
1012 "82==0x%04x "
1013 "83==0x%04x "
1014 "84==0x%04x \n",
1015 dev->id[80],
1016 dev->id[81],
1017 dev->id[82],
1018 dev->id[83],
1019 dev->id[84]);
1020 DPRINTK("88==0x%04x "
1021 "93==0x%04x\n",
1022 dev->id[88],
1023 dev->id[93]);
1024}
1025
Alan Cox11e29e22005-10-21 18:46:32 -04001026/*
1027 * Compute the PIO modes available for this device. This is not as
1028 * trivial as it seems if we must consider early devices correctly.
1029 *
1030 * FIXME: pre IDE drive timing (do we care ?).
1031 */
1032
Jeff Garzik057ace52005-10-22 14:27:05 -04001033static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001034{
1035 u16 modes;
1036
1037 /* Usual case. Word 53 indicates word 88 is valid */
1038 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1039 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1040 modes <<= 3;
1041 modes |= 0x7;
1042 return modes;
1043 }
1044
1045 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1046 for the maximum. Turn it into a mask and return it */
1047 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1048 return modes;
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/**
1052 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1053 * @ap: port on which device we wish to probe resides
1054 * @device: device bus address, starting at zero
1055 *
1056 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1057 * command, and read back the 512-byte device information page.
1058 * The device information page is fed to us via the standard
1059 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1060 * using standard PIO-IN paths)
1061 *
1062 * After reading the device information page, we use several
1063 * bits of information from it to initialize data structures
1064 * that will be used during the lifetime of the ata_device.
1065 * Other data from the info page is used to disqualify certain
1066 * older ATA devices we do not wish to support.
1067 *
1068 * LOCKING:
1069 * Inherited from caller. Some functions called by this function
1070 * obtain the host_set lock.
1071 */
1072
1073static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1074{
1075 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001076 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 u16 tmp;
1078 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 unsigned int using_edd;
1080 DECLARE_COMPLETION(wait);
1081 struct ata_queued_cmd *qc;
1082 unsigned long flags;
1083 int rc;
1084
1085 if (!ata_dev_present(dev)) {
1086 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1087 ap->id, device);
1088 return;
1089 }
1090
1091 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1092 using_edd = 0;
1093 else
1094 using_edd = 1;
1095
1096 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1097
1098 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1099 dev->class == ATA_DEV_NONE);
1100
1101 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1102
1103 qc = ata_qc_new_init(ap, dev);
1104 BUG_ON(qc == NULL);
1105
1106 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1107 qc->dma_dir = DMA_FROM_DEVICE;
1108 qc->tf.protocol = ATA_PROT_PIO;
1109 qc->nsect = 1;
1110
1111retry:
1112 if (dev->class == ATA_DEV_ATA) {
1113 qc->tf.command = ATA_CMD_ID_ATA;
1114 DPRINTK("do ATA identify\n");
1115 } else {
1116 qc->tf.command = ATA_CMD_ID_ATAPI;
1117 DPRINTK("do ATAPI identify\n");
1118 }
1119
1120 qc->waiting = &wait;
1121 qc->complete_fn = ata_qc_complete_noop;
1122
1123 spin_lock_irqsave(&ap->host_set->lock, flags);
1124 rc = ata_qc_issue(qc);
1125 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1126
1127 if (rc)
1128 goto err_out;
1129 else
1130 wait_for_completion(&wait);
1131
Jeff Garzik0169e282005-10-29 21:25:10 -04001132 spin_lock_irqsave(&ap->host_set->lock, flags);
1133 ap->ops->tf_read(ap, &qc->tf);
1134 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1135
1136 if (qc->tf.command & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /*
1138 * arg! EDD works for all test cases, but seems to return
1139 * the ATA signature for some ATAPI devices. Until the
1140 * reason for this is found and fixed, we fix up the mess
1141 * here. If IDENTIFY DEVICE returns command aborted
1142 * (as ATAPI devices do), then we issue an
1143 * IDENTIFY PACKET DEVICE.
1144 *
1145 * ATA software reset (SRST, the default) does not appear
1146 * to have this problem.
1147 */
1148 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
Jeff Garzik0169e282005-10-29 21:25:10 -04001149 u8 err = qc->tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (err & ATA_ABORTED) {
1151 dev->class = ATA_DEV_ATAPI;
1152 qc->cursg = 0;
1153 qc->cursg_ofs = 0;
1154 qc->cursect = 0;
1155 qc->nsect = 1;
1156 goto retry;
1157 }
1158 }
1159 goto err_out;
1160 }
1161
1162 swap_buf_le16(dev->id, ATA_ID_WORDS);
1163
1164 /* print device capabilities */
1165 printk(KERN_DEBUG "ata%u: dev %u cfg "
1166 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1167 ap->id, device, dev->id[49],
1168 dev->id[82], dev->id[83], dev->id[84],
1169 dev->id[85], dev->id[86], dev->id[87],
1170 dev->id[88]);
1171
1172 /*
1173 * common ATA, ATAPI feature tests
1174 */
1175
Albert Lee8bf62ece2005-05-12 15:29:42 -04001176 /* we require DMA support (bits 8 of word 49) */
1177 if (!ata_id_has_dma(dev->id)) {
1178 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto err_out_nosup;
1180 }
1181
1182 /* quick-n-dirty find max transfer mode; for printk only */
1183 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1184 if (!xfer_modes)
1185 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001186 if (!xfer_modes)
1187 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 ata_dump_id(dev);
1190
1191 /* ATA-specific feature tests */
1192 if (dev->class == ATA_DEV_ATA) {
1193 if (!ata_id_is_ata(dev->id)) /* sanity check */
1194 goto err_out_nosup;
1195
Albert Lee8bf62ece2005-05-12 15:29:42 -04001196 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001198 for (major_version = 14; major_version >= 1; major_version--)
1199 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
1201
Albert Lee8bf62ece2005-05-12 15:29:42 -04001202 /*
1203 * The exact sequence expected by certain pre-ATA4 drives is:
1204 * SRST RESET
1205 * IDENTIFY
1206 * INITIALIZE DEVICE PARAMETERS
1207 * anything else..
1208 * Some drives were very specific about that exact sequence.
1209 */
Albert Lee59a10b12005-10-12 15:09:42 +08001210 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001211 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Albert Lee59a10b12005-10-12 15:09:42 +08001213 /* current CHS translation info (id[53-58]) might be
1214 * changed. reread the identify device info.
1215 */
1216 ata_dev_reread_id(ap, dev);
1217 }
1218
Albert Lee8bf62ece2005-05-12 15:29:42 -04001219 if (ata_id_has_lba(dev->id)) {
1220 dev->flags |= ATA_DFLAG_LBA;
1221
1222 if (ata_id_has_lba48(dev->id)) {
1223 dev->flags |= ATA_DFLAG_LBA48;
1224 dev->n_sectors = ata_id_u64(dev->id, 100);
1225 } else {
1226 dev->n_sectors = ata_id_u32(dev->id, 60);
1227 }
1228
1229 /* print device info to dmesg */
1230 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1231 ap->id, device,
1232 major_version,
1233 ata_mode_string(xfer_modes),
1234 (unsigned long long)dev->n_sectors,
1235 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1236 } else {
1237 /* CHS */
1238
1239 /* Default translation */
1240 dev->cylinders = dev->id[1];
1241 dev->heads = dev->id[3];
1242 dev->sectors = dev->id[6];
1243 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1244
1245 if (ata_id_current_chs_valid(dev->id)) {
1246 /* Current CHS translation is valid. */
1247 dev->cylinders = dev->id[54];
1248 dev->heads = dev->id[55];
1249 dev->sectors = dev->id[56];
1250
1251 dev->n_sectors = ata_id_u32(dev->id, 57);
1252 }
1253
1254 /* print device info to dmesg */
1255 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1256 ap->id, device,
1257 major_version,
1258 ata_mode_string(xfer_modes),
1259 (unsigned long long)dev->n_sectors,
1260 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
Albert Lee07f6f7d2005-11-01 19:33:20 +08001264 if (dev->id[59] & 0x100) {
1265 dev->multi_count = dev->id[59] & 0xff;
1266 DPRINTK("ata%u: dev %u multi count %u\n",
1267 ap->id, device, dev->multi_count);
1268 }
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272
1273 /* ATAPI-specific feature tests */
1274 else {
1275 if (ata_id_is_ata(dev->id)) /* sanity check */
1276 goto err_out_nosup;
1277
1278 rc = atapi_cdb_len(dev->id);
1279 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1280 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1281 goto err_out_nosup;
1282 }
1283 ap->cdb_len = (unsigned int) rc;
1284 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1285
Albert Lee312f7da2005-09-27 17:38:03 +08001286 if (ata_id_cdb_intr(dev->id))
1287 dev->flags |= ATA_DFLAG_CDB_INTR;
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /* print device info to dmesg */
1290 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1291 ap->id, device,
1292 ata_mode_string(xfer_modes));
1293 }
1294
1295 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1296 return;
1297
1298err_out_nosup:
1299 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1300 ap->id, device);
1301err_out:
1302 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1303 DPRINTK("EXIT, err\n");
1304}
1305
Brad Campbell6f2f3812005-05-12 15:07:47 -04001306
Jeff Garzik057ace52005-10-22 14:27:05 -04001307static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001308{
1309 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1310}
1311
1312/**
1313 * ata_dev_config - Run device specific handlers and check for
1314 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001315 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001316 * @i: Device
1317 *
1318 * LOCKING:
1319 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001320
Brad Campbell6f2f3812005-05-12 15:07:47 -04001321void ata_dev_config(struct ata_port *ap, unsigned int i)
1322{
1323 /* limit bridge transfers to udma5, 200 sectors */
1324 if (ata_dev_knobble(ap)) {
1325 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1326 ap->id, ap->device->devno);
1327 ap->udma_mask &= ATA_UDMA5;
1328 ap->host->max_sectors = ATA_MAX_SECTORS;
1329 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1330 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1331 }
1332
1333 if (ap->ops->dev_config)
1334 ap->ops->dev_config(ap, &ap->device[i]);
1335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337/**
1338 * ata_bus_probe - Reset and probe ATA bus
1339 * @ap: Bus to probe
1340 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001341 * Master ATA bus probing function. Initiates a hardware-dependent
1342 * bus reset, then attempts to identify any devices found on
1343 * the bus.
1344 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001346 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 *
1348 * RETURNS:
1349 * Zero on success, non-zero on error.
1350 */
1351
1352static int ata_bus_probe(struct ata_port *ap)
1353{
1354 unsigned int i, found = 0;
1355
1356 ap->ops->phy_reset(ap);
1357 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1358 goto err_out;
1359
1360 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1361 ata_dev_identify(ap, i);
1362 if (ata_dev_present(&ap->device[i])) {
1363 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001364 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366 }
1367
1368 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1369 goto err_out_disable;
1370
1371 ata_set_mode(ap);
1372 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1373 goto err_out_disable;
1374
1375 return 0;
1376
1377err_out_disable:
1378 ap->ops->port_disable(ap);
1379err_out:
1380 return -1;
1381}
1382
1383/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001384 * ata_port_probe - Mark port as enabled
1385 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001387 * Modify @ap data structure such that the system
1388 * thinks that the entire port is enabled.
1389 *
1390 * LOCKING: host_set lock, or some other form of
1391 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 */
1393
1394void ata_port_probe(struct ata_port *ap)
1395{
1396 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1397}
1398
1399/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001400 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1401 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001403 * This function issues commands to standard SATA Sxxx
1404 * PHY registers, to wake up the phy (and device), and
1405 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 *
1407 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001408 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 *
1410 */
1411void __sata_phy_reset(struct ata_port *ap)
1412{
1413 u32 sstatus;
1414 unsigned long timeout = jiffies + (HZ * 5);
1415
1416 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001417 /* issue phy wake/reset */
1418 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001419 /* Couldn't find anything in SATA I/II specs, but
1420 * AHCI-1.1 10.4.2 says at least 1 ms. */
1421 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001423 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 /* wait for phy to become ready, if necessary */
1426 do {
1427 msleep(200);
1428 sstatus = scr_read(ap, SCR_STATUS);
1429 if ((sstatus & 0xf) != 1)
1430 break;
1431 } while (time_before(jiffies, timeout));
1432
1433 /* TODO: phy layer with polling, timeouts, etc. */
1434 if (sata_dev_present(ap))
1435 ata_port_probe(ap);
1436 else {
1437 sstatus = scr_read(ap, SCR_STATUS);
1438 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1439 ap->id, sstatus);
1440 ata_port_disable(ap);
1441 }
1442
1443 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1444 return;
1445
1446 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1447 ata_port_disable(ap);
1448 return;
1449 }
1450
1451 ap->cbl = ATA_CBL_SATA;
1452}
1453
1454/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001455 * sata_phy_reset - Reset SATA bus.
1456 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001458 * This function resets the SATA bus, and then probes
1459 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 *
1461 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001462 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 *
1464 */
1465void sata_phy_reset(struct ata_port *ap)
1466{
1467 __sata_phy_reset(ap);
1468 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1469 return;
1470 ata_bus_reset(ap);
1471}
1472
1473/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001474 * ata_port_disable - Disable port.
1475 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001477 * Modify @ap data structure such that the system
1478 * thinks that the entire port is disabled, and should
1479 * never attempt to probe or communicate with devices
1480 * on this port.
1481 *
1482 * LOCKING: host_set lock, or some other form of
1483 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 */
1485
1486void ata_port_disable(struct ata_port *ap)
1487{
1488 ap->device[0].class = ATA_DEV_NONE;
1489 ap->device[1].class = ATA_DEV_NONE;
1490 ap->flags |= ATA_FLAG_PORT_DISABLED;
1491}
1492
Alan Cox452503f2005-10-21 19:01:32 -04001493/*
1494 * This mode timing computation functionality is ported over from
1495 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1496 */
1497/*
1498 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1499 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1500 * for PIO 5, which is a nonstandard extension and UDMA6, which
1501 * is currently supported only by Maxtor drives.
1502 */
1503
1504static const struct ata_timing ata_timing[] = {
1505
1506 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1507 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1508 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1509 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1510
1511 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1512 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1513 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1514
1515/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1516
1517 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1518 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1519 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1520
1521 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1522 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1523 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1524
1525/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1526 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1527 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1528
1529 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1530 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1531 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1532
1533/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1534
1535 { 0xFF }
1536};
1537
1538#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1539#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1540
1541static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1542{
1543 q->setup = EZ(t->setup * 1000, T);
1544 q->act8b = EZ(t->act8b * 1000, T);
1545 q->rec8b = EZ(t->rec8b * 1000, T);
1546 q->cyc8b = EZ(t->cyc8b * 1000, T);
1547 q->active = EZ(t->active * 1000, T);
1548 q->recover = EZ(t->recover * 1000, T);
1549 q->cycle = EZ(t->cycle * 1000, T);
1550 q->udma = EZ(t->udma * 1000, UT);
1551}
1552
1553void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1554 struct ata_timing *m, unsigned int what)
1555{
1556 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1557 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1558 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1559 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1560 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1561 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1562 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1563 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1564}
1565
1566static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1567{
1568 const struct ata_timing *t;
1569
1570 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001571 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001572 return NULL;
1573 return t;
1574}
1575
1576int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1577 struct ata_timing *t, int T, int UT)
1578{
1579 const struct ata_timing *s;
1580 struct ata_timing p;
1581
1582 /*
1583 * Find the mode.
1584 */
1585
1586 if (!(s = ata_timing_find_mode(speed)))
1587 return -EINVAL;
1588
1589 /*
1590 * If the drive is an EIDE drive, it can tell us it needs extended
1591 * PIO/MW_DMA cycle timing.
1592 */
1593
1594 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1595 memset(&p, 0, sizeof(p));
1596 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1597 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1598 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1599 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1600 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1601 }
1602 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1603 }
1604
1605 /*
1606 * Convert the timing to bus clock counts.
1607 */
1608
1609 ata_timing_quantize(s, t, T, UT);
1610
1611 /*
1612 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1613 * and some other commands. We have to ensure that the DMA cycle timing is
1614 * slower/equal than the fastest PIO timing.
1615 */
1616
1617 if (speed > XFER_PIO_4) {
1618 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1619 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1620 }
1621
1622 /*
1623 * Lenghten active & recovery time so that cycle time is correct.
1624 */
1625
1626 if (t->act8b + t->rec8b < t->cyc8b) {
1627 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1628 t->rec8b = t->cyc8b - t->act8b;
1629 }
1630
1631 if (t->active + t->recover < t->cycle) {
1632 t->active += (t->cycle - (t->active + t->recover)) / 2;
1633 t->recover = t->cycle - t->active;
1634 }
1635
1636 return 0;
1637}
1638
Jeff Garzik057ace52005-10-22 14:27:05 -04001639static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 unsigned int shift;
1641 u8 base;
1642} xfer_mode_classes[] = {
1643 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1644 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1645 { ATA_SHIFT_PIO, XFER_PIO_0 },
1646};
1647
1648static inline u8 base_from_shift(unsigned int shift)
1649{
1650 int i;
1651
1652 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1653 if (xfer_mode_classes[i].shift == shift)
1654 return xfer_mode_classes[i].base;
1655
1656 return 0xff;
1657}
1658
1659static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1660{
1661 int ofs, idx;
1662 u8 base;
1663
1664 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1665 return;
1666
1667 if (dev->xfer_shift == ATA_SHIFT_PIO)
1668 dev->flags |= ATA_DFLAG_PIO;
1669
1670 ata_dev_set_xfermode(ap, dev);
1671
1672 base = base_from_shift(dev->xfer_shift);
1673 ofs = dev->xfer_mode - base;
1674 idx = ofs + dev->xfer_shift;
1675 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1676
1677 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1678 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1679
1680 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1681 ap->id, dev->devno, xfer_mode_str[idx]);
1682}
1683
1684static int ata_host_set_pio(struct ata_port *ap)
1685{
1686 unsigned int mask;
1687 int x, i;
1688 u8 base, xfer_mode;
1689
1690 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1691 x = fgb(mask);
1692 if (x < 0) {
1693 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1694 return -1;
1695 }
1696
1697 base = base_from_shift(ATA_SHIFT_PIO);
1698 xfer_mode = base + x;
1699
1700 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1701 (int)base, (int)xfer_mode, mask, x);
1702
1703 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1704 struct ata_device *dev = &ap->device[i];
1705 if (ata_dev_present(dev)) {
1706 dev->pio_mode = xfer_mode;
1707 dev->xfer_mode = xfer_mode;
1708 dev->xfer_shift = ATA_SHIFT_PIO;
1709 if (ap->ops->set_piomode)
1710 ap->ops->set_piomode(ap, dev);
1711 }
1712 }
1713
1714 return 0;
1715}
1716
1717static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1718 unsigned int xfer_shift)
1719{
1720 int i;
1721
1722 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1723 struct ata_device *dev = &ap->device[i];
1724 if (ata_dev_present(dev)) {
1725 dev->dma_mode = xfer_mode;
1726 dev->xfer_mode = xfer_mode;
1727 dev->xfer_shift = xfer_shift;
1728 if (ap->ops->set_dmamode)
1729 ap->ops->set_dmamode(ap, dev);
1730 }
1731 }
1732}
1733
1734/**
1735 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1736 * @ap: port on which timings will be programmed
1737 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001738 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1739 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001741 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 *
1743 */
1744static void ata_set_mode(struct ata_port *ap)
1745{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001746 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 u8 xfer_mode;
1748 int rc;
1749
1750 /* step 1: always set host PIO timings */
1751 rc = ata_host_set_pio(ap);
1752 if (rc)
1753 goto err_out;
1754
1755 /* step 2: choose the best data xfer mode */
1756 xfer_mode = xfer_shift = 0;
1757 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1758 if (rc)
1759 goto err_out;
1760
1761 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1762 if (xfer_shift != ATA_SHIFT_PIO)
1763 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1764
1765 /* step 4: update devices' xfer mode */
1766 ata_dev_set_mode(ap, &ap->device[0]);
1767 ata_dev_set_mode(ap, &ap->device[1]);
1768
1769 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1770 return;
1771
1772 if (ap->ops->post_set_mode)
1773 ap->ops->post_set_mode(ap);
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return;
1776
1777err_out:
1778 ata_port_disable(ap);
1779}
1780
1781/**
1782 * ata_busy_sleep - sleep until BSY clears, or timeout
1783 * @ap: port containing status register to be polled
1784 * @tmout_pat: impatience timeout
1785 * @tmout: overall timeout
1786 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001787 * Sleep until ATA Status register bit BSY clears,
1788 * or a timeout occurs.
1789 *
1790 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 *
1792 */
1793
1794static unsigned int ata_busy_sleep (struct ata_port *ap,
1795 unsigned long tmout_pat,
1796 unsigned long tmout)
1797{
1798 unsigned long timer_start, timeout;
1799 u8 status;
1800
1801 status = ata_busy_wait(ap, ATA_BUSY, 300);
1802 timer_start = jiffies;
1803 timeout = timer_start + tmout_pat;
1804 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1805 msleep(50);
1806 status = ata_busy_wait(ap, ATA_BUSY, 3);
1807 }
1808
1809 if (status & ATA_BUSY)
1810 printk(KERN_WARNING "ata%u is slow to respond, "
1811 "please be patient\n", ap->id);
1812
1813 timeout = timer_start + tmout;
1814 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1815 msleep(50);
1816 status = ata_chk_status(ap);
1817 }
1818
1819 if (status & ATA_BUSY) {
1820 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1821 ap->id, tmout / HZ);
1822 return 1;
1823 }
1824
1825 return 0;
1826}
1827
1828static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1829{
1830 struct ata_ioports *ioaddr = &ap->ioaddr;
1831 unsigned int dev0 = devmask & (1 << 0);
1832 unsigned int dev1 = devmask & (1 << 1);
1833 unsigned long timeout;
1834
1835 /* if device 0 was found in ata_devchk, wait for its
1836 * BSY bit to clear
1837 */
1838 if (dev0)
1839 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1840
1841 /* if device 1 was found in ata_devchk, wait for
1842 * register access, then wait for BSY to clear
1843 */
1844 timeout = jiffies + ATA_TMOUT_BOOT;
1845 while (dev1) {
1846 u8 nsect, lbal;
1847
1848 ap->ops->dev_select(ap, 1);
1849 if (ap->flags & ATA_FLAG_MMIO) {
1850 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1851 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1852 } else {
1853 nsect = inb(ioaddr->nsect_addr);
1854 lbal = inb(ioaddr->lbal_addr);
1855 }
1856 if ((nsect == 1) && (lbal == 1))
1857 break;
1858 if (time_after(jiffies, timeout)) {
1859 dev1 = 0;
1860 break;
1861 }
1862 msleep(50); /* give drive a breather */
1863 }
1864 if (dev1)
1865 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1866
1867 /* is all this really necessary? */
1868 ap->ops->dev_select(ap, 0);
1869 if (dev1)
1870 ap->ops->dev_select(ap, 1);
1871 if (dev0)
1872 ap->ops->dev_select(ap, 0);
1873}
1874
1875/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001876 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1877 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001879 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1880 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 *
1882 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001883 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001884 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 *
1886 */
1887
1888static unsigned int ata_bus_edd(struct ata_port *ap)
1889{
1890 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001891 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 /* set up execute-device-diag (bus reset) taskfile */
1894 /* also, take interrupts to a known state (disabled) */
1895 DPRINTK("execute-device-diag\n");
1896 ata_tf_init(ap, &tf, 0);
1897 tf.ctl |= ATA_NIEN;
1898 tf.command = ATA_CMD_EDD;
1899 tf.protocol = ATA_PROT_NODATA;
1900
1901 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001902 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001904 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 /* spec says at least 2ms. but who knows with those
1907 * crazy ATAPI devices...
1908 */
1909 msleep(150);
1910
1911 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1912}
1913
1914static unsigned int ata_bus_softreset(struct ata_port *ap,
1915 unsigned int devmask)
1916{
1917 struct ata_ioports *ioaddr = &ap->ioaddr;
1918
1919 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1920
1921 /* software reset. causes dev0 to be selected */
1922 if (ap->flags & ATA_FLAG_MMIO) {
1923 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1924 udelay(20); /* FIXME: flush */
1925 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1926 udelay(20); /* FIXME: flush */
1927 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1928 } else {
1929 outb(ap->ctl, ioaddr->ctl_addr);
1930 udelay(10);
1931 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1932 udelay(10);
1933 outb(ap->ctl, ioaddr->ctl_addr);
1934 }
1935
1936 /* spec mandates ">= 2ms" before checking status.
1937 * We wait 150ms, because that was the magic delay used for
1938 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1939 * between when the ATA command register is written, and then
1940 * status is checked. Because waiting for "a while" before
1941 * checking status is fine, post SRST, we perform this magic
1942 * delay here as well.
1943 */
1944 msleep(150);
1945
1946 ata_bus_post_reset(ap, devmask);
1947
1948 return 0;
1949}
1950
1951/**
1952 * ata_bus_reset - reset host port and associated ATA channel
1953 * @ap: port to reset
1954 *
1955 * This is typically the first time we actually start issuing
1956 * commands to the ATA channel. We wait for BSY to clear, then
1957 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1958 * result. Determine what devices, if any, are on the channel
1959 * by looking at the device 0/1 error register. Look at the signature
1960 * stored in each device's taskfile registers, to determine if
1961 * the device is ATA or ATAPI.
1962 *
1963 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001964 * PCI/etc. bus probe sem.
1965 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 *
1967 * SIDE EFFECTS:
1968 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1969 */
1970
1971void ata_bus_reset(struct ata_port *ap)
1972{
1973 struct ata_ioports *ioaddr = &ap->ioaddr;
1974 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1975 u8 err;
1976 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1977
1978 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1979
1980 /* determine if device 0/1 are present */
1981 if (ap->flags & ATA_FLAG_SATA_RESET)
1982 dev0 = 1;
1983 else {
1984 dev0 = ata_devchk(ap, 0);
1985 if (slave_possible)
1986 dev1 = ata_devchk(ap, 1);
1987 }
1988
1989 if (dev0)
1990 devmask |= (1 << 0);
1991 if (dev1)
1992 devmask |= (1 << 1);
1993
1994 /* select device 0 again */
1995 ap->ops->dev_select(ap, 0);
1996
1997 /* issue bus reset */
1998 if (ap->flags & ATA_FLAG_SRST)
1999 rc = ata_bus_softreset(ap, devmask);
2000 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2001 /* set up device control */
2002 if (ap->flags & ATA_FLAG_MMIO)
2003 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2004 else
2005 outb(ap->ctl, ioaddr->ctl_addr);
2006 rc = ata_bus_edd(ap);
2007 }
2008
2009 if (rc)
2010 goto err_out;
2011
2012 /*
2013 * determine by signature whether we have ATA or ATAPI devices
2014 */
2015 err = ata_dev_try_classify(ap, 0);
2016 if ((slave_possible) && (err != 0x81))
2017 ata_dev_try_classify(ap, 1);
2018
2019 /* re-enable interrupts */
2020 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2021 ata_irq_on(ap);
2022
2023 /* is double-select really necessary? */
2024 if (ap->device[1].class != ATA_DEV_NONE)
2025 ap->ops->dev_select(ap, 1);
2026 if (ap->device[0].class != ATA_DEV_NONE)
2027 ap->ops->dev_select(ap, 0);
2028
2029 /* if no devices were detected, disable this port */
2030 if ((ap->device[0].class == ATA_DEV_NONE) &&
2031 (ap->device[1].class == ATA_DEV_NONE))
2032 goto err_out;
2033
2034 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2035 /* set up device control for ATA_FLAG_SATA_RESET */
2036 if (ap->flags & ATA_FLAG_MMIO)
2037 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2038 else
2039 outb(ap->ctl, ioaddr->ctl_addr);
2040 }
2041
2042 DPRINTK("EXIT\n");
2043 return;
2044
2045err_out:
2046 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2047 ap->ops->port_disable(ap);
2048
2049 DPRINTK("EXIT\n");
2050}
2051
Jeff Garzik057ace52005-10-22 14:27:05 -04002052static void ata_pr_blacklisted(const struct ata_port *ap,
2053 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2056 ap->id, dev->devno);
2057}
2058
2059static const char * ata_dma_blacklist [] = {
2060 "WDC AC11000H",
2061 "WDC AC22100H",
2062 "WDC AC32500H",
2063 "WDC AC33100H",
2064 "WDC AC31600H",
2065 "WDC AC32100H",
2066 "WDC AC23200L",
2067 "Compaq CRD-8241B",
2068 "CRD-8400B",
2069 "CRD-8480B",
2070 "CRD-8482B",
2071 "CRD-84",
2072 "SanDisk SDP3B",
2073 "SanDisk SDP3B-64",
2074 "SANYO CD-ROM CRD",
2075 "HITACHI CDR-8",
2076 "HITACHI CDR-8335",
2077 "HITACHI CDR-8435",
2078 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002079 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 "CD-532E-A",
2081 "E-IDE CD-ROM CR-840",
2082 "CD-ROM Drive/F5A",
2083 "WPI CDD-820",
2084 "SAMSUNG CD-ROM SC-148C",
2085 "SAMSUNG CD-ROM SC",
2086 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2088 "_NEC DV5800A",
2089};
2090
Jeff Garzik057ace52005-10-22 14:27:05 -04002091static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 unsigned char model_num[40];
2094 char *s;
2095 unsigned int len;
2096 int i;
2097
2098 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2099 sizeof(model_num));
2100 s = &model_num[0];
2101 len = strnlen(s, sizeof(model_num));
2102
2103 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2104 while ((len > 0) && (s[len - 1] == ' ')) {
2105 len--;
2106 s[len] = 0;
2107 }
2108
2109 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2110 if (!strncmp(ata_dma_blacklist[i], s, len))
2111 return 1;
2112
2113 return 0;
2114}
2115
Jeff Garzik057ace52005-10-22 14:27:05 -04002116static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Jeff Garzik057ace52005-10-22 14:27:05 -04002118 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 unsigned int mask;
2120
2121 master = &ap->device[0];
2122 slave = &ap->device[1];
2123
2124 assert (ata_dev_present(master) || ata_dev_present(slave));
2125
2126 if (shift == ATA_SHIFT_UDMA) {
2127 mask = ap->udma_mask;
2128 if (ata_dev_present(master)) {
2129 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002130 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 mask = 0;
2132 ata_pr_blacklisted(ap, master);
2133 }
2134 }
2135 if (ata_dev_present(slave)) {
2136 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002137 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 mask = 0;
2139 ata_pr_blacklisted(ap, slave);
2140 }
2141 }
2142 }
2143 else if (shift == ATA_SHIFT_MWDMA) {
2144 mask = ap->mwdma_mask;
2145 if (ata_dev_present(master)) {
2146 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002147 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 mask = 0;
2149 ata_pr_blacklisted(ap, master);
2150 }
2151 }
2152 if (ata_dev_present(slave)) {
2153 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002154 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 mask = 0;
2156 ata_pr_blacklisted(ap, slave);
2157 }
2158 }
2159 }
2160 else if (shift == ATA_SHIFT_PIO) {
2161 mask = ap->pio_mask;
2162 if (ata_dev_present(master)) {
2163 /* spec doesn't return explicit support for
2164 * PIO0-2, so we fake it
2165 */
2166 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2167 tmp_mode <<= 3;
2168 tmp_mode |= 0x7;
2169 mask &= tmp_mode;
2170 }
2171 if (ata_dev_present(slave)) {
2172 /* spec doesn't return explicit support for
2173 * PIO0-2, so we fake it
2174 */
2175 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2176 tmp_mode <<= 3;
2177 tmp_mode |= 0x7;
2178 mask &= tmp_mode;
2179 }
2180 }
2181 else {
2182 mask = 0xffffffff; /* shut up compiler warning */
2183 BUG();
2184 }
2185
2186 return mask;
2187}
2188
2189/* find greatest bit */
2190static int fgb(u32 bitmap)
2191{
2192 unsigned int i;
2193 int x = -1;
2194
2195 for (i = 0; i < 32; i++)
2196 if (bitmap & (1 << i))
2197 x = i;
2198
2199 return x;
2200}
2201
2202/**
2203 * ata_choose_xfer_mode - attempt to find best transfer mode
2204 * @ap: Port for which an xfer mode will be selected
2205 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2206 * @xfer_shift_out: (output) bit shift that selects this mode
2207 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002208 * Based on host and device capabilities, determine the
2209 * maximum transfer mode that is amenable to all.
2210 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002212 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 *
2214 * RETURNS:
2215 * Zero on success, negative on error.
2216 */
2217
Jeff Garzik057ace52005-10-22 14:27:05 -04002218static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 u8 *xfer_mode_out,
2220 unsigned int *xfer_shift_out)
2221{
2222 unsigned int mask, shift;
2223 int x, i;
2224
2225 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2226 shift = xfer_mode_classes[i].shift;
2227 mask = ata_get_mode_mask(ap, shift);
2228
2229 x = fgb(mask);
2230 if (x >= 0) {
2231 *xfer_mode_out = xfer_mode_classes[i].base + x;
2232 *xfer_shift_out = shift;
2233 return 0;
2234 }
2235 }
2236
2237 return -1;
2238}
2239
2240/**
2241 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2242 * @ap: Port associated with device @dev
2243 * @dev: Device to which command will be sent
2244 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002245 * Issue SET FEATURES - XFER MODE command to device @dev
2246 * on port @ap.
2247 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002249 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 */
2251
2252static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2253{
2254 DECLARE_COMPLETION(wait);
2255 struct ata_queued_cmd *qc;
2256 int rc;
2257 unsigned long flags;
2258
2259 /* set up set-features taskfile */
2260 DPRINTK("set features - xfer mode\n");
2261
2262 qc = ata_qc_new_init(ap, dev);
2263 BUG_ON(qc == NULL);
2264
2265 qc->tf.command = ATA_CMD_SET_FEATURES;
2266 qc->tf.feature = SETFEATURES_XFER;
2267 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2268 qc->tf.protocol = ATA_PROT_NODATA;
2269 qc->tf.nsect = dev->xfer_mode;
2270
2271 qc->waiting = &wait;
2272 qc->complete_fn = ata_qc_complete_noop;
2273
2274 spin_lock_irqsave(&ap->host_set->lock, flags);
2275 rc = ata_qc_issue(qc);
2276 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2277
2278 if (rc)
2279 ata_port_disable(ap);
2280 else
2281 wait_for_completion(&wait);
2282
2283 DPRINTK("EXIT\n");
2284}
2285
2286/**
Albert Lee59a10b12005-10-12 15:09:42 +08002287 * ata_dev_reread_id - Reread the device identify device info
2288 * @ap: port where the device is
2289 * @dev: device to reread the identify device info
2290 *
2291 * LOCKING:
2292 */
2293
2294static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2295{
2296 DECLARE_COMPLETION(wait);
2297 struct ata_queued_cmd *qc;
2298 unsigned long flags;
2299 int rc;
2300
2301 qc = ata_qc_new_init(ap, dev);
2302 BUG_ON(qc == NULL);
2303
2304 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2305 qc->dma_dir = DMA_FROM_DEVICE;
2306
2307 if (dev->class == ATA_DEV_ATA) {
2308 qc->tf.command = ATA_CMD_ID_ATA;
2309 DPRINTK("do ATA identify\n");
2310 } else {
2311 qc->tf.command = ATA_CMD_ID_ATAPI;
2312 DPRINTK("do ATAPI identify\n");
2313 }
2314
2315 qc->tf.flags |= ATA_TFLAG_DEVICE;
2316 qc->tf.protocol = ATA_PROT_PIO;
2317 qc->nsect = 1;
2318
2319 qc->waiting = &wait;
2320 qc->complete_fn = ata_qc_complete_noop;
2321
2322 spin_lock_irqsave(&ap->host_set->lock, flags);
2323 rc = ata_qc_issue(qc);
2324 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2325
2326 if (rc)
2327 goto err_out;
2328
2329 wait_for_completion(&wait);
2330
2331 swap_buf_le16(dev->id, ATA_ID_WORDS);
2332
2333 ata_dump_id(dev);
2334
2335 DPRINTK("EXIT\n");
2336
2337 return;
2338err_out:
2339 ata_port_disable(ap);
2340}
2341
2342/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002343 * ata_dev_init_params - Issue INIT DEV PARAMS command
2344 * @ap: Port associated with device @dev
2345 * @dev: Device to which command will be sent
2346 *
2347 * LOCKING:
2348 */
2349
2350static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2351{
2352 DECLARE_COMPLETION(wait);
2353 struct ata_queued_cmd *qc;
2354 int rc;
2355 unsigned long flags;
2356 u16 sectors = dev->id[6];
2357 u16 heads = dev->id[3];
2358
2359 /* Number of sectors per track 1-255. Number of heads 1-16 */
2360 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2361 return;
2362
2363 /* set up init dev params taskfile */
2364 DPRINTK("init dev params \n");
2365
2366 qc = ata_qc_new_init(ap, dev);
2367 BUG_ON(qc == NULL);
2368
2369 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2370 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2371 qc->tf.protocol = ATA_PROT_NODATA;
2372 qc->tf.nsect = sectors;
2373 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2374
2375 qc->waiting = &wait;
2376 qc->complete_fn = ata_qc_complete_noop;
2377
2378 spin_lock_irqsave(&ap->host_set->lock, flags);
2379 rc = ata_qc_issue(qc);
2380 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2381
2382 if (rc)
2383 ata_port_disable(ap);
2384 else
2385 wait_for_completion(&wait);
2386
2387 DPRINTK("EXIT\n");
2388}
2389
2390/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002391 * ata_sg_clean - Unmap DMA memory associated with command
2392 * @qc: Command containing DMA memory to be released
2393 *
2394 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 *
2396 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002397 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 */
2399
2400static void ata_sg_clean(struct ata_queued_cmd *qc)
2401{
2402 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002403 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002405 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2408 assert(sg != NULL);
2409
2410 if (qc->flags & ATA_QCFLAG_SINGLE)
2411 assert(qc->n_elem == 1);
2412
2413 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2414
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002415 /* if we padded the buffer out to 32-bit bound, and data
2416 * xfer direction is from-device, we must copy from the
2417 * pad buffer back into the supplied buffer
2418 */
2419 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2420 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2421
2422 if (qc->flags & ATA_QCFLAG_SG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002424 /* restore last sg */
2425 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2426 if (pad_buf) {
2427 struct scatterlist *psg = &qc->pad_sgent;
2428 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2429 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2430 kunmap_atomic(psg->page, KM_IRQ0);
2431 }
2432 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2434 sg_dma_len(&sg[0]), dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002435 /* restore sg */
2436 sg->length += qc->pad_len;
2437 if (pad_buf)
2438 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2439 pad_buf, qc->pad_len);
2440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002443 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
2446/**
2447 * ata_fill_sg - Fill PCI IDE PRD table
2448 * @qc: Metadata associated with taskfile to be transferred
2449 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002450 * Fill PCI IDE PRD (scatter-gather) table with segments
2451 * associated with the current disk command.
2452 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002454 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 *
2456 */
2457static void ata_fill_sg(struct ata_queued_cmd *qc)
2458{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002460 struct scatterlist *sg;
2461 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002463 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 assert(qc->n_elem > 0);
2465
2466 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002467 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 u32 addr, offset;
2469 u32 sg_len, len;
2470
2471 /* determine if physical DMA addr spans 64K boundary.
2472 * Note h/w doesn't support 64-bit, so we unconditionally
2473 * truncate dma_addr_t to u32.
2474 */
2475 addr = (u32) sg_dma_address(sg);
2476 sg_len = sg_dma_len(sg);
2477
2478 while (sg_len) {
2479 offset = addr & 0xffff;
2480 len = sg_len;
2481 if ((offset + sg_len) > 0x10000)
2482 len = 0x10000 - offset;
2483
2484 ap->prd[idx].addr = cpu_to_le32(addr);
2485 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2486 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2487
2488 idx++;
2489 sg_len -= len;
2490 addr += len;
2491 }
2492 }
2493
2494 if (idx)
2495 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2496}
2497/**
2498 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2499 * @qc: Metadata associated with taskfile to check
2500 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002501 * Allow low-level driver to filter ATA PACKET commands, returning
2502 * a status indicating whether or not it is OK to use DMA for the
2503 * supplied PACKET command.
2504 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002506 * spin_lock_irqsave(host_set lock)
2507 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 * RETURNS: 0 when ATAPI DMA can be used
2509 * nonzero otherwise
2510 */
2511int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2512{
2513 struct ata_port *ap = qc->ap;
2514 int rc = 0; /* Assume ATAPI DMA is OK by default */
2515
2516 if (ap->ops->check_atapi_dma)
2517 rc = ap->ops->check_atapi_dma(qc);
2518
2519 return rc;
2520}
2521/**
2522 * ata_qc_prep - Prepare taskfile for submission
2523 * @qc: Metadata associated with taskfile to be prepared
2524 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002525 * Prepare ATA taskfile for submission.
2526 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 * LOCKING:
2528 * spin_lock_irqsave(host_set lock)
2529 */
2530void ata_qc_prep(struct ata_queued_cmd *qc)
2531{
2532 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2533 return;
2534
2535 ata_fill_sg(qc);
2536}
2537
Jeff Garzik0cba6322005-05-30 19:49:12 -04002538/**
2539 * ata_sg_init_one - Associate command with memory buffer
2540 * @qc: Command to be associated
2541 * @buf: Memory buffer
2542 * @buflen: Length of memory buffer, in bytes.
2543 *
2544 * Initialize the data-related elements of queued_cmd @qc
2545 * to point to a single memory buffer, @buf of byte length @buflen.
2546 *
2547 * LOCKING:
2548 * spin_lock_irqsave(host_set lock)
2549 */
2550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2552{
2553 struct scatterlist *sg;
2554
2555 qc->flags |= ATA_QCFLAG_SINGLE;
2556
2557 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002558 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002560 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 qc->buf_virt = buf;
2562
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002563 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002564 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
Jeff Garzik0cba6322005-05-30 19:49:12 -04002567/**
2568 * ata_sg_init - Associate command with scatter-gather table.
2569 * @qc: Command to be associated
2570 * @sg: Scatter-gather table.
2571 * @n_elem: Number of elements in s/g table.
2572 *
2573 * Initialize the data-related elements of queued_cmd @qc
2574 * to point to a scatter-gather table @sg, containing @n_elem
2575 * elements.
2576 *
2577 * LOCKING:
2578 * spin_lock_irqsave(host_set lock)
2579 */
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2582 unsigned int n_elem)
2583{
2584 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002585 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002587 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
2590/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002591 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2592 * @qc: Command with memory buffer to be mapped.
2593 *
2594 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 *
2596 * LOCKING:
2597 * spin_lock_irqsave(host_set lock)
2598 *
2599 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002600 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 */
2602
2603static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2604{
2605 struct ata_port *ap = qc->ap;
2606 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002607 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 dma_addr_t dma_address;
2609
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002610 /* we must lengthen transfers to end on a 32-bit boundary */
2611 qc->pad_len = sg->length & 3;
2612 if (qc->pad_len) {
2613 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2614 struct scatterlist *psg = &qc->pad_sgent;
2615
2616 assert(qc->dev->class == ATA_DEV_ATAPI);
2617
2618 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2619
2620 if (qc->tf.flags & ATA_TFLAG_WRITE)
2621 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2622 qc->pad_len);
2623
2624 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2625 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2626 /* trim sg */
2627 sg->length -= qc->pad_len;
2628
2629 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2630 sg->length, qc->pad_len);
2631 }
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002634 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002635 if (dma_mapping_error(dma_address)) {
2636 /* restore sg */
2637 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002642 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2645 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2646
2647 return 0;
2648}
2649
2650/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002651 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2652 * @qc: Command with scatter-gather table to be mapped.
2653 *
2654 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 *
2656 * LOCKING:
2657 * spin_lock_irqsave(host_set lock)
2658 *
2659 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002660 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 *
2662 */
2663
2664static int ata_sg_setup(struct ata_queued_cmd *qc)
2665{
2666 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002667 struct scatterlist *sg = qc->__sg;
2668 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 int n_elem, dir;
2670
2671 VPRINTK("ENTER, ata%u\n", ap->id);
2672 assert(qc->flags & ATA_QCFLAG_SG);
2673
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002674 /* we must lengthen transfers to end on a 32-bit boundary */
2675 qc->pad_len = lsg->length & 3;
2676 if (qc->pad_len) {
2677 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2678 struct scatterlist *psg = &qc->pad_sgent;
2679 unsigned int offset;
2680
2681 assert(qc->dev->class == ATA_DEV_ATAPI);
2682
2683 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2684
2685 /*
2686 * psg->page/offset are used to copy to-be-written
2687 * data in this function or read data in ata_sg_clean.
2688 */
2689 offset = lsg->offset + lsg->length - qc->pad_len;
2690 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2691 psg->offset = offset_in_page(offset);
2692
2693 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2694 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2695 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2696 kunmap_atomic(psg->page, KM_IRQ0);
2697 }
2698
2699 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2700 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2701 /* trim last sg */
2702 lsg->length -= qc->pad_len;
2703
2704 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2705 qc->n_elem - 1, lsg->length, qc->pad_len);
2706 }
2707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 dir = qc->dma_dir;
2709 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002710 if (n_elem < 1) {
2711 /* restore last sg */
2712 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
2716 DPRINTK("%d sg elements mapped\n", n_elem);
2717
2718 qc->n_elem = n_elem;
2719
2720 return 0;
2721}
2722
2723/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002724 * ata_poll_qc_complete - turn irq back on and finish qc
2725 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002726 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002727 *
2728 * LOCKING:
2729 * None. (grabs host lock)
2730 */
2731
Jeff Garzika7dac442005-10-30 04:44:42 -05002732void ata_poll_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
Tejun Heo40e8c822005-08-22 17:12:45 +09002733{
2734 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002735 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002736
Jeff Garzikb8f61532005-08-25 22:01:20 -04002737 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002738 ata_irq_on(ap);
Jeff Garzika7dac442005-10-30 04:44:42 -05002739 ata_qc_complete(qc, err_mask);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002740 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002741}
2742
2743/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002745 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 *
2747 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002748 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 *
2750 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002751 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 */
2753
2754static unsigned long ata_pio_poll(struct ata_port *ap)
2755{
2756 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002757 unsigned int poll_state = HSM_ST_UNKNOWN;
2758 unsigned int reg_state = HSM_ST_UNKNOWN;
2759 const unsigned int tmout_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Albert Lee14be71f2005-09-27 17:36:35 +08002761 switch (ap->hsm_task_state) {
2762 case HSM_ST:
2763 case HSM_ST_POLL:
2764 poll_state = HSM_ST_POLL;
2765 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002767 case HSM_ST_LAST:
2768 case HSM_ST_LAST_POLL:
2769 poll_state = HSM_ST_LAST_POLL;
2770 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 break;
2772 default:
2773 BUG();
2774 break;
2775 }
2776
2777 status = ata_chk_status(ap);
2778 if (status & ATA_BUSY) {
2779 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002780 ap->hsm_task_state = tmout_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return 0;
2782 }
Albert Lee14be71f2005-09-27 17:36:35 +08002783 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return ATA_SHORT_PAUSE;
2785 }
2786
Albert Lee14be71f2005-09-27 17:36:35 +08002787 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return 0;
2789}
2790
2791/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002792 * ata_pio_complete - check if drive is busy or idle
2793 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 *
2795 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002796 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002797 *
2798 * RETURNS:
Albert Leefbcdd802005-11-01 19:30:05 +08002799 * Zero if qc completed.
2800 * Non-zero if has next.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 */
2802
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002803static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
2805 struct ata_queued_cmd *qc;
2806 u8 drv_stat;
2807
2808 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002809 * This is purely heuristic. This is a fast path. Sometimes when
2810 * we enter, BSY will be cleared in a chk-status or two. If not,
2811 * the drive is probably seeking or something. Snooze for a couple
2812 * msecs, then chk-status again. If still busy, fall back to
Albert Lee07f6f7d2005-11-01 19:33:20 +08002813 * HSM_ST_LAST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 */
2815 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2816 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2817 msleep(2);
2818 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2819 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002820 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Albert Leefbcdd802005-11-01 19:30:05 +08002822 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 }
2824 }
2825
2826 drv_stat = ata_wait_idle(ap);
2827 if (!ata_ok(drv_stat)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002828 ap->hsm_task_state = HSM_ST_ERR;
Albert Leefbcdd802005-11-01 19:30:05 +08002829 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 }
2831
2832 qc = ata_qc_from_tag(ap, ap->active_tag);
2833 assert(qc != NULL);
2834
Albert Lee14be71f2005-09-27 17:36:35 +08002835 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Jeff Garzika7dac442005-10-30 04:44:42 -05002837 ata_poll_qc_complete(qc, 0);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002838
2839 /* another command may start at this point */
2840
Albert Leefbcdd802005-11-01 19:30:05 +08002841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
Edward Falk0baab862005-06-02 18:17:13 -04002844
2845/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002846 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002847 * @buf: Buffer to swap
2848 * @buf_words: Number of 16-bit words in buffer.
2849 *
2850 * Swap halves of 16-bit words if needed to convert from
2851 * little-endian byte order to native cpu byte order, or
2852 * vice-versa.
2853 *
2854 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002855 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857void swap_buf_le16(u16 *buf, unsigned int buf_words)
2858{
2859#ifdef __BIG_ENDIAN
2860 unsigned int i;
2861
2862 for (i = 0; i < buf_words; i++)
2863 buf[i] = le16_to_cpu(buf[i]);
2864#endif /* __BIG_ENDIAN */
2865}
2866
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002867/**
2868 * ata_mmio_data_xfer - Transfer data by MMIO
2869 * @ap: port to read/write
2870 * @buf: data buffer
2871 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002872 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002873 *
2874 * Transfer data from/to the device data register by MMIO.
2875 *
2876 * LOCKING:
2877 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002878 */
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2881 unsigned int buflen, int write_data)
2882{
2883 unsigned int i;
2884 unsigned int words = buflen >> 1;
2885 u16 *buf16 = (u16 *) buf;
2886 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2887
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002888 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (write_data) {
2890 for (i = 0; i < words; i++)
2891 writew(le16_to_cpu(buf16[i]), mmio);
2892 } else {
2893 for (i = 0; i < words; i++)
2894 buf16[i] = cpu_to_le16(readw(mmio));
2895 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002896
2897 /* Transfer trailing 1 byte, if any. */
2898 if (unlikely(buflen & 0x01)) {
2899 u16 align_buf[1] = { 0 };
2900 unsigned char *trailing_buf = buf + buflen - 1;
2901
2902 if (write_data) {
2903 memcpy(align_buf, trailing_buf, 1);
2904 writew(le16_to_cpu(align_buf[0]), mmio);
2905 } else {
2906 align_buf[0] = cpu_to_le16(readw(mmio));
2907 memcpy(trailing_buf, align_buf, 1);
2908 }
2909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910}
2911
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002912/**
2913 * ata_pio_data_xfer - Transfer data by PIO
2914 * @ap: port to read/write
2915 * @buf: data buffer
2916 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002917 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002918 *
2919 * Transfer data from/to the device data register by PIO.
2920 *
2921 * LOCKING:
2922 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002923 */
2924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2926 unsigned int buflen, int write_data)
2927{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002928 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002930 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002932 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002934 insw(ap->ioaddr.data_addr, buf, words);
2935
2936 /* Transfer trailing 1 byte, if any. */
2937 if (unlikely(buflen & 0x01)) {
2938 u16 align_buf[1] = { 0 };
2939 unsigned char *trailing_buf = buf + buflen - 1;
2940
2941 if (write_data) {
2942 memcpy(align_buf, trailing_buf, 1);
2943 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2944 } else {
2945 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2946 memcpy(trailing_buf, align_buf, 1);
2947 }
2948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002951/**
2952 * ata_data_xfer - Transfer data from/to the data register.
2953 * @ap: port to read/write
2954 * @buf: data buffer
2955 * @buflen: buffer length
2956 * @do_write: read/write
2957 *
2958 * Transfer data from/to the device data register.
2959 *
2960 * LOCKING:
2961 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002962 */
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2965 unsigned int buflen, int do_write)
2966{
2967 if (ap->flags & ATA_FLAG_MMIO)
2968 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2969 else
2970 ata_pio_data_xfer(ap, buf, buflen, do_write);
2971}
2972
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002973/**
2974 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2975 * @qc: Command on going
2976 *
2977 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2978 *
2979 * LOCKING:
2980 * Inherited from caller.
2981 */
2982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983static void ata_pio_sector(struct ata_queued_cmd *qc)
2984{
2985 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002986 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 struct ata_port *ap = qc->ap;
2988 struct page *page;
2989 unsigned int offset;
2990 unsigned char *buf;
2991
2992 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08002993 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 page = sg[qc->cursg].page;
2996 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2997
2998 /* get the current page and offset */
2999 page = nth_page(page, (offset >> PAGE_SHIFT));
3000 offset %= PAGE_SIZE;
3001
Albert Lee7282aa42005-10-09 09:46:07 -04003002 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3003
Albert Lee91b8b312005-10-09 09:48:44 -04003004 if (PageHighMem(page)) {
3005 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003006
Albert Lee91b8b312005-10-09 09:48:44 -04003007 local_irq_save(flags);
3008 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003009
Albert Lee91b8b312005-10-09 09:48:44 -04003010 /* do the actual data transfer */
3011 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3012
3013 kunmap_atomic(buf, KM_IRQ0);
3014 local_irq_restore(flags);
3015 } else {
3016 buf = page_address(page);
3017 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3018 }
Albert Lee7282aa42005-10-09 09:46:07 -04003019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 qc->cursect++;
3021 qc->cursg_ofs++;
3022
Albert Lee32529e02005-05-26 03:49:42 -04003023 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 qc->cursg++;
3025 qc->cursg_ofs = 0;
3026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027}
3028
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003029/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003030 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3031 * @qc: Command on going
3032 *
3033 * Transfer one or many ATA_SECT_SIZE of data from/to the
3034 * ATA device for the DRQ request.
3035 *
3036 * LOCKING:
3037 * Inherited from caller.
3038 */
3039
3040static void ata_pio_sectors(struct ata_queued_cmd *qc)
3041{
3042 if (is_multi_taskfile(&qc->tf)) {
3043 /* READ/WRITE MULTIPLE */
3044 unsigned int nsect;
3045
3046 assert(qc->dev->multi_count);
3047
3048 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3049 while (nsect--)
3050 ata_pio_sector(qc);
3051 } else
3052 ata_pio_sector(qc);
3053}
3054
3055/**
Albert Leec71c1852005-10-04 06:03:45 -04003056 * atapi_send_cdb - Write CDB bytes to hardware
3057 * @ap: Port to which ATAPI device is attached.
3058 * @qc: Taskfile currently active
3059 *
3060 * When device has indicated its readiness to accept
3061 * a CDB, this function is called. Send the CDB.
3062 *
3063 * LOCKING:
3064 * caller.
3065 */
3066
3067static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3068{
3069 /* send SCSI cdb */
3070 DPRINTK("send cdb\n");
3071 assert(ap->cdb_len >= 12);
3072
3073 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3074 ata_altstatus(ap); /* flush */
3075
3076 switch (qc->tf.protocol) {
3077 case ATA_PROT_ATAPI:
3078 ap->hsm_task_state = HSM_ST;
3079 break;
3080 case ATA_PROT_ATAPI_NODATA:
3081 ap->hsm_task_state = HSM_ST_LAST;
3082 break;
3083 case ATA_PROT_ATAPI_DMA:
3084 ap->hsm_task_state = HSM_ST_LAST;
3085 /* initiate bmdma */
3086 ap->ops->bmdma_start(qc);
3087 break;
3088 }
3089}
3090
3091/**
Albert Leee27486d2005-11-01 19:24:49 +08003092 * ata_pio_first_block - Write first data block to hardware
3093 * @ap: Port to which ATA/ATAPI device is attached.
Albert Leec71c1852005-10-04 06:03:45 -04003094 *
3095 * When device has indicated its readiness to accept
3096 * the data, this function sends out the CDB or
3097 * the first data block by PIO.
3098 * After this,
3099 * - If polling, ata_pio_task() handles the rest.
3100 * - Otherwise, interrupt handler takes over.
3101 *
3102 * LOCKING:
3103 * Kernel thread context (may sleep)
Albert Leefbcdd802005-11-01 19:30:05 +08003104 *
3105 * RETURNS:
3106 * Zero if irq handler takes over
3107 * Non-zero if has next (polling).
Albert Leec71c1852005-10-04 06:03:45 -04003108 */
3109
Albert Leefbcdd802005-11-01 19:30:05 +08003110static int ata_pio_first_block(struct ata_port *ap)
Albert Leec71c1852005-10-04 06:03:45 -04003111{
Albert Leec71c1852005-10-04 06:03:45 -04003112 struct ata_queued_cmd *qc;
3113 u8 status;
3114 unsigned long flags;
Albert Leefbcdd802005-11-01 19:30:05 +08003115 int has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003116
3117 qc = ata_qc_from_tag(ap, ap->active_tag);
3118 assert(qc != NULL);
3119 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3120
Albert Leefbcdd802005-11-01 19:30:05 +08003121 /* if polling, we will stay in the work queue after sending the data.
3122 * otherwise, interrupt handler takes over after sending the data.
3123 */
3124 has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3125
Albert Leec71c1852005-10-04 06:03:45 -04003126 /* sleep-wait for BSY to clear */
3127 DPRINTK("busy wait\n");
Albert Leefbcdd802005-11-01 19:30:05 +08003128 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
3129 ap->hsm_task_state = HSM_ST_TMOUT;
Albert Leec71c1852005-10-04 06:03:45 -04003130 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003131 }
Albert Leec71c1852005-10-04 06:03:45 -04003132
3133 /* make sure DRQ is set */
3134 status = ata_chk_status(ap);
Albert Leefbcdd802005-11-01 19:30:05 +08003135 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3136 /* device status error */
3137 ap->hsm_task_state = HSM_ST_ERR;
Albert Leec71c1852005-10-04 06:03:45 -04003138 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003139 }
Albert Leec71c1852005-10-04 06:03:45 -04003140
3141 /* Send the CDB (atapi) or the first data block (ata pio out).
3142 * During the state transition, interrupt handler shouldn't
3143 * be invoked before the data transfer is complete and
3144 * hsm_task_state is changed. Hence, the following locking.
3145 */
3146 spin_lock_irqsave(&ap->host_set->lock, flags);
3147
3148 if (qc->tf.protocol == ATA_PROT_PIO) {
3149 /* PIO data out protocol.
3150 * send first data block.
3151 */
3152
Albert Lee07f6f7d2005-11-01 19:33:20 +08003153 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3154 * so, the state is changed here before ata_pio_sectors().
Albert Leec71c1852005-10-04 06:03:45 -04003155 */
3156 ap->hsm_task_state = HSM_ST;
Albert Lee07f6f7d2005-11-01 19:33:20 +08003157 ata_pio_sectors(qc);
Albert Leec71c1852005-10-04 06:03:45 -04003158 ata_altstatus(ap); /* flush */
3159 } else
3160 /* send CDB */
3161 atapi_send_cdb(ap, qc);
3162
Albert Leefbcdd802005-11-01 19:30:05 +08003163 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3164
Albert Leec71c1852005-10-04 06:03:45 -04003165 /* if polling, ata_pio_task() handles the rest.
3166 * otherwise, interrupt handler takes over from here.
3167 */
Albert Leefbcdd802005-11-01 19:30:05 +08003168 return has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003169
3170err_out:
Albert Leefbcdd802005-11-01 19:30:05 +08003171 return 1; /* has next */
Albert Leec71c1852005-10-04 06:03:45 -04003172}
3173
3174/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003175 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3176 * @qc: Command on going
3177 * @bytes: number of bytes
3178 *
3179 * Transfer Transfer data from/to the ATAPI device.
3180 *
3181 * LOCKING:
3182 * Inherited from caller.
3183 *
3184 */
3185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3187{
3188 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003189 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 struct ata_port *ap = qc->ap;
3191 struct page *page;
3192 unsigned char *buf;
3193 unsigned int offset, count;
3194
Albert Lee563a6e12005-08-12 14:17:50 +08003195 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003196 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003199 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003200 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003201 * The end of qc->sg is reached and the device expects
3202 * more data to transfer. In order not to overrun qc->sg
3203 * and fulfill length specified in the byte count register,
3204 * - for read case, discard trailing data from the device
3205 * - for write case, padding zero data to the device
3206 */
3207 u16 pad_buf[1] = { 0 };
3208 unsigned int words = bytes >> 1;
3209 unsigned int i;
3210
3211 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003212 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003213 ap->id, bytes);
3214
3215 for (i = 0; i < words; i++)
3216 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3217
Albert Lee14be71f2005-09-27 17:36:35 +08003218 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003219 return;
3220 }
3221
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003222 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 page = sg->page;
3225 offset = sg->offset + qc->cursg_ofs;
3226
3227 /* get the current page and offset */
3228 page = nth_page(page, (offset >> PAGE_SHIFT));
3229 offset %= PAGE_SIZE;
3230
Albert Lee6952df02005-06-06 15:56:03 +08003231 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003232 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
3234 /* don't cross page boundaries */
3235 count = min(count, (unsigned int)PAGE_SIZE - offset);
3236
Albert Lee7282aa42005-10-09 09:46:07 -04003237 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3238
Albert Lee91b8b312005-10-09 09:48:44 -04003239 if (PageHighMem(page)) {
3240 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003241
Albert Lee91b8b312005-10-09 09:48:44 -04003242 local_irq_save(flags);
3243 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003244
Albert Lee91b8b312005-10-09 09:48:44 -04003245 /* do the actual data transfer */
3246 ata_data_xfer(ap, buf + offset, count, do_write);
3247
3248 kunmap_atomic(buf, KM_IRQ0);
3249 local_irq_restore(flags);
3250 } else {
3251 buf = page_address(page);
3252 ata_data_xfer(ap, buf + offset, count, do_write);
3253 }
Albert Lee7282aa42005-10-09 09:46:07 -04003254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 bytes -= count;
3256 qc->curbytes += count;
3257 qc->cursg_ofs += count;
3258
Albert Lee32529e02005-05-26 03:49:42 -04003259 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 qc->cursg++;
3261 qc->cursg_ofs = 0;
3262 }
3263
Albert Lee563a6e12005-08-12 14:17:50 +08003264 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003268/**
3269 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3270 * @qc: Command on going
3271 *
3272 * Transfer Transfer data from/to the ATAPI device.
3273 *
3274 * LOCKING:
3275 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003276 */
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3279{
3280 struct ata_port *ap = qc->ap;
3281 struct ata_device *dev = qc->dev;
3282 unsigned int ireason, bc_lo, bc_hi, bytes;
3283 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3284
3285 ap->ops->tf_read(ap, &qc->tf);
3286 ireason = qc->tf.nsect;
3287 bc_lo = qc->tf.lbam;
3288 bc_hi = qc->tf.lbah;
3289 bytes = (bc_hi << 8) | bc_lo;
3290
3291 /* shall be cleared to zero, indicating xfer of data */
3292 if (ireason & (1 << 0))
3293 goto err_out;
3294
3295 /* make sure transfer direction matches expected */
3296 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3297 if (do_write != i_write)
3298 goto err_out;
3299
Albert Lee312f7da2005-09-27 17:38:03 +08003300 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 __atapi_pio_bytes(qc, bytes);
3303
3304 return;
3305
3306err_out:
3307 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3308 ap->id, dev->devno);
Albert Lee14be71f2005-09-27 17:36:35 +08003309 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
3312/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003313 * ata_pio_block - start PIO on a block
3314 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 *
3316 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003317 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 */
3319
3320static void ata_pio_block(struct ata_port *ap)
3321{
3322 struct ata_queued_cmd *qc;
3323 u8 status;
3324
3325 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003326 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 * Sometimes when we enter, BSY will be cleared in
3328 * a chk-status or two. If not, the drive is probably seeking
3329 * or something. Snooze for a couple msecs, then
3330 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003331 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 */
3333 status = ata_busy_wait(ap, ATA_BUSY, 5);
3334 if (status & ATA_BUSY) {
3335 msleep(2);
3336 status = ata_busy_wait(ap, ATA_BUSY, 10);
3337 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003338 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3340 return;
3341 }
3342 }
3343
3344 qc = ata_qc_from_tag(ap, ap->active_tag);
3345 assert(qc != NULL);
3346
3347 if (is_atapi_taskfile(&qc->tf)) {
3348 /* no more data to transfer or unsupported ATAPI command */
3349 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003350 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return;
3352 }
3353
3354 atapi_pio_bytes(qc);
3355 } else {
3356 /* handle BSY=0, DRQ=0 as error */
3357 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003358 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 return;
3360 }
3361
Albert Lee07f6f7d2005-11-01 19:33:20 +08003362 ata_pio_sectors(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
Albert Lee467b16d2005-11-01 19:19:01 +08003364
3365 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
3368static void ata_pio_error(struct ata_port *ap)
3369{
3370 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003371
3372 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
3374 qc = ata_qc_from_tag(ap, ap->active_tag);
3375 assert(qc != NULL);
3376
Albert Lee14be71f2005-09-27 17:36:35 +08003377 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Jeff Garzika7dac442005-10-30 04:44:42 -05003379 ata_poll_qc_complete(qc, AC_ERR_ATA_BUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380}
3381
3382static void ata_pio_task(void *_data)
3383{
3384 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003385 unsigned long timeout;
Albert Leefbcdd802005-11-01 19:30:05 +08003386 int has_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003387
3388fsm_start:
3389 timeout = 0;
Albert Leefbcdd802005-11-01 19:30:05 +08003390 has_next = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Albert Lee14be71f2005-09-27 17:36:35 +08003392 switch (ap->hsm_task_state) {
Albert Leee27486d2005-11-01 19:24:49 +08003393 case HSM_ST_FIRST:
Albert Leefbcdd802005-11-01 19:30:05 +08003394 has_next = ata_pio_first_block(ap);
3395 break;
Albert Leee27486d2005-11-01 19:24:49 +08003396
Albert Lee14be71f2005-09-27 17:36:35 +08003397 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 ata_pio_block(ap);
3399 break;
3400
Albert Lee14be71f2005-09-27 17:36:35 +08003401 case HSM_ST_LAST:
Albert Leefbcdd802005-11-01 19:30:05 +08003402 has_next = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 break;
3404
Albert Lee14be71f2005-09-27 17:36:35 +08003405 case HSM_ST_POLL:
3406 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 timeout = ata_pio_poll(ap);
3408 break;
3409
Albert Lee14be71f2005-09-27 17:36:35 +08003410 case HSM_ST_TMOUT:
3411 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 ata_pio_error(ap);
3413 return;
Albert Lee467b16d2005-11-01 19:19:01 +08003414
3415 default:
3416 BUG();
3417 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
3419
3420 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003421 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
Albert Leefbcdd802005-11-01 19:30:05 +08003422 else if (has_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003423 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426/**
3427 * ata_qc_timeout - Handle timeout of queued command
3428 * @qc: Command that timed out
3429 *
3430 * Some part of the kernel (currently, only the SCSI layer)
3431 * has noticed that the active command on port @ap has not
3432 * completed after a specified length of time. Handle this
3433 * condition by disabling DMA (if necessary) and completing
3434 * transactions, with error if necessary.
3435 *
3436 * This also handles the case of the "lost interrupt", where
3437 * for some reason (possibly hardware bug, possibly driver bug)
3438 * an interrupt was not delivered to the driver, even though the
3439 * transaction completed successfully.
3440 *
3441 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003442 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 */
3444
3445static void ata_qc_timeout(struct ata_queued_cmd *qc)
3446{
3447 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003448 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 struct ata_device *dev = qc->dev;
3450 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003451 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
3453 DPRINTK("ENTER\n");
3454
3455 /* FIXME: doesn't this conflict with timeout handling? */
3456 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3457 struct scsi_cmnd *cmd = qc->scsicmd;
3458
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02003459 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 /* finish completing original command */
Jeff Garzikb8f61532005-08-25 22:01:20 -04003462 spin_lock_irqsave(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 __ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003464 spin_unlock_irqrestore(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466 atapi_request_sense(ap, dev, cmd);
3467
3468 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3469 scsi_finish_command(cmd);
3470
3471 goto out;
3472 }
3473 }
3474
Jeff Garzikb8f61532005-08-25 22:01:20 -04003475 spin_lock_irqsave(&host_set->lock, flags);
3476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 /* hack alert! We cannot use the supplied completion
3478 * function from inside the ->eh_strategy_handler() thread.
3479 * libata is the only user of ->eh_strategy_handler() in
3480 * any kernel, so the default scsi_done() assumes it is
3481 * not being called from the SCSI EH.
3482 */
3483 qc->scsidone = scsi_finish_command;
3484
3485 switch (qc->tf.protocol) {
3486
3487 case ATA_PROT_DMA:
3488 case ATA_PROT_ATAPI_DMA:
3489 host_stat = ap->ops->bmdma_status(ap);
3490
3491 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003492 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
3494 /* fall through */
3495
3496 default:
3497 ata_altstatus(ap);
3498 drv_stat = ata_chk_status(ap);
3499
3500 /* ack bmdma irq events */
3501 ap->ops->irq_clear(ap);
3502
3503 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3504 ap->id, qc->tf.command, drv_stat, host_stat);
3505
Albert Lee312f7da2005-09-27 17:38:03 +08003506 ap->hsm_task_state = HSM_ST_IDLE;
3507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 /* complete taskfile transaction */
Jeff Garzika7dac442005-10-30 04:44:42 -05003509 ata_qc_complete(qc, ac_err_mask(drv_stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 break;
3511 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003512
3513 spin_unlock_irqrestore(&host_set->lock, flags);
3514
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515out:
3516 DPRINTK("EXIT\n");
3517}
3518
3519/**
3520 * ata_eng_timeout - Handle timeout of queued command
3521 * @ap: Port on which timed-out command is active
3522 *
3523 * Some part of the kernel (currently, only the SCSI layer)
3524 * has noticed that the active command on port @ap has not
3525 * completed after a specified length of time. Handle this
3526 * condition by disabling DMA (if necessary) and completing
3527 * transactions, with error if necessary.
3528 *
3529 * This also handles the case of the "lost interrupt", where
3530 * for some reason (possibly hardware bug, possibly driver bug)
3531 * an interrupt was not delivered to the driver, even though the
3532 * transaction completed successfully.
3533 *
3534 * LOCKING:
3535 * Inherited from SCSI layer (none, can sleep)
3536 */
3537
3538void ata_eng_timeout(struct ata_port *ap)
3539{
3540 struct ata_queued_cmd *qc;
3541
3542 DPRINTK("ENTER\n");
3543
3544 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003545 if (qc)
3546 ata_qc_timeout(qc);
3547 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3549 ap->id);
3550 goto out;
3551 }
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553out:
3554 DPRINTK("EXIT\n");
3555}
3556
3557/**
3558 * ata_qc_new - Request an available ATA command, for queueing
3559 * @ap: Port associated with device @dev
3560 * @dev: Device from whom we request an available command structure
3561 *
3562 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003563 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 */
3565
3566static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3567{
3568 struct ata_queued_cmd *qc = NULL;
3569 unsigned int i;
3570
3571 for (i = 0; i < ATA_MAX_QUEUE; i++)
3572 if (!test_and_set_bit(i, &ap->qactive)) {
3573 qc = ata_qc_from_tag(ap, i);
3574 break;
3575 }
3576
3577 if (qc)
3578 qc->tag = i;
3579
3580 return qc;
3581}
3582
3583/**
3584 * ata_qc_new_init - Request an available ATA command, and initialize it
3585 * @ap: Port associated with device @dev
3586 * @dev: Device from whom we request an available command structure
3587 *
3588 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003589 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 */
3591
3592struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3593 struct ata_device *dev)
3594{
3595 struct ata_queued_cmd *qc;
3596
3597 qc = ata_qc_new(ap);
3598 if (qc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003599 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 qc->flags = 0;
3601 qc->scsicmd = NULL;
3602 qc->ap = ap;
3603 qc->dev = dev;
3604 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3605 qc->nsect = 0;
3606 qc->nbytes = qc->curbytes = 0;
3607
3608 ata_tf_init(ap, &qc->tf, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
3610
3611 return qc;
3612}
3613
Jeff Garzika7dac442005-10-30 04:44:42 -05003614int ata_qc_complete_noop(struct ata_queued_cmd *qc, unsigned int err_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
3616 return 0;
3617}
3618
3619static void __ata_qc_complete(struct ata_queued_cmd *qc)
3620{
3621 struct ata_port *ap = qc->ap;
3622 unsigned int tag, do_clear = 0;
3623
3624 qc->flags = 0;
3625 tag = qc->tag;
3626 if (likely(ata_tag_valid(tag))) {
3627 if (tag == ap->active_tag)
3628 ap->active_tag = ATA_TAG_POISON;
3629 qc->tag = ATA_TAG_POISON;
3630 do_clear = 1;
3631 }
3632
3633 if (qc->waiting) {
3634 struct completion *waiting = qc->waiting;
3635 qc->waiting = NULL;
3636 complete(waiting);
3637 }
3638
3639 if (likely(do_clear))
3640 clear_bit(tag, &ap->qactive);
3641}
3642
3643/**
3644 * ata_qc_free - free unused ata_queued_cmd
3645 * @qc: Command to complete
3646 *
3647 * Designed to free unused ata_queued_cmd object
3648 * in case something prevents using it.
3649 *
3650 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003651 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 */
3653void ata_qc_free(struct ata_queued_cmd *qc)
3654{
3655 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3656 assert(qc->waiting == NULL); /* nothing should be waiting */
3657
3658 __ata_qc_complete(qc);
3659}
3660
3661/**
3662 * ata_qc_complete - Complete an active ATA command
3663 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003664 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003665 *
3666 * Indicate to the mid and upper layers that an ATA
3667 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 *
3669 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003670 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 */
3672
Jeff Garzika7dac442005-10-30 04:44:42 -05003673void ata_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
3675 int rc;
3676
3677 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3678 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3679
3680 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3681 ata_sg_clean(qc);
3682
Albert Lee3f3791d2005-08-16 14:25:38 +08003683 /* atapi: mark qc as inactive to prevent the interrupt handler
3684 * from completing the command twice later, before the error handler
3685 * is called. (when rc != 0 and atapi request sense is needed)
3686 */
3687 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 /* call completion callback */
Jeff Garzika7dac442005-10-30 04:44:42 -05003690 rc = qc->complete_fn(qc, err_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
3692 /* if callback indicates not to complete command (non-zero),
3693 * return immediately
3694 */
3695 if (rc != 0)
3696 return;
3697
3698 __ata_qc_complete(qc);
3699
3700 VPRINTK("EXIT\n");
3701}
3702
3703static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3704{
3705 struct ata_port *ap = qc->ap;
3706
3707 switch (qc->tf.protocol) {
3708 case ATA_PROT_DMA:
3709 case ATA_PROT_ATAPI_DMA:
3710 return 1;
3711
3712 case ATA_PROT_ATAPI:
3713 case ATA_PROT_PIO:
3714 case ATA_PROT_PIO_MULT:
3715 if (ap->flags & ATA_FLAG_PIO_DMA)
3716 return 1;
3717
3718 /* fall through */
3719
3720 default:
3721 return 0;
3722 }
3723
3724 /* never reached */
3725}
3726
3727/**
3728 * ata_qc_issue - issue taskfile to device
3729 * @qc: command to issue to device
3730 *
3731 * Prepare an ATA command to submission to device.
3732 * This includes mapping the data into a DMA-able
3733 * area, filling in the S/G table, and finally
3734 * writing the taskfile to hardware, starting the command.
3735 *
3736 * LOCKING:
3737 * spin_lock_irqsave(host_set lock)
3738 *
3739 * RETURNS:
3740 * Zero on success, negative on error.
3741 */
3742
3743int ata_qc_issue(struct ata_queued_cmd *qc)
3744{
3745 struct ata_port *ap = qc->ap;
3746
3747 if (ata_should_dma_map(qc)) {
3748 if (qc->flags & ATA_QCFLAG_SG) {
3749 if (ata_sg_setup(qc))
3750 goto err_out;
3751 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3752 if (ata_sg_setup_one(qc))
3753 goto err_out;
3754 }
3755 } else {
3756 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3757 }
3758
3759 ap->ops->qc_prep(qc);
3760
3761 qc->ap->active_tag = qc->tag;
3762 qc->flags |= ATA_QCFLAG_ACTIVE;
3763
3764 return ap->ops->qc_issue(qc);
3765
3766err_out:
3767 return -1;
3768}
3769
Edward Falk0baab862005-06-02 18:17:13 -04003770
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771/**
3772 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3773 * @qc: command to issue to device
3774 *
3775 * Using various libata functions and hooks, this function
3776 * starts an ATA command. ATA commands are grouped into
3777 * classes called "protocols", and issuing each type of protocol
3778 * is slightly different.
3779 *
Edward Falk0baab862005-06-02 18:17:13 -04003780 * May be used as the qc_issue() entry in ata_port_operations.
3781 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 * LOCKING:
3783 * spin_lock_irqsave(host_set lock)
3784 *
3785 * RETURNS:
3786 * Zero on success, negative on error.
3787 */
3788
3789int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3790{
3791 struct ata_port *ap = qc->ap;
3792
Albert Leee50362e2005-09-27 17:39:50 +08003793 /* Use polling pio if the LLD doesn't handle
3794 * interrupt driven pio and atapi CDB interrupt.
3795 */
3796 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3797 switch (qc->tf.protocol) {
3798 case ATA_PROT_PIO:
3799 case ATA_PROT_ATAPI:
3800 case ATA_PROT_ATAPI_NODATA:
3801 qc->tf.flags |= ATA_TFLAG_POLLING;
3802 break;
3803 case ATA_PROT_ATAPI_DMA:
3804 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3805 BUG();
3806 break;
3807 default:
3808 break;
3809 }
3810 }
3811
Albert Lee312f7da2005-09-27 17:38:03 +08003812 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 ata_dev_select(ap, qc->dev->devno, 1, 0);
3814
Albert Lee312f7da2005-09-27 17:38:03 +08003815 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 switch (qc->tf.protocol) {
3817 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003818 if (qc->tf.flags & ATA_TFLAG_POLLING)
3819 ata_qc_set_polling(qc);
3820
Jeff Garzike5338252005-10-30 21:37:17 -05003821 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003822 ap->hsm_task_state = HSM_ST_LAST;
3823
3824 if (qc->tf.flags & ATA_TFLAG_POLLING)
3825 queue_work(ata_wq, &ap->pio_task);
3826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 break;
3828
3829 case ATA_PROT_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003830 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3833 ap->ops->bmdma_setup(qc); /* set up bmdma */
3834 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003835 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 break;
3837
Albert Lee312f7da2005-09-27 17:38:03 +08003838 case ATA_PROT_PIO:
3839 if (qc->tf.flags & ATA_TFLAG_POLLING)
3840 ata_qc_set_polling(qc);
3841
Jeff Garzike5338252005-10-30 21:37:17 -05003842 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003843
Albert Lee54f00382005-09-30 19:14:19 +08003844 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3845 /* PIO data out protocol */
3846 ap->hsm_task_state = HSM_ST_FIRST;
Albert Leee27486d2005-11-01 19:24:49 +08003847 queue_work(ata_wq, &ap->pio_task);
Albert Lee54f00382005-09-30 19:14:19 +08003848
3849 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08003850 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08003851 */
Albert Lee312f7da2005-09-27 17:38:03 +08003852 } else {
Albert Lee54f00382005-09-30 19:14:19 +08003853 /* PIO data in protocol */
3854 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08003855
Albert Lee54f00382005-09-30 19:14:19 +08003856 if (qc->tf.flags & ATA_TFLAG_POLLING)
3857 queue_work(ata_wq, &ap->pio_task);
Albert Lee312f7da2005-09-27 17:38:03 +08003858
Albert Lee54f00382005-09-30 19:14:19 +08003859 /* if polling, ata_pio_task() handles the rest.
3860 * otherwise, interrupt handler takes over from here.
3861 */
Albert Lee312f7da2005-09-27 17:38:03 +08003862 }
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 break;
3865
3866 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003868 if (qc->tf.flags & ATA_TFLAG_POLLING)
3869 ata_qc_set_polling(qc);
3870
Jeff Garzike5338252005-10-30 21:37:17 -05003871 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003872 ap->hsm_task_state = HSM_ST_FIRST;
3873
3874 /* send cdb by polling if no cdb interrupt */
3875 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3876 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Leee27486d2005-11-01 19:24:49 +08003877 queue_work(ata_wq, &ap->pio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 break;
3879
3880 case ATA_PROT_ATAPI_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003881 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3884 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003885 ap->hsm_task_state = HSM_ST_FIRST;
3886
3887 /* send cdb by polling if no cdb interrupt */
3888 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Leee27486d2005-11-01 19:24:49 +08003889 queue_work(ata_wq, &ap->pio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 break;
3891
3892 default:
3893 WARN_ON(1);
3894 return -1;
3895 }
3896
3897 return 0;
3898}
3899
3900/**
Edward Falk0baab862005-06-02 18:17:13 -04003901 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 * @qc: Info associated with this ATA transaction.
3903 *
3904 * LOCKING:
3905 * spin_lock_irqsave(host_set lock)
3906 */
3907
3908static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3909{
3910 struct ata_port *ap = qc->ap;
3911 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3912 u8 dmactl;
3913 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3914
3915 /* load PRD table addr. */
3916 mb(); /* make sure PRD table writes are visible to controller */
3917 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3918
3919 /* specify data direction, triple-check start bit is clear */
3920 dmactl = readb(mmio + ATA_DMA_CMD);
3921 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3922 if (!rw)
3923 dmactl |= ATA_DMA_WR;
3924 writeb(dmactl, mmio + ATA_DMA_CMD);
3925
3926 /* issue r/w command */
3927 ap->ops->exec_command(ap, &qc->tf);
3928}
3929
3930/**
Alan Coxb73fc892005-08-26 16:03:19 +01003931 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 * @qc: Info associated with this ATA transaction.
3933 *
3934 * LOCKING:
3935 * spin_lock_irqsave(host_set lock)
3936 */
3937
3938static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3939{
3940 struct ata_port *ap = qc->ap;
3941 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3942 u8 dmactl;
3943
3944 /* start host DMA transaction */
3945 dmactl = readb(mmio + ATA_DMA_CMD);
3946 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3947
3948 /* Strictly, one may wish to issue a readb() here, to
3949 * flush the mmio write. However, control also passes
3950 * to the hardware at this point, and it will interrupt
3951 * us when we are to resume control. So, in effect,
3952 * we don't care when the mmio write flushes.
3953 * Further, a read of the DMA status register _immediately_
3954 * following the write may not be what certain flaky hardware
3955 * is expected, so I think it is best to not add a readb()
3956 * without first all the MMIO ATA cards/mobos.
3957 * Or maybe I'm just being paranoid.
3958 */
3959}
3960
3961/**
3962 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3963 * @qc: Info associated with this ATA transaction.
3964 *
3965 * LOCKING:
3966 * spin_lock_irqsave(host_set lock)
3967 */
3968
3969static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3970{
3971 struct ata_port *ap = qc->ap;
3972 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3973 u8 dmactl;
3974
3975 /* load PRD table addr. */
3976 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3977
3978 /* specify data direction, triple-check start bit is clear */
3979 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3980 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3981 if (!rw)
3982 dmactl |= ATA_DMA_WR;
3983 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3984
3985 /* issue r/w command */
3986 ap->ops->exec_command(ap, &qc->tf);
3987}
3988
3989/**
3990 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3991 * @qc: Info associated with this ATA transaction.
3992 *
3993 * LOCKING:
3994 * spin_lock_irqsave(host_set lock)
3995 */
3996
3997static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3998{
3999 struct ata_port *ap = qc->ap;
4000 u8 dmactl;
4001
4002 /* start host DMA transaction */
4003 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4004 outb(dmactl | ATA_DMA_START,
4005 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4006}
4007
Edward Falk0baab862005-06-02 18:17:13 -04004008
4009/**
4010 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4011 * @qc: Info associated with this ATA transaction.
4012 *
4013 * Writes the ATA_DMA_START flag to the DMA command register.
4014 *
4015 * May be used as the bmdma_start() entry in ata_port_operations.
4016 *
4017 * LOCKING:
4018 * spin_lock_irqsave(host_set lock)
4019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020void ata_bmdma_start(struct ata_queued_cmd *qc)
4021{
4022 if (qc->ap->flags & ATA_FLAG_MMIO)
4023 ata_bmdma_start_mmio(qc);
4024 else
4025 ata_bmdma_start_pio(qc);
4026}
4027
Edward Falk0baab862005-06-02 18:17:13 -04004028
4029/**
4030 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4031 * @qc: Info associated with this ATA transaction.
4032 *
4033 * Writes address of PRD table to device's PRD Table Address
4034 * register, sets the DMA control register, and calls
4035 * ops->exec_command() to start the transfer.
4036 *
4037 * May be used as the bmdma_setup() entry in ata_port_operations.
4038 *
4039 * LOCKING:
4040 * spin_lock_irqsave(host_set lock)
4041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042void ata_bmdma_setup(struct ata_queued_cmd *qc)
4043{
4044 if (qc->ap->flags & ATA_FLAG_MMIO)
4045 ata_bmdma_setup_mmio(qc);
4046 else
4047 ata_bmdma_setup_pio(qc);
4048}
4049
Edward Falk0baab862005-06-02 18:17:13 -04004050
4051/**
4052 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004053 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004054 *
4055 * Clear interrupt and error flags in DMA status register.
4056 *
4057 * May be used as the irq_clear() entry in ata_port_operations.
4058 *
4059 * LOCKING:
4060 * spin_lock_irqsave(host_set lock)
4061 */
4062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063void ata_bmdma_irq_clear(struct ata_port *ap)
4064{
4065 if (ap->flags & ATA_FLAG_MMIO) {
4066 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4067 writeb(readb(mmio), mmio);
4068 } else {
4069 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4070 outb(inb(addr), addr);
4071 }
4072
4073}
4074
Edward Falk0baab862005-06-02 18:17:13 -04004075
4076/**
4077 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004078 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004079 *
4080 * Read and return BMDMA status register.
4081 *
4082 * May be used as the bmdma_status() entry in ata_port_operations.
4083 *
4084 * LOCKING:
4085 * spin_lock_irqsave(host_set lock)
4086 */
4087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088u8 ata_bmdma_status(struct ata_port *ap)
4089{
4090 u8 host_stat;
4091 if (ap->flags & ATA_FLAG_MMIO) {
4092 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4093 host_stat = readb(mmio + ATA_DMA_STATUS);
4094 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004095 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 return host_stat;
4097}
4098
Edward Falk0baab862005-06-02 18:17:13 -04004099
4100/**
4101 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004102 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004103 *
4104 * Clears the ATA_DMA_START flag in the dma control register
4105 *
4106 * May be used as the bmdma_stop() entry in ata_port_operations.
4107 *
4108 * LOCKING:
4109 * spin_lock_irqsave(host_set lock)
4110 */
4111
Alan Coxb73fc892005-08-26 16:03:19 +01004112void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
Alan Coxb73fc892005-08-26 16:03:19 +01004114 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 if (ap->flags & ATA_FLAG_MMIO) {
4116 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4117
4118 /* clear start/stop bit */
4119 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4120 mmio + ATA_DMA_CMD);
4121 } else {
4122 /* clear start/stop bit */
4123 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4124 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4125 }
4126
4127 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4128 ata_altstatus(ap); /* dummy read */
4129}
4130
4131/**
4132 * ata_host_intr - Handle host interrupt for given (port, task)
4133 * @ap: Port on which interrupt arrived (possibly...)
4134 * @qc: Taskfile currently active in engine
4135 *
4136 * Handle host interrupt for given queued command. Currently,
4137 * only DMA interrupts are handled. All other commands are
4138 * handled via polling with interrupts disabled (nIEN bit).
4139 *
4140 * LOCKING:
4141 * spin_lock_irqsave(host_set lock)
4142 *
4143 * RETURNS:
4144 * One if interrupt was handled, zero if not (shared irq).
4145 */
4146
4147inline unsigned int ata_host_intr (struct ata_port *ap,
4148 struct ata_queued_cmd *qc)
4149{
Albert Lee312f7da2005-09-27 17:38:03 +08004150 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Albert Lee312f7da2005-09-27 17:38:03 +08004152 VPRINTK("ata%u: protocol %d task_state %d\n",
4153 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Albert Lee312f7da2005-09-27 17:38:03 +08004155 /* Check whether we are expecting interrupt in this state */
4156 switch (ap->hsm_task_state) {
4157 case HSM_ST_FIRST:
4158 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4159 * The flag was turned on only for atapi devices.
4160 * No need to check is_atapi_taskfile(&qc->tf) again.
4161 */
4162 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08004164 break;
4165 case HSM_ST_LAST:
4166 if (qc->tf.protocol == ATA_PROT_DMA ||
4167 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4168 /* check status of DMA engine */
4169 host_stat = ap->ops->bmdma_status(ap);
4170 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Albert Lee312f7da2005-09-27 17:38:03 +08004172 /* if it's not our irq... */
4173 if (!(host_stat & ATA_DMA_INTR))
4174 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Albert Lee312f7da2005-09-27 17:38:03 +08004176 /* before we do anything else, clear DMA-Start bit */
4177 ap->ops->bmdma_stop(qc);
4178 }
4179 break;
4180 case HSM_ST:
4181 break;
4182 default:
4183 goto idle_irq;
4184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Albert Lee312f7da2005-09-27 17:38:03 +08004186 /* check altstatus */
4187 status = ata_altstatus(ap);
4188 if (status & ATA_BUSY)
4189 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Albert Lee312f7da2005-09-27 17:38:03 +08004191 /* check main status, clearing INTRQ */
4192 status = ata_chk_status(ap);
4193 if (unlikely(status & ATA_BUSY))
4194 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Albert Lee312f7da2005-09-27 17:38:03 +08004196 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4197 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4198
4199 /* ack bmdma irq events */
4200 ap->ops->irq_clear(ap);
4201
4202 /* check error */
4203 if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
4204 ap->hsm_task_state = HSM_ST_ERR;
4205
4206fsm_start:
4207 switch (ap->hsm_task_state) {
4208 case HSM_ST_FIRST:
4209 /* Some pre-ATAPI-4 devices assert INTRQ
4210 * at this state when ready to receive CDB.
4211 */
4212
4213 /* check device status */
4214 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4215 /* Wrong status. Let EH handle this */
4216 ap->hsm_task_state = HSM_ST_ERR;
4217 goto fsm_start;
4218 }
4219
4220 atapi_send_cdb(ap, qc);
4221
4222 break;
4223
4224 case HSM_ST:
4225 /* complete command or read/write the data register */
4226 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4227 /* ATAPI PIO protocol */
4228 if ((status & ATA_DRQ) == 0) {
4229 /* no more data to transfer */
4230 ap->hsm_task_state = HSM_ST_LAST;
4231 goto fsm_start;
4232 }
4233
4234 atapi_pio_bytes(qc);
4235
4236 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4237 /* bad ireason reported by device */
4238 goto fsm_start;
4239
4240 } else {
4241 /* ATA PIO protocol */
4242 if (unlikely((status & ATA_DRQ) == 0)) {
4243 /* handle BSY=0, DRQ=0 as error */
4244 ap->hsm_task_state = HSM_ST_ERR;
4245 goto fsm_start;
4246 }
4247
Albert Lee07f6f7d2005-11-01 19:33:20 +08004248 ata_pio_sectors(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004249
4250 if (ap->hsm_task_state == HSM_ST_LAST &&
4251 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4252 /* all data read */
4253 ata_altstatus(ap);
4254 status = ata_chk_status(ap);
4255 goto fsm_start;
4256 }
4257 }
4258
4259 ata_altstatus(ap); /* flush */
4260 break;
4261
4262 case HSM_ST_LAST:
4263 if (unlikely(status & ATA_DRQ)) {
4264 /* handle DRQ=1 as error */
4265 ap->hsm_task_state = HSM_ST_ERR;
4266 goto fsm_start;
4267 }
4268
4269 /* no more data to transfer */
4270 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4271 ap->id, status);
4272
4273 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
4275 /* complete taskfile transaction */
Jeff Garzika7dac442005-10-30 04:44:42 -05004276 ata_qc_complete(qc, ac_err_mask(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 break;
4278
Albert Lee312f7da2005-09-27 17:38:03 +08004279 case HSM_ST_ERR:
4280 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4281 ap->id, status, host_stat);
4282
4283 ap->hsm_task_state = HSM_ST_IDLE;
4284 ata_qc_complete(qc, status | ATA_ERR);
4285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 default:
4287 goto idle_irq;
4288 }
4289
4290 return 1; /* irq handled */
4291
4292idle_irq:
4293 ap->stats.idle_irq++;
4294
4295#ifdef ATA_IRQ_TRAP
4296 if ((ap->stats.idle_irq % 1000) == 0) {
4297 handled = 1;
4298 ata_irq_ack(ap, 0); /* debug trap */
4299 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4300 }
4301#endif
4302 return 0; /* irq not handled */
4303}
4304
4305/**
4306 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004307 * @irq: irq line (unused)
4308 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 * @regs: unused
4310 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004311 * Default interrupt handler for PCI IDE devices. Calls
4312 * ata_host_intr() for each port that is not disabled.
4313 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004315 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 *
4317 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004318 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 */
4320
4321irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4322{
4323 struct ata_host_set *host_set = dev_instance;
4324 unsigned int i;
4325 unsigned int handled = 0;
4326 unsigned long flags;
4327
4328 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4329 spin_lock_irqsave(&host_set->lock, flags);
4330
4331 for (i = 0; i < host_set->n_ports; i++) {
4332 struct ata_port *ap;
4333
4334 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004335 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08004336 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 struct ata_queued_cmd *qc;
4338
4339 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004340 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004341 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 handled |= ata_host_intr(ap, qc);
4343 }
4344 }
4345
4346 spin_unlock_irqrestore(&host_set->lock, flags);
4347
4348 return IRQ_RETVAL(handled);
4349}
4350
4351/**
Edward Falk0baab862005-06-02 18:17:13 -04004352 * ata_port_start - Set port up for dma.
4353 * @ap: Port to initialize
4354 *
4355 * Called just after data structures for each port are
4356 * initialized. Allocates space for PRD table.
4357 *
4358 * May be used as the port_start() entry in ata_port_operations.
4359 *
4360 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004361 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004362 */
4363
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364int ata_port_start (struct ata_port *ap)
4365{
4366 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004367 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
4369 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4370 if (!ap->prd)
4371 return -ENOMEM;
4372
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004373 rc = ata_pad_alloc(ap, dev);
4374 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004375 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004376 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004377 }
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4380
4381 return 0;
4382}
4383
Edward Falk0baab862005-06-02 18:17:13 -04004384
4385/**
4386 * ata_port_stop - Undo ata_port_start()
4387 * @ap: Port to shut down
4388 *
4389 * Frees the PRD table.
4390 *
4391 * May be used as the port_stop() entry in ata_port_operations.
4392 *
4393 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004394 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004395 */
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397void ata_port_stop (struct ata_port *ap)
4398{
4399 struct device *dev = ap->host_set->dev;
4400
4401 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004402 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403}
4404
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004405void ata_host_stop (struct ata_host_set *host_set)
4406{
4407 if (host_set->mmio_base)
4408 iounmap(host_set->mmio_base);
4409}
4410
4411
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412/**
4413 * ata_host_remove - Unregister SCSI host structure with upper layers
4414 * @ap: Port to unregister
4415 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4416 *
4417 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004418 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 */
4420
4421static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4422{
4423 struct Scsi_Host *sh = ap->host;
4424
4425 DPRINTK("ENTER\n");
4426
4427 if (do_unregister)
4428 scsi_remove_host(sh);
4429
4430 ap->ops->port_stop(ap);
4431}
4432
4433/**
4434 * ata_host_init - Initialize an ata_port structure
4435 * @ap: Structure to initialize
4436 * @host: associated SCSI mid-layer structure
4437 * @host_set: Collection of hosts to which @ap belongs
4438 * @ent: Probe information provided by low-level driver
4439 * @port_no: Port number associated with this ata_port
4440 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004441 * Initialize a new ata_port structure, and its associated
4442 * scsi_host.
4443 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004445 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 */
4447
4448static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4449 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004450 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
4452 unsigned int i;
4453
4454 host->max_id = 16;
4455 host->max_lun = 1;
4456 host->max_channel = 1;
4457 host->unique_id = ata_unique_id++;
4458 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 ap->flags = ATA_FLAG_PORT_DISABLED;
4461 ap->id = host->unique_id;
4462 ap->host = host;
4463 ap->ctl = ATA_DEVCTL_OBS;
4464 ap->host_set = host_set;
4465 ap->port_no = port_no;
4466 ap->hard_port_no =
4467 ent->legacy_mode ? ent->hard_port_no : port_no;
4468 ap->pio_mask = ent->pio_mask;
4469 ap->mwdma_mask = ent->mwdma_mask;
4470 ap->udma_mask = ent->udma_mask;
4471 ap->flags |= ent->host_flags;
4472 ap->ops = ent->port_ops;
4473 ap->cbl = ATA_CBL_NONE;
4474 ap->active_tag = ATA_TAG_POISON;
4475 ap->last_ctl = 0xFF;
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4478
4479 for (i = 0; i < ATA_MAX_DEVICES; i++)
4480 ap->device[i].devno = i;
4481
4482#ifdef ATA_IRQ_TRAP
4483 ap->stats.unhandled_irq = 1;
4484 ap->stats.idle_irq = 1;
4485#endif
4486
4487 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4488}
4489
4490/**
4491 * ata_host_add - Attach low-level ATA driver to system
4492 * @ent: Information provided by low-level driver
4493 * @host_set: Collections of ports to which we add
4494 * @port_no: Port number associated with this host
4495 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004496 * Attach low-level ATA driver to system.
4497 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004499 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 *
4501 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004502 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 */
4504
Jeff Garzik057ace52005-10-22 14:27:05 -04004505static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 struct ata_host_set *host_set,
4507 unsigned int port_no)
4508{
4509 struct Scsi_Host *host;
4510 struct ata_port *ap;
4511 int rc;
4512
4513 DPRINTK("ENTER\n");
4514 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4515 if (!host)
4516 return NULL;
4517
4518 ap = (struct ata_port *) &host->hostdata[0];
4519
4520 ata_host_init(ap, host, host_set, ent, port_no);
4521
4522 rc = ap->ops->port_start(ap);
4523 if (rc)
4524 goto err_out;
4525
4526 return ap;
4527
4528err_out:
4529 scsi_host_put(host);
4530 return NULL;
4531}
4532
4533/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004534 * ata_device_add - Register hardware device with ATA and SCSI layers
4535 * @ent: Probe information describing hardware device to be registered
4536 *
4537 * This function processes the information provided in the probe
4538 * information struct @ent, allocates the necessary ATA and SCSI
4539 * host information structures, initializes them, and registers
4540 * everything with requisite kernel subsystems.
4541 *
4542 * This function requests irqs, probes the ATA bus, and probes
4543 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 *
4545 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004546 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 *
4548 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004549 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 */
4551
Jeff Garzik057ace52005-10-22 14:27:05 -04004552int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553{
4554 unsigned int count = 0, i;
4555 struct device *dev = ent->dev;
4556 struct ata_host_set *host_set;
4557
4558 DPRINTK("ENTER\n");
4559 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004560 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4562 if (!host_set)
4563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 spin_lock_init(&host_set->lock);
4565
4566 host_set->dev = dev;
4567 host_set->n_ports = ent->n_ports;
4568 host_set->irq = ent->irq;
4569 host_set->mmio_base = ent->mmio_base;
4570 host_set->private_data = ent->private_data;
4571 host_set->ops = ent->port_ops;
4572
4573 /* register each port bound to this device */
4574 for (i = 0; i < ent->n_ports; i++) {
4575 struct ata_port *ap;
4576 unsigned long xfer_mode_mask;
4577
4578 ap = ata_host_add(ent, host_set, i);
4579 if (!ap)
4580 goto err_out;
4581
4582 host_set->ports[i] = ap;
4583 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4584 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4585 (ap->pio_mask << ATA_SHIFT_PIO);
4586
4587 /* print per-port info to dmesg */
4588 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4589 "bmdma 0x%lX irq %lu\n",
4590 ap->id,
4591 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4592 ata_mode_string(xfer_mode_mask),
4593 ap->ioaddr.cmd_addr,
4594 ap->ioaddr.ctl_addr,
4595 ap->ioaddr.bmdma_addr,
4596 ent->irq);
4597
4598 ata_chk_status(ap);
4599 host_set->ops->irq_clear(ap);
4600 count++;
4601 }
4602
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004603 if (!count)
4604 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
4606 /* obtain irq, that is shared between channels */
4607 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4608 DRV_NAME, host_set))
4609 goto err_out;
4610
4611 /* perform each probe synchronously */
4612 DPRINTK("probe begin\n");
4613 for (i = 0; i < count; i++) {
4614 struct ata_port *ap;
4615 int rc;
4616
4617 ap = host_set->ports[i];
4618
4619 DPRINTK("ata%u: probe begin\n", ap->id);
4620 rc = ata_bus_probe(ap);
4621 DPRINTK("ata%u: probe end\n", ap->id);
4622
4623 if (rc) {
4624 /* FIXME: do something useful here?
4625 * Current libata behavior will
4626 * tear down everything when
4627 * the module is removed
4628 * or the h/w is unplugged.
4629 */
4630 }
4631
4632 rc = scsi_add_host(ap->host, dev);
4633 if (rc) {
4634 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4635 ap->id);
4636 /* FIXME: do something useful here */
4637 /* FIXME: handle unconditional calls to
4638 * scsi_scan_host and ata_host_remove, below,
4639 * at the very least
4640 */
4641 }
4642 }
4643
4644 /* probes are done, now scan each port's disk(s) */
4645 DPRINTK("probe begin\n");
4646 for (i = 0; i < count; i++) {
4647 struct ata_port *ap = host_set->ports[i];
4648
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004649 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 }
4651
4652 dev_set_drvdata(dev, host_set);
4653
4654 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4655 return ent->n_ports; /* success */
4656
4657err_out:
4658 for (i = 0; i < count; i++) {
4659 ata_host_remove(host_set->ports[i], 1);
4660 scsi_host_put(host_set->ports[i]->host);
4661 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004662err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 kfree(host_set);
4664 VPRINTK("EXIT, returning 0\n");
4665 return 0;
4666}
4667
4668/**
Alan Cox17b14452005-09-15 15:44:00 +01004669 * ata_host_set_remove - PCI layer callback for device removal
4670 * @host_set: ATA host set that was removed
4671 *
4672 * Unregister all objects associated with this host set. Free those
4673 * objects.
4674 *
4675 * LOCKING:
4676 * Inherited from calling layer (may sleep).
4677 */
4678
Alan Cox17b14452005-09-15 15:44:00 +01004679void ata_host_set_remove(struct ata_host_set *host_set)
4680{
4681 struct ata_port *ap;
4682 unsigned int i;
4683
4684 for (i = 0; i < host_set->n_ports; i++) {
4685 ap = host_set->ports[i];
4686 scsi_remove_host(ap->host);
4687 }
4688
4689 free_irq(host_set->irq, host_set);
4690
4691 for (i = 0; i < host_set->n_ports; i++) {
4692 ap = host_set->ports[i];
4693
4694 ata_scsi_release(ap->host);
4695
4696 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4697 struct ata_ioports *ioaddr = &ap->ioaddr;
4698
4699 if (ioaddr->cmd_addr == 0x1f0)
4700 release_region(0x1f0, 8);
4701 else if (ioaddr->cmd_addr == 0x170)
4702 release_region(0x170, 8);
4703 }
4704
4705 scsi_host_put(ap->host);
4706 }
4707
4708 if (host_set->ops->host_stop)
4709 host_set->ops->host_stop(host_set);
4710
4711 kfree(host_set);
4712}
4713
4714/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 * ata_scsi_release - SCSI layer callback hook for host unload
4716 * @host: libata host to be unloaded
4717 *
4718 * Performs all duties necessary to shut down a libata port...
4719 * Kill port kthread, disable port, and release resources.
4720 *
4721 * LOCKING:
4722 * Inherited from SCSI layer.
4723 *
4724 * RETURNS:
4725 * One.
4726 */
4727
4728int ata_scsi_release(struct Scsi_Host *host)
4729{
4730 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4731
4732 DPRINTK("ENTER\n");
4733
4734 ap->ops->port_disable(ap);
4735 ata_host_remove(ap, 0);
4736
4737 DPRINTK("EXIT\n");
4738 return 1;
4739}
4740
4741/**
4742 * ata_std_ports - initialize ioaddr with standard port offsets.
4743 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004744 *
4745 * Utility function which initializes data_addr, error_addr,
4746 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4747 * device_addr, status_addr, and command_addr to standard offsets
4748 * relative to cmd_addr.
4749 *
4750 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 */
Edward Falk0baab862005-06-02 18:17:13 -04004752
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753void ata_std_ports(struct ata_ioports *ioaddr)
4754{
4755 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4756 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4757 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4758 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4759 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4760 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4761 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4762 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4763 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4764 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4765}
4766
4767static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004768ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
4770 struct ata_probe_ent *probe_ent;
4771
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004772 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 if (!probe_ent) {
4774 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4775 kobject_name(&(dev->kobj)));
4776 return NULL;
4777 }
4778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 INIT_LIST_HEAD(&probe_ent->node);
4780 probe_ent->dev = dev;
4781
4782 probe_ent->sht = port->sht;
4783 probe_ent->host_flags = port->host_flags;
4784 probe_ent->pio_mask = port->pio_mask;
4785 probe_ent->mwdma_mask = port->mwdma_mask;
4786 probe_ent->udma_mask = port->udma_mask;
4787 probe_ent->port_ops = port->port_ops;
4788
4789 return probe_ent;
4790}
4791
Edward Falk0baab862005-06-02 18:17:13 -04004792
4793
Jeff Garzik374b1872005-08-30 05:42:52 -04004794#ifdef CONFIG_PCI
4795
4796void ata_pci_host_stop (struct ata_host_set *host_set)
4797{
4798 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4799
4800 pci_iounmap(pdev, host_set->mmio_base);
4801}
4802
Edward Falk0baab862005-06-02 18:17:13 -04004803/**
4804 * ata_pci_init_native_mode - Initialize native-mode driver
4805 * @pdev: pci device to be initialized
4806 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004807 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004808 *
4809 * Utility function which allocates and initializes an
4810 * ata_probe_ent structure for a standard dual-port
4811 * PIO-based IDE controller. The returned ata_probe_ent
4812 * structure can be passed to ata_device_add(). The returned
4813 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004814 *
4815 * The caller need only pass the address of the primary port, the
4816 * secondary will be deduced automatically. If the device has non
4817 * standard secondary port mappings this function can be called twice,
4818 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004819 */
4820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004822ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
4824 struct ata_probe_ent *probe_ent =
4825 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004826 int p = 0;
4827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 if (!probe_ent)
4829 return NULL;
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 probe_ent->irq = pdev->irq;
4832 probe_ent->irq_flags = SA_SHIRQ;
4833
Alan Cox47a86592005-10-04 08:09:19 -04004834 if (ports & ATA_PORT_PRIMARY) {
4835 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4836 probe_ent->port[p].altstatus_addr =
4837 probe_ent->port[p].ctl_addr =
4838 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4839 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4840 ata_std_ports(&probe_ent->port[p]);
4841 p++;
4842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Alan Cox47a86592005-10-04 08:09:19 -04004844 if (ports & ATA_PORT_SECONDARY) {
4845 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4846 probe_ent->port[p].altstatus_addr =
4847 probe_ent->port[p].ctl_addr =
4848 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4849 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4850 ata_std_ports(&probe_ent->port[p]);
4851 p++;
4852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Alan Cox47a86592005-10-04 08:09:19 -04004854 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 return probe_ent;
4856}
4857
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004858static 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 -07004859{
Alan Cox47a86592005-10-04 08:09:19 -04004860 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004862 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 if (!probe_ent)
4864 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004867 probe_ent->n_ports = 1;
4868 probe_ent->hard_port_no = port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Alan Cox47a86592005-10-04 08:09:19 -04004870 switch(port_num)
4871 {
4872 case 0:
4873 probe_ent->irq = 14;
4874 probe_ent->port[0].cmd_addr = 0x1f0;
4875 probe_ent->port[0].altstatus_addr =
4876 probe_ent->port[0].ctl_addr = 0x3f6;
4877 break;
4878 case 1:
4879 probe_ent->irq = 15;
4880 probe_ent->port[0].cmd_addr = 0x170;
4881 probe_ent->port[0].altstatus_addr =
4882 probe_ent->port[0].ctl_addr = 0x376;
4883 break;
4884 }
4885 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 return probe_ent;
4888}
4889
4890/**
4891 * ata_pci_init_one - Initialize/register PCI IDE host controller
4892 * @pdev: Controller to be initialized
4893 * @port_info: Information from low-level host driver
4894 * @n_ports: Number of ports attached to host controller
4895 *
Edward Falk0baab862005-06-02 18:17:13 -04004896 * This is a helper function which can be called from a driver's
4897 * xxx_init_one() probe function if the hardware uses traditional
4898 * IDE taskfile registers.
4899 *
4900 * This function calls pci_enable_device(), reserves its register
4901 * regions, sets the dma mask, enables bus master mode, and calls
4902 * ata_device_add()
4903 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 * LOCKING:
4905 * Inherited from PCI layer (may sleep).
4906 *
4907 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004908 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 */
4910
4911int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4912 unsigned int n_ports)
4913{
Alan Cox47a86592005-10-04 08:09:19 -04004914 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 struct ata_port_info *port[2];
4916 u8 tmp8, mask;
4917 unsigned int legacy_mode = 0;
4918 int disable_dev_on_err = 1;
4919 int rc;
4920
4921 DPRINTK("ENTER\n");
4922
4923 port[0] = port_info[0];
4924 if (n_ports > 1)
4925 port[1] = port_info[1];
4926 else
4927 port[1] = port[0];
4928
4929 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4930 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004931 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4933 mask = (1 << 2) | (1 << 0);
4934 if ((tmp8 & mask) != mask)
4935 legacy_mode = (1 << 3);
4936 }
4937
4938 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004939 if ((!legacy_mode) && (n_ports > 2)) {
4940 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4941 n_ports = 2;
4942 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 }
4944
Alan Cox47a86592005-10-04 08:09:19 -04004945 /* FIXME: Really for ATA it isn't safe because the device may be
4946 multi-purpose and we want to leave it alone if it was already
4947 enabled. Secondly for shared use as Arjan says we want refcounting
4948
4949 Checking dev->is_enabled is insufficient as this is not set at
4950 boot for the primary video which is BIOS enabled
4951 */
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 rc = pci_enable_device(pdev);
4954 if (rc)
4955 return rc;
4956
4957 rc = pci_request_regions(pdev, DRV_NAME);
4958 if (rc) {
4959 disable_dev_on_err = 0;
4960 goto err_out;
4961 }
4962
Alan Cox47a86592005-10-04 08:09:19 -04004963 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 if (legacy_mode) {
4965 if (!request_region(0x1f0, 8, "libata")) {
4966 struct resource *conflict, res;
4967 res.start = 0x1f0;
4968 res.end = 0x1f0 + 8 - 1;
4969 conflict = ____request_resource(&ioport_resource, &res);
4970 if (!strcmp(conflict->name, "libata"))
4971 legacy_mode |= (1 << 0);
4972 else {
4973 disable_dev_on_err = 0;
4974 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4975 }
4976 } else
4977 legacy_mode |= (1 << 0);
4978
4979 if (!request_region(0x170, 8, "libata")) {
4980 struct resource *conflict, res;
4981 res.start = 0x170;
4982 res.end = 0x170 + 8 - 1;
4983 conflict = ____request_resource(&ioport_resource, &res);
4984 if (!strcmp(conflict->name, "libata"))
4985 legacy_mode |= (1 << 1);
4986 else {
4987 disable_dev_on_err = 0;
4988 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4989 }
4990 } else
4991 legacy_mode |= (1 << 1);
4992 }
4993
4994 /* we have legacy mode, but all ports are unavailable */
4995 if (legacy_mode == (1 << 3)) {
4996 rc = -EBUSY;
4997 goto err_out_regions;
4998 }
4999
5000 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
5001 if (rc)
5002 goto err_out_regions;
5003 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
5004 if (rc)
5005 goto err_out_regions;
5006
5007 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04005008 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005009 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04005010 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005011 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04005012 } else {
5013 if (n_ports == 2)
5014 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
5015 else
5016 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
5017 }
5018 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 rc = -ENOMEM;
5020 goto err_out_regions;
5021 }
5022
5023 pci_set_master(pdev);
5024
5025 /* FIXME: check ata_device_add return */
5026 if (legacy_mode) {
5027 if (legacy_mode & (1 << 0))
5028 ata_device_add(probe_ent);
5029 if (legacy_mode & (1 << 1))
5030 ata_device_add(probe_ent2);
5031 } else
5032 ata_device_add(probe_ent);
5033
5034 kfree(probe_ent);
5035 kfree(probe_ent2);
5036
5037 return 0;
5038
5039err_out_regions:
5040 if (legacy_mode & (1 << 0))
5041 release_region(0x1f0, 8);
5042 if (legacy_mode & (1 << 1))
5043 release_region(0x170, 8);
5044 pci_release_regions(pdev);
5045err_out:
5046 if (disable_dev_on_err)
5047 pci_disable_device(pdev);
5048 return rc;
5049}
5050
5051/**
5052 * ata_pci_remove_one - PCI layer callback for device removal
5053 * @pdev: PCI device that was removed
5054 *
5055 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005056 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 * Handle this by unregistering all objects associated
5058 * with this PCI device. Free those objects. Then finally
5059 * release PCI resources and disable device.
5060 *
5061 * LOCKING:
5062 * Inherited from PCI layer (may sleep).
5063 */
5064
5065void ata_pci_remove_one (struct pci_dev *pdev)
5066{
5067 struct device *dev = pci_dev_to_dev(pdev);
5068 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
Alan Cox17b14452005-09-15 15:44:00 +01005070 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 pci_release_regions(pdev);
5072 pci_disable_device(pdev);
5073 dev_set_drvdata(dev, NULL);
5074}
5075
5076/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005077int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078{
5079 unsigned long tmp = 0;
5080
5081 switch (bits->width) {
5082 case 1: {
5083 u8 tmp8 = 0;
5084 pci_read_config_byte(pdev, bits->reg, &tmp8);
5085 tmp = tmp8;
5086 break;
5087 }
5088 case 2: {
5089 u16 tmp16 = 0;
5090 pci_read_config_word(pdev, bits->reg, &tmp16);
5091 tmp = tmp16;
5092 break;
5093 }
5094 case 4: {
5095 u32 tmp32 = 0;
5096 pci_read_config_dword(pdev, bits->reg, &tmp32);
5097 tmp = tmp32;
5098 break;
5099 }
5100
5101 default:
5102 return -EINVAL;
5103 }
5104
5105 tmp &= bits->mask;
5106
5107 return (tmp == bits->val) ? 1 : 0;
5108}
5109#endif /* CONFIG_PCI */
5110
5111
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112static int __init ata_init(void)
5113{
5114 ata_wq = create_workqueue("ata");
5115 if (!ata_wq)
5116 return -ENOMEM;
5117
5118 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5119 return 0;
5120}
5121
5122static void __exit ata_exit(void)
5123{
5124 destroy_workqueue(ata_wq);
5125}
5126
5127module_init(ata_init);
5128module_exit(ata_exit);
5129
Jeff Garzik67846b32005-10-05 02:58:32 -04005130static unsigned long ratelimit_time;
5131static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5132
5133int ata_ratelimit(void)
5134{
5135 int rc;
5136 unsigned long flags;
5137
5138 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5139
5140 if (time_after(jiffies, ratelimit_time)) {
5141 rc = 1;
5142 ratelimit_time = jiffies + (HZ/5);
5143 } else
5144 rc = 0;
5145
5146 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5147
5148 return rc;
5149}
5150
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151/*
5152 * libata is essentially a library of internal helper functions for
5153 * low-level ATA host controller drivers. As such, the API/ABI is
5154 * likely to change as new drivers are added and updated.
5155 * Do not depend on ABI/API stability.
5156 */
5157
5158EXPORT_SYMBOL_GPL(ata_std_bios_param);
5159EXPORT_SYMBOL_GPL(ata_std_ports);
5160EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005161EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162EXPORT_SYMBOL_GPL(ata_sg_init);
5163EXPORT_SYMBOL_GPL(ata_sg_init_one);
5164EXPORT_SYMBOL_GPL(ata_qc_complete);
5165EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5166EXPORT_SYMBOL_GPL(ata_eng_timeout);
5167EXPORT_SYMBOL_GPL(ata_tf_load);
5168EXPORT_SYMBOL_GPL(ata_tf_read);
5169EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5170EXPORT_SYMBOL_GPL(ata_std_dev_select);
5171EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5172EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5173EXPORT_SYMBOL_GPL(ata_check_status);
5174EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175EXPORT_SYMBOL_GPL(ata_exec_command);
5176EXPORT_SYMBOL_GPL(ata_port_start);
5177EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005178EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179EXPORT_SYMBOL_GPL(ata_interrupt);
5180EXPORT_SYMBOL_GPL(ata_qc_prep);
5181EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5182EXPORT_SYMBOL_GPL(ata_bmdma_start);
5183EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5184EXPORT_SYMBOL_GPL(ata_bmdma_status);
5185EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5186EXPORT_SYMBOL_GPL(ata_port_probe);
5187EXPORT_SYMBOL_GPL(sata_phy_reset);
5188EXPORT_SYMBOL_GPL(__sata_phy_reset);
5189EXPORT_SYMBOL_GPL(ata_bus_reset);
5190EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005191EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5193EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5194EXPORT_SYMBOL_GPL(ata_scsi_error);
5195EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5196EXPORT_SYMBOL_GPL(ata_scsi_release);
5197EXPORT_SYMBOL_GPL(ata_host_intr);
5198EXPORT_SYMBOL_GPL(ata_dev_classify);
5199EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005200EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5202
Alan Cox452503f2005-10-21 19:01:32 -04005203EXPORT_SYMBOL_GPL(ata_timing_compute);
5204EXPORT_SYMBOL_GPL(ata_timing_merge);
5205
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206#ifdef CONFIG_PCI
5207EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005208EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5210EXPORT_SYMBOL_GPL(ata_pci_init_one);
5211EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5212#endif /* CONFIG_PCI */