blob: d2f71a2331bb0a638422c091dcdc5be25ff0606f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <scsi/scsi.h>
53#include "scsi.h"
54#include "scsi_priv.h"
55#include <scsi/scsi_host.h>
56#include <linux/libata.h>
57#include <asm/io.h>
58#include <asm/semaphore.h>
59#include <asm/byteorder.h>
60
61#include "libata.h"
62
63static unsigned int ata_busy_sleep (struct ata_port *ap,
64 unsigned long tmout_pat,
65 unsigned long tmout);
Albert Lee59a10b12005-10-12 15:09:42 +080066static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ec2005-05-12 15:29:42 -040067static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void ata_set_mode(struct ata_port *ap);
69static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040070static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040072static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 u8 *xfer_mode_out,
74 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static void __ata_qc_complete(struct ata_queued_cmd *qc);
76
77static unsigned int ata_unique_id = 1;
78static struct workqueue_struct *ata_wq;
79
Jeff Garzik1623c812005-08-30 03:37:42 -040080int atapi_enabled = 0;
81module_param(atapi_enabled, int, 0444);
82MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_AUTHOR("Jeff Garzik");
85MODULE_DESCRIPTION("Library module for ATA devices");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(DRV_VERSION);
88
89/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040090 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * @ap: Port to which output is sent
92 * @tf: ATA taskfile register set
93 *
94 * Outputs ATA taskfile to standard ATA host controller.
95 *
96 * LOCKING:
97 * Inherited from caller.
98 */
99
Jeff Garzik057ace52005-10-22 14:27:05 -0400100static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct ata_ioports *ioaddr = &ap->ioaddr;
103 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
104
105 if (tf->ctl != ap->last_ctl) {
106 outb(tf->ctl, ioaddr->ctl_addr);
107 ap->last_ctl = tf->ctl;
108 ata_wait_idle(ap);
109 }
110
111 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
112 outb(tf->hob_feature, ioaddr->feature_addr);
113 outb(tf->hob_nsect, ioaddr->nsect_addr);
114 outb(tf->hob_lbal, ioaddr->lbal_addr);
115 outb(tf->hob_lbam, ioaddr->lbam_addr);
116 outb(tf->hob_lbah, ioaddr->lbah_addr);
117 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
118 tf->hob_feature,
119 tf->hob_nsect,
120 tf->hob_lbal,
121 tf->hob_lbam,
122 tf->hob_lbah);
123 }
124
125 if (is_addr) {
126 outb(tf->feature, ioaddr->feature_addr);
127 outb(tf->nsect, ioaddr->nsect_addr);
128 outb(tf->lbal, ioaddr->lbal_addr);
129 outb(tf->lbam, ioaddr->lbam_addr);
130 outb(tf->lbah, ioaddr->lbah_addr);
131 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
132 tf->feature,
133 tf->nsect,
134 tf->lbal,
135 tf->lbam,
136 tf->lbah);
137 }
138
139 if (tf->flags & ATA_TFLAG_DEVICE) {
140 outb(tf->device, ioaddr->device_addr);
141 VPRINTK("device 0x%X\n", tf->device);
142 }
143
144 ata_wait_idle(ap);
145}
146
147/**
148 * ata_tf_load_mmio - send taskfile registers to host controller
149 * @ap: Port to which output is sent
150 * @tf: ATA taskfile register set
151 *
152 * Outputs ATA taskfile to standard ATA host controller using MMIO.
153 *
154 * LOCKING:
155 * Inherited from caller.
156 */
157
Jeff Garzik057ace52005-10-22 14:27:05 -0400158static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct ata_ioports *ioaddr = &ap->ioaddr;
161 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
162
163 if (tf->ctl != ap->last_ctl) {
164 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
165 ap->last_ctl = tf->ctl;
166 ata_wait_idle(ap);
167 }
168
169 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
170 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
171 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
172 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
173 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
174 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
175 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
176 tf->hob_feature,
177 tf->hob_nsect,
178 tf->hob_lbal,
179 tf->hob_lbam,
180 tf->hob_lbah);
181 }
182
183 if (is_addr) {
184 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
185 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
186 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
187 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
188 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
189 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
190 tf->feature,
191 tf->nsect,
192 tf->lbal,
193 tf->lbam,
194 tf->lbah);
195 }
196
197 if (tf->flags & ATA_TFLAG_DEVICE) {
198 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
199 VPRINTK("device 0x%X\n", tf->device);
200 }
201
202 ata_wait_idle(ap);
203}
204
Edward Falk0baab862005-06-02 18:17:13 -0400205
206/**
207 * ata_tf_load - send taskfile registers to host controller
208 * @ap: Port to which output is sent
209 * @tf: ATA taskfile register set
210 *
211 * Outputs ATA taskfile to standard ATA host controller using MMIO
212 * or PIO as indicated by the ATA_FLAG_MMIO flag.
213 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
214 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
215 * hob_lbal, hob_lbam, and hob_lbah.
216 *
217 * This function waits for idle (!BUSY and !DRQ) after writing
218 * registers. If the control register has a new value, this
219 * function also waits for idle after writing control and before
220 * writing the remaining registers.
221 *
222 * May be used as the tf_load() entry in ata_port_operations.
223 *
224 * LOCKING:
225 * Inherited from caller.
226 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400227void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 if (ap->flags & ATA_FLAG_MMIO)
230 ata_tf_load_mmio(ap, tf);
231 else
232 ata_tf_load_pio(ap, tf);
233}
234
235/**
Edward Falk0baab862005-06-02 18:17:13 -0400236 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * @ap: port to which command is being issued
238 * @tf: ATA taskfile register set
239 *
Edward Falk0baab862005-06-02 18:17:13 -0400240 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * synchronization with interrupt handler / other threads.
242 *
243 * LOCKING:
244 * spin_lock_irqsave(host_set lock)
245 */
246
Jeff Garzik057ace52005-10-22 14:27:05 -0400247static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
250
251 outb(tf->command, ap->ioaddr.command_addr);
252 ata_pause(ap);
253}
254
255
256/**
257 * ata_exec_command_mmio - issue ATA command to host controller
258 * @ap: port to which command is being issued
259 * @tf: ATA taskfile register set
260 *
261 * Issues MMIO write to ATA command register, with proper
262 * synchronization with interrupt handler / other threads.
263 *
264 * LOCKING:
265 * spin_lock_irqsave(host_set lock)
266 */
267
Jeff Garzik057ace52005-10-22 14:27:05 -0400268static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
271
272 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
273 ata_pause(ap);
274}
275
Edward Falk0baab862005-06-02 18:17:13 -0400276
277/**
278 * ata_exec_command - issue ATA command to host controller
279 * @ap: port to which command is being issued
280 * @tf: ATA taskfile register set
281 *
282 * Issues PIO/MMIO write to ATA command register, with proper
283 * synchronization with interrupt handler / other threads.
284 *
285 * LOCKING:
286 * spin_lock_irqsave(host_set lock)
287 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400288void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 if (ap->flags & ATA_FLAG_MMIO)
291 ata_exec_command_mmio(ap, tf);
292 else
293 ata_exec_command_pio(ap, tf);
294}
295
296/**
297 * ata_exec - issue ATA command to host controller
298 * @ap: port to which command is being issued
299 * @tf: ATA taskfile register set
300 *
301 * Issues PIO/MMIO write to ATA command register, with proper
302 * synchronization with interrupt handler / other threads.
303 *
304 * LOCKING:
305 * Obtains host_set lock.
306 */
307
Jeff Garzik057ace52005-10-22 14:27:05 -0400308static inline void ata_exec(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 unsigned long flags;
311
312 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
313 spin_lock_irqsave(&ap->host_set->lock, flags);
314 ap->ops->exec_command(ap, tf);
315 spin_unlock_irqrestore(&ap->host_set->lock, flags);
316}
317
318/**
319 * ata_tf_to_host - issue ATA taskfile to host controller
320 * @ap: port to which command is being issued
321 * @tf: ATA taskfile register set
322 *
323 * Issues ATA taskfile register set to ATA host controller,
324 * with proper synchronization with interrupt handler and
325 * other threads.
326 *
327 * LOCKING:
328 * Obtains host_set lock.
329 */
330
Jeff Garzik057ace52005-10-22 14:27:05 -0400331static void ata_tf_to_host(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 ap->ops->tf_load(ap, tf);
334
335 ata_exec(ap, tf);
336}
337
338/**
339 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
340 * @ap: port to which command is being issued
341 * @tf: ATA taskfile register set
342 *
343 * Issues ATA taskfile register set to ATA host controller,
344 * with proper synchronization with interrupt handler and
345 * other threads.
346 *
347 * LOCKING:
348 * spin_lock_irqsave(host_set lock)
349 */
350
Jeff Garzik057ace52005-10-22 14:27:05 -0400351void ata_tf_to_host_nolock(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 ap->ops->tf_load(ap, tf);
354 ap->ops->exec_command(ap, tf);
355}
356
357/**
Edward Falk0baab862005-06-02 18:17:13 -0400358 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * @ap: Port from which input is read
360 * @tf: ATA taskfile register set for storing input
361 *
362 * Reads ATA taskfile registers for currently-selected device
363 * into @tf.
364 *
365 * LOCKING:
366 * Inherited from caller.
367 */
368
369static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
370{
371 struct ata_ioports *ioaddr = &ap->ioaddr;
372
Jeff Garzikac19bff2005-10-29 13:58:21 -0400373 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400374 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 tf->nsect = inb(ioaddr->nsect_addr);
376 tf->lbal = inb(ioaddr->lbal_addr);
377 tf->lbam = inb(ioaddr->lbam_addr);
378 tf->lbah = inb(ioaddr->lbah_addr);
379 tf->device = inb(ioaddr->device_addr);
380
381 if (tf->flags & ATA_TFLAG_LBA48) {
382 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
383 tf->hob_feature = inb(ioaddr->error_addr);
384 tf->hob_nsect = inb(ioaddr->nsect_addr);
385 tf->hob_lbal = inb(ioaddr->lbal_addr);
386 tf->hob_lbam = inb(ioaddr->lbam_addr);
387 tf->hob_lbah = inb(ioaddr->lbah_addr);
388 }
389}
390
391/**
392 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
393 * @ap: Port from which input is read
394 * @tf: ATA taskfile register set for storing input
395 *
396 * Reads ATA taskfile registers for currently-selected device
397 * into @tf via MMIO.
398 *
399 * LOCKING:
400 * Inherited from caller.
401 */
402
403static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
404{
405 struct ata_ioports *ioaddr = &ap->ioaddr;
406
Jeff Garzikac19bff2005-10-29 13:58:21 -0400407 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400408 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
410 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
411 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
412 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
413 tf->device = readb((void __iomem *)ioaddr->device_addr);
414
415 if (tf->flags & ATA_TFLAG_LBA48) {
416 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
417 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
418 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
419 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
420 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
421 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
422 }
423}
424
Edward Falk0baab862005-06-02 18:17:13 -0400425
426/**
427 * ata_tf_read - input device's ATA taskfile shadow registers
428 * @ap: Port from which input is read
429 * @tf: ATA taskfile register set for storing input
430 *
431 * Reads ATA taskfile registers for currently-selected device
432 * into @tf.
433 *
434 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
435 * is set, also reads the hob registers.
436 *
437 * May be used as the tf_read() entry in ata_port_operations.
438 *
439 * LOCKING:
440 * Inherited from caller.
441 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
443{
444 if (ap->flags & ATA_FLAG_MMIO)
445 ata_tf_read_mmio(ap, tf);
446 else
447 ata_tf_read_pio(ap, tf);
448}
449
450/**
451 * ata_check_status_pio - Read device status reg & clear interrupt
452 * @ap: port where the device is
453 *
454 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400455 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * from this device
457 *
458 * LOCKING:
459 * Inherited from caller.
460 */
461static u8 ata_check_status_pio(struct ata_port *ap)
462{
463 return inb(ap->ioaddr.status_addr);
464}
465
466/**
467 * ata_check_status_mmio - Read device status reg & clear interrupt
468 * @ap: port where the device is
469 *
470 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400471 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * from this device
473 *
474 * LOCKING:
475 * Inherited from caller.
476 */
477static u8 ata_check_status_mmio(struct ata_port *ap)
478{
479 return readb((void __iomem *) ap->ioaddr.status_addr);
480}
481
Edward Falk0baab862005-06-02 18:17:13 -0400482
483/**
484 * ata_check_status - Read device status reg & clear interrupt
485 * @ap: port where the device is
486 *
487 * Reads ATA taskfile status register for currently-selected device
488 * and return its value. This also clears pending interrupts
489 * from this device
490 *
491 * May be used as the check_status() entry in ata_port_operations.
492 *
493 * LOCKING:
494 * Inherited from caller.
495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496u8 ata_check_status(struct ata_port *ap)
497{
498 if (ap->flags & ATA_FLAG_MMIO)
499 return ata_check_status_mmio(ap);
500 return ata_check_status_pio(ap);
501}
502
Edward Falk0baab862005-06-02 18:17:13 -0400503
504/**
505 * ata_altstatus - Read device alternate status reg
506 * @ap: port where the device is
507 *
508 * Reads ATA taskfile alternate status register for
509 * currently-selected device and return its value.
510 *
511 * Note: may NOT be used as the check_altstatus() entry in
512 * ata_port_operations.
513 *
514 * LOCKING:
515 * Inherited from caller.
516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517u8 ata_altstatus(struct ata_port *ap)
518{
519 if (ap->ops->check_altstatus)
520 return ap->ops->check_altstatus(ap);
521
522 if (ap->flags & ATA_FLAG_MMIO)
523 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
524 return inb(ap->ioaddr.altstatus_addr);
525}
526
Edward Falk0baab862005-06-02 18:17:13 -0400527
528/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
530 * @tf: Taskfile to convert
531 * @fis: Buffer into which data will output
532 * @pmp: Port multiplier port
533 *
534 * Converts a standard ATA taskfile to a Serial ATA
535 * FIS structure (Register - Host to Device).
536 *
537 * LOCKING:
538 * Inherited from caller.
539 */
540
Jeff Garzik057ace52005-10-22 14:27:05 -0400541void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 fis[0] = 0x27; /* Register - Host to Device FIS */
544 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
545 bit 7 indicates Command FIS */
546 fis[2] = tf->command;
547 fis[3] = tf->feature;
548
549 fis[4] = tf->lbal;
550 fis[5] = tf->lbam;
551 fis[6] = tf->lbah;
552 fis[7] = tf->device;
553
554 fis[8] = tf->hob_lbal;
555 fis[9] = tf->hob_lbam;
556 fis[10] = tf->hob_lbah;
557 fis[11] = tf->hob_feature;
558
559 fis[12] = tf->nsect;
560 fis[13] = tf->hob_nsect;
561 fis[14] = 0;
562 fis[15] = tf->ctl;
563
564 fis[16] = 0;
565 fis[17] = 0;
566 fis[18] = 0;
567 fis[19] = 0;
568}
569
570/**
571 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
572 * @fis: Buffer from which data will be input
573 * @tf: Taskfile to output
574 *
575 * Converts a standard ATA taskfile to a Serial ATA
576 * FIS structure (Register - Host to Device).
577 *
578 * LOCKING:
579 * Inherited from caller.
580 */
581
Jeff Garzik057ace52005-10-22 14:27:05 -0400582void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 tf->command = fis[2]; /* status */
585 tf->feature = fis[3]; /* error */
586
587 tf->lbal = fis[4];
588 tf->lbam = fis[5];
589 tf->lbah = fis[6];
590 tf->device = fis[7];
591
592 tf->hob_lbal = fis[8];
593 tf->hob_lbam = fis[9];
594 tf->hob_lbah = fis[10];
595
596 tf->nsect = fis[12];
597 tf->hob_nsect = fis[13];
598}
599
Albert Lee8cbd6df2005-10-12 15:06:27 +0800600static const u8 ata_rw_cmds[] = {
601 /* pio multi */
602 ATA_CMD_READ_MULTI,
603 ATA_CMD_WRITE_MULTI,
604 ATA_CMD_READ_MULTI_EXT,
605 ATA_CMD_WRITE_MULTI_EXT,
606 /* pio */
607 ATA_CMD_PIO_READ,
608 ATA_CMD_PIO_WRITE,
609 ATA_CMD_PIO_READ_EXT,
610 ATA_CMD_PIO_WRITE_EXT,
611 /* dma */
612 ATA_CMD_READ,
613 ATA_CMD_WRITE,
614 ATA_CMD_READ_EXT,
615 ATA_CMD_WRITE_EXT
616};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800619 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
620 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800622 * Examine the device configuration and tf->flags to calculate
623 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 *
625 * LOCKING:
626 * caller.
627 */
Albert Lee8cbd6df2005-10-12 15:06:27 +0800628void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800630 struct ata_taskfile *tf = &qc->tf;
631 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Albert Lee8cbd6df2005-10-12 15:06:27 +0800633 int index, lba48, write;
634
635 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
636 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Albert Lee8cbd6df2005-10-12 15:06:27 +0800638 if (dev->flags & ATA_DFLAG_PIO) {
639 tf->protocol = ATA_PROT_PIO;
640 index = dev->multi_count ? 0 : 4;
641 } else {
642 tf->protocol = ATA_PROT_DMA;
643 index = 8;
644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Albert Lee8cbd6df2005-10-12 15:06:27 +0800646 tf->command = ata_rw_cmds[index + lba48 + write];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649static const char * xfer_mode_str[] = {
650 "UDMA/16",
651 "UDMA/25",
652 "UDMA/33",
653 "UDMA/44",
654 "UDMA/66",
655 "UDMA/100",
656 "UDMA/133",
657 "UDMA7",
658 "MWDMA0",
659 "MWDMA1",
660 "MWDMA2",
661 "PIO0",
662 "PIO1",
663 "PIO2",
664 "PIO3",
665 "PIO4",
666};
667
668/**
669 * ata_udma_string - convert UDMA bit offset to string
670 * @mask: mask of bits supported; only highest bit counts.
671 *
672 * Determine string which represents the highest speed
673 * (highest bit in @udma_mask).
674 *
675 * LOCKING:
676 * None.
677 *
678 * RETURNS:
679 * Constant C string representing highest speed listed in
680 * @udma_mask, or the constant C string "<n/a>".
681 */
682
683static const char *ata_mode_string(unsigned int mask)
684{
685 int i;
686
687 for (i = 7; i >= 0; i--)
688 if (mask & (1 << i))
689 goto out;
690 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
691 if (mask & (1 << i))
692 goto out;
693 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
694 if (mask & (1 << i))
695 goto out;
696
697 return "<n/a>";
698
699out:
700 return xfer_mode_str[i];
701}
702
703/**
704 * ata_pio_devchk - PATA device presence detection
705 * @ap: ATA channel to examine
706 * @device: Device to examine (starting at zero)
707 *
708 * This technique was originally described in
709 * Hale Landis's ATADRVR (www.ata-atapi.com), and
710 * later found its way into the ATA/ATAPI spec.
711 *
712 * Write a pattern to the ATA shadow registers,
713 * and if a device is present, it will respond by
714 * correctly storing and echoing back the
715 * ATA shadow register contents.
716 *
717 * LOCKING:
718 * caller.
719 */
720
721static unsigned int ata_pio_devchk(struct ata_port *ap,
722 unsigned int device)
723{
724 struct ata_ioports *ioaddr = &ap->ioaddr;
725 u8 nsect, lbal;
726
727 ap->ops->dev_select(ap, device);
728
729 outb(0x55, ioaddr->nsect_addr);
730 outb(0xaa, ioaddr->lbal_addr);
731
732 outb(0xaa, ioaddr->nsect_addr);
733 outb(0x55, ioaddr->lbal_addr);
734
735 outb(0x55, ioaddr->nsect_addr);
736 outb(0xaa, ioaddr->lbal_addr);
737
738 nsect = inb(ioaddr->nsect_addr);
739 lbal = inb(ioaddr->lbal_addr);
740
741 if ((nsect == 0x55) && (lbal == 0xaa))
742 return 1; /* we found a device */
743
744 return 0; /* nothing found */
745}
746
747/**
748 * ata_mmio_devchk - PATA device presence detection
749 * @ap: ATA channel to examine
750 * @device: Device to examine (starting at zero)
751 *
752 * This technique was originally described in
753 * Hale Landis's ATADRVR (www.ata-atapi.com), and
754 * later found its way into the ATA/ATAPI spec.
755 *
756 * Write a pattern to the ATA shadow registers,
757 * and if a device is present, it will respond by
758 * correctly storing and echoing back the
759 * ATA shadow register contents.
760 *
761 * LOCKING:
762 * caller.
763 */
764
765static unsigned int ata_mmio_devchk(struct ata_port *ap,
766 unsigned int device)
767{
768 struct ata_ioports *ioaddr = &ap->ioaddr;
769 u8 nsect, lbal;
770
771 ap->ops->dev_select(ap, device);
772
773 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
774 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
775
776 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
777 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
778
779 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
780 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
781
782 nsect = readb((void __iomem *) ioaddr->nsect_addr);
783 lbal = readb((void __iomem *) ioaddr->lbal_addr);
784
785 if ((nsect == 0x55) && (lbal == 0xaa))
786 return 1; /* we found a device */
787
788 return 0; /* nothing found */
789}
790
791/**
792 * ata_devchk - PATA device presence detection
793 * @ap: ATA channel to examine
794 * @device: Device to examine (starting at zero)
795 *
796 * Dispatch ATA device presence detection, depending
797 * on whether we are using PIO or MMIO to talk to the
798 * ATA shadow registers.
799 *
800 * LOCKING:
801 * caller.
802 */
803
804static unsigned int ata_devchk(struct ata_port *ap,
805 unsigned int device)
806{
807 if (ap->flags & ATA_FLAG_MMIO)
808 return ata_mmio_devchk(ap, device);
809 return ata_pio_devchk(ap, device);
810}
811
812/**
813 * ata_dev_classify - determine device type based on ATA-spec signature
814 * @tf: ATA taskfile register set for device to be identified
815 *
816 * Determine from taskfile register contents whether a device is
817 * ATA or ATAPI, as per "Signature and persistence" section
818 * of ATA/PI spec (volume 1, sect 5.14).
819 *
820 * LOCKING:
821 * None.
822 *
823 * RETURNS:
824 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
825 * the event of failure.
826 */
827
Jeff Garzik057ace52005-10-22 14:27:05 -0400828unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 /* Apple's open source Darwin code hints that some devices only
831 * put a proper signature into the LBA mid/high registers,
832 * So, we only check those. It's sufficient for uniqueness.
833 */
834
835 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
836 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
837 DPRINTK("found ATA device by sig\n");
838 return ATA_DEV_ATA;
839 }
840
841 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
842 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
843 DPRINTK("found ATAPI device by sig\n");
844 return ATA_DEV_ATAPI;
845 }
846
847 DPRINTK("unknown device\n");
848 return ATA_DEV_UNKNOWN;
849}
850
851/**
852 * ata_dev_try_classify - Parse returned ATA device signature
853 * @ap: ATA channel to examine
854 * @device: Device to examine (starting at zero)
855 *
856 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
857 * an ATA/ATAPI-defined set of values is placed in the ATA
858 * shadow registers, indicating the results of device detection
859 * and diagnostics.
860 *
861 * Select the ATA device, and read the values from the ATA shadow
862 * registers. Then parse according to the Error register value,
863 * and the spec-defined values examined by ata_dev_classify().
864 *
865 * LOCKING:
866 * caller.
867 */
868
869static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
870{
871 struct ata_device *dev = &ap->device[device];
872 struct ata_taskfile tf;
873 unsigned int class;
874 u8 err;
875
876 ap->ops->dev_select(ap, device);
877
878 memset(&tf, 0, sizeof(tf));
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400881 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 dev->class = ATA_DEV_NONE;
884
885 /* see if device passed diags */
886 if (err == 1)
887 /* do nothing */ ;
888 else if ((device == 0) && (err == 0x81))
889 /* do nothing */ ;
890 else
891 return err;
892
893 /* determine if device if ATA or ATAPI */
894 class = ata_dev_classify(&tf);
895 if (class == ATA_DEV_UNKNOWN)
896 return err;
897 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
898 return err;
899
900 dev->class = class;
901
902 return err;
903}
904
905/**
906 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
907 * @id: IDENTIFY DEVICE results we will examine
908 * @s: string into which data is output
909 * @ofs: offset into identify device page
910 * @len: length of string to return. must be an even number.
911 *
912 * The strings in the IDENTIFY DEVICE page are broken up into
913 * 16-bit chunks. Run through the string, and output each
914 * 8-bit chunk linearly, regardless of platform.
915 *
916 * LOCKING:
917 * caller.
918 */
919
Jeff Garzik057ace52005-10-22 14:27:05 -0400920void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 unsigned int ofs, unsigned int len)
922{
923 unsigned int c;
924
925 while (len > 0) {
926 c = id[ofs] >> 8;
927 *s = c;
928 s++;
929
930 c = id[ofs] & 0xff;
931 *s = c;
932 s++;
933
934 ofs++;
935 len -= 2;
936 }
937}
938
Edward Falk0baab862005-06-02 18:17:13 -0400939
940/**
941 * ata_noop_dev_select - Select device 0/1 on ATA bus
942 * @ap: ATA channel to manipulate
943 * @device: ATA device (numbered from zero) to select
944 *
945 * This function performs no actual function.
946 *
947 * May be used as the dev_select() entry in ata_port_operations.
948 *
949 * LOCKING:
950 * caller.
951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
953{
954}
955
Edward Falk0baab862005-06-02 18:17:13 -0400956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957/**
958 * ata_std_dev_select - Select device 0/1 on ATA bus
959 * @ap: ATA channel to manipulate
960 * @device: ATA device (numbered from zero) to select
961 *
962 * Use the method defined in the ATA specification to
963 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400964 * ATA channel. Works with both PIO and MMIO.
965 *
966 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 *
968 * LOCKING:
969 * caller.
970 */
971
972void ata_std_dev_select (struct ata_port *ap, unsigned int device)
973{
974 u8 tmp;
975
976 if (device == 0)
977 tmp = ATA_DEVICE_OBS;
978 else
979 tmp = ATA_DEVICE_OBS | ATA_DEV1;
980
981 if (ap->flags & ATA_FLAG_MMIO) {
982 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
983 } else {
984 outb(tmp, ap->ioaddr.device_addr);
985 }
986 ata_pause(ap); /* needed; also flushes, for mmio */
987}
988
989/**
990 * ata_dev_select - Select device 0/1 on ATA bus
991 * @ap: ATA channel to manipulate
992 * @device: ATA device (numbered from zero) to select
993 * @wait: non-zero to wait for Status register BSY bit to clear
994 * @can_sleep: non-zero if context allows sleeping
995 *
996 * Use the method defined in the ATA specification to
997 * make either device 0, or device 1, active on the
998 * ATA channel.
999 *
1000 * This is a high-level version of ata_std_dev_select(),
1001 * which additionally provides the services of inserting
1002 * the proper pauses and status polling, where needed.
1003 *
1004 * LOCKING:
1005 * caller.
1006 */
1007
1008void ata_dev_select(struct ata_port *ap, unsigned int device,
1009 unsigned int wait, unsigned int can_sleep)
1010{
1011 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1012 ap->id, device, wait);
1013
1014 if (wait)
1015 ata_wait_idle(ap);
1016
1017 ap->ops->dev_select(ap, device);
1018
1019 if (wait) {
1020 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1021 msleep(150);
1022 ata_wait_idle(ap);
1023 }
1024}
1025
1026/**
1027 * ata_dump_id - IDENTIFY DEVICE info debugging output
1028 * @dev: Device whose IDENTIFY DEVICE page we will dump
1029 *
1030 * Dump selected 16-bit words from a detected device's
1031 * IDENTIFY PAGE page.
1032 *
1033 * LOCKING:
1034 * caller.
1035 */
1036
Jeff Garzik057ace52005-10-22 14:27:05 -04001037static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 DPRINTK("49==0x%04x "
1040 "53==0x%04x "
1041 "63==0x%04x "
1042 "64==0x%04x "
1043 "75==0x%04x \n",
1044 dev->id[49],
1045 dev->id[53],
1046 dev->id[63],
1047 dev->id[64],
1048 dev->id[75]);
1049 DPRINTK("80==0x%04x "
1050 "81==0x%04x "
1051 "82==0x%04x "
1052 "83==0x%04x "
1053 "84==0x%04x \n",
1054 dev->id[80],
1055 dev->id[81],
1056 dev->id[82],
1057 dev->id[83],
1058 dev->id[84]);
1059 DPRINTK("88==0x%04x "
1060 "93==0x%04x\n",
1061 dev->id[88],
1062 dev->id[93]);
1063}
1064
Alan Cox11e29e22005-10-21 18:46:32 -04001065/*
1066 * Compute the PIO modes available for this device. This is not as
1067 * trivial as it seems if we must consider early devices correctly.
1068 *
1069 * FIXME: pre IDE drive timing (do we care ?).
1070 */
1071
Jeff Garzik057ace52005-10-22 14:27:05 -04001072static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001073{
1074 u16 modes;
1075
1076 /* Usual case. Word 53 indicates word 88 is valid */
1077 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1078 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1079 modes <<= 3;
1080 modes |= 0x7;
1081 return modes;
1082 }
1083
1084 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1085 for the maximum. Turn it into a mask and return it */
1086 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1087 return modes;
1088}
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090/**
1091 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1092 * @ap: port on which device we wish to probe resides
1093 * @device: device bus address, starting at zero
1094 *
1095 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1096 * command, and read back the 512-byte device information page.
1097 * The device information page is fed to us via the standard
1098 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1099 * using standard PIO-IN paths)
1100 *
1101 * After reading the device information page, we use several
1102 * bits of information from it to initialize data structures
1103 * that will be used during the lifetime of the ata_device.
1104 * Other data from the info page is used to disqualify certain
1105 * older ATA devices we do not wish to support.
1106 *
1107 * LOCKING:
1108 * Inherited from caller. Some functions called by this function
1109 * obtain the host_set lock.
1110 */
1111
1112static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1113{
1114 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001115 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 u16 tmp;
1117 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 unsigned int using_edd;
1119 DECLARE_COMPLETION(wait);
1120 struct ata_queued_cmd *qc;
1121 unsigned long flags;
1122 int rc;
1123
1124 if (!ata_dev_present(dev)) {
1125 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1126 ap->id, device);
1127 return;
1128 }
1129
1130 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1131 using_edd = 0;
1132 else
1133 using_edd = 1;
1134
1135 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1136
1137 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1138 dev->class == ATA_DEV_NONE);
1139
1140 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1141
1142 qc = ata_qc_new_init(ap, dev);
1143 BUG_ON(qc == NULL);
1144
1145 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1146 qc->dma_dir = DMA_FROM_DEVICE;
1147 qc->tf.protocol = ATA_PROT_PIO;
1148 qc->nsect = 1;
1149
1150retry:
1151 if (dev->class == ATA_DEV_ATA) {
1152 qc->tf.command = ATA_CMD_ID_ATA;
1153 DPRINTK("do ATA identify\n");
1154 } else {
1155 qc->tf.command = ATA_CMD_ID_ATAPI;
1156 DPRINTK("do ATAPI identify\n");
1157 }
1158
1159 qc->waiting = &wait;
1160 qc->complete_fn = ata_qc_complete_noop;
1161
1162 spin_lock_irqsave(&ap->host_set->lock, flags);
1163 rc = ata_qc_issue(qc);
1164 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1165
1166 if (rc)
1167 goto err_out;
1168 else
1169 wait_for_completion(&wait);
1170
Jeff Garzik0169e282005-10-29 21:25:10 -04001171 spin_lock_irqsave(&ap->host_set->lock, flags);
1172 ap->ops->tf_read(ap, &qc->tf);
1173 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1174
1175 if (qc->tf.command & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /*
1177 * arg! EDD works for all test cases, but seems to return
1178 * the ATA signature for some ATAPI devices. Until the
1179 * reason for this is found and fixed, we fix up the mess
1180 * here. If IDENTIFY DEVICE returns command aborted
1181 * (as ATAPI devices do), then we issue an
1182 * IDENTIFY PACKET DEVICE.
1183 *
1184 * ATA software reset (SRST, the default) does not appear
1185 * to have this problem.
1186 */
1187 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
Jeff Garzik0169e282005-10-29 21:25:10 -04001188 u8 err = qc->tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (err & ATA_ABORTED) {
1190 dev->class = ATA_DEV_ATAPI;
1191 qc->cursg = 0;
1192 qc->cursg_ofs = 0;
1193 qc->cursect = 0;
1194 qc->nsect = 1;
1195 goto retry;
1196 }
1197 }
1198 goto err_out;
1199 }
1200
1201 swap_buf_le16(dev->id, ATA_ID_WORDS);
1202
1203 /* print device capabilities */
1204 printk(KERN_DEBUG "ata%u: dev %u cfg "
1205 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1206 ap->id, device, dev->id[49],
1207 dev->id[82], dev->id[83], dev->id[84],
1208 dev->id[85], dev->id[86], dev->id[87],
1209 dev->id[88]);
1210
1211 /*
1212 * common ATA, ATAPI feature tests
1213 */
1214
Albert Lee8bf62ec2005-05-12 15:29:42 -04001215 /* we require DMA support (bits 8 of word 49) */
1216 if (!ata_id_has_dma(dev->id)) {
1217 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto err_out_nosup;
1219 }
1220
1221 /* quick-n-dirty find max transfer mode; for printk only */
1222 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1223 if (!xfer_modes)
1224 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001225 if (!xfer_modes)
1226 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 ata_dump_id(dev);
1229
1230 /* ATA-specific feature tests */
1231 if (dev->class == ATA_DEV_ATA) {
1232 if (!ata_id_is_ata(dev->id)) /* sanity check */
1233 goto err_out_nosup;
1234
Albert Lee8bf62ec2005-05-12 15:29:42 -04001235 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001237 for (major_version = 14; major_version >= 1; major_version--)
1238 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 break;
1240
Albert Lee8bf62ec2005-05-12 15:29:42 -04001241 /*
1242 * The exact sequence expected by certain pre-ATA4 drives is:
1243 * SRST RESET
1244 * IDENTIFY
1245 * INITIALIZE DEVICE PARAMETERS
1246 * anything else..
1247 * Some drives were very specific about that exact sequence.
1248 */
Albert Lee59a10b12005-10-12 15:09:42 +08001249 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001250 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Albert Lee59a10b12005-10-12 15:09:42 +08001252 /* current CHS translation info (id[53-58]) might be
1253 * changed. reread the identify device info.
1254 */
1255 ata_dev_reread_id(ap, dev);
1256 }
1257
Albert Lee8bf62ec2005-05-12 15:29:42 -04001258 if (ata_id_has_lba(dev->id)) {
1259 dev->flags |= ATA_DFLAG_LBA;
1260
1261 if (ata_id_has_lba48(dev->id)) {
1262 dev->flags |= ATA_DFLAG_LBA48;
1263 dev->n_sectors = ata_id_u64(dev->id, 100);
1264 } else {
1265 dev->n_sectors = ata_id_u32(dev->id, 60);
1266 }
1267
1268 /* print device info to dmesg */
1269 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1270 ap->id, device,
1271 major_version,
1272 ata_mode_string(xfer_modes),
1273 (unsigned long long)dev->n_sectors,
1274 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1275 } else {
1276 /* CHS */
1277
1278 /* Default translation */
1279 dev->cylinders = dev->id[1];
1280 dev->heads = dev->id[3];
1281 dev->sectors = dev->id[6];
1282 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1283
1284 if (ata_id_current_chs_valid(dev->id)) {
1285 /* Current CHS translation is valid. */
1286 dev->cylinders = dev->id[54];
1287 dev->heads = dev->id[55];
1288 dev->sectors = dev->id[56];
1289
1290 dev->n_sectors = ata_id_u32(dev->id, 57);
1291 }
1292
1293 /* print device info to dmesg */
1294 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1295 ap->id, device,
1296 major_version,
1297 ata_mode_string(xfer_modes),
1298 (unsigned long long)dev->n_sectors,
1299 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
1303 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
1306 /* ATAPI-specific feature tests */
1307 else {
1308 if (ata_id_is_ata(dev->id)) /* sanity check */
1309 goto err_out_nosup;
1310
1311 rc = atapi_cdb_len(dev->id);
1312 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1313 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1314 goto err_out_nosup;
1315 }
1316 ap->cdb_len = (unsigned int) rc;
1317 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1318
1319 /* print device info to dmesg */
1320 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1321 ap->id, device,
1322 ata_mode_string(xfer_modes));
1323 }
1324
1325 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1326 return;
1327
1328err_out_nosup:
1329 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1330 ap->id, device);
1331err_out:
1332 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1333 DPRINTK("EXIT, err\n");
1334}
1335
Brad Campbell6f2f3812005-05-12 15:07:47 -04001336
Jeff Garzik057ace52005-10-22 14:27:05 -04001337static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001338{
1339 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1340}
1341
1342/**
1343 * ata_dev_config - Run device specific handlers and check for
1344 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001345 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001346 * @i: Device
1347 *
1348 * LOCKING:
1349 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001350
Brad Campbell6f2f3812005-05-12 15:07:47 -04001351void ata_dev_config(struct ata_port *ap, unsigned int i)
1352{
1353 /* limit bridge transfers to udma5, 200 sectors */
1354 if (ata_dev_knobble(ap)) {
1355 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1356 ap->id, ap->device->devno);
1357 ap->udma_mask &= ATA_UDMA5;
1358 ap->host->max_sectors = ATA_MAX_SECTORS;
1359 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1360 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1361 }
1362
1363 if (ap->ops->dev_config)
1364 ap->ops->dev_config(ap, &ap->device[i]);
1365}
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367/**
1368 * ata_bus_probe - Reset and probe ATA bus
1369 * @ap: Bus to probe
1370 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001371 * Master ATA bus probing function. Initiates a hardware-dependent
1372 * bus reset, then attempts to identify any devices found on
1373 * the bus.
1374 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001376 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 *
1378 * RETURNS:
1379 * Zero on success, non-zero on error.
1380 */
1381
1382static int ata_bus_probe(struct ata_port *ap)
1383{
1384 unsigned int i, found = 0;
1385
1386 ap->ops->phy_reset(ap);
1387 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1388 goto err_out;
1389
1390 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1391 ata_dev_identify(ap, i);
1392 if (ata_dev_present(&ap->device[i])) {
1393 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001394 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 }
1397
1398 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1399 goto err_out_disable;
1400
1401 ata_set_mode(ap);
1402 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1403 goto err_out_disable;
1404
1405 return 0;
1406
1407err_out_disable:
1408 ap->ops->port_disable(ap);
1409err_out:
1410 return -1;
1411}
1412
1413/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001414 * ata_port_probe - Mark port as enabled
1415 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001417 * Modify @ap data structure such that the system
1418 * thinks that the entire port is enabled.
1419 *
1420 * LOCKING: host_set lock, or some other form of
1421 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
1423
1424void ata_port_probe(struct ata_port *ap)
1425{
1426 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1427}
1428
1429/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001430 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1431 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001433 * This function issues commands to standard SATA Sxxx
1434 * PHY registers, to wake up the phy (and device), and
1435 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 *
1437 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001438 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 *
1440 */
1441void __sata_phy_reset(struct ata_port *ap)
1442{
1443 u32 sstatus;
1444 unsigned long timeout = jiffies + (HZ * 5);
1445
1446 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001447 /* issue phy wake/reset */
1448 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001449 /* Couldn't find anything in SATA I/II specs, but
1450 * AHCI-1.1 10.4.2 says at least 1 ms. */
1451 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
Brett Russcdcca892005-03-28 15:10:27 -05001453 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /* wait for phy to become ready, if necessary */
1456 do {
1457 msleep(200);
1458 sstatus = scr_read(ap, SCR_STATUS);
1459 if ((sstatus & 0xf) != 1)
1460 break;
1461 } while (time_before(jiffies, timeout));
1462
1463 /* TODO: phy layer with polling, timeouts, etc. */
1464 if (sata_dev_present(ap))
1465 ata_port_probe(ap);
1466 else {
1467 sstatus = scr_read(ap, SCR_STATUS);
1468 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1469 ap->id, sstatus);
1470 ata_port_disable(ap);
1471 }
1472
1473 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1474 return;
1475
1476 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1477 ata_port_disable(ap);
1478 return;
1479 }
1480
1481 ap->cbl = ATA_CBL_SATA;
1482}
1483
1484/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001485 * sata_phy_reset - Reset SATA bus.
1486 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001488 * This function resets the SATA bus, and then probes
1489 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 *
1491 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001492 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 *
1494 */
1495void sata_phy_reset(struct ata_port *ap)
1496{
1497 __sata_phy_reset(ap);
1498 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1499 return;
1500 ata_bus_reset(ap);
1501}
1502
1503/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001504 * ata_port_disable - Disable port.
1505 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001507 * Modify @ap data structure such that the system
1508 * thinks that the entire port is disabled, and should
1509 * never attempt to probe or communicate with devices
1510 * on this port.
1511 *
1512 * LOCKING: host_set lock, or some other form of
1513 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
1515
1516void ata_port_disable(struct ata_port *ap)
1517{
1518 ap->device[0].class = ATA_DEV_NONE;
1519 ap->device[1].class = ATA_DEV_NONE;
1520 ap->flags |= ATA_FLAG_PORT_DISABLED;
1521}
1522
Alan Cox452503f2005-10-21 19:01:32 -04001523/*
1524 * This mode timing computation functionality is ported over from
1525 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1526 */
1527/*
1528 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1529 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1530 * for PIO 5, which is a nonstandard extension and UDMA6, which
1531 * is currently supported only by Maxtor drives.
1532 */
1533
1534static const struct ata_timing ata_timing[] = {
1535
1536 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1537 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1538 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1539 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1540
1541 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1542 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1543 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1544
1545/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1546
1547 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1548 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1549 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1550
1551 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1552 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1553 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1554
1555/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1556 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1557 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1558
1559 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1560 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1561 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1562
1563/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1564
1565 { 0xFF }
1566};
1567
1568#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1569#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1570
1571static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1572{
1573 q->setup = EZ(t->setup * 1000, T);
1574 q->act8b = EZ(t->act8b * 1000, T);
1575 q->rec8b = EZ(t->rec8b * 1000, T);
1576 q->cyc8b = EZ(t->cyc8b * 1000, T);
1577 q->active = EZ(t->active * 1000, T);
1578 q->recover = EZ(t->recover * 1000, T);
1579 q->cycle = EZ(t->cycle * 1000, T);
1580 q->udma = EZ(t->udma * 1000, UT);
1581}
1582
1583void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1584 struct ata_timing *m, unsigned int what)
1585{
1586 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1587 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1588 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1589 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1590 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1591 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1592 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1593 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1594}
1595
1596static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1597{
1598 const struct ata_timing *t;
1599
1600 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001601 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001602 return NULL;
1603 return t;
1604}
1605
1606int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1607 struct ata_timing *t, int T, int UT)
1608{
1609 const struct ata_timing *s;
1610 struct ata_timing p;
1611
1612 /*
1613 * Find the mode.
1614 */
1615
1616 if (!(s = ata_timing_find_mode(speed)))
1617 return -EINVAL;
1618
1619 /*
1620 * If the drive is an EIDE drive, it can tell us it needs extended
1621 * PIO/MW_DMA cycle timing.
1622 */
1623
1624 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1625 memset(&p, 0, sizeof(p));
1626 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1627 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1628 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1629 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1630 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1631 }
1632 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1633 }
1634
1635 /*
1636 * Convert the timing to bus clock counts.
1637 */
1638
1639 ata_timing_quantize(s, t, T, UT);
1640
1641 /*
1642 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1643 * and some other commands. We have to ensure that the DMA cycle timing is
1644 * slower/equal than the fastest PIO timing.
1645 */
1646
1647 if (speed > XFER_PIO_4) {
1648 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1649 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1650 }
1651
1652 /*
1653 * Lenghten active & recovery time so that cycle time is correct.
1654 */
1655
1656 if (t->act8b + t->rec8b < t->cyc8b) {
1657 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1658 t->rec8b = t->cyc8b - t->act8b;
1659 }
1660
1661 if (t->active + t->recover < t->cycle) {
1662 t->active += (t->cycle - (t->active + t->recover)) / 2;
1663 t->recover = t->cycle - t->active;
1664 }
1665
1666 return 0;
1667}
1668
Jeff Garzik057ace52005-10-22 14:27:05 -04001669static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 unsigned int shift;
1671 u8 base;
1672} xfer_mode_classes[] = {
1673 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1674 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1675 { ATA_SHIFT_PIO, XFER_PIO_0 },
1676};
1677
1678static inline u8 base_from_shift(unsigned int shift)
1679{
1680 int i;
1681
1682 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1683 if (xfer_mode_classes[i].shift == shift)
1684 return xfer_mode_classes[i].base;
1685
1686 return 0xff;
1687}
1688
1689static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1690{
1691 int ofs, idx;
1692 u8 base;
1693
1694 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1695 return;
1696
1697 if (dev->xfer_shift == ATA_SHIFT_PIO)
1698 dev->flags |= ATA_DFLAG_PIO;
1699
1700 ata_dev_set_xfermode(ap, dev);
1701
1702 base = base_from_shift(dev->xfer_shift);
1703 ofs = dev->xfer_mode - base;
1704 idx = ofs + dev->xfer_shift;
1705 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1706
1707 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1708 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1709
1710 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1711 ap->id, dev->devno, xfer_mode_str[idx]);
1712}
1713
1714static int ata_host_set_pio(struct ata_port *ap)
1715{
1716 unsigned int mask;
1717 int x, i;
1718 u8 base, xfer_mode;
1719
1720 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1721 x = fgb(mask);
1722 if (x < 0) {
1723 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1724 return -1;
1725 }
1726
1727 base = base_from_shift(ATA_SHIFT_PIO);
1728 xfer_mode = base + x;
1729
1730 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1731 (int)base, (int)xfer_mode, mask, x);
1732
1733 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1734 struct ata_device *dev = &ap->device[i];
1735 if (ata_dev_present(dev)) {
1736 dev->pio_mode = xfer_mode;
1737 dev->xfer_mode = xfer_mode;
1738 dev->xfer_shift = ATA_SHIFT_PIO;
1739 if (ap->ops->set_piomode)
1740 ap->ops->set_piomode(ap, dev);
1741 }
1742 }
1743
1744 return 0;
1745}
1746
1747static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1748 unsigned int xfer_shift)
1749{
1750 int i;
1751
1752 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1753 struct ata_device *dev = &ap->device[i];
1754 if (ata_dev_present(dev)) {
1755 dev->dma_mode = xfer_mode;
1756 dev->xfer_mode = xfer_mode;
1757 dev->xfer_shift = xfer_shift;
1758 if (ap->ops->set_dmamode)
1759 ap->ops->set_dmamode(ap, dev);
1760 }
1761 }
1762}
1763
1764/**
1765 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1766 * @ap: port on which timings will be programmed
1767 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001768 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001771 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 *
1773 */
1774static void ata_set_mode(struct ata_port *ap)
1775{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001776 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 u8 xfer_mode;
1778 int rc;
1779
1780 /* step 1: always set host PIO timings */
1781 rc = ata_host_set_pio(ap);
1782 if (rc)
1783 goto err_out;
1784
1785 /* step 2: choose the best data xfer mode */
1786 xfer_mode = xfer_shift = 0;
1787 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1788 if (rc)
1789 goto err_out;
1790
1791 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1792 if (xfer_shift != ATA_SHIFT_PIO)
1793 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1794
1795 /* step 4: update devices' xfer mode */
1796 ata_dev_set_mode(ap, &ap->device[0]);
1797 ata_dev_set_mode(ap, &ap->device[1]);
1798
1799 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1800 return;
1801
1802 if (ap->ops->post_set_mode)
1803 ap->ops->post_set_mode(ap);
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return;
1806
1807err_out:
1808 ata_port_disable(ap);
1809}
1810
1811/**
1812 * ata_busy_sleep - sleep until BSY clears, or timeout
1813 * @ap: port containing status register to be polled
1814 * @tmout_pat: impatience timeout
1815 * @tmout: overall timeout
1816 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001817 * Sleep until ATA Status register bit BSY clears,
1818 * or a timeout occurs.
1819 *
1820 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 *
1822 */
1823
1824static unsigned int ata_busy_sleep (struct ata_port *ap,
1825 unsigned long tmout_pat,
1826 unsigned long tmout)
1827{
1828 unsigned long timer_start, timeout;
1829 u8 status;
1830
1831 status = ata_busy_wait(ap, ATA_BUSY, 300);
1832 timer_start = jiffies;
1833 timeout = timer_start + tmout_pat;
1834 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1835 msleep(50);
1836 status = ata_busy_wait(ap, ATA_BUSY, 3);
1837 }
1838
1839 if (status & ATA_BUSY)
1840 printk(KERN_WARNING "ata%u is slow to respond, "
1841 "please be patient\n", ap->id);
1842
1843 timeout = timer_start + tmout;
1844 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1845 msleep(50);
1846 status = ata_chk_status(ap);
1847 }
1848
1849 if (status & ATA_BUSY) {
1850 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1851 ap->id, tmout / HZ);
1852 return 1;
1853 }
1854
1855 return 0;
1856}
1857
1858static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1859{
1860 struct ata_ioports *ioaddr = &ap->ioaddr;
1861 unsigned int dev0 = devmask & (1 << 0);
1862 unsigned int dev1 = devmask & (1 << 1);
1863 unsigned long timeout;
1864
1865 /* if device 0 was found in ata_devchk, wait for its
1866 * BSY bit to clear
1867 */
1868 if (dev0)
1869 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1870
1871 /* if device 1 was found in ata_devchk, wait for
1872 * register access, then wait for BSY to clear
1873 */
1874 timeout = jiffies + ATA_TMOUT_BOOT;
1875 while (dev1) {
1876 u8 nsect, lbal;
1877
1878 ap->ops->dev_select(ap, 1);
1879 if (ap->flags & ATA_FLAG_MMIO) {
1880 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1881 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1882 } else {
1883 nsect = inb(ioaddr->nsect_addr);
1884 lbal = inb(ioaddr->lbal_addr);
1885 }
1886 if ((nsect == 1) && (lbal == 1))
1887 break;
1888 if (time_after(jiffies, timeout)) {
1889 dev1 = 0;
1890 break;
1891 }
1892 msleep(50); /* give drive a breather */
1893 }
1894 if (dev1)
1895 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1896
1897 /* is all this really necessary? */
1898 ap->ops->dev_select(ap, 0);
1899 if (dev1)
1900 ap->ops->dev_select(ap, 1);
1901 if (dev0)
1902 ap->ops->dev_select(ap, 0);
1903}
1904
1905/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001906 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1907 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001909 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1910 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 *
1912 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001913 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 *
1915 */
1916
1917static unsigned int ata_bus_edd(struct ata_port *ap)
1918{
1919 struct ata_taskfile tf;
1920
1921 /* set up execute-device-diag (bus reset) taskfile */
1922 /* also, take interrupts to a known state (disabled) */
1923 DPRINTK("execute-device-diag\n");
1924 ata_tf_init(ap, &tf, 0);
1925 tf.ctl |= ATA_NIEN;
1926 tf.command = ATA_CMD_EDD;
1927 tf.protocol = ATA_PROT_NODATA;
1928
1929 /* do bus reset */
1930 ata_tf_to_host(ap, &tf);
1931
1932 /* spec says at least 2ms. but who knows with those
1933 * crazy ATAPI devices...
1934 */
1935 msleep(150);
1936
1937 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1938}
1939
1940static unsigned int ata_bus_softreset(struct ata_port *ap,
1941 unsigned int devmask)
1942{
1943 struct ata_ioports *ioaddr = &ap->ioaddr;
1944
1945 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1946
1947 /* software reset. causes dev0 to be selected */
1948 if (ap->flags & ATA_FLAG_MMIO) {
1949 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1950 udelay(20); /* FIXME: flush */
1951 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1952 udelay(20); /* FIXME: flush */
1953 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1954 } else {
1955 outb(ap->ctl, ioaddr->ctl_addr);
1956 udelay(10);
1957 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1958 udelay(10);
1959 outb(ap->ctl, ioaddr->ctl_addr);
1960 }
1961
1962 /* spec mandates ">= 2ms" before checking status.
1963 * We wait 150ms, because that was the magic delay used for
1964 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1965 * between when the ATA command register is written, and then
1966 * status is checked. Because waiting for "a while" before
1967 * checking status is fine, post SRST, we perform this magic
1968 * delay here as well.
1969 */
1970 msleep(150);
1971
1972 ata_bus_post_reset(ap, devmask);
1973
1974 return 0;
1975}
1976
1977/**
1978 * ata_bus_reset - reset host port and associated ATA channel
1979 * @ap: port to reset
1980 *
1981 * This is typically the first time we actually start issuing
1982 * commands to the ATA channel. We wait for BSY to clear, then
1983 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1984 * result. Determine what devices, if any, are on the channel
1985 * by looking at the device 0/1 error register. Look at the signature
1986 * stored in each device's taskfile registers, to determine if
1987 * the device is ATA or ATAPI.
1988 *
1989 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001990 * PCI/etc. bus probe sem.
1991 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 *
1993 * SIDE EFFECTS:
1994 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1995 */
1996
1997void ata_bus_reset(struct ata_port *ap)
1998{
1999 struct ata_ioports *ioaddr = &ap->ioaddr;
2000 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2001 u8 err;
2002 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2003
2004 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2005
2006 /* determine if device 0/1 are present */
2007 if (ap->flags & ATA_FLAG_SATA_RESET)
2008 dev0 = 1;
2009 else {
2010 dev0 = ata_devchk(ap, 0);
2011 if (slave_possible)
2012 dev1 = ata_devchk(ap, 1);
2013 }
2014
2015 if (dev0)
2016 devmask |= (1 << 0);
2017 if (dev1)
2018 devmask |= (1 << 1);
2019
2020 /* select device 0 again */
2021 ap->ops->dev_select(ap, 0);
2022
2023 /* issue bus reset */
2024 if (ap->flags & ATA_FLAG_SRST)
2025 rc = ata_bus_softreset(ap, devmask);
2026 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2027 /* set up device control */
2028 if (ap->flags & ATA_FLAG_MMIO)
2029 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2030 else
2031 outb(ap->ctl, ioaddr->ctl_addr);
2032 rc = ata_bus_edd(ap);
2033 }
2034
2035 if (rc)
2036 goto err_out;
2037
2038 /*
2039 * determine by signature whether we have ATA or ATAPI devices
2040 */
2041 err = ata_dev_try_classify(ap, 0);
2042 if ((slave_possible) && (err != 0x81))
2043 ata_dev_try_classify(ap, 1);
2044
2045 /* re-enable interrupts */
2046 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2047 ata_irq_on(ap);
2048
2049 /* is double-select really necessary? */
2050 if (ap->device[1].class != ATA_DEV_NONE)
2051 ap->ops->dev_select(ap, 1);
2052 if (ap->device[0].class != ATA_DEV_NONE)
2053 ap->ops->dev_select(ap, 0);
2054
2055 /* if no devices were detected, disable this port */
2056 if ((ap->device[0].class == ATA_DEV_NONE) &&
2057 (ap->device[1].class == ATA_DEV_NONE))
2058 goto err_out;
2059
2060 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2061 /* set up device control for ATA_FLAG_SATA_RESET */
2062 if (ap->flags & ATA_FLAG_MMIO)
2063 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2064 else
2065 outb(ap->ctl, ioaddr->ctl_addr);
2066 }
2067
2068 DPRINTK("EXIT\n");
2069 return;
2070
2071err_out:
2072 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2073 ap->ops->port_disable(ap);
2074
2075 DPRINTK("EXIT\n");
2076}
2077
Jeff Garzik057ace52005-10-22 14:27:05 -04002078static void ata_pr_blacklisted(const struct ata_port *ap,
2079 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
2081 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2082 ap->id, dev->devno);
2083}
2084
2085static const char * ata_dma_blacklist [] = {
2086 "WDC AC11000H",
2087 "WDC AC22100H",
2088 "WDC AC32500H",
2089 "WDC AC33100H",
2090 "WDC AC31600H",
2091 "WDC AC32100H",
2092 "WDC AC23200L",
2093 "Compaq CRD-8241B",
2094 "CRD-8400B",
2095 "CRD-8480B",
2096 "CRD-8482B",
2097 "CRD-84",
2098 "SanDisk SDP3B",
2099 "SanDisk SDP3B-64",
2100 "SANYO CD-ROM CRD",
2101 "HITACHI CDR-8",
2102 "HITACHI CDR-8335",
2103 "HITACHI CDR-8435",
2104 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002105 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 "CD-532E-A",
2107 "E-IDE CD-ROM CR-840",
2108 "CD-ROM Drive/F5A",
2109 "WPI CDD-820",
2110 "SAMSUNG CD-ROM SC-148C",
2111 "SAMSUNG CD-ROM SC",
2112 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2114 "_NEC DV5800A",
2115};
2116
Jeff Garzik057ace52005-10-22 14:27:05 -04002117static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
2119 unsigned char model_num[40];
2120 char *s;
2121 unsigned int len;
2122 int i;
2123
2124 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2125 sizeof(model_num));
2126 s = &model_num[0];
2127 len = strnlen(s, sizeof(model_num));
2128
2129 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2130 while ((len > 0) && (s[len - 1] == ' ')) {
2131 len--;
2132 s[len] = 0;
2133 }
2134
2135 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2136 if (!strncmp(ata_dma_blacklist[i], s, len))
2137 return 1;
2138
2139 return 0;
2140}
2141
Jeff Garzik057ace52005-10-22 14:27:05 -04002142static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Jeff Garzik057ace52005-10-22 14:27:05 -04002144 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 unsigned int mask;
2146
2147 master = &ap->device[0];
2148 slave = &ap->device[1];
2149
2150 assert (ata_dev_present(master) || ata_dev_present(slave));
2151
2152 if (shift == ATA_SHIFT_UDMA) {
2153 mask = ap->udma_mask;
2154 if (ata_dev_present(master)) {
2155 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002156 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 mask = 0;
2158 ata_pr_blacklisted(ap, master);
2159 }
2160 }
2161 if (ata_dev_present(slave)) {
2162 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002163 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 mask = 0;
2165 ata_pr_blacklisted(ap, slave);
2166 }
2167 }
2168 }
2169 else if (shift == ATA_SHIFT_MWDMA) {
2170 mask = ap->mwdma_mask;
2171 if (ata_dev_present(master)) {
2172 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002173 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 mask = 0;
2175 ata_pr_blacklisted(ap, master);
2176 }
2177 }
2178 if (ata_dev_present(slave)) {
2179 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002180 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 mask = 0;
2182 ata_pr_blacklisted(ap, slave);
2183 }
2184 }
2185 }
2186 else if (shift == ATA_SHIFT_PIO) {
2187 mask = ap->pio_mask;
2188 if (ata_dev_present(master)) {
2189 /* spec doesn't return explicit support for
2190 * PIO0-2, so we fake it
2191 */
2192 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2193 tmp_mode <<= 3;
2194 tmp_mode |= 0x7;
2195 mask &= tmp_mode;
2196 }
2197 if (ata_dev_present(slave)) {
2198 /* spec doesn't return explicit support for
2199 * PIO0-2, so we fake it
2200 */
2201 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2202 tmp_mode <<= 3;
2203 tmp_mode |= 0x7;
2204 mask &= tmp_mode;
2205 }
2206 }
2207 else {
2208 mask = 0xffffffff; /* shut up compiler warning */
2209 BUG();
2210 }
2211
2212 return mask;
2213}
2214
2215/* find greatest bit */
2216static int fgb(u32 bitmap)
2217{
2218 unsigned int i;
2219 int x = -1;
2220
2221 for (i = 0; i < 32; i++)
2222 if (bitmap & (1 << i))
2223 x = i;
2224
2225 return x;
2226}
2227
2228/**
2229 * ata_choose_xfer_mode - attempt to find best transfer mode
2230 * @ap: Port for which an xfer mode will be selected
2231 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2232 * @xfer_shift_out: (output) bit shift that selects this mode
2233 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002234 * Based on host and device capabilities, determine the
2235 * maximum transfer mode that is amenable to all.
2236 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002238 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 *
2240 * RETURNS:
2241 * Zero on success, negative on error.
2242 */
2243
Jeff Garzik057ace52005-10-22 14:27:05 -04002244static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 u8 *xfer_mode_out,
2246 unsigned int *xfer_shift_out)
2247{
2248 unsigned int mask, shift;
2249 int x, i;
2250
2251 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2252 shift = xfer_mode_classes[i].shift;
2253 mask = ata_get_mode_mask(ap, shift);
2254
2255 x = fgb(mask);
2256 if (x >= 0) {
2257 *xfer_mode_out = xfer_mode_classes[i].base + x;
2258 *xfer_shift_out = shift;
2259 return 0;
2260 }
2261 }
2262
2263 return -1;
2264}
2265
2266/**
2267 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2268 * @ap: Port associated with device @dev
2269 * @dev: Device to which command will be sent
2270 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002271 * Issue SET FEATURES - XFER MODE command to device @dev
2272 * on port @ap.
2273 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002275 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 */
2277
2278static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2279{
2280 DECLARE_COMPLETION(wait);
2281 struct ata_queued_cmd *qc;
2282 int rc;
2283 unsigned long flags;
2284
2285 /* set up set-features taskfile */
2286 DPRINTK("set features - xfer mode\n");
2287
2288 qc = ata_qc_new_init(ap, dev);
2289 BUG_ON(qc == NULL);
2290
2291 qc->tf.command = ATA_CMD_SET_FEATURES;
2292 qc->tf.feature = SETFEATURES_XFER;
2293 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2294 qc->tf.protocol = ATA_PROT_NODATA;
2295 qc->tf.nsect = dev->xfer_mode;
2296
2297 qc->waiting = &wait;
2298 qc->complete_fn = ata_qc_complete_noop;
2299
2300 spin_lock_irqsave(&ap->host_set->lock, flags);
2301 rc = ata_qc_issue(qc);
2302 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2303
2304 if (rc)
2305 ata_port_disable(ap);
2306 else
2307 wait_for_completion(&wait);
2308
2309 DPRINTK("EXIT\n");
2310}
2311
2312/**
Albert Lee59a10b12005-10-12 15:09:42 +08002313 * ata_dev_reread_id - Reread the device identify device info
2314 * @ap: port where the device is
2315 * @dev: device to reread the identify device info
2316 *
2317 * LOCKING:
2318 */
2319
2320static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2321{
2322 DECLARE_COMPLETION(wait);
2323 struct ata_queued_cmd *qc;
2324 unsigned long flags;
2325 int rc;
2326
2327 qc = ata_qc_new_init(ap, dev);
2328 BUG_ON(qc == NULL);
2329
2330 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2331 qc->dma_dir = DMA_FROM_DEVICE;
2332
2333 if (dev->class == ATA_DEV_ATA) {
2334 qc->tf.command = ATA_CMD_ID_ATA;
2335 DPRINTK("do ATA identify\n");
2336 } else {
2337 qc->tf.command = ATA_CMD_ID_ATAPI;
2338 DPRINTK("do ATAPI identify\n");
2339 }
2340
2341 qc->tf.flags |= ATA_TFLAG_DEVICE;
2342 qc->tf.protocol = ATA_PROT_PIO;
2343 qc->nsect = 1;
2344
2345 qc->waiting = &wait;
2346 qc->complete_fn = ata_qc_complete_noop;
2347
2348 spin_lock_irqsave(&ap->host_set->lock, flags);
2349 rc = ata_qc_issue(qc);
2350 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2351
2352 if (rc)
2353 goto err_out;
2354
2355 wait_for_completion(&wait);
2356
2357 swap_buf_le16(dev->id, ATA_ID_WORDS);
2358
2359 ata_dump_id(dev);
2360
2361 DPRINTK("EXIT\n");
2362
2363 return;
2364err_out:
2365 ata_port_disable(ap);
2366}
2367
2368/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002369 * ata_dev_init_params - Issue INIT DEV PARAMS command
2370 * @ap: Port associated with device @dev
2371 * @dev: Device to which command will be sent
2372 *
2373 * LOCKING:
2374 */
2375
2376static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2377{
2378 DECLARE_COMPLETION(wait);
2379 struct ata_queued_cmd *qc;
2380 int rc;
2381 unsigned long flags;
2382 u16 sectors = dev->id[6];
2383 u16 heads = dev->id[3];
2384
2385 /* Number of sectors per track 1-255. Number of heads 1-16 */
2386 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2387 return;
2388
2389 /* set up init dev params taskfile */
2390 DPRINTK("init dev params \n");
2391
2392 qc = ata_qc_new_init(ap, dev);
2393 BUG_ON(qc == NULL);
2394
2395 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2396 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2397 qc->tf.protocol = ATA_PROT_NODATA;
2398 qc->tf.nsect = sectors;
2399 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2400
2401 qc->waiting = &wait;
2402 qc->complete_fn = ata_qc_complete_noop;
2403
2404 spin_lock_irqsave(&ap->host_set->lock, flags);
2405 rc = ata_qc_issue(qc);
2406 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2407
2408 if (rc)
2409 ata_port_disable(ap);
2410 else
2411 wait_for_completion(&wait);
2412
2413 DPRINTK("EXIT\n");
2414}
2415
2416/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002417 * ata_sg_clean - Unmap DMA memory associated with command
2418 * @qc: Command containing DMA memory to be released
2419 *
2420 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 *
2422 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002423 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 */
2425
2426static void ata_sg_clean(struct ata_queued_cmd *qc)
2427{
2428 struct ata_port *ap = qc->ap;
2429 struct scatterlist *sg = qc->sg;
2430 int dir = qc->dma_dir;
2431
2432 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2433 assert(sg != NULL);
2434
2435 if (qc->flags & ATA_QCFLAG_SINGLE)
2436 assert(qc->n_elem == 1);
2437
2438 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2439
2440 if (qc->flags & ATA_QCFLAG_SG)
2441 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2442 else
2443 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2444 sg_dma_len(&sg[0]), dir);
2445
2446 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2447 qc->sg = NULL;
2448}
2449
2450/**
2451 * ata_fill_sg - Fill PCI IDE PRD table
2452 * @qc: Metadata associated with taskfile to be transferred
2453 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002454 * Fill PCI IDE PRD (scatter-gather) table with segments
2455 * associated with the current disk command.
2456 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002458 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 *
2460 */
2461static void ata_fill_sg(struct ata_queued_cmd *qc)
2462{
2463 struct scatterlist *sg = qc->sg;
2464 struct ata_port *ap = qc->ap;
2465 unsigned int idx, nelem;
2466
2467 assert(sg != NULL);
2468 assert(qc->n_elem > 0);
2469
2470 idx = 0;
2471 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2472 u32 addr, offset;
2473 u32 sg_len, len;
2474
2475 /* determine if physical DMA addr spans 64K boundary.
2476 * Note h/w doesn't support 64-bit, so we unconditionally
2477 * truncate dma_addr_t to u32.
2478 */
2479 addr = (u32) sg_dma_address(sg);
2480 sg_len = sg_dma_len(sg);
2481
2482 while (sg_len) {
2483 offset = addr & 0xffff;
2484 len = sg_len;
2485 if ((offset + sg_len) > 0x10000)
2486 len = 0x10000 - offset;
2487
2488 ap->prd[idx].addr = cpu_to_le32(addr);
2489 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2490 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2491
2492 idx++;
2493 sg_len -= len;
2494 addr += len;
2495 }
2496 }
2497
2498 if (idx)
2499 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2500}
2501/**
2502 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2503 * @qc: Metadata associated with taskfile to check
2504 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002505 * Allow low-level driver to filter ATA PACKET commands, returning
2506 * a status indicating whether or not it is OK to use DMA for the
2507 * supplied PACKET command.
2508 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002510 * spin_lock_irqsave(host_set lock)
2511 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 * RETURNS: 0 when ATAPI DMA can be used
2513 * nonzero otherwise
2514 */
2515int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2516{
2517 struct ata_port *ap = qc->ap;
2518 int rc = 0; /* Assume ATAPI DMA is OK by default */
2519
2520 if (ap->ops->check_atapi_dma)
2521 rc = ap->ops->check_atapi_dma(qc);
2522
2523 return rc;
2524}
2525/**
2526 * ata_qc_prep - Prepare taskfile for submission
2527 * @qc: Metadata associated with taskfile to be prepared
2528 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002529 * Prepare ATA taskfile for submission.
2530 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 * LOCKING:
2532 * spin_lock_irqsave(host_set lock)
2533 */
2534void ata_qc_prep(struct ata_queued_cmd *qc)
2535{
2536 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2537 return;
2538
2539 ata_fill_sg(qc);
2540}
2541
Jeff Garzik0cba6322005-05-30 19:49:12 -04002542/**
2543 * ata_sg_init_one - Associate command with memory buffer
2544 * @qc: Command to be associated
2545 * @buf: Memory buffer
2546 * @buflen: Length of memory buffer, in bytes.
2547 *
2548 * Initialize the data-related elements of queued_cmd @qc
2549 * to point to a single memory buffer, @buf of byte length @buflen.
2550 *
2551 * LOCKING:
2552 * spin_lock_irqsave(host_set lock)
2553 */
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2556{
2557 struct scatterlist *sg;
2558
2559 qc->flags |= ATA_QCFLAG_SINGLE;
2560
2561 memset(&qc->sgent, 0, sizeof(qc->sgent));
2562 qc->sg = &qc->sgent;
2563 qc->n_elem = 1;
2564 qc->buf_virt = buf;
2565
2566 sg = qc->sg;
2567 sg->page = virt_to_page(buf);
2568 sg->offset = (unsigned long) buf & ~PAGE_MASK;
Albert Lee32529e02005-05-26 03:49:42 -04002569 sg->length = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Jeff Garzik0cba6322005-05-30 19:49:12 -04002572/**
2573 * ata_sg_init - Associate command with scatter-gather table.
2574 * @qc: Command to be associated
2575 * @sg: Scatter-gather table.
2576 * @n_elem: Number of elements in s/g table.
2577 *
2578 * Initialize the data-related elements of queued_cmd @qc
2579 * to point to a scatter-gather table @sg, containing @n_elem
2580 * elements.
2581 *
2582 * LOCKING:
2583 * spin_lock_irqsave(host_set lock)
2584 */
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2587 unsigned int n_elem)
2588{
2589 qc->flags |= ATA_QCFLAG_SG;
2590 qc->sg = sg;
2591 qc->n_elem = n_elem;
2592}
2593
2594/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002595 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2596 * @qc: Command with memory buffer to be mapped.
2597 *
2598 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 *
2600 * LOCKING:
2601 * spin_lock_irqsave(host_set lock)
2602 *
2603 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002604 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 */
2606
2607static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2608{
2609 struct ata_port *ap = qc->ap;
2610 int dir = qc->dma_dir;
2611 struct scatterlist *sg = qc->sg;
2612 dma_addr_t dma_address;
2613
2614 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002615 sg->length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 if (dma_mapping_error(dma_address))
2617 return -1;
2618
2619 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002620 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2623 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2624
2625 return 0;
2626}
2627
2628/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002629 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2630 * @qc: Command with scatter-gather table to be mapped.
2631 *
2632 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 *
2634 * LOCKING:
2635 * spin_lock_irqsave(host_set lock)
2636 *
2637 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002638 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 *
2640 */
2641
2642static int ata_sg_setup(struct ata_queued_cmd *qc)
2643{
2644 struct ata_port *ap = qc->ap;
2645 struct scatterlist *sg = qc->sg;
2646 int n_elem, dir;
2647
2648 VPRINTK("ENTER, ata%u\n", ap->id);
2649 assert(qc->flags & ATA_QCFLAG_SG);
2650
2651 dir = qc->dma_dir;
2652 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2653 if (n_elem < 1)
2654 return -1;
2655
2656 DPRINTK("%d sg elements mapped\n", n_elem);
2657
2658 qc->n_elem = n_elem;
2659
2660 return 0;
2661}
2662
2663/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002664 * ata_poll_qc_complete - turn irq back on and finish qc
2665 * @qc: Command to complete
2666 * @drv_stat: ATA status register content
2667 *
2668 * LOCKING:
2669 * None. (grabs host lock)
2670 */
2671
2672void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2673{
2674 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002675 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002676
Jeff Garzikb8f61532005-08-25 22:01:20 -04002677 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002678 ap->flags &= ~ATA_FLAG_NOINTR;
2679 ata_irq_on(ap);
2680 ata_qc_complete(qc, drv_stat);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002681 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002682}
2683
2684/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002686 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 *
2688 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002689 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 *
2691 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002692 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 */
2694
2695static unsigned long ata_pio_poll(struct ata_port *ap)
2696{
2697 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002698 unsigned int poll_state = HSM_ST_UNKNOWN;
2699 unsigned int reg_state = HSM_ST_UNKNOWN;
2700 const unsigned int tmout_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Albert Lee14be71f2005-09-27 17:36:35 +08002702 switch (ap->hsm_task_state) {
2703 case HSM_ST:
2704 case HSM_ST_POLL:
2705 poll_state = HSM_ST_POLL;
2706 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002708 case HSM_ST_LAST:
2709 case HSM_ST_LAST_POLL:
2710 poll_state = HSM_ST_LAST_POLL;
2711 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 break;
2713 default:
2714 BUG();
2715 break;
2716 }
2717
2718 status = ata_chk_status(ap);
2719 if (status & ATA_BUSY) {
2720 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002721 ap->hsm_task_state = tmout_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return 0;
2723 }
Albert Lee14be71f2005-09-27 17:36:35 +08002724 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return ATA_SHORT_PAUSE;
2726 }
2727
Albert Lee14be71f2005-09-27 17:36:35 +08002728 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return 0;
2730}
2731
2732/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002733 * ata_pio_complete - check if drive is busy or idle
2734 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 *
2736 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002737 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002738 *
2739 * RETURNS:
2740 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 */
2742
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002743static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 struct ata_queued_cmd *qc;
2746 u8 drv_stat;
2747
2748 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002749 * This is purely heuristic. This is a fast path. Sometimes when
2750 * we enter, BSY will be cleared in a chk-status or two. If not,
2751 * the drive is probably seeking or something. Snooze for a couple
2752 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002753 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 */
2755 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2756 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2757 msleep(2);
2758 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2759 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002760 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
2764 }
2765
2766 drv_stat = ata_wait_idle(ap);
2767 if (!ata_ok(drv_stat)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002768 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
2771
2772 qc = ata_qc_from_tag(ap, ap->active_tag);
2773 assert(qc != NULL);
2774
Albert Lee14be71f2005-09-27 17:36:35 +08002775 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Tejun Heo40e8c822005-08-22 17:12:45 +09002777 ata_poll_qc_complete(qc, drv_stat);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002778
2779 /* another command may start at this point */
2780
2781 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782}
2783
Edward Falk0baab862005-06-02 18:17:13 -04002784
2785/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002786 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002787 * @buf: Buffer to swap
2788 * @buf_words: Number of 16-bit words in buffer.
2789 *
2790 * Swap halves of 16-bit words if needed to convert from
2791 * little-endian byte order to native cpu byte order, or
2792 * vice-versa.
2793 *
2794 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002795 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002796 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797void swap_buf_le16(u16 *buf, unsigned int buf_words)
2798{
2799#ifdef __BIG_ENDIAN
2800 unsigned int i;
2801
2802 for (i = 0; i < buf_words; i++)
2803 buf[i] = le16_to_cpu(buf[i]);
2804#endif /* __BIG_ENDIAN */
2805}
2806
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002807/**
2808 * ata_mmio_data_xfer - Transfer data by MMIO
2809 * @ap: port to read/write
2810 * @buf: data buffer
2811 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002812 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002813 *
2814 * Transfer data from/to the device data register by MMIO.
2815 *
2816 * LOCKING:
2817 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002818 */
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2821 unsigned int buflen, int write_data)
2822{
2823 unsigned int i;
2824 unsigned int words = buflen >> 1;
2825 u16 *buf16 = (u16 *) buf;
2826 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2827
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002828 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (write_data) {
2830 for (i = 0; i < words; i++)
2831 writew(le16_to_cpu(buf16[i]), mmio);
2832 } else {
2833 for (i = 0; i < words; i++)
2834 buf16[i] = cpu_to_le16(readw(mmio));
2835 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002836
2837 /* Transfer trailing 1 byte, if any. */
2838 if (unlikely(buflen & 0x01)) {
2839 u16 align_buf[1] = { 0 };
2840 unsigned char *trailing_buf = buf + buflen - 1;
2841
2842 if (write_data) {
2843 memcpy(align_buf, trailing_buf, 1);
2844 writew(le16_to_cpu(align_buf[0]), mmio);
2845 } else {
2846 align_buf[0] = cpu_to_le16(readw(mmio));
2847 memcpy(trailing_buf, align_buf, 1);
2848 }
2849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002852/**
2853 * ata_pio_data_xfer - Transfer data by PIO
2854 * @ap: port to read/write
2855 * @buf: data buffer
2856 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002857 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002858 *
2859 * Transfer data from/to the device data register by PIO.
2860 *
2861 * LOCKING:
2862 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002863 */
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2866 unsigned int buflen, int write_data)
2867{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002868 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002870 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002872 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002874 insw(ap->ioaddr.data_addr, buf, words);
2875
2876 /* Transfer trailing 1 byte, if any. */
2877 if (unlikely(buflen & 0x01)) {
2878 u16 align_buf[1] = { 0 };
2879 unsigned char *trailing_buf = buf + buflen - 1;
2880
2881 if (write_data) {
2882 memcpy(align_buf, trailing_buf, 1);
2883 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2884 } else {
2885 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2886 memcpy(trailing_buf, align_buf, 1);
2887 }
2888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
2890
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002891/**
2892 * ata_data_xfer - Transfer data from/to the data register.
2893 * @ap: port to read/write
2894 * @buf: data buffer
2895 * @buflen: buffer length
2896 * @do_write: read/write
2897 *
2898 * Transfer data from/to the device data register.
2899 *
2900 * LOCKING:
2901 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002902 */
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2905 unsigned int buflen, int do_write)
2906{
2907 if (ap->flags & ATA_FLAG_MMIO)
2908 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2909 else
2910 ata_pio_data_xfer(ap, buf, buflen, do_write);
2911}
2912
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002913/**
2914 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2915 * @qc: Command on going
2916 *
2917 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2918 *
2919 * LOCKING:
2920 * Inherited from caller.
2921 */
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923static void ata_pio_sector(struct ata_queued_cmd *qc)
2924{
2925 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2926 struct scatterlist *sg = qc->sg;
2927 struct ata_port *ap = qc->ap;
2928 struct page *page;
2929 unsigned int offset;
2930 unsigned char *buf;
2931
2932 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08002933 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 page = sg[qc->cursg].page;
2936 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2937
2938 /* get the current page and offset */
2939 page = nth_page(page, (offset >> PAGE_SHIFT));
2940 offset %= PAGE_SIZE;
2941
2942 buf = kmap(page) + offset;
2943
2944 qc->cursect++;
2945 qc->cursg_ofs++;
2946
Albert Lee32529e02005-05-26 03:49:42 -04002947 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 qc->cursg++;
2949 qc->cursg_ofs = 0;
2950 }
2951
2952 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2953
2954 /* do the actual data transfer */
2955 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2956 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2957
2958 kunmap(page);
2959}
2960
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002961/**
2962 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2963 * @qc: Command on going
2964 * @bytes: number of bytes
2965 *
2966 * Transfer Transfer data from/to the ATAPI device.
2967 *
2968 * LOCKING:
2969 * Inherited from caller.
2970 *
2971 */
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2974{
2975 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2976 struct scatterlist *sg = qc->sg;
2977 struct ata_port *ap = qc->ap;
2978 struct page *page;
2979 unsigned char *buf;
2980 unsigned int offset, count;
2981
Albert Lee563a6e12005-08-12 14:17:50 +08002982 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08002983 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08002986 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002987 /*
Albert Lee563a6e12005-08-12 14:17:50 +08002988 * The end of qc->sg is reached and the device expects
2989 * more data to transfer. In order not to overrun qc->sg
2990 * and fulfill length specified in the byte count register,
2991 * - for read case, discard trailing data from the device
2992 * - for write case, padding zero data to the device
2993 */
2994 u16 pad_buf[1] = { 0 };
2995 unsigned int words = bytes >> 1;
2996 unsigned int i;
2997
2998 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002999 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003000 ap->id, bytes);
3001
3002 for (i = 0; i < words; i++)
3003 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3004
Albert Lee14be71f2005-09-27 17:36:35 +08003005 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003006 return;
3007 }
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 sg = &qc->sg[qc->cursg];
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 page = sg->page;
3012 offset = sg->offset + qc->cursg_ofs;
3013
3014 /* get the current page and offset */
3015 page = nth_page(page, (offset >> PAGE_SHIFT));
3016 offset %= PAGE_SIZE;
3017
Albert Lee6952df02005-06-06 15:56:03 +08003018 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003019 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
3021 /* don't cross page boundaries */
3022 count = min(count, (unsigned int)PAGE_SIZE - offset);
3023
3024 buf = kmap(page) + offset;
3025
3026 bytes -= count;
3027 qc->curbytes += count;
3028 qc->cursg_ofs += count;
3029
Albert Lee32529e02005-05-26 03:49:42 -04003030 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 qc->cursg++;
3032 qc->cursg_ofs = 0;
3033 }
3034
3035 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3036
3037 /* do the actual data transfer */
3038 ata_data_xfer(ap, buf, count, do_write);
3039
3040 kunmap(page);
3041
Albert Lee563a6e12005-08-12 14:17:50 +08003042 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003046/**
3047 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3048 * @qc: Command on going
3049 *
3050 * Transfer Transfer data from/to the ATAPI device.
3051 *
3052 * LOCKING:
3053 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003054 */
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3057{
3058 struct ata_port *ap = qc->ap;
3059 struct ata_device *dev = qc->dev;
3060 unsigned int ireason, bc_lo, bc_hi, bytes;
3061 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3062
3063 ap->ops->tf_read(ap, &qc->tf);
3064 ireason = qc->tf.nsect;
3065 bc_lo = qc->tf.lbam;
3066 bc_hi = qc->tf.lbah;
3067 bytes = (bc_hi << 8) | bc_lo;
3068
3069 /* shall be cleared to zero, indicating xfer of data */
3070 if (ireason & (1 << 0))
3071 goto err_out;
3072
3073 /* make sure transfer direction matches expected */
3074 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3075 if (do_write != i_write)
3076 goto err_out;
3077
3078 __atapi_pio_bytes(qc, bytes);
3079
3080 return;
3081
3082err_out:
3083 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3084 ap->id, dev->devno);
Albert Lee14be71f2005-09-27 17:36:35 +08003085 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
3088/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003089 * ata_pio_block - start PIO on a block
3090 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 *
3092 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003093 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 */
3095
3096static void ata_pio_block(struct ata_port *ap)
3097{
3098 struct ata_queued_cmd *qc;
3099 u8 status;
3100
3101 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003102 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 * Sometimes when we enter, BSY will be cleared in
3104 * a chk-status or two. If not, the drive is probably seeking
3105 * or something. Snooze for a couple msecs, then
3106 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003107 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 */
3109 status = ata_busy_wait(ap, ATA_BUSY, 5);
3110 if (status & ATA_BUSY) {
3111 msleep(2);
3112 status = ata_busy_wait(ap, ATA_BUSY, 10);
3113 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003114 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3116 return;
3117 }
3118 }
3119
3120 qc = ata_qc_from_tag(ap, ap->active_tag);
3121 assert(qc != NULL);
3122
3123 if (is_atapi_taskfile(&qc->tf)) {
3124 /* no more data to transfer or unsupported ATAPI command */
3125 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003126 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 return;
3128 }
3129
3130 atapi_pio_bytes(qc);
3131 } else {
3132 /* handle BSY=0, DRQ=0 as error */
3133 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003134 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return;
3136 }
3137
3138 ata_pio_sector(qc);
3139 }
3140}
3141
3142static void ata_pio_error(struct ata_port *ap)
3143{
3144 struct ata_queued_cmd *qc;
3145 u8 drv_stat;
3146
3147 qc = ata_qc_from_tag(ap, ap->active_tag);
3148 assert(qc != NULL);
3149
3150 drv_stat = ata_chk_status(ap);
3151 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
3152 ap->id, drv_stat);
3153
Albert Lee14be71f2005-09-27 17:36:35 +08003154 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Tejun Heo40e8c822005-08-22 17:12:45 +09003156 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157}
3158
3159static void ata_pio_task(void *_data)
3160{
3161 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003162 unsigned long timeout;
3163 int qc_completed;
3164
3165fsm_start:
3166 timeout = 0;
3167 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Albert Lee14be71f2005-09-27 17:36:35 +08003169 switch (ap->hsm_task_state) {
3170 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return;
3172
Albert Lee14be71f2005-09-27 17:36:35 +08003173 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 ata_pio_block(ap);
3175 break;
3176
Albert Lee14be71f2005-09-27 17:36:35 +08003177 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003178 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 break;
3180
Albert Lee14be71f2005-09-27 17:36:35 +08003181 case HSM_ST_POLL:
3182 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 timeout = ata_pio_poll(ap);
3184 break;
3185
Albert Lee14be71f2005-09-27 17:36:35 +08003186 case HSM_ST_TMOUT:
3187 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 ata_pio_error(ap);
3189 return;
3190 }
3191
3192 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003193 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3194 else if (!qc_completed)
3195 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198/**
3199 * ata_qc_timeout - Handle timeout of queued command
3200 * @qc: Command that timed out
3201 *
3202 * Some part of the kernel (currently, only the SCSI layer)
3203 * has noticed that the active command on port @ap has not
3204 * completed after a specified length of time. Handle this
3205 * condition by disabling DMA (if necessary) and completing
3206 * transactions, with error if necessary.
3207 *
3208 * This also handles the case of the "lost interrupt", where
3209 * for some reason (possibly hardware bug, possibly driver bug)
3210 * an interrupt was not delivered to the driver, even though the
3211 * transaction completed successfully.
3212 *
3213 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003214 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 */
3216
3217static void ata_qc_timeout(struct ata_queued_cmd *qc)
3218{
3219 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003220 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 struct ata_device *dev = qc->dev;
3222 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003223 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
3225 DPRINTK("ENTER\n");
3226
3227 /* FIXME: doesn't this conflict with timeout handling? */
3228 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3229 struct scsi_cmnd *cmd = qc->scsicmd;
3230
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02003231 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
3233 /* finish completing original command */
Jeff Garzikb8f61532005-08-25 22:01:20 -04003234 spin_lock_irqsave(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 __ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003236 spin_unlock_irqrestore(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
3238 atapi_request_sense(ap, dev, cmd);
3239
3240 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3241 scsi_finish_command(cmd);
3242
3243 goto out;
3244 }
3245 }
3246
Jeff Garzikb8f61532005-08-25 22:01:20 -04003247 spin_lock_irqsave(&host_set->lock, flags);
3248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 /* hack alert! We cannot use the supplied completion
3250 * function from inside the ->eh_strategy_handler() thread.
3251 * libata is the only user of ->eh_strategy_handler() in
3252 * any kernel, so the default scsi_done() assumes it is
3253 * not being called from the SCSI EH.
3254 */
3255 qc->scsidone = scsi_finish_command;
3256
3257 switch (qc->tf.protocol) {
3258
3259 case ATA_PROT_DMA:
3260 case ATA_PROT_ATAPI_DMA:
3261 host_stat = ap->ops->bmdma_status(ap);
3262
3263 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003264 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 /* fall through */
3267
3268 default:
3269 ata_altstatus(ap);
3270 drv_stat = ata_chk_status(ap);
3271
3272 /* ack bmdma irq events */
3273 ap->ops->irq_clear(ap);
3274
3275 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3276 ap->id, qc->tf.command, drv_stat, host_stat);
3277
3278 /* complete taskfile transaction */
3279 ata_qc_complete(qc, drv_stat);
3280 break;
3281 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003282
3283 spin_unlock_irqrestore(&host_set->lock, flags);
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285out:
3286 DPRINTK("EXIT\n");
3287}
3288
3289/**
3290 * ata_eng_timeout - Handle timeout of queued command
3291 * @ap: Port on which timed-out command is active
3292 *
3293 * Some part of the kernel (currently, only the SCSI layer)
3294 * has noticed that the active command on port @ap has not
3295 * completed after a specified length of time. Handle this
3296 * condition by disabling DMA (if necessary) and completing
3297 * transactions, with error if necessary.
3298 *
3299 * This also handles the case of the "lost interrupt", where
3300 * for some reason (possibly hardware bug, possibly driver bug)
3301 * an interrupt was not delivered to the driver, even though the
3302 * transaction completed successfully.
3303 *
3304 * LOCKING:
3305 * Inherited from SCSI layer (none, can sleep)
3306 */
3307
3308void ata_eng_timeout(struct ata_port *ap)
3309{
3310 struct ata_queued_cmd *qc;
3311
3312 DPRINTK("ENTER\n");
3313
3314 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003315 if (qc)
3316 ata_qc_timeout(qc);
3317 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3319 ap->id);
3320 goto out;
3321 }
3322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323out:
3324 DPRINTK("EXIT\n");
3325}
3326
3327/**
3328 * ata_qc_new - Request an available ATA command, for queueing
3329 * @ap: Port associated with device @dev
3330 * @dev: Device from whom we request an available command structure
3331 *
3332 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003333 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 */
3335
3336static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3337{
3338 struct ata_queued_cmd *qc = NULL;
3339 unsigned int i;
3340
3341 for (i = 0; i < ATA_MAX_QUEUE; i++)
3342 if (!test_and_set_bit(i, &ap->qactive)) {
3343 qc = ata_qc_from_tag(ap, i);
3344 break;
3345 }
3346
3347 if (qc)
3348 qc->tag = i;
3349
3350 return qc;
3351}
3352
3353/**
3354 * ata_qc_new_init - Request an available ATA command, and initialize it
3355 * @ap: Port associated with device @dev
3356 * @dev: Device from whom we request an available command structure
3357 *
3358 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003359 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 */
3361
3362struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3363 struct ata_device *dev)
3364{
3365 struct ata_queued_cmd *qc;
3366
3367 qc = ata_qc_new(ap);
3368 if (qc) {
3369 qc->sg = NULL;
3370 qc->flags = 0;
3371 qc->scsicmd = NULL;
3372 qc->ap = ap;
3373 qc->dev = dev;
3374 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3375 qc->nsect = 0;
3376 qc->nbytes = qc->curbytes = 0;
3377
3378 ata_tf_init(ap, &qc->tf, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 }
3380
3381 return qc;
3382}
3383
Jeff Garzika939c962005-10-05 17:09:16 -04003384int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385{
3386 return 0;
3387}
3388
3389static void __ata_qc_complete(struct ata_queued_cmd *qc)
3390{
3391 struct ata_port *ap = qc->ap;
3392 unsigned int tag, do_clear = 0;
3393
3394 qc->flags = 0;
3395 tag = qc->tag;
3396 if (likely(ata_tag_valid(tag))) {
3397 if (tag == ap->active_tag)
3398 ap->active_tag = ATA_TAG_POISON;
3399 qc->tag = ATA_TAG_POISON;
3400 do_clear = 1;
3401 }
3402
3403 if (qc->waiting) {
3404 struct completion *waiting = qc->waiting;
3405 qc->waiting = NULL;
3406 complete(waiting);
3407 }
3408
3409 if (likely(do_clear))
3410 clear_bit(tag, &ap->qactive);
3411}
3412
3413/**
3414 * ata_qc_free - free unused ata_queued_cmd
3415 * @qc: Command to complete
3416 *
3417 * Designed to free unused ata_queued_cmd object
3418 * in case something prevents using it.
3419 *
3420 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003421 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 */
3423void ata_qc_free(struct ata_queued_cmd *qc)
3424{
3425 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3426 assert(qc->waiting == NULL); /* nothing should be waiting */
3427
3428 __ata_qc_complete(qc);
3429}
3430
3431/**
3432 * ata_qc_complete - Complete an active ATA command
3433 * @qc: Command to complete
Jeff Garzik0cba6322005-05-30 19:49:12 -04003434 * @drv_stat: ATA Status register contents
3435 *
3436 * Indicate to the mid and upper layers that an ATA
3437 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 *
3439 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003440 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 */
3442
3443void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3444{
3445 int rc;
3446
3447 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3448 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3449
3450 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3451 ata_sg_clean(qc);
3452
Albert Lee3f3791d2005-08-16 14:25:38 +08003453 /* atapi: mark qc as inactive to prevent the interrupt handler
3454 * from completing the command twice later, before the error handler
3455 * is called. (when rc != 0 and atapi request sense is needed)
3456 */
3457 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 /* call completion callback */
3460 rc = qc->complete_fn(qc, drv_stat);
3461
3462 /* if callback indicates not to complete command (non-zero),
3463 * return immediately
3464 */
3465 if (rc != 0)
3466 return;
3467
3468 __ata_qc_complete(qc);
3469
3470 VPRINTK("EXIT\n");
3471}
3472
3473static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3474{
3475 struct ata_port *ap = qc->ap;
3476
3477 switch (qc->tf.protocol) {
3478 case ATA_PROT_DMA:
3479 case ATA_PROT_ATAPI_DMA:
3480 return 1;
3481
3482 case ATA_PROT_ATAPI:
3483 case ATA_PROT_PIO:
3484 case ATA_PROT_PIO_MULT:
3485 if (ap->flags & ATA_FLAG_PIO_DMA)
3486 return 1;
3487
3488 /* fall through */
3489
3490 default:
3491 return 0;
3492 }
3493
3494 /* never reached */
3495}
3496
3497/**
3498 * ata_qc_issue - issue taskfile to device
3499 * @qc: command to issue to device
3500 *
3501 * Prepare an ATA command to submission to device.
3502 * This includes mapping the data into a DMA-able
3503 * area, filling in the S/G table, and finally
3504 * writing the taskfile to hardware, starting the command.
3505 *
3506 * LOCKING:
3507 * spin_lock_irqsave(host_set lock)
3508 *
3509 * RETURNS:
3510 * Zero on success, negative on error.
3511 */
3512
3513int ata_qc_issue(struct ata_queued_cmd *qc)
3514{
3515 struct ata_port *ap = qc->ap;
3516
3517 if (ata_should_dma_map(qc)) {
3518 if (qc->flags & ATA_QCFLAG_SG) {
3519 if (ata_sg_setup(qc))
3520 goto err_out;
3521 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3522 if (ata_sg_setup_one(qc))
3523 goto err_out;
3524 }
3525 } else {
3526 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3527 }
3528
3529 ap->ops->qc_prep(qc);
3530
3531 qc->ap->active_tag = qc->tag;
3532 qc->flags |= ATA_QCFLAG_ACTIVE;
3533
3534 return ap->ops->qc_issue(qc);
3535
3536err_out:
3537 return -1;
3538}
3539
Edward Falk0baab862005-06-02 18:17:13 -04003540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541/**
3542 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3543 * @qc: command to issue to device
3544 *
3545 * Using various libata functions and hooks, this function
3546 * starts an ATA command. ATA commands are grouped into
3547 * classes called "protocols", and issuing each type of protocol
3548 * is slightly different.
3549 *
Edward Falk0baab862005-06-02 18:17:13 -04003550 * May be used as the qc_issue() entry in ata_port_operations.
3551 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 * LOCKING:
3553 * spin_lock_irqsave(host_set lock)
3554 *
3555 * RETURNS:
3556 * Zero on success, negative on error.
3557 */
3558
3559int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3560{
3561 struct ata_port *ap = qc->ap;
3562
3563 ata_dev_select(ap, qc->dev->devno, 1, 0);
3564
3565 switch (qc->tf.protocol) {
3566 case ATA_PROT_NODATA:
3567 ata_tf_to_host_nolock(ap, &qc->tf);
3568 break;
3569
3570 case ATA_PROT_DMA:
3571 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3572 ap->ops->bmdma_setup(qc); /* set up bmdma */
3573 ap->ops->bmdma_start(qc); /* initiate bmdma */
3574 break;
3575
3576 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3577 ata_qc_set_polling(qc);
3578 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003579 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 queue_work(ata_wq, &ap->pio_task);
3581 break;
3582
3583 case ATA_PROT_ATAPI:
3584 ata_qc_set_polling(qc);
3585 ata_tf_to_host_nolock(ap, &qc->tf);
3586 queue_work(ata_wq, &ap->packet_task);
3587 break;
3588
3589 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003590 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 ata_tf_to_host_nolock(ap, &qc->tf);
3592 queue_work(ata_wq, &ap->packet_task);
3593 break;
3594
3595 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003596 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3598 ap->ops->bmdma_setup(qc); /* set up bmdma */
3599 queue_work(ata_wq, &ap->packet_task);
3600 break;
3601
3602 default:
3603 WARN_ON(1);
3604 return -1;
3605 }
3606
3607 return 0;
3608}
3609
3610/**
Edward Falk0baab862005-06-02 18:17:13 -04003611 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 * @qc: Info associated with this ATA transaction.
3613 *
3614 * LOCKING:
3615 * spin_lock_irqsave(host_set lock)
3616 */
3617
3618static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3619{
3620 struct ata_port *ap = qc->ap;
3621 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3622 u8 dmactl;
3623 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3624
3625 /* load PRD table addr. */
3626 mb(); /* make sure PRD table writes are visible to controller */
3627 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3628
3629 /* specify data direction, triple-check start bit is clear */
3630 dmactl = readb(mmio + ATA_DMA_CMD);
3631 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3632 if (!rw)
3633 dmactl |= ATA_DMA_WR;
3634 writeb(dmactl, mmio + ATA_DMA_CMD);
3635
3636 /* issue r/w command */
3637 ap->ops->exec_command(ap, &qc->tf);
3638}
3639
3640/**
Alan Coxb73fc892005-08-26 16:03:19 +01003641 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 * @qc: Info associated with this ATA transaction.
3643 *
3644 * LOCKING:
3645 * spin_lock_irqsave(host_set lock)
3646 */
3647
3648static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3649{
3650 struct ata_port *ap = qc->ap;
3651 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3652 u8 dmactl;
3653
3654 /* start host DMA transaction */
3655 dmactl = readb(mmio + ATA_DMA_CMD);
3656 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3657
3658 /* Strictly, one may wish to issue a readb() here, to
3659 * flush the mmio write. However, control also passes
3660 * to the hardware at this point, and it will interrupt
3661 * us when we are to resume control. So, in effect,
3662 * we don't care when the mmio write flushes.
3663 * Further, a read of the DMA status register _immediately_
3664 * following the write may not be what certain flaky hardware
3665 * is expected, so I think it is best to not add a readb()
3666 * without first all the MMIO ATA cards/mobos.
3667 * Or maybe I'm just being paranoid.
3668 */
3669}
3670
3671/**
3672 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3673 * @qc: Info associated with this ATA transaction.
3674 *
3675 * LOCKING:
3676 * spin_lock_irqsave(host_set lock)
3677 */
3678
3679static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3680{
3681 struct ata_port *ap = qc->ap;
3682 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3683 u8 dmactl;
3684
3685 /* load PRD table addr. */
3686 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3687
3688 /* specify data direction, triple-check start bit is clear */
3689 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3690 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3691 if (!rw)
3692 dmactl |= ATA_DMA_WR;
3693 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3694
3695 /* issue r/w command */
3696 ap->ops->exec_command(ap, &qc->tf);
3697}
3698
3699/**
3700 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3701 * @qc: Info associated with this ATA transaction.
3702 *
3703 * LOCKING:
3704 * spin_lock_irqsave(host_set lock)
3705 */
3706
3707static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3708{
3709 struct ata_port *ap = qc->ap;
3710 u8 dmactl;
3711
3712 /* start host DMA transaction */
3713 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3714 outb(dmactl | ATA_DMA_START,
3715 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3716}
3717
Edward Falk0baab862005-06-02 18:17:13 -04003718
3719/**
3720 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3721 * @qc: Info associated with this ATA transaction.
3722 *
3723 * Writes the ATA_DMA_START flag to the DMA command register.
3724 *
3725 * May be used as the bmdma_start() entry in ata_port_operations.
3726 *
3727 * LOCKING:
3728 * spin_lock_irqsave(host_set lock)
3729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730void ata_bmdma_start(struct ata_queued_cmd *qc)
3731{
3732 if (qc->ap->flags & ATA_FLAG_MMIO)
3733 ata_bmdma_start_mmio(qc);
3734 else
3735 ata_bmdma_start_pio(qc);
3736}
3737
Edward Falk0baab862005-06-02 18:17:13 -04003738
3739/**
3740 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3741 * @qc: Info associated with this ATA transaction.
3742 *
3743 * Writes address of PRD table to device's PRD Table Address
3744 * register, sets the DMA control register, and calls
3745 * ops->exec_command() to start the transfer.
3746 *
3747 * May be used as the bmdma_setup() entry in ata_port_operations.
3748 *
3749 * LOCKING:
3750 * spin_lock_irqsave(host_set lock)
3751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752void ata_bmdma_setup(struct ata_queued_cmd *qc)
3753{
3754 if (qc->ap->flags & ATA_FLAG_MMIO)
3755 ata_bmdma_setup_mmio(qc);
3756 else
3757 ata_bmdma_setup_pio(qc);
3758}
3759
Edward Falk0baab862005-06-02 18:17:13 -04003760
3761/**
3762 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003763 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003764 *
3765 * Clear interrupt and error flags in DMA status register.
3766 *
3767 * May be used as the irq_clear() entry in ata_port_operations.
3768 *
3769 * LOCKING:
3770 * spin_lock_irqsave(host_set lock)
3771 */
3772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773void ata_bmdma_irq_clear(struct ata_port *ap)
3774{
3775 if (ap->flags & ATA_FLAG_MMIO) {
3776 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3777 writeb(readb(mmio), mmio);
3778 } else {
3779 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3780 outb(inb(addr), addr);
3781 }
3782
3783}
3784
Edward Falk0baab862005-06-02 18:17:13 -04003785
3786/**
3787 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003788 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003789 *
3790 * Read and return BMDMA status register.
3791 *
3792 * May be used as the bmdma_status() entry in ata_port_operations.
3793 *
3794 * LOCKING:
3795 * spin_lock_irqsave(host_set lock)
3796 */
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798u8 ata_bmdma_status(struct ata_port *ap)
3799{
3800 u8 host_stat;
3801 if (ap->flags & ATA_FLAG_MMIO) {
3802 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3803 host_stat = readb(mmio + ATA_DMA_STATUS);
3804 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003805 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 return host_stat;
3807}
3808
Edward Falk0baab862005-06-02 18:17:13 -04003809
3810/**
3811 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003812 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003813 *
3814 * Clears the ATA_DMA_START flag in the dma control register
3815 *
3816 * May be used as the bmdma_stop() entry in ata_port_operations.
3817 *
3818 * LOCKING:
3819 * spin_lock_irqsave(host_set lock)
3820 */
3821
Alan Coxb73fc892005-08-26 16:03:19 +01003822void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823{
Alan Coxb73fc892005-08-26 16:03:19 +01003824 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 if (ap->flags & ATA_FLAG_MMIO) {
3826 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3827
3828 /* clear start/stop bit */
3829 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3830 mmio + ATA_DMA_CMD);
3831 } else {
3832 /* clear start/stop bit */
3833 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3834 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3835 }
3836
3837 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3838 ata_altstatus(ap); /* dummy read */
3839}
3840
3841/**
3842 * ata_host_intr - Handle host interrupt for given (port, task)
3843 * @ap: Port on which interrupt arrived (possibly...)
3844 * @qc: Taskfile currently active in engine
3845 *
3846 * Handle host interrupt for given queued command. Currently,
3847 * only DMA interrupts are handled. All other commands are
3848 * handled via polling with interrupts disabled (nIEN bit).
3849 *
3850 * LOCKING:
3851 * spin_lock_irqsave(host_set lock)
3852 *
3853 * RETURNS:
3854 * One if interrupt was handled, zero if not (shared irq).
3855 */
3856
3857inline unsigned int ata_host_intr (struct ata_port *ap,
3858 struct ata_queued_cmd *qc)
3859{
3860 u8 status, host_stat;
3861
3862 switch (qc->tf.protocol) {
3863
3864 case ATA_PROT_DMA:
3865 case ATA_PROT_ATAPI_DMA:
3866 case ATA_PROT_ATAPI:
3867 /* check status of DMA engine */
3868 host_stat = ap->ops->bmdma_status(ap);
3869 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3870
3871 /* if it's not our irq... */
3872 if (!(host_stat & ATA_DMA_INTR))
3873 goto idle_irq;
3874
3875 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003876 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 /* fall through */
3879
3880 case ATA_PROT_ATAPI_NODATA:
3881 case ATA_PROT_NODATA:
3882 /* check altstatus */
3883 status = ata_altstatus(ap);
3884 if (status & ATA_BUSY)
3885 goto idle_irq;
3886
3887 /* check main status, clearing INTRQ */
3888 status = ata_chk_status(ap);
3889 if (unlikely(status & ATA_BUSY))
3890 goto idle_irq;
3891 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3892 ap->id, qc->tf.protocol, status);
3893
3894 /* ack bmdma irq events */
3895 ap->ops->irq_clear(ap);
3896
3897 /* complete taskfile transaction */
3898 ata_qc_complete(qc, status);
3899 break;
3900
3901 default:
3902 goto idle_irq;
3903 }
3904
3905 return 1; /* irq handled */
3906
3907idle_irq:
3908 ap->stats.idle_irq++;
3909
3910#ifdef ATA_IRQ_TRAP
3911 if ((ap->stats.idle_irq % 1000) == 0) {
3912 handled = 1;
3913 ata_irq_ack(ap, 0); /* debug trap */
3914 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3915 }
3916#endif
3917 return 0; /* irq not handled */
3918}
3919
3920/**
3921 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04003922 * @irq: irq line (unused)
3923 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 * @regs: unused
3925 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04003926 * Default interrupt handler for PCI IDE devices. Calls
3927 * ata_host_intr() for each port that is not disabled.
3928 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003930 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 *
3932 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003933 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 */
3935
3936irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3937{
3938 struct ata_host_set *host_set = dev_instance;
3939 unsigned int i;
3940 unsigned int handled = 0;
3941 unsigned long flags;
3942
3943 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3944 spin_lock_irqsave(&host_set->lock, flags);
3945
3946 for (i = 0; i < host_set->n_ports; i++) {
3947 struct ata_port *ap;
3948
3949 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09003950 if (ap &&
3951 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 struct ata_queued_cmd *qc;
3953
3954 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08003955 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3956 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 handled |= ata_host_intr(ap, qc);
3958 }
3959 }
3960
3961 spin_unlock_irqrestore(&host_set->lock, flags);
3962
3963 return IRQ_RETVAL(handled);
3964}
3965
3966/**
3967 * atapi_packet_task - Write CDB bytes to hardware
3968 * @_data: Port to which ATAPI device is attached.
3969 *
3970 * When device has indicated its readiness to accept
3971 * a CDB, this function is called. Send the CDB.
3972 * If DMA is to be performed, exit immediately.
3973 * Otherwise, we are in polling mode, so poll
3974 * status under operation succeeds or fails.
3975 *
3976 * LOCKING:
3977 * Kernel thread context (may sleep)
3978 */
3979
3980static void atapi_packet_task(void *_data)
3981{
3982 struct ata_port *ap = _data;
3983 struct ata_queued_cmd *qc;
3984 u8 status;
3985
3986 qc = ata_qc_from_tag(ap, ap->active_tag);
3987 assert(qc != NULL);
3988 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3989
3990 /* sleep-wait for BSY to clear */
3991 DPRINTK("busy wait\n");
3992 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3993 goto err_out;
3994
3995 /* make sure DRQ is set */
3996 status = ata_chk_status(ap);
3997 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3998 goto err_out;
3999
4000 /* send SCSI cdb */
4001 DPRINTK("send cdb\n");
4002 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Tejun Heoc1389502005-08-22 14:59:24 +09004004 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4005 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4006 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Tejun Heoc1389502005-08-22 14:59:24 +09004008 /* Once we're done issuing command and kicking bmdma,
4009 * irq handler takes over. To not lose irq, we need
4010 * to clear NOINTR flag before sending cdb, but
4011 * interrupt handler shouldn't be invoked before we're
4012 * finished. Hence, the following locking.
4013 */
4014 spin_lock_irqsave(&ap->host_set->lock, flags);
4015 ap->flags &= ~ATA_FLAG_NOINTR;
4016 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4017 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4018 ap->ops->bmdma_start(qc); /* initiate bmdma */
4019 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4020 } else {
4021 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Tejun Heoc1389502005-08-22 14:59:24 +09004023 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004024 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 queue_work(ata_wq, &ap->pio_task);
4026 }
4027
4028 return;
4029
4030err_out:
Tejun Heo40e8c822005-08-22 17:12:45 +09004031 ata_poll_qc_complete(qc, ATA_ERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032}
4033
Edward Falk0baab862005-06-02 18:17:13 -04004034
4035/**
4036 * ata_port_start - Set port up for dma.
4037 * @ap: Port to initialize
4038 *
4039 * Called just after data structures for each port are
4040 * initialized. Allocates space for PRD table.
4041 *
4042 * May be used as the port_start() entry in ata_port_operations.
4043 *
4044 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004045 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004046 */
4047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048int ata_port_start (struct ata_port *ap)
4049{
4050 struct device *dev = ap->host_set->dev;
4051
4052 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4053 if (!ap->prd)
4054 return -ENOMEM;
4055
4056 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4057
4058 return 0;
4059}
4060
Edward Falk0baab862005-06-02 18:17:13 -04004061
4062/**
4063 * ata_port_stop - Undo ata_port_start()
4064 * @ap: Port to shut down
4065 *
4066 * Frees the PRD table.
4067 *
4068 * May be used as the port_stop() entry in ata_port_operations.
4069 *
4070 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004071 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004072 */
4073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074void ata_port_stop (struct ata_port *ap)
4075{
4076 struct device *dev = ap->host_set->dev;
4077
4078 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4079}
4080
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004081void ata_host_stop (struct ata_host_set *host_set)
4082{
4083 if (host_set->mmio_base)
4084 iounmap(host_set->mmio_base);
4085}
4086
4087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088/**
4089 * ata_host_remove - Unregister SCSI host structure with upper layers
4090 * @ap: Port to unregister
4091 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4092 *
4093 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004094 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 */
4096
4097static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4098{
4099 struct Scsi_Host *sh = ap->host;
4100
4101 DPRINTK("ENTER\n");
4102
4103 if (do_unregister)
4104 scsi_remove_host(sh);
4105
4106 ap->ops->port_stop(ap);
4107}
4108
4109/**
4110 * ata_host_init - Initialize an ata_port structure
4111 * @ap: Structure to initialize
4112 * @host: associated SCSI mid-layer structure
4113 * @host_set: Collection of hosts to which @ap belongs
4114 * @ent: Probe information provided by low-level driver
4115 * @port_no: Port number associated with this ata_port
4116 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004117 * Initialize a new ata_port structure, and its associated
4118 * scsi_host.
4119 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004121 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 */
4123
4124static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4125 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004126 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127{
4128 unsigned int i;
4129
4130 host->max_id = 16;
4131 host->max_lun = 1;
4132 host->max_channel = 1;
4133 host->unique_id = ata_unique_id++;
4134 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 scsi_assign_lock(host, &host_set->lock);
4137
4138 ap->flags = ATA_FLAG_PORT_DISABLED;
4139 ap->id = host->unique_id;
4140 ap->host = host;
4141 ap->ctl = ATA_DEVCTL_OBS;
4142 ap->host_set = host_set;
4143 ap->port_no = port_no;
4144 ap->hard_port_no =
4145 ent->legacy_mode ? ent->hard_port_no : port_no;
4146 ap->pio_mask = ent->pio_mask;
4147 ap->mwdma_mask = ent->mwdma_mask;
4148 ap->udma_mask = ent->udma_mask;
4149 ap->flags |= ent->host_flags;
4150 ap->ops = ent->port_ops;
4151 ap->cbl = ATA_CBL_NONE;
4152 ap->active_tag = ATA_TAG_POISON;
4153 ap->last_ctl = 0xFF;
4154
4155 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4156 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4157
4158 for (i = 0; i < ATA_MAX_DEVICES; i++)
4159 ap->device[i].devno = i;
4160
4161#ifdef ATA_IRQ_TRAP
4162 ap->stats.unhandled_irq = 1;
4163 ap->stats.idle_irq = 1;
4164#endif
4165
4166 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4167}
4168
4169/**
4170 * ata_host_add - Attach low-level ATA driver to system
4171 * @ent: Information provided by low-level driver
4172 * @host_set: Collections of ports to which we add
4173 * @port_no: Port number associated with this host
4174 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004175 * Attach low-level ATA driver to system.
4176 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004178 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 *
4180 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004181 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 */
4183
Jeff Garzik057ace52005-10-22 14:27:05 -04004184static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 struct ata_host_set *host_set,
4186 unsigned int port_no)
4187{
4188 struct Scsi_Host *host;
4189 struct ata_port *ap;
4190 int rc;
4191
4192 DPRINTK("ENTER\n");
4193 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4194 if (!host)
4195 return NULL;
4196
4197 ap = (struct ata_port *) &host->hostdata[0];
4198
4199 ata_host_init(ap, host, host_set, ent, port_no);
4200
4201 rc = ap->ops->port_start(ap);
4202 if (rc)
4203 goto err_out;
4204
4205 return ap;
4206
4207err_out:
4208 scsi_host_put(host);
4209 return NULL;
4210}
4211
4212/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004213 * ata_device_add - Register hardware device with ATA and SCSI layers
4214 * @ent: Probe information describing hardware device to be registered
4215 *
4216 * This function processes the information provided in the probe
4217 * information struct @ent, allocates the necessary ATA and SCSI
4218 * host information structures, initializes them, and registers
4219 * everything with requisite kernel subsystems.
4220 *
4221 * This function requests irqs, probes the ATA bus, and probes
4222 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 *
4224 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004225 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 *
4227 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004228 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 */
4230
Jeff Garzik057ace52005-10-22 14:27:05 -04004231int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
4233 unsigned int count = 0, i;
4234 struct device *dev = ent->dev;
4235 struct ata_host_set *host_set;
4236
4237 DPRINTK("ENTER\n");
4238 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004239 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4241 if (!host_set)
4242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 spin_lock_init(&host_set->lock);
4244
4245 host_set->dev = dev;
4246 host_set->n_ports = ent->n_ports;
4247 host_set->irq = ent->irq;
4248 host_set->mmio_base = ent->mmio_base;
4249 host_set->private_data = ent->private_data;
4250 host_set->ops = ent->port_ops;
4251
4252 /* register each port bound to this device */
4253 for (i = 0; i < ent->n_ports; i++) {
4254 struct ata_port *ap;
4255 unsigned long xfer_mode_mask;
4256
4257 ap = ata_host_add(ent, host_set, i);
4258 if (!ap)
4259 goto err_out;
4260
4261 host_set->ports[i] = ap;
4262 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4263 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4264 (ap->pio_mask << ATA_SHIFT_PIO);
4265
4266 /* print per-port info to dmesg */
4267 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4268 "bmdma 0x%lX irq %lu\n",
4269 ap->id,
4270 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4271 ata_mode_string(xfer_mode_mask),
4272 ap->ioaddr.cmd_addr,
4273 ap->ioaddr.ctl_addr,
4274 ap->ioaddr.bmdma_addr,
4275 ent->irq);
4276
4277 ata_chk_status(ap);
4278 host_set->ops->irq_clear(ap);
4279 count++;
4280 }
4281
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004282 if (!count)
4283 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 /* obtain irq, that is shared between channels */
4286 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4287 DRV_NAME, host_set))
4288 goto err_out;
4289
4290 /* perform each probe synchronously */
4291 DPRINTK("probe begin\n");
4292 for (i = 0; i < count; i++) {
4293 struct ata_port *ap;
4294 int rc;
4295
4296 ap = host_set->ports[i];
4297
4298 DPRINTK("ata%u: probe begin\n", ap->id);
4299 rc = ata_bus_probe(ap);
4300 DPRINTK("ata%u: probe end\n", ap->id);
4301
4302 if (rc) {
4303 /* FIXME: do something useful here?
4304 * Current libata behavior will
4305 * tear down everything when
4306 * the module is removed
4307 * or the h/w is unplugged.
4308 */
4309 }
4310
4311 rc = scsi_add_host(ap->host, dev);
4312 if (rc) {
4313 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4314 ap->id);
4315 /* FIXME: do something useful here */
4316 /* FIXME: handle unconditional calls to
4317 * scsi_scan_host and ata_host_remove, below,
4318 * at the very least
4319 */
4320 }
4321 }
4322
4323 /* probes are done, now scan each port's disk(s) */
4324 DPRINTK("probe begin\n");
4325 for (i = 0; i < count; i++) {
4326 struct ata_port *ap = host_set->ports[i];
4327
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004328 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 }
4330
4331 dev_set_drvdata(dev, host_set);
4332
4333 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4334 return ent->n_ports; /* success */
4335
4336err_out:
4337 for (i = 0; i < count; i++) {
4338 ata_host_remove(host_set->ports[i], 1);
4339 scsi_host_put(host_set->ports[i]->host);
4340 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004341err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 kfree(host_set);
4343 VPRINTK("EXIT, returning 0\n");
4344 return 0;
4345}
4346
4347/**
Alan Cox17b14452005-09-15 15:44:00 +01004348 * ata_host_set_remove - PCI layer callback for device removal
4349 * @host_set: ATA host set that was removed
4350 *
4351 * Unregister all objects associated with this host set. Free those
4352 * objects.
4353 *
4354 * LOCKING:
4355 * Inherited from calling layer (may sleep).
4356 */
4357
Alan Cox17b14452005-09-15 15:44:00 +01004358void ata_host_set_remove(struct ata_host_set *host_set)
4359{
4360 struct ata_port *ap;
4361 unsigned int i;
4362
4363 for (i = 0; i < host_set->n_ports; i++) {
4364 ap = host_set->ports[i];
4365 scsi_remove_host(ap->host);
4366 }
4367
4368 free_irq(host_set->irq, host_set);
4369
4370 for (i = 0; i < host_set->n_ports; i++) {
4371 ap = host_set->ports[i];
4372
4373 ata_scsi_release(ap->host);
4374
4375 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4376 struct ata_ioports *ioaddr = &ap->ioaddr;
4377
4378 if (ioaddr->cmd_addr == 0x1f0)
4379 release_region(0x1f0, 8);
4380 else if (ioaddr->cmd_addr == 0x170)
4381 release_region(0x170, 8);
4382 }
4383
4384 scsi_host_put(ap->host);
4385 }
4386
4387 if (host_set->ops->host_stop)
4388 host_set->ops->host_stop(host_set);
4389
4390 kfree(host_set);
4391}
4392
4393/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 * ata_scsi_release - SCSI layer callback hook for host unload
4395 * @host: libata host to be unloaded
4396 *
4397 * Performs all duties necessary to shut down a libata port...
4398 * Kill port kthread, disable port, and release resources.
4399 *
4400 * LOCKING:
4401 * Inherited from SCSI layer.
4402 *
4403 * RETURNS:
4404 * One.
4405 */
4406
4407int ata_scsi_release(struct Scsi_Host *host)
4408{
4409 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4410
4411 DPRINTK("ENTER\n");
4412
4413 ap->ops->port_disable(ap);
4414 ata_host_remove(ap, 0);
4415
4416 DPRINTK("EXIT\n");
4417 return 1;
4418}
4419
4420/**
4421 * ata_std_ports - initialize ioaddr with standard port offsets.
4422 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004423 *
4424 * Utility function which initializes data_addr, error_addr,
4425 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4426 * device_addr, status_addr, and command_addr to standard offsets
4427 * relative to cmd_addr.
4428 *
4429 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 */
Edward Falk0baab862005-06-02 18:17:13 -04004431
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432void ata_std_ports(struct ata_ioports *ioaddr)
4433{
4434 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4435 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4436 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4437 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4438 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4439 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4440 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4441 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4442 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4443 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4444}
4445
4446static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004447ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448{
4449 struct ata_probe_ent *probe_ent;
4450
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004451 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 if (!probe_ent) {
4453 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4454 kobject_name(&(dev->kobj)));
4455 return NULL;
4456 }
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 INIT_LIST_HEAD(&probe_ent->node);
4459 probe_ent->dev = dev;
4460
4461 probe_ent->sht = port->sht;
4462 probe_ent->host_flags = port->host_flags;
4463 probe_ent->pio_mask = port->pio_mask;
4464 probe_ent->mwdma_mask = port->mwdma_mask;
4465 probe_ent->udma_mask = port->udma_mask;
4466 probe_ent->port_ops = port->port_ops;
4467
4468 return probe_ent;
4469}
4470
Edward Falk0baab862005-06-02 18:17:13 -04004471
4472
Jeff Garzik374b1872005-08-30 05:42:52 -04004473#ifdef CONFIG_PCI
4474
4475void ata_pci_host_stop (struct ata_host_set *host_set)
4476{
4477 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4478
4479 pci_iounmap(pdev, host_set->mmio_base);
4480}
4481
Edward Falk0baab862005-06-02 18:17:13 -04004482/**
4483 * ata_pci_init_native_mode - Initialize native-mode driver
4484 * @pdev: pci device to be initialized
4485 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004486 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004487 *
4488 * Utility function which allocates and initializes an
4489 * ata_probe_ent structure for a standard dual-port
4490 * PIO-based IDE controller. The returned ata_probe_ent
4491 * structure can be passed to ata_device_add(). The returned
4492 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004493 *
4494 * The caller need only pass the address of the primary port, the
4495 * secondary will be deduced automatically. If the device has non
4496 * standard secondary port mappings this function can be called twice,
4497 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004498 */
4499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004501ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
4503 struct ata_probe_ent *probe_ent =
4504 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004505 int p = 0;
4506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 if (!probe_ent)
4508 return NULL;
4509
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 probe_ent->irq = pdev->irq;
4511 probe_ent->irq_flags = SA_SHIRQ;
4512
Alan Cox47a86592005-10-04 08:09:19 -04004513 if (ports & ATA_PORT_PRIMARY) {
4514 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4515 probe_ent->port[p].altstatus_addr =
4516 probe_ent->port[p].ctl_addr =
4517 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4518 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4519 ata_std_ports(&probe_ent->port[p]);
4520 p++;
4521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Alan Cox47a86592005-10-04 08:09:19 -04004523 if (ports & ATA_PORT_SECONDARY) {
4524 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4525 probe_ent->port[p].altstatus_addr =
4526 probe_ent->port[p].ctl_addr =
4527 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4528 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4529 ata_std_ports(&probe_ent->port[p]);
4530 p++;
4531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Alan Cox47a86592005-10-04 08:09:19 -04004533 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 return probe_ent;
4535}
4536
Alan Cox47a86592005-10-04 08:09:19 -04004537static 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 -07004538{
Alan Cox47a86592005-10-04 08:09:19 -04004539 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
4541 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4542 if (!probe_ent)
4543 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004546 probe_ent->n_ports = 1;
4547 probe_ent->hard_port_no = port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Alan Cox47a86592005-10-04 08:09:19 -04004549 switch(port_num)
4550 {
4551 case 0:
4552 probe_ent->irq = 14;
4553 probe_ent->port[0].cmd_addr = 0x1f0;
4554 probe_ent->port[0].altstatus_addr =
4555 probe_ent->port[0].ctl_addr = 0x3f6;
4556 break;
4557 case 1:
4558 probe_ent->irq = 15;
4559 probe_ent->port[0].cmd_addr = 0x170;
4560 probe_ent->port[0].altstatus_addr =
4561 probe_ent->port[0].ctl_addr = 0x376;
4562 break;
4563 }
4564 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 return probe_ent;
4567}
4568
4569/**
4570 * ata_pci_init_one - Initialize/register PCI IDE host controller
4571 * @pdev: Controller to be initialized
4572 * @port_info: Information from low-level host driver
4573 * @n_ports: Number of ports attached to host controller
4574 *
Edward Falk0baab862005-06-02 18:17:13 -04004575 * This is a helper function which can be called from a driver's
4576 * xxx_init_one() probe function if the hardware uses traditional
4577 * IDE taskfile registers.
4578 *
4579 * This function calls pci_enable_device(), reserves its register
4580 * regions, sets the dma mask, enables bus master mode, and calls
4581 * ata_device_add()
4582 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 * LOCKING:
4584 * Inherited from PCI layer (may sleep).
4585 *
4586 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004587 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 */
4589
4590int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4591 unsigned int n_ports)
4592{
Alan Cox47a86592005-10-04 08:09:19 -04004593 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 struct ata_port_info *port[2];
4595 u8 tmp8, mask;
4596 unsigned int legacy_mode = 0;
4597 int disable_dev_on_err = 1;
4598 int rc;
4599
4600 DPRINTK("ENTER\n");
4601
4602 port[0] = port_info[0];
4603 if (n_ports > 1)
4604 port[1] = port_info[1];
4605 else
4606 port[1] = port[0];
4607
4608 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4609 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004610 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4612 mask = (1 << 2) | (1 << 0);
4613 if ((tmp8 & mask) != mask)
4614 legacy_mode = (1 << 3);
4615 }
4616
4617 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004618 if ((!legacy_mode) && (n_ports > 2)) {
4619 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4620 n_ports = 2;
4621 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 }
4623
Alan Cox47a86592005-10-04 08:09:19 -04004624 /* FIXME: Really for ATA it isn't safe because the device may be
4625 multi-purpose and we want to leave it alone if it was already
4626 enabled. Secondly for shared use as Arjan says we want refcounting
4627
4628 Checking dev->is_enabled is insufficient as this is not set at
4629 boot for the primary video which is BIOS enabled
4630 */
4631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 rc = pci_enable_device(pdev);
4633 if (rc)
4634 return rc;
4635
4636 rc = pci_request_regions(pdev, DRV_NAME);
4637 if (rc) {
4638 disable_dev_on_err = 0;
4639 goto err_out;
4640 }
4641
Alan Cox47a86592005-10-04 08:09:19 -04004642 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 if (legacy_mode) {
4644 if (!request_region(0x1f0, 8, "libata")) {
4645 struct resource *conflict, res;
4646 res.start = 0x1f0;
4647 res.end = 0x1f0 + 8 - 1;
4648 conflict = ____request_resource(&ioport_resource, &res);
4649 if (!strcmp(conflict->name, "libata"))
4650 legacy_mode |= (1 << 0);
4651 else {
4652 disable_dev_on_err = 0;
4653 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4654 }
4655 } else
4656 legacy_mode |= (1 << 0);
4657
4658 if (!request_region(0x170, 8, "libata")) {
4659 struct resource *conflict, res;
4660 res.start = 0x170;
4661 res.end = 0x170 + 8 - 1;
4662 conflict = ____request_resource(&ioport_resource, &res);
4663 if (!strcmp(conflict->name, "libata"))
4664 legacy_mode |= (1 << 1);
4665 else {
4666 disable_dev_on_err = 0;
4667 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4668 }
4669 } else
4670 legacy_mode |= (1 << 1);
4671 }
4672
4673 /* we have legacy mode, but all ports are unavailable */
4674 if (legacy_mode == (1 << 3)) {
4675 rc = -EBUSY;
4676 goto err_out_regions;
4677 }
4678
4679 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4680 if (rc)
4681 goto err_out_regions;
4682 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4683 if (rc)
4684 goto err_out_regions;
4685
4686 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004687 if (legacy_mode & (1 << 0))
4688 probe_ent = ata_pci_init_legacy_port(pdev, port, 0);
4689 if (legacy_mode & (1 << 1))
4690 probe_ent2 = ata_pci_init_legacy_port(pdev, port, 1);
4691 } else {
4692 if (n_ports == 2)
4693 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4694 else
4695 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4696 }
4697 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 rc = -ENOMEM;
4699 goto err_out_regions;
4700 }
4701
4702 pci_set_master(pdev);
4703
4704 /* FIXME: check ata_device_add return */
4705 if (legacy_mode) {
4706 if (legacy_mode & (1 << 0))
4707 ata_device_add(probe_ent);
4708 if (legacy_mode & (1 << 1))
4709 ata_device_add(probe_ent2);
4710 } else
4711 ata_device_add(probe_ent);
4712
4713 kfree(probe_ent);
4714 kfree(probe_ent2);
4715
4716 return 0;
4717
4718err_out_regions:
4719 if (legacy_mode & (1 << 0))
4720 release_region(0x1f0, 8);
4721 if (legacy_mode & (1 << 1))
4722 release_region(0x170, 8);
4723 pci_release_regions(pdev);
4724err_out:
4725 if (disable_dev_on_err)
4726 pci_disable_device(pdev);
4727 return rc;
4728}
4729
4730/**
4731 * ata_pci_remove_one - PCI layer callback for device removal
4732 * @pdev: PCI device that was removed
4733 *
4734 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004735 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 * Handle this by unregistering all objects associated
4737 * with this PCI device. Free those objects. Then finally
4738 * release PCI resources and disable device.
4739 *
4740 * LOCKING:
4741 * Inherited from PCI layer (may sleep).
4742 */
4743
4744void ata_pci_remove_one (struct pci_dev *pdev)
4745{
4746 struct device *dev = pci_dev_to_dev(pdev);
4747 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
Alan Cox17b14452005-09-15 15:44:00 +01004749 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 pci_release_regions(pdev);
4751 pci_disable_device(pdev);
4752 dev_set_drvdata(dev, NULL);
4753}
4754
4755/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004756int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757{
4758 unsigned long tmp = 0;
4759
4760 switch (bits->width) {
4761 case 1: {
4762 u8 tmp8 = 0;
4763 pci_read_config_byte(pdev, bits->reg, &tmp8);
4764 tmp = tmp8;
4765 break;
4766 }
4767 case 2: {
4768 u16 tmp16 = 0;
4769 pci_read_config_word(pdev, bits->reg, &tmp16);
4770 tmp = tmp16;
4771 break;
4772 }
4773 case 4: {
4774 u32 tmp32 = 0;
4775 pci_read_config_dword(pdev, bits->reg, &tmp32);
4776 tmp = tmp32;
4777 break;
4778 }
4779
4780 default:
4781 return -EINVAL;
4782 }
4783
4784 tmp &= bits->mask;
4785
4786 return (tmp == bits->val) ? 1 : 0;
4787}
4788#endif /* CONFIG_PCI */
4789
4790
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791static int __init ata_init(void)
4792{
4793 ata_wq = create_workqueue("ata");
4794 if (!ata_wq)
4795 return -ENOMEM;
4796
4797 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4798 return 0;
4799}
4800
4801static void __exit ata_exit(void)
4802{
4803 destroy_workqueue(ata_wq);
4804}
4805
4806module_init(ata_init);
4807module_exit(ata_exit);
4808
Jeff Garzik67846b32005-10-05 02:58:32 -04004809static unsigned long ratelimit_time;
4810static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4811
4812int ata_ratelimit(void)
4813{
4814 int rc;
4815 unsigned long flags;
4816
4817 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4818
4819 if (time_after(jiffies, ratelimit_time)) {
4820 rc = 1;
4821 ratelimit_time = jiffies + (HZ/5);
4822 } else
4823 rc = 0;
4824
4825 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4826
4827 return rc;
4828}
4829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830/*
4831 * libata is essentially a library of internal helper functions for
4832 * low-level ATA host controller drivers. As such, the API/ABI is
4833 * likely to change as new drivers are added and updated.
4834 * Do not depend on ABI/API stability.
4835 */
4836
4837EXPORT_SYMBOL_GPL(ata_std_bios_param);
4838EXPORT_SYMBOL_GPL(ata_std_ports);
4839EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004840EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841EXPORT_SYMBOL_GPL(ata_sg_init);
4842EXPORT_SYMBOL_GPL(ata_sg_init_one);
4843EXPORT_SYMBOL_GPL(ata_qc_complete);
4844EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4845EXPORT_SYMBOL_GPL(ata_eng_timeout);
4846EXPORT_SYMBOL_GPL(ata_tf_load);
4847EXPORT_SYMBOL_GPL(ata_tf_read);
4848EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4849EXPORT_SYMBOL_GPL(ata_std_dev_select);
4850EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4851EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4852EXPORT_SYMBOL_GPL(ata_check_status);
4853EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854EXPORT_SYMBOL_GPL(ata_exec_command);
4855EXPORT_SYMBOL_GPL(ata_port_start);
4856EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004857EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858EXPORT_SYMBOL_GPL(ata_interrupt);
4859EXPORT_SYMBOL_GPL(ata_qc_prep);
4860EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4861EXPORT_SYMBOL_GPL(ata_bmdma_start);
4862EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4863EXPORT_SYMBOL_GPL(ata_bmdma_status);
4864EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4865EXPORT_SYMBOL_GPL(ata_port_probe);
4866EXPORT_SYMBOL_GPL(sata_phy_reset);
4867EXPORT_SYMBOL_GPL(__sata_phy_reset);
4868EXPORT_SYMBOL_GPL(ata_bus_reset);
4869EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004870EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4872EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4873EXPORT_SYMBOL_GPL(ata_scsi_error);
4874EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4875EXPORT_SYMBOL_GPL(ata_scsi_release);
4876EXPORT_SYMBOL_GPL(ata_host_intr);
4877EXPORT_SYMBOL_GPL(ata_dev_classify);
4878EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004879EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4881
Alan Cox452503f2005-10-21 19:01:32 -04004882EXPORT_SYMBOL_GPL(ata_timing_compute);
4883EXPORT_SYMBOL_GPL(ata_timing_merge);
4884
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885#ifdef CONFIG_PCI
4886EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004887EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4889EXPORT_SYMBOL_GPL(ata_pci_init_one);
4890EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4891#endif /* CONFIG_PCI */