blob: cc2d1308826ea5c5a51ce2ae53da87466f766a0b [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 Russcdcca89e2005-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 Russcdcca89e2005-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 Lee312f7da2005-09-27 17:38:03 +08003359 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 ata_dev_select(ap, qc->dev->devno, 1, 0);
3361
Albert Lee312f7da2005-09-27 17:38:03 +08003362 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 switch (qc->tf.protocol) {
3364 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003365 if (qc->tf.flags & ATA_TFLAG_POLLING)
3366 ata_qc_set_polling(qc);
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003369 ap->hsm_task_state = HSM_ST_LAST;
3370
3371 if (qc->tf.flags & ATA_TFLAG_POLLING)
3372 queue_work(ata_wq, &ap->pio_task);
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 break;
3375
3376 case ATA_PROT_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003377 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3380 ap->ops->bmdma_setup(qc); /* set up bmdma */
3381 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003382 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 break;
3384
Albert Lee312f7da2005-09-27 17:38:03 +08003385 case ATA_PROT_PIO:
3386 if (qc->tf.flags & ATA_TFLAG_POLLING)
3387 ata_qc_set_polling(qc);
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003390
3391 if (qc->tf.flags & ATA_TFLAG_POLLING) {
3392 /* polling PIO */
3393 ap->hsm_task_state = HSM_ST;
3394 queue_work(ata_wq, &ap->pio_task);
3395 } else {
3396 /* interrupt driven PIO */
3397 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3398 /* PIO data out protocol */
3399 ap->hsm_task_state = HSM_ST_FIRST;
3400 queue_work(ata_wq, &ap->packet_task);
3401
3402 /* send first data block by polling */
3403 } else {
3404 /* PIO data in protocol */
3405 ap->hsm_task_state = HSM_ST;
3406
3407 /* interrupt handler takes over from here */
3408 }
3409 }
3410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 break;
3412
3413 case ATA_PROT_ATAPI:
Albert Lee312f7da2005-09-27 17:38:03 +08003414 if (qc->tf.flags & ATA_TFLAG_POLLING)
3415 ata_qc_set_polling(qc);
3416
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003418 ap->hsm_task_state = HSM_ST_FIRST;
3419
3420 /* send cdb by polling if no cdb interrupt */
3421 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3422 (qc->tf.flags & ATA_TFLAG_POLLING))
3423 queue_work(ata_wq, &ap->packet_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 break;
3425
3426 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003427 if (qc->tf.flags & ATA_TFLAG_POLLING)
3428 ata_qc_set_polling(qc);
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 ata_tf_to_host_nolock(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003431 ap->hsm_task_state = HSM_ST_FIRST;
3432
3433 /* send cdb by polling if no cdb interrupt */
3434 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
3435 (qc->tf.flags & ATA_TFLAG_POLLING))
3436 queue_work(ata_wq, &ap->packet_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 break;
3438
3439 case ATA_PROT_ATAPI_DMA:
Albert Lee312f7da2005-09-27 17:38:03 +08003440 assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3443 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08003444 ap->hsm_task_state = HSM_ST_FIRST;
3445
3446 /* send cdb by polling if no cdb interrupt */
3447 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3448 queue_work(ata_wq, &ap->packet_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 break;
3450
3451 default:
3452 WARN_ON(1);
3453 return -1;
3454 }
3455
3456 return 0;
3457}
3458
3459/**
Edward Falk0baab862005-06-02 18:17:13 -04003460 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 * @qc: Info associated with this ATA transaction.
3462 *
3463 * LOCKING:
3464 * spin_lock_irqsave(host_set lock)
3465 */
3466
3467static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3468{
3469 struct ata_port *ap = qc->ap;
3470 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3471 u8 dmactl;
3472 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3473
3474 /* load PRD table addr. */
3475 mb(); /* make sure PRD table writes are visible to controller */
3476 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3477
3478 /* specify data direction, triple-check start bit is clear */
3479 dmactl = readb(mmio + ATA_DMA_CMD);
3480 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3481 if (!rw)
3482 dmactl |= ATA_DMA_WR;
3483 writeb(dmactl, mmio + ATA_DMA_CMD);
3484
3485 /* issue r/w command */
3486 ap->ops->exec_command(ap, &qc->tf);
3487}
3488
3489/**
Alan Coxb73fc892005-08-26 16:03:19 +01003490 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 * @qc: Info associated with this ATA transaction.
3492 *
3493 * LOCKING:
3494 * spin_lock_irqsave(host_set lock)
3495 */
3496
3497static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3498{
3499 struct ata_port *ap = qc->ap;
3500 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3501 u8 dmactl;
3502
3503 /* start host DMA transaction */
3504 dmactl = readb(mmio + ATA_DMA_CMD);
3505 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3506
3507 /* Strictly, one may wish to issue a readb() here, to
3508 * flush the mmio write. However, control also passes
3509 * to the hardware at this point, and it will interrupt
3510 * us when we are to resume control. So, in effect,
3511 * we don't care when the mmio write flushes.
3512 * Further, a read of the DMA status register _immediately_
3513 * following the write may not be what certain flaky hardware
3514 * is expected, so I think it is best to not add a readb()
3515 * without first all the MMIO ATA cards/mobos.
3516 * Or maybe I'm just being paranoid.
3517 */
3518}
3519
3520/**
3521 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3522 * @qc: Info associated with this ATA transaction.
3523 *
3524 * LOCKING:
3525 * spin_lock_irqsave(host_set lock)
3526 */
3527
3528static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3529{
3530 struct ata_port *ap = qc->ap;
3531 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3532 u8 dmactl;
3533
3534 /* load PRD table addr. */
3535 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3536
3537 /* specify data direction, triple-check start bit is clear */
3538 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3539 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3540 if (!rw)
3541 dmactl |= ATA_DMA_WR;
3542 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3543
3544 /* issue r/w command */
3545 ap->ops->exec_command(ap, &qc->tf);
3546}
3547
3548/**
3549 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3550 * @qc: Info associated with this ATA transaction.
3551 *
3552 * LOCKING:
3553 * spin_lock_irqsave(host_set lock)
3554 */
3555
3556static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3557{
3558 struct ata_port *ap = qc->ap;
3559 u8 dmactl;
3560
3561 /* start host DMA transaction */
3562 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3563 outb(dmactl | ATA_DMA_START,
3564 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3565}
3566
Edward Falk0baab862005-06-02 18:17:13 -04003567
3568/**
3569 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3570 * @qc: Info associated with this ATA transaction.
3571 *
3572 * Writes the ATA_DMA_START flag to the DMA command register.
3573 *
3574 * May be used as the bmdma_start() entry in ata_port_operations.
3575 *
3576 * LOCKING:
3577 * spin_lock_irqsave(host_set lock)
3578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579void ata_bmdma_start(struct ata_queued_cmd *qc)
3580{
3581 if (qc->ap->flags & ATA_FLAG_MMIO)
3582 ata_bmdma_start_mmio(qc);
3583 else
3584 ata_bmdma_start_pio(qc);
3585}
3586
Edward Falk0baab862005-06-02 18:17:13 -04003587
3588/**
3589 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3590 * @qc: Info associated with this ATA transaction.
3591 *
3592 * Writes address of PRD table to device's PRD Table Address
3593 * register, sets the DMA control register, and calls
3594 * ops->exec_command() to start the transfer.
3595 *
3596 * May be used as the bmdma_setup() entry in ata_port_operations.
3597 *
3598 * LOCKING:
3599 * spin_lock_irqsave(host_set lock)
3600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601void ata_bmdma_setup(struct ata_queued_cmd *qc)
3602{
3603 if (qc->ap->flags & ATA_FLAG_MMIO)
3604 ata_bmdma_setup_mmio(qc);
3605 else
3606 ata_bmdma_setup_pio(qc);
3607}
3608
Edward Falk0baab862005-06-02 18:17:13 -04003609
3610/**
3611 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003612 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003613 *
3614 * Clear interrupt and error flags in DMA status register.
3615 *
3616 * May be used as the irq_clear() entry in ata_port_operations.
3617 *
3618 * LOCKING:
3619 * spin_lock_irqsave(host_set lock)
3620 */
3621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622void ata_bmdma_irq_clear(struct ata_port *ap)
3623{
3624 if (ap->flags & ATA_FLAG_MMIO) {
3625 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3626 writeb(readb(mmio), mmio);
3627 } else {
3628 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3629 outb(inb(addr), addr);
3630 }
3631
3632}
3633
Edward Falk0baab862005-06-02 18:17:13 -04003634
3635/**
3636 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003637 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003638 *
3639 * Read and return BMDMA status register.
3640 *
3641 * May be used as the bmdma_status() entry in ata_port_operations.
3642 *
3643 * LOCKING:
3644 * spin_lock_irqsave(host_set lock)
3645 */
3646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647u8 ata_bmdma_status(struct ata_port *ap)
3648{
3649 u8 host_stat;
3650 if (ap->flags & ATA_FLAG_MMIO) {
3651 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3652 host_stat = readb(mmio + ATA_DMA_STATUS);
3653 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003654 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return host_stat;
3656}
3657
Edward Falk0baab862005-06-02 18:17:13 -04003658
3659/**
3660 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003661 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003662 *
3663 * Clears the ATA_DMA_START flag in the dma control register
3664 *
3665 * May be used as the bmdma_stop() entry in ata_port_operations.
3666 *
3667 * LOCKING:
3668 * spin_lock_irqsave(host_set lock)
3669 */
3670
Alan Coxb73fc892005-08-26 16:03:19 +01003671void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Alan Coxb73fc892005-08-26 16:03:19 +01003673 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 if (ap->flags & ATA_FLAG_MMIO) {
3675 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3676
3677 /* clear start/stop bit */
3678 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3679 mmio + ATA_DMA_CMD);
3680 } else {
3681 /* clear start/stop bit */
3682 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3683 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3684 }
3685
3686 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3687 ata_altstatus(ap); /* dummy read */
3688}
3689
3690/**
Albert Lee312f7da2005-09-27 17:38:03 +08003691 * atapi_send_cdb - Write CDB bytes to hardware
3692 * @ap: Port to which ATAPI device is attached.
3693 * @qc: Taskfile currently active
3694 *
3695 * When device has indicated its readiness to accept
3696 * a CDB, this function is called. Send the CDB.
3697 *
3698 * LOCKING:
3699 * caller.
3700 */
3701
3702static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3703{
3704 /* send SCSI cdb */
3705 DPRINTK("send cdb\n");
3706 assert(ap->cdb_len >= 12);
3707
3708 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3709 ata_altstatus(ap); /* flush */
3710
3711 switch (qc->tf.protocol) {
3712 case ATA_PROT_ATAPI:
3713 ap->hsm_task_state = HSM_ST;
3714 break;
3715 case ATA_PROT_ATAPI_NODATA:
3716 ap->hsm_task_state = HSM_ST_LAST;
3717 break;
3718 case ATA_PROT_ATAPI_DMA:
3719 ap->hsm_task_state = HSM_ST_LAST;
3720 /* initiate bmdma */
3721 ap->ops->bmdma_start(qc);
3722 break;
3723 }
3724}
3725
3726/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 * ata_host_intr - Handle host interrupt for given (port, task)
3728 * @ap: Port on which interrupt arrived (possibly...)
3729 * @qc: Taskfile currently active in engine
3730 *
3731 * Handle host interrupt for given queued command. Currently,
3732 * only DMA interrupts are handled. All other commands are
3733 * handled via polling with interrupts disabled (nIEN bit).
3734 *
3735 * LOCKING:
3736 * spin_lock_irqsave(host_set lock)
3737 *
3738 * RETURNS:
3739 * One if interrupt was handled, zero if not (shared irq).
3740 */
3741
3742inline unsigned int ata_host_intr (struct ata_port *ap,
3743 struct ata_queued_cmd *qc)
3744{
Albert Lee312f7da2005-09-27 17:38:03 +08003745 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Albert Lee312f7da2005-09-27 17:38:03 +08003747 VPRINTK("ata%u: protocol %d task_state %d\n",
3748 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
Albert Lee312f7da2005-09-27 17:38:03 +08003750 /* Check whether we are expecting interrupt in this state */
3751 switch (ap->hsm_task_state) {
3752 case HSM_ST_FIRST:
3753 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
3754 * The flag was turned on only for atapi devices.
3755 * No need to check is_atapi_taskfile(&qc->tf) again.
3756 */
3757 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08003759 break;
3760 case HSM_ST_LAST:
3761 if (qc->tf.protocol == ATA_PROT_DMA ||
3762 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
3763 /* check status of DMA engine */
3764 host_stat = ap->ops->bmdma_status(ap);
3765 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Albert Lee312f7da2005-09-27 17:38:03 +08003767 /* if it's not our irq... */
3768 if (!(host_stat & ATA_DMA_INTR))
3769 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Albert Lee312f7da2005-09-27 17:38:03 +08003771 /* before we do anything else, clear DMA-Start bit */
3772 ap->ops->bmdma_stop(qc);
3773 }
3774 break;
3775 case HSM_ST:
3776 break;
3777 default:
3778 goto idle_irq;
3779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Albert Lee312f7da2005-09-27 17:38:03 +08003781 /* check altstatus */
3782 status = ata_altstatus(ap);
3783 if (status & ATA_BUSY)
3784 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Albert Lee312f7da2005-09-27 17:38:03 +08003786 /* check main status, clearing INTRQ */
3787 status = ata_chk_status(ap);
3788 if (unlikely(status & ATA_BUSY))
3789 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Albert Lee312f7da2005-09-27 17:38:03 +08003791 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
3792 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
3793
3794 /* ack bmdma irq events */
3795 ap->ops->irq_clear(ap);
3796
3797 /* check error */
3798 if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
3799 ap->hsm_task_state = HSM_ST_ERR;
3800
3801fsm_start:
3802 switch (ap->hsm_task_state) {
3803 case HSM_ST_FIRST:
3804 /* Some pre-ATAPI-4 devices assert INTRQ
3805 * at this state when ready to receive CDB.
3806 */
3807
3808 /* check device status */
3809 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
3810 /* Wrong status. Let EH handle this */
3811 ap->hsm_task_state = HSM_ST_ERR;
3812 goto fsm_start;
3813 }
3814
3815 atapi_send_cdb(ap, qc);
3816
3817 break;
3818
3819 case HSM_ST:
3820 /* complete command or read/write the data register */
3821 if (qc->tf.protocol == ATA_PROT_ATAPI) {
3822 /* ATAPI PIO protocol */
3823 if ((status & ATA_DRQ) == 0) {
3824 /* no more data to transfer */
3825 ap->hsm_task_state = HSM_ST_LAST;
3826 goto fsm_start;
3827 }
3828
3829 atapi_pio_bytes(qc);
3830
3831 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
3832 /* bad ireason reported by device */
3833 goto fsm_start;
3834
3835 } else {
3836 /* ATA PIO protocol */
3837 if (unlikely((status & ATA_DRQ) == 0)) {
3838 /* handle BSY=0, DRQ=0 as error */
3839 ap->hsm_task_state = HSM_ST_ERR;
3840 goto fsm_start;
3841 }
3842
3843 ata_pio_sector(qc);
3844
3845 if (ap->hsm_task_state == HSM_ST_LAST &&
3846 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
3847 /* all data read */
3848 ata_altstatus(ap);
3849 status = ata_chk_status(ap);
3850 goto fsm_start;
3851 }
3852 }
3853
3854 ata_altstatus(ap); /* flush */
3855 break;
3856
3857 case HSM_ST_LAST:
3858 if (unlikely(status & ATA_DRQ)) {
3859 /* handle DRQ=1 as error */
3860 ap->hsm_task_state = HSM_ST_ERR;
3861 goto fsm_start;
3862 }
3863
3864 /* no more data to transfer */
3865 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
3866 ap->id, status);
3867
3868 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
3870 /* complete taskfile transaction */
3871 ata_qc_complete(qc, status);
3872 break;
3873
Albert Lee312f7da2005-09-27 17:38:03 +08003874 case HSM_ST_ERR:
3875 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
3876 ap->id, status, host_stat);
3877
3878 ap->hsm_task_state = HSM_ST_IDLE;
3879 ata_qc_complete(qc, status | ATA_ERR);
3880 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 default:
3882 goto idle_irq;
3883 }
3884
3885 return 1; /* irq handled */
3886
3887idle_irq:
3888 ap->stats.idle_irq++;
3889
3890#ifdef ATA_IRQ_TRAP
3891 if ((ap->stats.idle_irq % 1000) == 0) {
3892 handled = 1;
3893 ata_irq_ack(ap, 0); /* debug trap */
3894 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3895 }
3896#endif
3897 return 0; /* irq not handled */
3898}
3899
3900/**
3901 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04003902 * @irq: irq line (unused)
3903 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 * @regs: unused
3905 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04003906 * Default interrupt handler for PCI IDE devices. Calls
3907 * ata_host_intr() for each port that is not disabled.
3908 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003910 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 *
3912 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003913 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 *
3915 */
3916
3917irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3918{
3919 struct ata_host_set *host_set = dev_instance;
3920 unsigned int i;
3921 unsigned int handled = 0;
3922 unsigned long flags;
3923
3924 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3925 spin_lock_irqsave(&host_set->lock, flags);
3926
3927 for (i = 0; i < host_set->n_ports; i++) {
3928 struct ata_port *ap;
3929
3930 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09003931 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08003932 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 struct ata_queued_cmd *qc;
3934
3935 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08003936 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08003937 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 handled |= ata_host_intr(ap, qc);
3939 }
3940 }
3941
3942 spin_unlock_irqrestore(&host_set->lock, flags);
3943
3944 return IRQ_RETVAL(handled);
3945}
3946
3947/**
3948 * atapi_packet_task - Write CDB bytes to hardware
3949 * @_data: Port to which ATAPI device is attached.
3950 *
3951 * When device has indicated its readiness to accept
3952 * a CDB, this function is called. Send the CDB.
3953 * If DMA is to be performed, exit immediately.
3954 * Otherwise, we are in polling mode, so poll
3955 * status under operation succeeds or fails.
3956 *
3957 * LOCKING:
3958 * Kernel thread context (may sleep)
3959 */
3960
3961static void atapi_packet_task(void *_data)
3962{
3963 struct ata_port *ap = _data;
3964 struct ata_queued_cmd *qc;
3965 u8 status;
Albert Lee312f7da2005-09-27 17:38:03 +08003966 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
3968 qc = ata_qc_from_tag(ap, ap->active_tag);
3969 assert(qc != NULL);
3970 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3971
3972 /* sleep-wait for BSY to clear */
3973 DPRINTK("busy wait\n");
3974 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3975 goto err_out;
3976
3977 /* make sure DRQ is set */
3978 status = ata_chk_status(ap);
3979 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3980 goto err_out;
3981
Albert Lee312f7da2005-09-27 17:38:03 +08003982 /* Send the CDB (atapi) or the first data block (ata pio out).
3983 * During the state transition, interrupt handler shouldn't
3984 * be invoked before the data transfer is complete and
3985 * hsm_task_state is changed. Hence, the following locking.
3986 */
3987 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Albert Lee312f7da2005-09-27 17:38:03 +08003989 if (is_atapi_taskfile(&qc->tf)) {
3990 /* send CDB */
3991 atapi_send_cdb(ap, qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Albert Lee312f7da2005-09-27 17:38:03 +08003993 if (qc->tf.flags & ATA_TFLAG_POLLING)
3994 queue_work(ata_wq, &ap->pio_task);
Tejun Heoc1389502005-08-22 14:59:24 +09003995 } else {
Albert Lee312f7da2005-09-27 17:38:03 +08003996 /* PIO data out protocol.
3997 * send first data block.
3998 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
Albert Lee312f7da2005-09-27 17:38:03 +08004000 /* ata_pio_sector() might change the state to HSM_ST_LAST.
4001 * so, the state is changed here before ata_pio_sector().
4002 */
Albert Lee14be71f2005-09-27 17:36:35 +08004003 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08004004 ata_pio_sector(qc);
4005 ata_altstatus(ap); /* flush */
4006
4007 /* interrupt handler takes over from here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
4009
Albert Lee312f7da2005-09-27 17:38:03 +08004010 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 return;
4013
4014err_out:
Albert Lee312f7da2005-09-27 17:38:03 +08004015 ata_pio_error(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016}
4017
Edward Falk0baab862005-06-02 18:17:13 -04004018
4019/**
4020 * ata_port_start - Set port up for dma.
4021 * @ap: Port to initialize
4022 *
4023 * Called just after data structures for each port are
4024 * initialized. Allocates space for PRD table.
4025 *
4026 * May be used as the port_start() entry in ata_port_operations.
4027 *
4028 * LOCKING:
4029 */
4030
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031int ata_port_start (struct ata_port *ap)
4032{
4033 struct device *dev = ap->host_set->dev;
4034
4035 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4036 if (!ap->prd)
4037 return -ENOMEM;
4038
4039 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4040
4041 return 0;
4042}
4043
Edward Falk0baab862005-06-02 18:17:13 -04004044
4045/**
4046 * ata_port_stop - Undo ata_port_start()
4047 * @ap: Port to shut down
4048 *
4049 * Frees the PRD table.
4050 *
4051 * May be used as the port_stop() entry in ata_port_operations.
4052 *
4053 * LOCKING:
4054 */
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056void ata_port_stop (struct ata_port *ap)
4057{
4058 struct device *dev = ap->host_set->dev;
4059
4060 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4061}
4062
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004063void ata_host_stop (struct ata_host_set *host_set)
4064{
4065 if (host_set->mmio_base)
4066 iounmap(host_set->mmio_base);
4067}
4068
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070/**
4071 * ata_host_remove - Unregister SCSI host structure with upper layers
4072 * @ap: Port to unregister
4073 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4074 *
4075 * LOCKING:
4076 */
4077
4078static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4079{
4080 struct Scsi_Host *sh = ap->host;
4081
4082 DPRINTK("ENTER\n");
4083
4084 if (do_unregister)
4085 scsi_remove_host(sh);
4086
4087 ap->ops->port_stop(ap);
4088}
4089
4090/**
4091 * ata_host_init - Initialize an ata_port structure
4092 * @ap: Structure to initialize
4093 * @host: associated SCSI mid-layer structure
4094 * @host_set: Collection of hosts to which @ap belongs
4095 * @ent: Probe information provided by low-level driver
4096 * @port_no: Port number associated with this ata_port
4097 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004098 * Initialize a new ata_port structure, and its associated
4099 * scsi_host.
4100 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004102 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 *
4104 */
4105
4106static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4107 struct ata_host_set *host_set,
4108 struct ata_probe_ent *ent, unsigned int port_no)
4109{
4110 unsigned int i;
4111
4112 host->max_id = 16;
4113 host->max_lun = 1;
4114 host->max_channel = 1;
4115 host->unique_id = ata_unique_id++;
4116 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 scsi_assign_lock(host, &host_set->lock);
4119
4120 ap->flags = ATA_FLAG_PORT_DISABLED;
4121 ap->id = host->unique_id;
4122 ap->host = host;
4123 ap->ctl = ATA_DEVCTL_OBS;
4124 ap->host_set = host_set;
4125 ap->port_no = port_no;
4126 ap->hard_port_no =
4127 ent->legacy_mode ? ent->hard_port_no : port_no;
4128 ap->pio_mask = ent->pio_mask;
4129 ap->mwdma_mask = ent->mwdma_mask;
4130 ap->udma_mask = ent->udma_mask;
4131 ap->flags |= ent->host_flags;
4132 ap->ops = ent->port_ops;
4133 ap->cbl = ATA_CBL_NONE;
4134 ap->active_tag = ATA_TAG_POISON;
4135 ap->last_ctl = 0xFF;
4136
4137 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4138 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4139
4140 for (i = 0; i < ATA_MAX_DEVICES; i++)
4141 ap->device[i].devno = i;
4142
4143#ifdef ATA_IRQ_TRAP
4144 ap->stats.unhandled_irq = 1;
4145 ap->stats.idle_irq = 1;
4146#endif
4147
4148 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4149}
4150
4151/**
4152 * ata_host_add - Attach low-level ATA driver to system
4153 * @ent: Information provided by low-level driver
4154 * @host_set: Collections of ports to which we add
4155 * @port_no: Port number associated with this host
4156 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004157 * Attach low-level ATA driver to system.
4158 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004160 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 *
4162 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004163 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 *
4165 */
4166
4167static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
4168 struct ata_host_set *host_set,
4169 unsigned int port_no)
4170{
4171 struct Scsi_Host *host;
4172 struct ata_port *ap;
4173 int rc;
4174
4175 DPRINTK("ENTER\n");
4176 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4177 if (!host)
4178 return NULL;
4179
4180 ap = (struct ata_port *) &host->hostdata[0];
4181
4182 ata_host_init(ap, host, host_set, ent, port_no);
4183
4184 rc = ap->ops->port_start(ap);
4185 if (rc)
4186 goto err_out;
4187
4188 return ap;
4189
4190err_out:
4191 scsi_host_put(host);
4192 return NULL;
4193}
4194
4195/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004196 * ata_device_add - Register hardware device with ATA and SCSI layers
4197 * @ent: Probe information describing hardware device to be registered
4198 *
4199 * This function processes the information provided in the probe
4200 * information struct @ent, allocates the necessary ATA and SCSI
4201 * host information structures, initializes them, and registers
4202 * everything with requisite kernel subsystems.
4203 *
4204 * This function requests irqs, probes the ATA bus, and probes
4205 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 *
4207 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004208 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 *
4210 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004211 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 *
4213 */
4214
4215int ata_device_add(struct ata_probe_ent *ent)
4216{
4217 unsigned int count = 0, i;
4218 struct device *dev = ent->dev;
4219 struct ata_host_set *host_set;
4220
4221 DPRINTK("ENTER\n");
4222 /* alloc a container for our list of ATA ports (buses) */
4223 host_set = kmalloc(sizeof(struct ata_host_set) +
4224 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4225 if (!host_set)
4226 return 0;
4227 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4228 spin_lock_init(&host_set->lock);
4229
4230 host_set->dev = dev;
4231 host_set->n_ports = ent->n_ports;
4232 host_set->irq = ent->irq;
4233 host_set->mmio_base = ent->mmio_base;
4234 host_set->private_data = ent->private_data;
4235 host_set->ops = ent->port_ops;
4236
4237 /* register each port bound to this device */
4238 for (i = 0; i < ent->n_ports; i++) {
4239 struct ata_port *ap;
4240 unsigned long xfer_mode_mask;
4241
4242 ap = ata_host_add(ent, host_set, i);
4243 if (!ap)
4244 goto err_out;
4245
4246 host_set->ports[i] = ap;
4247 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4248 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4249 (ap->pio_mask << ATA_SHIFT_PIO);
4250
4251 /* print per-port info to dmesg */
4252 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4253 "bmdma 0x%lX irq %lu\n",
4254 ap->id,
4255 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4256 ata_mode_string(xfer_mode_mask),
4257 ap->ioaddr.cmd_addr,
4258 ap->ioaddr.ctl_addr,
4259 ap->ioaddr.bmdma_addr,
4260 ent->irq);
4261
4262 ata_chk_status(ap);
4263 host_set->ops->irq_clear(ap);
4264 count++;
4265 }
4266
4267 if (!count) {
4268 kfree(host_set);
4269 return 0;
4270 }
4271
4272 /* obtain irq, that is shared between channels */
4273 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4274 DRV_NAME, host_set))
4275 goto err_out;
4276
4277 /* perform each probe synchronously */
4278 DPRINTK("probe begin\n");
4279 for (i = 0; i < count; i++) {
4280 struct ata_port *ap;
4281 int rc;
4282
4283 ap = host_set->ports[i];
4284
4285 DPRINTK("ata%u: probe begin\n", ap->id);
4286 rc = ata_bus_probe(ap);
4287 DPRINTK("ata%u: probe end\n", ap->id);
4288
4289 if (rc) {
4290 /* FIXME: do something useful here?
4291 * Current libata behavior will
4292 * tear down everything when
4293 * the module is removed
4294 * or the h/w is unplugged.
4295 */
4296 }
4297
4298 rc = scsi_add_host(ap->host, dev);
4299 if (rc) {
4300 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4301 ap->id);
4302 /* FIXME: do something useful here */
4303 /* FIXME: handle unconditional calls to
4304 * scsi_scan_host and ata_host_remove, below,
4305 * at the very least
4306 */
4307 }
4308 }
4309
4310 /* probes are done, now scan each port's disk(s) */
4311 DPRINTK("probe begin\n");
4312 for (i = 0; i < count; i++) {
4313 struct ata_port *ap = host_set->ports[i];
4314
4315 scsi_scan_host(ap->host);
4316 }
4317
4318 dev_set_drvdata(dev, host_set);
4319
4320 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4321 return ent->n_ports; /* success */
4322
4323err_out:
4324 for (i = 0; i < count; i++) {
4325 ata_host_remove(host_set->ports[i], 1);
4326 scsi_host_put(host_set->ports[i]->host);
4327 }
4328 kfree(host_set);
4329 VPRINTK("EXIT, returning 0\n");
4330 return 0;
4331}
4332
4333/**
Alan Cox17b14452005-09-15 15:44:00 +01004334 * ata_host_set_remove - PCI layer callback for device removal
4335 * @host_set: ATA host set that was removed
4336 *
4337 * Unregister all objects associated with this host set. Free those
4338 * objects.
4339 *
4340 * LOCKING:
4341 * Inherited from calling layer (may sleep).
4342 */
4343
4344
4345void ata_host_set_remove(struct ata_host_set *host_set)
4346{
4347 struct ata_port *ap;
4348 unsigned int i;
4349
4350 for (i = 0; i < host_set->n_ports; i++) {
4351 ap = host_set->ports[i];
4352 scsi_remove_host(ap->host);
4353 }
4354
4355 free_irq(host_set->irq, host_set);
4356
4357 for (i = 0; i < host_set->n_ports; i++) {
4358 ap = host_set->ports[i];
4359
4360 ata_scsi_release(ap->host);
4361
4362 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4363 struct ata_ioports *ioaddr = &ap->ioaddr;
4364
4365 if (ioaddr->cmd_addr == 0x1f0)
4366 release_region(0x1f0, 8);
4367 else if (ioaddr->cmd_addr == 0x170)
4368 release_region(0x170, 8);
4369 }
4370
4371 scsi_host_put(ap->host);
4372 }
4373
4374 if (host_set->ops->host_stop)
4375 host_set->ops->host_stop(host_set);
4376
4377 kfree(host_set);
4378}
4379
4380/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 * ata_scsi_release - SCSI layer callback hook for host unload
4382 * @host: libata host to be unloaded
4383 *
4384 * Performs all duties necessary to shut down a libata port...
4385 * Kill port kthread, disable port, and release resources.
4386 *
4387 * LOCKING:
4388 * Inherited from SCSI layer.
4389 *
4390 * RETURNS:
4391 * One.
4392 */
4393
4394int ata_scsi_release(struct Scsi_Host *host)
4395{
4396 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4397
4398 DPRINTK("ENTER\n");
4399
4400 ap->ops->port_disable(ap);
4401 ata_host_remove(ap, 0);
4402
4403 DPRINTK("EXIT\n");
4404 return 1;
4405}
4406
4407/**
4408 * ata_std_ports - initialize ioaddr with standard port offsets.
4409 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004410 *
4411 * Utility function which initializes data_addr, error_addr,
4412 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4413 * device_addr, status_addr, and command_addr to standard offsets
4414 * relative to cmd_addr.
4415 *
4416 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 */
Edward Falk0baab862005-06-02 18:17:13 -04004418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419void ata_std_ports(struct ata_ioports *ioaddr)
4420{
4421 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4422 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4423 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4424 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4425 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4426 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4427 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4428 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4429 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4430 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4431}
4432
4433static struct ata_probe_ent *
4434ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
4435{
4436 struct ata_probe_ent *probe_ent;
4437
4438 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4439 if (!probe_ent) {
4440 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4441 kobject_name(&(dev->kobj)));
4442 return NULL;
4443 }
4444
4445 memset(probe_ent, 0, sizeof(*probe_ent));
4446
4447 INIT_LIST_HEAD(&probe_ent->node);
4448 probe_ent->dev = dev;
4449
4450 probe_ent->sht = port->sht;
4451 probe_ent->host_flags = port->host_flags;
4452 probe_ent->pio_mask = port->pio_mask;
4453 probe_ent->mwdma_mask = port->mwdma_mask;
4454 probe_ent->udma_mask = port->udma_mask;
4455 probe_ent->port_ops = port->port_ops;
4456
4457 return probe_ent;
4458}
4459
Edward Falk0baab862005-06-02 18:17:13 -04004460
4461
Jeff Garzik374b1872005-08-30 05:42:52 -04004462#ifdef CONFIG_PCI
4463
4464void ata_pci_host_stop (struct ata_host_set *host_set)
4465{
4466 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4467
4468 pci_iounmap(pdev, host_set->mmio_base);
4469}
4470
Edward Falk0baab862005-06-02 18:17:13 -04004471/**
4472 * ata_pci_init_native_mode - Initialize native-mode driver
4473 * @pdev: pci device to be initialized
4474 * @port: array[2] of pointers to port info structures.
4475 *
4476 * Utility function which allocates and initializes an
4477 * ata_probe_ent structure for a standard dual-port
4478 * PIO-based IDE controller. The returned ata_probe_ent
4479 * structure can be passed to ata_device_add(). The returned
4480 * ata_probe_ent structure should then be freed with kfree().
4481 */
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483struct ata_probe_ent *
4484ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port)
4485{
4486 struct ata_probe_ent *probe_ent =
4487 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4488 if (!probe_ent)
4489 return NULL;
4490
4491 probe_ent->n_ports = 2;
4492 probe_ent->irq = pdev->irq;
4493 probe_ent->irq_flags = SA_SHIRQ;
4494
4495 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
4496 probe_ent->port[0].altstatus_addr =
4497 probe_ent->port[0].ctl_addr =
4498 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4499 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4500
4501 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
4502 probe_ent->port[1].altstatus_addr =
4503 probe_ent->port[1].ctl_addr =
4504 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4505 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4506
4507 ata_std_ports(&probe_ent->port[0]);
4508 ata_std_ports(&probe_ent->port[1]);
4509
4510 return probe_ent;
4511}
4512
4513static struct ata_probe_ent *
4514ata_pci_init_legacy_mode(struct pci_dev *pdev, struct ata_port_info **port,
4515 struct ata_probe_ent **ppe2)
4516{
4517 struct ata_probe_ent *probe_ent, *probe_ent2;
4518
4519 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4520 if (!probe_ent)
4521 return NULL;
4522 probe_ent2 = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[1]);
4523 if (!probe_ent2) {
4524 kfree(probe_ent);
4525 return NULL;
4526 }
4527
4528 probe_ent->n_ports = 1;
4529 probe_ent->irq = 14;
4530
4531 probe_ent->hard_port_no = 0;
4532 probe_ent->legacy_mode = 1;
4533
4534 probe_ent2->n_ports = 1;
4535 probe_ent2->irq = 15;
4536
4537 probe_ent2->hard_port_no = 1;
4538 probe_ent2->legacy_mode = 1;
4539
4540 probe_ent->port[0].cmd_addr = 0x1f0;
4541 probe_ent->port[0].altstatus_addr =
4542 probe_ent->port[0].ctl_addr = 0x3f6;
4543 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
4544
4545 probe_ent2->port[0].cmd_addr = 0x170;
4546 probe_ent2->port[0].altstatus_addr =
4547 probe_ent2->port[0].ctl_addr = 0x376;
4548 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
4549
4550 ata_std_ports(&probe_ent->port[0]);
4551 ata_std_ports(&probe_ent2->port[0]);
4552
4553 *ppe2 = probe_ent2;
4554 return probe_ent;
4555}
4556
4557/**
4558 * ata_pci_init_one - Initialize/register PCI IDE host controller
4559 * @pdev: Controller to be initialized
4560 * @port_info: Information from low-level host driver
4561 * @n_ports: Number of ports attached to host controller
4562 *
Edward Falk0baab862005-06-02 18:17:13 -04004563 * This is a helper function which can be called from a driver's
4564 * xxx_init_one() probe function if the hardware uses traditional
4565 * IDE taskfile registers.
4566 *
4567 * This function calls pci_enable_device(), reserves its register
4568 * regions, sets the dma mask, enables bus master mode, and calls
4569 * ata_device_add()
4570 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 * LOCKING:
4572 * Inherited from PCI layer (may sleep).
4573 *
4574 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004575 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 *
4577 */
4578
4579int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4580 unsigned int n_ports)
4581{
4582 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
4583 struct ata_port_info *port[2];
4584 u8 tmp8, mask;
4585 unsigned int legacy_mode = 0;
4586 int disable_dev_on_err = 1;
4587 int rc;
4588
4589 DPRINTK("ENTER\n");
4590
4591 port[0] = port_info[0];
4592 if (n_ports > 1)
4593 port[1] = port_info[1];
4594 else
4595 port[1] = port[0];
4596
4597 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4598 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4599 /* TODO: support transitioning to native mode? */
4600 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4601 mask = (1 << 2) | (1 << 0);
4602 if ((tmp8 & mask) != mask)
4603 legacy_mode = (1 << 3);
4604 }
4605
4606 /* FIXME... */
4607 if ((!legacy_mode) && (n_ports > 1)) {
4608 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
4609 return -EINVAL;
4610 }
4611
4612 rc = pci_enable_device(pdev);
4613 if (rc)
4614 return rc;
4615
4616 rc = pci_request_regions(pdev, DRV_NAME);
4617 if (rc) {
4618 disable_dev_on_err = 0;
4619 goto err_out;
4620 }
4621
4622 if (legacy_mode) {
4623 if (!request_region(0x1f0, 8, "libata")) {
4624 struct resource *conflict, res;
4625 res.start = 0x1f0;
4626 res.end = 0x1f0 + 8 - 1;
4627 conflict = ____request_resource(&ioport_resource, &res);
4628 if (!strcmp(conflict->name, "libata"))
4629 legacy_mode |= (1 << 0);
4630 else {
4631 disable_dev_on_err = 0;
4632 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4633 }
4634 } else
4635 legacy_mode |= (1 << 0);
4636
4637 if (!request_region(0x170, 8, "libata")) {
4638 struct resource *conflict, res;
4639 res.start = 0x170;
4640 res.end = 0x170 + 8 - 1;
4641 conflict = ____request_resource(&ioport_resource, &res);
4642 if (!strcmp(conflict->name, "libata"))
4643 legacy_mode |= (1 << 1);
4644 else {
4645 disable_dev_on_err = 0;
4646 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4647 }
4648 } else
4649 legacy_mode |= (1 << 1);
4650 }
4651
4652 /* we have legacy mode, but all ports are unavailable */
4653 if (legacy_mode == (1 << 3)) {
4654 rc = -EBUSY;
4655 goto err_out_regions;
4656 }
4657
4658 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4659 if (rc)
4660 goto err_out_regions;
4661 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4662 if (rc)
4663 goto err_out_regions;
4664
4665 if (legacy_mode) {
4666 probe_ent = ata_pci_init_legacy_mode(pdev, port, &probe_ent2);
4667 } else
4668 probe_ent = ata_pci_init_native_mode(pdev, port);
4669 if (!probe_ent) {
4670 rc = -ENOMEM;
4671 goto err_out_regions;
4672 }
4673
4674 pci_set_master(pdev);
4675
4676 /* FIXME: check ata_device_add return */
4677 if (legacy_mode) {
4678 if (legacy_mode & (1 << 0))
4679 ata_device_add(probe_ent);
4680 if (legacy_mode & (1 << 1))
4681 ata_device_add(probe_ent2);
4682 } else
4683 ata_device_add(probe_ent);
4684
4685 kfree(probe_ent);
4686 kfree(probe_ent2);
4687
4688 return 0;
4689
4690err_out_regions:
4691 if (legacy_mode & (1 << 0))
4692 release_region(0x1f0, 8);
4693 if (legacy_mode & (1 << 1))
4694 release_region(0x170, 8);
4695 pci_release_regions(pdev);
4696err_out:
4697 if (disable_dev_on_err)
4698 pci_disable_device(pdev);
4699 return rc;
4700}
4701
4702/**
4703 * ata_pci_remove_one - PCI layer callback for device removal
4704 * @pdev: PCI device that was removed
4705 *
4706 * PCI layer indicates to libata via this hook that
4707 * hot-unplug or module unload event has occured.
4708 * Handle this by unregistering all objects associated
4709 * with this PCI device. Free those objects. Then finally
4710 * release PCI resources and disable device.
4711 *
4712 * LOCKING:
4713 * Inherited from PCI layer (may sleep).
4714 */
4715
4716void ata_pci_remove_one (struct pci_dev *pdev)
4717{
4718 struct device *dev = pci_dev_to_dev(pdev);
4719 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
Alan Cox17b14452005-09-15 15:44:00 +01004721 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 pci_release_regions(pdev);
4723 pci_disable_device(pdev);
4724 dev_set_drvdata(dev, NULL);
4725}
4726
4727/* move to PCI subsystem */
4728int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4729{
4730 unsigned long tmp = 0;
4731
4732 switch (bits->width) {
4733 case 1: {
4734 u8 tmp8 = 0;
4735 pci_read_config_byte(pdev, bits->reg, &tmp8);
4736 tmp = tmp8;
4737 break;
4738 }
4739 case 2: {
4740 u16 tmp16 = 0;
4741 pci_read_config_word(pdev, bits->reg, &tmp16);
4742 tmp = tmp16;
4743 break;
4744 }
4745 case 4: {
4746 u32 tmp32 = 0;
4747 pci_read_config_dword(pdev, bits->reg, &tmp32);
4748 tmp = tmp32;
4749 break;
4750 }
4751
4752 default:
4753 return -EINVAL;
4754 }
4755
4756 tmp &= bits->mask;
4757
4758 return (tmp == bits->val) ? 1 : 0;
4759}
4760#endif /* CONFIG_PCI */
4761
4762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763static int __init ata_init(void)
4764{
4765 ata_wq = create_workqueue("ata");
4766 if (!ata_wq)
4767 return -ENOMEM;
4768
4769 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4770 return 0;
4771}
4772
4773static void __exit ata_exit(void)
4774{
4775 destroy_workqueue(ata_wq);
4776}
4777
4778module_init(ata_init);
4779module_exit(ata_exit);
4780
4781/*
4782 * libata is essentially a library of internal helper functions for
4783 * low-level ATA host controller drivers. As such, the API/ABI is
4784 * likely to change as new drivers are added and updated.
4785 * Do not depend on ABI/API stability.
4786 */
4787
4788EXPORT_SYMBOL_GPL(ata_std_bios_param);
4789EXPORT_SYMBOL_GPL(ata_std_ports);
4790EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004791EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792EXPORT_SYMBOL_GPL(ata_sg_init);
4793EXPORT_SYMBOL_GPL(ata_sg_init_one);
4794EXPORT_SYMBOL_GPL(ata_qc_complete);
4795EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4796EXPORT_SYMBOL_GPL(ata_eng_timeout);
4797EXPORT_SYMBOL_GPL(ata_tf_load);
4798EXPORT_SYMBOL_GPL(ata_tf_read);
4799EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4800EXPORT_SYMBOL_GPL(ata_std_dev_select);
4801EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4802EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4803EXPORT_SYMBOL_GPL(ata_check_status);
4804EXPORT_SYMBOL_GPL(ata_altstatus);
4805EXPORT_SYMBOL_GPL(ata_chk_err);
4806EXPORT_SYMBOL_GPL(ata_exec_command);
4807EXPORT_SYMBOL_GPL(ata_port_start);
4808EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004809EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810EXPORT_SYMBOL_GPL(ata_interrupt);
4811EXPORT_SYMBOL_GPL(ata_qc_prep);
4812EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4813EXPORT_SYMBOL_GPL(ata_bmdma_start);
4814EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4815EXPORT_SYMBOL_GPL(ata_bmdma_status);
4816EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4817EXPORT_SYMBOL_GPL(ata_port_probe);
4818EXPORT_SYMBOL_GPL(sata_phy_reset);
4819EXPORT_SYMBOL_GPL(__sata_phy_reset);
4820EXPORT_SYMBOL_GPL(ata_bus_reset);
4821EXPORT_SYMBOL_GPL(ata_port_disable);
4822EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4823EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4824EXPORT_SYMBOL_GPL(ata_scsi_error);
4825EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4826EXPORT_SYMBOL_GPL(ata_scsi_release);
4827EXPORT_SYMBOL_GPL(ata_host_intr);
4828EXPORT_SYMBOL_GPL(ata_dev_classify);
4829EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004830EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4832
4833#ifdef CONFIG_PCI
4834EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004835EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4837EXPORT_SYMBOL_GPL(ata_pci_init_one);
4838EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4839#endif /* CONFIG_PCI */