blob: b369dbd7cb238dadbf8c41d20bd6f71e3a1efec9 [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 Lee8bf62ece2005-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{
1077 queue_work(ata_wq, &ap->packet_task);
1078}
1079
1080static inline void
1081ata_queue_pio_task(struct ata_port *ap)
1082{
1083 queue_work(ata_wq, &ap->pio_task);
1084}
1085
1086static inline void
1087ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
1088{
1089 queue_delayed_work(ata_wq, &ap->pio_task, delay);
1090}
1091
Tejun Heo77853bf2006-01-23 13:09:36 +09001092void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001093{
Tejun Heo77853bf2006-01-23 13:09:36 +09001094 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001095
Tejun Heo77853bf2006-01-23 13:09:36 +09001096 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001097 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001098}
1099
1100/**
1101 * ata_exec_internal - execute libata internal command
1102 * @ap: Port to which the command is sent
1103 * @dev: Device to which the command is sent
1104 * @tf: Taskfile registers for the command and the result
1105 * @dma_dir: Data tranfer direction of the command
1106 * @buf: Data buffer of the command
1107 * @buflen: Length of data buffer
1108 *
1109 * Executes libata internal command with timeout. @tf contains
1110 * command on entry and result on return. Timeout and error
1111 * conditions are reported via return value. No recovery action
1112 * is taken after a command times out. It's caller's duty to
1113 * clean up after timeout.
1114 *
1115 * LOCKING:
1116 * None. Should be called with kernel context, might sleep.
1117 */
1118
1119static unsigned
1120ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1121 struct ata_taskfile *tf,
1122 int dma_dir, void *buf, unsigned int buflen)
1123{
1124 u8 command = tf->command;
1125 struct ata_queued_cmd *qc;
1126 DECLARE_COMPLETION(wait);
1127 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001128 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001129
1130 spin_lock_irqsave(&ap->host_set->lock, flags);
1131
1132 qc = ata_qc_new_init(ap, dev);
1133 BUG_ON(qc == NULL);
1134
1135 qc->tf = *tf;
1136 qc->dma_dir = dma_dir;
1137 if (dma_dir != DMA_NONE) {
1138 ata_sg_init_one(qc, buf, buflen);
1139 qc->nsect = buflen / ATA_SECT_SIZE;
1140 }
1141
Tejun Heo77853bf2006-01-23 13:09:36 +09001142 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001143 qc->complete_fn = ata_qc_complete_internal;
1144
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09001145 qc->err_mask = ata_qc_issue(qc);
1146 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +09001147 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001148
1149 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1150
1151 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1152 spin_lock_irqsave(&ap->host_set->lock, flags);
1153
1154 /* We're racing with irq here. If we lose, the
1155 * following test prevents us from completing the qc
1156 * again. If completion irq occurs after here but
1157 * before the caller cleans up, it will result in a
1158 * spurious interrupt. We can live with that.
1159 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001160 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001161 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001162 ata_qc_complete(qc);
1163 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1164 ap->id, command);
1165 }
1166
1167 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1168 }
1169
Tejun Heo77853bf2006-01-23 13:09:36 +09001170 *tf = qc->tf;
1171 err_mask = qc->err_mask;
1172
1173 ata_qc_free(qc);
1174
1175 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001179 * ata_pio_need_iordy - check if iordy needed
1180 * @adev: ATA device
1181 *
1182 * Check if the current speed of the device requires IORDY. Used
1183 * by various controllers for chip configuration.
1184 */
1185
1186unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1187{
1188 int pio;
1189 int speed = adev->pio_mode - XFER_PIO_0;
1190
1191 if (speed < 2)
1192 return 0;
1193 if (speed > 2)
1194 return 1;
1195
1196 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1197
1198 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1199 pio = adev->id[ATA_ID_EIDE_PIO];
1200 /* Is the speed faster than the drive allows non IORDY ? */
1201 if (pio) {
1202 /* This is cycle times not frequency - watch the logic! */
1203 if (pio > 240) /* PIO2 is 240nS per cycle */
1204 return 1;
1205 return 0;
1206 }
1207 }
1208 return 0;
1209}
1210
1211/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1213 * @ap: port on which device we wish to probe resides
1214 * @device: device bus address, starting at zero
1215 *
1216 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1217 * command, and read back the 512-byte device information page.
1218 * The device information page is fed to us via the standard
1219 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1220 * using standard PIO-IN paths)
1221 *
1222 * After reading the device information page, we use several
1223 * bits of information from it to initialize data structures
1224 * that will be used during the lifetime of the ata_device.
1225 * Other data from the info page is used to disqualify certain
1226 * older ATA devices we do not wish to support.
1227 *
1228 * LOCKING:
1229 * Inherited from caller. Some functions called by this function
1230 * obtain the host_set lock.
1231 */
1232
1233static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1234{
1235 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001236 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 u16 tmp;
1238 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001240 struct ata_taskfile tf;
1241 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int rc;
1243
1244 if (!ata_dev_present(dev)) {
1245 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1246 ap->id, device);
1247 return;
1248 }
1249
1250 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1251 using_edd = 0;
1252 else
1253 using_edd = 1;
1254
1255 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1256
1257 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1258 dev->class == ATA_DEV_NONE);
1259
1260 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001263 ata_tf_init(ap, &tf, device);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001266 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 DPRINTK("do ATA identify\n");
1268 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001269 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 DPRINTK("do ATAPI identify\n");
1271 }
1272
Tejun Heoa0123702005-12-13 14:49:31 +09001273 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Tejun Heoa0123702005-12-13 14:49:31 +09001275 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1276 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Tejun Heoa0123702005-12-13 14:49:31 +09001278 if (err_mask) {
1279 if (err_mask & ~AC_ERR_DEV)
1280 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
1283 * arg! EDD works for all test cases, but seems to return
1284 * the ATA signature for some ATAPI devices. Until the
1285 * reason for this is found and fixed, we fix up the mess
1286 * here. If IDENTIFY DEVICE returns command aborted
1287 * (as ATAPI devices do), then we issue an
1288 * IDENTIFY PACKET DEVICE.
1289 *
1290 * ATA software reset (SRST, the default) does not appear
1291 * to have this problem.
1292 */
Albert Lee7c398332005-11-09 13:03:30 +08001293 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001294 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (err & ATA_ABORTED) {
1296 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 goto retry;
1298 }
1299 }
1300 goto err_out;
1301 }
1302
1303 swap_buf_le16(dev->id, ATA_ID_WORDS);
1304
1305 /* print device capabilities */
1306 printk(KERN_DEBUG "ata%u: dev %u cfg "
1307 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1308 ap->id, device, dev->id[49],
1309 dev->id[82], dev->id[83], dev->id[84],
1310 dev->id[85], dev->id[86], dev->id[87],
1311 dev->id[88]);
1312
1313 /*
1314 * common ATA, ATAPI feature tests
1315 */
1316
Albert Lee8bf62ece2005-05-12 15:29:42 -04001317 /* we require DMA support (bits 8 of word 49) */
1318 if (!ata_id_has_dma(dev->id)) {
1319 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 goto err_out_nosup;
1321 }
1322
1323 /* quick-n-dirty find max transfer mode; for printk only */
1324 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1325 if (!xfer_modes)
1326 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001327 if (!xfer_modes)
1328 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 ata_dump_id(dev);
1331
1332 /* ATA-specific feature tests */
1333 if (dev->class == ATA_DEV_ATA) {
1334 if (!ata_id_is_ata(dev->id)) /* sanity check */
1335 goto err_out_nosup;
1336
Albert Lee8bf62ece2005-05-12 15:29:42 -04001337 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001339 for (major_version = 14; major_version >= 1; major_version--)
1340 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 break;
1342
Albert Lee8bf62ece2005-05-12 15:29:42 -04001343 /*
1344 * The exact sequence expected by certain pre-ATA4 drives is:
1345 * SRST RESET
1346 * IDENTIFY
1347 * INITIALIZE DEVICE PARAMETERS
1348 * anything else..
1349 * Some drives were very specific about that exact sequence.
1350 */
Albert Lee59a10b12005-10-12 15:09:42 +08001351 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001352 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Albert Lee59a10b12005-10-12 15:09:42 +08001354 /* current CHS translation info (id[53-58]) might be
1355 * changed. reread the identify device info.
1356 */
1357 ata_dev_reread_id(ap, dev);
1358 }
1359
Albert Lee8bf62ece2005-05-12 15:29:42 -04001360 if (ata_id_has_lba(dev->id)) {
1361 dev->flags |= ATA_DFLAG_LBA;
1362
1363 if (ata_id_has_lba48(dev->id)) {
1364 dev->flags |= ATA_DFLAG_LBA48;
1365 dev->n_sectors = ata_id_u64(dev->id, 100);
1366 } else {
1367 dev->n_sectors = ata_id_u32(dev->id, 60);
1368 }
1369
1370 /* print device info to dmesg */
1371 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1372 ap->id, device,
1373 major_version,
1374 ata_mode_string(xfer_modes),
1375 (unsigned long long)dev->n_sectors,
1376 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1377 } else {
1378 /* CHS */
1379
1380 /* Default translation */
1381 dev->cylinders = dev->id[1];
1382 dev->heads = dev->id[3];
1383 dev->sectors = dev->id[6];
1384 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1385
1386 if (ata_id_current_chs_valid(dev->id)) {
1387 /* Current CHS translation is valid. */
1388 dev->cylinders = dev->id[54];
1389 dev->heads = dev->id[55];
1390 dev->sectors = dev->id[56];
1391
1392 dev->n_sectors = ata_id_u32(dev->id, 57);
1393 }
1394
1395 /* print device info to dmesg */
1396 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1397 ap->id, device,
1398 major_version,
1399 ata_mode_string(xfer_modes),
1400 (unsigned long long)dev->n_sectors,
1401 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
1405 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
1408 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001409 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (ata_id_is_ata(dev->id)) /* sanity check */
1411 goto err_out_nosup;
1412
1413 rc = atapi_cdb_len(dev->id);
1414 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1415 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1416 goto err_out_nosup;
1417 }
1418 ap->cdb_len = (unsigned int) rc;
1419 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1420
1421 /* print device info to dmesg */
1422 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1423 ap->id, device,
1424 ata_mode_string(xfer_modes));
1425 }
1426
1427 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1428 return;
1429
1430err_out_nosup:
1431 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1432 ap->id, device);
1433err_out:
1434 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1435 DPRINTK("EXIT, err\n");
1436}
1437
Brad Campbell6f2f3812005-05-12 15:07:47 -04001438
Jeff Garzik057ace52005-10-22 14:27:05 -04001439static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001440{
1441 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1442}
1443
1444/**
1445 * ata_dev_config - Run device specific handlers and check for
1446 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001447 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001448 * @i: Device
1449 *
1450 * LOCKING:
1451 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001452
Brad Campbell6f2f3812005-05-12 15:07:47 -04001453void ata_dev_config(struct ata_port *ap, unsigned int i)
1454{
1455 /* limit bridge transfers to udma5, 200 sectors */
1456 if (ata_dev_knobble(ap)) {
1457 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1458 ap->id, ap->device->devno);
1459 ap->udma_mask &= ATA_UDMA5;
1460 ap->host->max_sectors = ATA_MAX_SECTORS;
1461 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001462 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001463 }
1464
1465 if (ap->ops->dev_config)
1466 ap->ops->dev_config(ap, &ap->device[i]);
1467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/**
1470 * ata_bus_probe - Reset and probe ATA bus
1471 * @ap: Bus to probe
1472 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001473 * Master ATA bus probing function. Initiates a hardware-dependent
1474 * bus reset, then attempts to identify any devices found on
1475 * the bus.
1476 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001478 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 *
1480 * RETURNS:
1481 * Zero on success, non-zero on error.
1482 */
1483
1484static int ata_bus_probe(struct ata_port *ap)
1485{
1486 unsigned int i, found = 0;
1487
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001488 if (ap->ops->probe_reset) {
1489 unsigned int classes[ATA_MAX_DEVICES];
1490 int rc;
1491
1492 ata_port_probe(ap);
1493
1494 rc = ap->ops->probe_reset(ap, classes);
1495 if (rc == 0) {
1496 for (i = 0; i < ATA_MAX_DEVICES; i++)
1497 ap->device[i].class = classes[i];
1498 } else {
1499 printk(KERN_ERR "ata%u: probe reset failed, "
1500 "disabling port\n", ap->id);
1501 ata_port_disable(ap);
1502 }
1503 } else
1504 ap->ops->phy_reset(ap);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1507 goto err_out;
1508
1509 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1510 ata_dev_identify(ap, i);
1511 if (ata_dev_present(&ap->device[i])) {
1512 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001513 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515 }
1516
1517 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1518 goto err_out_disable;
1519
1520 ata_set_mode(ap);
1521 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1522 goto err_out_disable;
1523
1524 return 0;
1525
1526err_out_disable:
1527 ap->ops->port_disable(ap);
1528err_out:
1529 return -1;
1530}
1531
1532/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001533 * ata_port_probe - Mark port as enabled
1534 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001536 * Modify @ap data structure such that the system
1537 * thinks that the entire port is enabled.
1538 *
1539 * LOCKING: host_set lock, or some other form of
1540 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
1542
1543void ata_port_probe(struct ata_port *ap)
1544{
1545 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1546}
1547
1548/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001549 * sata_print_link_status - Print SATA link status
1550 * @ap: SATA port to printk link status about
1551 *
1552 * This function prints link speed and status of a SATA link.
1553 *
1554 * LOCKING:
1555 * None.
1556 */
1557static void sata_print_link_status(struct ata_port *ap)
1558{
1559 u32 sstatus, tmp;
1560 const char *speed;
1561
1562 if (!ap->ops->scr_read)
1563 return;
1564
1565 sstatus = scr_read(ap, SCR_STATUS);
1566
1567 if (sata_dev_present(ap)) {
1568 tmp = (sstatus >> 4) & 0xf;
1569 if (tmp & (1 << 0))
1570 speed = "1.5";
1571 else if (tmp & (1 << 1))
1572 speed = "3.0";
1573 else
1574 speed = "<unknown>";
1575 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1576 ap->id, speed, sstatus);
1577 } else {
1578 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1579 ap->id, sstatus);
1580 }
1581}
1582
1583/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001584 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1585 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001587 * This function issues commands to standard SATA Sxxx
1588 * PHY registers, to wake up the phy (and device), and
1589 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 *
1591 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001592 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 *
1594 */
1595void __sata_phy_reset(struct ata_port *ap)
1596{
1597 u32 sstatus;
1598 unsigned long timeout = jiffies + (HZ * 5);
1599
1600 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001601 /* issue phy wake/reset */
1602 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001603 /* Couldn't find anything in SATA I/II specs, but
1604 * AHCI-1.1 10.4.2 says at least 1 ms. */
1605 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001607 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 /* wait for phy to become ready, if necessary */
1610 do {
1611 msleep(200);
1612 sstatus = scr_read(ap, SCR_STATUS);
1613 if ((sstatus & 0xf) != 1)
1614 break;
1615 } while (time_before(jiffies, timeout));
1616
Tejun Heo3be680b2005-12-19 22:35:02 +09001617 /* print link status */
1618 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001619
Tejun Heo3be680b2005-12-19 22:35:02 +09001620 /* TODO: phy layer with polling, timeouts, etc. */
1621 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001623 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1627 return;
1628
1629 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1630 ata_port_disable(ap);
1631 return;
1632 }
1633
1634 ap->cbl = ATA_CBL_SATA;
1635}
1636
1637/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001638 * sata_phy_reset - Reset SATA bus.
1639 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001641 * This function resets the SATA bus, and then probes
1642 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 *
1644 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001645 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 *
1647 */
1648void sata_phy_reset(struct ata_port *ap)
1649{
1650 __sata_phy_reset(ap);
1651 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1652 return;
1653 ata_bus_reset(ap);
1654}
1655
1656/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001657 * ata_port_disable - Disable port.
1658 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001660 * Modify @ap data structure such that the system
1661 * thinks that the entire port is disabled, and should
1662 * never attempt to probe or communicate with devices
1663 * on this port.
1664 *
1665 * LOCKING: host_set lock, or some other form of
1666 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
1668
1669void ata_port_disable(struct ata_port *ap)
1670{
1671 ap->device[0].class = ATA_DEV_NONE;
1672 ap->device[1].class = ATA_DEV_NONE;
1673 ap->flags |= ATA_FLAG_PORT_DISABLED;
1674}
1675
Alan Cox452503f2005-10-21 19:01:32 -04001676/*
1677 * This mode timing computation functionality is ported over from
1678 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1679 */
1680/*
1681 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1682 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1683 * for PIO 5, which is a nonstandard extension and UDMA6, which
1684 * is currently supported only by Maxtor drives.
1685 */
1686
1687static const struct ata_timing ata_timing[] = {
1688
1689 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1690 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1691 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1692 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1693
1694 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1695 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1696 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1697
1698/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1699
1700 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1701 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1702 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1703
1704 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1705 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1706 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1707
1708/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1709 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1710 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1711
1712 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1713 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1714 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1715
1716/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1717
1718 { 0xFF }
1719};
1720
1721#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1722#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1723
1724static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1725{
1726 q->setup = EZ(t->setup * 1000, T);
1727 q->act8b = EZ(t->act8b * 1000, T);
1728 q->rec8b = EZ(t->rec8b * 1000, T);
1729 q->cyc8b = EZ(t->cyc8b * 1000, T);
1730 q->active = EZ(t->active * 1000, T);
1731 q->recover = EZ(t->recover * 1000, T);
1732 q->cycle = EZ(t->cycle * 1000, T);
1733 q->udma = EZ(t->udma * 1000, UT);
1734}
1735
1736void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1737 struct ata_timing *m, unsigned int what)
1738{
1739 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1740 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1741 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1742 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1743 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1744 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1745 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1746 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1747}
1748
1749static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1750{
1751 const struct ata_timing *t;
1752
1753 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001754 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001755 return NULL;
1756 return t;
1757}
1758
1759int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1760 struct ata_timing *t, int T, int UT)
1761{
1762 const struct ata_timing *s;
1763 struct ata_timing p;
1764
1765 /*
1766 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001767 */
Alan Cox452503f2005-10-21 19:01:32 -04001768
1769 if (!(s = ata_timing_find_mode(speed)))
1770 return -EINVAL;
1771
Albert Lee75b1f2f2005-11-16 17:06:18 +08001772 memcpy(t, s, sizeof(*s));
1773
Alan Cox452503f2005-10-21 19:01:32 -04001774 /*
1775 * If the drive is an EIDE drive, it can tell us it needs extended
1776 * PIO/MW_DMA cycle timing.
1777 */
1778
1779 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1780 memset(&p, 0, sizeof(p));
1781 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1782 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1783 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1784 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1785 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1786 }
1787 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1788 }
1789
1790 /*
1791 * Convert the timing to bus clock counts.
1792 */
1793
Albert Lee75b1f2f2005-11-16 17:06:18 +08001794 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001795
1796 /*
1797 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1798 * and some other commands. We have to ensure that the DMA cycle timing is
1799 * slower/equal than the fastest PIO timing.
1800 */
1801
1802 if (speed > XFER_PIO_4) {
1803 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1804 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1805 }
1806
1807 /*
1808 * Lenghten active & recovery time so that cycle time is correct.
1809 */
1810
1811 if (t->act8b + t->rec8b < t->cyc8b) {
1812 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1813 t->rec8b = t->cyc8b - t->act8b;
1814 }
1815
1816 if (t->active + t->recover < t->cycle) {
1817 t->active += (t->cycle - (t->active + t->recover)) / 2;
1818 t->recover = t->cycle - t->active;
1819 }
1820
1821 return 0;
1822}
1823
Jeff Garzik057ace52005-10-22 14:27:05 -04001824static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 unsigned int shift;
1826 u8 base;
1827} xfer_mode_classes[] = {
1828 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1829 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1830 { ATA_SHIFT_PIO, XFER_PIO_0 },
1831};
1832
Arjan van de Ven858119e2006-01-14 13:20:43 -08001833static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 int i;
1836
1837 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1838 if (xfer_mode_classes[i].shift == shift)
1839 return xfer_mode_classes[i].base;
1840
1841 return 0xff;
1842}
1843
1844static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1845{
1846 int ofs, idx;
1847 u8 base;
1848
1849 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1850 return;
1851
1852 if (dev->xfer_shift == ATA_SHIFT_PIO)
1853 dev->flags |= ATA_DFLAG_PIO;
1854
1855 ata_dev_set_xfermode(ap, dev);
1856
1857 base = base_from_shift(dev->xfer_shift);
1858 ofs = dev->xfer_mode - base;
1859 idx = ofs + dev->xfer_shift;
1860 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1861
1862 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1863 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1864
1865 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1866 ap->id, dev->devno, xfer_mode_str[idx]);
1867}
1868
1869static int ata_host_set_pio(struct ata_port *ap)
1870{
1871 unsigned int mask;
1872 int x, i;
1873 u8 base, xfer_mode;
1874
1875 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1876 x = fgb(mask);
1877 if (x < 0) {
1878 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1879 return -1;
1880 }
1881
1882 base = base_from_shift(ATA_SHIFT_PIO);
1883 xfer_mode = base + x;
1884
1885 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1886 (int)base, (int)xfer_mode, mask, x);
1887
1888 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1889 struct ata_device *dev = &ap->device[i];
1890 if (ata_dev_present(dev)) {
1891 dev->pio_mode = xfer_mode;
1892 dev->xfer_mode = xfer_mode;
1893 dev->xfer_shift = ATA_SHIFT_PIO;
1894 if (ap->ops->set_piomode)
1895 ap->ops->set_piomode(ap, dev);
1896 }
1897 }
1898
1899 return 0;
1900}
1901
1902static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1903 unsigned int xfer_shift)
1904{
1905 int i;
1906
1907 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1908 struct ata_device *dev = &ap->device[i];
1909 if (ata_dev_present(dev)) {
1910 dev->dma_mode = xfer_mode;
1911 dev->xfer_mode = xfer_mode;
1912 dev->xfer_shift = xfer_shift;
1913 if (ap->ops->set_dmamode)
1914 ap->ops->set_dmamode(ap, dev);
1915 }
1916 }
1917}
1918
1919/**
1920 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1921 * @ap: port on which timings will be programmed
1922 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001923 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1924 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001926 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 *
1928 */
1929static void ata_set_mode(struct ata_port *ap)
1930{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001931 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 u8 xfer_mode;
1933 int rc;
1934
1935 /* step 1: always set host PIO timings */
1936 rc = ata_host_set_pio(ap);
1937 if (rc)
1938 goto err_out;
1939
1940 /* step 2: choose the best data xfer mode */
1941 xfer_mode = xfer_shift = 0;
1942 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1943 if (rc)
1944 goto err_out;
1945
1946 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1947 if (xfer_shift != ATA_SHIFT_PIO)
1948 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1949
1950 /* step 4: update devices' xfer mode */
1951 ata_dev_set_mode(ap, &ap->device[0]);
1952 ata_dev_set_mode(ap, &ap->device[1]);
1953
1954 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1955 return;
1956
1957 if (ap->ops->post_set_mode)
1958 ap->ops->post_set_mode(ap);
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return;
1961
1962err_out:
1963 ata_port_disable(ap);
1964}
1965
1966/**
1967 * ata_busy_sleep - sleep until BSY clears, or timeout
1968 * @ap: port containing status register to be polled
1969 * @tmout_pat: impatience timeout
1970 * @tmout: overall timeout
1971 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001972 * Sleep until ATA Status register bit BSY clears,
1973 * or a timeout occurs.
1974 *
1975 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 *
1977 */
1978
Tejun Heo6f8b9952006-01-24 17:05:21 +09001979unsigned int ata_busy_sleep (struct ata_port *ap,
1980 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 unsigned long timer_start, timeout;
1983 u8 status;
1984
1985 status = ata_busy_wait(ap, ATA_BUSY, 300);
1986 timer_start = jiffies;
1987 timeout = timer_start + tmout_pat;
1988 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1989 msleep(50);
1990 status = ata_busy_wait(ap, ATA_BUSY, 3);
1991 }
1992
1993 if (status & ATA_BUSY)
1994 printk(KERN_WARNING "ata%u is slow to respond, "
1995 "please be patient\n", ap->id);
1996
1997 timeout = timer_start + tmout;
1998 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1999 msleep(50);
2000 status = ata_chk_status(ap);
2001 }
2002
2003 if (status & ATA_BUSY) {
2004 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
2005 ap->id, tmout / HZ);
2006 return 1;
2007 }
2008
2009 return 0;
2010}
2011
2012static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2013{
2014 struct ata_ioports *ioaddr = &ap->ioaddr;
2015 unsigned int dev0 = devmask & (1 << 0);
2016 unsigned int dev1 = devmask & (1 << 1);
2017 unsigned long timeout;
2018
2019 /* if device 0 was found in ata_devchk, wait for its
2020 * BSY bit to clear
2021 */
2022 if (dev0)
2023 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2024
2025 /* if device 1 was found in ata_devchk, wait for
2026 * register access, then wait for BSY to clear
2027 */
2028 timeout = jiffies + ATA_TMOUT_BOOT;
2029 while (dev1) {
2030 u8 nsect, lbal;
2031
2032 ap->ops->dev_select(ap, 1);
2033 if (ap->flags & ATA_FLAG_MMIO) {
2034 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2035 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2036 } else {
2037 nsect = inb(ioaddr->nsect_addr);
2038 lbal = inb(ioaddr->lbal_addr);
2039 }
2040 if ((nsect == 1) && (lbal == 1))
2041 break;
2042 if (time_after(jiffies, timeout)) {
2043 dev1 = 0;
2044 break;
2045 }
2046 msleep(50); /* give drive a breather */
2047 }
2048 if (dev1)
2049 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2050
2051 /* is all this really necessary? */
2052 ap->ops->dev_select(ap, 0);
2053 if (dev1)
2054 ap->ops->dev_select(ap, 1);
2055 if (dev0)
2056 ap->ops->dev_select(ap, 0);
2057}
2058
2059/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002060 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2061 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002063 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2064 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 *
2066 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002067 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05002068 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 *
2070 */
2071
2072static unsigned int ata_bus_edd(struct ata_port *ap)
2073{
2074 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05002075 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 /* set up execute-device-diag (bus reset) taskfile */
2078 /* also, take interrupts to a known state (disabled) */
2079 DPRINTK("execute-device-diag\n");
2080 ata_tf_init(ap, &tf, 0);
2081 tf.ctl |= ATA_NIEN;
2082 tf.command = ATA_CMD_EDD;
2083 tf.protocol = ATA_PROT_NODATA;
2084
2085 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002086 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002088 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 /* spec says at least 2ms. but who knows with those
2091 * crazy ATAPI devices...
2092 */
2093 msleep(150);
2094
2095 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2096}
2097
2098static unsigned int ata_bus_softreset(struct ata_port *ap,
2099 unsigned int devmask)
2100{
2101 struct ata_ioports *ioaddr = &ap->ioaddr;
2102
2103 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2104
2105 /* software reset. causes dev0 to be selected */
2106 if (ap->flags & ATA_FLAG_MMIO) {
2107 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2108 udelay(20); /* FIXME: flush */
2109 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2110 udelay(20); /* FIXME: flush */
2111 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2112 } else {
2113 outb(ap->ctl, ioaddr->ctl_addr);
2114 udelay(10);
2115 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2116 udelay(10);
2117 outb(ap->ctl, ioaddr->ctl_addr);
2118 }
2119
2120 /* spec mandates ">= 2ms" before checking status.
2121 * We wait 150ms, because that was the magic delay used for
2122 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2123 * between when the ATA command register is written, and then
2124 * status is checked. Because waiting for "a while" before
2125 * checking status is fine, post SRST, we perform this magic
2126 * delay here as well.
2127 */
2128 msleep(150);
2129
2130 ata_bus_post_reset(ap, devmask);
2131
2132 return 0;
2133}
2134
2135/**
2136 * ata_bus_reset - reset host port and associated ATA channel
2137 * @ap: port to reset
2138 *
2139 * This is typically the first time we actually start issuing
2140 * commands to the ATA channel. We wait for BSY to clear, then
2141 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2142 * result. Determine what devices, if any, are on the channel
2143 * by looking at the device 0/1 error register. Look at the signature
2144 * stored in each device's taskfile registers, to determine if
2145 * the device is ATA or ATAPI.
2146 *
2147 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002148 * PCI/etc. bus probe sem.
2149 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 *
2151 * SIDE EFFECTS:
2152 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2153 */
2154
2155void ata_bus_reset(struct ata_port *ap)
2156{
2157 struct ata_ioports *ioaddr = &ap->ioaddr;
2158 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2159 u8 err;
2160 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2161
2162 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2163
2164 /* determine if device 0/1 are present */
2165 if (ap->flags & ATA_FLAG_SATA_RESET)
2166 dev0 = 1;
2167 else {
2168 dev0 = ata_devchk(ap, 0);
2169 if (slave_possible)
2170 dev1 = ata_devchk(ap, 1);
2171 }
2172
2173 if (dev0)
2174 devmask |= (1 << 0);
2175 if (dev1)
2176 devmask |= (1 << 1);
2177
2178 /* select device 0 again */
2179 ap->ops->dev_select(ap, 0);
2180
2181 /* issue bus reset */
2182 if (ap->flags & ATA_FLAG_SRST)
2183 rc = ata_bus_softreset(ap, devmask);
2184 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2185 /* set up device control */
2186 if (ap->flags & ATA_FLAG_MMIO)
2187 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2188 else
2189 outb(ap->ctl, ioaddr->ctl_addr);
2190 rc = ata_bus_edd(ap);
2191 }
2192
2193 if (rc)
2194 goto err_out;
2195
2196 /*
2197 * determine by signature whether we have ATA or ATAPI devices
2198 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002199 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002201 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* re-enable interrupts */
2204 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2205 ata_irq_on(ap);
2206
2207 /* is double-select really necessary? */
2208 if (ap->device[1].class != ATA_DEV_NONE)
2209 ap->ops->dev_select(ap, 1);
2210 if (ap->device[0].class != ATA_DEV_NONE)
2211 ap->ops->dev_select(ap, 0);
2212
2213 /* if no devices were detected, disable this port */
2214 if ((ap->device[0].class == ATA_DEV_NONE) &&
2215 (ap->device[1].class == ATA_DEV_NONE))
2216 goto err_out;
2217
2218 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2219 /* set up device control for ATA_FLAG_SATA_RESET */
2220 if (ap->flags & ATA_FLAG_MMIO)
2221 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2222 else
2223 outb(ap->ctl, ioaddr->ctl_addr);
2224 }
2225
2226 DPRINTK("EXIT\n");
2227 return;
2228
2229err_out:
2230 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2231 ap->ops->port_disable(ap);
2232
2233 DPRINTK("EXIT\n");
2234}
2235
Tejun Heoc2bd5802006-01-24 17:05:22 +09002236/**
2237 * ata_std_softreset - reset host port via ATA SRST
2238 * @ap: port to reset
2239 * @verbose: fail verbosely
2240 * @classes: resulting classes of attached devices
2241 *
2242 * Reset host port using ATA SRST. This function is to be used
2243 * as standard callback for ata_drive_*_reset() functions.
2244 *
2245 * LOCKING:
2246 * Kernel thread context (may sleep)
2247 *
2248 * RETURNS:
2249 * 0 on success, -errno otherwise.
2250 */
2251int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2252{
2253 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2254 unsigned int devmask = 0, err_mask;
2255 u8 err;
2256
2257 DPRINTK("ENTER\n");
2258
2259 /* determine if device 0/1 are present */
2260 if (ata_devchk(ap, 0))
2261 devmask |= (1 << 0);
2262 if (slave_possible && ata_devchk(ap, 1))
2263 devmask |= (1 << 1);
2264
2265 /* devchk reports device presence without actual device on
2266 * most SATA controllers. Check SStatus and turn devmask off
2267 * if link is offline. Note that we should continue resetting
2268 * even when it seems like there's no device.
2269 */
2270 if (ap->ops->scr_read && !sata_dev_present(ap))
2271 devmask = 0;
2272
2273 /* select device 0 again */
2274 ap->ops->dev_select(ap, 0);
2275
2276 /* issue bus reset */
2277 DPRINTK("about to softreset, devmask=%x\n", devmask);
2278 err_mask = ata_bus_softreset(ap, devmask);
2279 if (err_mask) {
2280 if (verbose)
2281 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2282 ap->id, err_mask);
2283 else
2284 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2285 err_mask);
2286 return -EIO;
2287 }
2288
2289 /* determine by signature whether we have ATA or ATAPI devices */
2290 classes[0] = ata_dev_try_classify(ap, 0, &err);
2291 if (slave_possible && err != 0x81)
2292 classes[1] = ata_dev_try_classify(ap, 1, &err);
2293
2294 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2295 return 0;
2296}
2297
2298/**
2299 * sata_std_hardreset - reset host port via SATA phy reset
2300 * @ap: port to reset
2301 * @verbose: fail verbosely
2302 * @class: resulting class of attached device
2303 *
2304 * SATA phy-reset host port using DET bits of SControl register.
2305 * This function is to be used as standard callback for
2306 * ata_drive_*_reset().
2307 *
2308 * LOCKING:
2309 * Kernel thread context (may sleep)
2310 *
2311 * RETURNS:
2312 * 0 on success, -errno otherwise.
2313 */
2314int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2315{
2316 u32 sstatus, serror;
2317 unsigned long timeout = jiffies + (HZ * 5);
2318
2319 DPRINTK("ENTER\n");
2320
2321 /* Issue phy wake/reset */
2322 scr_write_flush(ap, SCR_CONTROL, 0x301);
2323
2324 /*
2325 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2326 * 10.4.2 says at least 1 ms.
2327 */
2328 msleep(1);
2329
2330 scr_write_flush(ap, SCR_CONTROL, 0x300);
2331
2332 /* Wait for phy to become ready, if necessary. */
2333 do {
2334 msleep(200);
2335 sstatus = scr_read(ap, SCR_STATUS);
2336 if ((sstatus & 0xf) != 1)
2337 break;
2338 } while (time_before(jiffies, timeout));
2339
2340 /* Clear SError */
2341 serror = scr_read(ap, SCR_ERROR);
2342 scr_write(ap, SCR_ERROR, serror);
2343
2344 /* TODO: phy layer with polling, timeouts, etc. */
2345 if (!sata_dev_present(ap)) {
2346 *class = ATA_DEV_NONE;
2347 DPRINTK("EXIT, link offline\n");
2348 return 0;
2349 }
2350
2351 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2352 if (verbose)
2353 printk(KERN_ERR "ata%u: COMRESET failed "
2354 "(device not ready)\n", ap->id);
2355 else
2356 DPRINTK("EXIT, device not ready\n");
2357 return -EIO;
2358 }
2359
2360 *class = ata_dev_try_classify(ap, 0, NULL);
2361
2362 DPRINTK("EXIT, class=%u\n", *class);
2363 return 0;
2364}
2365
2366/**
2367 * ata_std_postreset - standard postreset callback
2368 * @ap: the target ata_port
2369 * @classes: classes of attached devices
2370 *
2371 * This function is invoked after a successful reset. Note that
2372 * the device might have been reset more than once using
2373 * different reset methods before postreset is invoked.
2374 * postreset is also reponsible for setting cable type.
2375 *
2376 * This function is to be used as standard callback for
2377 * ata_drive_*_reset().
2378 *
2379 * LOCKING:
2380 * Kernel thread context (may sleep)
2381 */
2382void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2383{
2384 DPRINTK("ENTER\n");
2385
2386 /* set cable type */
2387 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2388 ap->cbl = ATA_CBL_SATA;
2389
2390 /* print link status */
2391 if (ap->cbl == ATA_CBL_SATA)
2392 sata_print_link_status(ap);
2393
2394 /* bail out if no device is present */
2395 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2396 DPRINTK("EXIT, no device\n");
2397 return;
2398 }
2399
2400 /* is double-select really necessary? */
2401 if (classes[0] != ATA_DEV_NONE)
2402 ap->ops->dev_select(ap, 1);
2403 if (classes[1] != ATA_DEV_NONE)
2404 ap->ops->dev_select(ap, 0);
2405
2406 /* re-enable interrupts & set up device control */
2407 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2408 ata_irq_on(ap);
2409
2410 DPRINTK("EXIT\n");
2411}
2412
2413/**
2414 * ata_std_probe_reset - standard probe reset method
2415 * @ap: prot to perform probe-reset
2416 * @classes: resulting classes of attached devices
2417 *
2418 * The stock off-the-shelf ->probe_reset method.
2419 *
2420 * LOCKING:
2421 * Kernel thread context (may sleep)
2422 *
2423 * RETURNS:
2424 * 0 on success, -errno otherwise.
2425 */
2426int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2427{
2428 ata_reset_fn_t hardreset;
2429
2430 hardreset = NULL;
2431 if (ap->cbl == ATA_CBL_SATA && ap->ops->scr_read)
2432 hardreset = sata_std_hardreset;
2433
2434 return ata_drive_probe_reset(ap, ata_std_softreset, hardreset,
2435 ata_std_postreset, classes);
2436}
2437
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002438static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2439 ata_postreset_fn_t postreset,
2440 unsigned int *classes)
2441{
2442 int i, rc;
2443
2444 for (i = 0; i < ATA_MAX_DEVICES; i++)
2445 classes[i] = ATA_DEV_UNKNOWN;
2446
2447 rc = reset(ap, 0, classes);
2448 if (rc)
2449 return rc;
2450
2451 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2452 * is complete and convert all ATA_DEV_UNKNOWN to
2453 * ATA_DEV_NONE.
2454 */
2455 for (i = 0; i < ATA_MAX_DEVICES; i++)
2456 if (classes[i] != ATA_DEV_UNKNOWN)
2457 break;
2458
2459 if (i < ATA_MAX_DEVICES)
2460 for (i = 0; i < ATA_MAX_DEVICES; i++)
2461 if (classes[i] == ATA_DEV_UNKNOWN)
2462 classes[i] = ATA_DEV_NONE;
2463
2464 if (postreset)
2465 postreset(ap, classes);
2466
2467 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2468}
2469
2470/**
2471 * ata_drive_probe_reset - Perform probe reset with given methods
2472 * @ap: port to reset
2473 * @softreset: softreset method (can be NULL)
2474 * @hardreset: hardreset method (can be NULL)
2475 * @postreset: postreset method (can be NULL)
2476 * @classes: resulting classes of attached devices
2477 *
2478 * Reset the specified port and classify attached devices using
2479 * given methods. This function prefers softreset but tries all
2480 * possible reset sequences to reset and classify devices. This
2481 * function is intended to be used for constructing ->probe_reset
2482 * callback by low level drivers.
2483 *
2484 * Reset methods should follow the following rules.
2485 *
2486 * - Return 0 on sucess, -errno on failure.
2487 * - If classification is supported, fill classes[] with
2488 * recognized class codes.
2489 * - If classification is not supported, leave classes[] alone.
2490 * - If verbose is non-zero, print error message on failure;
2491 * otherwise, shut up.
2492 *
2493 * LOCKING:
2494 * Kernel thread context (may sleep)
2495 *
2496 * RETURNS:
2497 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2498 * if classification fails, and any error code from reset
2499 * methods.
2500 */
2501int ata_drive_probe_reset(struct ata_port *ap,
2502 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2503 ata_postreset_fn_t postreset, unsigned int *classes)
2504{
2505 int rc = -EINVAL;
2506
2507 if (softreset) {
2508 rc = do_probe_reset(ap, softreset, postreset, classes);
2509 if (rc == 0)
2510 return 0;
2511 }
2512
2513 if (!hardreset)
2514 return rc;
2515
2516 rc = do_probe_reset(ap, hardreset, postreset, classes);
2517 if (rc == 0 || rc != -ENODEV)
2518 return rc;
2519
2520 if (softreset)
2521 rc = do_probe_reset(ap, softreset, postreset, classes);
2522
2523 return rc;
2524}
2525
Jeff Garzik057ace52005-10-22 14:27:05 -04002526static void ata_pr_blacklisted(const struct ata_port *ap,
2527 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
2529 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2530 ap->id, dev->devno);
2531}
2532
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002533static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 "WDC AC11000H",
2535 "WDC AC22100H",
2536 "WDC AC32500H",
2537 "WDC AC33100H",
2538 "WDC AC31600H",
2539 "WDC AC32100H",
2540 "WDC AC23200L",
2541 "Compaq CRD-8241B",
2542 "CRD-8400B",
2543 "CRD-8480B",
2544 "CRD-8482B",
2545 "CRD-84",
2546 "SanDisk SDP3B",
2547 "SanDisk SDP3B-64",
2548 "SANYO CD-ROM CRD",
2549 "HITACHI CDR-8",
2550 "HITACHI CDR-8335",
2551 "HITACHI CDR-8435",
2552 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002553 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 "CD-532E-A",
2555 "E-IDE CD-ROM CR-840",
2556 "CD-ROM Drive/F5A",
2557 "WPI CDD-820",
2558 "SAMSUNG CD-ROM SC-148C",
2559 "SAMSUNG CD-ROM SC",
2560 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2562 "_NEC DV5800A",
2563};
2564
Jeff Garzik057ace52005-10-22 14:27:05 -04002565static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
2567 unsigned char model_num[40];
2568 char *s;
2569 unsigned int len;
2570 int i;
2571
2572 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2573 sizeof(model_num));
2574 s = &model_num[0];
2575 len = strnlen(s, sizeof(model_num));
2576
2577 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2578 while ((len > 0) && (s[len - 1] == ' ')) {
2579 len--;
2580 s[len] = 0;
2581 }
2582
2583 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2584 if (!strncmp(ata_dma_blacklist[i], s, len))
2585 return 1;
2586
2587 return 0;
2588}
2589
Jeff Garzik057ace52005-10-22 14:27:05 -04002590static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Jeff Garzik057ace52005-10-22 14:27:05 -04002592 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 unsigned int mask;
2594
2595 master = &ap->device[0];
2596 slave = &ap->device[1];
2597
2598 assert (ata_dev_present(master) || ata_dev_present(slave));
2599
2600 if (shift == ATA_SHIFT_UDMA) {
2601 mask = ap->udma_mask;
2602 if (ata_dev_present(master)) {
2603 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002604 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 mask = 0;
2606 ata_pr_blacklisted(ap, master);
2607 }
2608 }
2609 if (ata_dev_present(slave)) {
2610 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002611 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 mask = 0;
2613 ata_pr_blacklisted(ap, slave);
2614 }
2615 }
2616 }
2617 else if (shift == ATA_SHIFT_MWDMA) {
2618 mask = ap->mwdma_mask;
2619 if (ata_dev_present(master)) {
2620 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002621 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 mask = 0;
2623 ata_pr_blacklisted(ap, master);
2624 }
2625 }
2626 if (ata_dev_present(slave)) {
2627 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002628 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 mask = 0;
2630 ata_pr_blacklisted(ap, slave);
2631 }
2632 }
2633 }
2634 else if (shift == ATA_SHIFT_PIO) {
2635 mask = ap->pio_mask;
2636 if (ata_dev_present(master)) {
2637 /* spec doesn't return explicit support for
2638 * PIO0-2, so we fake it
2639 */
2640 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2641 tmp_mode <<= 3;
2642 tmp_mode |= 0x7;
2643 mask &= tmp_mode;
2644 }
2645 if (ata_dev_present(slave)) {
2646 /* spec doesn't return explicit support for
2647 * PIO0-2, so we fake it
2648 */
2649 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2650 tmp_mode <<= 3;
2651 tmp_mode |= 0x7;
2652 mask &= tmp_mode;
2653 }
2654 }
2655 else {
2656 mask = 0xffffffff; /* shut up compiler warning */
2657 BUG();
2658 }
2659
2660 return mask;
2661}
2662
2663/* find greatest bit */
2664static int fgb(u32 bitmap)
2665{
2666 unsigned int i;
2667 int x = -1;
2668
2669 for (i = 0; i < 32; i++)
2670 if (bitmap & (1 << i))
2671 x = i;
2672
2673 return x;
2674}
2675
2676/**
2677 * ata_choose_xfer_mode - attempt to find best transfer mode
2678 * @ap: Port for which an xfer mode will be selected
2679 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2680 * @xfer_shift_out: (output) bit shift that selects this mode
2681 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002682 * Based on host and device capabilities, determine the
2683 * maximum transfer mode that is amenable to all.
2684 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002686 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 *
2688 * RETURNS:
2689 * Zero on success, negative on error.
2690 */
2691
Jeff Garzik057ace52005-10-22 14:27:05 -04002692static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 u8 *xfer_mode_out,
2694 unsigned int *xfer_shift_out)
2695{
2696 unsigned int mask, shift;
2697 int x, i;
2698
2699 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2700 shift = xfer_mode_classes[i].shift;
2701 mask = ata_get_mode_mask(ap, shift);
2702
2703 x = fgb(mask);
2704 if (x >= 0) {
2705 *xfer_mode_out = xfer_mode_classes[i].base + x;
2706 *xfer_shift_out = shift;
2707 return 0;
2708 }
2709 }
2710
2711 return -1;
2712}
2713
2714/**
2715 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2716 * @ap: Port associated with device @dev
2717 * @dev: Device to which command will be sent
2718 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002719 * Issue SET FEATURES - XFER MODE command to device @dev
2720 * on port @ap.
2721 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002723 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 */
2725
2726static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2727{
Tejun Heoa0123702005-12-13 14:49:31 +09002728 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730 /* set up set-features taskfile */
2731 DPRINTK("set features - xfer mode\n");
2732
Tejun Heoa0123702005-12-13 14:49:31 +09002733 ata_tf_init(ap, &tf, dev->devno);
2734 tf.command = ATA_CMD_SET_FEATURES;
2735 tf.feature = SETFEATURES_XFER;
2736 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2737 tf.protocol = ATA_PROT_NODATA;
2738 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Tejun Heoa0123702005-12-13 14:49:31 +09002740 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2741 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2742 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746 DPRINTK("EXIT\n");
2747}
2748
2749/**
Albert Lee59a10b12005-10-12 15:09:42 +08002750 * ata_dev_reread_id - Reread the device identify device info
2751 * @ap: port where the device is
2752 * @dev: device to reread the identify device info
2753 *
2754 * LOCKING:
2755 */
2756
2757static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2758{
Tejun Heoa0123702005-12-13 14:49:31 +09002759 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002760
Tejun Heoa0123702005-12-13 14:49:31 +09002761 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002762
2763 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002764 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002765 DPRINTK("do ATA identify\n");
2766 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002767 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002768 DPRINTK("do ATAPI identify\n");
2769 }
2770
Tejun Heoa0123702005-12-13 14:49:31 +09002771 tf.flags |= ATA_TFLAG_DEVICE;
2772 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002773
Tejun Heoa0123702005-12-13 14:49:31 +09002774 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2775 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002776 goto err_out;
2777
Albert Lee59a10b12005-10-12 15:09:42 +08002778 swap_buf_le16(dev->id, ATA_ID_WORDS);
2779
2780 ata_dump_id(dev);
2781
2782 DPRINTK("EXIT\n");
2783
2784 return;
2785err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002786 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002787 ata_port_disable(ap);
2788}
2789
2790/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002791 * ata_dev_init_params - Issue INIT DEV PARAMS command
2792 * @ap: Port associated with device @dev
2793 * @dev: Device to which command will be sent
2794 *
2795 * LOCKING:
2796 */
2797
2798static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2799{
Tejun Heoa0123702005-12-13 14:49:31 +09002800 struct ata_taskfile tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002801 u16 sectors = dev->id[6];
2802 u16 heads = dev->id[3];
2803
2804 /* Number of sectors per track 1-255. Number of heads 1-16 */
2805 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2806 return;
2807
2808 /* set up init dev params taskfile */
2809 DPRINTK("init dev params \n");
2810
Tejun Heoa0123702005-12-13 14:49:31 +09002811 ata_tf_init(ap, &tf, dev->devno);
2812 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2813 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2814 tf.protocol = ATA_PROT_NODATA;
2815 tf.nsect = sectors;
2816 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04002817
Tejun Heoa0123702005-12-13 14:49:31 +09002818 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2819 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2820 ap->id);
Albert Lee8bf62ece2005-05-12 15:29:42 -04002821 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002822 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04002823
2824 DPRINTK("EXIT\n");
2825}
2826
2827/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002828 * ata_sg_clean - Unmap DMA memory associated with command
2829 * @qc: Command containing DMA memory to be released
2830 *
2831 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 *
2833 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002834 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 */
2836
2837static void ata_sg_clean(struct ata_queued_cmd *qc)
2838{
2839 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002840 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002842 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2845 assert(sg != NULL);
2846
2847 if (qc->flags & ATA_QCFLAG_SINGLE)
2848 assert(qc->n_elem == 1);
2849
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002850 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002852 /* if we padded the buffer out to 32-bit bound, and data
2853 * xfer direction is from-device, we must copy from the
2854 * pad buffer back into the supplied buffer
2855 */
2856 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2857 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2858
2859 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002860 if (qc->n_elem)
2861 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002862 /* restore last sg */
2863 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2864 if (pad_buf) {
2865 struct scatterlist *psg = &qc->pad_sgent;
2866 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2867 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002868 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002869 }
2870 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002871 if (sg_dma_len(&sg[0]) > 0)
2872 dma_unmap_single(ap->host_set->dev,
2873 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2874 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002875 /* restore sg */
2876 sg->length += qc->pad_len;
2877 if (pad_buf)
2878 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2879 pad_buf, qc->pad_len);
2880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002883 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
2886/**
2887 * ata_fill_sg - Fill PCI IDE PRD table
2888 * @qc: Metadata associated with taskfile to be transferred
2889 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002890 * Fill PCI IDE PRD (scatter-gather) table with segments
2891 * associated with the current disk command.
2892 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002894 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 *
2896 */
2897static void ata_fill_sg(struct ata_queued_cmd *qc)
2898{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002900 struct scatterlist *sg;
2901 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002903 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 assert(qc->n_elem > 0);
2905
2906 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002907 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 u32 addr, offset;
2909 u32 sg_len, len;
2910
2911 /* determine if physical DMA addr spans 64K boundary.
2912 * Note h/w doesn't support 64-bit, so we unconditionally
2913 * truncate dma_addr_t to u32.
2914 */
2915 addr = (u32) sg_dma_address(sg);
2916 sg_len = sg_dma_len(sg);
2917
2918 while (sg_len) {
2919 offset = addr & 0xffff;
2920 len = sg_len;
2921 if ((offset + sg_len) > 0x10000)
2922 len = 0x10000 - offset;
2923
2924 ap->prd[idx].addr = cpu_to_le32(addr);
2925 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2926 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2927
2928 idx++;
2929 sg_len -= len;
2930 addr += len;
2931 }
2932 }
2933
2934 if (idx)
2935 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2936}
2937/**
2938 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2939 * @qc: Metadata associated with taskfile to check
2940 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002941 * Allow low-level driver to filter ATA PACKET commands, returning
2942 * a status indicating whether or not it is OK to use DMA for the
2943 * supplied PACKET command.
2944 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002946 * spin_lock_irqsave(host_set lock)
2947 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 * RETURNS: 0 when ATAPI DMA can be used
2949 * nonzero otherwise
2950 */
2951int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2952{
2953 struct ata_port *ap = qc->ap;
2954 int rc = 0; /* Assume ATAPI DMA is OK by default */
2955
2956 if (ap->ops->check_atapi_dma)
2957 rc = ap->ops->check_atapi_dma(qc);
2958
2959 return rc;
2960}
2961/**
2962 * ata_qc_prep - Prepare taskfile for submission
2963 * @qc: Metadata associated with taskfile to be prepared
2964 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002965 * Prepare ATA taskfile for submission.
2966 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 * LOCKING:
2968 * spin_lock_irqsave(host_set lock)
2969 */
2970void ata_qc_prep(struct ata_queued_cmd *qc)
2971{
2972 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2973 return;
2974
2975 ata_fill_sg(qc);
2976}
2977
Jeff Garzik0cba6322005-05-30 19:49:12 -04002978/**
2979 * ata_sg_init_one - Associate command with memory buffer
2980 * @qc: Command to be associated
2981 * @buf: Memory buffer
2982 * @buflen: Length of memory buffer, in bytes.
2983 *
2984 * Initialize the data-related elements of queued_cmd @qc
2985 * to point to a single memory buffer, @buf of byte length @buflen.
2986 *
2987 * LOCKING:
2988 * spin_lock_irqsave(host_set lock)
2989 */
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2992{
2993 struct scatterlist *sg;
2994
2995 qc->flags |= ATA_QCFLAG_SINGLE;
2996
2997 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002998 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003000 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 qc->buf_virt = buf;
3002
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003003 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05003004 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
Jeff Garzik0cba6322005-05-30 19:49:12 -04003007/**
3008 * ata_sg_init - Associate command with scatter-gather table.
3009 * @qc: Command to be associated
3010 * @sg: Scatter-gather table.
3011 * @n_elem: Number of elements in s/g table.
3012 *
3013 * Initialize the data-related elements of queued_cmd @qc
3014 * to point to a scatter-gather table @sg, containing @n_elem
3015 * elements.
3016 *
3017 * LOCKING:
3018 * spin_lock_irqsave(host_set lock)
3019 */
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3022 unsigned int n_elem)
3023{
3024 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003025 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003027 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
3030/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003031 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3032 * @qc: Command with memory buffer to be mapped.
3033 *
3034 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 *
3036 * LOCKING:
3037 * spin_lock_irqsave(host_set lock)
3038 *
3039 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003040 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 */
3042
3043static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3044{
3045 struct ata_port *ap = qc->ap;
3046 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003047 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 dma_addr_t dma_address;
3049
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003050 /* we must lengthen transfers to end on a 32-bit boundary */
3051 qc->pad_len = sg->length & 3;
3052 if (qc->pad_len) {
3053 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3054 struct scatterlist *psg = &qc->pad_sgent;
3055
3056 assert(qc->dev->class == ATA_DEV_ATAPI);
3057
3058 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3059
3060 if (qc->tf.flags & ATA_TFLAG_WRITE)
3061 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3062 qc->pad_len);
3063
3064 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3065 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3066 /* trim sg */
3067 sg->length -= qc->pad_len;
3068
3069 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3070 sg->length, qc->pad_len);
3071 }
3072
Jeff Garzike1410f22005-11-14 14:06:26 -05003073 if (!sg->length) {
3074 sg_dma_address(sg) = 0;
3075 goto skip_map;
3076 }
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003079 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003080 if (dma_mapping_error(dma_address)) {
3081 /* restore sg */
3082 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05003087skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04003088 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
3090 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3091 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3092
3093 return 0;
3094}
3095
3096/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003097 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3098 * @qc: Command with scatter-gather table to be mapped.
3099 *
3100 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 *
3102 * LOCKING:
3103 * spin_lock_irqsave(host_set lock)
3104 *
3105 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003106 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 *
3108 */
3109
3110static int ata_sg_setup(struct ata_queued_cmd *qc)
3111{
3112 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003113 struct scatterlist *sg = qc->__sg;
3114 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003115 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 VPRINTK("ENTER, ata%u\n", ap->id);
3118 assert(qc->flags & ATA_QCFLAG_SG);
3119
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003120 /* we must lengthen transfers to end on a 32-bit boundary */
3121 qc->pad_len = lsg->length & 3;
3122 if (qc->pad_len) {
3123 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3124 struct scatterlist *psg = &qc->pad_sgent;
3125 unsigned int offset;
3126
3127 assert(qc->dev->class == ATA_DEV_ATAPI);
3128
3129 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3130
3131 /*
3132 * psg->page/offset are used to copy to-be-written
3133 * data in this function or read data in ata_sg_clean.
3134 */
3135 offset = lsg->offset + lsg->length - qc->pad_len;
3136 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3137 psg->offset = offset_in_page(offset);
3138
3139 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3140 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3141 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003142 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003143 }
3144
3145 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3146 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3147 /* trim last sg */
3148 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003149 if (lsg->length == 0)
3150 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003151
3152 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3153 qc->n_elem - 1, lsg->length, qc->pad_len);
3154 }
3155
Jeff Garzike1410f22005-11-14 14:06:26 -05003156 pre_n_elem = qc->n_elem;
3157 if (trim_sg && pre_n_elem)
3158 pre_n_elem--;
3159
3160 if (!pre_n_elem) {
3161 n_elem = 0;
3162 goto skip_map;
3163 }
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05003166 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003167 if (n_elem < 1) {
3168 /* restore last sg */
3169 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 DPRINTK("%d sg elements mapped\n", n_elem);
3174
Jeff Garzike1410f22005-11-14 14:06:26 -05003175skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 qc->n_elem = n_elem;
3177
3178 return 0;
3179}
3180
3181/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003182 * ata_poll_qc_complete - turn irq back on and finish qc
3183 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003184 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003185 *
3186 * LOCKING:
3187 * None. (grabs host lock)
3188 */
3189
Albert Leea22e2eb2005-12-05 15:38:02 +08003190void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003191{
3192 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003193 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003194
Jeff Garzikb8f61532005-08-25 22:01:20 -04003195 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003196 ap->flags &= ~ATA_FLAG_NOINTR;
3197 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003198 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003199 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003200}
3201
3202/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003204 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 *
3206 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003207 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 *
3209 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003210 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 */
3212
3213static unsigned long ata_pio_poll(struct ata_port *ap)
3214{
Albert Leec14b8332005-12-05 15:36:08 +08003215 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003217 unsigned int poll_state = HSM_ST_UNKNOWN;
3218 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Albert Leec14b8332005-12-05 15:36:08 +08003220 qc = ata_qc_from_tag(ap, ap->active_tag);
3221 assert(qc != NULL);
3222
Albert Lee14be71f2005-09-27 17:36:35 +08003223 switch (ap->hsm_task_state) {
3224 case HSM_ST:
3225 case HSM_ST_POLL:
3226 poll_state = HSM_ST_POLL;
3227 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003229 case HSM_ST_LAST:
3230 case HSM_ST_LAST_POLL:
3231 poll_state = HSM_ST_LAST_POLL;
3232 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 break;
3234 default:
3235 BUG();
3236 break;
3237 }
3238
3239 status = ata_chk_status(ap);
3240 if (status & ATA_BUSY) {
3241 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003242 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003243 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return 0;
3245 }
Albert Lee14be71f2005-09-27 17:36:35 +08003246 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return ATA_SHORT_PAUSE;
3248 }
3249
Albert Lee14be71f2005-09-27 17:36:35 +08003250 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return 0;
3252}
3253
3254/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003255 * ata_pio_complete - check if drive is busy or idle
3256 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 *
3258 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003259 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003260 *
3261 * RETURNS:
3262 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 */
3264
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003265static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
3267 struct ata_queued_cmd *qc;
3268 u8 drv_stat;
3269
3270 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003271 * This is purely heuristic. This is a fast path. Sometimes when
3272 * we enter, BSY will be cleared in a chk-status or two. If not,
3273 * the drive is probably seeking or something. Snooze for a couple
3274 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003275 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 */
Albert Leefe79e682005-12-06 11:34:59 +08003277 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3278 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003280 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3281 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003282 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 }
3286 }
3287
Albert Leec14b8332005-12-05 15:36:08 +08003288 qc = ata_qc_from_tag(ap, ap->active_tag);
3289 assert(qc != NULL);
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 drv_stat = ata_wait_idle(ap);
3292 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003293 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003294 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 }
3297
Albert Lee14be71f2005-09-27 17:36:35 +08003298 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Albert Leea22e2eb2005-12-05 15:38:02 +08003300 assert(qc->err_mask == 0);
3301 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003302
3303 /* another command may start at this point */
3304
3305 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
Edward Falk0baab862005-06-02 18:17:13 -04003308
3309/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003310 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04003311 * @buf: Buffer to swap
3312 * @buf_words: Number of 16-bit words in buffer.
3313 *
3314 * Swap halves of 16-bit words if needed to convert from
3315 * little-endian byte order to native cpu byte order, or
3316 * vice-versa.
3317 *
3318 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003319 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321void swap_buf_le16(u16 *buf, unsigned int buf_words)
3322{
3323#ifdef __BIG_ENDIAN
3324 unsigned int i;
3325
3326 for (i = 0; i < buf_words; i++)
3327 buf[i] = le16_to_cpu(buf[i]);
3328#endif /* __BIG_ENDIAN */
3329}
3330
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003331/**
3332 * ata_mmio_data_xfer - Transfer data by MMIO
3333 * @ap: port to read/write
3334 * @buf: data buffer
3335 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003336 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003337 *
3338 * Transfer data from/to the device data register by MMIO.
3339 *
3340 * LOCKING:
3341 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003342 */
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3345 unsigned int buflen, int write_data)
3346{
3347 unsigned int i;
3348 unsigned int words = buflen >> 1;
3349 u16 *buf16 = (u16 *) buf;
3350 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3351
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003352 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 if (write_data) {
3354 for (i = 0; i < words; i++)
3355 writew(le16_to_cpu(buf16[i]), mmio);
3356 } else {
3357 for (i = 0; i < words; i++)
3358 buf16[i] = cpu_to_le16(readw(mmio));
3359 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003360
3361 /* Transfer trailing 1 byte, if any. */
3362 if (unlikely(buflen & 0x01)) {
3363 u16 align_buf[1] = { 0 };
3364 unsigned char *trailing_buf = buf + buflen - 1;
3365
3366 if (write_data) {
3367 memcpy(align_buf, trailing_buf, 1);
3368 writew(le16_to_cpu(align_buf[0]), mmio);
3369 } else {
3370 align_buf[0] = cpu_to_le16(readw(mmio));
3371 memcpy(trailing_buf, align_buf, 1);
3372 }
3373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374}
3375
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003376/**
3377 * ata_pio_data_xfer - Transfer data by PIO
3378 * @ap: port to read/write
3379 * @buf: data buffer
3380 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003381 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003382 *
3383 * Transfer data from/to the device data register by PIO.
3384 *
3385 * LOCKING:
3386 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003387 */
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3390 unsigned int buflen, int write_data)
3391{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003392 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003394 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003396 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003398 insw(ap->ioaddr.data_addr, buf, words);
3399
3400 /* Transfer trailing 1 byte, if any. */
3401 if (unlikely(buflen & 0x01)) {
3402 u16 align_buf[1] = { 0 };
3403 unsigned char *trailing_buf = buf + buflen - 1;
3404
3405 if (write_data) {
3406 memcpy(align_buf, trailing_buf, 1);
3407 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3408 } else {
3409 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3410 memcpy(trailing_buf, align_buf, 1);
3411 }
3412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003415/**
3416 * ata_data_xfer - Transfer data from/to the data register.
3417 * @ap: port to read/write
3418 * @buf: data buffer
3419 * @buflen: buffer length
3420 * @do_write: read/write
3421 *
3422 * Transfer data from/to the device data register.
3423 *
3424 * LOCKING:
3425 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003426 */
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3429 unsigned int buflen, int do_write)
3430{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003431 /* Make the crap hardware pay the costs not the good stuff */
3432 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3433 unsigned long flags;
3434 local_irq_save(flags);
3435 if (ap->flags & ATA_FLAG_MMIO)
3436 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3437 else
3438 ata_pio_data_xfer(ap, buf, buflen, do_write);
3439 local_irq_restore(flags);
3440 } else {
3441 if (ap->flags & ATA_FLAG_MMIO)
3442 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3443 else
3444 ata_pio_data_xfer(ap, buf, buflen, do_write);
3445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446}
3447
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003448/**
3449 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3450 * @qc: Command on going
3451 *
3452 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3453 *
3454 * LOCKING:
3455 * Inherited from caller.
3456 */
3457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458static void ata_pio_sector(struct ata_queued_cmd *qc)
3459{
3460 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003461 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 struct ata_port *ap = qc->ap;
3463 struct page *page;
3464 unsigned int offset;
3465 unsigned char *buf;
3466
3467 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003468 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470 page = sg[qc->cursg].page;
3471 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3472
3473 /* get the current page and offset */
3474 page = nth_page(page, (offset >> PAGE_SHIFT));
3475 offset %= PAGE_SIZE;
3476
3477 buf = kmap(page) + offset;
3478
3479 qc->cursect++;
3480 qc->cursg_ofs++;
3481
Albert Lee32529e02005-05-26 03:49:42 -04003482 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 qc->cursg++;
3484 qc->cursg_ofs = 0;
3485 }
3486
3487 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3488
3489 /* do the actual data transfer */
3490 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3491 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3492
3493 kunmap(page);
3494}
3495
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003496/**
3497 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3498 * @qc: Command on going
3499 * @bytes: number of bytes
3500 *
3501 * Transfer Transfer data from/to the ATAPI device.
3502 *
3503 * LOCKING:
3504 * Inherited from caller.
3505 *
3506 */
3507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3509{
3510 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003511 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 struct ata_port *ap = qc->ap;
3513 struct page *page;
3514 unsigned char *buf;
3515 unsigned int offset, count;
3516
Albert Lee563a6e12005-08-12 14:17:50 +08003517 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003518 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
3520next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003521 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003522 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003523 * The end of qc->sg is reached and the device expects
3524 * more data to transfer. In order not to overrun qc->sg
3525 * and fulfill length specified in the byte count register,
3526 * - for read case, discard trailing data from the device
3527 * - for write case, padding zero data to the device
3528 */
3529 u16 pad_buf[1] = { 0 };
3530 unsigned int words = bytes >> 1;
3531 unsigned int i;
3532
3533 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003534 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003535 ap->id, bytes);
3536
3537 for (i = 0; i < words; i++)
3538 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3539
Albert Lee14be71f2005-09-27 17:36:35 +08003540 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003541 return;
3542 }
3543
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003544 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 page = sg->page;
3547 offset = sg->offset + qc->cursg_ofs;
3548
3549 /* get the current page and offset */
3550 page = nth_page(page, (offset >> PAGE_SHIFT));
3551 offset %= PAGE_SIZE;
3552
Albert Lee6952df02005-06-06 15:56:03 +08003553 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003554 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 /* don't cross page boundaries */
3557 count = min(count, (unsigned int)PAGE_SIZE - offset);
3558
3559 buf = kmap(page) + offset;
3560
3561 bytes -= count;
3562 qc->curbytes += count;
3563 qc->cursg_ofs += count;
3564
Albert Lee32529e02005-05-26 03:49:42 -04003565 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 qc->cursg++;
3567 qc->cursg_ofs = 0;
3568 }
3569
3570 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3571
3572 /* do the actual data transfer */
3573 ata_data_xfer(ap, buf, count, do_write);
3574
3575 kunmap(page);
3576
Albert Lee563a6e12005-08-12 14:17:50 +08003577 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003581/**
3582 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3583 * @qc: Command on going
3584 *
3585 * Transfer Transfer data from/to the ATAPI device.
3586 *
3587 * LOCKING:
3588 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003589 */
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3592{
3593 struct ata_port *ap = qc->ap;
3594 struct ata_device *dev = qc->dev;
3595 unsigned int ireason, bc_lo, bc_hi, bytes;
3596 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3597
3598 ap->ops->tf_read(ap, &qc->tf);
3599 ireason = qc->tf.nsect;
3600 bc_lo = qc->tf.lbam;
3601 bc_hi = qc->tf.lbah;
3602 bytes = (bc_hi << 8) | bc_lo;
3603
3604 /* shall be cleared to zero, indicating xfer of data */
3605 if (ireason & (1 << 0))
3606 goto err_out;
3607
3608 /* make sure transfer direction matches expected */
3609 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3610 if (do_write != i_write)
3611 goto err_out;
3612
3613 __atapi_pio_bytes(qc, bytes);
3614
3615 return;
3616
3617err_out:
3618 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3619 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003620 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003621 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
3624/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003625 * ata_pio_block - start PIO on a block
3626 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 *
3628 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003629 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 */
3631
3632static void ata_pio_block(struct ata_port *ap)
3633{
3634 struct ata_queued_cmd *qc;
3635 u8 status;
3636
3637 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003638 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 * Sometimes when we enter, BSY will be cleared in
3640 * a chk-status or two. If not, the drive is probably seeking
3641 * or something. Snooze for a couple msecs, then
3642 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003643 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 */
3645 status = ata_busy_wait(ap, ATA_BUSY, 5);
3646 if (status & ATA_BUSY) {
3647 msleep(2);
3648 status = ata_busy_wait(ap, ATA_BUSY, 10);
3649 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003650 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3652 return;
3653 }
3654 }
3655
3656 qc = ata_qc_from_tag(ap, ap->active_tag);
3657 assert(qc != NULL);
3658
Albert Leefe79e682005-12-06 11:34:59 +08003659 /* check error */
3660 if (status & (ATA_ERR | ATA_DF)) {
3661 qc->err_mask |= AC_ERR_DEV;
3662 ap->hsm_task_state = HSM_ST_ERR;
3663 return;
3664 }
3665
3666 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003668 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003670 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return;
3672 }
3673
3674 atapi_pio_bytes(qc);
3675 } else {
3676 /* handle BSY=0, DRQ=0 as error */
3677 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003678 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003679 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 return;
3681 }
3682
3683 ata_pio_sector(qc);
3684 }
3685}
3686
3687static void ata_pio_error(struct ata_port *ap)
3688{
3689 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003690
3691 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
3693 qc = ata_qc_from_tag(ap, ap->active_tag);
3694 assert(qc != NULL);
3695
Albert Lee1c848982005-12-05 15:40:15 +08003696 /* make sure qc->err_mask is available to
3697 * know what's wrong and recover
3698 */
3699 assert(qc->err_mask);
3700
Albert Lee14be71f2005-09-27 17:36:35 +08003701 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Albert Leea22e2eb2005-12-05 15:38:02 +08003703 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704}
3705
3706static void ata_pio_task(void *_data)
3707{
3708 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003709 unsigned long timeout;
3710 int qc_completed;
3711
3712fsm_start:
3713 timeout = 0;
3714 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Albert Lee14be71f2005-09-27 17:36:35 +08003716 switch (ap->hsm_task_state) {
3717 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 return;
3719
Albert Lee14be71f2005-09-27 17:36:35 +08003720 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 ata_pio_block(ap);
3722 break;
3723
Albert Lee14be71f2005-09-27 17:36:35 +08003724 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003725 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 break;
3727
Albert Lee14be71f2005-09-27 17:36:35 +08003728 case HSM_ST_POLL:
3729 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 timeout = ata_pio_poll(ap);
3731 break;
3732
Albert Lee14be71f2005-09-27 17:36:35 +08003733 case HSM_ST_TMOUT:
3734 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 ata_pio_error(ap);
3736 return;
3737 }
3738
3739 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003740 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003741 else if (!qc_completed)
3742 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
3744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745/**
3746 * ata_qc_timeout - Handle timeout of queued command
3747 * @qc: Command that timed out
3748 *
3749 * Some part of the kernel (currently, only the SCSI layer)
3750 * has noticed that the active command on port @ap has not
3751 * completed after a specified length of time. Handle this
3752 * condition by disabling DMA (if necessary) and completing
3753 * transactions, with error if necessary.
3754 *
3755 * This also handles the case of the "lost interrupt", where
3756 * for some reason (possibly hardware bug, possibly driver bug)
3757 * an interrupt was not delivered to the driver, even though the
3758 * transaction completed successfully.
3759 *
3760 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003761 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 */
3763
3764static void ata_qc_timeout(struct ata_queued_cmd *qc)
3765{
3766 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003767 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003769 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
3771 DPRINTK("ENTER\n");
3772
Jeff Garzikb8f61532005-08-25 22:01:20 -04003773 spin_lock_irqsave(&host_set->lock, flags);
3774
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 switch (qc->tf.protocol) {
3776
3777 case ATA_PROT_DMA:
3778 case ATA_PROT_ATAPI_DMA:
3779 host_stat = ap->ops->bmdma_status(ap);
3780
3781 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003782 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
3784 /* fall through */
3785
3786 default:
3787 ata_altstatus(ap);
3788 drv_stat = ata_chk_status(ap);
3789
3790 /* ack bmdma irq events */
3791 ap->ops->irq_clear(ap);
3792
3793 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3794 ap->id, qc->tf.command, drv_stat, host_stat);
3795
3796 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003797 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 break;
3799 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003800
3801 spin_unlock_irqrestore(&host_set->lock, flags);
3802
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003803 ata_eh_qc_complete(qc);
3804
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 DPRINTK("EXIT\n");
3806}
3807
3808/**
3809 * ata_eng_timeout - Handle timeout of queued command
3810 * @ap: Port on which timed-out command is active
3811 *
3812 * Some part of the kernel (currently, only the SCSI layer)
3813 * has noticed that the active command on port @ap has not
3814 * completed after a specified length of time. Handle this
3815 * condition by disabling DMA (if necessary) and completing
3816 * transactions, with error if necessary.
3817 *
3818 * This also handles the case of the "lost interrupt", where
3819 * for some reason (possibly hardware bug, possibly driver bug)
3820 * an interrupt was not delivered to the driver, even though the
3821 * transaction completed successfully.
3822 *
3823 * LOCKING:
3824 * Inherited from SCSI layer (none, can sleep)
3825 */
3826
3827void ata_eng_timeout(struct ata_port *ap)
3828{
3829 struct ata_queued_cmd *qc;
3830
3831 DPRINTK("ENTER\n");
3832
3833 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003834 if (qc)
3835 ata_qc_timeout(qc);
3836 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3838 ap->id);
3839 goto out;
3840 }
3841
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842out:
3843 DPRINTK("EXIT\n");
3844}
3845
3846/**
3847 * ata_qc_new - Request an available ATA command, for queueing
3848 * @ap: Port associated with device @dev
3849 * @dev: Device from whom we request an available command structure
3850 *
3851 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003852 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 */
3854
3855static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3856{
3857 struct ata_queued_cmd *qc = NULL;
3858 unsigned int i;
3859
3860 for (i = 0; i < ATA_MAX_QUEUE; i++)
3861 if (!test_and_set_bit(i, &ap->qactive)) {
3862 qc = ata_qc_from_tag(ap, i);
3863 break;
3864 }
3865
3866 if (qc)
3867 qc->tag = i;
3868
3869 return qc;
3870}
3871
3872/**
3873 * ata_qc_new_init - Request an available ATA command, and initialize it
3874 * @ap: Port associated with device @dev
3875 * @dev: Device from whom we request an available command structure
3876 *
3877 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003878 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 */
3880
3881struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3882 struct ata_device *dev)
3883{
3884 struct ata_queued_cmd *qc;
3885
3886 qc = ata_qc_new(ap);
3887 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 qc->scsicmd = NULL;
3889 qc->ap = ap;
3890 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003892 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
3894
3895 return qc;
3896}
3897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898/**
3899 * ata_qc_free - free unused ata_queued_cmd
3900 * @qc: Command to complete
3901 *
3902 * Designed to free unused ata_queued_cmd object
3903 * in case something prevents using it.
3904 *
3905 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003906 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 */
3908void ata_qc_free(struct ata_queued_cmd *qc)
3909{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003910 struct ata_port *ap = qc->ap;
3911 unsigned int tag;
3912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Tejun Heo4ba946e2006-01-23 13:09:36 +09003915 qc->flags = 0;
3916 tag = qc->tag;
3917 if (likely(ata_tag_valid(tag))) {
3918 if (tag == ap->active_tag)
3919 ap->active_tag = ATA_TAG_POISON;
3920 qc->tag = ATA_TAG_POISON;
3921 clear_bit(tag, &ap->qactive);
3922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923}
3924
3925/**
3926 * ata_qc_complete - Complete an active ATA command
3927 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003928 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003929 *
3930 * Indicate to the mid and upper layers that an ATA
3931 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 *
3933 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003934 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 */
3936
Albert Leea22e2eb2005-12-05 15:38:02 +08003937void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3940 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3941
3942 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3943 ata_sg_clean(qc);
3944
Albert Lee3f3791d2005-08-16 14:25:38 +08003945 /* atapi: mark qc as inactive to prevent the interrupt handler
3946 * from completing the command twice later, before the error handler
3947 * is called. (when rc != 0 and atapi request sense is needed)
3948 */
3949 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003952 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953}
3954
3955static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3956{
3957 struct ata_port *ap = qc->ap;
3958
3959 switch (qc->tf.protocol) {
3960 case ATA_PROT_DMA:
3961 case ATA_PROT_ATAPI_DMA:
3962 return 1;
3963
3964 case ATA_PROT_ATAPI:
3965 case ATA_PROT_PIO:
3966 case ATA_PROT_PIO_MULT:
3967 if (ap->flags & ATA_FLAG_PIO_DMA)
3968 return 1;
3969
3970 /* fall through */
3971
3972 default:
3973 return 0;
3974 }
3975
3976 /* never reached */
3977}
3978
3979/**
3980 * ata_qc_issue - issue taskfile to device
3981 * @qc: command to issue to device
3982 *
3983 * Prepare an ATA command to submission to device.
3984 * This includes mapping the data into a DMA-able
3985 * area, filling in the S/G table, and finally
3986 * writing the taskfile to hardware, starting the command.
3987 *
3988 * LOCKING:
3989 * spin_lock_irqsave(host_set lock)
3990 *
3991 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003992 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 */
3994
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003995unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
3997 struct ata_port *ap = qc->ap;
3998
3999 if (ata_should_dma_map(qc)) {
4000 if (qc->flags & ATA_QCFLAG_SG) {
4001 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004002 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4004 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004005 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 }
4007 } else {
4008 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4009 }
4010
4011 ap->ops->qc_prep(qc);
4012
4013 qc->ap->active_tag = qc->tag;
4014 qc->flags |= ATA_QCFLAG_ACTIVE;
4015
4016 return ap->ops->qc_issue(qc);
4017
Tejun Heo8e436af2006-01-23 13:09:36 +09004018sg_err:
4019 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004020 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021}
4022
Edward Falk0baab862005-06-02 18:17:13 -04004023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024/**
4025 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4026 * @qc: command to issue to device
4027 *
4028 * Using various libata functions and hooks, this function
4029 * starts an ATA command. ATA commands are grouped into
4030 * classes called "protocols", and issuing each type of protocol
4031 * is slightly different.
4032 *
Edward Falk0baab862005-06-02 18:17:13 -04004033 * May be used as the qc_issue() entry in ata_port_operations.
4034 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 * LOCKING:
4036 * spin_lock_irqsave(host_set lock)
4037 *
4038 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004039 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 */
4041
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004042unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
4044 struct ata_port *ap = qc->ap;
4045
4046 ata_dev_select(ap, qc->dev->devno, 1, 0);
4047
4048 switch (qc->tf.protocol) {
4049 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05004050 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 break;
4052
4053 case ATA_PROT_DMA:
4054 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4055 ap->ops->bmdma_setup(qc); /* set up bmdma */
4056 ap->ops->bmdma_start(qc); /* initiate bmdma */
4057 break;
4058
4059 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4060 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004061 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08004062 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004063 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 break;
4065
4066 case ATA_PROT_ATAPI:
4067 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004068 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09004069 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 break;
4071
4072 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09004073 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05004074 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09004075 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 break;
4077
4078 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09004079 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4081 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09004082 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 break;
4084
4085 default:
4086 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004087 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
4089
4090 return 0;
4091}
4092
4093/**
Edward Falk0baab862005-06-02 18:17:13 -04004094 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 * @qc: Info associated with this ATA transaction.
4096 *
4097 * LOCKING:
4098 * spin_lock_irqsave(host_set lock)
4099 */
4100
4101static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4102{
4103 struct ata_port *ap = qc->ap;
4104 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4105 u8 dmactl;
4106 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4107
4108 /* load PRD table addr. */
4109 mb(); /* make sure PRD table writes are visible to controller */
4110 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4111
4112 /* specify data direction, triple-check start bit is clear */
4113 dmactl = readb(mmio + ATA_DMA_CMD);
4114 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4115 if (!rw)
4116 dmactl |= ATA_DMA_WR;
4117 writeb(dmactl, mmio + ATA_DMA_CMD);
4118
4119 /* issue r/w command */
4120 ap->ops->exec_command(ap, &qc->tf);
4121}
4122
4123/**
Alan Coxb73fc892005-08-26 16:03:19 +01004124 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 * @qc: Info associated with this ATA transaction.
4126 *
4127 * LOCKING:
4128 * spin_lock_irqsave(host_set lock)
4129 */
4130
4131static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4132{
4133 struct ata_port *ap = qc->ap;
4134 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4135 u8 dmactl;
4136
4137 /* start host DMA transaction */
4138 dmactl = readb(mmio + ATA_DMA_CMD);
4139 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4140
4141 /* Strictly, one may wish to issue a readb() here, to
4142 * flush the mmio write. However, control also passes
4143 * to the hardware at this point, and it will interrupt
4144 * us when we are to resume control. So, in effect,
4145 * we don't care when the mmio write flushes.
4146 * Further, a read of the DMA status register _immediately_
4147 * following the write may not be what certain flaky hardware
4148 * is expected, so I think it is best to not add a readb()
4149 * without first all the MMIO ATA cards/mobos.
4150 * Or maybe I'm just being paranoid.
4151 */
4152}
4153
4154/**
4155 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4156 * @qc: Info associated with this ATA transaction.
4157 *
4158 * LOCKING:
4159 * spin_lock_irqsave(host_set lock)
4160 */
4161
4162static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4163{
4164 struct ata_port *ap = qc->ap;
4165 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4166 u8 dmactl;
4167
4168 /* load PRD table addr. */
4169 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4170
4171 /* specify data direction, triple-check start bit is clear */
4172 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4173 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4174 if (!rw)
4175 dmactl |= ATA_DMA_WR;
4176 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4177
4178 /* issue r/w command */
4179 ap->ops->exec_command(ap, &qc->tf);
4180}
4181
4182/**
4183 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4184 * @qc: Info associated with this ATA transaction.
4185 *
4186 * LOCKING:
4187 * spin_lock_irqsave(host_set lock)
4188 */
4189
4190static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4191{
4192 struct ata_port *ap = qc->ap;
4193 u8 dmactl;
4194
4195 /* start host DMA transaction */
4196 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4197 outb(dmactl | ATA_DMA_START,
4198 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4199}
4200
Edward Falk0baab862005-06-02 18:17:13 -04004201
4202/**
4203 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4204 * @qc: Info associated with this ATA transaction.
4205 *
4206 * Writes the ATA_DMA_START flag to the DMA command register.
4207 *
4208 * May be used as the bmdma_start() entry in ata_port_operations.
4209 *
4210 * LOCKING:
4211 * spin_lock_irqsave(host_set lock)
4212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213void ata_bmdma_start(struct ata_queued_cmd *qc)
4214{
4215 if (qc->ap->flags & ATA_FLAG_MMIO)
4216 ata_bmdma_start_mmio(qc);
4217 else
4218 ata_bmdma_start_pio(qc);
4219}
4220
Edward Falk0baab862005-06-02 18:17:13 -04004221
4222/**
4223 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4224 * @qc: Info associated with this ATA transaction.
4225 *
4226 * Writes address of PRD table to device's PRD Table Address
4227 * register, sets the DMA control register, and calls
4228 * ops->exec_command() to start the transfer.
4229 *
4230 * May be used as the bmdma_setup() entry in ata_port_operations.
4231 *
4232 * LOCKING:
4233 * spin_lock_irqsave(host_set lock)
4234 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235void ata_bmdma_setup(struct ata_queued_cmd *qc)
4236{
4237 if (qc->ap->flags & ATA_FLAG_MMIO)
4238 ata_bmdma_setup_mmio(qc);
4239 else
4240 ata_bmdma_setup_pio(qc);
4241}
4242
Edward Falk0baab862005-06-02 18:17:13 -04004243
4244/**
4245 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004246 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004247 *
4248 * Clear interrupt and error flags in DMA status register.
4249 *
4250 * May be used as the irq_clear() entry in ata_port_operations.
4251 *
4252 * LOCKING:
4253 * spin_lock_irqsave(host_set lock)
4254 */
4255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256void ata_bmdma_irq_clear(struct ata_port *ap)
4257{
4258 if (ap->flags & ATA_FLAG_MMIO) {
4259 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4260 writeb(readb(mmio), mmio);
4261 } else {
4262 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4263 outb(inb(addr), addr);
4264 }
4265
4266}
4267
Edward Falk0baab862005-06-02 18:17:13 -04004268
4269/**
4270 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004271 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004272 *
4273 * Read and return BMDMA status register.
4274 *
4275 * May be used as the bmdma_status() entry in ata_port_operations.
4276 *
4277 * LOCKING:
4278 * spin_lock_irqsave(host_set lock)
4279 */
4280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281u8 ata_bmdma_status(struct ata_port *ap)
4282{
4283 u8 host_stat;
4284 if (ap->flags & ATA_FLAG_MMIO) {
4285 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4286 host_stat = readb(mmio + ATA_DMA_STATUS);
4287 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004288 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 return host_stat;
4290}
4291
Edward Falk0baab862005-06-02 18:17:13 -04004292
4293/**
4294 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004295 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004296 *
4297 * Clears the ATA_DMA_START flag in the dma control register
4298 *
4299 * May be used as the bmdma_stop() entry in ata_port_operations.
4300 *
4301 * LOCKING:
4302 * spin_lock_irqsave(host_set lock)
4303 */
4304
Alan Coxb73fc892005-08-26 16:03:19 +01004305void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
Alan Coxb73fc892005-08-26 16:03:19 +01004307 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 if (ap->flags & ATA_FLAG_MMIO) {
4309 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4310
4311 /* clear start/stop bit */
4312 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4313 mmio + ATA_DMA_CMD);
4314 } else {
4315 /* clear start/stop bit */
4316 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4317 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4318 }
4319
4320 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4321 ata_altstatus(ap); /* dummy read */
4322}
4323
4324/**
4325 * ata_host_intr - Handle host interrupt for given (port, task)
4326 * @ap: Port on which interrupt arrived (possibly...)
4327 * @qc: Taskfile currently active in engine
4328 *
4329 * Handle host interrupt for given queued command. Currently,
4330 * only DMA interrupts are handled. All other commands are
4331 * handled via polling with interrupts disabled (nIEN bit).
4332 *
4333 * LOCKING:
4334 * spin_lock_irqsave(host_set lock)
4335 *
4336 * RETURNS:
4337 * One if interrupt was handled, zero if not (shared irq).
4338 */
4339
4340inline unsigned int ata_host_intr (struct ata_port *ap,
4341 struct ata_queued_cmd *qc)
4342{
4343 u8 status, host_stat;
4344
4345 switch (qc->tf.protocol) {
4346
4347 case ATA_PROT_DMA:
4348 case ATA_PROT_ATAPI_DMA:
4349 case ATA_PROT_ATAPI:
4350 /* check status of DMA engine */
4351 host_stat = ap->ops->bmdma_status(ap);
4352 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4353
4354 /* if it's not our irq... */
4355 if (!(host_stat & ATA_DMA_INTR))
4356 goto idle_irq;
4357
4358 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004359 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
4361 /* fall through */
4362
4363 case ATA_PROT_ATAPI_NODATA:
4364 case ATA_PROT_NODATA:
4365 /* check altstatus */
4366 status = ata_altstatus(ap);
4367 if (status & ATA_BUSY)
4368 goto idle_irq;
4369
4370 /* check main status, clearing INTRQ */
4371 status = ata_chk_status(ap);
4372 if (unlikely(status & ATA_BUSY))
4373 goto idle_irq;
4374 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4375 ap->id, qc->tf.protocol, status);
4376
4377 /* ack bmdma irq events */
4378 ap->ops->irq_clear(ap);
4379
4380 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004381 qc->err_mask |= ac_err_mask(status);
4382 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 break;
4384
4385 default:
4386 goto idle_irq;
4387 }
4388
4389 return 1; /* irq handled */
4390
4391idle_irq:
4392 ap->stats.idle_irq++;
4393
4394#ifdef ATA_IRQ_TRAP
4395 if ((ap->stats.idle_irq % 1000) == 0) {
4396 handled = 1;
4397 ata_irq_ack(ap, 0); /* debug trap */
4398 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4399 }
4400#endif
4401 return 0; /* irq not handled */
4402}
4403
4404/**
4405 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004406 * @irq: irq line (unused)
4407 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 * @regs: unused
4409 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004410 * Default interrupt handler for PCI IDE devices. Calls
4411 * ata_host_intr() for each port that is not disabled.
4412 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004414 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 *
4416 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004417 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 */
4419
4420irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4421{
4422 struct ata_host_set *host_set = dev_instance;
4423 unsigned int i;
4424 unsigned int handled = 0;
4425 unsigned long flags;
4426
4427 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4428 spin_lock_irqsave(&host_set->lock, flags);
4429
4430 for (i = 0; i < host_set->n_ports; i++) {
4431 struct ata_port *ap;
4432
4433 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004434 if (ap &&
4435 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 struct ata_queued_cmd *qc;
4437
4438 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004439 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4440 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 handled |= ata_host_intr(ap, qc);
4442 }
4443 }
4444
4445 spin_unlock_irqrestore(&host_set->lock, flags);
4446
4447 return IRQ_RETVAL(handled);
4448}
4449
4450/**
4451 * atapi_packet_task - Write CDB bytes to hardware
4452 * @_data: Port to which ATAPI device is attached.
4453 *
4454 * When device has indicated its readiness to accept
4455 * a CDB, this function is called. Send the CDB.
4456 * If DMA is to be performed, exit immediately.
4457 * Otherwise, we are in polling mode, so poll
4458 * status under operation succeeds or fails.
4459 *
4460 * LOCKING:
4461 * Kernel thread context (may sleep)
4462 */
4463
4464static void atapi_packet_task(void *_data)
4465{
4466 struct ata_port *ap = _data;
4467 struct ata_queued_cmd *qc;
4468 u8 status;
4469
4470 qc = ata_qc_from_tag(ap, ap->active_tag);
4471 assert(qc != NULL);
4472 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4473
4474 /* sleep-wait for BSY to clear */
4475 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004476 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004477 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004478 goto err_out;
4479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
4481 /* make sure DRQ is set */
4482 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004483 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004484 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
4488 /* send SCSI cdb */
4489 DPRINTK("send cdb\n");
4490 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Tejun Heoc1389502005-08-22 14:59:24 +09004492 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4493 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4494 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
Tejun Heoc1389502005-08-22 14:59:24 +09004496 /* Once we're done issuing command and kicking bmdma,
4497 * irq handler takes over. To not lose irq, we need
4498 * to clear NOINTR flag before sending cdb, but
4499 * interrupt handler shouldn't be invoked before we're
4500 * finished. Hence, the following locking.
4501 */
4502 spin_lock_irqsave(&ap->host_set->lock, flags);
4503 ap->flags &= ~ATA_FLAG_NOINTR;
4504 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4505 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4506 ap->ops->bmdma_start(qc); /* initiate bmdma */
4507 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4508 } else {
4509 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Tejun Heoc1389502005-08-22 14:59:24 +09004511 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004512 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004513 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 }
4515
4516 return;
4517
4518err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004519 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
Edward Falk0baab862005-06-02 18:17:13 -04004522
4523/**
4524 * ata_port_start - Set port up for dma.
4525 * @ap: Port to initialize
4526 *
4527 * Called just after data structures for each port are
4528 * initialized. Allocates space for PRD table.
4529 *
4530 * May be used as the port_start() entry in ata_port_operations.
4531 *
4532 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004533 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004534 */
4535
Jens Axboe9b847542006-01-06 09:28:07 +01004536/*
4537 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4538 * without filling any other registers
4539 */
4540static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4541 u8 cmd)
4542{
4543 struct ata_taskfile tf;
4544 int err;
4545
4546 ata_tf_init(ap, &tf, dev->devno);
4547
4548 tf.command = cmd;
4549 tf.flags |= ATA_TFLAG_DEVICE;
4550 tf.protocol = ATA_PROT_NODATA;
4551
4552 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4553 if (err)
4554 printk(KERN_ERR "%s: ata command failed: %d\n",
4555 __FUNCTION__, err);
4556
4557 return err;
4558}
4559
4560static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4561{
4562 u8 cmd;
4563
4564 if (!ata_try_flush_cache(dev))
4565 return 0;
4566
4567 if (ata_id_has_flush_ext(dev->id))
4568 cmd = ATA_CMD_FLUSH_EXT;
4569 else
4570 cmd = ATA_CMD_FLUSH;
4571
4572 return ata_do_simple_cmd(ap, dev, cmd);
4573}
4574
4575static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4576{
4577 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4578}
4579
4580static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4581{
4582 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4583}
4584
4585/**
4586 * ata_device_resume - wakeup a previously suspended devices
4587 *
4588 * Kick the drive back into action, by sending it an idle immediate
4589 * command and making sure its transfer mode matches between drive
4590 * and host.
4591 *
4592 */
4593int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4594{
4595 if (ap->flags & ATA_FLAG_SUSPENDED) {
4596 ap->flags &= ~ATA_FLAG_SUSPENDED;
4597 ata_set_mode(ap);
4598 }
4599 if (!ata_dev_present(dev))
4600 return 0;
4601 if (dev->class == ATA_DEV_ATA)
4602 ata_start_drive(ap, dev);
4603
4604 return 0;
4605}
4606
4607/**
4608 * ata_device_suspend - prepare a device for suspend
4609 *
4610 * Flush the cache on the drive, if appropriate, then issue a
4611 * standbynow command.
4612 *
4613 */
4614int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4615{
4616 if (!ata_dev_present(dev))
4617 return 0;
4618 if (dev->class == ATA_DEV_ATA)
4619 ata_flush_cache(ap, dev);
4620
4621 ata_standby_drive(ap, dev);
4622 ap->flags |= ATA_FLAG_SUSPENDED;
4623 return 0;
4624}
4625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626int ata_port_start (struct ata_port *ap)
4627{
4628 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004629 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
4631 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4632 if (!ap->prd)
4633 return -ENOMEM;
4634
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004635 rc = ata_pad_alloc(ap, dev);
4636 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004637 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004638 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004639 }
4640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4642
4643 return 0;
4644}
4645
Edward Falk0baab862005-06-02 18:17:13 -04004646
4647/**
4648 * ata_port_stop - Undo ata_port_start()
4649 * @ap: Port to shut down
4650 *
4651 * Frees the PRD table.
4652 *
4653 * May be used as the port_stop() entry in ata_port_operations.
4654 *
4655 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004656 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004657 */
4658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659void ata_port_stop (struct ata_port *ap)
4660{
4661 struct device *dev = ap->host_set->dev;
4662
4663 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004664 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004667void ata_host_stop (struct ata_host_set *host_set)
4668{
4669 if (host_set->mmio_base)
4670 iounmap(host_set->mmio_base);
4671}
4672
4673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674/**
4675 * ata_host_remove - Unregister SCSI host structure with upper layers
4676 * @ap: Port to unregister
4677 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4678 *
4679 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004680 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 */
4682
4683static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4684{
4685 struct Scsi_Host *sh = ap->host;
4686
4687 DPRINTK("ENTER\n");
4688
4689 if (do_unregister)
4690 scsi_remove_host(sh);
4691
4692 ap->ops->port_stop(ap);
4693}
4694
4695/**
4696 * ata_host_init - Initialize an ata_port structure
4697 * @ap: Structure to initialize
4698 * @host: associated SCSI mid-layer structure
4699 * @host_set: Collection of hosts to which @ap belongs
4700 * @ent: Probe information provided by low-level driver
4701 * @port_no: Port number associated with this ata_port
4702 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004703 * Initialize a new ata_port structure, and its associated
4704 * scsi_host.
4705 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004707 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 */
4709
4710static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4711 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004712 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713{
4714 unsigned int i;
4715
4716 host->max_id = 16;
4717 host->max_lun = 1;
4718 host->max_channel = 1;
4719 host->unique_id = ata_unique_id++;
4720 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004721
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 ap->flags = ATA_FLAG_PORT_DISABLED;
4723 ap->id = host->unique_id;
4724 ap->host = host;
4725 ap->ctl = ATA_DEVCTL_OBS;
4726 ap->host_set = host_set;
4727 ap->port_no = port_no;
4728 ap->hard_port_no =
4729 ent->legacy_mode ? ent->hard_port_no : port_no;
4730 ap->pio_mask = ent->pio_mask;
4731 ap->mwdma_mask = ent->mwdma_mask;
4732 ap->udma_mask = ent->udma_mask;
4733 ap->flags |= ent->host_flags;
4734 ap->ops = ent->port_ops;
4735 ap->cbl = ATA_CBL_NONE;
4736 ap->active_tag = ATA_TAG_POISON;
4737 ap->last_ctl = 0xFF;
4738
4739 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4740 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004741 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
4743 for (i = 0; i < ATA_MAX_DEVICES; i++)
4744 ap->device[i].devno = i;
4745
4746#ifdef ATA_IRQ_TRAP
4747 ap->stats.unhandled_irq = 1;
4748 ap->stats.idle_irq = 1;
4749#endif
4750
4751 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4752}
4753
4754/**
4755 * ata_host_add - Attach low-level ATA driver to system
4756 * @ent: Information provided by low-level driver
4757 * @host_set: Collections of ports to which we add
4758 * @port_no: Port number associated with this host
4759 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004760 * Attach low-level ATA driver to system.
4761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004763 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 *
4765 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004766 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 */
4768
Jeff Garzik057ace52005-10-22 14:27:05 -04004769static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 struct ata_host_set *host_set,
4771 unsigned int port_no)
4772{
4773 struct Scsi_Host *host;
4774 struct ata_port *ap;
4775 int rc;
4776
4777 DPRINTK("ENTER\n");
4778 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4779 if (!host)
4780 return NULL;
4781
4782 ap = (struct ata_port *) &host->hostdata[0];
4783
4784 ata_host_init(ap, host, host_set, ent, port_no);
4785
4786 rc = ap->ops->port_start(ap);
4787 if (rc)
4788 goto err_out;
4789
4790 return ap;
4791
4792err_out:
4793 scsi_host_put(host);
4794 return NULL;
4795}
4796
4797/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004798 * ata_device_add - Register hardware device with ATA and SCSI layers
4799 * @ent: Probe information describing hardware device to be registered
4800 *
4801 * This function processes the information provided in the probe
4802 * information struct @ent, allocates the necessary ATA and SCSI
4803 * host information structures, initializes them, and registers
4804 * everything with requisite kernel subsystems.
4805 *
4806 * This function requests irqs, probes the ATA bus, and probes
4807 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 *
4809 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004810 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 *
4812 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004813 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 */
4815
Jeff Garzik057ace52005-10-22 14:27:05 -04004816int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
4818 unsigned int count = 0, i;
4819 struct device *dev = ent->dev;
4820 struct ata_host_set *host_set;
4821
4822 DPRINTK("ENTER\n");
4823 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004824 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4826 if (!host_set)
4827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 spin_lock_init(&host_set->lock);
4829
4830 host_set->dev = dev;
4831 host_set->n_ports = ent->n_ports;
4832 host_set->irq = ent->irq;
4833 host_set->mmio_base = ent->mmio_base;
4834 host_set->private_data = ent->private_data;
4835 host_set->ops = ent->port_ops;
4836
4837 /* register each port bound to this device */
4838 for (i = 0; i < ent->n_ports; i++) {
4839 struct ata_port *ap;
4840 unsigned long xfer_mode_mask;
4841
4842 ap = ata_host_add(ent, host_set, i);
4843 if (!ap)
4844 goto err_out;
4845
4846 host_set->ports[i] = ap;
4847 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4848 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4849 (ap->pio_mask << ATA_SHIFT_PIO);
4850
4851 /* print per-port info to dmesg */
4852 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4853 "bmdma 0x%lX irq %lu\n",
4854 ap->id,
4855 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4856 ata_mode_string(xfer_mode_mask),
4857 ap->ioaddr.cmd_addr,
4858 ap->ioaddr.ctl_addr,
4859 ap->ioaddr.bmdma_addr,
4860 ent->irq);
4861
4862 ata_chk_status(ap);
4863 host_set->ops->irq_clear(ap);
4864 count++;
4865 }
4866
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004867 if (!count)
4868 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
4870 /* obtain irq, that is shared between channels */
4871 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4872 DRV_NAME, host_set))
4873 goto err_out;
4874
4875 /* perform each probe synchronously */
4876 DPRINTK("probe begin\n");
4877 for (i = 0; i < count; i++) {
4878 struct ata_port *ap;
4879 int rc;
4880
4881 ap = host_set->ports[i];
4882
4883 DPRINTK("ata%u: probe begin\n", ap->id);
4884 rc = ata_bus_probe(ap);
4885 DPRINTK("ata%u: probe end\n", ap->id);
4886
4887 if (rc) {
4888 /* FIXME: do something useful here?
4889 * Current libata behavior will
4890 * tear down everything when
4891 * the module is removed
4892 * or the h/w is unplugged.
4893 */
4894 }
4895
4896 rc = scsi_add_host(ap->host, dev);
4897 if (rc) {
4898 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4899 ap->id);
4900 /* FIXME: do something useful here */
4901 /* FIXME: handle unconditional calls to
4902 * scsi_scan_host and ata_host_remove, below,
4903 * at the very least
4904 */
4905 }
4906 }
4907
4908 /* probes are done, now scan each port's disk(s) */
4909 DPRINTK("probe begin\n");
4910 for (i = 0; i < count; i++) {
4911 struct ata_port *ap = host_set->ports[i];
4912
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004913 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 }
4915
4916 dev_set_drvdata(dev, host_set);
4917
4918 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4919 return ent->n_ports; /* success */
4920
4921err_out:
4922 for (i = 0; i < count; i++) {
4923 ata_host_remove(host_set->ports[i], 1);
4924 scsi_host_put(host_set->ports[i]->host);
4925 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004926err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 kfree(host_set);
4928 VPRINTK("EXIT, returning 0\n");
4929 return 0;
4930}
4931
4932/**
Alan Cox17b14452005-09-15 15:44:00 +01004933 * ata_host_set_remove - PCI layer callback for device removal
4934 * @host_set: ATA host set that was removed
4935 *
4936 * Unregister all objects associated with this host set. Free those
4937 * objects.
4938 *
4939 * LOCKING:
4940 * Inherited from calling layer (may sleep).
4941 */
4942
Alan Cox17b14452005-09-15 15:44:00 +01004943void ata_host_set_remove(struct ata_host_set *host_set)
4944{
4945 struct ata_port *ap;
4946 unsigned int i;
4947
4948 for (i = 0; i < host_set->n_ports; i++) {
4949 ap = host_set->ports[i];
4950 scsi_remove_host(ap->host);
4951 }
4952
4953 free_irq(host_set->irq, host_set);
4954
4955 for (i = 0; i < host_set->n_ports; i++) {
4956 ap = host_set->ports[i];
4957
4958 ata_scsi_release(ap->host);
4959
4960 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4961 struct ata_ioports *ioaddr = &ap->ioaddr;
4962
4963 if (ioaddr->cmd_addr == 0x1f0)
4964 release_region(0x1f0, 8);
4965 else if (ioaddr->cmd_addr == 0x170)
4966 release_region(0x170, 8);
4967 }
4968
4969 scsi_host_put(ap->host);
4970 }
4971
4972 if (host_set->ops->host_stop)
4973 host_set->ops->host_stop(host_set);
4974
4975 kfree(host_set);
4976}
4977
4978/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 * ata_scsi_release - SCSI layer callback hook for host unload
4980 * @host: libata host to be unloaded
4981 *
4982 * Performs all duties necessary to shut down a libata port...
4983 * Kill port kthread, disable port, and release resources.
4984 *
4985 * LOCKING:
4986 * Inherited from SCSI layer.
4987 *
4988 * RETURNS:
4989 * One.
4990 */
4991
4992int ata_scsi_release(struct Scsi_Host *host)
4993{
4994 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4995
4996 DPRINTK("ENTER\n");
4997
4998 ap->ops->port_disable(ap);
4999 ata_host_remove(ap, 0);
5000
5001 DPRINTK("EXIT\n");
5002 return 1;
5003}
5004
5005/**
5006 * ata_std_ports - initialize ioaddr with standard port offsets.
5007 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04005008 *
5009 * Utility function which initializes data_addr, error_addr,
5010 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5011 * device_addr, status_addr, and command_addr to standard offsets
5012 * relative to cmd_addr.
5013 *
5014 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 */
Edward Falk0baab862005-06-02 18:17:13 -04005016
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017void ata_std_ports(struct ata_ioports *ioaddr)
5018{
5019 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5020 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5021 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5022 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5023 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5024 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5025 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5026 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5027 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5028 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5029}
5030
5031static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04005032ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033{
5034 struct ata_probe_ent *probe_ent;
5035
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005036 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 if (!probe_ent) {
5038 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
5039 kobject_name(&(dev->kobj)));
5040 return NULL;
5041 }
5042
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 INIT_LIST_HEAD(&probe_ent->node);
5044 probe_ent->dev = dev;
5045
5046 probe_ent->sht = port->sht;
5047 probe_ent->host_flags = port->host_flags;
5048 probe_ent->pio_mask = port->pio_mask;
5049 probe_ent->mwdma_mask = port->mwdma_mask;
5050 probe_ent->udma_mask = port->udma_mask;
5051 probe_ent->port_ops = port->port_ops;
5052
5053 return probe_ent;
5054}
5055
Edward Falk0baab862005-06-02 18:17:13 -04005056
5057
Jeff Garzik374b1872005-08-30 05:42:52 -04005058#ifdef CONFIG_PCI
5059
5060void ata_pci_host_stop (struct ata_host_set *host_set)
5061{
5062 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5063
5064 pci_iounmap(pdev, host_set->mmio_base);
5065}
5066
Edward Falk0baab862005-06-02 18:17:13 -04005067/**
5068 * ata_pci_init_native_mode - Initialize native-mode driver
5069 * @pdev: pci device to be initialized
5070 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04005071 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04005072 *
5073 * Utility function which allocates and initializes an
5074 * ata_probe_ent structure for a standard dual-port
5075 * PIO-based IDE controller. The returned ata_probe_ent
5076 * structure can be passed to ata_device_add(). The returned
5077 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04005078 *
5079 * The caller need only pass the address of the primary port, the
5080 * secondary will be deduced automatically. If the device has non
5081 * standard secondary port mappings this function can be called twice,
5082 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04005083 */
5084
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04005086ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087{
5088 struct ata_probe_ent *probe_ent =
5089 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04005090 int p = 0;
5091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 if (!probe_ent)
5093 return NULL;
5094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 probe_ent->irq = pdev->irq;
5096 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00005097 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
Alan Cox47a86592005-10-04 08:09:19 -04005099 if (ports & ATA_PORT_PRIMARY) {
5100 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
5101 probe_ent->port[p].altstatus_addr =
5102 probe_ent->port[p].ctl_addr =
5103 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
5104 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
5105 ata_std_ports(&probe_ent->port[p]);
5106 p++;
5107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
Alan Cox47a86592005-10-04 08:09:19 -04005109 if (ports & ATA_PORT_SECONDARY) {
5110 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
5111 probe_ent->port[p].altstatus_addr =
5112 probe_ent->port[p].ctl_addr =
5113 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
5114 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
5115 ata_std_ports(&probe_ent->port[p]);
5116 p++;
5117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
Alan Cox47a86592005-10-04 08:09:19 -04005119 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 return probe_ent;
5121}
5122
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005123static 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 -07005124{
Alan Cox47a86592005-10-04 08:09:19 -04005125 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005127 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 if (!probe_ent)
5129 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04005132 probe_ent->n_ports = 1;
5133 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00005134 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
Alan Cox47a86592005-10-04 08:09:19 -04005136 switch(port_num)
5137 {
5138 case 0:
5139 probe_ent->irq = 14;
5140 probe_ent->port[0].cmd_addr = 0x1f0;
5141 probe_ent->port[0].altstatus_addr =
5142 probe_ent->port[0].ctl_addr = 0x3f6;
5143 break;
5144 case 1:
5145 probe_ent->irq = 15;
5146 probe_ent->port[0].cmd_addr = 0x170;
5147 probe_ent->port[0].altstatus_addr =
5148 probe_ent->port[0].ctl_addr = 0x376;
5149 break;
5150 }
5151 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 return probe_ent;
5154}
5155
5156/**
5157 * ata_pci_init_one - Initialize/register PCI IDE host controller
5158 * @pdev: Controller to be initialized
5159 * @port_info: Information from low-level host driver
5160 * @n_ports: Number of ports attached to host controller
5161 *
Edward Falk0baab862005-06-02 18:17:13 -04005162 * This is a helper function which can be called from a driver's
5163 * xxx_init_one() probe function if the hardware uses traditional
5164 * IDE taskfile registers.
5165 *
5166 * This function calls pci_enable_device(), reserves its register
5167 * regions, sets the dma mask, enables bus master mode, and calls
5168 * ata_device_add()
5169 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 * LOCKING:
5171 * Inherited from PCI layer (may sleep).
5172 *
5173 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005174 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 */
5176
5177int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
5178 unsigned int n_ports)
5179{
Alan Cox47a86592005-10-04 08:09:19 -04005180 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 struct ata_port_info *port[2];
5182 u8 tmp8, mask;
5183 unsigned int legacy_mode = 0;
5184 int disable_dev_on_err = 1;
5185 int rc;
5186
5187 DPRINTK("ENTER\n");
5188
5189 port[0] = port_info[0];
5190 if (n_ports > 1)
5191 port[1] = port_info[1];
5192 else
5193 port[1] = port[0];
5194
5195 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
5196 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04005197 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
5199 mask = (1 << 2) | (1 << 0);
5200 if ((tmp8 & mask) != mask)
5201 legacy_mode = (1 << 3);
5202 }
5203
5204 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04005205 if ((!legacy_mode) && (n_ports > 2)) {
5206 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
5207 n_ports = 2;
5208 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 }
5210
Alan Cox47a86592005-10-04 08:09:19 -04005211 /* FIXME: Really for ATA it isn't safe because the device may be
5212 multi-purpose and we want to leave it alone if it was already
5213 enabled. Secondly for shared use as Arjan says we want refcounting
5214
5215 Checking dev->is_enabled is insufficient as this is not set at
5216 boot for the primary video which is BIOS enabled
5217 */
5218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 rc = pci_enable_device(pdev);
5220 if (rc)
5221 return rc;
5222
5223 rc = pci_request_regions(pdev, DRV_NAME);
5224 if (rc) {
5225 disable_dev_on_err = 0;
5226 goto err_out;
5227 }
5228
Alan Cox47a86592005-10-04 08:09:19 -04005229 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 if (legacy_mode) {
5231 if (!request_region(0x1f0, 8, "libata")) {
5232 struct resource *conflict, res;
5233 res.start = 0x1f0;
5234 res.end = 0x1f0 + 8 - 1;
5235 conflict = ____request_resource(&ioport_resource, &res);
5236 if (!strcmp(conflict->name, "libata"))
5237 legacy_mode |= (1 << 0);
5238 else {
5239 disable_dev_on_err = 0;
5240 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
5241 }
5242 } else
5243 legacy_mode |= (1 << 0);
5244
5245 if (!request_region(0x170, 8, "libata")) {
5246 struct resource *conflict, res;
5247 res.start = 0x170;
5248 res.end = 0x170 + 8 - 1;
5249 conflict = ____request_resource(&ioport_resource, &res);
5250 if (!strcmp(conflict->name, "libata"))
5251 legacy_mode |= (1 << 1);
5252 else {
5253 disable_dev_on_err = 0;
5254 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
5255 }
5256 } else
5257 legacy_mode |= (1 << 1);
5258 }
5259
5260 /* we have legacy mode, but all ports are unavailable */
5261 if (legacy_mode == (1 << 3)) {
5262 rc = -EBUSY;
5263 goto err_out_regions;
5264 }
5265
5266 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
5267 if (rc)
5268 goto err_out_regions;
5269 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
5270 if (rc)
5271 goto err_out_regions;
5272
5273 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04005274 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005275 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04005276 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05005277 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04005278 } else {
5279 if (n_ports == 2)
5280 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
5281 else
5282 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
5283 }
5284 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 rc = -ENOMEM;
5286 goto err_out_regions;
5287 }
5288
5289 pci_set_master(pdev);
5290
5291 /* FIXME: check ata_device_add return */
5292 if (legacy_mode) {
5293 if (legacy_mode & (1 << 0))
5294 ata_device_add(probe_ent);
5295 if (legacy_mode & (1 << 1))
5296 ata_device_add(probe_ent2);
5297 } else
5298 ata_device_add(probe_ent);
5299
5300 kfree(probe_ent);
5301 kfree(probe_ent2);
5302
5303 return 0;
5304
5305err_out_regions:
5306 if (legacy_mode & (1 << 0))
5307 release_region(0x1f0, 8);
5308 if (legacy_mode & (1 << 1))
5309 release_region(0x170, 8);
5310 pci_release_regions(pdev);
5311err_out:
5312 if (disable_dev_on_err)
5313 pci_disable_device(pdev);
5314 return rc;
5315}
5316
5317/**
5318 * ata_pci_remove_one - PCI layer callback for device removal
5319 * @pdev: PCI device that was removed
5320 *
5321 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005322 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 * Handle this by unregistering all objects associated
5324 * with this PCI device. Free those objects. Then finally
5325 * release PCI resources and disable device.
5326 *
5327 * LOCKING:
5328 * Inherited from PCI layer (may sleep).
5329 */
5330
5331void ata_pci_remove_one (struct pci_dev *pdev)
5332{
5333 struct device *dev = pci_dev_to_dev(pdev);
5334 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
Alan Cox17b14452005-09-15 15:44:00 +01005336 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 pci_release_regions(pdev);
5338 pci_disable_device(pdev);
5339 dev_set_drvdata(dev, NULL);
5340}
5341
5342/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005343int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344{
5345 unsigned long tmp = 0;
5346
5347 switch (bits->width) {
5348 case 1: {
5349 u8 tmp8 = 0;
5350 pci_read_config_byte(pdev, bits->reg, &tmp8);
5351 tmp = tmp8;
5352 break;
5353 }
5354 case 2: {
5355 u16 tmp16 = 0;
5356 pci_read_config_word(pdev, bits->reg, &tmp16);
5357 tmp = tmp16;
5358 break;
5359 }
5360 case 4: {
5361 u32 tmp32 = 0;
5362 pci_read_config_dword(pdev, bits->reg, &tmp32);
5363 tmp = tmp32;
5364 break;
5365 }
5366
5367 default:
5368 return -EINVAL;
5369 }
5370
5371 tmp &= bits->mask;
5372
5373 return (tmp == bits->val) ? 1 : 0;
5374}
Jens Axboe9b847542006-01-06 09:28:07 +01005375
5376int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5377{
5378 pci_save_state(pdev);
5379 pci_disable_device(pdev);
5380 pci_set_power_state(pdev, PCI_D3hot);
5381 return 0;
5382}
5383
5384int ata_pci_device_resume(struct pci_dev *pdev)
5385{
5386 pci_set_power_state(pdev, PCI_D0);
5387 pci_restore_state(pdev);
5388 pci_enable_device(pdev);
5389 pci_set_master(pdev);
5390 return 0;
5391}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392#endif /* CONFIG_PCI */
5393
5394
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395static int __init ata_init(void)
5396{
5397 ata_wq = create_workqueue("ata");
5398 if (!ata_wq)
5399 return -ENOMEM;
5400
5401 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5402 return 0;
5403}
5404
5405static void __exit ata_exit(void)
5406{
5407 destroy_workqueue(ata_wq);
5408}
5409
5410module_init(ata_init);
5411module_exit(ata_exit);
5412
Jeff Garzik67846b32005-10-05 02:58:32 -04005413static unsigned long ratelimit_time;
5414static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5415
5416int ata_ratelimit(void)
5417{
5418 int rc;
5419 unsigned long flags;
5420
5421 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5422
5423 if (time_after(jiffies, ratelimit_time)) {
5424 rc = 1;
5425 ratelimit_time = jiffies + (HZ/5);
5426 } else
5427 rc = 0;
5428
5429 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5430
5431 return rc;
5432}
5433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434/*
5435 * libata is essentially a library of internal helper functions for
5436 * low-level ATA host controller drivers. As such, the API/ABI is
5437 * likely to change as new drivers are added and updated.
5438 * Do not depend on ABI/API stability.
5439 */
5440
5441EXPORT_SYMBOL_GPL(ata_std_bios_param);
5442EXPORT_SYMBOL_GPL(ata_std_ports);
5443EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005444EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445EXPORT_SYMBOL_GPL(ata_sg_init);
5446EXPORT_SYMBOL_GPL(ata_sg_init_one);
5447EXPORT_SYMBOL_GPL(ata_qc_complete);
5448EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5449EXPORT_SYMBOL_GPL(ata_eng_timeout);
5450EXPORT_SYMBOL_GPL(ata_tf_load);
5451EXPORT_SYMBOL_GPL(ata_tf_read);
5452EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5453EXPORT_SYMBOL_GPL(ata_std_dev_select);
5454EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5455EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5456EXPORT_SYMBOL_GPL(ata_check_status);
5457EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458EXPORT_SYMBOL_GPL(ata_exec_command);
5459EXPORT_SYMBOL_GPL(ata_port_start);
5460EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005461EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462EXPORT_SYMBOL_GPL(ata_interrupt);
5463EXPORT_SYMBOL_GPL(ata_qc_prep);
5464EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5465EXPORT_SYMBOL_GPL(ata_bmdma_start);
5466EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5467EXPORT_SYMBOL_GPL(ata_bmdma_status);
5468EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5469EXPORT_SYMBOL_GPL(ata_port_probe);
5470EXPORT_SYMBOL_GPL(sata_phy_reset);
5471EXPORT_SYMBOL_GPL(__sata_phy_reset);
5472EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005473EXPORT_SYMBOL_GPL(ata_std_softreset);
5474EXPORT_SYMBOL_GPL(sata_std_hardreset);
5475EXPORT_SYMBOL_GPL(ata_std_postreset);
5476EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005477EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005479EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005480EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5482EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5483EXPORT_SYMBOL_GPL(ata_scsi_error);
5484EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5485EXPORT_SYMBOL_GPL(ata_scsi_release);
5486EXPORT_SYMBOL_GPL(ata_host_intr);
5487EXPORT_SYMBOL_GPL(ata_dev_classify);
5488EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005489EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005491EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5492EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005494EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005495EXPORT_SYMBOL_GPL(ata_timing_compute);
5496EXPORT_SYMBOL_GPL(ata_timing_merge);
5497
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498#ifdef CONFIG_PCI
5499EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005500EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5502EXPORT_SYMBOL_GPL(ata_pci_init_one);
5503EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005504EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5505EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005507
5508EXPORT_SYMBOL_GPL(ata_device_suspend);
5509EXPORT_SYMBOL_GPL(ata_device_resume);
5510EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5511EXPORT_SYMBOL_GPL(ata_scsi_device_resume);