blob: 9eb24d3f18db9d03afbf1028511ecf321e45e51e [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 Lee8bf62ec2005-05-12 15:29:42 -040066static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static void ata_set_mode(struct ata_port *ap);
68static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
69static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
70static int fgb(u32 bitmap);
71static int ata_choose_xfer_mode(struct ata_port *ap,
72 u8 *xfer_mode_out,
73 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static void __ata_qc_complete(struct ata_queued_cmd *qc);
Jeff Garzike33b9df2005-10-09 09:51:46 -040075static void ata_pio_error(struct ata_port *ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
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/**
90 * ata_tf_load - send taskfile registers to host controller
91 * @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
100static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
101{
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
158static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
159{
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
228{
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
247static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
248{
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
268static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
269{
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
289{
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
308static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
309{
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
331static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
332{
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
351void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
352{
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
373 tf->nsect = inb(ioaddr->nsect_addr);
374 tf->lbal = inb(ioaddr->lbal_addr);
375 tf->lbam = inb(ioaddr->lbam_addr);
376 tf->lbah = inb(ioaddr->lbah_addr);
377 tf->device = inb(ioaddr->device_addr);
378
379 if (tf->flags & ATA_TFLAG_LBA48) {
380 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
381 tf->hob_feature = inb(ioaddr->error_addr);
382 tf->hob_nsect = inb(ioaddr->nsect_addr);
383 tf->hob_lbal = inb(ioaddr->lbal_addr);
384 tf->hob_lbam = inb(ioaddr->lbam_addr);
385 tf->hob_lbah = inb(ioaddr->lbah_addr);
386 }
387}
388
389/**
390 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
391 * @ap: Port from which input is read
392 * @tf: ATA taskfile register set for storing input
393 *
394 * Reads ATA taskfile registers for currently-selected device
395 * into @tf via MMIO.
396 *
397 * LOCKING:
398 * Inherited from caller.
399 */
400
401static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
402{
403 struct ata_ioports *ioaddr = &ap->ioaddr;
404
405 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
406 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
407 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
408 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
409 tf->device = readb((void __iomem *)ioaddr->device_addr);
410
411 if (tf->flags & ATA_TFLAG_LBA48) {
412 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
413 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
414 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
415 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
416 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
417 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
418 }
419}
420
Edward Falk0baab862005-06-02 18:17:13 -0400421
422/**
423 * ata_tf_read - input device's ATA taskfile shadow registers
424 * @ap: Port from which input is read
425 * @tf: ATA taskfile register set for storing input
426 *
427 * Reads ATA taskfile registers for currently-selected device
428 * into @tf.
429 *
430 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
431 * is set, also reads the hob registers.
432 *
433 * May be used as the tf_read() entry in ata_port_operations.
434 *
435 * LOCKING:
436 * Inherited from caller.
437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
439{
440 if (ap->flags & ATA_FLAG_MMIO)
441 ata_tf_read_mmio(ap, tf);
442 else
443 ata_tf_read_pio(ap, tf);
444}
445
446/**
447 * ata_check_status_pio - Read device status reg & clear interrupt
448 * @ap: port where the device is
449 *
450 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400451 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * from this device
453 *
454 * LOCKING:
455 * Inherited from caller.
456 */
457static u8 ata_check_status_pio(struct ata_port *ap)
458{
459 return inb(ap->ioaddr.status_addr);
460}
461
462/**
463 * ata_check_status_mmio - Read device status reg & clear interrupt
464 * @ap: port where the device is
465 *
466 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400467 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * from this device
469 *
470 * LOCKING:
471 * Inherited from caller.
472 */
473static u8 ata_check_status_mmio(struct ata_port *ap)
474{
475 return readb((void __iomem *) ap->ioaddr.status_addr);
476}
477
Edward Falk0baab862005-06-02 18:17:13 -0400478
479/**
480 * ata_check_status - Read device status reg & clear interrupt
481 * @ap: port where the device is
482 *
483 * Reads ATA taskfile status register for currently-selected device
484 * and return its value. This also clears pending interrupts
485 * from this device
486 *
487 * May be used as the check_status() entry in ata_port_operations.
488 *
489 * LOCKING:
490 * Inherited from caller.
491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492u8 ata_check_status(struct ata_port *ap)
493{
494 if (ap->flags & ATA_FLAG_MMIO)
495 return ata_check_status_mmio(ap);
496 return ata_check_status_pio(ap);
497}
498
Edward Falk0baab862005-06-02 18:17:13 -0400499
500/**
501 * ata_altstatus - Read device alternate status reg
502 * @ap: port where the device is
503 *
504 * Reads ATA taskfile alternate status register for
505 * currently-selected device and return its value.
506 *
507 * Note: may NOT be used as the check_altstatus() entry in
508 * ata_port_operations.
509 *
510 * LOCKING:
511 * Inherited from caller.
512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513u8 ata_altstatus(struct ata_port *ap)
514{
515 if (ap->ops->check_altstatus)
516 return ap->ops->check_altstatus(ap);
517
518 if (ap->flags & ATA_FLAG_MMIO)
519 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
520 return inb(ap->ioaddr.altstatus_addr);
521}
522
Edward Falk0baab862005-06-02 18:17:13 -0400523
524/**
525 * ata_chk_err - Read device error reg
526 * @ap: port where the device is
527 *
528 * Reads ATA taskfile error register for
529 * currently-selected device and return its value.
530 *
531 * Note: may NOT be used as the check_err() entry in
532 * ata_port_operations.
533 *
534 * LOCKING:
535 * Inherited from caller.
536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537u8 ata_chk_err(struct ata_port *ap)
538{
539 if (ap->ops->check_err)
540 return ap->ops->check_err(ap);
541
542 if (ap->flags & ATA_FLAG_MMIO) {
543 return readb((void __iomem *) ap->ioaddr.error_addr);
544 }
545 return inb(ap->ioaddr.error_addr);
546}
547
548/**
549 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
550 * @tf: Taskfile to convert
551 * @fis: Buffer into which data will output
552 * @pmp: Port multiplier port
553 *
554 * Converts a standard ATA taskfile to a Serial ATA
555 * FIS structure (Register - Host to Device).
556 *
557 * LOCKING:
558 * Inherited from caller.
559 */
560
561void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
562{
563 fis[0] = 0x27; /* Register - Host to Device FIS */
564 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
565 bit 7 indicates Command FIS */
566 fis[2] = tf->command;
567 fis[3] = tf->feature;
568
569 fis[4] = tf->lbal;
570 fis[5] = tf->lbam;
571 fis[6] = tf->lbah;
572 fis[7] = tf->device;
573
574 fis[8] = tf->hob_lbal;
575 fis[9] = tf->hob_lbam;
576 fis[10] = tf->hob_lbah;
577 fis[11] = tf->hob_feature;
578
579 fis[12] = tf->nsect;
580 fis[13] = tf->hob_nsect;
581 fis[14] = 0;
582 fis[15] = tf->ctl;
583
584 fis[16] = 0;
585 fis[17] = 0;
586 fis[18] = 0;
587 fis[19] = 0;
588}
589
590/**
591 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
592 * @fis: Buffer from which data will be input
593 * @tf: Taskfile to output
594 *
595 * Converts a standard ATA taskfile to a Serial ATA
596 * FIS structure (Register - Host to Device).
597 *
598 * LOCKING:
599 * Inherited from caller.
600 */
601
602void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
603{
604 tf->command = fis[2]; /* status */
605 tf->feature = fis[3]; /* error */
606
607 tf->lbal = fis[4];
608 tf->lbam = fis[5];
609 tf->lbah = fis[6];
610 tf->device = fis[7];
611
612 tf->hob_lbal = fis[8];
613 tf->hob_lbam = fis[9];
614 tf->hob_lbah = fis[10];
615
616 tf->nsect = fis[12];
617 tf->hob_nsect = fis[13];
618}
619
620/**
621 * ata_prot_to_cmd - determine which read/write opcodes to use
622 * @protocol: ATA_PROT_xxx taskfile protocol
623 * @lba48: true is lba48 is present
624 *
625 * Given necessary input, determine which read/write commands
626 * to use to transfer data.
627 *
628 * LOCKING:
629 * None.
630 */
631static int ata_prot_to_cmd(int protocol, int lba48)
632{
633 int rcmd = 0, wcmd = 0;
634
635 switch (protocol) {
636 case ATA_PROT_PIO:
637 if (lba48) {
638 rcmd = ATA_CMD_PIO_READ_EXT;
639 wcmd = ATA_CMD_PIO_WRITE_EXT;
640 } else {
641 rcmd = ATA_CMD_PIO_READ;
642 wcmd = ATA_CMD_PIO_WRITE;
643 }
644 break;
645
646 case ATA_PROT_DMA:
647 if (lba48) {
648 rcmd = ATA_CMD_READ_EXT;
649 wcmd = ATA_CMD_WRITE_EXT;
650 } else {
651 rcmd = ATA_CMD_READ;
652 wcmd = ATA_CMD_WRITE;
653 }
654 break;
655
656 default:
657 return -1;
658 }
659
660 return rcmd | (wcmd << 8);
661}
662
663/**
664 * ata_dev_set_protocol - set taskfile protocol and r/w commands
665 * @dev: device to examine and configure
666 *
667 * Examine the device configuration, after we have
668 * read the identify-device page and configured the
669 * data transfer mode. Set internal state related to
670 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
671 * and calculate the proper read/write commands to use.
672 *
673 * LOCKING:
674 * caller.
675 */
676static void ata_dev_set_protocol(struct ata_device *dev)
677{
678 int pio = (dev->flags & ATA_DFLAG_PIO);
679 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
680 int proto, cmd;
681
682 if (pio)
683 proto = dev->xfer_protocol = ATA_PROT_PIO;
684 else
685 proto = dev->xfer_protocol = ATA_PROT_DMA;
686
687 cmd = ata_prot_to_cmd(proto, lba48);
688 if (cmd < 0)
689 BUG();
690
691 dev->read_cmd = cmd & 0xff;
692 dev->write_cmd = (cmd >> 8) & 0xff;
693}
694
695static const char * xfer_mode_str[] = {
696 "UDMA/16",
697 "UDMA/25",
698 "UDMA/33",
699 "UDMA/44",
700 "UDMA/66",
701 "UDMA/100",
702 "UDMA/133",
703 "UDMA7",
704 "MWDMA0",
705 "MWDMA1",
706 "MWDMA2",
707 "PIO0",
708 "PIO1",
709 "PIO2",
710 "PIO3",
711 "PIO4",
712};
713
714/**
715 * ata_udma_string - convert UDMA bit offset to string
716 * @mask: mask of bits supported; only highest bit counts.
717 *
718 * Determine string which represents the highest speed
719 * (highest bit in @udma_mask).
720 *
721 * LOCKING:
722 * None.
723 *
724 * RETURNS:
725 * Constant C string representing highest speed listed in
726 * @udma_mask, or the constant C string "<n/a>".
727 */
728
729static const char *ata_mode_string(unsigned int mask)
730{
731 int i;
732
733 for (i = 7; i >= 0; i--)
734 if (mask & (1 << i))
735 goto out;
736 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
737 if (mask & (1 << i))
738 goto out;
739 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
740 if (mask & (1 << i))
741 goto out;
742
743 return "<n/a>";
744
745out:
746 return xfer_mode_str[i];
747}
748
749/**
750 * ata_pio_devchk - PATA device presence detection
751 * @ap: ATA channel to examine
752 * @device: Device to examine (starting at zero)
753 *
754 * This technique was originally described in
755 * Hale Landis's ATADRVR (www.ata-atapi.com), and
756 * later found its way into the ATA/ATAPI spec.
757 *
758 * Write a pattern to the ATA shadow registers,
759 * and if a device is present, it will respond by
760 * correctly storing and echoing back the
761 * ATA shadow register contents.
762 *
763 * LOCKING:
764 * caller.
765 */
766
767static unsigned int ata_pio_devchk(struct ata_port *ap,
768 unsigned int device)
769{
770 struct ata_ioports *ioaddr = &ap->ioaddr;
771 u8 nsect, lbal;
772
773 ap->ops->dev_select(ap, device);
774
775 outb(0x55, ioaddr->nsect_addr);
776 outb(0xaa, ioaddr->lbal_addr);
777
778 outb(0xaa, ioaddr->nsect_addr);
779 outb(0x55, ioaddr->lbal_addr);
780
781 outb(0x55, ioaddr->nsect_addr);
782 outb(0xaa, ioaddr->lbal_addr);
783
784 nsect = inb(ioaddr->nsect_addr);
785 lbal = inb(ioaddr->lbal_addr);
786
787 if ((nsect == 0x55) && (lbal == 0xaa))
788 return 1; /* we found a device */
789
790 return 0; /* nothing found */
791}
792
793/**
794 * ata_mmio_devchk - PATA device presence detection
795 * @ap: ATA channel to examine
796 * @device: Device to examine (starting at zero)
797 *
798 * This technique was originally described in
799 * Hale Landis's ATADRVR (www.ata-atapi.com), and
800 * later found its way into the ATA/ATAPI spec.
801 *
802 * Write a pattern to the ATA shadow registers,
803 * and if a device is present, it will respond by
804 * correctly storing and echoing back the
805 * ATA shadow register contents.
806 *
807 * LOCKING:
808 * caller.
809 */
810
811static unsigned int ata_mmio_devchk(struct ata_port *ap,
812 unsigned int device)
813{
814 struct ata_ioports *ioaddr = &ap->ioaddr;
815 u8 nsect, lbal;
816
817 ap->ops->dev_select(ap, device);
818
819 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
820 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
821
822 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
823 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
824
825 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
826 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
827
828 nsect = readb((void __iomem *) ioaddr->nsect_addr);
829 lbal = readb((void __iomem *) ioaddr->lbal_addr);
830
831 if ((nsect == 0x55) && (lbal == 0xaa))
832 return 1; /* we found a device */
833
834 return 0; /* nothing found */
835}
836
837/**
838 * ata_devchk - PATA device presence detection
839 * @ap: ATA channel to examine
840 * @device: Device to examine (starting at zero)
841 *
842 * Dispatch ATA device presence detection, depending
843 * on whether we are using PIO or MMIO to talk to the
844 * ATA shadow registers.
845 *
846 * LOCKING:
847 * caller.
848 */
849
850static unsigned int ata_devchk(struct ata_port *ap,
851 unsigned int device)
852{
853 if (ap->flags & ATA_FLAG_MMIO)
854 return ata_mmio_devchk(ap, device);
855 return ata_pio_devchk(ap, device);
856}
857
858/**
859 * ata_dev_classify - determine device type based on ATA-spec signature
860 * @tf: ATA taskfile register set for device to be identified
861 *
862 * Determine from taskfile register contents whether a device is
863 * ATA or ATAPI, as per "Signature and persistence" section
864 * of ATA/PI spec (volume 1, sect 5.14).
865 *
866 * LOCKING:
867 * None.
868 *
869 * RETURNS:
870 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
871 * the event of failure.
872 */
873
874unsigned int ata_dev_classify(struct ata_taskfile *tf)
875{
876 /* Apple's open source Darwin code hints that some devices only
877 * put a proper signature into the LBA mid/high registers,
878 * So, we only check those. It's sufficient for uniqueness.
879 */
880
881 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
882 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
883 DPRINTK("found ATA device by sig\n");
884 return ATA_DEV_ATA;
885 }
886
887 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
888 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
889 DPRINTK("found ATAPI device by sig\n");
890 return ATA_DEV_ATAPI;
891 }
892
893 DPRINTK("unknown device\n");
894 return ATA_DEV_UNKNOWN;
895}
896
897/**
898 * ata_dev_try_classify - Parse returned ATA device signature
899 * @ap: ATA channel to examine
900 * @device: Device to examine (starting at zero)
901 *
902 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
903 * an ATA/ATAPI-defined set of values is placed in the ATA
904 * shadow registers, indicating the results of device detection
905 * and diagnostics.
906 *
907 * Select the ATA device, and read the values from the ATA shadow
908 * registers. Then parse according to the Error register value,
909 * and the spec-defined values examined by ata_dev_classify().
910 *
911 * LOCKING:
912 * caller.
913 */
914
915static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
916{
917 struct ata_device *dev = &ap->device[device];
918 struct ata_taskfile tf;
919 unsigned int class;
920 u8 err;
921
922 ap->ops->dev_select(ap, device);
923
924 memset(&tf, 0, sizeof(tf));
925
926 err = ata_chk_err(ap);
927 ap->ops->tf_read(ap, &tf);
928
929 dev->class = ATA_DEV_NONE;
930
931 /* see if device passed diags */
932 if (err == 1)
933 /* do nothing */ ;
934 else if ((device == 0) && (err == 0x81))
935 /* do nothing */ ;
936 else
937 return err;
938
939 /* determine if device if ATA or ATAPI */
940 class = ata_dev_classify(&tf);
941 if (class == ATA_DEV_UNKNOWN)
942 return err;
943 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
944 return err;
945
946 dev->class = class;
947
948 return err;
949}
950
951/**
952 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
953 * @id: IDENTIFY DEVICE results we will examine
954 * @s: string into which data is output
955 * @ofs: offset into identify device page
956 * @len: length of string to return. must be an even number.
957 *
958 * The strings in the IDENTIFY DEVICE page are broken up into
959 * 16-bit chunks. Run through the string, and output each
960 * 8-bit chunk linearly, regardless of platform.
961 *
962 * LOCKING:
963 * caller.
964 */
965
966void ata_dev_id_string(u16 *id, unsigned char *s,
967 unsigned int ofs, unsigned int len)
968{
969 unsigned int c;
970
971 while (len > 0) {
972 c = id[ofs] >> 8;
973 *s = c;
974 s++;
975
976 c = id[ofs] & 0xff;
977 *s = c;
978 s++;
979
980 ofs++;
981 len -= 2;
982 }
983}
984
Edward Falk0baab862005-06-02 18:17:13 -0400985
986/**
987 * ata_noop_dev_select - Select device 0/1 on ATA bus
988 * @ap: ATA channel to manipulate
989 * @device: ATA device (numbered from zero) to select
990 *
991 * This function performs no actual function.
992 *
993 * May be used as the dev_select() entry in ata_port_operations.
994 *
995 * LOCKING:
996 * caller.
997 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
999{
1000}
1001
Edward Falk0baab862005-06-02 18:17:13 -04001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003/**
1004 * ata_std_dev_select - Select device 0/1 on ATA bus
1005 * @ap: ATA channel to manipulate
1006 * @device: ATA device (numbered from zero) to select
1007 *
1008 * Use the method defined in the ATA specification to
1009 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -04001010 * ATA channel. Works with both PIO and MMIO.
1011 *
1012 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 *
1014 * LOCKING:
1015 * caller.
1016 */
1017
1018void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1019{
1020 u8 tmp;
1021
1022 if (device == 0)
1023 tmp = ATA_DEVICE_OBS;
1024 else
1025 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1026
1027 if (ap->flags & ATA_FLAG_MMIO) {
1028 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1029 } else {
1030 outb(tmp, ap->ioaddr.device_addr);
1031 }
1032 ata_pause(ap); /* needed; also flushes, for mmio */
1033}
1034
1035/**
1036 * ata_dev_select - Select device 0/1 on ATA bus
1037 * @ap: ATA channel to manipulate
1038 * @device: ATA device (numbered from zero) to select
1039 * @wait: non-zero to wait for Status register BSY bit to clear
1040 * @can_sleep: non-zero if context allows sleeping
1041 *
1042 * Use the method defined in the ATA specification to
1043 * make either device 0, or device 1, active on the
1044 * ATA channel.
1045 *
1046 * This is a high-level version of ata_std_dev_select(),
1047 * which additionally provides the services of inserting
1048 * the proper pauses and status polling, where needed.
1049 *
1050 * LOCKING:
1051 * caller.
1052 */
1053
1054void ata_dev_select(struct ata_port *ap, unsigned int device,
1055 unsigned int wait, unsigned int can_sleep)
1056{
1057 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1058 ap->id, device, wait);
1059
1060 if (wait)
1061 ata_wait_idle(ap);
1062
1063 ap->ops->dev_select(ap, device);
1064
1065 if (wait) {
1066 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1067 msleep(150);
1068 ata_wait_idle(ap);
1069 }
1070}
1071
1072/**
1073 * ata_dump_id - IDENTIFY DEVICE info debugging output
1074 * @dev: Device whose IDENTIFY DEVICE page we will dump
1075 *
1076 * Dump selected 16-bit words from a detected device's
1077 * IDENTIFY PAGE page.
1078 *
1079 * LOCKING:
1080 * caller.
1081 */
1082
1083static inline void ata_dump_id(struct ata_device *dev)
1084{
1085 DPRINTK("49==0x%04x "
1086 "53==0x%04x "
1087 "63==0x%04x "
1088 "64==0x%04x "
1089 "75==0x%04x \n",
1090 dev->id[49],
1091 dev->id[53],
1092 dev->id[63],
1093 dev->id[64],
1094 dev->id[75]);
1095 DPRINTK("80==0x%04x "
1096 "81==0x%04x "
1097 "82==0x%04x "
1098 "83==0x%04x "
1099 "84==0x%04x \n",
1100 dev->id[80],
1101 dev->id[81],
1102 dev->id[82],
1103 dev->id[83],
1104 dev->id[84]);
1105 DPRINTK("88==0x%04x "
1106 "93==0x%04x\n",
1107 dev->id[88],
1108 dev->id[93]);
1109}
1110
1111/**
1112 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1113 * @ap: port on which device we wish to probe resides
1114 * @device: device bus address, starting at zero
1115 *
1116 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1117 * command, and read back the 512-byte device information page.
1118 * The device information page is fed to us via the standard
1119 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1120 * using standard PIO-IN paths)
1121 *
1122 * After reading the device information page, we use several
1123 * bits of information from it to initialize data structures
1124 * that will be used during the lifetime of the ata_device.
1125 * Other data from the info page is used to disqualify certain
1126 * older ATA devices we do not wish to support.
1127 *
1128 * LOCKING:
1129 * Inherited from caller. Some functions called by this function
1130 * obtain the host_set lock.
1131 */
1132
1133static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1134{
1135 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001136 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 u16 tmp;
1138 unsigned long xfer_modes;
1139 u8 status;
1140 unsigned int using_edd;
1141 DECLARE_COMPLETION(wait);
1142 struct ata_queued_cmd *qc;
1143 unsigned long flags;
1144 int rc;
1145
1146 if (!ata_dev_present(dev)) {
1147 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1148 ap->id, device);
1149 return;
1150 }
1151
1152 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1153 using_edd = 0;
1154 else
1155 using_edd = 1;
1156
1157 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1158
1159 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1160 dev->class == ATA_DEV_NONE);
1161
1162 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1163
1164 qc = ata_qc_new_init(ap, dev);
1165 BUG_ON(qc == NULL);
1166
1167 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1168 qc->dma_dir = DMA_FROM_DEVICE;
1169 qc->tf.protocol = ATA_PROT_PIO;
1170 qc->nsect = 1;
1171
1172retry:
1173 if (dev->class == ATA_DEV_ATA) {
1174 qc->tf.command = ATA_CMD_ID_ATA;
1175 DPRINTK("do ATA identify\n");
1176 } else {
1177 qc->tf.command = ATA_CMD_ID_ATAPI;
1178 DPRINTK("do ATAPI identify\n");
1179 }
1180
1181 qc->waiting = &wait;
1182 qc->complete_fn = ata_qc_complete_noop;
1183
1184 spin_lock_irqsave(&ap->host_set->lock, flags);
1185 rc = ata_qc_issue(qc);
1186 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1187
1188 if (rc)
1189 goto err_out;
1190 else
1191 wait_for_completion(&wait);
1192
1193 status = ata_chk_status(ap);
1194 if (status & ATA_ERR) {
1195 /*
1196 * arg! EDD works for all test cases, but seems to return
1197 * the ATA signature for some ATAPI devices. Until the
1198 * reason for this is found and fixed, we fix up the mess
1199 * here. If IDENTIFY DEVICE returns command aborted
1200 * (as ATAPI devices do), then we issue an
1201 * IDENTIFY PACKET DEVICE.
1202 *
1203 * ATA software reset (SRST, the default) does not appear
1204 * to have this problem.
1205 */
1206 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1207 u8 err = ata_chk_err(ap);
1208 if (err & ATA_ABORTED) {
1209 dev->class = ATA_DEV_ATAPI;
1210 qc->cursg = 0;
1211 qc->cursg_ofs = 0;
1212 qc->cursect = 0;
1213 qc->nsect = 1;
1214 goto retry;
1215 }
1216 }
1217 goto err_out;
1218 }
1219
1220 swap_buf_le16(dev->id, ATA_ID_WORDS);
1221
1222 /* print device capabilities */
1223 printk(KERN_DEBUG "ata%u: dev %u cfg "
1224 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1225 ap->id, device, dev->id[49],
1226 dev->id[82], dev->id[83], dev->id[84],
1227 dev->id[85], dev->id[86], dev->id[87],
1228 dev->id[88]);
1229
1230 /*
1231 * common ATA, ATAPI feature tests
1232 */
1233
Albert Lee8bf62ec2005-05-12 15:29:42 -04001234 /* we require DMA support (bits 8 of word 49) */
1235 if (!ata_id_has_dma(dev->id)) {
1236 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 goto err_out_nosup;
1238 }
1239
1240 /* quick-n-dirty find max transfer mode; for printk only */
1241 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1242 if (!xfer_modes)
1243 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1244 if (!xfer_modes) {
1245 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1246 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1247 }
1248
1249 ata_dump_id(dev);
1250
1251 /* ATA-specific feature tests */
1252 if (dev->class == ATA_DEV_ATA) {
1253 if (!ata_id_is_ata(dev->id)) /* sanity check */
1254 goto err_out_nosup;
1255
Albert Lee8bf62ec2005-05-12 15:29:42 -04001256 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001258 for (major_version = 14; major_version >= 1; major_version--)
1259 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 break;
1261
Albert Lee8bf62ec2005-05-12 15:29:42 -04001262 /*
1263 * The exact sequence expected by certain pre-ATA4 drives is:
1264 * SRST RESET
1265 * IDENTIFY
1266 * INITIALIZE DEVICE PARAMETERS
1267 * anything else..
1268 * Some drives were very specific about that exact sequence.
1269 */
1270 if (major_version < 4 || (!ata_id_has_lba(dev->id)))
1271 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Albert Lee8bf62ec2005-05-12 15:29:42 -04001273 if (ata_id_has_lba(dev->id)) {
1274 dev->flags |= ATA_DFLAG_LBA;
1275
1276 if (ata_id_has_lba48(dev->id)) {
1277 dev->flags |= ATA_DFLAG_LBA48;
1278 dev->n_sectors = ata_id_u64(dev->id, 100);
1279 } else {
1280 dev->n_sectors = ata_id_u32(dev->id, 60);
1281 }
1282
1283 /* print device info to dmesg */
1284 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1285 ap->id, device,
1286 major_version,
1287 ata_mode_string(xfer_modes),
1288 (unsigned long long)dev->n_sectors,
1289 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1290 } else {
1291 /* CHS */
1292
1293 /* Default translation */
1294 dev->cylinders = dev->id[1];
1295 dev->heads = dev->id[3];
1296 dev->sectors = dev->id[6];
1297 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1298
1299 if (ata_id_current_chs_valid(dev->id)) {
1300 /* Current CHS translation is valid. */
1301 dev->cylinders = dev->id[54];
1302 dev->heads = dev->id[55];
1303 dev->sectors = dev->id[56];
1304
1305 dev->n_sectors = ata_id_u32(dev->id, 57);
1306 }
1307
1308 /* print device info to dmesg */
1309 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1310 ap->id, device,
1311 major_version,
1312 ata_mode_string(xfer_modes),
1313 (unsigned long long)dev->n_sectors,
1314 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
1318 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
1321 /* ATAPI-specific feature tests */
1322 else {
1323 if (ata_id_is_ata(dev->id)) /* sanity check */
1324 goto err_out_nosup;
1325
1326 rc = atapi_cdb_len(dev->id);
1327 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1328 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1329 goto err_out_nosup;
1330 }
1331 ap->cdb_len = (unsigned int) rc;
1332 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1333
Albert Lee312f7da2005-09-27 17:38:03 +08001334 if (ata_id_cdb_intr(dev->id))
1335 dev->flags |= ATA_DFLAG_CDB_INTR;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* print device info to dmesg */
1338 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1339 ap->id, device,
1340 ata_mode_string(xfer_modes));
1341 }
1342
1343 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1344 return;
1345
1346err_out_nosup:
1347 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1348 ap->id, device);
1349err_out:
1350 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1351 DPRINTK("EXIT, err\n");
1352}
1353
Brad Campbell6f2f3812005-05-12 15:07:47 -04001354
1355static inline u8 ata_dev_knobble(struct ata_port *ap)
1356{
1357 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1358}
1359
1360/**
1361 * ata_dev_config - Run device specific handlers and check for
1362 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001363 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001364 * @i: Device
1365 *
1366 * LOCKING:
1367 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001368
Brad Campbell6f2f3812005-05-12 15:07:47 -04001369void ata_dev_config(struct ata_port *ap, unsigned int i)
1370{
1371 /* limit bridge transfers to udma5, 200 sectors */
1372 if (ata_dev_knobble(ap)) {
1373 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1374 ap->id, ap->device->devno);
1375 ap->udma_mask &= ATA_UDMA5;
1376 ap->host->max_sectors = ATA_MAX_SECTORS;
1377 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1378 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1379 }
1380
1381 if (ap->ops->dev_config)
1382 ap->ops->dev_config(ap, &ap->device[i]);
1383}
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/**
1386 * ata_bus_probe - Reset and probe ATA bus
1387 * @ap: Bus to probe
1388 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001389 * Master ATA bus probing function. Initiates a hardware-dependent
1390 * bus reset, then attempts to identify any devices found on
1391 * the bus.
1392 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001394 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 *
1396 * RETURNS:
1397 * Zero on success, non-zero on error.
1398 */
1399
1400static int ata_bus_probe(struct ata_port *ap)
1401{
1402 unsigned int i, found = 0;
1403
1404 ap->ops->phy_reset(ap);
1405 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1406 goto err_out;
1407
1408 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1409 ata_dev_identify(ap, i);
1410 if (ata_dev_present(&ap->device[i])) {
1411 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001412 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414 }
1415
1416 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1417 goto err_out_disable;
1418
1419 ata_set_mode(ap);
1420 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1421 goto err_out_disable;
1422
1423 return 0;
1424
1425err_out_disable:
1426 ap->ops->port_disable(ap);
1427err_out:
1428 return -1;
1429}
1430
1431/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001432 * ata_port_probe - Mark port as enabled
1433 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001435 * Modify @ap data structure such that the system
1436 * thinks that the entire port is enabled.
1437 *
1438 * LOCKING: host_set lock, or some other form of
1439 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
1441
1442void ata_port_probe(struct ata_port *ap)
1443{
1444 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1445}
1446
1447/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001448 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1449 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001451 * This function issues commands to standard SATA Sxxx
1452 * PHY registers, to wake up the phy (and device), and
1453 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 *
1455 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001456 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
1458 */
1459void __sata_phy_reset(struct ata_port *ap)
1460{
1461 u32 sstatus;
1462 unsigned long timeout = jiffies + (HZ * 5);
1463
1464 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001465 /* issue phy wake/reset */
1466 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001467 /* Couldn't find anything in SATA I/II specs, but
1468 * AHCI-1.1 10.4.2 says at least 1 ms. */
1469 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
Brett Russcdcca892005-03-28 15:10:27 -05001471 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /* wait for phy to become ready, if necessary */
1474 do {
1475 msleep(200);
1476 sstatus = scr_read(ap, SCR_STATUS);
1477 if ((sstatus & 0xf) != 1)
1478 break;
1479 } while (time_before(jiffies, timeout));
1480
1481 /* TODO: phy layer with polling, timeouts, etc. */
1482 if (sata_dev_present(ap))
1483 ata_port_probe(ap);
1484 else {
1485 sstatus = scr_read(ap, SCR_STATUS);
1486 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1487 ap->id, sstatus);
1488 ata_port_disable(ap);
1489 }
1490
1491 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1492 return;
1493
1494 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1495 ata_port_disable(ap);
1496 return;
1497 }
1498
1499 ap->cbl = ATA_CBL_SATA;
1500}
1501
1502/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001503 * sata_phy_reset - Reset SATA bus.
1504 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001506 * This function resets the SATA bus, and then probes
1507 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 *
1509 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001510 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 *
1512 */
1513void sata_phy_reset(struct ata_port *ap)
1514{
1515 __sata_phy_reset(ap);
1516 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1517 return;
1518 ata_bus_reset(ap);
1519}
1520
1521/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001522 * ata_port_disable - Disable port.
1523 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001525 * Modify @ap data structure such that the system
1526 * thinks that the entire port is disabled, and should
1527 * never attempt to probe or communicate with devices
1528 * on this port.
1529 *
1530 * LOCKING: host_set lock, or some other form of
1531 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
1533
1534void ata_port_disable(struct ata_port *ap)
1535{
1536 ap->device[0].class = ATA_DEV_NONE;
1537 ap->device[1].class = ATA_DEV_NONE;
1538 ap->flags |= ATA_FLAG_PORT_DISABLED;
1539}
1540
1541static struct {
1542 unsigned int shift;
1543 u8 base;
1544} xfer_mode_classes[] = {
1545 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1546 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1547 { ATA_SHIFT_PIO, XFER_PIO_0 },
1548};
1549
1550static inline u8 base_from_shift(unsigned int shift)
1551{
1552 int i;
1553
1554 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1555 if (xfer_mode_classes[i].shift == shift)
1556 return xfer_mode_classes[i].base;
1557
1558 return 0xff;
1559}
1560
1561static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1562{
1563 int ofs, idx;
1564 u8 base;
1565
1566 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1567 return;
1568
1569 if (dev->xfer_shift == ATA_SHIFT_PIO)
1570 dev->flags |= ATA_DFLAG_PIO;
1571
1572 ata_dev_set_xfermode(ap, dev);
1573
1574 base = base_from_shift(dev->xfer_shift);
1575 ofs = dev->xfer_mode - base;
1576 idx = ofs + dev->xfer_shift;
1577 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1578
1579 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1580 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1581
1582 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1583 ap->id, dev->devno, xfer_mode_str[idx]);
1584}
1585
1586static int ata_host_set_pio(struct ata_port *ap)
1587{
1588 unsigned int mask;
1589 int x, i;
1590 u8 base, xfer_mode;
1591
1592 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1593 x = fgb(mask);
1594 if (x < 0) {
1595 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1596 return -1;
1597 }
1598
1599 base = base_from_shift(ATA_SHIFT_PIO);
1600 xfer_mode = base + x;
1601
1602 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1603 (int)base, (int)xfer_mode, mask, x);
1604
1605 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1606 struct ata_device *dev = &ap->device[i];
1607 if (ata_dev_present(dev)) {
1608 dev->pio_mode = xfer_mode;
1609 dev->xfer_mode = xfer_mode;
1610 dev->xfer_shift = ATA_SHIFT_PIO;
1611 if (ap->ops->set_piomode)
1612 ap->ops->set_piomode(ap, dev);
1613 }
1614 }
1615
1616 return 0;
1617}
1618
1619static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1620 unsigned int xfer_shift)
1621{
1622 int i;
1623
1624 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1625 struct ata_device *dev = &ap->device[i];
1626 if (ata_dev_present(dev)) {
1627 dev->dma_mode = xfer_mode;
1628 dev->xfer_mode = xfer_mode;
1629 dev->xfer_shift = xfer_shift;
1630 if (ap->ops->set_dmamode)
1631 ap->ops->set_dmamode(ap, dev);
1632 }
1633 }
1634}
1635
1636/**
1637 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1638 * @ap: port on which timings will be programmed
1639 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001640 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1641 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001643 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 *
1645 */
1646static void ata_set_mode(struct ata_port *ap)
1647{
1648 unsigned int i, xfer_shift;
1649 u8 xfer_mode;
1650 int rc;
1651
1652 /* step 1: always set host PIO timings */
1653 rc = ata_host_set_pio(ap);
1654 if (rc)
1655 goto err_out;
1656
1657 /* step 2: choose the best data xfer mode */
1658 xfer_mode = xfer_shift = 0;
1659 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1660 if (rc)
1661 goto err_out;
1662
1663 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1664 if (xfer_shift != ATA_SHIFT_PIO)
1665 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1666
1667 /* step 4: update devices' xfer mode */
1668 ata_dev_set_mode(ap, &ap->device[0]);
1669 ata_dev_set_mode(ap, &ap->device[1]);
1670
1671 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1672 return;
1673
1674 if (ap->ops->post_set_mode)
1675 ap->ops->post_set_mode(ap);
1676
1677 for (i = 0; i < 2; i++) {
1678 struct ata_device *dev = &ap->device[i];
1679 ata_dev_set_protocol(dev);
1680 }
1681
1682 return;
1683
1684err_out:
1685 ata_port_disable(ap);
1686}
1687
1688/**
1689 * ata_busy_sleep - sleep until BSY clears, or timeout
1690 * @ap: port containing status register to be polled
1691 * @tmout_pat: impatience timeout
1692 * @tmout: overall timeout
1693 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001694 * Sleep until ATA Status register bit BSY clears,
1695 * or a timeout occurs.
1696 *
1697 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 *
1699 */
1700
1701static unsigned int ata_busy_sleep (struct ata_port *ap,
1702 unsigned long tmout_pat,
1703 unsigned long tmout)
1704{
1705 unsigned long timer_start, timeout;
1706 u8 status;
1707
1708 status = ata_busy_wait(ap, ATA_BUSY, 300);
1709 timer_start = jiffies;
1710 timeout = timer_start + tmout_pat;
1711 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1712 msleep(50);
1713 status = ata_busy_wait(ap, ATA_BUSY, 3);
1714 }
1715
1716 if (status & ATA_BUSY)
1717 printk(KERN_WARNING "ata%u is slow to respond, "
1718 "please be patient\n", ap->id);
1719
1720 timeout = timer_start + tmout;
1721 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1722 msleep(50);
1723 status = ata_chk_status(ap);
1724 }
1725
1726 if (status & ATA_BUSY) {
1727 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1728 ap->id, tmout / HZ);
1729 return 1;
1730 }
1731
1732 return 0;
1733}
1734
1735static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1736{
1737 struct ata_ioports *ioaddr = &ap->ioaddr;
1738 unsigned int dev0 = devmask & (1 << 0);
1739 unsigned int dev1 = devmask & (1 << 1);
1740 unsigned long timeout;
1741
1742 /* if device 0 was found in ata_devchk, wait for its
1743 * BSY bit to clear
1744 */
1745 if (dev0)
1746 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1747
1748 /* if device 1 was found in ata_devchk, wait for
1749 * register access, then wait for BSY to clear
1750 */
1751 timeout = jiffies + ATA_TMOUT_BOOT;
1752 while (dev1) {
1753 u8 nsect, lbal;
1754
1755 ap->ops->dev_select(ap, 1);
1756 if (ap->flags & ATA_FLAG_MMIO) {
1757 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1758 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1759 } else {
1760 nsect = inb(ioaddr->nsect_addr);
1761 lbal = inb(ioaddr->lbal_addr);
1762 }
1763 if ((nsect == 1) && (lbal == 1))
1764 break;
1765 if (time_after(jiffies, timeout)) {
1766 dev1 = 0;
1767 break;
1768 }
1769 msleep(50); /* give drive a breather */
1770 }
1771 if (dev1)
1772 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1773
1774 /* is all this really necessary? */
1775 ap->ops->dev_select(ap, 0);
1776 if (dev1)
1777 ap->ops->dev_select(ap, 1);
1778 if (dev0)
1779 ap->ops->dev_select(ap, 0);
1780}
1781
1782/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001783 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1784 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001786 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1787 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 *
1789 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001790 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 *
1792 */
1793
1794static unsigned int ata_bus_edd(struct ata_port *ap)
1795{
1796 struct ata_taskfile tf;
1797
1798 /* set up execute-device-diag (bus reset) taskfile */
1799 /* also, take interrupts to a known state (disabled) */
1800 DPRINTK("execute-device-diag\n");
1801 ata_tf_init(ap, &tf, 0);
1802 tf.ctl |= ATA_NIEN;
1803 tf.command = ATA_CMD_EDD;
1804 tf.protocol = ATA_PROT_NODATA;
1805
1806 /* do bus reset */
1807 ata_tf_to_host(ap, &tf);
1808
1809 /* spec says at least 2ms. but who knows with those
1810 * crazy ATAPI devices...
1811 */
1812 msleep(150);
1813
1814 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1815}
1816
1817static unsigned int ata_bus_softreset(struct ata_port *ap,
1818 unsigned int devmask)
1819{
1820 struct ata_ioports *ioaddr = &ap->ioaddr;
1821
1822 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1823
1824 /* software reset. causes dev0 to be selected */
1825 if (ap->flags & ATA_FLAG_MMIO) {
1826 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1827 udelay(20); /* FIXME: flush */
1828 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1829 udelay(20); /* FIXME: flush */
1830 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1831 } else {
1832 outb(ap->ctl, ioaddr->ctl_addr);
1833 udelay(10);
1834 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1835 udelay(10);
1836 outb(ap->ctl, ioaddr->ctl_addr);
1837 }
1838
1839 /* spec mandates ">= 2ms" before checking status.
1840 * We wait 150ms, because that was the magic delay used for
1841 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1842 * between when the ATA command register is written, and then
1843 * status is checked. Because waiting for "a while" before
1844 * checking status is fine, post SRST, we perform this magic
1845 * delay here as well.
1846 */
1847 msleep(150);
1848
1849 ata_bus_post_reset(ap, devmask);
1850
1851 return 0;
1852}
1853
1854/**
1855 * ata_bus_reset - reset host port and associated ATA channel
1856 * @ap: port to reset
1857 *
1858 * This is typically the first time we actually start issuing
1859 * commands to the ATA channel. We wait for BSY to clear, then
1860 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1861 * result. Determine what devices, if any, are on the channel
1862 * by looking at the device 0/1 error register. Look at the signature
1863 * stored in each device's taskfile registers, to determine if
1864 * the device is ATA or ATAPI.
1865 *
1866 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001867 * PCI/etc. bus probe sem.
1868 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 *
1870 * SIDE EFFECTS:
1871 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1872 */
1873
1874void ata_bus_reset(struct ata_port *ap)
1875{
1876 struct ata_ioports *ioaddr = &ap->ioaddr;
1877 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1878 u8 err;
1879 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1880
1881 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1882
1883 /* determine if device 0/1 are present */
1884 if (ap->flags & ATA_FLAG_SATA_RESET)
1885 dev0 = 1;
1886 else {
1887 dev0 = ata_devchk(ap, 0);
1888 if (slave_possible)
1889 dev1 = ata_devchk(ap, 1);
1890 }
1891
1892 if (dev0)
1893 devmask |= (1 << 0);
1894 if (dev1)
1895 devmask |= (1 << 1);
1896
1897 /* select device 0 again */
1898 ap->ops->dev_select(ap, 0);
1899
1900 /* issue bus reset */
1901 if (ap->flags & ATA_FLAG_SRST)
1902 rc = ata_bus_softreset(ap, devmask);
1903 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1904 /* set up device control */
1905 if (ap->flags & ATA_FLAG_MMIO)
1906 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1907 else
1908 outb(ap->ctl, ioaddr->ctl_addr);
1909 rc = ata_bus_edd(ap);
1910 }
1911
1912 if (rc)
1913 goto err_out;
1914
1915 /*
1916 * determine by signature whether we have ATA or ATAPI devices
1917 */
1918 err = ata_dev_try_classify(ap, 0);
1919 if ((slave_possible) && (err != 0x81))
1920 ata_dev_try_classify(ap, 1);
1921
1922 /* re-enable interrupts */
1923 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1924 ata_irq_on(ap);
1925
1926 /* is double-select really necessary? */
1927 if (ap->device[1].class != ATA_DEV_NONE)
1928 ap->ops->dev_select(ap, 1);
1929 if (ap->device[0].class != ATA_DEV_NONE)
1930 ap->ops->dev_select(ap, 0);
1931
1932 /* if no devices were detected, disable this port */
1933 if ((ap->device[0].class == ATA_DEV_NONE) &&
1934 (ap->device[1].class == ATA_DEV_NONE))
1935 goto err_out;
1936
1937 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1938 /* set up device control for ATA_FLAG_SATA_RESET */
1939 if (ap->flags & ATA_FLAG_MMIO)
1940 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1941 else
1942 outb(ap->ctl, ioaddr->ctl_addr);
1943 }
1944
1945 DPRINTK("EXIT\n");
1946 return;
1947
1948err_out:
1949 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1950 ap->ops->port_disable(ap);
1951
1952 DPRINTK("EXIT\n");
1953}
1954
1955static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1956{
1957 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1958 ap->id, dev->devno);
1959}
1960
1961static const char * ata_dma_blacklist [] = {
1962 "WDC AC11000H",
1963 "WDC AC22100H",
1964 "WDC AC32500H",
1965 "WDC AC33100H",
1966 "WDC AC31600H",
1967 "WDC AC32100H",
1968 "WDC AC23200L",
1969 "Compaq CRD-8241B",
1970 "CRD-8400B",
1971 "CRD-8480B",
1972 "CRD-8482B",
1973 "CRD-84",
1974 "SanDisk SDP3B",
1975 "SanDisk SDP3B-64",
1976 "SANYO CD-ROM CRD",
1977 "HITACHI CDR-8",
1978 "HITACHI CDR-8335",
1979 "HITACHI CDR-8435",
1980 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04001981 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 "CD-532E-A",
1983 "E-IDE CD-ROM CR-840",
1984 "CD-ROM Drive/F5A",
1985 "WPI CDD-820",
1986 "SAMSUNG CD-ROM SC-148C",
1987 "SAMSUNG CD-ROM SC",
1988 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1990 "_NEC DV5800A",
1991};
1992
1993static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1994{
1995 unsigned char model_num[40];
1996 char *s;
1997 unsigned int len;
1998 int i;
1999
2000 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2001 sizeof(model_num));
2002 s = &model_num[0];
2003 len = strnlen(s, sizeof(model_num));
2004
2005 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2006 while ((len > 0) && (s[len - 1] == ' ')) {
2007 len--;
2008 s[len] = 0;
2009 }
2010
2011 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2012 if (!strncmp(ata_dma_blacklist[i], s, len))
2013 return 1;
2014
2015 return 0;
2016}
2017
2018static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
2019{
2020 struct ata_device *master, *slave;
2021 unsigned int mask;
2022
2023 master = &ap->device[0];
2024 slave = &ap->device[1];
2025
2026 assert (ata_dev_present(master) || ata_dev_present(slave));
2027
2028 if (shift == ATA_SHIFT_UDMA) {
2029 mask = ap->udma_mask;
2030 if (ata_dev_present(master)) {
2031 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2032 if (ata_dma_blacklisted(ap, master)) {
2033 mask = 0;
2034 ata_pr_blacklisted(ap, master);
2035 }
2036 }
2037 if (ata_dev_present(slave)) {
2038 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2039 if (ata_dma_blacklisted(ap, slave)) {
2040 mask = 0;
2041 ata_pr_blacklisted(ap, slave);
2042 }
2043 }
2044 }
2045 else if (shift == ATA_SHIFT_MWDMA) {
2046 mask = ap->mwdma_mask;
2047 if (ata_dev_present(master)) {
2048 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2049 if (ata_dma_blacklisted(ap, master)) {
2050 mask = 0;
2051 ata_pr_blacklisted(ap, master);
2052 }
2053 }
2054 if (ata_dev_present(slave)) {
2055 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2056 if (ata_dma_blacklisted(ap, slave)) {
2057 mask = 0;
2058 ata_pr_blacklisted(ap, slave);
2059 }
2060 }
2061 }
2062 else if (shift == ATA_SHIFT_PIO) {
2063 mask = ap->pio_mask;
2064 if (ata_dev_present(master)) {
2065 /* spec doesn't return explicit support for
2066 * PIO0-2, so we fake it
2067 */
2068 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2069 tmp_mode <<= 3;
2070 tmp_mode |= 0x7;
2071 mask &= tmp_mode;
2072 }
2073 if (ata_dev_present(slave)) {
2074 /* spec doesn't return explicit support for
2075 * PIO0-2, so we fake it
2076 */
2077 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2078 tmp_mode <<= 3;
2079 tmp_mode |= 0x7;
2080 mask &= tmp_mode;
2081 }
2082 }
2083 else {
2084 mask = 0xffffffff; /* shut up compiler warning */
2085 BUG();
2086 }
2087
2088 return mask;
2089}
2090
2091/* find greatest bit */
2092static int fgb(u32 bitmap)
2093{
2094 unsigned int i;
2095 int x = -1;
2096
2097 for (i = 0; i < 32; i++)
2098 if (bitmap & (1 << i))
2099 x = i;
2100
2101 return x;
2102}
2103
2104/**
2105 * ata_choose_xfer_mode - attempt to find best transfer mode
2106 * @ap: Port for which an xfer mode will be selected
2107 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2108 * @xfer_shift_out: (output) bit shift that selects this mode
2109 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002110 * Based on host and device capabilities, determine the
2111 * maximum transfer mode that is amenable to all.
2112 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002114 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 *
2116 * RETURNS:
2117 * Zero on success, negative on error.
2118 */
2119
2120static int ata_choose_xfer_mode(struct ata_port *ap,
2121 u8 *xfer_mode_out,
2122 unsigned int *xfer_shift_out)
2123{
2124 unsigned int mask, shift;
2125 int x, i;
2126
2127 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2128 shift = xfer_mode_classes[i].shift;
2129 mask = ata_get_mode_mask(ap, shift);
2130
2131 x = fgb(mask);
2132 if (x >= 0) {
2133 *xfer_mode_out = xfer_mode_classes[i].base + x;
2134 *xfer_shift_out = shift;
2135 return 0;
2136 }
2137 }
2138
2139 return -1;
2140}
2141
2142/**
2143 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2144 * @ap: Port associated with device @dev
2145 * @dev: Device to which command will be sent
2146 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002147 * Issue SET FEATURES - XFER MODE command to device @dev
2148 * on port @ap.
2149 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002151 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 */
2153
2154static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2155{
2156 DECLARE_COMPLETION(wait);
2157 struct ata_queued_cmd *qc;
2158 int rc;
2159 unsigned long flags;
2160
2161 /* set up set-features taskfile */
2162 DPRINTK("set features - xfer mode\n");
2163
2164 qc = ata_qc_new_init(ap, dev);
2165 BUG_ON(qc == NULL);
2166
2167 qc->tf.command = ATA_CMD_SET_FEATURES;
2168 qc->tf.feature = SETFEATURES_XFER;
2169 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2170 qc->tf.protocol = ATA_PROT_NODATA;
2171 qc->tf.nsect = dev->xfer_mode;
2172
2173 qc->waiting = &wait;
2174 qc->complete_fn = ata_qc_complete_noop;
2175
2176 spin_lock_irqsave(&ap->host_set->lock, flags);
2177 rc = ata_qc_issue(qc);
2178 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2179
2180 if (rc)
2181 ata_port_disable(ap);
2182 else
2183 wait_for_completion(&wait);
2184
2185 DPRINTK("EXIT\n");
2186}
2187
2188/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002189 * ata_dev_init_params - Issue INIT DEV PARAMS command
2190 * @ap: Port associated with device @dev
2191 * @dev: Device to which command will be sent
2192 *
2193 * LOCKING:
2194 */
2195
2196static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2197{
2198 DECLARE_COMPLETION(wait);
2199 struct ata_queued_cmd *qc;
2200 int rc;
2201 unsigned long flags;
2202 u16 sectors = dev->id[6];
2203 u16 heads = dev->id[3];
2204
2205 /* Number of sectors per track 1-255. Number of heads 1-16 */
2206 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2207 return;
2208
2209 /* set up init dev params taskfile */
2210 DPRINTK("init dev params \n");
2211
2212 qc = ata_qc_new_init(ap, dev);
2213 BUG_ON(qc == NULL);
2214
2215 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2216 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2217 qc->tf.protocol = ATA_PROT_NODATA;
2218 qc->tf.nsect = sectors;
2219 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2220
2221 qc->waiting = &wait;
2222 qc->complete_fn = ata_qc_complete_noop;
2223
2224 spin_lock_irqsave(&ap->host_set->lock, flags);
2225 rc = ata_qc_issue(qc);
2226 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2227
2228 if (rc)
2229 ata_port_disable(ap);
2230 else
2231 wait_for_completion(&wait);
2232
2233 DPRINTK("EXIT\n");
2234}
2235
2236/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002237 * ata_sg_clean - Unmap DMA memory associated with command
2238 * @qc: Command containing DMA memory to be released
2239 *
2240 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 *
2242 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002243 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 */
2245
2246static void ata_sg_clean(struct ata_queued_cmd *qc)
2247{
2248 struct ata_port *ap = qc->ap;
2249 struct scatterlist *sg = qc->sg;
2250 int dir = qc->dma_dir;
2251
2252 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2253 assert(sg != NULL);
2254
2255 if (qc->flags & ATA_QCFLAG_SINGLE)
2256 assert(qc->n_elem == 1);
2257
2258 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2259
2260 if (qc->flags & ATA_QCFLAG_SG)
2261 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2262 else
2263 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2264 sg_dma_len(&sg[0]), dir);
2265
2266 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2267 qc->sg = NULL;
2268}
2269
2270/**
2271 * ata_fill_sg - Fill PCI IDE PRD table
2272 * @qc: Metadata associated with taskfile to be transferred
2273 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002274 * Fill PCI IDE PRD (scatter-gather) table with segments
2275 * associated with the current disk command.
2276 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002278 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 *
2280 */
2281static void ata_fill_sg(struct ata_queued_cmd *qc)
2282{
2283 struct scatterlist *sg = qc->sg;
2284 struct ata_port *ap = qc->ap;
2285 unsigned int idx, nelem;
2286
2287 assert(sg != NULL);
2288 assert(qc->n_elem > 0);
2289
2290 idx = 0;
2291 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2292 u32 addr, offset;
2293 u32 sg_len, len;
2294
2295 /* determine if physical DMA addr spans 64K boundary.
2296 * Note h/w doesn't support 64-bit, so we unconditionally
2297 * truncate dma_addr_t to u32.
2298 */
2299 addr = (u32) sg_dma_address(sg);
2300 sg_len = sg_dma_len(sg);
2301
2302 while (sg_len) {
2303 offset = addr & 0xffff;
2304 len = sg_len;
2305 if ((offset + sg_len) > 0x10000)
2306 len = 0x10000 - offset;
2307
2308 ap->prd[idx].addr = cpu_to_le32(addr);
2309 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2310 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2311
2312 idx++;
2313 sg_len -= len;
2314 addr += len;
2315 }
2316 }
2317
2318 if (idx)
2319 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2320}
2321/**
2322 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2323 * @qc: Metadata associated with taskfile to check
2324 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002325 * Allow low-level driver to filter ATA PACKET commands, returning
2326 * a status indicating whether or not it is OK to use DMA for the
2327 * supplied PACKET command.
2328 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002330 * spin_lock_irqsave(host_set lock)
2331 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 * RETURNS: 0 when ATAPI DMA can be used
2333 * nonzero otherwise
2334 */
2335int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2336{
2337 struct ata_port *ap = qc->ap;
2338 int rc = 0; /* Assume ATAPI DMA is OK by default */
2339
2340 if (ap->ops->check_atapi_dma)
2341 rc = ap->ops->check_atapi_dma(qc);
2342
2343 return rc;
2344}
2345/**
2346 * ata_qc_prep - Prepare taskfile for submission
2347 * @qc: Metadata associated with taskfile to be prepared
2348 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002349 * Prepare ATA taskfile for submission.
2350 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 * LOCKING:
2352 * spin_lock_irqsave(host_set lock)
2353 */
2354void ata_qc_prep(struct ata_queued_cmd *qc)
2355{
2356 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2357 return;
2358
2359 ata_fill_sg(qc);
2360}
2361
Jeff Garzik0cba6322005-05-30 19:49:12 -04002362/**
2363 * ata_sg_init_one - Associate command with memory buffer
2364 * @qc: Command to be associated
2365 * @buf: Memory buffer
2366 * @buflen: Length of memory buffer, in bytes.
2367 *
2368 * Initialize the data-related elements of queued_cmd @qc
2369 * to point to a single memory buffer, @buf of byte length @buflen.
2370 *
2371 * LOCKING:
2372 * spin_lock_irqsave(host_set lock)
2373 */
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2376{
2377 struct scatterlist *sg;
2378
2379 qc->flags |= ATA_QCFLAG_SINGLE;
2380
2381 memset(&qc->sgent, 0, sizeof(qc->sgent));
2382 qc->sg = &qc->sgent;
2383 qc->n_elem = 1;
2384 qc->buf_virt = buf;
2385
2386 sg = qc->sg;
2387 sg->page = virt_to_page(buf);
2388 sg->offset = (unsigned long) buf & ~PAGE_MASK;
Albert Lee32529e02005-05-26 03:49:42 -04002389 sg->length = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
Jeff Garzik0cba6322005-05-30 19:49:12 -04002392/**
2393 * ata_sg_init - Associate command with scatter-gather table.
2394 * @qc: Command to be associated
2395 * @sg: Scatter-gather table.
2396 * @n_elem: Number of elements in s/g table.
2397 *
2398 * Initialize the data-related elements of queued_cmd @qc
2399 * to point to a scatter-gather table @sg, containing @n_elem
2400 * elements.
2401 *
2402 * LOCKING:
2403 * spin_lock_irqsave(host_set lock)
2404 */
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2407 unsigned int n_elem)
2408{
2409 qc->flags |= ATA_QCFLAG_SG;
2410 qc->sg = sg;
2411 qc->n_elem = n_elem;
2412}
2413
2414/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002415 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2416 * @qc: Command with memory buffer to be mapped.
2417 *
2418 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 *
2420 * LOCKING:
2421 * spin_lock_irqsave(host_set lock)
2422 *
2423 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002424 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 */
2426
2427static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2428{
2429 struct ata_port *ap = qc->ap;
2430 int dir = qc->dma_dir;
2431 struct scatterlist *sg = qc->sg;
2432 dma_addr_t dma_address;
2433
2434 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002435 sg->length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (dma_mapping_error(dma_address))
2437 return -1;
2438
2439 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002440 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2443 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2444
2445 return 0;
2446}
2447
2448/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002449 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2450 * @qc: Command with scatter-gather table to be mapped.
2451 *
2452 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 *
2454 * LOCKING:
2455 * spin_lock_irqsave(host_set lock)
2456 *
2457 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002458 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 *
2460 */
2461
2462static int ata_sg_setup(struct ata_queued_cmd *qc)
2463{
2464 struct ata_port *ap = qc->ap;
2465 struct scatterlist *sg = qc->sg;
2466 int n_elem, dir;
2467
2468 VPRINTK("ENTER, ata%u\n", ap->id);
2469 assert(qc->flags & ATA_QCFLAG_SG);
2470
2471 dir = qc->dma_dir;
2472 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2473 if (n_elem < 1)
2474 return -1;
2475
2476 DPRINTK("%d sg elements mapped\n", n_elem);
2477
2478 qc->n_elem = n_elem;
2479
2480 return 0;
2481}
2482
2483/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002484 * ata_poll_qc_complete - turn irq back on and finish qc
2485 * @qc: Command to complete
2486 * @drv_stat: ATA status register content
2487 *
2488 * LOCKING:
2489 * None. (grabs host lock)
2490 */
2491
2492void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2493{
2494 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002495 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002496
Jeff Garzikb8f61532005-08-25 22:01:20 -04002497 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002498 ata_irq_on(ap);
2499 ata_qc_complete(qc, drv_stat);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002500 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002501}
2502
2503/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 * ata_pio_poll -
2505 * @ap:
2506 *
2507 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002508 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 *
2510 * RETURNS:
2511 *
2512 */
2513
2514static unsigned long ata_pio_poll(struct ata_port *ap)
2515{
2516 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002517 unsigned int poll_state = HSM_ST_UNKNOWN;
2518 unsigned int reg_state = HSM_ST_UNKNOWN;
2519 const unsigned int tmout_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Albert Lee14be71f2005-09-27 17:36:35 +08002521 switch (ap->hsm_task_state) {
2522 case HSM_ST:
2523 case HSM_ST_POLL:
2524 poll_state = HSM_ST_POLL;
2525 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002527 case HSM_ST_LAST:
2528 case HSM_ST_LAST_POLL:
2529 poll_state = HSM_ST_LAST_POLL;
2530 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 break;
2532 default:
2533 BUG();
2534 break;
2535 }
2536
2537 status = ata_chk_status(ap);
2538 if (status & ATA_BUSY) {
2539 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002540 ap->hsm_task_state = tmout_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 return 0;
2542 }
Albert Lee14be71f2005-09-27 17:36:35 +08002543 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return ATA_SHORT_PAUSE;
2545 }
2546
Albert Lee14be71f2005-09-27 17:36:35 +08002547 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 return 0;
2549}
2550
2551/**
2552 * ata_pio_complete -
2553 * @ap:
2554 *
2555 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002556 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002557 *
2558 * RETURNS:
2559 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 */
2561
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002562static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 struct ata_queued_cmd *qc;
2565 u8 drv_stat;
2566
2567 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002568 * This is purely heuristic. This is a fast path. Sometimes when
2569 * we enter, BSY will be cleared in a chk-status or two. If not,
2570 * the drive is probably seeking or something. Snooze for a couple
2571 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002572 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 */
2574 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2575 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2576 msleep(2);
2577 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2578 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002579 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583 }
2584
2585 drv_stat = ata_wait_idle(ap);
2586 if (!ata_ok(drv_stat)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002587 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590
2591 qc = ata_qc_from_tag(ap, ap->active_tag);
2592 assert(qc != NULL);
2593
Albert Lee14be71f2005-09-27 17:36:35 +08002594 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Tejun Heo40e8c822005-08-22 17:12:45 +09002596 ata_poll_qc_complete(qc, drv_stat);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002597
2598 /* another command may start at this point */
2599
2600 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601}
2602
Edward Falk0baab862005-06-02 18:17:13 -04002603
2604/**
2605 * swap_buf_le16 -
2606 * @buf: Buffer to swap
2607 * @buf_words: Number of 16-bit words in buffer.
2608 *
2609 * Swap halves of 16-bit words if needed to convert from
2610 * little-endian byte order to native cpu byte order, or
2611 * vice-versa.
2612 *
2613 * LOCKING:
2614 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615void swap_buf_le16(u16 *buf, unsigned int buf_words)
2616{
2617#ifdef __BIG_ENDIAN
2618 unsigned int i;
2619
2620 for (i = 0; i < buf_words; i++)
2621 buf[i] = le16_to_cpu(buf[i]);
2622#endif /* __BIG_ENDIAN */
2623}
2624
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002625/**
2626 * ata_mmio_data_xfer - Transfer data by MMIO
2627 * @ap: port to read/write
2628 * @buf: data buffer
2629 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002630 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002631 *
2632 * Transfer data from/to the device data register by MMIO.
2633 *
2634 * LOCKING:
2635 * Inherited from caller.
2636 *
2637 */
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2640 unsigned int buflen, int write_data)
2641{
2642 unsigned int i;
2643 unsigned int words = buflen >> 1;
2644 u16 *buf16 = (u16 *) buf;
2645 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2646
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002647 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (write_data) {
2649 for (i = 0; i < words; i++)
2650 writew(le16_to_cpu(buf16[i]), mmio);
2651 } else {
2652 for (i = 0; i < words; i++)
2653 buf16[i] = cpu_to_le16(readw(mmio));
2654 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002655
2656 /* Transfer trailing 1 byte, if any. */
2657 if (unlikely(buflen & 0x01)) {
2658 u16 align_buf[1] = { 0 };
2659 unsigned char *trailing_buf = buf + buflen - 1;
2660
2661 if (write_data) {
2662 memcpy(align_buf, trailing_buf, 1);
2663 writew(le16_to_cpu(align_buf[0]), mmio);
2664 } else {
2665 align_buf[0] = cpu_to_le16(readw(mmio));
2666 memcpy(trailing_buf, align_buf, 1);
2667 }
2668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002671/**
2672 * ata_pio_data_xfer - Transfer data by PIO
2673 * @ap: port to read/write
2674 * @buf: data buffer
2675 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002676 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002677 *
2678 * Transfer data from/to the device data register by PIO.
2679 *
2680 * LOCKING:
2681 * Inherited from caller.
2682 *
2683 */
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2686 unsigned int buflen, int write_data)
2687{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002688 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002690 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002692 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002694 insw(ap->ioaddr.data_addr, buf, words);
2695
2696 /* Transfer trailing 1 byte, if any. */
2697 if (unlikely(buflen & 0x01)) {
2698 u16 align_buf[1] = { 0 };
2699 unsigned char *trailing_buf = buf + buflen - 1;
2700
2701 if (write_data) {
2702 memcpy(align_buf, trailing_buf, 1);
2703 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2704 } else {
2705 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2706 memcpy(trailing_buf, align_buf, 1);
2707 }
2708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709}
2710
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002711/**
2712 * ata_data_xfer - Transfer data from/to the data register.
2713 * @ap: port to read/write
2714 * @buf: data buffer
2715 * @buflen: buffer length
2716 * @do_write: read/write
2717 *
2718 * Transfer data from/to the device data register.
2719 *
2720 * LOCKING:
2721 * Inherited from caller.
2722 *
2723 */
2724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2726 unsigned int buflen, int do_write)
2727{
2728 if (ap->flags & ATA_FLAG_MMIO)
2729 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2730 else
2731 ata_pio_data_xfer(ap, buf, buflen, do_write);
2732}
2733
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002734/**
2735 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2736 * @qc: Command on going
2737 *
2738 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2739 *
2740 * LOCKING:
2741 * Inherited from caller.
2742 */
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744static void ata_pio_sector(struct ata_queued_cmd *qc)
2745{
2746 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2747 struct scatterlist *sg = qc->sg;
2748 struct ata_port *ap = qc->ap;
2749 struct page *page;
2750 unsigned int offset;
2751 unsigned char *buf;
2752
2753 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08002754 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 page = sg[qc->cursg].page;
2757 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2758
2759 /* get the current page and offset */
2760 page = nth_page(page, (offset >> PAGE_SHIFT));
2761 offset %= PAGE_SIZE;
2762
Albert Lee7282aa42005-10-09 09:46:07 -04002763 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2764
Albert Lee91b8b312005-10-09 09:48:44 -04002765 if (PageHighMem(page)) {
2766 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04002767
Albert Lee91b8b312005-10-09 09:48:44 -04002768 local_irq_save(flags);
2769 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04002770
Albert Lee91b8b312005-10-09 09:48:44 -04002771 /* do the actual data transfer */
2772 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
2773
2774 kunmap_atomic(buf, KM_IRQ0);
2775 local_irq_restore(flags);
2776 } else {
2777 buf = page_address(page);
2778 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
2779 }
Albert Lee7282aa42005-10-09 09:46:07 -04002780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 qc->cursect++;
2782 qc->cursg_ofs++;
2783
Albert Lee32529e02005-05-26 03:49:42 -04002784 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 qc->cursg++;
2786 qc->cursg_ofs = 0;
2787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002790/**
Albert Leec71c1852005-10-04 06:03:45 -04002791 * atapi_send_cdb - Write CDB bytes to hardware
2792 * @ap: Port to which ATAPI device is attached.
2793 * @qc: Taskfile currently active
2794 *
2795 * When device has indicated its readiness to accept
2796 * a CDB, this function is called. Send the CDB.
2797 *
2798 * LOCKING:
2799 * caller.
2800 */
2801
2802static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
2803{
2804 /* send SCSI cdb */
2805 DPRINTK("send cdb\n");
2806 assert(ap->cdb_len >= 12);
2807
2808 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
2809 ata_altstatus(ap); /* flush */
2810
2811 switch (qc->tf.protocol) {
2812 case ATA_PROT_ATAPI:
2813 ap->hsm_task_state = HSM_ST;
2814 break;
2815 case ATA_PROT_ATAPI_NODATA:
2816 ap->hsm_task_state = HSM_ST_LAST;
2817 break;
2818 case ATA_PROT_ATAPI_DMA:
2819 ap->hsm_task_state = HSM_ST_LAST;
2820 /* initiate bmdma */
2821 ap->ops->bmdma_start(qc);
2822 break;
2823 }
2824}
2825
2826/**
2827 * ata_dataout_task - Write first data block to hardware
2828 * @_data: Port to which ATA/ATAPI device is attached.
2829 *
2830 * When device has indicated its readiness to accept
2831 * the data, this function sends out the CDB or
2832 * the first data block by PIO.
2833 * After this,
2834 * - If polling, ata_pio_task() handles the rest.
2835 * - Otherwise, interrupt handler takes over.
2836 *
2837 * LOCKING:
2838 * Kernel thread context (may sleep)
2839 */
2840
2841static void ata_dataout_task(void *_data)
2842{
2843 struct ata_port *ap = _data;
2844 struct ata_queued_cmd *qc;
2845 u8 status;
2846 unsigned long flags;
2847
2848 qc = ata_qc_from_tag(ap, ap->active_tag);
2849 assert(qc != NULL);
2850 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2851
2852 /* sleep-wait for BSY to clear */
2853 DPRINTK("busy wait\n");
2854 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT))
2855 goto err_out;
2856
2857 /* make sure DRQ is set */
2858 status = ata_chk_status(ap);
2859 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
2860 goto err_out;
2861
2862 /* Send the CDB (atapi) or the first data block (ata pio out).
2863 * During the state transition, interrupt handler shouldn't
2864 * be invoked before the data transfer is complete and
2865 * hsm_task_state is changed. Hence, the following locking.
2866 */
2867 spin_lock_irqsave(&ap->host_set->lock, flags);
2868
2869 if (qc->tf.protocol == ATA_PROT_PIO) {
2870 /* PIO data out protocol.
2871 * send first data block.
2872 */
2873
2874 /* ata_pio_sector() might change the state to HSM_ST_LAST.
2875 * so, the state is changed here before ata_pio_sector().
2876 */
2877 ap->hsm_task_state = HSM_ST;
2878 ata_pio_sector(qc);
2879 ata_altstatus(ap); /* flush */
2880 } else
2881 /* send CDB */
2882 atapi_send_cdb(ap, qc);
2883
2884 /* if polling, ata_pio_task() handles the rest.
2885 * otherwise, interrupt handler takes over from here.
2886 */
2887 if (qc->tf.flags & ATA_TFLAG_POLLING)
2888 queue_work(ata_wq, &ap->pio_task);
2889
2890 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2891
2892 return;
2893
2894err_out:
2895 ata_pio_error(ap);
2896}
2897
2898/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002899 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2900 * @qc: Command on going
2901 * @bytes: number of bytes
2902 *
2903 * Transfer Transfer data from/to the ATAPI device.
2904 *
2905 * LOCKING:
2906 * Inherited from caller.
2907 *
2908 */
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2911{
2912 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2913 struct scatterlist *sg = qc->sg;
2914 struct ata_port *ap = qc->ap;
2915 struct page *page;
2916 unsigned char *buf;
2917 unsigned int offset, count;
2918
Albert Lee563a6e12005-08-12 14:17:50 +08002919 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08002920 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08002923 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002924 /*
Albert Lee563a6e12005-08-12 14:17:50 +08002925 * The end of qc->sg is reached and the device expects
2926 * more data to transfer. In order not to overrun qc->sg
2927 * and fulfill length specified in the byte count register,
2928 * - for read case, discard trailing data from the device
2929 * - for write case, padding zero data to the device
2930 */
2931 u16 pad_buf[1] = { 0 };
2932 unsigned int words = bytes >> 1;
2933 unsigned int i;
2934
2935 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002936 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08002937 ap->id, bytes);
2938
2939 for (i = 0; i < words; i++)
2940 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2941
Albert Lee14be71f2005-09-27 17:36:35 +08002942 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08002943 return;
2944 }
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 sg = &qc->sg[qc->cursg];
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 page = sg->page;
2949 offset = sg->offset + qc->cursg_ofs;
2950
2951 /* get the current page and offset */
2952 page = nth_page(page, (offset >> PAGE_SHIFT));
2953 offset %= PAGE_SIZE;
2954
Albert Lee6952df02005-06-06 15:56:03 +08002955 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04002956 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 /* don't cross page boundaries */
2959 count = min(count, (unsigned int)PAGE_SIZE - offset);
2960
Albert Lee7282aa42005-10-09 09:46:07 -04002961 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2962
Albert Lee91b8b312005-10-09 09:48:44 -04002963 if (PageHighMem(page)) {
2964 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04002965
Albert Lee91b8b312005-10-09 09:48:44 -04002966 local_irq_save(flags);
2967 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04002968
Albert Lee91b8b312005-10-09 09:48:44 -04002969 /* do the actual data transfer */
2970 ata_data_xfer(ap, buf + offset, count, do_write);
2971
2972 kunmap_atomic(buf, KM_IRQ0);
2973 local_irq_restore(flags);
2974 } else {
2975 buf = page_address(page);
2976 ata_data_xfer(ap, buf + offset, count, do_write);
2977 }
Albert Lee7282aa42005-10-09 09:46:07 -04002978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 bytes -= count;
2980 qc->curbytes += count;
2981 qc->cursg_ofs += count;
2982
Albert Lee32529e02005-05-26 03:49:42 -04002983 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 qc->cursg++;
2985 qc->cursg_ofs = 0;
2986 }
2987
Albert Lee563a6e12005-08-12 14:17:50 +08002988 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002992/**
2993 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2994 * @qc: Command on going
2995 *
2996 * Transfer Transfer data from/to the ATAPI device.
2997 *
2998 * LOCKING:
2999 * Inherited from caller.
3000 *
3001 */
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3004{
3005 struct ata_port *ap = qc->ap;
3006 struct ata_device *dev = qc->dev;
3007 unsigned int ireason, bc_lo, bc_hi, bytes;
3008 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3009
3010 ap->ops->tf_read(ap, &qc->tf);
3011 ireason = qc->tf.nsect;
3012 bc_lo = qc->tf.lbam;
3013 bc_hi = qc->tf.lbah;
3014 bytes = (bc_hi << 8) | bc_lo;
3015
3016 /* shall be cleared to zero, indicating xfer of data */
3017 if (ireason & (1 << 0))
3018 goto err_out;
3019
3020 /* make sure transfer direction matches expected */
3021 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3022 if (do_write != i_write)
3023 goto err_out;
3024
Albert Lee312f7da2005-09-27 17:38:03 +08003025 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 __atapi_pio_bytes(qc, bytes);
3028
3029 return;
3030
3031err_out:
3032 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3033 ap->id, dev->devno);
Albert Lee14be71f2005-09-27 17:36:35 +08003034 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035}
3036
3037/**
3038 * ata_pio_sector -
3039 * @ap:
3040 *
3041 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003042 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 */
3044
3045static void ata_pio_block(struct ata_port *ap)
3046{
3047 struct ata_queued_cmd *qc;
3048 u8 status;
3049
3050 /*
3051 * This is purely hueristic. This is a fast path.
3052 * Sometimes when we enter, BSY will be cleared in
3053 * a chk-status or two. If not, the drive is probably seeking
3054 * or something. Snooze for a couple msecs, then
3055 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003056 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 */
3058 status = ata_busy_wait(ap, ATA_BUSY, 5);
3059 if (status & ATA_BUSY) {
3060 msleep(2);
3061 status = ata_busy_wait(ap, ATA_BUSY, 10);
3062 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003063 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3065 return;
3066 }
3067 }
3068
3069 qc = ata_qc_from_tag(ap, ap->active_tag);
3070 assert(qc != NULL);
3071
3072 if (is_atapi_taskfile(&qc->tf)) {
3073 /* no more data to transfer or unsupported ATAPI command */
3074 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003075 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return;
3077 }
3078
3079 atapi_pio_bytes(qc);
3080 } else {
3081 /* handle BSY=0, DRQ=0 as error */
3082 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003083 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return;
3085 }
3086
3087 ata_pio_sector(qc);
3088 }
3089}
3090
3091static void ata_pio_error(struct ata_port *ap)
3092{
3093 struct ata_queued_cmd *qc;
3094 u8 drv_stat;
3095
3096 qc = ata_qc_from_tag(ap, ap->active_tag);
3097 assert(qc != NULL);
3098
3099 drv_stat = ata_chk_status(ap);
3100 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
3101 ap->id, drv_stat);
3102
Albert Lee14be71f2005-09-27 17:36:35 +08003103 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Tejun Heo40e8c822005-08-22 17:12:45 +09003105 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
3108static void ata_pio_task(void *_data)
3109{
3110 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003111 unsigned long timeout;
3112 int qc_completed;
3113
3114fsm_start:
3115 timeout = 0;
3116 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Albert Lee14be71f2005-09-27 17:36:35 +08003118 switch (ap->hsm_task_state) {
3119 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return;
3121
Albert Lee14be71f2005-09-27 17:36:35 +08003122 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 ata_pio_block(ap);
3124 break;
3125
Albert Lee14be71f2005-09-27 17:36:35 +08003126 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003127 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 break;
3129
Albert Lee14be71f2005-09-27 17:36:35 +08003130 case HSM_ST_POLL:
3131 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 timeout = ata_pio_poll(ap);
3133 break;
3134
Albert Lee14be71f2005-09-27 17:36:35 +08003135 case HSM_ST_TMOUT:
3136 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 ata_pio_error(ap);
3138 return;
3139 }
3140
3141 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003142 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3143 else if (!qc_completed)
3144 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147/**
3148 * ata_qc_timeout - Handle timeout of queued command
3149 * @qc: Command that timed out
3150 *
3151 * Some part of the kernel (currently, only the SCSI layer)
3152 * has noticed that the active command on port @ap has not
3153 * completed after a specified length of time. Handle this
3154 * condition by disabling DMA (if necessary) and completing
3155 * transactions, with error if necessary.
3156 *
3157 * This also handles the case of the "lost interrupt", where
3158 * for some reason (possibly hardware bug, possibly driver bug)
3159 * an interrupt was not delivered to the driver, even though the
3160 * transaction completed successfully.
3161 *
3162 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003163 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 */
3165
3166static void ata_qc_timeout(struct ata_queued_cmd *qc)
3167{
3168 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003169 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 struct ata_device *dev = qc->dev;
3171 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003172 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
3174 DPRINTK("ENTER\n");
3175
3176 /* FIXME: doesn't this conflict with timeout handling? */
3177 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3178 struct scsi_cmnd *cmd = qc->scsicmd;
3179
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02003180 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182 /* finish completing original command */
Jeff Garzikb8f61532005-08-25 22:01:20 -04003183 spin_lock_irqsave(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 __ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003185 spin_unlock_irqrestore(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 atapi_request_sense(ap, dev, cmd);
3188
3189 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3190 scsi_finish_command(cmd);
3191
3192 goto out;
3193 }
3194 }
3195
Jeff Garzikb8f61532005-08-25 22:01:20 -04003196 spin_lock_irqsave(&host_set->lock, flags);
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 /* hack alert! We cannot use the supplied completion
3199 * function from inside the ->eh_strategy_handler() thread.
3200 * libata is the only user of ->eh_strategy_handler() in
3201 * any kernel, so the default scsi_done() assumes it is
3202 * not being called from the SCSI EH.
3203 */
3204 qc->scsidone = scsi_finish_command;
3205
3206 switch (qc->tf.protocol) {
3207
3208 case ATA_PROT_DMA:
3209 case ATA_PROT_ATAPI_DMA:
3210 host_stat = ap->ops->bmdma_status(ap);
3211
3212 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003213 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
3215 /* fall through */
3216
3217 default:
3218 ata_altstatus(ap);
3219 drv_stat = ata_chk_status(ap);
3220
3221 /* ack bmdma irq events */
3222 ap->ops->irq_clear(ap);
3223
3224 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3225 ap->id, qc->tf.command, drv_stat, host_stat);
3226
Albert Lee312f7da2005-09-27 17:38:03 +08003227 ap->hsm_task_state = HSM_ST_IDLE;
3228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 /* complete taskfile transaction */
3230 ata_qc_complete(qc, drv_stat);
3231 break;
3232 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003233
3234 spin_unlock_irqrestore(&host_set->lock, flags);
3235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236out:
3237 DPRINTK("EXIT\n");
3238}
3239
3240/**
3241 * ata_eng_timeout - Handle timeout of queued command
3242 * @ap: Port on which timed-out command is active
3243 *
3244 * Some part of the kernel (currently, only the SCSI layer)
3245 * has noticed that the active command on port @ap has not
3246 * completed after a specified length of time. Handle this
3247 * condition by disabling DMA (if necessary) and completing
3248 * transactions, with error if necessary.
3249 *
3250 * This also handles the case of the "lost interrupt", where
3251 * for some reason (possibly hardware bug, possibly driver bug)
3252 * an interrupt was not delivered to the driver, even though the
3253 * transaction completed successfully.
3254 *
3255 * LOCKING:
3256 * Inherited from SCSI layer (none, can sleep)
3257 */
3258
3259void ata_eng_timeout(struct ata_port *ap)
3260{
3261 struct ata_queued_cmd *qc;
3262
3263 DPRINTK("ENTER\n");
3264
3265 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003266 if (qc)
3267 ata_qc_timeout(qc);
3268 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3270 ap->id);
3271 goto out;
3272 }
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274out:
3275 DPRINTK("EXIT\n");
3276}
3277
3278/**
3279 * ata_qc_new - Request an available ATA command, for queueing
3280 * @ap: Port associated with device @dev
3281 * @dev: Device from whom we request an available command structure
3282 *
3283 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003284 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 */
3286
3287static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3288{
3289 struct ata_queued_cmd *qc = NULL;
3290 unsigned int i;
3291
3292 for (i = 0; i < ATA_MAX_QUEUE; i++)
3293 if (!test_and_set_bit(i, &ap->qactive)) {
3294 qc = ata_qc_from_tag(ap, i);
3295 break;
3296 }
3297
3298 if (qc)
3299 qc->tag = i;
3300
3301 return qc;
3302}
3303
3304/**
3305 * ata_qc_new_init - Request an available ATA command, and initialize it
3306 * @ap: Port associated with device @dev
3307 * @dev: Device from whom we request an available command structure
3308 *
3309 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003310 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 */
3312
3313struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3314 struct ata_device *dev)
3315{
3316 struct ata_queued_cmd *qc;
3317
3318 qc = ata_qc_new(ap);
3319 if (qc) {
3320 qc->sg = NULL;
3321 qc->flags = 0;
3322 qc->scsicmd = NULL;
3323 qc->ap = ap;
3324 qc->dev = dev;
3325 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3326 qc->nsect = 0;
3327 qc->nbytes = qc->curbytes = 0;
3328
3329 ata_tf_init(ap, &qc->tf, dev->devno);
3330
Albert Lee8bf62ec2005-05-12 15:29:42 -04003331 if (dev->flags & ATA_DFLAG_LBA) {
3332 qc->tf.flags |= ATA_TFLAG_LBA;
3333
3334 if (dev->flags & ATA_DFLAG_LBA48)
3335 qc->tf.flags |= ATA_TFLAG_LBA48;
3336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 }
3338
3339 return qc;
3340}
3341
Jeff Garzika939c962005-10-05 17:09:16 -04003342int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
3344 return 0;
3345}
3346
3347static void __ata_qc_complete(struct ata_queued_cmd *qc)
3348{
3349 struct ata_port *ap = qc->ap;
3350 unsigned int tag, do_clear = 0;
3351
3352 qc->flags = 0;
3353 tag = qc->tag;
3354 if (likely(ata_tag_valid(tag))) {
3355 if (tag == ap->active_tag)
3356 ap->active_tag = ATA_TAG_POISON;
3357 qc->tag = ATA_TAG_POISON;
3358 do_clear = 1;
3359 }
3360
3361 if (qc->waiting) {
3362 struct completion *waiting = qc->waiting;
3363 qc->waiting = NULL;
3364 complete(waiting);
3365 }
3366
3367 if (likely(do_clear))
3368 clear_bit(tag, &ap->qactive);
3369}
3370
3371/**
3372 * ata_qc_free - free unused ata_queued_cmd
3373 * @qc: Command to complete
3374 *
3375 * Designed to free unused ata_queued_cmd object
3376 * in case something prevents using it.
3377 *
3378 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003379 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 *
3381 */
3382void ata_qc_free(struct ata_queued_cmd *qc)
3383{
3384 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3385 assert(qc->waiting == NULL); /* nothing should be waiting */
3386
3387 __ata_qc_complete(qc);
3388}
3389
3390/**
3391 * ata_qc_complete - Complete an active ATA command
3392 * @qc: Command to complete
Jeff Garzik0cba6322005-05-30 19:49:12 -04003393 * @drv_stat: ATA Status register contents
3394 *
3395 * Indicate to the mid and upper layers that an ATA
3396 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 *
3398 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003399 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 *
3401 */
3402
3403void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3404{
3405 int rc;
3406
3407 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3408 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3409
3410 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3411 ata_sg_clean(qc);
3412
Albert Lee3f3791d2005-08-16 14:25:38 +08003413 /* atapi: mark qc as inactive to prevent the interrupt handler
3414 * from completing the command twice later, before the error handler
3415 * is called. (when rc != 0 and atapi request sense is needed)
3416 */
3417 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /* call completion callback */
3420 rc = qc->complete_fn(qc, drv_stat);
3421
3422 /* if callback indicates not to complete command (non-zero),
3423 * return immediately
3424 */
3425 if (rc != 0)
3426 return;
3427
3428 __ata_qc_complete(qc);
3429
3430 VPRINTK("EXIT\n");
3431}
3432
3433static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3434{
3435 struct ata_port *ap = qc->ap;
3436
3437 switch (qc->tf.protocol) {
3438 case ATA_PROT_DMA:
3439 case ATA_PROT_ATAPI_DMA:
3440 return 1;
3441
3442 case ATA_PROT_ATAPI:
3443 case ATA_PROT_PIO:
3444 case ATA_PROT_PIO_MULT:
3445 if (ap->flags & ATA_FLAG_PIO_DMA)
3446 return 1;
3447
3448 /* fall through */
3449
3450 default:
3451 return 0;
3452 }
3453
3454 /* never reached */
3455}
3456
3457/**
3458 * ata_qc_issue - issue taskfile to device
3459 * @qc: command to issue to device
3460 *
3461 * Prepare an ATA command to submission to device.
3462 * This includes mapping the data into a DMA-able
3463 * area, filling in the S/G table, and finally
3464 * writing the taskfile to hardware, starting the command.
3465 *
3466 * LOCKING:
3467 * spin_lock_irqsave(host_set lock)
3468 *
3469 * RETURNS:
3470 * Zero on success, negative on error.
3471 */
3472
3473int ata_qc_issue(struct ata_queued_cmd *qc)
3474{
3475 struct ata_port *ap = qc->ap;
3476
3477 if (ata_should_dma_map(qc)) {
3478 if (qc->flags & ATA_QCFLAG_SG) {
3479 if (ata_sg_setup(qc))
3480 goto err_out;
3481 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3482 if (ata_sg_setup_one(qc))
3483 goto err_out;
3484 }
3485 } else {
3486 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3487 }
3488
3489 ap->ops->qc_prep(qc);
3490
3491 qc->ap->active_tag = qc->tag;
3492 qc->flags |= ATA_QCFLAG_ACTIVE;
3493
3494 return ap->ops->qc_issue(qc);
3495
3496err_out:
3497 return -1;
3498}
3499
Edward Falk0baab862005-06-02 18:17:13 -04003500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501/**
3502 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3503 * @qc: command to issue to device
3504 *
3505 * Using various libata functions and hooks, this function
3506 * starts an ATA command. ATA commands are grouped into
3507 * classes called "protocols", and issuing each type of protocol
3508 * is slightly different.
3509 *
Edward Falk0baab862005-06-02 18:17:13 -04003510 * May be used as the qc_issue() entry in ata_port_operations.
3511 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 * LOCKING:
3513 * spin_lock_irqsave(host_set lock)
3514 *
3515 * RETURNS:
3516 * Zero on success, negative on error.
3517 */
3518
3519int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3520{
3521 struct ata_port *ap = qc->ap;
3522
Albert Leee50362e2005-09-27 17:39:50 +08003523 /* Use polling pio if the LLD doesn't handle
3524 * interrupt driven pio and atapi CDB interrupt.
3525 */
3526 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3527 switch (qc->tf.protocol) {
3528 case ATA_PROT_PIO:
3529 case ATA_PROT_ATAPI:
3530 case ATA_PROT_ATAPI_NODATA:
3531 qc->tf.flags |= ATA_TFLAG_POLLING;
3532 break;
3533 case ATA_PROT_ATAPI_DMA:
3534 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3535 BUG();
3536 break;
3537 default:
3538 break;
3539 }
3540 }
3541
Albert Lee312f7da2005-09-27 17:38:03 +08003542 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 ata_dev_select(ap, qc->dev->devno, 1, 0);
3544
Albert Lee312f7da2005-09-27 17:38:03 +08003545 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 switch (qc->tf.protocol) {
3547 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003548 if (qc->tf.flags & ATA_TFLAG_POLLING)
3549 ata_qc_set_polling(qc);
3550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003552 ap->hsm_task_state = HSM_ST_LAST;
3553
3554 if (qc->tf.flags & ATA_TFLAG_POLLING)
3555 queue_work(ata_wq, &ap->pio_task);
3556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 break;
3558
3559 case ATA_PROT_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003560 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3563 ap->ops->bmdma_setup(qc); /* set up bmdma */
3564 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003565 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 break;
3567
Albert Lee312f7da2005-09-27 17:38:03 +08003568 case ATA_PROT_PIO:
3569 if (qc->tf.flags & ATA_TFLAG_POLLING)
3570 ata_qc_set_polling(qc);
3571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003573
Albert Lee54f00382005-09-30 19:14:19 +08003574 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3575 /* PIO data out protocol */
3576 ap->hsm_task_state = HSM_ST_FIRST;
3577 queue_work(ata_wq, &ap->dataout_task);
3578
3579 /* always send first data block using
3580 * the ata_dataout_task() codepath.
3581 */
Albert Lee312f7da2005-09-27 17:38:03 +08003582 } else {
Albert Lee54f00382005-09-30 19:14:19 +08003583 /* PIO data in protocol */
3584 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08003585
Albert Lee54f00382005-09-30 19:14:19 +08003586 if (qc->tf.flags & ATA_TFLAG_POLLING)
3587 queue_work(ata_wq, &ap->pio_task);
Albert Lee312f7da2005-09-27 17:38:03 +08003588
Albert Lee54f00382005-09-30 19:14:19 +08003589 /* if polling, ata_pio_task() handles the rest.
3590 * otherwise, interrupt handler takes over from here.
3591 */
Albert Lee312f7da2005-09-27 17:38:03 +08003592 }
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 break;
3595
3596 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003598 if (qc->tf.flags & ATA_TFLAG_POLLING)
3599 ata_qc_set_polling(qc);
3600
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003602 ap->hsm_task_state = HSM_ST_FIRST;
3603
3604 /* send cdb by polling if no cdb interrupt */
3605 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3606 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Leef9997be2005-09-30 19:09:31 +08003607 queue_work(ata_wq, &ap->dataout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 break;
3609
3610 case ATA_PROT_ATAPI_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003611 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3614 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003615 ap->hsm_task_state = HSM_ST_FIRST;
3616
3617 /* send cdb by polling if no cdb interrupt */
3618 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Leef9997be2005-09-30 19:09:31 +08003619 queue_work(ata_wq, &ap->dataout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 break;
3621
3622 default:
3623 WARN_ON(1);
3624 return -1;
3625 }
3626
3627 return 0;
3628}
3629
3630/**
Edward Falk0baab862005-06-02 18:17:13 -04003631 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 * @qc: Info associated with this ATA transaction.
3633 *
3634 * LOCKING:
3635 * spin_lock_irqsave(host_set lock)
3636 */
3637
3638static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3639{
3640 struct ata_port *ap = qc->ap;
3641 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3642 u8 dmactl;
3643 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3644
3645 /* load PRD table addr. */
3646 mb(); /* make sure PRD table writes are visible to controller */
3647 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3648
3649 /* specify data direction, triple-check start bit is clear */
3650 dmactl = readb(mmio + ATA_DMA_CMD);
3651 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3652 if (!rw)
3653 dmactl |= ATA_DMA_WR;
3654 writeb(dmactl, mmio + ATA_DMA_CMD);
3655
3656 /* issue r/w command */
3657 ap->ops->exec_command(ap, &qc->tf);
3658}
3659
3660/**
Alan Coxb73fc892005-08-26 16:03:19 +01003661 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 * @qc: Info associated with this ATA transaction.
3663 *
3664 * LOCKING:
3665 * spin_lock_irqsave(host_set lock)
3666 */
3667
3668static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3669{
3670 struct ata_port *ap = qc->ap;
3671 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3672 u8 dmactl;
3673
3674 /* start host DMA transaction */
3675 dmactl = readb(mmio + ATA_DMA_CMD);
3676 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3677
3678 /* Strictly, one may wish to issue a readb() here, to
3679 * flush the mmio write. However, control also passes
3680 * to the hardware at this point, and it will interrupt
3681 * us when we are to resume control. So, in effect,
3682 * we don't care when the mmio write flushes.
3683 * Further, a read of the DMA status register _immediately_
3684 * following the write may not be what certain flaky hardware
3685 * is expected, so I think it is best to not add a readb()
3686 * without first all the MMIO ATA cards/mobos.
3687 * Or maybe I'm just being paranoid.
3688 */
3689}
3690
3691/**
3692 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3693 * @qc: Info associated with this ATA transaction.
3694 *
3695 * LOCKING:
3696 * spin_lock_irqsave(host_set lock)
3697 */
3698
3699static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3700{
3701 struct ata_port *ap = qc->ap;
3702 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3703 u8 dmactl;
3704
3705 /* load PRD table addr. */
3706 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3707
3708 /* specify data direction, triple-check start bit is clear */
3709 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3710 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3711 if (!rw)
3712 dmactl |= ATA_DMA_WR;
3713 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3714
3715 /* issue r/w command */
3716 ap->ops->exec_command(ap, &qc->tf);
3717}
3718
3719/**
3720 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3721 * @qc: Info associated with this ATA transaction.
3722 *
3723 * LOCKING:
3724 * spin_lock_irqsave(host_set lock)
3725 */
3726
3727static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3728{
3729 struct ata_port *ap = qc->ap;
3730 u8 dmactl;
3731
3732 /* start host DMA transaction */
3733 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3734 outb(dmactl | ATA_DMA_START,
3735 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3736}
3737
Edward Falk0baab862005-06-02 18:17:13 -04003738
3739/**
3740 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3741 * @qc: Info associated with this ATA transaction.
3742 *
3743 * Writes the ATA_DMA_START flag to the DMA command register.
3744 *
3745 * May be used as the bmdma_start() entry in ata_port_operations.
3746 *
3747 * LOCKING:
3748 * spin_lock_irqsave(host_set lock)
3749 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750void ata_bmdma_start(struct ata_queued_cmd *qc)
3751{
3752 if (qc->ap->flags & ATA_FLAG_MMIO)
3753 ata_bmdma_start_mmio(qc);
3754 else
3755 ata_bmdma_start_pio(qc);
3756}
3757
Edward Falk0baab862005-06-02 18:17:13 -04003758
3759/**
3760 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3761 * @qc: Info associated with this ATA transaction.
3762 *
3763 * Writes address of PRD table to device's PRD Table Address
3764 * register, sets the DMA control register, and calls
3765 * ops->exec_command() to start the transfer.
3766 *
3767 * May be used as the bmdma_setup() entry in ata_port_operations.
3768 *
3769 * LOCKING:
3770 * spin_lock_irqsave(host_set lock)
3771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772void ata_bmdma_setup(struct ata_queued_cmd *qc)
3773{
3774 if (qc->ap->flags & ATA_FLAG_MMIO)
3775 ata_bmdma_setup_mmio(qc);
3776 else
3777 ata_bmdma_setup_pio(qc);
3778}
3779
Edward Falk0baab862005-06-02 18:17:13 -04003780
3781/**
3782 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003783 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003784 *
3785 * Clear interrupt and error flags in DMA status register.
3786 *
3787 * May be used as the irq_clear() entry in ata_port_operations.
3788 *
3789 * LOCKING:
3790 * spin_lock_irqsave(host_set lock)
3791 */
3792
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793void ata_bmdma_irq_clear(struct ata_port *ap)
3794{
3795 if (ap->flags & ATA_FLAG_MMIO) {
3796 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3797 writeb(readb(mmio), mmio);
3798 } else {
3799 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3800 outb(inb(addr), addr);
3801 }
3802
3803}
3804
Edward Falk0baab862005-06-02 18:17:13 -04003805
3806/**
3807 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003808 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003809 *
3810 * Read and return BMDMA status register.
3811 *
3812 * May be used as the bmdma_status() entry in ata_port_operations.
3813 *
3814 * LOCKING:
3815 * spin_lock_irqsave(host_set lock)
3816 */
3817
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818u8 ata_bmdma_status(struct ata_port *ap)
3819{
3820 u8 host_stat;
3821 if (ap->flags & ATA_FLAG_MMIO) {
3822 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3823 host_stat = readb(mmio + ATA_DMA_STATUS);
3824 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003825 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 return host_stat;
3827}
3828
Edward Falk0baab862005-06-02 18:17:13 -04003829
3830/**
3831 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003832 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003833 *
3834 * Clears the ATA_DMA_START flag in the dma control register
3835 *
3836 * May be used as the bmdma_stop() entry in ata_port_operations.
3837 *
3838 * LOCKING:
3839 * spin_lock_irqsave(host_set lock)
3840 */
3841
Alan Coxb73fc892005-08-26 16:03:19 +01003842void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843{
Alan Coxb73fc892005-08-26 16:03:19 +01003844 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 if (ap->flags & ATA_FLAG_MMIO) {
3846 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3847
3848 /* clear start/stop bit */
3849 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3850 mmio + ATA_DMA_CMD);
3851 } else {
3852 /* clear start/stop bit */
3853 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3854 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3855 }
3856
3857 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3858 ata_altstatus(ap); /* dummy read */
3859}
3860
3861/**
3862 * ata_host_intr - Handle host interrupt for given (port, task)
3863 * @ap: Port on which interrupt arrived (possibly...)
3864 * @qc: Taskfile currently active in engine
3865 *
3866 * Handle host interrupt for given queued command. Currently,
3867 * only DMA interrupts are handled. All other commands are
3868 * handled via polling with interrupts disabled (nIEN bit).
3869 *
3870 * LOCKING:
3871 * spin_lock_irqsave(host_set lock)
3872 *
3873 * RETURNS:
3874 * One if interrupt was handled, zero if not (shared irq).
3875 */
3876
3877inline unsigned int ata_host_intr (struct ata_port *ap,
3878 struct ata_queued_cmd *qc)
3879{
Albert Lee312f7da2005-09-27 17:38:03 +08003880 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Albert Lee312f7da2005-09-27 17:38:03 +08003882 VPRINTK("ata%u: protocol %d task_state %d\n",
3883 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Albert Lee312f7da2005-09-27 17:38:03 +08003885 /* Check whether we are expecting interrupt in this state */
3886 switch (ap->hsm_task_state) {
3887 case HSM_ST_FIRST:
3888 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
3889 * The flag was turned on only for atapi devices.
3890 * No need to check is_atapi_taskfile(&qc->tf) again.
3891 */
3892 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08003894 break;
3895 case HSM_ST_LAST:
3896 if (qc->tf.protocol == ATA_PROT_DMA ||
3897 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
3898 /* check status of DMA engine */
3899 host_stat = ap->ops->bmdma_status(ap);
3900 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Albert Lee312f7da2005-09-27 17:38:03 +08003902 /* if it's not our irq... */
3903 if (!(host_stat & ATA_DMA_INTR))
3904 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Albert Lee312f7da2005-09-27 17:38:03 +08003906 /* before we do anything else, clear DMA-Start bit */
3907 ap->ops->bmdma_stop(qc);
3908 }
3909 break;
3910 case HSM_ST:
3911 break;
3912 default:
3913 goto idle_irq;
3914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Albert Lee312f7da2005-09-27 17:38:03 +08003916 /* check altstatus */
3917 status = ata_altstatus(ap);
3918 if (status & ATA_BUSY)
3919 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Albert Lee312f7da2005-09-27 17:38:03 +08003921 /* check main status, clearing INTRQ */
3922 status = ata_chk_status(ap);
3923 if (unlikely(status & ATA_BUSY))
3924 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Albert Lee312f7da2005-09-27 17:38:03 +08003926 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
3927 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
3928
3929 /* ack bmdma irq events */
3930 ap->ops->irq_clear(ap);
3931
3932 /* check error */
3933 if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
3934 ap->hsm_task_state = HSM_ST_ERR;
3935
3936fsm_start:
3937 switch (ap->hsm_task_state) {
3938 case HSM_ST_FIRST:
3939 /* Some pre-ATAPI-4 devices assert INTRQ
3940 * at this state when ready to receive CDB.
3941 */
3942
3943 /* check device status */
3944 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
3945 /* Wrong status. Let EH handle this */
3946 ap->hsm_task_state = HSM_ST_ERR;
3947 goto fsm_start;
3948 }
3949
3950 atapi_send_cdb(ap, qc);
3951
3952 break;
3953
3954 case HSM_ST:
3955 /* complete command or read/write the data register */
3956 if (qc->tf.protocol == ATA_PROT_ATAPI) {
3957 /* ATAPI PIO protocol */
3958 if ((status & ATA_DRQ) == 0) {
3959 /* no more data to transfer */
3960 ap->hsm_task_state = HSM_ST_LAST;
3961 goto fsm_start;
3962 }
3963
3964 atapi_pio_bytes(qc);
3965
3966 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
3967 /* bad ireason reported by device */
3968 goto fsm_start;
3969
3970 } else {
3971 /* ATA PIO protocol */
3972 if (unlikely((status & ATA_DRQ) == 0)) {
3973 /* handle BSY=0, DRQ=0 as error */
3974 ap->hsm_task_state = HSM_ST_ERR;
3975 goto fsm_start;
3976 }
3977
3978 ata_pio_sector(qc);
3979
3980 if (ap->hsm_task_state == HSM_ST_LAST &&
3981 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
3982 /* all data read */
3983 ata_altstatus(ap);
3984 status = ata_chk_status(ap);
3985 goto fsm_start;
3986 }
3987 }
3988
3989 ata_altstatus(ap); /* flush */
3990 break;
3991
3992 case HSM_ST_LAST:
3993 if (unlikely(status & ATA_DRQ)) {
3994 /* handle DRQ=1 as error */
3995 ap->hsm_task_state = HSM_ST_ERR;
3996 goto fsm_start;
3997 }
3998
3999 /* no more data to transfer */
4000 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4001 ap->id, status);
4002
4003 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
4005 /* complete taskfile transaction */
4006 ata_qc_complete(qc, status);
4007 break;
4008
Albert Lee312f7da2005-09-27 17:38:03 +08004009 case HSM_ST_ERR:
4010 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4011 ap->id, status, host_stat);
4012
4013 ap->hsm_task_state = HSM_ST_IDLE;
4014 ata_qc_complete(qc, status | ATA_ERR);
4015 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 default:
4017 goto idle_irq;
4018 }
4019
4020 return 1; /* irq handled */
4021
4022idle_irq:
4023 ap->stats.idle_irq++;
4024
4025#ifdef ATA_IRQ_TRAP
4026 if ((ap->stats.idle_irq % 1000) == 0) {
4027 handled = 1;
4028 ata_irq_ack(ap, 0); /* debug trap */
4029 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4030 }
4031#endif
4032 return 0; /* irq not handled */
4033}
4034
4035/**
4036 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004037 * @irq: irq line (unused)
4038 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 * @regs: unused
4040 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004041 * Default interrupt handler for PCI IDE devices. Calls
4042 * ata_host_intr() for each port that is not disabled.
4043 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004045 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 *
4047 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004048 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 *
4050 */
4051
4052irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4053{
4054 struct ata_host_set *host_set = dev_instance;
4055 unsigned int i;
4056 unsigned int handled = 0;
4057 unsigned long flags;
4058
4059 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4060 spin_lock_irqsave(&host_set->lock, flags);
4061
4062 for (i = 0; i < host_set->n_ports; i++) {
4063 struct ata_port *ap;
4064
4065 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004066 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08004067 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 struct ata_queued_cmd *qc;
4069
4070 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004071 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004072 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 handled |= ata_host_intr(ap, qc);
4074 }
4075 }
4076
4077 spin_unlock_irqrestore(&host_set->lock, flags);
4078
4079 return IRQ_RETVAL(handled);
4080}
4081
4082/**
Edward Falk0baab862005-06-02 18:17:13 -04004083 * ata_port_start - Set port up for dma.
4084 * @ap: Port to initialize
4085 *
4086 * Called just after data structures for each port are
4087 * initialized. Allocates space for PRD table.
4088 *
4089 * May be used as the port_start() entry in ata_port_operations.
4090 *
4091 * LOCKING:
4092 */
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094int ata_port_start (struct ata_port *ap)
4095{
4096 struct device *dev = ap->host_set->dev;
4097
4098 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4099 if (!ap->prd)
4100 return -ENOMEM;
4101
4102 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4103
4104 return 0;
4105}
4106
Edward Falk0baab862005-06-02 18:17:13 -04004107
4108/**
4109 * ata_port_stop - Undo ata_port_start()
4110 * @ap: Port to shut down
4111 *
4112 * Frees the PRD table.
4113 *
4114 * May be used as the port_stop() entry in ata_port_operations.
4115 *
4116 * LOCKING:
4117 */
4118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119void ata_port_stop (struct ata_port *ap)
4120{
4121 struct device *dev = ap->host_set->dev;
4122
4123 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4124}
4125
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004126void ata_host_stop (struct ata_host_set *host_set)
4127{
4128 if (host_set->mmio_base)
4129 iounmap(host_set->mmio_base);
4130}
4131
4132
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133/**
4134 * ata_host_remove - Unregister SCSI host structure with upper layers
4135 * @ap: Port to unregister
4136 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4137 *
4138 * LOCKING:
4139 */
4140
4141static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4142{
4143 struct Scsi_Host *sh = ap->host;
4144
4145 DPRINTK("ENTER\n");
4146
4147 if (do_unregister)
4148 scsi_remove_host(sh);
4149
4150 ap->ops->port_stop(ap);
4151}
4152
4153/**
4154 * ata_host_init - Initialize an ata_port structure
4155 * @ap: Structure to initialize
4156 * @host: associated SCSI mid-layer structure
4157 * @host_set: Collection of hosts to which @ap belongs
4158 * @ent: Probe information provided by low-level driver
4159 * @port_no: Port number associated with this ata_port
4160 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004161 * Initialize a new ata_port structure, and its associated
4162 * scsi_host.
4163 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004165 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 *
4167 */
4168
4169static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4170 struct ata_host_set *host_set,
4171 struct ata_probe_ent *ent, unsigned int port_no)
4172{
4173 unsigned int i;
4174
4175 host->max_id = 16;
4176 host->max_lun = 1;
4177 host->max_channel = 1;
4178 host->unique_id = ata_unique_id++;
4179 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 scsi_assign_lock(host, &host_set->lock);
4182
4183 ap->flags = ATA_FLAG_PORT_DISABLED;
4184 ap->id = host->unique_id;
4185 ap->host = host;
4186 ap->ctl = ATA_DEVCTL_OBS;
4187 ap->host_set = host_set;
4188 ap->port_no = port_no;
4189 ap->hard_port_no =
4190 ent->legacy_mode ? ent->hard_port_no : port_no;
4191 ap->pio_mask = ent->pio_mask;
4192 ap->mwdma_mask = ent->mwdma_mask;
4193 ap->udma_mask = ent->udma_mask;
4194 ap->flags |= ent->host_flags;
4195 ap->ops = ent->port_ops;
4196 ap->cbl = ATA_CBL_NONE;
4197 ap->active_tag = ATA_TAG_POISON;
4198 ap->last_ctl = 0xFF;
4199
Albert Leef9997be2005-09-30 19:09:31 +08004200 INIT_WORK(&ap->dataout_task, ata_dataout_task, ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4202
4203 for (i = 0; i < ATA_MAX_DEVICES; i++)
4204 ap->device[i].devno = i;
4205
4206#ifdef ATA_IRQ_TRAP
4207 ap->stats.unhandled_irq = 1;
4208 ap->stats.idle_irq = 1;
4209#endif
4210
4211 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4212}
4213
4214/**
4215 * ata_host_add - Attach low-level ATA driver to system
4216 * @ent: Information provided by low-level driver
4217 * @host_set: Collections of ports to which we add
4218 * @port_no: Port number associated with this host
4219 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004220 * Attach low-level ATA driver to system.
4221 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004223 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 *
4225 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004226 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 *
4228 */
4229
4230static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4231 struct ata_host_set *host_set,
4232 unsigned int port_no)
4233{
4234 struct Scsi_Host *host;
4235 struct ata_port *ap;
4236 int rc;
4237
4238 DPRINTK("ENTER\n");
4239 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4240 if (!host)
4241 return NULL;
4242
4243 ap = (struct ata_port *) &host->hostdata[0];
4244
4245 ata_host_init(ap, host, host_set, ent, port_no);
4246
4247 rc = ap->ops->port_start(ap);
4248 if (rc)
4249 goto err_out;
4250
4251 return ap;
4252
4253err_out:
4254 scsi_host_put(host);
4255 return NULL;
4256}
4257
4258/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004259 * ata_device_add - Register hardware device with ATA and SCSI layers
4260 * @ent: Probe information describing hardware device to be registered
4261 *
4262 * This function processes the information provided in the probe
4263 * information struct @ent, allocates the necessary ATA and SCSI
4264 * host information structures, initializes them, and registers
4265 * everything with requisite kernel subsystems.
4266 *
4267 * This function requests irqs, probes the ATA bus, and probes
4268 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 *
4270 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004271 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 *
4273 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004274 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 *
4276 */
4277
4278int ata_device_add(struct ata_probe_ent *ent)
4279{
4280 unsigned int count = 0, i;
4281 struct device *dev = ent->dev;
4282 struct ata_host_set *host_set;
4283
4284 DPRINTK("ENTER\n");
4285 /* alloc a container for our list of ATA ports (buses) */
4286 host_set = kmalloc(sizeof(struct ata_host_set) +
4287 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4288 if (!host_set)
4289 return 0;
4290 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4291 spin_lock_init(&host_set->lock);
4292
4293 host_set->dev = dev;
4294 host_set->n_ports = ent->n_ports;
4295 host_set->irq = ent->irq;
4296 host_set->mmio_base = ent->mmio_base;
4297 host_set->private_data = ent->private_data;
4298 host_set->ops = ent->port_ops;
4299
4300 /* register each port bound to this device */
4301 for (i = 0; i < ent->n_ports; i++) {
4302 struct ata_port *ap;
4303 unsigned long xfer_mode_mask;
4304
4305 ap = ata_host_add(ent, host_set, i);
4306 if (!ap)
4307 goto err_out;
4308
4309 host_set->ports[i] = ap;
4310 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4311 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4312 (ap->pio_mask << ATA_SHIFT_PIO);
4313
4314 /* print per-port info to dmesg */
4315 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4316 "bmdma 0x%lX irq %lu\n",
4317 ap->id,
4318 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4319 ata_mode_string(xfer_mode_mask),
4320 ap->ioaddr.cmd_addr,
4321 ap->ioaddr.ctl_addr,
4322 ap->ioaddr.bmdma_addr,
4323 ent->irq);
4324
4325 ata_chk_status(ap);
4326 host_set->ops->irq_clear(ap);
4327 count++;
4328 }
4329
4330 if (!count) {
4331 kfree(host_set);
4332 return 0;
4333 }
4334
4335 /* obtain irq, that is shared between channels */
4336 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4337 DRV_NAME, host_set))
4338 goto err_out;
4339
4340 /* perform each probe synchronously */
4341 DPRINTK("probe begin\n");
4342 for (i = 0; i < count; i++) {
4343 struct ata_port *ap;
4344 int rc;
4345
4346 ap = host_set->ports[i];
4347
4348 DPRINTK("ata%u: probe begin\n", ap->id);
4349 rc = ata_bus_probe(ap);
4350 DPRINTK("ata%u: probe end\n", ap->id);
4351
4352 if (rc) {
4353 /* FIXME: do something useful here?
4354 * Current libata behavior will
4355 * tear down everything when
4356 * the module is removed
4357 * or the h/w is unplugged.
4358 */
4359 }
4360
4361 rc = scsi_add_host(ap->host, dev);
4362 if (rc) {
4363 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4364 ap->id);
4365 /* FIXME: do something useful here */
4366 /* FIXME: handle unconditional calls to
4367 * scsi_scan_host and ata_host_remove, below,
4368 * at the very least
4369 */
4370 }
4371 }
4372
4373 /* probes are done, now scan each port's disk(s) */
4374 DPRINTK("probe begin\n");
4375 for (i = 0; i < count; i++) {
4376 struct ata_port *ap = host_set->ports[i];
4377
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004378 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 }
4380
4381 dev_set_drvdata(dev, host_set);
4382
4383 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4384 return ent->n_ports; /* success */
4385
4386err_out:
4387 for (i = 0; i < count; i++) {
4388 ata_host_remove(host_set->ports[i], 1);
4389 scsi_host_put(host_set->ports[i]->host);
4390 }
4391 kfree(host_set);
4392 VPRINTK("EXIT, returning 0\n");
4393 return 0;
4394}
4395
4396/**
Alan Cox17b14452005-09-15 15:44:00 +01004397 * ata_host_set_remove - PCI layer callback for device removal
4398 * @host_set: ATA host set that was removed
4399 *
4400 * Unregister all objects associated with this host set. Free those
4401 * objects.
4402 *
4403 * LOCKING:
4404 * Inherited from calling layer (may sleep).
4405 */
4406
4407
4408void ata_host_set_remove(struct ata_host_set *host_set)
4409{
4410 struct ata_port *ap;
4411 unsigned int i;
4412
4413 for (i = 0; i < host_set->n_ports; i++) {
4414 ap = host_set->ports[i];
4415 scsi_remove_host(ap->host);
4416 }
4417
4418 free_irq(host_set->irq, host_set);
4419
4420 for (i = 0; i < host_set->n_ports; i++) {
4421 ap = host_set->ports[i];
4422
4423 ata_scsi_release(ap->host);
4424
4425 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4426 struct ata_ioports *ioaddr = &ap->ioaddr;
4427
4428 if (ioaddr->cmd_addr == 0x1f0)
4429 release_region(0x1f0, 8);
4430 else if (ioaddr->cmd_addr == 0x170)
4431 release_region(0x170, 8);
4432 }
4433
4434 scsi_host_put(ap->host);
4435 }
4436
4437 if (host_set->ops->host_stop)
4438 host_set->ops->host_stop(host_set);
4439
4440 kfree(host_set);
4441}
4442
4443/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 * ata_scsi_release - SCSI layer callback hook for host unload
4445 * @host: libata host to be unloaded
4446 *
4447 * Performs all duties necessary to shut down a libata port...
4448 * Kill port kthread, disable port, and release resources.
4449 *
4450 * LOCKING:
4451 * Inherited from SCSI layer.
4452 *
4453 * RETURNS:
4454 * One.
4455 */
4456
4457int ata_scsi_release(struct Scsi_Host *host)
4458{
4459 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4460
4461 DPRINTK("ENTER\n");
4462
4463 ap->ops->port_disable(ap);
4464 ata_host_remove(ap, 0);
4465
4466 DPRINTK("EXIT\n");
4467 return 1;
4468}
4469
4470/**
4471 * ata_std_ports - initialize ioaddr with standard port offsets.
4472 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004473 *
4474 * Utility function which initializes data_addr, error_addr,
4475 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4476 * device_addr, status_addr, and command_addr to standard offsets
4477 * relative to cmd_addr.
4478 *
4479 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 */
Edward Falk0baab862005-06-02 18:17:13 -04004481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482void ata_std_ports(struct ata_ioports *ioaddr)
4483{
4484 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4485 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4486 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4487 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4488 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4489 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4490 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4491 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4492 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4493 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4494}
4495
4496static struct ata_probe_ent *
4497ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4498{
4499 struct ata_probe_ent *probe_ent;
4500
4501 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4502 if (!probe_ent) {
4503 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4504 kobject_name(&(dev->kobj)));
4505 return NULL;
4506 }
4507
4508 memset(probe_ent, 0, sizeof(*probe_ent));
4509
4510 INIT_LIST_HEAD(&probe_ent->node);
4511 probe_ent->dev = dev;
4512
4513 probe_ent->sht = port->sht;
4514 probe_ent->host_flags = port->host_flags;
4515 probe_ent->pio_mask = port->pio_mask;
4516 probe_ent->mwdma_mask = port->mwdma_mask;
4517 probe_ent->udma_mask = port->udma_mask;
4518 probe_ent->port_ops = port->port_ops;
4519
4520 return probe_ent;
4521}
4522
Edward Falk0baab862005-06-02 18:17:13 -04004523
4524
Jeff Garzik374b1872005-08-30 05:42:52 -04004525#ifdef CONFIG_PCI
4526
4527void ata_pci_host_stop (struct ata_host_set *host_set)
4528{
4529 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4530
4531 pci_iounmap(pdev, host_set->mmio_base);
4532}
4533
Edward Falk0baab862005-06-02 18:17:13 -04004534/**
4535 * ata_pci_init_native_mode - Initialize native-mode driver
4536 * @pdev: pci device to be initialized
4537 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004538 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004539 *
4540 * Utility function which allocates and initializes an
4541 * ata_probe_ent structure for a standard dual-port
4542 * PIO-based IDE controller. The returned ata_probe_ent
4543 * structure can be passed to ata_device_add(). The returned
4544 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004545 *
4546 * The caller need only pass the address of the primary port, the
4547 * secondary will be deduced automatically. If the device has non
4548 * standard secondary port mappings this function can be called twice,
4549 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004550 */
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004553ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
4555 struct ata_probe_ent *probe_ent =
4556 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004557 int p = 0;
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 if (!probe_ent)
4560 return NULL;
4561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 probe_ent->irq = pdev->irq;
4563 probe_ent->irq_flags = SA_SHIRQ;
4564
Alan Cox47a86592005-10-04 08:09:19 -04004565 if (ports & ATA_PORT_PRIMARY) {
4566 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4567 probe_ent->port[p].altstatus_addr =
4568 probe_ent->port[p].ctl_addr =
4569 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4570 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4571 ata_std_ports(&probe_ent->port[p]);
4572 p++;
4573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Alan Cox47a86592005-10-04 08:09:19 -04004575 if (ports & ATA_PORT_SECONDARY) {
4576 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4577 probe_ent->port[p].altstatus_addr =
4578 probe_ent->port[p].ctl_addr =
4579 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4580 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4581 ata_std_ports(&probe_ent->port[p]);
4582 p++;
4583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
Alan Cox47a86592005-10-04 08:09:19 -04004585 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 return probe_ent;
4587}
4588
Alan Cox47a86592005-10-04 08:09:19 -04004589static 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 -07004590{
Alan Cox47a86592005-10-04 08:09:19 -04004591 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
4593 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4594 if (!probe_ent)
4595 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Alan Cox47a86592005-10-04 08:09:19 -04004597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004599 probe_ent->n_ports = 1;
4600 probe_ent->hard_port_no = port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Alan Cox47a86592005-10-04 08:09:19 -04004602 switch(port_num)
4603 {
4604 case 0:
4605 probe_ent->irq = 14;
4606 probe_ent->port[0].cmd_addr = 0x1f0;
4607 probe_ent->port[0].altstatus_addr =
4608 probe_ent->port[0].ctl_addr = 0x3f6;
4609 break;
4610 case 1:
4611 probe_ent->irq = 15;
4612 probe_ent->port[0].cmd_addr = 0x170;
4613 probe_ent->port[0].altstatus_addr =
4614 probe_ent->port[0].ctl_addr = 0x376;
4615 break;
4616 }
4617 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 return probe_ent;
4620}
4621
4622/**
4623 * ata_pci_init_one - Initialize/register PCI IDE host controller
4624 * @pdev: Controller to be initialized
4625 * @port_info: Information from low-level host driver
4626 * @n_ports: Number of ports attached to host controller
4627 *
Edward Falk0baab862005-06-02 18:17:13 -04004628 * This is a helper function which can be called from a driver's
4629 * xxx_init_one() probe function if the hardware uses traditional
4630 * IDE taskfile registers.
4631 *
4632 * This function calls pci_enable_device(), reserves its register
4633 * regions, sets the dma mask, enables bus master mode, and calls
4634 * ata_device_add()
4635 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 * LOCKING:
4637 * Inherited from PCI layer (may sleep).
4638 *
4639 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004640 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 *
4642 */
4643
4644int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4645 unsigned int n_ports)
4646{
Alan Cox47a86592005-10-04 08:09:19 -04004647 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 struct ata_port_info *port[2];
4649 u8 tmp8, mask;
4650 unsigned int legacy_mode = 0;
4651 int disable_dev_on_err = 1;
4652 int rc;
4653
4654 DPRINTK("ENTER\n");
4655
4656 port[0] = port_info[0];
4657 if (n_ports > 1)
4658 port[1] = port_info[1];
4659 else
4660 port[1] = port[0];
4661
4662 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4663 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004664 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4666 mask = (1 << 2) | (1 << 0);
4667 if ((tmp8 & mask) != mask)
4668 legacy_mode = (1 << 3);
4669 }
4670
4671 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004672 if ((!legacy_mode) && (n_ports > 2)) {
4673 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4674 n_ports = 2;
4675 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 }
4677
Alan Cox47a86592005-10-04 08:09:19 -04004678 /* FIXME: Really for ATA it isn't safe because the device may be
4679 multi-purpose and we want to leave it alone if it was already
4680 enabled. Secondly for shared use as Arjan says we want refcounting
4681
4682 Checking dev->is_enabled is insufficient as this is not set at
4683 boot for the primary video which is BIOS enabled
4684 */
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 rc = pci_enable_device(pdev);
4687 if (rc)
4688 return rc;
4689
4690 rc = pci_request_regions(pdev, DRV_NAME);
4691 if (rc) {
4692 disable_dev_on_err = 0;
4693 goto err_out;
4694 }
4695
Alan Cox47a86592005-10-04 08:09:19 -04004696 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 if (legacy_mode) {
4698 if (!request_region(0x1f0, 8, "libata")) {
4699 struct resource *conflict, res;
4700 res.start = 0x1f0;
4701 res.end = 0x1f0 + 8 - 1;
4702 conflict = ____request_resource(&ioport_resource, &res);
4703 if (!strcmp(conflict->name, "libata"))
4704 legacy_mode |= (1 << 0);
4705 else {
4706 disable_dev_on_err = 0;
4707 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4708 }
4709 } else
4710 legacy_mode |= (1 << 0);
4711
4712 if (!request_region(0x170, 8, "libata")) {
4713 struct resource *conflict, res;
4714 res.start = 0x170;
4715 res.end = 0x170 + 8 - 1;
4716 conflict = ____request_resource(&ioport_resource, &res);
4717 if (!strcmp(conflict->name, "libata"))
4718 legacy_mode |= (1 << 1);
4719 else {
4720 disable_dev_on_err = 0;
4721 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4722 }
4723 } else
4724 legacy_mode |= (1 << 1);
4725 }
4726
4727 /* we have legacy mode, but all ports are unavailable */
4728 if (legacy_mode == (1 << 3)) {
4729 rc = -EBUSY;
4730 goto err_out_regions;
4731 }
4732
4733 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4734 if (rc)
4735 goto err_out_regions;
4736 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4737 if (rc)
4738 goto err_out_regions;
4739
4740 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004741 if (legacy_mode & (1 << 0))
4742 probe_ent = ata_pci_init_legacy_port(pdev, port, 0);
4743 if (legacy_mode & (1 << 1))
4744 probe_ent2 = ata_pci_init_legacy_port(pdev, port, 1);
4745 } else {
4746 if (n_ports == 2)
4747 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4748 else
4749 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4750 }
4751 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 rc = -ENOMEM;
4753 goto err_out_regions;
4754 }
4755
4756 pci_set_master(pdev);
4757
4758 /* FIXME: check ata_device_add return */
4759 if (legacy_mode) {
4760 if (legacy_mode & (1 << 0))
4761 ata_device_add(probe_ent);
4762 if (legacy_mode & (1 << 1))
4763 ata_device_add(probe_ent2);
4764 } else
4765 ata_device_add(probe_ent);
4766
4767 kfree(probe_ent);
4768 kfree(probe_ent2);
4769
4770 return 0;
4771
4772err_out_regions:
4773 if (legacy_mode & (1 << 0))
4774 release_region(0x1f0, 8);
4775 if (legacy_mode & (1 << 1))
4776 release_region(0x170, 8);
4777 pci_release_regions(pdev);
4778err_out:
4779 if (disable_dev_on_err)
4780 pci_disable_device(pdev);
4781 return rc;
4782}
4783
4784/**
4785 * ata_pci_remove_one - PCI layer callback for device removal
4786 * @pdev: PCI device that was removed
4787 *
4788 * PCI layer indicates to libata via this hook that
4789 * hot-unplug or module unload event has occured.
4790 * Handle this by unregistering all objects associated
4791 * with this PCI device. Free those objects. Then finally
4792 * release PCI resources and disable device.
4793 *
4794 * LOCKING:
4795 * Inherited from PCI layer (may sleep).
4796 */
4797
4798void ata_pci_remove_one (struct pci_dev *pdev)
4799{
4800 struct device *dev = pci_dev_to_dev(pdev);
4801 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Alan Cox17b14452005-09-15 15:44:00 +01004803 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 pci_release_regions(pdev);
4805 pci_disable_device(pdev);
4806 dev_set_drvdata(dev, NULL);
4807}
4808
4809/* move to PCI subsystem */
4810int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4811{
4812 unsigned long tmp = 0;
4813
4814 switch (bits->width) {
4815 case 1: {
4816 u8 tmp8 = 0;
4817 pci_read_config_byte(pdev, bits->reg, &tmp8);
4818 tmp = tmp8;
4819 break;
4820 }
4821 case 2: {
4822 u16 tmp16 = 0;
4823 pci_read_config_word(pdev, bits->reg, &tmp16);
4824 tmp = tmp16;
4825 break;
4826 }
4827 case 4: {
4828 u32 tmp32 = 0;
4829 pci_read_config_dword(pdev, bits->reg, &tmp32);
4830 tmp = tmp32;
4831 break;
4832 }
4833
4834 default:
4835 return -EINVAL;
4836 }
4837
4838 tmp &= bits->mask;
4839
4840 return (tmp == bits->val) ? 1 : 0;
4841}
4842#endif /* CONFIG_PCI */
4843
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845static int __init ata_init(void)
4846{
4847 ata_wq = create_workqueue("ata");
4848 if (!ata_wq)
4849 return -ENOMEM;
4850
4851 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4852 return 0;
4853}
4854
4855static void __exit ata_exit(void)
4856{
4857 destroy_workqueue(ata_wq);
4858}
4859
4860module_init(ata_init);
4861module_exit(ata_exit);
4862
Jeff Garzik67846b32005-10-05 02:58:32 -04004863static unsigned long ratelimit_time;
4864static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4865
4866int ata_ratelimit(void)
4867{
4868 int rc;
4869 unsigned long flags;
4870
4871 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4872
4873 if (time_after(jiffies, ratelimit_time)) {
4874 rc = 1;
4875 ratelimit_time = jiffies + (HZ/5);
4876 } else
4877 rc = 0;
4878
4879 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4880
4881 return rc;
4882}
4883
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884/*
4885 * libata is essentially a library of internal helper functions for
4886 * low-level ATA host controller drivers. As such, the API/ABI is
4887 * likely to change as new drivers are added and updated.
4888 * Do not depend on ABI/API stability.
4889 */
4890
4891EXPORT_SYMBOL_GPL(ata_std_bios_param);
4892EXPORT_SYMBOL_GPL(ata_std_ports);
4893EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004894EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895EXPORT_SYMBOL_GPL(ata_sg_init);
4896EXPORT_SYMBOL_GPL(ata_sg_init_one);
4897EXPORT_SYMBOL_GPL(ata_qc_complete);
4898EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4899EXPORT_SYMBOL_GPL(ata_eng_timeout);
4900EXPORT_SYMBOL_GPL(ata_tf_load);
4901EXPORT_SYMBOL_GPL(ata_tf_read);
4902EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4903EXPORT_SYMBOL_GPL(ata_std_dev_select);
4904EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4905EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4906EXPORT_SYMBOL_GPL(ata_check_status);
4907EXPORT_SYMBOL_GPL(ata_altstatus);
4908EXPORT_SYMBOL_GPL(ata_chk_err);
4909EXPORT_SYMBOL_GPL(ata_exec_command);
4910EXPORT_SYMBOL_GPL(ata_port_start);
4911EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004912EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913EXPORT_SYMBOL_GPL(ata_interrupt);
4914EXPORT_SYMBOL_GPL(ata_qc_prep);
4915EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4916EXPORT_SYMBOL_GPL(ata_bmdma_start);
4917EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4918EXPORT_SYMBOL_GPL(ata_bmdma_status);
4919EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4920EXPORT_SYMBOL_GPL(ata_port_probe);
4921EXPORT_SYMBOL_GPL(sata_phy_reset);
4922EXPORT_SYMBOL_GPL(__sata_phy_reset);
4923EXPORT_SYMBOL_GPL(ata_bus_reset);
4924EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004925EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4927EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4928EXPORT_SYMBOL_GPL(ata_scsi_error);
4929EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4930EXPORT_SYMBOL_GPL(ata_scsi_release);
4931EXPORT_SYMBOL_GPL(ata_host_intr);
4932EXPORT_SYMBOL_GPL(ata_dev_classify);
4933EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004934EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4936
4937#ifdef CONFIG_PCI
4938EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004939EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4941EXPORT_SYMBOL_GPL(ata_pci_init_one);
4942EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4943#endif /* CONFIG_PCI */