blob: 943b44c3c16fc196b3cc75988d45c39ad61bcc3e [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>
51#include <scsi/scsi.h>
52#include "scsi.h"
53#include "scsi_priv.h"
54#include <scsi/scsi_host.h>
55#include <linux/libata.h>
56#include <asm/io.h>
57#include <asm/semaphore.h>
58#include <asm/byteorder.h>
59
60#include "libata.h"
61
62static unsigned int ata_busy_sleep (struct ata_port *ap,
63 unsigned long tmout_pat,
64 unsigned long tmout);
65static void ata_set_mode(struct ata_port *ap);
66static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
67static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
68static int fgb(u32 bitmap);
69static int ata_choose_xfer_mode(struct ata_port *ap,
70 u8 *xfer_mode_out,
71 unsigned int *xfer_shift_out);
72static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat);
73static void __ata_qc_complete(struct ata_queued_cmd *qc);
74
75static unsigned int ata_unique_id = 1;
76static struct workqueue_struct *ata_wq;
77
Jeff Garzik1623c812005-08-30 03:37:42 -040078int atapi_enabled = 0;
79module_param(atapi_enabled, int, 0444);
80MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082MODULE_AUTHOR("Jeff Garzik");
83MODULE_DESCRIPTION("Library module for ATA devices");
84MODULE_LICENSE("GPL");
85MODULE_VERSION(DRV_VERSION);
86
87/**
88 * ata_tf_load - send taskfile registers to host controller
89 * @ap: Port to which output is sent
90 * @tf: ATA taskfile register set
91 *
92 * Outputs ATA taskfile to standard ATA host controller.
93 *
94 * LOCKING:
95 * Inherited from caller.
96 */
97
98static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
99{
100 struct ata_ioports *ioaddr = &ap->ioaddr;
101 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
102
103 if (tf->ctl != ap->last_ctl) {
104 outb(tf->ctl, ioaddr->ctl_addr);
105 ap->last_ctl = tf->ctl;
106 ata_wait_idle(ap);
107 }
108
109 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
110 outb(tf->hob_feature, ioaddr->feature_addr);
111 outb(tf->hob_nsect, ioaddr->nsect_addr);
112 outb(tf->hob_lbal, ioaddr->lbal_addr);
113 outb(tf->hob_lbam, ioaddr->lbam_addr);
114 outb(tf->hob_lbah, ioaddr->lbah_addr);
115 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
116 tf->hob_feature,
117 tf->hob_nsect,
118 tf->hob_lbal,
119 tf->hob_lbam,
120 tf->hob_lbah);
121 }
122
123 if (is_addr) {
124 outb(tf->feature, ioaddr->feature_addr);
125 outb(tf->nsect, ioaddr->nsect_addr);
126 outb(tf->lbal, ioaddr->lbal_addr);
127 outb(tf->lbam, ioaddr->lbam_addr);
128 outb(tf->lbah, ioaddr->lbah_addr);
129 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
130 tf->feature,
131 tf->nsect,
132 tf->lbal,
133 tf->lbam,
134 tf->lbah);
135 }
136
137 if (tf->flags & ATA_TFLAG_DEVICE) {
138 outb(tf->device, ioaddr->device_addr);
139 VPRINTK("device 0x%X\n", tf->device);
140 }
141
142 ata_wait_idle(ap);
143}
144
145/**
146 * ata_tf_load_mmio - send taskfile registers to host controller
147 * @ap: Port to which output is sent
148 * @tf: ATA taskfile register set
149 *
150 * Outputs ATA taskfile to standard ATA host controller using MMIO.
151 *
152 * LOCKING:
153 * Inherited from caller.
154 */
155
156static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
157{
158 struct ata_ioports *ioaddr = &ap->ioaddr;
159 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
160
161 if (tf->ctl != ap->last_ctl) {
162 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
163 ap->last_ctl = tf->ctl;
164 ata_wait_idle(ap);
165 }
166
167 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
168 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
169 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
170 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
171 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
172 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
173 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
174 tf->hob_feature,
175 tf->hob_nsect,
176 tf->hob_lbal,
177 tf->hob_lbam,
178 tf->hob_lbah);
179 }
180
181 if (is_addr) {
182 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
183 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
184 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
185 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
186 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
187 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
188 tf->feature,
189 tf->nsect,
190 tf->lbal,
191 tf->lbam,
192 tf->lbah);
193 }
194
195 if (tf->flags & ATA_TFLAG_DEVICE) {
196 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
197 VPRINTK("device 0x%X\n", tf->device);
198 }
199
200 ata_wait_idle(ap);
201}
202
Edward Falk0baab862005-06-02 18:17:13 -0400203
204/**
205 * ata_tf_load - send taskfile registers to host controller
206 * @ap: Port to which output is sent
207 * @tf: ATA taskfile register set
208 *
209 * Outputs ATA taskfile to standard ATA host controller using MMIO
210 * or PIO as indicated by the ATA_FLAG_MMIO flag.
211 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
212 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
213 * hob_lbal, hob_lbam, and hob_lbah.
214 *
215 * This function waits for idle (!BUSY and !DRQ) after writing
216 * registers. If the control register has a new value, this
217 * function also waits for idle after writing control and before
218 * writing the remaining registers.
219 *
220 * May be used as the tf_load() entry in ata_port_operations.
221 *
222 * LOCKING:
223 * Inherited from caller.
224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
226{
227 if (ap->flags & ATA_FLAG_MMIO)
228 ata_tf_load_mmio(ap, tf);
229 else
230 ata_tf_load_pio(ap, tf);
231}
232
233/**
Edward Falk0baab862005-06-02 18:17:13 -0400234 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * @ap: port to which command is being issued
236 * @tf: ATA taskfile register set
237 *
Edward Falk0baab862005-06-02 18:17:13 -0400238 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * synchronization with interrupt handler / other threads.
240 *
241 * LOCKING:
242 * spin_lock_irqsave(host_set lock)
243 */
244
245static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
246{
247 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
248
249 outb(tf->command, ap->ioaddr.command_addr);
250 ata_pause(ap);
251}
252
253
254/**
255 * ata_exec_command_mmio - issue ATA command to host controller
256 * @ap: port to which command is being issued
257 * @tf: ATA taskfile register set
258 *
259 * Issues MMIO write to ATA command register, with proper
260 * synchronization with interrupt handler / other threads.
261 *
262 * LOCKING:
263 * spin_lock_irqsave(host_set lock)
264 */
265
266static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
267{
268 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
269
270 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
271 ata_pause(ap);
272}
273
Edward Falk0baab862005-06-02 18:17:13 -0400274
275/**
276 * ata_exec_command - issue ATA command to host controller
277 * @ap: port to which command is being issued
278 * @tf: ATA taskfile register set
279 *
280 * Issues PIO/MMIO write to ATA command register, with proper
281 * synchronization with interrupt handler / other threads.
282 *
283 * LOCKING:
284 * spin_lock_irqsave(host_set lock)
285 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
287{
288 if (ap->flags & ATA_FLAG_MMIO)
289 ata_exec_command_mmio(ap, tf);
290 else
291 ata_exec_command_pio(ap, tf);
292}
293
294/**
295 * ata_exec - issue ATA command to host controller
296 * @ap: port to which command is being issued
297 * @tf: ATA taskfile register set
298 *
299 * Issues PIO/MMIO write to ATA command register, with proper
300 * synchronization with interrupt handler / other threads.
301 *
302 * LOCKING:
303 * Obtains host_set lock.
304 */
305
306static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
307{
308 unsigned long flags;
309
310 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
311 spin_lock_irqsave(&ap->host_set->lock, flags);
312 ap->ops->exec_command(ap, tf);
313 spin_unlock_irqrestore(&ap->host_set->lock, flags);
314}
315
316/**
317 * ata_tf_to_host - issue ATA taskfile to host controller
318 * @ap: port to which command is being issued
319 * @tf: ATA taskfile register set
320 *
321 * Issues ATA taskfile register set to ATA host controller,
322 * with proper synchronization with interrupt handler and
323 * other threads.
324 *
325 * LOCKING:
326 * Obtains host_set lock.
327 */
328
329static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
330{
331 ap->ops->tf_load(ap, tf);
332
333 ata_exec(ap, tf);
334}
335
336/**
337 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
338 * @ap: port to which command is being issued
339 * @tf: ATA taskfile register set
340 *
341 * Issues ATA taskfile register set to ATA host controller,
342 * with proper synchronization with interrupt handler and
343 * other threads.
344 *
345 * LOCKING:
346 * spin_lock_irqsave(host_set lock)
347 */
348
349void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
350{
351 ap->ops->tf_load(ap, tf);
352 ap->ops->exec_command(ap, tf);
353}
354
355/**
Edward Falk0baab862005-06-02 18:17:13 -0400356 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * @ap: Port from which input is read
358 * @tf: ATA taskfile register set for storing input
359 *
360 * Reads ATA taskfile registers for currently-selected device
361 * into @tf.
362 *
363 * LOCKING:
364 * Inherited from caller.
365 */
366
367static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
368{
369 struct ata_ioports *ioaddr = &ap->ioaddr;
370
371 tf->nsect = inb(ioaddr->nsect_addr);
372 tf->lbal = inb(ioaddr->lbal_addr);
373 tf->lbam = inb(ioaddr->lbam_addr);
374 tf->lbah = inb(ioaddr->lbah_addr);
375 tf->device = inb(ioaddr->device_addr);
376
377 if (tf->flags & ATA_TFLAG_LBA48) {
378 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
379 tf->hob_feature = inb(ioaddr->error_addr);
380 tf->hob_nsect = inb(ioaddr->nsect_addr);
381 tf->hob_lbal = inb(ioaddr->lbal_addr);
382 tf->hob_lbam = inb(ioaddr->lbam_addr);
383 tf->hob_lbah = inb(ioaddr->lbah_addr);
384 }
385}
386
387/**
388 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
389 * @ap: Port from which input is read
390 * @tf: ATA taskfile register set for storing input
391 *
392 * Reads ATA taskfile registers for currently-selected device
393 * into @tf via MMIO.
394 *
395 * LOCKING:
396 * Inherited from caller.
397 */
398
399static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
400{
401 struct ata_ioports *ioaddr = &ap->ioaddr;
402
403 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
404 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
405 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
406 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
407 tf->device = readb((void __iomem *)ioaddr->device_addr);
408
409 if (tf->flags & ATA_TFLAG_LBA48) {
410 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
411 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
412 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
413 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
414 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
415 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
416 }
417}
418
Edward Falk0baab862005-06-02 18:17:13 -0400419
420/**
421 * ata_tf_read - input device's ATA taskfile shadow registers
422 * @ap: Port from which input is read
423 * @tf: ATA taskfile register set for storing input
424 *
425 * Reads ATA taskfile registers for currently-selected device
426 * into @tf.
427 *
428 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
429 * is set, also reads the hob registers.
430 *
431 * May be used as the tf_read() entry in ata_port_operations.
432 *
433 * LOCKING:
434 * Inherited from caller.
435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
437{
438 if (ap->flags & ATA_FLAG_MMIO)
439 ata_tf_read_mmio(ap, tf);
440 else
441 ata_tf_read_pio(ap, tf);
442}
443
444/**
445 * ata_check_status_pio - Read device status reg & clear interrupt
446 * @ap: port where the device is
447 *
448 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400449 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * from this device
451 *
452 * LOCKING:
453 * Inherited from caller.
454 */
455static u8 ata_check_status_pio(struct ata_port *ap)
456{
457 return inb(ap->ioaddr.status_addr);
458}
459
460/**
461 * ata_check_status_mmio - Read device status reg & clear interrupt
462 * @ap: port where the device is
463 *
464 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400465 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * from this device
467 *
468 * LOCKING:
469 * Inherited from caller.
470 */
471static u8 ata_check_status_mmio(struct ata_port *ap)
472{
473 return readb((void __iomem *) ap->ioaddr.status_addr);
474}
475
Edward Falk0baab862005-06-02 18:17:13 -0400476
477/**
478 * ata_check_status - Read device status reg & clear interrupt
479 * @ap: port where the device is
480 *
481 * Reads ATA taskfile status register for currently-selected device
482 * and return its value. This also clears pending interrupts
483 * from this device
484 *
485 * May be used as the check_status() entry in ata_port_operations.
486 *
487 * LOCKING:
488 * Inherited from caller.
489 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490u8 ata_check_status(struct ata_port *ap)
491{
492 if (ap->flags & ATA_FLAG_MMIO)
493 return ata_check_status_mmio(ap);
494 return ata_check_status_pio(ap);
495}
496
Edward Falk0baab862005-06-02 18:17:13 -0400497
498/**
499 * ata_altstatus - Read device alternate status reg
500 * @ap: port where the device is
501 *
502 * Reads ATA taskfile alternate status register for
503 * currently-selected device and return its value.
504 *
505 * Note: may NOT be used as the check_altstatus() entry in
506 * ata_port_operations.
507 *
508 * LOCKING:
509 * Inherited from caller.
510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511u8 ata_altstatus(struct ata_port *ap)
512{
513 if (ap->ops->check_altstatus)
514 return ap->ops->check_altstatus(ap);
515
516 if (ap->flags & ATA_FLAG_MMIO)
517 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
518 return inb(ap->ioaddr.altstatus_addr);
519}
520
Edward Falk0baab862005-06-02 18:17:13 -0400521
522/**
523 * ata_chk_err - Read device error reg
524 * @ap: port where the device is
525 *
526 * Reads ATA taskfile error register for
527 * currently-selected device and return its value.
528 *
529 * Note: may NOT be used as the check_err() entry in
530 * ata_port_operations.
531 *
532 * LOCKING:
533 * Inherited from caller.
534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535u8 ata_chk_err(struct ata_port *ap)
536{
537 if (ap->ops->check_err)
538 return ap->ops->check_err(ap);
539
540 if (ap->flags & ATA_FLAG_MMIO) {
541 return readb((void __iomem *) ap->ioaddr.error_addr);
542 }
543 return inb(ap->ioaddr.error_addr);
544}
545
546/**
547 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
548 * @tf: Taskfile to convert
549 * @fis: Buffer into which data will output
550 * @pmp: Port multiplier port
551 *
552 * Converts a standard ATA taskfile to a Serial ATA
553 * FIS structure (Register - Host to Device).
554 *
555 * LOCKING:
556 * Inherited from caller.
557 */
558
559void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
560{
561 fis[0] = 0x27; /* Register - Host to Device FIS */
562 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
563 bit 7 indicates Command FIS */
564 fis[2] = tf->command;
565 fis[3] = tf->feature;
566
567 fis[4] = tf->lbal;
568 fis[5] = tf->lbam;
569 fis[6] = tf->lbah;
570 fis[7] = tf->device;
571
572 fis[8] = tf->hob_lbal;
573 fis[9] = tf->hob_lbam;
574 fis[10] = tf->hob_lbah;
575 fis[11] = tf->hob_feature;
576
577 fis[12] = tf->nsect;
578 fis[13] = tf->hob_nsect;
579 fis[14] = 0;
580 fis[15] = tf->ctl;
581
582 fis[16] = 0;
583 fis[17] = 0;
584 fis[18] = 0;
585 fis[19] = 0;
586}
587
588/**
589 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
590 * @fis: Buffer from which data will be input
591 * @tf: Taskfile to output
592 *
593 * Converts a standard ATA taskfile to a Serial ATA
594 * FIS structure (Register - Host to Device).
595 *
596 * LOCKING:
597 * Inherited from caller.
598 */
599
600void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
601{
602 tf->command = fis[2]; /* status */
603 tf->feature = fis[3]; /* error */
604
605 tf->lbal = fis[4];
606 tf->lbam = fis[5];
607 tf->lbah = fis[6];
608 tf->device = fis[7];
609
610 tf->hob_lbal = fis[8];
611 tf->hob_lbam = fis[9];
612 tf->hob_lbah = fis[10];
613
614 tf->nsect = fis[12];
615 tf->hob_nsect = fis[13];
616}
617
618/**
619 * ata_prot_to_cmd - determine which read/write opcodes to use
620 * @protocol: ATA_PROT_xxx taskfile protocol
621 * @lba48: true is lba48 is present
622 *
623 * Given necessary input, determine which read/write commands
624 * to use to transfer data.
625 *
626 * LOCKING:
627 * None.
628 */
629static int ata_prot_to_cmd(int protocol, int lba48)
630{
631 int rcmd = 0, wcmd = 0;
632
633 switch (protocol) {
634 case ATA_PROT_PIO:
635 if (lba48) {
636 rcmd = ATA_CMD_PIO_READ_EXT;
637 wcmd = ATA_CMD_PIO_WRITE_EXT;
638 } else {
639 rcmd = ATA_CMD_PIO_READ;
640 wcmd = ATA_CMD_PIO_WRITE;
641 }
642 break;
643
644 case ATA_PROT_DMA:
645 if (lba48) {
646 rcmd = ATA_CMD_READ_EXT;
647 wcmd = ATA_CMD_WRITE_EXT;
648 } else {
649 rcmd = ATA_CMD_READ;
650 wcmd = ATA_CMD_WRITE;
651 }
652 break;
653
654 default:
655 return -1;
656 }
657
658 return rcmd | (wcmd << 8);
659}
660
661/**
662 * ata_dev_set_protocol - set taskfile protocol and r/w commands
663 * @dev: device to examine and configure
664 *
665 * Examine the device configuration, after we have
666 * read the identify-device page and configured the
667 * data transfer mode. Set internal state related to
668 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
669 * and calculate the proper read/write commands to use.
670 *
671 * LOCKING:
672 * caller.
673 */
674static void ata_dev_set_protocol(struct ata_device *dev)
675{
676 int pio = (dev->flags & ATA_DFLAG_PIO);
677 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
678 int proto, cmd;
679
680 if (pio)
681 proto = dev->xfer_protocol = ATA_PROT_PIO;
682 else
683 proto = dev->xfer_protocol = ATA_PROT_DMA;
684
685 cmd = ata_prot_to_cmd(proto, lba48);
686 if (cmd < 0)
687 BUG();
688
689 dev->read_cmd = cmd & 0xff;
690 dev->write_cmd = (cmd >> 8) & 0xff;
691}
692
693static const char * xfer_mode_str[] = {
694 "UDMA/16",
695 "UDMA/25",
696 "UDMA/33",
697 "UDMA/44",
698 "UDMA/66",
699 "UDMA/100",
700 "UDMA/133",
701 "UDMA7",
702 "MWDMA0",
703 "MWDMA1",
704 "MWDMA2",
705 "PIO0",
706 "PIO1",
707 "PIO2",
708 "PIO3",
709 "PIO4",
710};
711
712/**
713 * ata_udma_string - convert UDMA bit offset to string
714 * @mask: mask of bits supported; only highest bit counts.
715 *
716 * Determine string which represents the highest speed
717 * (highest bit in @udma_mask).
718 *
719 * LOCKING:
720 * None.
721 *
722 * RETURNS:
723 * Constant C string representing highest speed listed in
724 * @udma_mask, or the constant C string "<n/a>".
725 */
726
727static const char *ata_mode_string(unsigned int mask)
728{
729 int i;
730
731 for (i = 7; i >= 0; i--)
732 if (mask & (1 << i))
733 goto out;
734 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
735 if (mask & (1 << i))
736 goto out;
737 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
738 if (mask & (1 << i))
739 goto out;
740
741 return "<n/a>";
742
743out:
744 return xfer_mode_str[i];
745}
746
747/**
748 * ata_pio_devchk - PATA device presence detection
749 * @ap: ATA channel to examine
750 * @device: Device to examine (starting at zero)
751 *
752 * This technique was originally described in
753 * Hale Landis's ATADRVR (www.ata-atapi.com), and
754 * later found its way into the ATA/ATAPI spec.
755 *
756 * Write a pattern to the ATA shadow registers,
757 * and if a device is present, it will respond by
758 * correctly storing and echoing back the
759 * ATA shadow register contents.
760 *
761 * LOCKING:
762 * caller.
763 */
764
765static unsigned int ata_pio_devchk(struct ata_port *ap,
766 unsigned int device)
767{
768 struct ata_ioports *ioaddr = &ap->ioaddr;
769 u8 nsect, lbal;
770
771 ap->ops->dev_select(ap, device);
772
773 outb(0x55, ioaddr->nsect_addr);
774 outb(0xaa, ioaddr->lbal_addr);
775
776 outb(0xaa, ioaddr->nsect_addr);
777 outb(0x55, ioaddr->lbal_addr);
778
779 outb(0x55, ioaddr->nsect_addr);
780 outb(0xaa, ioaddr->lbal_addr);
781
782 nsect = inb(ioaddr->nsect_addr);
783 lbal = inb(ioaddr->lbal_addr);
784
785 if ((nsect == 0x55) && (lbal == 0xaa))
786 return 1; /* we found a device */
787
788 return 0; /* nothing found */
789}
790
791/**
792 * ata_mmio_devchk - PATA device presence detection
793 * @ap: ATA channel to examine
794 * @device: Device to examine (starting at zero)
795 *
796 * This technique was originally described in
797 * Hale Landis's ATADRVR (www.ata-atapi.com), and
798 * later found its way into the ATA/ATAPI spec.
799 *
800 * Write a pattern to the ATA shadow registers,
801 * and if a device is present, it will respond by
802 * correctly storing and echoing back the
803 * ATA shadow register contents.
804 *
805 * LOCKING:
806 * caller.
807 */
808
809static unsigned int ata_mmio_devchk(struct ata_port *ap,
810 unsigned int device)
811{
812 struct ata_ioports *ioaddr = &ap->ioaddr;
813 u8 nsect, lbal;
814
815 ap->ops->dev_select(ap, device);
816
817 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
818 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
819
820 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
821 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
822
823 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
824 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
825
826 nsect = readb((void __iomem *) ioaddr->nsect_addr);
827 lbal = readb((void __iomem *) ioaddr->lbal_addr);
828
829 if ((nsect == 0x55) && (lbal == 0xaa))
830 return 1; /* we found a device */
831
832 return 0; /* nothing found */
833}
834
835/**
836 * ata_devchk - PATA device presence detection
837 * @ap: ATA channel to examine
838 * @device: Device to examine (starting at zero)
839 *
840 * Dispatch ATA device presence detection, depending
841 * on whether we are using PIO or MMIO to talk to the
842 * ATA shadow registers.
843 *
844 * LOCKING:
845 * caller.
846 */
847
848static unsigned int ata_devchk(struct ata_port *ap,
849 unsigned int device)
850{
851 if (ap->flags & ATA_FLAG_MMIO)
852 return ata_mmio_devchk(ap, device);
853 return ata_pio_devchk(ap, device);
854}
855
856/**
857 * ata_dev_classify - determine device type based on ATA-spec signature
858 * @tf: ATA taskfile register set for device to be identified
859 *
860 * Determine from taskfile register contents whether a device is
861 * ATA or ATAPI, as per "Signature and persistence" section
862 * of ATA/PI spec (volume 1, sect 5.14).
863 *
864 * LOCKING:
865 * None.
866 *
867 * RETURNS:
868 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
869 * the event of failure.
870 */
871
872unsigned int ata_dev_classify(struct ata_taskfile *tf)
873{
874 /* Apple's open source Darwin code hints that some devices only
875 * put a proper signature into the LBA mid/high registers,
876 * So, we only check those. It's sufficient for uniqueness.
877 */
878
879 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
880 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
881 DPRINTK("found ATA device by sig\n");
882 return ATA_DEV_ATA;
883 }
884
885 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
886 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
887 DPRINTK("found ATAPI device by sig\n");
888 return ATA_DEV_ATAPI;
889 }
890
891 DPRINTK("unknown device\n");
892 return ATA_DEV_UNKNOWN;
893}
894
895/**
896 * ata_dev_try_classify - Parse returned ATA device signature
897 * @ap: ATA channel to examine
898 * @device: Device to examine (starting at zero)
899 *
900 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
901 * an ATA/ATAPI-defined set of values is placed in the ATA
902 * shadow registers, indicating the results of device detection
903 * and diagnostics.
904 *
905 * Select the ATA device, and read the values from the ATA shadow
906 * registers. Then parse according to the Error register value,
907 * and the spec-defined values examined by ata_dev_classify().
908 *
909 * LOCKING:
910 * caller.
911 */
912
913static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
914{
915 struct ata_device *dev = &ap->device[device];
916 struct ata_taskfile tf;
917 unsigned int class;
918 u8 err;
919
920 ap->ops->dev_select(ap, device);
921
922 memset(&tf, 0, sizeof(tf));
923
924 err = ata_chk_err(ap);
925 ap->ops->tf_read(ap, &tf);
926
927 dev->class = ATA_DEV_NONE;
928
929 /* see if device passed diags */
930 if (err == 1)
931 /* do nothing */ ;
932 else if ((device == 0) && (err == 0x81))
933 /* do nothing */ ;
934 else
935 return err;
936
937 /* determine if device if ATA or ATAPI */
938 class = ata_dev_classify(&tf);
939 if (class == ATA_DEV_UNKNOWN)
940 return err;
941 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
942 return err;
943
944 dev->class = class;
945
946 return err;
947}
948
949/**
950 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
951 * @id: IDENTIFY DEVICE results we will examine
952 * @s: string into which data is output
953 * @ofs: offset into identify device page
954 * @len: length of string to return. must be an even number.
955 *
956 * The strings in the IDENTIFY DEVICE page are broken up into
957 * 16-bit chunks. Run through the string, and output each
958 * 8-bit chunk linearly, regardless of platform.
959 *
960 * LOCKING:
961 * caller.
962 */
963
964void ata_dev_id_string(u16 *id, unsigned char *s,
965 unsigned int ofs, unsigned int len)
966{
967 unsigned int c;
968
969 while (len > 0) {
970 c = id[ofs] >> 8;
971 *s = c;
972 s++;
973
974 c = id[ofs] & 0xff;
975 *s = c;
976 s++;
977
978 ofs++;
979 len -= 2;
980 }
981}
982
Edward Falk0baab862005-06-02 18:17:13 -0400983
984/**
985 * ata_noop_dev_select - Select device 0/1 on ATA bus
986 * @ap: ATA channel to manipulate
987 * @device: ATA device (numbered from zero) to select
988 *
989 * This function performs no actual function.
990 *
991 * May be used as the dev_select() entry in ata_port_operations.
992 *
993 * LOCKING:
994 * caller.
995 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
997{
998}
999
Edward Falk0baab862005-06-02 18:17:13 -04001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/**
1002 * ata_std_dev_select - Select device 0/1 on ATA bus
1003 * @ap: ATA channel to manipulate
1004 * @device: ATA device (numbered from zero) to select
1005 *
1006 * Use the method defined in the ATA specification to
1007 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -04001008 * ATA channel. Works with both PIO and MMIO.
1009 *
1010 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 *
1012 * LOCKING:
1013 * caller.
1014 */
1015
1016void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1017{
1018 u8 tmp;
1019
1020 if (device == 0)
1021 tmp = ATA_DEVICE_OBS;
1022 else
1023 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1024
1025 if (ap->flags & ATA_FLAG_MMIO) {
1026 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1027 } else {
1028 outb(tmp, ap->ioaddr.device_addr);
1029 }
1030 ata_pause(ap); /* needed; also flushes, for mmio */
1031}
1032
1033/**
1034 * ata_dev_select - Select device 0/1 on ATA bus
1035 * @ap: ATA channel to manipulate
1036 * @device: ATA device (numbered from zero) to select
1037 * @wait: non-zero to wait for Status register BSY bit to clear
1038 * @can_sleep: non-zero if context allows sleeping
1039 *
1040 * Use the method defined in the ATA specification to
1041 * make either device 0, or device 1, active on the
1042 * ATA channel.
1043 *
1044 * This is a high-level version of ata_std_dev_select(),
1045 * which additionally provides the services of inserting
1046 * the proper pauses and status polling, where needed.
1047 *
1048 * LOCKING:
1049 * caller.
1050 */
1051
1052void ata_dev_select(struct ata_port *ap, unsigned int device,
1053 unsigned int wait, unsigned int can_sleep)
1054{
1055 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1056 ap->id, device, wait);
1057
1058 if (wait)
1059 ata_wait_idle(ap);
1060
1061 ap->ops->dev_select(ap, device);
1062
1063 if (wait) {
1064 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1065 msleep(150);
1066 ata_wait_idle(ap);
1067 }
1068}
1069
1070/**
1071 * ata_dump_id - IDENTIFY DEVICE info debugging output
1072 * @dev: Device whose IDENTIFY DEVICE page we will dump
1073 *
1074 * Dump selected 16-bit words from a detected device's
1075 * IDENTIFY PAGE page.
1076 *
1077 * LOCKING:
1078 * caller.
1079 */
1080
1081static inline void ata_dump_id(struct ata_device *dev)
1082{
1083 DPRINTK("49==0x%04x "
1084 "53==0x%04x "
1085 "63==0x%04x "
1086 "64==0x%04x "
1087 "75==0x%04x \n",
1088 dev->id[49],
1089 dev->id[53],
1090 dev->id[63],
1091 dev->id[64],
1092 dev->id[75]);
1093 DPRINTK("80==0x%04x "
1094 "81==0x%04x "
1095 "82==0x%04x "
1096 "83==0x%04x "
1097 "84==0x%04x \n",
1098 dev->id[80],
1099 dev->id[81],
1100 dev->id[82],
1101 dev->id[83],
1102 dev->id[84]);
1103 DPRINTK("88==0x%04x "
1104 "93==0x%04x\n",
1105 dev->id[88],
1106 dev->id[93]);
1107}
1108
1109/**
1110 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1111 * @ap: port on which device we wish to probe resides
1112 * @device: device bus address, starting at zero
1113 *
1114 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1115 * command, and read back the 512-byte device information page.
1116 * The device information page is fed to us via the standard
1117 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1118 * using standard PIO-IN paths)
1119 *
1120 * After reading the device information page, we use several
1121 * bits of information from it to initialize data structures
1122 * that will be used during the lifetime of the ata_device.
1123 * Other data from the info page is used to disqualify certain
1124 * older ATA devices we do not wish to support.
1125 *
1126 * LOCKING:
1127 * Inherited from caller. Some functions called by this function
1128 * obtain the host_set lock.
1129 */
1130
1131static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1132{
1133 struct ata_device *dev = &ap->device[device];
1134 unsigned int i;
1135 u16 tmp;
1136 unsigned long xfer_modes;
1137 u8 status;
1138 unsigned int using_edd;
1139 DECLARE_COMPLETION(wait);
1140 struct ata_queued_cmd *qc;
1141 unsigned long flags;
1142 int rc;
1143
1144 if (!ata_dev_present(dev)) {
1145 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1146 ap->id, device);
1147 return;
1148 }
1149
1150 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1151 using_edd = 0;
1152 else
1153 using_edd = 1;
1154
1155 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1156
1157 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1158 dev->class == ATA_DEV_NONE);
1159
1160 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1161
1162 qc = ata_qc_new_init(ap, dev);
1163 BUG_ON(qc == NULL);
1164
1165 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1166 qc->dma_dir = DMA_FROM_DEVICE;
1167 qc->tf.protocol = ATA_PROT_PIO;
1168 qc->nsect = 1;
1169
1170retry:
1171 if (dev->class == ATA_DEV_ATA) {
1172 qc->tf.command = ATA_CMD_ID_ATA;
1173 DPRINTK("do ATA identify\n");
1174 } else {
1175 qc->tf.command = ATA_CMD_ID_ATAPI;
1176 DPRINTK("do ATAPI identify\n");
1177 }
1178
1179 qc->waiting = &wait;
1180 qc->complete_fn = ata_qc_complete_noop;
1181
1182 spin_lock_irqsave(&ap->host_set->lock, flags);
1183 rc = ata_qc_issue(qc);
1184 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1185
1186 if (rc)
1187 goto err_out;
1188 else
1189 wait_for_completion(&wait);
1190
1191 status = ata_chk_status(ap);
1192 if (status & ATA_ERR) {
1193 /*
1194 * arg! EDD works for all test cases, but seems to return
1195 * the ATA signature for some ATAPI devices. Until the
1196 * reason for this is found and fixed, we fix up the mess
1197 * here. If IDENTIFY DEVICE returns command aborted
1198 * (as ATAPI devices do), then we issue an
1199 * IDENTIFY PACKET DEVICE.
1200 *
1201 * ATA software reset (SRST, the default) does not appear
1202 * to have this problem.
1203 */
1204 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1205 u8 err = ata_chk_err(ap);
1206 if (err & ATA_ABORTED) {
1207 dev->class = ATA_DEV_ATAPI;
1208 qc->cursg = 0;
1209 qc->cursg_ofs = 0;
1210 qc->cursect = 0;
1211 qc->nsect = 1;
1212 goto retry;
1213 }
1214 }
1215 goto err_out;
1216 }
1217
1218 swap_buf_le16(dev->id, ATA_ID_WORDS);
1219
1220 /* print device capabilities */
1221 printk(KERN_DEBUG "ata%u: dev %u cfg "
1222 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1223 ap->id, device, dev->id[49],
1224 dev->id[82], dev->id[83], dev->id[84],
1225 dev->id[85], dev->id[86], dev->id[87],
1226 dev->id[88]);
1227
1228 /*
1229 * common ATA, ATAPI feature tests
1230 */
1231
1232 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1233 if (!ata_id_has_dma(dev->id) || !ata_id_has_lba(dev->id)) {
1234 printk(KERN_DEBUG "ata%u: no dma/lba\n", ap->id);
1235 goto err_out_nosup;
1236 }
1237
1238 /* quick-n-dirty find max transfer mode; for printk only */
1239 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1240 if (!xfer_modes)
1241 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1242 if (!xfer_modes) {
1243 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1244 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1245 }
1246
1247 ata_dump_id(dev);
1248
1249 /* ATA-specific feature tests */
1250 if (dev->class == ATA_DEV_ATA) {
1251 if (!ata_id_is_ata(dev->id)) /* sanity check */
1252 goto err_out_nosup;
1253
1254 tmp = dev->id[ATA_ID_MAJOR_VER];
1255 for (i = 14; i >= 1; i--)
1256 if (tmp & (1 << i))
1257 break;
1258
1259 /* we require at least ATA-3 */
1260 if (i < 3) {
1261 printk(KERN_DEBUG "ata%u: no ATA-3\n", ap->id);
1262 goto err_out_nosup;
1263 }
1264
1265 if (ata_id_has_lba48(dev->id)) {
1266 dev->flags |= ATA_DFLAG_LBA48;
1267 dev->n_sectors = ata_id_u64(dev->id, 100);
1268 } else {
1269 dev->n_sectors = ata_id_u32(dev->id, 60);
1270 }
1271
1272 ap->host->max_cmd_len = 16;
1273
1274 /* print device info to dmesg */
1275 printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1276 ap->id, device,
1277 ata_mode_string(xfer_modes),
1278 (unsigned long long)dev->n_sectors,
1279 dev->flags & ATA_DFLAG_LBA48 ? " lba48" : "");
1280 }
1281
1282 /* ATAPI-specific feature tests */
1283 else {
1284 if (ata_id_is_ata(dev->id)) /* sanity check */
1285 goto err_out_nosup;
1286
1287 rc = atapi_cdb_len(dev->id);
1288 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1289 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1290 goto err_out_nosup;
1291 }
1292 ap->cdb_len = (unsigned int) rc;
1293 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1294
1295 /* print device info to dmesg */
1296 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1297 ap->id, device,
1298 ata_mode_string(xfer_modes));
1299 }
1300
1301 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1302 return;
1303
1304err_out_nosup:
1305 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1306 ap->id, device);
1307err_out:
1308 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1309 DPRINTK("EXIT, err\n");
1310}
1311
Brad Campbell6f2f3812005-05-12 15:07:47 -04001312
1313static inline u8 ata_dev_knobble(struct ata_port *ap)
1314{
1315 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1316}
1317
1318/**
1319 * ata_dev_config - Run device specific handlers and check for
1320 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001321 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001322 * @i: Device
1323 *
1324 * LOCKING:
1325 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001326
Brad Campbell6f2f3812005-05-12 15:07:47 -04001327void ata_dev_config(struct ata_port *ap, unsigned int i)
1328{
1329 /* limit bridge transfers to udma5, 200 sectors */
1330 if (ata_dev_knobble(ap)) {
1331 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1332 ap->id, ap->device->devno);
1333 ap->udma_mask &= ATA_UDMA5;
1334 ap->host->max_sectors = ATA_MAX_SECTORS;
1335 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1336 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1337 }
1338
1339 if (ap->ops->dev_config)
1340 ap->ops->dev_config(ap, &ap->device[i]);
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/**
1344 * ata_bus_probe - Reset and probe ATA bus
1345 * @ap: Bus to probe
1346 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001347 * Master ATA bus probing function. Initiates a hardware-dependent
1348 * bus reset, then attempts to identify any devices found on
1349 * the bus.
1350 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001352 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 *
1354 * RETURNS:
1355 * Zero on success, non-zero on error.
1356 */
1357
1358static int ata_bus_probe(struct ata_port *ap)
1359{
1360 unsigned int i, found = 0;
1361
1362 ap->ops->phy_reset(ap);
1363 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1364 goto err_out;
1365
1366 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1367 ata_dev_identify(ap, i);
1368 if (ata_dev_present(&ap->device[i])) {
1369 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001370 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 }
1373
1374 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1375 goto err_out_disable;
1376
1377 ata_set_mode(ap);
1378 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1379 goto err_out_disable;
1380
1381 return 0;
1382
1383err_out_disable:
1384 ap->ops->port_disable(ap);
1385err_out:
1386 return -1;
1387}
1388
1389/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001390 * ata_port_probe - Mark port as enabled
1391 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001393 * Modify @ap data structure such that the system
1394 * thinks that the entire port is enabled.
1395 *
1396 * LOCKING: host_set lock, or some other form of
1397 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 */
1399
1400void ata_port_probe(struct ata_port *ap)
1401{
1402 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1403}
1404
1405/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001406 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1407 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001409 * This function issues commands to standard SATA Sxxx
1410 * PHY registers, to wake up the phy (and device), and
1411 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 *
1413 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001414 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 *
1416 */
1417void __sata_phy_reset(struct ata_port *ap)
1418{
1419 u32 sstatus;
1420 unsigned long timeout = jiffies + (HZ * 5);
1421
1422 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001423 /* issue phy wake/reset */
1424 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001425 /* Couldn't find anything in SATA I/II specs, but
1426 * AHCI-1.1 10.4.2 says at least 1 ms. */
1427 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Brett Russcdcca892005-03-28 15:10:27 -05001429 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /* wait for phy to become ready, if necessary */
1432 do {
1433 msleep(200);
1434 sstatus = scr_read(ap, SCR_STATUS);
1435 if ((sstatus & 0xf) != 1)
1436 break;
1437 } while (time_before(jiffies, timeout));
1438
1439 /* TODO: phy layer with polling, timeouts, etc. */
1440 if (sata_dev_present(ap))
1441 ata_port_probe(ap);
1442 else {
1443 sstatus = scr_read(ap, SCR_STATUS);
1444 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1445 ap->id, sstatus);
1446 ata_port_disable(ap);
1447 }
1448
1449 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1450 return;
1451
1452 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1453 ata_port_disable(ap);
1454 return;
1455 }
1456
1457 ap->cbl = ATA_CBL_SATA;
1458}
1459
1460/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001461 * sata_phy_reset - Reset SATA bus.
1462 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001464 * This function resets the SATA bus, and then probes
1465 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
1467 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001468 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 *
1470 */
1471void sata_phy_reset(struct ata_port *ap)
1472{
1473 __sata_phy_reset(ap);
1474 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1475 return;
1476 ata_bus_reset(ap);
1477}
1478
1479/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001480 * ata_port_disable - Disable port.
1481 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001483 * Modify @ap data structure such that the system
1484 * thinks that the entire port is disabled, and should
1485 * never attempt to probe or communicate with devices
1486 * on this port.
1487 *
1488 * LOCKING: host_set lock, or some other form of
1489 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 */
1491
1492void ata_port_disable(struct ata_port *ap)
1493{
1494 ap->device[0].class = ATA_DEV_NONE;
1495 ap->device[1].class = ATA_DEV_NONE;
1496 ap->flags |= ATA_FLAG_PORT_DISABLED;
1497}
1498
1499static struct {
1500 unsigned int shift;
1501 u8 base;
1502} xfer_mode_classes[] = {
1503 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1504 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1505 { ATA_SHIFT_PIO, XFER_PIO_0 },
1506};
1507
1508static inline u8 base_from_shift(unsigned int shift)
1509{
1510 int i;
1511
1512 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1513 if (xfer_mode_classes[i].shift == shift)
1514 return xfer_mode_classes[i].base;
1515
1516 return 0xff;
1517}
1518
1519static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1520{
1521 int ofs, idx;
1522 u8 base;
1523
1524 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1525 return;
1526
1527 if (dev->xfer_shift == ATA_SHIFT_PIO)
1528 dev->flags |= ATA_DFLAG_PIO;
1529
1530 ata_dev_set_xfermode(ap, dev);
1531
1532 base = base_from_shift(dev->xfer_shift);
1533 ofs = dev->xfer_mode - base;
1534 idx = ofs + dev->xfer_shift;
1535 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1536
1537 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1538 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1539
1540 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1541 ap->id, dev->devno, xfer_mode_str[idx]);
1542}
1543
1544static int ata_host_set_pio(struct ata_port *ap)
1545{
1546 unsigned int mask;
1547 int x, i;
1548 u8 base, xfer_mode;
1549
1550 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1551 x = fgb(mask);
1552 if (x < 0) {
1553 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1554 return -1;
1555 }
1556
1557 base = base_from_shift(ATA_SHIFT_PIO);
1558 xfer_mode = base + x;
1559
1560 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1561 (int)base, (int)xfer_mode, mask, x);
1562
1563 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1564 struct ata_device *dev = &ap->device[i];
1565 if (ata_dev_present(dev)) {
1566 dev->pio_mode = xfer_mode;
1567 dev->xfer_mode = xfer_mode;
1568 dev->xfer_shift = ATA_SHIFT_PIO;
1569 if (ap->ops->set_piomode)
1570 ap->ops->set_piomode(ap, dev);
1571 }
1572 }
1573
1574 return 0;
1575}
1576
1577static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1578 unsigned int xfer_shift)
1579{
1580 int i;
1581
1582 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1583 struct ata_device *dev = &ap->device[i];
1584 if (ata_dev_present(dev)) {
1585 dev->dma_mode = xfer_mode;
1586 dev->xfer_mode = xfer_mode;
1587 dev->xfer_shift = xfer_shift;
1588 if (ap->ops->set_dmamode)
1589 ap->ops->set_dmamode(ap, dev);
1590 }
1591 }
1592}
1593
1594/**
1595 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1596 * @ap: port on which timings will be programmed
1597 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001598 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1599 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001601 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 *
1603 */
1604static void ata_set_mode(struct ata_port *ap)
1605{
1606 unsigned int i, xfer_shift;
1607 u8 xfer_mode;
1608 int rc;
1609
1610 /* step 1: always set host PIO timings */
1611 rc = ata_host_set_pio(ap);
1612 if (rc)
1613 goto err_out;
1614
1615 /* step 2: choose the best data xfer mode */
1616 xfer_mode = xfer_shift = 0;
1617 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1618 if (rc)
1619 goto err_out;
1620
1621 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1622 if (xfer_shift != ATA_SHIFT_PIO)
1623 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1624
1625 /* step 4: update devices' xfer mode */
1626 ata_dev_set_mode(ap, &ap->device[0]);
1627 ata_dev_set_mode(ap, &ap->device[1]);
1628
1629 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1630 return;
1631
1632 if (ap->ops->post_set_mode)
1633 ap->ops->post_set_mode(ap);
1634
1635 for (i = 0; i < 2; i++) {
1636 struct ata_device *dev = &ap->device[i];
1637 ata_dev_set_protocol(dev);
1638 }
1639
1640 return;
1641
1642err_out:
1643 ata_port_disable(ap);
1644}
1645
1646/**
1647 * ata_busy_sleep - sleep until BSY clears, or timeout
1648 * @ap: port containing status register to be polled
1649 * @tmout_pat: impatience timeout
1650 * @tmout: overall timeout
1651 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001652 * Sleep until ATA Status register bit BSY clears,
1653 * or a timeout occurs.
1654 *
1655 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 *
1657 */
1658
1659static unsigned int ata_busy_sleep (struct ata_port *ap,
1660 unsigned long tmout_pat,
1661 unsigned long tmout)
1662{
1663 unsigned long timer_start, timeout;
1664 u8 status;
1665
1666 status = ata_busy_wait(ap, ATA_BUSY, 300);
1667 timer_start = jiffies;
1668 timeout = timer_start + tmout_pat;
1669 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1670 msleep(50);
1671 status = ata_busy_wait(ap, ATA_BUSY, 3);
1672 }
1673
1674 if (status & ATA_BUSY)
1675 printk(KERN_WARNING "ata%u is slow to respond, "
1676 "please be patient\n", ap->id);
1677
1678 timeout = timer_start + tmout;
1679 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1680 msleep(50);
1681 status = ata_chk_status(ap);
1682 }
1683
1684 if (status & ATA_BUSY) {
1685 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1686 ap->id, tmout / HZ);
1687 return 1;
1688 }
1689
1690 return 0;
1691}
1692
1693static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1694{
1695 struct ata_ioports *ioaddr = &ap->ioaddr;
1696 unsigned int dev0 = devmask & (1 << 0);
1697 unsigned int dev1 = devmask & (1 << 1);
1698 unsigned long timeout;
1699
1700 /* if device 0 was found in ata_devchk, wait for its
1701 * BSY bit to clear
1702 */
1703 if (dev0)
1704 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1705
1706 /* if device 1 was found in ata_devchk, wait for
1707 * register access, then wait for BSY to clear
1708 */
1709 timeout = jiffies + ATA_TMOUT_BOOT;
1710 while (dev1) {
1711 u8 nsect, lbal;
1712
1713 ap->ops->dev_select(ap, 1);
1714 if (ap->flags & ATA_FLAG_MMIO) {
1715 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1716 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1717 } else {
1718 nsect = inb(ioaddr->nsect_addr);
1719 lbal = inb(ioaddr->lbal_addr);
1720 }
1721 if ((nsect == 1) && (lbal == 1))
1722 break;
1723 if (time_after(jiffies, timeout)) {
1724 dev1 = 0;
1725 break;
1726 }
1727 msleep(50); /* give drive a breather */
1728 }
1729 if (dev1)
1730 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1731
1732 /* is all this really necessary? */
1733 ap->ops->dev_select(ap, 0);
1734 if (dev1)
1735 ap->ops->dev_select(ap, 1);
1736 if (dev0)
1737 ap->ops->dev_select(ap, 0);
1738}
1739
1740/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001741 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1742 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001744 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1745 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 *
1747 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001748 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 *
1750 */
1751
1752static unsigned int ata_bus_edd(struct ata_port *ap)
1753{
1754 struct ata_taskfile tf;
1755
1756 /* set up execute-device-diag (bus reset) taskfile */
1757 /* also, take interrupts to a known state (disabled) */
1758 DPRINTK("execute-device-diag\n");
1759 ata_tf_init(ap, &tf, 0);
1760 tf.ctl |= ATA_NIEN;
1761 tf.command = ATA_CMD_EDD;
1762 tf.protocol = ATA_PROT_NODATA;
1763
1764 /* do bus reset */
1765 ata_tf_to_host(ap, &tf);
1766
1767 /* spec says at least 2ms. but who knows with those
1768 * crazy ATAPI devices...
1769 */
1770 msleep(150);
1771
1772 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1773}
1774
1775static unsigned int ata_bus_softreset(struct ata_port *ap,
1776 unsigned int devmask)
1777{
1778 struct ata_ioports *ioaddr = &ap->ioaddr;
1779
1780 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1781
1782 /* software reset. causes dev0 to be selected */
1783 if (ap->flags & ATA_FLAG_MMIO) {
1784 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1785 udelay(20); /* FIXME: flush */
1786 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1787 udelay(20); /* FIXME: flush */
1788 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1789 } else {
1790 outb(ap->ctl, ioaddr->ctl_addr);
1791 udelay(10);
1792 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1793 udelay(10);
1794 outb(ap->ctl, ioaddr->ctl_addr);
1795 }
1796
1797 /* spec mandates ">= 2ms" before checking status.
1798 * We wait 150ms, because that was the magic delay used for
1799 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1800 * between when the ATA command register is written, and then
1801 * status is checked. Because waiting for "a while" before
1802 * checking status is fine, post SRST, we perform this magic
1803 * delay here as well.
1804 */
1805 msleep(150);
1806
1807 ata_bus_post_reset(ap, devmask);
1808
1809 return 0;
1810}
1811
1812/**
1813 * ata_bus_reset - reset host port and associated ATA channel
1814 * @ap: port to reset
1815 *
1816 * This is typically the first time we actually start issuing
1817 * commands to the ATA channel. We wait for BSY to clear, then
1818 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1819 * result. Determine what devices, if any, are on the channel
1820 * by looking at the device 0/1 error register. Look at the signature
1821 * stored in each device's taskfile registers, to determine if
1822 * the device is ATA or ATAPI.
1823 *
1824 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001825 * PCI/etc. bus probe sem.
1826 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 *
1828 * SIDE EFFECTS:
1829 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1830 */
1831
1832void ata_bus_reset(struct ata_port *ap)
1833{
1834 struct ata_ioports *ioaddr = &ap->ioaddr;
1835 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1836 u8 err;
1837 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1838
1839 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1840
1841 /* determine if device 0/1 are present */
1842 if (ap->flags & ATA_FLAG_SATA_RESET)
1843 dev0 = 1;
1844 else {
1845 dev0 = ata_devchk(ap, 0);
1846 if (slave_possible)
1847 dev1 = ata_devchk(ap, 1);
1848 }
1849
1850 if (dev0)
1851 devmask |= (1 << 0);
1852 if (dev1)
1853 devmask |= (1 << 1);
1854
1855 /* select device 0 again */
1856 ap->ops->dev_select(ap, 0);
1857
1858 /* issue bus reset */
1859 if (ap->flags & ATA_FLAG_SRST)
1860 rc = ata_bus_softreset(ap, devmask);
1861 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1862 /* set up device control */
1863 if (ap->flags & ATA_FLAG_MMIO)
1864 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1865 else
1866 outb(ap->ctl, ioaddr->ctl_addr);
1867 rc = ata_bus_edd(ap);
1868 }
1869
1870 if (rc)
1871 goto err_out;
1872
1873 /*
1874 * determine by signature whether we have ATA or ATAPI devices
1875 */
1876 err = ata_dev_try_classify(ap, 0);
1877 if ((slave_possible) && (err != 0x81))
1878 ata_dev_try_classify(ap, 1);
1879
1880 /* re-enable interrupts */
1881 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1882 ata_irq_on(ap);
1883
1884 /* is double-select really necessary? */
1885 if (ap->device[1].class != ATA_DEV_NONE)
1886 ap->ops->dev_select(ap, 1);
1887 if (ap->device[0].class != ATA_DEV_NONE)
1888 ap->ops->dev_select(ap, 0);
1889
1890 /* if no devices were detected, disable this port */
1891 if ((ap->device[0].class == ATA_DEV_NONE) &&
1892 (ap->device[1].class == ATA_DEV_NONE))
1893 goto err_out;
1894
1895 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1896 /* set up device control for ATA_FLAG_SATA_RESET */
1897 if (ap->flags & ATA_FLAG_MMIO)
1898 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1899 else
1900 outb(ap->ctl, ioaddr->ctl_addr);
1901 }
1902
1903 DPRINTK("EXIT\n");
1904 return;
1905
1906err_out:
1907 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1908 ap->ops->port_disable(ap);
1909
1910 DPRINTK("EXIT\n");
1911}
1912
1913static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1914{
1915 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1916 ap->id, dev->devno);
1917}
1918
1919static const char * ata_dma_blacklist [] = {
1920 "WDC AC11000H",
1921 "WDC AC22100H",
1922 "WDC AC32500H",
1923 "WDC AC33100H",
1924 "WDC AC31600H",
1925 "WDC AC32100H",
1926 "WDC AC23200L",
1927 "Compaq CRD-8241B",
1928 "CRD-8400B",
1929 "CRD-8480B",
1930 "CRD-8482B",
1931 "CRD-84",
1932 "SanDisk SDP3B",
1933 "SanDisk SDP3B-64",
1934 "SANYO CD-ROM CRD",
1935 "HITACHI CDR-8",
1936 "HITACHI CDR-8335",
1937 "HITACHI CDR-8435",
1938 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04001939 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 "CD-532E-A",
1941 "E-IDE CD-ROM CR-840",
1942 "CD-ROM Drive/F5A",
1943 "WPI CDD-820",
1944 "SAMSUNG CD-ROM SC-148C",
1945 "SAMSUNG CD-ROM SC",
1946 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1948 "_NEC DV5800A",
1949};
1950
1951static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1952{
1953 unsigned char model_num[40];
1954 char *s;
1955 unsigned int len;
1956 int i;
1957
1958 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1959 sizeof(model_num));
1960 s = &model_num[0];
1961 len = strnlen(s, sizeof(model_num));
1962
1963 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1964 while ((len > 0) && (s[len - 1] == ' ')) {
1965 len--;
1966 s[len] = 0;
1967 }
1968
1969 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
1970 if (!strncmp(ata_dma_blacklist[i], s, len))
1971 return 1;
1972
1973 return 0;
1974}
1975
1976static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
1977{
1978 struct ata_device *master, *slave;
1979 unsigned int mask;
1980
1981 master = &ap->device[0];
1982 slave = &ap->device[1];
1983
1984 assert (ata_dev_present(master) || ata_dev_present(slave));
1985
1986 if (shift == ATA_SHIFT_UDMA) {
1987 mask = ap->udma_mask;
1988 if (ata_dev_present(master)) {
1989 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1990 if (ata_dma_blacklisted(ap, master)) {
1991 mask = 0;
1992 ata_pr_blacklisted(ap, master);
1993 }
1994 }
1995 if (ata_dev_present(slave)) {
1996 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
1997 if (ata_dma_blacklisted(ap, slave)) {
1998 mask = 0;
1999 ata_pr_blacklisted(ap, slave);
2000 }
2001 }
2002 }
2003 else if (shift == ATA_SHIFT_MWDMA) {
2004 mask = ap->mwdma_mask;
2005 if (ata_dev_present(master)) {
2006 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2007 if (ata_dma_blacklisted(ap, master)) {
2008 mask = 0;
2009 ata_pr_blacklisted(ap, master);
2010 }
2011 }
2012 if (ata_dev_present(slave)) {
2013 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2014 if (ata_dma_blacklisted(ap, slave)) {
2015 mask = 0;
2016 ata_pr_blacklisted(ap, slave);
2017 }
2018 }
2019 }
2020 else if (shift == ATA_SHIFT_PIO) {
2021 mask = ap->pio_mask;
2022 if (ata_dev_present(master)) {
2023 /* spec doesn't return explicit support for
2024 * PIO0-2, so we fake it
2025 */
2026 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2027 tmp_mode <<= 3;
2028 tmp_mode |= 0x7;
2029 mask &= tmp_mode;
2030 }
2031 if (ata_dev_present(slave)) {
2032 /* spec doesn't return explicit support for
2033 * PIO0-2, so we fake it
2034 */
2035 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2036 tmp_mode <<= 3;
2037 tmp_mode |= 0x7;
2038 mask &= tmp_mode;
2039 }
2040 }
2041 else {
2042 mask = 0xffffffff; /* shut up compiler warning */
2043 BUG();
2044 }
2045
2046 return mask;
2047}
2048
2049/* find greatest bit */
2050static int fgb(u32 bitmap)
2051{
2052 unsigned int i;
2053 int x = -1;
2054
2055 for (i = 0; i < 32; i++)
2056 if (bitmap & (1 << i))
2057 x = i;
2058
2059 return x;
2060}
2061
2062/**
2063 * ata_choose_xfer_mode - attempt to find best transfer mode
2064 * @ap: Port for which an xfer mode will be selected
2065 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2066 * @xfer_shift_out: (output) bit shift that selects this mode
2067 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002068 * Based on host and device capabilities, determine the
2069 * maximum transfer mode that is amenable to all.
2070 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002072 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 *
2074 * RETURNS:
2075 * Zero on success, negative on error.
2076 */
2077
2078static int ata_choose_xfer_mode(struct ata_port *ap,
2079 u8 *xfer_mode_out,
2080 unsigned int *xfer_shift_out)
2081{
2082 unsigned int mask, shift;
2083 int x, i;
2084
2085 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2086 shift = xfer_mode_classes[i].shift;
2087 mask = ata_get_mode_mask(ap, shift);
2088
2089 x = fgb(mask);
2090 if (x >= 0) {
2091 *xfer_mode_out = xfer_mode_classes[i].base + x;
2092 *xfer_shift_out = shift;
2093 return 0;
2094 }
2095 }
2096
2097 return -1;
2098}
2099
2100/**
2101 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2102 * @ap: Port associated with device @dev
2103 * @dev: Device to which command will be sent
2104 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002105 * Issue SET FEATURES - XFER MODE command to device @dev
2106 * on port @ap.
2107 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002109 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 */
2111
2112static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2113{
2114 DECLARE_COMPLETION(wait);
2115 struct ata_queued_cmd *qc;
2116 int rc;
2117 unsigned long flags;
2118
2119 /* set up set-features taskfile */
2120 DPRINTK("set features - xfer mode\n");
2121
2122 qc = ata_qc_new_init(ap, dev);
2123 BUG_ON(qc == NULL);
2124
2125 qc->tf.command = ATA_CMD_SET_FEATURES;
2126 qc->tf.feature = SETFEATURES_XFER;
2127 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2128 qc->tf.protocol = ATA_PROT_NODATA;
2129 qc->tf.nsect = dev->xfer_mode;
2130
2131 qc->waiting = &wait;
2132 qc->complete_fn = ata_qc_complete_noop;
2133
2134 spin_lock_irqsave(&ap->host_set->lock, flags);
2135 rc = ata_qc_issue(qc);
2136 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2137
2138 if (rc)
2139 ata_port_disable(ap);
2140 else
2141 wait_for_completion(&wait);
2142
2143 DPRINTK("EXIT\n");
2144}
2145
2146/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002147 * ata_sg_clean - Unmap DMA memory associated with command
2148 * @qc: Command containing DMA memory to be released
2149 *
2150 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 *
2152 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002153 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 */
2155
2156static void ata_sg_clean(struct ata_queued_cmd *qc)
2157{
2158 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002159 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002161 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2164 assert(sg != NULL);
2165
2166 if (qc->flags & ATA_QCFLAG_SINGLE)
2167 assert(qc->n_elem == 1);
2168
2169 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2170
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002171 /* if we padded the buffer out to 32-bit bound, and data
2172 * xfer direction is from-device, we must copy from the
2173 * pad buffer back into the supplied buffer
2174 */
2175 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2176 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2177
2178 if (qc->flags & ATA_QCFLAG_SG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002180 /* restore last sg */
2181 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2182 if (pad_buf) {
2183 struct scatterlist *psg = &qc->pad_sgent;
2184 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2185 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2186 kunmap_atomic(psg->page, KM_IRQ0);
2187 }
2188 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2190 sg_dma_len(&sg[0]), dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002191 /* restore sg */
2192 sg->length += qc->pad_len;
2193 if (pad_buf)
2194 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2195 pad_buf, qc->pad_len);
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002199 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
2202/**
2203 * ata_fill_sg - Fill PCI IDE PRD table
2204 * @qc: Metadata associated with taskfile to be transferred
2205 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002206 * Fill PCI IDE PRD (scatter-gather) table with segments
2207 * associated with the current disk command.
2208 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002210 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 *
2212 */
2213static void ata_fill_sg(struct ata_queued_cmd *qc)
2214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002216 struct scatterlist *sg;
2217 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002219 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 assert(qc->n_elem > 0);
2221
2222 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002223 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 u32 addr, offset;
2225 u32 sg_len, len;
2226
2227 /* determine if physical DMA addr spans 64K boundary.
2228 * Note h/w doesn't support 64-bit, so we unconditionally
2229 * truncate dma_addr_t to u32.
2230 */
2231 addr = (u32) sg_dma_address(sg);
2232 sg_len = sg_dma_len(sg);
2233
2234 while (sg_len) {
2235 offset = addr & 0xffff;
2236 len = sg_len;
2237 if ((offset + sg_len) > 0x10000)
2238 len = 0x10000 - offset;
2239
2240 ap->prd[idx].addr = cpu_to_le32(addr);
2241 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2242 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2243
2244 idx++;
2245 sg_len -= len;
2246 addr += len;
2247 }
2248 }
2249
2250 if (idx)
2251 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2252}
2253/**
2254 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2255 * @qc: Metadata associated with taskfile to check
2256 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002257 * Allow low-level driver to filter ATA PACKET commands, returning
2258 * a status indicating whether or not it is OK to use DMA for the
2259 * supplied PACKET command.
2260 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002262 * spin_lock_irqsave(host_set lock)
2263 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 * RETURNS: 0 when ATAPI DMA can be used
2265 * nonzero otherwise
2266 */
2267int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2268{
2269 struct ata_port *ap = qc->ap;
2270 int rc = 0; /* Assume ATAPI DMA is OK by default */
2271
2272 if (ap->ops->check_atapi_dma)
2273 rc = ap->ops->check_atapi_dma(qc);
2274
2275 return rc;
2276}
2277/**
2278 * ata_qc_prep - Prepare taskfile for submission
2279 * @qc: Metadata associated with taskfile to be prepared
2280 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002281 * Prepare ATA taskfile for submission.
2282 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 * LOCKING:
2284 * spin_lock_irqsave(host_set lock)
2285 */
2286void ata_qc_prep(struct ata_queued_cmd *qc)
2287{
2288 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2289 return;
2290
2291 ata_fill_sg(qc);
2292}
2293
Jeff Garzik0cba6322005-05-30 19:49:12 -04002294/**
2295 * ata_sg_init_one - Associate command with memory buffer
2296 * @qc: Command to be associated
2297 * @buf: Memory buffer
2298 * @buflen: Length of memory buffer, in bytes.
2299 *
2300 * Initialize the data-related elements of queued_cmd @qc
2301 * to point to a single memory buffer, @buf of byte length @buflen.
2302 *
2303 * LOCKING:
2304 * spin_lock_irqsave(host_set lock)
2305 */
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2308{
2309 struct scatterlist *sg;
2310
2311 qc->flags |= ATA_QCFLAG_SINGLE;
2312
2313 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002314 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002316 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 qc->buf_virt = buf;
2318
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002319 sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 sg->page = virt_to_page(buf);
2321 sg->offset = (unsigned long) buf & ~PAGE_MASK;
Albert Lee32529e02005-05-26 03:49:42 -04002322 sg->length = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Jeff Garzik0cba6322005-05-30 19:49:12 -04002325/**
2326 * ata_sg_init - Associate command with scatter-gather table.
2327 * @qc: Command to be associated
2328 * @sg: Scatter-gather table.
2329 * @n_elem: Number of elements in s/g table.
2330 *
2331 * Initialize the data-related elements of queued_cmd @qc
2332 * to point to a scatter-gather table @sg, containing @n_elem
2333 * elements.
2334 *
2335 * LOCKING:
2336 * spin_lock_irqsave(host_set lock)
2337 */
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2340 unsigned int n_elem)
2341{
2342 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002343 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002345 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
2348/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002349 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2350 * @qc: Command with memory buffer to be mapped.
2351 *
2352 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 *
2354 * LOCKING:
2355 * spin_lock_irqsave(host_set lock)
2356 *
2357 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002358 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 */
2360
2361static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2362{
2363 struct ata_port *ap = qc->ap;
2364 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002365 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 dma_addr_t dma_address;
2367
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002368 /* we must lengthen transfers to end on a 32-bit boundary */
2369 qc->pad_len = sg->length & 3;
2370 if (qc->pad_len) {
2371 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2372 struct scatterlist *psg = &qc->pad_sgent;
2373
2374 assert(qc->dev->class == ATA_DEV_ATAPI);
2375
2376 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2377
2378 if (qc->tf.flags & ATA_TFLAG_WRITE)
2379 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2380 qc->pad_len);
2381
2382 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2383 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2384 /* trim sg */
2385 sg->length -= qc->pad_len;
2386
2387 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2388 sg->length, qc->pad_len);
2389 }
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002392 sg->length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (dma_mapping_error(dma_address))
2394 return -1;
2395
2396 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002397 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2400 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2401
2402 return 0;
2403}
2404
2405/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002406 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2407 * @qc: Command with scatter-gather table to be mapped.
2408 *
2409 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 *
2411 * LOCKING:
2412 * spin_lock_irqsave(host_set lock)
2413 *
2414 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002415 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 *
2417 */
2418
2419static int ata_sg_setup(struct ata_queued_cmd *qc)
2420{
2421 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002422 struct scatterlist *sg = qc->__sg;
2423 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int n_elem, dir;
2425
2426 VPRINTK("ENTER, ata%u\n", ap->id);
2427 assert(qc->flags & ATA_QCFLAG_SG);
2428
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002429 /* we must lengthen transfers to end on a 32-bit boundary */
2430 qc->pad_len = lsg->length & 3;
2431 if (qc->pad_len) {
2432 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2433 struct scatterlist *psg = &qc->pad_sgent;
2434 unsigned int offset;
2435
2436 assert(qc->dev->class == ATA_DEV_ATAPI);
2437
2438 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2439
2440 /*
2441 * psg->page/offset are used to copy to-be-written
2442 * data in this function or read data in ata_sg_clean.
2443 */
2444 offset = lsg->offset + lsg->length - qc->pad_len;
2445 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2446 psg->offset = offset_in_page(offset);
2447
2448 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2449 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2450 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2451 kunmap_atomic(psg->page, KM_IRQ0);
2452 }
2453
2454 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2455 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2456 /* trim last sg */
2457 lsg->length -= qc->pad_len;
2458
2459 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2460 qc->n_elem - 1, lsg->length, qc->pad_len);
2461 }
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 dir = qc->dma_dir;
2464 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2465 if (n_elem < 1)
2466 return -1;
2467
2468 DPRINTK("%d sg elements mapped\n", n_elem);
2469
2470 qc->n_elem = n_elem;
2471
2472 return 0;
2473}
2474
2475/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002476 * ata_poll_qc_complete - turn irq back on and finish qc
2477 * @qc: Command to complete
2478 * @drv_stat: ATA status register content
2479 *
2480 * LOCKING:
2481 * None. (grabs host lock)
2482 */
2483
2484void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2485{
2486 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002487 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002488
Jeff Garzikb8f61532005-08-25 22:01:20 -04002489 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002490 ap->flags &= ~ATA_FLAG_NOINTR;
2491 ata_irq_on(ap);
2492 ata_qc_complete(qc, drv_stat);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002493 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002494}
2495
2496/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 * ata_pio_poll -
2498 * @ap:
2499 *
2500 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002501 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 *
2503 * RETURNS:
2504 *
2505 */
2506
2507static unsigned long ata_pio_poll(struct ata_port *ap)
2508{
2509 u8 status;
2510 unsigned int poll_state = PIO_ST_UNKNOWN;
2511 unsigned int reg_state = PIO_ST_UNKNOWN;
2512 const unsigned int tmout_state = PIO_ST_TMOUT;
2513
2514 switch (ap->pio_task_state) {
2515 case PIO_ST:
2516 case PIO_ST_POLL:
2517 poll_state = PIO_ST_POLL;
2518 reg_state = PIO_ST;
2519 break;
2520 case PIO_ST_LAST:
2521 case PIO_ST_LAST_POLL:
2522 poll_state = PIO_ST_LAST_POLL;
2523 reg_state = PIO_ST_LAST;
2524 break;
2525 default:
2526 BUG();
2527 break;
2528 }
2529
2530 status = ata_chk_status(ap);
2531 if (status & ATA_BUSY) {
2532 if (time_after(jiffies, ap->pio_task_timeout)) {
2533 ap->pio_task_state = tmout_state;
2534 return 0;
2535 }
2536 ap->pio_task_state = poll_state;
2537 return ATA_SHORT_PAUSE;
2538 }
2539
2540 ap->pio_task_state = reg_state;
2541 return 0;
2542}
2543
2544/**
2545 * ata_pio_complete -
2546 * @ap:
2547 *
2548 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002549 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002550 *
2551 * RETURNS:
2552 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 */
2554
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002555static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
2557 struct ata_queued_cmd *qc;
2558 u8 drv_stat;
2559
2560 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002561 * This is purely heuristic. This is a fast path. Sometimes when
2562 * we enter, BSY will be cleared in a chk-status or two. If not,
2563 * the drive is probably seeking or something. Snooze for a couple
2564 * msecs, then chk-status again. If still busy, fall back to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 * PIO_ST_POLL state.
2566 */
2567 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2568 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2569 msleep(2);
2570 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2571 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2572 ap->pio_task_state = PIO_ST_LAST_POLL;
2573 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576 }
2577
2578 drv_stat = ata_wait_idle(ap);
2579 if (!ata_ok(drv_stat)) {
2580 ap->pio_task_state = PIO_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583
2584 qc = ata_qc_from_tag(ap, ap->active_tag);
2585 assert(qc != NULL);
2586
2587 ap->pio_task_state = PIO_ST_IDLE;
2588
Tejun Heo40e8c822005-08-22 17:12:45 +09002589 ata_poll_qc_complete(qc, drv_stat);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002590
2591 /* another command may start at this point */
2592
2593 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Edward Falk0baab862005-06-02 18:17:13 -04002596
2597/**
2598 * swap_buf_le16 -
2599 * @buf: Buffer to swap
2600 * @buf_words: Number of 16-bit words in buffer.
2601 *
2602 * Swap halves of 16-bit words if needed to convert from
2603 * little-endian byte order to native cpu byte order, or
2604 * vice-versa.
2605 *
2606 * LOCKING:
2607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608void swap_buf_le16(u16 *buf, unsigned int buf_words)
2609{
2610#ifdef __BIG_ENDIAN
2611 unsigned int i;
2612
2613 for (i = 0; i < buf_words; i++)
2614 buf[i] = le16_to_cpu(buf[i]);
2615#endif /* __BIG_ENDIAN */
2616}
2617
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002618/**
2619 * ata_mmio_data_xfer - Transfer data by MMIO
2620 * @ap: port to read/write
2621 * @buf: data buffer
2622 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002623 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002624 *
2625 * Transfer data from/to the device data register by MMIO.
2626 *
2627 * LOCKING:
2628 * Inherited from caller.
2629 *
2630 */
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2633 unsigned int buflen, int write_data)
2634{
2635 unsigned int i;
2636 unsigned int words = buflen >> 1;
2637 u16 *buf16 = (u16 *) buf;
2638 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2639
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002640 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (write_data) {
2642 for (i = 0; i < words; i++)
2643 writew(le16_to_cpu(buf16[i]), mmio);
2644 } else {
2645 for (i = 0; i < words; i++)
2646 buf16[i] = cpu_to_le16(readw(mmio));
2647 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002648
2649 /* Transfer trailing 1 byte, if any. */
2650 if (unlikely(buflen & 0x01)) {
2651 u16 align_buf[1] = { 0 };
2652 unsigned char *trailing_buf = buf + buflen - 1;
2653
2654 if (write_data) {
2655 memcpy(align_buf, trailing_buf, 1);
2656 writew(le16_to_cpu(align_buf[0]), mmio);
2657 } else {
2658 align_buf[0] = cpu_to_le16(readw(mmio));
2659 memcpy(trailing_buf, align_buf, 1);
2660 }
2661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002664/**
2665 * ata_pio_data_xfer - Transfer data by PIO
2666 * @ap: port to read/write
2667 * @buf: data buffer
2668 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002669 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002670 *
2671 * Transfer data from/to the device data register by PIO.
2672 *
2673 * LOCKING:
2674 * Inherited from caller.
2675 *
2676 */
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2679 unsigned int buflen, int write_data)
2680{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002681 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002683 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002685 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002687 insw(ap->ioaddr.data_addr, buf, words);
2688
2689 /* Transfer trailing 1 byte, if any. */
2690 if (unlikely(buflen & 0x01)) {
2691 u16 align_buf[1] = { 0 };
2692 unsigned char *trailing_buf = buf + buflen - 1;
2693
2694 if (write_data) {
2695 memcpy(align_buf, trailing_buf, 1);
2696 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2697 } else {
2698 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2699 memcpy(trailing_buf, align_buf, 1);
2700 }
2701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002704/**
2705 * ata_data_xfer - Transfer data from/to the data register.
2706 * @ap: port to read/write
2707 * @buf: data buffer
2708 * @buflen: buffer length
2709 * @do_write: read/write
2710 *
2711 * Transfer data from/to the device data register.
2712 *
2713 * LOCKING:
2714 * Inherited from caller.
2715 *
2716 */
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2719 unsigned int buflen, int do_write)
2720{
2721 if (ap->flags & ATA_FLAG_MMIO)
2722 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2723 else
2724 ata_pio_data_xfer(ap, buf, buflen, do_write);
2725}
2726
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002727/**
2728 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2729 * @qc: Command on going
2730 *
2731 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2732 *
2733 * LOCKING:
2734 * Inherited from caller.
2735 */
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737static void ata_pio_sector(struct ata_queued_cmd *qc)
2738{
2739 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002740 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 struct ata_port *ap = qc->ap;
2742 struct page *page;
2743 unsigned int offset;
2744 unsigned char *buf;
2745
2746 if (qc->cursect == (qc->nsect - 1))
2747 ap->pio_task_state = PIO_ST_LAST;
2748
2749 page = sg[qc->cursg].page;
2750 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2751
2752 /* get the current page and offset */
2753 page = nth_page(page, (offset >> PAGE_SHIFT));
2754 offset %= PAGE_SIZE;
2755
2756 buf = kmap(page) + offset;
2757
2758 qc->cursect++;
2759 qc->cursg_ofs++;
2760
Albert Lee32529e02005-05-26 03:49:42 -04002761 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 qc->cursg++;
2763 qc->cursg_ofs = 0;
2764 }
2765
2766 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2767
2768 /* do the actual data transfer */
2769 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2770 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2771
2772 kunmap(page);
2773}
2774
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002775/**
2776 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2777 * @qc: Command on going
2778 * @bytes: number of bytes
2779 *
2780 * Transfer Transfer data from/to the ATAPI device.
2781 *
2782 * LOCKING:
2783 * Inherited from caller.
2784 *
2785 */
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2788{
2789 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002790 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 struct ata_port *ap = qc->ap;
2792 struct page *page;
2793 unsigned char *buf;
2794 unsigned int offset, count;
2795
Albert Lee563a6e12005-08-12 14:17:50 +08002796 if (qc->curbytes + bytes >= qc->nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 ap->pio_task_state = PIO_ST_LAST;
2798
2799next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08002800 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002801 /*
Albert Lee563a6e12005-08-12 14:17:50 +08002802 * The end of qc->sg is reached and the device expects
2803 * more data to transfer. In order not to overrun qc->sg
2804 * and fulfill length specified in the byte count register,
2805 * - for read case, discard trailing data from the device
2806 * - for write case, padding zero data to the device
2807 */
2808 u16 pad_buf[1] = { 0 };
2809 unsigned int words = bytes >> 1;
2810 unsigned int i;
2811
2812 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002813 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08002814 ap->id, bytes);
2815
2816 for (i = 0; i < words; i++)
2817 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2818
2819 ap->pio_task_state = PIO_ST_LAST;
2820 return;
2821 }
2822
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002823 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 page = sg->page;
2826 offset = sg->offset + qc->cursg_ofs;
2827
2828 /* get the current page and offset */
2829 page = nth_page(page, (offset >> PAGE_SHIFT));
2830 offset %= PAGE_SIZE;
2831
Albert Lee6952df02005-06-06 15:56:03 +08002832 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04002833 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 /* don't cross page boundaries */
2836 count = min(count, (unsigned int)PAGE_SIZE - offset);
2837
2838 buf = kmap(page) + offset;
2839
2840 bytes -= count;
2841 qc->curbytes += count;
2842 qc->cursg_ofs += count;
2843
Albert Lee32529e02005-05-26 03:49:42 -04002844 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 qc->cursg++;
2846 qc->cursg_ofs = 0;
2847 }
2848
2849 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2850
2851 /* do the actual data transfer */
2852 ata_data_xfer(ap, buf, count, do_write);
2853
2854 kunmap(page);
2855
Albert Lee563a6e12005-08-12 14:17:50 +08002856 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858}
2859
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002860/**
2861 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2862 * @qc: Command on going
2863 *
2864 * Transfer Transfer data from/to the ATAPI device.
2865 *
2866 * LOCKING:
2867 * Inherited from caller.
2868 *
2869 */
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2872{
2873 struct ata_port *ap = qc->ap;
2874 struct ata_device *dev = qc->dev;
2875 unsigned int ireason, bc_lo, bc_hi, bytes;
2876 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2877
2878 ap->ops->tf_read(ap, &qc->tf);
2879 ireason = qc->tf.nsect;
2880 bc_lo = qc->tf.lbam;
2881 bc_hi = qc->tf.lbah;
2882 bytes = (bc_hi << 8) | bc_lo;
2883
2884 /* shall be cleared to zero, indicating xfer of data */
2885 if (ireason & (1 << 0))
2886 goto err_out;
2887
2888 /* make sure transfer direction matches expected */
2889 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2890 if (do_write != i_write)
2891 goto err_out;
2892
2893 __atapi_pio_bytes(qc, bytes);
2894
2895 return;
2896
2897err_out:
2898 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2899 ap->id, dev->devno);
2900 ap->pio_task_state = PIO_ST_ERR;
2901}
2902
2903/**
2904 * ata_pio_sector -
2905 * @ap:
2906 *
2907 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002908 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 */
2910
2911static void ata_pio_block(struct ata_port *ap)
2912{
2913 struct ata_queued_cmd *qc;
2914 u8 status;
2915
2916 /*
2917 * This is purely hueristic. This is a fast path.
2918 * Sometimes when we enter, BSY will be cleared in
2919 * a chk-status or two. If not, the drive is probably seeking
2920 * or something. Snooze for a couple msecs, then
2921 * chk-status again. If still busy, fall back to
2922 * PIO_ST_POLL state.
2923 */
2924 status = ata_busy_wait(ap, ATA_BUSY, 5);
2925 if (status & ATA_BUSY) {
2926 msleep(2);
2927 status = ata_busy_wait(ap, ATA_BUSY, 10);
2928 if (status & ATA_BUSY) {
2929 ap->pio_task_state = PIO_ST_POLL;
2930 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2931 return;
2932 }
2933 }
2934
2935 qc = ata_qc_from_tag(ap, ap->active_tag);
2936 assert(qc != NULL);
2937
2938 if (is_atapi_taskfile(&qc->tf)) {
2939 /* no more data to transfer or unsupported ATAPI command */
2940 if ((status & ATA_DRQ) == 0) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002941 ap->pio_task_state = PIO_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return;
2943 }
2944
2945 atapi_pio_bytes(qc);
2946 } else {
2947 /* handle BSY=0, DRQ=0 as error */
2948 if ((status & ATA_DRQ) == 0) {
2949 ap->pio_task_state = PIO_ST_ERR;
2950 return;
2951 }
2952
2953 ata_pio_sector(qc);
2954 }
2955}
2956
2957static void ata_pio_error(struct ata_port *ap)
2958{
2959 struct ata_queued_cmd *qc;
2960 u8 drv_stat;
2961
2962 qc = ata_qc_from_tag(ap, ap->active_tag);
2963 assert(qc != NULL);
2964
2965 drv_stat = ata_chk_status(ap);
2966 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2967 ap->id, drv_stat);
2968
2969 ap->pio_task_state = PIO_ST_IDLE;
2970
Tejun Heo40e8c822005-08-22 17:12:45 +09002971 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
2974static void ata_pio_task(void *_data)
2975{
2976 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002977 unsigned long timeout;
2978 int qc_completed;
2979
2980fsm_start:
2981 timeout = 0;
2982 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 switch (ap->pio_task_state) {
2985 case PIO_ST_IDLE:
2986 return;
2987
2988 case PIO_ST:
2989 ata_pio_block(ap);
2990 break;
2991
2992 case PIO_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002993 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 break;
2995
2996 case PIO_ST_POLL:
2997 case PIO_ST_LAST_POLL:
2998 timeout = ata_pio_poll(ap);
2999 break;
3000
3001 case PIO_ST_TMOUT:
3002 case PIO_ST_ERR:
3003 ata_pio_error(ap);
3004 return;
3005 }
3006
3007 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003008 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3009 else if (!qc_completed)
3010 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
3013static void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
3014 struct scsi_cmnd *cmd)
3015{
3016 DECLARE_COMPLETION(wait);
3017 struct ata_queued_cmd *qc;
3018 unsigned long flags;
3019 int rc;
3020
3021 DPRINTK("ATAPI request sense\n");
3022
3023 qc = ata_qc_new_init(ap, dev);
3024 BUG_ON(qc == NULL);
3025
3026 /* FIXME: is this needed? */
3027 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
3028
3029 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
3030 qc->dma_dir = DMA_FROM_DEVICE;
3031
Albert Lee21b1ed72005-04-29 17:34:59 +08003032 memset(&qc->cdb, 0, ap->cdb_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 qc->cdb[0] = REQUEST_SENSE;
3034 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
3035
3036 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3037 qc->tf.command = ATA_CMD_PACKET;
3038
3039 qc->tf.protocol = ATA_PROT_ATAPI;
3040 qc->tf.lbam = (8 * 1024) & 0xff;
3041 qc->tf.lbah = (8 * 1024) >> 8;
3042 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
3043
3044 qc->waiting = &wait;
3045 qc->complete_fn = ata_qc_complete_noop;
3046
3047 spin_lock_irqsave(&ap->host_set->lock, flags);
3048 rc = ata_qc_issue(qc);
3049 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3050
3051 if (rc)
3052 ata_port_disable(ap);
3053 else
3054 wait_for_completion(&wait);
3055
3056 DPRINTK("EXIT\n");
3057}
3058
3059/**
3060 * ata_qc_timeout - Handle timeout of queued command
3061 * @qc: Command that timed out
3062 *
3063 * Some part of the kernel (currently, only the SCSI layer)
3064 * has noticed that the active command on port @ap has not
3065 * completed after a specified length of time. Handle this
3066 * condition by disabling DMA (if necessary) and completing
3067 * transactions, with error if necessary.
3068 *
3069 * This also handles the case of the "lost interrupt", where
3070 * for some reason (possibly hardware bug, possibly driver bug)
3071 * an interrupt was not delivered to the driver, even though the
3072 * transaction completed successfully.
3073 *
3074 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003075 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 */
3077
3078static void ata_qc_timeout(struct ata_queued_cmd *qc)
3079{
3080 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003081 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 struct ata_device *dev = qc->dev;
3083 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003084 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 DPRINTK("ENTER\n");
3087
3088 /* FIXME: doesn't this conflict with timeout handling? */
3089 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3090 struct scsi_cmnd *cmd = qc->scsicmd;
3091
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02003092 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094 /* finish completing original command */
Jeff Garzikb8f61532005-08-25 22:01:20 -04003095 spin_lock_irqsave(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 __ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003097 spin_unlock_irqrestore(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 atapi_request_sense(ap, dev, cmd);
3100
3101 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3102 scsi_finish_command(cmd);
3103
3104 goto out;
3105 }
3106 }
3107
Jeff Garzikb8f61532005-08-25 22:01:20 -04003108 spin_lock_irqsave(&host_set->lock, flags);
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 /* hack alert! We cannot use the supplied completion
3111 * function from inside the ->eh_strategy_handler() thread.
3112 * libata is the only user of ->eh_strategy_handler() in
3113 * any kernel, so the default scsi_done() assumes it is
3114 * not being called from the SCSI EH.
3115 */
3116 qc->scsidone = scsi_finish_command;
3117
3118 switch (qc->tf.protocol) {
3119
3120 case ATA_PROT_DMA:
3121 case ATA_PROT_ATAPI_DMA:
3122 host_stat = ap->ops->bmdma_status(ap);
3123
3124 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003125 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 /* fall through */
3128
3129 default:
3130 ata_altstatus(ap);
3131 drv_stat = ata_chk_status(ap);
3132
3133 /* ack bmdma irq events */
3134 ap->ops->irq_clear(ap);
3135
3136 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3137 ap->id, qc->tf.command, drv_stat, host_stat);
3138
3139 /* complete taskfile transaction */
3140 ata_qc_complete(qc, drv_stat);
3141 break;
3142 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003143
3144 spin_unlock_irqrestore(&host_set->lock, flags);
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146out:
3147 DPRINTK("EXIT\n");
3148}
3149
3150/**
3151 * ata_eng_timeout - Handle timeout of queued command
3152 * @ap: Port on which timed-out command is active
3153 *
3154 * Some part of the kernel (currently, only the SCSI layer)
3155 * has noticed that the active command on port @ap has not
3156 * completed after a specified length of time. Handle this
3157 * condition by disabling DMA (if necessary) and completing
3158 * transactions, with error if necessary.
3159 *
3160 * This also handles the case of the "lost interrupt", where
3161 * for some reason (possibly hardware bug, possibly driver bug)
3162 * an interrupt was not delivered to the driver, even though the
3163 * transaction completed successfully.
3164 *
3165 * LOCKING:
3166 * Inherited from SCSI layer (none, can sleep)
3167 */
3168
3169void ata_eng_timeout(struct ata_port *ap)
3170{
3171 struct ata_queued_cmd *qc;
3172
3173 DPRINTK("ENTER\n");
3174
3175 qc = ata_qc_from_tag(ap, ap->active_tag);
3176 if (!qc) {
3177 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3178 ap->id);
3179 goto out;
3180 }
3181
3182 ata_qc_timeout(qc);
3183
3184out:
3185 DPRINTK("EXIT\n");
3186}
3187
3188/**
3189 * ata_qc_new - Request an available ATA command, for queueing
3190 * @ap: Port associated with device @dev
3191 * @dev: Device from whom we request an available command structure
3192 *
3193 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003194 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 */
3196
3197static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3198{
3199 struct ata_queued_cmd *qc = NULL;
3200 unsigned int i;
3201
3202 for (i = 0; i < ATA_MAX_QUEUE; i++)
3203 if (!test_and_set_bit(i, &ap->qactive)) {
3204 qc = ata_qc_from_tag(ap, i);
3205 break;
3206 }
3207
3208 if (qc)
3209 qc->tag = i;
3210
3211 return qc;
3212}
3213
3214/**
3215 * ata_qc_new_init - Request an available ATA command, and initialize it
3216 * @ap: Port associated with device @dev
3217 * @dev: Device from whom we request an available command structure
3218 *
3219 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003220 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 */
3222
3223struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3224 struct ata_device *dev)
3225{
3226 struct ata_queued_cmd *qc;
3227
3228 qc = ata_qc_new(ap);
3229 if (qc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003230 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 qc->flags = 0;
3232 qc->scsicmd = NULL;
3233 qc->ap = ap;
3234 qc->dev = dev;
3235 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3236 qc->nsect = 0;
3237 qc->nbytes = qc->curbytes = 0;
3238
3239 ata_tf_init(ap, &qc->tf, dev->devno);
3240
3241 if (dev->flags & ATA_DFLAG_LBA48)
3242 qc->tf.flags |= ATA_TFLAG_LBA48;
3243 }
3244
3245 return qc;
3246}
3247
3248static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3249{
3250 return 0;
3251}
3252
3253static void __ata_qc_complete(struct ata_queued_cmd *qc)
3254{
3255 struct ata_port *ap = qc->ap;
3256 unsigned int tag, do_clear = 0;
3257
3258 qc->flags = 0;
3259 tag = qc->tag;
3260 if (likely(ata_tag_valid(tag))) {
3261 if (tag == ap->active_tag)
3262 ap->active_tag = ATA_TAG_POISON;
3263 qc->tag = ATA_TAG_POISON;
3264 do_clear = 1;
3265 }
3266
3267 if (qc->waiting) {
3268 struct completion *waiting = qc->waiting;
3269 qc->waiting = NULL;
3270 complete(waiting);
3271 }
3272
3273 if (likely(do_clear))
3274 clear_bit(tag, &ap->qactive);
3275}
3276
3277/**
3278 * ata_qc_free - free unused ata_queued_cmd
3279 * @qc: Command to complete
3280 *
3281 * Designed to free unused ata_queued_cmd object
3282 * in case something prevents using it.
3283 *
3284 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003285 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 *
3287 */
3288void ata_qc_free(struct ata_queued_cmd *qc)
3289{
3290 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3291 assert(qc->waiting == NULL); /* nothing should be waiting */
3292
3293 __ata_qc_complete(qc);
3294}
3295
3296/**
3297 * ata_qc_complete - Complete an active ATA command
3298 * @qc: Command to complete
Jeff Garzik0cba6322005-05-30 19:49:12 -04003299 * @drv_stat: ATA Status register contents
3300 *
3301 * Indicate to the mid and upper layers that an ATA
3302 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 *
3304 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003305 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 *
3307 */
3308
3309void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3310{
3311 int rc;
3312
3313 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3314 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3315
3316 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3317 ata_sg_clean(qc);
3318
Albert Lee3f3791d2005-08-16 14:25:38 +08003319 /* atapi: mark qc as inactive to prevent the interrupt handler
3320 * from completing the command twice later, before the error handler
3321 * is called. (when rc != 0 and atapi request sense is needed)
3322 */
3323 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 /* call completion callback */
3326 rc = qc->complete_fn(qc, drv_stat);
3327
3328 /* if callback indicates not to complete command (non-zero),
3329 * return immediately
3330 */
3331 if (rc != 0)
3332 return;
3333
3334 __ata_qc_complete(qc);
3335
3336 VPRINTK("EXIT\n");
3337}
3338
3339static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3340{
3341 struct ata_port *ap = qc->ap;
3342
3343 switch (qc->tf.protocol) {
3344 case ATA_PROT_DMA:
3345 case ATA_PROT_ATAPI_DMA:
3346 return 1;
3347
3348 case ATA_PROT_ATAPI:
3349 case ATA_PROT_PIO:
3350 case ATA_PROT_PIO_MULT:
3351 if (ap->flags & ATA_FLAG_PIO_DMA)
3352 return 1;
3353
3354 /* fall through */
3355
3356 default:
3357 return 0;
3358 }
3359
3360 /* never reached */
3361}
3362
3363/**
3364 * ata_qc_issue - issue taskfile to device
3365 * @qc: command to issue to device
3366 *
3367 * Prepare an ATA command to submission to device.
3368 * This includes mapping the data into a DMA-able
3369 * area, filling in the S/G table, and finally
3370 * writing the taskfile to hardware, starting the command.
3371 *
3372 * LOCKING:
3373 * spin_lock_irqsave(host_set lock)
3374 *
3375 * RETURNS:
3376 * Zero on success, negative on error.
3377 */
3378
3379int ata_qc_issue(struct ata_queued_cmd *qc)
3380{
3381 struct ata_port *ap = qc->ap;
3382
3383 if (ata_should_dma_map(qc)) {
3384 if (qc->flags & ATA_QCFLAG_SG) {
3385 if (ata_sg_setup(qc))
3386 goto err_out;
3387 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3388 if (ata_sg_setup_one(qc))
3389 goto err_out;
3390 }
3391 } else {
3392 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3393 }
3394
3395 ap->ops->qc_prep(qc);
3396
3397 qc->ap->active_tag = qc->tag;
3398 qc->flags |= ATA_QCFLAG_ACTIVE;
3399
3400 return ap->ops->qc_issue(qc);
3401
3402err_out:
3403 return -1;
3404}
3405
Edward Falk0baab862005-06-02 18:17:13 -04003406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407/**
3408 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3409 * @qc: command to issue to device
3410 *
3411 * Using various libata functions and hooks, this function
3412 * starts an ATA command. ATA commands are grouped into
3413 * classes called "protocols", and issuing each type of protocol
3414 * is slightly different.
3415 *
Edward Falk0baab862005-06-02 18:17:13 -04003416 * May be used as the qc_issue() entry in ata_port_operations.
3417 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 * LOCKING:
3419 * spin_lock_irqsave(host_set lock)
3420 *
3421 * RETURNS:
3422 * Zero on success, negative on error.
3423 */
3424
3425int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3426{
3427 struct ata_port *ap = qc->ap;
3428
3429 ata_dev_select(ap, qc->dev->devno, 1, 0);
3430
3431 switch (qc->tf.protocol) {
3432 case ATA_PROT_NODATA:
3433 ata_tf_to_host_nolock(ap, &qc->tf);
3434 break;
3435
3436 case ATA_PROT_DMA:
3437 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3438 ap->ops->bmdma_setup(qc); /* set up bmdma */
3439 ap->ops->bmdma_start(qc); /* initiate bmdma */
3440 break;
3441
3442 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3443 ata_qc_set_polling(qc);
3444 ata_tf_to_host_nolock(ap, &qc->tf);
3445 ap->pio_task_state = PIO_ST;
3446 queue_work(ata_wq, &ap->pio_task);
3447 break;
3448
3449 case ATA_PROT_ATAPI:
3450 ata_qc_set_polling(qc);
3451 ata_tf_to_host_nolock(ap, &qc->tf);
3452 queue_work(ata_wq, &ap->packet_task);
3453 break;
3454
3455 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003456 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 ata_tf_to_host_nolock(ap, &qc->tf);
3458 queue_work(ata_wq, &ap->packet_task);
3459 break;
3460
3461 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003462 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3464 ap->ops->bmdma_setup(qc); /* set up bmdma */
3465 queue_work(ata_wq, &ap->packet_task);
3466 break;
3467
3468 default:
3469 WARN_ON(1);
3470 return -1;
3471 }
3472
3473 return 0;
3474}
3475
3476/**
Edward Falk0baab862005-06-02 18:17:13 -04003477 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 * @qc: Info associated with this ATA transaction.
3479 *
3480 * LOCKING:
3481 * spin_lock_irqsave(host_set lock)
3482 */
3483
3484static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3485{
3486 struct ata_port *ap = qc->ap;
3487 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3488 u8 dmactl;
3489 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3490
3491 /* load PRD table addr. */
3492 mb(); /* make sure PRD table writes are visible to controller */
3493 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3494
3495 /* specify data direction, triple-check start bit is clear */
3496 dmactl = readb(mmio + ATA_DMA_CMD);
3497 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3498 if (!rw)
3499 dmactl |= ATA_DMA_WR;
3500 writeb(dmactl, mmio + ATA_DMA_CMD);
3501
3502 /* issue r/w command */
3503 ap->ops->exec_command(ap, &qc->tf);
3504}
3505
3506/**
Alan Coxb73fc892005-08-26 16:03:19 +01003507 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 * @qc: Info associated with this ATA transaction.
3509 *
3510 * LOCKING:
3511 * spin_lock_irqsave(host_set lock)
3512 */
3513
3514static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3515{
3516 struct ata_port *ap = qc->ap;
3517 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3518 u8 dmactl;
3519
3520 /* start host DMA transaction */
3521 dmactl = readb(mmio + ATA_DMA_CMD);
3522 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3523
3524 /* Strictly, one may wish to issue a readb() here, to
3525 * flush the mmio write. However, control also passes
3526 * to the hardware at this point, and it will interrupt
3527 * us when we are to resume control. So, in effect,
3528 * we don't care when the mmio write flushes.
3529 * Further, a read of the DMA status register _immediately_
3530 * following the write may not be what certain flaky hardware
3531 * is expected, so I think it is best to not add a readb()
3532 * without first all the MMIO ATA cards/mobos.
3533 * Or maybe I'm just being paranoid.
3534 */
3535}
3536
3537/**
3538 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3539 * @qc: Info associated with this ATA transaction.
3540 *
3541 * LOCKING:
3542 * spin_lock_irqsave(host_set lock)
3543 */
3544
3545static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3546{
3547 struct ata_port *ap = qc->ap;
3548 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3549 u8 dmactl;
3550
3551 /* load PRD table addr. */
3552 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3553
3554 /* specify data direction, triple-check start bit is clear */
3555 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3556 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3557 if (!rw)
3558 dmactl |= ATA_DMA_WR;
3559 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3560
3561 /* issue r/w command */
3562 ap->ops->exec_command(ap, &qc->tf);
3563}
3564
3565/**
3566 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3567 * @qc: Info associated with this ATA transaction.
3568 *
3569 * LOCKING:
3570 * spin_lock_irqsave(host_set lock)
3571 */
3572
3573static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3574{
3575 struct ata_port *ap = qc->ap;
3576 u8 dmactl;
3577
3578 /* start host DMA transaction */
3579 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3580 outb(dmactl | ATA_DMA_START,
3581 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3582}
3583
Edward Falk0baab862005-06-02 18:17:13 -04003584
3585/**
3586 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3587 * @qc: Info associated with this ATA transaction.
3588 *
3589 * Writes the ATA_DMA_START flag to the DMA command register.
3590 *
3591 * May be used as the bmdma_start() entry in ata_port_operations.
3592 *
3593 * LOCKING:
3594 * spin_lock_irqsave(host_set lock)
3595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596void ata_bmdma_start(struct ata_queued_cmd *qc)
3597{
3598 if (qc->ap->flags & ATA_FLAG_MMIO)
3599 ata_bmdma_start_mmio(qc);
3600 else
3601 ata_bmdma_start_pio(qc);
3602}
3603
Edward Falk0baab862005-06-02 18:17:13 -04003604
3605/**
3606 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3607 * @qc: Info associated with this ATA transaction.
3608 *
3609 * Writes address of PRD table to device's PRD Table Address
3610 * register, sets the DMA control register, and calls
3611 * ops->exec_command() to start the transfer.
3612 *
3613 * May be used as the bmdma_setup() entry in ata_port_operations.
3614 *
3615 * LOCKING:
3616 * spin_lock_irqsave(host_set lock)
3617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618void ata_bmdma_setup(struct ata_queued_cmd *qc)
3619{
3620 if (qc->ap->flags & ATA_FLAG_MMIO)
3621 ata_bmdma_setup_mmio(qc);
3622 else
3623 ata_bmdma_setup_pio(qc);
3624}
3625
Edward Falk0baab862005-06-02 18:17:13 -04003626
3627/**
3628 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003629 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003630 *
3631 * Clear interrupt and error flags in DMA status register.
3632 *
3633 * May be used as the irq_clear() entry in ata_port_operations.
3634 *
3635 * LOCKING:
3636 * spin_lock_irqsave(host_set lock)
3637 */
3638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639void ata_bmdma_irq_clear(struct ata_port *ap)
3640{
3641 if (ap->flags & ATA_FLAG_MMIO) {
3642 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3643 writeb(readb(mmio), mmio);
3644 } else {
3645 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3646 outb(inb(addr), addr);
3647 }
3648
3649}
3650
Edward Falk0baab862005-06-02 18:17:13 -04003651
3652/**
3653 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003654 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003655 *
3656 * Read and return BMDMA status register.
3657 *
3658 * May be used as the bmdma_status() entry in ata_port_operations.
3659 *
3660 * LOCKING:
3661 * spin_lock_irqsave(host_set lock)
3662 */
3663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664u8 ata_bmdma_status(struct ata_port *ap)
3665{
3666 u8 host_stat;
3667 if (ap->flags & ATA_FLAG_MMIO) {
3668 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3669 host_stat = readb(mmio + ATA_DMA_STATUS);
3670 } else
3671 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3672 return host_stat;
3673}
3674
Edward Falk0baab862005-06-02 18:17:13 -04003675
3676/**
3677 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003678 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003679 *
3680 * Clears the ATA_DMA_START flag in the dma control register
3681 *
3682 * May be used as the bmdma_stop() entry in ata_port_operations.
3683 *
3684 * LOCKING:
3685 * spin_lock_irqsave(host_set lock)
3686 */
3687
Alan Coxb73fc892005-08-26 16:03:19 +01003688void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
Alan Coxb73fc892005-08-26 16:03:19 +01003690 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 if (ap->flags & ATA_FLAG_MMIO) {
3692 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3693
3694 /* clear start/stop bit */
3695 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3696 mmio + ATA_DMA_CMD);
3697 } else {
3698 /* clear start/stop bit */
3699 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3700 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3701 }
3702
3703 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3704 ata_altstatus(ap); /* dummy read */
3705}
3706
3707/**
3708 * ata_host_intr - Handle host interrupt for given (port, task)
3709 * @ap: Port on which interrupt arrived (possibly...)
3710 * @qc: Taskfile currently active in engine
3711 *
3712 * Handle host interrupt for given queued command. Currently,
3713 * only DMA interrupts are handled. All other commands are
3714 * handled via polling with interrupts disabled (nIEN bit).
3715 *
3716 * LOCKING:
3717 * spin_lock_irqsave(host_set lock)
3718 *
3719 * RETURNS:
3720 * One if interrupt was handled, zero if not (shared irq).
3721 */
3722
3723inline unsigned int ata_host_intr (struct ata_port *ap,
3724 struct ata_queued_cmd *qc)
3725{
3726 u8 status, host_stat;
3727
3728 switch (qc->tf.protocol) {
3729
3730 case ATA_PROT_DMA:
3731 case ATA_PROT_ATAPI_DMA:
3732 case ATA_PROT_ATAPI:
3733 /* check status of DMA engine */
3734 host_stat = ap->ops->bmdma_status(ap);
3735 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3736
3737 /* if it's not our irq... */
3738 if (!(host_stat & ATA_DMA_INTR))
3739 goto idle_irq;
3740
3741 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003742 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
3744 /* fall through */
3745
3746 case ATA_PROT_ATAPI_NODATA:
3747 case ATA_PROT_NODATA:
3748 /* check altstatus */
3749 status = ata_altstatus(ap);
3750 if (status & ATA_BUSY)
3751 goto idle_irq;
3752
3753 /* check main status, clearing INTRQ */
3754 status = ata_chk_status(ap);
3755 if (unlikely(status & ATA_BUSY))
3756 goto idle_irq;
3757 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3758 ap->id, qc->tf.protocol, status);
3759
3760 /* ack bmdma irq events */
3761 ap->ops->irq_clear(ap);
3762
3763 /* complete taskfile transaction */
3764 ata_qc_complete(qc, status);
3765 break;
3766
3767 default:
3768 goto idle_irq;
3769 }
3770
3771 return 1; /* irq handled */
3772
3773idle_irq:
3774 ap->stats.idle_irq++;
3775
3776#ifdef ATA_IRQ_TRAP
3777 if ((ap->stats.idle_irq % 1000) == 0) {
3778 handled = 1;
3779 ata_irq_ack(ap, 0); /* debug trap */
3780 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3781 }
3782#endif
3783 return 0; /* irq not handled */
3784}
3785
3786/**
3787 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04003788 * @irq: irq line (unused)
3789 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 * @regs: unused
3791 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04003792 * Default interrupt handler for PCI IDE devices. Calls
3793 * ata_host_intr() for each port that is not disabled.
3794 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003796 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 *
3798 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003799 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 *
3801 */
3802
3803irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3804{
3805 struct ata_host_set *host_set = dev_instance;
3806 unsigned int i;
3807 unsigned int handled = 0;
3808 unsigned long flags;
3809
3810 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3811 spin_lock_irqsave(&host_set->lock, flags);
3812
3813 for (i = 0; i < host_set->n_ports; i++) {
3814 struct ata_port *ap;
3815
3816 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09003817 if (ap &&
3818 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 struct ata_queued_cmd *qc;
3820
3821 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08003822 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3823 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 handled |= ata_host_intr(ap, qc);
3825 }
3826 }
3827
3828 spin_unlock_irqrestore(&host_set->lock, flags);
3829
3830 return IRQ_RETVAL(handled);
3831}
3832
3833/**
3834 * atapi_packet_task - Write CDB bytes to hardware
3835 * @_data: Port to which ATAPI device is attached.
3836 *
3837 * When device has indicated its readiness to accept
3838 * a CDB, this function is called. Send the CDB.
3839 * If DMA is to be performed, exit immediately.
3840 * Otherwise, we are in polling mode, so poll
3841 * status under operation succeeds or fails.
3842 *
3843 * LOCKING:
3844 * Kernel thread context (may sleep)
3845 */
3846
3847static void atapi_packet_task(void *_data)
3848{
3849 struct ata_port *ap = _data;
3850 struct ata_queued_cmd *qc;
3851 u8 status;
3852
3853 qc = ata_qc_from_tag(ap, ap->active_tag);
3854 assert(qc != NULL);
3855 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3856
3857 /* sleep-wait for BSY to clear */
3858 DPRINTK("busy wait\n");
3859 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3860 goto err_out;
3861
3862 /* make sure DRQ is set */
3863 status = ata_chk_status(ap);
3864 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3865 goto err_out;
3866
3867 /* send SCSI cdb */
3868 DPRINTK("send cdb\n");
3869 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Tejun Heoc1389502005-08-22 14:59:24 +09003871 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3872 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3873 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Tejun Heoc1389502005-08-22 14:59:24 +09003875 /* Once we're done issuing command and kicking bmdma,
3876 * irq handler takes over. To not lose irq, we need
3877 * to clear NOINTR flag before sending cdb, but
3878 * interrupt handler shouldn't be invoked before we're
3879 * finished. Hence, the following locking.
3880 */
3881 spin_lock_irqsave(&ap->host_set->lock, flags);
3882 ap->flags &= ~ATA_FLAG_NOINTR;
3883 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3884 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3885 ap->ops->bmdma_start(qc); /* initiate bmdma */
3886 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3887 } else {
3888 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Tejun Heoc1389502005-08-22 14:59:24 +09003890 /* PIO commands are handled by polling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 ap->pio_task_state = PIO_ST;
3892 queue_work(ata_wq, &ap->pio_task);
3893 }
3894
3895 return;
3896
3897err_out:
Tejun Heo40e8c822005-08-22 17:12:45 +09003898 ata_poll_qc_complete(qc, ATA_ERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899}
3900
Edward Falk0baab862005-06-02 18:17:13 -04003901
3902/**
3903 * ata_port_start - Set port up for dma.
3904 * @ap: Port to initialize
3905 *
3906 * Called just after data structures for each port are
3907 * initialized. Allocates space for PRD table.
3908 *
3909 * May be used as the port_start() entry in ata_port_operations.
3910 *
3911 * LOCKING:
3912 */
3913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914int ata_port_start (struct ata_port *ap)
3915{
3916 struct device *dev = ap->host_set->dev;
3917
3918 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
3919 if (!ap->prd)
3920 return -ENOMEM;
3921
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003922 ap->pad = dma_alloc_coherent(dev, ATA_DMA_PAD_BUF_SZ, &ap->pad_dma, GFP_KERNEL);
3923 if (!ap->pad) {
3924 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
3925 return -ENOMEM;
3926 }
3927
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
3929
3930 return 0;
3931}
3932
Edward Falk0baab862005-06-02 18:17:13 -04003933
3934/**
3935 * ata_port_stop - Undo ata_port_start()
3936 * @ap: Port to shut down
3937 *
3938 * Frees the PRD table.
3939 *
3940 * May be used as the port_stop() entry in ata_port_operations.
3941 *
3942 * LOCKING:
3943 */
3944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945void ata_port_stop (struct ata_port *ap)
3946{
3947 struct device *dev = ap->host_set->dev;
3948
3949 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003950 dma_free_coherent(dev, ATA_DMA_PAD_BUF_SZ, ap->pad, ap->pad_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951}
3952
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04003953void ata_host_stop (struct ata_host_set *host_set)
3954{
3955 if (host_set->mmio_base)
3956 iounmap(host_set->mmio_base);
3957}
3958
3959
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960/**
3961 * ata_host_remove - Unregister SCSI host structure with upper layers
3962 * @ap: Port to unregister
3963 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3964 *
3965 * LOCKING:
3966 */
3967
3968static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
3969{
3970 struct Scsi_Host *sh = ap->host;
3971
3972 DPRINTK("ENTER\n");
3973
3974 if (do_unregister)
3975 scsi_remove_host(sh);
3976
3977 ap->ops->port_stop(ap);
3978}
3979
3980/**
3981 * ata_host_init - Initialize an ata_port structure
3982 * @ap: Structure to initialize
3983 * @host: associated SCSI mid-layer structure
3984 * @host_set: Collection of hosts to which @ap belongs
3985 * @ent: Probe information provided by low-level driver
3986 * @port_no: Port number associated with this ata_port
3987 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04003988 * Initialize a new ata_port structure, and its associated
3989 * scsi_host.
3990 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003992 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 *
3994 */
3995
3996static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
3997 struct ata_host_set *host_set,
3998 struct ata_probe_ent *ent, unsigned int port_no)
3999{
4000 unsigned int i;
4001
4002 host->max_id = 16;
4003 host->max_lun = 1;
4004 host->max_channel = 1;
4005 host->unique_id = ata_unique_id++;
4006 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 scsi_assign_lock(host, &host_set->lock);
4009
4010 ap->flags = ATA_FLAG_PORT_DISABLED;
4011 ap->id = host->unique_id;
4012 ap->host = host;
4013 ap->ctl = ATA_DEVCTL_OBS;
4014 ap->host_set = host_set;
4015 ap->port_no = port_no;
4016 ap->hard_port_no =
4017 ent->legacy_mode ? ent->hard_port_no : port_no;
4018 ap->pio_mask = ent->pio_mask;
4019 ap->mwdma_mask = ent->mwdma_mask;
4020 ap->udma_mask = ent->udma_mask;
4021 ap->flags |= ent->host_flags;
4022 ap->ops = ent->port_ops;
4023 ap->cbl = ATA_CBL_NONE;
4024 ap->active_tag = ATA_TAG_POISON;
4025 ap->last_ctl = 0xFF;
4026
4027 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4028 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4029
4030 for (i = 0; i < ATA_MAX_DEVICES; i++)
4031 ap->device[i].devno = i;
4032
4033#ifdef ATA_IRQ_TRAP
4034 ap->stats.unhandled_irq = 1;
4035 ap->stats.idle_irq = 1;
4036#endif
4037
4038 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4039}
4040
4041/**
4042 * ata_host_add - Attach low-level ATA driver to system
4043 * @ent: Information provided by low-level driver
4044 * @host_set: Collections of ports to which we add
4045 * @port_no: Port number associated with this host
4046 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004047 * Attach low-level ATA driver to system.
4048 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004050 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 *
4052 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004053 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 *
4055 */
4056
4057static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4058 struct ata_host_set *host_set,
4059 unsigned int port_no)
4060{
4061 struct Scsi_Host *host;
4062 struct ata_port *ap;
4063 int rc;
4064
4065 DPRINTK("ENTER\n");
4066 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4067 if (!host)
4068 return NULL;
4069
4070 ap = (struct ata_port *) &host->hostdata[0];
4071
4072 ata_host_init(ap, host, host_set, ent, port_no);
4073
4074 rc = ap->ops->port_start(ap);
4075 if (rc)
4076 goto err_out;
4077
4078 return ap;
4079
4080err_out:
4081 scsi_host_put(host);
4082 return NULL;
4083}
4084
4085/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004086 * ata_device_add - Register hardware device with ATA and SCSI layers
4087 * @ent: Probe information describing hardware device to be registered
4088 *
4089 * This function processes the information provided in the probe
4090 * information struct @ent, allocates the necessary ATA and SCSI
4091 * host information structures, initializes them, and registers
4092 * everything with requisite kernel subsystems.
4093 *
4094 * This function requests irqs, probes the ATA bus, and probes
4095 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 *
4097 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004098 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 *
4100 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004101 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 *
4103 */
4104
4105int ata_device_add(struct ata_probe_ent *ent)
4106{
4107 unsigned int count = 0, i;
4108 struct device *dev = ent->dev;
4109 struct ata_host_set *host_set;
4110
4111 DPRINTK("ENTER\n");
4112 /* alloc a container for our list of ATA ports (buses) */
4113 host_set = kmalloc(sizeof(struct ata_host_set) +
4114 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4115 if (!host_set)
4116 return 0;
4117 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4118 spin_lock_init(&host_set->lock);
4119
4120 host_set->dev = dev;
4121 host_set->n_ports = ent->n_ports;
4122 host_set->irq = ent->irq;
4123 host_set->mmio_base = ent->mmio_base;
4124 host_set->private_data = ent->private_data;
4125 host_set->ops = ent->port_ops;
4126
4127 /* register each port bound to this device */
4128 for (i = 0; i < ent->n_ports; i++) {
4129 struct ata_port *ap;
4130 unsigned long xfer_mode_mask;
4131
4132 ap = ata_host_add(ent, host_set, i);
4133 if (!ap)
4134 goto err_out;
4135
4136 host_set->ports[i] = ap;
4137 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4138 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4139 (ap->pio_mask << ATA_SHIFT_PIO);
4140
4141 /* print per-port info to dmesg */
4142 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4143 "bmdma 0x%lX irq %lu\n",
4144 ap->id,
4145 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4146 ata_mode_string(xfer_mode_mask),
4147 ap->ioaddr.cmd_addr,
4148 ap->ioaddr.ctl_addr,
4149 ap->ioaddr.bmdma_addr,
4150 ent->irq);
4151
4152 ata_chk_status(ap);
4153 host_set->ops->irq_clear(ap);
4154 count++;
4155 }
4156
4157 if (!count) {
4158 kfree(host_set);
4159 return 0;
4160 }
4161
4162 /* obtain irq, that is shared between channels */
4163 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4164 DRV_NAME, host_set))
4165 goto err_out;
4166
4167 /* perform each probe synchronously */
4168 DPRINTK("probe begin\n");
4169 for (i = 0; i < count; i++) {
4170 struct ata_port *ap;
4171 int rc;
4172
4173 ap = host_set->ports[i];
4174
4175 DPRINTK("ata%u: probe begin\n", ap->id);
4176 rc = ata_bus_probe(ap);
4177 DPRINTK("ata%u: probe end\n", ap->id);
4178
4179 if (rc) {
4180 /* FIXME: do something useful here?
4181 * Current libata behavior will
4182 * tear down everything when
4183 * the module is removed
4184 * or the h/w is unplugged.
4185 */
4186 }
4187
4188 rc = scsi_add_host(ap->host, dev);
4189 if (rc) {
4190 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4191 ap->id);
4192 /* FIXME: do something useful here */
4193 /* FIXME: handle unconditional calls to
4194 * scsi_scan_host and ata_host_remove, below,
4195 * at the very least
4196 */
4197 }
4198 }
4199
4200 /* probes are done, now scan each port's disk(s) */
4201 DPRINTK("probe begin\n");
4202 for (i = 0; i < count; i++) {
4203 struct ata_port *ap = host_set->ports[i];
4204
4205 scsi_scan_host(ap->host);
4206 }
4207
4208 dev_set_drvdata(dev, host_set);
4209
4210 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4211 return ent->n_ports; /* success */
4212
4213err_out:
4214 for (i = 0; i < count; i++) {
4215 ata_host_remove(host_set->ports[i], 1);
4216 scsi_host_put(host_set->ports[i]->host);
4217 }
4218 kfree(host_set);
4219 VPRINTK("EXIT, returning 0\n");
4220 return 0;
4221}
4222
4223/**
Alan Cox17b14452005-09-15 15:44:00 +01004224 * ata_host_set_remove - PCI layer callback for device removal
4225 * @host_set: ATA host set that was removed
4226 *
4227 * Unregister all objects associated with this host set. Free those
4228 * objects.
4229 *
4230 * LOCKING:
4231 * Inherited from calling layer (may sleep).
4232 */
4233
4234
4235void ata_host_set_remove(struct ata_host_set *host_set)
4236{
4237 struct ata_port *ap;
4238 unsigned int i;
4239
4240 for (i = 0; i < host_set->n_ports; i++) {
4241 ap = host_set->ports[i];
4242 scsi_remove_host(ap->host);
4243 }
4244
4245 free_irq(host_set->irq, host_set);
4246
4247 for (i = 0; i < host_set->n_ports; i++) {
4248 ap = host_set->ports[i];
4249
4250 ata_scsi_release(ap->host);
4251
4252 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4253 struct ata_ioports *ioaddr = &ap->ioaddr;
4254
4255 if (ioaddr->cmd_addr == 0x1f0)
4256 release_region(0x1f0, 8);
4257 else if (ioaddr->cmd_addr == 0x170)
4258 release_region(0x170, 8);
4259 }
4260
4261 scsi_host_put(ap->host);
4262 }
4263
4264 if (host_set->ops->host_stop)
4265 host_set->ops->host_stop(host_set);
4266
4267 kfree(host_set);
4268}
4269
4270/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 * ata_scsi_release - SCSI layer callback hook for host unload
4272 * @host: libata host to be unloaded
4273 *
4274 * Performs all duties necessary to shut down a libata port...
4275 * Kill port kthread, disable port, and release resources.
4276 *
4277 * LOCKING:
4278 * Inherited from SCSI layer.
4279 *
4280 * RETURNS:
4281 * One.
4282 */
4283
4284int ata_scsi_release(struct Scsi_Host *host)
4285{
4286 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4287
4288 DPRINTK("ENTER\n");
4289
4290 ap->ops->port_disable(ap);
4291 ata_host_remove(ap, 0);
4292
4293 DPRINTK("EXIT\n");
4294 return 1;
4295}
4296
4297/**
4298 * ata_std_ports - initialize ioaddr with standard port offsets.
4299 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004300 *
4301 * Utility function which initializes data_addr, error_addr,
4302 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4303 * device_addr, status_addr, and command_addr to standard offsets
4304 * relative to cmd_addr.
4305 *
4306 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 */
Edward Falk0baab862005-06-02 18:17:13 -04004308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309void ata_std_ports(struct ata_ioports *ioaddr)
4310{
4311 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4312 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4313 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4314 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4315 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4316 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4317 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4318 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4319 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4320 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4321}
4322
4323static struct ata_probe_ent *
4324ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4325{
4326 struct ata_probe_ent *probe_ent;
4327
4328 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4329 if (!probe_ent) {
4330 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4331 kobject_name(&(dev->kobj)));
4332 return NULL;
4333 }
4334
4335 memset(probe_ent, 0, sizeof(*probe_ent));
4336
4337 INIT_LIST_HEAD(&probe_ent->node);
4338 probe_ent->dev = dev;
4339
4340 probe_ent->sht = port->sht;
4341 probe_ent->host_flags = port->host_flags;
4342 probe_ent->pio_mask = port->pio_mask;
4343 probe_ent->mwdma_mask = port->mwdma_mask;
4344 probe_ent->udma_mask = port->udma_mask;
4345 probe_ent->port_ops = port->port_ops;
4346
4347 return probe_ent;
4348}
4349
Edward Falk0baab862005-06-02 18:17:13 -04004350
4351
Jeff Garzik374b1872005-08-30 05:42:52 -04004352#ifdef CONFIG_PCI
4353
4354void ata_pci_host_stop (struct ata_host_set *host_set)
4355{
4356 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4357
4358 pci_iounmap(pdev, host_set->mmio_base);
4359}
4360
Edward Falk0baab862005-06-02 18:17:13 -04004361/**
4362 * ata_pci_init_native_mode - Initialize native-mode driver
4363 * @pdev: pci device to be initialized
4364 * @port: array[2] of pointers to port info structures.
4365 *
4366 * Utility function which allocates and initializes an
4367 * ata_probe_ent structure for a standard dual-port
4368 * PIO-based IDE controller. The returned ata_probe_ent
4369 * structure can be passed to ata_device_add(). The returned
4370 * ata_probe_ent structure should then be freed with kfree().
4371 */
4372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373struct ata_probe_ent *
4374ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port)
4375{
4376 struct ata_probe_ent *probe_ent =
4377 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4378 if (!probe_ent)
4379 return NULL;
4380
4381 probe_ent->n_ports = 2;
4382 probe_ent->irq = pdev->irq;
4383 probe_ent->irq_flags = SA_SHIRQ;
4384
4385 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
4386 probe_ent->port[0].altstatus_addr =
4387 probe_ent->port[0].ctl_addr =
4388 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4389 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4390
4391 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
4392 probe_ent->port[1].altstatus_addr =
4393 probe_ent->port[1].ctl_addr =
4394 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4395 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4396
4397 ata_std_ports(&probe_ent->port[0]);
4398 ata_std_ports(&probe_ent->port[1]);
4399
4400 return probe_ent;
4401}
4402
4403static struct ata_probe_ent *
4404ata_pci_init_legacy_mode(struct pci_dev *pdev, struct ata_port_info **port,
4405 struct ata_probe_ent **ppe2)
4406{
4407 struct ata_probe_ent *probe_ent, *probe_ent2;
4408
4409 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4410 if (!probe_ent)
4411 return NULL;
4412 probe_ent2 = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[1]);
4413 if (!probe_ent2) {
4414 kfree(probe_ent);
4415 return NULL;
4416 }
4417
4418 probe_ent->n_ports = 1;
4419 probe_ent->irq = 14;
4420
4421 probe_ent->hard_port_no = 0;
4422 probe_ent->legacy_mode = 1;
4423
4424 probe_ent2->n_ports = 1;
4425 probe_ent2->irq = 15;
4426
4427 probe_ent2->hard_port_no = 1;
4428 probe_ent2->legacy_mode = 1;
4429
4430 probe_ent->port[0].cmd_addr = 0x1f0;
4431 probe_ent->port[0].altstatus_addr =
4432 probe_ent->port[0].ctl_addr = 0x3f6;
4433 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4434
4435 probe_ent2->port[0].cmd_addr = 0x170;
4436 probe_ent2->port[0].altstatus_addr =
4437 probe_ent2->port[0].ctl_addr = 0x376;
4438 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
4439
4440 ata_std_ports(&probe_ent->port[0]);
4441 ata_std_ports(&probe_ent2->port[0]);
4442
4443 *ppe2 = probe_ent2;
4444 return probe_ent;
4445}
4446
4447/**
4448 * ata_pci_init_one - Initialize/register PCI IDE host controller
4449 * @pdev: Controller to be initialized
4450 * @port_info: Information from low-level host driver
4451 * @n_ports: Number of ports attached to host controller
4452 *
Edward Falk0baab862005-06-02 18:17:13 -04004453 * This is a helper function which can be called from a driver's
4454 * xxx_init_one() probe function if the hardware uses traditional
4455 * IDE taskfile registers.
4456 *
4457 * This function calls pci_enable_device(), reserves its register
4458 * regions, sets the dma mask, enables bus master mode, and calls
4459 * ata_device_add()
4460 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 * LOCKING:
4462 * Inherited from PCI layer (may sleep).
4463 *
4464 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004465 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 *
4467 */
4468
4469int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4470 unsigned int n_ports)
4471{
4472 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
4473 struct ata_port_info *port[2];
4474 u8 tmp8, mask;
4475 unsigned int legacy_mode = 0;
4476 int disable_dev_on_err = 1;
4477 int rc;
4478
4479 DPRINTK("ENTER\n");
4480
4481 port[0] = port_info[0];
4482 if (n_ports > 1)
4483 port[1] = port_info[1];
4484 else
4485 port[1] = port[0];
4486
4487 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4488 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4489 /* TODO: support transitioning to native mode? */
4490 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4491 mask = (1 << 2) | (1 << 0);
4492 if ((tmp8 & mask) != mask)
4493 legacy_mode = (1 << 3);
4494 }
4495
4496 /* FIXME... */
4497 if ((!legacy_mode) && (n_ports > 1)) {
4498 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
4499 return -EINVAL;
4500 }
4501
4502 rc = pci_enable_device(pdev);
4503 if (rc)
4504 return rc;
4505
4506 rc = pci_request_regions(pdev, DRV_NAME);
4507 if (rc) {
4508 disable_dev_on_err = 0;
4509 goto err_out;
4510 }
4511
4512 if (legacy_mode) {
4513 if (!request_region(0x1f0, 8, "libata")) {
4514 struct resource *conflict, res;
4515 res.start = 0x1f0;
4516 res.end = 0x1f0 + 8 - 1;
4517 conflict = ____request_resource(&ioport_resource, &res);
4518 if (!strcmp(conflict->name, "libata"))
4519 legacy_mode |= (1 << 0);
4520 else {
4521 disable_dev_on_err = 0;
4522 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4523 }
4524 } else
4525 legacy_mode |= (1 << 0);
4526
4527 if (!request_region(0x170, 8, "libata")) {
4528 struct resource *conflict, res;
4529 res.start = 0x170;
4530 res.end = 0x170 + 8 - 1;
4531 conflict = ____request_resource(&ioport_resource, &res);
4532 if (!strcmp(conflict->name, "libata"))
4533 legacy_mode |= (1 << 1);
4534 else {
4535 disable_dev_on_err = 0;
4536 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4537 }
4538 } else
4539 legacy_mode |= (1 << 1);
4540 }
4541
4542 /* we have legacy mode, but all ports are unavailable */
4543 if (legacy_mode == (1 << 3)) {
4544 rc = -EBUSY;
4545 goto err_out_regions;
4546 }
4547
4548 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4549 if (rc)
4550 goto err_out_regions;
4551 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4552 if (rc)
4553 goto err_out_regions;
4554
4555 if (legacy_mode) {
4556 probe_ent = ata_pci_init_legacy_mode(pdev, port, &probe_ent2);
4557 } else
4558 probe_ent = ata_pci_init_native_mode(pdev, port);
4559 if (!probe_ent) {
4560 rc = -ENOMEM;
4561 goto err_out_regions;
4562 }
4563
4564 pci_set_master(pdev);
4565
4566 /* FIXME: check ata_device_add return */
4567 if (legacy_mode) {
4568 if (legacy_mode & (1 << 0))
4569 ata_device_add(probe_ent);
4570 if (legacy_mode & (1 << 1))
4571 ata_device_add(probe_ent2);
4572 } else
4573 ata_device_add(probe_ent);
4574
4575 kfree(probe_ent);
4576 kfree(probe_ent2);
4577
4578 return 0;
4579
4580err_out_regions:
4581 if (legacy_mode & (1 << 0))
4582 release_region(0x1f0, 8);
4583 if (legacy_mode & (1 << 1))
4584 release_region(0x170, 8);
4585 pci_release_regions(pdev);
4586err_out:
4587 if (disable_dev_on_err)
4588 pci_disable_device(pdev);
4589 return rc;
4590}
4591
4592/**
4593 * ata_pci_remove_one - PCI layer callback for device removal
4594 * @pdev: PCI device that was removed
4595 *
4596 * PCI layer indicates to libata via this hook that
4597 * hot-unplug or module unload event has occured.
4598 * Handle this by unregistering all objects associated
4599 * with this PCI device. Free those objects. Then finally
4600 * release PCI resources and disable device.
4601 *
4602 * LOCKING:
4603 * Inherited from PCI layer (may sleep).
4604 */
4605
4606void ata_pci_remove_one (struct pci_dev *pdev)
4607{
4608 struct device *dev = pci_dev_to_dev(pdev);
4609 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
Alan Cox17b14452005-09-15 15:44:00 +01004611 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 pci_release_regions(pdev);
4613 pci_disable_device(pdev);
4614 dev_set_drvdata(dev, NULL);
4615}
4616
4617/* move to PCI subsystem */
4618int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4619{
4620 unsigned long tmp = 0;
4621
4622 switch (bits->width) {
4623 case 1: {
4624 u8 tmp8 = 0;
4625 pci_read_config_byte(pdev, bits->reg, &tmp8);
4626 tmp = tmp8;
4627 break;
4628 }
4629 case 2: {
4630 u16 tmp16 = 0;
4631 pci_read_config_word(pdev, bits->reg, &tmp16);
4632 tmp = tmp16;
4633 break;
4634 }
4635 case 4: {
4636 u32 tmp32 = 0;
4637 pci_read_config_dword(pdev, bits->reg, &tmp32);
4638 tmp = tmp32;
4639 break;
4640 }
4641
4642 default:
4643 return -EINVAL;
4644 }
4645
4646 tmp &= bits->mask;
4647
4648 return (tmp == bits->val) ? 1 : 0;
4649}
4650#endif /* CONFIG_PCI */
4651
4652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653static int __init ata_init(void)
4654{
4655 ata_wq = create_workqueue("ata");
4656 if (!ata_wq)
4657 return -ENOMEM;
4658
4659 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4660 return 0;
4661}
4662
4663static void __exit ata_exit(void)
4664{
4665 destroy_workqueue(ata_wq);
4666}
4667
4668module_init(ata_init);
4669module_exit(ata_exit);
4670
4671/*
4672 * libata is essentially a library of internal helper functions for
4673 * low-level ATA host controller drivers. As such, the API/ABI is
4674 * likely to change as new drivers are added and updated.
4675 * Do not depend on ABI/API stability.
4676 */
4677
4678EXPORT_SYMBOL_GPL(ata_std_bios_param);
4679EXPORT_SYMBOL_GPL(ata_std_ports);
4680EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004681EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682EXPORT_SYMBOL_GPL(ata_sg_init);
4683EXPORT_SYMBOL_GPL(ata_sg_init_one);
4684EXPORT_SYMBOL_GPL(ata_qc_complete);
4685EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4686EXPORT_SYMBOL_GPL(ata_eng_timeout);
4687EXPORT_SYMBOL_GPL(ata_tf_load);
4688EXPORT_SYMBOL_GPL(ata_tf_read);
4689EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4690EXPORT_SYMBOL_GPL(ata_std_dev_select);
4691EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4692EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4693EXPORT_SYMBOL_GPL(ata_check_status);
4694EXPORT_SYMBOL_GPL(ata_altstatus);
4695EXPORT_SYMBOL_GPL(ata_chk_err);
4696EXPORT_SYMBOL_GPL(ata_exec_command);
4697EXPORT_SYMBOL_GPL(ata_port_start);
4698EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004699EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700EXPORT_SYMBOL_GPL(ata_interrupt);
4701EXPORT_SYMBOL_GPL(ata_qc_prep);
4702EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4703EXPORT_SYMBOL_GPL(ata_bmdma_start);
4704EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4705EXPORT_SYMBOL_GPL(ata_bmdma_status);
4706EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4707EXPORT_SYMBOL_GPL(ata_port_probe);
4708EXPORT_SYMBOL_GPL(sata_phy_reset);
4709EXPORT_SYMBOL_GPL(__sata_phy_reset);
4710EXPORT_SYMBOL_GPL(ata_bus_reset);
4711EXPORT_SYMBOL_GPL(ata_port_disable);
4712EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4713EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4714EXPORT_SYMBOL_GPL(ata_scsi_error);
4715EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4716EXPORT_SYMBOL_GPL(ata_scsi_release);
4717EXPORT_SYMBOL_GPL(ata_host_intr);
4718EXPORT_SYMBOL_GPL(ata_dev_classify);
4719EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004720EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4722
4723#ifdef CONFIG_PCI
4724EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004725EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4727EXPORT_SYMBOL_GPL(ata_pci_init_one);
4728EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4729#endif /* CONFIG_PCI */