blob: 9c66d405939911d9329b7209b95fdb2b40fae30d [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,
565 /* pio */
566 ATA_CMD_PIO_READ,
567 ATA_CMD_PIO_WRITE,
568 ATA_CMD_PIO_READ_EXT,
569 ATA_CMD_PIO_WRITE_EXT,
570 /* dma */
571 ATA_CMD_READ,
572 ATA_CMD_WRITE,
573 ATA_CMD_READ_EXT,
574 ATA_CMD_WRITE_EXT
575};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800578 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
579 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800581 * Examine the device configuration and tf->flags to calculate
582 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 *
584 * LOCKING:
585 * caller.
586 */
Albert Lee8cbd6df2005-10-12 15:06:27 +0800587void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800589 struct ata_taskfile *tf = &qc->tf;
590 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Albert Lee8cbd6df2005-10-12 15:06:27 +0800592 int index, lba48, write;
593
594 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
595 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Albert Lee8cbd6df2005-10-12 15:06:27 +0800597 if (dev->flags & ATA_DFLAG_PIO) {
598 tf->protocol = ATA_PROT_PIO;
599 index = dev->multi_count ? 0 : 4;
600 } else {
601 tf->protocol = ATA_PROT_DMA;
602 index = 8;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Albert Lee8cbd6df2005-10-12 15:06:27 +0800605 tf->command = ata_rw_cmds[index + lba48 + write];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100608static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 "UDMA/16",
610 "UDMA/25",
611 "UDMA/33",
612 "UDMA/44",
613 "UDMA/66",
614 "UDMA/100",
615 "UDMA/133",
616 "UDMA7",
617 "MWDMA0",
618 "MWDMA1",
619 "MWDMA2",
620 "PIO0",
621 "PIO1",
622 "PIO2",
623 "PIO3",
624 "PIO4",
625};
626
627/**
628 * ata_udma_string - convert UDMA bit offset to string
629 * @mask: mask of bits supported; only highest bit counts.
630 *
631 * Determine string which represents the highest speed
632 * (highest bit in @udma_mask).
633 *
634 * LOCKING:
635 * None.
636 *
637 * RETURNS:
638 * Constant C string representing highest speed listed in
639 * @udma_mask, or the constant C string "<n/a>".
640 */
641
642static const char *ata_mode_string(unsigned int mask)
643{
644 int i;
645
646 for (i = 7; i >= 0; i--)
647 if (mask & (1 << i))
648 goto out;
649 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
650 if (mask & (1 << i))
651 goto out;
652 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
653 if (mask & (1 << i))
654 goto out;
655
656 return "<n/a>";
657
658out:
659 return xfer_mode_str[i];
660}
661
662/**
663 * ata_pio_devchk - PATA device presence detection
664 * @ap: ATA channel to examine
665 * @device: Device to examine (starting at zero)
666 *
667 * This technique was originally described in
668 * Hale Landis's ATADRVR (www.ata-atapi.com), and
669 * later found its way into the ATA/ATAPI spec.
670 *
671 * Write a pattern to the ATA shadow registers,
672 * and if a device is present, it will respond by
673 * correctly storing and echoing back the
674 * ATA shadow register contents.
675 *
676 * LOCKING:
677 * caller.
678 */
679
680static unsigned int ata_pio_devchk(struct ata_port *ap,
681 unsigned int device)
682{
683 struct ata_ioports *ioaddr = &ap->ioaddr;
684 u8 nsect, lbal;
685
686 ap->ops->dev_select(ap, device);
687
688 outb(0x55, ioaddr->nsect_addr);
689 outb(0xaa, ioaddr->lbal_addr);
690
691 outb(0xaa, ioaddr->nsect_addr);
692 outb(0x55, ioaddr->lbal_addr);
693
694 outb(0x55, ioaddr->nsect_addr);
695 outb(0xaa, ioaddr->lbal_addr);
696
697 nsect = inb(ioaddr->nsect_addr);
698 lbal = inb(ioaddr->lbal_addr);
699
700 if ((nsect == 0x55) && (lbal == 0xaa))
701 return 1; /* we found a device */
702
703 return 0; /* nothing found */
704}
705
706/**
707 * ata_mmio_devchk - PATA device presence detection
708 * @ap: ATA channel to examine
709 * @device: Device to examine (starting at zero)
710 *
711 * This technique was originally described in
712 * Hale Landis's ATADRVR (www.ata-atapi.com), and
713 * later found its way into the ATA/ATAPI spec.
714 *
715 * Write a pattern to the ATA shadow registers,
716 * and if a device is present, it will respond by
717 * correctly storing and echoing back the
718 * ATA shadow register contents.
719 *
720 * LOCKING:
721 * caller.
722 */
723
724static unsigned int ata_mmio_devchk(struct ata_port *ap,
725 unsigned int device)
726{
727 struct ata_ioports *ioaddr = &ap->ioaddr;
728 u8 nsect, lbal;
729
730 ap->ops->dev_select(ap, device);
731
732 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
733 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
734
735 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
736 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
737
738 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
739 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
740
741 nsect = readb((void __iomem *) ioaddr->nsect_addr);
742 lbal = readb((void __iomem *) ioaddr->lbal_addr);
743
744 if ((nsect == 0x55) && (lbal == 0xaa))
745 return 1; /* we found a device */
746
747 return 0; /* nothing found */
748}
749
750/**
751 * ata_devchk - PATA device presence detection
752 * @ap: ATA channel to examine
753 * @device: Device to examine (starting at zero)
754 *
755 * Dispatch ATA device presence detection, depending
756 * on whether we are using PIO or MMIO to talk to the
757 * ATA shadow registers.
758 *
759 * LOCKING:
760 * caller.
761 */
762
763static unsigned int ata_devchk(struct ata_port *ap,
764 unsigned int device)
765{
766 if (ap->flags & ATA_FLAG_MMIO)
767 return ata_mmio_devchk(ap, device);
768 return ata_pio_devchk(ap, device);
769}
770
771/**
772 * ata_dev_classify - determine device type based on ATA-spec signature
773 * @tf: ATA taskfile register set for device to be identified
774 *
775 * Determine from taskfile register contents whether a device is
776 * ATA or ATAPI, as per "Signature and persistence" section
777 * of ATA/PI spec (volume 1, sect 5.14).
778 *
779 * LOCKING:
780 * None.
781 *
782 * RETURNS:
783 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
784 * the event of failure.
785 */
786
Jeff Garzik057ace52005-10-22 14:27:05 -0400787unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 /* Apple's open source Darwin code hints that some devices only
790 * put a proper signature into the LBA mid/high registers,
791 * So, we only check those. It's sufficient for uniqueness.
792 */
793
794 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
795 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
796 DPRINTK("found ATA device by sig\n");
797 return ATA_DEV_ATA;
798 }
799
800 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
801 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
802 DPRINTK("found ATAPI device by sig\n");
803 return ATA_DEV_ATAPI;
804 }
805
806 DPRINTK("unknown device\n");
807 return ATA_DEV_UNKNOWN;
808}
809
810/**
811 * ata_dev_try_classify - Parse returned ATA device signature
812 * @ap: ATA channel to examine
813 * @device: Device to examine (starting at zero)
814 *
815 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
816 * an ATA/ATAPI-defined set of values is placed in the ATA
817 * shadow registers, indicating the results of device detection
818 * and diagnostics.
819 *
820 * Select the ATA device, and read the values from the ATA shadow
821 * registers. Then parse according to the Error register value,
822 * and the spec-defined values examined by ata_dev_classify().
823 *
824 * LOCKING:
825 * caller.
826 */
827
828static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
829{
830 struct ata_device *dev = &ap->device[device];
831 struct ata_taskfile tf;
832 unsigned int class;
833 u8 err;
834
835 ap->ops->dev_select(ap, device);
836
837 memset(&tf, 0, sizeof(tf));
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400840 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 dev->class = ATA_DEV_NONE;
843
844 /* see if device passed diags */
845 if (err == 1)
846 /* do nothing */ ;
847 else if ((device == 0) && (err == 0x81))
848 /* do nothing */ ;
849 else
850 return err;
851
852 /* determine if device if ATA or ATAPI */
853 class = ata_dev_classify(&tf);
854 if (class == ATA_DEV_UNKNOWN)
855 return err;
856 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
857 return err;
858
859 dev->class = class;
860
861 return err;
862}
863
864/**
865 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
866 * @id: IDENTIFY DEVICE results we will examine
867 * @s: string into which data is output
868 * @ofs: offset into identify device page
869 * @len: length of string to return. must be an even number.
870 *
871 * The strings in the IDENTIFY DEVICE page are broken up into
872 * 16-bit chunks. Run through the string, and output each
873 * 8-bit chunk linearly, regardless of platform.
874 *
875 * LOCKING:
876 * caller.
877 */
878
Jeff Garzik057ace52005-10-22 14:27:05 -0400879void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 unsigned int ofs, unsigned int len)
881{
882 unsigned int c;
883
884 while (len > 0) {
885 c = id[ofs] >> 8;
886 *s = c;
887 s++;
888
889 c = id[ofs] & 0xff;
890 *s = c;
891 s++;
892
893 ofs++;
894 len -= 2;
895 }
896}
897
Edward Falk0baab862005-06-02 18:17:13 -0400898
899/**
900 * ata_noop_dev_select - Select device 0/1 on ATA bus
901 * @ap: ATA channel to manipulate
902 * @device: ATA device (numbered from zero) to select
903 *
904 * This function performs no actual function.
905 *
906 * May be used as the dev_select() entry in ata_port_operations.
907 *
908 * LOCKING:
909 * caller.
910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
912{
913}
914
Edward Falk0baab862005-06-02 18:17:13 -0400915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/**
917 * ata_std_dev_select - Select device 0/1 on ATA bus
918 * @ap: ATA channel to manipulate
919 * @device: ATA device (numbered from zero) to select
920 *
921 * Use the method defined in the ATA specification to
922 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400923 * ATA channel. Works with both PIO and MMIO.
924 *
925 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
927 * LOCKING:
928 * caller.
929 */
930
931void ata_std_dev_select (struct ata_port *ap, unsigned int device)
932{
933 u8 tmp;
934
935 if (device == 0)
936 tmp = ATA_DEVICE_OBS;
937 else
938 tmp = ATA_DEVICE_OBS | ATA_DEV1;
939
940 if (ap->flags & ATA_FLAG_MMIO) {
941 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
942 } else {
943 outb(tmp, ap->ioaddr.device_addr);
944 }
945 ata_pause(ap); /* needed; also flushes, for mmio */
946}
947
948/**
949 * ata_dev_select - Select device 0/1 on ATA bus
950 * @ap: ATA channel to manipulate
951 * @device: ATA device (numbered from zero) to select
952 * @wait: non-zero to wait for Status register BSY bit to clear
953 * @can_sleep: non-zero if context allows sleeping
954 *
955 * Use the method defined in the ATA specification to
956 * make either device 0, or device 1, active on the
957 * ATA channel.
958 *
959 * This is a high-level version of ata_std_dev_select(),
960 * which additionally provides the services of inserting
961 * the proper pauses and status polling, where needed.
962 *
963 * LOCKING:
964 * caller.
965 */
966
967void ata_dev_select(struct ata_port *ap, unsigned int device,
968 unsigned int wait, unsigned int can_sleep)
969{
970 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
971 ap->id, device, wait);
972
973 if (wait)
974 ata_wait_idle(ap);
975
976 ap->ops->dev_select(ap, device);
977
978 if (wait) {
979 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
980 msleep(150);
981 ata_wait_idle(ap);
982 }
983}
984
985/**
986 * ata_dump_id - IDENTIFY DEVICE info debugging output
987 * @dev: Device whose IDENTIFY DEVICE page we will dump
988 *
989 * Dump selected 16-bit words from a detected device's
990 * IDENTIFY PAGE page.
991 *
992 * LOCKING:
993 * caller.
994 */
995
Jeff Garzik057ace52005-10-22 14:27:05 -0400996static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 DPRINTK("49==0x%04x "
999 "53==0x%04x "
1000 "63==0x%04x "
1001 "64==0x%04x "
1002 "75==0x%04x \n",
1003 dev->id[49],
1004 dev->id[53],
1005 dev->id[63],
1006 dev->id[64],
1007 dev->id[75]);
1008 DPRINTK("80==0x%04x "
1009 "81==0x%04x "
1010 "82==0x%04x "
1011 "83==0x%04x "
1012 "84==0x%04x \n",
1013 dev->id[80],
1014 dev->id[81],
1015 dev->id[82],
1016 dev->id[83],
1017 dev->id[84]);
1018 DPRINTK("88==0x%04x "
1019 "93==0x%04x\n",
1020 dev->id[88],
1021 dev->id[93]);
1022}
1023
Alan Cox11e29e22005-10-21 18:46:32 -04001024/*
1025 * Compute the PIO modes available for this device. This is not as
1026 * trivial as it seems if we must consider early devices correctly.
1027 *
1028 * FIXME: pre IDE drive timing (do we care ?).
1029 */
1030
Jeff Garzik057ace52005-10-22 14:27:05 -04001031static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001032{
1033 u16 modes;
1034
1035 /* Usual case. Word 53 indicates word 88 is valid */
1036 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1037 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1038 modes <<= 3;
1039 modes |= 0x7;
1040 return modes;
1041 }
1042
1043 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1044 for the maximum. Turn it into a mask and return it */
1045 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1046 return modes;
1047}
1048
Tejun Heoa2a7a662005-12-13 14:48:31 +09001049struct ata_exec_internal_arg {
1050 unsigned int err_mask;
1051 struct ata_taskfile *tf;
1052 struct completion *waiting;
1053};
1054
1055int ata_qc_complete_internal(struct ata_queued_cmd *qc)
1056{
1057 struct ata_exec_internal_arg *arg = qc->private_data;
1058 struct completion *waiting = arg->waiting;
1059
1060 if (!(qc->err_mask & ~AC_ERR_DEV))
1061 qc->ap->ops->tf_read(qc->ap, arg->tf);
1062 arg->err_mask = qc->err_mask;
1063 arg->waiting = NULL;
1064 complete(waiting);
1065
1066 return 0;
1067}
1068
1069/**
1070 * ata_exec_internal - execute libata internal command
1071 * @ap: Port to which the command is sent
1072 * @dev: Device to which the command is sent
1073 * @tf: Taskfile registers for the command and the result
1074 * @dma_dir: Data tranfer direction of the command
1075 * @buf: Data buffer of the command
1076 * @buflen: Length of data buffer
1077 *
1078 * Executes libata internal command with timeout. @tf contains
1079 * command on entry and result on return. Timeout and error
1080 * conditions are reported via return value. No recovery action
1081 * is taken after a command times out. It's caller's duty to
1082 * clean up after timeout.
1083 *
1084 * LOCKING:
1085 * None. Should be called with kernel context, might sleep.
1086 */
1087
1088static unsigned
1089ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1090 struct ata_taskfile *tf,
1091 int dma_dir, void *buf, unsigned int buflen)
1092{
1093 u8 command = tf->command;
1094 struct ata_queued_cmd *qc;
1095 DECLARE_COMPLETION(wait);
1096 unsigned long flags;
1097 struct ata_exec_internal_arg arg;
1098
1099 spin_lock_irqsave(&ap->host_set->lock, flags);
1100
1101 qc = ata_qc_new_init(ap, dev);
1102 BUG_ON(qc == NULL);
1103
1104 qc->tf = *tf;
1105 qc->dma_dir = dma_dir;
1106 if (dma_dir != DMA_NONE) {
1107 ata_sg_init_one(qc, buf, buflen);
1108 qc->nsect = buflen / ATA_SECT_SIZE;
1109 }
1110
1111 arg.waiting = &wait;
1112 arg.tf = tf;
1113 qc->private_data = &arg;
1114 qc->complete_fn = ata_qc_complete_internal;
1115
1116 if (ata_qc_issue(qc))
1117 goto issue_fail;
1118
1119 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1120
1121 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1122 spin_lock_irqsave(&ap->host_set->lock, flags);
1123
1124 /* We're racing with irq here. If we lose, the
1125 * following test prevents us from completing the qc
1126 * again. If completion irq occurs after here but
1127 * before the caller cleans up, it will result in a
1128 * spurious interrupt. We can live with that.
1129 */
1130 if (arg.waiting) {
1131 qc->err_mask = AC_ERR_OTHER;
1132 ata_qc_complete(qc);
1133 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1134 ap->id, command);
1135 }
1136
1137 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1138 }
1139
1140 return arg.err_mask;
1141
1142 issue_fail:
1143 ata_qc_free(qc);
1144 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1145 return AC_ERR_OTHER;
1146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/**
1149 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1150 * @ap: port on which device we wish to probe resides
1151 * @device: device bus address, starting at zero
1152 *
1153 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1154 * command, and read back the 512-byte device information page.
1155 * The device information page is fed to us via the standard
1156 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1157 * using standard PIO-IN paths)
1158 *
1159 * After reading the device information page, we use several
1160 * bits of information from it to initialize data structures
1161 * that will be used during the lifetime of the ata_device.
1162 * Other data from the info page is used to disqualify certain
1163 * older ATA devices we do not wish to support.
1164 *
1165 * LOCKING:
1166 * Inherited from caller. Some functions called by this function
1167 * obtain the host_set lock.
1168 */
1169
1170static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1171{
1172 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001173 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 u16 tmp;
1175 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001177 struct ata_taskfile tf;
1178 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int rc;
1180
1181 if (!ata_dev_present(dev)) {
1182 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1183 ap->id, device);
1184 return;
1185 }
1186
1187 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1188 using_edd = 0;
1189 else
1190 using_edd = 1;
1191
1192 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1193
1194 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1195 dev->class == ATA_DEV_NONE);
1196
1197 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001200 ata_tf_init(ap, &tf, device);
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001203 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 DPRINTK("do ATA identify\n");
1205 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001206 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 DPRINTK("do ATAPI identify\n");
1208 }
1209
Tejun Heoa0123702005-12-13 14:49:31 +09001210 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Tejun Heoa0123702005-12-13 14:49:31 +09001212 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1213 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Tejun Heoa0123702005-12-13 14:49:31 +09001215 if (err_mask) {
1216 if (err_mask & ~AC_ERR_DEV)
1217 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /*
1220 * arg! EDD works for all test cases, but seems to return
1221 * the ATA signature for some ATAPI devices. Until the
1222 * reason for this is found and fixed, we fix up the mess
1223 * here. If IDENTIFY DEVICE returns command aborted
1224 * (as ATAPI devices do), then we issue an
1225 * IDENTIFY PACKET DEVICE.
1226 *
1227 * ATA software reset (SRST, the default) does not appear
1228 * to have this problem.
1229 */
Albert Lee7c398332005-11-09 13:03:30 +08001230 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001231 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (err & ATA_ABORTED) {
1233 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 goto retry;
1235 }
1236 }
1237 goto err_out;
1238 }
1239
1240 swap_buf_le16(dev->id, ATA_ID_WORDS);
1241
1242 /* print device capabilities */
1243 printk(KERN_DEBUG "ata%u: dev %u cfg "
1244 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1245 ap->id, device, dev->id[49],
1246 dev->id[82], dev->id[83], dev->id[84],
1247 dev->id[85], dev->id[86], dev->id[87],
1248 dev->id[88]);
1249
1250 /*
1251 * common ATA, ATAPI feature tests
1252 */
1253
Albert Lee8bf62ec2005-05-12 15:29:42 -04001254 /* we require DMA support (bits 8 of word 49) */
1255 if (!ata_id_has_dma(dev->id)) {
1256 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto err_out_nosup;
1258 }
1259
1260 /* quick-n-dirty find max transfer mode; for printk only */
1261 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1262 if (!xfer_modes)
1263 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001264 if (!xfer_modes)
1265 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 ata_dump_id(dev);
1268
1269 /* ATA-specific feature tests */
1270 if (dev->class == ATA_DEV_ATA) {
1271 if (!ata_id_is_ata(dev->id)) /* sanity check */
1272 goto err_out_nosup;
1273
Albert Lee8bf62ec2005-05-12 15:29:42 -04001274 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001276 for (major_version = 14; major_version >= 1; major_version--)
1277 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
1279
Albert Lee8bf62ec2005-05-12 15:29:42 -04001280 /*
1281 * The exact sequence expected by certain pre-ATA4 drives is:
1282 * SRST RESET
1283 * IDENTIFY
1284 * INITIALIZE DEVICE PARAMETERS
1285 * anything else..
1286 * Some drives were very specific about that exact sequence.
1287 */
Albert Lee59a10b12005-10-12 15:09:42 +08001288 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001289 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Albert Lee59a10b12005-10-12 15:09:42 +08001291 /* current CHS translation info (id[53-58]) might be
1292 * changed. reread the identify device info.
1293 */
1294 ata_dev_reread_id(ap, dev);
1295 }
1296
Albert Lee8bf62ec2005-05-12 15:29:42 -04001297 if (ata_id_has_lba(dev->id)) {
1298 dev->flags |= ATA_DFLAG_LBA;
1299
1300 if (ata_id_has_lba48(dev->id)) {
1301 dev->flags |= ATA_DFLAG_LBA48;
1302 dev->n_sectors = ata_id_u64(dev->id, 100);
1303 } else {
1304 dev->n_sectors = ata_id_u32(dev->id, 60);
1305 }
1306
1307 /* print device info to dmesg */
1308 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1309 ap->id, device,
1310 major_version,
1311 ata_mode_string(xfer_modes),
1312 (unsigned long long)dev->n_sectors,
1313 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1314 } else {
1315 /* CHS */
1316
1317 /* Default translation */
1318 dev->cylinders = dev->id[1];
1319 dev->heads = dev->id[3];
1320 dev->sectors = dev->id[6];
1321 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1322
1323 if (ata_id_current_chs_valid(dev->id)) {
1324 /* Current CHS translation is valid. */
1325 dev->cylinders = dev->id[54];
1326 dev->heads = dev->id[55];
1327 dev->sectors = dev->id[56];
1328
1329 dev->n_sectors = ata_id_u32(dev->id, 57);
1330 }
1331
1332 /* print device info to dmesg */
1333 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1334 ap->id, device,
1335 major_version,
1336 ata_mode_string(xfer_modes),
1337 (unsigned long long)dev->n_sectors,
1338 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
1342 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
1345 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001346 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (ata_id_is_ata(dev->id)) /* sanity check */
1348 goto err_out_nosup;
1349
1350 rc = atapi_cdb_len(dev->id);
1351 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1352 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1353 goto err_out_nosup;
1354 }
1355 ap->cdb_len = (unsigned int) rc;
1356 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1357
1358 /* print device info to dmesg */
1359 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1360 ap->id, device,
1361 ata_mode_string(xfer_modes));
1362 }
1363
1364 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1365 return;
1366
1367err_out_nosup:
1368 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1369 ap->id, device);
1370err_out:
1371 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1372 DPRINTK("EXIT, err\n");
1373}
1374
Brad Campbell6f2f3812005-05-12 15:07:47 -04001375
Jeff Garzik057ace52005-10-22 14:27:05 -04001376static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001377{
1378 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1379}
1380
1381/**
1382 * ata_dev_config - Run device specific handlers and check for
1383 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001384 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001385 * @i: Device
1386 *
1387 * LOCKING:
1388 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001389
Brad Campbell6f2f3812005-05-12 15:07:47 -04001390void ata_dev_config(struct ata_port *ap, unsigned int i)
1391{
1392 /* limit bridge transfers to udma5, 200 sectors */
1393 if (ata_dev_knobble(ap)) {
1394 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1395 ap->id, ap->device->devno);
1396 ap->udma_mask &= ATA_UDMA5;
1397 ap->host->max_sectors = ATA_MAX_SECTORS;
1398 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1399 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1400 }
1401
1402 if (ap->ops->dev_config)
1403 ap->ops->dev_config(ap, &ap->device[i]);
1404}
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406/**
1407 * ata_bus_probe - Reset and probe ATA bus
1408 * @ap: Bus to probe
1409 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001410 * Master ATA bus probing function. Initiates a hardware-dependent
1411 * bus reset, then attempts to identify any devices found on
1412 * the bus.
1413 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001415 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
1417 * RETURNS:
1418 * Zero on success, non-zero on error.
1419 */
1420
1421static int ata_bus_probe(struct ata_port *ap)
1422{
1423 unsigned int i, found = 0;
1424
1425 ap->ops->phy_reset(ap);
1426 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1427 goto err_out;
1428
1429 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1430 ata_dev_identify(ap, i);
1431 if (ata_dev_present(&ap->device[i])) {
1432 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001433 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435 }
1436
1437 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1438 goto err_out_disable;
1439
1440 ata_set_mode(ap);
1441 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1442 goto err_out_disable;
1443
1444 return 0;
1445
1446err_out_disable:
1447 ap->ops->port_disable(ap);
1448err_out:
1449 return -1;
1450}
1451
1452/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001453 * ata_port_probe - Mark port as enabled
1454 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001456 * Modify @ap data structure such that the system
1457 * thinks that the entire port is enabled.
1458 *
1459 * LOCKING: host_set lock, or some other form of
1460 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 */
1462
1463void ata_port_probe(struct ata_port *ap)
1464{
1465 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1466}
1467
1468/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001469 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1470 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001472 * This function issues commands to standard SATA Sxxx
1473 * PHY registers, to wake up the phy (and device), and
1474 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 *
1476 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001477 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 *
1479 */
1480void __sata_phy_reset(struct ata_port *ap)
1481{
1482 u32 sstatus;
1483 unsigned long timeout = jiffies + (HZ * 5);
1484
1485 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001486 /* issue phy wake/reset */
1487 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001488 /* Couldn't find anything in SATA I/II specs, but
1489 * AHCI-1.1 10.4.2 says at least 1 ms. */
1490 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
Brett Russcdcca892005-03-28 15:10:27 -05001492 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 /* wait for phy to become ready, if necessary */
1495 do {
1496 msleep(200);
1497 sstatus = scr_read(ap, SCR_STATUS);
1498 if ((sstatus & 0xf) != 1)
1499 break;
1500 } while (time_before(jiffies, timeout));
1501
1502 /* TODO: phy layer with polling, timeouts, etc. */
Jeff Garzik656563e2005-11-20 03:36:45 -05001503 sstatus = scr_read(ap, SCR_STATUS);
1504 if (sata_dev_present(ap)) {
1505 const char *speed;
1506 u32 tmp;
1507
1508 tmp = (sstatus >> 4) & 0xf;
1509 if (tmp & (1 << 0))
1510 speed = "1.5";
1511 else if (tmp & (1 << 1))
1512 speed = "3.0";
1513 else
1514 speed = "<unknown>";
1515 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1516 ap->id, speed, sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 ata_port_probe(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001518 } else {
1519 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 ap->id, sstatus);
1521 ata_port_disable(ap);
1522 }
1523
1524 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1525 return;
1526
1527 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1528 ata_port_disable(ap);
1529 return;
1530 }
1531
1532 ap->cbl = ATA_CBL_SATA;
1533}
1534
1535/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001536 * sata_phy_reset - Reset SATA bus.
1537 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001539 * This function resets the SATA bus, and then probes
1540 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 *
1542 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001543 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 *
1545 */
1546void sata_phy_reset(struct ata_port *ap)
1547{
1548 __sata_phy_reset(ap);
1549 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1550 return;
1551 ata_bus_reset(ap);
1552}
1553
1554/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001555 * ata_port_disable - Disable port.
1556 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001558 * Modify @ap data structure such that the system
1559 * thinks that the entire port is disabled, and should
1560 * never attempt to probe or communicate with devices
1561 * on this port.
1562 *
1563 * LOCKING: host_set lock, or some other form of
1564 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 */
1566
1567void ata_port_disable(struct ata_port *ap)
1568{
1569 ap->device[0].class = ATA_DEV_NONE;
1570 ap->device[1].class = ATA_DEV_NONE;
1571 ap->flags |= ATA_FLAG_PORT_DISABLED;
1572}
1573
Alan Cox452503f2005-10-21 19:01:32 -04001574/*
1575 * This mode timing computation functionality is ported over from
1576 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1577 */
1578/*
1579 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1580 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1581 * for PIO 5, which is a nonstandard extension and UDMA6, which
1582 * is currently supported only by Maxtor drives.
1583 */
1584
1585static const struct ata_timing ata_timing[] = {
1586
1587 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1588 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1589 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1590 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1591
1592 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1593 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1594 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1595
1596/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1597
1598 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1599 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1600 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1601
1602 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1603 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1604 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1605
1606/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1607 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1608 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1609
1610 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1611 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1612 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1613
1614/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1615
1616 { 0xFF }
1617};
1618
1619#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1620#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1621
1622static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1623{
1624 q->setup = EZ(t->setup * 1000, T);
1625 q->act8b = EZ(t->act8b * 1000, T);
1626 q->rec8b = EZ(t->rec8b * 1000, T);
1627 q->cyc8b = EZ(t->cyc8b * 1000, T);
1628 q->active = EZ(t->active * 1000, T);
1629 q->recover = EZ(t->recover * 1000, T);
1630 q->cycle = EZ(t->cycle * 1000, T);
1631 q->udma = EZ(t->udma * 1000, UT);
1632}
1633
1634void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1635 struct ata_timing *m, unsigned int what)
1636{
1637 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1638 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1639 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1640 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1641 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1642 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1643 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1644 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1645}
1646
1647static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1648{
1649 const struct ata_timing *t;
1650
1651 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001652 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001653 return NULL;
1654 return t;
1655}
1656
1657int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1658 struct ata_timing *t, int T, int UT)
1659{
1660 const struct ata_timing *s;
1661 struct ata_timing p;
1662
1663 /*
1664 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001665 */
Alan Cox452503f2005-10-21 19:01:32 -04001666
1667 if (!(s = ata_timing_find_mode(speed)))
1668 return -EINVAL;
1669
Albert Lee75b1f2f2005-11-16 17:06:18 +08001670 memcpy(t, s, sizeof(*s));
1671
Alan Cox452503f2005-10-21 19:01:32 -04001672 /*
1673 * If the drive is an EIDE drive, it can tell us it needs extended
1674 * PIO/MW_DMA cycle timing.
1675 */
1676
1677 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1678 memset(&p, 0, sizeof(p));
1679 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1680 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1681 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1682 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1683 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1684 }
1685 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1686 }
1687
1688 /*
1689 * Convert the timing to bus clock counts.
1690 */
1691
Albert Lee75b1f2f2005-11-16 17:06:18 +08001692 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001693
1694 /*
1695 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1696 * and some other commands. We have to ensure that the DMA cycle timing is
1697 * slower/equal than the fastest PIO timing.
1698 */
1699
1700 if (speed > XFER_PIO_4) {
1701 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1702 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1703 }
1704
1705 /*
1706 * Lenghten active & recovery time so that cycle time is correct.
1707 */
1708
1709 if (t->act8b + t->rec8b < t->cyc8b) {
1710 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1711 t->rec8b = t->cyc8b - t->act8b;
1712 }
1713
1714 if (t->active + t->recover < t->cycle) {
1715 t->active += (t->cycle - (t->active + t->recover)) / 2;
1716 t->recover = t->cycle - t->active;
1717 }
1718
1719 return 0;
1720}
1721
Jeff Garzik057ace52005-10-22 14:27:05 -04001722static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 unsigned int shift;
1724 u8 base;
1725} xfer_mode_classes[] = {
1726 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1727 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1728 { ATA_SHIFT_PIO, XFER_PIO_0 },
1729};
1730
1731static inline u8 base_from_shift(unsigned int shift)
1732{
1733 int i;
1734
1735 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1736 if (xfer_mode_classes[i].shift == shift)
1737 return xfer_mode_classes[i].base;
1738
1739 return 0xff;
1740}
1741
1742static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1743{
1744 int ofs, idx;
1745 u8 base;
1746
1747 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1748 return;
1749
1750 if (dev->xfer_shift == ATA_SHIFT_PIO)
1751 dev->flags |= ATA_DFLAG_PIO;
1752
1753 ata_dev_set_xfermode(ap, dev);
1754
1755 base = base_from_shift(dev->xfer_shift);
1756 ofs = dev->xfer_mode - base;
1757 idx = ofs + dev->xfer_shift;
1758 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1759
1760 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1761 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1762
1763 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1764 ap->id, dev->devno, xfer_mode_str[idx]);
1765}
1766
1767static int ata_host_set_pio(struct ata_port *ap)
1768{
1769 unsigned int mask;
1770 int x, i;
1771 u8 base, xfer_mode;
1772
1773 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1774 x = fgb(mask);
1775 if (x < 0) {
1776 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1777 return -1;
1778 }
1779
1780 base = base_from_shift(ATA_SHIFT_PIO);
1781 xfer_mode = base + x;
1782
1783 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1784 (int)base, (int)xfer_mode, mask, x);
1785
1786 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1787 struct ata_device *dev = &ap->device[i];
1788 if (ata_dev_present(dev)) {
1789 dev->pio_mode = xfer_mode;
1790 dev->xfer_mode = xfer_mode;
1791 dev->xfer_shift = ATA_SHIFT_PIO;
1792 if (ap->ops->set_piomode)
1793 ap->ops->set_piomode(ap, dev);
1794 }
1795 }
1796
1797 return 0;
1798}
1799
1800static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1801 unsigned int xfer_shift)
1802{
1803 int i;
1804
1805 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1806 struct ata_device *dev = &ap->device[i];
1807 if (ata_dev_present(dev)) {
1808 dev->dma_mode = xfer_mode;
1809 dev->xfer_mode = xfer_mode;
1810 dev->xfer_shift = xfer_shift;
1811 if (ap->ops->set_dmamode)
1812 ap->ops->set_dmamode(ap, dev);
1813 }
1814 }
1815}
1816
1817/**
1818 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1819 * @ap: port on which timings will be programmed
1820 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001821 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1822 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001824 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 *
1826 */
1827static void ata_set_mode(struct ata_port *ap)
1828{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001829 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 u8 xfer_mode;
1831 int rc;
1832
1833 /* step 1: always set host PIO timings */
1834 rc = ata_host_set_pio(ap);
1835 if (rc)
1836 goto err_out;
1837
1838 /* step 2: choose the best data xfer mode */
1839 xfer_mode = xfer_shift = 0;
1840 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1841 if (rc)
1842 goto err_out;
1843
1844 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1845 if (xfer_shift != ATA_SHIFT_PIO)
1846 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1847
1848 /* step 4: update devices' xfer mode */
1849 ata_dev_set_mode(ap, &ap->device[0]);
1850 ata_dev_set_mode(ap, &ap->device[1]);
1851
1852 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1853 return;
1854
1855 if (ap->ops->post_set_mode)
1856 ap->ops->post_set_mode(ap);
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return;
1859
1860err_out:
1861 ata_port_disable(ap);
1862}
1863
1864/**
1865 * ata_busy_sleep - sleep until BSY clears, or timeout
1866 * @ap: port containing status register to be polled
1867 * @tmout_pat: impatience timeout
1868 * @tmout: overall timeout
1869 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001870 * Sleep until ATA Status register bit BSY clears,
1871 * or a timeout occurs.
1872 *
1873 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 *
1875 */
1876
1877static unsigned int ata_busy_sleep (struct ata_port *ap,
1878 unsigned long tmout_pat,
1879 unsigned long tmout)
1880{
1881 unsigned long timer_start, timeout;
1882 u8 status;
1883
1884 status = ata_busy_wait(ap, ATA_BUSY, 300);
1885 timer_start = jiffies;
1886 timeout = timer_start + tmout_pat;
1887 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1888 msleep(50);
1889 status = ata_busy_wait(ap, ATA_BUSY, 3);
1890 }
1891
1892 if (status & ATA_BUSY)
1893 printk(KERN_WARNING "ata%u is slow to respond, "
1894 "please be patient\n", ap->id);
1895
1896 timeout = timer_start + tmout;
1897 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1898 msleep(50);
1899 status = ata_chk_status(ap);
1900 }
1901
1902 if (status & ATA_BUSY) {
1903 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1904 ap->id, tmout / HZ);
1905 return 1;
1906 }
1907
1908 return 0;
1909}
1910
1911static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1912{
1913 struct ata_ioports *ioaddr = &ap->ioaddr;
1914 unsigned int dev0 = devmask & (1 << 0);
1915 unsigned int dev1 = devmask & (1 << 1);
1916 unsigned long timeout;
1917
1918 /* if device 0 was found in ata_devchk, wait for its
1919 * BSY bit to clear
1920 */
1921 if (dev0)
1922 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1923
1924 /* if device 1 was found in ata_devchk, wait for
1925 * register access, then wait for BSY to clear
1926 */
1927 timeout = jiffies + ATA_TMOUT_BOOT;
1928 while (dev1) {
1929 u8 nsect, lbal;
1930
1931 ap->ops->dev_select(ap, 1);
1932 if (ap->flags & ATA_FLAG_MMIO) {
1933 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1934 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1935 } else {
1936 nsect = inb(ioaddr->nsect_addr);
1937 lbal = inb(ioaddr->lbal_addr);
1938 }
1939 if ((nsect == 1) && (lbal == 1))
1940 break;
1941 if (time_after(jiffies, timeout)) {
1942 dev1 = 0;
1943 break;
1944 }
1945 msleep(50); /* give drive a breather */
1946 }
1947 if (dev1)
1948 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1949
1950 /* is all this really necessary? */
1951 ap->ops->dev_select(ap, 0);
1952 if (dev1)
1953 ap->ops->dev_select(ap, 1);
1954 if (dev0)
1955 ap->ops->dev_select(ap, 0);
1956}
1957
1958/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001959 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1960 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001962 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1963 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 *
1965 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001966 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001967 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 *
1969 */
1970
1971static unsigned int ata_bus_edd(struct ata_port *ap)
1972{
1973 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001974 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 /* set up execute-device-diag (bus reset) taskfile */
1977 /* also, take interrupts to a known state (disabled) */
1978 DPRINTK("execute-device-diag\n");
1979 ata_tf_init(ap, &tf, 0);
1980 tf.ctl |= ATA_NIEN;
1981 tf.command = ATA_CMD_EDD;
1982 tf.protocol = ATA_PROT_NODATA;
1983
1984 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001985 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001987 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 /* spec says at least 2ms. but who knows with those
1990 * crazy ATAPI devices...
1991 */
1992 msleep(150);
1993
1994 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1995}
1996
1997static unsigned int ata_bus_softreset(struct ata_port *ap,
1998 unsigned int devmask)
1999{
2000 struct ata_ioports *ioaddr = &ap->ioaddr;
2001
2002 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2003
2004 /* software reset. causes dev0 to be selected */
2005 if (ap->flags & ATA_FLAG_MMIO) {
2006 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2007 udelay(20); /* FIXME: flush */
2008 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2009 udelay(20); /* FIXME: flush */
2010 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2011 } else {
2012 outb(ap->ctl, ioaddr->ctl_addr);
2013 udelay(10);
2014 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2015 udelay(10);
2016 outb(ap->ctl, ioaddr->ctl_addr);
2017 }
2018
2019 /* spec mandates ">= 2ms" before checking status.
2020 * We wait 150ms, because that was the magic delay used for
2021 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2022 * between when the ATA command register is written, and then
2023 * status is checked. Because waiting for "a while" before
2024 * checking status is fine, post SRST, we perform this magic
2025 * delay here as well.
2026 */
2027 msleep(150);
2028
2029 ata_bus_post_reset(ap, devmask);
2030
2031 return 0;
2032}
2033
2034/**
2035 * ata_bus_reset - reset host port and associated ATA channel
2036 * @ap: port to reset
2037 *
2038 * This is typically the first time we actually start issuing
2039 * commands to the ATA channel. We wait for BSY to clear, then
2040 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2041 * result. Determine what devices, if any, are on the channel
2042 * by looking at the device 0/1 error register. Look at the signature
2043 * stored in each device's taskfile registers, to determine if
2044 * the device is ATA or ATAPI.
2045 *
2046 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002047 * PCI/etc. bus probe sem.
2048 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 *
2050 * SIDE EFFECTS:
2051 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2052 */
2053
2054void ata_bus_reset(struct ata_port *ap)
2055{
2056 struct ata_ioports *ioaddr = &ap->ioaddr;
2057 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2058 u8 err;
2059 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2060
2061 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2062
2063 /* determine if device 0/1 are present */
2064 if (ap->flags & ATA_FLAG_SATA_RESET)
2065 dev0 = 1;
2066 else {
2067 dev0 = ata_devchk(ap, 0);
2068 if (slave_possible)
2069 dev1 = ata_devchk(ap, 1);
2070 }
2071
2072 if (dev0)
2073 devmask |= (1 << 0);
2074 if (dev1)
2075 devmask |= (1 << 1);
2076
2077 /* select device 0 again */
2078 ap->ops->dev_select(ap, 0);
2079
2080 /* issue bus reset */
2081 if (ap->flags & ATA_FLAG_SRST)
2082 rc = ata_bus_softreset(ap, devmask);
2083 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2084 /* set up device control */
2085 if (ap->flags & ATA_FLAG_MMIO)
2086 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2087 else
2088 outb(ap->ctl, ioaddr->ctl_addr);
2089 rc = ata_bus_edd(ap);
2090 }
2091
2092 if (rc)
2093 goto err_out;
2094
2095 /*
2096 * determine by signature whether we have ATA or ATAPI devices
2097 */
2098 err = ata_dev_try_classify(ap, 0);
2099 if ((slave_possible) && (err != 0x81))
2100 ata_dev_try_classify(ap, 1);
2101
2102 /* re-enable interrupts */
2103 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2104 ata_irq_on(ap);
2105
2106 /* is double-select really necessary? */
2107 if (ap->device[1].class != ATA_DEV_NONE)
2108 ap->ops->dev_select(ap, 1);
2109 if (ap->device[0].class != ATA_DEV_NONE)
2110 ap->ops->dev_select(ap, 0);
2111
2112 /* if no devices were detected, disable this port */
2113 if ((ap->device[0].class == ATA_DEV_NONE) &&
2114 (ap->device[1].class == ATA_DEV_NONE))
2115 goto err_out;
2116
2117 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2118 /* set up device control for ATA_FLAG_SATA_RESET */
2119 if (ap->flags & ATA_FLAG_MMIO)
2120 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2121 else
2122 outb(ap->ctl, ioaddr->ctl_addr);
2123 }
2124
2125 DPRINTK("EXIT\n");
2126 return;
2127
2128err_out:
2129 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2130 ap->ops->port_disable(ap);
2131
2132 DPRINTK("EXIT\n");
2133}
2134
Jeff Garzik057ace52005-10-22 14:27:05 -04002135static void ata_pr_blacklisted(const struct ata_port *ap,
2136 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2139 ap->id, dev->devno);
2140}
2141
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002142static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 "WDC AC11000H",
2144 "WDC AC22100H",
2145 "WDC AC32500H",
2146 "WDC AC33100H",
2147 "WDC AC31600H",
2148 "WDC AC32100H",
2149 "WDC AC23200L",
2150 "Compaq CRD-8241B",
2151 "CRD-8400B",
2152 "CRD-8480B",
2153 "CRD-8482B",
2154 "CRD-84",
2155 "SanDisk SDP3B",
2156 "SanDisk SDP3B-64",
2157 "SANYO CD-ROM CRD",
2158 "HITACHI CDR-8",
2159 "HITACHI CDR-8335",
2160 "HITACHI CDR-8435",
2161 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002162 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 "CD-532E-A",
2164 "E-IDE CD-ROM CR-840",
2165 "CD-ROM Drive/F5A",
2166 "WPI CDD-820",
2167 "SAMSUNG CD-ROM SC-148C",
2168 "SAMSUNG CD-ROM SC",
2169 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2171 "_NEC DV5800A",
2172};
2173
Jeff Garzik057ace52005-10-22 14:27:05 -04002174static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 unsigned char model_num[40];
2177 char *s;
2178 unsigned int len;
2179 int i;
2180
2181 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2182 sizeof(model_num));
2183 s = &model_num[0];
2184 len = strnlen(s, sizeof(model_num));
2185
2186 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2187 while ((len > 0) && (s[len - 1] == ' ')) {
2188 len--;
2189 s[len] = 0;
2190 }
2191
2192 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2193 if (!strncmp(ata_dma_blacklist[i], s, len))
2194 return 1;
2195
2196 return 0;
2197}
2198
Jeff Garzik057ace52005-10-22 14:27:05 -04002199static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Jeff Garzik057ace52005-10-22 14:27:05 -04002201 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 unsigned int mask;
2203
2204 master = &ap->device[0];
2205 slave = &ap->device[1];
2206
2207 assert (ata_dev_present(master) || ata_dev_present(slave));
2208
2209 if (shift == ATA_SHIFT_UDMA) {
2210 mask = ap->udma_mask;
2211 if (ata_dev_present(master)) {
2212 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002213 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 mask = 0;
2215 ata_pr_blacklisted(ap, master);
2216 }
2217 }
2218 if (ata_dev_present(slave)) {
2219 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002220 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 mask = 0;
2222 ata_pr_blacklisted(ap, slave);
2223 }
2224 }
2225 }
2226 else if (shift == ATA_SHIFT_MWDMA) {
2227 mask = ap->mwdma_mask;
2228 if (ata_dev_present(master)) {
2229 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002230 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 mask = 0;
2232 ata_pr_blacklisted(ap, master);
2233 }
2234 }
2235 if (ata_dev_present(slave)) {
2236 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002237 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 mask = 0;
2239 ata_pr_blacklisted(ap, slave);
2240 }
2241 }
2242 }
2243 else if (shift == ATA_SHIFT_PIO) {
2244 mask = ap->pio_mask;
2245 if (ata_dev_present(master)) {
2246 /* spec doesn't return explicit support for
2247 * PIO0-2, so we fake it
2248 */
2249 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2250 tmp_mode <<= 3;
2251 tmp_mode |= 0x7;
2252 mask &= tmp_mode;
2253 }
2254 if (ata_dev_present(slave)) {
2255 /* spec doesn't return explicit support for
2256 * PIO0-2, so we fake it
2257 */
2258 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2259 tmp_mode <<= 3;
2260 tmp_mode |= 0x7;
2261 mask &= tmp_mode;
2262 }
2263 }
2264 else {
2265 mask = 0xffffffff; /* shut up compiler warning */
2266 BUG();
2267 }
2268
2269 return mask;
2270}
2271
2272/* find greatest bit */
2273static int fgb(u32 bitmap)
2274{
2275 unsigned int i;
2276 int x = -1;
2277
2278 for (i = 0; i < 32; i++)
2279 if (bitmap & (1 << i))
2280 x = i;
2281
2282 return x;
2283}
2284
2285/**
2286 * ata_choose_xfer_mode - attempt to find best transfer mode
2287 * @ap: Port for which an xfer mode will be selected
2288 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2289 * @xfer_shift_out: (output) bit shift that selects this mode
2290 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002291 * Based on host and device capabilities, determine the
2292 * maximum transfer mode that is amenable to all.
2293 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002295 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 *
2297 * RETURNS:
2298 * Zero on success, negative on error.
2299 */
2300
Jeff Garzik057ace52005-10-22 14:27:05 -04002301static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 u8 *xfer_mode_out,
2303 unsigned int *xfer_shift_out)
2304{
2305 unsigned int mask, shift;
2306 int x, i;
2307
2308 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2309 shift = xfer_mode_classes[i].shift;
2310 mask = ata_get_mode_mask(ap, shift);
2311
2312 x = fgb(mask);
2313 if (x >= 0) {
2314 *xfer_mode_out = xfer_mode_classes[i].base + x;
2315 *xfer_shift_out = shift;
2316 return 0;
2317 }
2318 }
2319
2320 return -1;
2321}
2322
2323/**
2324 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2325 * @ap: Port associated with device @dev
2326 * @dev: Device to which command will be sent
2327 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002328 * Issue SET FEATURES - XFER MODE command to device @dev
2329 * on port @ap.
2330 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002332 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 */
2334
2335static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2336{
Tejun Heoa0123702005-12-13 14:49:31 +09002337 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 /* set up set-features taskfile */
2340 DPRINTK("set features - xfer mode\n");
2341
Tejun Heoa0123702005-12-13 14:49:31 +09002342 ata_tf_init(ap, &tf, dev->devno);
2343 tf.command = ATA_CMD_SET_FEATURES;
2344 tf.feature = SETFEATURES_XFER;
2345 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2346 tf.protocol = ATA_PROT_NODATA;
2347 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Tejun Heoa0123702005-12-13 14:49:31 +09002349 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2350 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2351 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 DPRINTK("EXIT\n");
2356}
2357
2358/**
Albert Lee59a10b12005-10-12 15:09:42 +08002359 * ata_dev_reread_id - Reread the device identify device info
2360 * @ap: port where the device is
2361 * @dev: device to reread the identify device info
2362 *
2363 * LOCKING:
2364 */
2365
2366static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2367{
Tejun Heoa0123702005-12-13 14:49:31 +09002368 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002369
Tejun Heoa0123702005-12-13 14:49:31 +09002370 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002371
2372 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002373 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002374 DPRINTK("do ATA identify\n");
2375 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002376 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002377 DPRINTK("do ATAPI identify\n");
2378 }
2379
Tejun Heoa0123702005-12-13 14:49:31 +09002380 tf.flags |= ATA_TFLAG_DEVICE;
2381 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002382
Tejun Heoa0123702005-12-13 14:49:31 +09002383 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2384 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002385 goto err_out;
2386
Albert Lee59a10b12005-10-12 15:09:42 +08002387 swap_buf_le16(dev->id, ATA_ID_WORDS);
2388
2389 ata_dump_id(dev);
2390
2391 DPRINTK("EXIT\n");
2392
2393 return;
2394err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002395 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002396 ata_port_disable(ap);
2397}
2398
2399/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002400 * ata_dev_init_params - Issue INIT DEV PARAMS command
2401 * @ap: Port associated with device @dev
2402 * @dev: Device to which command will be sent
2403 *
2404 * LOCKING:
2405 */
2406
2407static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2408{
Tejun Heoa0123702005-12-13 14:49:31 +09002409 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002410 u16 sectors = dev->id[6];
2411 u16 heads = dev->id[3];
2412
2413 /* Number of sectors per track 1-255. Number of heads 1-16 */
2414 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2415 return;
2416
2417 /* set up init dev params taskfile */
2418 DPRINTK("init dev params \n");
2419
Tejun Heoa0123702005-12-13 14:49:31 +09002420 ata_tf_init(ap, &tf, dev->devno);
2421 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2422 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2423 tf.protocol = ATA_PROT_NODATA;
2424 tf.nsect = sectors;
2425 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002426
Tejun Heoa0123702005-12-13 14:49:31 +09002427 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2428 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2429 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002430 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002431 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002432
2433 DPRINTK("EXIT\n");
2434}
2435
2436/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002437 * ata_sg_clean - Unmap DMA memory associated with command
2438 * @qc: Command containing DMA memory to be released
2439 *
2440 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 *
2442 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002443 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 */
2445
2446static void ata_sg_clean(struct ata_queued_cmd *qc)
2447{
2448 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002449 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002451 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2454 assert(sg != NULL);
2455
2456 if (qc->flags & ATA_QCFLAG_SINGLE)
2457 assert(qc->n_elem == 1);
2458
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002459 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002461 /* if we padded the buffer out to 32-bit bound, and data
2462 * xfer direction is from-device, we must copy from the
2463 * pad buffer back into the supplied buffer
2464 */
2465 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2466 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2467
2468 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002469 if (qc->n_elem)
2470 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002471 /* restore last sg */
2472 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2473 if (pad_buf) {
2474 struct scatterlist *psg = &qc->pad_sgent;
2475 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2476 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002477 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002478 }
2479 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002480 if (sg_dma_len(&sg[0]) > 0)
2481 dma_unmap_single(ap->host_set->dev,
2482 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2483 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002484 /* restore sg */
2485 sg->length += qc->pad_len;
2486 if (pad_buf)
2487 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2488 pad_buf, qc->pad_len);
2489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002492 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
2494
2495/**
2496 * ata_fill_sg - Fill PCI IDE PRD table
2497 * @qc: Metadata associated with taskfile to be transferred
2498 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002499 * Fill PCI IDE PRD (scatter-gather) table with segments
2500 * associated with the current disk command.
2501 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002503 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 *
2505 */
2506static void ata_fill_sg(struct ata_queued_cmd *qc)
2507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002509 struct scatterlist *sg;
2510 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002512 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 assert(qc->n_elem > 0);
2514
2515 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002516 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 u32 addr, offset;
2518 u32 sg_len, len;
2519
2520 /* determine if physical DMA addr spans 64K boundary.
2521 * Note h/w doesn't support 64-bit, so we unconditionally
2522 * truncate dma_addr_t to u32.
2523 */
2524 addr = (u32) sg_dma_address(sg);
2525 sg_len = sg_dma_len(sg);
2526
2527 while (sg_len) {
2528 offset = addr & 0xffff;
2529 len = sg_len;
2530 if ((offset + sg_len) > 0x10000)
2531 len = 0x10000 - offset;
2532
2533 ap->prd[idx].addr = cpu_to_le32(addr);
2534 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2535 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2536
2537 idx++;
2538 sg_len -= len;
2539 addr += len;
2540 }
2541 }
2542
2543 if (idx)
2544 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2545}
2546/**
2547 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2548 * @qc: Metadata associated with taskfile to check
2549 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002550 * Allow low-level driver to filter ATA PACKET commands, returning
2551 * a status indicating whether or not it is OK to use DMA for the
2552 * supplied PACKET command.
2553 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002555 * spin_lock_irqsave(host_set lock)
2556 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 * RETURNS: 0 when ATAPI DMA can be used
2558 * nonzero otherwise
2559 */
2560int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2561{
2562 struct ata_port *ap = qc->ap;
2563 int rc = 0; /* Assume ATAPI DMA is OK by default */
2564
2565 if (ap->ops->check_atapi_dma)
2566 rc = ap->ops->check_atapi_dma(qc);
2567
2568 return rc;
2569}
2570/**
2571 * ata_qc_prep - Prepare taskfile for submission
2572 * @qc: Metadata associated with taskfile to be prepared
2573 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002574 * Prepare ATA taskfile for submission.
2575 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 * LOCKING:
2577 * spin_lock_irqsave(host_set lock)
2578 */
2579void ata_qc_prep(struct ata_queued_cmd *qc)
2580{
2581 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2582 return;
2583
2584 ata_fill_sg(qc);
2585}
2586
Jeff Garzik0cba6322005-05-30 19:49:12 -04002587/**
2588 * ata_sg_init_one - Associate command with memory buffer
2589 * @qc: Command to be associated
2590 * @buf: Memory buffer
2591 * @buflen: Length of memory buffer, in bytes.
2592 *
2593 * Initialize the data-related elements of queued_cmd @qc
2594 * to point to a single memory buffer, @buf of byte length @buflen.
2595 *
2596 * LOCKING:
2597 * spin_lock_irqsave(host_set lock)
2598 */
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2601{
2602 struct scatterlist *sg;
2603
2604 qc->flags |= ATA_QCFLAG_SINGLE;
2605
2606 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002607 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002609 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 qc->buf_virt = buf;
2611
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002612 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002613 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
Jeff Garzik0cba6322005-05-30 19:49:12 -04002616/**
2617 * ata_sg_init - Associate command with scatter-gather table.
2618 * @qc: Command to be associated
2619 * @sg: Scatter-gather table.
2620 * @n_elem: Number of elements in s/g table.
2621 *
2622 * Initialize the data-related elements of queued_cmd @qc
2623 * to point to a scatter-gather table @sg, containing @n_elem
2624 * elements.
2625 *
2626 * LOCKING:
2627 * spin_lock_irqsave(host_set lock)
2628 */
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2631 unsigned int n_elem)
2632{
2633 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002634 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002636 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
2638
2639/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002640 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2641 * @qc: Command with memory buffer to be mapped.
2642 *
2643 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 *
2645 * LOCKING:
2646 * spin_lock_irqsave(host_set lock)
2647 *
2648 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002649 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 */
2651
2652static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2653{
2654 struct ata_port *ap = qc->ap;
2655 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002656 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 dma_addr_t dma_address;
2658
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002659 /* we must lengthen transfers to end on a 32-bit boundary */
2660 qc->pad_len = sg->length & 3;
2661 if (qc->pad_len) {
2662 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2663 struct scatterlist *psg = &qc->pad_sgent;
2664
2665 assert(qc->dev->class == ATA_DEV_ATAPI);
2666
2667 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2668
2669 if (qc->tf.flags & ATA_TFLAG_WRITE)
2670 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2671 qc->pad_len);
2672
2673 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2674 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2675 /* trim sg */
2676 sg->length -= qc->pad_len;
2677
2678 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2679 sg->length, qc->pad_len);
2680 }
2681
Jeff Garzike1410f22005-11-14 14:06:26 -05002682 if (!sg->length) {
2683 sg_dma_address(sg) = 0;
2684 goto skip_map;
2685 }
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002688 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002689 if (dma_mapping_error(dma_address)) {
2690 /* restore sg */
2691 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002696skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002697 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2700 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2701
2702 return 0;
2703}
2704
2705/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002706 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2707 * @qc: Command with scatter-gather table to be mapped.
2708 *
2709 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 *
2711 * LOCKING:
2712 * spin_lock_irqsave(host_set lock)
2713 *
2714 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002715 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 *
2717 */
2718
2719static int ata_sg_setup(struct ata_queued_cmd *qc)
2720{
2721 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002722 struct scatterlist *sg = qc->__sg;
2723 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002724 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 VPRINTK("ENTER, ata%u\n", ap->id);
2727 assert(qc->flags & ATA_QCFLAG_SG);
2728
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002729 /* we must lengthen transfers to end on a 32-bit boundary */
2730 qc->pad_len = lsg->length & 3;
2731 if (qc->pad_len) {
2732 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2733 struct scatterlist *psg = &qc->pad_sgent;
2734 unsigned int offset;
2735
2736 assert(qc->dev->class == ATA_DEV_ATAPI);
2737
2738 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2739
2740 /*
2741 * psg->page/offset are used to copy to-be-written
2742 * data in this function or read data in ata_sg_clean.
2743 */
2744 offset = lsg->offset + lsg->length - qc->pad_len;
2745 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2746 psg->offset = offset_in_page(offset);
2747
2748 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2749 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2750 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002751 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002752 }
2753
2754 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2755 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2756 /* trim last sg */
2757 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002758 if (lsg->length == 0)
2759 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002760
2761 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2762 qc->n_elem - 1, lsg->length, qc->pad_len);
2763 }
2764
Jeff Garzike1410f22005-11-14 14:06:26 -05002765 pre_n_elem = qc->n_elem;
2766 if (trim_sg && pre_n_elem)
2767 pre_n_elem--;
2768
2769 if (!pre_n_elem) {
2770 n_elem = 0;
2771 goto skip_map;
2772 }
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002775 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002776 if (n_elem < 1) {
2777 /* restore last sg */
2778 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 DPRINTK("%d sg elements mapped\n", n_elem);
2783
Jeff Garzike1410f22005-11-14 14:06:26 -05002784skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 qc->n_elem = n_elem;
2786
2787 return 0;
2788}
2789
2790/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002791 * ata_poll_qc_complete - turn irq back on and finish qc
2792 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002793 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002794 *
2795 * LOCKING:
2796 * None. (grabs host lock)
2797 */
2798
Albert Leea22e2eb2005-12-05 15:38:02 +08002799void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002800{
2801 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002802 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002803
Jeff Garzikb8f61532005-08-25 22:01:20 -04002804 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002805 ap->flags &= ~ATA_FLAG_NOINTR;
2806 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002807 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002808 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002809}
2810
2811/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002813 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 *
2815 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002816 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 *
2818 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002819 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 */
2821
2822static unsigned long ata_pio_poll(struct ata_port *ap)
2823{
Albert Leec14b8332005-12-05 15:36:08 +08002824 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002826 unsigned int poll_state = HSM_ST_UNKNOWN;
2827 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Albert Leec14b8332005-12-05 15:36:08 +08002829 qc = ata_qc_from_tag(ap, ap->active_tag);
2830 assert(qc != NULL);
2831
Albert Lee14be71f2005-09-27 17:36:35 +08002832 switch (ap->hsm_task_state) {
2833 case HSM_ST:
2834 case HSM_ST_POLL:
2835 poll_state = HSM_ST_POLL;
2836 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002838 case HSM_ST_LAST:
2839 case HSM_ST_LAST_POLL:
2840 poll_state = HSM_ST_LAST_POLL;
2841 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 break;
2843 default:
2844 BUG();
2845 break;
2846 }
2847
2848 status = ata_chk_status(ap);
2849 if (status & ATA_BUSY) {
2850 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee1c848982005-12-05 15:40:15 +08002851 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee7c398332005-11-09 13:03:30 +08002852 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return 0;
2854 }
Albert Lee14be71f2005-09-27 17:36:35 +08002855 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return ATA_SHORT_PAUSE;
2857 }
2858
Albert Lee14be71f2005-09-27 17:36:35 +08002859 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return 0;
2861}
2862
2863/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002864 * ata_pio_complete - check if drive is busy or idle
2865 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 *
2867 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002868 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002869 *
2870 * RETURNS:
2871 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 */
2873
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002874static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
2876 struct ata_queued_cmd *qc;
2877 u8 drv_stat;
2878
2879 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002880 * This is purely heuristic. This is a fast path. Sometimes when
2881 * we enter, BSY will be cleared in a chk-status or two. If not,
2882 * the drive is probably seeking or something. Snooze for a couple
2883 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002884 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 */
Albert Leefe79e682005-12-06 11:34:59 +08002886 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2887 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002889 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2890 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002891 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895 }
2896
Albert Leec14b8332005-12-05 15:36:08 +08002897 qc = ata_qc_from_tag(ap, ap->active_tag);
2898 assert(qc != NULL);
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 drv_stat = ata_wait_idle(ap);
2901 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002902 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002903 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002904 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
2906
Albert Lee14be71f2005-09-27 17:36:35 +08002907 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Albert Leea22e2eb2005-12-05 15:38:02 +08002909 assert(qc->err_mask == 0);
2910 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002911
2912 /* another command may start at this point */
2913
2914 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
Edward Falk0baab862005-06-02 18:17:13 -04002917
2918/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002919 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002920 * @buf: Buffer to swap
2921 * @buf_words: Number of 16-bit words in buffer.
2922 *
2923 * Swap halves of 16-bit words if needed to convert from
2924 * little-endian byte order to native cpu byte order, or
2925 * vice-versa.
2926 *
2927 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002928 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930void swap_buf_le16(u16 *buf, unsigned int buf_words)
2931{
2932#ifdef __BIG_ENDIAN
2933 unsigned int i;
2934
2935 for (i = 0; i < buf_words; i++)
2936 buf[i] = le16_to_cpu(buf[i]);
2937#endif /* __BIG_ENDIAN */
2938}
2939
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002940/**
2941 * ata_mmio_data_xfer - Transfer data by MMIO
2942 * @ap: port to read/write
2943 * @buf: data buffer
2944 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002945 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002946 *
2947 * Transfer data from/to the device data register by MMIO.
2948 *
2949 * LOCKING:
2950 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002951 */
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2954 unsigned int buflen, int write_data)
2955{
2956 unsigned int i;
2957 unsigned int words = buflen >> 1;
2958 u16 *buf16 = (u16 *) buf;
2959 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2960
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002961 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (write_data) {
2963 for (i = 0; i < words; i++)
2964 writew(le16_to_cpu(buf16[i]), mmio);
2965 } else {
2966 for (i = 0; i < words; i++)
2967 buf16[i] = cpu_to_le16(readw(mmio));
2968 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002969
2970 /* Transfer trailing 1 byte, if any. */
2971 if (unlikely(buflen & 0x01)) {
2972 u16 align_buf[1] = { 0 };
2973 unsigned char *trailing_buf = buf + buflen - 1;
2974
2975 if (write_data) {
2976 memcpy(align_buf, trailing_buf, 1);
2977 writew(le16_to_cpu(align_buf[0]), mmio);
2978 } else {
2979 align_buf[0] = cpu_to_le16(readw(mmio));
2980 memcpy(trailing_buf, align_buf, 1);
2981 }
2982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002985/**
2986 * ata_pio_data_xfer - Transfer data by PIO
2987 * @ap: port to read/write
2988 * @buf: data buffer
2989 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002990 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002991 *
2992 * Transfer data from/to the device data register by PIO.
2993 *
2994 * LOCKING:
2995 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002996 */
2997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2999 unsigned int buflen, int write_data)
3000{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003001 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003003 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003005 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003007 insw(ap->ioaddr.data_addr, buf, words);
3008
3009 /* Transfer trailing 1 byte, if any. */
3010 if (unlikely(buflen & 0x01)) {
3011 u16 align_buf[1] = { 0 };
3012 unsigned char *trailing_buf = buf + buflen - 1;
3013
3014 if (write_data) {
3015 memcpy(align_buf, trailing_buf, 1);
3016 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3017 } else {
3018 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3019 memcpy(trailing_buf, align_buf, 1);
3020 }
3021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003024/**
3025 * ata_data_xfer - Transfer data from/to the data register.
3026 * @ap: port to read/write
3027 * @buf: data buffer
3028 * @buflen: buffer length
3029 * @do_write: read/write
3030 *
3031 * Transfer data from/to the device data register.
3032 *
3033 * LOCKING:
3034 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003035 */
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3038 unsigned int buflen, int do_write)
3039{
3040 if (ap->flags & ATA_FLAG_MMIO)
3041 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3042 else
3043 ata_pio_data_xfer(ap, buf, buflen, do_write);
3044}
3045
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003046/**
3047 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3048 * @qc: Command on going
3049 *
3050 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3051 *
3052 * LOCKING:
3053 * Inherited from caller.
3054 */
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056static void ata_pio_sector(struct ata_queued_cmd *qc)
3057{
3058 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003059 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 struct ata_port *ap = qc->ap;
3061 struct page *page;
3062 unsigned int offset;
3063 unsigned char *buf;
3064
3065 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003066 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 page = sg[qc->cursg].page;
3069 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3070
3071 /* get the current page and offset */
3072 page = nth_page(page, (offset >> PAGE_SHIFT));
3073 offset %= PAGE_SIZE;
3074
3075 buf = kmap(page) + offset;
3076
3077 qc->cursect++;
3078 qc->cursg_ofs++;
3079
Albert Lee32529e02005-05-26 03:49:42 -04003080 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 qc->cursg++;
3082 qc->cursg_ofs = 0;
3083 }
3084
3085 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3086
3087 /* do the actual data transfer */
3088 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3089 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3090
3091 kunmap(page);
3092}
3093
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003094/**
3095 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3096 * @qc: Command on going
3097 * @bytes: number of bytes
3098 *
3099 * Transfer Transfer data from/to the ATAPI device.
3100 *
3101 * LOCKING:
3102 * Inherited from caller.
3103 *
3104 */
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3107{
3108 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003109 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 struct ata_port *ap = qc->ap;
3111 struct page *page;
3112 unsigned char *buf;
3113 unsigned int offset, count;
3114
Albert Lee563a6e12005-08-12 14:17:50 +08003115 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003116 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003119 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003120 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003121 * The end of qc->sg is reached and the device expects
3122 * more data to transfer. In order not to overrun qc->sg
3123 * and fulfill length specified in the byte count register,
3124 * - for read case, discard trailing data from the device
3125 * - for write case, padding zero data to the device
3126 */
3127 u16 pad_buf[1] = { 0 };
3128 unsigned int words = bytes >> 1;
3129 unsigned int i;
3130
3131 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003132 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003133 ap->id, bytes);
3134
3135 for (i = 0; i < words; i++)
3136 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3137
Albert Lee14be71f2005-09-27 17:36:35 +08003138 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003139 return;
3140 }
3141
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003142 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 page = sg->page;
3145 offset = sg->offset + qc->cursg_ofs;
3146
3147 /* get the current page and offset */
3148 page = nth_page(page, (offset >> PAGE_SHIFT));
3149 offset %= PAGE_SIZE;
3150
Albert Lee6952df02005-06-06 15:56:03 +08003151 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003152 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 /* don't cross page boundaries */
3155 count = min(count, (unsigned int)PAGE_SIZE - offset);
3156
3157 buf = kmap(page) + offset;
3158
3159 bytes -= count;
3160 qc->curbytes += count;
3161 qc->cursg_ofs += count;
3162
Albert Lee32529e02005-05-26 03:49:42 -04003163 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 qc->cursg++;
3165 qc->cursg_ofs = 0;
3166 }
3167
3168 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3169
3170 /* do the actual data transfer */
3171 ata_data_xfer(ap, buf, count, do_write);
3172
3173 kunmap(page);
3174
Albert Lee563a6e12005-08-12 14:17:50 +08003175 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
3178
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003179/**
3180 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3181 * @qc: Command on going
3182 *
3183 * Transfer Transfer data from/to the ATAPI device.
3184 *
3185 * LOCKING:
3186 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003187 */
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3190{
3191 struct ata_port *ap = qc->ap;
3192 struct ata_device *dev = qc->dev;
3193 unsigned int ireason, bc_lo, bc_hi, bytes;
3194 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3195
3196 ap->ops->tf_read(ap, &qc->tf);
3197 ireason = qc->tf.nsect;
3198 bc_lo = qc->tf.lbam;
3199 bc_hi = qc->tf.lbah;
3200 bytes = (bc_hi << 8) | bc_lo;
3201
3202 /* shall be cleared to zero, indicating xfer of data */
3203 if (ireason & (1 << 0))
3204 goto err_out;
3205
3206 /* make sure transfer direction matches expected */
3207 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3208 if (do_write != i_write)
3209 goto err_out;
3210
3211 __atapi_pio_bytes(qc, bytes);
3212
3213 return;
3214
3215err_out:
3216 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3217 ap->id, dev->devno);
Albert Lee1c848982005-12-05 15:40:15 +08003218 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003219 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
3222/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003223 * ata_pio_block - start PIO on a block
3224 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 *
3226 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003227 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 */
3229
3230static void ata_pio_block(struct ata_port *ap)
3231{
3232 struct ata_queued_cmd *qc;
3233 u8 status;
3234
3235 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003236 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 * Sometimes when we enter, BSY will be cleared in
3238 * a chk-status or two. If not, the drive is probably seeking
3239 * or something. Snooze for a couple msecs, then
3240 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003241 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 */
3243 status = ata_busy_wait(ap, ATA_BUSY, 5);
3244 if (status & ATA_BUSY) {
3245 msleep(2);
3246 status = ata_busy_wait(ap, ATA_BUSY, 10);
3247 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003248 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3250 return;
3251 }
3252 }
3253
3254 qc = ata_qc_from_tag(ap, ap->active_tag);
3255 assert(qc != NULL);
3256
Albert Leefe79e682005-12-06 11:34:59 +08003257 /* check error */
3258 if (status & (ATA_ERR | ATA_DF)) {
3259 qc->err_mask |= AC_ERR_DEV;
3260 ap->hsm_task_state = HSM_ST_ERR;
3261 return;
3262 }
3263
3264 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003266 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003268 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return;
3270 }
3271
3272 atapi_pio_bytes(qc);
3273 } else {
3274 /* handle BSY=0, DRQ=0 as error */
3275 if ((status & ATA_DRQ) == 0) {
Albert Lee1c848982005-12-05 15:40:15 +08003276 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003277 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return;
3279 }
3280
3281 ata_pio_sector(qc);
3282 }
3283}
3284
3285static void ata_pio_error(struct ata_port *ap)
3286{
3287 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003288
3289 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
3291 qc = ata_qc_from_tag(ap, ap->active_tag);
3292 assert(qc != NULL);
3293
Albert Lee1c848982005-12-05 15:40:15 +08003294 /* make sure qc->err_mask is available to
3295 * know what's wrong and recover
3296 */
3297 assert(qc->err_mask);
3298
Albert Lee14be71f2005-09-27 17:36:35 +08003299 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Albert Leea22e2eb2005-12-05 15:38:02 +08003301 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302}
3303
3304static void ata_pio_task(void *_data)
3305{
3306 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003307 unsigned long timeout;
3308 int qc_completed;
3309
3310fsm_start:
3311 timeout = 0;
3312 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Albert Lee14be71f2005-09-27 17:36:35 +08003314 switch (ap->hsm_task_state) {
3315 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 return;
3317
Albert Lee14be71f2005-09-27 17:36:35 +08003318 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 ata_pio_block(ap);
3320 break;
3321
Albert Lee14be71f2005-09-27 17:36:35 +08003322 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003323 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 break;
3325
Albert Lee14be71f2005-09-27 17:36:35 +08003326 case HSM_ST_POLL:
3327 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 timeout = ata_pio_poll(ap);
3329 break;
3330
Albert Lee14be71f2005-09-27 17:36:35 +08003331 case HSM_ST_TMOUT:
3332 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 ata_pio_error(ap);
3334 return;
3335 }
3336
3337 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003338 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3339 else if (!qc_completed)
3340 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343/**
3344 * ata_qc_timeout - Handle timeout of queued command
3345 * @qc: Command that timed out
3346 *
3347 * Some part of the kernel (currently, only the SCSI layer)
3348 * has noticed that the active command on port @ap has not
3349 * completed after a specified length of time. Handle this
3350 * condition by disabling DMA (if necessary) and completing
3351 * transactions, with error if necessary.
3352 *
3353 * This also handles the case of the "lost interrupt", where
3354 * for some reason (possibly hardware bug, possibly driver bug)
3355 * an interrupt was not delivered to the driver, even though the
3356 * transaction completed successfully.
3357 *
3358 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003359 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 */
3361
3362static void ata_qc_timeout(struct ata_queued_cmd *qc)
3363{
3364 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003365 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003367 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
3369 DPRINTK("ENTER\n");
3370
Jeff Garzikb8f61532005-08-25 22:01:20 -04003371 spin_lock_irqsave(&host_set->lock, flags);
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 /* hack alert! We cannot use the supplied completion
3374 * function from inside the ->eh_strategy_handler() thread.
3375 * libata is the only user of ->eh_strategy_handler() in
3376 * any kernel, so the default scsi_done() assumes it is
3377 * not being called from the SCSI EH.
3378 */
3379 qc->scsidone = scsi_finish_command;
3380
3381 switch (qc->tf.protocol) {
3382
3383 case ATA_PROT_DMA:
3384 case ATA_PROT_ATAPI_DMA:
3385 host_stat = ap->ops->bmdma_status(ap);
3386
3387 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003388 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390 /* fall through */
3391
3392 default:
3393 ata_altstatus(ap);
3394 drv_stat = ata_chk_status(ap);
3395
3396 /* ack bmdma irq events */
3397 ap->ops->irq_clear(ap);
3398
3399 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3400 ap->id, qc->tf.command, drv_stat, host_stat);
3401
3402 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003403 qc->err_mask |= ac_err_mask(drv_stat);
3404 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 break;
3406 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003407
3408 spin_unlock_irqrestore(&host_set->lock, flags);
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 DPRINTK("EXIT\n");
3411}
3412
3413/**
3414 * ata_eng_timeout - Handle timeout of queued command
3415 * @ap: Port on which timed-out command is active
3416 *
3417 * Some part of the kernel (currently, only the SCSI layer)
3418 * has noticed that the active command on port @ap has not
3419 * completed after a specified length of time. Handle this
3420 * condition by disabling DMA (if necessary) and completing
3421 * transactions, with error if necessary.
3422 *
3423 * This also handles the case of the "lost interrupt", where
3424 * for some reason (possibly hardware bug, possibly driver bug)
3425 * an interrupt was not delivered to the driver, even though the
3426 * transaction completed successfully.
3427 *
3428 * LOCKING:
3429 * Inherited from SCSI layer (none, can sleep)
3430 */
3431
3432void ata_eng_timeout(struct ata_port *ap)
3433{
3434 struct ata_queued_cmd *qc;
3435
3436 DPRINTK("ENTER\n");
3437
3438 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003439 if (qc)
3440 ata_qc_timeout(qc);
3441 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3443 ap->id);
3444 goto out;
3445 }
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447out:
3448 DPRINTK("EXIT\n");
3449}
3450
3451/**
3452 * ata_qc_new - Request an available ATA command, for queueing
3453 * @ap: Port associated with device @dev
3454 * @dev: Device from whom we request an available command structure
3455 *
3456 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003457 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 */
3459
3460static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3461{
3462 struct ata_queued_cmd *qc = NULL;
3463 unsigned int i;
3464
3465 for (i = 0; i < ATA_MAX_QUEUE; i++)
3466 if (!test_and_set_bit(i, &ap->qactive)) {
3467 qc = ata_qc_from_tag(ap, i);
3468 break;
3469 }
3470
3471 if (qc)
3472 qc->tag = i;
3473
3474 return qc;
3475}
3476
3477/**
3478 * ata_qc_new_init - Request an available ATA command, and initialize it
3479 * @ap: Port associated with device @dev
3480 * @dev: Device from whom we request an available command structure
3481 *
3482 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003483 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 */
3485
3486struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3487 struct ata_device *dev)
3488{
3489 struct ata_queued_cmd *qc;
3490
3491 qc = ata_qc_new(ap);
3492 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 qc->scsicmd = NULL;
3494 qc->ap = ap;
3495 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003497 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 }
3499
3500 return qc;
3501}
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503static void __ata_qc_complete(struct ata_queued_cmd *qc)
3504{
3505 struct ata_port *ap = qc->ap;
Tejun Heob5632302005-12-13 14:51:25 +09003506 unsigned int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
3508 qc->flags = 0;
3509 tag = qc->tag;
3510 if (likely(ata_tag_valid(tag))) {
3511 if (tag == ap->active_tag)
3512 ap->active_tag = ATA_TAG_POISON;
3513 qc->tag = ATA_TAG_POISON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 clear_bit(tag, &ap->qactive);
Tejun Heob5632302005-12-13 14:51:25 +09003515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516}
3517
3518/**
3519 * ata_qc_free - free unused ata_queued_cmd
3520 * @qc: Command to complete
3521 *
3522 * Designed to free unused ata_queued_cmd object
3523 * in case something prevents using it.
3524 *
3525 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003526 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 */
3528void ata_qc_free(struct ata_queued_cmd *qc)
3529{
3530 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 __ata_qc_complete(qc);
3533}
3534
3535/**
3536 * ata_qc_complete - Complete an active ATA command
3537 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003538 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003539 *
3540 * Indicate to the mid and upper layers that an ATA
3541 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 *
3543 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003544 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 */
3546
Albert Leea22e2eb2005-12-05 15:38:02 +08003547void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
3549 int rc;
3550
3551 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3552 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3553
3554 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3555 ata_sg_clean(qc);
3556
Albert Lee3f3791d2005-08-16 14:25:38 +08003557 /* atapi: mark qc as inactive to prevent the interrupt handler
3558 * from completing the command twice later, before the error handler
3559 * is called. (when rc != 0 and atapi request sense is needed)
3560 */
3561 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 /* call completion callback */
Albert Leea22e2eb2005-12-05 15:38:02 +08003564 rc = qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566 /* if callback indicates not to complete command (non-zero),
3567 * return immediately
3568 */
3569 if (rc != 0)
3570 return;
3571
3572 __ata_qc_complete(qc);
3573
3574 VPRINTK("EXIT\n");
3575}
3576
3577static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3578{
3579 struct ata_port *ap = qc->ap;
3580
3581 switch (qc->tf.protocol) {
3582 case ATA_PROT_DMA:
3583 case ATA_PROT_ATAPI_DMA:
3584 return 1;
3585
3586 case ATA_PROT_ATAPI:
3587 case ATA_PROT_PIO:
3588 case ATA_PROT_PIO_MULT:
3589 if (ap->flags & ATA_FLAG_PIO_DMA)
3590 return 1;
3591
3592 /* fall through */
3593
3594 default:
3595 return 0;
3596 }
3597
3598 /* never reached */
3599}
3600
3601/**
3602 * ata_qc_issue - issue taskfile to device
3603 * @qc: command to issue to device
3604 *
3605 * Prepare an ATA command to submission to device.
3606 * This includes mapping the data into a DMA-able
3607 * area, filling in the S/G table, and finally
3608 * writing the taskfile to hardware, starting the command.
3609 *
3610 * LOCKING:
3611 * spin_lock_irqsave(host_set lock)
3612 *
3613 * RETURNS:
3614 * Zero on success, negative on error.
3615 */
3616
3617int ata_qc_issue(struct ata_queued_cmd *qc)
3618{
3619 struct ata_port *ap = qc->ap;
3620
3621 if (ata_should_dma_map(qc)) {
3622 if (qc->flags & ATA_QCFLAG_SG) {
3623 if (ata_sg_setup(qc))
3624 goto err_out;
3625 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3626 if (ata_sg_setup_one(qc))
3627 goto err_out;
3628 }
3629 } else {
3630 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3631 }
3632
3633 ap->ops->qc_prep(qc);
3634
3635 qc->ap->active_tag = qc->tag;
3636 qc->flags |= ATA_QCFLAG_ACTIVE;
3637
3638 return ap->ops->qc_issue(qc);
3639
3640err_out:
3641 return -1;
3642}
3643
Edward Falk0baab862005-06-02 18:17:13 -04003644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645/**
3646 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3647 * @qc: command to issue to device
3648 *
3649 * Using various libata functions and hooks, this function
3650 * starts an ATA command. ATA commands are grouped into
3651 * classes called "protocols", and issuing each type of protocol
3652 * is slightly different.
3653 *
Edward Falk0baab862005-06-02 18:17:13 -04003654 * May be used as the qc_issue() entry in ata_port_operations.
3655 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 * LOCKING:
3657 * spin_lock_irqsave(host_set lock)
3658 *
3659 * RETURNS:
3660 * Zero on success, negative on error.
3661 */
3662
3663int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3664{
3665 struct ata_port *ap = qc->ap;
3666
3667 ata_dev_select(ap, qc->dev->devno, 1, 0);
3668
3669 switch (qc->tf.protocol) {
3670 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003671 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 break;
3673
3674 case ATA_PROT_DMA:
3675 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3676 ap->ops->bmdma_setup(qc); /* set up bmdma */
3677 ap->ops->bmdma_start(qc); /* initiate bmdma */
3678 break;
3679
3680 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3681 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003682 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003683 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 queue_work(ata_wq, &ap->pio_task);
3685 break;
3686
3687 case ATA_PROT_ATAPI:
3688 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003689 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 queue_work(ata_wq, &ap->packet_task);
3691 break;
3692
3693 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003694 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003695 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 queue_work(ata_wq, &ap->packet_task);
3697 break;
3698
3699 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003700 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3702 ap->ops->bmdma_setup(qc); /* set up bmdma */
3703 queue_work(ata_wq, &ap->packet_task);
3704 break;
3705
3706 default:
3707 WARN_ON(1);
3708 return -1;
3709 }
3710
3711 return 0;
3712}
3713
3714/**
Edward Falk0baab862005-06-02 18:17:13 -04003715 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 * @qc: Info associated with this ATA transaction.
3717 *
3718 * LOCKING:
3719 * spin_lock_irqsave(host_set lock)
3720 */
3721
3722static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3723{
3724 struct ata_port *ap = qc->ap;
3725 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3726 u8 dmactl;
3727 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3728
3729 /* load PRD table addr. */
3730 mb(); /* make sure PRD table writes are visible to controller */
3731 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3732
3733 /* specify data direction, triple-check start bit is clear */
3734 dmactl = readb(mmio + ATA_DMA_CMD);
3735 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3736 if (!rw)
3737 dmactl |= ATA_DMA_WR;
3738 writeb(dmactl, mmio + ATA_DMA_CMD);
3739
3740 /* issue r/w command */
3741 ap->ops->exec_command(ap, &qc->tf);
3742}
3743
3744/**
Alan Coxb73fc892005-08-26 16:03:19 +01003745 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 * @qc: Info associated with this ATA transaction.
3747 *
3748 * LOCKING:
3749 * spin_lock_irqsave(host_set lock)
3750 */
3751
3752static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3753{
3754 struct ata_port *ap = qc->ap;
3755 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3756 u8 dmactl;
3757
3758 /* start host DMA transaction */
3759 dmactl = readb(mmio + ATA_DMA_CMD);
3760 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3761
3762 /* Strictly, one may wish to issue a readb() here, to
3763 * flush the mmio write. However, control also passes
3764 * to the hardware at this point, and it will interrupt
3765 * us when we are to resume control. So, in effect,
3766 * we don't care when the mmio write flushes.
3767 * Further, a read of the DMA status register _immediately_
3768 * following the write may not be what certain flaky hardware
3769 * is expected, so I think it is best to not add a readb()
3770 * without first all the MMIO ATA cards/mobos.
3771 * Or maybe I'm just being paranoid.
3772 */
3773}
3774
3775/**
3776 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3777 * @qc: Info associated with this ATA transaction.
3778 *
3779 * LOCKING:
3780 * spin_lock_irqsave(host_set lock)
3781 */
3782
3783static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3784{
3785 struct ata_port *ap = qc->ap;
3786 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3787 u8 dmactl;
3788
3789 /* load PRD table addr. */
3790 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3791
3792 /* specify data direction, triple-check start bit is clear */
3793 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3794 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3795 if (!rw)
3796 dmactl |= ATA_DMA_WR;
3797 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3798
3799 /* issue r/w command */
3800 ap->ops->exec_command(ap, &qc->tf);
3801}
3802
3803/**
3804 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3805 * @qc: Info associated with this ATA transaction.
3806 *
3807 * LOCKING:
3808 * spin_lock_irqsave(host_set lock)
3809 */
3810
3811static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3812{
3813 struct ata_port *ap = qc->ap;
3814 u8 dmactl;
3815
3816 /* start host DMA transaction */
3817 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3818 outb(dmactl | ATA_DMA_START,
3819 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3820}
3821
Edward Falk0baab862005-06-02 18:17:13 -04003822
3823/**
3824 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3825 * @qc: Info associated with this ATA transaction.
3826 *
3827 * Writes the ATA_DMA_START flag to the DMA command register.
3828 *
3829 * May be used as the bmdma_start() entry in ata_port_operations.
3830 *
3831 * LOCKING:
3832 * spin_lock_irqsave(host_set lock)
3833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834void ata_bmdma_start(struct ata_queued_cmd *qc)
3835{
3836 if (qc->ap->flags & ATA_FLAG_MMIO)
3837 ata_bmdma_start_mmio(qc);
3838 else
3839 ata_bmdma_start_pio(qc);
3840}
3841
Edward Falk0baab862005-06-02 18:17:13 -04003842
3843/**
3844 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3845 * @qc: Info associated with this ATA transaction.
3846 *
3847 * Writes address of PRD table to device's PRD Table Address
3848 * register, sets the DMA control register, and calls
3849 * ops->exec_command() to start the transfer.
3850 *
3851 * May be used as the bmdma_setup() entry in ata_port_operations.
3852 *
3853 * LOCKING:
3854 * spin_lock_irqsave(host_set lock)
3855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856void ata_bmdma_setup(struct ata_queued_cmd *qc)
3857{
3858 if (qc->ap->flags & ATA_FLAG_MMIO)
3859 ata_bmdma_setup_mmio(qc);
3860 else
3861 ata_bmdma_setup_pio(qc);
3862}
3863
Edward Falk0baab862005-06-02 18:17:13 -04003864
3865/**
3866 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003867 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003868 *
3869 * Clear interrupt and error flags in DMA status register.
3870 *
3871 * May be used as the irq_clear() entry in ata_port_operations.
3872 *
3873 * LOCKING:
3874 * spin_lock_irqsave(host_set lock)
3875 */
3876
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877void ata_bmdma_irq_clear(struct ata_port *ap)
3878{
3879 if (ap->flags & ATA_FLAG_MMIO) {
3880 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3881 writeb(readb(mmio), mmio);
3882 } else {
3883 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3884 outb(inb(addr), addr);
3885 }
3886
3887}
3888
Edward Falk0baab862005-06-02 18:17:13 -04003889
3890/**
3891 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003892 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003893 *
3894 * Read and return BMDMA status register.
3895 *
3896 * May be used as the bmdma_status() entry in ata_port_operations.
3897 *
3898 * LOCKING:
3899 * spin_lock_irqsave(host_set lock)
3900 */
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902u8 ata_bmdma_status(struct ata_port *ap)
3903{
3904 u8 host_stat;
3905 if (ap->flags & ATA_FLAG_MMIO) {
3906 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3907 host_stat = readb(mmio + ATA_DMA_STATUS);
3908 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003909 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return host_stat;
3911}
3912
Edward Falk0baab862005-06-02 18:17:13 -04003913
3914/**
3915 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003916 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003917 *
3918 * Clears the ATA_DMA_START flag in the dma control register
3919 *
3920 * May be used as the bmdma_stop() entry in ata_port_operations.
3921 *
3922 * LOCKING:
3923 * spin_lock_irqsave(host_set lock)
3924 */
3925
Alan Coxb73fc892005-08-26 16:03:19 +01003926void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
Alan Coxb73fc892005-08-26 16:03:19 +01003928 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 if (ap->flags & ATA_FLAG_MMIO) {
3930 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3931
3932 /* clear start/stop bit */
3933 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3934 mmio + ATA_DMA_CMD);
3935 } else {
3936 /* clear start/stop bit */
3937 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3938 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3939 }
3940
3941 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3942 ata_altstatus(ap); /* dummy read */
3943}
3944
3945/**
3946 * ata_host_intr - Handle host interrupt for given (port, task)
3947 * @ap: Port on which interrupt arrived (possibly...)
3948 * @qc: Taskfile currently active in engine
3949 *
3950 * Handle host interrupt for given queued command. Currently,
3951 * only DMA interrupts are handled. All other commands are
3952 * handled via polling with interrupts disabled (nIEN bit).
3953 *
3954 * LOCKING:
3955 * spin_lock_irqsave(host_set lock)
3956 *
3957 * RETURNS:
3958 * One if interrupt was handled, zero if not (shared irq).
3959 */
3960
3961inline unsigned int ata_host_intr (struct ata_port *ap,
3962 struct ata_queued_cmd *qc)
3963{
3964 u8 status, host_stat;
3965
3966 switch (qc->tf.protocol) {
3967
3968 case ATA_PROT_DMA:
3969 case ATA_PROT_ATAPI_DMA:
3970 case ATA_PROT_ATAPI:
3971 /* check status of DMA engine */
3972 host_stat = ap->ops->bmdma_status(ap);
3973 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3974
3975 /* if it's not our irq... */
3976 if (!(host_stat & ATA_DMA_INTR))
3977 goto idle_irq;
3978
3979 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003980 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 /* fall through */
3983
3984 case ATA_PROT_ATAPI_NODATA:
3985 case ATA_PROT_NODATA:
3986 /* check altstatus */
3987 status = ata_altstatus(ap);
3988 if (status & ATA_BUSY)
3989 goto idle_irq;
3990
3991 /* check main status, clearing INTRQ */
3992 status = ata_chk_status(ap);
3993 if (unlikely(status & ATA_BUSY))
3994 goto idle_irq;
3995 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3996 ap->id, qc->tf.protocol, status);
3997
3998 /* ack bmdma irq events */
3999 ap->ops->irq_clear(ap);
4000
4001 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004002 qc->err_mask |= ac_err_mask(status);
4003 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 break;
4005
4006 default:
4007 goto idle_irq;
4008 }
4009
4010 return 1; /* irq handled */
4011
4012idle_irq:
4013 ap->stats.idle_irq++;
4014
4015#ifdef ATA_IRQ_TRAP
4016 if ((ap->stats.idle_irq % 1000) == 0) {
4017 handled = 1;
4018 ata_irq_ack(ap, 0); /* debug trap */
4019 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4020 }
4021#endif
4022 return 0; /* irq not handled */
4023}
4024
4025/**
4026 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004027 * @irq: irq line (unused)
4028 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 * @regs: unused
4030 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004031 * Default interrupt handler for PCI IDE devices. Calls
4032 * ata_host_intr() for each port that is not disabled.
4033 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004035 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 *
4037 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004038 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 */
4040
4041irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4042{
4043 struct ata_host_set *host_set = dev_instance;
4044 unsigned int i;
4045 unsigned int handled = 0;
4046 unsigned long flags;
4047
4048 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4049 spin_lock_irqsave(&host_set->lock, flags);
4050
4051 for (i = 0; i < host_set->n_ports; i++) {
4052 struct ata_port *ap;
4053
4054 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004055 if (ap &&
4056 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 struct ata_queued_cmd *qc;
4058
4059 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004060 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4061 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 handled |= ata_host_intr(ap, qc);
4063 }
4064 }
4065
4066 spin_unlock_irqrestore(&host_set->lock, flags);
4067
4068 return IRQ_RETVAL(handled);
4069}
4070
4071/**
4072 * atapi_packet_task - Write CDB bytes to hardware
4073 * @_data: Port to which ATAPI device is attached.
4074 *
4075 * When device has indicated its readiness to accept
4076 * a CDB, this function is called. Send the CDB.
4077 * If DMA is to be performed, exit immediately.
4078 * Otherwise, we are in polling mode, so poll
4079 * status under operation succeeds or fails.
4080 *
4081 * LOCKING:
4082 * Kernel thread context (may sleep)
4083 */
4084
4085static void atapi_packet_task(void *_data)
4086{
4087 struct ata_port *ap = _data;
4088 struct ata_queued_cmd *qc;
4089 u8 status;
4090
4091 qc = ata_qc_from_tag(ap, ap->active_tag);
4092 assert(qc != NULL);
4093 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4094
4095 /* sleep-wait for BSY to clear */
4096 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004097 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4098 qc->err_mask |= AC_ERR_ATA_BUS;
4099 goto err_out;
4100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
4102 /* make sure DRQ is set */
4103 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004104 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4105 qc->err_mask |= AC_ERR_ATA_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
4109 /* send SCSI cdb */
4110 DPRINTK("send cdb\n");
4111 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Tejun Heoc1389502005-08-22 14:59:24 +09004113 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4114 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4115 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Tejun Heoc1389502005-08-22 14:59:24 +09004117 /* Once we're done issuing command and kicking bmdma,
4118 * irq handler takes over. To not lose irq, we need
4119 * to clear NOINTR flag before sending cdb, but
4120 * interrupt handler shouldn't be invoked before we're
4121 * finished. Hence, the following locking.
4122 */
4123 spin_lock_irqsave(&ap->host_set->lock, flags);
4124 ap->flags &= ~ATA_FLAG_NOINTR;
4125 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4126 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4127 ap->ops->bmdma_start(qc); /* initiate bmdma */
4128 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4129 } else {
4130 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Tejun Heoc1389502005-08-22 14:59:24 +09004132 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004133 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 queue_work(ata_wq, &ap->pio_task);
4135 }
4136
4137 return;
4138
4139err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004140 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
Edward Falk0baab862005-06-02 18:17:13 -04004143
4144/**
4145 * ata_port_start - Set port up for dma.
4146 * @ap: Port to initialize
4147 *
4148 * Called just after data structures for each port are
4149 * initialized. Allocates space for PRD table.
4150 *
4151 * May be used as the port_start() entry in ata_port_operations.
4152 *
4153 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004154 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004155 */
4156
Jens Axboe9b847542006-01-06 09:28:07 +01004157/*
4158 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4159 * without filling any other registers
4160 */
4161static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4162 u8 cmd)
4163{
4164 struct ata_taskfile tf;
4165 int err;
4166
4167 ata_tf_init(ap, &tf, dev->devno);
4168
4169 tf.command = cmd;
4170 tf.flags |= ATA_TFLAG_DEVICE;
4171 tf.protocol = ATA_PROT_NODATA;
4172
4173 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4174 if (err)
4175 printk(KERN_ERR "%s: ata command failed: %d\n",
4176 __FUNCTION__, err);
4177
4178 return err;
4179}
4180
4181static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4182{
4183 u8 cmd;
4184
4185 if (!ata_try_flush_cache(dev))
4186 return 0;
4187
4188 if (ata_id_has_flush_ext(dev->id))
4189 cmd = ATA_CMD_FLUSH_EXT;
4190 else
4191 cmd = ATA_CMD_FLUSH;
4192
4193 return ata_do_simple_cmd(ap, dev, cmd);
4194}
4195
4196static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4197{
4198 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4199}
4200
4201static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4202{
4203 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4204}
4205
4206/**
4207 * ata_device_resume - wakeup a previously suspended devices
4208 *
4209 * Kick the drive back into action, by sending it an idle immediate
4210 * command and making sure its transfer mode matches between drive
4211 * and host.
4212 *
4213 */
4214int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4215{
4216 if (ap->flags & ATA_FLAG_SUSPENDED) {
4217 ap->flags &= ~ATA_FLAG_SUSPENDED;
4218 ata_set_mode(ap);
4219 }
4220 if (!ata_dev_present(dev))
4221 return 0;
4222 if (dev->class == ATA_DEV_ATA)
4223 ata_start_drive(ap, dev);
4224
4225 return 0;
4226}
4227
4228/**
4229 * ata_device_suspend - prepare a device for suspend
4230 *
4231 * Flush the cache on the drive, if appropriate, then issue a
4232 * standbynow command.
4233 *
4234 */
4235int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4236{
4237 if (!ata_dev_present(dev))
4238 return 0;
4239 if (dev->class == ATA_DEV_ATA)
4240 ata_flush_cache(ap, dev);
4241
4242 ata_standby_drive(ap, dev);
4243 ap->flags |= ATA_FLAG_SUSPENDED;
4244 return 0;
4245}
4246
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247int ata_port_start (struct ata_port *ap)
4248{
4249 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004250 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
4252 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4253 if (!ap->prd)
4254 return -ENOMEM;
4255
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004256 rc = ata_pad_alloc(ap, dev);
4257 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004258 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004259 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004260 }
4261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4263
4264 return 0;
4265}
4266
Edward Falk0baab862005-06-02 18:17:13 -04004267
4268/**
4269 * ata_port_stop - Undo ata_port_start()
4270 * @ap: Port to shut down
4271 *
4272 * Frees the PRD table.
4273 *
4274 * May be used as the port_stop() entry in ata_port_operations.
4275 *
4276 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004277 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004278 */
4279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280void ata_port_stop (struct ata_port *ap)
4281{
4282 struct device *dev = ap->host_set->dev;
4283
4284 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004285 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286}
4287
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004288void ata_host_stop (struct ata_host_set *host_set)
4289{
4290 if (host_set->mmio_base)
4291 iounmap(host_set->mmio_base);
4292}
4293
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295/**
4296 * ata_host_remove - Unregister SCSI host structure with upper layers
4297 * @ap: Port to unregister
4298 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4299 *
4300 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004301 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 */
4303
4304static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4305{
4306 struct Scsi_Host *sh = ap->host;
4307
4308 DPRINTK("ENTER\n");
4309
4310 if (do_unregister)
4311 scsi_remove_host(sh);
4312
4313 ap->ops->port_stop(ap);
4314}
4315
4316/**
4317 * ata_host_init - Initialize an ata_port structure
4318 * @ap: Structure to initialize
4319 * @host: associated SCSI mid-layer structure
4320 * @host_set: Collection of hosts to which @ap belongs
4321 * @ent: Probe information provided by low-level driver
4322 * @port_no: Port number associated with this ata_port
4323 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004324 * Initialize a new ata_port structure, and its associated
4325 * scsi_host.
4326 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004328 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 */
4330
4331static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4332 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004333 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334{
4335 unsigned int i;
4336
4337 host->max_id = 16;
4338 host->max_lun = 1;
4339 host->max_channel = 1;
4340 host->unique_id = ata_unique_id++;
4341 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 ap->flags = ATA_FLAG_PORT_DISABLED;
4344 ap->id = host->unique_id;
4345 ap->host = host;
4346 ap->ctl = ATA_DEVCTL_OBS;
4347 ap->host_set = host_set;
4348 ap->port_no = port_no;
4349 ap->hard_port_no =
4350 ent->legacy_mode ? ent->hard_port_no : port_no;
4351 ap->pio_mask = ent->pio_mask;
4352 ap->mwdma_mask = ent->mwdma_mask;
4353 ap->udma_mask = ent->udma_mask;
4354 ap->flags |= ent->host_flags;
4355 ap->ops = ent->port_ops;
4356 ap->cbl = ATA_CBL_NONE;
4357 ap->active_tag = ATA_TAG_POISON;
4358 ap->last_ctl = 0xFF;
4359
4360 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4361 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4362
4363 for (i = 0; i < ATA_MAX_DEVICES; i++)
4364 ap->device[i].devno = i;
4365
4366#ifdef ATA_IRQ_TRAP
4367 ap->stats.unhandled_irq = 1;
4368 ap->stats.idle_irq = 1;
4369#endif
4370
4371 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4372}
4373
4374/**
4375 * ata_host_add - Attach low-level ATA driver to system
4376 * @ent: Information provided by low-level driver
4377 * @host_set: Collections of ports to which we add
4378 * @port_no: Port number associated with this host
4379 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004380 * Attach low-level ATA driver to system.
4381 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004383 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 *
4385 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004386 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 */
4388
Jeff Garzik057ace52005-10-22 14:27:05 -04004389static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 struct ata_host_set *host_set,
4391 unsigned int port_no)
4392{
4393 struct Scsi_Host *host;
4394 struct ata_port *ap;
4395 int rc;
4396
4397 DPRINTK("ENTER\n");
4398 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4399 if (!host)
4400 return NULL;
4401
4402 ap = (struct ata_port *) &host->hostdata[0];
4403
4404 ata_host_init(ap, host, host_set, ent, port_no);
4405
4406 rc = ap->ops->port_start(ap);
4407 if (rc)
4408 goto err_out;
4409
4410 return ap;
4411
4412err_out:
4413 scsi_host_put(host);
4414 return NULL;
4415}
4416
4417/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004418 * ata_device_add - Register hardware device with ATA and SCSI layers
4419 * @ent: Probe information describing hardware device to be registered
4420 *
4421 * This function processes the information provided in the probe
4422 * information struct @ent, allocates the necessary ATA and SCSI
4423 * host information structures, initializes them, and registers
4424 * everything with requisite kernel subsystems.
4425 *
4426 * This function requests irqs, probes the ATA bus, and probes
4427 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 *
4429 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004430 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 *
4432 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004433 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 */
4435
Jeff Garzik057ace52005-10-22 14:27:05 -04004436int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
4438 unsigned int count = 0, i;
4439 struct device *dev = ent->dev;
4440 struct ata_host_set *host_set;
4441
4442 DPRINTK("ENTER\n");
4443 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004444 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4446 if (!host_set)
4447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 spin_lock_init(&host_set->lock);
4449
4450 host_set->dev = dev;
4451 host_set->n_ports = ent->n_ports;
4452 host_set->irq = ent->irq;
4453 host_set->mmio_base = ent->mmio_base;
4454 host_set->private_data = ent->private_data;
4455 host_set->ops = ent->port_ops;
4456
4457 /* register each port bound to this device */
4458 for (i = 0; i < ent->n_ports; i++) {
4459 struct ata_port *ap;
4460 unsigned long xfer_mode_mask;
4461
4462 ap = ata_host_add(ent, host_set, i);
4463 if (!ap)
4464 goto err_out;
4465
4466 host_set->ports[i] = ap;
4467 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4468 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4469 (ap->pio_mask << ATA_SHIFT_PIO);
4470
4471 /* print per-port info to dmesg */
4472 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4473 "bmdma 0x%lX irq %lu\n",
4474 ap->id,
4475 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4476 ata_mode_string(xfer_mode_mask),
4477 ap->ioaddr.cmd_addr,
4478 ap->ioaddr.ctl_addr,
4479 ap->ioaddr.bmdma_addr,
4480 ent->irq);
4481
4482 ata_chk_status(ap);
4483 host_set->ops->irq_clear(ap);
4484 count++;
4485 }
4486
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004487 if (!count)
4488 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
4490 /* obtain irq, that is shared between channels */
4491 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4492 DRV_NAME, host_set))
4493 goto err_out;
4494
4495 /* perform each probe synchronously */
4496 DPRINTK("probe begin\n");
4497 for (i = 0; i < count; i++) {
4498 struct ata_port *ap;
4499 int rc;
4500
4501 ap = host_set->ports[i];
4502
4503 DPRINTK("ata%u: probe begin\n", ap->id);
4504 rc = ata_bus_probe(ap);
4505 DPRINTK("ata%u: probe end\n", ap->id);
4506
4507 if (rc) {
4508 /* FIXME: do something useful here?
4509 * Current libata behavior will
4510 * tear down everything when
4511 * the module is removed
4512 * or the h/w is unplugged.
4513 */
4514 }
4515
4516 rc = scsi_add_host(ap->host, dev);
4517 if (rc) {
4518 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4519 ap->id);
4520 /* FIXME: do something useful here */
4521 /* FIXME: handle unconditional calls to
4522 * scsi_scan_host and ata_host_remove, below,
4523 * at the very least
4524 */
4525 }
4526 }
4527
4528 /* probes are done, now scan each port's disk(s) */
4529 DPRINTK("probe begin\n");
4530 for (i = 0; i < count; i++) {
4531 struct ata_port *ap = host_set->ports[i];
4532
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004533 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 }
4535
4536 dev_set_drvdata(dev, host_set);
4537
4538 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4539 return ent->n_ports; /* success */
4540
4541err_out:
4542 for (i = 0; i < count; i++) {
4543 ata_host_remove(host_set->ports[i], 1);
4544 scsi_host_put(host_set->ports[i]->host);
4545 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004546err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 kfree(host_set);
4548 VPRINTK("EXIT, returning 0\n");
4549 return 0;
4550}
4551
4552/**
Alan Cox17b14452005-09-15 15:44:00 +01004553 * ata_host_set_remove - PCI layer callback for device removal
4554 * @host_set: ATA host set that was removed
4555 *
4556 * Unregister all objects associated with this host set. Free those
4557 * objects.
4558 *
4559 * LOCKING:
4560 * Inherited from calling layer (may sleep).
4561 */
4562
Alan Cox17b14452005-09-15 15:44:00 +01004563void ata_host_set_remove(struct ata_host_set *host_set)
4564{
4565 struct ata_port *ap;
4566 unsigned int i;
4567
4568 for (i = 0; i < host_set->n_ports; i++) {
4569 ap = host_set->ports[i];
4570 scsi_remove_host(ap->host);
4571 }
4572
4573 free_irq(host_set->irq, host_set);
4574
4575 for (i = 0; i < host_set->n_ports; i++) {
4576 ap = host_set->ports[i];
4577
4578 ata_scsi_release(ap->host);
4579
4580 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4581 struct ata_ioports *ioaddr = &ap->ioaddr;
4582
4583 if (ioaddr->cmd_addr == 0x1f0)
4584 release_region(0x1f0, 8);
4585 else if (ioaddr->cmd_addr == 0x170)
4586 release_region(0x170, 8);
4587 }
4588
4589 scsi_host_put(ap->host);
4590 }
4591
4592 if (host_set->ops->host_stop)
4593 host_set->ops->host_stop(host_set);
4594
4595 kfree(host_set);
4596}
4597
4598/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 * ata_scsi_release - SCSI layer callback hook for host unload
4600 * @host: libata host to be unloaded
4601 *
4602 * Performs all duties necessary to shut down a libata port...
4603 * Kill port kthread, disable port, and release resources.
4604 *
4605 * LOCKING:
4606 * Inherited from SCSI layer.
4607 *
4608 * RETURNS:
4609 * One.
4610 */
4611
4612int ata_scsi_release(struct Scsi_Host *host)
4613{
4614 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4615
4616 DPRINTK("ENTER\n");
4617
4618 ap->ops->port_disable(ap);
4619 ata_host_remove(ap, 0);
4620
4621 DPRINTK("EXIT\n");
4622 return 1;
4623}
4624
4625/**
4626 * ata_std_ports - initialize ioaddr with standard port offsets.
4627 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004628 *
4629 * Utility function which initializes data_addr, error_addr,
4630 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4631 * device_addr, status_addr, and command_addr to standard offsets
4632 * relative to cmd_addr.
4633 *
4634 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 */
Edward Falk0baab862005-06-02 18:17:13 -04004636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637void ata_std_ports(struct ata_ioports *ioaddr)
4638{
4639 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4640 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4641 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4642 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4643 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4644 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4645 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4646 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4647 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4648 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4649}
4650
4651static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004652ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
4654 struct ata_probe_ent *probe_ent;
4655
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004656 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 if (!probe_ent) {
4658 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4659 kobject_name(&(dev->kobj)));
4660 return NULL;
4661 }
4662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 INIT_LIST_HEAD(&probe_ent->node);
4664 probe_ent->dev = dev;
4665
4666 probe_ent->sht = port->sht;
4667 probe_ent->host_flags = port->host_flags;
4668 probe_ent->pio_mask = port->pio_mask;
4669 probe_ent->mwdma_mask = port->mwdma_mask;
4670 probe_ent->udma_mask = port->udma_mask;
4671 probe_ent->port_ops = port->port_ops;
4672
4673 return probe_ent;
4674}
4675
Edward Falk0baab862005-06-02 18:17:13 -04004676
4677
Jeff Garzik374b1872005-08-30 05:42:52 -04004678#ifdef CONFIG_PCI
4679
4680void ata_pci_host_stop (struct ata_host_set *host_set)
4681{
4682 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4683
4684 pci_iounmap(pdev, host_set->mmio_base);
4685}
4686
Edward Falk0baab862005-06-02 18:17:13 -04004687/**
4688 * ata_pci_init_native_mode - Initialize native-mode driver
4689 * @pdev: pci device to be initialized
4690 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004691 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004692 *
4693 * Utility function which allocates and initializes an
4694 * ata_probe_ent structure for a standard dual-port
4695 * PIO-based IDE controller. The returned ata_probe_ent
4696 * structure can be passed to ata_device_add(). The returned
4697 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004698 *
4699 * The caller need only pass the address of the primary port, the
4700 * secondary will be deduced automatically. If the device has non
4701 * standard secondary port mappings this function can be called twice,
4702 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004703 */
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004706ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
4708 struct ata_probe_ent *probe_ent =
4709 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004710 int p = 0;
4711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 if (!probe_ent)
4713 return NULL;
4714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 probe_ent->irq = pdev->irq;
4716 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004717 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Alan Cox47a86592005-10-04 08:09:19 -04004719 if (ports & ATA_PORT_PRIMARY) {
4720 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4721 probe_ent->port[p].altstatus_addr =
4722 probe_ent->port[p].ctl_addr =
4723 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4724 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4725 ata_std_ports(&probe_ent->port[p]);
4726 p++;
4727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Alan Cox47a86592005-10-04 08:09:19 -04004729 if (ports & ATA_PORT_SECONDARY) {
4730 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4731 probe_ent->port[p].altstatus_addr =
4732 probe_ent->port[p].ctl_addr =
4733 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4734 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4735 ata_std_ports(&probe_ent->port[p]);
4736 p++;
4737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
Alan Cox47a86592005-10-04 08:09:19 -04004739 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 return probe_ent;
4741}
4742
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004743static 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 -07004744{
Alan Cox47a86592005-10-04 08:09:19 -04004745 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004747 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 if (!probe_ent)
4749 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004752 probe_ent->n_ports = 1;
4753 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004754 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Alan Cox47a86592005-10-04 08:09:19 -04004756 switch(port_num)
4757 {
4758 case 0:
4759 probe_ent->irq = 14;
4760 probe_ent->port[0].cmd_addr = 0x1f0;
4761 probe_ent->port[0].altstatus_addr =
4762 probe_ent->port[0].ctl_addr = 0x3f6;
4763 break;
4764 case 1:
4765 probe_ent->irq = 15;
4766 probe_ent->port[0].cmd_addr = 0x170;
4767 probe_ent->port[0].altstatus_addr =
4768 probe_ent->port[0].ctl_addr = 0x376;
4769 break;
4770 }
4771 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 return probe_ent;
4774}
4775
4776/**
4777 * ata_pci_init_one - Initialize/register PCI IDE host controller
4778 * @pdev: Controller to be initialized
4779 * @port_info: Information from low-level host driver
4780 * @n_ports: Number of ports attached to host controller
4781 *
Edward Falk0baab862005-06-02 18:17:13 -04004782 * This is a helper function which can be called from a driver's
4783 * xxx_init_one() probe function if the hardware uses traditional
4784 * IDE taskfile registers.
4785 *
4786 * This function calls pci_enable_device(), reserves its register
4787 * regions, sets the dma mask, enables bus master mode, and calls
4788 * ata_device_add()
4789 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 * LOCKING:
4791 * Inherited from PCI layer (may sleep).
4792 *
4793 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004794 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 */
4796
4797int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4798 unsigned int n_ports)
4799{
Alan Cox47a86592005-10-04 08:09:19 -04004800 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 struct ata_port_info *port[2];
4802 u8 tmp8, mask;
4803 unsigned int legacy_mode = 0;
4804 int disable_dev_on_err = 1;
4805 int rc;
4806
4807 DPRINTK("ENTER\n");
4808
4809 port[0] = port_info[0];
4810 if (n_ports > 1)
4811 port[1] = port_info[1];
4812 else
4813 port[1] = port[0];
4814
4815 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4816 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004817 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4819 mask = (1 << 2) | (1 << 0);
4820 if ((tmp8 & mask) != mask)
4821 legacy_mode = (1 << 3);
4822 }
4823
4824 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004825 if ((!legacy_mode) && (n_ports > 2)) {
4826 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4827 n_ports = 2;
4828 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 }
4830
Alan Cox47a86592005-10-04 08:09:19 -04004831 /* FIXME: Really for ATA it isn't safe because the device may be
4832 multi-purpose and we want to leave it alone if it was already
4833 enabled. Secondly for shared use as Arjan says we want refcounting
4834
4835 Checking dev->is_enabled is insufficient as this is not set at
4836 boot for the primary video which is BIOS enabled
4837 */
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 rc = pci_enable_device(pdev);
4840 if (rc)
4841 return rc;
4842
4843 rc = pci_request_regions(pdev, DRV_NAME);
4844 if (rc) {
4845 disable_dev_on_err = 0;
4846 goto err_out;
4847 }
4848
Alan Cox47a86592005-10-04 08:09:19 -04004849 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 if (legacy_mode) {
4851 if (!request_region(0x1f0, 8, "libata")) {
4852 struct resource *conflict, res;
4853 res.start = 0x1f0;
4854 res.end = 0x1f0 + 8 - 1;
4855 conflict = ____request_resource(&ioport_resource, &res);
4856 if (!strcmp(conflict->name, "libata"))
4857 legacy_mode |= (1 << 0);
4858 else {
4859 disable_dev_on_err = 0;
4860 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4861 }
4862 } else
4863 legacy_mode |= (1 << 0);
4864
4865 if (!request_region(0x170, 8, "libata")) {
4866 struct resource *conflict, res;
4867 res.start = 0x170;
4868 res.end = 0x170 + 8 - 1;
4869 conflict = ____request_resource(&ioport_resource, &res);
4870 if (!strcmp(conflict->name, "libata"))
4871 legacy_mode |= (1 << 1);
4872 else {
4873 disable_dev_on_err = 0;
4874 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4875 }
4876 } else
4877 legacy_mode |= (1 << 1);
4878 }
4879
4880 /* we have legacy mode, but all ports are unavailable */
4881 if (legacy_mode == (1 << 3)) {
4882 rc = -EBUSY;
4883 goto err_out_regions;
4884 }
4885
4886 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4887 if (rc)
4888 goto err_out_regions;
4889 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4890 if (rc)
4891 goto err_out_regions;
4892
4893 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004894 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004895 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004896 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004897 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004898 } else {
4899 if (n_ports == 2)
4900 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4901 else
4902 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4903 }
4904 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 rc = -ENOMEM;
4906 goto err_out_regions;
4907 }
4908
4909 pci_set_master(pdev);
4910
4911 /* FIXME: check ata_device_add return */
4912 if (legacy_mode) {
4913 if (legacy_mode & (1 << 0))
4914 ata_device_add(probe_ent);
4915 if (legacy_mode & (1 << 1))
4916 ata_device_add(probe_ent2);
4917 } else
4918 ata_device_add(probe_ent);
4919
4920 kfree(probe_ent);
4921 kfree(probe_ent2);
4922
4923 return 0;
4924
4925err_out_regions:
4926 if (legacy_mode & (1 << 0))
4927 release_region(0x1f0, 8);
4928 if (legacy_mode & (1 << 1))
4929 release_region(0x170, 8);
4930 pci_release_regions(pdev);
4931err_out:
4932 if (disable_dev_on_err)
4933 pci_disable_device(pdev);
4934 return rc;
4935}
4936
4937/**
4938 * ata_pci_remove_one - PCI layer callback for device removal
4939 * @pdev: PCI device that was removed
4940 *
4941 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004942 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 * Handle this by unregistering all objects associated
4944 * with this PCI device. Free those objects. Then finally
4945 * release PCI resources and disable device.
4946 *
4947 * LOCKING:
4948 * Inherited from PCI layer (may sleep).
4949 */
4950
4951void ata_pci_remove_one (struct pci_dev *pdev)
4952{
4953 struct device *dev = pci_dev_to_dev(pdev);
4954 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Alan Cox17b14452005-09-15 15:44:00 +01004956 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 pci_release_regions(pdev);
4958 pci_disable_device(pdev);
4959 dev_set_drvdata(dev, NULL);
4960}
4961
4962/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004963int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
4965 unsigned long tmp = 0;
4966
4967 switch (bits->width) {
4968 case 1: {
4969 u8 tmp8 = 0;
4970 pci_read_config_byte(pdev, bits->reg, &tmp8);
4971 tmp = tmp8;
4972 break;
4973 }
4974 case 2: {
4975 u16 tmp16 = 0;
4976 pci_read_config_word(pdev, bits->reg, &tmp16);
4977 tmp = tmp16;
4978 break;
4979 }
4980 case 4: {
4981 u32 tmp32 = 0;
4982 pci_read_config_dword(pdev, bits->reg, &tmp32);
4983 tmp = tmp32;
4984 break;
4985 }
4986
4987 default:
4988 return -EINVAL;
4989 }
4990
4991 tmp &= bits->mask;
4992
4993 return (tmp == bits->val) ? 1 : 0;
4994}
Jens Axboe9b847542006-01-06 09:28:07 +01004995
4996int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4997{
4998 pci_save_state(pdev);
4999 pci_disable_device(pdev);
5000 pci_set_power_state(pdev, PCI_D3hot);
5001 return 0;
5002}
5003
5004int ata_pci_device_resume(struct pci_dev *pdev)
5005{
5006 pci_set_power_state(pdev, PCI_D0);
5007 pci_restore_state(pdev);
5008 pci_enable_device(pdev);
5009 pci_set_master(pdev);
5010 return 0;
5011}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012#endif /* CONFIG_PCI */
5013
5014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015static int __init ata_init(void)
5016{
5017 ata_wq = create_workqueue("ata");
5018 if (!ata_wq)
5019 return -ENOMEM;
5020
5021 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5022 return 0;
5023}
5024
5025static void __exit ata_exit(void)
5026{
5027 destroy_workqueue(ata_wq);
5028}
5029
5030module_init(ata_init);
5031module_exit(ata_exit);
5032
Jeff Garzik67846b32005-10-05 02:58:32 -04005033static unsigned long ratelimit_time;
5034static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5035
5036int ata_ratelimit(void)
5037{
5038 int rc;
5039 unsigned long flags;
5040
5041 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5042
5043 if (time_after(jiffies, ratelimit_time)) {
5044 rc = 1;
5045 ratelimit_time = jiffies + (HZ/5);
5046 } else
5047 rc = 0;
5048
5049 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5050
5051 return rc;
5052}
5053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054/*
5055 * libata is essentially a library of internal helper functions for
5056 * low-level ATA host controller drivers. As such, the API/ABI is
5057 * likely to change as new drivers are added and updated.
5058 * Do not depend on ABI/API stability.
5059 */
5060
5061EXPORT_SYMBOL_GPL(ata_std_bios_param);
5062EXPORT_SYMBOL_GPL(ata_std_ports);
5063EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005064EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065EXPORT_SYMBOL_GPL(ata_sg_init);
5066EXPORT_SYMBOL_GPL(ata_sg_init_one);
5067EXPORT_SYMBOL_GPL(ata_qc_complete);
5068EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5069EXPORT_SYMBOL_GPL(ata_eng_timeout);
5070EXPORT_SYMBOL_GPL(ata_tf_load);
5071EXPORT_SYMBOL_GPL(ata_tf_read);
5072EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5073EXPORT_SYMBOL_GPL(ata_std_dev_select);
5074EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5075EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5076EXPORT_SYMBOL_GPL(ata_check_status);
5077EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078EXPORT_SYMBOL_GPL(ata_exec_command);
5079EXPORT_SYMBOL_GPL(ata_port_start);
5080EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005081EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082EXPORT_SYMBOL_GPL(ata_interrupt);
5083EXPORT_SYMBOL_GPL(ata_qc_prep);
5084EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5085EXPORT_SYMBOL_GPL(ata_bmdma_start);
5086EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5087EXPORT_SYMBOL_GPL(ata_bmdma_status);
5088EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5089EXPORT_SYMBOL_GPL(ata_port_probe);
5090EXPORT_SYMBOL_GPL(sata_phy_reset);
5091EXPORT_SYMBOL_GPL(__sata_phy_reset);
5092EXPORT_SYMBOL_GPL(ata_bus_reset);
5093EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005094EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5096EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5097EXPORT_SYMBOL_GPL(ata_scsi_error);
5098EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5099EXPORT_SYMBOL_GPL(ata_scsi_release);
5100EXPORT_SYMBOL_GPL(ata_host_intr);
5101EXPORT_SYMBOL_GPL(ata_dev_classify);
5102EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005103EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5105
Alan Cox452503f2005-10-21 19:01:32 -04005106EXPORT_SYMBOL_GPL(ata_timing_compute);
5107EXPORT_SYMBOL_GPL(ata_timing_merge);
5108
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109#ifdef CONFIG_PCI
5110EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005111EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5113EXPORT_SYMBOL_GPL(ata_pci_init_one);
5114EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005115EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5116EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005118
5119EXPORT_SYMBOL_GPL(ata_device_suspend);
5120EXPORT_SYMBOL_GPL(ata_device_resume);
5121EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5122EXPORT_SYMBOL_GPL(ata_scsi_device_resume);