blob: 02a7a9eb464d9f9228d337c3e0f160fc6738fc19 [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
Albert Lee312f7da2005-09-27 17:38:03 +08001295 if (ata_id_cdb_intr(dev->id))
1296 dev->flags |= ATA_DFLAG_CDB_INTR;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /* print device info to dmesg */
1299 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1300 ap->id, device,
1301 ata_mode_string(xfer_modes));
1302 }
1303
1304 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1305 return;
1306
1307err_out_nosup:
1308 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1309 ap->id, device);
1310err_out:
1311 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1312 DPRINTK("EXIT, err\n");
1313}
1314
Brad Campbell6f2f3812005-05-12 15:07:47 -04001315
1316static inline u8 ata_dev_knobble(struct ata_port *ap)
1317{
1318 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1319}
1320
1321/**
1322 * ata_dev_config - Run device specific handlers and check for
1323 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001324 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001325 * @i: Device
1326 *
1327 * LOCKING:
1328 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001329
Brad Campbell6f2f3812005-05-12 15:07:47 -04001330void ata_dev_config(struct ata_port *ap, unsigned int i)
1331{
1332 /* limit bridge transfers to udma5, 200 sectors */
1333 if (ata_dev_knobble(ap)) {
1334 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1335 ap->id, ap->device->devno);
1336 ap->udma_mask &= ATA_UDMA5;
1337 ap->host->max_sectors = ATA_MAX_SECTORS;
1338 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1339 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1340 }
1341
1342 if (ap->ops->dev_config)
1343 ap->ops->dev_config(ap, &ap->device[i]);
1344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346/**
1347 * ata_bus_probe - Reset and probe ATA bus
1348 * @ap: Bus to probe
1349 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001350 * Master ATA bus probing function. Initiates a hardware-dependent
1351 * bus reset, then attempts to identify any devices found on
1352 * the bus.
1353 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001355 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 *
1357 * RETURNS:
1358 * Zero on success, non-zero on error.
1359 */
1360
1361static int ata_bus_probe(struct ata_port *ap)
1362{
1363 unsigned int i, found = 0;
1364
1365 ap->ops->phy_reset(ap);
1366 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1367 goto err_out;
1368
1369 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1370 ata_dev_identify(ap, i);
1371 if (ata_dev_present(&ap->device[i])) {
1372 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001373 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 }
1376
1377 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1378 goto err_out_disable;
1379
1380 ata_set_mode(ap);
1381 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1382 goto err_out_disable;
1383
1384 return 0;
1385
1386err_out_disable:
1387 ap->ops->port_disable(ap);
1388err_out:
1389 return -1;
1390}
1391
1392/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001393 * ata_port_probe - Mark port as enabled
1394 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001396 * Modify @ap data structure such that the system
1397 * thinks that the entire port is enabled.
1398 *
1399 * LOCKING: host_set lock, or some other form of
1400 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
1402
1403void ata_port_probe(struct ata_port *ap)
1404{
1405 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1406}
1407
1408/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001409 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1410 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001412 * This function issues commands to standard SATA Sxxx
1413 * PHY registers, to wake up the phy (and device), and
1414 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 *
1416 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001417 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 *
1419 */
1420void __sata_phy_reset(struct ata_port *ap)
1421{
1422 u32 sstatus;
1423 unsigned long timeout = jiffies + (HZ * 5);
1424
1425 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001426 /* issue phy wake/reset */
1427 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001428 /* Couldn't find anything in SATA I/II specs, but
1429 * AHCI-1.1 10.4.2 says at least 1 ms. */
1430 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Brett Russcdcca892005-03-28 15:10:27 -05001432 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 /* wait for phy to become ready, if necessary */
1435 do {
1436 msleep(200);
1437 sstatus = scr_read(ap, SCR_STATUS);
1438 if ((sstatus & 0xf) != 1)
1439 break;
1440 } while (time_before(jiffies, timeout));
1441
1442 /* TODO: phy layer with polling, timeouts, etc. */
1443 if (sata_dev_present(ap))
1444 ata_port_probe(ap);
1445 else {
1446 sstatus = scr_read(ap, SCR_STATUS);
1447 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1448 ap->id, sstatus);
1449 ata_port_disable(ap);
1450 }
1451
1452 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1453 return;
1454
1455 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1456 ata_port_disable(ap);
1457 return;
1458 }
1459
1460 ap->cbl = ATA_CBL_SATA;
1461}
1462
1463/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001464 * sata_phy_reset - Reset SATA bus.
1465 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001467 * This function resets the SATA bus, and then probes
1468 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 *
1470 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001471 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 *
1473 */
1474void sata_phy_reset(struct ata_port *ap)
1475{
1476 __sata_phy_reset(ap);
1477 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1478 return;
1479 ata_bus_reset(ap);
1480}
1481
1482/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001483 * ata_port_disable - Disable port.
1484 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001486 * Modify @ap data structure such that the system
1487 * thinks that the entire port is disabled, and should
1488 * never attempt to probe or communicate with devices
1489 * on this port.
1490 *
1491 * LOCKING: host_set lock, or some other form of
1492 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 */
1494
1495void ata_port_disable(struct ata_port *ap)
1496{
1497 ap->device[0].class = ATA_DEV_NONE;
1498 ap->device[1].class = ATA_DEV_NONE;
1499 ap->flags |= ATA_FLAG_PORT_DISABLED;
1500}
1501
1502static struct {
1503 unsigned int shift;
1504 u8 base;
1505} xfer_mode_classes[] = {
1506 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1507 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1508 { ATA_SHIFT_PIO, XFER_PIO_0 },
1509};
1510
1511static inline u8 base_from_shift(unsigned int shift)
1512{
1513 int i;
1514
1515 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1516 if (xfer_mode_classes[i].shift == shift)
1517 return xfer_mode_classes[i].base;
1518
1519 return 0xff;
1520}
1521
1522static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1523{
1524 int ofs, idx;
1525 u8 base;
1526
1527 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1528 return;
1529
1530 if (dev->xfer_shift == ATA_SHIFT_PIO)
1531 dev->flags |= ATA_DFLAG_PIO;
1532
1533 ata_dev_set_xfermode(ap, dev);
1534
1535 base = base_from_shift(dev->xfer_shift);
1536 ofs = dev->xfer_mode - base;
1537 idx = ofs + dev->xfer_shift;
1538 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1539
1540 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1541 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1542
1543 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1544 ap->id, dev->devno, xfer_mode_str[idx]);
1545}
1546
1547static int ata_host_set_pio(struct ata_port *ap)
1548{
1549 unsigned int mask;
1550 int x, i;
1551 u8 base, xfer_mode;
1552
1553 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1554 x = fgb(mask);
1555 if (x < 0) {
1556 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1557 return -1;
1558 }
1559
1560 base = base_from_shift(ATA_SHIFT_PIO);
1561 xfer_mode = base + x;
1562
1563 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1564 (int)base, (int)xfer_mode, mask, x);
1565
1566 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1567 struct ata_device *dev = &ap->device[i];
1568 if (ata_dev_present(dev)) {
1569 dev->pio_mode = xfer_mode;
1570 dev->xfer_mode = xfer_mode;
1571 dev->xfer_shift = ATA_SHIFT_PIO;
1572 if (ap->ops->set_piomode)
1573 ap->ops->set_piomode(ap, dev);
1574 }
1575 }
1576
1577 return 0;
1578}
1579
1580static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1581 unsigned int xfer_shift)
1582{
1583 int i;
1584
1585 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1586 struct ata_device *dev = &ap->device[i];
1587 if (ata_dev_present(dev)) {
1588 dev->dma_mode = xfer_mode;
1589 dev->xfer_mode = xfer_mode;
1590 dev->xfer_shift = xfer_shift;
1591 if (ap->ops->set_dmamode)
1592 ap->ops->set_dmamode(ap, dev);
1593 }
1594 }
1595}
1596
1597/**
1598 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1599 * @ap: port on which timings will be programmed
1600 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001601 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1602 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001604 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 *
1606 */
1607static void ata_set_mode(struct ata_port *ap)
1608{
1609 unsigned int i, xfer_shift;
1610 u8 xfer_mode;
1611 int rc;
1612
1613 /* step 1: always set host PIO timings */
1614 rc = ata_host_set_pio(ap);
1615 if (rc)
1616 goto err_out;
1617
1618 /* step 2: choose the best data xfer mode */
1619 xfer_mode = xfer_shift = 0;
1620 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1621 if (rc)
1622 goto err_out;
1623
1624 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1625 if (xfer_shift != ATA_SHIFT_PIO)
1626 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1627
1628 /* step 4: update devices' xfer mode */
1629 ata_dev_set_mode(ap, &ap->device[0]);
1630 ata_dev_set_mode(ap, &ap->device[1]);
1631
1632 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1633 return;
1634
1635 if (ap->ops->post_set_mode)
1636 ap->ops->post_set_mode(ap);
1637
1638 for (i = 0; i < 2; i++) {
1639 struct ata_device *dev = &ap->device[i];
1640 ata_dev_set_protocol(dev);
1641 }
1642
1643 return;
1644
1645err_out:
1646 ata_port_disable(ap);
1647}
1648
1649/**
1650 * ata_busy_sleep - sleep until BSY clears, or timeout
1651 * @ap: port containing status register to be polled
1652 * @tmout_pat: impatience timeout
1653 * @tmout: overall timeout
1654 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001655 * Sleep until ATA Status register bit BSY clears,
1656 * or a timeout occurs.
1657 *
1658 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 *
1660 */
1661
1662static unsigned int ata_busy_sleep (struct ata_port *ap,
1663 unsigned long tmout_pat,
1664 unsigned long tmout)
1665{
1666 unsigned long timer_start, timeout;
1667 u8 status;
1668
1669 status = ata_busy_wait(ap, ATA_BUSY, 300);
1670 timer_start = jiffies;
1671 timeout = timer_start + tmout_pat;
1672 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1673 msleep(50);
1674 status = ata_busy_wait(ap, ATA_BUSY, 3);
1675 }
1676
1677 if (status & ATA_BUSY)
1678 printk(KERN_WARNING "ata%u is slow to respond, "
1679 "please be patient\n", ap->id);
1680
1681 timeout = timer_start + tmout;
1682 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1683 msleep(50);
1684 status = ata_chk_status(ap);
1685 }
1686
1687 if (status & ATA_BUSY) {
1688 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1689 ap->id, tmout / HZ);
1690 return 1;
1691 }
1692
1693 return 0;
1694}
1695
1696static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1697{
1698 struct ata_ioports *ioaddr = &ap->ioaddr;
1699 unsigned int dev0 = devmask & (1 << 0);
1700 unsigned int dev1 = devmask & (1 << 1);
1701 unsigned long timeout;
1702
1703 /* if device 0 was found in ata_devchk, wait for its
1704 * BSY bit to clear
1705 */
1706 if (dev0)
1707 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1708
1709 /* if device 1 was found in ata_devchk, wait for
1710 * register access, then wait for BSY to clear
1711 */
1712 timeout = jiffies + ATA_TMOUT_BOOT;
1713 while (dev1) {
1714 u8 nsect, lbal;
1715
1716 ap->ops->dev_select(ap, 1);
1717 if (ap->flags & ATA_FLAG_MMIO) {
1718 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1719 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1720 } else {
1721 nsect = inb(ioaddr->nsect_addr);
1722 lbal = inb(ioaddr->lbal_addr);
1723 }
1724 if ((nsect == 1) && (lbal == 1))
1725 break;
1726 if (time_after(jiffies, timeout)) {
1727 dev1 = 0;
1728 break;
1729 }
1730 msleep(50); /* give drive a breather */
1731 }
1732 if (dev1)
1733 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1734
1735 /* is all this really necessary? */
1736 ap->ops->dev_select(ap, 0);
1737 if (dev1)
1738 ap->ops->dev_select(ap, 1);
1739 if (dev0)
1740 ap->ops->dev_select(ap, 0);
1741}
1742
1743/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001744 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1745 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001747 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1748 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 *
1750 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001751 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 *
1753 */
1754
1755static unsigned int ata_bus_edd(struct ata_port *ap)
1756{
1757 struct ata_taskfile tf;
1758
1759 /* set up execute-device-diag (bus reset) taskfile */
1760 /* also, take interrupts to a known state (disabled) */
1761 DPRINTK("execute-device-diag\n");
1762 ata_tf_init(ap, &tf, 0);
1763 tf.ctl |= ATA_NIEN;
1764 tf.command = ATA_CMD_EDD;
1765 tf.protocol = ATA_PROT_NODATA;
1766
1767 /* do bus reset */
1768 ata_tf_to_host(ap, &tf);
1769
1770 /* spec says at least 2ms. but who knows with those
1771 * crazy ATAPI devices...
1772 */
1773 msleep(150);
1774
1775 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1776}
1777
1778static unsigned int ata_bus_softreset(struct ata_port *ap,
1779 unsigned int devmask)
1780{
1781 struct ata_ioports *ioaddr = &ap->ioaddr;
1782
1783 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1784
1785 /* software reset. causes dev0 to be selected */
1786 if (ap->flags & ATA_FLAG_MMIO) {
1787 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1788 udelay(20); /* FIXME: flush */
1789 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1790 udelay(20); /* FIXME: flush */
1791 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1792 } else {
1793 outb(ap->ctl, ioaddr->ctl_addr);
1794 udelay(10);
1795 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1796 udelay(10);
1797 outb(ap->ctl, ioaddr->ctl_addr);
1798 }
1799
1800 /* spec mandates ">= 2ms" before checking status.
1801 * We wait 150ms, because that was the magic delay used for
1802 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1803 * between when the ATA command register is written, and then
1804 * status is checked. Because waiting for "a while" before
1805 * checking status is fine, post SRST, we perform this magic
1806 * delay here as well.
1807 */
1808 msleep(150);
1809
1810 ata_bus_post_reset(ap, devmask);
1811
1812 return 0;
1813}
1814
1815/**
1816 * ata_bus_reset - reset host port and associated ATA channel
1817 * @ap: port to reset
1818 *
1819 * This is typically the first time we actually start issuing
1820 * commands to the ATA channel. We wait for BSY to clear, then
1821 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1822 * result. Determine what devices, if any, are on the channel
1823 * by looking at the device 0/1 error register. Look at the signature
1824 * stored in each device's taskfile registers, to determine if
1825 * the device is ATA or ATAPI.
1826 *
1827 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001828 * PCI/etc. bus probe sem.
1829 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 *
1831 * SIDE EFFECTS:
1832 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1833 */
1834
1835void ata_bus_reset(struct ata_port *ap)
1836{
1837 struct ata_ioports *ioaddr = &ap->ioaddr;
1838 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1839 u8 err;
1840 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1841
1842 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1843
1844 /* determine if device 0/1 are present */
1845 if (ap->flags & ATA_FLAG_SATA_RESET)
1846 dev0 = 1;
1847 else {
1848 dev0 = ata_devchk(ap, 0);
1849 if (slave_possible)
1850 dev1 = ata_devchk(ap, 1);
1851 }
1852
1853 if (dev0)
1854 devmask |= (1 << 0);
1855 if (dev1)
1856 devmask |= (1 << 1);
1857
1858 /* select device 0 again */
1859 ap->ops->dev_select(ap, 0);
1860
1861 /* issue bus reset */
1862 if (ap->flags & ATA_FLAG_SRST)
1863 rc = ata_bus_softreset(ap, devmask);
1864 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1865 /* set up device control */
1866 if (ap->flags & ATA_FLAG_MMIO)
1867 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1868 else
1869 outb(ap->ctl, ioaddr->ctl_addr);
1870 rc = ata_bus_edd(ap);
1871 }
1872
1873 if (rc)
1874 goto err_out;
1875
1876 /*
1877 * determine by signature whether we have ATA or ATAPI devices
1878 */
1879 err = ata_dev_try_classify(ap, 0);
1880 if ((slave_possible) && (err != 0x81))
1881 ata_dev_try_classify(ap, 1);
1882
1883 /* re-enable interrupts */
1884 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1885 ata_irq_on(ap);
1886
1887 /* is double-select really necessary? */
1888 if (ap->device[1].class != ATA_DEV_NONE)
1889 ap->ops->dev_select(ap, 1);
1890 if (ap->device[0].class != ATA_DEV_NONE)
1891 ap->ops->dev_select(ap, 0);
1892
1893 /* if no devices were detected, disable this port */
1894 if ((ap->device[0].class == ATA_DEV_NONE) &&
1895 (ap->device[1].class == ATA_DEV_NONE))
1896 goto err_out;
1897
1898 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1899 /* set up device control for ATA_FLAG_SATA_RESET */
1900 if (ap->flags & ATA_FLAG_MMIO)
1901 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1902 else
1903 outb(ap->ctl, ioaddr->ctl_addr);
1904 }
1905
1906 DPRINTK("EXIT\n");
1907 return;
1908
1909err_out:
1910 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1911 ap->ops->port_disable(ap);
1912
1913 DPRINTK("EXIT\n");
1914}
1915
1916static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1917{
1918 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1919 ap->id, dev->devno);
1920}
1921
1922static const char * ata_dma_blacklist [] = {
1923 "WDC AC11000H",
1924 "WDC AC22100H",
1925 "WDC AC32500H",
1926 "WDC AC33100H",
1927 "WDC AC31600H",
1928 "WDC AC32100H",
1929 "WDC AC23200L",
1930 "Compaq CRD-8241B",
1931 "CRD-8400B",
1932 "CRD-8480B",
1933 "CRD-8482B",
1934 "CRD-84",
1935 "SanDisk SDP3B",
1936 "SanDisk SDP3B-64",
1937 "SANYO CD-ROM CRD",
1938 "HITACHI CDR-8",
1939 "HITACHI CDR-8335",
1940 "HITACHI CDR-8435",
1941 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04001942 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 "CD-532E-A",
1944 "E-IDE CD-ROM CR-840",
1945 "CD-ROM Drive/F5A",
1946 "WPI CDD-820",
1947 "SAMSUNG CD-ROM SC-148C",
1948 "SAMSUNG CD-ROM SC",
1949 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1951 "_NEC DV5800A",
1952};
1953
1954static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1955{
1956 unsigned char model_num[40];
1957 char *s;
1958 unsigned int len;
1959 int i;
1960
1961 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1962 sizeof(model_num));
1963 s = &model_num[0];
1964 len = strnlen(s, sizeof(model_num));
1965
1966 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1967 while ((len > 0) && (s[len - 1] == ' ')) {
1968 len--;
1969 s[len] = 0;
1970 }
1971
1972 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
1973 if (!strncmp(ata_dma_blacklist[i], s, len))
1974 return 1;
1975
1976 return 0;
1977}
1978
1979static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
1980{
1981 struct ata_device *master, *slave;
1982 unsigned int mask;
1983
1984 master = &ap->device[0];
1985 slave = &ap->device[1];
1986
1987 assert (ata_dev_present(master) || ata_dev_present(slave));
1988
1989 if (shift == ATA_SHIFT_UDMA) {
1990 mask = ap->udma_mask;
1991 if (ata_dev_present(master)) {
1992 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1993 if (ata_dma_blacklisted(ap, master)) {
1994 mask = 0;
1995 ata_pr_blacklisted(ap, master);
1996 }
1997 }
1998 if (ata_dev_present(slave)) {
1999 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2000 if (ata_dma_blacklisted(ap, slave)) {
2001 mask = 0;
2002 ata_pr_blacklisted(ap, slave);
2003 }
2004 }
2005 }
2006 else if (shift == ATA_SHIFT_MWDMA) {
2007 mask = ap->mwdma_mask;
2008 if (ata_dev_present(master)) {
2009 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2010 if (ata_dma_blacklisted(ap, master)) {
2011 mask = 0;
2012 ata_pr_blacklisted(ap, master);
2013 }
2014 }
2015 if (ata_dev_present(slave)) {
2016 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2017 if (ata_dma_blacklisted(ap, slave)) {
2018 mask = 0;
2019 ata_pr_blacklisted(ap, slave);
2020 }
2021 }
2022 }
2023 else if (shift == ATA_SHIFT_PIO) {
2024 mask = ap->pio_mask;
2025 if (ata_dev_present(master)) {
2026 /* spec doesn't return explicit support for
2027 * PIO0-2, so we fake it
2028 */
2029 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2030 tmp_mode <<= 3;
2031 tmp_mode |= 0x7;
2032 mask &= tmp_mode;
2033 }
2034 if (ata_dev_present(slave)) {
2035 /* spec doesn't return explicit support for
2036 * PIO0-2, so we fake it
2037 */
2038 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2039 tmp_mode <<= 3;
2040 tmp_mode |= 0x7;
2041 mask &= tmp_mode;
2042 }
2043 }
2044 else {
2045 mask = 0xffffffff; /* shut up compiler warning */
2046 BUG();
2047 }
2048
2049 return mask;
2050}
2051
2052/* find greatest bit */
2053static int fgb(u32 bitmap)
2054{
2055 unsigned int i;
2056 int x = -1;
2057
2058 for (i = 0; i < 32; i++)
2059 if (bitmap & (1 << i))
2060 x = i;
2061
2062 return x;
2063}
2064
2065/**
2066 * ata_choose_xfer_mode - attempt to find best transfer mode
2067 * @ap: Port for which an xfer mode will be selected
2068 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2069 * @xfer_shift_out: (output) bit shift that selects this mode
2070 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002071 * Based on host and device capabilities, determine the
2072 * maximum transfer mode that is amenable to all.
2073 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002075 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 *
2077 * RETURNS:
2078 * Zero on success, negative on error.
2079 */
2080
2081static int ata_choose_xfer_mode(struct ata_port *ap,
2082 u8 *xfer_mode_out,
2083 unsigned int *xfer_shift_out)
2084{
2085 unsigned int mask, shift;
2086 int x, i;
2087
2088 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2089 shift = xfer_mode_classes[i].shift;
2090 mask = ata_get_mode_mask(ap, shift);
2091
2092 x = fgb(mask);
2093 if (x >= 0) {
2094 *xfer_mode_out = xfer_mode_classes[i].base + x;
2095 *xfer_shift_out = shift;
2096 return 0;
2097 }
2098 }
2099
2100 return -1;
2101}
2102
2103/**
2104 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2105 * @ap: Port associated with device @dev
2106 * @dev: Device to which command will be sent
2107 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002108 * Issue SET FEATURES - XFER MODE command to device @dev
2109 * on port @ap.
2110 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002112 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 */
2114
2115static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2116{
2117 DECLARE_COMPLETION(wait);
2118 struct ata_queued_cmd *qc;
2119 int rc;
2120 unsigned long flags;
2121
2122 /* set up set-features taskfile */
2123 DPRINTK("set features - xfer mode\n");
2124
2125 qc = ata_qc_new_init(ap, dev);
2126 BUG_ON(qc == NULL);
2127
2128 qc->tf.command = ATA_CMD_SET_FEATURES;
2129 qc->tf.feature = SETFEATURES_XFER;
2130 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2131 qc->tf.protocol = ATA_PROT_NODATA;
2132 qc->tf.nsect = dev->xfer_mode;
2133
2134 qc->waiting = &wait;
2135 qc->complete_fn = ata_qc_complete_noop;
2136
2137 spin_lock_irqsave(&ap->host_set->lock, flags);
2138 rc = ata_qc_issue(qc);
2139 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2140
2141 if (rc)
2142 ata_port_disable(ap);
2143 else
2144 wait_for_completion(&wait);
2145
2146 DPRINTK("EXIT\n");
2147}
2148
2149/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002150 * ata_sg_clean - Unmap DMA memory associated with command
2151 * @qc: Command containing DMA memory to be released
2152 *
2153 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 *
2155 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002156 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 */
2158
2159static void ata_sg_clean(struct ata_queued_cmd *qc)
2160{
2161 struct ata_port *ap = qc->ap;
2162 struct scatterlist *sg = qc->sg;
2163 int dir = qc->dma_dir;
2164
2165 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2166 assert(sg != NULL);
2167
2168 if (qc->flags & ATA_QCFLAG_SINGLE)
2169 assert(qc->n_elem == 1);
2170
2171 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2172
2173 if (qc->flags & ATA_QCFLAG_SG)
2174 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2175 else
2176 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2177 sg_dma_len(&sg[0]), dir);
2178
2179 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2180 qc->sg = NULL;
2181}
2182
2183/**
2184 * ata_fill_sg - Fill PCI IDE PRD table
2185 * @qc: Metadata associated with taskfile to be transferred
2186 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002187 * Fill PCI IDE PRD (scatter-gather) table with segments
2188 * associated with the current disk command.
2189 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002191 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 *
2193 */
2194static void ata_fill_sg(struct ata_queued_cmd *qc)
2195{
2196 struct scatterlist *sg = qc->sg;
2197 struct ata_port *ap = qc->ap;
2198 unsigned int idx, nelem;
2199
2200 assert(sg != NULL);
2201 assert(qc->n_elem > 0);
2202
2203 idx = 0;
2204 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2205 u32 addr, offset;
2206 u32 sg_len, len;
2207
2208 /* determine if physical DMA addr spans 64K boundary.
2209 * Note h/w doesn't support 64-bit, so we unconditionally
2210 * truncate dma_addr_t to u32.
2211 */
2212 addr = (u32) sg_dma_address(sg);
2213 sg_len = sg_dma_len(sg);
2214
2215 while (sg_len) {
2216 offset = addr & 0xffff;
2217 len = sg_len;
2218 if ((offset + sg_len) > 0x10000)
2219 len = 0x10000 - offset;
2220
2221 ap->prd[idx].addr = cpu_to_le32(addr);
2222 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2223 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2224
2225 idx++;
2226 sg_len -= len;
2227 addr += len;
2228 }
2229 }
2230
2231 if (idx)
2232 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2233}
2234/**
2235 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2236 * @qc: Metadata associated with taskfile to check
2237 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002238 * Allow low-level driver to filter ATA PACKET commands, returning
2239 * a status indicating whether or not it is OK to use DMA for the
2240 * supplied PACKET command.
2241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002243 * spin_lock_irqsave(host_set lock)
2244 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 * RETURNS: 0 when ATAPI DMA can be used
2246 * nonzero otherwise
2247 */
2248int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2249{
2250 struct ata_port *ap = qc->ap;
2251 int rc = 0; /* Assume ATAPI DMA is OK by default */
2252
2253 if (ap->ops->check_atapi_dma)
2254 rc = ap->ops->check_atapi_dma(qc);
2255
2256 return rc;
2257}
2258/**
2259 * ata_qc_prep - Prepare taskfile for submission
2260 * @qc: Metadata associated with taskfile to be prepared
2261 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002262 * Prepare ATA taskfile for submission.
2263 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 * LOCKING:
2265 * spin_lock_irqsave(host_set lock)
2266 */
2267void ata_qc_prep(struct ata_queued_cmd *qc)
2268{
2269 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2270 return;
2271
2272 ata_fill_sg(qc);
2273}
2274
Jeff Garzik0cba6322005-05-30 19:49:12 -04002275/**
2276 * ata_sg_init_one - Associate command with memory buffer
2277 * @qc: Command to be associated
2278 * @buf: Memory buffer
2279 * @buflen: Length of memory buffer, in bytes.
2280 *
2281 * Initialize the data-related elements of queued_cmd @qc
2282 * to point to a single memory buffer, @buf of byte length @buflen.
2283 *
2284 * LOCKING:
2285 * spin_lock_irqsave(host_set lock)
2286 */
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2289{
2290 struct scatterlist *sg;
2291
2292 qc->flags |= ATA_QCFLAG_SINGLE;
2293
2294 memset(&qc->sgent, 0, sizeof(qc->sgent));
2295 qc->sg = &qc->sgent;
2296 qc->n_elem = 1;
2297 qc->buf_virt = buf;
2298
2299 sg = qc->sg;
2300 sg->page = virt_to_page(buf);
2301 sg->offset = (unsigned long) buf & ~PAGE_MASK;
Albert Lee32529e02005-05-26 03:49:42 -04002302 sg->length = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Jeff Garzik0cba6322005-05-30 19:49:12 -04002305/**
2306 * ata_sg_init - Associate command with scatter-gather table.
2307 * @qc: Command to be associated
2308 * @sg: Scatter-gather table.
2309 * @n_elem: Number of elements in s/g table.
2310 *
2311 * Initialize the data-related elements of queued_cmd @qc
2312 * to point to a scatter-gather table @sg, containing @n_elem
2313 * elements.
2314 *
2315 * LOCKING:
2316 * spin_lock_irqsave(host_set lock)
2317 */
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2320 unsigned int n_elem)
2321{
2322 qc->flags |= ATA_QCFLAG_SG;
2323 qc->sg = sg;
2324 qc->n_elem = n_elem;
2325}
2326
2327/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002328 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2329 * @qc: Command with memory buffer to be mapped.
2330 *
2331 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 *
2333 * LOCKING:
2334 * spin_lock_irqsave(host_set lock)
2335 *
2336 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002337 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 */
2339
2340static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2341{
2342 struct ata_port *ap = qc->ap;
2343 int dir = qc->dma_dir;
2344 struct scatterlist *sg = qc->sg;
2345 dma_addr_t dma_address;
2346
2347 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002348 sg->length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (dma_mapping_error(dma_address))
2350 return -1;
2351
2352 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002353 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2356 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2357
2358 return 0;
2359}
2360
2361/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002362 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2363 * @qc: Command with scatter-gather table to be mapped.
2364 *
2365 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 *
2367 * LOCKING:
2368 * spin_lock_irqsave(host_set lock)
2369 *
2370 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002371 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 *
2373 */
2374
2375static int ata_sg_setup(struct ata_queued_cmd *qc)
2376{
2377 struct ata_port *ap = qc->ap;
2378 struct scatterlist *sg = qc->sg;
2379 int n_elem, dir;
2380
2381 VPRINTK("ENTER, ata%u\n", ap->id);
2382 assert(qc->flags & ATA_QCFLAG_SG);
2383
2384 dir = qc->dma_dir;
2385 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2386 if (n_elem < 1)
2387 return -1;
2388
2389 DPRINTK("%d sg elements mapped\n", n_elem);
2390
2391 qc->n_elem = n_elem;
2392
2393 return 0;
2394}
2395
2396/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002397 * ata_poll_qc_complete - turn irq back on and finish qc
2398 * @qc: Command to complete
2399 * @drv_stat: ATA status register content
2400 *
2401 * LOCKING:
2402 * None. (grabs host lock)
2403 */
2404
2405void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2406{
2407 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002408 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002409
Jeff Garzikb8f61532005-08-25 22:01:20 -04002410 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002411 ata_irq_on(ap);
2412 ata_qc_complete(qc, drv_stat);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002413 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002414}
2415
2416/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 * ata_pio_poll -
2418 * @ap:
2419 *
2420 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002421 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 *
2423 * RETURNS:
2424 *
2425 */
2426
2427static unsigned long ata_pio_poll(struct ata_port *ap)
2428{
2429 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002430 unsigned int poll_state = HSM_ST_UNKNOWN;
2431 unsigned int reg_state = HSM_ST_UNKNOWN;
2432 const unsigned int tmout_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Albert Lee14be71f2005-09-27 17:36:35 +08002434 switch (ap->hsm_task_state) {
2435 case HSM_ST:
2436 case HSM_ST_POLL:
2437 poll_state = HSM_ST_POLL;
2438 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002440 case HSM_ST_LAST:
2441 case HSM_ST_LAST_POLL:
2442 poll_state = HSM_ST_LAST_POLL;
2443 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 break;
2445 default:
2446 BUG();
2447 break;
2448 }
2449
2450 status = ata_chk_status(ap);
2451 if (status & ATA_BUSY) {
2452 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002453 ap->hsm_task_state = tmout_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return 0;
2455 }
Albert Lee14be71f2005-09-27 17:36:35 +08002456 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return ATA_SHORT_PAUSE;
2458 }
2459
Albert Lee14be71f2005-09-27 17:36:35 +08002460 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return 0;
2462}
2463
2464/**
2465 * ata_pio_complete -
2466 * @ap:
2467 *
2468 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002469 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002470 *
2471 * RETURNS:
2472 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 */
2474
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002475static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 struct ata_queued_cmd *qc;
2478 u8 drv_stat;
2479
2480 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002481 * This is purely heuristic. This is a fast path. Sometimes when
2482 * we enter, BSY will be cleared in a chk-status or two. If not,
2483 * the drive is probably seeking or something. Snooze for a couple
2484 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002485 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 */
2487 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2488 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2489 msleep(2);
2490 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2491 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002492 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496 }
2497
2498 drv_stat = ata_wait_idle(ap);
2499 if (!ata_ok(drv_stat)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002500 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 }
2503
2504 qc = ata_qc_from_tag(ap, ap->active_tag);
2505 assert(qc != NULL);
2506
Albert Lee14be71f2005-09-27 17:36:35 +08002507 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Tejun Heo40e8c822005-08-22 17:12:45 +09002509 ata_poll_qc_complete(qc, drv_stat);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002510
2511 /* another command may start at this point */
2512
2513 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
Edward Falk0baab862005-06-02 18:17:13 -04002516
2517/**
2518 * swap_buf_le16 -
2519 * @buf: Buffer to swap
2520 * @buf_words: Number of 16-bit words in buffer.
2521 *
2522 * Swap halves of 16-bit words if needed to convert from
2523 * little-endian byte order to native cpu byte order, or
2524 * vice-versa.
2525 *
2526 * LOCKING:
2527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528void swap_buf_le16(u16 *buf, unsigned int buf_words)
2529{
2530#ifdef __BIG_ENDIAN
2531 unsigned int i;
2532
2533 for (i = 0; i < buf_words; i++)
2534 buf[i] = le16_to_cpu(buf[i]);
2535#endif /* __BIG_ENDIAN */
2536}
2537
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002538/**
2539 * ata_mmio_data_xfer - Transfer data by MMIO
2540 * @ap: port to read/write
2541 * @buf: data buffer
2542 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002543 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002544 *
2545 * Transfer data from/to the device data register by MMIO.
2546 *
2547 * LOCKING:
2548 * Inherited from caller.
2549 *
2550 */
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2553 unsigned int buflen, int write_data)
2554{
2555 unsigned int i;
2556 unsigned int words = buflen >> 1;
2557 u16 *buf16 = (u16 *) buf;
2558 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2559
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002560 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (write_data) {
2562 for (i = 0; i < words; i++)
2563 writew(le16_to_cpu(buf16[i]), mmio);
2564 } else {
2565 for (i = 0; i < words; i++)
2566 buf16[i] = cpu_to_le16(readw(mmio));
2567 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002568
2569 /* Transfer trailing 1 byte, if any. */
2570 if (unlikely(buflen & 0x01)) {
2571 u16 align_buf[1] = { 0 };
2572 unsigned char *trailing_buf = buf + buflen - 1;
2573
2574 if (write_data) {
2575 memcpy(align_buf, trailing_buf, 1);
2576 writew(le16_to_cpu(align_buf[0]), mmio);
2577 } else {
2578 align_buf[0] = cpu_to_le16(readw(mmio));
2579 memcpy(trailing_buf, align_buf, 1);
2580 }
2581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002584/**
2585 * ata_pio_data_xfer - Transfer data by PIO
2586 * @ap: port to read/write
2587 * @buf: data buffer
2588 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002589 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002590 *
2591 * Transfer data from/to the device data register by PIO.
2592 *
2593 * LOCKING:
2594 * Inherited from caller.
2595 *
2596 */
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2599 unsigned int buflen, int write_data)
2600{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002601 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002603 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002605 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002607 insw(ap->ioaddr.data_addr, buf, words);
2608
2609 /* Transfer trailing 1 byte, if any. */
2610 if (unlikely(buflen & 0x01)) {
2611 u16 align_buf[1] = { 0 };
2612 unsigned char *trailing_buf = buf + buflen - 1;
2613
2614 if (write_data) {
2615 memcpy(align_buf, trailing_buf, 1);
2616 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2617 } else {
2618 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2619 memcpy(trailing_buf, align_buf, 1);
2620 }
2621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002624/**
2625 * ata_data_xfer - Transfer data from/to the data register.
2626 * @ap: port to read/write
2627 * @buf: data buffer
2628 * @buflen: buffer length
2629 * @do_write: read/write
2630 *
2631 * Transfer data from/to the device data register.
2632 *
2633 * LOCKING:
2634 * Inherited from caller.
2635 *
2636 */
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2639 unsigned int buflen, int do_write)
2640{
2641 if (ap->flags & ATA_FLAG_MMIO)
2642 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2643 else
2644 ata_pio_data_xfer(ap, buf, buflen, do_write);
2645}
2646
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002647/**
2648 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2649 * @qc: Command on going
2650 *
2651 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2652 *
2653 * LOCKING:
2654 * Inherited from caller.
2655 */
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657static void ata_pio_sector(struct ata_queued_cmd *qc)
2658{
2659 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2660 struct scatterlist *sg = qc->sg;
2661 struct ata_port *ap = qc->ap;
2662 struct page *page;
2663 unsigned int offset;
2664 unsigned char *buf;
Albert Lee312f7da2005-09-27 17:38:03 +08002665 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
2667 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08002668 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 page = sg[qc->cursg].page;
2671 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2672
2673 /* get the current page and offset */
2674 page = nth_page(page, (offset >> PAGE_SHIFT));
2675 offset %= PAGE_SIZE;
2676
Albert Lee312f7da2005-09-27 17:38:03 +08002677 local_irq_save(flags);
2678 buf = kmap_atomic(page, KM_IRQ0) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 qc->cursect++;
2681 qc->cursg_ofs++;
2682
Albert Lee32529e02005-05-26 03:49:42 -04002683 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 qc->cursg++;
2685 qc->cursg_ofs = 0;
2686 }
2687
2688 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2689
2690 /* do the actual data transfer */
2691 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2692 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2693
Albert Lee312f7da2005-09-27 17:38:03 +08002694 kunmap_atomic(buf - offset, KM_IRQ0);
2695 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002698/**
2699 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2700 * @qc: Command on going
2701 * @bytes: number of bytes
2702 *
2703 * Transfer Transfer data from/to the ATAPI device.
2704 *
2705 * LOCKING:
2706 * Inherited from caller.
2707 *
2708 */
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2711{
2712 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2713 struct scatterlist *sg = qc->sg;
2714 struct ata_port *ap = qc->ap;
2715 struct page *page;
2716 unsigned char *buf;
2717 unsigned int offset, count;
Albert Lee312f7da2005-09-27 17:38:03 +08002718 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Albert Lee563a6e12005-08-12 14:17:50 +08002720 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08002721 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08002724 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002725 /*
Albert Lee563a6e12005-08-12 14:17:50 +08002726 * The end of qc->sg is reached and the device expects
2727 * more data to transfer. In order not to overrun qc->sg
2728 * and fulfill length specified in the byte count register,
2729 * - for read case, discard trailing data from the device
2730 * - for write case, padding zero data to the device
2731 */
2732 u16 pad_buf[1] = { 0 };
2733 unsigned int words = bytes >> 1;
2734 unsigned int i;
2735
2736 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002737 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08002738 ap->id, bytes);
2739
2740 for (i = 0; i < words; i++)
2741 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
2742
Albert Lee14be71f2005-09-27 17:36:35 +08002743 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08002744 return;
2745 }
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 sg = &qc->sg[qc->cursg];
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 page = sg->page;
2750 offset = sg->offset + qc->cursg_ofs;
2751
2752 /* get the current page and offset */
2753 page = nth_page(page, (offset >> PAGE_SHIFT));
2754 offset %= PAGE_SIZE;
2755
Albert Lee6952df02005-06-06 15:56:03 +08002756 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04002757 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 /* don't cross page boundaries */
2760 count = min(count, (unsigned int)PAGE_SIZE - offset);
2761
Albert Lee312f7da2005-09-27 17:38:03 +08002762 local_irq_save(flags);
2763 buf = kmap_atomic(page, KM_IRQ0) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 bytes -= count;
2766 qc->curbytes += count;
2767 qc->cursg_ofs += count;
2768
Albert Lee32529e02005-05-26 03:49:42 -04002769 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 qc->cursg++;
2771 qc->cursg_ofs = 0;
2772 }
2773
2774 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2775
2776 /* do the actual data transfer */
2777 ata_data_xfer(ap, buf, count, do_write);
2778
Albert Lee312f7da2005-09-27 17:38:03 +08002779 kunmap_atomic(buf - offset, KM_IRQ0);
2780 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Albert Lee563a6e12005-08-12 14:17:50 +08002782 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002786/**
2787 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2788 * @qc: Command on going
2789 *
2790 * Transfer Transfer data from/to the ATAPI device.
2791 *
2792 * LOCKING:
2793 * Inherited from caller.
2794 *
2795 */
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2798{
2799 struct ata_port *ap = qc->ap;
2800 struct ata_device *dev = qc->dev;
2801 unsigned int ireason, bc_lo, bc_hi, bytes;
2802 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2803
2804 ap->ops->tf_read(ap, &qc->tf);
2805 ireason = qc->tf.nsect;
2806 bc_lo = qc->tf.lbam;
2807 bc_hi = qc->tf.lbah;
2808 bytes = (bc_hi << 8) | bc_lo;
2809
2810 /* shall be cleared to zero, indicating xfer of data */
2811 if (ireason & (1 << 0))
2812 goto err_out;
2813
2814 /* make sure transfer direction matches expected */
2815 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2816 if (do_write != i_write)
2817 goto err_out;
2818
Albert Lee312f7da2005-09-27 17:38:03 +08002819 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 __atapi_pio_bytes(qc, bytes);
2822
2823 return;
2824
2825err_out:
2826 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2827 ap->id, dev->devno);
Albert Lee14be71f2005-09-27 17:36:35 +08002828 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
2831/**
2832 * ata_pio_sector -
2833 * @ap:
2834 *
2835 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002836 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 */
2838
2839static void ata_pio_block(struct ata_port *ap)
2840{
2841 struct ata_queued_cmd *qc;
2842 u8 status;
2843
2844 /*
2845 * This is purely hueristic. This is a fast path.
2846 * Sometimes when we enter, BSY will be cleared in
2847 * a chk-status or two. If not, the drive is probably seeking
2848 * or something. Snooze for a couple msecs, then
2849 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002850 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 */
2852 status = ata_busy_wait(ap, ATA_BUSY, 5);
2853 if (status & ATA_BUSY) {
2854 msleep(2);
2855 status = ata_busy_wait(ap, ATA_BUSY, 10);
2856 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002857 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2859 return;
2860 }
2861 }
2862
2863 qc = ata_qc_from_tag(ap, ap->active_tag);
2864 assert(qc != NULL);
2865
2866 if (is_atapi_taskfile(&qc->tf)) {
2867 /* no more data to transfer or unsupported ATAPI command */
2868 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08002869 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return;
2871 }
2872
2873 atapi_pio_bytes(qc);
2874 } else {
2875 /* handle BSY=0, DRQ=0 as error */
2876 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08002877 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return;
2879 }
2880
2881 ata_pio_sector(qc);
2882 }
2883}
2884
2885static void ata_pio_error(struct ata_port *ap)
2886{
2887 struct ata_queued_cmd *qc;
2888 u8 drv_stat;
2889
2890 qc = ata_qc_from_tag(ap, ap->active_tag);
2891 assert(qc != NULL);
2892
2893 drv_stat = ata_chk_status(ap);
2894 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2895 ap->id, drv_stat);
2896
Albert Lee14be71f2005-09-27 17:36:35 +08002897 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Tejun Heo40e8c822005-08-22 17:12:45 +09002899 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
2902static void ata_pio_task(void *_data)
2903{
2904 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002905 unsigned long timeout;
2906 int qc_completed;
2907
2908fsm_start:
2909 timeout = 0;
2910 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Albert Lee14be71f2005-09-27 17:36:35 +08002912 switch (ap->hsm_task_state) {
2913 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return;
2915
Albert Lee14be71f2005-09-27 17:36:35 +08002916 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 ata_pio_block(ap);
2918 break;
2919
Albert Lee14be71f2005-09-27 17:36:35 +08002920 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002921 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 break;
2923
Albert Lee14be71f2005-09-27 17:36:35 +08002924 case HSM_ST_POLL:
2925 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 timeout = ata_pio_poll(ap);
2927 break;
2928
Albert Lee14be71f2005-09-27 17:36:35 +08002929 case HSM_ST_TMOUT:
2930 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 ata_pio_error(ap);
2932 return;
2933 }
2934
2935 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002936 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
2937 else if (!qc_completed)
2938 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
2941static void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
2942 struct scsi_cmnd *cmd)
2943{
2944 DECLARE_COMPLETION(wait);
2945 struct ata_queued_cmd *qc;
2946 unsigned long flags;
2947 int rc;
2948
2949 DPRINTK("ATAPI request sense\n");
2950
2951 qc = ata_qc_new_init(ap, dev);
2952 BUG_ON(qc == NULL);
2953
2954 /* FIXME: is this needed? */
2955 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2956
2957 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2958 qc->dma_dir = DMA_FROM_DEVICE;
2959
Albert Lee21b1ed72005-04-29 17:34:59 +08002960 memset(&qc->cdb, 0, ap->cdb_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 qc->cdb[0] = REQUEST_SENSE;
2962 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2963
2964 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2965 qc->tf.command = ATA_CMD_PACKET;
2966
2967 qc->tf.protocol = ATA_PROT_ATAPI;
2968 qc->tf.lbam = (8 * 1024) & 0xff;
2969 qc->tf.lbah = (8 * 1024) >> 8;
2970 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2971
2972 qc->waiting = &wait;
2973 qc->complete_fn = ata_qc_complete_noop;
2974
2975 spin_lock_irqsave(&ap->host_set->lock, flags);
2976 rc = ata_qc_issue(qc);
2977 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2978
2979 if (rc)
2980 ata_port_disable(ap);
2981 else
2982 wait_for_completion(&wait);
2983
2984 DPRINTK("EXIT\n");
2985}
2986
2987/**
2988 * ata_qc_timeout - Handle timeout of queued command
2989 * @qc: Command that timed out
2990 *
2991 * Some part of the kernel (currently, only the SCSI layer)
2992 * has noticed that the active command on port @ap has not
2993 * completed after a specified length of time. Handle this
2994 * condition by disabling DMA (if necessary) and completing
2995 * transactions, with error if necessary.
2996 *
2997 * This also handles the case of the "lost interrupt", where
2998 * for some reason (possibly hardware bug, possibly driver bug)
2999 * an interrupt was not delivered to the driver, even though the
3000 * transaction completed successfully.
3001 *
3002 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003003 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 */
3005
3006static void ata_qc_timeout(struct ata_queued_cmd *qc)
3007{
3008 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003009 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 struct ata_device *dev = qc->dev;
3011 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003012 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 DPRINTK("ENTER\n");
3015
3016 /* FIXME: doesn't this conflict with timeout handling? */
3017 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3018 struct scsi_cmnd *cmd = qc->scsicmd;
3019
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02003020 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 /* finish completing original command */
Jeff Garzikb8f61532005-08-25 22:01:20 -04003023 spin_lock_irqsave(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 __ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003025 spin_unlock_irqrestore(&host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
3027 atapi_request_sense(ap, dev, cmd);
3028
3029 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3030 scsi_finish_command(cmd);
3031
3032 goto out;
3033 }
3034 }
3035
Jeff Garzikb8f61532005-08-25 22:01:20 -04003036 spin_lock_irqsave(&host_set->lock, flags);
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 /* hack alert! We cannot use the supplied completion
3039 * function from inside the ->eh_strategy_handler() thread.
3040 * libata is the only user of ->eh_strategy_handler() in
3041 * any kernel, so the default scsi_done() assumes it is
3042 * not being called from the SCSI EH.
3043 */
3044 qc->scsidone = scsi_finish_command;
3045
3046 switch (qc->tf.protocol) {
3047
3048 case ATA_PROT_DMA:
3049 case ATA_PROT_ATAPI_DMA:
3050 host_stat = ap->ops->bmdma_status(ap);
3051
3052 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003053 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
3055 /* fall through */
3056
3057 default:
3058 ata_altstatus(ap);
3059 drv_stat = ata_chk_status(ap);
3060
3061 /* ack bmdma irq events */
3062 ap->ops->irq_clear(ap);
3063
3064 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3065 ap->id, qc->tf.command, drv_stat, host_stat);
3066
Albert Lee312f7da2005-09-27 17:38:03 +08003067 ap->hsm_task_state = HSM_ST_IDLE;
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 /* complete taskfile transaction */
3070 ata_qc_complete(qc, drv_stat);
3071 break;
3072 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003073
3074 spin_unlock_irqrestore(&host_set->lock, flags);
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076out:
3077 DPRINTK("EXIT\n");
3078}
3079
3080/**
3081 * ata_eng_timeout - Handle timeout of queued command
3082 * @ap: Port on which timed-out command is active
3083 *
3084 * Some part of the kernel (currently, only the SCSI layer)
3085 * has noticed that the active command on port @ap has not
3086 * completed after a specified length of time. Handle this
3087 * condition by disabling DMA (if necessary) and completing
3088 * transactions, with error if necessary.
3089 *
3090 * This also handles the case of the "lost interrupt", where
3091 * for some reason (possibly hardware bug, possibly driver bug)
3092 * an interrupt was not delivered to the driver, even though the
3093 * transaction completed successfully.
3094 *
3095 * LOCKING:
3096 * Inherited from SCSI layer (none, can sleep)
3097 */
3098
3099void ata_eng_timeout(struct ata_port *ap)
3100{
3101 struct ata_queued_cmd *qc;
3102
3103 DPRINTK("ENTER\n");
3104
3105 qc = ata_qc_from_tag(ap, ap->active_tag);
3106 if (!qc) {
3107 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3108 ap->id);
3109 goto out;
3110 }
3111
3112 ata_qc_timeout(qc);
3113
3114out:
3115 DPRINTK("EXIT\n");
3116}
3117
3118/**
3119 * ata_qc_new - Request an available ATA command, for queueing
3120 * @ap: Port associated with device @dev
3121 * @dev: Device from whom we request an available command structure
3122 *
3123 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003124 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 */
3126
3127static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3128{
3129 struct ata_queued_cmd *qc = NULL;
3130 unsigned int i;
3131
3132 for (i = 0; i < ATA_MAX_QUEUE; i++)
3133 if (!test_and_set_bit(i, &ap->qactive)) {
3134 qc = ata_qc_from_tag(ap, i);
3135 break;
3136 }
3137
3138 if (qc)
3139 qc->tag = i;
3140
3141 return qc;
3142}
3143
3144/**
3145 * ata_qc_new_init - Request an available ATA command, and initialize it
3146 * @ap: Port associated with device @dev
3147 * @dev: Device from whom we request an available command structure
3148 *
3149 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003150 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 */
3152
3153struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3154 struct ata_device *dev)
3155{
3156 struct ata_queued_cmd *qc;
3157
3158 qc = ata_qc_new(ap);
3159 if (qc) {
3160 qc->sg = NULL;
3161 qc->flags = 0;
3162 qc->scsicmd = NULL;
3163 qc->ap = ap;
3164 qc->dev = dev;
3165 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3166 qc->nsect = 0;
3167 qc->nbytes = qc->curbytes = 0;
3168
3169 ata_tf_init(ap, &qc->tf, dev->devno);
3170
3171 if (dev->flags & ATA_DFLAG_LBA48)
3172 qc->tf.flags |= ATA_TFLAG_LBA48;
3173 }
3174
3175 return qc;
3176}
3177
3178static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
3179{
3180 return 0;
3181}
3182
3183static void __ata_qc_complete(struct ata_queued_cmd *qc)
3184{
3185 struct ata_port *ap = qc->ap;
3186 unsigned int tag, do_clear = 0;
3187
3188 qc->flags = 0;
3189 tag = qc->tag;
3190 if (likely(ata_tag_valid(tag))) {
3191 if (tag == ap->active_tag)
3192 ap->active_tag = ATA_TAG_POISON;
3193 qc->tag = ATA_TAG_POISON;
3194 do_clear = 1;
3195 }
3196
3197 if (qc->waiting) {
3198 struct completion *waiting = qc->waiting;
3199 qc->waiting = NULL;
3200 complete(waiting);
3201 }
3202
3203 if (likely(do_clear))
3204 clear_bit(tag, &ap->qactive);
3205}
3206
3207/**
3208 * ata_qc_free - free unused ata_queued_cmd
3209 * @qc: Command to complete
3210 *
3211 * Designed to free unused ata_queued_cmd object
3212 * in case something prevents using it.
3213 *
3214 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003215 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 *
3217 */
3218void ata_qc_free(struct ata_queued_cmd *qc)
3219{
3220 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3221 assert(qc->waiting == NULL); /* nothing should be waiting */
3222
3223 __ata_qc_complete(qc);
3224}
3225
3226/**
3227 * ata_qc_complete - Complete an active ATA command
3228 * @qc: Command to complete
Jeff Garzik0cba6322005-05-30 19:49:12 -04003229 * @drv_stat: ATA Status register contents
3230 *
3231 * Indicate to the mid and upper layers that an ATA
3232 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 *
3234 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003235 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 *
3237 */
3238
3239void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3240{
3241 int rc;
3242
3243 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3244 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3245
3246 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3247 ata_sg_clean(qc);
3248
Albert Lee3f3791d2005-08-16 14:25:38 +08003249 /* atapi: mark qc as inactive to prevent the interrupt handler
3250 * from completing the command twice later, before the error handler
3251 * is called. (when rc != 0 and atapi request sense is needed)
3252 */
3253 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 /* call completion callback */
3256 rc = qc->complete_fn(qc, drv_stat);
3257
3258 /* if callback indicates not to complete command (non-zero),
3259 * return immediately
3260 */
3261 if (rc != 0)
3262 return;
3263
3264 __ata_qc_complete(qc);
3265
3266 VPRINTK("EXIT\n");
3267}
3268
3269static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3270{
3271 struct ata_port *ap = qc->ap;
3272
3273 switch (qc->tf.protocol) {
3274 case ATA_PROT_DMA:
3275 case ATA_PROT_ATAPI_DMA:
3276 return 1;
3277
3278 case ATA_PROT_ATAPI:
3279 case ATA_PROT_PIO:
3280 case ATA_PROT_PIO_MULT:
3281 if (ap->flags & ATA_FLAG_PIO_DMA)
3282 return 1;
3283
3284 /* fall through */
3285
3286 default:
3287 return 0;
3288 }
3289
3290 /* never reached */
3291}
3292
3293/**
3294 * ata_qc_issue - issue taskfile to device
3295 * @qc: command to issue to device
3296 *
3297 * Prepare an ATA command to submission to device.
3298 * This includes mapping the data into a DMA-able
3299 * area, filling in the S/G table, and finally
3300 * writing the taskfile to hardware, starting the command.
3301 *
3302 * LOCKING:
3303 * spin_lock_irqsave(host_set lock)
3304 *
3305 * RETURNS:
3306 * Zero on success, negative on error.
3307 */
3308
3309int ata_qc_issue(struct ata_queued_cmd *qc)
3310{
3311 struct ata_port *ap = qc->ap;
3312
3313 if (ata_should_dma_map(qc)) {
3314 if (qc->flags & ATA_QCFLAG_SG) {
3315 if (ata_sg_setup(qc))
3316 goto err_out;
3317 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3318 if (ata_sg_setup_one(qc))
3319 goto err_out;
3320 }
3321 } else {
3322 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3323 }
3324
3325 ap->ops->qc_prep(qc);
3326
3327 qc->ap->active_tag = qc->tag;
3328 qc->flags |= ATA_QCFLAG_ACTIVE;
3329
3330 return ap->ops->qc_issue(qc);
3331
3332err_out:
3333 return -1;
3334}
3335
Edward Falk0baab862005-06-02 18:17:13 -04003336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337/**
3338 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3339 * @qc: command to issue to device
3340 *
3341 * Using various libata functions and hooks, this function
3342 * starts an ATA command. ATA commands are grouped into
3343 * classes called "protocols", and issuing each type of protocol
3344 * is slightly different.
3345 *
Edward Falk0baab862005-06-02 18:17:13 -04003346 * May be used as the qc_issue() entry in ata_port_operations.
3347 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 * LOCKING:
3349 * spin_lock_irqsave(host_set lock)
3350 *
3351 * RETURNS:
3352 * Zero on success, negative on error.
3353 */
3354
3355int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3356{
3357 struct ata_port *ap = qc->ap;
3358
Albert Leee50362e2005-09-27 17:39:50 +08003359 /* Use polling pio if the LLD doesn't handle
3360 * interrupt driven pio and atapi CDB interrupt.
3361 */
3362 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3363 switch (qc->tf.protocol) {
3364 case ATA_PROT_PIO:
3365 case ATA_PROT_ATAPI:
3366 case ATA_PROT_ATAPI_NODATA:
3367 qc->tf.flags |= ATA_TFLAG_POLLING;
3368 break;
3369 case ATA_PROT_ATAPI_DMA:
3370 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3371 BUG();
3372 break;
3373 default:
3374 break;
3375 }
3376 }
3377
Albert Lee312f7da2005-09-27 17:38:03 +08003378 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 ata_dev_select(ap, qc->dev->devno, 1, 0);
3380
Albert Lee312f7da2005-09-27 17:38:03 +08003381 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 switch (qc->tf.protocol) {
3383 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003384 if (qc->tf.flags & ATA_TFLAG_POLLING)
3385 ata_qc_set_polling(qc);
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003388 ap->hsm_task_state = HSM_ST_LAST;
3389
3390 if (qc->tf.flags & ATA_TFLAG_POLLING)
3391 queue_work(ata_wq, &ap->pio_task);
3392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 break;
3394
3395 case ATA_PROT_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003396 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3399 ap->ops->bmdma_setup(qc); /* set up bmdma */
3400 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003401 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 break;
3403
Albert Lee312f7da2005-09-27 17:38:03 +08003404 case ATA_PROT_PIO:
3405 if (qc->tf.flags & ATA_TFLAG_POLLING)
3406 ata_qc_set_polling(qc);
3407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003409
Albert Lee54f00382005-09-30 19:14:19 +08003410 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3411 /* PIO data out protocol */
3412 ap->hsm_task_state = HSM_ST_FIRST;
3413 queue_work(ata_wq, &ap->dataout_task);
3414
3415 /* always send first data block using
3416 * the ata_dataout_task() codepath.
3417 */
Albert Lee312f7da2005-09-27 17:38:03 +08003418 } else {
Albert Lee54f00382005-09-30 19:14:19 +08003419 /* PIO data in protocol */
3420 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08003421
Albert Lee54f00382005-09-30 19:14:19 +08003422 if (qc->tf.flags & ATA_TFLAG_POLLING)
3423 queue_work(ata_wq, &ap->pio_task);
Albert Lee312f7da2005-09-27 17:38:03 +08003424
Albert Lee54f00382005-09-30 19:14:19 +08003425 /* if polling, ata_pio_task() handles the rest.
3426 * otherwise, interrupt handler takes over from here.
3427 */
Albert Lee312f7da2005-09-27 17:38:03 +08003428 }
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 break;
3431
3432 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003434 if (qc->tf.flags & ATA_TFLAG_POLLING)
3435 ata_qc_set_polling(qc);
3436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003438 ap->hsm_task_state = HSM_ST_FIRST;
3439
3440 /* send cdb by polling if no cdb interrupt */
3441 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3442 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Leef9997be2005-09-30 19:09:31 +08003443 queue_work(ata_wq, &ap->dataout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 break;
3445
3446 case ATA_PROT_ATAPI_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003447 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3450 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003451 ap->hsm_task_state = HSM_ST_FIRST;
3452
3453 /* send cdb by polling if no cdb interrupt */
3454 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Leef9997be2005-09-30 19:09:31 +08003455 queue_work(ata_wq, &ap->dataout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 break;
3457
3458 default:
3459 WARN_ON(1);
3460 return -1;
3461 }
3462
3463 return 0;
3464}
3465
3466/**
Edward Falk0baab862005-06-02 18:17:13 -04003467 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 * @qc: Info associated with this ATA transaction.
3469 *
3470 * LOCKING:
3471 * spin_lock_irqsave(host_set lock)
3472 */
3473
3474static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3475{
3476 struct ata_port *ap = qc->ap;
3477 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3478 u8 dmactl;
3479 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3480
3481 /* load PRD table addr. */
3482 mb(); /* make sure PRD table writes are visible to controller */
3483 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3484
3485 /* specify data direction, triple-check start bit is clear */
3486 dmactl = readb(mmio + ATA_DMA_CMD);
3487 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3488 if (!rw)
3489 dmactl |= ATA_DMA_WR;
3490 writeb(dmactl, mmio + ATA_DMA_CMD);
3491
3492 /* issue r/w command */
3493 ap->ops->exec_command(ap, &qc->tf);
3494}
3495
3496/**
Alan Coxb73fc892005-08-26 16:03:19 +01003497 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 * @qc: Info associated with this ATA transaction.
3499 *
3500 * LOCKING:
3501 * spin_lock_irqsave(host_set lock)
3502 */
3503
3504static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3505{
3506 struct ata_port *ap = qc->ap;
3507 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3508 u8 dmactl;
3509
3510 /* start host DMA transaction */
3511 dmactl = readb(mmio + ATA_DMA_CMD);
3512 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3513
3514 /* Strictly, one may wish to issue a readb() here, to
3515 * flush the mmio write. However, control also passes
3516 * to the hardware at this point, and it will interrupt
3517 * us when we are to resume control. So, in effect,
3518 * we don't care when the mmio write flushes.
3519 * Further, a read of the DMA status register _immediately_
3520 * following the write may not be what certain flaky hardware
3521 * is expected, so I think it is best to not add a readb()
3522 * without first all the MMIO ATA cards/mobos.
3523 * Or maybe I'm just being paranoid.
3524 */
3525}
3526
3527/**
3528 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3529 * @qc: Info associated with this ATA transaction.
3530 *
3531 * LOCKING:
3532 * spin_lock_irqsave(host_set lock)
3533 */
3534
3535static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3536{
3537 struct ata_port *ap = qc->ap;
3538 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3539 u8 dmactl;
3540
3541 /* load PRD table addr. */
3542 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3543
3544 /* specify data direction, triple-check start bit is clear */
3545 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3546 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3547 if (!rw)
3548 dmactl |= ATA_DMA_WR;
3549 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3550
3551 /* issue r/w command */
3552 ap->ops->exec_command(ap, &qc->tf);
3553}
3554
3555/**
3556 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3557 * @qc: Info associated with this ATA transaction.
3558 *
3559 * LOCKING:
3560 * spin_lock_irqsave(host_set lock)
3561 */
3562
3563static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3564{
3565 struct ata_port *ap = qc->ap;
3566 u8 dmactl;
3567
3568 /* start host DMA transaction */
3569 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3570 outb(dmactl | ATA_DMA_START,
3571 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3572}
3573
Edward Falk0baab862005-06-02 18:17:13 -04003574
3575/**
3576 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3577 * @qc: Info associated with this ATA transaction.
3578 *
3579 * Writes the ATA_DMA_START flag to the DMA command register.
3580 *
3581 * May be used as the bmdma_start() entry in ata_port_operations.
3582 *
3583 * LOCKING:
3584 * spin_lock_irqsave(host_set lock)
3585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586void ata_bmdma_start(struct ata_queued_cmd *qc)
3587{
3588 if (qc->ap->flags & ATA_FLAG_MMIO)
3589 ata_bmdma_start_mmio(qc);
3590 else
3591 ata_bmdma_start_pio(qc);
3592}
3593
Edward Falk0baab862005-06-02 18:17:13 -04003594
3595/**
3596 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3597 * @qc: Info associated with this ATA transaction.
3598 *
3599 * Writes address of PRD table to device's PRD Table Address
3600 * register, sets the DMA control register, and calls
3601 * ops->exec_command() to start the transfer.
3602 *
3603 * May be used as the bmdma_setup() entry in ata_port_operations.
3604 *
3605 * LOCKING:
3606 * spin_lock_irqsave(host_set lock)
3607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608void ata_bmdma_setup(struct ata_queued_cmd *qc)
3609{
3610 if (qc->ap->flags & ATA_FLAG_MMIO)
3611 ata_bmdma_setup_mmio(qc);
3612 else
3613 ata_bmdma_setup_pio(qc);
3614}
3615
Edward Falk0baab862005-06-02 18:17:13 -04003616
3617/**
3618 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003619 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003620 *
3621 * Clear interrupt and error flags in DMA status register.
3622 *
3623 * May be used as the irq_clear() entry in ata_port_operations.
3624 *
3625 * LOCKING:
3626 * spin_lock_irqsave(host_set lock)
3627 */
3628
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629void ata_bmdma_irq_clear(struct ata_port *ap)
3630{
3631 if (ap->flags & ATA_FLAG_MMIO) {
3632 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3633 writeb(readb(mmio), mmio);
3634 } else {
3635 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3636 outb(inb(addr), addr);
3637 }
3638
3639}
3640
Edward Falk0baab862005-06-02 18:17:13 -04003641
3642/**
3643 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003644 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003645 *
3646 * Read and return BMDMA status register.
3647 *
3648 * May be used as the bmdma_status() entry in ata_port_operations.
3649 *
3650 * LOCKING:
3651 * spin_lock_irqsave(host_set lock)
3652 */
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654u8 ata_bmdma_status(struct ata_port *ap)
3655{
3656 u8 host_stat;
3657 if (ap->flags & ATA_FLAG_MMIO) {
3658 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3659 host_stat = readb(mmio + ATA_DMA_STATUS);
3660 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003661 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 return host_stat;
3663}
3664
Edward Falk0baab862005-06-02 18:17:13 -04003665
3666/**
3667 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003668 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003669 *
3670 * Clears the ATA_DMA_START flag in the dma control register
3671 *
3672 * May be used as the bmdma_stop() entry in ata_port_operations.
3673 *
3674 * LOCKING:
3675 * spin_lock_irqsave(host_set lock)
3676 */
3677
Alan Coxb73fc892005-08-26 16:03:19 +01003678void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Alan Coxb73fc892005-08-26 16:03:19 +01003680 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 if (ap->flags & ATA_FLAG_MMIO) {
3682 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3683
3684 /* clear start/stop bit */
3685 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3686 mmio + ATA_DMA_CMD);
3687 } else {
3688 /* clear start/stop bit */
3689 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3690 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3691 }
3692
3693 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3694 ata_altstatus(ap); /* dummy read */
3695}
3696
3697/**
Albert Lee312f7da2005-09-27 17:38:03 +08003698 * atapi_send_cdb - Write CDB bytes to hardware
3699 * @ap: Port to which ATAPI device is attached.
3700 * @qc: Taskfile currently active
3701 *
3702 * When device has indicated its readiness to accept
3703 * a CDB, this function is called. Send the CDB.
3704 *
3705 * LOCKING:
3706 * caller.
3707 */
3708
3709static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3710{
3711 /* send SCSI cdb */
3712 DPRINTK("send cdb\n");
3713 assert(ap->cdb_len >= 12);
3714
3715 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3716 ata_altstatus(ap); /* flush */
3717
3718 switch (qc->tf.protocol) {
3719 case ATA_PROT_ATAPI:
3720 ap->hsm_task_state = HSM_ST;
3721 break;
3722 case ATA_PROT_ATAPI_NODATA:
3723 ap->hsm_task_state = HSM_ST_LAST;
3724 break;
3725 case ATA_PROT_ATAPI_DMA:
3726 ap->hsm_task_state = HSM_ST_LAST;
3727 /* initiate bmdma */
3728 ap->ops->bmdma_start(qc);
3729 break;
3730 }
3731}
3732
3733/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 * ata_host_intr - Handle host interrupt for given (port, task)
3735 * @ap: Port on which interrupt arrived (possibly...)
3736 * @qc: Taskfile currently active in engine
3737 *
3738 * Handle host interrupt for given queued command. Currently,
3739 * only DMA interrupts are handled. All other commands are
3740 * handled via polling with interrupts disabled (nIEN bit).
3741 *
3742 * LOCKING:
3743 * spin_lock_irqsave(host_set lock)
3744 *
3745 * RETURNS:
3746 * One if interrupt was handled, zero if not (shared irq).
3747 */
3748
3749inline unsigned int ata_host_intr (struct ata_port *ap,
3750 struct ata_queued_cmd *qc)
3751{
Albert Lee312f7da2005-09-27 17:38:03 +08003752 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Albert Lee312f7da2005-09-27 17:38:03 +08003754 VPRINTK("ata%u: protocol %d task_state %d\n",
3755 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Albert Lee312f7da2005-09-27 17:38:03 +08003757 /* Check whether we are expecting interrupt in this state */
3758 switch (ap->hsm_task_state) {
3759 case HSM_ST_FIRST:
3760 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
3761 * The flag was turned on only for atapi devices.
3762 * No need to check is_atapi_taskfile(&qc->tf) again.
3763 */
3764 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08003766 break;
3767 case HSM_ST_LAST:
3768 if (qc->tf.protocol == ATA_PROT_DMA ||
3769 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
3770 /* check status of DMA engine */
3771 host_stat = ap->ops->bmdma_status(ap);
3772 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Albert Lee312f7da2005-09-27 17:38:03 +08003774 /* if it's not our irq... */
3775 if (!(host_stat & ATA_DMA_INTR))
3776 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Albert Lee312f7da2005-09-27 17:38:03 +08003778 /* before we do anything else, clear DMA-Start bit */
3779 ap->ops->bmdma_stop(qc);
3780 }
3781 break;
3782 case HSM_ST:
3783 break;
3784 default:
3785 goto idle_irq;
3786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Albert Lee312f7da2005-09-27 17:38:03 +08003788 /* check altstatus */
3789 status = ata_altstatus(ap);
3790 if (status & ATA_BUSY)
3791 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Albert Lee312f7da2005-09-27 17:38:03 +08003793 /* check main status, clearing INTRQ */
3794 status = ata_chk_status(ap);
3795 if (unlikely(status & ATA_BUSY))
3796 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Albert Lee312f7da2005-09-27 17:38:03 +08003798 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
3799 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
3800
3801 /* ack bmdma irq events */
3802 ap->ops->irq_clear(ap);
3803
3804 /* check error */
3805 if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
3806 ap->hsm_task_state = HSM_ST_ERR;
3807
3808fsm_start:
3809 switch (ap->hsm_task_state) {
3810 case HSM_ST_FIRST:
3811 /* Some pre-ATAPI-4 devices assert INTRQ
3812 * at this state when ready to receive CDB.
3813 */
3814
3815 /* check device status */
3816 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
3817 /* Wrong status. Let EH handle this */
3818 ap->hsm_task_state = HSM_ST_ERR;
3819 goto fsm_start;
3820 }
3821
3822 atapi_send_cdb(ap, qc);
3823
3824 break;
3825
3826 case HSM_ST:
3827 /* complete command or read/write the data register */
3828 if (qc->tf.protocol == ATA_PROT_ATAPI) {
3829 /* ATAPI PIO protocol */
3830 if ((status & ATA_DRQ) == 0) {
3831 /* no more data to transfer */
3832 ap->hsm_task_state = HSM_ST_LAST;
3833 goto fsm_start;
3834 }
3835
3836 atapi_pio_bytes(qc);
3837
3838 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
3839 /* bad ireason reported by device */
3840 goto fsm_start;
3841
3842 } else {
3843 /* ATA PIO protocol */
3844 if (unlikely((status & ATA_DRQ) == 0)) {
3845 /* handle BSY=0, DRQ=0 as error */
3846 ap->hsm_task_state = HSM_ST_ERR;
3847 goto fsm_start;
3848 }
3849
3850 ata_pio_sector(qc);
3851
3852 if (ap->hsm_task_state == HSM_ST_LAST &&
3853 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
3854 /* all data read */
3855 ata_altstatus(ap);
3856 status = ata_chk_status(ap);
3857 goto fsm_start;
3858 }
3859 }
3860
3861 ata_altstatus(ap); /* flush */
3862 break;
3863
3864 case HSM_ST_LAST:
3865 if (unlikely(status & ATA_DRQ)) {
3866 /* handle DRQ=1 as error */
3867 ap->hsm_task_state = HSM_ST_ERR;
3868 goto fsm_start;
3869 }
3870
3871 /* no more data to transfer */
3872 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
3873 ap->id, status);
3874
3875 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
3877 /* complete taskfile transaction */
3878 ata_qc_complete(qc, status);
3879 break;
3880
Albert Lee312f7da2005-09-27 17:38:03 +08003881 case HSM_ST_ERR:
3882 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
3883 ap->id, status, host_stat);
3884
3885 ap->hsm_task_state = HSM_ST_IDLE;
3886 ata_qc_complete(qc, status | ATA_ERR);
3887 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 default:
3889 goto idle_irq;
3890 }
3891
3892 return 1; /* irq handled */
3893
3894idle_irq:
3895 ap->stats.idle_irq++;
3896
3897#ifdef ATA_IRQ_TRAP
3898 if ((ap->stats.idle_irq % 1000) == 0) {
3899 handled = 1;
3900 ata_irq_ack(ap, 0); /* debug trap */
3901 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3902 }
3903#endif
3904 return 0; /* irq not handled */
3905}
3906
3907/**
3908 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04003909 * @irq: irq line (unused)
3910 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 * @regs: unused
3912 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04003913 * Default interrupt handler for PCI IDE devices. Calls
3914 * ata_host_intr() for each port that is not disabled.
3915 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003917 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 *
3919 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003920 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 *
3922 */
3923
3924irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3925{
3926 struct ata_host_set *host_set = dev_instance;
3927 unsigned int i;
3928 unsigned int handled = 0;
3929 unsigned long flags;
3930
3931 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3932 spin_lock_irqsave(&host_set->lock, flags);
3933
3934 for (i = 0; i < host_set->n_ports; i++) {
3935 struct ata_port *ap;
3936
3937 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09003938 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08003939 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 struct ata_queued_cmd *qc;
3941
3942 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08003943 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08003944 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 handled |= ata_host_intr(ap, qc);
3946 }
3947 }
3948
3949 spin_unlock_irqrestore(&host_set->lock, flags);
3950
3951 return IRQ_RETVAL(handled);
3952}
3953
3954/**
Albert Leef9997be2005-09-30 19:09:31 +08003955 * ata_dataout_task - Write first data block to hardware
3956 * @_data: Port to which ATA/ATAPI device is attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 *
3958 * When device has indicated its readiness to accept
Albert Leef9997be2005-09-30 19:09:31 +08003959 * the data, this function sends out the CDB or
3960 * the first data block by PIO.
3961 * After this,
3962 * - If polling, ata_pio_task() handles the rest.
3963 * - Otherwise, interrupt handler takes over.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 *
3965 * LOCKING:
3966 * Kernel thread context (may sleep)
3967 */
3968
Albert Leef9997be2005-09-30 19:09:31 +08003969static void ata_dataout_task(void *_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970{
3971 struct ata_port *ap = _data;
3972 struct ata_queued_cmd *qc;
3973 u8 status;
Albert Lee312f7da2005-09-27 17:38:03 +08003974 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
3976 qc = ata_qc_from_tag(ap, ap->active_tag);
3977 assert(qc != NULL);
3978 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3979
3980 /* sleep-wait for BSY to clear */
3981 DPRINTK("busy wait\n");
Albert Leef9997be2005-09-30 19:09:31 +08003982 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 goto err_out;
3984
3985 /* make sure DRQ is set */
3986 status = ata_chk_status(ap);
3987 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3988 goto err_out;
3989
Albert Lee312f7da2005-09-27 17:38:03 +08003990 /* Send the CDB (atapi) or the first data block (ata pio out).
3991 * During the state transition, interrupt handler shouldn't
3992 * be invoked before the data transfer is complete and
3993 * hsm_task_state is changed. Hence, the following locking.
3994 */
3995 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Albert Lee86a73972005-09-30 19:11:35 +08003997 if (qc->tf.protocol == ATA_PROT_PIO) {
Albert Lee312f7da2005-09-27 17:38:03 +08003998 /* PIO data out protocol.
3999 * send first data block.
4000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Albert Lee312f7da2005-09-27 17:38:03 +08004002 /* ata_pio_sector() might change the state to HSM_ST_LAST.
4003 * so, the state is changed here before ata_pio_sector().
4004 */
Albert Lee14be71f2005-09-27 17:36:35 +08004005 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08004006 ata_pio_sector(qc);
4007 ata_altstatus(ap); /* flush */
Albert Lee54f00382005-09-30 19:14:19 +08004008 } else
Albert Lee86a73972005-09-30 19:11:35 +08004009 /* send CDB */
4010 atapi_send_cdb(ap, qc);
4011
Albert Lee54f00382005-09-30 19:14:19 +08004012 /* if polling, ata_pio_task() handles the rest.
4013 * otherwise, interrupt handler takes over from here.
4014 */
4015 if (qc->tf.flags & ATA_TFLAG_POLLING)
4016 queue_work(ata_wq, &ap->pio_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
Albert Lee312f7da2005-09-27 17:38:03 +08004018 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 return;
4021
4022err_out:
Albert Lee312f7da2005-09-27 17:38:03 +08004023 ata_pio_error(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
4025
Edward Falk0baab862005-06-02 18:17:13 -04004026
4027/**
4028 * ata_port_start - Set port up for dma.
4029 * @ap: Port to initialize
4030 *
4031 * Called just after data structures for each port are
4032 * initialized. Allocates space for PRD table.
4033 *
4034 * May be used as the port_start() entry in ata_port_operations.
4035 *
4036 * LOCKING:
4037 */
4038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039int ata_port_start (struct ata_port *ap)
4040{
4041 struct device *dev = ap->host_set->dev;
4042
4043 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4044 if (!ap->prd)
4045 return -ENOMEM;
4046
4047 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4048
4049 return 0;
4050}
4051
Edward Falk0baab862005-06-02 18:17:13 -04004052
4053/**
4054 * ata_port_stop - Undo ata_port_start()
4055 * @ap: Port to shut down
4056 *
4057 * Frees the PRD table.
4058 *
4059 * May be used as the port_stop() entry in ata_port_operations.
4060 *
4061 * LOCKING:
4062 */
4063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064void ata_port_stop (struct ata_port *ap)
4065{
4066 struct device *dev = ap->host_set->dev;
4067
4068 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4069}
4070
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004071void ata_host_stop (struct ata_host_set *host_set)
4072{
4073 if (host_set->mmio_base)
4074 iounmap(host_set->mmio_base);
4075}
4076
4077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078/**
4079 * ata_host_remove - Unregister SCSI host structure with upper layers
4080 * @ap: Port to unregister
4081 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4082 *
4083 * LOCKING:
4084 */
4085
4086static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4087{
4088 struct Scsi_Host *sh = ap->host;
4089
4090 DPRINTK("ENTER\n");
4091
4092 if (do_unregister)
4093 scsi_remove_host(sh);
4094
4095 ap->ops->port_stop(ap);
4096}
4097
4098/**
4099 * ata_host_init - Initialize an ata_port structure
4100 * @ap: Structure to initialize
4101 * @host: associated SCSI mid-layer structure
4102 * @host_set: Collection of hosts to which @ap belongs
4103 * @ent: Probe information provided by low-level driver
4104 * @port_no: Port number associated with this ata_port
4105 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004106 * Initialize a new ata_port structure, and its associated
4107 * scsi_host.
4108 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004110 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 *
4112 */
4113
4114static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4115 struct ata_host_set *host_set,
4116 struct ata_probe_ent *ent, unsigned int port_no)
4117{
4118 unsigned int i;
4119
4120 host->max_id = 16;
4121 host->max_lun = 1;
4122 host->max_channel = 1;
4123 host->unique_id = ata_unique_id++;
4124 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 scsi_assign_lock(host, &host_set->lock);
4127
4128 ap->flags = ATA_FLAG_PORT_DISABLED;
4129 ap->id = host->unique_id;
4130 ap->host = host;
4131 ap->ctl = ATA_DEVCTL_OBS;
4132 ap->host_set = host_set;
4133 ap->port_no = port_no;
4134 ap->hard_port_no =
4135 ent->legacy_mode ? ent->hard_port_no : port_no;
4136 ap->pio_mask = ent->pio_mask;
4137 ap->mwdma_mask = ent->mwdma_mask;
4138 ap->udma_mask = ent->udma_mask;
4139 ap->flags |= ent->host_flags;
4140 ap->ops = ent->port_ops;
4141 ap->cbl = ATA_CBL_NONE;
4142 ap->active_tag = ATA_TAG_POISON;
4143 ap->last_ctl = 0xFF;
4144
Albert Leef9997be2005-09-30 19:09:31 +08004145 INIT_WORK(&ap->dataout_task, ata_dataout_task, ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4147
4148 for (i = 0; i < ATA_MAX_DEVICES; i++)
4149 ap->device[i].devno = i;
4150
4151#ifdef ATA_IRQ_TRAP
4152 ap->stats.unhandled_irq = 1;
4153 ap->stats.idle_irq = 1;
4154#endif
4155
4156 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4157}
4158
4159/**
4160 * ata_host_add - Attach low-level ATA driver to system
4161 * @ent: Information provided by low-level driver
4162 * @host_set: Collections of ports to which we add
4163 * @port_no: Port number associated with this host
4164 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004165 * Attach low-level ATA driver to system.
4166 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004168 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 *
4170 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004171 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 *
4173 */
4174
4175static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4176 struct ata_host_set *host_set,
4177 unsigned int port_no)
4178{
4179 struct Scsi_Host *host;
4180 struct ata_port *ap;
4181 int rc;
4182
4183 DPRINTK("ENTER\n");
4184 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4185 if (!host)
4186 return NULL;
4187
4188 ap = (struct ata_port *) &host->hostdata[0];
4189
4190 ata_host_init(ap, host, host_set, ent, port_no);
4191
4192 rc = ap->ops->port_start(ap);
4193 if (rc)
4194 goto err_out;
4195
4196 return ap;
4197
4198err_out:
4199 scsi_host_put(host);
4200 return NULL;
4201}
4202
4203/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004204 * ata_device_add - Register hardware device with ATA and SCSI layers
4205 * @ent: Probe information describing hardware device to be registered
4206 *
4207 * This function processes the information provided in the probe
4208 * information struct @ent, allocates the necessary ATA and SCSI
4209 * host information structures, initializes them, and registers
4210 * everything with requisite kernel subsystems.
4211 *
4212 * This function requests irqs, probes the ATA bus, and probes
4213 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 *
4215 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004216 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 *
4218 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004219 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 *
4221 */
4222
4223int ata_device_add(struct ata_probe_ent *ent)
4224{
4225 unsigned int count = 0, i;
4226 struct device *dev = ent->dev;
4227 struct ata_host_set *host_set;
4228
4229 DPRINTK("ENTER\n");
4230 /* alloc a container for our list of ATA ports (buses) */
4231 host_set = kmalloc(sizeof(struct ata_host_set) +
4232 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4233 if (!host_set)
4234 return 0;
4235 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4236 spin_lock_init(&host_set->lock);
4237
4238 host_set->dev = dev;
4239 host_set->n_ports = ent->n_ports;
4240 host_set->irq = ent->irq;
4241 host_set->mmio_base = ent->mmio_base;
4242 host_set->private_data = ent->private_data;
4243 host_set->ops = ent->port_ops;
4244
4245 /* register each port bound to this device */
4246 for (i = 0; i < ent->n_ports; i++) {
4247 struct ata_port *ap;
4248 unsigned long xfer_mode_mask;
4249
4250 ap = ata_host_add(ent, host_set, i);
4251 if (!ap)
4252 goto err_out;
4253
4254 host_set->ports[i] = ap;
4255 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4256 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4257 (ap->pio_mask << ATA_SHIFT_PIO);
4258
4259 /* print per-port info to dmesg */
4260 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4261 "bmdma 0x%lX irq %lu\n",
4262 ap->id,
4263 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4264 ata_mode_string(xfer_mode_mask),
4265 ap->ioaddr.cmd_addr,
4266 ap->ioaddr.ctl_addr,
4267 ap->ioaddr.bmdma_addr,
4268 ent->irq);
4269
4270 ata_chk_status(ap);
4271 host_set->ops->irq_clear(ap);
4272 count++;
4273 }
4274
4275 if (!count) {
4276 kfree(host_set);
4277 return 0;
4278 }
4279
4280 /* obtain irq, that is shared between channels */
4281 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4282 DRV_NAME, host_set))
4283 goto err_out;
4284
4285 /* perform each probe synchronously */
4286 DPRINTK("probe begin\n");
4287 for (i = 0; i < count; i++) {
4288 struct ata_port *ap;
4289 int rc;
4290
4291 ap = host_set->ports[i];
4292
4293 DPRINTK("ata%u: probe begin\n", ap->id);
4294 rc = ata_bus_probe(ap);
4295 DPRINTK("ata%u: probe end\n", ap->id);
4296
4297 if (rc) {
4298 /* FIXME: do something useful here?
4299 * Current libata behavior will
4300 * tear down everything when
4301 * the module is removed
4302 * or the h/w is unplugged.
4303 */
4304 }
4305
4306 rc = scsi_add_host(ap->host, dev);
4307 if (rc) {
4308 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4309 ap->id);
4310 /* FIXME: do something useful here */
4311 /* FIXME: handle unconditional calls to
4312 * scsi_scan_host and ata_host_remove, below,
4313 * at the very least
4314 */
4315 }
4316 }
4317
4318 /* probes are done, now scan each port's disk(s) */
4319 DPRINTK("probe begin\n");
4320 for (i = 0; i < count; i++) {
4321 struct ata_port *ap = host_set->ports[i];
4322
4323 scsi_scan_host(ap->host);
4324 }
4325
4326 dev_set_drvdata(dev, host_set);
4327
4328 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4329 return ent->n_ports; /* success */
4330
4331err_out:
4332 for (i = 0; i < count; i++) {
4333 ata_host_remove(host_set->ports[i], 1);
4334 scsi_host_put(host_set->ports[i]->host);
4335 }
4336 kfree(host_set);
4337 VPRINTK("EXIT, returning 0\n");
4338 return 0;
4339}
4340
4341/**
Alan Cox17b14452005-09-15 15:44:00 +01004342 * ata_host_set_remove - PCI layer callback for device removal
4343 * @host_set: ATA host set that was removed
4344 *
4345 * Unregister all objects associated with this host set. Free those
4346 * objects.
4347 *
4348 * LOCKING:
4349 * Inherited from calling layer (may sleep).
4350 */
4351
4352
4353void ata_host_set_remove(struct ata_host_set *host_set)
4354{
4355 struct ata_port *ap;
4356 unsigned int i;
4357
4358 for (i = 0; i < host_set->n_ports; i++) {
4359 ap = host_set->ports[i];
4360 scsi_remove_host(ap->host);
4361 }
4362
4363 free_irq(host_set->irq, host_set);
4364
4365 for (i = 0; i < host_set->n_ports; i++) {
4366 ap = host_set->ports[i];
4367
4368 ata_scsi_release(ap->host);
4369
4370 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4371 struct ata_ioports *ioaddr = &ap->ioaddr;
4372
4373 if (ioaddr->cmd_addr == 0x1f0)
4374 release_region(0x1f0, 8);
4375 else if (ioaddr->cmd_addr == 0x170)
4376 release_region(0x170, 8);
4377 }
4378
4379 scsi_host_put(ap->host);
4380 }
4381
4382 if (host_set->ops->host_stop)
4383 host_set->ops->host_stop(host_set);
4384
4385 kfree(host_set);
4386}
4387
4388/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 * ata_scsi_release - SCSI layer callback hook for host unload
4390 * @host: libata host to be unloaded
4391 *
4392 * Performs all duties necessary to shut down a libata port...
4393 * Kill port kthread, disable port, and release resources.
4394 *
4395 * LOCKING:
4396 * Inherited from SCSI layer.
4397 *
4398 * RETURNS:
4399 * One.
4400 */
4401
4402int ata_scsi_release(struct Scsi_Host *host)
4403{
4404 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4405
4406 DPRINTK("ENTER\n");
4407
4408 ap->ops->port_disable(ap);
4409 ata_host_remove(ap, 0);
4410
4411 DPRINTK("EXIT\n");
4412 return 1;
4413}
4414
4415/**
4416 * ata_std_ports - initialize ioaddr with standard port offsets.
4417 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004418 *
4419 * Utility function which initializes data_addr, error_addr,
4420 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4421 * device_addr, status_addr, and command_addr to standard offsets
4422 * relative to cmd_addr.
4423 *
4424 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 */
Edward Falk0baab862005-06-02 18:17:13 -04004426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427void ata_std_ports(struct ata_ioports *ioaddr)
4428{
4429 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4430 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4431 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4432 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4433 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4434 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4435 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4436 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4437 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4438 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4439}
4440
4441static struct ata_probe_ent *
4442ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4443{
4444 struct ata_probe_ent *probe_ent;
4445
4446 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4447 if (!probe_ent) {
4448 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4449 kobject_name(&(dev->kobj)));
4450 return NULL;
4451 }
4452
4453 memset(probe_ent, 0, sizeof(*probe_ent));
4454
4455 INIT_LIST_HEAD(&probe_ent->node);
4456 probe_ent->dev = dev;
4457
4458 probe_ent->sht = port->sht;
4459 probe_ent->host_flags = port->host_flags;
4460 probe_ent->pio_mask = port->pio_mask;
4461 probe_ent->mwdma_mask = port->mwdma_mask;
4462 probe_ent->udma_mask = port->udma_mask;
4463 probe_ent->port_ops = port->port_ops;
4464
4465 return probe_ent;
4466}
4467
Edward Falk0baab862005-06-02 18:17:13 -04004468
4469
Jeff Garzik374b1872005-08-30 05:42:52 -04004470#ifdef CONFIG_PCI
4471
4472void ata_pci_host_stop (struct ata_host_set *host_set)
4473{
4474 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4475
4476 pci_iounmap(pdev, host_set->mmio_base);
4477}
4478
Edward Falk0baab862005-06-02 18:17:13 -04004479/**
4480 * ata_pci_init_native_mode - Initialize native-mode driver
4481 * @pdev: pci device to be initialized
4482 * @port: array[2] of pointers to port info structures.
4483 *
4484 * Utility function which allocates and initializes an
4485 * ata_probe_ent structure for a standard dual-port
4486 * PIO-based IDE controller. The returned ata_probe_ent
4487 * structure can be passed to ata_device_add(). The returned
4488 * ata_probe_ent structure should then be freed with kfree().
4489 */
4490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491struct ata_probe_ent *
4492ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port)
4493{
4494 struct ata_probe_ent *probe_ent =
4495 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4496 if (!probe_ent)
4497 return NULL;
4498
4499 probe_ent->n_ports = 2;
4500 probe_ent->irq = pdev->irq;
4501 probe_ent->irq_flags = SA_SHIRQ;
4502
4503 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
4504 probe_ent->port[0].altstatus_addr =
4505 probe_ent->port[0].ctl_addr =
4506 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4507 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4508
4509 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
4510 probe_ent->port[1].altstatus_addr =
4511 probe_ent->port[1].ctl_addr =
4512 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4513 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4514
4515 ata_std_ports(&probe_ent->port[0]);
4516 ata_std_ports(&probe_ent->port[1]);
4517
4518 return probe_ent;
4519}
4520
4521static struct ata_probe_ent *
4522ata_pci_init_legacy_mode(struct pci_dev *pdev, struct ata_port_info **port,
4523 struct ata_probe_ent **ppe2)
4524{
4525 struct ata_probe_ent *probe_ent, *probe_ent2;
4526
4527 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4528 if (!probe_ent)
4529 return NULL;
4530 probe_ent2 = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[1]);
4531 if (!probe_ent2) {
4532 kfree(probe_ent);
4533 return NULL;
4534 }
4535
4536 probe_ent->n_ports = 1;
4537 probe_ent->irq = 14;
4538
4539 probe_ent->hard_port_no = 0;
4540 probe_ent->legacy_mode = 1;
4541
4542 probe_ent2->n_ports = 1;
4543 probe_ent2->irq = 15;
4544
4545 probe_ent2->hard_port_no = 1;
4546 probe_ent2->legacy_mode = 1;
4547
4548 probe_ent->port[0].cmd_addr = 0x1f0;
4549 probe_ent->port[0].altstatus_addr =
4550 probe_ent->port[0].ctl_addr = 0x3f6;
4551 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4552
4553 probe_ent2->port[0].cmd_addr = 0x170;
4554 probe_ent2->port[0].altstatus_addr =
4555 probe_ent2->port[0].ctl_addr = 0x376;
4556 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
4557
4558 ata_std_ports(&probe_ent->port[0]);
4559 ata_std_ports(&probe_ent2->port[0]);
4560
4561 *ppe2 = probe_ent2;
4562 return probe_ent;
4563}
4564
4565/**
4566 * ata_pci_init_one - Initialize/register PCI IDE host controller
4567 * @pdev: Controller to be initialized
4568 * @port_info: Information from low-level host driver
4569 * @n_ports: Number of ports attached to host controller
4570 *
Edward Falk0baab862005-06-02 18:17:13 -04004571 * This is a helper function which can be called from a driver's
4572 * xxx_init_one() probe function if the hardware uses traditional
4573 * IDE taskfile registers.
4574 *
4575 * This function calls pci_enable_device(), reserves its register
4576 * regions, sets the dma mask, enables bus master mode, and calls
4577 * ata_device_add()
4578 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 * LOCKING:
4580 * Inherited from PCI layer (may sleep).
4581 *
4582 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004583 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 *
4585 */
4586
4587int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4588 unsigned int n_ports)
4589{
4590 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
4591 struct ata_port_info *port[2];
4592 u8 tmp8, mask;
4593 unsigned int legacy_mode = 0;
4594 int disable_dev_on_err = 1;
4595 int rc;
4596
4597 DPRINTK("ENTER\n");
4598
4599 port[0] = port_info[0];
4600 if (n_ports > 1)
4601 port[1] = port_info[1];
4602 else
4603 port[1] = port[0];
4604
4605 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4606 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4607 /* TODO: support transitioning to native mode? */
4608 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4609 mask = (1 << 2) | (1 << 0);
4610 if ((tmp8 & mask) != mask)
4611 legacy_mode = (1 << 3);
4612 }
4613
4614 /* FIXME... */
4615 if ((!legacy_mode) && (n_ports > 1)) {
4616 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
4617 return -EINVAL;
4618 }
4619
4620 rc = pci_enable_device(pdev);
4621 if (rc)
4622 return rc;
4623
4624 rc = pci_request_regions(pdev, DRV_NAME);
4625 if (rc) {
4626 disable_dev_on_err = 0;
4627 goto err_out;
4628 }
4629
4630 if (legacy_mode) {
4631 if (!request_region(0x1f0, 8, "libata")) {
4632 struct resource *conflict, res;
4633 res.start = 0x1f0;
4634 res.end = 0x1f0 + 8 - 1;
4635 conflict = ____request_resource(&ioport_resource, &res);
4636 if (!strcmp(conflict->name, "libata"))
4637 legacy_mode |= (1 << 0);
4638 else {
4639 disable_dev_on_err = 0;
4640 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4641 }
4642 } else
4643 legacy_mode |= (1 << 0);
4644
4645 if (!request_region(0x170, 8, "libata")) {
4646 struct resource *conflict, res;
4647 res.start = 0x170;
4648 res.end = 0x170 + 8 - 1;
4649 conflict = ____request_resource(&ioport_resource, &res);
4650 if (!strcmp(conflict->name, "libata"))
4651 legacy_mode |= (1 << 1);
4652 else {
4653 disable_dev_on_err = 0;
4654 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4655 }
4656 } else
4657 legacy_mode |= (1 << 1);
4658 }
4659
4660 /* we have legacy mode, but all ports are unavailable */
4661 if (legacy_mode == (1 << 3)) {
4662 rc = -EBUSY;
4663 goto err_out_regions;
4664 }
4665
4666 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4667 if (rc)
4668 goto err_out_regions;
4669 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4670 if (rc)
4671 goto err_out_regions;
4672
4673 if (legacy_mode) {
4674 probe_ent = ata_pci_init_legacy_mode(pdev, port, &probe_ent2);
4675 } else
4676 probe_ent = ata_pci_init_native_mode(pdev, port);
4677 if (!probe_ent) {
4678 rc = -ENOMEM;
4679 goto err_out_regions;
4680 }
4681
4682 pci_set_master(pdev);
4683
4684 /* FIXME: check ata_device_add return */
4685 if (legacy_mode) {
4686 if (legacy_mode & (1 << 0))
4687 ata_device_add(probe_ent);
4688 if (legacy_mode & (1 << 1))
4689 ata_device_add(probe_ent2);
4690 } else
4691 ata_device_add(probe_ent);
4692
4693 kfree(probe_ent);
4694 kfree(probe_ent2);
4695
4696 return 0;
4697
4698err_out_regions:
4699 if (legacy_mode & (1 << 0))
4700 release_region(0x1f0, 8);
4701 if (legacy_mode & (1 << 1))
4702 release_region(0x170, 8);
4703 pci_release_regions(pdev);
4704err_out:
4705 if (disable_dev_on_err)
4706 pci_disable_device(pdev);
4707 return rc;
4708}
4709
4710/**
4711 * ata_pci_remove_one - PCI layer callback for device removal
4712 * @pdev: PCI device that was removed
4713 *
4714 * PCI layer indicates to libata via this hook that
4715 * hot-unplug or module unload event has occured.
4716 * Handle this by unregistering all objects associated
4717 * with this PCI device. Free those objects. Then finally
4718 * release PCI resources and disable device.
4719 *
4720 * LOCKING:
4721 * Inherited from PCI layer (may sleep).
4722 */
4723
4724void ata_pci_remove_one (struct pci_dev *pdev)
4725{
4726 struct device *dev = pci_dev_to_dev(pdev);
4727 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Alan Cox17b14452005-09-15 15:44:00 +01004729 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 pci_release_regions(pdev);
4731 pci_disable_device(pdev);
4732 dev_set_drvdata(dev, NULL);
4733}
4734
4735/* move to PCI subsystem */
4736int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4737{
4738 unsigned long tmp = 0;
4739
4740 switch (bits->width) {
4741 case 1: {
4742 u8 tmp8 = 0;
4743 pci_read_config_byte(pdev, bits->reg, &tmp8);
4744 tmp = tmp8;
4745 break;
4746 }
4747 case 2: {
4748 u16 tmp16 = 0;
4749 pci_read_config_word(pdev, bits->reg, &tmp16);
4750 tmp = tmp16;
4751 break;
4752 }
4753 case 4: {
4754 u32 tmp32 = 0;
4755 pci_read_config_dword(pdev, bits->reg, &tmp32);
4756 tmp = tmp32;
4757 break;
4758 }
4759
4760 default:
4761 return -EINVAL;
4762 }
4763
4764 tmp &= bits->mask;
4765
4766 return (tmp == bits->val) ? 1 : 0;
4767}
4768#endif /* CONFIG_PCI */
4769
4770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771static int __init ata_init(void)
4772{
4773 ata_wq = create_workqueue("ata");
4774 if (!ata_wq)
4775 return -ENOMEM;
4776
4777 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4778 return 0;
4779}
4780
4781static void __exit ata_exit(void)
4782{
4783 destroy_workqueue(ata_wq);
4784}
4785
4786module_init(ata_init);
4787module_exit(ata_exit);
4788
4789/*
4790 * libata is essentially a library of internal helper functions for
4791 * low-level ATA host controller drivers. As such, the API/ABI is
4792 * likely to change as new drivers are added and updated.
4793 * Do not depend on ABI/API stability.
4794 */
4795
4796EXPORT_SYMBOL_GPL(ata_std_bios_param);
4797EXPORT_SYMBOL_GPL(ata_std_ports);
4798EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004799EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800EXPORT_SYMBOL_GPL(ata_sg_init);
4801EXPORT_SYMBOL_GPL(ata_sg_init_one);
4802EXPORT_SYMBOL_GPL(ata_qc_complete);
4803EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4804EXPORT_SYMBOL_GPL(ata_eng_timeout);
4805EXPORT_SYMBOL_GPL(ata_tf_load);
4806EXPORT_SYMBOL_GPL(ata_tf_read);
4807EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4808EXPORT_SYMBOL_GPL(ata_std_dev_select);
4809EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4810EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4811EXPORT_SYMBOL_GPL(ata_check_status);
4812EXPORT_SYMBOL_GPL(ata_altstatus);
4813EXPORT_SYMBOL_GPL(ata_chk_err);
4814EXPORT_SYMBOL_GPL(ata_exec_command);
4815EXPORT_SYMBOL_GPL(ata_port_start);
4816EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004817EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818EXPORT_SYMBOL_GPL(ata_interrupt);
4819EXPORT_SYMBOL_GPL(ata_qc_prep);
4820EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4821EXPORT_SYMBOL_GPL(ata_bmdma_start);
4822EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4823EXPORT_SYMBOL_GPL(ata_bmdma_status);
4824EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4825EXPORT_SYMBOL_GPL(ata_port_probe);
4826EXPORT_SYMBOL_GPL(sata_phy_reset);
4827EXPORT_SYMBOL_GPL(__sata_phy_reset);
4828EXPORT_SYMBOL_GPL(ata_bus_reset);
4829EXPORT_SYMBOL_GPL(ata_port_disable);
4830EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4831EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4832EXPORT_SYMBOL_GPL(ata_scsi_error);
4833EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4834EXPORT_SYMBOL_GPL(ata_scsi_release);
4835EXPORT_SYMBOL_GPL(ata_host_intr);
4836EXPORT_SYMBOL_GPL(ata_dev_classify);
4837EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004838EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4840
4841#ifdef CONFIG_PCI
4842EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004843EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4845EXPORT_SYMBOL_GPL(ata_pci_init_one);
4846EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4847#endif /* CONFIG_PCI */