blob: 8be7dc0b47b849858c8d5b37872e7112fb8e3b31 [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>
54#include "scsi.h"
55#include "scsi_priv.h"
56#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
64static unsigned int ata_busy_sleep (struct ata_port *ap,
65 unsigned long tmout_pat,
66 unsigned long tmout);
Albert Lee59a10b12005-10-12 15:09:42 +080067static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ec2005-05-12 15:29:42 -040068static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void ata_set_mode(struct ata_port *ap);
70static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040071static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040073static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 u8 *xfer_mode_out,
75 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void __ata_qc_complete(struct ata_queued_cmd *qc);
77
78static unsigned int ata_unique_id = 1;
79static struct workqueue_struct *ata_wq;
80
Jeff Garzik1623c812005-08-30 03:37:42 -040081int atapi_enabled = 0;
82module_param(atapi_enabled, int, 0444);
83MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_AUTHOR("Jeff Garzik");
86MODULE_DESCRIPTION("Library module for ATA devices");
87MODULE_LICENSE("GPL");
88MODULE_VERSION(DRV_VERSION);
89
90/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040091 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * @ap: Port to which output is sent
93 * @tf: ATA taskfile register set
94 *
95 * Outputs ATA taskfile to standard ATA host controller.
96 *
97 * LOCKING:
98 * Inherited from caller.
99 */
100
Jeff Garzik057ace52005-10-22 14:27:05 -0400101static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct ata_ioports *ioaddr = &ap->ioaddr;
104 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
105
106 if (tf->ctl != ap->last_ctl) {
107 outb(tf->ctl, ioaddr->ctl_addr);
108 ap->last_ctl = tf->ctl;
109 ata_wait_idle(ap);
110 }
111
112 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
113 outb(tf->hob_feature, ioaddr->feature_addr);
114 outb(tf->hob_nsect, ioaddr->nsect_addr);
115 outb(tf->hob_lbal, ioaddr->lbal_addr);
116 outb(tf->hob_lbam, ioaddr->lbam_addr);
117 outb(tf->hob_lbah, ioaddr->lbah_addr);
118 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
119 tf->hob_feature,
120 tf->hob_nsect,
121 tf->hob_lbal,
122 tf->hob_lbam,
123 tf->hob_lbah);
124 }
125
126 if (is_addr) {
127 outb(tf->feature, ioaddr->feature_addr);
128 outb(tf->nsect, ioaddr->nsect_addr);
129 outb(tf->lbal, ioaddr->lbal_addr);
130 outb(tf->lbam, ioaddr->lbam_addr);
131 outb(tf->lbah, ioaddr->lbah_addr);
132 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
133 tf->feature,
134 tf->nsect,
135 tf->lbal,
136 tf->lbam,
137 tf->lbah);
138 }
139
140 if (tf->flags & ATA_TFLAG_DEVICE) {
141 outb(tf->device, ioaddr->device_addr);
142 VPRINTK("device 0x%X\n", tf->device);
143 }
144
145 ata_wait_idle(ap);
146}
147
148/**
149 * ata_tf_load_mmio - send taskfile registers to host controller
150 * @ap: Port to which output is sent
151 * @tf: ATA taskfile register set
152 *
153 * Outputs ATA taskfile to standard ATA host controller using MMIO.
154 *
155 * LOCKING:
156 * Inherited from caller.
157 */
158
Jeff Garzik057ace52005-10-22 14:27:05 -0400159static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct ata_ioports *ioaddr = &ap->ioaddr;
162 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
163
164 if (tf->ctl != ap->last_ctl) {
165 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
166 ap->last_ctl = tf->ctl;
167 ata_wait_idle(ap);
168 }
169
170 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
171 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
172 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
173 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
174 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
175 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
176 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
177 tf->hob_feature,
178 tf->hob_nsect,
179 tf->hob_lbal,
180 tf->hob_lbam,
181 tf->hob_lbah);
182 }
183
184 if (is_addr) {
185 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
186 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
187 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
188 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
189 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
190 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
191 tf->feature,
192 tf->nsect,
193 tf->lbal,
194 tf->lbam,
195 tf->lbah);
196 }
197
198 if (tf->flags & ATA_TFLAG_DEVICE) {
199 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
200 VPRINTK("device 0x%X\n", tf->device);
201 }
202
203 ata_wait_idle(ap);
204}
205
Edward Falk0baab862005-06-02 18:17:13 -0400206
207/**
208 * ata_tf_load - send taskfile registers to host controller
209 * @ap: Port to which output is sent
210 * @tf: ATA taskfile register set
211 *
212 * Outputs ATA taskfile to standard ATA host controller using MMIO
213 * or PIO as indicated by the ATA_FLAG_MMIO flag.
214 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
215 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
216 * hob_lbal, hob_lbam, and hob_lbah.
217 *
218 * This function waits for idle (!BUSY and !DRQ) after writing
219 * registers. If the control register has a new value, this
220 * function also waits for idle after writing control and before
221 * writing the remaining registers.
222 *
223 * May be used as the tf_load() entry in ata_port_operations.
224 *
225 * LOCKING:
226 * Inherited from caller.
227 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400228void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 if (ap->flags & ATA_FLAG_MMIO)
231 ata_tf_load_mmio(ap, tf);
232 else
233 ata_tf_load_pio(ap, tf);
234}
235
236/**
Edward Falk0baab862005-06-02 18:17:13 -0400237 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * @ap: port to which command is being issued
239 * @tf: ATA taskfile register set
240 *
Edward Falk0baab862005-06-02 18:17:13 -0400241 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * synchronization with interrupt handler / other threads.
243 *
244 * LOCKING:
245 * spin_lock_irqsave(host_set lock)
246 */
247
Jeff Garzik057ace52005-10-22 14:27:05 -0400248static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
251
252 outb(tf->command, ap->ioaddr.command_addr);
253 ata_pause(ap);
254}
255
256
257/**
258 * ata_exec_command_mmio - issue ATA command to host controller
259 * @ap: port to which command is being issued
260 * @tf: ATA taskfile register set
261 *
262 * Issues MMIO write to ATA command register, with proper
263 * synchronization with interrupt handler / other threads.
264 *
265 * LOCKING:
266 * spin_lock_irqsave(host_set lock)
267 */
268
Jeff Garzik057ace52005-10-22 14:27:05 -0400269static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
272
273 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
274 ata_pause(ap);
275}
276
Edward Falk0baab862005-06-02 18:17:13 -0400277
278/**
279 * ata_exec_command - issue ATA command to host controller
280 * @ap: port to which command is being issued
281 * @tf: ATA taskfile register set
282 *
283 * Issues PIO/MMIO write to ATA command register, with proper
284 * synchronization with interrupt handler / other threads.
285 *
286 * LOCKING:
287 * spin_lock_irqsave(host_set lock)
288 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400289void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 if (ap->flags & ATA_FLAG_MMIO)
292 ata_exec_command_mmio(ap, tf);
293 else
294 ata_exec_command_pio(ap, tf);
295}
296
297/**
298 * ata_exec - issue ATA command to host controller
299 * @ap: port to which command is being issued
300 * @tf: ATA taskfile register set
301 *
302 * Issues PIO/MMIO write to ATA command register, with proper
303 * synchronization with interrupt handler / other threads.
304 *
305 * LOCKING:
306 * Obtains host_set lock.
307 */
308
Jeff Garzik057ace52005-10-22 14:27:05 -0400309static inline void ata_exec(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 unsigned long flags;
312
313 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
314 spin_lock_irqsave(&ap->host_set->lock, flags);
315 ap->ops->exec_command(ap, tf);
316 spin_unlock_irqrestore(&ap->host_set->lock, flags);
317}
318
319/**
320 * ata_tf_to_host - issue ATA taskfile to host controller
321 * @ap: port to which command is being issued
322 * @tf: ATA taskfile register set
323 *
324 * Issues ATA taskfile register set to ATA host controller,
325 * with proper synchronization with interrupt handler and
326 * other threads.
327 *
328 * LOCKING:
329 * Obtains host_set lock.
330 */
331
Jeff Garzik057ace52005-10-22 14:27:05 -0400332static void ata_tf_to_host(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 ap->ops->tf_load(ap, tf);
335
336 ata_exec(ap, tf);
337}
338
339/**
340 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
341 * @ap: port to which command is being issued
342 * @tf: ATA taskfile register set
343 *
344 * Issues ATA taskfile register set to ATA host controller,
345 * with proper synchronization with interrupt handler and
346 * other threads.
347 *
348 * LOCKING:
349 * spin_lock_irqsave(host_set lock)
350 */
351
Jeff Garzik057ace52005-10-22 14:27:05 -0400352void ata_tf_to_host_nolock(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 ap->ops->tf_load(ap, tf);
355 ap->ops->exec_command(ap, tf);
356}
357
358/**
Edward Falk0baab862005-06-02 18:17:13 -0400359 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * @ap: Port from which input is read
361 * @tf: ATA taskfile register set for storing input
362 *
363 * Reads ATA taskfile registers for currently-selected device
364 * into @tf.
365 *
366 * LOCKING:
367 * Inherited from caller.
368 */
369
370static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
371{
372 struct ata_ioports *ioaddr = &ap->ioaddr;
373
Jeff Garzikac19bff2005-10-29 13:58:21 -0400374 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400375 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 tf->nsect = inb(ioaddr->nsect_addr);
377 tf->lbal = inb(ioaddr->lbal_addr);
378 tf->lbam = inb(ioaddr->lbam_addr);
379 tf->lbah = inb(ioaddr->lbah_addr);
380 tf->device = inb(ioaddr->device_addr);
381
382 if (tf->flags & ATA_TFLAG_LBA48) {
383 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
384 tf->hob_feature = inb(ioaddr->error_addr);
385 tf->hob_nsect = inb(ioaddr->nsect_addr);
386 tf->hob_lbal = inb(ioaddr->lbal_addr);
387 tf->hob_lbam = inb(ioaddr->lbam_addr);
388 tf->hob_lbah = inb(ioaddr->lbah_addr);
389 }
390}
391
392/**
393 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
394 * @ap: Port from which input is read
395 * @tf: ATA taskfile register set for storing input
396 *
397 * Reads ATA taskfile registers for currently-selected device
398 * into @tf via MMIO.
399 *
400 * LOCKING:
401 * Inherited from caller.
402 */
403
404static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
405{
406 struct ata_ioports *ioaddr = &ap->ioaddr;
407
Jeff Garzikac19bff2005-10-29 13:58:21 -0400408 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400409 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
411 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
412 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
413 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
414 tf->device = readb((void __iomem *)ioaddr->device_addr);
415
416 if (tf->flags & ATA_TFLAG_LBA48) {
417 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
418 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
419 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
420 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
421 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
422 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
423 }
424}
425
Edward Falk0baab862005-06-02 18:17:13 -0400426
427/**
428 * ata_tf_read - input device's ATA taskfile shadow registers
429 * @ap: Port from which input is read
430 * @tf: ATA taskfile register set for storing input
431 *
432 * Reads ATA taskfile registers for currently-selected device
433 * into @tf.
434 *
435 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
436 * is set, also reads the hob registers.
437 *
438 * May be used as the tf_read() entry in ata_port_operations.
439 *
440 * LOCKING:
441 * Inherited from caller.
442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
444{
445 if (ap->flags & ATA_FLAG_MMIO)
446 ata_tf_read_mmio(ap, tf);
447 else
448 ata_tf_read_pio(ap, tf);
449}
450
451/**
452 * ata_check_status_pio - Read device status reg & clear interrupt
453 * @ap: port where the device is
454 *
455 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400456 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * from this device
458 *
459 * LOCKING:
460 * Inherited from caller.
461 */
462static u8 ata_check_status_pio(struct ata_port *ap)
463{
464 return inb(ap->ioaddr.status_addr);
465}
466
467/**
468 * ata_check_status_mmio - Read device status reg & clear interrupt
469 * @ap: port where the device is
470 *
471 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400472 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * from this device
474 *
475 * LOCKING:
476 * Inherited from caller.
477 */
478static u8 ata_check_status_mmio(struct ata_port *ap)
479{
480 return readb((void __iomem *) ap->ioaddr.status_addr);
481}
482
Edward Falk0baab862005-06-02 18:17:13 -0400483
484/**
485 * ata_check_status - Read device status reg & clear interrupt
486 * @ap: port where the device is
487 *
488 * Reads ATA taskfile status register for currently-selected device
489 * and return its value. This also clears pending interrupts
490 * from this device
491 *
492 * May be used as the check_status() entry in ata_port_operations.
493 *
494 * LOCKING:
495 * Inherited from caller.
496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497u8 ata_check_status(struct ata_port *ap)
498{
499 if (ap->flags & ATA_FLAG_MMIO)
500 return ata_check_status_mmio(ap);
501 return ata_check_status_pio(ap);
502}
503
Edward Falk0baab862005-06-02 18:17:13 -0400504
505/**
506 * ata_altstatus - Read device alternate status reg
507 * @ap: port where the device is
508 *
509 * Reads ATA taskfile alternate status register for
510 * currently-selected device and return its value.
511 *
512 * Note: may NOT be used as the check_altstatus() entry in
513 * ata_port_operations.
514 *
515 * LOCKING:
516 * Inherited from caller.
517 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518u8 ata_altstatus(struct ata_port *ap)
519{
520 if (ap->ops->check_altstatus)
521 return ap->ops->check_altstatus(ap);
522
523 if (ap->flags & ATA_FLAG_MMIO)
524 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
525 return inb(ap->ioaddr.altstatus_addr);
526}
527
Edward Falk0baab862005-06-02 18:17:13 -0400528
529/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
531 * @tf: Taskfile to convert
532 * @fis: Buffer into which data will output
533 * @pmp: Port multiplier port
534 *
535 * Converts a standard ATA taskfile to a Serial ATA
536 * FIS structure (Register - Host to Device).
537 *
538 * LOCKING:
539 * Inherited from caller.
540 */
541
Jeff Garzik057ace52005-10-22 14:27:05 -0400542void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 fis[0] = 0x27; /* Register - Host to Device FIS */
545 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
546 bit 7 indicates Command FIS */
547 fis[2] = tf->command;
548 fis[3] = tf->feature;
549
550 fis[4] = tf->lbal;
551 fis[5] = tf->lbam;
552 fis[6] = tf->lbah;
553 fis[7] = tf->device;
554
555 fis[8] = tf->hob_lbal;
556 fis[9] = tf->hob_lbam;
557 fis[10] = tf->hob_lbah;
558 fis[11] = tf->hob_feature;
559
560 fis[12] = tf->nsect;
561 fis[13] = tf->hob_nsect;
562 fis[14] = 0;
563 fis[15] = tf->ctl;
564
565 fis[16] = 0;
566 fis[17] = 0;
567 fis[18] = 0;
568 fis[19] = 0;
569}
570
571/**
572 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
573 * @fis: Buffer from which data will be input
574 * @tf: Taskfile to output
575 *
576 * Converts a standard ATA taskfile to a Serial ATA
577 * FIS structure (Register - Host to Device).
578 *
579 * LOCKING:
580 * Inherited from caller.
581 */
582
Jeff Garzik057ace52005-10-22 14:27:05 -0400583void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 tf->command = fis[2]; /* status */
586 tf->feature = fis[3]; /* error */
587
588 tf->lbal = fis[4];
589 tf->lbam = fis[5];
590 tf->lbah = fis[6];
591 tf->device = fis[7];
592
593 tf->hob_lbal = fis[8];
594 tf->hob_lbam = fis[9];
595 tf->hob_lbah = fis[10];
596
597 tf->nsect = fis[12];
598 tf->hob_nsect = fis[13];
599}
600
Albert Lee8cbd6df2005-10-12 15:06:27 +0800601static const u8 ata_rw_cmds[] = {
602 /* pio multi */
603 ATA_CMD_READ_MULTI,
604 ATA_CMD_WRITE_MULTI,
605 ATA_CMD_READ_MULTI_EXT,
606 ATA_CMD_WRITE_MULTI_EXT,
607 /* pio */
608 ATA_CMD_PIO_READ,
609 ATA_CMD_PIO_WRITE,
610 ATA_CMD_PIO_READ_EXT,
611 ATA_CMD_PIO_WRITE_EXT,
612 /* dma */
613 ATA_CMD_READ,
614 ATA_CMD_WRITE,
615 ATA_CMD_READ_EXT,
616 ATA_CMD_WRITE_EXT
617};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800620 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
621 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800623 * Examine the device configuration and tf->flags to calculate
624 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 *
626 * LOCKING:
627 * caller.
628 */
Albert Lee8cbd6df2005-10-12 15:06:27 +0800629void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800631 struct ata_taskfile *tf = &qc->tf;
632 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Albert Lee8cbd6df2005-10-12 15:06:27 +0800634 int index, lba48, write;
635
636 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
637 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Albert Lee8cbd6df2005-10-12 15:06:27 +0800639 if (dev->flags & ATA_DFLAG_PIO) {
640 tf->protocol = ATA_PROT_PIO;
641 index = dev->multi_count ? 0 : 4;
642 } else {
643 tf->protocol = ATA_PROT_DMA;
644 index = 8;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Albert Lee8cbd6df2005-10-12 15:06:27 +0800647 tf->command = ata_rw_cmds[index + lba48 + write];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650static const char * xfer_mode_str[] = {
651 "UDMA/16",
652 "UDMA/25",
653 "UDMA/33",
654 "UDMA/44",
655 "UDMA/66",
656 "UDMA/100",
657 "UDMA/133",
658 "UDMA7",
659 "MWDMA0",
660 "MWDMA1",
661 "MWDMA2",
662 "PIO0",
663 "PIO1",
664 "PIO2",
665 "PIO3",
666 "PIO4",
667};
668
669/**
670 * ata_udma_string - convert UDMA bit offset to string
671 * @mask: mask of bits supported; only highest bit counts.
672 *
673 * Determine string which represents the highest speed
674 * (highest bit in @udma_mask).
675 *
676 * LOCKING:
677 * None.
678 *
679 * RETURNS:
680 * Constant C string representing highest speed listed in
681 * @udma_mask, or the constant C string "<n/a>".
682 */
683
684static const char *ata_mode_string(unsigned int mask)
685{
686 int i;
687
688 for (i = 7; i >= 0; i--)
689 if (mask & (1 << i))
690 goto out;
691 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
692 if (mask & (1 << i))
693 goto out;
694 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
695 if (mask & (1 << i))
696 goto out;
697
698 return "<n/a>";
699
700out:
701 return xfer_mode_str[i];
702}
703
704/**
705 * ata_pio_devchk - PATA device presence detection
706 * @ap: ATA channel to examine
707 * @device: Device to examine (starting at zero)
708 *
709 * This technique was originally described in
710 * Hale Landis's ATADRVR (www.ata-atapi.com), and
711 * later found its way into the ATA/ATAPI spec.
712 *
713 * Write a pattern to the ATA shadow registers,
714 * and if a device is present, it will respond by
715 * correctly storing and echoing back the
716 * ATA shadow register contents.
717 *
718 * LOCKING:
719 * caller.
720 */
721
722static unsigned int ata_pio_devchk(struct ata_port *ap,
723 unsigned int device)
724{
725 struct ata_ioports *ioaddr = &ap->ioaddr;
726 u8 nsect, lbal;
727
728 ap->ops->dev_select(ap, device);
729
730 outb(0x55, ioaddr->nsect_addr);
731 outb(0xaa, ioaddr->lbal_addr);
732
733 outb(0xaa, ioaddr->nsect_addr);
734 outb(0x55, ioaddr->lbal_addr);
735
736 outb(0x55, ioaddr->nsect_addr);
737 outb(0xaa, ioaddr->lbal_addr);
738
739 nsect = inb(ioaddr->nsect_addr);
740 lbal = inb(ioaddr->lbal_addr);
741
742 if ((nsect == 0x55) && (lbal == 0xaa))
743 return 1; /* we found a device */
744
745 return 0; /* nothing found */
746}
747
748/**
749 * ata_mmio_devchk - PATA device presence detection
750 * @ap: ATA channel to examine
751 * @device: Device to examine (starting at zero)
752 *
753 * This technique was originally described in
754 * Hale Landis's ATADRVR (www.ata-atapi.com), and
755 * later found its way into the ATA/ATAPI spec.
756 *
757 * Write a pattern to the ATA shadow registers,
758 * and if a device is present, it will respond by
759 * correctly storing and echoing back the
760 * ATA shadow register contents.
761 *
762 * LOCKING:
763 * caller.
764 */
765
766static unsigned int ata_mmio_devchk(struct ata_port *ap,
767 unsigned int device)
768{
769 struct ata_ioports *ioaddr = &ap->ioaddr;
770 u8 nsect, lbal;
771
772 ap->ops->dev_select(ap, device);
773
774 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
775 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
776
777 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
778 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
779
780 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
781 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
782
783 nsect = readb((void __iomem *) ioaddr->nsect_addr);
784 lbal = readb((void __iomem *) ioaddr->lbal_addr);
785
786 if ((nsect == 0x55) && (lbal == 0xaa))
787 return 1; /* we found a device */
788
789 return 0; /* nothing found */
790}
791
792/**
793 * ata_devchk - PATA device presence detection
794 * @ap: ATA channel to examine
795 * @device: Device to examine (starting at zero)
796 *
797 * Dispatch ATA device presence detection, depending
798 * on whether we are using PIO or MMIO to talk to the
799 * ATA shadow registers.
800 *
801 * LOCKING:
802 * caller.
803 */
804
805static unsigned int ata_devchk(struct ata_port *ap,
806 unsigned int device)
807{
808 if (ap->flags & ATA_FLAG_MMIO)
809 return ata_mmio_devchk(ap, device);
810 return ata_pio_devchk(ap, device);
811}
812
813/**
814 * ata_dev_classify - determine device type based on ATA-spec signature
815 * @tf: ATA taskfile register set for device to be identified
816 *
817 * Determine from taskfile register contents whether a device is
818 * ATA or ATAPI, as per "Signature and persistence" section
819 * of ATA/PI spec (volume 1, sect 5.14).
820 *
821 * LOCKING:
822 * None.
823 *
824 * RETURNS:
825 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
826 * the event of failure.
827 */
828
Jeff Garzik057ace52005-10-22 14:27:05 -0400829unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 /* Apple's open source Darwin code hints that some devices only
832 * put a proper signature into the LBA mid/high registers,
833 * So, we only check those. It's sufficient for uniqueness.
834 */
835
836 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
837 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
838 DPRINTK("found ATA device by sig\n");
839 return ATA_DEV_ATA;
840 }
841
842 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
843 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
844 DPRINTK("found ATAPI device by sig\n");
845 return ATA_DEV_ATAPI;
846 }
847
848 DPRINTK("unknown device\n");
849 return ATA_DEV_UNKNOWN;
850}
851
852/**
853 * ata_dev_try_classify - Parse returned ATA device signature
854 * @ap: ATA channel to examine
855 * @device: Device to examine (starting at zero)
856 *
857 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
858 * an ATA/ATAPI-defined set of values is placed in the ATA
859 * shadow registers, indicating the results of device detection
860 * and diagnostics.
861 *
862 * Select the ATA device, and read the values from the ATA shadow
863 * registers. Then parse according to the Error register value,
864 * and the spec-defined values examined by ata_dev_classify().
865 *
866 * LOCKING:
867 * caller.
868 */
869
870static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
871{
872 struct ata_device *dev = &ap->device[device];
873 struct ata_taskfile tf;
874 unsigned int class;
875 u8 err;
876
877 ap->ops->dev_select(ap, device);
878
879 memset(&tf, 0, sizeof(tf));
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400882 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 dev->class = ATA_DEV_NONE;
885
886 /* see if device passed diags */
887 if (err == 1)
888 /* do nothing */ ;
889 else if ((device == 0) && (err == 0x81))
890 /* do nothing */ ;
891 else
892 return err;
893
894 /* determine if device if ATA or ATAPI */
895 class = ata_dev_classify(&tf);
896 if (class == ATA_DEV_UNKNOWN)
897 return err;
898 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
899 return err;
900
901 dev->class = class;
902
903 return err;
904}
905
906/**
907 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
908 * @id: IDENTIFY DEVICE results we will examine
909 * @s: string into which data is output
910 * @ofs: offset into identify device page
911 * @len: length of string to return. must be an even number.
912 *
913 * The strings in the IDENTIFY DEVICE page are broken up into
914 * 16-bit chunks. Run through the string, and output each
915 * 8-bit chunk linearly, regardless of platform.
916 *
917 * LOCKING:
918 * caller.
919 */
920
Jeff Garzik057ace52005-10-22 14:27:05 -0400921void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 unsigned int ofs, unsigned int len)
923{
924 unsigned int c;
925
926 while (len > 0) {
927 c = id[ofs] >> 8;
928 *s = c;
929 s++;
930
931 c = id[ofs] & 0xff;
932 *s = c;
933 s++;
934
935 ofs++;
936 len -= 2;
937 }
938}
939
Edward Falk0baab862005-06-02 18:17:13 -0400940
941/**
942 * ata_noop_dev_select - Select device 0/1 on ATA bus
943 * @ap: ATA channel to manipulate
944 * @device: ATA device (numbered from zero) to select
945 *
946 * This function performs no actual function.
947 *
948 * May be used as the dev_select() entry in ata_port_operations.
949 *
950 * LOCKING:
951 * caller.
952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
954{
955}
956
Edward Falk0baab862005-06-02 18:17:13 -0400957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/**
959 * ata_std_dev_select - Select device 0/1 on ATA bus
960 * @ap: ATA channel to manipulate
961 * @device: ATA device (numbered from zero) to select
962 *
963 * Use the method defined in the ATA specification to
964 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400965 * ATA channel. Works with both PIO and MMIO.
966 *
967 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 *
969 * LOCKING:
970 * caller.
971 */
972
973void ata_std_dev_select (struct ata_port *ap, unsigned int device)
974{
975 u8 tmp;
976
977 if (device == 0)
978 tmp = ATA_DEVICE_OBS;
979 else
980 tmp = ATA_DEVICE_OBS | ATA_DEV1;
981
982 if (ap->flags & ATA_FLAG_MMIO) {
983 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
984 } else {
985 outb(tmp, ap->ioaddr.device_addr);
986 }
987 ata_pause(ap); /* needed; also flushes, for mmio */
988}
989
990/**
991 * ata_dev_select - Select device 0/1 on ATA bus
992 * @ap: ATA channel to manipulate
993 * @device: ATA device (numbered from zero) to select
994 * @wait: non-zero to wait for Status register BSY bit to clear
995 * @can_sleep: non-zero if context allows sleeping
996 *
997 * Use the method defined in the ATA specification to
998 * make either device 0, or device 1, active on the
999 * ATA channel.
1000 *
1001 * This is a high-level version of ata_std_dev_select(),
1002 * which additionally provides the services of inserting
1003 * the proper pauses and status polling, where needed.
1004 *
1005 * LOCKING:
1006 * caller.
1007 */
1008
1009void ata_dev_select(struct ata_port *ap, unsigned int device,
1010 unsigned int wait, unsigned int can_sleep)
1011{
1012 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1013 ap->id, device, wait);
1014
1015 if (wait)
1016 ata_wait_idle(ap);
1017
1018 ap->ops->dev_select(ap, device);
1019
1020 if (wait) {
1021 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1022 msleep(150);
1023 ata_wait_idle(ap);
1024 }
1025}
1026
1027/**
1028 * ata_dump_id - IDENTIFY DEVICE info debugging output
1029 * @dev: Device whose IDENTIFY DEVICE page we will dump
1030 *
1031 * Dump selected 16-bit words from a detected device's
1032 * IDENTIFY PAGE page.
1033 *
1034 * LOCKING:
1035 * caller.
1036 */
1037
Jeff Garzik057ace52005-10-22 14:27:05 -04001038static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 DPRINTK("49==0x%04x "
1041 "53==0x%04x "
1042 "63==0x%04x "
1043 "64==0x%04x "
1044 "75==0x%04x \n",
1045 dev->id[49],
1046 dev->id[53],
1047 dev->id[63],
1048 dev->id[64],
1049 dev->id[75]);
1050 DPRINTK("80==0x%04x "
1051 "81==0x%04x "
1052 "82==0x%04x "
1053 "83==0x%04x "
1054 "84==0x%04x \n",
1055 dev->id[80],
1056 dev->id[81],
1057 dev->id[82],
1058 dev->id[83],
1059 dev->id[84]);
1060 DPRINTK("88==0x%04x "
1061 "93==0x%04x\n",
1062 dev->id[88],
1063 dev->id[93]);
1064}
1065
Alan Cox11e29e22005-10-21 18:46:32 -04001066/*
1067 * Compute the PIO modes available for this device. This is not as
1068 * trivial as it seems if we must consider early devices correctly.
1069 *
1070 * FIXME: pre IDE drive timing (do we care ?).
1071 */
1072
Jeff Garzik057ace52005-10-22 14:27:05 -04001073static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001074{
1075 u16 modes;
1076
1077 /* Usual case. Word 53 indicates word 88 is valid */
1078 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1079 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1080 modes <<= 3;
1081 modes |= 0x7;
1082 return modes;
1083 }
1084
1085 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1086 for the maximum. Turn it into a mask and return it */
1087 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1088 return modes;
1089}
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091/**
1092 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1093 * @ap: port on which device we wish to probe resides
1094 * @device: device bus address, starting at zero
1095 *
1096 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1097 * command, and read back the 512-byte device information page.
1098 * The device information page is fed to us via the standard
1099 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1100 * using standard PIO-IN paths)
1101 *
1102 * After reading the device information page, we use several
1103 * bits of information from it to initialize data structures
1104 * that will be used during the lifetime of the ata_device.
1105 * Other data from the info page is used to disqualify certain
1106 * older ATA devices we do not wish to support.
1107 *
1108 * LOCKING:
1109 * Inherited from caller. Some functions called by this function
1110 * obtain the host_set lock.
1111 */
1112
1113static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1114{
1115 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001116 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 u16 tmp;
1118 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 unsigned int using_edd;
1120 DECLARE_COMPLETION(wait);
1121 struct ata_queued_cmd *qc;
1122 unsigned long flags;
1123 int rc;
1124
1125 if (!ata_dev_present(dev)) {
1126 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1127 ap->id, device);
1128 return;
1129 }
1130
1131 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1132 using_edd = 0;
1133 else
1134 using_edd = 1;
1135
1136 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1137
1138 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1139 dev->class == ATA_DEV_NONE);
1140
1141 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1142
1143 qc = ata_qc_new_init(ap, dev);
1144 BUG_ON(qc == NULL);
1145
1146 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1147 qc->dma_dir = DMA_FROM_DEVICE;
1148 qc->tf.protocol = ATA_PROT_PIO;
1149 qc->nsect = 1;
1150
1151retry:
1152 if (dev->class == ATA_DEV_ATA) {
1153 qc->tf.command = ATA_CMD_ID_ATA;
1154 DPRINTK("do ATA identify\n");
1155 } else {
1156 qc->tf.command = ATA_CMD_ID_ATAPI;
1157 DPRINTK("do ATAPI identify\n");
1158 }
1159
1160 qc->waiting = &wait;
1161 qc->complete_fn = ata_qc_complete_noop;
1162
1163 spin_lock_irqsave(&ap->host_set->lock, flags);
1164 rc = ata_qc_issue(qc);
1165 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1166
1167 if (rc)
1168 goto err_out;
1169 else
1170 wait_for_completion(&wait);
1171
Jeff Garzik0169e282005-10-29 21:25:10 -04001172 spin_lock_irqsave(&ap->host_set->lock, flags);
1173 ap->ops->tf_read(ap, &qc->tf);
1174 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1175
1176 if (qc->tf.command & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /*
1178 * arg! EDD works for all test cases, but seems to return
1179 * the ATA signature for some ATAPI devices. Until the
1180 * reason for this is found and fixed, we fix up the mess
1181 * here. If IDENTIFY DEVICE returns command aborted
1182 * (as ATAPI devices do), then we issue an
1183 * IDENTIFY PACKET DEVICE.
1184 *
1185 * ATA software reset (SRST, the default) does not appear
1186 * to have this problem.
1187 */
1188 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
Jeff Garzik0169e282005-10-29 21:25:10 -04001189 u8 err = qc->tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (err & ATA_ABORTED) {
1191 dev->class = ATA_DEV_ATAPI;
1192 qc->cursg = 0;
1193 qc->cursg_ofs = 0;
1194 qc->cursect = 0;
1195 qc->nsect = 1;
1196 goto retry;
1197 }
1198 }
1199 goto err_out;
1200 }
1201
1202 swap_buf_le16(dev->id, ATA_ID_WORDS);
1203
1204 /* print device capabilities */
1205 printk(KERN_DEBUG "ata%u: dev %u cfg "
1206 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1207 ap->id, device, dev->id[49],
1208 dev->id[82], dev->id[83], dev->id[84],
1209 dev->id[85], dev->id[86], dev->id[87],
1210 dev->id[88]);
1211
1212 /*
1213 * common ATA, ATAPI feature tests
1214 */
1215
Albert Lee8bf62ec2005-05-12 15:29:42 -04001216 /* we require DMA support (bits 8 of word 49) */
1217 if (!ata_id_has_dma(dev->id)) {
1218 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 goto err_out_nosup;
1220 }
1221
1222 /* quick-n-dirty find max transfer mode; for printk only */
1223 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1224 if (!xfer_modes)
1225 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001226 if (!xfer_modes)
1227 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 ata_dump_id(dev);
1230
1231 /* ATA-specific feature tests */
1232 if (dev->class == ATA_DEV_ATA) {
1233 if (!ata_id_is_ata(dev->id)) /* sanity check */
1234 goto err_out_nosup;
1235
Albert Lee8bf62ec2005-05-12 15:29:42 -04001236 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001238 for (major_version = 14; major_version >= 1; major_version--)
1239 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 break;
1241
Albert Lee8bf62ec2005-05-12 15:29:42 -04001242 /*
1243 * The exact sequence expected by certain pre-ATA4 drives is:
1244 * SRST RESET
1245 * IDENTIFY
1246 * INITIALIZE DEVICE PARAMETERS
1247 * anything else..
1248 * Some drives were very specific about that exact sequence.
1249 */
Albert Lee59a10b12005-10-12 15:09:42 +08001250 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001251 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Albert Lee59a10b12005-10-12 15:09:42 +08001253 /* current CHS translation info (id[53-58]) might be
1254 * changed. reread the identify device info.
1255 */
1256 ata_dev_reread_id(ap, dev);
1257 }
1258
Albert Lee8bf62ec2005-05-12 15:29:42 -04001259 if (ata_id_has_lba(dev->id)) {
1260 dev->flags |= ATA_DFLAG_LBA;
1261
1262 if (ata_id_has_lba48(dev->id)) {
1263 dev->flags |= ATA_DFLAG_LBA48;
1264 dev->n_sectors = ata_id_u64(dev->id, 100);
1265 } else {
1266 dev->n_sectors = ata_id_u32(dev->id, 60);
1267 }
1268
1269 /* print device info to dmesg */
1270 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1271 ap->id, device,
1272 major_version,
1273 ata_mode_string(xfer_modes),
1274 (unsigned long long)dev->n_sectors,
1275 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1276 } else {
1277 /* CHS */
1278
1279 /* Default translation */
1280 dev->cylinders = dev->id[1];
1281 dev->heads = dev->id[3];
1282 dev->sectors = dev->id[6];
1283 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1284
1285 if (ata_id_current_chs_valid(dev->id)) {
1286 /* Current CHS translation is valid. */
1287 dev->cylinders = dev->id[54];
1288 dev->heads = dev->id[55];
1289 dev->sectors = dev->id[56];
1290
1291 dev->n_sectors = ata_id_u32(dev->id, 57);
1292 }
1293
1294 /* print device info to dmesg */
1295 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1296 ap->id, device,
1297 major_version,
1298 ata_mode_string(xfer_modes),
1299 (unsigned long long)dev->n_sectors,
1300 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
1304 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
1307 /* ATAPI-specific feature tests */
1308 else {
1309 if (ata_id_is_ata(dev->id)) /* sanity check */
1310 goto err_out_nosup;
1311
1312 rc = atapi_cdb_len(dev->id);
1313 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1314 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1315 goto err_out_nosup;
1316 }
1317 ap->cdb_len = (unsigned int) rc;
1318 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1319
1320 /* print device info to dmesg */
1321 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1322 ap->id, device,
1323 ata_mode_string(xfer_modes));
1324 }
1325
1326 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1327 return;
1328
1329err_out_nosup:
1330 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1331 ap->id, device);
1332err_out:
1333 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1334 DPRINTK("EXIT, err\n");
1335}
1336
Brad Campbell6f2f3812005-05-12 15:07:47 -04001337
Jeff Garzik057ace52005-10-22 14:27:05 -04001338static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001339{
1340 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1341}
1342
1343/**
1344 * ata_dev_config - Run device specific handlers and check for
1345 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001346 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001347 * @i: Device
1348 *
1349 * LOCKING:
1350 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001351
Brad Campbell6f2f3812005-05-12 15:07:47 -04001352void ata_dev_config(struct ata_port *ap, unsigned int i)
1353{
1354 /* limit bridge transfers to udma5, 200 sectors */
1355 if (ata_dev_knobble(ap)) {
1356 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1357 ap->id, ap->device->devno);
1358 ap->udma_mask &= ATA_UDMA5;
1359 ap->host->max_sectors = ATA_MAX_SECTORS;
1360 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1361 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1362 }
1363
1364 if (ap->ops->dev_config)
1365 ap->ops->dev_config(ap, &ap->device[i]);
1366}
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368/**
1369 * ata_bus_probe - Reset and probe ATA bus
1370 * @ap: Bus to probe
1371 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001372 * Master ATA bus probing function. Initiates a hardware-dependent
1373 * bus reset, then attempts to identify any devices found on
1374 * the bus.
1375 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001377 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 *
1379 * RETURNS:
1380 * Zero on success, non-zero on error.
1381 */
1382
1383static int ata_bus_probe(struct ata_port *ap)
1384{
1385 unsigned int i, found = 0;
1386
1387 ap->ops->phy_reset(ap);
1388 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1389 goto err_out;
1390
1391 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1392 ata_dev_identify(ap, i);
1393 if (ata_dev_present(&ap->device[i])) {
1394 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001395 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397 }
1398
1399 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1400 goto err_out_disable;
1401
1402 ata_set_mode(ap);
1403 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1404 goto err_out_disable;
1405
1406 return 0;
1407
1408err_out_disable:
1409 ap->ops->port_disable(ap);
1410err_out:
1411 return -1;
1412}
1413
1414/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001415 * ata_port_probe - Mark port as enabled
1416 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001418 * Modify @ap data structure such that the system
1419 * thinks that the entire port is enabled.
1420 *
1421 * LOCKING: host_set lock, or some other form of
1422 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
1424
1425void ata_port_probe(struct ata_port *ap)
1426{
1427 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1428}
1429
1430/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001431 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1432 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001434 * This function issues commands to standard SATA Sxxx
1435 * PHY registers, to wake up the phy (and device), and
1436 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 *
1438 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001439 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 *
1441 */
1442void __sata_phy_reset(struct ata_port *ap)
1443{
1444 u32 sstatus;
1445 unsigned long timeout = jiffies + (HZ * 5);
1446
1447 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001448 /* issue phy wake/reset */
1449 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001450 /* Couldn't find anything in SATA I/II specs, but
1451 * AHCI-1.1 10.4.2 says at least 1 ms. */
1452 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
Brett Russcdcca892005-03-28 15:10:27 -05001454 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /* wait for phy to become ready, if necessary */
1457 do {
1458 msleep(200);
1459 sstatus = scr_read(ap, SCR_STATUS);
1460 if ((sstatus & 0xf) != 1)
1461 break;
1462 } while (time_before(jiffies, timeout));
1463
1464 /* TODO: phy layer with polling, timeouts, etc. */
1465 if (sata_dev_present(ap))
1466 ata_port_probe(ap);
1467 else {
1468 sstatus = scr_read(ap, SCR_STATUS);
1469 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1470 ap->id, sstatus);
1471 ata_port_disable(ap);
1472 }
1473
1474 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1475 return;
1476
1477 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1478 ata_port_disable(ap);
1479 return;
1480 }
1481
1482 ap->cbl = ATA_CBL_SATA;
1483}
1484
1485/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001486 * sata_phy_reset - Reset SATA bus.
1487 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001489 * This function resets the SATA bus, and then probes
1490 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 *
1492 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001493 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 *
1495 */
1496void sata_phy_reset(struct ata_port *ap)
1497{
1498 __sata_phy_reset(ap);
1499 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1500 return;
1501 ata_bus_reset(ap);
1502}
1503
1504/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001505 * ata_port_disable - Disable port.
1506 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001508 * Modify @ap data structure such that the system
1509 * thinks that the entire port is disabled, and should
1510 * never attempt to probe or communicate with devices
1511 * on this port.
1512 *
1513 * LOCKING: host_set lock, or some other form of
1514 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 */
1516
1517void ata_port_disable(struct ata_port *ap)
1518{
1519 ap->device[0].class = ATA_DEV_NONE;
1520 ap->device[1].class = ATA_DEV_NONE;
1521 ap->flags |= ATA_FLAG_PORT_DISABLED;
1522}
1523
Alan Cox452503f2005-10-21 19:01:32 -04001524/*
1525 * This mode timing computation functionality is ported over from
1526 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1527 */
1528/*
1529 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1530 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1531 * for PIO 5, which is a nonstandard extension and UDMA6, which
1532 * is currently supported only by Maxtor drives.
1533 */
1534
1535static const struct ata_timing ata_timing[] = {
1536
1537 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1538 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1539 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1540 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1541
1542 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1543 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1544 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1545
1546/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1547
1548 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1549 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1550 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1551
1552 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1553 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1554 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1555
1556/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1557 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1558 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1559
1560 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1561 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1562 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1563
1564/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1565
1566 { 0xFF }
1567};
1568
1569#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1570#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1571
1572static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1573{
1574 q->setup = EZ(t->setup * 1000, T);
1575 q->act8b = EZ(t->act8b * 1000, T);
1576 q->rec8b = EZ(t->rec8b * 1000, T);
1577 q->cyc8b = EZ(t->cyc8b * 1000, T);
1578 q->active = EZ(t->active * 1000, T);
1579 q->recover = EZ(t->recover * 1000, T);
1580 q->cycle = EZ(t->cycle * 1000, T);
1581 q->udma = EZ(t->udma * 1000, UT);
1582}
1583
1584void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1585 struct ata_timing *m, unsigned int what)
1586{
1587 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1588 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1589 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1590 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1591 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1592 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1593 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1594 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1595}
1596
1597static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1598{
1599 const struct ata_timing *t;
1600
1601 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001602 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001603 return NULL;
1604 return t;
1605}
1606
1607int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1608 struct ata_timing *t, int T, int UT)
1609{
1610 const struct ata_timing *s;
1611 struct ata_timing p;
1612
1613 /*
1614 * Find the mode.
1615 */
1616
1617 if (!(s = ata_timing_find_mode(speed)))
1618 return -EINVAL;
1619
1620 /*
1621 * If the drive is an EIDE drive, it can tell us it needs extended
1622 * PIO/MW_DMA cycle timing.
1623 */
1624
1625 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1626 memset(&p, 0, sizeof(p));
1627 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1628 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1629 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1630 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1631 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1632 }
1633 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1634 }
1635
1636 /*
1637 * Convert the timing to bus clock counts.
1638 */
1639
1640 ata_timing_quantize(s, t, T, UT);
1641
1642 /*
1643 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1644 * and some other commands. We have to ensure that the DMA cycle timing is
1645 * slower/equal than the fastest PIO timing.
1646 */
1647
1648 if (speed > XFER_PIO_4) {
1649 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1650 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1651 }
1652
1653 /*
1654 * Lenghten active & recovery time so that cycle time is correct.
1655 */
1656
1657 if (t->act8b + t->rec8b < t->cyc8b) {
1658 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1659 t->rec8b = t->cyc8b - t->act8b;
1660 }
1661
1662 if (t->active + t->recover < t->cycle) {
1663 t->active += (t->cycle - (t->active + t->recover)) / 2;
1664 t->recover = t->cycle - t->active;
1665 }
1666
1667 return 0;
1668}
1669
Jeff Garzik057ace52005-10-22 14:27:05 -04001670static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 unsigned int shift;
1672 u8 base;
1673} xfer_mode_classes[] = {
1674 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1675 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1676 { ATA_SHIFT_PIO, XFER_PIO_0 },
1677};
1678
1679static inline u8 base_from_shift(unsigned int shift)
1680{
1681 int i;
1682
1683 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1684 if (xfer_mode_classes[i].shift == shift)
1685 return xfer_mode_classes[i].base;
1686
1687 return 0xff;
1688}
1689
1690static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1691{
1692 int ofs, idx;
1693 u8 base;
1694
1695 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1696 return;
1697
1698 if (dev->xfer_shift == ATA_SHIFT_PIO)
1699 dev->flags |= ATA_DFLAG_PIO;
1700
1701 ata_dev_set_xfermode(ap, dev);
1702
1703 base = base_from_shift(dev->xfer_shift);
1704 ofs = dev->xfer_mode - base;
1705 idx = ofs + dev->xfer_shift;
1706 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1707
1708 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1709 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1710
1711 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1712 ap->id, dev->devno, xfer_mode_str[idx]);
1713}
1714
1715static int ata_host_set_pio(struct ata_port *ap)
1716{
1717 unsigned int mask;
1718 int x, i;
1719 u8 base, xfer_mode;
1720
1721 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1722 x = fgb(mask);
1723 if (x < 0) {
1724 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1725 return -1;
1726 }
1727
1728 base = base_from_shift(ATA_SHIFT_PIO);
1729 xfer_mode = base + x;
1730
1731 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1732 (int)base, (int)xfer_mode, mask, x);
1733
1734 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1735 struct ata_device *dev = &ap->device[i];
1736 if (ata_dev_present(dev)) {
1737 dev->pio_mode = xfer_mode;
1738 dev->xfer_mode = xfer_mode;
1739 dev->xfer_shift = ATA_SHIFT_PIO;
1740 if (ap->ops->set_piomode)
1741 ap->ops->set_piomode(ap, dev);
1742 }
1743 }
1744
1745 return 0;
1746}
1747
1748static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1749 unsigned int xfer_shift)
1750{
1751 int i;
1752
1753 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1754 struct ata_device *dev = &ap->device[i];
1755 if (ata_dev_present(dev)) {
1756 dev->dma_mode = xfer_mode;
1757 dev->xfer_mode = xfer_mode;
1758 dev->xfer_shift = xfer_shift;
1759 if (ap->ops->set_dmamode)
1760 ap->ops->set_dmamode(ap, dev);
1761 }
1762 }
1763}
1764
1765/**
1766 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1767 * @ap: port on which timings will be programmed
1768 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001769 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1770 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001772 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 *
1774 */
1775static void ata_set_mode(struct ata_port *ap)
1776{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001777 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 u8 xfer_mode;
1779 int rc;
1780
1781 /* step 1: always set host PIO timings */
1782 rc = ata_host_set_pio(ap);
1783 if (rc)
1784 goto err_out;
1785
1786 /* step 2: choose the best data xfer mode */
1787 xfer_mode = xfer_shift = 0;
1788 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1789 if (rc)
1790 goto err_out;
1791
1792 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1793 if (xfer_shift != ATA_SHIFT_PIO)
1794 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1795
1796 /* step 4: update devices' xfer mode */
1797 ata_dev_set_mode(ap, &ap->device[0]);
1798 ata_dev_set_mode(ap, &ap->device[1]);
1799
1800 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1801 return;
1802
1803 if (ap->ops->post_set_mode)
1804 ap->ops->post_set_mode(ap);
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return;
1807
1808err_out:
1809 ata_port_disable(ap);
1810}
1811
1812/**
1813 * ata_busy_sleep - sleep until BSY clears, or timeout
1814 * @ap: port containing status register to be polled
1815 * @tmout_pat: impatience timeout
1816 * @tmout: overall timeout
1817 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001818 * Sleep until ATA Status register bit BSY clears,
1819 * or a timeout occurs.
1820 *
1821 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 *
1823 */
1824
1825static unsigned int ata_busy_sleep (struct ata_port *ap,
1826 unsigned long tmout_pat,
1827 unsigned long tmout)
1828{
1829 unsigned long timer_start, timeout;
1830 u8 status;
1831
1832 status = ata_busy_wait(ap, ATA_BUSY, 300);
1833 timer_start = jiffies;
1834 timeout = timer_start + tmout_pat;
1835 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1836 msleep(50);
1837 status = ata_busy_wait(ap, ATA_BUSY, 3);
1838 }
1839
1840 if (status & ATA_BUSY)
1841 printk(KERN_WARNING "ata%u is slow to respond, "
1842 "please be patient\n", ap->id);
1843
1844 timeout = timer_start + tmout;
1845 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1846 msleep(50);
1847 status = ata_chk_status(ap);
1848 }
1849
1850 if (status & ATA_BUSY) {
1851 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1852 ap->id, tmout / HZ);
1853 return 1;
1854 }
1855
1856 return 0;
1857}
1858
1859static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1860{
1861 struct ata_ioports *ioaddr = &ap->ioaddr;
1862 unsigned int dev0 = devmask & (1 << 0);
1863 unsigned int dev1 = devmask & (1 << 1);
1864 unsigned long timeout;
1865
1866 /* if device 0 was found in ata_devchk, wait for its
1867 * BSY bit to clear
1868 */
1869 if (dev0)
1870 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1871
1872 /* if device 1 was found in ata_devchk, wait for
1873 * register access, then wait for BSY to clear
1874 */
1875 timeout = jiffies + ATA_TMOUT_BOOT;
1876 while (dev1) {
1877 u8 nsect, lbal;
1878
1879 ap->ops->dev_select(ap, 1);
1880 if (ap->flags & ATA_FLAG_MMIO) {
1881 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1882 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1883 } else {
1884 nsect = inb(ioaddr->nsect_addr);
1885 lbal = inb(ioaddr->lbal_addr);
1886 }
1887 if ((nsect == 1) && (lbal == 1))
1888 break;
1889 if (time_after(jiffies, timeout)) {
1890 dev1 = 0;
1891 break;
1892 }
1893 msleep(50); /* give drive a breather */
1894 }
1895 if (dev1)
1896 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1897
1898 /* is all this really necessary? */
1899 ap->ops->dev_select(ap, 0);
1900 if (dev1)
1901 ap->ops->dev_select(ap, 1);
1902 if (dev0)
1903 ap->ops->dev_select(ap, 0);
1904}
1905
1906/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001907 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1908 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001910 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1911 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 *
1913 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001914 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 *
1916 */
1917
1918static unsigned int ata_bus_edd(struct ata_port *ap)
1919{
1920 struct ata_taskfile tf;
1921
1922 /* set up execute-device-diag (bus reset) taskfile */
1923 /* also, take interrupts to a known state (disabled) */
1924 DPRINTK("execute-device-diag\n");
1925 ata_tf_init(ap, &tf, 0);
1926 tf.ctl |= ATA_NIEN;
1927 tf.command = ATA_CMD_EDD;
1928 tf.protocol = ATA_PROT_NODATA;
1929
1930 /* do bus reset */
1931 ata_tf_to_host(ap, &tf);
1932
1933 /* spec says at least 2ms. but who knows with those
1934 * crazy ATAPI devices...
1935 */
1936 msleep(150);
1937
1938 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1939}
1940
1941static unsigned int ata_bus_softreset(struct ata_port *ap,
1942 unsigned int devmask)
1943{
1944 struct ata_ioports *ioaddr = &ap->ioaddr;
1945
1946 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1947
1948 /* software reset. causes dev0 to be selected */
1949 if (ap->flags & ATA_FLAG_MMIO) {
1950 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1951 udelay(20); /* FIXME: flush */
1952 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1953 udelay(20); /* FIXME: flush */
1954 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1955 } else {
1956 outb(ap->ctl, ioaddr->ctl_addr);
1957 udelay(10);
1958 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1959 udelay(10);
1960 outb(ap->ctl, ioaddr->ctl_addr);
1961 }
1962
1963 /* spec mandates ">= 2ms" before checking status.
1964 * We wait 150ms, because that was the magic delay used for
1965 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1966 * between when the ATA command register is written, and then
1967 * status is checked. Because waiting for "a while" before
1968 * checking status is fine, post SRST, we perform this magic
1969 * delay here as well.
1970 */
1971 msleep(150);
1972
1973 ata_bus_post_reset(ap, devmask);
1974
1975 return 0;
1976}
1977
1978/**
1979 * ata_bus_reset - reset host port and associated ATA channel
1980 * @ap: port to reset
1981 *
1982 * This is typically the first time we actually start issuing
1983 * commands to the ATA channel. We wait for BSY to clear, then
1984 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1985 * result. Determine what devices, if any, are on the channel
1986 * by looking at the device 0/1 error register. Look at the signature
1987 * stored in each device's taskfile registers, to determine if
1988 * the device is ATA or ATAPI.
1989 *
1990 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001991 * PCI/etc. bus probe sem.
1992 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 *
1994 * SIDE EFFECTS:
1995 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1996 */
1997
1998void ata_bus_reset(struct ata_port *ap)
1999{
2000 struct ata_ioports *ioaddr = &ap->ioaddr;
2001 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2002 u8 err;
2003 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2004
2005 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2006
2007 /* determine if device 0/1 are present */
2008 if (ap->flags & ATA_FLAG_SATA_RESET)
2009 dev0 = 1;
2010 else {
2011 dev0 = ata_devchk(ap, 0);
2012 if (slave_possible)
2013 dev1 = ata_devchk(ap, 1);
2014 }
2015
2016 if (dev0)
2017 devmask |= (1 << 0);
2018 if (dev1)
2019 devmask |= (1 << 1);
2020
2021 /* select device 0 again */
2022 ap->ops->dev_select(ap, 0);
2023
2024 /* issue bus reset */
2025 if (ap->flags & ATA_FLAG_SRST)
2026 rc = ata_bus_softreset(ap, devmask);
2027 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2028 /* set up device control */
2029 if (ap->flags & ATA_FLAG_MMIO)
2030 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2031 else
2032 outb(ap->ctl, ioaddr->ctl_addr);
2033 rc = ata_bus_edd(ap);
2034 }
2035
2036 if (rc)
2037 goto err_out;
2038
2039 /*
2040 * determine by signature whether we have ATA or ATAPI devices
2041 */
2042 err = ata_dev_try_classify(ap, 0);
2043 if ((slave_possible) && (err != 0x81))
2044 ata_dev_try_classify(ap, 1);
2045
2046 /* re-enable interrupts */
2047 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2048 ata_irq_on(ap);
2049
2050 /* is double-select really necessary? */
2051 if (ap->device[1].class != ATA_DEV_NONE)
2052 ap->ops->dev_select(ap, 1);
2053 if (ap->device[0].class != ATA_DEV_NONE)
2054 ap->ops->dev_select(ap, 0);
2055
2056 /* if no devices were detected, disable this port */
2057 if ((ap->device[0].class == ATA_DEV_NONE) &&
2058 (ap->device[1].class == ATA_DEV_NONE))
2059 goto err_out;
2060
2061 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2062 /* set up device control for ATA_FLAG_SATA_RESET */
2063 if (ap->flags & ATA_FLAG_MMIO)
2064 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2065 else
2066 outb(ap->ctl, ioaddr->ctl_addr);
2067 }
2068
2069 DPRINTK("EXIT\n");
2070 return;
2071
2072err_out:
2073 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2074 ap->ops->port_disable(ap);
2075
2076 DPRINTK("EXIT\n");
2077}
2078
Jeff Garzik057ace52005-10-22 14:27:05 -04002079static void ata_pr_blacklisted(const struct ata_port *ap,
2080 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2083 ap->id, dev->devno);
2084}
2085
2086static const char * ata_dma_blacklist [] = {
2087 "WDC AC11000H",
2088 "WDC AC22100H",
2089 "WDC AC32500H",
2090 "WDC AC33100H",
2091 "WDC AC31600H",
2092 "WDC AC32100H",
2093 "WDC AC23200L",
2094 "Compaq CRD-8241B",
2095 "CRD-8400B",
2096 "CRD-8480B",
2097 "CRD-8482B",
2098 "CRD-84",
2099 "SanDisk SDP3B",
2100 "SanDisk SDP3B-64",
2101 "SANYO CD-ROM CRD",
2102 "HITACHI CDR-8",
2103 "HITACHI CDR-8335",
2104 "HITACHI CDR-8435",
2105 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002106 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 "CD-532E-A",
2108 "E-IDE CD-ROM CR-840",
2109 "CD-ROM Drive/F5A",
2110 "WPI CDD-820",
2111 "SAMSUNG CD-ROM SC-148C",
2112 "SAMSUNG CD-ROM SC",
2113 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2115 "_NEC DV5800A",
2116};
2117
Jeff Garzik057ace52005-10-22 14:27:05 -04002118static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
2120 unsigned char model_num[40];
2121 char *s;
2122 unsigned int len;
2123 int i;
2124
2125 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2126 sizeof(model_num));
2127 s = &model_num[0];
2128 len = strnlen(s, sizeof(model_num));
2129
2130 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2131 while ((len > 0) && (s[len - 1] == ' ')) {
2132 len--;
2133 s[len] = 0;
2134 }
2135
2136 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2137 if (!strncmp(ata_dma_blacklist[i], s, len))
2138 return 1;
2139
2140 return 0;
2141}
2142
Jeff Garzik057ace52005-10-22 14:27:05 -04002143static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Jeff Garzik057ace52005-10-22 14:27:05 -04002145 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 unsigned int mask;
2147
2148 master = &ap->device[0];
2149 slave = &ap->device[1];
2150
2151 assert (ata_dev_present(master) || ata_dev_present(slave));
2152
2153 if (shift == ATA_SHIFT_UDMA) {
2154 mask = ap->udma_mask;
2155 if (ata_dev_present(master)) {
2156 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002157 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 mask = 0;
2159 ata_pr_blacklisted(ap, master);
2160 }
2161 }
2162 if (ata_dev_present(slave)) {
2163 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002164 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 mask = 0;
2166 ata_pr_blacklisted(ap, slave);
2167 }
2168 }
2169 }
2170 else if (shift == ATA_SHIFT_MWDMA) {
2171 mask = ap->mwdma_mask;
2172 if (ata_dev_present(master)) {
2173 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002174 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 mask = 0;
2176 ata_pr_blacklisted(ap, master);
2177 }
2178 }
2179 if (ata_dev_present(slave)) {
2180 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002181 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 mask = 0;
2183 ata_pr_blacklisted(ap, slave);
2184 }
2185 }
2186 }
2187 else if (shift == ATA_SHIFT_PIO) {
2188 mask = ap->pio_mask;
2189 if (ata_dev_present(master)) {
2190 /* spec doesn't return explicit support for
2191 * PIO0-2, so we fake it
2192 */
2193 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2194 tmp_mode <<= 3;
2195 tmp_mode |= 0x7;
2196 mask &= tmp_mode;
2197 }
2198 if (ata_dev_present(slave)) {
2199 /* spec doesn't return explicit support for
2200 * PIO0-2, so we fake it
2201 */
2202 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2203 tmp_mode <<= 3;
2204 tmp_mode |= 0x7;
2205 mask &= tmp_mode;
2206 }
2207 }
2208 else {
2209 mask = 0xffffffff; /* shut up compiler warning */
2210 BUG();
2211 }
2212
2213 return mask;
2214}
2215
2216/* find greatest bit */
2217static int fgb(u32 bitmap)
2218{
2219 unsigned int i;
2220 int x = -1;
2221
2222 for (i = 0; i < 32; i++)
2223 if (bitmap & (1 << i))
2224 x = i;
2225
2226 return x;
2227}
2228
2229/**
2230 * ata_choose_xfer_mode - attempt to find best transfer mode
2231 * @ap: Port for which an xfer mode will be selected
2232 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2233 * @xfer_shift_out: (output) bit shift that selects this mode
2234 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002235 * Based on host and device capabilities, determine the
2236 * maximum transfer mode that is amenable to all.
2237 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002239 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 *
2241 * RETURNS:
2242 * Zero on success, negative on error.
2243 */
2244
Jeff Garzik057ace52005-10-22 14:27:05 -04002245static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 u8 *xfer_mode_out,
2247 unsigned int *xfer_shift_out)
2248{
2249 unsigned int mask, shift;
2250 int x, i;
2251
2252 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2253 shift = xfer_mode_classes[i].shift;
2254 mask = ata_get_mode_mask(ap, shift);
2255
2256 x = fgb(mask);
2257 if (x >= 0) {
2258 *xfer_mode_out = xfer_mode_classes[i].base + x;
2259 *xfer_shift_out = shift;
2260 return 0;
2261 }
2262 }
2263
2264 return -1;
2265}
2266
2267/**
2268 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2269 * @ap: Port associated with device @dev
2270 * @dev: Device to which command will be sent
2271 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002272 * Issue SET FEATURES - XFER MODE command to device @dev
2273 * on port @ap.
2274 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002276 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 */
2278
2279static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2280{
2281 DECLARE_COMPLETION(wait);
2282 struct ata_queued_cmd *qc;
2283 int rc;
2284 unsigned long flags;
2285
2286 /* set up set-features taskfile */
2287 DPRINTK("set features - xfer mode\n");
2288
2289 qc = ata_qc_new_init(ap, dev);
2290 BUG_ON(qc == NULL);
2291
2292 qc->tf.command = ATA_CMD_SET_FEATURES;
2293 qc->tf.feature = SETFEATURES_XFER;
2294 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2295 qc->tf.protocol = ATA_PROT_NODATA;
2296 qc->tf.nsect = dev->xfer_mode;
2297
2298 qc->waiting = &wait;
2299 qc->complete_fn = ata_qc_complete_noop;
2300
2301 spin_lock_irqsave(&ap->host_set->lock, flags);
2302 rc = ata_qc_issue(qc);
2303 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2304
2305 if (rc)
2306 ata_port_disable(ap);
2307 else
2308 wait_for_completion(&wait);
2309
2310 DPRINTK("EXIT\n");
2311}
2312
2313/**
Albert Lee59a10b12005-10-12 15:09:42 +08002314 * ata_dev_reread_id - Reread the device identify device info
2315 * @ap: port where the device is
2316 * @dev: device to reread the identify device info
2317 *
2318 * LOCKING:
2319 */
2320
2321static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2322{
2323 DECLARE_COMPLETION(wait);
2324 struct ata_queued_cmd *qc;
2325 unsigned long flags;
2326 int rc;
2327
2328 qc = ata_qc_new_init(ap, dev);
2329 BUG_ON(qc == NULL);
2330
2331 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2332 qc->dma_dir = DMA_FROM_DEVICE;
2333
2334 if (dev->class == ATA_DEV_ATA) {
2335 qc->tf.command = ATA_CMD_ID_ATA;
2336 DPRINTK("do ATA identify\n");
2337 } else {
2338 qc->tf.command = ATA_CMD_ID_ATAPI;
2339 DPRINTK("do ATAPI identify\n");
2340 }
2341
2342 qc->tf.flags |= ATA_TFLAG_DEVICE;
2343 qc->tf.protocol = ATA_PROT_PIO;
2344 qc->nsect = 1;
2345
2346 qc->waiting = &wait;
2347 qc->complete_fn = ata_qc_complete_noop;
2348
2349 spin_lock_irqsave(&ap->host_set->lock, flags);
2350 rc = ata_qc_issue(qc);
2351 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2352
2353 if (rc)
2354 goto err_out;
2355
2356 wait_for_completion(&wait);
2357
2358 swap_buf_le16(dev->id, ATA_ID_WORDS);
2359
2360 ata_dump_id(dev);
2361
2362 DPRINTK("EXIT\n");
2363
2364 return;
2365err_out:
2366 ata_port_disable(ap);
2367}
2368
2369/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002370 * ata_dev_init_params - Issue INIT DEV PARAMS command
2371 * @ap: Port associated with device @dev
2372 * @dev: Device to which command will be sent
2373 *
2374 * LOCKING:
2375 */
2376
2377static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2378{
2379 DECLARE_COMPLETION(wait);
2380 struct ata_queued_cmd *qc;
2381 int rc;
2382 unsigned long flags;
2383 u16 sectors = dev->id[6];
2384 u16 heads = dev->id[3];
2385
2386 /* Number of sectors per track 1-255. Number of heads 1-16 */
2387 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2388 return;
2389
2390 /* set up init dev params taskfile */
2391 DPRINTK("init dev params \n");
2392
2393 qc = ata_qc_new_init(ap, dev);
2394 BUG_ON(qc == NULL);
2395
2396 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2397 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2398 qc->tf.protocol = ATA_PROT_NODATA;
2399 qc->tf.nsect = sectors;
2400 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2401
2402 qc->waiting = &wait;
2403 qc->complete_fn = ata_qc_complete_noop;
2404
2405 spin_lock_irqsave(&ap->host_set->lock, flags);
2406 rc = ata_qc_issue(qc);
2407 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2408
2409 if (rc)
2410 ata_port_disable(ap);
2411 else
2412 wait_for_completion(&wait);
2413
2414 DPRINTK("EXIT\n");
2415}
2416
2417/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002418 * ata_sg_clean - Unmap DMA memory associated with command
2419 * @qc: Command containing DMA memory to be released
2420 *
2421 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 *
2423 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002424 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 */
2426
2427static void ata_sg_clean(struct ata_queued_cmd *qc)
2428{
2429 struct ata_port *ap = qc->ap;
2430 struct scatterlist *sg = qc->sg;
2431 int dir = qc->dma_dir;
2432
2433 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2434 assert(sg != NULL);
2435
2436 if (qc->flags & ATA_QCFLAG_SINGLE)
2437 assert(qc->n_elem == 1);
2438
2439 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2440
2441 if (qc->flags & ATA_QCFLAG_SG)
2442 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2443 else
2444 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2445 sg_dma_len(&sg[0]), dir);
2446
2447 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2448 qc->sg = NULL;
2449}
2450
2451/**
2452 * ata_fill_sg - Fill PCI IDE PRD table
2453 * @qc: Metadata associated with taskfile to be transferred
2454 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002455 * Fill PCI IDE PRD (scatter-gather) table with segments
2456 * associated with the current disk command.
2457 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002459 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 *
2461 */
2462static void ata_fill_sg(struct ata_queued_cmd *qc)
2463{
2464 struct scatterlist *sg = qc->sg;
2465 struct ata_port *ap = qc->ap;
2466 unsigned int idx, nelem;
2467
2468 assert(sg != NULL);
2469 assert(qc->n_elem > 0);
2470
2471 idx = 0;
2472 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2473 u32 addr, offset;
2474 u32 sg_len, len;
2475
2476 /* determine if physical DMA addr spans 64K boundary.
2477 * Note h/w doesn't support 64-bit, so we unconditionally
2478 * truncate dma_addr_t to u32.
2479 */
2480 addr = (u32) sg_dma_address(sg);
2481 sg_len = sg_dma_len(sg);
2482
2483 while (sg_len) {
2484 offset = addr & 0xffff;
2485 len = sg_len;
2486 if ((offset + sg_len) > 0x10000)
2487 len = 0x10000 - offset;
2488
2489 ap->prd[idx].addr = cpu_to_le32(addr);
2490 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2491 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2492
2493 idx++;
2494 sg_len -= len;
2495 addr += len;
2496 }
2497 }
2498
2499 if (idx)
2500 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2501}
2502/**
2503 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2504 * @qc: Metadata associated with taskfile to check
2505 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002506 * Allow low-level driver to filter ATA PACKET commands, returning
2507 * a status indicating whether or not it is OK to use DMA for the
2508 * supplied PACKET command.
2509 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002511 * spin_lock_irqsave(host_set lock)
2512 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 * RETURNS: 0 when ATAPI DMA can be used
2514 * nonzero otherwise
2515 */
2516int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2517{
2518 struct ata_port *ap = qc->ap;
2519 int rc = 0; /* Assume ATAPI DMA is OK by default */
2520
2521 if (ap->ops->check_atapi_dma)
2522 rc = ap->ops->check_atapi_dma(qc);
2523
2524 return rc;
2525}
2526/**
2527 * ata_qc_prep - Prepare taskfile for submission
2528 * @qc: Metadata associated with taskfile to be prepared
2529 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002530 * Prepare ATA taskfile for submission.
2531 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 * LOCKING:
2533 * spin_lock_irqsave(host_set lock)
2534 */
2535void ata_qc_prep(struct ata_queued_cmd *qc)
2536{
2537 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2538 return;
2539
2540 ata_fill_sg(qc);
2541}
2542
Jeff Garzik0cba6322005-05-30 19:49:12 -04002543/**
2544 * ata_sg_init_one - Associate command with memory buffer
2545 * @qc: Command to be associated
2546 * @buf: Memory buffer
2547 * @buflen: Length of memory buffer, in bytes.
2548 *
2549 * Initialize the data-related elements of queued_cmd @qc
2550 * to point to a single memory buffer, @buf of byte length @buflen.
2551 *
2552 * LOCKING:
2553 * spin_lock_irqsave(host_set lock)
2554 */
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2557{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 qc->flags |= ATA_QCFLAG_SINGLE;
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 qc->sg = &qc->sgent;
2561 qc->n_elem = 1;
2562 qc->buf_virt = buf;
David Hardeman378f0582005-09-17 17:55:31 +10002563 sg_init_one(qc->sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
Jeff Garzik0cba6322005-05-30 19:49:12 -04002566/**
2567 * ata_sg_init - Associate command with scatter-gather table.
2568 * @qc: Command to be associated
2569 * @sg: Scatter-gather table.
2570 * @n_elem: Number of elements in s/g table.
2571 *
2572 * Initialize the data-related elements of queued_cmd @qc
2573 * to point to a scatter-gather table @sg, containing @n_elem
2574 * elements.
2575 *
2576 * LOCKING:
2577 * spin_lock_irqsave(host_set lock)
2578 */
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2581 unsigned int n_elem)
2582{
2583 qc->flags |= ATA_QCFLAG_SG;
2584 qc->sg = sg;
2585 qc->n_elem = n_elem;
2586}
2587
2588/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002589 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2590 * @qc: Command with memory buffer to be mapped.
2591 *
2592 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 *
2594 * LOCKING:
2595 * spin_lock_irqsave(host_set lock)
2596 *
2597 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002598 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 */
2600
2601static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2602{
2603 struct ata_port *ap = qc->ap;
2604 int dir = qc->dma_dir;
2605 struct scatterlist *sg = qc->sg;
2606 dma_addr_t dma_address;
2607
2608 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002609 sg->length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (dma_mapping_error(dma_address))
2611 return -1;
2612
2613 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002614 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2617 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2618
2619 return 0;
2620}
2621
2622/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002623 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2624 * @qc: Command with scatter-gather table to be mapped.
2625 *
2626 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 *
2628 * LOCKING:
2629 * spin_lock_irqsave(host_set lock)
2630 *
2631 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002632 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 *
2634 */
2635
2636static int ata_sg_setup(struct ata_queued_cmd *qc)
2637{
2638 struct ata_port *ap = qc->ap;
2639 struct scatterlist *sg = qc->sg;
2640 int n_elem, dir;
2641
2642 VPRINTK("ENTER, ata%u\n", ap->id);
2643 assert(qc->flags & ATA_QCFLAG_SG);
2644
2645 dir = qc->dma_dir;
2646 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2647 if (n_elem < 1)
2648 return -1;
2649
2650 DPRINTK("%d sg elements mapped\n", n_elem);
2651
2652 qc->n_elem = n_elem;
2653
2654 return 0;
2655}
2656
2657/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002658 * ata_poll_qc_complete - turn irq back on and finish qc
2659 * @qc: Command to complete
2660 * @drv_stat: ATA status register content
2661 *
2662 * LOCKING:
2663 * None. (grabs host lock)
2664 */
2665
Jeff Garzika7dac442005-10-30 04:44:42 -05002666void ata_poll_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
Tejun Heo40e8c822005-08-22 17:12:45 +09002667{
2668 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002669 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002670
Jeff Garzikb8f61532005-08-25 22:01:20 -04002671 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002672 ap->flags &= ~ATA_FLAG_NOINTR;
2673 ata_irq_on(ap);
Jeff Garzika7dac442005-10-30 04:44:42 -05002674 ata_qc_complete(qc, err_mask);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002675 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002676}
2677
2678/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002680 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 *
2682 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002683 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 *
2685 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002686 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 */
2688
2689static unsigned long ata_pio_poll(struct ata_port *ap)
2690{
2691 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002692 unsigned int poll_state = HSM_ST_UNKNOWN;
2693 unsigned int reg_state = HSM_ST_UNKNOWN;
2694 const unsigned int tmout_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Albert Lee14be71f2005-09-27 17:36:35 +08002696 switch (ap->hsm_task_state) {
2697 case HSM_ST:
2698 case HSM_ST_POLL:
2699 poll_state = HSM_ST_POLL;
2700 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002702 case HSM_ST_LAST:
2703 case HSM_ST_LAST_POLL:
2704 poll_state = HSM_ST_LAST_POLL;
2705 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 break;
2707 default:
2708 BUG();
2709 break;
2710 }
2711
2712 status = ata_chk_status(ap);
2713 if (status & ATA_BUSY) {
2714 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002715 ap->hsm_task_state = tmout_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return 0;
2717 }
Albert Lee14be71f2005-09-27 17:36:35 +08002718 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 return ATA_SHORT_PAUSE;
2720 }
2721
Albert Lee14be71f2005-09-27 17:36:35 +08002722 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return 0;
2724}
2725
2726/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002727 * ata_pio_complete - check if drive is busy or idle
2728 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 *
2730 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002731 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002732 *
2733 * RETURNS:
2734 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 */
2736
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002737static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 struct ata_queued_cmd *qc;
2740 u8 drv_stat;
2741
2742 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002743 * This is purely heuristic. This is a fast path. Sometimes when
2744 * we enter, BSY will be cleared in a chk-status or two. If not,
2745 * the drive is probably seeking or something. Snooze for a couple
2746 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002747 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 */
2749 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2750 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2751 msleep(2);
2752 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2753 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002754 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
2758 }
2759
2760 drv_stat = ata_wait_idle(ap);
2761 if (!ata_ok(drv_stat)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002762 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
2765
2766 qc = ata_qc_from_tag(ap, ap->active_tag);
2767 assert(qc != NULL);
2768
Albert Lee14be71f2005-09-27 17:36:35 +08002769 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Jeff Garzika7dac442005-10-30 04:44:42 -05002771 ata_poll_qc_complete(qc, 0);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002772
2773 /* another command may start at this point */
2774
2775 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
Edward Falk0baab862005-06-02 18:17:13 -04002778
2779/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002780 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002781 * @buf: Buffer to swap
2782 * @buf_words: Number of 16-bit words in buffer.
2783 *
2784 * Swap halves of 16-bit words if needed to convert from
2785 * little-endian byte order to native cpu byte order, or
2786 * vice-versa.
2787 *
2788 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002789 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791void swap_buf_le16(u16 *buf, unsigned int buf_words)
2792{
2793#ifdef __BIG_ENDIAN
2794 unsigned int i;
2795
2796 for (i = 0; i < buf_words; i++)
2797 buf[i] = le16_to_cpu(buf[i]);
2798#endif /* __BIG_ENDIAN */
2799}
2800
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002801/**
2802 * ata_mmio_data_xfer - Transfer data by MMIO
2803 * @ap: port to read/write
2804 * @buf: data buffer
2805 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002806 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002807 *
2808 * Transfer data from/to the device data register by MMIO.
2809 *
2810 * LOCKING:
2811 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002812 */
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2815 unsigned int buflen, int write_data)
2816{
2817 unsigned int i;
2818 unsigned int words = buflen >> 1;
2819 u16 *buf16 = (u16 *) buf;
2820 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2821
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002822 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (write_data) {
2824 for (i = 0; i < words; i++)
2825 writew(le16_to_cpu(buf16[i]), mmio);
2826 } else {
2827 for (i = 0; i < words; i++)
2828 buf16[i] = cpu_to_le16(readw(mmio));
2829 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002830
2831 /* Transfer trailing 1 byte, if any. */
2832 if (unlikely(buflen & 0x01)) {
2833 u16 align_buf[1] = { 0 };
2834 unsigned char *trailing_buf = buf + buflen - 1;
2835
2836 if (write_data) {
2837 memcpy(align_buf, trailing_buf, 1);
2838 writew(le16_to_cpu(align_buf[0]), mmio);
2839 } else {
2840 align_buf[0] = cpu_to_le16(readw(mmio));
2841 memcpy(trailing_buf, align_buf, 1);
2842 }
2843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
2845
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002846/**
2847 * ata_pio_data_xfer - Transfer data by PIO
2848 * @ap: port to read/write
2849 * @buf: data buffer
2850 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002851 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002852 *
2853 * Transfer data from/to the device data register by PIO.
2854 *
2855 * LOCKING:
2856 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002857 */
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2860 unsigned int buflen, int write_data)
2861{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002862 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002864 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002866 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002868 insw(ap->ioaddr.data_addr, buf, words);
2869
2870 /* Transfer trailing 1 byte, if any. */
2871 if (unlikely(buflen & 0x01)) {
2872 u16 align_buf[1] = { 0 };
2873 unsigned char *trailing_buf = buf + buflen - 1;
2874
2875 if (write_data) {
2876 memcpy(align_buf, trailing_buf, 1);
2877 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2878 } else {
2879 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2880 memcpy(trailing_buf, align_buf, 1);
2881 }
2882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002885/**
2886 * ata_data_xfer - Transfer data from/to the data register.
2887 * @ap: port to read/write
2888 * @buf: data buffer
2889 * @buflen: buffer length
2890 * @do_write: read/write
2891 *
2892 * Transfer data from/to the device data register.
2893 *
2894 * LOCKING:
2895 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002896 */
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2899 unsigned int buflen, int do_write)
2900{
2901 if (ap->flags & ATA_FLAG_MMIO)
2902 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2903 else
2904 ata_pio_data_xfer(ap, buf, buflen, do_write);
2905}
2906
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002907/**
2908 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2909 * @qc: Command on going
2910 *
2911 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2912 *
2913 * LOCKING:
2914 * Inherited from caller.
2915 */
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917static void ata_pio_sector(struct ata_queued_cmd *qc)
2918{
2919 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2920 struct scatterlist *sg = qc->sg;
2921 struct ata_port *ap = qc->ap;
2922 struct page *page;
2923 unsigned int offset;
2924 unsigned char *buf;
2925
2926 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08002927 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 page = sg[qc->cursg].page;
2930 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2931
2932 /* get the current page and offset */
2933 page = nth_page(page, (offset >> PAGE_SHIFT));
2934 offset %= PAGE_SIZE;
2935
2936 buf = kmap(page) + offset;
2937
2938 qc->cursect++;
2939 qc->cursg_ofs++;
2940
Albert Lee32529e02005-05-26 03:49:42 -04002941 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 qc->cursg++;
2943 qc->cursg_ofs = 0;
2944 }
2945
2946 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2947
2948 /* do the actual data transfer */
2949 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2950 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2951
2952 kunmap(page);
2953}
2954
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002955/**
2956 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2957 * @qc: Command on going
2958 * @bytes: number of bytes
2959 *
2960 * Transfer Transfer data from/to the ATAPI device.
2961 *
2962 * LOCKING:
2963 * Inherited from caller.
2964 *
2965 */
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2968{
2969 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2970 struct scatterlist *sg = qc->sg;
2971 struct ata_port *ap = qc->ap;
2972 struct page *page;
2973 unsigned char *buf;
2974 unsigned int offset, count;
2975
Albert Lee563a6e12005-08-12 14:17:50 +08002976 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08002977 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08002980 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002981 /*
Albert Lee563a6e12005-08-12 14:17:50 +08002982 * The end of qc->sg is reached and the device expects
2983 * more data to transfer. In order not to overrun qc->sg
2984 * and fulfill length specified in the byte count register,
2985 * - for read case, discard trailing data from the device
2986 * - for write case, padding zero data to the device
2987 */
2988 u16 pad_buf[1] = { 0 };
2989 unsigned int words = bytes >> 1;
2990 unsigned int i;
2991
2992 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002993 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08002994 ap->id, bytes);
2995
2996 for (i = 0; i < words; i++)
2997 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2998
Albert Lee14be71f2005-09-27 17:36:35 +08002999 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003000 return;
3001 }
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 sg = &qc->sg[qc->cursg];
3004
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 page = sg->page;
3006 offset = sg->offset + qc->cursg_ofs;
3007
3008 /* get the current page and offset */
3009 page = nth_page(page, (offset >> PAGE_SHIFT));
3010 offset %= PAGE_SIZE;
3011
Albert Lee6952df02005-06-06 15:56:03 +08003012 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003013 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 /* don't cross page boundaries */
3016 count = min(count, (unsigned int)PAGE_SIZE - offset);
3017
3018 buf = kmap(page) + offset;
3019
3020 bytes -= count;
3021 qc->curbytes += count;
3022 qc->cursg_ofs += count;
3023
Albert Lee32529e02005-05-26 03:49:42 -04003024 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 qc->cursg++;
3026 qc->cursg_ofs = 0;
3027 }
3028
3029 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3030
3031 /* do the actual data transfer */
3032 ata_data_xfer(ap, buf, count, do_write);
3033
3034 kunmap(page);
3035
Albert Lee563a6e12005-08-12 14:17:50 +08003036 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003040/**
3041 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3042 * @qc: Command on going
3043 *
3044 * Transfer Transfer data from/to the ATAPI device.
3045 *
3046 * LOCKING:
3047 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003048 */
3049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3051{
3052 struct ata_port *ap = qc->ap;
3053 struct ata_device *dev = qc->dev;
3054 unsigned int ireason, bc_lo, bc_hi, bytes;
3055 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3056
3057 ap->ops->tf_read(ap, &qc->tf);
3058 ireason = qc->tf.nsect;
3059 bc_lo = qc->tf.lbam;
3060 bc_hi = qc->tf.lbah;
3061 bytes = (bc_hi << 8) | bc_lo;
3062
3063 /* shall be cleared to zero, indicating xfer of data */
3064 if (ireason & (1 << 0))
3065 goto err_out;
3066
3067 /* make sure transfer direction matches expected */
3068 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3069 if (do_write != i_write)
3070 goto err_out;
3071
3072 __atapi_pio_bytes(qc, bytes);
3073
3074 return;
3075
3076err_out:
3077 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3078 ap->id, dev->devno);
Albert Lee14be71f2005-09-27 17:36:35 +08003079 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080}
3081
3082/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003083 * ata_pio_block - start PIO on a block
3084 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 *
3086 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003087 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 */
3089
3090static void ata_pio_block(struct ata_port *ap)
3091{
3092 struct ata_queued_cmd *qc;
3093 u8 status;
3094
3095 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003096 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 * Sometimes when we enter, BSY will be cleared in
3098 * a chk-status or two. If not, the drive is probably seeking
3099 * or something. Snooze for a couple msecs, then
3100 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003101 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 */
3103 status = ata_busy_wait(ap, ATA_BUSY, 5);
3104 if (status & ATA_BUSY) {
3105 msleep(2);
3106 status = ata_busy_wait(ap, ATA_BUSY, 10);
3107 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003108 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3110 return;
3111 }
3112 }
3113
3114 qc = ata_qc_from_tag(ap, ap->active_tag);
3115 assert(qc != NULL);
3116
3117 if (is_atapi_taskfile(&qc->tf)) {
3118 /* no more data to transfer or unsupported ATAPI command */
3119 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003120 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return;
3122 }
3123
3124 atapi_pio_bytes(qc);
3125 } else {
3126 /* handle BSY=0, DRQ=0 as error */
3127 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003128 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 return;
3130 }
3131
3132 ata_pio_sector(qc);
3133 }
3134}
3135
3136static void ata_pio_error(struct ata_port *ap)
3137{
3138 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003139
3140 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142 qc = ata_qc_from_tag(ap, ap->active_tag);
3143 assert(qc != NULL);
3144
Albert Lee14be71f2005-09-27 17:36:35 +08003145 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Jeff Garzika7dac442005-10-30 04:44:42 -05003147 ata_poll_qc_complete(qc, AC_ERR_ATA_BUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
3150static void ata_pio_task(void *_data)
3151{
3152 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003153 unsigned long timeout;
3154 int qc_completed;
3155
3156fsm_start:
3157 timeout = 0;
3158 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Albert Lee14be71f2005-09-27 17:36:35 +08003160 switch (ap->hsm_task_state) {
3161 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 return;
3163
Albert Lee14be71f2005-09-27 17:36:35 +08003164 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 ata_pio_block(ap);
3166 break;
3167
Albert Lee14be71f2005-09-27 17:36:35 +08003168 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003169 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 break;
3171
Albert Lee14be71f2005-09-27 17:36:35 +08003172 case HSM_ST_POLL:
3173 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 timeout = ata_pio_poll(ap);
3175 break;
3176
Albert Lee14be71f2005-09-27 17:36:35 +08003177 case HSM_ST_TMOUT:
3178 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 ata_pio_error(ap);
3180 return;
3181 }
3182
3183 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003184 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3185 else if (!qc_completed)
3186 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189/**
3190 * ata_qc_timeout - Handle timeout of queued command
3191 * @qc: Command that timed out
3192 *
3193 * Some part of the kernel (currently, only the SCSI layer)
3194 * has noticed that the active command on port @ap has not
3195 * completed after a specified length of time. Handle this
3196 * condition by disabling DMA (if necessary) and completing
3197 * transactions, with error if necessary.
3198 *
3199 * This also handles the case of the "lost interrupt", where
3200 * for some reason (possibly hardware bug, possibly driver bug)
3201 * an interrupt was not delivered to the driver, even though the
3202 * transaction completed successfully.
3203 *
3204 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003205 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 */
3207
3208static void ata_qc_timeout(struct ata_queued_cmd *qc)
3209{
3210 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003211 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 struct ata_device *dev = qc->dev;
3213 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003214 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
3216 DPRINTK("ENTER\n");
3217
3218 /* FIXME: doesn't this conflict with timeout handling? */
3219 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3220 struct scsi_cmnd *cmd = qc->scsicmd;
3221
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02003222 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 /* finish completing original command */
Jeff Garzikb8f61532005-08-25 22:01:20 -04003225 spin_lock_irqsave(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 __ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003227 spin_unlock_irqrestore(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
3229 atapi_request_sense(ap, dev, cmd);
3230
3231 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3232 scsi_finish_command(cmd);
3233
3234 goto out;
3235 }
3236 }
3237
Jeff Garzikb8f61532005-08-25 22:01:20 -04003238 spin_lock_irqsave(&host_set->lock, flags);
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 /* hack alert! We cannot use the supplied completion
3241 * function from inside the ->eh_strategy_handler() thread.
3242 * libata is the only user of ->eh_strategy_handler() in
3243 * any kernel, so the default scsi_done() assumes it is
3244 * not being called from the SCSI EH.
3245 */
3246 qc->scsidone = scsi_finish_command;
3247
3248 switch (qc->tf.protocol) {
3249
3250 case ATA_PROT_DMA:
3251 case ATA_PROT_ATAPI_DMA:
3252 host_stat = ap->ops->bmdma_status(ap);
3253
3254 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003255 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
3257 /* fall through */
3258
3259 default:
3260 ata_altstatus(ap);
3261 drv_stat = ata_chk_status(ap);
3262
3263 /* ack bmdma irq events */
3264 ap->ops->irq_clear(ap);
3265
3266 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3267 ap->id, qc->tf.command, drv_stat, host_stat);
3268
3269 /* complete taskfile transaction */
Jeff Garzika7dac442005-10-30 04:44:42 -05003270 ata_qc_complete(qc, ac_err_mask(drv_stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 break;
3272 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003273
3274 spin_unlock_irqrestore(&host_set->lock, flags);
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276out:
3277 DPRINTK("EXIT\n");
3278}
3279
3280/**
3281 * ata_eng_timeout - Handle timeout of queued command
3282 * @ap: Port on which timed-out command is active
3283 *
3284 * Some part of the kernel (currently, only the SCSI layer)
3285 * has noticed that the active command on port @ap has not
3286 * completed after a specified length of time. Handle this
3287 * condition by disabling DMA (if necessary) and completing
3288 * transactions, with error if necessary.
3289 *
3290 * This also handles the case of the "lost interrupt", where
3291 * for some reason (possibly hardware bug, possibly driver bug)
3292 * an interrupt was not delivered to the driver, even though the
3293 * transaction completed successfully.
3294 *
3295 * LOCKING:
3296 * Inherited from SCSI layer (none, can sleep)
3297 */
3298
3299void ata_eng_timeout(struct ata_port *ap)
3300{
3301 struct ata_queued_cmd *qc;
3302
3303 DPRINTK("ENTER\n");
3304
3305 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003306 if (qc)
3307 ata_qc_timeout(qc);
3308 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3310 ap->id);
3311 goto out;
3312 }
3313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314out:
3315 DPRINTK("EXIT\n");
3316}
3317
3318/**
3319 * ata_qc_new - Request an available ATA command, for queueing
3320 * @ap: Port associated with device @dev
3321 * @dev: Device from whom we request an available command structure
3322 *
3323 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003324 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 */
3326
3327static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3328{
3329 struct ata_queued_cmd *qc = NULL;
3330 unsigned int i;
3331
3332 for (i = 0; i < ATA_MAX_QUEUE; i++)
3333 if (!test_and_set_bit(i, &ap->qactive)) {
3334 qc = ata_qc_from_tag(ap, i);
3335 break;
3336 }
3337
3338 if (qc)
3339 qc->tag = i;
3340
3341 return qc;
3342}
3343
3344/**
3345 * ata_qc_new_init - Request an available ATA command, and initialize it
3346 * @ap: Port associated with device @dev
3347 * @dev: Device from whom we request an available command structure
3348 *
3349 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003350 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 */
3352
3353struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3354 struct ata_device *dev)
3355{
3356 struct ata_queued_cmd *qc;
3357
3358 qc = ata_qc_new(ap);
3359 if (qc) {
3360 qc->sg = NULL;
3361 qc->flags = 0;
3362 qc->scsicmd = NULL;
3363 qc->ap = ap;
3364 qc->dev = dev;
3365 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3366 qc->nsect = 0;
3367 qc->nbytes = qc->curbytes = 0;
3368
3369 ata_tf_init(ap, &qc->tf, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 }
3371
3372 return qc;
3373}
3374
Jeff Garzika7dac442005-10-30 04:44:42 -05003375int ata_qc_complete_noop(struct ata_queued_cmd *qc, unsigned int err_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
3377 return 0;
3378}
3379
3380static void __ata_qc_complete(struct ata_queued_cmd *qc)
3381{
3382 struct ata_port *ap = qc->ap;
3383 unsigned int tag, do_clear = 0;
3384
3385 qc->flags = 0;
3386 tag = qc->tag;
3387 if (likely(ata_tag_valid(tag))) {
3388 if (tag == ap->active_tag)
3389 ap->active_tag = ATA_TAG_POISON;
3390 qc->tag = ATA_TAG_POISON;
3391 do_clear = 1;
3392 }
3393
3394 if (qc->waiting) {
3395 struct completion *waiting = qc->waiting;
3396 qc->waiting = NULL;
3397 complete(waiting);
3398 }
3399
3400 if (likely(do_clear))
3401 clear_bit(tag, &ap->qactive);
3402}
3403
3404/**
3405 * ata_qc_free - free unused ata_queued_cmd
3406 * @qc: Command to complete
3407 *
3408 * Designed to free unused ata_queued_cmd object
3409 * in case something prevents using it.
3410 *
3411 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003412 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 */
3414void ata_qc_free(struct ata_queued_cmd *qc)
3415{
3416 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3417 assert(qc->waiting == NULL); /* nothing should be waiting */
3418
3419 __ata_qc_complete(qc);
3420}
3421
3422/**
3423 * ata_qc_complete - Complete an active ATA command
3424 * @qc: Command to complete
Jeff Garzik0cba6322005-05-30 19:49:12 -04003425 * @drv_stat: ATA Status register contents
3426 *
3427 * Indicate to the mid and upper layers that an ATA
3428 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 *
3430 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003431 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 */
3433
Jeff Garzika7dac442005-10-30 04:44:42 -05003434void ata_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435{
3436 int rc;
3437
3438 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3439 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3440
3441 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3442 ata_sg_clean(qc);
3443
Albert Lee3f3791d2005-08-16 14:25:38 +08003444 /* atapi: mark qc as inactive to prevent the interrupt handler
3445 * from completing the command twice later, before the error handler
3446 * is called. (when rc != 0 and atapi request sense is needed)
3447 */
3448 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 /* call completion callback */
Jeff Garzika7dac442005-10-30 04:44:42 -05003451 rc = qc->complete_fn(qc, err_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
3453 /* if callback indicates not to complete command (non-zero),
3454 * return immediately
3455 */
3456 if (rc != 0)
3457 return;
3458
3459 __ata_qc_complete(qc);
3460
3461 VPRINTK("EXIT\n");
3462}
3463
3464static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3465{
3466 struct ata_port *ap = qc->ap;
3467
3468 switch (qc->tf.protocol) {
3469 case ATA_PROT_DMA:
3470 case ATA_PROT_ATAPI_DMA:
3471 return 1;
3472
3473 case ATA_PROT_ATAPI:
3474 case ATA_PROT_PIO:
3475 case ATA_PROT_PIO_MULT:
3476 if (ap->flags & ATA_FLAG_PIO_DMA)
3477 return 1;
3478
3479 /* fall through */
3480
3481 default:
3482 return 0;
3483 }
3484
3485 /* never reached */
3486}
3487
3488/**
3489 * ata_qc_issue - issue taskfile to device
3490 * @qc: command to issue to device
3491 *
3492 * Prepare an ATA command to submission to device.
3493 * This includes mapping the data into a DMA-able
3494 * area, filling in the S/G table, and finally
3495 * writing the taskfile to hardware, starting the command.
3496 *
3497 * LOCKING:
3498 * spin_lock_irqsave(host_set lock)
3499 *
3500 * RETURNS:
3501 * Zero on success, negative on error.
3502 */
3503
3504int ata_qc_issue(struct ata_queued_cmd *qc)
3505{
3506 struct ata_port *ap = qc->ap;
3507
3508 if (ata_should_dma_map(qc)) {
3509 if (qc->flags & ATA_QCFLAG_SG) {
3510 if (ata_sg_setup(qc))
3511 goto err_out;
3512 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3513 if (ata_sg_setup_one(qc))
3514 goto err_out;
3515 }
3516 } else {
3517 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3518 }
3519
3520 ap->ops->qc_prep(qc);
3521
3522 qc->ap->active_tag = qc->tag;
3523 qc->flags |= ATA_QCFLAG_ACTIVE;
3524
3525 return ap->ops->qc_issue(qc);
3526
3527err_out:
3528 return -1;
3529}
3530
Edward Falk0baab862005-06-02 18:17:13 -04003531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532/**
3533 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3534 * @qc: command to issue to device
3535 *
3536 * Using various libata functions and hooks, this function
3537 * starts an ATA command. ATA commands are grouped into
3538 * classes called "protocols", and issuing each type of protocol
3539 * is slightly different.
3540 *
Edward Falk0baab862005-06-02 18:17:13 -04003541 * May be used as the qc_issue() entry in ata_port_operations.
3542 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 * LOCKING:
3544 * spin_lock_irqsave(host_set lock)
3545 *
3546 * RETURNS:
3547 * Zero on success, negative on error.
3548 */
3549
3550int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3551{
3552 struct ata_port *ap = qc->ap;
3553
3554 ata_dev_select(ap, qc->dev->devno, 1, 0);
3555
3556 switch (qc->tf.protocol) {
3557 case ATA_PROT_NODATA:
3558 ata_tf_to_host_nolock(ap, &qc->tf);
3559 break;
3560
3561 case ATA_PROT_DMA:
3562 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3563 ap->ops->bmdma_setup(qc); /* set up bmdma */
3564 ap->ops->bmdma_start(qc); /* initiate bmdma */
3565 break;
3566
3567 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3568 ata_qc_set_polling(qc);
3569 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003570 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 queue_work(ata_wq, &ap->pio_task);
3572 break;
3573
3574 case ATA_PROT_ATAPI:
3575 ata_qc_set_polling(qc);
3576 ata_tf_to_host_nolock(ap, &qc->tf);
3577 queue_work(ata_wq, &ap->packet_task);
3578 break;
3579
3580 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003581 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 ata_tf_to_host_nolock(ap, &qc->tf);
3583 queue_work(ata_wq, &ap->packet_task);
3584 break;
3585
3586 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003587 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3589 ap->ops->bmdma_setup(qc); /* set up bmdma */
3590 queue_work(ata_wq, &ap->packet_task);
3591 break;
3592
3593 default:
3594 WARN_ON(1);
3595 return -1;
3596 }
3597
3598 return 0;
3599}
3600
3601/**
Edward Falk0baab862005-06-02 18:17:13 -04003602 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 * @qc: Info associated with this ATA transaction.
3604 *
3605 * LOCKING:
3606 * spin_lock_irqsave(host_set lock)
3607 */
3608
3609static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3610{
3611 struct ata_port *ap = qc->ap;
3612 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3613 u8 dmactl;
3614 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3615
3616 /* load PRD table addr. */
3617 mb(); /* make sure PRD table writes are visible to controller */
3618 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3619
3620 /* specify data direction, triple-check start bit is clear */
3621 dmactl = readb(mmio + ATA_DMA_CMD);
3622 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3623 if (!rw)
3624 dmactl |= ATA_DMA_WR;
3625 writeb(dmactl, mmio + ATA_DMA_CMD);
3626
3627 /* issue r/w command */
3628 ap->ops->exec_command(ap, &qc->tf);
3629}
3630
3631/**
Alan Coxb73fc892005-08-26 16:03:19 +01003632 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 * @qc: Info associated with this ATA transaction.
3634 *
3635 * LOCKING:
3636 * spin_lock_irqsave(host_set lock)
3637 */
3638
3639static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3640{
3641 struct ata_port *ap = qc->ap;
3642 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3643 u8 dmactl;
3644
3645 /* start host DMA transaction */
3646 dmactl = readb(mmio + ATA_DMA_CMD);
3647 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3648
3649 /* Strictly, one may wish to issue a readb() here, to
3650 * flush the mmio write. However, control also passes
3651 * to the hardware at this point, and it will interrupt
3652 * us when we are to resume control. So, in effect,
3653 * we don't care when the mmio write flushes.
3654 * Further, a read of the DMA status register _immediately_
3655 * following the write may not be what certain flaky hardware
3656 * is expected, so I think it is best to not add a readb()
3657 * without first all the MMIO ATA cards/mobos.
3658 * Or maybe I'm just being paranoid.
3659 */
3660}
3661
3662/**
3663 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3664 * @qc: Info associated with this ATA transaction.
3665 *
3666 * LOCKING:
3667 * spin_lock_irqsave(host_set lock)
3668 */
3669
3670static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3671{
3672 struct ata_port *ap = qc->ap;
3673 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3674 u8 dmactl;
3675
3676 /* load PRD table addr. */
3677 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3678
3679 /* specify data direction, triple-check start bit is clear */
3680 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3681 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3682 if (!rw)
3683 dmactl |= ATA_DMA_WR;
3684 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3685
3686 /* issue r/w command */
3687 ap->ops->exec_command(ap, &qc->tf);
3688}
3689
3690/**
3691 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3692 * @qc: Info associated with this ATA transaction.
3693 *
3694 * LOCKING:
3695 * spin_lock_irqsave(host_set lock)
3696 */
3697
3698static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3699{
3700 struct ata_port *ap = qc->ap;
3701 u8 dmactl;
3702
3703 /* start host DMA transaction */
3704 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3705 outb(dmactl | ATA_DMA_START,
3706 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3707}
3708
Edward Falk0baab862005-06-02 18:17:13 -04003709
3710/**
3711 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3712 * @qc: Info associated with this ATA transaction.
3713 *
3714 * Writes the ATA_DMA_START flag to the DMA command register.
3715 *
3716 * May be used as the bmdma_start() entry in ata_port_operations.
3717 *
3718 * LOCKING:
3719 * spin_lock_irqsave(host_set lock)
3720 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721void ata_bmdma_start(struct ata_queued_cmd *qc)
3722{
3723 if (qc->ap->flags & ATA_FLAG_MMIO)
3724 ata_bmdma_start_mmio(qc);
3725 else
3726 ata_bmdma_start_pio(qc);
3727}
3728
Edward Falk0baab862005-06-02 18:17:13 -04003729
3730/**
3731 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3732 * @qc: Info associated with this ATA transaction.
3733 *
3734 * Writes address of PRD table to device's PRD Table Address
3735 * register, sets the DMA control register, and calls
3736 * ops->exec_command() to start the transfer.
3737 *
3738 * May be used as the bmdma_setup() entry in ata_port_operations.
3739 *
3740 * LOCKING:
3741 * spin_lock_irqsave(host_set lock)
3742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743void ata_bmdma_setup(struct ata_queued_cmd *qc)
3744{
3745 if (qc->ap->flags & ATA_FLAG_MMIO)
3746 ata_bmdma_setup_mmio(qc);
3747 else
3748 ata_bmdma_setup_pio(qc);
3749}
3750
Edward Falk0baab862005-06-02 18:17:13 -04003751
3752/**
3753 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003754 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003755 *
3756 * Clear interrupt and error flags in DMA status register.
3757 *
3758 * May be used as the irq_clear() entry in ata_port_operations.
3759 *
3760 * LOCKING:
3761 * spin_lock_irqsave(host_set lock)
3762 */
3763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764void ata_bmdma_irq_clear(struct ata_port *ap)
3765{
3766 if (ap->flags & ATA_FLAG_MMIO) {
3767 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3768 writeb(readb(mmio), mmio);
3769 } else {
3770 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3771 outb(inb(addr), addr);
3772 }
3773
3774}
3775
Edward Falk0baab862005-06-02 18:17:13 -04003776
3777/**
3778 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003779 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003780 *
3781 * Read and return BMDMA status register.
3782 *
3783 * May be used as the bmdma_status() entry in ata_port_operations.
3784 *
3785 * LOCKING:
3786 * spin_lock_irqsave(host_set lock)
3787 */
3788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789u8 ata_bmdma_status(struct ata_port *ap)
3790{
3791 u8 host_stat;
3792 if (ap->flags & ATA_FLAG_MMIO) {
3793 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3794 host_stat = readb(mmio + ATA_DMA_STATUS);
3795 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003796 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 return host_stat;
3798}
3799
Edward Falk0baab862005-06-02 18:17:13 -04003800
3801/**
3802 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003803 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003804 *
3805 * Clears the ATA_DMA_START flag in the dma control register
3806 *
3807 * May be used as the bmdma_stop() entry in ata_port_operations.
3808 *
3809 * LOCKING:
3810 * spin_lock_irqsave(host_set lock)
3811 */
3812
Alan Coxb73fc892005-08-26 16:03:19 +01003813void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
Alan Coxb73fc892005-08-26 16:03:19 +01003815 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 if (ap->flags & ATA_FLAG_MMIO) {
3817 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3818
3819 /* clear start/stop bit */
3820 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3821 mmio + ATA_DMA_CMD);
3822 } else {
3823 /* clear start/stop bit */
3824 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3825 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3826 }
3827
3828 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3829 ata_altstatus(ap); /* dummy read */
3830}
3831
3832/**
3833 * ata_host_intr - Handle host interrupt for given (port, task)
3834 * @ap: Port on which interrupt arrived (possibly...)
3835 * @qc: Taskfile currently active in engine
3836 *
3837 * Handle host interrupt for given queued command. Currently,
3838 * only DMA interrupts are handled. All other commands are
3839 * handled via polling with interrupts disabled (nIEN bit).
3840 *
3841 * LOCKING:
3842 * spin_lock_irqsave(host_set lock)
3843 *
3844 * RETURNS:
3845 * One if interrupt was handled, zero if not (shared irq).
3846 */
3847
3848inline unsigned int ata_host_intr (struct ata_port *ap,
3849 struct ata_queued_cmd *qc)
3850{
3851 u8 status, host_stat;
3852
3853 switch (qc->tf.protocol) {
3854
3855 case ATA_PROT_DMA:
3856 case ATA_PROT_ATAPI_DMA:
3857 case ATA_PROT_ATAPI:
3858 /* check status of DMA engine */
3859 host_stat = ap->ops->bmdma_status(ap);
3860 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3861
3862 /* if it's not our irq... */
3863 if (!(host_stat & ATA_DMA_INTR))
3864 goto idle_irq;
3865
3866 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003867 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
3869 /* fall through */
3870
3871 case ATA_PROT_ATAPI_NODATA:
3872 case ATA_PROT_NODATA:
3873 /* check altstatus */
3874 status = ata_altstatus(ap);
3875 if (status & ATA_BUSY)
3876 goto idle_irq;
3877
3878 /* check main status, clearing INTRQ */
3879 status = ata_chk_status(ap);
3880 if (unlikely(status & ATA_BUSY))
3881 goto idle_irq;
3882 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3883 ap->id, qc->tf.protocol, status);
3884
3885 /* ack bmdma irq events */
3886 ap->ops->irq_clear(ap);
3887
3888 /* complete taskfile transaction */
Jeff Garzika7dac442005-10-30 04:44:42 -05003889 ata_qc_complete(qc, ac_err_mask(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 break;
3891
3892 default:
3893 goto idle_irq;
3894 }
3895
3896 return 1; /* irq handled */
3897
3898idle_irq:
3899 ap->stats.idle_irq++;
3900
3901#ifdef ATA_IRQ_TRAP
3902 if ((ap->stats.idle_irq % 1000) == 0) {
3903 handled = 1;
3904 ata_irq_ack(ap, 0); /* debug trap */
3905 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3906 }
3907#endif
3908 return 0; /* irq not handled */
3909}
3910
3911/**
3912 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04003913 * @irq: irq line (unused)
3914 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 * @regs: unused
3916 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04003917 * Default interrupt handler for PCI IDE devices. Calls
3918 * ata_host_intr() for each port that is not disabled.
3919 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003921 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 *
3923 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003924 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 */
3926
3927irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3928{
3929 struct ata_host_set *host_set = dev_instance;
3930 unsigned int i;
3931 unsigned int handled = 0;
3932 unsigned long flags;
3933
3934 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3935 spin_lock_irqsave(&host_set->lock, flags);
3936
3937 for (i = 0; i < host_set->n_ports; i++) {
3938 struct ata_port *ap;
3939
3940 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09003941 if (ap &&
3942 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 struct ata_queued_cmd *qc;
3944
3945 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08003946 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3947 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 handled |= ata_host_intr(ap, qc);
3949 }
3950 }
3951
3952 spin_unlock_irqrestore(&host_set->lock, flags);
3953
3954 return IRQ_RETVAL(handled);
3955}
3956
3957/**
3958 * atapi_packet_task - Write CDB bytes to hardware
3959 * @_data: Port to which ATAPI device is attached.
3960 *
3961 * When device has indicated its readiness to accept
3962 * a CDB, this function is called. Send the CDB.
3963 * If DMA is to be performed, exit immediately.
3964 * Otherwise, we are in polling mode, so poll
3965 * status under operation succeeds or fails.
3966 *
3967 * LOCKING:
3968 * Kernel thread context (may sleep)
3969 */
3970
3971static void atapi_packet_task(void *_data)
3972{
3973 struct ata_port *ap = _data;
3974 struct ata_queued_cmd *qc;
3975 u8 status;
3976
3977 qc = ata_qc_from_tag(ap, ap->active_tag);
3978 assert(qc != NULL);
3979 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3980
3981 /* sleep-wait for BSY to clear */
3982 DPRINTK("busy wait\n");
3983 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
Jeff Garzika7dac442005-10-30 04:44:42 -05003984 goto err_out_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986 /* make sure DRQ is set */
3987 status = ata_chk_status(ap);
3988 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3989 goto err_out;
3990
3991 /* send SCSI cdb */
3992 DPRINTK("send cdb\n");
3993 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Tejun Heoc1389502005-08-22 14:59:24 +09003995 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3996 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3997 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Tejun Heoc1389502005-08-22 14:59:24 +09003999 /* Once we're done issuing command and kicking bmdma,
4000 * irq handler takes over. To not lose irq, we need
4001 * to clear NOINTR flag before sending cdb, but
4002 * interrupt handler shouldn't be invoked before we're
4003 * finished. Hence, the following locking.
4004 */
4005 spin_lock_irqsave(&ap->host_set->lock, flags);
4006 ap->flags &= ~ATA_FLAG_NOINTR;
4007 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4008 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4009 ap->ops->bmdma_start(qc); /* initiate bmdma */
4010 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4011 } else {
4012 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Tejun Heoc1389502005-08-22 14:59:24 +09004014 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004015 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 queue_work(ata_wq, &ap->pio_task);
4017 }
4018
4019 return;
4020
Jeff Garzika7dac442005-10-30 04:44:42 -05004021err_out_status:
4022 status = ata_chk_status(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023err_out:
Jeff Garzika7dac442005-10-30 04:44:42 -05004024 ata_poll_qc_complete(qc, __ac_err_mask(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
4026
Edward Falk0baab862005-06-02 18:17:13 -04004027
4028/**
4029 * ata_port_start - Set port up for dma.
4030 * @ap: Port to initialize
4031 *
4032 * Called just after data structures for each port are
4033 * initialized. Allocates space for PRD table.
4034 *
4035 * May be used as the port_start() entry in ata_port_operations.
4036 *
4037 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004038 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004039 */
4040
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041int ata_port_start (struct ata_port *ap)
4042{
4043 struct device *dev = ap->host_set->dev;
4044
4045 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4046 if (!ap->prd)
4047 return -ENOMEM;
4048
4049 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4050
4051 return 0;
4052}
4053
Edward Falk0baab862005-06-02 18:17:13 -04004054
4055/**
4056 * ata_port_stop - Undo ata_port_start()
4057 * @ap: Port to shut down
4058 *
4059 * Frees the PRD table.
4060 *
4061 * May be used as the port_stop() entry in ata_port_operations.
4062 *
4063 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004064 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004065 */
4066
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067void ata_port_stop (struct ata_port *ap)
4068{
4069 struct device *dev = ap->host_set->dev;
4070
4071 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4072}
4073
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004074void ata_host_stop (struct ata_host_set *host_set)
4075{
4076 if (host_set->mmio_base)
4077 iounmap(host_set->mmio_base);
4078}
4079
4080
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081/**
4082 * ata_host_remove - Unregister SCSI host structure with upper layers
4083 * @ap: Port to unregister
4084 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4085 *
4086 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004087 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 */
4089
4090static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4091{
4092 struct Scsi_Host *sh = ap->host;
4093
4094 DPRINTK("ENTER\n");
4095
4096 if (do_unregister)
4097 scsi_remove_host(sh);
4098
4099 ap->ops->port_stop(ap);
4100}
4101
4102/**
4103 * ata_host_init - Initialize an ata_port structure
4104 * @ap: Structure to initialize
4105 * @host: associated SCSI mid-layer structure
4106 * @host_set: Collection of hosts to which @ap belongs
4107 * @ent: Probe information provided by low-level driver
4108 * @port_no: Port number associated with this ata_port
4109 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004110 * Initialize a new ata_port structure, and its associated
4111 * scsi_host.
4112 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004114 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 */
4116
4117static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4118 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004119 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120{
4121 unsigned int i;
4122
4123 host->max_id = 16;
4124 host->max_lun = 1;
4125 host->max_channel = 1;
4126 host->unique_id = ata_unique_id++;
4127 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 scsi_assign_lock(host, &host_set->lock);
4130
4131 ap->flags = ATA_FLAG_PORT_DISABLED;
4132 ap->id = host->unique_id;
4133 ap->host = host;
4134 ap->ctl = ATA_DEVCTL_OBS;
4135 ap->host_set = host_set;
4136 ap->port_no = port_no;
4137 ap->hard_port_no =
4138 ent->legacy_mode ? ent->hard_port_no : port_no;
4139 ap->pio_mask = ent->pio_mask;
4140 ap->mwdma_mask = ent->mwdma_mask;
4141 ap->udma_mask = ent->udma_mask;
4142 ap->flags |= ent->host_flags;
4143 ap->ops = ent->port_ops;
4144 ap->cbl = ATA_CBL_NONE;
4145 ap->active_tag = ATA_TAG_POISON;
4146 ap->last_ctl = 0xFF;
4147
4148 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4149 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4150
4151 for (i = 0; i < ATA_MAX_DEVICES; i++)
4152 ap->device[i].devno = i;
4153
4154#ifdef ATA_IRQ_TRAP
4155 ap->stats.unhandled_irq = 1;
4156 ap->stats.idle_irq = 1;
4157#endif
4158
4159 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4160}
4161
4162/**
4163 * ata_host_add - Attach low-level ATA driver to system
4164 * @ent: Information provided by low-level driver
4165 * @host_set: Collections of ports to which we add
4166 * @port_no: Port number associated with this host
4167 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004168 * Attach low-level ATA driver to system.
4169 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004171 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 *
4173 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004174 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 */
4176
Jeff Garzik057ace52005-10-22 14:27:05 -04004177static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 struct ata_host_set *host_set,
4179 unsigned int port_no)
4180{
4181 struct Scsi_Host *host;
4182 struct ata_port *ap;
4183 int rc;
4184
4185 DPRINTK("ENTER\n");
4186 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4187 if (!host)
4188 return NULL;
4189
4190 ap = (struct ata_port *) &host->hostdata[0];
4191
4192 ata_host_init(ap, host, host_set, ent, port_no);
4193
4194 rc = ap->ops->port_start(ap);
4195 if (rc)
4196 goto err_out;
4197
4198 return ap;
4199
4200err_out:
4201 scsi_host_put(host);
4202 return NULL;
4203}
4204
4205/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004206 * ata_device_add - Register hardware device with ATA and SCSI layers
4207 * @ent: Probe information describing hardware device to be registered
4208 *
4209 * This function processes the information provided in the probe
4210 * information struct @ent, allocates the necessary ATA and SCSI
4211 * host information structures, initializes them, and registers
4212 * everything with requisite kernel subsystems.
4213 *
4214 * This function requests irqs, probes the ATA bus, and probes
4215 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 *
4217 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004218 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 *
4220 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004221 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 */
4223
Jeff Garzik057ace52005-10-22 14:27:05 -04004224int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
4226 unsigned int count = 0, i;
4227 struct device *dev = ent->dev;
4228 struct ata_host_set *host_set;
4229
4230 DPRINTK("ENTER\n");
4231 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004232 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4234 if (!host_set)
4235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 spin_lock_init(&host_set->lock);
4237
4238 host_set->dev = dev;
4239 host_set->n_ports = ent->n_ports;
4240 host_set->irq = ent->irq;
4241 host_set->mmio_base = ent->mmio_base;
4242 host_set->private_data = ent->private_data;
4243 host_set->ops = ent->port_ops;
4244
4245 /* register each port bound to this device */
4246 for (i = 0; i < ent->n_ports; i++) {
4247 struct ata_port *ap;
4248 unsigned long xfer_mode_mask;
4249
4250 ap = ata_host_add(ent, host_set, i);
4251 if (!ap)
4252 goto err_out;
4253
4254 host_set->ports[i] = ap;
4255 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4256 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4257 (ap->pio_mask << ATA_SHIFT_PIO);
4258
4259 /* print per-port info to dmesg */
4260 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4261 "bmdma 0x%lX irq %lu\n",
4262 ap->id,
4263 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4264 ata_mode_string(xfer_mode_mask),
4265 ap->ioaddr.cmd_addr,
4266 ap->ioaddr.ctl_addr,
4267 ap->ioaddr.bmdma_addr,
4268 ent->irq);
4269
4270 ata_chk_status(ap);
4271 host_set->ops->irq_clear(ap);
4272 count++;
4273 }
4274
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004275 if (!count)
4276 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
4278 /* obtain irq, that is shared between channels */
4279 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4280 DRV_NAME, host_set))
4281 goto err_out;
4282
4283 /* perform each probe synchronously */
4284 DPRINTK("probe begin\n");
4285 for (i = 0; i < count; i++) {
4286 struct ata_port *ap;
4287 int rc;
4288
4289 ap = host_set->ports[i];
4290
4291 DPRINTK("ata%u: probe begin\n", ap->id);
4292 rc = ata_bus_probe(ap);
4293 DPRINTK("ata%u: probe end\n", ap->id);
4294
4295 if (rc) {
4296 /* FIXME: do something useful here?
4297 * Current libata behavior will
4298 * tear down everything when
4299 * the module is removed
4300 * or the h/w is unplugged.
4301 */
4302 }
4303
4304 rc = scsi_add_host(ap->host, dev);
4305 if (rc) {
4306 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4307 ap->id);
4308 /* FIXME: do something useful here */
4309 /* FIXME: handle unconditional calls to
4310 * scsi_scan_host and ata_host_remove, below,
4311 * at the very least
4312 */
4313 }
4314 }
4315
4316 /* probes are done, now scan each port's disk(s) */
4317 DPRINTK("probe begin\n");
4318 for (i = 0; i < count; i++) {
4319 struct ata_port *ap = host_set->ports[i];
4320
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004321 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
4323
4324 dev_set_drvdata(dev, host_set);
4325
4326 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4327 return ent->n_ports; /* success */
4328
4329err_out:
4330 for (i = 0; i < count; i++) {
4331 ata_host_remove(host_set->ports[i], 1);
4332 scsi_host_put(host_set->ports[i]->host);
4333 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004334err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 kfree(host_set);
4336 VPRINTK("EXIT, returning 0\n");
4337 return 0;
4338}
4339
4340/**
Alan Cox17b14452005-09-15 15:44:00 +01004341 * ata_host_set_remove - PCI layer callback for device removal
4342 * @host_set: ATA host set that was removed
4343 *
4344 * Unregister all objects associated with this host set. Free those
4345 * objects.
4346 *
4347 * LOCKING:
4348 * Inherited from calling layer (may sleep).
4349 */
4350
Alan Cox17b14452005-09-15 15:44:00 +01004351void ata_host_set_remove(struct ata_host_set *host_set)
4352{
4353 struct ata_port *ap;
4354 unsigned int i;
4355
4356 for (i = 0; i < host_set->n_ports; i++) {
4357 ap = host_set->ports[i];
4358 scsi_remove_host(ap->host);
4359 }
4360
4361 free_irq(host_set->irq, host_set);
4362
4363 for (i = 0; i < host_set->n_ports; i++) {
4364 ap = host_set->ports[i];
4365
4366 ata_scsi_release(ap->host);
4367
4368 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4369 struct ata_ioports *ioaddr = &ap->ioaddr;
4370
4371 if (ioaddr->cmd_addr == 0x1f0)
4372 release_region(0x1f0, 8);
4373 else if (ioaddr->cmd_addr == 0x170)
4374 release_region(0x170, 8);
4375 }
4376
4377 scsi_host_put(ap->host);
4378 }
4379
4380 if (host_set->ops->host_stop)
4381 host_set->ops->host_stop(host_set);
4382
4383 kfree(host_set);
4384}
4385
4386/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 * ata_scsi_release - SCSI layer callback hook for host unload
4388 * @host: libata host to be unloaded
4389 *
4390 * Performs all duties necessary to shut down a libata port...
4391 * Kill port kthread, disable port, and release resources.
4392 *
4393 * LOCKING:
4394 * Inherited from SCSI layer.
4395 *
4396 * RETURNS:
4397 * One.
4398 */
4399
4400int ata_scsi_release(struct Scsi_Host *host)
4401{
4402 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4403
4404 DPRINTK("ENTER\n");
4405
4406 ap->ops->port_disable(ap);
4407 ata_host_remove(ap, 0);
4408
4409 DPRINTK("EXIT\n");
4410 return 1;
4411}
4412
4413/**
4414 * ata_std_ports - initialize ioaddr with standard port offsets.
4415 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004416 *
4417 * Utility function which initializes data_addr, error_addr,
4418 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4419 * device_addr, status_addr, and command_addr to standard offsets
4420 * relative to cmd_addr.
4421 *
4422 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 */
Edward Falk0baab862005-06-02 18:17:13 -04004424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425void ata_std_ports(struct ata_ioports *ioaddr)
4426{
4427 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4428 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4429 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4430 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4431 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4432 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4433 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4434 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4435 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4436 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4437}
4438
4439static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004440ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441{
4442 struct ata_probe_ent *probe_ent;
4443
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004444 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 if (!probe_ent) {
4446 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4447 kobject_name(&(dev->kobj)));
4448 return NULL;
4449 }
4450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 INIT_LIST_HEAD(&probe_ent->node);
4452 probe_ent->dev = dev;
4453
4454 probe_ent->sht = port->sht;
4455 probe_ent->host_flags = port->host_flags;
4456 probe_ent->pio_mask = port->pio_mask;
4457 probe_ent->mwdma_mask = port->mwdma_mask;
4458 probe_ent->udma_mask = port->udma_mask;
4459 probe_ent->port_ops = port->port_ops;
4460
4461 return probe_ent;
4462}
4463
Edward Falk0baab862005-06-02 18:17:13 -04004464
4465
Jeff Garzik374b1872005-08-30 05:42:52 -04004466#ifdef CONFIG_PCI
4467
4468void ata_pci_host_stop (struct ata_host_set *host_set)
4469{
4470 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4471
4472 pci_iounmap(pdev, host_set->mmio_base);
4473}
4474
Edward Falk0baab862005-06-02 18:17:13 -04004475/**
4476 * ata_pci_init_native_mode - Initialize native-mode driver
4477 * @pdev: pci device to be initialized
4478 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004479 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004480 *
4481 * Utility function which allocates and initializes an
4482 * ata_probe_ent structure for a standard dual-port
4483 * PIO-based IDE controller. The returned ata_probe_ent
4484 * structure can be passed to ata_device_add(). The returned
4485 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004486 *
4487 * The caller need only pass the address of the primary port, the
4488 * secondary will be deduced automatically. If the device has non
4489 * standard secondary port mappings this function can be called twice,
4490 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004491 */
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004494ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495{
4496 struct ata_probe_ent *probe_ent =
4497 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004498 int p = 0;
4499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 if (!probe_ent)
4501 return NULL;
4502
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 probe_ent->irq = pdev->irq;
4504 probe_ent->irq_flags = SA_SHIRQ;
4505
Alan Cox47a86592005-10-04 08:09:19 -04004506 if (ports & ATA_PORT_PRIMARY) {
4507 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4508 probe_ent->port[p].altstatus_addr =
4509 probe_ent->port[p].ctl_addr =
4510 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4511 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4512 ata_std_ports(&probe_ent->port[p]);
4513 p++;
4514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Alan Cox47a86592005-10-04 08:09:19 -04004516 if (ports & ATA_PORT_SECONDARY) {
4517 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4518 probe_ent->port[p].altstatus_addr =
4519 probe_ent->port[p].ctl_addr =
4520 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4521 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4522 ata_std_ports(&probe_ent->port[p]);
4523 p++;
4524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
Alan Cox47a86592005-10-04 08:09:19 -04004526 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 return probe_ent;
4528}
4529
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004530static 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 -07004531{
Alan Cox47a86592005-10-04 08:09:19 -04004532 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004534 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 if (!probe_ent)
4536 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004539 probe_ent->n_ports = 1;
4540 probe_ent->hard_port_no = port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Alan Cox47a86592005-10-04 08:09:19 -04004542 switch(port_num)
4543 {
4544 case 0:
4545 probe_ent->irq = 14;
4546 probe_ent->port[0].cmd_addr = 0x1f0;
4547 probe_ent->port[0].altstatus_addr =
4548 probe_ent->port[0].ctl_addr = 0x3f6;
4549 break;
4550 case 1:
4551 probe_ent->irq = 15;
4552 probe_ent->port[0].cmd_addr = 0x170;
4553 probe_ent->port[0].altstatus_addr =
4554 probe_ent->port[0].ctl_addr = 0x376;
4555 break;
4556 }
4557 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 return probe_ent;
4560}
4561
4562/**
4563 * ata_pci_init_one - Initialize/register PCI IDE host controller
4564 * @pdev: Controller to be initialized
4565 * @port_info: Information from low-level host driver
4566 * @n_ports: Number of ports attached to host controller
4567 *
Edward Falk0baab862005-06-02 18:17:13 -04004568 * This is a helper function which can be called from a driver's
4569 * xxx_init_one() probe function if the hardware uses traditional
4570 * IDE taskfile registers.
4571 *
4572 * This function calls pci_enable_device(), reserves its register
4573 * regions, sets the dma mask, enables bus master mode, and calls
4574 * ata_device_add()
4575 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 * LOCKING:
4577 * Inherited from PCI layer (may sleep).
4578 *
4579 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004580 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 */
4582
4583int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4584 unsigned int n_ports)
4585{
Alan Cox47a86592005-10-04 08:09:19 -04004586 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 struct ata_port_info *port[2];
4588 u8 tmp8, mask;
4589 unsigned int legacy_mode = 0;
4590 int disable_dev_on_err = 1;
4591 int rc;
4592
4593 DPRINTK("ENTER\n");
4594
4595 port[0] = port_info[0];
4596 if (n_ports > 1)
4597 port[1] = port_info[1];
4598 else
4599 port[1] = port[0];
4600
4601 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4602 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004603 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4605 mask = (1 << 2) | (1 << 0);
4606 if ((tmp8 & mask) != mask)
4607 legacy_mode = (1 << 3);
4608 }
4609
4610 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004611 if ((!legacy_mode) && (n_ports > 2)) {
4612 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4613 n_ports = 2;
4614 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
4616
Alan Cox47a86592005-10-04 08:09:19 -04004617 /* FIXME: Really for ATA it isn't safe because the device may be
4618 multi-purpose and we want to leave it alone if it was already
4619 enabled. Secondly for shared use as Arjan says we want refcounting
4620
4621 Checking dev->is_enabled is insufficient as this is not set at
4622 boot for the primary video which is BIOS enabled
4623 */
4624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 rc = pci_enable_device(pdev);
4626 if (rc)
4627 return rc;
4628
4629 rc = pci_request_regions(pdev, DRV_NAME);
4630 if (rc) {
4631 disable_dev_on_err = 0;
4632 goto err_out;
4633 }
4634
Alan Cox47a86592005-10-04 08:09:19 -04004635 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (legacy_mode) {
4637 if (!request_region(0x1f0, 8, "libata")) {
4638 struct resource *conflict, res;
4639 res.start = 0x1f0;
4640 res.end = 0x1f0 + 8 - 1;
4641 conflict = ____request_resource(&ioport_resource, &res);
4642 if (!strcmp(conflict->name, "libata"))
4643 legacy_mode |= (1 << 0);
4644 else {
4645 disable_dev_on_err = 0;
4646 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4647 }
4648 } else
4649 legacy_mode |= (1 << 0);
4650
4651 if (!request_region(0x170, 8, "libata")) {
4652 struct resource *conflict, res;
4653 res.start = 0x170;
4654 res.end = 0x170 + 8 - 1;
4655 conflict = ____request_resource(&ioport_resource, &res);
4656 if (!strcmp(conflict->name, "libata"))
4657 legacy_mode |= (1 << 1);
4658 else {
4659 disable_dev_on_err = 0;
4660 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4661 }
4662 } else
4663 legacy_mode |= (1 << 1);
4664 }
4665
4666 /* we have legacy mode, but all ports are unavailable */
4667 if (legacy_mode == (1 << 3)) {
4668 rc = -EBUSY;
4669 goto err_out_regions;
4670 }
4671
4672 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4673 if (rc)
4674 goto err_out_regions;
4675 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4676 if (rc)
4677 goto err_out_regions;
4678
4679 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004680 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004681 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004682 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004683 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004684 } else {
4685 if (n_ports == 2)
4686 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4687 else
4688 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4689 }
4690 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 rc = -ENOMEM;
4692 goto err_out_regions;
4693 }
4694
4695 pci_set_master(pdev);
4696
4697 /* FIXME: check ata_device_add return */
4698 if (legacy_mode) {
4699 if (legacy_mode & (1 << 0))
4700 ata_device_add(probe_ent);
4701 if (legacy_mode & (1 << 1))
4702 ata_device_add(probe_ent2);
4703 } else
4704 ata_device_add(probe_ent);
4705
4706 kfree(probe_ent);
4707 kfree(probe_ent2);
4708
4709 return 0;
4710
4711err_out_regions:
4712 if (legacy_mode & (1 << 0))
4713 release_region(0x1f0, 8);
4714 if (legacy_mode & (1 << 1))
4715 release_region(0x170, 8);
4716 pci_release_regions(pdev);
4717err_out:
4718 if (disable_dev_on_err)
4719 pci_disable_device(pdev);
4720 return rc;
4721}
4722
4723/**
4724 * ata_pci_remove_one - PCI layer callback for device removal
4725 * @pdev: PCI device that was removed
4726 *
4727 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004728 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 * Handle this by unregistering all objects associated
4730 * with this PCI device. Free those objects. Then finally
4731 * release PCI resources and disable device.
4732 *
4733 * LOCKING:
4734 * Inherited from PCI layer (may sleep).
4735 */
4736
4737void ata_pci_remove_one (struct pci_dev *pdev)
4738{
4739 struct device *dev = pci_dev_to_dev(pdev);
4740 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Alan Cox17b14452005-09-15 15:44:00 +01004742 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 pci_release_regions(pdev);
4744 pci_disable_device(pdev);
4745 dev_set_drvdata(dev, NULL);
4746}
4747
4748/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004749int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750{
4751 unsigned long tmp = 0;
4752
4753 switch (bits->width) {
4754 case 1: {
4755 u8 tmp8 = 0;
4756 pci_read_config_byte(pdev, bits->reg, &tmp8);
4757 tmp = tmp8;
4758 break;
4759 }
4760 case 2: {
4761 u16 tmp16 = 0;
4762 pci_read_config_word(pdev, bits->reg, &tmp16);
4763 tmp = tmp16;
4764 break;
4765 }
4766 case 4: {
4767 u32 tmp32 = 0;
4768 pci_read_config_dword(pdev, bits->reg, &tmp32);
4769 tmp = tmp32;
4770 break;
4771 }
4772
4773 default:
4774 return -EINVAL;
4775 }
4776
4777 tmp &= bits->mask;
4778
4779 return (tmp == bits->val) ? 1 : 0;
4780}
4781#endif /* CONFIG_PCI */
4782
4783
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784static int __init ata_init(void)
4785{
4786 ata_wq = create_workqueue("ata");
4787 if (!ata_wq)
4788 return -ENOMEM;
4789
4790 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4791 return 0;
4792}
4793
4794static void __exit ata_exit(void)
4795{
4796 destroy_workqueue(ata_wq);
4797}
4798
4799module_init(ata_init);
4800module_exit(ata_exit);
4801
Jeff Garzik67846b32005-10-05 02:58:32 -04004802static unsigned long ratelimit_time;
4803static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4804
4805int ata_ratelimit(void)
4806{
4807 int rc;
4808 unsigned long flags;
4809
4810 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4811
4812 if (time_after(jiffies, ratelimit_time)) {
4813 rc = 1;
4814 ratelimit_time = jiffies + (HZ/5);
4815 } else
4816 rc = 0;
4817
4818 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4819
4820 return rc;
4821}
4822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823/*
4824 * libata is essentially a library of internal helper functions for
4825 * low-level ATA host controller drivers. As such, the API/ABI is
4826 * likely to change as new drivers are added and updated.
4827 * Do not depend on ABI/API stability.
4828 */
4829
4830EXPORT_SYMBOL_GPL(ata_std_bios_param);
4831EXPORT_SYMBOL_GPL(ata_std_ports);
4832EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004833EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834EXPORT_SYMBOL_GPL(ata_sg_init);
4835EXPORT_SYMBOL_GPL(ata_sg_init_one);
4836EXPORT_SYMBOL_GPL(ata_qc_complete);
4837EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4838EXPORT_SYMBOL_GPL(ata_eng_timeout);
4839EXPORT_SYMBOL_GPL(ata_tf_load);
4840EXPORT_SYMBOL_GPL(ata_tf_read);
4841EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4842EXPORT_SYMBOL_GPL(ata_std_dev_select);
4843EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4844EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4845EXPORT_SYMBOL_GPL(ata_check_status);
4846EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847EXPORT_SYMBOL_GPL(ata_exec_command);
4848EXPORT_SYMBOL_GPL(ata_port_start);
4849EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004850EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851EXPORT_SYMBOL_GPL(ata_interrupt);
4852EXPORT_SYMBOL_GPL(ata_qc_prep);
4853EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4854EXPORT_SYMBOL_GPL(ata_bmdma_start);
4855EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4856EXPORT_SYMBOL_GPL(ata_bmdma_status);
4857EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4858EXPORT_SYMBOL_GPL(ata_port_probe);
4859EXPORT_SYMBOL_GPL(sata_phy_reset);
4860EXPORT_SYMBOL_GPL(__sata_phy_reset);
4861EXPORT_SYMBOL_GPL(ata_bus_reset);
4862EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004863EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4865EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4866EXPORT_SYMBOL_GPL(ata_scsi_error);
4867EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4868EXPORT_SYMBOL_GPL(ata_scsi_release);
4869EXPORT_SYMBOL_GPL(ata_host_intr);
4870EXPORT_SYMBOL_GPL(ata_dev_classify);
4871EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004872EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4874
Alan Cox452503f2005-10-21 19:01:32 -04004875EXPORT_SYMBOL_GPL(ata_timing_compute);
4876EXPORT_SYMBOL_GPL(ata_timing_merge);
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878#ifdef CONFIG_PCI
4879EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004880EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4882EXPORT_SYMBOL_GPL(ata_pci_init_one);
4883EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4884#endif /* CONFIG_PCI */