blob: 5f1d7580218dde548ccd0d8ba4f71589ed5efcc6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040051#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100052#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "scsi_priv.h"
Jeff Garzik193515d2005-11-07 00:59:37 -050055#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
64static unsigned int ata_busy_sleep (struct ata_port *ap,
65 unsigned long tmout_pat,
66 unsigned long tmout);
Albert Lee59a10b12005-10-12 15:09:42 +080067static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ec2005-05-12 15:29:42 -040068static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void ata_set_mode(struct ata_port *ap);
70static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040071static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040073static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 u8 *xfer_mode_out,
75 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void __ata_qc_complete(struct ata_queued_cmd *qc);
77
78static unsigned int ata_unique_id = 1;
79static struct workqueue_struct *ata_wq;
80
Jeff Garzik1623c812005-08-30 03:37:42 -040081int atapi_enabled = 0;
82module_param(atapi_enabled, int, 0444);
83MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_AUTHOR("Jeff Garzik");
86MODULE_DESCRIPTION("Library module for ATA devices");
87MODULE_LICENSE("GPL");
88MODULE_VERSION(DRV_VERSION);
89
90/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040091 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * @ap: Port to which output is sent
93 * @tf: ATA taskfile register set
94 *
95 * Outputs ATA taskfile to standard ATA host controller.
96 *
97 * LOCKING:
98 * Inherited from caller.
99 */
100
Jeff Garzik057ace52005-10-22 14:27:05 -0400101static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct ata_ioports *ioaddr = &ap->ioaddr;
104 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
105
106 if (tf->ctl != ap->last_ctl) {
107 outb(tf->ctl, ioaddr->ctl_addr);
108 ap->last_ctl = tf->ctl;
109 ata_wait_idle(ap);
110 }
111
112 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
113 outb(tf->hob_feature, ioaddr->feature_addr);
114 outb(tf->hob_nsect, ioaddr->nsect_addr);
115 outb(tf->hob_lbal, ioaddr->lbal_addr);
116 outb(tf->hob_lbam, ioaddr->lbam_addr);
117 outb(tf->hob_lbah, ioaddr->lbah_addr);
118 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
119 tf->hob_feature,
120 tf->hob_nsect,
121 tf->hob_lbal,
122 tf->hob_lbam,
123 tf->hob_lbah);
124 }
125
126 if (is_addr) {
127 outb(tf->feature, ioaddr->feature_addr);
128 outb(tf->nsect, ioaddr->nsect_addr);
129 outb(tf->lbal, ioaddr->lbal_addr);
130 outb(tf->lbam, ioaddr->lbam_addr);
131 outb(tf->lbah, ioaddr->lbah_addr);
132 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
133 tf->feature,
134 tf->nsect,
135 tf->lbal,
136 tf->lbam,
137 tf->lbah);
138 }
139
140 if (tf->flags & ATA_TFLAG_DEVICE) {
141 outb(tf->device, ioaddr->device_addr);
142 VPRINTK("device 0x%X\n", tf->device);
143 }
144
145 ata_wait_idle(ap);
146}
147
148/**
149 * ata_tf_load_mmio - send taskfile registers to host controller
150 * @ap: Port to which output is sent
151 * @tf: ATA taskfile register set
152 *
153 * Outputs ATA taskfile to standard ATA host controller using MMIO.
154 *
155 * LOCKING:
156 * Inherited from caller.
157 */
158
Jeff Garzik057ace52005-10-22 14:27:05 -0400159static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct ata_ioports *ioaddr = &ap->ioaddr;
162 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
163
164 if (tf->ctl != ap->last_ctl) {
165 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
166 ap->last_ctl = tf->ctl;
167 ata_wait_idle(ap);
168 }
169
170 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
171 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
172 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
173 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
174 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
175 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
176 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
177 tf->hob_feature,
178 tf->hob_nsect,
179 tf->hob_lbal,
180 tf->hob_lbam,
181 tf->hob_lbah);
182 }
183
184 if (is_addr) {
185 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
186 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
187 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
188 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
189 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
190 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
191 tf->feature,
192 tf->nsect,
193 tf->lbal,
194 tf->lbam,
195 tf->lbah);
196 }
197
198 if (tf->flags & ATA_TFLAG_DEVICE) {
199 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
200 VPRINTK("device 0x%X\n", tf->device);
201 }
202
203 ata_wait_idle(ap);
204}
205
Edward Falk0baab862005-06-02 18:17:13 -0400206
207/**
208 * ata_tf_load - send taskfile registers to host controller
209 * @ap: Port to which output is sent
210 * @tf: ATA taskfile register set
211 *
212 * Outputs ATA taskfile to standard ATA host controller using MMIO
213 * or PIO as indicated by the ATA_FLAG_MMIO flag.
214 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
215 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
216 * hob_lbal, hob_lbam, and hob_lbah.
217 *
218 * This function waits for idle (!BUSY and !DRQ) after writing
219 * registers. If the control register has a new value, this
220 * function also waits for idle after writing control and before
221 * writing the remaining registers.
222 *
223 * May be used as the tf_load() entry in ata_port_operations.
224 *
225 * LOCKING:
226 * Inherited from caller.
227 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400228void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 if (ap->flags & ATA_FLAG_MMIO)
231 ata_tf_load_mmio(ap, tf);
232 else
233 ata_tf_load_pio(ap, tf);
234}
235
236/**
Edward Falk0baab862005-06-02 18:17:13 -0400237 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * @ap: port to which command is being issued
239 * @tf: ATA taskfile register set
240 *
Edward Falk0baab862005-06-02 18:17:13 -0400241 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * synchronization with interrupt handler / other threads.
243 *
244 * LOCKING:
245 * spin_lock_irqsave(host_set lock)
246 */
247
Jeff Garzik057ace52005-10-22 14:27:05 -0400248static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
251
252 outb(tf->command, ap->ioaddr.command_addr);
253 ata_pause(ap);
254}
255
256
257/**
258 * ata_exec_command_mmio - issue ATA command to host controller
259 * @ap: port to which command is being issued
260 * @tf: ATA taskfile register set
261 *
262 * Issues MMIO write to ATA command register, with proper
263 * synchronization with interrupt handler / other threads.
264 *
265 * LOCKING:
266 * spin_lock_irqsave(host_set lock)
267 */
268
Jeff Garzik057ace52005-10-22 14:27:05 -0400269static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
272
273 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
274 ata_pause(ap);
275}
276
Edward Falk0baab862005-06-02 18:17:13 -0400277
278/**
279 * ata_exec_command - issue ATA command to host controller
280 * @ap: port to which command is being issued
281 * @tf: ATA taskfile register set
282 *
283 * Issues PIO/MMIO write to ATA command register, with proper
284 * synchronization with interrupt handler / other threads.
285 *
286 * LOCKING:
287 * spin_lock_irqsave(host_set lock)
288 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400289void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 if (ap->flags & ATA_FLAG_MMIO)
292 ata_exec_command_mmio(ap, tf);
293 else
294 ata_exec_command_pio(ap, tf);
295}
296
297/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * ata_tf_to_host - issue ATA taskfile to host controller
299 * @ap: port to which command is being issued
300 * @tf: ATA taskfile register set
301 *
302 * Issues ATA taskfile register set to ATA host controller,
303 * with proper synchronization with interrupt handler and
304 * other threads.
305 *
306 * LOCKING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * spin_lock_irqsave(host_set lock)
308 */
309
Jeff Garzike5338252005-10-30 21:37:17 -0500310static inline void ata_tf_to_host(struct ata_port *ap,
311 const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 ap->ops->tf_load(ap, tf);
314 ap->ops->exec_command(ap, tf);
315}
316
317/**
Edward Falk0baab862005-06-02 18:17:13 -0400318 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * @ap: Port from which input is read
320 * @tf: ATA taskfile register set for storing input
321 *
322 * Reads ATA taskfile registers for currently-selected device
323 * into @tf.
324 *
325 * LOCKING:
326 * Inherited from caller.
327 */
328
329static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
330{
331 struct ata_ioports *ioaddr = &ap->ioaddr;
332
Jeff Garzikac19bff2005-10-29 13:58:21 -0400333 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400334 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 tf->nsect = inb(ioaddr->nsect_addr);
336 tf->lbal = inb(ioaddr->lbal_addr);
337 tf->lbam = inb(ioaddr->lbam_addr);
338 tf->lbah = inb(ioaddr->lbah_addr);
339 tf->device = inb(ioaddr->device_addr);
340
341 if (tf->flags & ATA_TFLAG_LBA48) {
342 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
343 tf->hob_feature = inb(ioaddr->error_addr);
344 tf->hob_nsect = inb(ioaddr->nsect_addr);
345 tf->hob_lbal = inb(ioaddr->lbal_addr);
346 tf->hob_lbam = inb(ioaddr->lbam_addr);
347 tf->hob_lbah = inb(ioaddr->lbah_addr);
348 }
349}
350
351/**
352 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
353 * @ap: Port from which input is read
354 * @tf: ATA taskfile register set for storing input
355 *
356 * Reads ATA taskfile registers for currently-selected device
357 * into @tf via MMIO.
358 *
359 * LOCKING:
360 * Inherited from caller.
361 */
362
363static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
364{
365 struct ata_ioports *ioaddr = &ap->ioaddr;
366
Jeff Garzikac19bff2005-10-29 13:58:21 -0400367 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400368 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
370 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
371 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
372 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
373 tf->device = readb((void __iomem *)ioaddr->device_addr);
374
375 if (tf->flags & ATA_TFLAG_LBA48) {
376 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
377 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
378 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
379 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
380 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
381 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
382 }
383}
384
Edward Falk0baab862005-06-02 18:17:13 -0400385
386/**
387 * ata_tf_read - input device's ATA taskfile shadow registers
388 * @ap: Port from which input is read
389 * @tf: ATA taskfile register set for storing input
390 *
391 * Reads ATA taskfile registers for currently-selected device
392 * into @tf.
393 *
394 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
395 * is set, also reads the hob registers.
396 *
397 * May be used as the tf_read() entry in ata_port_operations.
398 *
399 * LOCKING:
400 * Inherited from caller.
401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
403{
404 if (ap->flags & ATA_FLAG_MMIO)
405 ata_tf_read_mmio(ap, tf);
406 else
407 ata_tf_read_pio(ap, tf);
408}
409
410/**
411 * ata_check_status_pio - Read device status reg & clear interrupt
412 * @ap: port where the device is
413 *
414 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400415 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * from this device
417 *
418 * LOCKING:
419 * Inherited from caller.
420 */
421static u8 ata_check_status_pio(struct ata_port *ap)
422{
423 return inb(ap->ioaddr.status_addr);
424}
425
426/**
427 * ata_check_status_mmio - Read device status reg & clear interrupt
428 * @ap: port where the device is
429 *
430 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400431 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * from this device
433 *
434 * LOCKING:
435 * Inherited from caller.
436 */
437static u8 ata_check_status_mmio(struct ata_port *ap)
438{
439 return readb((void __iomem *) ap->ioaddr.status_addr);
440}
441
Edward Falk0baab862005-06-02 18:17:13 -0400442
443/**
444 * ata_check_status - Read device status reg & clear interrupt
445 * @ap: port where the device is
446 *
447 * Reads ATA taskfile status register for currently-selected device
448 * and return its value. This also clears pending interrupts
449 * from this device
450 *
451 * May be used as the check_status() entry in ata_port_operations.
452 *
453 * LOCKING:
454 * Inherited from caller.
455 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456u8 ata_check_status(struct ata_port *ap)
457{
458 if (ap->flags & ATA_FLAG_MMIO)
459 return ata_check_status_mmio(ap);
460 return ata_check_status_pio(ap);
461}
462
Edward Falk0baab862005-06-02 18:17:13 -0400463
464/**
465 * ata_altstatus - Read device alternate status reg
466 * @ap: port where the device is
467 *
468 * Reads ATA taskfile alternate status register for
469 * currently-selected device and return its value.
470 *
471 * Note: may NOT be used as the check_altstatus() entry in
472 * ata_port_operations.
473 *
474 * LOCKING:
475 * Inherited from caller.
476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477u8 ata_altstatus(struct ata_port *ap)
478{
479 if (ap->ops->check_altstatus)
480 return ap->ops->check_altstatus(ap);
481
482 if (ap->flags & ATA_FLAG_MMIO)
483 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
484 return inb(ap->ioaddr.altstatus_addr);
485}
486
Edward Falk0baab862005-06-02 18:17:13 -0400487
488/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
490 * @tf: Taskfile to convert
491 * @fis: Buffer into which data will output
492 * @pmp: Port multiplier port
493 *
494 * Converts a standard ATA taskfile to a Serial ATA
495 * FIS structure (Register - Host to Device).
496 *
497 * LOCKING:
498 * Inherited from caller.
499 */
500
Jeff Garzik057ace52005-10-22 14:27:05 -0400501void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 fis[0] = 0x27; /* Register - Host to Device FIS */
504 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
505 bit 7 indicates Command FIS */
506 fis[2] = tf->command;
507 fis[3] = tf->feature;
508
509 fis[4] = tf->lbal;
510 fis[5] = tf->lbam;
511 fis[6] = tf->lbah;
512 fis[7] = tf->device;
513
514 fis[8] = tf->hob_lbal;
515 fis[9] = tf->hob_lbam;
516 fis[10] = tf->hob_lbah;
517 fis[11] = tf->hob_feature;
518
519 fis[12] = tf->nsect;
520 fis[13] = tf->hob_nsect;
521 fis[14] = 0;
522 fis[15] = tf->ctl;
523
524 fis[16] = 0;
525 fis[17] = 0;
526 fis[18] = 0;
527 fis[19] = 0;
528}
529
530/**
531 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
532 * @fis: Buffer from which data will be input
533 * @tf: Taskfile to output
534 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500535 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 *
537 * LOCKING:
538 * Inherited from caller.
539 */
540
Jeff Garzik057ace52005-10-22 14:27:05 -0400541void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 tf->command = fis[2]; /* status */
544 tf->feature = fis[3]; /* error */
545
546 tf->lbal = fis[4];
547 tf->lbam = fis[5];
548 tf->lbah = fis[6];
549 tf->device = fis[7];
550
551 tf->hob_lbal = fis[8];
552 tf->hob_lbam = fis[9];
553 tf->hob_lbah = fis[10];
554
555 tf->nsect = fis[12];
556 tf->hob_nsect = fis[13];
557}
558
Albert Lee8cbd6df2005-10-12 15:06:27 +0800559static const u8 ata_rw_cmds[] = {
560 /* pio multi */
561 ATA_CMD_READ_MULTI,
562 ATA_CMD_WRITE_MULTI,
563 ATA_CMD_READ_MULTI_EXT,
564 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100565 0,
566 0,
567 0,
568 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800569 /* pio */
570 ATA_CMD_PIO_READ,
571 ATA_CMD_PIO_WRITE,
572 ATA_CMD_PIO_READ_EXT,
573 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100574 0,
575 0,
576 0,
577 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800578 /* dma */
579 ATA_CMD_READ,
580 ATA_CMD_WRITE,
581 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100582 ATA_CMD_WRITE_EXT,
583 0,
584 0,
585 0,
586 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800587};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800590 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
591 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800593 * Examine the device configuration and tf->flags to calculate
594 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 *
596 * LOCKING:
597 * caller.
598 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100599int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800601 struct ata_taskfile *tf = &qc->tf;
602 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100603 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100605 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800606
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100607 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800608 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
609 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Albert Lee8cbd6df2005-10-12 15:06:27 +0800611 if (dev->flags & ATA_DFLAG_PIO) {
612 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100613 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000614 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
615 /* Unable to use DMA due to host limitation */
616 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800617 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800618 } else {
619 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100620 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100623 cmd = ata_rw_cmds[index + fua + lba48 + write];
624 if (cmd) {
625 tf->command = cmd;
626 return 0;
627 }
628 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100631static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 "UDMA/16",
633 "UDMA/25",
634 "UDMA/33",
635 "UDMA/44",
636 "UDMA/66",
637 "UDMA/100",
638 "UDMA/133",
639 "UDMA7",
640 "MWDMA0",
641 "MWDMA1",
642 "MWDMA2",
643 "PIO0",
644 "PIO1",
645 "PIO2",
646 "PIO3",
647 "PIO4",
648};
649
650/**
651 * ata_udma_string - convert UDMA bit offset to string
652 * @mask: mask of bits supported; only highest bit counts.
653 *
654 * Determine string which represents the highest speed
655 * (highest bit in @udma_mask).
656 *
657 * LOCKING:
658 * None.
659 *
660 * RETURNS:
661 * Constant C string representing highest speed listed in
662 * @udma_mask, or the constant C string "<n/a>".
663 */
664
665static const char *ata_mode_string(unsigned int mask)
666{
667 int i;
668
669 for (i = 7; i >= 0; i--)
670 if (mask & (1 << i))
671 goto out;
672 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
673 if (mask & (1 << i))
674 goto out;
675 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
676 if (mask & (1 << i))
677 goto out;
678
679 return "<n/a>";
680
681out:
682 return xfer_mode_str[i];
683}
684
685/**
686 * ata_pio_devchk - PATA device presence detection
687 * @ap: ATA channel to examine
688 * @device: Device to examine (starting at zero)
689 *
690 * This technique was originally described in
691 * Hale Landis's ATADRVR (www.ata-atapi.com), and
692 * later found its way into the ATA/ATAPI spec.
693 *
694 * Write a pattern to the ATA shadow registers,
695 * and if a device is present, it will respond by
696 * correctly storing and echoing back the
697 * ATA shadow register contents.
698 *
699 * LOCKING:
700 * caller.
701 */
702
703static unsigned int ata_pio_devchk(struct ata_port *ap,
704 unsigned int device)
705{
706 struct ata_ioports *ioaddr = &ap->ioaddr;
707 u8 nsect, lbal;
708
709 ap->ops->dev_select(ap, device);
710
711 outb(0x55, ioaddr->nsect_addr);
712 outb(0xaa, ioaddr->lbal_addr);
713
714 outb(0xaa, ioaddr->nsect_addr);
715 outb(0x55, ioaddr->lbal_addr);
716
717 outb(0x55, ioaddr->nsect_addr);
718 outb(0xaa, ioaddr->lbal_addr);
719
720 nsect = inb(ioaddr->nsect_addr);
721 lbal = inb(ioaddr->lbal_addr);
722
723 if ((nsect == 0x55) && (lbal == 0xaa))
724 return 1; /* we found a device */
725
726 return 0; /* nothing found */
727}
728
729/**
730 * ata_mmio_devchk - PATA device presence detection
731 * @ap: ATA channel to examine
732 * @device: Device to examine (starting at zero)
733 *
734 * This technique was originally described in
735 * Hale Landis's ATADRVR (www.ata-atapi.com), and
736 * later found its way into the ATA/ATAPI spec.
737 *
738 * Write a pattern to the ATA shadow registers,
739 * and if a device is present, it will respond by
740 * correctly storing and echoing back the
741 * ATA shadow register contents.
742 *
743 * LOCKING:
744 * caller.
745 */
746
747static unsigned int ata_mmio_devchk(struct ata_port *ap,
748 unsigned int device)
749{
750 struct ata_ioports *ioaddr = &ap->ioaddr;
751 u8 nsect, lbal;
752
753 ap->ops->dev_select(ap, device);
754
755 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
756 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
757
758 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
759 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
760
761 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
762 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
763
764 nsect = readb((void __iomem *) ioaddr->nsect_addr);
765 lbal = readb((void __iomem *) ioaddr->lbal_addr);
766
767 if ((nsect == 0x55) && (lbal == 0xaa))
768 return 1; /* we found a device */
769
770 return 0; /* nothing found */
771}
772
773/**
774 * ata_devchk - PATA device presence detection
775 * @ap: ATA channel to examine
776 * @device: Device to examine (starting at zero)
777 *
778 * Dispatch ATA device presence detection, depending
779 * on whether we are using PIO or MMIO to talk to the
780 * ATA shadow registers.
781 *
782 * LOCKING:
783 * caller.
784 */
785
786static unsigned int ata_devchk(struct ata_port *ap,
787 unsigned int device)
788{
789 if (ap->flags & ATA_FLAG_MMIO)
790 return ata_mmio_devchk(ap, device);
791 return ata_pio_devchk(ap, device);
792}
793
794/**
795 * ata_dev_classify - determine device type based on ATA-spec signature
796 * @tf: ATA taskfile register set for device to be identified
797 *
798 * Determine from taskfile register contents whether a device is
799 * ATA or ATAPI, as per "Signature and persistence" section
800 * of ATA/PI spec (volume 1, sect 5.14).
801 *
802 * LOCKING:
803 * None.
804 *
805 * RETURNS:
806 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
807 * the event of failure.
808 */
809
Jeff Garzik057ace52005-10-22 14:27:05 -0400810unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 /* Apple's open source Darwin code hints that some devices only
813 * put a proper signature into the LBA mid/high registers,
814 * So, we only check those. It's sufficient for uniqueness.
815 */
816
817 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
818 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
819 DPRINTK("found ATA device by sig\n");
820 return ATA_DEV_ATA;
821 }
822
823 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
824 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
825 DPRINTK("found ATAPI device by sig\n");
826 return ATA_DEV_ATAPI;
827 }
828
829 DPRINTK("unknown device\n");
830 return ATA_DEV_UNKNOWN;
831}
832
833/**
834 * ata_dev_try_classify - Parse returned ATA device signature
835 * @ap: ATA channel to examine
836 * @device: Device to examine (starting at zero)
837 *
838 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
839 * an ATA/ATAPI-defined set of values is placed in the ATA
840 * shadow registers, indicating the results of device detection
841 * and diagnostics.
842 *
843 * Select the ATA device, and read the values from the ATA shadow
844 * registers. Then parse according to the Error register value,
845 * and the spec-defined values examined by ata_dev_classify().
846 *
847 * LOCKING:
848 * caller.
849 */
850
851static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
852{
853 struct ata_device *dev = &ap->device[device];
854 struct ata_taskfile tf;
855 unsigned int class;
856 u8 err;
857
858 ap->ops->dev_select(ap, device);
859
860 memset(&tf, 0, sizeof(tf));
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400863 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 dev->class = ATA_DEV_NONE;
866
867 /* see if device passed diags */
868 if (err == 1)
869 /* do nothing */ ;
870 else if ((device == 0) && (err == 0x81))
871 /* do nothing */ ;
872 else
873 return err;
874
875 /* determine if device if ATA or ATAPI */
876 class = ata_dev_classify(&tf);
877 if (class == ATA_DEV_UNKNOWN)
878 return err;
879 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
880 return err;
881
882 dev->class = class;
883
884 return err;
885}
886
887/**
888 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
889 * @id: IDENTIFY DEVICE results we will examine
890 * @s: string into which data is output
891 * @ofs: offset into identify device page
892 * @len: length of string to return. must be an even number.
893 *
894 * The strings in the IDENTIFY DEVICE page are broken up into
895 * 16-bit chunks. Run through the string, and output each
896 * 8-bit chunk linearly, regardless of platform.
897 *
898 * LOCKING:
899 * caller.
900 */
901
Jeff Garzik057ace52005-10-22 14:27:05 -0400902void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 unsigned int ofs, unsigned int len)
904{
905 unsigned int c;
906
907 while (len > 0) {
908 c = id[ofs] >> 8;
909 *s = c;
910 s++;
911
912 c = id[ofs] & 0xff;
913 *s = c;
914 s++;
915
916 ofs++;
917 len -= 2;
918 }
919}
920
Edward Falk0baab862005-06-02 18:17:13 -0400921
922/**
923 * ata_noop_dev_select - Select device 0/1 on ATA bus
924 * @ap: ATA channel to manipulate
925 * @device: ATA device (numbered from zero) to select
926 *
927 * This function performs no actual function.
928 *
929 * May be used as the dev_select() entry in ata_port_operations.
930 *
931 * LOCKING:
932 * caller.
933 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
935{
936}
937
Edward Falk0baab862005-06-02 18:17:13 -0400938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939/**
940 * ata_std_dev_select - Select device 0/1 on ATA bus
941 * @ap: ATA channel to manipulate
942 * @device: ATA device (numbered from zero) to select
943 *
944 * Use the method defined in the ATA specification to
945 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400946 * ATA channel. Works with both PIO and MMIO.
947 *
948 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 *
950 * LOCKING:
951 * caller.
952 */
953
954void ata_std_dev_select (struct ata_port *ap, unsigned int device)
955{
956 u8 tmp;
957
958 if (device == 0)
959 tmp = ATA_DEVICE_OBS;
960 else
961 tmp = ATA_DEVICE_OBS | ATA_DEV1;
962
963 if (ap->flags & ATA_FLAG_MMIO) {
964 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
965 } else {
966 outb(tmp, ap->ioaddr.device_addr);
967 }
968 ata_pause(ap); /* needed; also flushes, for mmio */
969}
970
971/**
972 * ata_dev_select - Select device 0/1 on ATA bus
973 * @ap: ATA channel to manipulate
974 * @device: ATA device (numbered from zero) to select
975 * @wait: non-zero to wait for Status register BSY bit to clear
976 * @can_sleep: non-zero if context allows sleeping
977 *
978 * Use the method defined in the ATA specification to
979 * make either device 0, or device 1, active on the
980 * ATA channel.
981 *
982 * This is a high-level version of ata_std_dev_select(),
983 * which additionally provides the services of inserting
984 * the proper pauses and status polling, where needed.
985 *
986 * LOCKING:
987 * caller.
988 */
989
990void ata_dev_select(struct ata_port *ap, unsigned int device,
991 unsigned int wait, unsigned int can_sleep)
992{
993 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
994 ap->id, device, wait);
995
996 if (wait)
997 ata_wait_idle(ap);
998
999 ap->ops->dev_select(ap, device);
1000
1001 if (wait) {
1002 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1003 msleep(150);
1004 ata_wait_idle(ap);
1005 }
1006}
1007
1008/**
1009 * ata_dump_id - IDENTIFY DEVICE info debugging output
1010 * @dev: Device whose IDENTIFY DEVICE page we will dump
1011 *
1012 * Dump selected 16-bit words from a detected device's
1013 * IDENTIFY PAGE page.
1014 *
1015 * LOCKING:
1016 * caller.
1017 */
1018
Jeff Garzik057ace52005-10-22 14:27:05 -04001019static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 DPRINTK("49==0x%04x "
1022 "53==0x%04x "
1023 "63==0x%04x "
1024 "64==0x%04x "
1025 "75==0x%04x \n",
1026 dev->id[49],
1027 dev->id[53],
1028 dev->id[63],
1029 dev->id[64],
1030 dev->id[75]);
1031 DPRINTK("80==0x%04x "
1032 "81==0x%04x "
1033 "82==0x%04x "
1034 "83==0x%04x "
1035 "84==0x%04x \n",
1036 dev->id[80],
1037 dev->id[81],
1038 dev->id[82],
1039 dev->id[83],
1040 dev->id[84]);
1041 DPRINTK("88==0x%04x "
1042 "93==0x%04x\n",
1043 dev->id[88],
1044 dev->id[93]);
1045}
1046
Alan Cox11e29e22005-10-21 18:46:32 -04001047/*
1048 * Compute the PIO modes available for this device. This is not as
1049 * trivial as it seems if we must consider early devices correctly.
1050 *
1051 * FIXME: pre IDE drive timing (do we care ?).
1052 */
1053
Jeff Garzik057ace52005-10-22 14:27:05 -04001054static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001055{
1056 u16 modes;
1057
Alan Coxffa29452006-01-09 17:14:40 +00001058 /* Usual case. Word 53 indicates word 64 is valid */
1059 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -04001060 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1061 modes <<= 3;
1062 modes |= 0x7;
1063 return modes;
1064 }
1065
Alan Coxffa29452006-01-09 17:14:40 +00001066 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
1067 number for the maximum. Turn it into a mask and return it */
1068 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -04001069 return modes;
Alan Coxffa29452006-01-09 17:14:40 +00001070 /* But wait.. there's more. Design your standards by committee and
1071 you too can get a free iordy field to process. However its the
1072 speeds not the modes that are supported... Note drivers using the
1073 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -04001074}
1075
Tejun Heoa2a7a662005-12-13 14:48:31 +09001076struct ata_exec_internal_arg {
1077 unsigned int err_mask;
1078 struct ata_taskfile *tf;
1079 struct completion *waiting;
1080};
1081
1082int ata_qc_complete_internal(struct ata_queued_cmd *qc)
1083{
1084 struct ata_exec_internal_arg *arg = qc->private_data;
1085 struct completion *waiting = arg->waiting;
1086
1087 if (!(qc->err_mask & ~AC_ERR_DEV))
1088 qc->ap->ops->tf_read(qc->ap, arg->tf);
1089 arg->err_mask = qc->err_mask;
1090 arg->waiting = NULL;
1091 complete(waiting);
1092
1093 return 0;
1094}
1095
1096/**
1097 * ata_exec_internal - execute libata internal command
1098 * @ap: Port to which the command is sent
1099 * @dev: Device to which the command is sent
1100 * @tf: Taskfile registers for the command and the result
1101 * @dma_dir: Data tranfer direction of the command
1102 * @buf: Data buffer of the command
1103 * @buflen: Length of data buffer
1104 *
1105 * Executes libata internal command with timeout. @tf contains
1106 * command on entry and result on return. Timeout and error
1107 * conditions are reported via return value. No recovery action
1108 * is taken after a command times out. It's caller's duty to
1109 * clean up after timeout.
1110 *
1111 * LOCKING:
1112 * None. Should be called with kernel context, might sleep.
1113 */
1114
1115static unsigned
1116ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1117 struct ata_taskfile *tf,
1118 int dma_dir, void *buf, unsigned int buflen)
1119{
1120 u8 command = tf->command;
1121 struct ata_queued_cmd *qc;
1122 DECLARE_COMPLETION(wait);
1123 unsigned long flags;
1124 struct ata_exec_internal_arg arg;
1125
1126 spin_lock_irqsave(&ap->host_set->lock, flags);
1127
1128 qc = ata_qc_new_init(ap, dev);
1129 BUG_ON(qc == NULL);
1130
1131 qc->tf = *tf;
1132 qc->dma_dir = dma_dir;
1133 if (dma_dir != DMA_NONE) {
1134 ata_sg_init_one(qc, buf, buflen);
1135 qc->nsect = buflen / ATA_SECT_SIZE;
1136 }
1137
1138 arg.waiting = &wait;
1139 arg.tf = tf;
1140 qc->private_data = &arg;
1141 qc->complete_fn = ata_qc_complete_internal;
1142
1143 if (ata_qc_issue(qc))
1144 goto issue_fail;
1145
1146 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1147
1148 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1149 spin_lock_irqsave(&ap->host_set->lock, flags);
1150
1151 /* We're racing with irq here. If we lose, the
1152 * following test prevents us from completing the qc
1153 * again. If completion irq occurs after here but
1154 * before the caller cleans up, it will result in a
1155 * spurious interrupt. We can live with that.
1156 */
1157 if (arg.waiting) {
1158 qc->err_mask = AC_ERR_OTHER;
1159 ata_qc_complete(qc);
1160 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1161 ap->id, command);
1162 }
1163
1164 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1165 }
1166
1167 return arg.err_mask;
1168
1169 issue_fail:
1170 ata_qc_free(qc);
1171 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1172 return AC_ERR_OTHER;
1173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001176 * ata_pio_need_iordy - check if iordy needed
1177 * @adev: ATA device
1178 *
1179 * Check if the current speed of the device requires IORDY. Used
1180 * by various controllers for chip configuration.
1181 */
1182
1183unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1184{
1185 int pio;
1186 int speed = adev->pio_mode - XFER_PIO_0;
1187
1188 if (speed < 2)
1189 return 0;
1190 if (speed > 2)
1191 return 1;
1192
1193 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1194
1195 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1196 pio = adev->id[ATA_ID_EIDE_PIO];
1197 /* Is the speed faster than the drive allows non IORDY ? */
1198 if (pio) {
1199 /* This is cycle times not frequency - watch the logic! */
1200 if (pio > 240) /* PIO2 is 240nS per cycle */
1201 return 1;
1202 return 0;
1203 }
1204 }
1205 return 0;
1206}
1207
1208/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1210 * @ap: port on which device we wish to probe resides
1211 * @device: device bus address, starting at zero
1212 *
1213 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1214 * command, and read back the 512-byte device information page.
1215 * The device information page is fed to us via the standard
1216 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1217 * using standard PIO-IN paths)
1218 *
1219 * After reading the device information page, we use several
1220 * bits of information from it to initialize data structures
1221 * that will be used during the lifetime of the ata_device.
1222 * Other data from the info page is used to disqualify certain
1223 * older ATA devices we do not wish to support.
1224 *
1225 * LOCKING:
1226 * Inherited from caller. Some functions called by this function
1227 * obtain the host_set lock.
1228 */
1229
1230static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1231{
1232 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001233 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 u16 tmp;
1235 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001237 struct ata_taskfile tf;
1238 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int rc;
1240
1241 if (!ata_dev_present(dev)) {
1242 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1243 ap->id, device);
1244 return;
1245 }
1246
1247 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1248 using_edd = 0;
1249 else
1250 using_edd = 1;
1251
1252 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1253
1254 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1255 dev->class == ATA_DEV_NONE);
1256
1257 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001260 ata_tf_init(ap, &tf, device);
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001263 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 DPRINTK("do ATA identify\n");
1265 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001266 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 DPRINTK("do ATAPI identify\n");
1268 }
1269
Tejun Heoa0123702005-12-13 14:49:31 +09001270 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Tejun Heoa0123702005-12-13 14:49:31 +09001272 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1273 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Tejun Heoa0123702005-12-13 14:49:31 +09001275 if (err_mask) {
1276 if (err_mask & ~AC_ERR_DEV)
1277 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * arg! EDD works for all test cases, but seems to return
1281 * the ATA signature for some ATAPI devices. Until the
1282 * reason for this is found and fixed, we fix up the mess
1283 * here. If IDENTIFY DEVICE returns command aborted
1284 * (as ATAPI devices do), then we issue an
1285 * IDENTIFY PACKET DEVICE.
1286 *
1287 * ATA software reset (SRST, the default) does not appear
1288 * to have this problem.
1289 */
Albert Lee7c398332005-11-09 13:03:30 +08001290 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001291 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (err & ATA_ABORTED) {
1293 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto retry;
1295 }
1296 }
1297 goto err_out;
1298 }
1299
1300 swap_buf_le16(dev->id, ATA_ID_WORDS);
1301
1302 /* print device capabilities */
1303 printk(KERN_DEBUG "ata%u: dev %u cfg "
1304 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1305 ap->id, device, dev->id[49],
1306 dev->id[82], dev->id[83], dev->id[84],
1307 dev->id[85], dev->id[86], dev->id[87],
1308 dev->id[88]);
1309
1310 /*
1311 * common ATA, ATAPI feature tests
1312 */
1313
Albert Lee8bf62ec2005-05-12 15:29:42 -04001314 /* we require DMA support (bits 8 of word 49) */
1315 if (!ata_id_has_dma(dev->id)) {
1316 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 goto err_out_nosup;
1318 }
1319
1320 /* quick-n-dirty find max transfer mode; for printk only */
1321 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1322 if (!xfer_modes)
1323 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001324 if (!xfer_modes)
1325 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 ata_dump_id(dev);
1328
1329 /* ATA-specific feature tests */
1330 if (dev->class == ATA_DEV_ATA) {
1331 if (!ata_id_is_ata(dev->id)) /* sanity check */
1332 goto err_out_nosup;
1333
Albert Lee8bf62ec2005-05-12 15:29:42 -04001334 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001336 for (major_version = 14; major_version >= 1; major_version--)
1337 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 break;
1339
Albert Lee8bf62ec2005-05-12 15:29:42 -04001340 /*
1341 * The exact sequence expected by certain pre-ATA4 drives is:
1342 * SRST RESET
1343 * IDENTIFY
1344 * INITIALIZE DEVICE PARAMETERS
1345 * anything else..
1346 * Some drives were very specific about that exact sequence.
1347 */
Albert Lee59a10b12005-10-12 15:09:42 +08001348 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001349 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Albert Lee59a10b12005-10-12 15:09:42 +08001351 /* current CHS translation info (id[53-58]) might be
1352 * changed. reread the identify device info.
1353 */
1354 ata_dev_reread_id(ap, dev);
1355 }
1356
Albert Lee8bf62ec2005-05-12 15:29:42 -04001357 if (ata_id_has_lba(dev->id)) {
1358 dev->flags |= ATA_DFLAG_LBA;
1359
1360 if (ata_id_has_lba48(dev->id)) {
1361 dev->flags |= ATA_DFLAG_LBA48;
1362 dev->n_sectors = ata_id_u64(dev->id, 100);
1363 } else {
1364 dev->n_sectors = ata_id_u32(dev->id, 60);
1365 }
1366
1367 /* print device info to dmesg */
1368 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1369 ap->id, device,
1370 major_version,
1371 ata_mode_string(xfer_modes),
1372 (unsigned long long)dev->n_sectors,
1373 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1374 } else {
1375 /* CHS */
1376
1377 /* Default translation */
1378 dev->cylinders = dev->id[1];
1379 dev->heads = dev->id[3];
1380 dev->sectors = dev->id[6];
1381 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1382
1383 if (ata_id_current_chs_valid(dev->id)) {
1384 /* Current CHS translation is valid. */
1385 dev->cylinders = dev->id[54];
1386 dev->heads = dev->id[55];
1387 dev->sectors = dev->id[56];
1388
1389 dev->n_sectors = ata_id_u32(dev->id, 57);
1390 }
1391
1392 /* print device info to dmesg */
1393 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1394 ap->id, device,
1395 major_version,
1396 ata_mode_string(xfer_modes),
1397 (unsigned long long)dev->n_sectors,
1398 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
1402 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
1405 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001406 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (ata_id_is_ata(dev->id)) /* sanity check */
1408 goto err_out_nosup;
1409
1410 rc = atapi_cdb_len(dev->id);
1411 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1412 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1413 goto err_out_nosup;
1414 }
1415 ap->cdb_len = (unsigned int) rc;
1416 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1417
1418 /* print device info to dmesg */
1419 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1420 ap->id, device,
1421 ata_mode_string(xfer_modes));
1422 }
1423
1424 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1425 return;
1426
1427err_out_nosup:
1428 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1429 ap->id, device);
1430err_out:
1431 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1432 DPRINTK("EXIT, err\n");
1433}
1434
Brad Campbell6f2f3812005-05-12 15:07:47 -04001435
Jeff Garzik057ace52005-10-22 14:27:05 -04001436static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001437{
1438 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1439}
1440
1441/**
1442 * ata_dev_config - Run device specific handlers and check for
1443 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001444 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001445 * @i: Device
1446 *
1447 * LOCKING:
1448 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001449
Brad Campbell6f2f3812005-05-12 15:07:47 -04001450void ata_dev_config(struct ata_port *ap, unsigned int i)
1451{
1452 /* limit bridge transfers to udma5, 200 sectors */
1453 if (ata_dev_knobble(ap)) {
1454 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1455 ap->id, ap->device->devno);
1456 ap->udma_mask &= ATA_UDMA5;
1457 ap->host->max_sectors = ATA_MAX_SECTORS;
1458 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001459 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001460 }
1461
1462 if (ap->ops->dev_config)
1463 ap->ops->dev_config(ap, &ap->device[i]);
1464}
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/**
1467 * ata_bus_probe - Reset and probe ATA bus
1468 * @ap: Bus to probe
1469 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001470 * Master ATA bus probing function. Initiates a hardware-dependent
1471 * bus reset, then attempts to identify any devices found on
1472 * the bus.
1473 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001475 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 *
1477 * RETURNS:
1478 * Zero on success, non-zero on error.
1479 */
1480
1481static int ata_bus_probe(struct ata_port *ap)
1482{
1483 unsigned int i, found = 0;
1484
1485 ap->ops->phy_reset(ap);
1486 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1487 goto err_out;
1488
1489 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1490 ata_dev_identify(ap, i);
1491 if (ata_dev_present(&ap->device[i])) {
1492 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001493 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495 }
1496
1497 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1498 goto err_out_disable;
1499
1500 ata_set_mode(ap);
1501 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1502 goto err_out_disable;
1503
1504 return 0;
1505
1506err_out_disable:
1507 ap->ops->port_disable(ap);
1508err_out:
1509 return -1;
1510}
1511
1512/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001513 * ata_port_probe - Mark port as enabled
1514 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001516 * Modify @ap data structure such that the system
1517 * thinks that the entire port is enabled.
1518 *
1519 * LOCKING: host_set lock, or some other form of
1520 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 */
1522
1523void ata_port_probe(struct ata_port *ap)
1524{
1525 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1526}
1527
1528/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001529 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1530 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001532 * This function issues commands to standard SATA Sxxx
1533 * PHY registers, to wake up the phy (and device), and
1534 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 *
1536 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001537 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 *
1539 */
1540void __sata_phy_reset(struct ata_port *ap)
1541{
1542 u32 sstatus;
1543 unsigned long timeout = jiffies + (HZ * 5);
1544
1545 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001546 /* issue phy wake/reset */
1547 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001548 /* Couldn't find anything in SATA I/II specs, but
1549 * AHCI-1.1 10.4.2 says at least 1 ms. */
1550 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
Brett Russcdcca892005-03-28 15:10:27 -05001552 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 /* wait for phy to become ready, if necessary */
1555 do {
1556 msleep(200);
1557 sstatus = scr_read(ap, SCR_STATUS);
1558 if ((sstatus & 0xf) != 1)
1559 break;
1560 } while (time_before(jiffies, timeout));
1561
1562 /* TODO: phy layer with polling, timeouts, etc. */
Jeff Garzik656563e2005-11-20 03:36:45 -05001563 sstatus = scr_read(ap, SCR_STATUS);
1564 if (sata_dev_present(ap)) {
1565 const char *speed;
1566 u32 tmp;
1567
1568 tmp = (sstatus >> 4) & 0xf;
1569 if (tmp & (1 << 0))
1570 speed = "1.5";
1571 else if (tmp & (1 << 1))
1572 speed = "3.0";
1573 else
1574 speed = "<unknown>";
1575 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1576 ap->id, speed, sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 ata_port_probe(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001578 } else {
1579 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 ap->id, sstatus);
1581 ata_port_disable(ap);
1582 }
1583
1584 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1585 return;
1586
1587 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1588 ata_port_disable(ap);
1589 return;
1590 }
1591
1592 ap->cbl = ATA_CBL_SATA;
1593}
1594
1595/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001596 * sata_phy_reset - Reset SATA bus.
1597 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001599 * This function resets the SATA bus, and then probes
1600 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 *
1602 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001603 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 *
1605 */
1606void sata_phy_reset(struct ata_port *ap)
1607{
1608 __sata_phy_reset(ap);
1609 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1610 return;
1611 ata_bus_reset(ap);
1612}
1613
1614/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001615 * ata_port_disable - Disable port.
1616 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001618 * Modify @ap data structure such that the system
1619 * thinks that the entire port is disabled, and should
1620 * never attempt to probe or communicate with devices
1621 * on this port.
1622 *
1623 * LOCKING: host_set lock, or some other form of
1624 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 */
1626
1627void ata_port_disable(struct ata_port *ap)
1628{
1629 ap->device[0].class = ATA_DEV_NONE;
1630 ap->device[1].class = ATA_DEV_NONE;
1631 ap->flags |= ATA_FLAG_PORT_DISABLED;
1632}
1633
Alan Cox452503f2005-10-21 19:01:32 -04001634/*
1635 * This mode timing computation functionality is ported over from
1636 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1637 */
1638/*
1639 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1640 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1641 * for PIO 5, which is a nonstandard extension and UDMA6, which
1642 * is currently supported only by Maxtor drives.
1643 */
1644
1645static const struct ata_timing ata_timing[] = {
1646
1647 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1648 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1649 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1650 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1651
1652 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1653 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1654 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1655
1656/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1657
1658 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1659 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1660 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1661
1662 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1663 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1664 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1665
1666/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1667 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1668 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1669
1670 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1671 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1672 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1673
1674/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1675
1676 { 0xFF }
1677};
1678
1679#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1680#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1681
1682static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1683{
1684 q->setup = EZ(t->setup * 1000, T);
1685 q->act8b = EZ(t->act8b * 1000, T);
1686 q->rec8b = EZ(t->rec8b * 1000, T);
1687 q->cyc8b = EZ(t->cyc8b * 1000, T);
1688 q->active = EZ(t->active * 1000, T);
1689 q->recover = EZ(t->recover * 1000, T);
1690 q->cycle = EZ(t->cycle * 1000, T);
1691 q->udma = EZ(t->udma * 1000, UT);
1692}
1693
1694void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1695 struct ata_timing *m, unsigned int what)
1696{
1697 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1698 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1699 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1700 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1701 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1702 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1703 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1704 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1705}
1706
1707static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1708{
1709 const struct ata_timing *t;
1710
1711 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001712 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001713 return NULL;
1714 return t;
1715}
1716
1717int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1718 struct ata_timing *t, int T, int UT)
1719{
1720 const struct ata_timing *s;
1721 struct ata_timing p;
1722
1723 /*
1724 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001725 */
Alan Cox452503f2005-10-21 19:01:32 -04001726
1727 if (!(s = ata_timing_find_mode(speed)))
1728 return -EINVAL;
1729
Albert Lee75b1f2f2005-11-16 17:06:18 +08001730 memcpy(t, s, sizeof(*s));
1731
Alan Cox452503f2005-10-21 19:01:32 -04001732 /*
1733 * If the drive is an EIDE drive, it can tell us it needs extended
1734 * PIO/MW_DMA cycle timing.
1735 */
1736
1737 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1738 memset(&p, 0, sizeof(p));
1739 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1740 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1741 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1742 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1743 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1744 }
1745 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1746 }
1747
1748 /*
1749 * Convert the timing to bus clock counts.
1750 */
1751
Albert Lee75b1f2f2005-11-16 17:06:18 +08001752 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001753
1754 /*
1755 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1756 * and some other commands. We have to ensure that the DMA cycle timing is
1757 * slower/equal than the fastest PIO timing.
1758 */
1759
1760 if (speed > XFER_PIO_4) {
1761 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1762 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1763 }
1764
1765 /*
1766 * Lenghten active & recovery time so that cycle time is correct.
1767 */
1768
1769 if (t->act8b + t->rec8b < t->cyc8b) {
1770 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1771 t->rec8b = t->cyc8b - t->act8b;
1772 }
1773
1774 if (t->active + t->recover < t->cycle) {
1775 t->active += (t->cycle - (t->active + t->recover)) / 2;
1776 t->recover = t->cycle - t->active;
1777 }
1778
1779 return 0;
1780}
1781
Jeff Garzik057ace52005-10-22 14:27:05 -04001782static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 unsigned int shift;
1784 u8 base;
1785} xfer_mode_classes[] = {
1786 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1787 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1788 { ATA_SHIFT_PIO, XFER_PIO_0 },
1789};
1790
Arjan van de Ven858119e2006-01-14 13:20:43 -08001791static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 int i;
1794
1795 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1796 if (xfer_mode_classes[i].shift == shift)
1797 return xfer_mode_classes[i].base;
1798
1799 return 0xff;
1800}
1801
1802static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1803{
1804 int ofs, idx;
1805 u8 base;
1806
1807 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1808 return;
1809
1810 if (dev->xfer_shift == ATA_SHIFT_PIO)
1811 dev->flags |= ATA_DFLAG_PIO;
1812
1813 ata_dev_set_xfermode(ap, dev);
1814
1815 base = base_from_shift(dev->xfer_shift);
1816 ofs = dev->xfer_mode - base;
1817 idx = ofs + dev->xfer_shift;
1818 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1819
1820 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1821 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1822
1823 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1824 ap->id, dev->devno, xfer_mode_str[idx]);
1825}
1826
1827static int ata_host_set_pio(struct ata_port *ap)
1828{
1829 unsigned int mask;
1830 int x, i;
1831 u8 base, xfer_mode;
1832
1833 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1834 x = fgb(mask);
1835 if (x < 0) {
1836 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1837 return -1;
1838 }
1839
1840 base = base_from_shift(ATA_SHIFT_PIO);
1841 xfer_mode = base + x;
1842
1843 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1844 (int)base, (int)xfer_mode, mask, x);
1845
1846 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1847 struct ata_device *dev = &ap->device[i];
1848 if (ata_dev_present(dev)) {
1849 dev->pio_mode = xfer_mode;
1850 dev->xfer_mode = xfer_mode;
1851 dev->xfer_shift = ATA_SHIFT_PIO;
1852 if (ap->ops->set_piomode)
1853 ap->ops->set_piomode(ap, dev);
1854 }
1855 }
1856
1857 return 0;
1858}
1859
1860static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1861 unsigned int xfer_shift)
1862{
1863 int i;
1864
1865 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1866 struct ata_device *dev = &ap->device[i];
1867 if (ata_dev_present(dev)) {
1868 dev->dma_mode = xfer_mode;
1869 dev->xfer_mode = xfer_mode;
1870 dev->xfer_shift = xfer_shift;
1871 if (ap->ops->set_dmamode)
1872 ap->ops->set_dmamode(ap, dev);
1873 }
1874 }
1875}
1876
1877/**
1878 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1879 * @ap: port on which timings will be programmed
1880 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001881 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1882 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001884 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 *
1886 */
1887static void ata_set_mode(struct ata_port *ap)
1888{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001889 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 u8 xfer_mode;
1891 int rc;
1892
1893 /* step 1: always set host PIO timings */
1894 rc = ata_host_set_pio(ap);
1895 if (rc)
1896 goto err_out;
1897
1898 /* step 2: choose the best data xfer mode */
1899 xfer_mode = xfer_shift = 0;
1900 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1901 if (rc)
1902 goto err_out;
1903
1904 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1905 if (xfer_shift != ATA_SHIFT_PIO)
1906 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1907
1908 /* step 4: update devices' xfer mode */
1909 ata_dev_set_mode(ap, &ap->device[0]);
1910 ata_dev_set_mode(ap, &ap->device[1]);
1911
1912 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1913 return;
1914
1915 if (ap->ops->post_set_mode)
1916 ap->ops->post_set_mode(ap);
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return;
1919
1920err_out:
1921 ata_port_disable(ap);
1922}
1923
1924/**
1925 * ata_busy_sleep - sleep until BSY clears, or timeout
1926 * @ap: port containing status register to be polled
1927 * @tmout_pat: impatience timeout
1928 * @tmout: overall timeout
1929 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001930 * Sleep until ATA Status register bit BSY clears,
1931 * or a timeout occurs.
1932 *
1933 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 *
1935 */
1936
1937static unsigned int ata_busy_sleep (struct ata_port *ap,
1938 unsigned long tmout_pat,
1939 unsigned long tmout)
1940{
1941 unsigned long timer_start, timeout;
1942 u8 status;
1943
1944 status = ata_busy_wait(ap, ATA_BUSY, 300);
1945 timer_start = jiffies;
1946 timeout = timer_start + tmout_pat;
1947 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1948 msleep(50);
1949 status = ata_busy_wait(ap, ATA_BUSY, 3);
1950 }
1951
1952 if (status & ATA_BUSY)
1953 printk(KERN_WARNING "ata%u is slow to respond, "
1954 "please be patient\n", ap->id);
1955
1956 timeout = timer_start + tmout;
1957 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1958 msleep(50);
1959 status = ata_chk_status(ap);
1960 }
1961
1962 if (status & ATA_BUSY) {
1963 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1964 ap->id, tmout / HZ);
1965 return 1;
1966 }
1967
1968 return 0;
1969}
1970
1971static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1972{
1973 struct ata_ioports *ioaddr = &ap->ioaddr;
1974 unsigned int dev0 = devmask & (1 << 0);
1975 unsigned int dev1 = devmask & (1 << 1);
1976 unsigned long timeout;
1977
1978 /* if device 0 was found in ata_devchk, wait for its
1979 * BSY bit to clear
1980 */
1981 if (dev0)
1982 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1983
1984 /* if device 1 was found in ata_devchk, wait for
1985 * register access, then wait for BSY to clear
1986 */
1987 timeout = jiffies + ATA_TMOUT_BOOT;
1988 while (dev1) {
1989 u8 nsect, lbal;
1990
1991 ap->ops->dev_select(ap, 1);
1992 if (ap->flags & ATA_FLAG_MMIO) {
1993 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1994 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1995 } else {
1996 nsect = inb(ioaddr->nsect_addr);
1997 lbal = inb(ioaddr->lbal_addr);
1998 }
1999 if ((nsect == 1) && (lbal == 1))
2000 break;
2001 if (time_after(jiffies, timeout)) {
2002 dev1 = 0;
2003 break;
2004 }
2005 msleep(50); /* give drive a breather */
2006 }
2007 if (dev1)
2008 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2009
2010 /* is all this really necessary? */
2011 ap->ops->dev_select(ap, 0);
2012 if (dev1)
2013 ap->ops->dev_select(ap, 1);
2014 if (dev0)
2015 ap->ops->dev_select(ap, 0);
2016}
2017
2018/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002019 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2020 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002022 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2023 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 *
2025 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002026 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05002027 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 *
2029 */
2030
2031static unsigned int ata_bus_edd(struct ata_port *ap)
2032{
2033 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05002034 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 /* set up execute-device-diag (bus reset) taskfile */
2037 /* also, take interrupts to a known state (disabled) */
2038 DPRINTK("execute-device-diag\n");
2039 ata_tf_init(ap, &tf, 0);
2040 tf.ctl |= ATA_NIEN;
2041 tf.command = ATA_CMD_EDD;
2042 tf.protocol = ATA_PROT_NODATA;
2043
2044 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002045 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002047 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 /* spec says at least 2ms. but who knows with those
2050 * crazy ATAPI devices...
2051 */
2052 msleep(150);
2053
2054 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2055}
2056
2057static unsigned int ata_bus_softreset(struct ata_port *ap,
2058 unsigned int devmask)
2059{
2060 struct ata_ioports *ioaddr = &ap->ioaddr;
2061
2062 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2063
2064 /* software reset. causes dev0 to be selected */
2065 if (ap->flags & ATA_FLAG_MMIO) {
2066 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2067 udelay(20); /* FIXME: flush */
2068 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2069 udelay(20); /* FIXME: flush */
2070 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2071 } else {
2072 outb(ap->ctl, ioaddr->ctl_addr);
2073 udelay(10);
2074 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2075 udelay(10);
2076 outb(ap->ctl, ioaddr->ctl_addr);
2077 }
2078
2079 /* spec mandates ">= 2ms" before checking status.
2080 * We wait 150ms, because that was the magic delay used for
2081 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2082 * between when the ATA command register is written, and then
2083 * status is checked. Because waiting for "a while" before
2084 * checking status is fine, post SRST, we perform this magic
2085 * delay here as well.
2086 */
2087 msleep(150);
2088
2089 ata_bus_post_reset(ap, devmask);
2090
2091 return 0;
2092}
2093
2094/**
2095 * ata_bus_reset - reset host port and associated ATA channel
2096 * @ap: port to reset
2097 *
2098 * This is typically the first time we actually start issuing
2099 * commands to the ATA channel. We wait for BSY to clear, then
2100 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2101 * result. Determine what devices, if any, are on the channel
2102 * by looking at the device 0/1 error register. Look at the signature
2103 * stored in each device's taskfile registers, to determine if
2104 * the device is ATA or ATAPI.
2105 *
2106 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002107 * PCI/etc. bus probe sem.
2108 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 *
2110 * SIDE EFFECTS:
2111 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2112 */
2113
2114void ata_bus_reset(struct ata_port *ap)
2115{
2116 struct ata_ioports *ioaddr = &ap->ioaddr;
2117 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2118 u8 err;
2119 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2120
2121 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2122
2123 /* determine if device 0/1 are present */
2124 if (ap->flags & ATA_FLAG_SATA_RESET)
2125 dev0 = 1;
2126 else {
2127 dev0 = ata_devchk(ap, 0);
2128 if (slave_possible)
2129 dev1 = ata_devchk(ap, 1);
2130 }
2131
2132 if (dev0)
2133 devmask |= (1 << 0);
2134 if (dev1)
2135 devmask |= (1 << 1);
2136
2137 /* select device 0 again */
2138 ap->ops->dev_select(ap, 0);
2139
2140 /* issue bus reset */
2141 if (ap->flags & ATA_FLAG_SRST)
2142 rc = ata_bus_softreset(ap, devmask);
2143 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2144 /* set up device control */
2145 if (ap->flags & ATA_FLAG_MMIO)
2146 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2147 else
2148 outb(ap->ctl, ioaddr->ctl_addr);
2149 rc = ata_bus_edd(ap);
2150 }
2151
2152 if (rc)
2153 goto err_out;
2154
2155 /*
2156 * determine by signature whether we have ATA or ATAPI devices
2157 */
2158 err = ata_dev_try_classify(ap, 0);
2159 if ((slave_possible) && (err != 0x81))
2160 ata_dev_try_classify(ap, 1);
2161
2162 /* re-enable interrupts */
2163 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2164 ata_irq_on(ap);
2165
2166 /* is double-select really necessary? */
2167 if (ap->device[1].class != ATA_DEV_NONE)
2168 ap->ops->dev_select(ap, 1);
2169 if (ap->device[0].class != ATA_DEV_NONE)
2170 ap->ops->dev_select(ap, 0);
2171
2172 /* if no devices were detected, disable this port */
2173 if ((ap->device[0].class == ATA_DEV_NONE) &&
2174 (ap->device[1].class == ATA_DEV_NONE))
2175 goto err_out;
2176
2177 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2178 /* set up device control for ATA_FLAG_SATA_RESET */
2179 if (ap->flags & ATA_FLAG_MMIO)
2180 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2181 else
2182 outb(ap->ctl, ioaddr->ctl_addr);
2183 }
2184
2185 DPRINTK("EXIT\n");
2186 return;
2187
2188err_out:
2189 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2190 ap->ops->port_disable(ap);
2191
2192 DPRINTK("EXIT\n");
2193}
2194
Jeff Garzik057ace52005-10-22 14:27:05 -04002195static void ata_pr_blacklisted(const struct ata_port *ap,
2196 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2199 ap->id, dev->devno);
2200}
2201
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002202static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 "WDC AC11000H",
2204 "WDC AC22100H",
2205 "WDC AC32500H",
2206 "WDC AC33100H",
2207 "WDC AC31600H",
2208 "WDC AC32100H",
2209 "WDC AC23200L",
2210 "Compaq CRD-8241B",
2211 "CRD-8400B",
2212 "CRD-8480B",
2213 "CRD-8482B",
2214 "CRD-84",
2215 "SanDisk SDP3B",
2216 "SanDisk SDP3B-64",
2217 "SANYO CD-ROM CRD",
2218 "HITACHI CDR-8",
2219 "HITACHI CDR-8335",
2220 "HITACHI CDR-8435",
2221 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002222 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 "CD-532E-A",
2224 "E-IDE CD-ROM CR-840",
2225 "CD-ROM Drive/F5A",
2226 "WPI CDD-820",
2227 "SAMSUNG CD-ROM SC-148C",
2228 "SAMSUNG CD-ROM SC",
2229 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2231 "_NEC DV5800A",
2232};
2233
Jeff Garzik057ace52005-10-22 14:27:05 -04002234static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
2236 unsigned char model_num[40];
2237 char *s;
2238 unsigned int len;
2239 int i;
2240
2241 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2242 sizeof(model_num));
2243 s = &model_num[0];
2244 len = strnlen(s, sizeof(model_num));
2245
2246 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2247 while ((len > 0) && (s[len - 1] == ' ')) {
2248 len--;
2249 s[len] = 0;
2250 }
2251
2252 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2253 if (!strncmp(ata_dma_blacklist[i], s, len))
2254 return 1;
2255
2256 return 0;
2257}
2258
Jeff Garzik057ace52005-10-22 14:27:05 -04002259static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Jeff Garzik057ace52005-10-22 14:27:05 -04002261 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 unsigned int mask;
2263
2264 master = &ap->device[0];
2265 slave = &ap->device[1];
2266
2267 assert (ata_dev_present(master) || ata_dev_present(slave));
2268
2269 if (shift == ATA_SHIFT_UDMA) {
2270 mask = ap->udma_mask;
2271 if (ata_dev_present(master)) {
2272 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002273 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 mask = 0;
2275 ata_pr_blacklisted(ap, master);
2276 }
2277 }
2278 if (ata_dev_present(slave)) {
2279 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002280 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 mask = 0;
2282 ata_pr_blacklisted(ap, slave);
2283 }
2284 }
2285 }
2286 else if (shift == ATA_SHIFT_MWDMA) {
2287 mask = ap->mwdma_mask;
2288 if (ata_dev_present(master)) {
2289 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002290 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 mask = 0;
2292 ata_pr_blacklisted(ap, master);
2293 }
2294 }
2295 if (ata_dev_present(slave)) {
2296 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002297 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 mask = 0;
2299 ata_pr_blacklisted(ap, slave);
2300 }
2301 }
2302 }
2303 else if (shift == ATA_SHIFT_PIO) {
2304 mask = ap->pio_mask;
2305 if (ata_dev_present(master)) {
2306 /* spec doesn't return explicit support for
2307 * PIO0-2, so we fake it
2308 */
2309 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2310 tmp_mode <<= 3;
2311 tmp_mode |= 0x7;
2312 mask &= tmp_mode;
2313 }
2314 if (ata_dev_present(slave)) {
2315 /* spec doesn't return explicit support for
2316 * PIO0-2, so we fake it
2317 */
2318 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2319 tmp_mode <<= 3;
2320 tmp_mode |= 0x7;
2321 mask &= tmp_mode;
2322 }
2323 }
2324 else {
2325 mask = 0xffffffff; /* shut up compiler warning */
2326 BUG();
2327 }
2328
2329 return mask;
2330}
2331
2332/* find greatest bit */
2333static int fgb(u32 bitmap)
2334{
2335 unsigned int i;
2336 int x = -1;
2337
2338 for (i = 0; i < 32; i++)
2339 if (bitmap & (1 << i))
2340 x = i;
2341
2342 return x;
2343}
2344
2345/**
2346 * ata_choose_xfer_mode - attempt to find best transfer mode
2347 * @ap: Port for which an xfer mode will be selected
2348 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2349 * @xfer_shift_out: (output) bit shift that selects this mode
2350 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002351 * Based on host and device capabilities, determine the
2352 * maximum transfer mode that is amenable to all.
2353 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002355 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 *
2357 * RETURNS:
2358 * Zero on success, negative on error.
2359 */
2360
Jeff Garzik057ace52005-10-22 14:27:05 -04002361static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 u8 *xfer_mode_out,
2363 unsigned int *xfer_shift_out)
2364{
2365 unsigned int mask, shift;
2366 int x, i;
2367
2368 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2369 shift = xfer_mode_classes[i].shift;
2370 mask = ata_get_mode_mask(ap, shift);
2371
2372 x = fgb(mask);
2373 if (x >= 0) {
2374 *xfer_mode_out = xfer_mode_classes[i].base + x;
2375 *xfer_shift_out = shift;
2376 return 0;
2377 }
2378 }
2379
2380 return -1;
2381}
2382
2383/**
2384 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2385 * @ap: Port associated with device @dev
2386 * @dev: Device to which command will be sent
2387 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002388 * Issue SET FEATURES - XFER MODE command to device @dev
2389 * on port @ap.
2390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002392 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 */
2394
2395static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2396{
Tejun Heoa0123702005-12-13 14:49:31 +09002397 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 /* set up set-features taskfile */
2400 DPRINTK("set features - xfer mode\n");
2401
Tejun Heoa0123702005-12-13 14:49:31 +09002402 ata_tf_init(ap, &tf, dev->devno);
2403 tf.command = ATA_CMD_SET_FEATURES;
2404 tf.feature = SETFEATURES_XFER;
2405 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2406 tf.protocol = ATA_PROT_NODATA;
2407 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Tejun Heoa0123702005-12-13 14:49:31 +09002409 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2410 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2411 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415 DPRINTK("EXIT\n");
2416}
2417
2418/**
Albert Lee59a10b12005-10-12 15:09:42 +08002419 * ata_dev_reread_id - Reread the device identify device info
2420 * @ap: port where the device is
2421 * @dev: device to reread the identify device info
2422 *
2423 * LOCKING:
2424 */
2425
2426static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2427{
Tejun Heoa0123702005-12-13 14:49:31 +09002428 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002429
Tejun Heoa0123702005-12-13 14:49:31 +09002430 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002431
2432 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002433 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002434 DPRINTK("do ATA identify\n");
2435 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002436 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002437 DPRINTK("do ATAPI identify\n");
2438 }
2439
Tejun Heoa0123702005-12-13 14:49:31 +09002440 tf.flags |= ATA_TFLAG_DEVICE;
2441 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002442
Tejun Heoa0123702005-12-13 14:49:31 +09002443 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2444 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002445 goto err_out;
2446
Albert Lee59a10b12005-10-12 15:09:42 +08002447 swap_buf_le16(dev->id, ATA_ID_WORDS);
2448
2449 ata_dump_id(dev);
2450
2451 DPRINTK("EXIT\n");
2452
2453 return;
2454err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002455 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002456 ata_port_disable(ap);
2457}
2458
2459/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002460 * ata_dev_init_params - Issue INIT DEV PARAMS command
2461 * @ap: Port associated with device @dev
2462 * @dev: Device to which command will be sent
2463 *
2464 * LOCKING:
2465 */
2466
2467static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2468{
Tejun Heoa0123702005-12-13 14:49:31 +09002469 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002470 u16 sectors = dev->id[6];
2471 u16 heads = dev->id[3];
2472
2473 /* Number of sectors per track 1-255. Number of heads 1-16 */
2474 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2475 return;
2476
2477 /* set up init dev params taskfile */
2478 DPRINTK("init dev params \n");
2479
Tejun Heoa0123702005-12-13 14:49:31 +09002480 ata_tf_init(ap, &tf, dev->devno);
2481 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2482 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2483 tf.protocol = ATA_PROT_NODATA;
2484 tf.nsect = sectors;
2485 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002486
Tejun Heoa0123702005-12-13 14:49:31 +09002487 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2488 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2489 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002490 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002491 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002492
2493 DPRINTK("EXIT\n");
2494}
2495
2496/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002497 * ata_sg_clean - Unmap DMA memory associated with command
2498 * @qc: Command containing DMA memory to be released
2499 *
2500 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 *
2502 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002503 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 */
2505
2506static void ata_sg_clean(struct ata_queued_cmd *qc)
2507{
2508 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002509 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002511 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2514 assert(sg != NULL);
2515
2516 if (qc->flags & ATA_QCFLAG_SINGLE)
Tejun Heo2e242fa2006-02-20 23:48:38 +09002517 assert(qc->n_elem <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002519 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002521 /* if we padded the buffer out to 32-bit bound, and data
2522 * xfer direction is from-device, we must copy from the
2523 * pad buffer back into the supplied buffer
2524 */
2525 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2526 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2527
2528 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002529 if (qc->n_elem)
2530 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002531 /* restore last sg */
2532 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2533 if (pad_buf) {
2534 struct scatterlist *psg = &qc->pad_sgent;
2535 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2536 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002537 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002538 }
2539 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002540 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002541 dma_unmap_single(ap->host_set->dev,
2542 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2543 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002544 /* restore sg */
2545 sg->length += qc->pad_len;
2546 if (pad_buf)
2547 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2548 pad_buf, qc->pad_len);
2549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002552 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
2555/**
2556 * ata_fill_sg - Fill PCI IDE PRD table
2557 * @qc: Metadata associated with taskfile to be transferred
2558 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002559 * Fill PCI IDE PRD (scatter-gather) table with segments
2560 * associated with the current disk command.
2561 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002563 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 *
2565 */
2566static void ata_fill_sg(struct ata_queued_cmd *qc)
2567{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002569 struct scatterlist *sg;
2570 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002572 assert(qc->__sg != NULL);
Tejun Heo9ae61c62006-02-20 23:48:37 +09002573 assert(qc->n_elem > 0 || qc->pad_len > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002576 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 u32 addr, offset;
2578 u32 sg_len, len;
2579
2580 /* determine if physical DMA addr spans 64K boundary.
2581 * Note h/w doesn't support 64-bit, so we unconditionally
2582 * truncate dma_addr_t to u32.
2583 */
2584 addr = (u32) sg_dma_address(sg);
2585 sg_len = sg_dma_len(sg);
2586
2587 while (sg_len) {
2588 offset = addr & 0xffff;
2589 len = sg_len;
2590 if ((offset + sg_len) > 0x10000)
2591 len = 0x10000 - offset;
2592
2593 ap->prd[idx].addr = cpu_to_le32(addr);
2594 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2595 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2596
2597 idx++;
2598 sg_len -= len;
2599 addr += len;
2600 }
2601 }
2602
2603 if (idx)
2604 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2605}
2606/**
2607 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2608 * @qc: Metadata associated with taskfile to check
2609 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002610 * Allow low-level driver to filter ATA PACKET commands, returning
2611 * a status indicating whether or not it is OK to use DMA for the
2612 * supplied PACKET command.
2613 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002615 * spin_lock_irqsave(host_set lock)
2616 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 * RETURNS: 0 when ATAPI DMA can be used
2618 * nonzero otherwise
2619 */
2620int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2621{
2622 struct ata_port *ap = qc->ap;
2623 int rc = 0; /* Assume ATAPI DMA is OK by default */
2624
2625 if (ap->ops->check_atapi_dma)
2626 rc = ap->ops->check_atapi_dma(qc);
2627
2628 return rc;
2629}
2630/**
2631 * ata_qc_prep - Prepare taskfile for submission
2632 * @qc: Metadata associated with taskfile to be prepared
2633 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002634 * Prepare ATA taskfile for submission.
2635 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 * LOCKING:
2637 * spin_lock_irqsave(host_set lock)
2638 */
2639void ata_qc_prep(struct ata_queued_cmd *qc)
2640{
2641 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2642 return;
2643
2644 ata_fill_sg(qc);
2645}
2646
Jeff Garzik0cba6322005-05-30 19:49:12 -04002647/**
2648 * ata_sg_init_one - Associate command with memory buffer
2649 * @qc: Command to be associated
2650 * @buf: Memory buffer
2651 * @buflen: Length of memory buffer, in bytes.
2652 *
2653 * Initialize the data-related elements of queued_cmd @qc
2654 * to point to a single memory buffer, @buf of byte length @buflen.
2655 *
2656 * LOCKING:
2657 * spin_lock_irqsave(host_set lock)
2658 */
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2661{
2662 struct scatterlist *sg;
2663
2664 qc->flags |= ATA_QCFLAG_SINGLE;
2665
2666 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002667 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002669 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 qc->buf_virt = buf;
2671
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002672 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002673 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
Jeff Garzik0cba6322005-05-30 19:49:12 -04002676/**
2677 * ata_sg_init - Associate command with scatter-gather table.
2678 * @qc: Command to be associated
2679 * @sg: Scatter-gather table.
2680 * @n_elem: Number of elements in s/g table.
2681 *
2682 * Initialize the data-related elements of queued_cmd @qc
2683 * to point to a scatter-gather table @sg, containing @n_elem
2684 * elements.
2685 *
2686 * LOCKING:
2687 * spin_lock_irqsave(host_set lock)
2688 */
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2691 unsigned int n_elem)
2692{
2693 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002694 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002696 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
2699/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002700 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2701 * @qc: Command with memory buffer to be mapped.
2702 *
2703 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 *
2705 * LOCKING:
2706 * spin_lock_irqsave(host_set lock)
2707 *
2708 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002709 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 */
2711
2712static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2713{
2714 struct ata_port *ap = qc->ap;
2715 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002716 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002718 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002720 /* we must lengthen transfers to end on a 32-bit boundary */
2721 qc->pad_len = sg->length & 3;
2722 if (qc->pad_len) {
2723 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2724 struct scatterlist *psg = &qc->pad_sgent;
2725
2726 assert(qc->dev->class == ATA_DEV_ATAPI);
2727
2728 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2729
2730 if (qc->tf.flags & ATA_TFLAG_WRITE)
2731 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2732 qc->pad_len);
2733
2734 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2735 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2736 /* trim sg */
2737 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002738 if (sg->length == 0)
2739 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002740
2741 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2742 sg->length, qc->pad_len);
2743 }
2744
Tejun Heo2e242fa2006-02-20 23:48:38 +09002745 if (trim_sg) {
2746 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05002747 goto skip_map;
2748 }
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002751 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002752 if (dma_mapping_error(dma_address)) {
2753 /* restore sg */
2754 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002759 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Tejun Heo2e242fa2006-02-20 23:48:38 +09002761skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2763 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2764
2765 return 0;
2766}
2767
2768/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002769 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2770 * @qc: Command with scatter-gather table to be mapped.
2771 *
2772 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 *
2774 * LOCKING:
2775 * spin_lock_irqsave(host_set lock)
2776 *
2777 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002778 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 *
2780 */
2781
2782static int ata_sg_setup(struct ata_queued_cmd *qc)
2783{
2784 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002785 struct scatterlist *sg = qc->__sg;
2786 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002787 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 VPRINTK("ENTER, ata%u\n", ap->id);
2790 assert(qc->flags & ATA_QCFLAG_SG);
2791
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002792 /* we must lengthen transfers to end on a 32-bit boundary */
2793 qc->pad_len = lsg->length & 3;
2794 if (qc->pad_len) {
2795 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2796 struct scatterlist *psg = &qc->pad_sgent;
2797 unsigned int offset;
2798
2799 assert(qc->dev->class == ATA_DEV_ATAPI);
2800
2801 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2802
2803 /*
2804 * psg->page/offset are used to copy to-be-written
2805 * data in this function or read data in ata_sg_clean.
2806 */
2807 offset = lsg->offset + lsg->length - qc->pad_len;
2808 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2809 psg->offset = offset_in_page(offset);
2810
2811 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2812 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2813 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002814 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002815 }
2816
2817 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2818 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2819 /* trim last sg */
2820 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002821 if (lsg->length == 0)
2822 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002823
2824 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2825 qc->n_elem - 1, lsg->length, qc->pad_len);
2826 }
2827
Jeff Garzike1410f22005-11-14 14:06:26 -05002828 pre_n_elem = qc->n_elem;
2829 if (trim_sg && pre_n_elem)
2830 pre_n_elem--;
2831
2832 if (!pre_n_elem) {
2833 n_elem = 0;
2834 goto skip_map;
2835 }
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002838 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002839 if (n_elem < 1) {
2840 /* restore last sg */
2841 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 DPRINTK("%d sg elements mapped\n", n_elem);
2846
Jeff Garzike1410f22005-11-14 14:06:26 -05002847skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 qc->n_elem = n_elem;
2849
2850 return 0;
2851}
2852
2853/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002854 * ata_poll_qc_complete - turn irq back on and finish qc
2855 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002856 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002857 *
2858 * LOCKING:
2859 * None. (grabs host lock)
2860 */
2861
Albert Leea22e2eb2005-12-05 15:38:02 +08002862void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002863{
2864 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002865 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002866
Jeff Garzikb8f61532005-08-25 22:01:20 -04002867 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002868 ap->flags &= ~ATA_FLAG_NOINTR;
2869 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002870 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002871 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002872}
2873
2874/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002876 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 *
2878 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002879 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 *
2881 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002882 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 */
2884
2885static unsigned long ata_pio_poll(struct ata_port *ap)
2886{
Albert Leec14b8332005-12-05 15:36:08 +08002887 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002889 unsigned int poll_state = HSM_ST_UNKNOWN;
2890 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Albert Leec14b8332005-12-05 15:36:08 +08002892 qc = ata_qc_from_tag(ap, ap->active_tag);
2893 assert(qc != NULL);
2894
Albert Lee14be71f2005-09-27 17:36:35 +08002895 switch (ap->hsm_task_state) {
2896 case HSM_ST:
2897 case HSM_ST_POLL:
2898 poll_state = HSM_ST_POLL;
2899 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002901 case HSM_ST_LAST:
2902 case HSM_ST_LAST_POLL:
2903 poll_state = HSM_ST_LAST_POLL;
2904 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 break;
2906 default:
2907 BUG();
2908 break;
2909 }
2910
2911 status = ata_chk_status(ap);
2912 if (status & ATA_BUSY) {
2913 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee1c848982005-12-05 15:40:15 +08002914 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee7c398332005-11-09 13:03:30 +08002915 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return 0;
2917 }
Albert Lee14be71f2005-09-27 17:36:35 +08002918 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return ATA_SHORT_PAUSE;
2920 }
2921
Albert Lee14be71f2005-09-27 17:36:35 +08002922 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return 0;
2924}
2925
2926/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002927 * ata_pio_complete - check if drive is busy or idle
2928 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 *
2930 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002931 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002932 *
2933 * RETURNS:
2934 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 */
2936
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002937static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 struct ata_queued_cmd *qc;
2940 u8 drv_stat;
2941
2942 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002943 * This is purely heuristic. This is a fast path. Sometimes when
2944 * we enter, BSY will be cleared in a chk-status or two. If not,
2945 * the drive is probably seeking or something. Snooze for a couple
2946 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002947 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 */
Albert Leefe79e682005-12-06 11:34:59 +08002949 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2950 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002952 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2953 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002954 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958 }
2959
Albert Leec14b8332005-12-05 15:36:08 +08002960 qc = ata_qc_from_tag(ap, ap->active_tag);
2961 assert(qc != NULL);
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 drv_stat = ata_wait_idle(ap);
2964 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002965 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002966 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
2969
Albert Lee14be71f2005-09-27 17:36:35 +08002970 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Albert Leea22e2eb2005-12-05 15:38:02 +08002972 assert(qc->err_mask == 0);
2973 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002974
2975 /* another command may start at this point */
2976
2977 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
Edward Falk0baab862005-06-02 18:17:13 -04002980
2981/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002982 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002983 * @buf: Buffer to swap
2984 * @buf_words: Number of 16-bit words in buffer.
2985 *
2986 * Swap halves of 16-bit words if needed to convert from
2987 * little-endian byte order to native cpu byte order, or
2988 * vice-versa.
2989 *
2990 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002991 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993void swap_buf_le16(u16 *buf, unsigned int buf_words)
2994{
2995#ifdef __BIG_ENDIAN
2996 unsigned int i;
2997
2998 for (i = 0; i < buf_words; i++)
2999 buf[i] = le16_to_cpu(buf[i]);
3000#endif /* __BIG_ENDIAN */
3001}
3002
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003003/**
3004 * ata_mmio_data_xfer - Transfer data by MMIO
3005 * @ap: port to read/write
3006 * @buf: data buffer
3007 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003008 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003009 *
3010 * Transfer data from/to the device data register by MMIO.
3011 *
3012 * LOCKING:
3013 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003014 */
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3017 unsigned int buflen, int write_data)
3018{
3019 unsigned int i;
3020 unsigned int words = buflen >> 1;
3021 u16 *buf16 = (u16 *) buf;
3022 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3023
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003024 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (write_data) {
3026 for (i = 0; i < words; i++)
3027 writew(le16_to_cpu(buf16[i]), mmio);
3028 } else {
3029 for (i = 0; i < words; i++)
3030 buf16[i] = cpu_to_le16(readw(mmio));
3031 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003032
3033 /* Transfer trailing 1 byte, if any. */
3034 if (unlikely(buflen & 0x01)) {
3035 u16 align_buf[1] = { 0 };
3036 unsigned char *trailing_buf = buf + buflen - 1;
3037
3038 if (write_data) {
3039 memcpy(align_buf, trailing_buf, 1);
3040 writew(le16_to_cpu(align_buf[0]), mmio);
3041 } else {
3042 align_buf[0] = cpu_to_le16(readw(mmio));
3043 memcpy(trailing_buf, align_buf, 1);
3044 }
3045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003048/**
3049 * ata_pio_data_xfer - Transfer data by PIO
3050 * @ap: port to read/write
3051 * @buf: data buffer
3052 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003053 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003054 *
3055 * Transfer data from/to the device data register by PIO.
3056 *
3057 * LOCKING:
3058 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003059 */
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3062 unsigned int buflen, int write_data)
3063{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003064 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003066 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003068 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003070 insw(ap->ioaddr.data_addr, buf, words);
3071
3072 /* Transfer trailing 1 byte, if any. */
3073 if (unlikely(buflen & 0x01)) {
3074 u16 align_buf[1] = { 0 };
3075 unsigned char *trailing_buf = buf + buflen - 1;
3076
3077 if (write_data) {
3078 memcpy(align_buf, trailing_buf, 1);
3079 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3080 } else {
3081 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3082 memcpy(trailing_buf, align_buf, 1);
3083 }
3084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003087/**
3088 * ata_data_xfer - Transfer data from/to the data register.
3089 * @ap: port to read/write
3090 * @buf: data buffer
3091 * @buflen: buffer length
3092 * @do_write: read/write
3093 *
3094 * Transfer data from/to the device data register.
3095 *
3096 * LOCKING:
3097 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003098 */
3099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3101 unsigned int buflen, int do_write)
3102{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003103 /* Make the crap hardware pay the costs not the good stuff */
3104 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3105 unsigned long flags;
3106 local_irq_save(flags);
3107 if (ap->flags & ATA_FLAG_MMIO)
3108 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3109 else
3110 ata_pio_data_xfer(ap, buf, buflen, do_write);
3111 local_irq_restore(flags);
3112 } else {
3113 if (ap->flags & ATA_FLAG_MMIO)
3114 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3115 else
3116 ata_pio_data_xfer(ap, buf, buflen, do_write);
3117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003120/**
3121 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3122 * @qc: Command on going
3123 *
3124 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3125 *
3126 * LOCKING:
3127 * Inherited from caller.
3128 */
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130static void ata_pio_sector(struct ata_queued_cmd *qc)
3131{
3132 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003133 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 struct ata_port *ap = qc->ap;
3135 struct page *page;
3136 unsigned int offset;
3137 unsigned char *buf;
3138
3139 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003140 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142 page = sg[qc->cursg].page;
3143 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3144
3145 /* get the current page and offset */
3146 page = nth_page(page, (offset >> PAGE_SHIFT));
3147 offset %= PAGE_SIZE;
3148
3149 buf = kmap(page) + offset;
3150
3151 qc->cursect++;
3152 qc->cursg_ofs++;
3153
Albert Lee32529e02005-05-26 03:49:42 -04003154 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 qc->cursg++;
3156 qc->cursg_ofs = 0;
3157 }
3158
3159 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3160
3161 /* do the actual data transfer */
3162 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3163 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3164
3165 kunmap(page);
3166}
3167
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003168/**
3169 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3170 * @qc: Command on going
3171 * @bytes: number of bytes
3172 *
3173 * Transfer Transfer data from/to the ATAPI device.
3174 *
3175 * LOCKING:
3176 * Inherited from caller.
3177 *
3178 */
3179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3181{
3182 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003183 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 struct ata_port *ap = qc->ap;
3185 struct page *page;
3186 unsigned char *buf;
3187 unsigned int offset, count;
3188
Albert Lee563a6e12005-08-12 14:17:50 +08003189 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003190 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003193 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003194 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003195 * The end of qc->sg is reached and the device expects
3196 * more data to transfer. In order not to overrun qc->sg
3197 * and fulfill length specified in the byte count register,
3198 * - for read case, discard trailing data from the device
3199 * - for write case, padding zero data to the device
3200 */
3201 u16 pad_buf[1] = { 0 };
3202 unsigned int words = bytes >> 1;
3203 unsigned int i;
3204
3205 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003206 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003207 ap->id, bytes);
3208
3209 for (i = 0; i < words; i++)
3210 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3211
Albert Lee14be71f2005-09-27 17:36:35 +08003212 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003213 return;
3214 }
3215
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003216 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 page = sg->page;
3219 offset = sg->offset + qc->cursg_ofs;
3220
3221 /* get the current page and offset */
3222 page = nth_page(page, (offset >> PAGE_SHIFT));
3223 offset %= PAGE_SIZE;
3224
Albert Lee6952df02005-06-06 15:56:03 +08003225 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003226 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 /* don't cross page boundaries */
3229 count = min(count, (unsigned int)PAGE_SIZE - offset);
3230
3231 buf = kmap(page) + offset;
3232
3233 bytes -= count;
3234 qc->curbytes += count;
3235 qc->cursg_ofs += count;
3236
Albert Lee32529e02005-05-26 03:49:42 -04003237 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 qc->cursg++;
3239 qc->cursg_ofs = 0;
3240 }
3241
3242 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3243
3244 /* do the actual data transfer */
3245 ata_data_xfer(ap, buf, count, do_write);
3246
3247 kunmap(page);
3248
Albert Lee563a6e12005-08-12 14:17:50 +08003249 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003253/**
3254 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3255 * @qc: Command on going
3256 *
3257 * Transfer Transfer data from/to the ATAPI device.
3258 *
3259 * LOCKING:
3260 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003261 */
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3264{
3265 struct ata_port *ap = qc->ap;
3266 struct ata_device *dev = qc->dev;
3267 unsigned int ireason, bc_lo, bc_hi, bytes;
3268 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3269
3270 ap->ops->tf_read(ap, &qc->tf);
3271 ireason = qc->tf.nsect;
3272 bc_lo = qc->tf.lbam;
3273 bc_hi = qc->tf.lbah;
3274 bytes = (bc_hi << 8) | bc_lo;
3275
3276 /* shall be cleared to zero, indicating xfer of data */
3277 if (ireason & (1 << 0))
3278 goto err_out;
3279
3280 /* make sure transfer direction matches expected */
3281 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3282 if (do_write != i_write)
3283 goto err_out;
3284
3285 __atapi_pio_bytes(qc, bytes);
3286
3287 return;
3288
3289err_out:
3290 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3291 ap->id, dev->devno);
Albert Lee1c848982005-12-05 15:40:15 +08003292 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003293 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
3296/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003297 * ata_pio_block - start PIO on a block
3298 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 *
3300 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003301 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 */
3303
3304static void ata_pio_block(struct ata_port *ap)
3305{
3306 struct ata_queued_cmd *qc;
3307 u8 status;
3308
3309 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003310 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 * Sometimes when we enter, BSY will be cleared in
3312 * a chk-status or two. If not, the drive is probably seeking
3313 * or something. Snooze for a couple msecs, then
3314 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003315 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 */
3317 status = ata_busy_wait(ap, ATA_BUSY, 5);
3318 if (status & ATA_BUSY) {
3319 msleep(2);
3320 status = ata_busy_wait(ap, ATA_BUSY, 10);
3321 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003322 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3324 return;
3325 }
3326 }
3327
3328 qc = ata_qc_from_tag(ap, ap->active_tag);
3329 assert(qc != NULL);
3330
Albert Leefe79e682005-12-06 11:34:59 +08003331 /* check error */
3332 if (status & (ATA_ERR | ATA_DF)) {
3333 qc->err_mask |= AC_ERR_DEV;
3334 ap->hsm_task_state = HSM_ST_ERR;
3335 return;
3336 }
3337
3338 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003340 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003342 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return;
3344 }
3345
3346 atapi_pio_bytes(qc);
3347 } else {
3348 /* handle BSY=0, DRQ=0 as error */
3349 if ((status & ATA_DRQ) == 0) {
Albert Lee1c848982005-12-05 15:40:15 +08003350 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003351 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 return;
3353 }
3354
3355 ata_pio_sector(qc);
3356 }
3357}
3358
3359static void ata_pio_error(struct ata_port *ap)
3360{
3361 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 qc = ata_qc_from_tag(ap, ap->active_tag);
3364 assert(qc != NULL);
3365
Albert Lee0565c262006-02-13 18:55:25 +08003366 if (qc->tf.command != ATA_CMD_PACKET)
3367 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3368
Albert Lee1c848982005-12-05 15:40:15 +08003369 /* make sure qc->err_mask is available to
3370 * know what's wrong and recover
3371 */
3372 assert(qc->err_mask);
3373
Albert Lee14be71f2005-09-27 17:36:35 +08003374 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Albert Leea22e2eb2005-12-05 15:38:02 +08003376 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
3378
3379static void ata_pio_task(void *_data)
3380{
3381 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003382 unsigned long timeout;
3383 int qc_completed;
3384
3385fsm_start:
3386 timeout = 0;
3387 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Albert Lee14be71f2005-09-27 17:36:35 +08003389 switch (ap->hsm_task_state) {
3390 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 return;
3392
Albert Lee14be71f2005-09-27 17:36:35 +08003393 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 ata_pio_block(ap);
3395 break;
3396
Albert Lee14be71f2005-09-27 17:36:35 +08003397 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003398 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 break;
3400
Albert Lee14be71f2005-09-27 17:36:35 +08003401 case HSM_ST_POLL:
3402 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 timeout = ata_pio_poll(ap);
3404 break;
3405
Albert Lee14be71f2005-09-27 17:36:35 +08003406 case HSM_ST_TMOUT:
3407 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 ata_pio_error(ap);
3409 return;
3410 }
3411
3412 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003413 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3414 else if (!qc_completed)
3415 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418/**
3419 * ata_qc_timeout - Handle timeout of queued command
3420 * @qc: Command that timed out
3421 *
3422 * Some part of the kernel (currently, only the SCSI layer)
3423 * has noticed that the active command on port @ap has not
3424 * completed after a specified length of time. Handle this
3425 * condition by disabling DMA (if necessary) and completing
3426 * transactions, with error if necessary.
3427 *
3428 * This also handles the case of the "lost interrupt", where
3429 * for some reason (possibly hardware bug, possibly driver bug)
3430 * an interrupt was not delivered to the driver, even though the
3431 * transaction completed successfully.
3432 *
3433 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003434 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 */
3436
3437static void ata_qc_timeout(struct ata_queued_cmd *qc)
3438{
3439 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003440 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003442 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
3444 DPRINTK("ENTER\n");
3445
Jeff Garzikb8f61532005-08-25 22:01:20 -04003446 spin_lock_irqsave(&host_set->lock, flags);
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 /* hack alert! We cannot use the supplied completion
3449 * function from inside the ->eh_strategy_handler() thread.
3450 * libata is the only user of ->eh_strategy_handler() in
3451 * any kernel, so the default scsi_done() assumes it is
3452 * not being called from the SCSI EH.
3453 */
3454 qc->scsidone = scsi_finish_command;
3455
3456 switch (qc->tf.protocol) {
3457
3458 case ATA_PROT_DMA:
3459 case ATA_PROT_ATAPI_DMA:
3460 host_stat = ap->ops->bmdma_status(ap);
3461
3462 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003463 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
3465 /* fall through */
3466
3467 default:
3468 ata_altstatus(ap);
3469 drv_stat = ata_chk_status(ap);
3470
3471 /* ack bmdma irq events */
3472 ap->ops->irq_clear(ap);
3473
3474 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3475 ap->id, qc->tf.command, drv_stat, host_stat);
3476
3477 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003478 qc->err_mask |= ac_err_mask(drv_stat);
3479 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 break;
3481 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003482
3483 spin_unlock_irqrestore(&host_set->lock, flags);
3484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 DPRINTK("EXIT\n");
3486}
3487
3488/**
3489 * ata_eng_timeout - Handle timeout of queued command
3490 * @ap: Port on which timed-out command is active
3491 *
3492 * Some part of the kernel (currently, only the SCSI layer)
3493 * has noticed that the active command on port @ap has not
3494 * completed after a specified length of time. Handle this
3495 * condition by disabling DMA (if necessary) and completing
3496 * transactions, with error if necessary.
3497 *
3498 * This also handles the case of the "lost interrupt", where
3499 * for some reason (possibly hardware bug, possibly driver bug)
3500 * an interrupt was not delivered to the driver, even though the
3501 * transaction completed successfully.
3502 *
3503 * LOCKING:
3504 * Inherited from SCSI layer (none, can sleep)
3505 */
3506
3507void ata_eng_timeout(struct ata_port *ap)
3508{
3509 struct ata_queued_cmd *qc;
3510
3511 DPRINTK("ENTER\n");
3512
3513 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003514 if (qc)
3515 ata_qc_timeout(qc);
3516 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3518 ap->id);
3519 goto out;
3520 }
3521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522out:
3523 DPRINTK("EXIT\n");
3524}
3525
3526/**
3527 * ata_qc_new - Request an available ATA command, for queueing
3528 * @ap: Port associated with device @dev
3529 * @dev: Device from whom we request an available command structure
3530 *
3531 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003532 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 */
3534
3535static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3536{
3537 struct ata_queued_cmd *qc = NULL;
3538 unsigned int i;
3539
3540 for (i = 0; i < ATA_MAX_QUEUE; i++)
3541 if (!test_and_set_bit(i, &ap->qactive)) {
3542 qc = ata_qc_from_tag(ap, i);
3543 break;
3544 }
3545
3546 if (qc)
3547 qc->tag = i;
3548
3549 return qc;
3550}
3551
3552/**
3553 * ata_qc_new_init - Request an available ATA command, and initialize it
3554 * @ap: Port associated with device @dev
3555 * @dev: Device from whom we request an available command structure
3556 *
3557 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003558 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 */
3560
3561struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3562 struct ata_device *dev)
3563{
3564 struct ata_queued_cmd *qc;
3565
3566 qc = ata_qc_new(ap);
3567 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 qc->scsicmd = NULL;
3569 qc->ap = ap;
3570 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003572 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 }
3574
3575 return qc;
3576}
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578static void __ata_qc_complete(struct ata_queued_cmd *qc)
3579{
3580 struct ata_port *ap = qc->ap;
Tejun Heob5632302005-12-13 14:51:25 +09003581 unsigned int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 qc->flags = 0;
3584 tag = qc->tag;
3585 if (likely(ata_tag_valid(tag))) {
3586 if (tag == ap->active_tag)
3587 ap->active_tag = ATA_TAG_POISON;
3588 qc->tag = ATA_TAG_POISON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 clear_bit(tag, &ap->qactive);
Tejun Heob5632302005-12-13 14:51:25 +09003590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
3593/**
3594 * ata_qc_free - free unused ata_queued_cmd
3595 * @qc: Command to complete
3596 *
3597 * Designed to free unused ata_queued_cmd object
3598 * in case something prevents using it.
3599 *
3600 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003601 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 */
3603void ata_qc_free(struct ata_queued_cmd *qc)
3604{
3605 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
3607 __ata_qc_complete(qc);
3608}
3609
3610/**
3611 * ata_qc_complete - Complete an active ATA command
3612 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003613 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003614 *
3615 * Indicate to the mid and upper layers that an ATA
3616 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 *
3618 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003619 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 */
3621
Albert Leea22e2eb2005-12-05 15:38:02 +08003622void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 int rc;
3625
3626 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3627 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3628
3629 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3630 ata_sg_clean(qc);
3631
Albert Lee3f3791d2005-08-16 14:25:38 +08003632 /* atapi: mark qc as inactive to prevent the interrupt handler
3633 * from completing the command twice later, before the error handler
3634 * is called. (when rc != 0 and atapi request sense is needed)
3635 */
3636 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3637
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 /* call completion callback */
Albert Leea22e2eb2005-12-05 15:38:02 +08003639 rc = qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 /* if callback indicates not to complete command (non-zero),
3642 * return immediately
3643 */
3644 if (rc != 0)
3645 return;
3646
3647 __ata_qc_complete(qc);
3648
3649 VPRINTK("EXIT\n");
3650}
3651
3652static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3653{
3654 struct ata_port *ap = qc->ap;
3655
3656 switch (qc->tf.protocol) {
3657 case ATA_PROT_DMA:
3658 case ATA_PROT_ATAPI_DMA:
3659 return 1;
3660
3661 case ATA_PROT_ATAPI:
3662 case ATA_PROT_PIO:
3663 case ATA_PROT_PIO_MULT:
3664 if (ap->flags & ATA_FLAG_PIO_DMA)
3665 return 1;
3666
3667 /* fall through */
3668
3669 default:
3670 return 0;
3671 }
3672
3673 /* never reached */
3674}
3675
3676/**
3677 * ata_qc_issue - issue taskfile to device
3678 * @qc: command to issue to device
3679 *
3680 * Prepare an ATA command to submission to device.
3681 * This includes mapping the data into a DMA-able
3682 * area, filling in the S/G table, and finally
3683 * writing the taskfile to hardware, starting the command.
3684 *
3685 * LOCKING:
3686 * spin_lock_irqsave(host_set lock)
3687 *
3688 * RETURNS:
3689 * Zero on success, negative on error.
3690 */
3691
3692int ata_qc_issue(struct ata_queued_cmd *qc)
3693{
3694 struct ata_port *ap = qc->ap;
3695
3696 if (ata_should_dma_map(qc)) {
3697 if (qc->flags & ATA_QCFLAG_SG) {
3698 if (ata_sg_setup(qc))
3699 goto err_out;
3700 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3701 if (ata_sg_setup_one(qc))
3702 goto err_out;
3703 }
3704 } else {
3705 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3706 }
3707
3708 ap->ops->qc_prep(qc);
3709
3710 qc->ap->active_tag = qc->tag;
3711 qc->flags |= ATA_QCFLAG_ACTIVE;
3712
3713 return ap->ops->qc_issue(qc);
3714
3715err_out:
3716 return -1;
3717}
3718
Edward Falk0baab862005-06-02 18:17:13 -04003719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720/**
3721 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3722 * @qc: command to issue to device
3723 *
3724 * Using various libata functions and hooks, this function
3725 * starts an ATA command. ATA commands are grouped into
3726 * classes called "protocols", and issuing each type of protocol
3727 * is slightly different.
3728 *
Edward Falk0baab862005-06-02 18:17:13 -04003729 * May be used as the qc_issue() entry in ata_port_operations.
3730 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 * LOCKING:
3732 * spin_lock_irqsave(host_set lock)
3733 *
3734 * RETURNS:
3735 * Zero on success, negative on error.
3736 */
3737
3738int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3739{
3740 struct ata_port *ap = qc->ap;
3741
3742 ata_dev_select(ap, qc->dev->devno, 1, 0);
3743
3744 switch (qc->tf.protocol) {
3745 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003746 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 break;
3748
3749 case ATA_PROT_DMA:
3750 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3751 ap->ops->bmdma_setup(qc); /* set up bmdma */
3752 ap->ops->bmdma_start(qc); /* initiate bmdma */
3753 break;
3754
3755 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3756 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003757 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003758 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 queue_work(ata_wq, &ap->pio_task);
3760 break;
3761
3762 case ATA_PROT_ATAPI:
3763 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003764 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 queue_work(ata_wq, &ap->packet_task);
3766 break;
3767
3768 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003769 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003770 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 queue_work(ata_wq, &ap->packet_task);
3772 break;
3773
3774 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003775 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3777 ap->ops->bmdma_setup(qc); /* set up bmdma */
3778 queue_work(ata_wq, &ap->packet_task);
3779 break;
3780
3781 default:
3782 WARN_ON(1);
3783 return -1;
3784 }
3785
3786 return 0;
3787}
3788
3789/**
Edward Falk0baab862005-06-02 18:17:13 -04003790 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 * @qc: Info associated with this ATA transaction.
3792 *
3793 * LOCKING:
3794 * spin_lock_irqsave(host_set lock)
3795 */
3796
3797static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3798{
3799 struct ata_port *ap = qc->ap;
3800 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3801 u8 dmactl;
3802 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3803
3804 /* load PRD table addr. */
3805 mb(); /* make sure PRD table writes are visible to controller */
3806 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3807
3808 /* specify data direction, triple-check start bit is clear */
3809 dmactl = readb(mmio + ATA_DMA_CMD);
3810 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3811 if (!rw)
3812 dmactl |= ATA_DMA_WR;
3813 writeb(dmactl, mmio + ATA_DMA_CMD);
3814
3815 /* issue r/w command */
3816 ap->ops->exec_command(ap, &qc->tf);
3817}
3818
3819/**
Alan Coxb73fc892005-08-26 16:03:19 +01003820 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 * @qc: Info associated with this ATA transaction.
3822 *
3823 * LOCKING:
3824 * spin_lock_irqsave(host_set lock)
3825 */
3826
3827static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3828{
3829 struct ata_port *ap = qc->ap;
3830 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3831 u8 dmactl;
3832
3833 /* start host DMA transaction */
3834 dmactl = readb(mmio + ATA_DMA_CMD);
3835 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3836
3837 /* Strictly, one may wish to issue a readb() here, to
3838 * flush the mmio write. However, control also passes
3839 * to the hardware at this point, and it will interrupt
3840 * us when we are to resume control. So, in effect,
3841 * we don't care when the mmio write flushes.
3842 * Further, a read of the DMA status register _immediately_
3843 * following the write may not be what certain flaky hardware
3844 * is expected, so I think it is best to not add a readb()
3845 * without first all the MMIO ATA cards/mobos.
3846 * Or maybe I'm just being paranoid.
3847 */
3848}
3849
3850/**
3851 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3852 * @qc: Info associated with this ATA transaction.
3853 *
3854 * LOCKING:
3855 * spin_lock_irqsave(host_set lock)
3856 */
3857
3858static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3859{
3860 struct ata_port *ap = qc->ap;
3861 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3862 u8 dmactl;
3863
3864 /* load PRD table addr. */
3865 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3866
3867 /* specify data direction, triple-check start bit is clear */
3868 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3869 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3870 if (!rw)
3871 dmactl |= ATA_DMA_WR;
3872 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3873
3874 /* issue r/w command */
3875 ap->ops->exec_command(ap, &qc->tf);
3876}
3877
3878/**
3879 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3880 * @qc: Info associated with this ATA transaction.
3881 *
3882 * LOCKING:
3883 * spin_lock_irqsave(host_set lock)
3884 */
3885
3886static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3887{
3888 struct ata_port *ap = qc->ap;
3889 u8 dmactl;
3890
3891 /* start host DMA transaction */
3892 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3893 outb(dmactl | ATA_DMA_START,
3894 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3895}
3896
Edward Falk0baab862005-06-02 18:17:13 -04003897
3898/**
3899 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3900 * @qc: Info associated with this ATA transaction.
3901 *
3902 * Writes the ATA_DMA_START flag to the DMA command register.
3903 *
3904 * May be used as the bmdma_start() entry in ata_port_operations.
3905 *
3906 * LOCKING:
3907 * spin_lock_irqsave(host_set lock)
3908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909void ata_bmdma_start(struct ata_queued_cmd *qc)
3910{
3911 if (qc->ap->flags & ATA_FLAG_MMIO)
3912 ata_bmdma_start_mmio(qc);
3913 else
3914 ata_bmdma_start_pio(qc);
3915}
3916
Edward Falk0baab862005-06-02 18:17:13 -04003917
3918/**
3919 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3920 * @qc: Info associated with this ATA transaction.
3921 *
3922 * Writes address of PRD table to device's PRD Table Address
3923 * register, sets the DMA control register, and calls
3924 * ops->exec_command() to start the transfer.
3925 *
3926 * May be used as the bmdma_setup() entry in ata_port_operations.
3927 *
3928 * LOCKING:
3929 * spin_lock_irqsave(host_set lock)
3930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931void ata_bmdma_setup(struct ata_queued_cmd *qc)
3932{
3933 if (qc->ap->flags & ATA_FLAG_MMIO)
3934 ata_bmdma_setup_mmio(qc);
3935 else
3936 ata_bmdma_setup_pio(qc);
3937}
3938
Edward Falk0baab862005-06-02 18:17:13 -04003939
3940/**
3941 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003942 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003943 *
3944 * Clear interrupt and error flags in DMA status register.
3945 *
3946 * May be used as the irq_clear() entry in ata_port_operations.
3947 *
3948 * LOCKING:
3949 * spin_lock_irqsave(host_set lock)
3950 */
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952void ata_bmdma_irq_clear(struct ata_port *ap)
3953{
3954 if (ap->flags & ATA_FLAG_MMIO) {
3955 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3956 writeb(readb(mmio), mmio);
3957 } else {
3958 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3959 outb(inb(addr), addr);
3960 }
3961
3962}
3963
Edward Falk0baab862005-06-02 18:17:13 -04003964
3965/**
3966 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003967 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003968 *
3969 * Read and return BMDMA status register.
3970 *
3971 * May be used as the bmdma_status() entry in ata_port_operations.
3972 *
3973 * LOCKING:
3974 * spin_lock_irqsave(host_set lock)
3975 */
3976
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977u8 ata_bmdma_status(struct ata_port *ap)
3978{
3979 u8 host_stat;
3980 if (ap->flags & ATA_FLAG_MMIO) {
3981 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3982 host_stat = readb(mmio + ATA_DMA_STATUS);
3983 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003984 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 return host_stat;
3986}
3987
Edward Falk0baab862005-06-02 18:17:13 -04003988
3989/**
3990 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003991 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003992 *
3993 * Clears the ATA_DMA_START flag in the dma control register
3994 *
3995 * May be used as the bmdma_stop() entry in ata_port_operations.
3996 *
3997 * LOCKING:
3998 * spin_lock_irqsave(host_set lock)
3999 */
4000
Alan Coxb73fc892005-08-26 16:03:19 +01004001void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
Alan Coxb73fc892005-08-26 16:03:19 +01004003 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 if (ap->flags & ATA_FLAG_MMIO) {
4005 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4006
4007 /* clear start/stop bit */
4008 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4009 mmio + ATA_DMA_CMD);
4010 } else {
4011 /* clear start/stop bit */
4012 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4013 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4014 }
4015
4016 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4017 ata_altstatus(ap); /* dummy read */
4018}
4019
4020/**
4021 * ata_host_intr - Handle host interrupt for given (port, task)
4022 * @ap: Port on which interrupt arrived (possibly...)
4023 * @qc: Taskfile currently active in engine
4024 *
4025 * Handle host interrupt for given queued command. Currently,
4026 * only DMA interrupts are handled. All other commands are
4027 * handled via polling with interrupts disabled (nIEN bit).
4028 *
4029 * LOCKING:
4030 * spin_lock_irqsave(host_set lock)
4031 *
4032 * RETURNS:
4033 * One if interrupt was handled, zero if not (shared irq).
4034 */
4035
4036inline unsigned int ata_host_intr (struct ata_port *ap,
4037 struct ata_queued_cmd *qc)
4038{
4039 u8 status, host_stat;
4040
4041 switch (qc->tf.protocol) {
4042
4043 case ATA_PROT_DMA:
4044 case ATA_PROT_ATAPI_DMA:
4045 case ATA_PROT_ATAPI:
4046 /* check status of DMA engine */
4047 host_stat = ap->ops->bmdma_status(ap);
4048 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4049
4050 /* if it's not our irq... */
4051 if (!(host_stat & ATA_DMA_INTR))
4052 goto idle_irq;
4053
4054 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004055 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
4057 /* fall through */
4058
4059 case ATA_PROT_ATAPI_NODATA:
4060 case ATA_PROT_NODATA:
4061 /* check altstatus */
4062 status = ata_altstatus(ap);
4063 if (status & ATA_BUSY)
4064 goto idle_irq;
4065
4066 /* check main status, clearing INTRQ */
4067 status = ata_chk_status(ap);
4068 if (unlikely(status & ATA_BUSY))
4069 goto idle_irq;
4070 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4071 ap->id, qc->tf.protocol, status);
4072
4073 /* ack bmdma irq events */
4074 ap->ops->irq_clear(ap);
4075
4076 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004077 qc->err_mask |= ac_err_mask(status);
4078 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 break;
4080
4081 default:
4082 goto idle_irq;
4083 }
4084
4085 return 1; /* irq handled */
4086
4087idle_irq:
4088 ap->stats.idle_irq++;
4089
4090#ifdef ATA_IRQ_TRAP
4091 if ((ap->stats.idle_irq % 1000) == 0) {
4092 handled = 1;
4093 ata_irq_ack(ap, 0); /* debug trap */
4094 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4095 }
4096#endif
4097 return 0; /* irq not handled */
4098}
4099
4100/**
4101 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004102 * @irq: irq line (unused)
4103 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 * @regs: unused
4105 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004106 * Default interrupt handler for PCI IDE devices. Calls
4107 * ata_host_intr() for each port that is not disabled.
4108 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004110 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 *
4112 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004113 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 */
4115
4116irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4117{
4118 struct ata_host_set *host_set = dev_instance;
4119 unsigned int i;
4120 unsigned int handled = 0;
4121 unsigned long flags;
4122
4123 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4124 spin_lock_irqsave(&host_set->lock, flags);
4125
4126 for (i = 0; i < host_set->n_ports; i++) {
4127 struct ata_port *ap;
4128
4129 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004130 if (ap &&
4131 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 struct ata_queued_cmd *qc;
4133
4134 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004135 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4136 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 handled |= ata_host_intr(ap, qc);
4138 }
4139 }
4140
4141 spin_unlock_irqrestore(&host_set->lock, flags);
4142
4143 return IRQ_RETVAL(handled);
4144}
4145
4146/**
4147 * atapi_packet_task - Write CDB bytes to hardware
4148 * @_data: Port to which ATAPI device is attached.
4149 *
4150 * When device has indicated its readiness to accept
4151 * a CDB, this function is called. Send the CDB.
4152 * If DMA is to be performed, exit immediately.
4153 * Otherwise, we are in polling mode, so poll
4154 * status under operation succeeds or fails.
4155 *
4156 * LOCKING:
4157 * Kernel thread context (may sleep)
4158 */
4159
4160static void atapi_packet_task(void *_data)
4161{
4162 struct ata_port *ap = _data;
4163 struct ata_queued_cmd *qc;
4164 u8 status;
4165
4166 qc = ata_qc_from_tag(ap, ap->active_tag);
4167 assert(qc != NULL);
4168 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4169
4170 /* sleep-wait for BSY to clear */
4171 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004172 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4173 qc->err_mask |= AC_ERR_ATA_BUS;
4174 goto err_out;
4175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177 /* make sure DRQ is set */
4178 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004179 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4180 qc->err_mask |= AC_ERR_ATA_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
4184 /* send SCSI cdb */
4185 DPRINTK("send cdb\n");
4186 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Tejun Heoc1389502005-08-22 14:59:24 +09004188 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4189 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4190 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Tejun Heoc1389502005-08-22 14:59:24 +09004192 /* Once we're done issuing command and kicking bmdma,
4193 * irq handler takes over. To not lose irq, we need
4194 * to clear NOINTR flag before sending cdb, but
4195 * interrupt handler shouldn't be invoked before we're
4196 * finished. Hence, the following locking.
4197 */
4198 spin_lock_irqsave(&ap->host_set->lock, flags);
4199 ap->flags &= ~ATA_FLAG_NOINTR;
4200 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4201 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4202 ap->ops->bmdma_start(qc); /* initiate bmdma */
4203 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4204 } else {
4205 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Tejun Heoc1389502005-08-22 14:59:24 +09004207 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004208 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 queue_work(ata_wq, &ap->pio_task);
4210 }
4211
4212 return;
4213
4214err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004215 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
4217
Edward Falk0baab862005-06-02 18:17:13 -04004218
4219/**
4220 * ata_port_start - Set port up for dma.
4221 * @ap: Port to initialize
4222 *
4223 * Called just after data structures for each port are
4224 * initialized. Allocates space for PRD table.
4225 *
4226 * May be used as the port_start() entry in ata_port_operations.
4227 *
4228 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004229 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004230 */
4231
Jens Axboe9b847542006-01-06 09:28:07 +01004232/*
4233 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4234 * without filling any other registers
4235 */
4236static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4237 u8 cmd)
4238{
4239 struct ata_taskfile tf;
4240 int err;
4241
4242 ata_tf_init(ap, &tf, dev->devno);
4243
4244 tf.command = cmd;
4245 tf.flags |= ATA_TFLAG_DEVICE;
4246 tf.protocol = ATA_PROT_NODATA;
4247
4248 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4249 if (err)
4250 printk(KERN_ERR "%s: ata command failed: %d\n",
4251 __FUNCTION__, err);
4252
4253 return err;
4254}
4255
4256static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4257{
4258 u8 cmd;
4259
4260 if (!ata_try_flush_cache(dev))
4261 return 0;
4262
4263 if (ata_id_has_flush_ext(dev->id))
4264 cmd = ATA_CMD_FLUSH_EXT;
4265 else
4266 cmd = ATA_CMD_FLUSH;
4267
4268 return ata_do_simple_cmd(ap, dev, cmd);
4269}
4270
4271static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4272{
4273 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4274}
4275
4276static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4277{
4278 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4279}
4280
4281/**
4282 * ata_device_resume - wakeup a previously suspended devices
4283 *
4284 * Kick the drive back into action, by sending it an idle immediate
4285 * command and making sure its transfer mode matches between drive
4286 * and host.
4287 *
4288 */
4289int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4290{
4291 if (ap->flags & ATA_FLAG_SUSPENDED) {
4292 ap->flags &= ~ATA_FLAG_SUSPENDED;
4293 ata_set_mode(ap);
4294 }
4295 if (!ata_dev_present(dev))
4296 return 0;
4297 if (dev->class == ATA_DEV_ATA)
4298 ata_start_drive(ap, dev);
4299
4300 return 0;
4301}
4302
4303/**
4304 * ata_device_suspend - prepare a device for suspend
4305 *
4306 * Flush the cache on the drive, if appropriate, then issue a
4307 * standbynow command.
4308 *
4309 */
4310int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4311{
4312 if (!ata_dev_present(dev))
4313 return 0;
4314 if (dev->class == ATA_DEV_ATA)
4315 ata_flush_cache(ap, dev);
4316
4317 ata_standby_drive(ap, dev);
4318 ap->flags |= ATA_FLAG_SUSPENDED;
4319 return 0;
4320}
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322int ata_port_start (struct ata_port *ap)
4323{
4324 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004325 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
4327 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4328 if (!ap->prd)
4329 return -ENOMEM;
4330
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004331 rc = ata_pad_alloc(ap, dev);
4332 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004333 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004334 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004335 }
4336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4338
4339 return 0;
4340}
4341
Edward Falk0baab862005-06-02 18:17:13 -04004342
4343/**
4344 * ata_port_stop - Undo ata_port_start()
4345 * @ap: Port to shut down
4346 *
4347 * Frees the PRD table.
4348 *
4349 * May be used as the port_stop() entry in ata_port_operations.
4350 *
4351 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004352 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004353 */
4354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355void ata_port_stop (struct ata_port *ap)
4356{
4357 struct device *dev = ap->host_set->dev;
4358
4359 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004360 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361}
4362
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004363void ata_host_stop (struct ata_host_set *host_set)
4364{
4365 if (host_set->mmio_base)
4366 iounmap(host_set->mmio_base);
4367}
4368
4369
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370/**
4371 * ata_host_remove - Unregister SCSI host structure with upper layers
4372 * @ap: Port to unregister
4373 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4374 *
4375 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004376 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 */
4378
4379static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4380{
4381 struct Scsi_Host *sh = ap->host;
4382
4383 DPRINTK("ENTER\n");
4384
4385 if (do_unregister)
4386 scsi_remove_host(sh);
4387
4388 ap->ops->port_stop(ap);
4389}
4390
4391/**
4392 * ata_host_init - Initialize an ata_port structure
4393 * @ap: Structure to initialize
4394 * @host: associated SCSI mid-layer structure
4395 * @host_set: Collection of hosts to which @ap belongs
4396 * @ent: Probe information provided by low-level driver
4397 * @port_no: Port number associated with this ata_port
4398 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004399 * Initialize a new ata_port structure, and its associated
4400 * scsi_host.
4401 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004403 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 */
4405
4406static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4407 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004408 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
4410 unsigned int i;
4411
4412 host->max_id = 16;
4413 host->max_lun = 1;
4414 host->max_channel = 1;
4415 host->unique_id = ata_unique_id++;
4416 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 ap->flags = ATA_FLAG_PORT_DISABLED;
4419 ap->id = host->unique_id;
4420 ap->host = host;
4421 ap->ctl = ATA_DEVCTL_OBS;
4422 ap->host_set = host_set;
4423 ap->port_no = port_no;
4424 ap->hard_port_no =
4425 ent->legacy_mode ? ent->hard_port_no : port_no;
4426 ap->pio_mask = ent->pio_mask;
4427 ap->mwdma_mask = ent->mwdma_mask;
4428 ap->udma_mask = ent->udma_mask;
4429 ap->flags |= ent->host_flags;
4430 ap->ops = ent->port_ops;
4431 ap->cbl = ATA_CBL_NONE;
4432 ap->active_tag = ATA_TAG_POISON;
4433 ap->last_ctl = 0xFF;
4434
4435 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4436 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4437
4438 for (i = 0; i < ATA_MAX_DEVICES; i++)
4439 ap->device[i].devno = i;
4440
4441#ifdef ATA_IRQ_TRAP
4442 ap->stats.unhandled_irq = 1;
4443 ap->stats.idle_irq = 1;
4444#endif
4445
4446 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4447}
4448
4449/**
4450 * ata_host_add - Attach low-level ATA driver to system
4451 * @ent: Information provided by low-level driver
4452 * @host_set: Collections of ports to which we add
4453 * @port_no: Port number associated with this host
4454 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004455 * Attach low-level ATA driver to system.
4456 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004458 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 *
4460 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004461 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 */
4463
Jeff Garzik057ace52005-10-22 14:27:05 -04004464static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 struct ata_host_set *host_set,
4466 unsigned int port_no)
4467{
4468 struct Scsi_Host *host;
4469 struct ata_port *ap;
4470 int rc;
4471
4472 DPRINTK("ENTER\n");
4473 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4474 if (!host)
4475 return NULL;
4476
4477 ap = (struct ata_port *) &host->hostdata[0];
4478
4479 ata_host_init(ap, host, host_set, ent, port_no);
4480
4481 rc = ap->ops->port_start(ap);
4482 if (rc)
4483 goto err_out;
4484
4485 return ap;
4486
4487err_out:
4488 scsi_host_put(host);
4489 return NULL;
4490}
4491
4492/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004493 * ata_device_add - Register hardware device with ATA and SCSI layers
4494 * @ent: Probe information describing hardware device to be registered
4495 *
4496 * This function processes the information provided in the probe
4497 * information struct @ent, allocates the necessary ATA and SCSI
4498 * host information structures, initializes them, and registers
4499 * everything with requisite kernel subsystems.
4500 *
4501 * This function requests irqs, probes the ATA bus, and probes
4502 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 *
4504 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004505 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 *
4507 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004508 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 */
4510
Jeff Garzik057ace52005-10-22 14:27:05 -04004511int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
4513 unsigned int count = 0, i;
4514 struct device *dev = ent->dev;
4515 struct ata_host_set *host_set;
4516
4517 DPRINTK("ENTER\n");
4518 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004519 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4521 if (!host_set)
4522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 spin_lock_init(&host_set->lock);
4524
4525 host_set->dev = dev;
4526 host_set->n_ports = ent->n_ports;
4527 host_set->irq = ent->irq;
4528 host_set->mmio_base = ent->mmio_base;
4529 host_set->private_data = ent->private_data;
4530 host_set->ops = ent->port_ops;
4531
4532 /* register each port bound to this device */
4533 for (i = 0; i < ent->n_ports; i++) {
4534 struct ata_port *ap;
4535 unsigned long xfer_mode_mask;
4536
4537 ap = ata_host_add(ent, host_set, i);
4538 if (!ap)
4539 goto err_out;
4540
4541 host_set->ports[i] = ap;
4542 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4543 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4544 (ap->pio_mask << ATA_SHIFT_PIO);
4545
4546 /* print per-port info to dmesg */
4547 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4548 "bmdma 0x%lX irq %lu\n",
4549 ap->id,
4550 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4551 ata_mode_string(xfer_mode_mask),
4552 ap->ioaddr.cmd_addr,
4553 ap->ioaddr.ctl_addr,
4554 ap->ioaddr.bmdma_addr,
4555 ent->irq);
4556
4557 ata_chk_status(ap);
4558 host_set->ops->irq_clear(ap);
4559 count++;
4560 }
4561
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004562 if (!count)
4563 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
4565 /* obtain irq, that is shared between channels */
4566 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4567 DRV_NAME, host_set))
4568 goto err_out;
4569
4570 /* perform each probe synchronously */
4571 DPRINTK("probe begin\n");
4572 for (i = 0; i < count; i++) {
4573 struct ata_port *ap;
4574 int rc;
4575
4576 ap = host_set->ports[i];
4577
4578 DPRINTK("ata%u: probe begin\n", ap->id);
4579 rc = ata_bus_probe(ap);
4580 DPRINTK("ata%u: probe end\n", ap->id);
4581
4582 if (rc) {
4583 /* FIXME: do something useful here?
4584 * Current libata behavior will
4585 * tear down everything when
4586 * the module is removed
4587 * or the h/w is unplugged.
4588 */
4589 }
4590
4591 rc = scsi_add_host(ap->host, dev);
4592 if (rc) {
4593 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4594 ap->id);
4595 /* FIXME: do something useful here */
4596 /* FIXME: handle unconditional calls to
4597 * scsi_scan_host and ata_host_remove, below,
4598 * at the very least
4599 */
4600 }
4601 }
4602
4603 /* probes are done, now scan each port's disk(s) */
4604 DPRINTK("probe begin\n");
4605 for (i = 0; i < count; i++) {
4606 struct ata_port *ap = host_set->ports[i];
4607
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004608 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 }
4610
4611 dev_set_drvdata(dev, host_set);
4612
4613 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4614 return ent->n_ports; /* success */
4615
4616err_out:
4617 for (i = 0; i < count; i++) {
4618 ata_host_remove(host_set->ports[i], 1);
4619 scsi_host_put(host_set->ports[i]->host);
4620 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004621err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 kfree(host_set);
4623 VPRINTK("EXIT, returning 0\n");
4624 return 0;
4625}
4626
4627/**
Alan Cox17b14452005-09-15 15:44:00 +01004628 * ata_host_set_remove - PCI layer callback for device removal
4629 * @host_set: ATA host set that was removed
4630 *
4631 * Unregister all objects associated with this host set. Free those
4632 * objects.
4633 *
4634 * LOCKING:
4635 * Inherited from calling layer (may sleep).
4636 */
4637
Alan Cox17b14452005-09-15 15:44:00 +01004638void ata_host_set_remove(struct ata_host_set *host_set)
4639{
4640 struct ata_port *ap;
4641 unsigned int i;
4642
4643 for (i = 0; i < host_set->n_ports; i++) {
4644 ap = host_set->ports[i];
4645 scsi_remove_host(ap->host);
4646 }
4647
4648 free_irq(host_set->irq, host_set);
4649
4650 for (i = 0; i < host_set->n_ports; i++) {
4651 ap = host_set->ports[i];
4652
4653 ata_scsi_release(ap->host);
4654
4655 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4656 struct ata_ioports *ioaddr = &ap->ioaddr;
4657
4658 if (ioaddr->cmd_addr == 0x1f0)
4659 release_region(0x1f0, 8);
4660 else if (ioaddr->cmd_addr == 0x170)
4661 release_region(0x170, 8);
4662 }
4663
4664 scsi_host_put(ap->host);
4665 }
4666
4667 if (host_set->ops->host_stop)
4668 host_set->ops->host_stop(host_set);
4669
4670 kfree(host_set);
4671}
4672
4673/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 * ata_scsi_release - SCSI layer callback hook for host unload
4675 * @host: libata host to be unloaded
4676 *
4677 * Performs all duties necessary to shut down a libata port...
4678 * Kill port kthread, disable port, and release resources.
4679 *
4680 * LOCKING:
4681 * Inherited from SCSI layer.
4682 *
4683 * RETURNS:
4684 * One.
4685 */
4686
4687int ata_scsi_release(struct Scsi_Host *host)
4688{
4689 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4690
4691 DPRINTK("ENTER\n");
4692
4693 ap->ops->port_disable(ap);
4694 ata_host_remove(ap, 0);
4695
4696 DPRINTK("EXIT\n");
4697 return 1;
4698}
4699
4700/**
4701 * ata_std_ports - initialize ioaddr with standard port offsets.
4702 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004703 *
4704 * Utility function which initializes data_addr, error_addr,
4705 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4706 * device_addr, status_addr, and command_addr to standard offsets
4707 * relative to cmd_addr.
4708 *
4709 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 */
Edward Falk0baab862005-06-02 18:17:13 -04004711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712void ata_std_ports(struct ata_ioports *ioaddr)
4713{
4714 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4715 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4716 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4717 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4718 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4719 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4720 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4721 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4722 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4723 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4724}
4725
4726static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004727ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728{
4729 struct ata_probe_ent *probe_ent;
4730
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004731 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 if (!probe_ent) {
4733 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4734 kobject_name(&(dev->kobj)));
4735 return NULL;
4736 }
4737
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 INIT_LIST_HEAD(&probe_ent->node);
4739 probe_ent->dev = dev;
4740
4741 probe_ent->sht = port->sht;
4742 probe_ent->host_flags = port->host_flags;
4743 probe_ent->pio_mask = port->pio_mask;
4744 probe_ent->mwdma_mask = port->mwdma_mask;
4745 probe_ent->udma_mask = port->udma_mask;
4746 probe_ent->port_ops = port->port_ops;
4747
4748 return probe_ent;
4749}
4750
Edward Falk0baab862005-06-02 18:17:13 -04004751
4752
Jeff Garzik374b1872005-08-30 05:42:52 -04004753#ifdef CONFIG_PCI
4754
4755void ata_pci_host_stop (struct ata_host_set *host_set)
4756{
4757 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4758
4759 pci_iounmap(pdev, host_set->mmio_base);
4760}
4761
Edward Falk0baab862005-06-02 18:17:13 -04004762/**
4763 * ata_pci_init_native_mode - Initialize native-mode driver
4764 * @pdev: pci device to be initialized
4765 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004766 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004767 *
4768 * Utility function which allocates and initializes an
4769 * ata_probe_ent structure for a standard dual-port
4770 * PIO-based IDE controller. The returned ata_probe_ent
4771 * structure can be passed to ata_device_add(). The returned
4772 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004773 *
4774 * The caller need only pass the address of the primary port, the
4775 * secondary will be deduced automatically. If the device has non
4776 * standard secondary port mappings this function can be called twice,
4777 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004778 */
4779
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004781ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
4783 struct ata_probe_ent *probe_ent =
4784 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004785 int p = 0;
4786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 if (!probe_ent)
4788 return NULL;
4789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 probe_ent->irq = pdev->irq;
4791 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004792 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Alan Cox47a86592005-10-04 08:09:19 -04004794 if (ports & ATA_PORT_PRIMARY) {
4795 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4796 probe_ent->port[p].altstatus_addr =
4797 probe_ent->port[p].ctl_addr =
4798 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4799 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4800 ata_std_ports(&probe_ent->port[p]);
4801 p++;
4802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Alan Cox47a86592005-10-04 08:09:19 -04004804 if (ports & ATA_PORT_SECONDARY) {
4805 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4806 probe_ent->port[p].altstatus_addr =
4807 probe_ent->port[p].ctl_addr =
4808 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4809 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4810 ata_std_ports(&probe_ent->port[p]);
4811 p++;
4812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Alan Cox47a86592005-10-04 08:09:19 -04004814 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 return probe_ent;
4816}
4817
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004818static 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 -07004819{
Alan Cox47a86592005-10-04 08:09:19 -04004820 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004822 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 if (!probe_ent)
4824 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004827 probe_ent->n_ports = 1;
4828 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004829 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Alan Cox47a86592005-10-04 08:09:19 -04004831 switch(port_num)
4832 {
4833 case 0:
4834 probe_ent->irq = 14;
4835 probe_ent->port[0].cmd_addr = 0x1f0;
4836 probe_ent->port[0].altstatus_addr =
4837 probe_ent->port[0].ctl_addr = 0x3f6;
4838 break;
4839 case 1:
4840 probe_ent->irq = 15;
4841 probe_ent->port[0].cmd_addr = 0x170;
4842 probe_ent->port[0].altstatus_addr =
4843 probe_ent->port[0].ctl_addr = 0x376;
4844 break;
4845 }
4846 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 return probe_ent;
4849}
4850
4851/**
4852 * ata_pci_init_one - Initialize/register PCI IDE host controller
4853 * @pdev: Controller to be initialized
4854 * @port_info: Information from low-level host driver
4855 * @n_ports: Number of ports attached to host controller
4856 *
Edward Falk0baab862005-06-02 18:17:13 -04004857 * This is a helper function which can be called from a driver's
4858 * xxx_init_one() probe function if the hardware uses traditional
4859 * IDE taskfile registers.
4860 *
4861 * This function calls pci_enable_device(), reserves its register
4862 * regions, sets the dma mask, enables bus master mode, and calls
4863 * ata_device_add()
4864 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 * LOCKING:
4866 * Inherited from PCI layer (may sleep).
4867 *
4868 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004869 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 */
4871
4872int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4873 unsigned int n_ports)
4874{
Alan Cox47a86592005-10-04 08:09:19 -04004875 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 struct ata_port_info *port[2];
4877 u8 tmp8, mask;
4878 unsigned int legacy_mode = 0;
4879 int disable_dev_on_err = 1;
4880 int rc;
4881
4882 DPRINTK("ENTER\n");
4883
4884 port[0] = port_info[0];
4885 if (n_ports > 1)
4886 port[1] = port_info[1];
4887 else
4888 port[1] = port[0];
4889
4890 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4891 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004892 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4894 mask = (1 << 2) | (1 << 0);
4895 if ((tmp8 & mask) != mask)
4896 legacy_mode = (1 << 3);
4897 }
4898
4899 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004900 if ((!legacy_mode) && (n_ports > 2)) {
4901 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4902 n_ports = 2;
4903 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 }
4905
Alan Cox47a86592005-10-04 08:09:19 -04004906 /* FIXME: Really for ATA it isn't safe because the device may be
4907 multi-purpose and we want to leave it alone if it was already
4908 enabled. Secondly for shared use as Arjan says we want refcounting
4909
4910 Checking dev->is_enabled is insufficient as this is not set at
4911 boot for the primary video which is BIOS enabled
4912 */
4913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 rc = pci_enable_device(pdev);
4915 if (rc)
4916 return rc;
4917
4918 rc = pci_request_regions(pdev, DRV_NAME);
4919 if (rc) {
4920 disable_dev_on_err = 0;
4921 goto err_out;
4922 }
4923
Alan Cox47a86592005-10-04 08:09:19 -04004924 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 if (legacy_mode) {
4926 if (!request_region(0x1f0, 8, "libata")) {
4927 struct resource *conflict, res;
4928 res.start = 0x1f0;
4929 res.end = 0x1f0 + 8 - 1;
4930 conflict = ____request_resource(&ioport_resource, &res);
4931 if (!strcmp(conflict->name, "libata"))
4932 legacy_mode |= (1 << 0);
4933 else {
4934 disable_dev_on_err = 0;
4935 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4936 }
4937 } else
4938 legacy_mode |= (1 << 0);
4939
4940 if (!request_region(0x170, 8, "libata")) {
4941 struct resource *conflict, res;
4942 res.start = 0x170;
4943 res.end = 0x170 + 8 - 1;
4944 conflict = ____request_resource(&ioport_resource, &res);
4945 if (!strcmp(conflict->name, "libata"))
4946 legacy_mode |= (1 << 1);
4947 else {
4948 disable_dev_on_err = 0;
4949 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4950 }
4951 } else
4952 legacy_mode |= (1 << 1);
4953 }
4954
4955 /* we have legacy mode, but all ports are unavailable */
4956 if (legacy_mode == (1 << 3)) {
4957 rc = -EBUSY;
4958 goto err_out_regions;
4959 }
4960
4961 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4962 if (rc)
4963 goto err_out_regions;
4964 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4965 if (rc)
4966 goto err_out_regions;
4967
4968 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004969 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004970 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004971 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004972 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004973 } else {
4974 if (n_ports == 2)
4975 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4976 else
4977 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4978 }
4979 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 rc = -ENOMEM;
4981 goto err_out_regions;
4982 }
4983
4984 pci_set_master(pdev);
4985
4986 /* FIXME: check ata_device_add return */
4987 if (legacy_mode) {
4988 if (legacy_mode & (1 << 0))
4989 ata_device_add(probe_ent);
4990 if (legacy_mode & (1 << 1))
4991 ata_device_add(probe_ent2);
4992 } else
4993 ata_device_add(probe_ent);
4994
4995 kfree(probe_ent);
4996 kfree(probe_ent2);
4997
4998 return 0;
4999
5000err_out_regions:
5001 if (legacy_mode & (1 << 0))
5002 release_region(0x1f0, 8);
5003 if (legacy_mode & (1 << 1))
5004 release_region(0x170, 8);
5005 pci_release_regions(pdev);
5006err_out:
5007 if (disable_dev_on_err)
5008 pci_disable_device(pdev);
5009 return rc;
5010}
5011
5012/**
5013 * ata_pci_remove_one - PCI layer callback for device removal
5014 * @pdev: PCI device that was removed
5015 *
5016 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005017 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 * Handle this by unregistering all objects associated
5019 * with this PCI device. Free those objects. Then finally
5020 * release PCI resources and disable device.
5021 *
5022 * LOCKING:
5023 * Inherited from PCI layer (may sleep).
5024 */
5025
5026void ata_pci_remove_one (struct pci_dev *pdev)
5027{
5028 struct device *dev = pci_dev_to_dev(pdev);
5029 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Alan Cox17b14452005-09-15 15:44:00 +01005031 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 pci_release_regions(pdev);
5033 pci_disable_device(pdev);
5034 dev_set_drvdata(dev, NULL);
5035}
5036
5037/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005038int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039{
5040 unsigned long tmp = 0;
5041
5042 switch (bits->width) {
5043 case 1: {
5044 u8 tmp8 = 0;
5045 pci_read_config_byte(pdev, bits->reg, &tmp8);
5046 tmp = tmp8;
5047 break;
5048 }
5049 case 2: {
5050 u16 tmp16 = 0;
5051 pci_read_config_word(pdev, bits->reg, &tmp16);
5052 tmp = tmp16;
5053 break;
5054 }
5055 case 4: {
5056 u32 tmp32 = 0;
5057 pci_read_config_dword(pdev, bits->reg, &tmp32);
5058 tmp = tmp32;
5059 break;
5060 }
5061
5062 default:
5063 return -EINVAL;
5064 }
5065
5066 tmp &= bits->mask;
5067
5068 return (tmp == bits->val) ? 1 : 0;
5069}
Jens Axboe9b847542006-01-06 09:28:07 +01005070
5071int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5072{
5073 pci_save_state(pdev);
5074 pci_disable_device(pdev);
5075 pci_set_power_state(pdev, PCI_D3hot);
5076 return 0;
5077}
5078
5079int ata_pci_device_resume(struct pci_dev *pdev)
5080{
5081 pci_set_power_state(pdev, PCI_D0);
5082 pci_restore_state(pdev);
5083 pci_enable_device(pdev);
5084 pci_set_master(pdev);
5085 return 0;
5086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087#endif /* CONFIG_PCI */
5088
5089
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090static int __init ata_init(void)
5091{
5092 ata_wq = create_workqueue("ata");
5093 if (!ata_wq)
5094 return -ENOMEM;
5095
5096 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5097 return 0;
5098}
5099
5100static void __exit ata_exit(void)
5101{
5102 destroy_workqueue(ata_wq);
5103}
5104
5105module_init(ata_init);
5106module_exit(ata_exit);
5107
Jeff Garzik67846b32005-10-05 02:58:32 -04005108static unsigned long ratelimit_time;
5109static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5110
5111int ata_ratelimit(void)
5112{
5113 int rc;
5114 unsigned long flags;
5115
5116 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5117
5118 if (time_after(jiffies, ratelimit_time)) {
5119 rc = 1;
5120 ratelimit_time = jiffies + (HZ/5);
5121 } else
5122 rc = 0;
5123
5124 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5125
5126 return rc;
5127}
5128
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129/*
5130 * libata is essentially a library of internal helper functions for
5131 * low-level ATA host controller drivers. As such, the API/ABI is
5132 * likely to change as new drivers are added and updated.
5133 * Do not depend on ABI/API stability.
5134 */
5135
5136EXPORT_SYMBOL_GPL(ata_std_bios_param);
5137EXPORT_SYMBOL_GPL(ata_std_ports);
5138EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005139EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140EXPORT_SYMBOL_GPL(ata_sg_init);
5141EXPORT_SYMBOL_GPL(ata_sg_init_one);
5142EXPORT_SYMBOL_GPL(ata_qc_complete);
5143EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5144EXPORT_SYMBOL_GPL(ata_eng_timeout);
5145EXPORT_SYMBOL_GPL(ata_tf_load);
5146EXPORT_SYMBOL_GPL(ata_tf_read);
5147EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5148EXPORT_SYMBOL_GPL(ata_std_dev_select);
5149EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5150EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5151EXPORT_SYMBOL_GPL(ata_check_status);
5152EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153EXPORT_SYMBOL_GPL(ata_exec_command);
5154EXPORT_SYMBOL_GPL(ata_port_start);
5155EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005156EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157EXPORT_SYMBOL_GPL(ata_interrupt);
5158EXPORT_SYMBOL_GPL(ata_qc_prep);
5159EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5160EXPORT_SYMBOL_GPL(ata_bmdma_start);
5161EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5162EXPORT_SYMBOL_GPL(ata_bmdma_status);
5163EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5164EXPORT_SYMBOL_GPL(ata_port_probe);
5165EXPORT_SYMBOL_GPL(sata_phy_reset);
5166EXPORT_SYMBOL_GPL(__sata_phy_reset);
5167EXPORT_SYMBOL_GPL(ata_bus_reset);
5168EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005169EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5171EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5172EXPORT_SYMBOL_GPL(ata_scsi_error);
5173EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5174EXPORT_SYMBOL_GPL(ata_scsi_release);
5175EXPORT_SYMBOL_GPL(ata_host_intr);
5176EXPORT_SYMBOL_GPL(ata_dev_classify);
5177EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005178EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5180
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005181EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005182EXPORT_SYMBOL_GPL(ata_timing_compute);
5183EXPORT_SYMBOL_GPL(ata_timing_merge);
5184
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185#ifdef CONFIG_PCI
5186EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005187EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5189EXPORT_SYMBOL_GPL(ata_pci_init_one);
5190EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005191EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5192EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005194
5195EXPORT_SYMBOL_GPL(ata_device_suspend);
5196EXPORT_SYMBOL_GPL(ata_device_resume);
5197EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5198EXPORT_SYMBOL_GPL(ata_scsi_device_resume);