blob: 14cdbb336dd501990cdf6b0e8ed2882988d51c3c [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
Albert Lee59a10b12005-10-12 15:09:42 +080064static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ec2005-05-12 15:29:42 -040065static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ata_set_mode(struct ata_port *ap);
67static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040068static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040070static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u8 *xfer_mode_out,
72 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static unsigned int ata_unique_id = 1;
75static struct workqueue_struct *ata_wq;
76
Jeff Garzik1623c812005-08-30 03:37:42 -040077int atapi_enabled = 0;
78module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081MODULE_AUTHOR("Jeff Garzik");
82MODULE_DESCRIPTION("Library module for ATA devices");
83MODULE_LICENSE("GPL");
84MODULE_VERSION(DRV_VERSION);
85
86/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040087 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * @ap: Port to which output is sent
89 * @tf: ATA taskfile register set
90 *
91 * Outputs ATA taskfile to standard ATA host controller.
92 *
93 * LOCKING:
94 * Inherited from caller.
95 */
96
Jeff Garzik057ace52005-10-22 14:27:05 -040097static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct ata_ioports *ioaddr = &ap->ioaddr;
100 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
101
102 if (tf->ctl != ap->last_ctl) {
103 outb(tf->ctl, ioaddr->ctl_addr);
104 ap->last_ctl = tf->ctl;
105 ata_wait_idle(ap);
106 }
107
108 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
109 outb(tf->hob_feature, ioaddr->feature_addr);
110 outb(tf->hob_nsect, ioaddr->nsect_addr);
111 outb(tf->hob_lbal, ioaddr->lbal_addr);
112 outb(tf->hob_lbam, ioaddr->lbam_addr);
113 outb(tf->hob_lbah, ioaddr->lbah_addr);
114 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
115 tf->hob_feature,
116 tf->hob_nsect,
117 tf->hob_lbal,
118 tf->hob_lbam,
119 tf->hob_lbah);
120 }
121
122 if (is_addr) {
123 outb(tf->feature, ioaddr->feature_addr);
124 outb(tf->nsect, ioaddr->nsect_addr);
125 outb(tf->lbal, ioaddr->lbal_addr);
126 outb(tf->lbam, ioaddr->lbam_addr);
127 outb(tf->lbah, ioaddr->lbah_addr);
128 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
129 tf->feature,
130 tf->nsect,
131 tf->lbal,
132 tf->lbam,
133 tf->lbah);
134 }
135
136 if (tf->flags & ATA_TFLAG_DEVICE) {
137 outb(tf->device, ioaddr->device_addr);
138 VPRINTK("device 0x%X\n", tf->device);
139 }
140
141 ata_wait_idle(ap);
142}
143
144/**
145 * ata_tf_load_mmio - send taskfile registers to host controller
146 * @ap: Port to which output is sent
147 * @tf: ATA taskfile register set
148 *
149 * Outputs ATA taskfile to standard ATA host controller using MMIO.
150 *
151 * LOCKING:
152 * Inherited from caller.
153 */
154
Jeff Garzik057ace52005-10-22 14:27:05 -0400155static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct ata_ioports *ioaddr = &ap->ioaddr;
158 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
159
160 if (tf->ctl != ap->last_ctl) {
161 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
162 ap->last_ctl = tf->ctl;
163 ata_wait_idle(ap);
164 }
165
166 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
167 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
168 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
169 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
170 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
171 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
172 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
173 tf->hob_feature,
174 tf->hob_nsect,
175 tf->hob_lbal,
176 tf->hob_lbam,
177 tf->hob_lbah);
178 }
179
180 if (is_addr) {
181 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
182 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
183 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
184 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
185 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
186 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
187 tf->feature,
188 tf->nsect,
189 tf->lbal,
190 tf->lbam,
191 tf->lbah);
192 }
193
194 if (tf->flags & ATA_TFLAG_DEVICE) {
195 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
196 VPRINTK("device 0x%X\n", tf->device);
197 }
198
199 ata_wait_idle(ap);
200}
201
Edward Falk0baab862005-06-02 18:17:13 -0400202
203/**
204 * ata_tf_load - send taskfile registers to host controller
205 * @ap: Port to which output is sent
206 * @tf: ATA taskfile register set
207 *
208 * Outputs ATA taskfile to standard ATA host controller using MMIO
209 * or PIO as indicated by the ATA_FLAG_MMIO flag.
210 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
211 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
212 * hob_lbal, hob_lbam, and hob_lbah.
213 *
214 * This function waits for idle (!BUSY and !DRQ) after writing
215 * registers. If the control register has a new value, this
216 * function also waits for idle after writing control and before
217 * writing the remaining registers.
218 *
219 * May be used as the tf_load() entry in ata_port_operations.
220 *
221 * LOCKING:
222 * Inherited from caller.
223 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400224void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 if (ap->flags & ATA_FLAG_MMIO)
227 ata_tf_load_mmio(ap, tf);
228 else
229 ata_tf_load_pio(ap, tf);
230}
231
232/**
Edward Falk0baab862005-06-02 18:17:13 -0400233 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * @ap: port to which command is being issued
235 * @tf: ATA taskfile register set
236 *
Edward Falk0baab862005-06-02 18:17:13 -0400237 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * synchronization with interrupt handler / other threads.
239 *
240 * LOCKING:
241 * spin_lock_irqsave(host_set lock)
242 */
243
Jeff Garzik057ace52005-10-22 14:27:05 -0400244static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
247
248 outb(tf->command, ap->ioaddr.command_addr);
249 ata_pause(ap);
250}
251
252
253/**
254 * ata_exec_command_mmio - issue ATA command to host controller
255 * @ap: port to which command is being issued
256 * @tf: ATA taskfile register set
257 *
258 * Issues MMIO write to ATA command register, with proper
259 * synchronization with interrupt handler / other threads.
260 *
261 * LOCKING:
262 * spin_lock_irqsave(host_set lock)
263 */
264
Jeff Garzik057ace52005-10-22 14:27:05 -0400265static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
268
269 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
270 ata_pause(ap);
271}
272
Edward Falk0baab862005-06-02 18:17:13 -0400273
274/**
275 * ata_exec_command - issue ATA command to host controller
276 * @ap: port to which command is being issued
277 * @tf: ATA taskfile register set
278 *
279 * Issues PIO/MMIO write to ATA command register, with proper
280 * synchronization with interrupt handler / other threads.
281 *
282 * LOCKING:
283 * spin_lock_irqsave(host_set lock)
284 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400285void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 if (ap->flags & ATA_FLAG_MMIO)
288 ata_exec_command_mmio(ap, tf);
289 else
290 ata_exec_command_pio(ap, tf);
291}
292
293/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * ata_tf_to_host - issue ATA taskfile to host controller
295 * @ap: port to which command is being issued
296 * @tf: ATA taskfile register set
297 *
298 * Issues ATA taskfile register set to ATA host controller,
299 * with proper synchronization with interrupt handler and
300 * other threads.
301 *
302 * LOCKING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 * spin_lock_irqsave(host_set lock)
304 */
305
Jeff Garzike5338252005-10-30 21:37:17 -0500306static inline void ata_tf_to_host(struct ata_port *ap,
307 const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 ap->ops->tf_load(ap, tf);
310 ap->ops->exec_command(ap, tf);
311}
312
313/**
Edward Falk0baab862005-06-02 18:17:13 -0400314 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * @ap: Port from which input is read
316 * @tf: ATA taskfile register set for storing input
317 *
318 * Reads ATA taskfile registers for currently-selected device
319 * into @tf.
320 *
321 * LOCKING:
322 * Inherited from caller.
323 */
324
325static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
326{
327 struct ata_ioports *ioaddr = &ap->ioaddr;
328
Jeff Garzikac19bff2005-10-29 13:58:21 -0400329 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400330 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 tf->nsect = inb(ioaddr->nsect_addr);
332 tf->lbal = inb(ioaddr->lbal_addr);
333 tf->lbam = inb(ioaddr->lbam_addr);
334 tf->lbah = inb(ioaddr->lbah_addr);
335 tf->device = inb(ioaddr->device_addr);
336
337 if (tf->flags & ATA_TFLAG_LBA48) {
338 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
339 tf->hob_feature = inb(ioaddr->error_addr);
340 tf->hob_nsect = inb(ioaddr->nsect_addr);
341 tf->hob_lbal = inb(ioaddr->lbal_addr);
342 tf->hob_lbam = inb(ioaddr->lbam_addr);
343 tf->hob_lbah = inb(ioaddr->lbah_addr);
344 }
345}
346
347/**
348 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
349 * @ap: Port from which input is read
350 * @tf: ATA taskfile register set for storing input
351 *
352 * Reads ATA taskfile registers for currently-selected device
353 * into @tf via MMIO.
354 *
355 * LOCKING:
356 * Inherited from caller.
357 */
358
359static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
360{
361 struct ata_ioports *ioaddr = &ap->ioaddr;
362
Jeff Garzikac19bff2005-10-29 13:58:21 -0400363 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400364 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
366 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
367 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
368 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
369 tf->device = readb((void __iomem *)ioaddr->device_addr);
370
371 if (tf->flags & ATA_TFLAG_LBA48) {
372 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
373 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
374 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
375 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
376 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
377 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
378 }
379}
380
Edward Falk0baab862005-06-02 18:17:13 -0400381
382/**
383 * ata_tf_read - input device's ATA taskfile shadow registers
384 * @ap: Port from which input is read
385 * @tf: ATA taskfile register set for storing input
386 *
387 * Reads ATA taskfile registers for currently-selected device
388 * into @tf.
389 *
390 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
391 * is set, also reads the hob registers.
392 *
393 * May be used as the tf_read() entry in ata_port_operations.
394 *
395 * LOCKING:
396 * Inherited from caller.
397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
399{
400 if (ap->flags & ATA_FLAG_MMIO)
401 ata_tf_read_mmio(ap, tf);
402 else
403 ata_tf_read_pio(ap, tf);
404}
405
406/**
407 * ata_check_status_pio - Read device status reg & clear interrupt
408 * @ap: port where the device is
409 *
410 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400411 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * from this device
413 *
414 * LOCKING:
415 * Inherited from caller.
416 */
417static u8 ata_check_status_pio(struct ata_port *ap)
418{
419 return inb(ap->ioaddr.status_addr);
420}
421
422/**
423 * ata_check_status_mmio - Read device status reg & clear interrupt
424 * @ap: port where the device is
425 *
426 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400427 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * from this device
429 *
430 * LOCKING:
431 * Inherited from caller.
432 */
433static u8 ata_check_status_mmio(struct ata_port *ap)
434{
435 return readb((void __iomem *) ap->ioaddr.status_addr);
436}
437
Edward Falk0baab862005-06-02 18:17:13 -0400438
439/**
440 * ata_check_status - Read device status reg & clear interrupt
441 * @ap: port where the device is
442 *
443 * Reads ATA taskfile status register for currently-selected device
444 * and return its value. This also clears pending interrupts
445 * from this device
446 *
447 * May be used as the check_status() entry in ata_port_operations.
448 *
449 * LOCKING:
450 * Inherited from caller.
451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452u8 ata_check_status(struct ata_port *ap)
453{
454 if (ap->flags & ATA_FLAG_MMIO)
455 return ata_check_status_mmio(ap);
456 return ata_check_status_pio(ap);
457}
458
Edward Falk0baab862005-06-02 18:17:13 -0400459
460/**
461 * ata_altstatus - Read device alternate status reg
462 * @ap: port where the device is
463 *
464 * Reads ATA taskfile alternate status register for
465 * currently-selected device and return its value.
466 *
467 * Note: may NOT be used as the check_altstatus() entry in
468 * ata_port_operations.
469 *
470 * LOCKING:
471 * Inherited from caller.
472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473u8 ata_altstatus(struct ata_port *ap)
474{
475 if (ap->ops->check_altstatus)
476 return ap->ops->check_altstatus(ap);
477
478 if (ap->flags & ATA_FLAG_MMIO)
479 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
480 return inb(ap->ioaddr.altstatus_addr);
481}
482
Edward Falk0baab862005-06-02 18:17:13 -0400483
484/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
486 * @tf: Taskfile to convert
487 * @fis: Buffer into which data will output
488 * @pmp: Port multiplier port
489 *
490 * Converts a standard ATA taskfile to a Serial ATA
491 * FIS structure (Register - Host to Device).
492 *
493 * LOCKING:
494 * Inherited from caller.
495 */
496
Jeff Garzik057ace52005-10-22 14:27:05 -0400497void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 fis[0] = 0x27; /* Register - Host to Device FIS */
500 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
501 bit 7 indicates Command FIS */
502 fis[2] = tf->command;
503 fis[3] = tf->feature;
504
505 fis[4] = tf->lbal;
506 fis[5] = tf->lbam;
507 fis[6] = tf->lbah;
508 fis[7] = tf->device;
509
510 fis[8] = tf->hob_lbal;
511 fis[9] = tf->hob_lbam;
512 fis[10] = tf->hob_lbah;
513 fis[11] = tf->hob_feature;
514
515 fis[12] = tf->nsect;
516 fis[13] = tf->hob_nsect;
517 fis[14] = 0;
518 fis[15] = tf->ctl;
519
520 fis[16] = 0;
521 fis[17] = 0;
522 fis[18] = 0;
523 fis[19] = 0;
524}
525
526/**
527 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
528 * @fis: Buffer from which data will be input
529 * @tf: Taskfile to output
530 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500531 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *
533 * LOCKING:
534 * Inherited from caller.
535 */
536
Jeff Garzik057ace52005-10-22 14:27:05 -0400537void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 tf->command = fis[2]; /* status */
540 tf->feature = fis[3]; /* error */
541
542 tf->lbal = fis[4];
543 tf->lbam = fis[5];
544 tf->lbah = fis[6];
545 tf->device = fis[7];
546
547 tf->hob_lbal = fis[8];
548 tf->hob_lbam = fis[9];
549 tf->hob_lbah = fis[10];
550
551 tf->nsect = fis[12];
552 tf->hob_nsect = fis[13];
553}
554
Albert Lee8cbd6df2005-10-12 15:06:27 +0800555static const u8 ata_rw_cmds[] = {
556 /* pio multi */
557 ATA_CMD_READ_MULTI,
558 ATA_CMD_WRITE_MULTI,
559 ATA_CMD_READ_MULTI_EXT,
560 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100561 0,
562 0,
563 0,
564 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800565 /* pio */
566 ATA_CMD_PIO_READ,
567 ATA_CMD_PIO_WRITE,
568 ATA_CMD_PIO_READ_EXT,
569 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100570 0,
571 0,
572 0,
573 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800574 /* dma */
575 ATA_CMD_READ,
576 ATA_CMD_WRITE,
577 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100578 ATA_CMD_WRITE_EXT,
579 0,
580 0,
581 0,
582 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800583};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800586 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
587 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800589 * Examine the device configuration and tf->flags to calculate
590 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 *
592 * LOCKING:
593 * caller.
594 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100595int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800597 struct ata_taskfile *tf = &qc->tf;
598 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100599 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100601 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800602
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100603 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800604 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
605 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Albert Lee8cbd6df2005-10-12 15:06:27 +0800607 if (dev->flags & ATA_DFLAG_PIO) {
608 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100609 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000610 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
611 /* Unable to use DMA due to host limitation */
612 tf->protocol = ATA_PROT_PIO;
613 index = dev->multi_count ? 0 : 4;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800614 } else {
615 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100616 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100619 cmd = ata_rw_cmds[index + fua + lba48 + write];
620 if (cmd) {
621 tf->command = cmd;
622 return 0;
623 }
624 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100627static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 "UDMA/16",
629 "UDMA/25",
630 "UDMA/33",
631 "UDMA/44",
632 "UDMA/66",
633 "UDMA/100",
634 "UDMA/133",
635 "UDMA7",
636 "MWDMA0",
637 "MWDMA1",
638 "MWDMA2",
639 "PIO0",
640 "PIO1",
641 "PIO2",
642 "PIO3",
643 "PIO4",
644};
645
646/**
647 * ata_udma_string - convert UDMA bit offset to string
648 * @mask: mask of bits supported; only highest bit counts.
649 *
650 * Determine string which represents the highest speed
651 * (highest bit in @udma_mask).
652 *
653 * LOCKING:
654 * None.
655 *
656 * RETURNS:
657 * Constant C string representing highest speed listed in
658 * @udma_mask, or the constant C string "<n/a>".
659 */
660
661static const char *ata_mode_string(unsigned int mask)
662{
663 int i;
664
665 for (i = 7; i >= 0; i--)
666 if (mask & (1 << i))
667 goto out;
668 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
669 if (mask & (1 << i))
670 goto out;
671 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
672 if (mask & (1 << i))
673 goto out;
674
675 return "<n/a>";
676
677out:
678 return xfer_mode_str[i];
679}
680
681/**
682 * ata_pio_devchk - PATA device presence detection
683 * @ap: ATA channel to examine
684 * @device: Device to examine (starting at zero)
685 *
686 * This technique was originally described in
687 * Hale Landis's ATADRVR (www.ata-atapi.com), and
688 * later found its way into the ATA/ATAPI spec.
689 *
690 * Write a pattern to the ATA shadow registers,
691 * and if a device is present, it will respond by
692 * correctly storing and echoing back the
693 * ATA shadow register contents.
694 *
695 * LOCKING:
696 * caller.
697 */
698
699static unsigned int ata_pio_devchk(struct ata_port *ap,
700 unsigned int device)
701{
702 struct ata_ioports *ioaddr = &ap->ioaddr;
703 u8 nsect, lbal;
704
705 ap->ops->dev_select(ap, device);
706
707 outb(0x55, ioaddr->nsect_addr);
708 outb(0xaa, ioaddr->lbal_addr);
709
710 outb(0xaa, ioaddr->nsect_addr);
711 outb(0x55, ioaddr->lbal_addr);
712
713 outb(0x55, ioaddr->nsect_addr);
714 outb(0xaa, ioaddr->lbal_addr);
715
716 nsect = inb(ioaddr->nsect_addr);
717 lbal = inb(ioaddr->lbal_addr);
718
719 if ((nsect == 0x55) && (lbal == 0xaa))
720 return 1; /* we found a device */
721
722 return 0; /* nothing found */
723}
724
725/**
726 * ata_mmio_devchk - PATA device presence detection
727 * @ap: ATA channel to examine
728 * @device: Device to examine (starting at zero)
729 *
730 * This technique was originally described in
731 * Hale Landis's ATADRVR (www.ata-atapi.com), and
732 * later found its way into the ATA/ATAPI spec.
733 *
734 * Write a pattern to the ATA shadow registers,
735 * and if a device is present, it will respond by
736 * correctly storing and echoing back the
737 * ATA shadow register contents.
738 *
739 * LOCKING:
740 * caller.
741 */
742
743static unsigned int ata_mmio_devchk(struct ata_port *ap,
744 unsigned int device)
745{
746 struct ata_ioports *ioaddr = &ap->ioaddr;
747 u8 nsect, lbal;
748
749 ap->ops->dev_select(ap, device);
750
751 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
752 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
753
754 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
755 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
756
757 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
758 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
759
760 nsect = readb((void __iomem *) ioaddr->nsect_addr);
761 lbal = readb((void __iomem *) ioaddr->lbal_addr);
762
763 if ((nsect == 0x55) && (lbal == 0xaa))
764 return 1; /* we found a device */
765
766 return 0; /* nothing found */
767}
768
769/**
770 * ata_devchk - PATA device presence detection
771 * @ap: ATA channel to examine
772 * @device: Device to examine (starting at zero)
773 *
774 * Dispatch ATA device presence detection, depending
775 * on whether we are using PIO or MMIO to talk to the
776 * ATA shadow registers.
777 *
778 * LOCKING:
779 * caller.
780 */
781
782static unsigned int ata_devchk(struct ata_port *ap,
783 unsigned int device)
784{
785 if (ap->flags & ATA_FLAG_MMIO)
786 return ata_mmio_devchk(ap, device);
787 return ata_pio_devchk(ap, device);
788}
789
790/**
791 * ata_dev_classify - determine device type based on ATA-spec signature
792 * @tf: ATA taskfile register set for device to be identified
793 *
794 * Determine from taskfile register contents whether a device is
795 * ATA or ATAPI, as per "Signature and persistence" section
796 * of ATA/PI spec (volume 1, sect 5.14).
797 *
798 * LOCKING:
799 * None.
800 *
801 * RETURNS:
802 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
803 * the event of failure.
804 */
805
Jeff Garzik057ace52005-10-22 14:27:05 -0400806unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 /* Apple's open source Darwin code hints that some devices only
809 * put a proper signature into the LBA mid/high registers,
810 * So, we only check those. It's sufficient for uniqueness.
811 */
812
813 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
814 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
815 DPRINTK("found ATA device by sig\n");
816 return ATA_DEV_ATA;
817 }
818
819 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
820 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
821 DPRINTK("found ATAPI device by sig\n");
822 return ATA_DEV_ATAPI;
823 }
824
825 DPRINTK("unknown device\n");
826 return ATA_DEV_UNKNOWN;
827}
828
829/**
830 * ata_dev_try_classify - Parse returned ATA device signature
831 * @ap: ATA channel to examine
832 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900833 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 *
835 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
836 * an ATA/ATAPI-defined set of values is placed in the ATA
837 * shadow registers, indicating the results of device detection
838 * and diagnostics.
839 *
840 * Select the ATA device, and read the values from the ATA shadow
841 * registers. Then parse according to the Error register value,
842 * and the spec-defined values examined by ata_dev_classify().
843 *
844 * LOCKING:
845 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900846 *
847 * RETURNS:
848 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
850
Tejun Heob4dc7622006-01-24 17:05:22 +0900851static unsigned int
852ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct ata_taskfile tf;
855 unsigned int class;
856 u8 err;
857
858 ap->ops->dev_select(ap, device);
859
860 memset(&tf, 0, sizeof(tf));
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400863 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900864 if (r_err)
865 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /* see if device passed diags */
868 if (err == 1)
869 /* do nothing */ ;
870 else if ((device == 0) && (err == 0x81))
871 /* do nothing */ ;
872 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900873 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Tejun Heob4dc7622006-01-24 17:05:22 +0900875 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900879 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900881 return ATA_DEV_NONE;
882 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885/**
886 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
887 * @id: IDENTIFY DEVICE results we will examine
888 * @s: string into which data is output
889 * @ofs: offset into identify device page
890 * @len: length of string to return. must be an even number.
891 *
892 * The strings in the IDENTIFY DEVICE page are broken up into
893 * 16-bit chunks. Run through the string, and output each
894 * 8-bit chunk linearly, regardless of platform.
895 *
896 * LOCKING:
897 * caller.
898 */
899
Jeff Garzik057ace52005-10-22 14:27:05 -0400900void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 unsigned int ofs, unsigned int len)
902{
903 unsigned int c;
904
905 while (len > 0) {
906 c = id[ofs] >> 8;
907 *s = c;
908 s++;
909
910 c = id[ofs] & 0xff;
911 *s = c;
912 s++;
913
914 ofs++;
915 len -= 2;
916 }
917}
918
Edward Falk0baab862005-06-02 18:17:13 -0400919
920/**
921 * ata_noop_dev_select - Select device 0/1 on ATA bus
922 * @ap: ATA channel to manipulate
923 * @device: ATA device (numbered from zero) to select
924 *
925 * This function performs no actual function.
926 *
927 * May be used as the dev_select() entry in ata_port_operations.
928 *
929 * LOCKING:
930 * caller.
931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
933{
934}
935
Edward Falk0baab862005-06-02 18:17:13 -0400936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/**
938 * ata_std_dev_select - Select device 0/1 on ATA bus
939 * @ap: ATA channel to manipulate
940 * @device: ATA device (numbered from zero) to select
941 *
942 * Use the method defined in the ATA specification to
943 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400944 * ATA channel. Works with both PIO and MMIO.
945 *
946 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 *
948 * LOCKING:
949 * caller.
950 */
951
952void ata_std_dev_select (struct ata_port *ap, unsigned int device)
953{
954 u8 tmp;
955
956 if (device == 0)
957 tmp = ATA_DEVICE_OBS;
958 else
959 tmp = ATA_DEVICE_OBS | ATA_DEV1;
960
961 if (ap->flags & ATA_FLAG_MMIO) {
962 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
963 } else {
964 outb(tmp, ap->ioaddr.device_addr);
965 }
966 ata_pause(ap); /* needed; also flushes, for mmio */
967}
968
969/**
970 * ata_dev_select - Select device 0/1 on ATA bus
971 * @ap: ATA channel to manipulate
972 * @device: ATA device (numbered from zero) to select
973 * @wait: non-zero to wait for Status register BSY bit to clear
974 * @can_sleep: non-zero if context allows sleeping
975 *
976 * Use the method defined in the ATA specification to
977 * make either device 0, or device 1, active on the
978 * ATA channel.
979 *
980 * This is a high-level version of ata_std_dev_select(),
981 * which additionally provides the services of inserting
982 * the proper pauses and status polling, where needed.
983 *
984 * LOCKING:
985 * caller.
986 */
987
988void ata_dev_select(struct ata_port *ap, unsigned int device,
989 unsigned int wait, unsigned int can_sleep)
990{
991 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
992 ap->id, device, wait);
993
994 if (wait)
995 ata_wait_idle(ap);
996
997 ap->ops->dev_select(ap, device);
998
999 if (wait) {
1000 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1001 msleep(150);
1002 ata_wait_idle(ap);
1003 }
1004}
1005
1006/**
1007 * ata_dump_id - IDENTIFY DEVICE info debugging output
1008 * @dev: Device whose IDENTIFY DEVICE page we will dump
1009 *
1010 * Dump selected 16-bit words from a detected device's
1011 * IDENTIFY PAGE page.
1012 *
1013 * LOCKING:
1014 * caller.
1015 */
1016
Jeff Garzik057ace52005-10-22 14:27:05 -04001017static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 DPRINTK("49==0x%04x "
1020 "53==0x%04x "
1021 "63==0x%04x "
1022 "64==0x%04x "
1023 "75==0x%04x \n",
1024 dev->id[49],
1025 dev->id[53],
1026 dev->id[63],
1027 dev->id[64],
1028 dev->id[75]);
1029 DPRINTK("80==0x%04x "
1030 "81==0x%04x "
1031 "82==0x%04x "
1032 "83==0x%04x "
1033 "84==0x%04x \n",
1034 dev->id[80],
1035 dev->id[81],
1036 dev->id[82],
1037 dev->id[83],
1038 dev->id[84]);
1039 DPRINTK("88==0x%04x "
1040 "93==0x%04x\n",
1041 dev->id[88],
1042 dev->id[93]);
1043}
1044
Alan Cox11e29e22005-10-21 18:46:32 -04001045/*
1046 * Compute the PIO modes available for this device. This is not as
1047 * trivial as it seems if we must consider early devices correctly.
1048 *
1049 * FIXME: pre IDE drive timing (do we care ?).
1050 */
1051
Jeff Garzik057ace52005-10-22 14:27:05 -04001052static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001053{
1054 u16 modes;
1055
Alan Coxffa29452006-01-09 17:14:40 +00001056 /* Usual case. Word 53 indicates word 64 is valid */
1057 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -04001058 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1059 modes <<= 3;
1060 modes |= 0x7;
1061 return modes;
1062 }
1063
Alan Coxffa29452006-01-09 17:14:40 +00001064 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
1065 number for the maximum. Turn it into a mask and return it */
1066 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -04001067 return modes;
Alan Coxffa29452006-01-09 17:14:40 +00001068 /* But wait.. there's more. Design your standards by committee and
1069 you too can get a free iordy field to process. However its the
1070 speeds not the modes that are supported... Note drivers using the
1071 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -04001072}
1073
Tejun Heo95064372006-01-23 13:09:37 +09001074static inline void
1075ata_queue_packet_task(struct ata_port *ap)
1076{
Tejun Heoc18d06f2006-02-02 00:56:10 +09001077 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
1078 queue_work(ata_wq, &ap->packet_task);
Tejun Heo95064372006-01-23 13:09:37 +09001079}
1080
1081static inline void
1082ata_queue_pio_task(struct ata_port *ap)
1083{
Tejun Heoc18d06f2006-02-02 00:56:10 +09001084 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
1085 queue_work(ata_wq, &ap->pio_task);
Tejun Heo95064372006-01-23 13:09:37 +09001086}
1087
1088static inline void
1089ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
1090{
Tejun Heoc18d06f2006-02-02 00:56:10 +09001091 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
1092 queue_delayed_work(ata_wq, &ap->pio_task, delay);
1093}
1094
1095/**
1096 * ata_flush_pio_tasks - Flush pio_task and packet_task
1097 * @ap: the target ata_port
1098 *
1099 * After this function completes, pio_task and packet_task are
1100 * guranteed not to be running or scheduled.
1101 *
1102 * LOCKING:
1103 * Kernel thread context (may sleep)
1104 */
1105
1106static void ata_flush_pio_tasks(struct ata_port *ap)
1107{
1108 int tmp = 0;
1109 unsigned long flags;
1110
1111 DPRINTK("ENTER\n");
1112
1113 spin_lock_irqsave(&ap->host_set->lock, flags);
1114 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
1115 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1116
1117 DPRINTK("flush #1\n");
1118 flush_workqueue(ata_wq);
1119
1120 /*
1121 * At this point, if a task is running, it's guaranteed to see
1122 * the FLUSH flag; thus, it will never queue pio tasks again.
1123 * Cancel and flush.
1124 */
1125 tmp |= cancel_delayed_work(&ap->pio_task);
1126 tmp |= cancel_delayed_work(&ap->packet_task);
1127 if (!tmp) {
1128 DPRINTK("flush #2\n");
1129 flush_workqueue(ata_wq);
1130 }
1131
1132 spin_lock_irqsave(&ap->host_set->lock, flags);
1133 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
1134 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1135
1136 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +09001137}
1138
Tejun Heo77853bf2006-01-23 13:09:36 +09001139void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001140{
Tejun Heo77853bf2006-01-23 13:09:36 +09001141 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001142
Tejun Heo77853bf2006-01-23 13:09:36 +09001143 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001144 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001145}
1146
1147/**
1148 * ata_exec_internal - execute libata internal command
1149 * @ap: Port to which the command is sent
1150 * @dev: Device to which the command is sent
1151 * @tf: Taskfile registers for the command and the result
1152 * @dma_dir: Data tranfer direction of the command
1153 * @buf: Data buffer of the command
1154 * @buflen: Length of data buffer
1155 *
1156 * Executes libata internal command with timeout. @tf contains
1157 * command on entry and result on return. Timeout and error
1158 * conditions are reported via return value. No recovery action
1159 * is taken after a command times out. It's caller's duty to
1160 * clean up after timeout.
1161 *
1162 * LOCKING:
1163 * None. Should be called with kernel context, might sleep.
1164 */
1165
1166static unsigned
1167ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1168 struct ata_taskfile *tf,
1169 int dma_dir, void *buf, unsigned int buflen)
1170{
1171 u8 command = tf->command;
1172 struct ata_queued_cmd *qc;
1173 DECLARE_COMPLETION(wait);
1174 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001175 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001176
1177 spin_lock_irqsave(&ap->host_set->lock, flags);
1178
1179 qc = ata_qc_new_init(ap, dev);
1180 BUG_ON(qc == NULL);
1181
1182 qc->tf = *tf;
1183 qc->dma_dir = dma_dir;
1184 if (dma_dir != DMA_NONE) {
1185 ata_sg_init_one(qc, buf, buflen);
1186 qc->nsect = buflen / ATA_SECT_SIZE;
1187 }
1188
Tejun Heo77853bf2006-01-23 13:09:36 +09001189 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001190 qc->complete_fn = ata_qc_complete_internal;
1191
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09001192 qc->err_mask = ata_qc_issue(qc);
1193 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +09001194 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001195
1196 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1197
1198 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1199 spin_lock_irqsave(&ap->host_set->lock, flags);
1200
1201 /* We're racing with irq here. If we lose, the
1202 * following test prevents us from completing the qc
1203 * again. If completion irq occurs after here but
1204 * before the caller cleans up, it will result in a
1205 * spurious interrupt. We can live with that.
1206 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001207 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001208 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001209 ata_qc_complete(qc);
1210 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1211 ap->id, command);
1212 }
1213
1214 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1215 }
1216
Tejun Heo77853bf2006-01-23 13:09:36 +09001217 *tf = qc->tf;
1218 err_mask = qc->err_mask;
1219
1220 ata_qc_free(qc);
1221
1222 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001226 * ata_pio_need_iordy - check if iordy needed
1227 * @adev: ATA device
1228 *
1229 * Check if the current speed of the device requires IORDY. Used
1230 * by various controllers for chip configuration.
1231 */
1232
1233unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1234{
1235 int pio;
1236 int speed = adev->pio_mode - XFER_PIO_0;
1237
1238 if (speed < 2)
1239 return 0;
1240 if (speed > 2)
1241 return 1;
1242
1243 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1244
1245 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1246 pio = adev->id[ATA_ID_EIDE_PIO];
1247 /* Is the speed faster than the drive allows non IORDY ? */
1248 if (pio) {
1249 /* This is cycle times not frequency - watch the logic! */
1250 if (pio > 240) /* PIO2 is 240nS per cycle */
1251 return 1;
1252 return 0;
1253 }
1254 }
1255 return 0;
1256}
1257
1258/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1260 * @ap: port on which device we wish to probe resides
1261 * @device: device bus address, starting at zero
1262 *
1263 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1264 * command, and read back the 512-byte device information page.
1265 * The device information page is fed to us via the standard
1266 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1267 * using standard PIO-IN paths)
1268 *
1269 * After reading the device information page, we use several
1270 * bits of information from it to initialize data structures
1271 * that will be used during the lifetime of the ata_device.
1272 * Other data from the info page is used to disqualify certain
1273 * older ATA devices we do not wish to support.
1274 *
1275 * LOCKING:
1276 * Inherited from caller. Some functions called by this function
1277 * obtain the host_set lock.
1278 */
1279
1280static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1281{
1282 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001283 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 u16 tmp;
1285 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001287 struct ata_taskfile tf;
1288 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int rc;
1290
1291 if (!ata_dev_present(dev)) {
1292 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1293 ap->id, device);
1294 return;
1295 }
1296
1297 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1298 using_edd = 0;
1299 else
1300 using_edd = 1;
1301
1302 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1303
1304 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1305 dev->class == ATA_DEV_NONE);
1306
1307 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001310 ata_tf_init(ap, &tf, device);
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001313 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 DPRINTK("do ATA identify\n");
1315 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001316 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 DPRINTK("do ATAPI identify\n");
1318 }
1319
Tejun Heoa0123702005-12-13 14:49:31 +09001320 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Tejun Heoa0123702005-12-13 14:49:31 +09001322 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1323 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Tejun Heoa0123702005-12-13 14:49:31 +09001325 if (err_mask) {
1326 if (err_mask & ~AC_ERR_DEV)
1327 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /*
1330 * arg! EDD works for all test cases, but seems to return
1331 * the ATA signature for some ATAPI devices. Until the
1332 * reason for this is found and fixed, we fix up the mess
1333 * here. If IDENTIFY DEVICE returns command aborted
1334 * (as ATAPI devices do), then we issue an
1335 * IDENTIFY PACKET DEVICE.
1336 *
1337 * ATA software reset (SRST, the default) does not appear
1338 * to have this problem.
1339 */
Albert Lee7c398332005-11-09 13:03:30 +08001340 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001341 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (err & ATA_ABORTED) {
1343 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 goto retry;
1345 }
1346 }
1347 goto err_out;
1348 }
1349
1350 swap_buf_le16(dev->id, ATA_ID_WORDS);
1351
1352 /* print device capabilities */
1353 printk(KERN_DEBUG "ata%u: dev %u cfg "
1354 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1355 ap->id, device, dev->id[49],
1356 dev->id[82], dev->id[83], dev->id[84],
1357 dev->id[85], dev->id[86], dev->id[87],
1358 dev->id[88]);
1359
1360 /*
1361 * common ATA, ATAPI feature tests
1362 */
1363
Albert Lee8bf62ec2005-05-12 15:29:42 -04001364 /* we require DMA support (bits 8 of word 49) */
1365 if (!ata_id_has_dma(dev->id)) {
1366 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 goto err_out_nosup;
1368 }
1369
1370 /* quick-n-dirty find max transfer mode; for printk only */
1371 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1372 if (!xfer_modes)
1373 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001374 if (!xfer_modes)
1375 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 ata_dump_id(dev);
1378
1379 /* ATA-specific feature tests */
1380 if (dev->class == ATA_DEV_ATA) {
1381 if (!ata_id_is_ata(dev->id)) /* sanity check */
1382 goto err_out_nosup;
1383
Albert Lee8bf62ec2005-05-12 15:29:42 -04001384 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001386 for (major_version = 14; major_version >= 1; major_version--)
1387 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389
Albert Lee8bf62ec2005-05-12 15:29:42 -04001390 /*
1391 * The exact sequence expected by certain pre-ATA4 drives is:
1392 * SRST RESET
1393 * IDENTIFY
1394 * INITIALIZE DEVICE PARAMETERS
1395 * anything else..
1396 * Some drives were very specific about that exact sequence.
1397 */
Albert Lee59a10b12005-10-12 15:09:42 +08001398 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001399 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Albert Lee59a10b12005-10-12 15:09:42 +08001401 /* current CHS translation info (id[53-58]) might be
1402 * changed. reread the identify device info.
1403 */
1404 ata_dev_reread_id(ap, dev);
1405 }
1406
Albert Lee8bf62ec2005-05-12 15:29:42 -04001407 if (ata_id_has_lba(dev->id)) {
1408 dev->flags |= ATA_DFLAG_LBA;
1409
1410 if (ata_id_has_lba48(dev->id)) {
1411 dev->flags |= ATA_DFLAG_LBA48;
1412 dev->n_sectors = ata_id_u64(dev->id, 100);
1413 } else {
1414 dev->n_sectors = ata_id_u32(dev->id, 60);
1415 }
1416
1417 /* print device info to dmesg */
1418 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1419 ap->id, device,
1420 major_version,
1421 ata_mode_string(xfer_modes),
1422 (unsigned long long)dev->n_sectors,
1423 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1424 } else {
1425 /* CHS */
1426
1427 /* Default translation */
1428 dev->cylinders = dev->id[1];
1429 dev->heads = dev->id[3];
1430 dev->sectors = dev->id[6];
1431 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1432
1433 if (ata_id_current_chs_valid(dev->id)) {
1434 /* Current CHS translation is valid. */
1435 dev->cylinders = dev->id[54];
1436 dev->heads = dev->id[55];
1437 dev->sectors = dev->id[56];
1438
1439 dev->n_sectors = ata_id_u32(dev->id, 57);
1440 }
1441
1442 /* print device info to dmesg */
1443 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1444 ap->id, device,
1445 major_version,
1446 ata_mode_string(xfer_modes),
1447 (unsigned long long)dev->n_sectors,
1448 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
1452 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454
1455 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001456 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (ata_id_is_ata(dev->id)) /* sanity check */
1458 goto err_out_nosup;
1459
1460 rc = atapi_cdb_len(dev->id);
1461 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1462 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1463 goto err_out_nosup;
1464 }
1465 ap->cdb_len = (unsigned int) rc;
1466 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1467
1468 /* print device info to dmesg */
1469 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1470 ap->id, device,
1471 ata_mode_string(xfer_modes));
1472 }
1473
1474 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1475 return;
1476
1477err_out_nosup:
1478 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1479 ap->id, device);
1480err_out:
1481 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1482 DPRINTK("EXIT, err\n");
1483}
1484
Brad Campbell6f2f3812005-05-12 15:07:47 -04001485
Jeff Garzik057ace52005-10-22 14:27:05 -04001486static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001487{
1488 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1489}
1490
1491/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001492 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1493 * @ap: Bus
1494 * @i: Device
Brad Campbell6f2f3812005-05-12 15:07:47 -04001495 *
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001496 * LOCKING:
Brad Campbell6f2f3812005-05-12 15:07:47 -04001497 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001498
Brad Campbell6f2f3812005-05-12 15:07:47 -04001499void ata_dev_config(struct ata_port *ap, unsigned int i)
1500{
1501 /* limit bridge transfers to udma5, 200 sectors */
1502 if (ata_dev_knobble(ap)) {
1503 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1504 ap->id, ap->device->devno);
1505 ap->udma_mask &= ATA_UDMA5;
1506 ap->host->max_sectors = ATA_MAX_SECTORS;
1507 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001508 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001509 }
1510
1511 if (ap->ops->dev_config)
1512 ap->ops->dev_config(ap, &ap->device[i]);
1513}
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515/**
1516 * ata_bus_probe - Reset and probe ATA bus
1517 * @ap: Bus to probe
1518 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001519 * Master ATA bus probing function. Initiates a hardware-dependent
1520 * bus reset, then attempts to identify any devices found on
1521 * the bus.
1522 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001524 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 *
1526 * RETURNS:
1527 * Zero on success, non-zero on error.
1528 */
1529
1530static int ata_bus_probe(struct ata_port *ap)
1531{
1532 unsigned int i, found = 0;
1533
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001534 if (ap->ops->probe_reset) {
1535 unsigned int classes[ATA_MAX_DEVICES];
1536 int rc;
1537
1538 ata_port_probe(ap);
1539
1540 rc = ap->ops->probe_reset(ap, classes);
1541 if (rc == 0) {
1542 for (i = 0; i < ATA_MAX_DEVICES; i++)
1543 ap->device[i].class = classes[i];
1544 } else {
1545 printk(KERN_ERR "ata%u: probe reset failed, "
1546 "disabling port\n", ap->id);
1547 ata_port_disable(ap);
1548 }
1549 } else
1550 ap->ops->phy_reset(ap);
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1553 goto err_out;
1554
1555 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1556 ata_dev_identify(ap, i);
1557 if (ata_dev_present(&ap->device[i])) {
1558 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001559 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561 }
1562
1563 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1564 goto err_out_disable;
1565
1566 ata_set_mode(ap);
1567 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1568 goto err_out_disable;
1569
1570 return 0;
1571
1572err_out_disable:
1573 ap->ops->port_disable(ap);
1574err_out:
1575 return -1;
1576}
1577
1578/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001579 * ata_port_probe - Mark port as enabled
1580 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001582 * Modify @ap data structure such that the system
1583 * thinks that the entire port is enabled.
1584 *
1585 * LOCKING: host_set lock, or some other form of
1586 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 */
1588
1589void ata_port_probe(struct ata_port *ap)
1590{
1591 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1592}
1593
1594/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001595 * sata_print_link_status - Print SATA link status
1596 * @ap: SATA port to printk link status about
1597 *
1598 * This function prints link speed and status of a SATA link.
1599 *
1600 * LOCKING:
1601 * None.
1602 */
1603static void sata_print_link_status(struct ata_port *ap)
1604{
1605 u32 sstatus, tmp;
1606 const char *speed;
1607
1608 if (!ap->ops->scr_read)
1609 return;
1610
1611 sstatus = scr_read(ap, SCR_STATUS);
1612
1613 if (sata_dev_present(ap)) {
1614 tmp = (sstatus >> 4) & 0xf;
1615 if (tmp & (1 << 0))
1616 speed = "1.5";
1617 else if (tmp & (1 << 1))
1618 speed = "3.0";
1619 else
1620 speed = "<unknown>";
1621 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1622 ap->id, speed, sstatus);
1623 } else {
1624 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1625 ap->id, sstatus);
1626 }
1627}
1628
1629/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001630 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1631 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001633 * This function issues commands to standard SATA Sxxx
1634 * PHY registers, to wake up the phy (and device), and
1635 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 *
1637 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001638 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 *
1640 */
1641void __sata_phy_reset(struct ata_port *ap)
1642{
1643 u32 sstatus;
1644 unsigned long timeout = jiffies + (HZ * 5);
1645
1646 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001647 /* issue phy wake/reset */
1648 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001649 /* Couldn't find anything in SATA I/II specs, but
1650 * AHCI-1.1 10.4.2 says at least 1 ms. */
1651 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
Brett Russcdcca892005-03-28 15:10:27 -05001653 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 /* wait for phy to become ready, if necessary */
1656 do {
1657 msleep(200);
1658 sstatus = scr_read(ap, SCR_STATUS);
1659 if ((sstatus & 0xf) != 1)
1660 break;
1661 } while (time_before(jiffies, timeout));
1662
Tejun Heo3be680b2005-12-19 22:35:02 +09001663 /* print link status */
1664 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001665
Tejun Heo3be680b2005-12-19 22:35:02 +09001666 /* TODO: phy layer with polling, timeouts, etc. */
1667 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001669 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1673 return;
1674
1675 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1676 ata_port_disable(ap);
1677 return;
1678 }
1679
1680 ap->cbl = ATA_CBL_SATA;
1681}
1682
1683/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001684 * sata_phy_reset - Reset SATA bus.
1685 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001687 * This function resets the SATA bus, and then probes
1688 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 *
1690 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001691 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 *
1693 */
1694void sata_phy_reset(struct ata_port *ap)
1695{
1696 __sata_phy_reset(ap);
1697 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1698 return;
1699 ata_bus_reset(ap);
1700}
1701
1702/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001703 * ata_port_disable - Disable port.
1704 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001706 * Modify @ap data structure such that the system
1707 * thinks that the entire port is disabled, and should
1708 * never attempt to probe or communicate with devices
1709 * on this port.
1710 *
1711 * LOCKING: host_set lock, or some other form of
1712 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 */
1714
1715void ata_port_disable(struct ata_port *ap)
1716{
1717 ap->device[0].class = ATA_DEV_NONE;
1718 ap->device[1].class = ATA_DEV_NONE;
1719 ap->flags |= ATA_FLAG_PORT_DISABLED;
1720}
1721
Alan Cox452503f2005-10-21 19:01:32 -04001722/*
1723 * This mode timing computation functionality is ported over from
1724 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1725 */
1726/*
1727 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1728 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1729 * for PIO 5, which is a nonstandard extension and UDMA6, which
1730 * is currently supported only by Maxtor drives.
1731 */
1732
1733static const struct ata_timing ata_timing[] = {
1734
1735 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1736 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1737 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1738 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1739
1740 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1741 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1742 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1743
1744/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1745
1746 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1747 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1748 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1749
1750 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1751 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1752 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1753
1754/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1755 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1756 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1757
1758 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1759 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1760 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1761
1762/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1763
1764 { 0xFF }
1765};
1766
1767#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1768#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1769
1770static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1771{
1772 q->setup = EZ(t->setup * 1000, T);
1773 q->act8b = EZ(t->act8b * 1000, T);
1774 q->rec8b = EZ(t->rec8b * 1000, T);
1775 q->cyc8b = EZ(t->cyc8b * 1000, T);
1776 q->active = EZ(t->active * 1000, T);
1777 q->recover = EZ(t->recover * 1000, T);
1778 q->cycle = EZ(t->cycle * 1000, T);
1779 q->udma = EZ(t->udma * 1000, UT);
1780}
1781
1782void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1783 struct ata_timing *m, unsigned int what)
1784{
1785 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1786 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1787 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1788 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1789 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1790 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1791 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1792 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1793}
1794
1795static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1796{
1797 const struct ata_timing *t;
1798
1799 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001800 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001801 return NULL;
1802 return t;
1803}
1804
1805int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1806 struct ata_timing *t, int T, int UT)
1807{
1808 const struct ata_timing *s;
1809 struct ata_timing p;
1810
1811 /*
1812 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001813 */
Alan Cox452503f2005-10-21 19:01:32 -04001814
1815 if (!(s = ata_timing_find_mode(speed)))
1816 return -EINVAL;
1817
Albert Lee75b1f2f2005-11-16 17:06:18 +08001818 memcpy(t, s, sizeof(*s));
1819
Alan Cox452503f2005-10-21 19:01:32 -04001820 /*
1821 * If the drive is an EIDE drive, it can tell us it needs extended
1822 * PIO/MW_DMA cycle timing.
1823 */
1824
1825 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1826 memset(&p, 0, sizeof(p));
1827 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1828 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1829 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1830 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1831 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1832 }
1833 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1834 }
1835
1836 /*
1837 * Convert the timing to bus clock counts.
1838 */
1839
Albert Lee75b1f2f2005-11-16 17:06:18 +08001840 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001841
1842 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001843 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1844 * S.M.A.R.T * and some other commands. We have to ensure that the
1845 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001846 */
1847
1848 if (speed > XFER_PIO_4) {
1849 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1850 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1851 }
1852
1853 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001854 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001855 */
1856
1857 if (t->act8b + t->rec8b < t->cyc8b) {
1858 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1859 t->rec8b = t->cyc8b - t->act8b;
1860 }
1861
1862 if (t->active + t->recover < t->cycle) {
1863 t->active += (t->cycle - (t->active + t->recover)) / 2;
1864 t->recover = t->cycle - t->active;
1865 }
1866
1867 return 0;
1868}
1869
Jeff Garzik057ace52005-10-22 14:27:05 -04001870static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 unsigned int shift;
1872 u8 base;
1873} xfer_mode_classes[] = {
1874 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1875 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1876 { ATA_SHIFT_PIO, XFER_PIO_0 },
1877};
1878
Arjan van de Ven858119e2006-01-14 13:20:43 -08001879static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 int i;
1882
1883 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1884 if (xfer_mode_classes[i].shift == shift)
1885 return xfer_mode_classes[i].base;
1886
1887 return 0xff;
1888}
1889
1890static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1891{
1892 int ofs, idx;
1893 u8 base;
1894
1895 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1896 return;
1897
1898 if (dev->xfer_shift == ATA_SHIFT_PIO)
1899 dev->flags |= ATA_DFLAG_PIO;
1900
1901 ata_dev_set_xfermode(ap, dev);
1902
1903 base = base_from_shift(dev->xfer_shift);
1904 ofs = dev->xfer_mode - base;
1905 idx = ofs + dev->xfer_shift;
1906 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1907
1908 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1909 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1910
1911 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1912 ap->id, dev->devno, xfer_mode_str[idx]);
1913}
1914
1915static int ata_host_set_pio(struct ata_port *ap)
1916{
1917 unsigned int mask;
1918 int x, i;
1919 u8 base, xfer_mode;
1920
1921 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1922 x = fgb(mask);
1923 if (x < 0) {
1924 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1925 return -1;
1926 }
1927
1928 base = base_from_shift(ATA_SHIFT_PIO);
1929 xfer_mode = base + x;
1930
1931 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1932 (int)base, (int)xfer_mode, mask, x);
1933
1934 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1935 struct ata_device *dev = &ap->device[i];
1936 if (ata_dev_present(dev)) {
1937 dev->pio_mode = xfer_mode;
1938 dev->xfer_mode = xfer_mode;
1939 dev->xfer_shift = ATA_SHIFT_PIO;
1940 if (ap->ops->set_piomode)
1941 ap->ops->set_piomode(ap, dev);
1942 }
1943 }
1944
1945 return 0;
1946}
1947
1948static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1949 unsigned int xfer_shift)
1950{
1951 int i;
1952
1953 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1954 struct ata_device *dev = &ap->device[i];
1955 if (ata_dev_present(dev)) {
1956 dev->dma_mode = xfer_mode;
1957 dev->xfer_mode = xfer_mode;
1958 dev->xfer_shift = xfer_shift;
1959 if (ap->ops->set_dmamode)
1960 ap->ops->set_dmamode(ap, dev);
1961 }
1962 }
1963}
1964
1965/**
1966 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1967 * @ap: port on which timings will be programmed
1968 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001969 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1970 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001972 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 */
1974static void ata_set_mode(struct ata_port *ap)
1975{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001976 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 u8 xfer_mode;
1978 int rc;
1979
1980 /* step 1: always set host PIO timings */
1981 rc = ata_host_set_pio(ap);
1982 if (rc)
1983 goto err_out;
1984
1985 /* step 2: choose the best data xfer mode */
1986 xfer_mode = xfer_shift = 0;
1987 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1988 if (rc)
1989 goto err_out;
1990
1991 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1992 if (xfer_shift != ATA_SHIFT_PIO)
1993 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1994
1995 /* step 4: update devices' xfer mode */
1996 ata_dev_set_mode(ap, &ap->device[0]);
1997 ata_dev_set_mode(ap, &ap->device[1]);
1998
1999 if (ap->flags & ATA_FLAG_PORT_DISABLED)
2000 return;
2001
2002 if (ap->ops->post_set_mode)
2003 ap->ops->post_set_mode(ap);
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return;
2006
2007err_out:
2008 ata_port_disable(ap);
2009}
2010
2011/**
2012 * ata_busy_sleep - sleep until BSY clears, or timeout
2013 * @ap: port containing status register to be polled
2014 * @tmout_pat: impatience timeout
2015 * @tmout: overall timeout
2016 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002017 * Sleep until ATA Status register bit BSY clears,
2018 * or a timeout occurs.
2019 *
2020 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 */
2022
Tejun Heo6f8b9952006-01-24 17:05:21 +09002023unsigned int ata_busy_sleep (struct ata_port *ap,
2024 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
2026 unsigned long timer_start, timeout;
2027 u8 status;
2028
2029 status = ata_busy_wait(ap, ATA_BUSY, 300);
2030 timer_start = jiffies;
2031 timeout = timer_start + tmout_pat;
2032 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2033 msleep(50);
2034 status = ata_busy_wait(ap, ATA_BUSY, 3);
2035 }
2036
2037 if (status & ATA_BUSY)
2038 printk(KERN_WARNING "ata%u is slow to respond, "
2039 "please be patient\n", ap->id);
2040
2041 timeout = timer_start + tmout;
2042 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2043 msleep(50);
2044 status = ata_chk_status(ap);
2045 }
2046
2047 if (status & ATA_BUSY) {
2048 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
2049 ap->id, tmout / HZ);
2050 return 1;
2051 }
2052
2053 return 0;
2054}
2055
2056static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2057{
2058 struct ata_ioports *ioaddr = &ap->ioaddr;
2059 unsigned int dev0 = devmask & (1 << 0);
2060 unsigned int dev1 = devmask & (1 << 1);
2061 unsigned long timeout;
2062
2063 /* if device 0 was found in ata_devchk, wait for its
2064 * BSY bit to clear
2065 */
2066 if (dev0)
2067 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2068
2069 /* if device 1 was found in ata_devchk, wait for
2070 * register access, then wait for BSY to clear
2071 */
2072 timeout = jiffies + ATA_TMOUT_BOOT;
2073 while (dev1) {
2074 u8 nsect, lbal;
2075
2076 ap->ops->dev_select(ap, 1);
2077 if (ap->flags & ATA_FLAG_MMIO) {
2078 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2079 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2080 } else {
2081 nsect = inb(ioaddr->nsect_addr);
2082 lbal = inb(ioaddr->lbal_addr);
2083 }
2084 if ((nsect == 1) && (lbal == 1))
2085 break;
2086 if (time_after(jiffies, timeout)) {
2087 dev1 = 0;
2088 break;
2089 }
2090 msleep(50); /* give drive a breather */
2091 }
2092 if (dev1)
2093 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2094
2095 /* is all this really necessary? */
2096 ap->ops->dev_select(ap, 0);
2097 if (dev1)
2098 ap->ops->dev_select(ap, 1);
2099 if (dev0)
2100 ap->ops->dev_select(ap, 0);
2101}
2102
2103/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002104 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2105 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002107 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2108 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 *
2110 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002111 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05002112 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 *
2114 */
2115
2116static unsigned int ata_bus_edd(struct ata_port *ap)
2117{
2118 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05002119 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /* set up execute-device-diag (bus reset) taskfile */
2122 /* also, take interrupts to a known state (disabled) */
2123 DPRINTK("execute-device-diag\n");
2124 ata_tf_init(ap, &tf, 0);
2125 tf.ctl |= ATA_NIEN;
2126 tf.command = ATA_CMD_EDD;
2127 tf.protocol = ATA_PROT_NODATA;
2128
2129 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002130 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002132 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /* spec says at least 2ms. but who knows with those
2135 * crazy ATAPI devices...
2136 */
2137 msleep(150);
2138
2139 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2140}
2141
2142static unsigned int ata_bus_softreset(struct ata_port *ap,
2143 unsigned int devmask)
2144{
2145 struct ata_ioports *ioaddr = &ap->ioaddr;
2146
2147 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2148
2149 /* software reset. causes dev0 to be selected */
2150 if (ap->flags & ATA_FLAG_MMIO) {
2151 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2152 udelay(20); /* FIXME: flush */
2153 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2154 udelay(20); /* FIXME: flush */
2155 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2156 } else {
2157 outb(ap->ctl, ioaddr->ctl_addr);
2158 udelay(10);
2159 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2160 udelay(10);
2161 outb(ap->ctl, ioaddr->ctl_addr);
2162 }
2163
2164 /* spec mandates ">= 2ms" before checking status.
2165 * We wait 150ms, because that was the magic delay used for
2166 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2167 * between when the ATA command register is written, and then
2168 * status is checked. Because waiting for "a while" before
2169 * checking status is fine, post SRST, we perform this magic
2170 * delay here as well.
2171 */
2172 msleep(150);
2173
2174 ata_bus_post_reset(ap, devmask);
2175
2176 return 0;
2177}
2178
2179/**
2180 * ata_bus_reset - reset host port and associated ATA channel
2181 * @ap: port to reset
2182 *
2183 * This is typically the first time we actually start issuing
2184 * commands to the ATA channel. We wait for BSY to clear, then
2185 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2186 * result. Determine what devices, if any, are on the channel
2187 * by looking at the device 0/1 error register. Look at the signature
2188 * stored in each device's taskfile registers, to determine if
2189 * the device is ATA or ATAPI.
2190 *
2191 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002192 * PCI/etc. bus probe sem.
2193 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 *
2195 * SIDE EFFECTS:
2196 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2197 */
2198
2199void ata_bus_reset(struct ata_port *ap)
2200{
2201 struct ata_ioports *ioaddr = &ap->ioaddr;
2202 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2203 u8 err;
2204 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2205
2206 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2207
2208 /* determine if device 0/1 are present */
2209 if (ap->flags & ATA_FLAG_SATA_RESET)
2210 dev0 = 1;
2211 else {
2212 dev0 = ata_devchk(ap, 0);
2213 if (slave_possible)
2214 dev1 = ata_devchk(ap, 1);
2215 }
2216
2217 if (dev0)
2218 devmask |= (1 << 0);
2219 if (dev1)
2220 devmask |= (1 << 1);
2221
2222 /* select device 0 again */
2223 ap->ops->dev_select(ap, 0);
2224
2225 /* issue bus reset */
2226 if (ap->flags & ATA_FLAG_SRST)
2227 rc = ata_bus_softreset(ap, devmask);
2228 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2229 /* set up device control */
2230 if (ap->flags & ATA_FLAG_MMIO)
2231 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2232 else
2233 outb(ap->ctl, ioaddr->ctl_addr);
2234 rc = ata_bus_edd(ap);
2235 }
2236
2237 if (rc)
2238 goto err_out;
2239
2240 /*
2241 * determine by signature whether we have ATA or ATAPI devices
2242 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002243 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002245 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 /* re-enable interrupts */
2248 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2249 ata_irq_on(ap);
2250
2251 /* is double-select really necessary? */
2252 if (ap->device[1].class != ATA_DEV_NONE)
2253 ap->ops->dev_select(ap, 1);
2254 if (ap->device[0].class != ATA_DEV_NONE)
2255 ap->ops->dev_select(ap, 0);
2256
2257 /* if no devices were detected, disable this port */
2258 if ((ap->device[0].class == ATA_DEV_NONE) &&
2259 (ap->device[1].class == ATA_DEV_NONE))
2260 goto err_out;
2261
2262 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2263 /* set up device control for ATA_FLAG_SATA_RESET */
2264 if (ap->flags & ATA_FLAG_MMIO)
2265 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2266 else
2267 outb(ap->ctl, ioaddr->ctl_addr);
2268 }
2269
2270 DPRINTK("EXIT\n");
2271 return;
2272
2273err_out:
2274 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2275 ap->ops->port_disable(ap);
2276
2277 DPRINTK("EXIT\n");
2278}
2279
Tejun Heo7a7921e2006-02-02 18:20:00 +09002280static int sata_phy_resume(struct ata_port *ap)
2281{
2282 unsigned long timeout = jiffies + (HZ * 5);
2283 u32 sstatus;
2284
2285 scr_write_flush(ap, SCR_CONTROL, 0x300);
2286
2287 /* Wait for phy to become ready, if necessary. */
2288 do {
2289 msleep(200);
2290 sstatus = scr_read(ap, SCR_STATUS);
2291 if ((sstatus & 0xf) != 1)
2292 return 0;
2293 } while (time_before(jiffies, timeout));
2294
2295 return -1;
2296}
2297
Tejun Heoc2bd5802006-01-24 17:05:22 +09002298/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002299 * ata_std_probeinit - initialize probing
2300 * @ap: port to be probed
2301 *
2302 * @ap is about to be probed. Initialize it. This function is
2303 * to be used as standard callback for ata_drive_probe_reset().
2304 */
2305extern void ata_std_probeinit(struct ata_port *ap)
2306{
2307 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2308 sata_phy_resume(ap);
2309}
2310
2311/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002312 * ata_std_softreset - reset host port via ATA SRST
2313 * @ap: port to reset
2314 * @verbose: fail verbosely
2315 * @classes: resulting classes of attached devices
2316 *
2317 * Reset host port using ATA SRST. This function is to be used
2318 * as standard callback for ata_drive_*_reset() functions.
2319 *
2320 * LOCKING:
2321 * Kernel thread context (may sleep)
2322 *
2323 * RETURNS:
2324 * 0 on success, -errno otherwise.
2325 */
2326int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2327{
2328 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2329 unsigned int devmask = 0, err_mask;
2330 u8 err;
2331
2332 DPRINTK("ENTER\n");
2333
2334 /* determine if device 0/1 are present */
2335 if (ata_devchk(ap, 0))
2336 devmask |= (1 << 0);
2337 if (slave_possible && ata_devchk(ap, 1))
2338 devmask |= (1 << 1);
2339
2340 /* devchk reports device presence without actual device on
2341 * most SATA controllers. Check SStatus and turn devmask off
2342 * if link is offline. Note that we should continue resetting
2343 * even when it seems like there's no device.
2344 */
2345 if (ap->ops->scr_read && !sata_dev_present(ap))
2346 devmask = 0;
2347
2348 /* select device 0 again */
2349 ap->ops->dev_select(ap, 0);
2350
2351 /* issue bus reset */
2352 DPRINTK("about to softreset, devmask=%x\n", devmask);
2353 err_mask = ata_bus_softreset(ap, devmask);
2354 if (err_mask) {
2355 if (verbose)
2356 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2357 ap->id, err_mask);
2358 else
2359 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2360 err_mask);
2361 return -EIO;
2362 }
2363
2364 /* determine by signature whether we have ATA or ATAPI devices */
2365 classes[0] = ata_dev_try_classify(ap, 0, &err);
2366 if (slave_possible && err != 0x81)
2367 classes[1] = ata_dev_try_classify(ap, 1, &err);
2368
2369 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2370 return 0;
2371}
2372
2373/**
2374 * sata_std_hardreset - reset host port via SATA phy reset
2375 * @ap: port to reset
2376 * @verbose: fail verbosely
2377 * @class: resulting class of attached device
2378 *
2379 * SATA phy-reset host port using DET bits of SControl register.
2380 * This function is to be used as standard callback for
2381 * ata_drive_*_reset().
2382 *
2383 * LOCKING:
2384 * Kernel thread context (may sleep)
2385 *
2386 * RETURNS:
2387 * 0 on success, -errno otherwise.
2388 */
2389int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2390{
Tejun Heo7a7921e2006-02-02 18:20:00 +09002391 u32 serror;
Tejun Heoc2bd5802006-01-24 17:05:22 +09002392
2393 DPRINTK("ENTER\n");
2394
2395 /* Issue phy wake/reset */
2396 scr_write_flush(ap, SCR_CONTROL, 0x301);
2397
2398 /*
2399 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2400 * 10.4.2 says at least 1 ms.
2401 */
2402 msleep(1);
2403
Tejun Heo7a7921e2006-02-02 18:20:00 +09002404 /* Bring phy back */
2405 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002406
2407 /* Clear SError */
2408 serror = scr_read(ap, SCR_ERROR);
2409 scr_write(ap, SCR_ERROR, serror);
2410
2411 /* TODO: phy layer with polling, timeouts, etc. */
2412 if (!sata_dev_present(ap)) {
2413 *class = ATA_DEV_NONE;
2414 DPRINTK("EXIT, link offline\n");
2415 return 0;
2416 }
2417
2418 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2419 if (verbose)
2420 printk(KERN_ERR "ata%u: COMRESET failed "
2421 "(device not ready)\n", ap->id);
2422 else
2423 DPRINTK("EXIT, device not ready\n");
2424 return -EIO;
2425 }
2426
2427 *class = ata_dev_try_classify(ap, 0, NULL);
2428
2429 DPRINTK("EXIT, class=%u\n", *class);
2430 return 0;
2431}
2432
2433/**
2434 * ata_std_postreset - standard postreset callback
2435 * @ap: the target ata_port
2436 * @classes: classes of attached devices
2437 *
2438 * This function is invoked after a successful reset. Note that
2439 * the device might have been reset more than once using
2440 * different reset methods before postreset is invoked.
2441 * postreset is also reponsible for setting cable type.
2442 *
2443 * This function is to be used as standard callback for
2444 * ata_drive_*_reset().
2445 *
2446 * LOCKING:
2447 * Kernel thread context (may sleep)
2448 */
2449void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2450{
2451 DPRINTK("ENTER\n");
2452
2453 /* set cable type */
2454 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2455 ap->cbl = ATA_CBL_SATA;
2456
2457 /* print link status */
2458 if (ap->cbl == ATA_CBL_SATA)
2459 sata_print_link_status(ap);
2460
2461 /* bail out if no device is present */
2462 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2463 DPRINTK("EXIT, no device\n");
2464 return;
2465 }
2466
2467 /* is double-select really necessary? */
2468 if (classes[0] != ATA_DEV_NONE)
2469 ap->ops->dev_select(ap, 1);
2470 if (classes[1] != ATA_DEV_NONE)
2471 ap->ops->dev_select(ap, 0);
2472
2473 /* re-enable interrupts & set up device control */
2474 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2475 ata_irq_on(ap);
2476
2477 DPRINTK("EXIT\n");
2478}
2479
2480/**
2481 * ata_std_probe_reset - standard probe reset method
2482 * @ap: prot to perform probe-reset
2483 * @classes: resulting classes of attached devices
2484 *
2485 * The stock off-the-shelf ->probe_reset method.
2486 *
2487 * LOCKING:
2488 * Kernel thread context (may sleep)
2489 *
2490 * RETURNS:
2491 * 0 on success, -errno otherwise.
2492 */
2493int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2494{
2495 ata_reset_fn_t hardreset;
2496
2497 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002498 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002499 hardreset = sata_std_hardreset;
2500
Tejun Heo8a19ac82006-02-02 18:20:00 +09002501 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002502 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002503 ata_std_postreset, classes);
2504}
2505
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002506static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2507 ata_postreset_fn_t postreset,
2508 unsigned int *classes)
2509{
2510 int i, rc;
2511
2512 for (i = 0; i < ATA_MAX_DEVICES; i++)
2513 classes[i] = ATA_DEV_UNKNOWN;
2514
2515 rc = reset(ap, 0, classes);
2516 if (rc)
2517 return rc;
2518
2519 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2520 * is complete and convert all ATA_DEV_UNKNOWN to
2521 * ATA_DEV_NONE.
2522 */
2523 for (i = 0; i < ATA_MAX_DEVICES; i++)
2524 if (classes[i] != ATA_DEV_UNKNOWN)
2525 break;
2526
2527 if (i < ATA_MAX_DEVICES)
2528 for (i = 0; i < ATA_MAX_DEVICES; i++)
2529 if (classes[i] == ATA_DEV_UNKNOWN)
2530 classes[i] = ATA_DEV_NONE;
2531
2532 if (postreset)
2533 postreset(ap, classes);
2534
2535 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2536}
2537
2538/**
2539 * ata_drive_probe_reset - Perform probe reset with given methods
2540 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002541 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002542 * @softreset: softreset method (can be NULL)
2543 * @hardreset: hardreset method (can be NULL)
2544 * @postreset: postreset method (can be NULL)
2545 * @classes: resulting classes of attached devices
2546 *
2547 * Reset the specified port and classify attached devices using
2548 * given methods. This function prefers softreset but tries all
2549 * possible reset sequences to reset and classify devices. This
2550 * function is intended to be used for constructing ->probe_reset
2551 * callback by low level drivers.
2552 *
2553 * Reset methods should follow the following rules.
2554 *
2555 * - Return 0 on sucess, -errno on failure.
2556 * - If classification is supported, fill classes[] with
2557 * recognized class codes.
2558 * - If classification is not supported, leave classes[] alone.
2559 * - If verbose is non-zero, print error message on failure;
2560 * otherwise, shut up.
2561 *
2562 * LOCKING:
2563 * Kernel thread context (may sleep)
2564 *
2565 * RETURNS:
2566 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2567 * if classification fails, and any error code from reset
2568 * methods.
2569 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002570int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002571 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2572 ata_postreset_fn_t postreset, unsigned int *classes)
2573{
2574 int rc = -EINVAL;
2575
Tejun Heo7944ea92006-02-02 18:20:00 +09002576 if (probeinit)
2577 probeinit(ap);
2578
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002579 if (softreset) {
2580 rc = do_probe_reset(ap, softreset, postreset, classes);
2581 if (rc == 0)
2582 return 0;
2583 }
2584
2585 if (!hardreset)
2586 return rc;
2587
2588 rc = do_probe_reset(ap, hardreset, postreset, classes);
2589 if (rc == 0 || rc != -ENODEV)
2590 return rc;
2591
2592 if (softreset)
2593 rc = do_probe_reset(ap, softreset, postreset, classes);
2594
2595 return rc;
2596}
2597
Jeff Garzik057ace52005-10-22 14:27:05 -04002598static void ata_pr_blacklisted(const struct ata_port *ap,
2599 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2602 ap->id, dev->devno);
2603}
2604
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002605static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 "WDC AC11000H",
2607 "WDC AC22100H",
2608 "WDC AC32500H",
2609 "WDC AC33100H",
2610 "WDC AC31600H",
2611 "WDC AC32100H",
2612 "WDC AC23200L",
2613 "Compaq CRD-8241B",
2614 "CRD-8400B",
2615 "CRD-8480B",
2616 "CRD-8482B",
2617 "CRD-84",
2618 "SanDisk SDP3B",
2619 "SanDisk SDP3B-64",
2620 "SANYO CD-ROM CRD",
2621 "HITACHI CDR-8",
2622 "HITACHI CDR-8335",
2623 "HITACHI CDR-8435",
2624 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002625 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 "CD-532E-A",
2627 "E-IDE CD-ROM CR-840",
2628 "CD-ROM Drive/F5A",
2629 "WPI CDD-820",
2630 "SAMSUNG CD-ROM SC-148C",
2631 "SAMSUNG CD-ROM SC",
2632 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2634 "_NEC DV5800A",
2635};
2636
Jeff Garzik057ace52005-10-22 14:27:05 -04002637static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
2639 unsigned char model_num[40];
2640 char *s;
2641 unsigned int len;
2642 int i;
2643
2644 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2645 sizeof(model_num));
2646 s = &model_num[0];
2647 len = strnlen(s, sizeof(model_num));
2648
2649 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2650 while ((len > 0) && (s[len - 1] == ' ')) {
2651 len--;
2652 s[len] = 0;
2653 }
2654
2655 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2656 if (!strncmp(ata_dma_blacklist[i], s, len))
2657 return 1;
2658
2659 return 0;
2660}
2661
Jeff Garzik057ace52005-10-22 14:27:05 -04002662static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
Jeff Garzik057ace52005-10-22 14:27:05 -04002664 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 unsigned int mask;
2666
2667 master = &ap->device[0];
2668 slave = &ap->device[1];
2669
2670 assert (ata_dev_present(master) || ata_dev_present(slave));
2671
2672 if (shift == ATA_SHIFT_UDMA) {
2673 mask = ap->udma_mask;
2674 if (ata_dev_present(master)) {
2675 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002676 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 mask = 0;
2678 ata_pr_blacklisted(ap, master);
2679 }
2680 }
2681 if (ata_dev_present(slave)) {
2682 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002683 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 mask = 0;
2685 ata_pr_blacklisted(ap, slave);
2686 }
2687 }
2688 }
2689 else if (shift == ATA_SHIFT_MWDMA) {
2690 mask = ap->mwdma_mask;
2691 if (ata_dev_present(master)) {
2692 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002693 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 mask = 0;
2695 ata_pr_blacklisted(ap, master);
2696 }
2697 }
2698 if (ata_dev_present(slave)) {
2699 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002700 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 mask = 0;
2702 ata_pr_blacklisted(ap, slave);
2703 }
2704 }
2705 }
2706 else if (shift == ATA_SHIFT_PIO) {
2707 mask = ap->pio_mask;
2708 if (ata_dev_present(master)) {
2709 /* spec doesn't return explicit support for
2710 * PIO0-2, so we fake it
2711 */
2712 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2713 tmp_mode <<= 3;
2714 tmp_mode |= 0x7;
2715 mask &= tmp_mode;
2716 }
2717 if (ata_dev_present(slave)) {
2718 /* spec doesn't return explicit support for
2719 * PIO0-2, so we fake it
2720 */
2721 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2722 tmp_mode <<= 3;
2723 tmp_mode |= 0x7;
2724 mask &= tmp_mode;
2725 }
2726 }
2727 else {
2728 mask = 0xffffffff; /* shut up compiler warning */
2729 BUG();
2730 }
2731
2732 return mask;
2733}
2734
2735/* find greatest bit */
2736static int fgb(u32 bitmap)
2737{
2738 unsigned int i;
2739 int x = -1;
2740
2741 for (i = 0; i < 32; i++)
2742 if (bitmap & (1 << i))
2743 x = i;
2744
2745 return x;
2746}
2747
2748/**
2749 * ata_choose_xfer_mode - attempt to find best transfer mode
2750 * @ap: Port for which an xfer mode will be selected
2751 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2752 * @xfer_shift_out: (output) bit shift that selects this mode
2753 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002754 * Based on host and device capabilities, determine the
2755 * maximum transfer mode that is amenable to all.
2756 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002758 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 *
2760 * RETURNS:
2761 * Zero on success, negative on error.
2762 */
2763
Jeff Garzik057ace52005-10-22 14:27:05 -04002764static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 u8 *xfer_mode_out,
2766 unsigned int *xfer_shift_out)
2767{
2768 unsigned int mask, shift;
2769 int x, i;
2770
2771 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2772 shift = xfer_mode_classes[i].shift;
2773 mask = ata_get_mode_mask(ap, shift);
2774
2775 x = fgb(mask);
2776 if (x >= 0) {
2777 *xfer_mode_out = xfer_mode_classes[i].base + x;
2778 *xfer_shift_out = shift;
2779 return 0;
2780 }
2781 }
2782
2783 return -1;
2784}
2785
2786/**
2787 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2788 * @ap: Port associated with device @dev
2789 * @dev: Device to which command will be sent
2790 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002791 * Issue SET FEATURES - XFER MODE command to device @dev
2792 * on port @ap.
2793 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002795 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 */
2797
2798static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2799{
Tejun Heoa0123702005-12-13 14:49:31 +09002800 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 /* set up set-features taskfile */
2803 DPRINTK("set features - xfer mode\n");
2804
Tejun Heoa0123702005-12-13 14:49:31 +09002805 ata_tf_init(ap, &tf, dev->devno);
2806 tf.command = ATA_CMD_SET_FEATURES;
2807 tf.feature = SETFEATURES_XFER;
2808 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2809 tf.protocol = ATA_PROT_NODATA;
2810 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Tejun Heoa0123702005-12-13 14:49:31 +09002812 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2813 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2814 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 DPRINTK("EXIT\n");
2819}
2820
2821/**
Albert Lee59a10b12005-10-12 15:09:42 +08002822 * ata_dev_reread_id - Reread the device identify device info
2823 * @ap: port where the device is
2824 * @dev: device to reread the identify device info
2825 *
2826 * LOCKING:
2827 */
2828
2829static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2830{
Tejun Heoa0123702005-12-13 14:49:31 +09002831 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002832
Tejun Heoa0123702005-12-13 14:49:31 +09002833 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002834
2835 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002836 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002837 DPRINTK("do ATA identify\n");
2838 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002839 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002840 DPRINTK("do ATAPI identify\n");
2841 }
2842
Tejun Heoa0123702005-12-13 14:49:31 +09002843 tf.flags |= ATA_TFLAG_DEVICE;
2844 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002845
Tejun Heoa0123702005-12-13 14:49:31 +09002846 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2847 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002848 goto err_out;
2849
Albert Lee59a10b12005-10-12 15:09:42 +08002850 swap_buf_le16(dev->id, ATA_ID_WORDS);
2851
2852 ata_dump_id(dev);
2853
2854 DPRINTK("EXIT\n");
2855
2856 return;
2857err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002858 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002859 ata_port_disable(ap);
2860}
2861
2862/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002863 * ata_dev_init_params - Issue INIT DEV PARAMS command
2864 * @ap: Port associated with device @dev
2865 * @dev: Device to which command will be sent
2866 *
2867 * LOCKING:
2868 */
2869
2870static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2871{
Tejun Heoa0123702005-12-13 14:49:31 +09002872 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002873 u16 sectors = dev->id[6];
2874 u16 heads = dev->id[3];
2875
2876 /* Number of sectors per track 1-255. Number of heads 1-16 */
2877 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2878 return;
2879
2880 /* set up init dev params taskfile */
2881 DPRINTK("init dev params \n");
2882
Tejun Heoa0123702005-12-13 14:49:31 +09002883 ata_tf_init(ap, &tf, dev->devno);
2884 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2885 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2886 tf.protocol = ATA_PROT_NODATA;
2887 tf.nsect = sectors;
2888 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002889
Tejun Heoa0123702005-12-13 14:49:31 +09002890 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2891 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2892 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002893 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002894 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002895
2896 DPRINTK("EXIT\n");
2897}
2898
2899/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002900 * ata_sg_clean - Unmap DMA memory associated with command
2901 * @qc: Command containing DMA memory to be released
2902 *
2903 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 *
2905 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002906 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 */
2908
2909static void ata_sg_clean(struct ata_queued_cmd *qc)
2910{
2911 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002912 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002914 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
2916 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2917 assert(sg != NULL);
2918
2919 if (qc->flags & ATA_QCFLAG_SINGLE)
2920 assert(qc->n_elem == 1);
2921
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002922 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002924 /* if we padded the buffer out to 32-bit bound, and data
2925 * xfer direction is from-device, we must copy from the
2926 * pad buffer back into the supplied buffer
2927 */
2928 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2929 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2930
2931 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002932 if (qc->n_elem)
2933 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002934 /* restore last sg */
2935 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2936 if (pad_buf) {
2937 struct scatterlist *psg = &qc->pad_sgent;
2938 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2939 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002940 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002941 }
2942 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002943 if (sg_dma_len(&sg[0]) > 0)
2944 dma_unmap_single(ap->host_set->dev,
2945 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2946 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002947 /* restore sg */
2948 sg->length += qc->pad_len;
2949 if (pad_buf)
2950 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2951 pad_buf, qc->pad_len);
2952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002955 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}
2957
2958/**
2959 * ata_fill_sg - Fill PCI IDE PRD table
2960 * @qc: Metadata associated with taskfile to be transferred
2961 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002962 * Fill PCI IDE PRD (scatter-gather) table with segments
2963 * associated with the current disk command.
2964 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002966 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 *
2968 */
2969static void ata_fill_sg(struct ata_queued_cmd *qc)
2970{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002972 struct scatterlist *sg;
2973 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002975 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 assert(qc->n_elem > 0);
2977
2978 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002979 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 u32 addr, offset;
2981 u32 sg_len, len;
2982
2983 /* determine if physical DMA addr spans 64K boundary.
2984 * Note h/w doesn't support 64-bit, so we unconditionally
2985 * truncate dma_addr_t to u32.
2986 */
2987 addr = (u32) sg_dma_address(sg);
2988 sg_len = sg_dma_len(sg);
2989
2990 while (sg_len) {
2991 offset = addr & 0xffff;
2992 len = sg_len;
2993 if ((offset + sg_len) > 0x10000)
2994 len = 0x10000 - offset;
2995
2996 ap->prd[idx].addr = cpu_to_le32(addr);
2997 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2998 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2999
3000 idx++;
3001 sg_len -= len;
3002 addr += len;
3003 }
3004 }
3005
3006 if (idx)
3007 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3008}
3009/**
3010 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3011 * @qc: Metadata associated with taskfile to check
3012 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003013 * Allow low-level driver to filter ATA PACKET commands, returning
3014 * a status indicating whether or not it is OK to use DMA for the
3015 * supplied PACKET command.
3016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003018 * spin_lock_irqsave(host_set lock)
3019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 * RETURNS: 0 when ATAPI DMA can be used
3021 * nonzero otherwise
3022 */
3023int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3024{
3025 struct ata_port *ap = qc->ap;
3026 int rc = 0; /* Assume ATAPI DMA is OK by default */
3027
3028 if (ap->ops->check_atapi_dma)
3029 rc = ap->ops->check_atapi_dma(qc);
3030
3031 return rc;
3032}
3033/**
3034 * ata_qc_prep - Prepare taskfile for submission
3035 * @qc: Metadata associated with taskfile to be prepared
3036 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003037 * Prepare ATA taskfile for submission.
3038 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 * LOCKING:
3040 * spin_lock_irqsave(host_set lock)
3041 */
3042void ata_qc_prep(struct ata_queued_cmd *qc)
3043{
3044 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3045 return;
3046
3047 ata_fill_sg(qc);
3048}
3049
Jeff Garzik0cba6322005-05-30 19:49:12 -04003050/**
3051 * ata_sg_init_one - Associate command with memory buffer
3052 * @qc: Command to be associated
3053 * @buf: Memory buffer
3054 * @buflen: Length of memory buffer, in bytes.
3055 *
3056 * Initialize the data-related elements of queued_cmd @qc
3057 * to point to a single memory buffer, @buf of byte length @buflen.
3058 *
3059 * LOCKING:
3060 * spin_lock_irqsave(host_set lock)
3061 */
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3064{
3065 struct scatterlist *sg;
3066
3067 qc->flags |= ATA_QCFLAG_SINGLE;
3068
3069 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003070 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003072 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 qc->buf_virt = buf;
3074
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003075 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05003076 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
Jeff Garzik0cba6322005-05-30 19:49:12 -04003079/**
3080 * ata_sg_init - Associate command with scatter-gather table.
3081 * @qc: Command to be associated
3082 * @sg: Scatter-gather table.
3083 * @n_elem: Number of elements in s/g table.
3084 *
3085 * Initialize the data-related elements of queued_cmd @qc
3086 * to point to a scatter-gather table @sg, containing @n_elem
3087 * elements.
3088 *
3089 * LOCKING:
3090 * spin_lock_irqsave(host_set lock)
3091 */
3092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3094 unsigned int n_elem)
3095{
3096 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003097 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003099 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
3102/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003103 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3104 * @qc: Command with memory buffer to be mapped.
3105 *
3106 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 *
3108 * LOCKING:
3109 * spin_lock_irqsave(host_set lock)
3110 *
3111 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003112 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 */
3114
3115static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3116{
3117 struct ata_port *ap = qc->ap;
3118 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003119 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 dma_addr_t dma_address;
3121
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003122 /* we must lengthen transfers to end on a 32-bit boundary */
3123 qc->pad_len = sg->length & 3;
3124 if (qc->pad_len) {
3125 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3126 struct scatterlist *psg = &qc->pad_sgent;
3127
3128 assert(qc->dev->class == ATA_DEV_ATAPI);
3129
3130 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3131
3132 if (qc->tf.flags & ATA_TFLAG_WRITE)
3133 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3134 qc->pad_len);
3135
3136 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3137 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3138 /* trim sg */
3139 sg->length -= qc->pad_len;
3140
3141 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3142 sg->length, qc->pad_len);
3143 }
3144
Jeff Garzike1410f22005-11-14 14:06:26 -05003145 if (!sg->length) {
3146 sg_dma_address(sg) = 0;
3147 goto skip_map;
3148 }
3149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003151 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003152 if (dma_mapping_error(dma_address)) {
3153 /* restore sg */
3154 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05003159skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04003160 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3163 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3164
3165 return 0;
3166}
3167
3168/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003169 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3170 * @qc: Command with scatter-gather table to be mapped.
3171 *
3172 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 *
3174 * LOCKING:
3175 * spin_lock_irqsave(host_set lock)
3176 *
3177 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003178 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 *
3180 */
3181
3182static int ata_sg_setup(struct ata_queued_cmd *qc)
3183{
3184 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003185 struct scatterlist *sg = qc->__sg;
3186 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003187 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189 VPRINTK("ENTER, ata%u\n", ap->id);
3190 assert(qc->flags & ATA_QCFLAG_SG);
3191
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003192 /* we must lengthen transfers to end on a 32-bit boundary */
3193 qc->pad_len = lsg->length & 3;
3194 if (qc->pad_len) {
3195 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3196 struct scatterlist *psg = &qc->pad_sgent;
3197 unsigned int offset;
3198
3199 assert(qc->dev->class == ATA_DEV_ATAPI);
3200
3201 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3202
3203 /*
3204 * psg->page/offset are used to copy to-be-written
3205 * data in this function or read data in ata_sg_clean.
3206 */
3207 offset = lsg->offset + lsg->length - qc->pad_len;
3208 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3209 psg->offset = offset_in_page(offset);
3210
3211 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3212 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3213 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003214 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003215 }
3216
3217 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3218 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3219 /* trim last sg */
3220 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003221 if (lsg->length == 0)
3222 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003223
3224 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3225 qc->n_elem - 1, lsg->length, qc->pad_len);
3226 }
3227
Jeff Garzike1410f22005-11-14 14:06:26 -05003228 pre_n_elem = qc->n_elem;
3229 if (trim_sg && pre_n_elem)
3230 pre_n_elem--;
3231
3232 if (!pre_n_elem) {
3233 n_elem = 0;
3234 goto skip_map;
3235 }
3236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05003238 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003239 if (n_elem < 1) {
3240 /* restore last sg */
3241 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 DPRINTK("%d sg elements mapped\n", n_elem);
3246
Jeff Garzike1410f22005-11-14 14:06:26 -05003247skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 qc->n_elem = n_elem;
3249
3250 return 0;
3251}
3252
3253/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003254 * ata_poll_qc_complete - turn irq back on and finish qc
3255 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003256 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003257 *
3258 * LOCKING:
3259 * None. (grabs host lock)
3260 */
3261
Albert Leea22e2eb2005-12-05 15:38:02 +08003262void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003263{
3264 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003265 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003266
Jeff Garzikb8f61532005-08-25 22:01:20 -04003267 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003268 ap->flags &= ~ATA_FLAG_NOINTR;
3269 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003270 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003271 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003272}
3273
3274/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003275 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003276 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 *
3278 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003279 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 *
3281 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003282 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 */
3284
3285static unsigned long ata_pio_poll(struct ata_port *ap)
3286{
Albert Leec14b8332005-12-05 15:36:08 +08003287 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003289 unsigned int poll_state = HSM_ST_UNKNOWN;
3290 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Albert Leec14b8332005-12-05 15:36:08 +08003292 qc = ata_qc_from_tag(ap, ap->active_tag);
3293 assert(qc != NULL);
3294
Albert Lee14be71f2005-09-27 17:36:35 +08003295 switch (ap->hsm_task_state) {
3296 case HSM_ST:
3297 case HSM_ST_POLL:
3298 poll_state = HSM_ST_POLL;
3299 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003301 case HSM_ST_LAST:
3302 case HSM_ST_LAST_POLL:
3303 poll_state = HSM_ST_LAST_POLL;
3304 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 break;
3306 default:
3307 BUG();
3308 break;
3309 }
3310
3311 status = ata_chk_status(ap);
3312 if (status & ATA_BUSY) {
3313 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003314 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003315 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 return 0;
3317 }
Albert Lee14be71f2005-09-27 17:36:35 +08003318 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 return ATA_SHORT_PAUSE;
3320 }
3321
Albert Lee14be71f2005-09-27 17:36:35 +08003322 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 return 0;
3324}
3325
3326/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003327 * ata_pio_complete - check if drive is busy or idle
3328 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 *
3330 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003331 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003332 *
3333 * RETURNS:
3334 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 */
3336
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003337static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 struct ata_queued_cmd *qc;
3340 u8 drv_stat;
3341
3342 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003343 * This is purely heuristic. This is a fast path. Sometimes when
3344 * we enter, BSY will be cleared in a chk-status or two. If not,
3345 * the drive is probably seeking or something. Snooze for a couple
3346 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003347 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 */
Albert Leefe79e682005-12-06 11:34:59 +08003349 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3350 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003352 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3353 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003354 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 }
3358 }
3359
Albert Leec14b8332005-12-05 15:36:08 +08003360 qc = ata_qc_from_tag(ap, ap->active_tag);
3361 assert(qc != NULL);
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 drv_stat = ata_wait_idle(ap);
3364 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003365 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003366 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
3369
Albert Lee14be71f2005-09-27 17:36:35 +08003370 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Albert Leea22e2eb2005-12-05 15:38:02 +08003372 assert(qc->err_mask == 0);
3373 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003374
3375 /* another command may start at this point */
3376
3377 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
Edward Falk0baab862005-06-02 18:17:13 -04003380
3381/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003382 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003383 * @buf: Buffer to swap
3384 * @buf_words: Number of 16-bit words in buffer.
3385 *
3386 * Swap halves of 16-bit words if needed to convert from
3387 * little-endian byte order to native cpu byte order, or
3388 * vice-versa.
3389 *
3390 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003391 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393void swap_buf_le16(u16 *buf, unsigned int buf_words)
3394{
3395#ifdef __BIG_ENDIAN
3396 unsigned int i;
3397
3398 for (i = 0; i < buf_words; i++)
3399 buf[i] = le16_to_cpu(buf[i]);
3400#endif /* __BIG_ENDIAN */
3401}
3402
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003403/**
3404 * ata_mmio_data_xfer - Transfer data by MMIO
3405 * @ap: port to read/write
3406 * @buf: data buffer
3407 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003408 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003409 *
3410 * Transfer data from/to the device data register by MMIO.
3411 *
3412 * LOCKING:
3413 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003414 */
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3417 unsigned int buflen, int write_data)
3418{
3419 unsigned int i;
3420 unsigned int words = buflen >> 1;
3421 u16 *buf16 = (u16 *) buf;
3422 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3423
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003424 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 if (write_data) {
3426 for (i = 0; i < words; i++)
3427 writew(le16_to_cpu(buf16[i]), mmio);
3428 } else {
3429 for (i = 0; i < words; i++)
3430 buf16[i] = cpu_to_le16(readw(mmio));
3431 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003432
3433 /* Transfer trailing 1 byte, if any. */
3434 if (unlikely(buflen & 0x01)) {
3435 u16 align_buf[1] = { 0 };
3436 unsigned char *trailing_buf = buf + buflen - 1;
3437
3438 if (write_data) {
3439 memcpy(align_buf, trailing_buf, 1);
3440 writew(le16_to_cpu(align_buf[0]), mmio);
3441 } else {
3442 align_buf[0] = cpu_to_le16(readw(mmio));
3443 memcpy(trailing_buf, align_buf, 1);
3444 }
3445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446}
3447
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003448/**
3449 * ata_pio_data_xfer - Transfer data by PIO
3450 * @ap: port to read/write
3451 * @buf: data buffer
3452 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003453 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003454 *
3455 * Transfer data from/to the device data register by PIO.
3456 *
3457 * LOCKING:
3458 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003459 */
3460
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3462 unsigned int buflen, int write_data)
3463{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003464 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003466 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003468 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003470 insw(ap->ioaddr.data_addr, buf, words);
3471
3472 /* Transfer trailing 1 byte, if any. */
3473 if (unlikely(buflen & 0x01)) {
3474 u16 align_buf[1] = { 0 };
3475 unsigned char *trailing_buf = buf + buflen - 1;
3476
3477 if (write_data) {
3478 memcpy(align_buf, trailing_buf, 1);
3479 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3480 } else {
3481 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3482 memcpy(trailing_buf, align_buf, 1);
3483 }
3484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003487/**
3488 * ata_data_xfer - Transfer data from/to the data register.
3489 * @ap: port to read/write
3490 * @buf: data buffer
3491 * @buflen: buffer length
3492 * @do_write: read/write
3493 *
3494 * Transfer data from/to the device data register.
3495 *
3496 * LOCKING:
3497 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003498 */
3499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3501 unsigned int buflen, int do_write)
3502{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003503 /* Make the crap hardware pay the costs not the good stuff */
3504 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3505 unsigned long flags;
3506 local_irq_save(flags);
3507 if (ap->flags & ATA_FLAG_MMIO)
3508 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3509 else
3510 ata_pio_data_xfer(ap, buf, buflen, do_write);
3511 local_irq_restore(flags);
3512 } else {
3513 if (ap->flags & ATA_FLAG_MMIO)
3514 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3515 else
3516 ata_pio_data_xfer(ap, buf, buflen, do_write);
3517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518}
3519
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003520/**
3521 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3522 * @qc: Command on going
3523 *
3524 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3525 *
3526 * LOCKING:
3527 * Inherited from caller.
3528 */
3529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530static void ata_pio_sector(struct ata_queued_cmd *qc)
3531{
3532 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003533 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 struct ata_port *ap = qc->ap;
3535 struct page *page;
3536 unsigned int offset;
3537 unsigned char *buf;
3538
3539 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003540 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 page = sg[qc->cursg].page;
3543 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3544
3545 /* get the current page and offset */
3546 page = nth_page(page, (offset >> PAGE_SHIFT));
3547 offset %= PAGE_SIZE;
3548
3549 buf = kmap(page) + offset;
3550
3551 qc->cursect++;
3552 qc->cursg_ofs++;
3553
Albert Lee32529e02005-05-26 03:49:42 -04003554 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 qc->cursg++;
3556 qc->cursg_ofs = 0;
3557 }
3558
3559 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3560
3561 /* do the actual data transfer */
3562 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3563 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3564
3565 kunmap(page);
3566}
3567
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003568/**
3569 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3570 * @qc: Command on going
3571 * @bytes: number of bytes
3572 *
3573 * Transfer Transfer data from/to the ATAPI device.
3574 *
3575 * LOCKING:
3576 * Inherited from caller.
3577 *
3578 */
3579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3581{
3582 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003583 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 struct ata_port *ap = qc->ap;
3585 struct page *page;
3586 unsigned char *buf;
3587 unsigned int offset, count;
3588
Albert Lee563a6e12005-08-12 14:17:50 +08003589 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003590 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
3592next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003593 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003594 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003595 * The end of qc->sg is reached and the device expects
3596 * more data to transfer. In order not to overrun qc->sg
3597 * and fulfill length specified in the byte count register,
3598 * - for read case, discard trailing data from the device
3599 * - for write case, padding zero data to the device
3600 */
3601 u16 pad_buf[1] = { 0 };
3602 unsigned int words = bytes >> 1;
3603 unsigned int i;
3604
3605 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003606 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003607 ap->id, bytes);
3608
3609 for (i = 0; i < words; i++)
3610 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3611
Albert Lee14be71f2005-09-27 17:36:35 +08003612 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003613 return;
3614 }
3615
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003616 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 page = sg->page;
3619 offset = sg->offset + qc->cursg_ofs;
3620
3621 /* get the current page and offset */
3622 page = nth_page(page, (offset >> PAGE_SHIFT));
3623 offset %= PAGE_SIZE;
3624
Albert Lee6952df02005-06-06 15:56:03 +08003625 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003626 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
3628 /* don't cross page boundaries */
3629 count = min(count, (unsigned int)PAGE_SIZE - offset);
3630
3631 buf = kmap(page) + offset;
3632
3633 bytes -= count;
3634 qc->curbytes += count;
3635 qc->cursg_ofs += count;
3636
Albert Lee32529e02005-05-26 03:49:42 -04003637 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 qc->cursg++;
3639 qc->cursg_ofs = 0;
3640 }
3641
3642 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3643
3644 /* do the actual data transfer */
3645 ata_data_xfer(ap, buf, count, do_write);
3646
3647 kunmap(page);
3648
Albert Lee563a6e12005-08-12 14:17:50 +08003649 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
3652
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003653/**
3654 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3655 * @qc: Command on going
3656 *
3657 * Transfer Transfer data from/to the ATAPI device.
3658 *
3659 * LOCKING:
3660 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003661 */
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3664{
3665 struct ata_port *ap = qc->ap;
3666 struct ata_device *dev = qc->dev;
3667 unsigned int ireason, bc_lo, bc_hi, bytes;
3668 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3669
3670 ap->ops->tf_read(ap, &qc->tf);
3671 ireason = qc->tf.nsect;
3672 bc_lo = qc->tf.lbam;
3673 bc_hi = qc->tf.lbah;
3674 bytes = (bc_hi << 8) | bc_lo;
3675
3676 /* shall be cleared to zero, indicating xfer of data */
3677 if (ireason & (1 << 0))
3678 goto err_out;
3679
3680 /* make sure transfer direction matches expected */
3681 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3682 if (do_write != i_write)
3683 goto err_out;
3684
3685 __atapi_pio_bytes(qc, bytes);
3686
3687 return;
3688
3689err_out:
3690 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3691 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003692 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003693 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694}
3695
3696/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003697 * ata_pio_block - start PIO on a block
3698 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 *
3700 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003701 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 */
3703
3704static void ata_pio_block(struct ata_port *ap)
3705{
3706 struct ata_queued_cmd *qc;
3707 u8 status;
3708
3709 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003710 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 * Sometimes when we enter, BSY will be cleared in
3712 * a chk-status or two. If not, the drive is probably seeking
3713 * or something. Snooze for a couple msecs, then
3714 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003715 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 */
3717 status = ata_busy_wait(ap, ATA_BUSY, 5);
3718 if (status & ATA_BUSY) {
3719 msleep(2);
3720 status = ata_busy_wait(ap, ATA_BUSY, 10);
3721 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003722 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3724 return;
3725 }
3726 }
3727
3728 qc = ata_qc_from_tag(ap, ap->active_tag);
3729 assert(qc != NULL);
3730
Albert Leefe79e682005-12-06 11:34:59 +08003731 /* check error */
3732 if (status & (ATA_ERR | ATA_DF)) {
3733 qc->err_mask |= AC_ERR_DEV;
3734 ap->hsm_task_state = HSM_ST_ERR;
3735 return;
3736 }
3737
3738 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003740 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003742 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return;
3744 }
3745
3746 atapi_pio_bytes(qc);
3747 } else {
3748 /* handle BSY=0, DRQ=0 as error */
3749 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003750 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003751 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 return;
3753 }
3754
3755 ata_pio_sector(qc);
3756 }
3757}
3758
3759static void ata_pio_error(struct ata_port *ap)
3760{
3761 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003762
3763 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
3765 qc = ata_qc_from_tag(ap, ap->active_tag);
3766 assert(qc != NULL);
3767
Albert Lee1c848982005-12-05 15:40:15 +08003768 /* make sure qc->err_mask is available to
3769 * know what's wrong and recover
3770 */
3771 assert(qc->err_mask);
3772
Albert Lee14be71f2005-09-27 17:36:35 +08003773 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Albert Leea22e2eb2005-12-05 15:38:02 +08003775 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776}
3777
3778static void ata_pio_task(void *_data)
3779{
3780 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003781 unsigned long timeout;
3782 int qc_completed;
3783
3784fsm_start:
3785 timeout = 0;
3786 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Albert Lee14be71f2005-09-27 17:36:35 +08003788 switch (ap->hsm_task_state) {
3789 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return;
3791
Albert Lee14be71f2005-09-27 17:36:35 +08003792 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 ata_pio_block(ap);
3794 break;
3795
Albert Lee14be71f2005-09-27 17:36:35 +08003796 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003797 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 break;
3799
Albert Lee14be71f2005-09-27 17:36:35 +08003800 case HSM_ST_POLL:
3801 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 timeout = ata_pio_poll(ap);
3803 break;
3804
Albert Lee14be71f2005-09-27 17:36:35 +08003805 case HSM_ST_TMOUT:
3806 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 ata_pio_error(ap);
3808 return;
3809 }
3810
3811 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003812 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003813 else if (!qc_completed)
3814 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815}
3816
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817/**
3818 * ata_qc_timeout - Handle timeout of queued command
3819 * @qc: Command that timed out
3820 *
3821 * Some part of the kernel (currently, only the SCSI layer)
3822 * has noticed that the active command on port @ap has not
3823 * completed after a specified length of time. Handle this
3824 * condition by disabling DMA (if necessary) and completing
3825 * transactions, with error if necessary.
3826 *
3827 * This also handles the case of the "lost interrupt", where
3828 * for some reason (possibly hardware bug, possibly driver bug)
3829 * an interrupt was not delivered to the driver, even though the
3830 * transaction completed successfully.
3831 *
3832 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003833 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 */
3835
3836static void ata_qc_timeout(struct ata_queued_cmd *qc)
3837{
3838 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003839 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003841 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
3843 DPRINTK("ENTER\n");
3844
Tejun Heoc18d06f2006-02-02 00:56:10 +09003845 ata_flush_pio_tasks(ap);
3846 ap->hsm_task_state = HSM_ST_IDLE;
3847
Jeff Garzikb8f61532005-08-25 22:01:20 -04003848 spin_lock_irqsave(&host_set->lock, flags);
3849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 switch (qc->tf.protocol) {
3851
3852 case ATA_PROT_DMA:
3853 case ATA_PROT_ATAPI_DMA:
3854 host_stat = ap->ops->bmdma_status(ap);
3855
3856 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003857 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
3859 /* fall through */
3860
3861 default:
3862 ata_altstatus(ap);
3863 drv_stat = ata_chk_status(ap);
3864
3865 /* ack bmdma irq events */
3866 ap->ops->irq_clear(ap);
3867
3868 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3869 ap->id, qc->tf.command, drv_stat, host_stat);
3870
3871 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003872 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 break;
3874 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003875
3876 spin_unlock_irqrestore(&host_set->lock, flags);
3877
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003878 ata_eh_qc_complete(qc);
3879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 DPRINTK("EXIT\n");
3881}
3882
3883/**
3884 * ata_eng_timeout - Handle timeout of queued command
3885 * @ap: Port on which timed-out command is active
3886 *
3887 * Some part of the kernel (currently, only the SCSI layer)
3888 * has noticed that the active command on port @ap has not
3889 * completed after a specified length of time. Handle this
3890 * condition by disabling DMA (if necessary) and completing
3891 * transactions, with error if necessary.
3892 *
3893 * This also handles the case of the "lost interrupt", where
3894 * for some reason (possibly hardware bug, possibly driver bug)
3895 * an interrupt was not delivered to the driver, even though the
3896 * transaction completed successfully.
3897 *
3898 * LOCKING:
3899 * Inherited from SCSI layer (none, can sleep)
3900 */
3901
3902void ata_eng_timeout(struct ata_port *ap)
3903{
3904 struct ata_queued_cmd *qc;
3905
3906 DPRINTK("ENTER\n");
3907
3908 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003909 if (qc)
3910 ata_qc_timeout(qc);
3911 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3913 ap->id);
3914 goto out;
3915 }
3916
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917out:
3918 DPRINTK("EXIT\n");
3919}
3920
3921/**
3922 * ata_qc_new - Request an available ATA command, for queueing
3923 * @ap: Port associated with device @dev
3924 * @dev: Device from whom we request an available command structure
3925 *
3926 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003927 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 */
3929
3930static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3931{
3932 struct ata_queued_cmd *qc = NULL;
3933 unsigned int i;
3934
3935 for (i = 0; i < ATA_MAX_QUEUE; i++)
3936 if (!test_and_set_bit(i, &ap->qactive)) {
3937 qc = ata_qc_from_tag(ap, i);
3938 break;
3939 }
3940
3941 if (qc)
3942 qc->tag = i;
3943
3944 return qc;
3945}
3946
3947/**
3948 * ata_qc_new_init - Request an available ATA command, and initialize it
3949 * @ap: Port associated with device @dev
3950 * @dev: Device from whom we request an available command structure
3951 *
3952 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003953 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 */
3955
3956struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3957 struct ata_device *dev)
3958{
3959 struct ata_queued_cmd *qc;
3960
3961 qc = ata_qc_new(ap);
3962 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 qc->scsicmd = NULL;
3964 qc->ap = ap;
3965 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003967 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 }
3969
3970 return qc;
3971}
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973/**
3974 * ata_qc_free - free unused ata_queued_cmd
3975 * @qc: Command to complete
3976 *
3977 * Designed to free unused ata_queued_cmd object
3978 * in case something prevents using it.
3979 *
3980 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003981 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 */
3983void ata_qc_free(struct ata_queued_cmd *qc)
3984{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003985 struct ata_port *ap = qc->ap;
3986 unsigned int tag;
3987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Tejun Heo4ba946e2006-01-23 13:09:36 +09003990 qc->flags = 0;
3991 tag = qc->tag;
3992 if (likely(ata_tag_valid(tag))) {
3993 if (tag == ap->active_tag)
3994 ap->active_tag = ATA_TAG_POISON;
3995 qc->tag = ATA_TAG_POISON;
3996 clear_bit(tag, &ap->qactive);
3997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998}
3999
4000/**
4001 * ata_qc_complete - Complete an active ATA command
4002 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08004003 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04004004 *
4005 * Indicate to the mid and upper layers that an ATA
4006 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 *
4008 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004009 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 */
4011
Albert Leea22e2eb2005-12-05 15:38:02 +08004012void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
4015 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4016
4017 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4018 ata_sg_clean(qc);
4019
Albert Lee3f3791d2005-08-16 14:25:38 +08004020 /* atapi: mark qc as inactive to prevent the interrupt handler
4021 * from completing the command twice later, before the error handler
4022 * is called. (when rc != 0 and atapi request sense is needed)
4023 */
4024 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4025
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004027 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028}
4029
4030static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4031{
4032 struct ata_port *ap = qc->ap;
4033
4034 switch (qc->tf.protocol) {
4035 case ATA_PROT_DMA:
4036 case ATA_PROT_ATAPI_DMA:
4037 return 1;
4038
4039 case ATA_PROT_ATAPI:
4040 case ATA_PROT_PIO:
4041 case ATA_PROT_PIO_MULT:
4042 if (ap->flags & ATA_FLAG_PIO_DMA)
4043 return 1;
4044
4045 /* fall through */
4046
4047 default:
4048 return 0;
4049 }
4050
4051 /* never reached */
4052}
4053
4054/**
4055 * ata_qc_issue - issue taskfile to device
4056 * @qc: command to issue to device
4057 *
4058 * Prepare an ATA command to submission to device.
4059 * This includes mapping the data into a DMA-able
4060 * area, filling in the S/G table, and finally
4061 * writing the taskfile to hardware, starting the command.
4062 *
4063 * LOCKING:
4064 * spin_lock_irqsave(host_set lock)
4065 *
4066 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004067 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 */
4069
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004070unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
4072 struct ata_port *ap = qc->ap;
4073
4074 if (ata_should_dma_map(qc)) {
4075 if (qc->flags & ATA_QCFLAG_SG) {
4076 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004077 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4079 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004080 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 }
4082 } else {
4083 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4084 }
4085
4086 ap->ops->qc_prep(qc);
4087
4088 qc->ap->active_tag = qc->tag;
4089 qc->flags |= ATA_QCFLAG_ACTIVE;
4090
4091 return ap->ops->qc_issue(qc);
4092
Tejun Heo8e436af2006-01-23 13:09:36 +09004093sg_err:
4094 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004095 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096}
4097
Edward Falk0baab862005-06-02 18:17:13 -04004098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099/**
4100 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4101 * @qc: command to issue to device
4102 *
4103 * Using various libata functions and hooks, this function
4104 * starts an ATA command. ATA commands are grouped into
4105 * classes called "protocols", and issuing each type of protocol
4106 * is slightly different.
4107 *
Edward Falk0baab862005-06-02 18:17:13 -04004108 * May be used as the qc_issue() entry in ata_port_operations.
4109 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 * LOCKING:
4111 * spin_lock_irqsave(host_set lock)
4112 *
4113 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004114 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 */
4116
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004117unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
4119 struct ata_port *ap = qc->ap;
4120
4121 ata_dev_select(ap, qc->dev->devno, 1, 0);
4122
4123 switch (qc->tf.protocol) {
4124 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05004125 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 break;
4127
4128 case ATA_PROT_DMA:
4129 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4130 ap->ops->bmdma_setup(qc); /* set up bmdma */
4131 ap->ops->bmdma_start(qc); /* initiate bmdma */
4132 break;
4133
4134 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4135 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004136 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08004137 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004138 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 break;
4140
4141 case ATA_PROT_ATAPI:
4142 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004143 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09004144 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 break;
4146
4147 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09004148 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05004149 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09004150 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 break;
4152
4153 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09004154 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4156 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09004157 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 break;
4159
4160 default:
4161 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004162 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 }
4164
4165 return 0;
4166}
4167
4168/**
Edward Falk0baab862005-06-02 18:17:13 -04004169 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 * @qc: Info associated with this ATA transaction.
4171 *
4172 * LOCKING:
4173 * spin_lock_irqsave(host_set lock)
4174 */
4175
4176static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4177{
4178 struct ata_port *ap = qc->ap;
4179 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4180 u8 dmactl;
4181 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4182
4183 /* load PRD table addr. */
4184 mb(); /* make sure PRD table writes are visible to controller */
4185 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4186
4187 /* specify data direction, triple-check start bit is clear */
4188 dmactl = readb(mmio + ATA_DMA_CMD);
4189 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4190 if (!rw)
4191 dmactl |= ATA_DMA_WR;
4192 writeb(dmactl, mmio + ATA_DMA_CMD);
4193
4194 /* issue r/w command */
4195 ap->ops->exec_command(ap, &qc->tf);
4196}
4197
4198/**
Alan Coxb73fc892005-08-26 16:03:19 +01004199 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 * @qc: Info associated with this ATA transaction.
4201 *
4202 * LOCKING:
4203 * spin_lock_irqsave(host_set lock)
4204 */
4205
4206static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4207{
4208 struct ata_port *ap = qc->ap;
4209 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4210 u8 dmactl;
4211
4212 /* start host DMA transaction */
4213 dmactl = readb(mmio + ATA_DMA_CMD);
4214 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4215
4216 /* Strictly, one may wish to issue a readb() here, to
4217 * flush the mmio write. However, control also passes
4218 * to the hardware at this point, and it will interrupt
4219 * us when we are to resume control. So, in effect,
4220 * we don't care when the mmio write flushes.
4221 * Further, a read of the DMA status register _immediately_
4222 * following the write may not be what certain flaky hardware
4223 * is expected, so I think it is best to not add a readb()
4224 * without first all the MMIO ATA cards/mobos.
4225 * Or maybe I'm just being paranoid.
4226 */
4227}
4228
4229/**
4230 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4231 * @qc: Info associated with this ATA transaction.
4232 *
4233 * LOCKING:
4234 * spin_lock_irqsave(host_set lock)
4235 */
4236
4237static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4238{
4239 struct ata_port *ap = qc->ap;
4240 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4241 u8 dmactl;
4242
4243 /* load PRD table addr. */
4244 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4245
4246 /* specify data direction, triple-check start bit is clear */
4247 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4248 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4249 if (!rw)
4250 dmactl |= ATA_DMA_WR;
4251 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4252
4253 /* issue r/w command */
4254 ap->ops->exec_command(ap, &qc->tf);
4255}
4256
4257/**
4258 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4259 * @qc: Info associated with this ATA transaction.
4260 *
4261 * LOCKING:
4262 * spin_lock_irqsave(host_set lock)
4263 */
4264
4265static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4266{
4267 struct ata_port *ap = qc->ap;
4268 u8 dmactl;
4269
4270 /* start host DMA transaction */
4271 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4272 outb(dmactl | ATA_DMA_START,
4273 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4274}
4275
Edward Falk0baab862005-06-02 18:17:13 -04004276
4277/**
4278 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4279 * @qc: Info associated with this ATA transaction.
4280 *
4281 * Writes the ATA_DMA_START flag to the DMA command register.
4282 *
4283 * May be used as the bmdma_start() entry in ata_port_operations.
4284 *
4285 * LOCKING:
4286 * spin_lock_irqsave(host_set lock)
4287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288void ata_bmdma_start(struct ata_queued_cmd *qc)
4289{
4290 if (qc->ap->flags & ATA_FLAG_MMIO)
4291 ata_bmdma_start_mmio(qc);
4292 else
4293 ata_bmdma_start_pio(qc);
4294}
4295
Edward Falk0baab862005-06-02 18:17:13 -04004296
4297/**
4298 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4299 * @qc: Info associated with this ATA transaction.
4300 *
4301 * Writes address of PRD table to device's PRD Table Address
4302 * register, sets the DMA control register, and calls
4303 * ops->exec_command() to start the transfer.
4304 *
4305 * May be used as the bmdma_setup() entry in ata_port_operations.
4306 *
4307 * LOCKING:
4308 * spin_lock_irqsave(host_set lock)
4309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310void ata_bmdma_setup(struct ata_queued_cmd *qc)
4311{
4312 if (qc->ap->flags & ATA_FLAG_MMIO)
4313 ata_bmdma_setup_mmio(qc);
4314 else
4315 ata_bmdma_setup_pio(qc);
4316}
4317
Edward Falk0baab862005-06-02 18:17:13 -04004318
4319/**
4320 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004321 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004322 *
4323 * Clear interrupt and error flags in DMA status register.
4324 *
4325 * May be used as the irq_clear() entry in ata_port_operations.
4326 *
4327 * LOCKING:
4328 * spin_lock_irqsave(host_set lock)
4329 */
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331void ata_bmdma_irq_clear(struct ata_port *ap)
4332{
4333 if (ap->flags & ATA_FLAG_MMIO) {
4334 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4335 writeb(readb(mmio), mmio);
4336 } else {
4337 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4338 outb(inb(addr), addr);
4339 }
4340
4341}
4342
Edward Falk0baab862005-06-02 18:17:13 -04004343
4344/**
4345 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004346 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004347 *
4348 * Read and return BMDMA status register.
4349 *
4350 * May be used as the bmdma_status() entry in ata_port_operations.
4351 *
4352 * LOCKING:
4353 * spin_lock_irqsave(host_set lock)
4354 */
4355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356u8 ata_bmdma_status(struct ata_port *ap)
4357{
4358 u8 host_stat;
4359 if (ap->flags & ATA_FLAG_MMIO) {
4360 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4361 host_stat = readb(mmio + ATA_DMA_STATUS);
4362 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004363 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 return host_stat;
4365}
4366
Edward Falk0baab862005-06-02 18:17:13 -04004367
4368/**
4369 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004370 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004371 *
4372 * Clears the ATA_DMA_START flag in the dma control register
4373 *
4374 * May be used as the bmdma_stop() entry in ata_port_operations.
4375 *
4376 * LOCKING:
4377 * spin_lock_irqsave(host_set lock)
4378 */
4379
Alan Coxb73fc892005-08-26 16:03:19 +01004380void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Alan Coxb73fc892005-08-26 16:03:19 +01004382 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 if (ap->flags & ATA_FLAG_MMIO) {
4384 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4385
4386 /* clear start/stop bit */
4387 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4388 mmio + ATA_DMA_CMD);
4389 } else {
4390 /* clear start/stop bit */
4391 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4392 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4393 }
4394
4395 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4396 ata_altstatus(ap); /* dummy read */
4397}
4398
4399/**
4400 * ata_host_intr - Handle host interrupt for given (port, task)
4401 * @ap: Port on which interrupt arrived (possibly...)
4402 * @qc: Taskfile currently active in engine
4403 *
4404 * Handle host interrupt for given queued command. Currently,
4405 * only DMA interrupts are handled. All other commands are
4406 * handled via polling with interrupts disabled (nIEN bit).
4407 *
4408 * LOCKING:
4409 * spin_lock_irqsave(host_set lock)
4410 *
4411 * RETURNS:
4412 * One if interrupt was handled, zero if not (shared irq).
4413 */
4414
4415inline unsigned int ata_host_intr (struct ata_port *ap,
4416 struct ata_queued_cmd *qc)
4417{
4418 u8 status, host_stat;
4419
4420 switch (qc->tf.protocol) {
4421
4422 case ATA_PROT_DMA:
4423 case ATA_PROT_ATAPI_DMA:
4424 case ATA_PROT_ATAPI:
4425 /* check status of DMA engine */
4426 host_stat = ap->ops->bmdma_status(ap);
4427 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4428
4429 /* if it's not our irq... */
4430 if (!(host_stat & ATA_DMA_INTR))
4431 goto idle_irq;
4432
4433 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004434 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
4436 /* fall through */
4437
4438 case ATA_PROT_ATAPI_NODATA:
4439 case ATA_PROT_NODATA:
4440 /* check altstatus */
4441 status = ata_altstatus(ap);
4442 if (status & ATA_BUSY)
4443 goto idle_irq;
4444
4445 /* check main status, clearing INTRQ */
4446 status = ata_chk_status(ap);
4447 if (unlikely(status & ATA_BUSY))
4448 goto idle_irq;
4449 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4450 ap->id, qc->tf.protocol, status);
4451
4452 /* ack bmdma irq events */
4453 ap->ops->irq_clear(ap);
4454
4455 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004456 qc->err_mask |= ac_err_mask(status);
4457 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 break;
4459
4460 default:
4461 goto idle_irq;
4462 }
4463
4464 return 1; /* irq handled */
4465
4466idle_irq:
4467 ap->stats.idle_irq++;
4468
4469#ifdef ATA_IRQ_TRAP
4470 if ((ap->stats.idle_irq % 1000) == 0) {
4471 handled = 1;
4472 ata_irq_ack(ap, 0); /* debug trap */
4473 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4474 }
4475#endif
4476 return 0; /* irq not handled */
4477}
4478
4479/**
4480 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004481 * @irq: irq line (unused)
4482 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 * @regs: unused
4484 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004485 * Default interrupt handler for PCI IDE devices. Calls
4486 * ata_host_intr() for each port that is not disabled.
4487 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004489 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 *
4491 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004492 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 */
4494
4495irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4496{
4497 struct ata_host_set *host_set = dev_instance;
4498 unsigned int i;
4499 unsigned int handled = 0;
4500 unsigned long flags;
4501
4502 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4503 spin_lock_irqsave(&host_set->lock, flags);
4504
4505 for (i = 0; i < host_set->n_ports; i++) {
4506 struct ata_port *ap;
4507
4508 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004509 if (ap &&
4510 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 struct ata_queued_cmd *qc;
4512
4513 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004514 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4515 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 handled |= ata_host_intr(ap, qc);
4517 }
4518 }
4519
4520 spin_unlock_irqrestore(&host_set->lock, flags);
4521
4522 return IRQ_RETVAL(handled);
4523}
4524
4525/**
4526 * atapi_packet_task - Write CDB bytes to hardware
4527 * @_data: Port to which ATAPI device is attached.
4528 *
4529 * When device has indicated its readiness to accept
4530 * a CDB, this function is called. Send the CDB.
4531 * If DMA is to be performed, exit immediately.
4532 * Otherwise, we are in polling mode, so poll
4533 * status under operation succeeds or fails.
4534 *
4535 * LOCKING:
4536 * Kernel thread context (may sleep)
4537 */
4538
4539static void atapi_packet_task(void *_data)
4540{
4541 struct ata_port *ap = _data;
4542 struct ata_queued_cmd *qc;
4543 u8 status;
4544
4545 qc = ata_qc_from_tag(ap, ap->active_tag);
4546 assert(qc != NULL);
4547 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4548
4549 /* sleep-wait for BSY to clear */
4550 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004551 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004552 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004553 goto err_out;
4554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
4556 /* make sure DRQ is set */
4557 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004558 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004559 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
4563 /* send SCSI cdb */
4564 DPRINTK("send cdb\n");
4565 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Tejun Heoc1389502005-08-22 14:59:24 +09004567 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4568 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4569 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Tejun Heoc1389502005-08-22 14:59:24 +09004571 /* Once we're done issuing command and kicking bmdma,
4572 * irq handler takes over. To not lose irq, we need
4573 * to clear NOINTR flag before sending cdb, but
4574 * interrupt handler shouldn't be invoked before we're
4575 * finished. Hence, the following locking.
4576 */
4577 spin_lock_irqsave(&ap->host_set->lock, flags);
4578 ap->flags &= ~ATA_FLAG_NOINTR;
4579 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4580 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4581 ap->ops->bmdma_start(qc); /* initiate bmdma */
4582 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4583 } else {
4584 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Tejun Heoc1389502005-08-22 14:59:24 +09004586 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004587 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004588 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 }
4590
4591 return;
4592
4593err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004594 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595}
4596
Edward Falk0baab862005-06-02 18:17:13 -04004597
Jens Axboe9b847542006-01-06 09:28:07 +01004598/*
4599 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4600 * without filling any other registers
4601 */
4602static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4603 u8 cmd)
4604{
4605 struct ata_taskfile tf;
4606 int err;
4607
4608 ata_tf_init(ap, &tf, dev->devno);
4609
4610 tf.command = cmd;
4611 tf.flags |= ATA_TFLAG_DEVICE;
4612 tf.protocol = ATA_PROT_NODATA;
4613
4614 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4615 if (err)
4616 printk(KERN_ERR "%s: ata command failed: %d\n",
4617 __FUNCTION__, err);
4618
4619 return err;
4620}
4621
4622static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4623{
4624 u8 cmd;
4625
4626 if (!ata_try_flush_cache(dev))
4627 return 0;
4628
4629 if (ata_id_has_flush_ext(dev->id))
4630 cmd = ATA_CMD_FLUSH_EXT;
4631 else
4632 cmd = ATA_CMD_FLUSH;
4633
4634 return ata_do_simple_cmd(ap, dev, cmd);
4635}
4636
4637static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4638{
4639 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4640}
4641
4642static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4643{
4644 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4645}
4646
4647/**
4648 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004649 * @ap: port the device is connected to
4650 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004651 *
4652 * Kick the drive back into action, by sending it an idle immediate
4653 * command and making sure its transfer mode matches between drive
4654 * and host.
4655 *
4656 */
4657int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4658{
4659 if (ap->flags & ATA_FLAG_SUSPENDED) {
4660 ap->flags &= ~ATA_FLAG_SUSPENDED;
4661 ata_set_mode(ap);
4662 }
4663 if (!ata_dev_present(dev))
4664 return 0;
4665 if (dev->class == ATA_DEV_ATA)
4666 ata_start_drive(ap, dev);
4667
4668 return 0;
4669}
4670
4671/**
4672 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004673 * @ap: port the device is connected to
4674 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004675 *
4676 * Flush the cache on the drive, if appropriate, then issue a
4677 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004678 */
4679int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4680{
4681 if (!ata_dev_present(dev))
4682 return 0;
4683 if (dev->class == ATA_DEV_ATA)
4684 ata_flush_cache(ap, dev);
4685
4686 ata_standby_drive(ap, dev);
4687 ap->flags |= ATA_FLAG_SUSPENDED;
4688 return 0;
4689}
4690
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004691/**
4692 * ata_port_start - Set port up for dma.
4693 * @ap: Port to initialize
4694 *
4695 * Called just after data structures for each port are
4696 * initialized. Allocates space for PRD table.
4697 *
4698 * May be used as the port_start() entry in ata_port_operations.
4699 *
4700 * LOCKING:
4701 * Inherited from caller.
4702 */
4703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704int ata_port_start (struct ata_port *ap)
4705{
4706 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004707 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4710 if (!ap->prd)
4711 return -ENOMEM;
4712
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004713 rc = ata_pad_alloc(ap, dev);
4714 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004715 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004716 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004717 }
4718
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4720
4721 return 0;
4722}
4723
Edward Falk0baab862005-06-02 18:17:13 -04004724
4725/**
4726 * ata_port_stop - Undo ata_port_start()
4727 * @ap: Port to shut down
4728 *
4729 * Frees the PRD table.
4730 *
4731 * May be used as the port_stop() entry in ata_port_operations.
4732 *
4733 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004734 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004735 */
4736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737void ata_port_stop (struct ata_port *ap)
4738{
4739 struct device *dev = ap->host_set->dev;
4740
4741 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004742 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004745void ata_host_stop (struct ata_host_set *host_set)
4746{
4747 if (host_set->mmio_base)
4748 iounmap(host_set->mmio_base);
4749}
4750
4751
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752/**
4753 * ata_host_remove - Unregister SCSI host structure with upper layers
4754 * @ap: Port to unregister
4755 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4756 *
4757 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004758 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 */
4760
4761static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4762{
4763 struct Scsi_Host *sh = ap->host;
4764
4765 DPRINTK("ENTER\n");
4766
4767 if (do_unregister)
4768 scsi_remove_host(sh);
4769
4770 ap->ops->port_stop(ap);
4771}
4772
4773/**
4774 * ata_host_init - Initialize an ata_port structure
4775 * @ap: Structure to initialize
4776 * @host: associated SCSI mid-layer structure
4777 * @host_set: Collection of hosts to which @ap belongs
4778 * @ent: Probe information provided by low-level driver
4779 * @port_no: Port number associated with this ata_port
4780 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004781 * Initialize a new ata_port structure, and its associated
4782 * scsi_host.
4783 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004785 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 */
4787
4788static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4789 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004790 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791{
4792 unsigned int i;
4793
4794 host->max_id = 16;
4795 host->max_lun = 1;
4796 host->max_channel = 1;
4797 host->unique_id = ata_unique_id++;
4798 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004799
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 ap->flags = ATA_FLAG_PORT_DISABLED;
4801 ap->id = host->unique_id;
4802 ap->host = host;
4803 ap->ctl = ATA_DEVCTL_OBS;
4804 ap->host_set = host_set;
4805 ap->port_no = port_no;
4806 ap->hard_port_no =
4807 ent->legacy_mode ? ent->hard_port_no : port_no;
4808 ap->pio_mask = ent->pio_mask;
4809 ap->mwdma_mask = ent->mwdma_mask;
4810 ap->udma_mask = ent->udma_mask;
4811 ap->flags |= ent->host_flags;
4812 ap->ops = ent->port_ops;
4813 ap->cbl = ATA_CBL_NONE;
4814 ap->active_tag = ATA_TAG_POISON;
4815 ap->last_ctl = 0xFF;
4816
4817 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4818 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004819 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
4821 for (i = 0; i < ATA_MAX_DEVICES; i++)
4822 ap->device[i].devno = i;
4823
4824#ifdef ATA_IRQ_TRAP
4825 ap->stats.unhandled_irq = 1;
4826 ap->stats.idle_irq = 1;
4827#endif
4828
4829 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4830}
4831
4832/**
4833 * ata_host_add - Attach low-level ATA driver to system
4834 * @ent: Information provided by low-level driver
4835 * @host_set: Collections of ports to which we add
4836 * @port_no: Port number associated with this host
4837 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004838 * Attach low-level ATA driver to system.
4839 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004841 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 *
4843 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004844 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 */
4846
Jeff Garzik057ace52005-10-22 14:27:05 -04004847static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 struct ata_host_set *host_set,
4849 unsigned int port_no)
4850{
4851 struct Scsi_Host *host;
4852 struct ata_port *ap;
4853 int rc;
4854
4855 DPRINTK("ENTER\n");
4856 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4857 if (!host)
4858 return NULL;
4859
4860 ap = (struct ata_port *) &host->hostdata[0];
4861
4862 ata_host_init(ap, host, host_set, ent, port_no);
4863
4864 rc = ap->ops->port_start(ap);
4865 if (rc)
4866 goto err_out;
4867
4868 return ap;
4869
4870err_out:
4871 scsi_host_put(host);
4872 return NULL;
4873}
4874
4875/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004876 * ata_device_add - Register hardware device with ATA and SCSI layers
4877 * @ent: Probe information describing hardware device to be registered
4878 *
4879 * This function processes the information provided in the probe
4880 * information struct @ent, allocates the necessary ATA and SCSI
4881 * host information structures, initializes them, and registers
4882 * everything with requisite kernel subsystems.
4883 *
4884 * This function requests irqs, probes the ATA bus, and probes
4885 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 *
4887 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004888 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 *
4890 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004891 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 */
4893
Jeff Garzik057ace52005-10-22 14:27:05 -04004894int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
4896 unsigned int count = 0, i;
4897 struct device *dev = ent->dev;
4898 struct ata_host_set *host_set;
4899
4900 DPRINTK("ENTER\n");
4901 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004902 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4904 if (!host_set)
4905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 spin_lock_init(&host_set->lock);
4907
4908 host_set->dev = dev;
4909 host_set->n_ports = ent->n_ports;
4910 host_set->irq = ent->irq;
4911 host_set->mmio_base = ent->mmio_base;
4912 host_set->private_data = ent->private_data;
4913 host_set->ops = ent->port_ops;
4914
4915 /* register each port bound to this device */
4916 for (i = 0; i < ent->n_ports; i++) {
4917 struct ata_port *ap;
4918 unsigned long xfer_mode_mask;
4919
4920 ap = ata_host_add(ent, host_set, i);
4921 if (!ap)
4922 goto err_out;
4923
4924 host_set->ports[i] = ap;
4925 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4926 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4927 (ap->pio_mask << ATA_SHIFT_PIO);
4928
4929 /* print per-port info to dmesg */
4930 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4931 "bmdma 0x%lX irq %lu\n",
4932 ap->id,
4933 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4934 ata_mode_string(xfer_mode_mask),
4935 ap->ioaddr.cmd_addr,
4936 ap->ioaddr.ctl_addr,
4937 ap->ioaddr.bmdma_addr,
4938 ent->irq);
4939
4940 ata_chk_status(ap);
4941 host_set->ops->irq_clear(ap);
4942 count++;
4943 }
4944
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004945 if (!count)
4946 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
4948 /* obtain irq, that is shared between channels */
4949 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4950 DRV_NAME, host_set))
4951 goto err_out;
4952
4953 /* perform each probe synchronously */
4954 DPRINTK("probe begin\n");
4955 for (i = 0; i < count; i++) {
4956 struct ata_port *ap;
4957 int rc;
4958
4959 ap = host_set->ports[i];
4960
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004961 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004963 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
4965 if (rc) {
4966 /* FIXME: do something useful here?
4967 * Current libata behavior will
4968 * tear down everything when
4969 * the module is removed
4970 * or the h/w is unplugged.
4971 */
4972 }
4973
4974 rc = scsi_add_host(ap->host, dev);
4975 if (rc) {
4976 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4977 ap->id);
4978 /* FIXME: do something useful here */
4979 /* FIXME: handle unconditional calls to
4980 * scsi_scan_host and ata_host_remove, below,
4981 * at the very least
4982 */
4983 }
4984 }
4985
4986 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004987 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 for (i = 0; i < count; i++) {
4989 struct ata_port *ap = host_set->ports[i];
4990
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004991 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 }
4993
4994 dev_set_drvdata(dev, host_set);
4995
4996 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4997 return ent->n_ports; /* success */
4998
4999err_out:
5000 for (i = 0; i < count; i++) {
5001 ata_host_remove(host_set->ports[i], 1);
5002 scsi_host_put(host_set->ports[i]->host);
5003 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005004err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 kfree(host_set);
5006 VPRINTK("EXIT, returning 0\n");
5007 return 0;
5008}
5009
5010/**
Alan Cox17b14452005-09-15 15:44:00 +01005011 * ata_host_set_remove - PCI layer callback for device removal
5012 * @host_set: ATA host set that was removed
5013 *
5014 * Unregister all objects associated with this host set. Free those
5015 * objects.
5016 *
5017 * LOCKING:
5018 * Inherited from calling layer (may sleep).
5019 */
5020
Alan Cox17b14452005-09-15 15:44:00 +01005021void ata_host_set_remove(struct ata_host_set *host_set)
5022{
5023 struct ata_port *ap;
5024 unsigned int i;
5025
5026 for (i = 0; i < host_set->n_ports; i++) {
5027 ap = host_set->ports[i];
5028 scsi_remove_host(ap->host);
5029 }
5030
5031 free_irq(host_set->irq, host_set);
5032
5033 for (i = 0; i < host_set->n_ports; i++) {
5034 ap = host_set->ports[i];
5035
5036 ata_scsi_release(ap->host);
5037
5038 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
5039 struct ata_ioports *ioaddr = &ap->ioaddr;
5040
5041 if (ioaddr->cmd_addr == 0x1f0)
5042 release_region(0x1f0, 8);
5043 else if (ioaddr->cmd_addr == 0x170)
5044 release_region(0x170, 8);
5045 }
5046
5047 scsi_host_put(ap->host);
5048 }
5049
5050 if (host_set->ops->host_stop)
5051 host_set->ops->host_stop(host_set);
5052
5053 kfree(host_set);
5054}
5055
5056/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 * ata_scsi_release - SCSI layer callback hook for host unload
5058 * @host: libata host to be unloaded
5059 *
5060 * Performs all duties necessary to shut down a libata port...
5061 * Kill port kthread, disable port, and release resources.
5062 *
5063 * LOCKING:
5064 * Inherited from SCSI layer.
5065 *
5066 * RETURNS:
5067 * One.
5068 */
5069
5070int ata_scsi_release(struct Scsi_Host *host)
5071{
5072 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
5073
5074 DPRINTK("ENTER\n");
5075
5076 ap->ops->port_disable(ap);
5077 ata_host_remove(ap, 0);
5078
5079 DPRINTK("EXIT\n");
5080 return 1;
5081}
5082
5083/**
5084 * ata_std_ports - initialize ioaddr with standard port offsets.
5085 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04005086 *
5087 * Utility function which initializes data_addr, error_addr,
5088 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5089 * device_addr, status_addr, and command_addr to standard offsets
5090 * relative to cmd_addr.
5091 *
5092 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 */
Edward Falk0baab862005-06-02 18:17:13 -04005094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095void ata_std_ports(struct ata_ioports *ioaddr)
5096{
5097 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5098 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5099 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5100 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5101 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5102 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5103 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5104 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5105 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5106 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5107}
5108
5109static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04005110ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111{
5112 struct ata_probe_ent *probe_ent;
5113
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005114 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 if (!probe_ent) {
5116 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
5117 kobject_name(&(dev->kobj)));
5118 return NULL;
5119 }
5120
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 INIT_LIST_HEAD(&probe_ent->node);
5122 probe_ent->dev = dev;
5123
5124 probe_ent->sht = port->sht;
5125 probe_ent->host_flags = port->host_flags;
5126 probe_ent->pio_mask = port->pio_mask;
5127 probe_ent->mwdma_mask = port->mwdma_mask;
5128 probe_ent->udma_mask = port->udma_mask;
5129 probe_ent->port_ops = port->port_ops;
5130
5131 return probe_ent;
5132}
5133
Edward Falk0baab862005-06-02 18:17:13 -04005134
5135
Jeff Garzik374b1872005-08-30 05:42:52 -04005136#ifdef CONFIG_PCI
5137
5138void ata_pci_host_stop (struct ata_host_set *host_set)
5139{
5140 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5141
5142 pci_iounmap(pdev, host_set->mmio_base);
5143}
5144
Edward Falk0baab862005-06-02 18:17:13 -04005145/**
5146 * ata_pci_init_native_mode - Initialize native-mode driver
5147 * @pdev: pci device to be initialized
5148 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04005149 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04005150 *
5151 * Utility function which allocates and initializes an
5152 * ata_probe_ent structure for a standard dual-port
5153 * PIO-based IDE controller. The returned ata_probe_ent
5154 * structure can be passed to ata_device_add(). The returned
5155 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04005156 *
5157 * The caller need only pass the address of the primary port, the
5158 * secondary will be deduced automatically. If the device has non
5159 * standard secondary port mappings this function can be called twice,
5160 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04005161 */
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04005164ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165{
5166 struct ata_probe_ent *probe_ent =
5167 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04005168 int p = 0;
5169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 if (!probe_ent)
5171 return NULL;
5172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 probe_ent->irq = pdev->irq;
5174 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00005175 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
Alan Cox47a86592005-10-04 08:09:19 -04005177 if (ports & ATA_PORT_PRIMARY) {
5178 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
5179 probe_ent->port[p].altstatus_addr =
5180 probe_ent->port[p].ctl_addr =
5181 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
5182 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
5183 ata_std_ports(&probe_ent->port[p]);
5184 p++;
5185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Alan Cox47a86592005-10-04 08:09:19 -04005187 if (ports & ATA_PORT_SECONDARY) {
5188 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
5189 probe_ent->port[p].altstatus_addr =
5190 probe_ent->port[p].ctl_addr =
5191 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
5192 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
5193 ata_std_ports(&probe_ent->port[p]);
5194 p++;
5195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
Alan Cox47a86592005-10-04 08:09:19 -04005197 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 return probe_ent;
5199}
5200
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005201static 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 -07005202{
Alan Cox47a86592005-10-04 08:09:19 -04005203 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005205 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 if (!probe_ent)
5207 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04005210 probe_ent->n_ports = 1;
5211 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00005212 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
Alan Cox47a86592005-10-04 08:09:19 -04005214 switch(port_num)
5215 {
5216 case 0:
5217 probe_ent->irq = 14;
5218 probe_ent->port[0].cmd_addr = 0x1f0;
5219 probe_ent->port[0].altstatus_addr =
5220 probe_ent->port[0].ctl_addr = 0x3f6;
5221 break;
5222 case 1:
5223 probe_ent->irq = 15;
5224 probe_ent->port[0].cmd_addr = 0x170;
5225 probe_ent->port[0].altstatus_addr =
5226 probe_ent->port[0].ctl_addr = 0x376;
5227 break;
5228 }
5229 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 return probe_ent;
5232}
5233
5234/**
5235 * ata_pci_init_one - Initialize/register PCI IDE host controller
5236 * @pdev: Controller to be initialized
5237 * @port_info: Information from low-level host driver
5238 * @n_ports: Number of ports attached to host controller
5239 *
Edward Falk0baab862005-06-02 18:17:13 -04005240 * This is a helper function which can be called from a driver's
5241 * xxx_init_one() probe function if the hardware uses traditional
5242 * IDE taskfile registers.
5243 *
5244 * This function calls pci_enable_device(), reserves its register
5245 * regions, sets the dma mask, enables bus master mode, and calls
5246 * ata_device_add()
5247 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 * LOCKING:
5249 * Inherited from PCI layer (may sleep).
5250 *
5251 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005252 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 */
5254
5255int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
5256 unsigned int n_ports)
5257{
Alan Cox47a86592005-10-04 08:09:19 -04005258 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 struct ata_port_info *port[2];
5260 u8 tmp8, mask;
5261 unsigned int legacy_mode = 0;
5262 int disable_dev_on_err = 1;
5263 int rc;
5264
5265 DPRINTK("ENTER\n");
5266
5267 port[0] = port_info[0];
5268 if (n_ports > 1)
5269 port[1] = port_info[1];
5270 else
5271 port[1] = port[0];
5272
5273 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
5274 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04005275 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
5277 mask = (1 << 2) | (1 << 0);
5278 if ((tmp8 & mask) != mask)
5279 legacy_mode = (1 << 3);
5280 }
5281
5282 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04005283 if ((!legacy_mode) && (n_ports > 2)) {
5284 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
5285 n_ports = 2;
5286 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 }
5288
Alan Cox47a86592005-10-04 08:09:19 -04005289 /* FIXME: Really for ATA it isn't safe because the device may be
5290 multi-purpose and we want to leave it alone if it was already
5291 enabled. Secondly for shared use as Arjan says we want refcounting
5292
5293 Checking dev->is_enabled is insufficient as this is not set at
5294 boot for the primary video which is BIOS enabled
5295 */
5296
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 rc = pci_enable_device(pdev);
5298 if (rc)
5299 return rc;
5300
5301 rc = pci_request_regions(pdev, DRV_NAME);
5302 if (rc) {
5303 disable_dev_on_err = 0;
5304 goto err_out;
5305 }
5306
Alan Cox47a86592005-10-04 08:09:19 -04005307 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 if (legacy_mode) {
5309 if (!request_region(0x1f0, 8, "libata")) {
5310 struct resource *conflict, res;
5311 res.start = 0x1f0;
5312 res.end = 0x1f0 + 8 - 1;
5313 conflict = ____request_resource(&ioport_resource, &res);
5314 if (!strcmp(conflict->name, "libata"))
5315 legacy_mode |= (1 << 0);
5316 else {
5317 disable_dev_on_err = 0;
5318 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
5319 }
5320 } else
5321 legacy_mode |= (1 << 0);
5322
5323 if (!request_region(0x170, 8, "libata")) {
5324 struct resource *conflict, res;
5325 res.start = 0x170;
5326 res.end = 0x170 + 8 - 1;
5327 conflict = ____request_resource(&ioport_resource, &res);
5328 if (!strcmp(conflict->name, "libata"))
5329 legacy_mode |= (1 << 1);
5330 else {
5331 disable_dev_on_err = 0;
5332 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
5333 }
5334 } else
5335 legacy_mode |= (1 << 1);
5336 }
5337
5338 /* we have legacy mode, but all ports are unavailable */
5339 if (legacy_mode == (1 << 3)) {
5340 rc = -EBUSY;
5341 goto err_out_regions;
5342 }
5343
5344 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
5345 if (rc)
5346 goto err_out_regions;
5347 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
5348 if (rc)
5349 goto err_out_regions;
5350
5351 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04005352 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005353 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04005354 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005355 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04005356 } else {
5357 if (n_ports == 2)
5358 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
5359 else
5360 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
5361 }
5362 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 rc = -ENOMEM;
5364 goto err_out_regions;
5365 }
5366
5367 pci_set_master(pdev);
5368
5369 /* FIXME: check ata_device_add return */
5370 if (legacy_mode) {
5371 if (legacy_mode & (1 << 0))
5372 ata_device_add(probe_ent);
5373 if (legacy_mode & (1 << 1))
5374 ata_device_add(probe_ent2);
5375 } else
5376 ata_device_add(probe_ent);
5377
5378 kfree(probe_ent);
5379 kfree(probe_ent2);
5380
5381 return 0;
5382
5383err_out_regions:
5384 if (legacy_mode & (1 << 0))
5385 release_region(0x1f0, 8);
5386 if (legacy_mode & (1 << 1))
5387 release_region(0x170, 8);
5388 pci_release_regions(pdev);
5389err_out:
5390 if (disable_dev_on_err)
5391 pci_disable_device(pdev);
5392 return rc;
5393}
5394
5395/**
5396 * ata_pci_remove_one - PCI layer callback for device removal
5397 * @pdev: PCI device that was removed
5398 *
5399 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005400 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 * Handle this by unregistering all objects associated
5402 * with this PCI device. Free those objects. Then finally
5403 * release PCI resources and disable device.
5404 *
5405 * LOCKING:
5406 * Inherited from PCI layer (may sleep).
5407 */
5408
5409void ata_pci_remove_one (struct pci_dev *pdev)
5410{
5411 struct device *dev = pci_dev_to_dev(pdev);
5412 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
Alan Cox17b14452005-09-15 15:44:00 +01005414 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 pci_release_regions(pdev);
5416 pci_disable_device(pdev);
5417 dev_set_drvdata(dev, NULL);
5418}
5419
5420/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005421int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422{
5423 unsigned long tmp = 0;
5424
5425 switch (bits->width) {
5426 case 1: {
5427 u8 tmp8 = 0;
5428 pci_read_config_byte(pdev, bits->reg, &tmp8);
5429 tmp = tmp8;
5430 break;
5431 }
5432 case 2: {
5433 u16 tmp16 = 0;
5434 pci_read_config_word(pdev, bits->reg, &tmp16);
5435 tmp = tmp16;
5436 break;
5437 }
5438 case 4: {
5439 u32 tmp32 = 0;
5440 pci_read_config_dword(pdev, bits->reg, &tmp32);
5441 tmp = tmp32;
5442 break;
5443 }
5444
5445 default:
5446 return -EINVAL;
5447 }
5448
5449 tmp &= bits->mask;
5450
5451 return (tmp == bits->val) ? 1 : 0;
5452}
Jens Axboe9b847542006-01-06 09:28:07 +01005453
5454int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5455{
5456 pci_save_state(pdev);
5457 pci_disable_device(pdev);
5458 pci_set_power_state(pdev, PCI_D3hot);
5459 return 0;
5460}
5461
5462int ata_pci_device_resume(struct pci_dev *pdev)
5463{
5464 pci_set_power_state(pdev, PCI_D0);
5465 pci_restore_state(pdev);
5466 pci_enable_device(pdev);
5467 pci_set_master(pdev);
5468 return 0;
5469}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470#endif /* CONFIG_PCI */
5471
5472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473static int __init ata_init(void)
5474{
5475 ata_wq = create_workqueue("ata");
5476 if (!ata_wq)
5477 return -ENOMEM;
5478
5479 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5480 return 0;
5481}
5482
5483static void __exit ata_exit(void)
5484{
5485 destroy_workqueue(ata_wq);
5486}
5487
5488module_init(ata_init);
5489module_exit(ata_exit);
5490
Jeff Garzik67846b32005-10-05 02:58:32 -04005491static unsigned long ratelimit_time;
5492static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5493
5494int ata_ratelimit(void)
5495{
5496 int rc;
5497 unsigned long flags;
5498
5499 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5500
5501 if (time_after(jiffies, ratelimit_time)) {
5502 rc = 1;
5503 ratelimit_time = jiffies + (HZ/5);
5504 } else
5505 rc = 0;
5506
5507 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5508
5509 return rc;
5510}
5511
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512/*
5513 * libata is essentially a library of internal helper functions for
5514 * low-level ATA host controller drivers. As such, the API/ABI is
5515 * likely to change as new drivers are added and updated.
5516 * Do not depend on ABI/API stability.
5517 */
5518
5519EXPORT_SYMBOL_GPL(ata_std_bios_param);
5520EXPORT_SYMBOL_GPL(ata_std_ports);
5521EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005522EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523EXPORT_SYMBOL_GPL(ata_sg_init);
5524EXPORT_SYMBOL_GPL(ata_sg_init_one);
5525EXPORT_SYMBOL_GPL(ata_qc_complete);
5526EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5527EXPORT_SYMBOL_GPL(ata_eng_timeout);
5528EXPORT_SYMBOL_GPL(ata_tf_load);
5529EXPORT_SYMBOL_GPL(ata_tf_read);
5530EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5531EXPORT_SYMBOL_GPL(ata_std_dev_select);
5532EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5533EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5534EXPORT_SYMBOL_GPL(ata_check_status);
5535EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536EXPORT_SYMBOL_GPL(ata_exec_command);
5537EXPORT_SYMBOL_GPL(ata_port_start);
5538EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005539EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540EXPORT_SYMBOL_GPL(ata_interrupt);
5541EXPORT_SYMBOL_GPL(ata_qc_prep);
5542EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5543EXPORT_SYMBOL_GPL(ata_bmdma_start);
5544EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5545EXPORT_SYMBOL_GPL(ata_bmdma_status);
5546EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5547EXPORT_SYMBOL_GPL(ata_port_probe);
5548EXPORT_SYMBOL_GPL(sata_phy_reset);
5549EXPORT_SYMBOL_GPL(__sata_phy_reset);
5550EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005551EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005552EXPORT_SYMBOL_GPL(ata_std_softreset);
5553EXPORT_SYMBOL_GPL(sata_std_hardreset);
5554EXPORT_SYMBOL_GPL(ata_std_postreset);
5555EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005556EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005558EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005559EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5561EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5562EXPORT_SYMBOL_GPL(ata_scsi_error);
5563EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5564EXPORT_SYMBOL_GPL(ata_scsi_release);
5565EXPORT_SYMBOL_GPL(ata_host_intr);
5566EXPORT_SYMBOL_GPL(ata_dev_classify);
5567EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005568EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005570EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5571EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005573EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005574EXPORT_SYMBOL_GPL(ata_timing_compute);
5575EXPORT_SYMBOL_GPL(ata_timing_merge);
5576
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577#ifdef CONFIG_PCI
5578EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005579EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5581EXPORT_SYMBOL_GPL(ata_pci_init_one);
5582EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005583EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5584EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005586
5587EXPORT_SYMBOL_GPL(ata_device_suspend);
5588EXPORT_SYMBOL_GPL(ata_device_resume);
5589EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5590EXPORT_SYMBOL_GPL(ata_scsi_device_resume);