blob: 96b8bbaa7631df66680f44649a8e50b12304f376 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040051#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100052#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "scsi_priv.h"
Jeff Garzik193515d2005-11-07 00:59:37 -050055#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
64static unsigned int ata_busy_sleep (struct ata_port *ap,
65 unsigned long tmout_pat,
66 unsigned long tmout);
Albert Lee59a10b12005-10-12 15:09:42 +080067static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ece2005-05-12 15:29:42 -040068static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void ata_set_mode(struct ata_port *ap);
70static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040071static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040073static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 u8 *xfer_mode_out,
75 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void __ata_qc_complete(struct ata_queued_cmd *qc);
Jeff Garzike33b9df2005-10-09 09:51:46 -040077static void ata_pio_error(struct ata_port *ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static unsigned int ata_unique_id = 1;
80static struct workqueue_struct *ata_wq;
81
Jeff Garzik1623c812005-08-30 03:37:42 -040082int atapi_enabled = 0;
83module_param(atapi_enabled, int, 0444);
84MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_AUTHOR("Jeff Garzik");
87MODULE_DESCRIPTION("Library module for ATA devices");
88MODULE_LICENSE("GPL");
89MODULE_VERSION(DRV_VERSION);
90
91/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040092 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * @ap: Port to which output is sent
94 * @tf: ATA taskfile register set
95 *
96 * Outputs ATA taskfile to standard ATA host controller.
97 *
98 * LOCKING:
99 * Inherited from caller.
100 */
101
Jeff Garzik057ace52005-10-22 14:27:05 -0400102static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct ata_ioports *ioaddr = &ap->ioaddr;
105 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
106
107 if (tf->ctl != ap->last_ctl) {
108 outb(tf->ctl, ioaddr->ctl_addr);
109 ap->last_ctl = tf->ctl;
110 ata_wait_idle(ap);
111 }
112
113 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
114 outb(tf->hob_feature, ioaddr->feature_addr);
115 outb(tf->hob_nsect, ioaddr->nsect_addr);
116 outb(tf->hob_lbal, ioaddr->lbal_addr);
117 outb(tf->hob_lbam, ioaddr->lbam_addr);
118 outb(tf->hob_lbah, ioaddr->lbah_addr);
119 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
120 tf->hob_feature,
121 tf->hob_nsect,
122 tf->hob_lbal,
123 tf->hob_lbam,
124 tf->hob_lbah);
125 }
126
127 if (is_addr) {
128 outb(tf->feature, ioaddr->feature_addr);
129 outb(tf->nsect, ioaddr->nsect_addr);
130 outb(tf->lbal, ioaddr->lbal_addr);
131 outb(tf->lbam, ioaddr->lbam_addr);
132 outb(tf->lbah, ioaddr->lbah_addr);
133 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
134 tf->feature,
135 tf->nsect,
136 tf->lbal,
137 tf->lbam,
138 tf->lbah);
139 }
140
141 if (tf->flags & ATA_TFLAG_DEVICE) {
142 outb(tf->device, ioaddr->device_addr);
143 VPRINTK("device 0x%X\n", tf->device);
144 }
145
146 ata_wait_idle(ap);
147}
148
149/**
150 * ata_tf_load_mmio - send taskfile registers to host controller
151 * @ap: Port to which output is sent
152 * @tf: ATA taskfile register set
153 *
154 * Outputs ATA taskfile to standard ATA host controller using MMIO.
155 *
156 * LOCKING:
157 * Inherited from caller.
158 */
159
Jeff Garzik057ace52005-10-22 14:27:05 -0400160static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct ata_ioports *ioaddr = &ap->ioaddr;
163 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
164
165 if (tf->ctl != ap->last_ctl) {
166 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
167 ap->last_ctl = tf->ctl;
168 ata_wait_idle(ap);
169 }
170
171 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
172 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
173 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
174 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
175 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
176 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
177 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
178 tf->hob_feature,
179 tf->hob_nsect,
180 tf->hob_lbal,
181 tf->hob_lbam,
182 tf->hob_lbah);
183 }
184
185 if (is_addr) {
186 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
187 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
188 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
189 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
190 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
191 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
192 tf->feature,
193 tf->nsect,
194 tf->lbal,
195 tf->lbam,
196 tf->lbah);
197 }
198
199 if (tf->flags & ATA_TFLAG_DEVICE) {
200 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
201 VPRINTK("device 0x%X\n", tf->device);
202 }
203
204 ata_wait_idle(ap);
205}
206
Edward Falk0baab862005-06-02 18:17:13 -0400207
208/**
209 * ata_tf_load - send taskfile registers to host controller
210 * @ap: Port to which output is sent
211 * @tf: ATA taskfile register set
212 *
213 * Outputs ATA taskfile to standard ATA host controller using MMIO
214 * or PIO as indicated by the ATA_FLAG_MMIO flag.
215 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
216 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
217 * hob_lbal, hob_lbam, and hob_lbah.
218 *
219 * This function waits for idle (!BUSY and !DRQ) after writing
220 * registers. If the control register has a new value, this
221 * function also waits for idle after writing control and before
222 * writing the remaining registers.
223 *
224 * May be used as the tf_load() entry in ata_port_operations.
225 *
226 * LOCKING:
227 * Inherited from caller.
228 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400229void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 if (ap->flags & ATA_FLAG_MMIO)
232 ata_tf_load_mmio(ap, tf);
233 else
234 ata_tf_load_pio(ap, tf);
235}
236
237/**
Edward Falk0baab862005-06-02 18:17:13 -0400238 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * @ap: port to which command is being issued
240 * @tf: ATA taskfile register set
241 *
Edward Falk0baab862005-06-02 18:17:13 -0400242 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * synchronization with interrupt handler / other threads.
244 *
245 * LOCKING:
246 * spin_lock_irqsave(host_set lock)
247 */
248
Jeff Garzik057ace52005-10-22 14:27:05 -0400249static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
252
253 outb(tf->command, ap->ioaddr.command_addr);
254 ata_pause(ap);
255}
256
257
258/**
259 * ata_exec_command_mmio - issue ATA command to host controller
260 * @ap: port to which command is being issued
261 * @tf: ATA taskfile register set
262 *
263 * Issues MMIO write to ATA command register, with proper
264 * synchronization with interrupt handler / other threads.
265 *
266 * LOCKING:
267 * spin_lock_irqsave(host_set lock)
268 */
269
Jeff Garzik057ace52005-10-22 14:27:05 -0400270static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
273
274 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
275 ata_pause(ap);
276}
277
Edward Falk0baab862005-06-02 18:17:13 -0400278
279/**
280 * ata_exec_command - issue ATA command to host controller
281 * @ap: port to which command is being issued
282 * @tf: ATA taskfile register set
283 *
284 * Issues PIO/MMIO write to ATA command register, with proper
285 * synchronization with interrupt handler / other threads.
286 *
287 * LOCKING:
288 * spin_lock_irqsave(host_set lock)
289 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400290void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 if (ap->flags & ATA_FLAG_MMIO)
293 ata_exec_command_mmio(ap, tf);
294 else
295 ata_exec_command_pio(ap, tf);
296}
297
298/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * ata_tf_to_host - issue ATA taskfile to host controller
300 * @ap: port to which command is being issued
301 * @tf: ATA taskfile register set
302 *
303 * Issues ATA taskfile register set to ATA host controller,
304 * with proper synchronization with interrupt handler and
305 * other threads.
306 *
307 * LOCKING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * spin_lock_irqsave(host_set lock)
309 */
310
Jeff Garzike5338252005-10-30 21:37:17 -0500311static inline void ata_tf_to_host(struct ata_port *ap,
312 const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 ap->ops->tf_load(ap, tf);
315 ap->ops->exec_command(ap, tf);
316}
317
318/**
Edward Falk0baab862005-06-02 18:17:13 -0400319 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * @ap: Port from which input is read
321 * @tf: ATA taskfile register set for storing input
322 *
323 * Reads ATA taskfile registers for currently-selected device
324 * into @tf.
325 *
326 * LOCKING:
327 * Inherited from caller.
328 */
329
330static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
331{
332 struct ata_ioports *ioaddr = &ap->ioaddr;
333
Jeff Garzikac19bff2005-10-29 13:58:21 -0400334 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400335 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 tf->nsect = inb(ioaddr->nsect_addr);
337 tf->lbal = inb(ioaddr->lbal_addr);
338 tf->lbam = inb(ioaddr->lbam_addr);
339 tf->lbah = inb(ioaddr->lbah_addr);
340 tf->device = inb(ioaddr->device_addr);
341
342 if (tf->flags & ATA_TFLAG_LBA48) {
343 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
344 tf->hob_feature = inb(ioaddr->error_addr);
345 tf->hob_nsect = inb(ioaddr->nsect_addr);
346 tf->hob_lbal = inb(ioaddr->lbal_addr);
347 tf->hob_lbam = inb(ioaddr->lbam_addr);
348 tf->hob_lbah = inb(ioaddr->lbah_addr);
349 }
350}
351
352/**
353 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
354 * @ap: Port from which input is read
355 * @tf: ATA taskfile register set for storing input
356 *
357 * Reads ATA taskfile registers for currently-selected device
358 * into @tf via MMIO.
359 *
360 * LOCKING:
361 * Inherited from caller.
362 */
363
364static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
365{
366 struct ata_ioports *ioaddr = &ap->ioaddr;
367
Jeff Garzikac19bff2005-10-29 13:58:21 -0400368 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400369 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
371 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
372 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
373 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
374 tf->device = readb((void __iomem *)ioaddr->device_addr);
375
376 if (tf->flags & ATA_TFLAG_LBA48) {
377 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
378 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
379 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
380 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
381 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
382 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
383 }
384}
385
Edward Falk0baab862005-06-02 18:17:13 -0400386
387/**
388 * ata_tf_read - input device's ATA taskfile shadow registers
389 * @ap: Port from which input is read
390 * @tf: ATA taskfile register set for storing input
391 *
392 * Reads ATA taskfile registers for currently-selected device
393 * into @tf.
394 *
395 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
396 * is set, also reads the hob registers.
397 *
398 * May be used as the tf_read() entry in ata_port_operations.
399 *
400 * LOCKING:
401 * Inherited from caller.
402 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
404{
405 if (ap->flags & ATA_FLAG_MMIO)
406 ata_tf_read_mmio(ap, tf);
407 else
408 ata_tf_read_pio(ap, tf);
409}
410
411/**
412 * ata_check_status_pio - Read device status reg & clear interrupt
413 * @ap: port where the device is
414 *
415 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400416 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * from this device
418 *
419 * LOCKING:
420 * Inherited from caller.
421 */
422static u8 ata_check_status_pio(struct ata_port *ap)
423{
424 return inb(ap->ioaddr.status_addr);
425}
426
427/**
428 * ata_check_status_mmio - Read device status reg & clear interrupt
429 * @ap: port where the device is
430 *
431 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400432 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * from this device
434 *
435 * LOCKING:
436 * Inherited from caller.
437 */
438static u8 ata_check_status_mmio(struct ata_port *ap)
439{
440 return readb((void __iomem *) ap->ioaddr.status_addr);
441}
442
Edward Falk0baab862005-06-02 18:17:13 -0400443
444/**
445 * ata_check_status - Read device status reg & clear interrupt
446 * @ap: port where the device is
447 *
448 * Reads ATA taskfile status register for currently-selected device
449 * and return its value. This also clears pending interrupts
450 * from this device
451 *
452 * May be used as the check_status() entry in ata_port_operations.
453 *
454 * LOCKING:
455 * Inherited from caller.
456 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457u8 ata_check_status(struct ata_port *ap)
458{
459 if (ap->flags & ATA_FLAG_MMIO)
460 return ata_check_status_mmio(ap);
461 return ata_check_status_pio(ap);
462}
463
Edward Falk0baab862005-06-02 18:17:13 -0400464
465/**
466 * ata_altstatus - Read device alternate status reg
467 * @ap: port where the device is
468 *
469 * Reads ATA taskfile alternate status register for
470 * currently-selected device and return its value.
471 *
472 * Note: may NOT be used as the check_altstatus() entry in
473 * ata_port_operations.
474 *
475 * LOCKING:
476 * Inherited from caller.
477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478u8 ata_altstatus(struct ata_port *ap)
479{
480 if (ap->ops->check_altstatus)
481 return ap->ops->check_altstatus(ap);
482
483 if (ap->flags & ATA_FLAG_MMIO)
484 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
485 return inb(ap->ioaddr.altstatus_addr);
486}
487
Edward Falk0baab862005-06-02 18:17:13 -0400488
489/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
491 * @tf: Taskfile to convert
492 * @fis: Buffer into which data will output
493 * @pmp: Port multiplier port
494 *
495 * Converts a standard ATA taskfile to a Serial ATA
496 * FIS structure (Register - Host to Device).
497 *
498 * LOCKING:
499 * Inherited from caller.
500 */
501
Jeff Garzik057ace52005-10-22 14:27:05 -0400502void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 fis[0] = 0x27; /* Register - Host to Device FIS */
505 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
506 bit 7 indicates Command FIS */
507 fis[2] = tf->command;
508 fis[3] = tf->feature;
509
510 fis[4] = tf->lbal;
511 fis[5] = tf->lbam;
512 fis[6] = tf->lbah;
513 fis[7] = tf->device;
514
515 fis[8] = tf->hob_lbal;
516 fis[9] = tf->hob_lbam;
517 fis[10] = tf->hob_lbah;
518 fis[11] = tf->hob_feature;
519
520 fis[12] = tf->nsect;
521 fis[13] = tf->hob_nsect;
522 fis[14] = 0;
523 fis[15] = tf->ctl;
524
525 fis[16] = 0;
526 fis[17] = 0;
527 fis[18] = 0;
528 fis[19] = 0;
529}
530
531/**
532 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
533 * @fis: Buffer from which data will be input
534 * @tf: Taskfile to output
535 *
536 * Converts a standard ATA taskfile to a Serial ATA
537 * FIS structure (Register - Host to Device).
538 *
539 * LOCKING:
540 * Inherited from caller.
541 */
542
Jeff Garzik057ace52005-10-22 14:27:05 -0400543void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 tf->command = fis[2]; /* status */
546 tf->feature = fis[3]; /* error */
547
548 tf->lbal = fis[4];
549 tf->lbam = fis[5];
550 tf->lbah = fis[6];
551 tf->device = fis[7];
552
553 tf->hob_lbal = fis[8];
554 tf->hob_lbam = fis[9];
555 tf->hob_lbah = fis[10];
556
557 tf->nsect = fis[12];
558 tf->hob_nsect = fis[13];
559}
560
Albert Lee8cbd6df2005-10-12 15:06:27 +0800561static const u8 ata_rw_cmds[] = {
562 /* pio multi */
563 ATA_CMD_READ_MULTI,
564 ATA_CMD_WRITE_MULTI,
565 ATA_CMD_READ_MULTI_EXT,
566 ATA_CMD_WRITE_MULTI_EXT,
567 /* pio */
568 ATA_CMD_PIO_READ,
569 ATA_CMD_PIO_WRITE,
570 ATA_CMD_PIO_READ_EXT,
571 ATA_CMD_PIO_WRITE_EXT,
572 /* dma */
573 ATA_CMD_READ,
574 ATA_CMD_WRITE,
575 ATA_CMD_READ_EXT,
576 ATA_CMD_WRITE_EXT
577};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800580 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
581 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800583 * Examine the device configuration and tf->flags to calculate
584 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
586 * LOCKING:
587 * caller.
588 */
Albert Lee8cbd6df2005-10-12 15:06:27 +0800589void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800591 struct ata_taskfile *tf = &qc->tf;
592 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Albert Lee8cbd6df2005-10-12 15:06:27 +0800594 int index, lba48, write;
595
596 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
597 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Albert Lee8cbd6df2005-10-12 15:06:27 +0800599 if (dev->flags & ATA_DFLAG_PIO) {
600 tf->protocol = ATA_PROT_PIO;
601 index = dev->multi_count ? 0 : 4;
602 } else {
603 tf->protocol = ATA_PROT_DMA;
604 index = 8;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Albert Lee8cbd6df2005-10-12 15:06:27 +0800607 tf->command = ata_rw_cmds[index + lba48 + write];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610static const char * xfer_mode_str[] = {
611 "UDMA/16",
612 "UDMA/25",
613 "UDMA/33",
614 "UDMA/44",
615 "UDMA/66",
616 "UDMA/100",
617 "UDMA/133",
618 "UDMA7",
619 "MWDMA0",
620 "MWDMA1",
621 "MWDMA2",
622 "PIO0",
623 "PIO1",
624 "PIO2",
625 "PIO3",
626 "PIO4",
627};
628
629/**
630 * ata_udma_string - convert UDMA bit offset to string
631 * @mask: mask of bits supported; only highest bit counts.
632 *
633 * Determine string which represents the highest speed
634 * (highest bit in @udma_mask).
635 *
636 * LOCKING:
637 * None.
638 *
639 * RETURNS:
640 * Constant C string representing highest speed listed in
641 * @udma_mask, or the constant C string "<n/a>".
642 */
643
644static const char *ata_mode_string(unsigned int mask)
645{
646 int i;
647
648 for (i = 7; i >= 0; i--)
649 if (mask & (1 << i))
650 goto out;
651 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
652 if (mask & (1 << i))
653 goto out;
654 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
655 if (mask & (1 << i))
656 goto out;
657
658 return "<n/a>";
659
660out:
661 return xfer_mode_str[i];
662}
663
664/**
665 * ata_pio_devchk - PATA device presence detection
666 * @ap: ATA channel to examine
667 * @device: Device to examine (starting at zero)
668 *
669 * This technique was originally described in
670 * Hale Landis's ATADRVR (www.ata-atapi.com), and
671 * later found its way into the ATA/ATAPI spec.
672 *
673 * Write a pattern to the ATA shadow registers,
674 * and if a device is present, it will respond by
675 * correctly storing and echoing back the
676 * ATA shadow register contents.
677 *
678 * LOCKING:
679 * caller.
680 */
681
682static unsigned int ata_pio_devchk(struct ata_port *ap,
683 unsigned int device)
684{
685 struct ata_ioports *ioaddr = &ap->ioaddr;
686 u8 nsect, lbal;
687
688 ap->ops->dev_select(ap, device);
689
690 outb(0x55, ioaddr->nsect_addr);
691 outb(0xaa, ioaddr->lbal_addr);
692
693 outb(0xaa, ioaddr->nsect_addr);
694 outb(0x55, ioaddr->lbal_addr);
695
696 outb(0x55, ioaddr->nsect_addr);
697 outb(0xaa, ioaddr->lbal_addr);
698
699 nsect = inb(ioaddr->nsect_addr);
700 lbal = inb(ioaddr->lbal_addr);
701
702 if ((nsect == 0x55) && (lbal == 0xaa))
703 return 1; /* we found a device */
704
705 return 0; /* nothing found */
706}
707
708/**
709 * ata_mmio_devchk - PATA device presence detection
710 * @ap: ATA channel to examine
711 * @device: Device to examine (starting at zero)
712 *
713 * This technique was originally described in
714 * Hale Landis's ATADRVR (www.ata-atapi.com), and
715 * later found its way into the ATA/ATAPI spec.
716 *
717 * Write a pattern to the ATA shadow registers,
718 * and if a device is present, it will respond by
719 * correctly storing and echoing back the
720 * ATA shadow register contents.
721 *
722 * LOCKING:
723 * caller.
724 */
725
726static unsigned int ata_mmio_devchk(struct ata_port *ap,
727 unsigned int device)
728{
729 struct ata_ioports *ioaddr = &ap->ioaddr;
730 u8 nsect, lbal;
731
732 ap->ops->dev_select(ap, device);
733
734 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
735 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
736
737 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
738 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
739
740 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
741 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
742
743 nsect = readb((void __iomem *) ioaddr->nsect_addr);
744 lbal = readb((void __iomem *) ioaddr->lbal_addr);
745
746 if ((nsect == 0x55) && (lbal == 0xaa))
747 return 1; /* we found a device */
748
749 return 0; /* nothing found */
750}
751
752/**
753 * ata_devchk - PATA device presence detection
754 * @ap: ATA channel to examine
755 * @device: Device to examine (starting at zero)
756 *
757 * Dispatch ATA device presence detection, depending
758 * on whether we are using PIO or MMIO to talk to the
759 * ATA shadow registers.
760 *
761 * LOCKING:
762 * caller.
763 */
764
765static unsigned int ata_devchk(struct ata_port *ap,
766 unsigned int device)
767{
768 if (ap->flags & ATA_FLAG_MMIO)
769 return ata_mmio_devchk(ap, device);
770 return ata_pio_devchk(ap, device);
771}
772
773/**
774 * ata_dev_classify - determine device type based on ATA-spec signature
775 * @tf: ATA taskfile register set for device to be identified
776 *
777 * Determine from taskfile register contents whether a device is
778 * ATA or ATAPI, as per "Signature and persistence" section
779 * of ATA/PI spec (volume 1, sect 5.14).
780 *
781 * LOCKING:
782 * None.
783 *
784 * RETURNS:
785 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
786 * the event of failure.
787 */
788
Jeff Garzik057ace52005-10-22 14:27:05 -0400789unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 /* Apple's open source Darwin code hints that some devices only
792 * put a proper signature into the LBA mid/high registers,
793 * So, we only check those. It's sufficient for uniqueness.
794 */
795
796 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
797 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
798 DPRINTK("found ATA device by sig\n");
799 return ATA_DEV_ATA;
800 }
801
802 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
803 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
804 DPRINTK("found ATAPI device by sig\n");
805 return ATA_DEV_ATAPI;
806 }
807
808 DPRINTK("unknown device\n");
809 return ATA_DEV_UNKNOWN;
810}
811
812/**
813 * ata_dev_try_classify - Parse returned ATA device signature
814 * @ap: ATA channel to examine
815 * @device: Device to examine (starting at zero)
816 *
817 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
818 * an ATA/ATAPI-defined set of values is placed in the ATA
819 * shadow registers, indicating the results of device detection
820 * and diagnostics.
821 *
822 * Select the ATA device, and read the values from the ATA shadow
823 * registers. Then parse according to the Error register value,
824 * and the spec-defined values examined by ata_dev_classify().
825 *
826 * LOCKING:
827 * caller.
828 */
829
830static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
831{
832 struct ata_device *dev = &ap->device[device];
833 struct ata_taskfile tf;
834 unsigned int class;
835 u8 err;
836
837 ap->ops->dev_select(ap, device);
838
839 memset(&tf, 0, sizeof(tf));
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400842 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 dev->class = ATA_DEV_NONE;
845
846 /* see if device passed diags */
847 if (err == 1)
848 /* do nothing */ ;
849 else if ((device == 0) && (err == 0x81))
850 /* do nothing */ ;
851 else
852 return err;
853
854 /* determine if device if ATA or ATAPI */
855 class = ata_dev_classify(&tf);
856 if (class == ATA_DEV_UNKNOWN)
857 return err;
858 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
859 return err;
860
861 dev->class = class;
862
863 return err;
864}
865
866/**
867 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
868 * @id: IDENTIFY DEVICE results we will examine
869 * @s: string into which data is output
870 * @ofs: offset into identify device page
871 * @len: length of string to return. must be an even number.
872 *
873 * The strings in the IDENTIFY DEVICE page are broken up into
874 * 16-bit chunks. Run through the string, and output each
875 * 8-bit chunk linearly, regardless of platform.
876 *
877 * LOCKING:
878 * caller.
879 */
880
Jeff Garzik057ace52005-10-22 14:27:05 -0400881void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 unsigned int ofs, unsigned int len)
883{
884 unsigned int c;
885
886 while (len > 0) {
887 c = id[ofs] >> 8;
888 *s = c;
889 s++;
890
891 c = id[ofs] & 0xff;
892 *s = c;
893 s++;
894
895 ofs++;
896 len -= 2;
897 }
898}
899
Edward Falk0baab862005-06-02 18:17:13 -0400900
901/**
902 * ata_noop_dev_select - Select device 0/1 on ATA bus
903 * @ap: ATA channel to manipulate
904 * @device: ATA device (numbered from zero) to select
905 *
906 * This function performs no actual function.
907 *
908 * May be used as the dev_select() entry in ata_port_operations.
909 *
910 * LOCKING:
911 * caller.
912 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
914{
915}
916
Edward Falk0baab862005-06-02 18:17:13 -0400917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/**
919 * ata_std_dev_select - Select device 0/1 on ATA bus
920 * @ap: ATA channel to manipulate
921 * @device: ATA device (numbered from zero) to select
922 *
923 * Use the method defined in the ATA specification to
924 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400925 * ATA channel. Works with both PIO and MMIO.
926 *
927 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 *
929 * LOCKING:
930 * caller.
931 */
932
933void ata_std_dev_select (struct ata_port *ap, unsigned int device)
934{
935 u8 tmp;
936
937 if (device == 0)
938 tmp = ATA_DEVICE_OBS;
939 else
940 tmp = ATA_DEVICE_OBS | ATA_DEV1;
941
942 if (ap->flags & ATA_FLAG_MMIO) {
943 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
944 } else {
945 outb(tmp, ap->ioaddr.device_addr);
946 }
947 ata_pause(ap); /* needed; also flushes, for mmio */
948}
949
950/**
951 * ata_dev_select - Select device 0/1 on ATA bus
952 * @ap: ATA channel to manipulate
953 * @device: ATA device (numbered from zero) to select
954 * @wait: non-zero to wait for Status register BSY bit to clear
955 * @can_sleep: non-zero if context allows sleeping
956 *
957 * Use the method defined in the ATA specification to
958 * make either device 0, or device 1, active on the
959 * ATA channel.
960 *
961 * This is a high-level version of ata_std_dev_select(),
962 * which additionally provides the services of inserting
963 * the proper pauses and status polling, where needed.
964 *
965 * LOCKING:
966 * caller.
967 */
968
969void ata_dev_select(struct ata_port *ap, unsigned int device,
970 unsigned int wait, unsigned int can_sleep)
971{
972 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
973 ap->id, device, wait);
974
975 if (wait)
976 ata_wait_idle(ap);
977
978 ap->ops->dev_select(ap, device);
979
980 if (wait) {
981 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
982 msleep(150);
983 ata_wait_idle(ap);
984 }
985}
986
987/**
988 * ata_dump_id - IDENTIFY DEVICE info debugging output
989 * @dev: Device whose IDENTIFY DEVICE page we will dump
990 *
991 * Dump selected 16-bit words from a detected device's
992 * IDENTIFY PAGE page.
993 *
994 * LOCKING:
995 * caller.
996 */
997
Jeff Garzik057ace52005-10-22 14:27:05 -0400998static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 DPRINTK("49==0x%04x "
1001 "53==0x%04x "
1002 "63==0x%04x "
1003 "64==0x%04x "
1004 "75==0x%04x \n",
1005 dev->id[49],
1006 dev->id[53],
1007 dev->id[63],
1008 dev->id[64],
1009 dev->id[75]);
1010 DPRINTK("80==0x%04x "
1011 "81==0x%04x "
1012 "82==0x%04x "
1013 "83==0x%04x "
1014 "84==0x%04x \n",
1015 dev->id[80],
1016 dev->id[81],
1017 dev->id[82],
1018 dev->id[83],
1019 dev->id[84]);
1020 DPRINTK("88==0x%04x "
1021 "93==0x%04x\n",
1022 dev->id[88],
1023 dev->id[93]);
1024}
1025
Alan Cox11e29e22005-10-21 18:46:32 -04001026/*
1027 * Compute the PIO modes available for this device. This is not as
1028 * trivial as it seems if we must consider early devices correctly.
1029 *
1030 * FIXME: pre IDE drive timing (do we care ?).
1031 */
1032
Jeff Garzik057ace52005-10-22 14:27:05 -04001033static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001034{
1035 u16 modes;
1036
1037 /* Usual case. Word 53 indicates word 88 is valid */
1038 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1039 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1040 modes <<= 3;
1041 modes |= 0x7;
1042 return modes;
1043 }
1044
1045 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1046 for the maximum. Turn it into a mask and return it */
1047 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1048 return modes;
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/**
1052 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1053 * @ap: port on which device we wish to probe resides
1054 * @device: device bus address, starting at zero
1055 *
1056 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1057 * command, and read back the 512-byte device information page.
1058 * The device information page is fed to us via the standard
1059 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1060 * using standard PIO-IN paths)
1061 *
1062 * After reading the device information page, we use several
1063 * bits of information from it to initialize data structures
1064 * that will be used during the lifetime of the ata_device.
1065 * Other data from the info page is used to disqualify certain
1066 * older ATA devices we do not wish to support.
1067 *
1068 * LOCKING:
1069 * Inherited from caller. Some functions called by this function
1070 * obtain the host_set lock.
1071 */
1072
1073static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1074{
1075 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001076 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 u16 tmp;
1078 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 unsigned int using_edd;
1080 DECLARE_COMPLETION(wait);
1081 struct ata_queued_cmd *qc;
1082 unsigned long flags;
1083 int rc;
1084
1085 if (!ata_dev_present(dev)) {
1086 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1087 ap->id, device);
1088 return;
1089 }
1090
1091 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1092 using_edd = 0;
1093 else
1094 using_edd = 1;
1095
1096 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1097
1098 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1099 dev->class == ATA_DEV_NONE);
1100
1101 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1102
1103 qc = ata_qc_new_init(ap, dev);
1104 BUG_ON(qc == NULL);
1105
1106 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1107 qc->dma_dir = DMA_FROM_DEVICE;
1108 qc->tf.protocol = ATA_PROT_PIO;
1109 qc->nsect = 1;
1110
1111retry:
1112 if (dev->class == ATA_DEV_ATA) {
1113 qc->tf.command = ATA_CMD_ID_ATA;
1114 DPRINTK("do ATA identify\n");
1115 } else {
1116 qc->tf.command = ATA_CMD_ID_ATAPI;
1117 DPRINTK("do ATAPI identify\n");
1118 }
1119
1120 qc->waiting = &wait;
1121 qc->complete_fn = ata_qc_complete_noop;
1122
1123 spin_lock_irqsave(&ap->host_set->lock, flags);
1124 rc = ata_qc_issue(qc);
1125 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1126
1127 if (rc)
1128 goto err_out;
1129 else
1130 wait_for_completion(&wait);
1131
Jeff Garzik0169e282005-10-29 21:25:10 -04001132 spin_lock_irqsave(&ap->host_set->lock, flags);
1133 ap->ops->tf_read(ap, &qc->tf);
1134 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1135
1136 if (qc->tf.command & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /*
1138 * arg! EDD works for all test cases, but seems to return
1139 * the ATA signature for some ATAPI devices. Until the
1140 * reason for this is found and fixed, we fix up the mess
1141 * here. If IDENTIFY DEVICE returns command aborted
1142 * (as ATAPI devices do), then we issue an
1143 * IDENTIFY PACKET DEVICE.
1144 *
1145 * ATA software reset (SRST, the default) does not appear
1146 * to have this problem.
1147 */
1148 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
Jeff Garzik0169e282005-10-29 21:25:10 -04001149 u8 err = qc->tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (err & ATA_ABORTED) {
1151 dev->class = ATA_DEV_ATAPI;
1152 qc->cursg = 0;
1153 qc->cursg_ofs = 0;
1154 qc->cursect = 0;
1155 qc->nsect = 1;
1156 goto retry;
1157 }
1158 }
1159 goto err_out;
1160 }
1161
1162 swap_buf_le16(dev->id, ATA_ID_WORDS);
1163
1164 /* print device capabilities */
1165 printk(KERN_DEBUG "ata%u: dev %u cfg "
1166 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1167 ap->id, device, dev->id[49],
1168 dev->id[82], dev->id[83], dev->id[84],
1169 dev->id[85], dev->id[86], dev->id[87],
1170 dev->id[88]);
1171
1172 /*
1173 * common ATA, ATAPI feature tests
1174 */
1175
Albert Lee8bf62ece2005-05-12 15:29:42 -04001176 /* we require DMA support (bits 8 of word 49) */
1177 if (!ata_id_has_dma(dev->id)) {
1178 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto err_out_nosup;
1180 }
1181
1182 /* quick-n-dirty find max transfer mode; for printk only */
1183 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1184 if (!xfer_modes)
1185 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001186 if (!xfer_modes)
1187 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 ata_dump_id(dev);
1190
1191 /* ATA-specific feature tests */
1192 if (dev->class == ATA_DEV_ATA) {
1193 if (!ata_id_is_ata(dev->id)) /* sanity check */
1194 goto err_out_nosup;
1195
Albert Lee8bf62ece2005-05-12 15:29:42 -04001196 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001198 for (major_version = 14; major_version >= 1; major_version--)
1199 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
1201
Albert Lee8bf62ece2005-05-12 15:29:42 -04001202 /*
1203 * The exact sequence expected by certain pre-ATA4 drives is:
1204 * SRST RESET
1205 * IDENTIFY
1206 * INITIALIZE DEVICE PARAMETERS
1207 * anything else..
1208 * Some drives were very specific about that exact sequence.
1209 */
Albert Lee59a10b12005-10-12 15:09:42 +08001210 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001211 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Albert Lee59a10b12005-10-12 15:09:42 +08001213 /* current CHS translation info (id[53-58]) might be
1214 * changed. reread the identify device info.
1215 */
1216 ata_dev_reread_id(ap, dev);
1217 }
1218
Albert Lee8bf62ece2005-05-12 15:29:42 -04001219 if (ata_id_has_lba(dev->id)) {
1220 dev->flags |= ATA_DFLAG_LBA;
1221
1222 if (ata_id_has_lba48(dev->id)) {
1223 dev->flags |= ATA_DFLAG_LBA48;
1224 dev->n_sectors = ata_id_u64(dev->id, 100);
1225 } else {
1226 dev->n_sectors = ata_id_u32(dev->id, 60);
1227 }
1228
1229 /* print device info to dmesg */
1230 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1231 ap->id, device,
1232 major_version,
1233 ata_mode_string(xfer_modes),
1234 (unsigned long long)dev->n_sectors,
1235 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1236 } else {
1237 /* CHS */
1238
1239 /* Default translation */
1240 dev->cylinders = dev->id[1];
1241 dev->heads = dev->id[3];
1242 dev->sectors = dev->id[6];
1243 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1244
1245 if (ata_id_current_chs_valid(dev->id)) {
1246 /* Current CHS translation is valid. */
1247 dev->cylinders = dev->id[54];
1248 dev->heads = dev->id[55];
1249 dev->sectors = dev->id[56];
1250
1251 dev->n_sectors = ata_id_u32(dev->id, 57);
1252 }
1253
1254 /* print device info to dmesg */
1255 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1256 ap->id, device,
1257 major_version,
1258 ata_mode_string(xfer_modes),
1259 (unsigned long long)dev->n_sectors,
1260 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
1264 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
1267 /* ATAPI-specific feature tests */
1268 else {
1269 if (ata_id_is_ata(dev->id)) /* sanity check */
1270 goto err_out_nosup;
1271
1272 rc = atapi_cdb_len(dev->id);
1273 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1274 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1275 goto err_out_nosup;
1276 }
1277 ap->cdb_len = (unsigned int) rc;
1278 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1279
Albert Lee312f7da2005-09-27 17:38:03 +08001280 if (ata_id_cdb_intr(dev->id))
1281 dev->flags |= ATA_DFLAG_CDB_INTR;
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* print device info to dmesg */
1284 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1285 ap->id, device,
1286 ata_mode_string(xfer_modes));
1287 }
1288
1289 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1290 return;
1291
1292err_out_nosup:
1293 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1294 ap->id, device);
1295err_out:
1296 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1297 DPRINTK("EXIT, err\n");
1298}
1299
Brad Campbell6f2f3812005-05-12 15:07:47 -04001300
Jeff Garzik057ace52005-10-22 14:27:05 -04001301static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001302{
1303 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1304}
1305
1306/**
1307 * ata_dev_config - Run device specific handlers and check for
1308 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001309 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001310 * @i: Device
1311 *
1312 * LOCKING:
1313 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001314
Brad Campbell6f2f3812005-05-12 15:07:47 -04001315void ata_dev_config(struct ata_port *ap, unsigned int i)
1316{
1317 /* limit bridge transfers to udma5, 200 sectors */
1318 if (ata_dev_knobble(ap)) {
1319 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1320 ap->id, ap->device->devno);
1321 ap->udma_mask &= ATA_UDMA5;
1322 ap->host->max_sectors = ATA_MAX_SECTORS;
1323 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1324 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1325 }
1326
1327 if (ap->ops->dev_config)
1328 ap->ops->dev_config(ap, &ap->device[i]);
1329}
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331/**
1332 * ata_bus_probe - Reset and probe ATA bus
1333 * @ap: Bus to probe
1334 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001335 * Master ATA bus probing function. Initiates a hardware-dependent
1336 * bus reset, then attempts to identify any devices found on
1337 * the bus.
1338 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001340 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 *
1342 * RETURNS:
1343 * Zero on success, non-zero on error.
1344 */
1345
1346static int ata_bus_probe(struct ata_port *ap)
1347{
1348 unsigned int i, found = 0;
1349
1350 ap->ops->phy_reset(ap);
1351 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1352 goto err_out;
1353
1354 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1355 ata_dev_identify(ap, i);
1356 if (ata_dev_present(&ap->device[i])) {
1357 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001358 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360 }
1361
1362 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1363 goto err_out_disable;
1364
1365 ata_set_mode(ap);
1366 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1367 goto err_out_disable;
1368
1369 return 0;
1370
1371err_out_disable:
1372 ap->ops->port_disable(ap);
1373err_out:
1374 return -1;
1375}
1376
1377/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001378 * ata_port_probe - Mark port as enabled
1379 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001381 * Modify @ap data structure such that the system
1382 * thinks that the entire port is enabled.
1383 *
1384 * LOCKING: host_set lock, or some other form of
1385 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 */
1387
1388void ata_port_probe(struct ata_port *ap)
1389{
1390 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1391}
1392
1393/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001394 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1395 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001397 * This function issues commands to standard SATA Sxxx
1398 * PHY registers, to wake up the phy (and device), and
1399 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 *
1401 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001402 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 *
1404 */
1405void __sata_phy_reset(struct ata_port *ap)
1406{
1407 u32 sstatus;
1408 unsigned long timeout = jiffies + (HZ * 5);
1409
1410 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001411 /* issue phy wake/reset */
1412 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001413 /* Couldn't find anything in SATA I/II specs, but
1414 * AHCI-1.1 10.4.2 says at least 1 ms. */
1415 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001417 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 /* wait for phy to become ready, if necessary */
1420 do {
1421 msleep(200);
1422 sstatus = scr_read(ap, SCR_STATUS);
1423 if ((sstatus & 0xf) != 1)
1424 break;
1425 } while (time_before(jiffies, timeout));
1426
1427 /* TODO: phy layer with polling, timeouts, etc. */
1428 if (sata_dev_present(ap))
1429 ata_port_probe(ap);
1430 else {
1431 sstatus = scr_read(ap, SCR_STATUS);
1432 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1433 ap->id, sstatus);
1434 ata_port_disable(ap);
1435 }
1436
1437 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1438 return;
1439
1440 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1441 ata_port_disable(ap);
1442 return;
1443 }
1444
1445 ap->cbl = ATA_CBL_SATA;
1446}
1447
1448/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001449 * sata_phy_reset - Reset SATA bus.
1450 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001452 * This function resets the SATA bus, and then probes
1453 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 *
1455 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001456 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
1458 */
1459void sata_phy_reset(struct ata_port *ap)
1460{
1461 __sata_phy_reset(ap);
1462 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1463 return;
1464 ata_bus_reset(ap);
1465}
1466
1467/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001468 * ata_port_disable - Disable port.
1469 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001471 * Modify @ap data structure such that the system
1472 * thinks that the entire port is disabled, and should
1473 * never attempt to probe or communicate with devices
1474 * on this port.
1475 *
1476 * LOCKING: host_set lock, or some other form of
1477 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 */
1479
1480void ata_port_disable(struct ata_port *ap)
1481{
1482 ap->device[0].class = ATA_DEV_NONE;
1483 ap->device[1].class = ATA_DEV_NONE;
1484 ap->flags |= ATA_FLAG_PORT_DISABLED;
1485}
1486
Alan Cox452503f2005-10-21 19:01:32 -04001487/*
1488 * This mode timing computation functionality is ported over from
1489 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1490 */
1491/*
1492 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1493 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1494 * for PIO 5, which is a nonstandard extension and UDMA6, which
1495 * is currently supported only by Maxtor drives.
1496 */
1497
1498static const struct ata_timing ata_timing[] = {
1499
1500 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1501 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1502 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1503 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1504
1505 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1506 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1507 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1508
1509/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1510
1511 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1512 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1513 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1514
1515 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1516 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1517 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1518
1519/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1520 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1521 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1522
1523 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1524 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1525 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1526
1527/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1528
1529 { 0xFF }
1530};
1531
1532#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1533#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1534
1535static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1536{
1537 q->setup = EZ(t->setup * 1000, T);
1538 q->act8b = EZ(t->act8b * 1000, T);
1539 q->rec8b = EZ(t->rec8b * 1000, T);
1540 q->cyc8b = EZ(t->cyc8b * 1000, T);
1541 q->active = EZ(t->active * 1000, T);
1542 q->recover = EZ(t->recover * 1000, T);
1543 q->cycle = EZ(t->cycle * 1000, T);
1544 q->udma = EZ(t->udma * 1000, UT);
1545}
1546
1547void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1548 struct ata_timing *m, unsigned int what)
1549{
1550 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1551 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1552 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1553 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1554 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1555 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1556 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1557 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1558}
1559
1560static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1561{
1562 const struct ata_timing *t;
1563
1564 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001565 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001566 return NULL;
1567 return t;
1568}
1569
1570int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1571 struct ata_timing *t, int T, int UT)
1572{
1573 const struct ata_timing *s;
1574 struct ata_timing p;
1575
1576 /*
1577 * Find the mode.
1578 */
1579
1580 if (!(s = ata_timing_find_mode(speed)))
1581 return -EINVAL;
1582
1583 /*
1584 * If the drive is an EIDE drive, it can tell us it needs extended
1585 * PIO/MW_DMA cycle timing.
1586 */
1587
1588 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1589 memset(&p, 0, sizeof(p));
1590 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1591 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1592 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1593 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1594 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1595 }
1596 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1597 }
1598
1599 /*
1600 * Convert the timing to bus clock counts.
1601 */
1602
1603 ata_timing_quantize(s, t, T, UT);
1604
1605 /*
1606 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1607 * and some other commands. We have to ensure that the DMA cycle timing is
1608 * slower/equal than the fastest PIO timing.
1609 */
1610
1611 if (speed > XFER_PIO_4) {
1612 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1613 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1614 }
1615
1616 /*
1617 * Lenghten active & recovery time so that cycle time is correct.
1618 */
1619
1620 if (t->act8b + t->rec8b < t->cyc8b) {
1621 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1622 t->rec8b = t->cyc8b - t->act8b;
1623 }
1624
1625 if (t->active + t->recover < t->cycle) {
1626 t->active += (t->cycle - (t->active + t->recover)) / 2;
1627 t->recover = t->cycle - t->active;
1628 }
1629
1630 return 0;
1631}
1632
Jeff Garzik057ace52005-10-22 14:27:05 -04001633static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 unsigned int shift;
1635 u8 base;
1636} xfer_mode_classes[] = {
1637 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1638 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1639 { ATA_SHIFT_PIO, XFER_PIO_0 },
1640};
1641
1642static inline u8 base_from_shift(unsigned int shift)
1643{
1644 int i;
1645
1646 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1647 if (xfer_mode_classes[i].shift == shift)
1648 return xfer_mode_classes[i].base;
1649
1650 return 0xff;
1651}
1652
1653static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1654{
1655 int ofs, idx;
1656 u8 base;
1657
1658 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1659 return;
1660
1661 if (dev->xfer_shift == ATA_SHIFT_PIO)
1662 dev->flags |= ATA_DFLAG_PIO;
1663
1664 ata_dev_set_xfermode(ap, dev);
1665
1666 base = base_from_shift(dev->xfer_shift);
1667 ofs = dev->xfer_mode - base;
1668 idx = ofs + dev->xfer_shift;
1669 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1670
1671 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1672 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1673
1674 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1675 ap->id, dev->devno, xfer_mode_str[idx]);
1676}
1677
1678static int ata_host_set_pio(struct ata_port *ap)
1679{
1680 unsigned int mask;
1681 int x, i;
1682 u8 base, xfer_mode;
1683
1684 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1685 x = fgb(mask);
1686 if (x < 0) {
1687 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1688 return -1;
1689 }
1690
1691 base = base_from_shift(ATA_SHIFT_PIO);
1692 xfer_mode = base + x;
1693
1694 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1695 (int)base, (int)xfer_mode, mask, x);
1696
1697 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1698 struct ata_device *dev = &ap->device[i];
1699 if (ata_dev_present(dev)) {
1700 dev->pio_mode = xfer_mode;
1701 dev->xfer_mode = xfer_mode;
1702 dev->xfer_shift = ATA_SHIFT_PIO;
1703 if (ap->ops->set_piomode)
1704 ap->ops->set_piomode(ap, dev);
1705 }
1706 }
1707
1708 return 0;
1709}
1710
1711static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1712 unsigned int xfer_shift)
1713{
1714 int i;
1715
1716 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1717 struct ata_device *dev = &ap->device[i];
1718 if (ata_dev_present(dev)) {
1719 dev->dma_mode = xfer_mode;
1720 dev->xfer_mode = xfer_mode;
1721 dev->xfer_shift = xfer_shift;
1722 if (ap->ops->set_dmamode)
1723 ap->ops->set_dmamode(ap, dev);
1724 }
1725 }
1726}
1727
1728/**
1729 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1730 * @ap: port on which timings will be programmed
1731 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001732 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1733 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001735 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 *
1737 */
1738static void ata_set_mode(struct ata_port *ap)
1739{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001740 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 u8 xfer_mode;
1742 int rc;
1743
1744 /* step 1: always set host PIO timings */
1745 rc = ata_host_set_pio(ap);
1746 if (rc)
1747 goto err_out;
1748
1749 /* step 2: choose the best data xfer mode */
1750 xfer_mode = xfer_shift = 0;
1751 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1752 if (rc)
1753 goto err_out;
1754
1755 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1756 if (xfer_shift != ATA_SHIFT_PIO)
1757 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1758
1759 /* step 4: update devices' xfer mode */
1760 ata_dev_set_mode(ap, &ap->device[0]);
1761 ata_dev_set_mode(ap, &ap->device[1]);
1762
1763 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1764 return;
1765
1766 if (ap->ops->post_set_mode)
1767 ap->ops->post_set_mode(ap);
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return;
1770
1771err_out:
1772 ata_port_disable(ap);
1773}
1774
1775/**
1776 * ata_busy_sleep - sleep until BSY clears, or timeout
1777 * @ap: port containing status register to be polled
1778 * @tmout_pat: impatience timeout
1779 * @tmout: overall timeout
1780 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001781 * Sleep until ATA Status register bit BSY clears,
1782 * or a timeout occurs.
1783 *
1784 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 *
1786 */
1787
1788static unsigned int ata_busy_sleep (struct ata_port *ap,
1789 unsigned long tmout_pat,
1790 unsigned long tmout)
1791{
1792 unsigned long timer_start, timeout;
1793 u8 status;
1794
1795 status = ata_busy_wait(ap, ATA_BUSY, 300);
1796 timer_start = jiffies;
1797 timeout = timer_start + tmout_pat;
1798 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1799 msleep(50);
1800 status = ata_busy_wait(ap, ATA_BUSY, 3);
1801 }
1802
1803 if (status & ATA_BUSY)
1804 printk(KERN_WARNING "ata%u is slow to respond, "
1805 "please be patient\n", ap->id);
1806
1807 timeout = timer_start + tmout;
1808 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1809 msleep(50);
1810 status = ata_chk_status(ap);
1811 }
1812
1813 if (status & ATA_BUSY) {
1814 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1815 ap->id, tmout / HZ);
1816 return 1;
1817 }
1818
1819 return 0;
1820}
1821
1822static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1823{
1824 struct ata_ioports *ioaddr = &ap->ioaddr;
1825 unsigned int dev0 = devmask & (1 << 0);
1826 unsigned int dev1 = devmask & (1 << 1);
1827 unsigned long timeout;
1828
1829 /* if device 0 was found in ata_devchk, wait for its
1830 * BSY bit to clear
1831 */
1832 if (dev0)
1833 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1834
1835 /* if device 1 was found in ata_devchk, wait for
1836 * register access, then wait for BSY to clear
1837 */
1838 timeout = jiffies + ATA_TMOUT_BOOT;
1839 while (dev1) {
1840 u8 nsect, lbal;
1841
1842 ap->ops->dev_select(ap, 1);
1843 if (ap->flags & ATA_FLAG_MMIO) {
1844 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1845 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1846 } else {
1847 nsect = inb(ioaddr->nsect_addr);
1848 lbal = inb(ioaddr->lbal_addr);
1849 }
1850 if ((nsect == 1) && (lbal == 1))
1851 break;
1852 if (time_after(jiffies, timeout)) {
1853 dev1 = 0;
1854 break;
1855 }
1856 msleep(50); /* give drive a breather */
1857 }
1858 if (dev1)
1859 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1860
1861 /* is all this really necessary? */
1862 ap->ops->dev_select(ap, 0);
1863 if (dev1)
1864 ap->ops->dev_select(ap, 1);
1865 if (dev0)
1866 ap->ops->dev_select(ap, 0);
1867}
1868
1869/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001870 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1871 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001873 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1874 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 *
1876 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001877 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001878 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 *
1880 */
1881
1882static unsigned int ata_bus_edd(struct ata_port *ap)
1883{
1884 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001885 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 /* set up execute-device-diag (bus reset) taskfile */
1888 /* also, take interrupts to a known state (disabled) */
1889 DPRINTK("execute-device-diag\n");
1890 ata_tf_init(ap, &tf, 0);
1891 tf.ctl |= ATA_NIEN;
1892 tf.command = ATA_CMD_EDD;
1893 tf.protocol = ATA_PROT_NODATA;
1894
1895 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001896 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001898 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 /* spec says at least 2ms. but who knows with those
1901 * crazy ATAPI devices...
1902 */
1903 msleep(150);
1904
1905 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1906}
1907
1908static unsigned int ata_bus_softreset(struct ata_port *ap,
1909 unsigned int devmask)
1910{
1911 struct ata_ioports *ioaddr = &ap->ioaddr;
1912
1913 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1914
1915 /* software reset. causes dev0 to be selected */
1916 if (ap->flags & ATA_FLAG_MMIO) {
1917 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1918 udelay(20); /* FIXME: flush */
1919 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1920 udelay(20); /* FIXME: flush */
1921 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1922 } else {
1923 outb(ap->ctl, ioaddr->ctl_addr);
1924 udelay(10);
1925 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1926 udelay(10);
1927 outb(ap->ctl, ioaddr->ctl_addr);
1928 }
1929
1930 /* spec mandates ">= 2ms" before checking status.
1931 * We wait 150ms, because that was the magic delay used for
1932 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1933 * between when the ATA command register is written, and then
1934 * status is checked. Because waiting for "a while" before
1935 * checking status is fine, post SRST, we perform this magic
1936 * delay here as well.
1937 */
1938 msleep(150);
1939
1940 ata_bus_post_reset(ap, devmask);
1941
1942 return 0;
1943}
1944
1945/**
1946 * ata_bus_reset - reset host port and associated ATA channel
1947 * @ap: port to reset
1948 *
1949 * This is typically the first time we actually start issuing
1950 * commands to the ATA channel. We wait for BSY to clear, then
1951 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1952 * result. Determine what devices, if any, are on the channel
1953 * by looking at the device 0/1 error register. Look at the signature
1954 * stored in each device's taskfile registers, to determine if
1955 * the device is ATA or ATAPI.
1956 *
1957 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001958 * PCI/etc. bus probe sem.
1959 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 *
1961 * SIDE EFFECTS:
1962 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1963 */
1964
1965void ata_bus_reset(struct ata_port *ap)
1966{
1967 struct ata_ioports *ioaddr = &ap->ioaddr;
1968 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1969 u8 err;
1970 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1971
1972 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1973
1974 /* determine if device 0/1 are present */
1975 if (ap->flags & ATA_FLAG_SATA_RESET)
1976 dev0 = 1;
1977 else {
1978 dev0 = ata_devchk(ap, 0);
1979 if (slave_possible)
1980 dev1 = ata_devchk(ap, 1);
1981 }
1982
1983 if (dev0)
1984 devmask |= (1 << 0);
1985 if (dev1)
1986 devmask |= (1 << 1);
1987
1988 /* select device 0 again */
1989 ap->ops->dev_select(ap, 0);
1990
1991 /* issue bus reset */
1992 if (ap->flags & ATA_FLAG_SRST)
1993 rc = ata_bus_softreset(ap, devmask);
1994 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1995 /* set up device control */
1996 if (ap->flags & ATA_FLAG_MMIO)
1997 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1998 else
1999 outb(ap->ctl, ioaddr->ctl_addr);
2000 rc = ata_bus_edd(ap);
2001 }
2002
2003 if (rc)
2004 goto err_out;
2005
2006 /*
2007 * determine by signature whether we have ATA or ATAPI devices
2008 */
2009 err = ata_dev_try_classify(ap, 0);
2010 if ((slave_possible) && (err != 0x81))
2011 ata_dev_try_classify(ap, 1);
2012
2013 /* re-enable interrupts */
2014 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2015 ata_irq_on(ap);
2016
2017 /* is double-select really necessary? */
2018 if (ap->device[1].class != ATA_DEV_NONE)
2019 ap->ops->dev_select(ap, 1);
2020 if (ap->device[0].class != ATA_DEV_NONE)
2021 ap->ops->dev_select(ap, 0);
2022
2023 /* if no devices were detected, disable this port */
2024 if ((ap->device[0].class == ATA_DEV_NONE) &&
2025 (ap->device[1].class == ATA_DEV_NONE))
2026 goto err_out;
2027
2028 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2029 /* set up device control for ATA_FLAG_SATA_RESET */
2030 if (ap->flags & ATA_FLAG_MMIO)
2031 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2032 else
2033 outb(ap->ctl, ioaddr->ctl_addr);
2034 }
2035
2036 DPRINTK("EXIT\n");
2037 return;
2038
2039err_out:
2040 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2041 ap->ops->port_disable(ap);
2042
2043 DPRINTK("EXIT\n");
2044}
2045
Jeff Garzik057ace52005-10-22 14:27:05 -04002046static void ata_pr_blacklisted(const struct ata_port *ap,
2047 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2050 ap->id, dev->devno);
2051}
2052
2053static const char * ata_dma_blacklist [] = {
2054 "WDC AC11000H",
2055 "WDC AC22100H",
2056 "WDC AC32500H",
2057 "WDC AC33100H",
2058 "WDC AC31600H",
2059 "WDC AC32100H",
2060 "WDC AC23200L",
2061 "Compaq CRD-8241B",
2062 "CRD-8400B",
2063 "CRD-8480B",
2064 "CRD-8482B",
2065 "CRD-84",
2066 "SanDisk SDP3B",
2067 "SanDisk SDP3B-64",
2068 "SANYO CD-ROM CRD",
2069 "HITACHI CDR-8",
2070 "HITACHI CDR-8335",
2071 "HITACHI CDR-8435",
2072 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002073 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 "CD-532E-A",
2075 "E-IDE CD-ROM CR-840",
2076 "CD-ROM Drive/F5A",
2077 "WPI CDD-820",
2078 "SAMSUNG CD-ROM SC-148C",
2079 "SAMSUNG CD-ROM SC",
2080 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2082 "_NEC DV5800A",
2083};
2084
Jeff Garzik057ace52005-10-22 14:27:05 -04002085static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 unsigned char model_num[40];
2088 char *s;
2089 unsigned int len;
2090 int i;
2091
2092 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2093 sizeof(model_num));
2094 s = &model_num[0];
2095 len = strnlen(s, sizeof(model_num));
2096
2097 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2098 while ((len > 0) && (s[len - 1] == ' ')) {
2099 len--;
2100 s[len] = 0;
2101 }
2102
2103 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2104 if (!strncmp(ata_dma_blacklist[i], s, len))
2105 return 1;
2106
2107 return 0;
2108}
2109
Jeff Garzik057ace52005-10-22 14:27:05 -04002110static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Jeff Garzik057ace52005-10-22 14:27:05 -04002112 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 unsigned int mask;
2114
2115 master = &ap->device[0];
2116 slave = &ap->device[1];
2117
2118 assert (ata_dev_present(master) || ata_dev_present(slave));
2119
2120 if (shift == ATA_SHIFT_UDMA) {
2121 mask = ap->udma_mask;
2122 if (ata_dev_present(master)) {
2123 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002124 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 mask = 0;
2126 ata_pr_blacklisted(ap, master);
2127 }
2128 }
2129 if (ata_dev_present(slave)) {
2130 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002131 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 mask = 0;
2133 ata_pr_blacklisted(ap, slave);
2134 }
2135 }
2136 }
2137 else if (shift == ATA_SHIFT_MWDMA) {
2138 mask = ap->mwdma_mask;
2139 if (ata_dev_present(master)) {
2140 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002141 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 mask = 0;
2143 ata_pr_blacklisted(ap, master);
2144 }
2145 }
2146 if (ata_dev_present(slave)) {
2147 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002148 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 mask = 0;
2150 ata_pr_blacklisted(ap, slave);
2151 }
2152 }
2153 }
2154 else if (shift == ATA_SHIFT_PIO) {
2155 mask = ap->pio_mask;
2156 if (ata_dev_present(master)) {
2157 /* spec doesn't return explicit support for
2158 * PIO0-2, so we fake it
2159 */
2160 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2161 tmp_mode <<= 3;
2162 tmp_mode |= 0x7;
2163 mask &= tmp_mode;
2164 }
2165 if (ata_dev_present(slave)) {
2166 /* spec doesn't return explicit support for
2167 * PIO0-2, so we fake it
2168 */
2169 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2170 tmp_mode <<= 3;
2171 tmp_mode |= 0x7;
2172 mask &= tmp_mode;
2173 }
2174 }
2175 else {
2176 mask = 0xffffffff; /* shut up compiler warning */
2177 BUG();
2178 }
2179
2180 return mask;
2181}
2182
2183/* find greatest bit */
2184static int fgb(u32 bitmap)
2185{
2186 unsigned int i;
2187 int x = -1;
2188
2189 for (i = 0; i < 32; i++)
2190 if (bitmap & (1 << i))
2191 x = i;
2192
2193 return x;
2194}
2195
2196/**
2197 * ata_choose_xfer_mode - attempt to find best transfer mode
2198 * @ap: Port for which an xfer mode will be selected
2199 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2200 * @xfer_shift_out: (output) bit shift that selects this mode
2201 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002202 * Based on host and device capabilities, determine the
2203 * maximum transfer mode that is amenable to all.
2204 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002206 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 *
2208 * RETURNS:
2209 * Zero on success, negative on error.
2210 */
2211
Jeff Garzik057ace52005-10-22 14:27:05 -04002212static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 u8 *xfer_mode_out,
2214 unsigned int *xfer_shift_out)
2215{
2216 unsigned int mask, shift;
2217 int x, i;
2218
2219 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2220 shift = xfer_mode_classes[i].shift;
2221 mask = ata_get_mode_mask(ap, shift);
2222
2223 x = fgb(mask);
2224 if (x >= 0) {
2225 *xfer_mode_out = xfer_mode_classes[i].base + x;
2226 *xfer_shift_out = shift;
2227 return 0;
2228 }
2229 }
2230
2231 return -1;
2232}
2233
2234/**
2235 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2236 * @ap: Port associated with device @dev
2237 * @dev: Device to which command will be sent
2238 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002239 * Issue SET FEATURES - XFER MODE command to device @dev
2240 * on port @ap.
2241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002243 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 */
2245
2246static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2247{
2248 DECLARE_COMPLETION(wait);
2249 struct ata_queued_cmd *qc;
2250 int rc;
2251 unsigned long flags;
2252
2253 /* set up set-features taskfile */
2254 DPRINTK("set features - xfer mode\n");
2255
2256 qc = ata_qc_new_init(ap, dev);
2257 BUG_ON(qc == NULL);
2258
2259 qc->tf.command = ATA_CMD_SET_FEATURES;
2260 qc->tf.feature = SETFEATURES_XFER;
2261 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2262 qc->tf.protocol = ATA_PROT_NODATA;
2263 qc->tf.nsect = dev->xfer_mode;
2264
2265 qc->waiting = &wait;
2266 qc->complete_fn = ata_qc_complete_noop;
2267
2268 spin_lock_irqsave(&ap->host_set->lock, flags);
2269 rc = ata_qc_issue(qc);
2270 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2271
2272 if (rc)
2273 ata_port_disable(ap);
2274 else
2275 wait_for_completion(&wait);
2276
2277 DPRINTK("EXIT\n");
2278}
2279
2280/**
Albert Lee59a10b12005-10-12 15:09:42 +08002281 * ata_dev_reread_id - Reread the device identify device info
2282 * @ap: port where the device is
2283 * @dev: device to reread the identify device info
2284 *
2285 * LOCKING:
2286 */
2287
2288static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2289{
2290 DECLARE_COMPLETION(wait);
2291 struct ata_queued_cmd *qc;
2292 unsigned long flags;
2293 int rc;
2294
2295 qc = ata_qc_new_init(ap, dev);
2296 BUG_ON(qc == NULL);
2297
2298 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2299 qc->dma_dir = DMA_FROM_DEVICE;
2300
2301 if (dev->class == ATA_DEV_ATA) {
2302 qc->tf.command = ATA_CMD_ID_ATA;
2303 DPRINTK("do ATA identify\n");
2304 } else {
2305 qc->tf.command = ATA_CMD_ID_ATAPI;
2306 DPRINTK("do ATAPI identify\n");
2307 }
2308
2309 qc->tf.flags |= ATA_TFLAG_DEVICE;
2310 qc->tf.protocol = ATA_PROT_PIO;
2311 qc->nsect = 1;
2312
2313 qc->waiting = &wait;
2314 qc->complete_fn = ata_qc_complete_noop;
2315
2316 spin_lock_irqsave(&ap->host_set->lock, flags);
2317 rc = ata_qc_issue(qc);
2318 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2319
2320 if (rc)
2321 goto err_out;
2322
2323 wait_for_completion(&wait);
2324
2325 swap_buf_le16(dev->id, ATA_ID_WORDS);
2326
2327 ata_dump_id(dev);
2328
2329 DPRINTK("EXIT\n");
2330
2331 return;
2332err_out:
2333 ata_port_disable(ap);
2334}
2335
2336/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002337 * ata_dev_init_params - Issue INIT DEV PARAMS command
2338 * @ap: Port associated with device @dev
2339 * @dev: Device to which command will be sent
2340 *
2341 * LOCKING:
2342 */
2343
2344static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2345{
2346 DECLARE_COMPLETION(wait);
2347 struct ata_queued_cmd *qc;
2348 int rc;
2349 unsigned long flags;
2350 u16 sectors = dev->id[6];
2351 u16 heads = dev->id[3];
2352
2353 /* Number of sectors per track 1-255. Number of heads 1-16 */
2354 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2355 return;
2356
2357 /* set up init dev params taskfile */
2358 DPRINTK("init dev params \n");
2359
2360 qc = ata_qc_new_init(ap, dev);
2361 BUG_ON(qc == NULL);
2362
2363 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2364 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2365 qc->tf.protocol = ATA_PROT_NODATA;
2366 qc->tf.nsect = sectors;
2367 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2368
2369 qc->waiting = &wait;
2370 qc->complete_fn = ata_qc_complete_noop;
2371
2372 spin_lock_irqsave(&ap->host_set->lock, flags);
2373 rc = ata_qc_issue(qc);
2374 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2375
2376 if (rc)
2377 ata_port_disable(ap);
2378 else
2379 wait_for_completion(&wait);
2380
2381 DPRINTK("EXIT\n");
2382}
2383
2384/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002385 * ata_sg_clean - Unmap DMA memory associated with command
2386 * @qc: Command containing DMA memory to be released
2387 *
2388 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 *
2390 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002391 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 */
2393
2394static void ata_sg_clean(struct ata_queued_cmd *qc)
2395{
2396 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002397 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002399 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2402 assert(sg != NULL);
2403
2404 if (qc->flags & ATA_QCFLAG_SINGLE)
2405 assert(qc->n_elem == 1);
2406
2407 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2408
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002409 /* if we padded the buffer out to 32-bit bound, and data
2410 * xfer direction is from-device, we must copy from the
2411 * pad buffer back into the supplied buffer
2412 */
2413 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2414 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2415
2416 if (qc->flags & ATA_QCFLAG_SG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002418 /* restore last sg */
2419 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2420 if (pad_buf) {
2421 struct scatterlist *psg = &qc->pad_sgent;
2422 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2423 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2424 kunmap_atomic(psg->page, KM_IRQ0);
2425 }
2426 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2428 sg_dma_len(&sg[0]), dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002429 /* restore sg */
2430 sg->length += qc->pad_len;
2431 if (pad_buf)
2432 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2433 pad_buf, qc->pad_len);
2434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002437 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
2440/**
2441 * ata_fill_sg - Fill PCI IDE PRD table
2442 * @qc: Metadata associated with taskfile to be transferred
2443 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002444 * Fill PCI IDE PRD (scatter-gather) table with segments
2445 * associated with the current disk command.
2446 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002448 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 *
2450 */
2451static void ata_fill_sg(struct ata_queued_cmd *qc)
2452{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002454 struct scatterlist *sg;
2455 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002457 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 assert(qc->n_elem > 0);
2459
2460 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002461 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 u32 addr, offset;
2463 u32 sg_len, len;
2464
2465 /* determine if physical DMA addr spans 64K boundary.
2466 * Note h/w doesn't support 64-bit, so we unconditionally
2467 * truncate dma_addr_t to u32.
2468 */
2469 addr = (u32) sg_dma_address(sg);
2470 sg_len = sg_dma_len(sg);
2471
2472 while (sg_len) {
2473 offset = addr & 0xffff;
2474 len = sg_len;
2475 if ((offset + sg_len) > 0x10000)
2476 len = 0x10000 - offset;
2477
2478 ap->prd[idx].addr = cpu_to_le32(addr);
2479 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2480 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2481
2482 idx++;
2483 sg_len -= len;
2484 addr += len;
2485 }
2486 }
2487
2488 if (idx)
2489 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2490}
2491/**
2492 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2493 * @qc: Metadata associated with taskfile to check
2494 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002495 * Allow low-level driver to filter ATA PACKET commands, returning
2496 * a status indicating whether or not it is OK to use DMA for the
2497 * supplied PACKET command.
2498 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002500 * spin_lock_irqsave(host_set lock)
2501 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 * RETURNS: 0 when ATAPI DMA can be used
2503 * nonzero otherwise
2504 */
2505int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2506{
2507 struct ata_port *ap = qc->ap;
2508 int rc = 0; /* Assume ATAPI DMA is OK by default */
2509
2510 if (ap->ops->check_atapi_dma)
2511 rc = ap->ops->check_atapi_dma(qc);
2512
2513 return rc;
2514}
2515/**
2516 * ata_qc_prep - Prepare taskfile for submission
2517 * @qc: Metadata associated with taskfile to be prepared
2518 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002519 * Prepare ATA taskfile for submission.
2520 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 * LOCKING:
2522 * spin_lock_irqsave(host_set lock)
2523 */
2524void ata_qc_prep(struct ata_queued_cmd *qc)
2525{
2526 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2527 return;
2528
2529 ata_fill_sg(qc);
2530}
2531
Jeff Garzik0cba6322005-05-30 19:49:12 -04002532/**
2533 * ata_sg_init_one - Associate command with memory buffer
2534 * @qc: Command to be associated
2535 * @buf: Memory buffer
2536 * @buflen: Length of memory buffer, in bytes.
2537 *
2538 * Initialize the data-related elements of queued_cmd @qc
2539 * to point to a single memory buffer, @buf of byte length @buflen.
2540 *
2541 * LOCKING:
2542 * spin_lock_irqsave(host_set lock)
2543 */
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2546{
2547 struct scatterlist *sg;
2548
2549 qc->flags |= ATA_QCFLAG_SINGLE;
2550
2551 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002552 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002554 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 qc->buf_virt = buf;
2556
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002557 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002558 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
Jeff Garzik0cba6322005-05-30 19:49:12 -04002561/**
2562 * ata_sg_init - Associate command with scatter-gather table.
2563 * @qc: Command to be associated
2564 * @sg: Scatter-gather table.
2565 * @n_elem: Number of elements in s/g table.
2566 *
2567 * Initialize the data-related elements of queued_cmd @qc
2568 * to point to a scatter-gather table @sg, containing @n_elem
2569 * elements.
2570 *
2571 * LOCKING:
2572 * spin_lock_irqsave(host_set lock)
2573 */
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2576 unsigned int n_elem)
2577{
2578 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002579 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002581 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
2584/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002585 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2586 * @qc: Command with memory buffer to be mapped.
2587 *
2588 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 *
2590 * LOCKING:
2591 * spin_lock_irqsave(host_set lock)
2592 *
2593 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002594 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 */
2596
2597static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2598{
2599 struct ata_port *ap = qc->ap;
2600 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002601 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 dma_addr_t dma_address;
2603
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002604 /* we must lengthen transfers to end on a 32-bit boundary */
2605 qc->pad_len = sg->length & 3;
2606 if (qc->pad_len) {
2607 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2608 struct scatterlist *psg = &qc->pad_sgent;
2609
2610 assert(qc->dev->class == ATA_DEV_ATAPI);
2611
2612 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2613
2614 if (qc->tf.flags & ATA_TFLAG_WRITE)
2615 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2616 qc->pad_len);
2617
2618 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2619 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2620 /* trim sg */
2621 sg->length -= qc->pad_len;
2622
2623 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2624 sg->length, qc->pad_len);
2625 }
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002628 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002629 if (dma_mapping_error(dma_address)) {
2630 /* restore sg */
2631 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002636 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2639 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2640
2641 return 0;
2642}
2643
2644/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002645 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2646 * @qc: Command with scatter-gather table to be mapped.
2647 *
2648 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 *
2650 * LOCKING:
2651 * spin_lock_irqsave(host_set lock)
2652 *
2653 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002654 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 *
2656 */
2657
2658static int ata_sg_setup(struct ata_queued_cmd *qc)
2659{
2660 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002661 struct scatterlist *sg = qc->__sg;
2662 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 int n_elem, dir;
2664
2665 VPRINTK("ENTER, ata%u\n", ap->id);
2666 assert(qc->flags & ATA_QCFLAG_SG);
2667
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002668 /* we must lengthen transfers to end on a 32-bit boundary */
2669 qc->pad_len = lsg->length & 3;
2670 if (qc->pad_len) {
2671 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2672 struct scatterlist *psg = &qc->pad_sgent;
2673 unsigned int offset;
2674
2675 assert(qc->dev->class == ATA_DEV_ATAPI);
2676
2677 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2678
2679 /*
2680 * psg->page/offset are used to copy to-be-written
2681 * data in this function or read data in ata_sg_clean.
2682 */
2683 offset = lsg->offset + lsg->length - qc->pad_len;
2684 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2685 psg->offset = offset_in_page(offset);
2686
2687 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2688 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2689 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2690 kunmap_atomic(psg->page, KM_IRQ0);
2691 }
2692
2693 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2694 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2695 /* trim last sg */
2696 lsg->length -= qc->pad_len;
2697
2698 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2699 qc->n_elem - 1, lsg->length, qc->pad_len);
2700 }
2701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 dir = qc->dma_dir;
2703 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002704 if (n_elem < 1) {
2705 /* restore last sg */
2706 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 DPRINTK("%d sg elements mapped\n", n_elem);
2711
2712 qc->n_elem = n_elem;
2713
2714 return 0;
2715}
2716
2717/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002718 * ata_poll_qc_complete - turn irq back on and finish qc
2719 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002720 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002721 *
2722 * LOCKING:
2723 * None. (grabs host lock)
2724 */
2725
Jeff Garzika7dac442005-10-30 04:44:42 -05002726void ata_poll_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
Tejun Heo40e8c822005-08-22 17:12:45 +09002727{
2728 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002729 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002730
Jeff Garzikb8f61532005-08-25 22:01:20 -04002731 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002732 ata_irq_on(ap);
Jeff Garzika7dac442005-10-30 04:44:42 -05002733 ata_qc_complete(qc, err_mask);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002734 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002735}
2736
2737/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002739 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 *
2741 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002742 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 *
2744 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002745 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 */
2747
2748static unsigned long ata_pio_poll(struct ata_port *ap)
2749{
2750 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002751 unsigned int poll_state = HSM_ST_UNKNOWN;
2752 unsigned int reg_state = HSM_ST_UNKNOWN;
2753 const unsigned int tmout_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Albert Lee14be71f2005-09-27 17:36:35 +08002755 switch (ap->hsm_task_state) {
2756 case HSM_ST:
2757 case HSM_ST_POLL:
2758 poll_state = HSM_ST_POLL;
2759 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002761 case HSM_ST_LAST:
2762 case HSM_ST_LAST_POLL:
2763 poll_state = HSM_ST_LAST_POLL;
2764 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 break;
2766 default:
2767 BUG();
2768 break;
2769 }
2770
2771 status = ata_chk_status(ap);
2772 if (status & ATA_BUSY) {
2773 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002774 ap->hsm_task_state = tmout_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return 0;
2776 }
Albert Lee14be71f2005-09-27 17:36:35 +08002777 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return ATA_SHORT_PAUSE;
2779 }
2780
Albert Lee14be71f2005-09-27 17:36:35 +08002781 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return 0;
2783}
2784
2785/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002786 * ata_pio_complete - check if drive is busy or idle
2787 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 *
2789 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002790 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002791 *
2792 * RETURNS:
2793 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 */
2795
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002796static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
2798 struct ata_queued_cmd *qc;
2799 u8 drv_stat;
2800
2801 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002802 * This is purely heuristic. This is a fast path. Sometimes when
2803 * we enter, BSY will be cleared in a chk-status or two. If not,
2804 * the drive is probably seeking or something. Snooze for a couple
2805 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002806 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 */
2808 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2809 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2810 msleep(2);
2811 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2812 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002813 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 }
2817 }
2818
2819 drv_stat = ata_wait_idle(ap);
2820 if (!ata_ok(drv_stat)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002821 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 }
2824
2825 qc = ata_qc_from_tag(ap, ap->active_tag);
2826 assert(qc != NULL);
2827
Albert Lee14be71f2005-09-27 17:36:35 +08002828 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Jeff Garzika7dac442005-10-30 04:44:42 -05002830 ata_poll_qc_complete(qc, 0);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002831
2832 /* another command may start at this point */
2833
2834 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
Edward Falk0baab862005-06-02 18:17:13 -04002837
2838/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002839 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002840 * @buf: Buffer to swap
2841 * @buf_words: Number of 16-bit words in buffer.
2842 *
2843 * Swap halves of 16-bit words if needed to convert from
2844 * little-endian byte order to native cpu byte order, or
2845 * vice-versa.
2846 *
2847 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002848 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850void swap_buf_le16(u16 *buf, unsigned int buf_words)
2851{
2852#ifdef __BIG_ENDIAN
2853 unsigned int i;
2854
2855 for (i = 0; i < buf_words; i++)
2856 buf[i] = le16_to_cpu(buf[i]);
2857#endif /* __BIG_ENDIAN */
2858}
2859
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002860/**
2861 * ata_mmio_data_xfer - Transfer data by MMIO
2862 * @ap: port to read/write
2863 * @buf: data buffer
2864 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002865 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002866 *
2867 * Transfer data from/to the device data register by MMIO.
2868 *
2869 * LOCKING:
2870 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002871 */
2872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2874 unsigned int buflen, int write_data)
2875{
2876 unsigned int i;
2877 unsigned int words = buflen >> 1;
2878 u16 *buf16 = (u16 *) buf;
2879 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2880
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002881 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (write_data) {
2883 for (i = 0; i < words; i++)
2884 writew(le16_to_cpu(buf16[i]), mmio);
2885 } else {
2886 for (i = 0; i < words; i++)
2887 buf16[i] = cpu_to_le16(readw(mmio));
2888 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002889
2890 /* Transfer trailing 1 byte, if any. */
2891 if (unlikely(buflen & 0x01)) {
2892 u16 align_buf[1] = { 0 };
2893 unsigned char *trailing_buf = buf + buflen - 1;
2894
2895 if (write_data) {
2896 memcpy(align_buf, trailing_buf, 1);
2897 writew(le16_to_cpu(align_buf[0]), mmio);
2898 } else {
2899 align_buf[0] = cpu_to_le16(readw(mmio));
2900 memcpy(trailing_buf, align_buf, 1);
2901 }
2902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002905/**
2906 * ata_pio_data_xfer - Transfer data by PIO
2907 * @ap: port to read/write
2908 * @buf: data buffer
2909 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002910 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002911 *
2912 * Transfer data from/to the device data register by PIO.
2913 *
2914 * LOCKING:
2915 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002916 */
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2919 unsigned int buflen, int write_data)
2920{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002921 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002923 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002925 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002927 insw(ap->ioaddr.data_addr, buf, words);
2928
2929 /* Transfer trailing 1 byte, if any. */
2930 if (unlikely(buflen & 0x01)) {
2931 u16 align_buf[1] = { 0 };
2932 unsigned char *trailing_buf = buf + buflen - 1;
2933
2934 if (write_data) {
2935 memcpy(align_buf, trailing_buf, 1);
2936 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2937 } else {
2938 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2939 memcpy(trailing_buf, align_buf, 1);
2940 }
2941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002944/**
2945 * ata_data_xfer - Transfer data from/to the data register.
2946 * @ap: port to read/write
2947 * @buf: data buffer
2948 * @buflen: buffer length
2949 * @do_write: read/write
2950 *
2951 * Transfer data from/to the device data register.
2952 *
2953 * LOCKING:
2954 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002955 */
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2958 unsigned int buflen, int do_write)
2959{
2960 if (ap->flags & ATA_FLAG_MMIO)
2961 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2962 else
2963 ata_pio_data_xfer(ap, buf, buflen, do_write);
2964}
2965
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002966/**
2967 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2968 * @qc: Command on going
2969 *
2970 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2971 *
2972 * LOCKING:
2973 * Inherited from caller.
2974 */
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976static void ata_pio_sector(struct ata_queued_cmd *qc)
2977{
2978 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002979 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 struct ata_port *ap = qc->ap;
2981 struct page *page;
2982 unsigned int offset;
2983 unsigned char *buf;
2984
2985 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08002986 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 page = sg[qc->cursg].page;
2989 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2990
2991 /* get the current page and offset */
2992 page = nth_page(page, (offset >> PAGE_SHIFT));
2993 offset %= PAGE_SIZE;
2994
Albert Lee7282aa42005-10-09 09:46:07 -04002995 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2996
Albert Lee91b8b312005-10-09 09:48:44 -04002997 if (PageHighMem(page)) {
2998 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04002999
Albert Lee91b8b312005-10-09 09:48:44 -04003000 local_irq_save(flags);
3001 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003002
Albert Lee91b8b312005-10-09 09:48:44 -04003003 /* do the actual data transfer */
3004 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3005
3006 kunmap_atomic(buf, KM_IRQ0);
3007 local_irq_restore(flags);
3008 } else {
3009 buf = page_address(page);
3010 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3011 }
Albert Lee7282aa42005-10-09 09:46:07 -04003012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 qc->cursect++;
3014 qc->cursg_ofs++;
3015
Albert Lee32529e02005-05-26 03:49:42 -04003016 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 qc->cursg++;
3018 qc->cursg_ofs = 0;
3019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003022/**
Albert Leec71c1852005-10-04 06:03:45 -04003023 * atapi_send_cdb - Write CDB bytes to hardware
3024 * @ap: Port to which ATAPI device is attached.
3025 * @qc: Taskfile currently active
3026 *
3027 * When device has indicated its readiness to accept
3028 * a CDB, this function is called. Send the CDB.
3029 *
3030 * LOCKING:
3031 * caller.
3032 */
3033
3034static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3035{
3036 /* send SCSI cdb */
3037 DPRINTK("send cdb\n");
3038 assert(ap->cdb_len >= 12);
3039
3040 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3041 ata_altstatus(ap); /* flush */
3042
3043 switch (qc->tf.protocol) {
3044 case ATA_PROT_ATAPI:
3045 ap->hsm_task_state = HSM_ST;
3046 break;
3047 case ATA_PROT_ATAPI_NODATA:
3048 ap->hsm_task_state = HSM_ST_LAST;
3049 break;
3050 case ATA_PROT_ATAPI_DMA:
3051 ap->hsm_task_state = HSM_ST_LAST;
3052 /* initiate bmdma */
3053 ap->ops->bmdma_start(qc);
3054 break;
3055 }
3056}
3057
3058/**
Albert Leee27486d2005-11-01 19:24:49 +08003059 * ata_pio_first_block - Write first data block to hardware
3060 * @ap: Port to which ATA/ATAPI device is attached.
Albert Leec71c1852005-10-04 06:03:45 -04003061 *
3062 * When device has indicated its readiness to accept
3063 * the data, this function sends out the CDB or
3064 * the first data block by PIO.
3065 * After this,
3066 * - If polling, ata_pio_task() handles the rest.
3067 * - Otherwise, interrupt handler takes over.
3068 *
3069 * LOCKING:
3070 * Kernel thread context (may sleep)
3071 */
3072
Albert Leee27486d2005-11-01 19:24:49 +08003073static void ata_pio_first_block(struct ata_port *ap)
Albert Leec71c1852005-10-04 06:03:45 -04003074{
Albert Leec71c1852005-10-04 06:03:45 -04003075 struct ata_queued_cmd *qc;
3076 u8 status;
3077 unsigned long flags;
3078
3079 qc = ata_qc_from_tag(ap, ap->active_tag);
3080 assert(qc != NULL);
3081 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3082
3083 /* sleep-wait for BSY to clear */
3084 DPRINTK("busy wait\n");
3085 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT))
3086 goto err_out;
3087
3088 /* make sure DRQ is set */
3089 status = ata_chk_status(ap);
3090 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3091 goto err_out;
3092
3093 /* Send the CDB (atapi) or the first data block (ata pio out).
3094 * During the state transition, interrupt handler shouldn't
3095 * be invoked before the data transfer is complete and
3096 * hsm_task_state is changed. Hence, the following locking.
3097 */
3098 spin_lock_irqsave(&ap->host_set->lock, flags);
3099
3100 if (qc->tf.protocol == ATA_PROT_PIO) {
3101 /* PIO data out protocol.
3102 * send first data block.
3103 */
3104
3105 /* ata_pio_sector() might change the state to HSM_ST_LAST.
3106 * so, the state is changed here before ata_pio_sector().
3107 */
3108 ap->hsm_task_state = HSM_ST;
3109 ata_pio_sector(qc);
3110 ata_altstatus(ap); /* flush */
3111 } else
3112 /* send CDB */
3113 atapi_send_cdb(ap, qc);
3114
3115 /* if polling, ata_pio_task() handles the rest.
3116 * otherwise, interrupt handler takes over from here.
3117 */
3118 if (qc->tf.flags & ATA_TFLAG_POLLING)
3119 queue_work(ata_wq, &ap->pio_task);
3120
3121 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3122
3123 return;
3124
3125err_out:
3126 ata_pio_error(ap);
3127}
3128
3129/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003130 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3131 * @qc: Command on going
3132 * @bytes: number of bytes
3133 *
3134 * Transfer Transfer data from/to the ATAPI device.
3135 *
3136 * LOCKING:
3137 * Inherited from caller.
3138 *
3139 */
3140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3142{
3143 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003144 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 struct ata_port *ap = qc->ap;
3146 struct page *page;
3147 unsigned char *buf;
3148 unsigned int offset, count;
3149
Albert Lee563a6e12005-08-12 14:17:50 +08003150 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003151 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003154 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003155 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003156 * The end of qc->sg is reached and the device expects
3157 * more data to transfer. In order not to overrun qc->sg
3158 * and fulfill length specified in the byte count register,
3159 * - for read case, discard trailing data from the device
3160 * - for write case, padding zero data to the device
3161 */
3162 u16 pad_buf[1] = { 0 };
3163 unsigned int words = bytes >> 1;
3164 unsigned int i;
3165
3166 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003167 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003168 ap->id, bytes);
3169
3170 for (i = 0; i < words; i++)
3171 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3172
Albert Lee14be71f2005-09-27 17:36:35 +08003173 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003174 return;
3175 }
3176
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003177 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 page = sg->page;
3180 offset = sg->offset + qc->cursg_ofs;
3181
3182 /* get the current page and offset */
3183 page = nth_page(page, (offset >> PAGE_SHIFT));
3184 offset %= PAGE_SIZE;
3185
Albert Lee6952df02005-06-06 15:56:03 +08003186 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003187 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189 /* don't cross page boundaries */
3190 count = min(count, (unsigned int)PAGE_SIZE - offset);
3191
Albert Lee7282aa42005-10-09 09:46:07 -04003192 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3193
Albert Lee91b8b312005-10-09 09:48:44 -04003194 if (PageHighMem(page)) {
3195 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003196
Albert Lee91b8b312005-10-09 09:48:44 -04003197 local_irq_save(flags);
3198 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003199
Albert Lee91b8b312005-10-09 09:48:44 -04003200 /* do the actual data transfer */
3201 ata_data_xfer(ap, buf + offset, count, do_write);
3202
3203 kunmap_atomic(buf, KM_IRQ0);
3204 local_irq_restore(flags);
3205 } else {
3206 buf = page_address(page);
3207 ata_data_xfer(ap, buf + offset, count, do_write);
3208 }
Albert Lee7282aa42005-10-09 09:46:07 -04003209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 bytes -= count;
3211 qc->curbytes += count;
3212 qc->cursg_ofs += count;
3213
Albert Lee32529e02005-05-26 03:49:42 -04003214 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 qc->cursg++;
3216 qc->cursg_ofs = 0;
3217 }
3218
Albert Lee563a6e12005-08-12 14:17:50 +08003219 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003223/**
3224 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3225 * @qc: Command on going
3226 *
3227 * Transfer Transfer data from/to the ATAPI device.
3228 *
3229 * LOCKING:
3230 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003231 */
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3234{
3235 struct ata_port *ap = qc->ap;
3236 struct ata_device *dev = qc->dev;
3237 unsigned int ireason, bc_lo, bc_hi, bytes;
3238 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3239
3240 ap->ops->tf_read(ap, &qc->tf);
3241 ireason = qc->tf.nsect;
3242 bc_lo = qc->tf.lbam;
3243 bc_hi = qc->tf.lbah;
3244 bytes = (bc_hi << 8) | bc_lo;
3245
3246 /* shall be cleared to zero, indicating xfer of data */
3247 if (ireason & (1 << 0))
3248 goto err_out;
3249
3250 /* make sure transfer direction matches expected */
3251 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3252 if (do_write != i_write)
3253 goto err_out;
3254
Albert Lee312f7da2005-09-27 17:38:03 +08003255 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 __atapi_pio_bytes(qc, bytes);
3258
3259 return;
3260
3261err_out:
3262 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3263 ap->id, dev->devno);
Albert Lee14be71f2005-09-27 17:36:35 +08003264 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266
3267/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003268 * ata_pio_block - start PIO on a block
3269 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 *
3271 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003272 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 */
3274
3275static void ata_pio_block(struct ata_port *ap)
3276{
3277 struct ata_queued_cmd *qc;
3278 u8 status;
3279
3280 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003281 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 * Sometimes when we enter, BSY will be cleared in
3283 * a chk-status or two. If not, the drive is probably seeking
3284 * or something. Snooze for a couple msecs, then
3285 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003286 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 */
3288 status = ata_busy_wait(ap, ATA_BUSY, 5);
3289 if (status & ATA_BUSY) {
3290 msleep(2);
3291 status = ata_busy_wait(ap, ATA_BUSY, 10);
3292 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003293 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3295 return;
3296 }
3297 }
3298
3299 qc = ata_qc_from_tag(ap, ap->active_tag);
3300 assert(qc != NULL);
3301
3302 if (is_atapi_taskfile(&qc->tf)) {
3303 /* no more data to transfer or unsupported ATAPI command */
3304 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003305 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return;
3307 }
3308
3309 atapi_pio_bytes(qc);
3310 } else {
3311 /* handle BSY=0, DRQ=0 as error */
3312 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003313 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 return;
3315 }
3316
3317 ata_pio_sector(qc);
3318 }
Albert Lee467b16d2005-11-01 19:19:01 +08003319
3320 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
3323static void ata_pio_error(struct ata_port *ap)
3324{
3325 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003326
3327 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
3329 qc = ata_qc_from_tag(ap, ap->active_tag);
3330 assert(qc != NULL);
3331
Albert Lee14be71f2005-09-27 17:36:35 +08003332 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Jeff Garzika7dac442005-10-30 04:44:42 -05003334 ata_poll_qc_complete(qc, AC_ERR_ATA_BUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
3337static void ata_pio_task(void *_data)
3338{
3339 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003340 unsigned long timeout;
3341 int qc_completed;
3342
3343fsm_start:
3344 timeout = 0;
3345 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Albert Lee14be71f2005-09-27 17:36:35 +08003347 switch (ap->hsm_task_state) {
Albert Leee27486d2005-11-01 19:24:49 +08003348 case HSM_ST_FIRST:
3349 ata_pio_first_block(ap);
3350 return;
3351
Albert Lee14be71f2005-09-27 17:36:35 +08003352 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 ata_pio_block(ap);
3354 break;
3355
Albert Lee14be71f2005-09-27 17:36:35 +08003356 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003357 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 break;
3359
Albert Lee14be71f2005-09-27 17:36:35 +08003360 case HSM_ST_POLL:
3361 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 timeout = ata_pio_poll(ap);
3363 break;
3364
Albert Lee14be71f2005-09-27 17:36:35 +08003365 case HSM_ST_TMOUT:
3366 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 ata_pio_error(ap);
3368 return;
Albert Lee467b16d2005-11-01 19:19:01 +08003369
3370 default:
3371 BUG();
3372 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
3374
3375 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003376 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3377 else if (!qc_completed)
3378 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379}
3380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381/**
3382 * ata_qc_timeout - Handle timeout of queued command
3383 * @qc: Command that timed out
3384 *
3385 * Some part of the kernel (currently, only the SCSI layer)
3386 * has noticed that the active command on port @ap has not
3387 * completed after a specified length of time. Handle this
3388 * condition by disabling DMA (if necessary) and completing
3389 * transactions, with error if necessary.
3390 *
3391 * This also handles the case of the "lost interrupt", where
3392 * for some reason (possibly hardware bug, possibly driver bug)
3393 * an interrupt was not delivered to the driver, even though the
3394 * transaction completed successfully.
3395 *
3396 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003397 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 */
3399
3400static void ata_qc_timeout(struct ata_queued_cmd *qc)
3401{
3402 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003403 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 struct ata_device *dev = qc->dev;
3405 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003406 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408 DPRINTK("ENTER\n");
3409
3410 /* FIXME: doesn't this conflict with timeout handling? */
3411 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3412 struct scsi_cmnd *cmd = qc->scsicmd;
3413
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02003414 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416 /* finish completing original command */
Jeff Garzikb8f61532005-08-25 22:01:20 -04003417 spin_lock_irqsave(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 __ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003419 spin_unlock_irqrestore(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
3421 atapi_request_sense(ap, dev, cmd);
3422
3423 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3424 scsi_finish_command(cmd);
3425
3426 goto out;
3427 }
3428 }
3429
Jeff Garzikb8f61532005-08-25 22:01:20 -04003430 spin_lock_irqsave(&host_set->lock, flags);
3431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 /* hack alert! We cannot use the supplied completion
3433 * function from inside the ->eh_strategy_handler() thread.
3434 * libata is the only user of ->eh_strategy_handler() in
3435 * any kernel, so the default scsi_done() assumes it is
3436 * not being called from the SCSI EH.
3437 */
3438 qc->scsidone = scsi_finish_command;
3439
3440 switch (qc->tf.protocol) {
3441
3442 case ATA_PROT_DMA:
3443 case ATA_PROT_ATAPI_DMA:
3444 host_stat = ap->ops->bmdma_status(ap);
3445
3446 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003447 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
3449 /* fall through */
3450
3451 default:
3452 ata_altstatus(ap);
3453 drv_stat = ata_chk_status(ap);
3454
3455 /* ack bmdma irq events */
3456 ap->ops->irq_clear(ap);
3457
3458 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3459 ap->id, qc->tf.command, drv_stat, host_stat);
3460
Albert Lee312f7da2005-09-27 17:38:03 +08003461 ap->hsm_task_state = HSM_ST_IDLE;
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 /* complete taskfile transaction */
Jeff Garzika7dac442005-10-30 04:44:42 -05003464 ata_qc_complete(qc, ac_err_mask(drv_stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 break;
3466 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003467
3468 spin_unlock_irqrestore(&host_set->lock, flags);
3469
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470out:
3471 DPRINTK("EXIT\n");
3472}
3473
3474/**
3475 * ata_eng_timeout - Handle timeout of queued command
3476 * @ap: Port on which timed-out command is active
3477 *
3478 * Some part of the kernel (currently, only the SCSI layer)
3479 * has noticed that the active command on port @ap has not
3480 * completed after a specified length of time. Handle this
3481 * condition by disabling DMA (if necessary) and completing
3482 * transactions, with error if necessary.
3483 *
3484 * This also handles the case of the "lost interrupt", where
3485 * for some reason (possibly hardware bug, possibly driver bug)
3486 * an interrupt was not delivered to the driver, even though the
3487 * transaction completed successfully.
3488 *
3489 * LOCKING:
3490 * Inherited from SCSI layer (none, can sleep)
3491 */
3492
3493void ata_eng_timeout(struct ata_port *ap)
3494{
3495 struct ata_queued_cmd *qc;
3496
3497 DPRINTK("ENTER\n");
3498
3499 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003500 if (qc)
3501 ata_qc_timeout(qc);
3502 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3504 ap->id);
3505 goto out;
3506 }
3507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508out:
3509 DPRINTK("EXIT\n");
3510}
3511
3512/**
3513 * ata_qc_new - Request an available ATA command, for queueing
3514 * @ap: Port associated with device @dev
3515 * @dev: Device from whom we request an available command structure
3516 *
3517 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003518 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 */
3520
3521static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3522{
3523 struct ata_queued_cmd *qc = NULL;
3524 unsigned int i;
3525
3526 for (i = 0; i < ATA_MAX_QUEUE; i++)
3527 if (!test_and_set_bit(i, &ap->qactive)) {
3528 qc = ata_qc_from_tag(ap, i);
3529 break;
3530 }
3531
3532 if (qc)
3533 qc->tag = i;
3534
3535 return qc;
3536}
3537
3538/**
3539 * ata_qc_new_init - Request an available ATA command, and initialize it
3540 * @ap: Port associated with device @dev
3541 * @dev: Device from whom we request an available command structure
3542 *
3543 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003544 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 */
3546
3547struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3548 struct ata_device *dev)
3549{
3550 struct ata_queued_cmd *qc;
3551
3552 qc = ata_qc_new(ap);
3553 if (qc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003554 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 qc->flags = 0;
3556 qc->scsicmd = NULL;
3557 qc->ap = ap;
3558 qc->dev = dev;
3559 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3560 qc->nsect = 0;
3561 qc->nbytes = qc->curbytes = 0;
3562
3563 ata_tf_init(ap, &qc->tf, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
3565
3566 return qc;
3567}
3568
Jeff Garzika7dac442005-10-30 04:44:42 -05003569int ata_qc_complete_noop(struct ata_queued_cmd *qc, unsigned int err_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
3571 return 0;
3572}
3573
3574static void __ata_qc_complete(struct ata_queued_cmd *qc)
3575{
3576 struct ata_port *ap = qc->ap;
3577 unsigned int tag, do_clear = 0;
3578
3579 qc->flags = 0;
3580 tag = qc->tag;
3581 if (likely(ata_tag_valid(tag))) {
3582 if (tag == ap->active_tag)
3583 ap->active_tag = ATA_TAG_POISON;
3584 qc->tag = ATA_TAG_POISON;
3585 do_clear = 1;
3586 }
3587
3588 if (qc->waiting) {
3589 struct completion *waiting = qc->waiting;
3590 qc->waiting = NULL;
3591 complete(waiting);
3592 }
3593
3594 if (likely(do_clear))
3595 clear_bit(tag, &ap->qactive);
3596}
3597
3598/**
3599 * ata_qc_free - free unused ata_queued_cmd
3600 * @qc: Command to complete
3601 *
3602 * Designed to free unused ata_queued_cmd object
3603 * in case something prevents using it.
3604 *
3605 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003606 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 */
3608void ata_qc_free(struct ata_queued_cmd *qc)
3609{
3610 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3611 assert(qc->waiting == NULL); /* nothing should be waiting */
3612
3613 __ata_qc_complete(qc);
3614}
3615
3616/**
3617 * ata_qc_complete - Complete an active ATA command
3618 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003619 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003620 *
3621 * Indicate to the mid and upper layers that an ATA
3622 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 *
3624 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003625 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 */
3627
Jeff Garzika7dac442005-10-30 04:44:42 -05003628void ata_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
3630 int rc;
3631
3632 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3633 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3634
3635 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3636 ata_sg_clean(qc);
3637
Albert Lee3f3791d2005-08-16 14:25:38 +08003638 /* atapi: mark qc as inactive to prevent the interrupt handler
3639 * from completing the command twice later, before the error handler
3640 * is called. (when rc != 0 and atapi request sense is needed)
3641 */
3642 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 /* call completion callback */
Jeff Garzika7dac442005-10-30 04:44:42 -05003645 rc = qc->complete_fn(qc, err_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
3647 /* if callback indicates not to complete command (non-zero),
3648 * return immediately
3649 */
3650 if (rc != 0)
3651 return;
3652
3653 __ata_qc_complete(qc);
3654
3655 VPRINTK("EXIT\n");
3656}
3657
3658static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3659{
3660 struct ata_port *ap = qc->ap;
3661
3662 switch (qc->tf.protocol) {
3663 case ATA_PROT_DMA:
3664 case ATA_PROT_ATAPI_DMA:
3665 return 1;
3666
3667 case ATA_PROT_ATAPI:
3668 case ATA_PROT_PIO:
3669 case ATA_PROT_PIO_MULT:
3670 if (ap->flags & ATA_FLAG_PIO_DMA)
3671 return 1;
3672
3673 /* fall through */
3674
3675 default:
3676 return 0;
3677 }
3678
3679 /* never reached */
3680}
3681
3682/**
3683 * ata_qc_issue - issue taskfile to device
3684 * @qc: command to issue to device
3685 *
3686 * Prepare an ATA command to submission to device.
3687 * This includes mapping the data into a DMA-able
3688 * area, filling in the S/G table, and finally
3689 * writing the taskfile to hardware, starting the command.
3690 *
3691 * LOCKING:
3692 * spin_lock_irqsave(host_set lock)
3693 *
3694 * RETURNS:
3695 * Zero on success, negative on error.
3696 */
3697
3698int ata_qc_issue(struct ata_queued_cmd *qc)
3699{
3700 struct ata_port *ap = qc->ap;
3701
3702 if (ata_should_dma_map(qc)) {
3703 if (qc->flags & ATA_QCFLAG_SG) {
3704 if (ata_sg_setup(qc))
3705 goto err_out;
3706 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3707 if (ata_sg_setup_one(qc))
3708 goto err_out;
3709 }
3710 } else {
3711 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3712 }
3713
3714 ap->ops->qc_prep(qc);
3715
3716 qc->ap->active_tag = qc->tag;
3717 qc->flags |= ATA_QCFLAG_ACTIVE;
3718
3719 return ap->ops->qc_issue(qc);
3720
3721err_out:
3722 return -1;
3723}
3724
Edward Falk0baab862005-06-02 18:17:13 -04003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726/**
3727 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3728 * @qc: command to issue to device
3729 *
3730 * Using various libata functions and hooks, this function
3731 * starts an ATA command. ATA commands are grouped into
3732 * classes called "protocols", and issuing each type of protocol
3733 * is slightly different.
3734 *
Edward Falk0baab862005-06-02 18:17:13 -04003735 * May be used as the qc_issue() entry in ata_port_operations.
3736 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 * LOCKING:
3738 * spin_lock_irqsave(host_set lock)
3739 *
3740 * RETURNS:
3741 * Zero on success, negative on error.
3742 */
3743
3744int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3745{
3746 struct ata_port *ap = qc->ap;
3747
Albert Leee50362e2005-09-27 17:39:50 +08003748 /* Use polling pio if the LLD doesn't handle
3749 * interrupt driven pio and atapi CDB interrupt.
3750 */
3751 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3752 switch (qc->tf.protocol) {
3753 case ATA_PROT_PIO:
3754 case ATA_PROT_ATAPI:
3755 case ATA_PROT_ATAPI_NODATA:
3756 qc->tf.flags |= ATA_TFLAG_POLLING;
3757 break;
3758 case ATA_PROT_ATAPI_DMA:
3759 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3760 BUG();
3761 break;
3762 default:
3763 break;
3764 }
3765 }
3766
Albert Lee312f7da2005-09-27 17:38:03 +08003767 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 ata_dev_select(ap, qc->dev->devno, 1, 0);
3769
Albert Lee312f7da2005-09-27 17:38:03 +08003770 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 switch (qc->tf.protocol) {
3772 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003773 if (qc->tf.flags & ATA_TFLAG_POLLING)
3774 ata_qc_set_polling(qc);
3775
Jeff Garzike5338252005-10-30 21:37:17 -05003776 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003777 ap->hsm_task_state = HSM_ST_LAST;
3778
3779 if (qc->tf.flags & ATA_TFLAG_POLLING)
3780 queue_work(ata_wq, &ap->pio_task);
3781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 break;
3783
3784 case ATA_PROT_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003785 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3788 ap->ops->bmdma_setup(qc); /* set up bmdma */
3789 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003790 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 break;
3792
Albert Lee312f7da2005-09-27 17:38:03 +08003793 case ATA_PROT_PIO:
3794 if (qc->tf.flags & ATA_TFLAG_POLLING)
3795 ata_qc_set_polling(qc);
3796
Jeff Garzike5338252005-10-30 21:37:17 -05003797 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003798
Albert Lee54f00382005-09-30 19:14:19 +08003799 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3800 /* PIO data out protocol */
3801 ap->hsm_task_state = HSM_ST_FIRST;
Albert Leee27486d2005-11-01 19:24:49 +08003802 queue_work(ata_wq, &ap->pio_task);
Albert Lee54f00382005-09-30 19:14:19 +08003803
3804 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08003805 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08003806 */
Albert Lee312f7da2005-09-27 17:38:03 +08003807 } else {
Albert Lee54f00382005-09-30 19:14:19 +08003808 /* PIO data in protocol */
3809 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08003810
Albert Lee54f00382005-09-30 19:14:19 +08003811 if (qc->tf.flags & ATA_TFLAG_POLLING)
3812 queue_work(ata_wq, &ap->pio_task);
Albert Lee312f7da2005-09-27 17:38:03 +08003813
Albert Lee54f00382005-09-30 19:14:19 +08003814 /* if polling, ata_pio_task() handles the rest.
3815 * otherwise, interrupt handler takes over from here.
3816 */
Albert Lee312f7da2005-09-27 17:38:03 +08003817 }
3818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 break;
3820
3821 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003823 if (qc->tf.flags & ATA_TFLAG_POLLING)
3824 ata_qc_set_polling(qc);
3825
Jeff Garzike5338252005-10-30 21:37:17 -05003826 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003827 ap->hsm_task_state = HSM_ST_FIRST;
3828
3829 /* send cdb by polling if no cdb interrupt */
3830 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3831 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Leee27486d2005-11-01 19:24:49 +08003832 queue_work(ata_wq, &ap->pio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 break;
3834
3835 case ATA_PROT_ATAPI_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003836 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3839 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003840 ap->hsm_task_state = HSM_ST_FIRST;
3841
3842 /* send cdb by polling if no cdb interrupt */
3843 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Leee27486d2005-11-01 19:24:49 +08003844 queue_work(ata_wq, &ap->pio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 break;
3846
3847 default:
3848 WARN_ON(1);
3849 return -1;
3850 }
3851
3852 return 0;
3853}
3854
3855/**
Edward Falk0baab862005-06-02 18:17:13 -04003856 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 * @qc: Info associated with this ATA transaction.
3858 *
3859 * LOCKING:
3860 * spin_lock_irqsave(host_set lock)
3861 */
3862
3863static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3864{
3865 struct ata_port *ap = qc->ap;
3866 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3867 u8 dmactl;
3868 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3869
3870 /* load PRD table addr. */
3871 mb(); /* make sure PRD table writes are visible to controller */
3872 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3873
3874 /* specify data direction, triple-check start bit is clear */
3875 dmactl = readb(mmio + ATA_DMA_CMD);
3876 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3877 if (!rw)
3878 dmactl |= ATA_DMA_WR;
3879 writeb(dmactl, mmio + ATA_DMA_CMD);
3880
3881 /* issue r/w command */
3882 ap->ops->exec_command(ap, &qc->tf);
3883}
3884
3885/**
Alan Coxb73fc892005-08-26 16:03:19 +01003886 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 * @qc: Info associated with this ATA transaction.
3888 *
3889 * LOCKING:
3890 * spin_lock_irqsave(host_set lock)
3891 */
3892
3893static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3894{
3895 struct ata_port *ap = qc->ap;
3896 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3897 u8 dmactl;
3898
3899 /* start host DMA transaction */
3900 dmactl = readb(mmio + ATA_DMA_CMD);
3901 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3902
3903 /* Strictly, one may wish to issue a readb() here, to
3904 * flush the mmio write. However, control also passes
3905 * to the hardware at this point, and it will interrupt
3906 * us when we are to resume control. So, in effect,
3907 * we don't care when the mmio write flushes.
3908 * Further, a read of the DMA status register _immediately_
3909 * following the write may not be what certain flaky hardware
3910 * is expected, so I think it is best to not add a readb()
3911 * without first all the MMIO ATA cards/mobos.
3912 * Or maybe I'm just being paranoid.
3913 */
3914}
3915
3916/**
3917 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3918 * @qc: Info associated with this ATA transaction.
3919 *
3920 * LOCKING:
3921 * spin_lock_irqsave(host_set lock)
3922 */
3923
3924static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3925{
3926 struct ata_port *ap = qc->ap;
3927 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3928 u8 dmactl;
3929
3930 /* load PRD table addr. */
3931 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3932
3933 /* specify data direction, triple-check start bit is clear */
3934 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3935 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3936 if (!rw)
3937 dmactl |= ATA_DMA_WR;
3938 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3939
3940 /* issue r/w command */
3941 ap->ops->exec_command(ap, &qc->tf);
3942}
3943
3944/**
3945 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3946 * @qc: Info associated with this ATA transaction.
3947 *
3948 * LOCKING:
3949 * spin_lock_irqsave(host_set lock)
3950 */
3951
3952static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3953{
3954 struct ata_port *ap = qc->ap;
3955 u8 dmactl;
3956
3957 /* start host DMA transaction */
3958 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3959 outb(dmactl | ATA_DMA_START,
3960 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3961}
3962
Edward Falk0baab862005-06-02 18:17:13 -04003963
3964/**
3965 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3966 * @qc: Info associated with this ATA transaction.
3967 *
3968 * Writes the ATA_DMA_START flag to the DMA command register.
3969 *
3970 * May be used as the bmdma_start() entry in ata_port_operations.
3971 *
3972 * LOCKING:
3973 * spin_lock_irqsave(host_set lock)
3974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975void ata_bmdma_start(struct ata_queued_cmd *qc)
3976{
3977 if (qc->ap->flags & ATA_FLAG_MMIO)
3978 ata_bmdma_start_mmio(qc);
3979 else
3980 ata_bmdma_start_pio(qc);
3981}
3982
Edward Falk0baab862005-06-02 18:17:13 -04003983
3984/**
3985 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3986 * @qc: Info associated with this ATA transaction.
3987 *
3988 * Writes address of PRD table to device's PRD Table Address
3989 * register, sets the DMA control register, and calls
3990 * ops->exec_command() to start the transfer.
3991 *
3992 * May be used as the bmdma_setup() entry in ata_port_operations.
3993 *
3994 * LOCKING:
3995 * spin_lock_irqsave(host_set lock)
3996 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997void ata_bmdma_setup(struct ata_queued_cmd *qc)
3998{
3999 if (qc->ap->flags & ATA_FLAG_MMIO)
4000 ata_bmdma_setup_mmio(qc);
4001 else
4002 ata_bmdma_setup_pio(qc);
4003}
4004
Edward Falk0baab862005-06-02 18:17:13 -04004005
4006/**
4007 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004008 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004009 *
4010 * Clear interrupt and error flags in DMA status register.
4011 *
4012 * May be used as the irq_clear() entry in ata_port_operations.
4013 *
4014 * LOCKING:
4015 * spin_lock_irqsave(host_set lock)
4016 */
4017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018void ata_bmdma_irq_clear(struct ata_port *ap)
4019{
4020 if (ap->flags & ATA_FLAG_MMIO) {
4021 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4022 writeb(readb(mmio), mmio);
4023 } else {
4024 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4025 outb(inb(addr), addr);
4026 }
4027
4028}
4029
Edward Falk0baab862005-06-02 18:17:13 -04004030
4031/**
4032 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004033 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004034 *
4035 * Read and return BMDMA status register.
4036 *
4037 * May be used as the bmdma_status() entry in ata_port_operations.
4038 *
4039 * LOCKING:
4040 * spin_lock_irqsave(host_set lock)
4041 */
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043u8 ata_bmdma_status(struct ata_port *ap)
4044{
4045 u8 host_stat;
4046 if (ap->flags & ATA_FLAG_MMIO) {
4047 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4048 host_stat = readb(mmio + ATA_DMA_STATUS);
4049 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004050 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 return host_stat;
4052}
4053
Edward Falk0baab862005-06-02 18:17:13 -04004054
4055/**
4056 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004057 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004058 *
4059 * Clears the ATA_DMA_START flag in the dma control register
4060 *
4061 * May be used as the bmdma_stop() entry in ata_port_operations.
4062 *
4063 * LOCKING:
4064 * spin_lock_irqsave(host_set lock)
4065 */
4066
Alan Coxb73fc892005-08-26 16:03:19 +01004067void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
Alan Coxb73fc892005-08-26 16:03:19 +01004069 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 if (ap->flags & ATA_FLAG_MMIO) {
4071 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4072
4073 /* clear start/stop bit */
4074 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4075 mmio + ATA_DMA_CMD);
4076 } else {
4077 /* clear start/stop bit */
4078 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4079 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4080 }
4081
4082 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4083 ata_altstatus(ap); /* dummy read */
4084}
4085
4086/**
4087 * ata_host_intr - Handle host interrupt for given (port, task)
4088 * @ap: Port on which interrupt arrived (possibly...)
4089 * @qc: Taskfile currently active in engine
4090 *
4091 * Handle host interrupt for given queued command. Currently,
4092 * only DMA interrupts are handled. All other commands are
4093 * handled via polling with interrupts disabled (nIEN bit).
4094 *
4095 * LOCKING:
4096 * spin_lock_irqsave(host_set lock)
4097 *
4098 * RETURNS:
4099 * One if interrupt was handled, zero if not (shared irq).
4100 */
4101
4102inline unsigned int ata_host_intr (struct ata_port *ap,
4103 struct ata_queued_cmd *qc)
4104{
Albert Lee312f7da2005-09-27 17:38:03 +08004105 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Albert Lee312f7da2005-09-27 17:38:03 +08004107 VPRINTK("ata%u: protocol %d task_state %d\n",
4108 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Albert Lee312f7da2005-09-27 17:38:03 +08004110 /* Check whether we are expecting interrupt in this state */
4111 switch (ap->hsm_task_state) {
4112 case HSM_ST_FIRST:
4113 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4114 * The flag was turned on only for atapi devices.
4115 * No need to check is_atapi_taskfile(&qc->tf) again.
4116 */
4117 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08004119 break;
4120 case HSM_ST_LAST:
4121 if (qc->tf.protocol == ATA_PROT_DMA ||
4122 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4123 /* check status of DMA engine */
4124 host_stat = ap->ops->bmdma_status(ap);
4125 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Albert Lee312f7da2005-09-27 17:38:03 +08004127 /* if it's not our irq... */
4128 if (!(host_stat & ATA_DMA_INTR))
4129 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Albert Lee312f7da2005-09-27 17:38:03 +08004131 /* before we do anything else, clear DMA-Start bit */
4132 ap->ops->bmdma_stop(qc);
4133 }
4134 break;
4135 case HSM_ST:
4136 break;
4137 default:
4138 goto idle_irq;
4139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Albert Lee312f7da2005-09-27 17:38:03 +08004141 /* check altstatus */
4142 status = ata_altstatus(ap);
4143 if (status & ATA_BUSY)
4144 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Albert Lee312f7da2005-09-27 17:38:03 +08004146 /* check main status, clearing INTRQ */
4147 status = ata_chk_status(ap);
4148 if (unlikely(status & ATA_BUSY))
4149 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Albert Lee312f7da2005-09-27 17:38:03 +08004151 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4152 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4153
4154 /* ack bmdma irq events */
4155 ap->ops->irq_clear(ap);
4156
4157 /* check error */
4158 if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
4159 ap->hsm_task_state = HSM_ST_ERR;
4160
4161fsm_start:
4162 switch (ap->hsm_task_state) {
4163 case HSM_ST_FIRST:
4164 /* Some pre-ATAPI-4 devices assert INTRQ
4165 * at this state when ready to receive CDB.
4166 */
4167
4168 /* check device status */
4169 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4170 /* Wrong status. Let EH handle this */
4171 ap->hsm_task_state = HSM_ST_ERR;
4172 goto fsm_start;
4173 }
4174
4175 atapi_send_cdb(ap, qc);
4176
4177 break;
4178
4179 case HSM_ST:
4180 /* complete command or read/write the data register */
4181 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4182 /* ATAPI PIO protocol */
4183 if ((status & ATA_DRQ) == 0) {
4184 /* no more data to transfer */
4185 ap->hsm_task_state = HSM_ST_LAST;
4186 goto fsm_start;
4187 }
4188
4189 atapi_pio_bytes(qc);
4190
4191 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4192 /* bad ireason reported by device */
4193 goto fsm_start;
4194
4195 } else {
4196 /* ATA PIO protocol */
4197 if (unlikely((status & ATA_DRQ) == 0)) {
4198 /* handle BSY=0, DRQ=0 as error */
4199 ap->hsm_task_state = HSM_ST_ERR;
4200 goto fsm_start;
4201 }
4202
4203 ata_pio_sector(qc);
4204
4205 if (ap->hsm_task_state == HSM_ST_LAST &&
4206 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4207 /* all data read */
4208 ata_altstatus(ap);
4209 status = ata_chk_status(ap);
4210 goto fsm_start;
4211 }
4212 }
4213
4214 ata_altstatus(ap); /* flush */
4215 break;
4216
4217 case HSM_ST_LAST:
4218 if (unlikely(status & ATA_DRQ)) {
4219 /* handle DRQ=1 as error */
4220 ap->hsm_task_state = HSM_ST_ERR;
4221 goto fsm_start;
4222 }
4223
4224 /* no more data to transfer */
4225 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4226 ap->id, status);
4227
4228 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
4230 /* complete taskfile transaction */
Jeff Garzika7dac442005-10-30 04:44:42 -05004231 ata_qc_complete(qc, ac_err_mask(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 break;
4233
Albert Lee312f7da2005-09-27 17:38:03 +08004234 case HSM_ST_ERR:
4235 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4236 ap->id, status, host_stat);
4237
4238 ap->hsm_task_state = HSM_ST_IDLE;
4239 ata_qc_complete(qc, status | ATA_ERR);
4240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 default:
4242 goto idle_irq;
4243 }
4244
4245 return 1; /* irq handled */
4246
4247idle_irq:
4248 ap->stats.idle_irq++;
4249
4250#ifdef ATA_IRQ_TRAP
4251 if ((ap->stats.idle_irq % 1000) == 0) {
4252 handled = 1;
4253 ata_irq_ack(ap, 0); /* debug trap */
4254 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4255 }
4256#endif
4257 return 0; /* irq not handled */
4258}
4259
4260/**
4261 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004262 * @irq: irq line (unused)
4263 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 * @regs: unused
4265 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004266 * Default interrupt handler for PCI IDE devices. Calls
4267 * ata_host_intr() for each port that is not disabled.
4268 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004270 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 *
4272 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004273 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 */
4275
4276irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4277{
4278 struct ata_host_set *host_set = dev_instance;
4279 unsigned int i;
4280 unsigned int handled = 0;
4281 unsigned long flags;
4282
4283 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4284 spin_lock_irqsave(&host_set->lock, flags);
4285
4286 for (i = 0; i < host_set->n_ports; i++) {
4287 struct ata_port *ap;
4288
4289 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004290 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08004291 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 struct ata_queued_cmd *qc;
4293
4294 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004295 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004296 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 handled |= ata_host_intr(ap, qc);
4298 }
4299 }
4300
4301 spin_unlock_irqrestore(&host_set->lock, flags);
4302
4303 return IRQ_RETVAL(handled);
4304}
4305
4306/**
Edward Falk0baab862005-06-02 18:17:13 -04004307 * ata_port_start - Set port up for dma.
4308 * @ap: Port to initialize
4309 *
4310 * Called just after data structures for each port are
4311 * initialized. Allocates space for PRD table.
4312 *
4313 * May be used as the port_start() entry in ata_port_operations.
4314 *
4315 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004316 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004317 */
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319int ata_port_start (struct ata_port *ap)
4320{
4321 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004322 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
4324 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4325 if (!ap->prd)
4326 return -ENOMEM;
4327
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004328 rc = ata_pad_alloc(ap, dev);
4329 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004330 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004331 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004332 }
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4335
4336 return 0;
4337}
4338
Edward Falk0baab862005-06-02 18:17:13 -04004339
4340/**
4341 * ata_port_stop - Undo ata_port_start()
4342 * @ap: Port to shut down
4343 *
4344 * Frees the PRD table.
4345 *
4346 * May be used as the port_stop() entry in ata_port_operations.
4347 *
4348 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004349 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004350 */
4351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352void ata_port_stop (struct ata_port *ap)
4353{
4354 struct device *dev = ap->host_set->dev;
4355
4356 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004357 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004360void ata_host_stop (struct ata_host_set *host_set)
4361{
4362 if (host_set->mmio_base)
4363 iounmap(host_set->mmio_base);
4364}
4365
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367/**
4368 * ata_host_remove - Unregister SCSI host structure with upper layers
4369 * @ap: Port to unregister
4370 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4371 *
4372 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004373 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 */
4375
4376static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4377{
4378 struct Scsi_Host *sh = ap->host;
4379
4380 DPRINTK("ENTER\n");
4381
4382 if (do_unregister)
4383 scsi_remove_host(sh);
4384
4385 ap->ops->port_stop(ap);
4386}
4387
4388/**
4389 * ata_host_init - Initialize an ata_port structure
4390 * @ap: Structure to initialize
4391 * @host: associated SCSI mid-layer structure
4392 * @host_set: Collection of hosts to which @ap belongs
4393 * @ent: Probe information provided by low-level driver
4394 * @port_no: Port number associated with this ata_port
4395 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004396 * Initialize a new ata_port structure, and its associated
4397 * scsi_host.
4398 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004400 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 */
4402
4403static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4404 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004405 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
4407 unsigned int i;
4408
4409 host->max_id = 16;
4410 host->max_lun = 1;
4411 host->max_channel = 1;
4412 host->unique_id = ata_unique_id++;
4413 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004414
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 ap->flags = ATA_FLAG_PORT_DISABLED;
4416 ap->id = host->unique_id;
4417 ap->host = host;
4418 ap->ctl = ATA_DEVCTL_OBS;
4419 ap->host_set = host_set;
4420 ap->port_no = port_no;
4421 ap->hard_port_no =
4422 ent->legacy_mode ? ent->hard_port_no : port_no;
4423 ap->pio_mask = ent->pio_mask;
4424 ap->mwdma_mask = ent->mwdma_mask;
4425 ap->udma_mask = ent->udma_mask;
4426 ap->flags |= ent->host_flags;
4427 ap->ops = ent->port_ops;
4428 ap->cbl = ATA_CBL_NONE;
4429 ap->active_tag = ATA_TAG_POISON;
4430 ap->last_ctl = 0xFF;
4431
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4433
4434 for (i = 0; i < ATA_MAX_DEVICES; i++)
4435 ap->device[i].devno = i;
4436
4437#ifdef ATA_IRQ_TRAP
4438 ap->stats.unhandled_irq = 1;
4439 ap->stats.idle_irq = 1;
4440#endif
4441
4442 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4443}
4444
4445/**
4446 * ata_host_add - Attach low-level ATA driver to system
4447 * @ent: Information provided by low-level driver
4448 * @host_set: Collections of ports to which we add
4449 * @port_no: Port number associated with this host
4450 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004451 * Attach low-level ATA driver to system.
4452 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004454 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 *
4456 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004457 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 */
4459
Jeff Garzik057ace52005-10-22 14:27:05 -04004460static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 struct ata_host_set *host_set,
4462 unsigned int port_no)
4463{
4464 struct Scsi_Host *host;
4465 struct ata_port *ap;
4466 int rc;
4467
4468 DPRINTK("ENTER\n");
4469 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4470 if (!host)
4471 return NULL;
4472
4473 ap = (struct ata_port *) &host->hostdata[0];
4474
4475 ata_host_init(ap, host, host_set, ent, port_no);
4476
4477 rc = ap->ops->port_start(ap);
4478 if (rc)
4479 goto err_out;
4480
4481 return ap;
4482
4483err_out:
4484 scsi_host_put(host);
4485 return NULL;
4486}
4487
4488/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004489 * ata_device_add - Register hardware device with ATA and SCSI layers
4490 * @ent: Probe information describing hardware device to be registered
4491 *
4492 * This function processes the information provided in the probe
4493 * information struct @ent, allocates the necessary ATA and SCSI
4494 * host information structures, initializes them, and registers
4495 * everything with requisite kernel subsystems.
4496 *
4497 * This function requests irqs, probes the ATA bus, and probes
4498 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 *
4500 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004501 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 *
4503 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004504 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 */
4506
Jeff Garzik057ace52005-10-22 14:27:05 -04004507int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508{
4509 unsigned int count = 0, i;
4510 struct device *dev = ent->dev;
4511 struct ata_host_set *host_set;
4512
4513 DPRINTK("ENTER\n");
4514 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004515 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4517 if (!host_set)
4518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 spin_lock_init(&host_set->lock);
4520
4521 host_set->dev = dev;
4522 host_set->n_ports = ent->n_ports;
4523 host_set->irq = ent->irq;
4524 host_set->mmio_base = ent->mmio_base;
4525 host_set->private_data = ent->private_data;
4526 host_set->ops = ent->port_ops;
4527
4528 /* register each port bound to this device */
4529 for (i = 0; i < ent->n_ports; i++) {
4530 struct ata_port *ap;
4531 unsigned long xfer_mode_mask;
4532
4533 ap = ata_host_add(ent, host_set, i);
4534 if (!ap)
4535 goto err_out;
4536
4537 host_set->ports[i] = ap;
4538 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4539 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4540 (ap->pio_mask << ATA_SHIFT_PIO);
4541
4542 /* print per-port info to dmesg */
4543 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4544 "bmdma 0x%lX irq %lu\n",
4545 ap->id,
4546 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4547 ata_mode_string(xfer_mode_mask),
4548 ap->ioaddr.cmd_addr,
4549 ap->ioaddr.ctl_addr,
4550 ap->ioaddr.bmdma_addr,
4551 ent->irq);
4552
4553 ata_chk_status(ap);
4554 host_set->ops->irq_clear(ap);
4555 count++;
4556 }
4557
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004558 if (!count)
4559 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
4561 /* obtain irq, that is shared between channels */
4562 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4563 DRV_NAME, host_set))
4564 goto err_out;
4565
4566 /* perform each probe synchronously */
4567 DPRINTK("probe begin\n");
4568 for (i = 0; i < count; i++) {
4569 struct ata_port *ap;
4570 int rc;
4571
4572 ap = host_set->ports[i];
4573
4574 DPRINTK("ata%u: probe begin\n", ap->id);
4575 rc = ata_bus_probe(ap);
4576 DPRINTK("ata%u: probe end\n", ap->id);
4577
4578 if (rc) {
4579 /* FIXME: do something useful here?
4580 * Current libata behavior will
4581 * tear down everything when
4582 * the module is removed
4583 * or the h/w is unplugged.
4584 */
4585 }
4586
4587 rc = scsi_add_host(ap->host, dev);
4588 if (rc) {
4589 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4590 ap->id);
4591 /* FIXME: do something useful here */
4592 /* FIXME: handle unconditional calls to
4593 * scsi_scan_host and ata_host_remove, below,
4594 * at the very least
4595 */
4596 }
4597 }
4598
4599 /* probes are done, now scan each port's disk(s) */
4600 DPRINTK("probe begin\n");
4601 for (i = 0; i < count; i++) {
4602 struct ata_port *ap = host_set->ports[i];
4603
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004604 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 }
4606
4607 dev_set_drvdata(dev, host_set);
4608
4609 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4610 return ent->n_ports; /* success */
4611
4612err_out:
4613 for (i = 0; i < count; i++) {
4614 ata_host_remove(host_set->ports[i], 1);
4615 scsi_host_put(host_set->ports[i]->host);
4616 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004617err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 kfree(host_set);
4619 VPRINTK("EXIT, returning 0\n");
4620 return 0;
4621}
4622
4623/**
Alan Cox17b14452005-09-15 15:44:00 +01004624 * ata_host_set_remove - PCI layer callback for device removal
4625 * @host_set: ATA host set that was removed
4626 *
4627 * Unregister all objects associated with this host set. Free those
4628 * objects.
4629 *
4630 * LOCKING:
4631 * Inherited from calling layer (may sleep).
4632 */
4633
Alan Cox17b14452005-09-15 15:44:00 +01004634void ata_host_set_remove(struct ata_host_set *host_set)
4635{
4636 struct ata_port *ap;
4637 unsigned int i;
4638
4639 for (i = 0; i < host_set->n_ports; i++) {
4640 ap = host_set->ports[i];
4641 scsi_remove_host(ap->host);
4642 }
4643
4644 free_irq(host_set->irq, host_set);
4645
4646 for (i = 0; i < host_set->n_ports; i++) {
4647 ap = host_set->ports[i];
4648
4649 ata_scsi_release(ap->host);
4650
4651 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4652 struct ata_ioports *ioaddr = &ap->ioaddr;
4653
4654 if (ioaddr->cmd_addr == 0x1f0)
4655 release_region(0x1f0, 8);
4656 else if (ioaddr->cmd_addr == 0x170)
4657 release_region(0x170, 8);
4658 }
4659
4660 scsi_host_put(ap->host);
4661 }
4662
4663 if (host_set->ops->host_stop)
4664 host_set->ops->host_stop(host_set);
4665
4666 kfree(host_set);
4667}
4668
4669/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 * ata_scsi_release - SCSI layer callback hook for host unload
4671 * @host: libata host to be unloaded
4672 *
4673 * Performs all duties necessary to shut down a libata port...
4674 * Kill port kthread, disable port, and release resources.
4675 *
4676 * LOCKING:
4677 * Inherited from SCSI layer.
4678 *
4679 * RETURNS:
4680 * One.
4681 */
4682
4683int ata_scsi_release(struct Scsi_Host *host)
4684{
4685 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4686
4687 DPRINTK("ENTER\n");
4688
4689 ap->ops->port_disable(ap);
4690 ata_host_remove(ap, 0);
4691
4692 DPRINTK("EXIT\n");
4693 return 1;
4694}
4695
4696/**
4697 * ata_std_ports - initialize ioaddr with standard port offsets.
4698 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004699 *
4700 * Utility function which initializes data_addr, error_addr,
4701 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4702 * device_addr, status_addr, and command_addr to standard offsets
4703 * relative to cmd_addr.
4704 *
4705 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 */
Edward Falk0baab862005-06-02 18:17:13 -04004707
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708void ata_std_ports(struct ata_ioports *ioaddr)
4709{
4710 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4711 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4712 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4713 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4714 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4715 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4716 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4717 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4718 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4719 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4720}
4721
4722static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004723ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724{
4725 struct ata_probe_ent *probe_ent;
4726
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004727 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 if (!probe_ent) {
4729 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4730 kobject_name(&(dev->kobj)));
4731 return NULL;
4732 }
4733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 INIT_LIST_HEAD(&probe_ent->node);
4735 probe_ent->dev = dev;
4736
4737 probe_ent->sht = port->sht;
4738 probe_ent->host_flags = port->host_flags;
4739 probe_ent->pio_mask = port->pio_mask;
4740 probe_ent->mwdma_mask = port->mwdma_mask;
4741 probe_ent->udma_mask = port->udma_mask;
4742 probe_ent->port_ops = port->port_ops;
4743
4744 return probe_ent;
4745}
4746
Edward Falk0baab862005-06-02 18:17:13 -04004747
4748
Jeff Garzik374b1872005-08-30 05:42:52 -04004749#ifdef CONFIG_PCI
4750
4751void ata_pci_host_stop (struct ata_host_set *host_set)
4752{
4753 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4754
4755 pci_iounmap(pdev, host_set->mmio_base);
4756}
4757
Edward Falk0baab862005-06-02 18:17:13 -04004758/**
4759 * ata_pci_init_native_mode - Initialize native-mode driver
4760 * @pdev: pci device to be initialized
4761 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004762 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004763 *
4764 * Utility function which allocates and initializes an
4765 * ata_probe_ent structure for a standard dual-port
4766 * PIO-based IDE controller. The returned ata_probe_ent
4767 * structure can be passed to ata_device_add(). The returned
4768 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004769 *
4770 * The caller need only pass the address of the primary port, the
4771 * secondary will be deduced automatically. If the device has non
4772 * standard secondary port mappings this function can be called twice,
4773 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004774 */
4775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004777ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778{
4779 struct ata_probe_ent *probe_ent =
4780 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004781 int p = 0;
4782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 if (!probe_ent)
4784 return NULL;
4785
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 probe_ent->irq = pdev->irq;
4787 probe_ent->irq_flags = SA_SHIRQ;
4788
Alan Cox47a86592005-10-04 08:09:19 -04004789 if (ports & ATA_PORT_PRIMARY) {
4790 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4791 probe_ent->port[p].altstatus_addr =
4792 probe_ent->port[p].ctl_addr =
4793 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4794 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4795 ata_std_ports(&probe_ent->port[p]);
4796 p++;
4797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Alan Cox47a86592005-10-04 08:09:19 -04004799 if (ports & ATA_PORT_SECONDARY) {
4800 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4801 probe_ent->port[p].altstatus_addr =
4802 probe_ent->port[p].ctl_addr =
4803 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4804 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4805 ata_std_ports(&probe_ent->port[p]);
4806 p++;
4807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Alan Cox47a86592005-10-04 08:09:19 -04004809 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 return probe_ent;
4811}
4812
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004813static 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 -07004814{
Alan Cox47a86592005-10-04 08:09:19 -04004815 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004817 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 if (!probe_ent)
4819 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004822 probe_ent->n_ports = 1;
4823 probe_ent->hard_port_no = port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824
Alan Cox47a86592005-10-04 08:09:19 -04004825 switch(port_num)
4826 {
4827 case 0:
4828 probe_ent->irq = 14;
4829 probe_ent->port[0].cmd_addr = 0x1f0;
4830 probe_ent->port[0].altstatus_addr =
4831 probe_ent->port[0].ctl_addr = 0x3f6;
4832 break;
4833 case 1:
4834 probe_ent->irq = 15;
4835 probe_ent->port[0].cmd_addr = 0x170;
4836 probe_ent->port[0].altstatus_addr =
4837 probe_ent->port[0].ctl_addr = 0x376;
4838 break;
4839 }
4840 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 return probe_ent;
4843}
4844
4845/**
4846 * ata_pci_init_one - Initialize/register PCI IDE host controller
4847 * @pdev: Controller to be initialized
4848 * @port_info: Information from low-level host driver
4849 * @n_ports: Number of ports attached to host controller
4850 *
Edward Falk0baab862005-06-02 18:17:13 -04004851 * This is a helper function which can be called from a driver's
4852 * xxx_init_one() probe function if the hardware uses traditional
4853 * IDE taskfile registers.
4854 *
4855 * This function calls pci_enable_device(), reserves its register
4856 * regions, sets the dma mask, enables bus master mode, and calls
4857 * ata_device_add()
4858 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 * LOCKING:
4860 * Inherited from PCI layer (may sleep).
4861 *
4862 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004863 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 */
4865
4866int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4867 unsigned int n_ports)
4868{
Alan Cox47a86592005-10-04 08:09:19 -04004869 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 struct ata_port_info *port[2];
4871 u8 tmp8, mask;
4872 unsigned int legacy_mode = 0;
4873 int disable_dev_on_err = 1;
4874 int rc;
4875
4876 DPRINTK("ENTER\n");
4877
4878 port[0] = port_info[0];
4879 if (n_ports > 1)
4880 port[1] = port_info[1];
4881 else
4882 port[1] = port[0];
4883
4884 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4885 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004886 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4888 mask = (1 << 2) | (1 << 0);
4889 if ((tmp8 & mask) != mask)
4890 legacy_mode = (1 << 3);
4891 }
4892
4893 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004894 if ((!legacy_mode) && (n_ports > 2)) {
4895 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4896 n_ports = 2;
4897 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 }
4899
Alan Cox47a86592005-10-04 08:09:19 -04004900 /* FIXME: Really for ATA it isn't safe because the device may be
4901 multi-purpose and we want to leave it alone if it was already
4902 enabled. Secondly for shared use as Arjan says we want refcounting
4903
4904 Checking dev->is_enabled is insufficient as this is not set at
4905 boot for the primary video which is BIOS enabled
4906 */
4907
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 rc = pci_enable_device(pdev);
4909 if (rc)
4910 return rc;
4911
4912 rc = pci_request_regions(pdev, DRV_NAME);
4913 if (rc) {
4914 disable_dev_on_err = 0;
4915 goto err_out;
4916 }
4917
Alan Cox47a86592005-10-04 08:09:19 -04004918 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 if (legacy_mode) {
4920 if (!request_region(0x1f0, 8, "libata")) {
4921 struct resource *conflict, res;
4922 res.start = 0x1f0;
4923 res.end = 0x1f0 + 8 - 1;
4924 conflict = ____request_resource(&ioport_resource, &res);
4925 if (!strcmp(conflict->name, "libata"))
4926 legacy_mode |= (1 << 0);
4927 else {
4928 disable_dev_on_err = 0;
4929 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4930 }
4931 } else
4932 legacy_mode |= (1 << 0);
4933
4934 if (!request_region(0x170, 8, "libata")) {
4935 struct resource *conflict, res;
4936 res.start = 0x170;
4937 res.end = 0x170 + 8 - 1;
4938 conflict = ____request_resource(&ioport_resource, &res);
4939 if (!strcmp(conflict->name, "libata"))
4940 legacy_mode |= (1 << 1);
4941 else {
4942 disable_dev_on_err = 0;
4943 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4944 }
4945 } else
4946 legacy_mode |= (1 << 1);
4947 }
4948
4949 /* we have legacy mode, but all ports are unavailable */
4950 if (legacy_mode == (1 << 3)) {
4951 rc = -EBUSY;
4952 goto err_out_regions;
4953 }
4954
4955 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4956 if (rc)
4957 goto err_out_regions;
4958 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4959 if (rc)
4960 goto err_out_regions;
4961
4962 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004963 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004964 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004965 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004966 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004967 } else {
4968 if (n_ports == 2)
4969 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4970 else
4971 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4972 }
4973 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 rc = -ENOMEM;
4975 goto err_out_regions;
4976 }
4977
4978 pci_set_master(pdev);
4979
4980 /* FIXME: check ata_device_add return */
4981 if (legacy_mode) {
4982 if (legacy_mode & (1 << 0))
4983 ata_device_add(probe_ent);
4984 if (legacy_mode & (1 << 1))
4985 ata_device_add(probe_ent2);
4986 } else
4987 ata_device_add(probe_ent);
4988
4989 kfree(probe_ent);
4990 kfree(probe_ent2);
4991
4992 return 0;
4993
4994err_out_regions:
4995 if (legacy_mode & (1 << 0))
4996 release_region(0x1f0, 8);
4997 if (legacy_mode & (1 << 1))
4998 release_region(0x170, 8);
4999 pci_release_regions(pdev);
5000err_out:
5001 if (disable_dev_on_err)
5002 pci_disable_device(pdev);
5003 return rc;
5004}
5005
5006/**
5007 * ata_pci_remove_one - PCI layer callback for device removal
5008 * @pdev: PCI device that was removed
5009 *
5010 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005011 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 * Handle this by unregistering all objects associated
5013 * with this PCI device. Free those objects. Then finally
5014 * release PCI resources and disable device.
5015 *
5016 * LOCKING:
5017 * Inherited from PCI layer (may sleep).
5018 */
5019
5020void ata_pci_remove_one (struct pci_dev *pdev)
5021{
5022 struct device *dev = pci_dev_to_dev(pdev);
5023 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Alan Cox17b14452005-09-15 15:44:00 +01005025 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 pci_release_regions(pdev);
5027 pci_disable_device(pdev);
5028 dev_set_drvdata(dev, NULL);
5029}
5030
5031/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005032int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033{
5034 unsigned long tmp = 0;
5035
5036 switch (bits->width) {
5037 case 1: {
5038 u8 tmp8 = 0;
5039 pci_read_config_byte(pdev, bits->reg, &tmp8);
5040 tmp = tmp8;
5041 break;
5042 }
5043 case 2: {
5044 u16 tmp16 = 0;
5045 pci_read_config_word(pdev, bits->reg, &tmp16);
5046 tmp = tmp16;
5047 break;
5048 }
5049 case 4: {
5050 u32 tmp32 = 0;
5051 pci_read_config_dword(pdev, bits->reg, &tmp32);
5052 tmp = tmp32;
5053 break;
5054 }
5055
5056 default:
5057 return -EINVAL;
5058 }
5059
5060 tmp &= bits->mask;
5061
5062 return (tmp == bits->val) ? 1 : 0;
5063}
5064#endif /* CONFIG_PCI */
5065
5066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067static int __init ata_init(void)
5068{
5069 ata_wq = create_workqueue("ata");
5070 if (!ata_wq)
5071 return -ENOMEM;
5072
5073 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5074 return 0;
5075}
5076
5077static void __exit ata_exit(void)
5078{
5079 destroy_workqueue(ata_wq);
5080}
5081
5082module_init(ata_init);
5083module_exit(ata_exit);
5084
Jeff Garzik67846b32005-10-05 02:58:32 -04005085static unsigned long ratelimit_time;
5086static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5087
5088int ata_ratelimit(void)
5089{
5090 int rc;
5091 unsigned long flags;
5092
5093 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5094
5095 if (time_after(jiffies, ratelimit_time)) {
5096 rc = 1;
5097 ratelimit_time = jiffies + (HZ/5);
5098 } else
5099 rc = 0;
5100
5101 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5102
5103 return rc;
5104}
5105
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106/*
5107 * libata is essentially a library of internal helper functions for
5108 * low-level ATA host controller drivers. As such, the API/ABI is
5109 * likely to change as new drivers are added and updated.
5110 * Do not depend on ABI/API stability.
5111 */
5112
5113EXPORT_SYMBOL_GPL(ata_std_bios_param);
5114EXPORT_SYMBOL_GPL(ata_std_ports);
5115EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005116EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117EXPORT_SYMBOL_GPL(ata_sg_init);
5118EXPORT_SYMBOL_GPL(ata_sg_init_one);
5119EXPORT_SYMBOL_GPL(ata_qc_complete);
5120EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5121EXPORT_SYMBOL_GPL(ata_eng_timeout);
5122EXPORT_SYMBOL_GPL(ata_tf_load);
5123EXPORT_SYMBOL_GPL(ata_tf_read);
5124EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5125EXPORT_SYMBOL_GPL(ata_std_dev_select);
5126EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5127EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5128EXPORT_SYMBOL_GPL(ata_check_status);
5129EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130EXPORT_SYMBOL_GPL(ata_exec_command);
5131EXPORT_SYMBOL_GPL(ata_port_start);
5132EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005133EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134EXPORT_SYMBOL_GPL(ata_interrupt);
5135EXPORT_SYMBOL_GPL(ata_qc_prep);
5136EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5137EXPORT_SYMBOL_GPL(ata_bmdma_start);
5138EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5139EXPORT_SYMBOL_GPL(ata_bmdma_status);
5140EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5141EXPORT_SYMBOL_GPL(ata_port_probe);
5142EXPORT_SYMBOL_GPL(sata_phy_reset);
5143EXPORT_SYMBOL_GPL(__sata_phy_reset);
5144EXPORT_SYMBOL_GPL(ata_bus_reset);
5145EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005146EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5148EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5149EXPORT_SYMBOL_GPL(ata_scsi_error);
5150EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5151EXPORT_SYMBOL_GPL(ata_scsi_release);
5152EXPORT_SYMBOL_GPL(ata_host_intr);
5153EXPORT_SYMBOL_GPL(ata_dev_classify);
5154EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005155EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5157
Alan Cox452503f2005-10-21 19:01:32 -04005158EXPORT_SYMBOL_GPL(ata_timing_compute);
5159EXPORT_SYMBOL_GPL(ata_timing_merge);
5160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161#ifdef CONFIG_PCI
5162EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005163EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5165EXPORT_SYMBOL_GPL(ata_pci_init_one);
5166EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5167#endif /* CONFIG_PCI */