]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/libata-core.c
libata: more docs updates
[net-next-2.6.git] / drivers / scsi / libata-core.c
CommitLineData
1da177e4
LT
1/*
2 libata-core.c - helper library for ATA
3
4 Copyright 2003-2004 Red Hat, Inc. All rights reserved.
5 Copyright 2003-2004 Jeff Garzik
6
7 The contents of this file are subject to the Open
8 Software License version 1.1 that can be found at
9 http://www.opensource.org/licenses/osl-1.1.txt and is included herein
10 by reference.
11
12 Alternatively, the contents of this file may be used under the terms
13 of the GNU General Public License version 2 (the "GPL") as distributed
14 in the kernel source COPYING file, in which case the provisions of
15 the GPL are applicable instead of the above. If you wish to allow
16 the use of your version of this file only under the terms of the
17 GPL and not to allow others to use your version of this file under
18 the OSL, indicate your decision by deleting the provisions above and
19 replace them with the notice and other provisions required by the GPL.
20 If you do not delete the provisions above, a recipient may use your
21 version of this file under either the OSL or the GPL.
22
23 */
24
25#include <linux/config.h>
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/pci.h>
29#include <linux/init.h>
30#include <linux/list.h>
31#include <linux/mm.h>
32#include <linux/highmem.h>
33#include <linux/spinlock.h>
34#include <linux/blkdev.h>
35#include <linux/delay.h>
36#include <linux/timer.h>
37#include <linux/interrupt.h>
38#include <linux/completion.h>
39#include <linux/suspend.h>
40#include <linux/workqueue.h>
41#include <scsi/scsi.h>
42#include "scsi.h"
43#include "scsi_priv.h"
44#include <scsi/scsi_host.h>
45#include <linux/libata.h>
46#include <asm/io.h>
47#include <asm/semaphore.h>
48#include <asm/byteorder.h>
49
50#include "libata.h"
51
52static unsigned int ata_busy_sleep (struct ata_port *ap,
53 unsigned long tmout_pat,
54 unsigned long tmout);
55static void ata_set_mode(struct ata_port *ap);
56static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
57static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift);
58static int fgb(u32 bitmap);
59static int ata_choose_xfer_mode(struct ata_port *ap,
60 u8 *xfer_mode_out,
61 unsigned int *xfer_shift_out);
62static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat);
63static void __ata_qc_complete(struct ata_queued_cmd *qc);
64
65static unsigned int ata_unique_id = 1;
66static struct workqueue_struct *ata_wq;
67
68MODULE_AUTHOR("Jeff Garzik");
69MODULE_DESCRIPTION("Library module for ATA devices");
70MODULE_LICENSE("GPL");
71MODULE_VERSION(DRV_VERSION);
72
73/**
74 * ata_tf_load - send taskfile registers to host controller
75 * @ap: Port to which output is sent
76 * @tf: ATA taskfile register set
77 *
78 * Outputs ATA taskfile to standard ATA host controller.
79 *
80 * LOCKING:
81 * Inherited from caller.
82 */
83
84static void ata_tf_load_pio(struct ata_port *ap, struct ata_taskfile *tf)
85{
86 struct ata_ioports *ioaddr = &ap->ioaddr;
87 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
88
89 if (tf->ctl != ap->last_ctl) {
90 outb(tf->ctl, ioaddr->ctl_addr);
91 ap->last_ctl = tf->ctl;
92 ata_wait_idle(ap);
93 }
94
95 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
96 outb(tf->hob_feature, ioaddr->feature_addr);
97 outb(tf->hob_nsect, ioaddr->nsect_addr);
98 outb(tf->hob_lbal, ioaddr->lbal_addr);
99 outb(tf->hob_lbam, ioaddr->lbam_addr);
100 outb(tf->hob_lbah, ioaddr->lbah_addr);
101 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
102 tf->hob_feature,
103 tf->hob_nsect,
104 tf->hob_lbal,
105 tf->hob_lbam,
106 tf->hob_lbah);
107 }
108
109 if (is_addr) {
110 outb(tf->feature, ioaddr->feature_addr);
111 outb(tf->nsect, ioaddr->nsect_addr);
112 outb(tf->lbal, ioaddr->lbal_addr);
113 outb(tf->lbam, ioaddr->lbam_addr);
114 outb(tf->lbah, ioaddr->lbah_addr);
115 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
116 tf->feature,
117 tf->nsect,
118 tf->lbal,
119 tf->lbam,
120 tf->lbah);
121 }
122
123 if (tf->flags & ATA_TFLAG_DEVICE) {
124 outb(tf->device, ioaddr->device_addr);
125 VPRINTK("device 0x%X\n", tf->device);
126 }
127
128 ata_wait_idle(ap);
129}
130
131/**
132 * ata_tf_load_mmio - send taskfile registers to host controller
133 * @ap: Port to which output is sent
134 * @tf: ATA taskfile register set
135 *
136 * Outputs ATA taskfile to standard ATA host controller using MMIO.
137 *
138 * LOCKING:
139 * Inherited from caller.
140 */
141
142static void ata_tf_load_mmio(struct ata_port *ap, struct ata_taskfile *tf)
143{
144 struct ata_ioports *ioaddr = &ap->ioaddr;
145 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
146
147 if (tf->ctl != ap->last_ctl) {
148 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
149 ap->last_ctl = tf->ctl;
150 ata_wait_idle(ap);
151 }
152
153 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
154 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
155 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
156 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
157 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
158 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
159 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
160 tf->hob_feature,
161 tf->hob_nsect,
162 tf->hob_lbal,
163 tf->hob_lbam,
164 tf->hob_lbah);
165 }
166
167 if (is_addr) {
168 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
169 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
170 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
171 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
172 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
173 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
174 tf->feature,
175 tf->nsect,
176 tf->lbal,
177 tf->lbam,
178 tf->lbah);
179 }
180
181 if (tf->flags & ATA_TFLAG_DEVICE) {
182 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
183 VPRINTK("device 0x%X\n", tf->device);
184 }
185
186 ata_wait_idle(ap);
187}
188
189void ata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
190{
191 if (ap->flags & ATA_FLAG_MMIO)
192 ata_tf_load_mmio(ap, tf);
193 else
194 ata_tf_load_pio(ap, tf);
195}
196
197/**
198 * ata_exec_command - issue ATA command to host controller
199 * @ap: port to which command is being issued
200 * @tf: ATA taskfile register set
201 *
202 * Issues PIO/MMIO write to ATA command register, with proper
203 * synchronization with interrupt handler / other threads.
204 *
205 * LOCKING:
206 * spin_lock_irqsave(host_set lock)
207 */
208
209static void ata_exec_command_pio(struct ata_port *ap, struct ata_taskfile *tf)
210{
211 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
212
213 outb(tf->command, ap->ioaddr.command_addr);
214 ata_pause(ap);
215}
216
217
218/**
219 * ata_exec_command_mmio - issue ATA command to host controller
220 * @ap: port to which command is being issued
221 * @tf: ATA taskfile register set
222 *
223 * Issues MMIO write to ATA command register, with proper
224 * synchronization with interrupt handler / other threads.
225 *
226 * LOCKING:
227 * spin_lock_irqsave(host_set lock)
228 */
229
230static void ata_exec_command_mmio(struct ata_port *ap, struct ata_taskfile *tf)
231{
232 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
233
234 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
235 ata_pause(ap);
236}
237
238void ata_exec_command(struct ata_port *ap, struct ata_taskfile *tf)
239{
240 if (ap->flags & ATA_FLAG_MMIO)
241 ata_exec_command_mmio(ap, tf);
242 else
243 ata_exec_command_pio(ap, tf);
244}
245
246/**
247 * ata_exec - issue ATA command to host controller
248 * @ap: port to which command is being issued
249 * @tf: ATA taskfile register set
250 *
251 * Issues PIO/MMIO write to ATA command register, with proper
252 * synchronization with interrupt handler / other threads.
253 *
254 * LOCKING:
255 * Obtains host_set lock.
256 */
257
258static inline void ata_exec(struct ata_port *ap, struct ata_taskfile *tf)
259{
260 unsigned long flags;
261
262 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
263 spin_lock_irqsave(&ap->host_set->lock, flags);
264 ap->ops->exec_command(ap, tf);
265 spin_unlock_irqrestore(&ap->host_set->lock, flags);
266}
267
268/**
269 * ata_tf_to_host - issue ATA taskfile to host controller
270 * @ap: port to which command is being issued
271 * @tf: ATA taskfile register set
272 *
273 * Issues ATA taskfile register set to ATA host controller,
274 * with proper synchronization with interrupt handler and
275 * other threads.
276 *
277 * LOCKING:
278 * Obtains host_set lock.
279 */
280
281static void ata_tf_to_host(struct ata_port *ap, struct ata_taskfile *tf)
282{
283 ap->ops->tf_load(ap, tf);
284
285 ata_exec(ap, tf);
286}
287
288/**
289 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
290 * @ap: port to which command is being issued
291 * @tf: ATA taskfile register set
292 *
293 * Issues ATA taskfile register set to ATA host controller,
294 * with proper synchronization with interrupt handler and
295 * other threads.
296 *
297 * LOCKING:
298 * spin_lock_irqsave(host_set lock)
299 */
300
301void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf)
302{
303 ap->ops->tf_load(ap, tf);
304 ap->ops->exec_command(ap, tf);
305}
306
307/**
308 * ata_tf_read - input device's ATA taskfile shadow registers
309 * @ap: Port from which input is read
310 * @tf: ATA taskfile register set for storing input
311 *
312 * Reads ATA taskfile registers for currently-selected device
313 * into @tf.
314 *
315 * LOCKING:
316 * Inherited from caller.
317 */
318
319static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
320{
321 struct ata_ioports *ioaddr = &ap->ioaddr;
322
323 tf->nsect = inb(ioaddr->nsect_addr);
324 tf->lbal = inb(ioaddr->lbal_addr);
325 tf->lbam = inb(ioaddr->lbam_addr);
326 tf->lbah = inb(ioaddr->lbah_addr);
327 tf->device = inb(ioaddr->device_addr);
328
329 if (tf->flags & ATA_TFLAG_LBA48) {
330 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
331 tf->hob_feature = inb(ioaddr->error_addr);
332 tf->hob_nsect = inb(ioaddr->nsect_addr);
333 tf->hob_lbal = inb(ioaddr->lbal_addr);
334 tf->hob_lbam = inb(ioaddr->lbam_addr);
335 tf->hob_lbah = inb(ioaddr->lbah_addr);
336 }
337}
338
339/**
340 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
341 * @ap: Port from which input is read
342 * @tf: ATA taskfile register set for storing input
343 *
344 * Reads ATA taskfile registers for currently-selected device
345 * into @tf via MMIO.
346 *
347 * LOCKING:
348 * Inherited from caller.
349 */
350
351static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
352{
353 struct ata_ioports *ioaddr = &ap->ioaddr;
354
355 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
356 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
357 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
358 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
359 tf->device = readb((void __iomem *)ioaddr->device_addr);
360
361 if (tf->flags & ATA_TFLAG_LBA48) {
362 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
363 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
364 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
365 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
366 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
367 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
368 }
369}
370
371void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
372{
373 if (ap->flags & ATA_FLAG_MMIO)
374 ata_tf_read_mmio(ap, tf);
375 else
376 ata_tf_read_pio(ap, tf);
377}
378
379/**
380 * ata_check_status_pio - Read device status reg & clear interrupt
381 * @ap: port where the device is
382 *
383 * Reads ATA taskfile status register for currently-selected device
384 * and return it's value. This also clears pending interrupts
385 * from this device
386 *
387 * LOCKING:
388 * Inherited from caller.
389 */
390static u8 ata_check_status_pio(struct ata_port *ap)
391{
392 return inb(ap->ioaddr.status_addr);
393}
394
395/**
396 * ata_check_status_mmio - Read device status reg & clear interrupt
397 * @ap: port where the device is
398 *
399 * Reads ATA taskfile status register for currently-selected device
400 * via MMIO and return it's value. This also clears pending interrupts
401 * from this device
402 *
403 * LOCKING:
404 * Inherited from caller.
405 */
406static u8 ata_check_status_mmio(struct ata_port *ap)
407{
408 return readb((void __iomem *) ap->ioaddr.status_addr);
409}
410
411u8 ata_check_status(struct ata_port *ap)
412{
413 if (ap->flags & ATA_FLAG_MMIO)
414 return ata_check_status_mmio(ap);
415 return ata_check_status_pio(ap);
416}
417
418u8 ata_altstatus(struct ata_port *ap)
419{
420 if (ap->ops->check_altstatus)
421 return ap->ops->check_altstatus(ap);
422
423 if (ap->flags & ATA_FLAG_MMIO)
424 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
425 return inb(ap->ioaddr.altstatus_addr);
426}
427
428u8 ata_chk_err(struct ata_port *ap)
429{
430 if (ap->ops->check_err)
431 return ap->ops->check_err(ap);
432
433 if (ap->flags & ATA_FLAG_MMIO) {
434 return readb((void __iomem *) ap->ioaddr.error_addr);
435 }
436 return inb(ap->ioaddr.error_addr);
437}
438
439/**
440 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
441 * @tf: Taskfile to convert
442 * @fis: Buffer into which data will output
443 * @pmp: Port multiplier port
444 *
445 * Converts a standard ATA taskfile to a Serial ATA
446 * FIS structure (Register - Host to Device).
447 *
448 * LOCKING:
449 * Inherited from caller.
450 */
451
452void ata_tf_to_fis(struct ata_taskfile *tf, u8 *fis, u8 pmp)
453{
454 fis[0] = 0x27; /* Register - Host to Device FIS */
455 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
456 bit 7 indicates Command FIS */
457 fis[2] = tf->command;
458 fis[3] = tf->feature;
459
460 fis[4] = tf->lbal;
461 fis[5] = tf->lbam;
462 fis[6] = tf->lbah;
463 fis[7] = tf->device;
464
465 fis[8] = tf->hob_lbal;
466 fis[9] = tf->hob_lbam;
467 fis[10] = tf->hob_lbah;
468 fis[11] = tf->hob_feature;
469
470 fis[12] = tf->nsect;
471 fis[13] = tf->hob_nsect;
472 fis[14] = 0;
473 fis[15] = tf->ctl;
474
475 fis[16] = 0;
476 fis[17] = 0;
477 fis[18] = 0;
478 fis[19] = 0;
479}
480
481/**
482 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
483 * @fis: Buffer from which data will be input
484 * @tf: Taskfile to output
485 *
486 * Converts a standard ATA taskfile to a Serial ATA
487 * FIS structure (Register - Host to Device).
488 *
489 * LOCKING:
490 * Inherited from caller.
491 */
492
493void ata_tf_from_fis(u8 *fis, struct ata_taskfile *tf)
494{
495 tf->command = fis[2]; /* status */
496 tf->feature = fis[3]; /* error */
497
498 tf->lbal = fis[4];
499 tf->lbam = fis[5];
500 tf->lbah = fis[6];
501 tf->device = fis[7];
502
503 tf->hob_lbal = fis[8];
504 tf->hob_lbam = fis[9];
505 tf->hob_lbah = fis[10];
506
507 tf->nsect = fis[12];
508 tf->hob_nsect = fis[13];
509}
510
511/**
512 * ata_prot_to_cmd - determine which read/write opcodes to use
513 * @protocol: ATA_PROT_xxx taskfile protocol
514 * @lba48: true is lba48 is present
515 *
516 * Given necessary input, determine which read/write commands
517 * to use to transfer data.
518 *
519 * LOCKING:
520 * None.
521 */
522static int ata_prot_to_cmd(int protocol, int lba48)
523{
524 int rcmd = 0, wcmd = 0;
525
526 switch (protocol) {
527 case ATA_PROT_PIO:
528 if (lba48) {
529 rcmd = ATA_CMD_PIO_READ_EXT;
530 wcmd = ATA_CMD_PIO_WRITE_EXT;
531 } else {
532 rcmd = ATA_CMD_PIO_READ;
533 wcmd = ATA_CMD_PIO_WRITE;
534 }
535 break;
536
537 case ATA_PROT_DMA:
538 if (lba48) {
539 rcmd = ATA_CMD_READ_EXT;
540 wcmd = ATA_CMD_WRITE_EXT;
541 } else {
542 rcmd = ATA_CMD_READ;
543 wcmd = ATA_CMD_WRITE;
544 }
545 break;
546
547 default:
548 return -1;
549 }
550
551 return rcmd | (wcmd << 8);
552}
553
554/**
555 * ata_dev_set_protocol - set taskfile protocol and r/w commands
556 * @dev: device to examine and configure
557 *
558 * Examine the device configuration, after we have
559 * read the identify-device page and configured the
560 * data transfer mode. Set internal state related to
561 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
562 * and calculate the proper read/write commands to use.
563 *
564 * LOCKING:
565 * caller.
566 */
567static void ata_dev_set_protocol(struct ata_device *dev)
568{
569 int pio = (dev->flags & ATA_DFLAG_PIO);
570 int lba48 = (dev->flags & ATA_DFLAG_LBA48);
571 int proto, cmd;
572
573 if (pio)
574 proto = dev->xfer_protocol = ATA_PROT_PIO;
575 else
576 proto = dev->xfer_protocol = ATA_PROT_DMA;
577
578 cmd = ata_prot_to_cmd(proto, lba48);
579 if (cmd < 0)
580 BUG();
581
582 dev->read_cmd = cmd & 0xff;
583 dev->write_cmd = (cmd >> 8) & 0xff;
584}
585
586static const char * xfer_mode_str[] = {
587 "UDMA/16",
588 "UDMA/25",
589 "UDMA/33",
590 "UDMA/44",
591 "UDMA/66",
592 "UDMA/100",
593 "UDMA/133",
594 "UDMA7",
595 "MWDMA0",
596 "MWDMA1",
597 "MWDMA2",
598 "PIO0",
599 "PIO1",
600 "PIO2",
601 "PIO3",
602 "PIO4",
603};
604
605/**
606 * ata_udma_string - convert UDMA bit offset to string
607 * @mask: mask of bits supported; only highest bit counts.
608 *
609 * Determine string which represents the highest speed
610 * (highest bit in @udma_mask).
611 *
612 * LOCKING:
613 * None.
614 *
615 * RETURNS:
616 * Constant C string representing highest speed listed in
617 * @udma_mask, or the constant C string "<n/a>".
618 */
619
620static const char *ata_mode_string(unsigned int mask)
621{
622 int i;
623
624 for (i = 7; i >= 0; i--)
625 if (mask & (1 << i))
626 goto out;
627 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
628 if (mask & (1 << i))
629 goto out;
630 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
631 if (mask & (1 << i))
632 goto out;
633
634 return "<n/a>";
635
636out:
637 return xfer_mode_str[i];
638}
639
640/**
641 * ata_pio_devchk - PATA device presence detection
642 * @ap: ATA channel to examine
643 * @device: Device to examine (starting at zero)
644 *
645 * This technique was originally described in
646 * Hale Landis's ATADRVR (www.ata-atapi.com), and
647 * later found its way into the ATA/ATAPI spec.
648 *
649 * Write a pattern to the ATA shadow registers,
650 * and if a device is present, it will respond by
651 * correctly storing and echoing back the
652 * ATA shadow register contents.
653 *
654 * LOCKING:
655 * caller.
656 */
657
658static unsigned int ata_pio_devchk(struct ata_port *ap,
659 unsigned int device)
660{
661 struct ata_ioports *ioaddr = &ap->ioaddr;
662 u8 nsect, lbal;
663
664 ap->ops->dev_select(ap, device);
665
666 outb(0x55, ioaddr->nsect_addr);
667 outb(0xaa, ioaddr->lbal_addr);
668
669 outb(0xaa, ioaddr->nsect_addr);
670 outb(0x55, ioaddr->lbal_addr);
671
672 outb(0x55, ioaddr->nsect_addr);
673 outb(0xaa, ioaddr->lbal_addr);
674
675 nsect = inb(ioaddr->nsect_addr);
676 lbal = inb(ioaddr->lbal_addr);
677
678 if ((nsect == 0x55) && (lbal == 0xaa))
679 return 1; /* we found a device */
680
681 return 0; /* nothing found */
682}
683
684/**
685 * ata_mmio_devchk - PATA device presence detection
686 * @ap: ATA channel to examine
687 * @device: Device to examine (starting at zero)
688 *
689 * This technique was originally described in
690 * Hale Landis's ATADRVR (www.ata-atapi.com), and
691 * later found its way into the ATA/ATAPI spec.
692 *
693 * Write a pattern to the ATA shadow registers,
694 * and if a device is present, it will respond by
695 * correctly storing and echoing back the
696 * ATA shadow register contents.
697 *
698 * LOCKING:
699 * caller.
700 */
701
702static unsigned int ata_mmio_devchk(struct ata_port *ap,
703 unsigned int device)
704{
705 struct ata_ioports *ioaddr = &ap->ioaddr;
706 u8 nsect, lbal;
707
708 ap->ops->dev_select(ap, device);
709
710 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
711 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
712
713 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
714 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
715
716 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
717 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
718
719 nsect = readb((void __iomem *) ioaddr->nsect_addr);
720 lbal = readb((void __iomem *) ioaddr->lbal_addr);
721
722 if ((nsect == 0x55) && (lbal == 0xaa))
723 return 1; /* we found a device */
724
725 return 0; /* nothing found */
726}
727
728/**
729 * ata_devchk - PATA device presence detection
730 * @ap: ATA channel to examine
731 * @device: Device to examine (starting at zero)
732 *
733 * Dispatch ATA device presence detection, depending
734 * on whether we are using PIO or MMIO to talk to the
735 * ATA shadow registers.
736 *
737 * LOCKING:
738 * caller.
739 */
740
741static unsigned int ata_devchk(struct ata_port *ap,
742 unsigned int device)
743{
744 if (ap->flags & ATA_FLAG_MMIO)
745 return ata_mmio_devchk(ap, device);
746 return ata_pio_devchk(ap, device);
747}
748
749/**
750 * ata_dev_classify - determine device type based on ATA-spec signature
751 * @tf: ATA taskfile register set for device to be identified
752 *
753 * Determine from taskfile register contents whether a device is
754 * ATA or ATAPI, as per "Signature and persistence" section
755 * of ATA/PI spec (volume 1, sect 5.14).
756 *
757 * LOCKING:
758 * None.
759 *
760 * RETURNS:
761 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
762 * the event of failure.
763 */
764
765unsigned int ata_dev_classify(struct ata_taskfile *tf)
766{
767 /* Apple's open source Darwin code hints that some devices only
768 * put a proper signature into the LBA mid/high registers,
769 * So, we only check those. It's sufficient for uniqueness.
770 */
771
772 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
773 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
774 DPRINTK("found ATA device by sig\n");
775 return ATA_DEV_ATA;
776 }
777
778 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
779 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
780 DPRINTK("found ATAPI device by sig\n");
781 return ATA_DEV_ATAPI;
782 }
783
784 DPRINTK("unknown device\n");
785 return ATA_DEV_UNKNOWN;
786}
787
788/**
789 * ata_dev_try_classify - Parse returned ATA device signature
790 * @ap: ATA channel to examine
791 * @device: Device to examine (starting at zero)
792 *
793 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
794 * an ATA/ATAPI-defined set of values is placed in the ATA
795 * shadow registers, indicating the results of device detection
796 * and diagnostics.
797 *
798 * Select the ATA device, and read the values from the ATA shadow
799 * registers. Then parse according to the Error register value,
800 * and the spec-defined values examined by ata_dev_classify().
801 *
802 * LOCKING:
803 * caller.
804 */
805
806static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
807{
808 struct ata_device *dev = &ap->device[device];
809 struct ata_taskfile tf;
810 unsigned int class;
811 u8 err;
812
813 ap->ops->dev_select(ap, device);
814
815 memset(&tf, 0, sizeof(tf));
816
817 err = ata_chk_err(ap);
818 ap->ops->tf_read(ap, &tf);
819
820 dev->class = ATA_DEV_NONE;
821
822 /* see if device passed diags */
823 if (err == 1)
824 /* do nothing */ ;
825 else if ((device == 0) && (err == 0x81))
826 /* do nothing */ ;
827 else
828 return err;
829
830 /* determine if device if ATA or ATAPI */
831 class = ata_dev_classify(&tf);
832 if (class == ATA_DEV_UNKNOWN)
833 return err;
834 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
835 return err;
836
837 dev->class = class;
838
839 return err;
840}
841
842/**
843 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
844 * @id: IDENTIFY DEVICE results we will examine
845 * @s: string into which data is output
846 * @ofs: offset into identify device page
847 * @len: length of string to return. must be an even number.
848 *
849 * The strings in the IDENTIFY DEVICE page are broken up into
850 * 16-bit chunks. Run through the string, and output each
851 * 8-bit chunk linearly, regardless of platform.
852 *
853 * LOCKING:
854 * caller.
855 */
856
857void ata_dev_id_string(u16 *id, unsigned char *s,
858 unsigned int ofs, unsigned int len)
859{
860 unsigned int c;
861
862 while (len > 0) {
863 c = id[ofs] >> 8;
864 *s = c;
865 s++;
866
867 c = id[ofs] & 0xff;
868 *s = c;
869 s++;
870
871 ofs++;
872 len -= 2;
873 }
874}
875
876void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
877{
878}
879
880/**
881 * ata_std_dev_select - Select device 0/1 on ATA bus
882 * @ap: ATA channel to manipulate
883 * @device: ATA device (numbered from zero) to select
884 *
885 * Use the method defined in the ATA specification to
886 * make either device 0, or device 1, active on the
887 * ATA channel.
888 *
889 * LOCKING:
890 * caller.
891 */
892
893void ata_std_dev_select (struct ata_port *ap, unsigned int device)
894{
895 u8 tmp;
896
897 if (device == 0)
898 tmp = ATA_DEVICE_OBS;
899 else
900 tmp = ATA_DEVICE_OBS | ATA_DEV1;
901
902 if (ap->flags & ATA_FLAG_MMIO) {
903 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
904 } else {
905 outb(tmp, ap->ioaddr.device_addr);
906 }
907 ata_pause(ap); /* needed; also flushes, for mmio */
908}
909
910/**
911 * ata_dev_select - Select device 0/1 on ATA bus
912 * @ap: ATA channel to manipulate
913 * @device: ATA device (numbered from zero) to select
914 * @wait: non-zero to wait for Status register BSY bit to clear
915 * @can_sleep: non-zero if context allows sleeping
916 *
917 * Use the method defined in the ATA specification to
918 * make either device 0, or device 1, active on the
919 * ATA channel.
920 *
921 * This is a high-level version of ata_std_dev_select(),
922 * which additionally provides the services of inserting
923 * the proper pauses and status polling, where needed.
924 *
925 * LOCKING:
926 * caller.
927 */
928
929void ata_dev_select(struct ata_port *ap, unsigned int device,
930 unsigned int wait, unsigned int can_sleep)
931{
932 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
933 ap->id, device, wait);
934
935 if (wait)
936 ata_wait_idle(ap);
937
938 ap->ops->dev_select(ap, device);
939
940 if (wait) {
941 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
942 msleep(150);
943 ata_wait_idle(ap);
944 }
945}
946
947/**
948 * ata_dump_id - IDENTIFY DEVICE info debugging output
949 * @dev: Device whose IDENTIFY DEVICE page we will dump
950 *
951 * Dump selected 16-bit words from a detected device's
952 * IDENTIFY PAGE page.
953 *
954 * LOCKING:
955 * caller.
956 */
957
958static inline void ata_dump_id(struct ata_device *dev)
959{
960 DPRINTK("49==0x%04x "
961 "53==0x%04x "
962 "63==0x%04x "
963 "64==0x%04x "
964 "75==0x%04x \n",
965 dev->id[49],
966 dev->id[53],
967 dev->id[63],
968 dev->id[64],
969 dev->id[75]);
970 DPRINTK("80==0x%04x "
971 "81==0x%04x "
972 "82==0x%04x "
973 "83==0x%04x "
974 "84==0x%04x \n",
975 dev->id[80],
976 dev->id[81],
977 dev->id[82],
978 dev->id[83],
979 dev->id[84]);
980 DPRINTK("88==0x%04x "
981 "93==0x%04x\n",
982 dev->id[88],
983 dev->id[93]);
984}
985
986/**
987 * ata_dev_identify - obtain IDENTIFY x DEVICE page
988 * @ap: port on which device we wish to probe resides
989 * @device: device bus address, starting at zero
990 *
991 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
992 * command, and read back the 512-byte device information page.
993 * The device information page is fed to us via the standard
994 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
995 * using standard PIO-IN paths)
996 *
997 * After reading the device information page, we use several
998 * bits of information from it to initialize data structures
999 * that will be used during the lifetime of the ata_device.
1000 * Other data from the info page is used to disqualify certain
1001 * older ATA devices we do not wish to support.
1002 *
1003 * LOCKING:
1004 * Inherited from caller. Some functions called by this function
1005 * obtain the host_set lock.
1006 */
1007
1008static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1009{
1010 struct ata_device *dev = &ap->device[device];
1011 unsigned int i;
1012 u16 tmp;
1013 unsigned long xfer_modes;
1014 u8 status;
1015 unsigned int using_edd;
1016 DECLARE_COMPLETION(wait);
1017 struct ata_queued_cmd *qc;
1018 unsigned long flags;
1019 int rc;
1020
1021 if (!ata_dev_present(dev)) {
1022 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1023 ap->id, device);
1024 return;
1025 }
1026
1027 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1028 using_edd = 0;
1029 else
1030 using_edd = 1;
1031
1032 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1033
1034 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1035 dev->class == ATA_DEV_NONE);
1036
1037 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1038
1039 qc = ata_qc_new_init(ap, dev);
1040 BUG_ON(qc == NULL);
1041
1042 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1043 qc->dma_dir = DMA_FROM_DEVICE;
1044 qc->tf.protocol = ATA_PROT_PIO;
1045 qc->nsect = 1;
1046
1047retry:
1048 if (dev->class == ATA_DEV_ATA) {
1049 qc->tf.command = ATA_CMD_ID_ATA;
1050 DPRINTK("do ATA identify\n");
1051 } else {
1052 qc->tf.command = ATA_CMD_ID_ATAPI;
1053 DPRINTK("do ATAPI identify\n");
1054 }
1055
1056 qc->waiting = &wait;
1057 qc->complete_fn = ata_qc_complete_noop;
1058
1059 spin_lock_irqsave(&ap->host_set->lock, flags);
1060 rc = ata_qc_issue(qc);
1061 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1062
1063 if (rc)
1064 goto err_out;
1065 else
1066 wait_for_completion(&wait);
1067
1068 status = ata_chk_status(ap);
1069 if (status & ATA_ERR) {
1070 /*
1071 * arg! EDD works for all test cases, but seems to return
1072 * the ATA signature for some ATAPI devices. Until the
1073 * reason for this is found and fixed, we fix up the mess
1074 * here. If IDENTIFY DEVICE returns command aborted
1075 * (as ATAPI devices do), then we issue an
1076 * IDENTIFY PACKET DEVICE.
1077 *
1078 * ATA software reset (SRST, the default) does not appear
1079 * to have this problem.
1080 */
1081 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1082 u8 err = ata_chk_err(ap);
1083 if (err & ATA_ABORTED) {
1084 dev->class = ATA_DEV_ATAPI;
1085 qc->cursg = 0;
1086 qc->cursg_ofs = 0;
1087 qc->cursect = 0;
1088 qc->nsect = 1;
1089 goto retry;
1090 }
1091 }
1092 goto err_out;
1093 }
1094
1095 swap_buf_le16(dev->id, ATA_ID_WORDS);
1096
1097 /* print device capabilities */
1098 printk(KERN_DEBUG "ata%u: dev %u cfg "
1099 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1100 ap->id, device, dev->id[49],
1101 dev->id[82], dev->id[83], dev->id[84],
1102 dev->id[85], dev->id[86], dev->id[87],
1103 dev->id[88]);
1104
1105 /*
1106 * common ATA, ATAPI feature tests
1107 */
1108
1109 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1110 if (!ata_id_has_dma(dev->id) || !ata_id_has_lba(dev->id)) {
1111 printk(KERN_DEBUG "ata%u: no dma/lba\n", ap->id);
1112 goto err_out_nosup;
1113 }
1114
1115 /* quick-n-dirty find max transfer mode; for printk only */
1116 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1117 if (!xfer_modes)
1118 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1119 if (!xfer_modes) {
1120 xfer_modes = (dev->id[ATA_ID_PIO_MODES]) << (ATA_SHIFT_PIO + 3);
1121 xfer_modes |= (0x7 << ATA_SHIFT_PIO);
1122 }
1123
1124 ata_dump_id(dev);
1125
1126 /* ATA-specific feature tests */
1127 if (dev->class == ATA_DEV_ATA) {
1128 if (!ata_id_is_ata(dev->id)) /* sanity check */
1129 goto err_out_nosup;
1130
1131 tmp = dev->id[ATA_ID_MAJOR_VER];
1132 for (i = 14; i >= 1; i--)
1133 if (tmp & (1 << i))
1134 break;
1135
1136 /* we require at least ATA-3 */
1137 if (i < 3) {
1138 printk(KERN_DEBUG "ata%u: no ATA-3\n", ap->id);
1139 goto err_out_nosup;
1140 }
1141
1142 if (ata_id_has_lba48(dev->id)) {
1143 dev->flags |= ATA_DFLAG_LBA48;
1144 dev->n_sectors = ata_id_u64(dev->id, 100);
1145 } else {
1146 dev->n_sectors = ata_id_u32(dev->id, 60);
1147 }
1148
1149 ap->host->max_cmd_len = 16;
1150
1151 /* print device info to dmesg */
1152 printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1153 ap->id, device,
1154 ata_mode_string(xfer_modes),
1155 (unsigned long long)dev->n_sectors,
1156 dev->flags & ATA_DFLAG_LBA48 ? " lba48" : "");
1157 }
1158
1159 /* ATAPI-specific feature tests */
1160 else {
1161 if (ata_id_is_ata(dev->id)) /* sanity check */
1162 goto err_out_nosup;
1163
1164 rc = atapi_cdb_len(dev->id);
1165 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1166 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1167 goto err_out_nosup;
1168 }
1169 ap->cdb_len = (unsigned int) rc;
1170 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1171
1172 /* print device info to dmesg */
1173 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1174 ap->id, device,
1175 ata_mode_string(xfer_modes));
1176 }
1177
1178 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1179 return;
1180
1181err_out_nosup:
1182 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1183 ap->id, device);
1184err_out:
1185 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1186 DPRINTK("EXIT, err\n");
1187}
1188
1189/**
1190 * ata_bus_probe - Reset and probe ATA bus
1191 * @ap: Bus to probe
1192 *
0cba632b
JG
1193 * Master ATA bus probing function. Initiates a hardware-dependent
1194 * bus reset, then attempts to identify any devices found on
1195 * the bus.
1196 *
1da177e4 1197 * LOCKING:
0cba632b 1198 * PCI/etc. bus probe sem.
1da177e4
LT
1199 *
1200 * RETURNS:
1201 * Zero on success, non-zero on error.
1202 */
1203
1204static int ata_bus_probe(struct ata_port *ap)
1205{
1206 unsigned int i, found = 0;
1207
1208 ap->ops->phy_reset(ap);
1209 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1210 goto err_out;
1211
1212 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1213 ata_dev_identify(ap, i);
1214 if (ata_dev_present(&ap->device[i])) {
1215 found = 1;
1216 if (ap->ops->dev_config)
1217 ap->ops->dev_config(ap, &ap->device[i]);
1218 }
1219 }
1220
1221 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1222 goto err_out_disable;
1223
1224 ata_set_mode(ap);
1225 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1226 goto err_out_disable;
1227
1228 return 0;
1229
1230err_out_disable:
1231 ap->ops->port_disable(ap);
1232err_out:
1233 return -1;
1234}
1235
1236/**
0cba632b
JG
1237 * ata_port_probe - Mark port as enabled
1238 * @ap: Port for which we indicate enablement
1da177e4 1239 *
0cba632b
JG
1240 * Modify @ap data structure such that the system
1241 * thinks that the entire port is enabled.
1242 *
1243 * LOCKING: host_set lock, or some other form of
1244 * serialization.
1da177e4
LT
1245 */
1246
1247void ata_port_probe(struct ata_port *ap)
1248{
1249 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1250}
1251
1252/**
780a87f7
JG
1253 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1254 * @ap: SATA port associated with target SATA PHY.
1da177e4 1255 *
780a87f7
JG
1256 * This function issues commands to standard SATA Sxxx
1257 * PHY registers, to wake up the phy (and device), and
1258 * clear any reset condition.
1259 *
0cba632b
JG
1260 * LOCKING:
1261 * PCI/etc. bus probe sem.
1da177e4
LT
1262 *
1263 */
1264void __sata_phy_reset(struct ata_port *ap)
1265{
1266 u32 sstatus;
1267 unsigned long timeout = jiffies + (HZ * 5);
1268
1269 if (ap->flags & ATA_FLAG_SATA_RESET) {
cdcca89e
BR
1270 /* issue phy wake/reset */
1271 scr_write_flush(ap, SCR_CONTROL, 0x301);
1da177e4
LT
1272 udelay(400); /* FIXME: a guess */
1273 }
cdcca89e 1274 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1da177e4
LT
1275
1276 /* wait for phy to become ready, if necessary */
1277 do {
1278 msleep(200);
1279 sstatus = scr_read(ap, SCR_STATUS);
1280 if ((sstatus & 0xf) != 1)
1281 break;
1282 } while (time_before(jiffies, timeout));
1283
1284 /* TODO: phy layer with polling, timeouts, etc. */
1285 if (sata_dev_present(ap))
1286 ata_port_probe(ap);
1287 else {
1288 sstatus = scr_read(ap, SCR_STATUS);
1289 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1290 ap->id, sstatus);
1291 ata_port_disable(ap);
1292 }
1293
1294 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1295 return;
1296
1297 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1298 ata_port_disable(ap);
1299 return;
1300 }
1301
1302 ap->cbl = ATA_CBL_SATA;
1303}
1304
1305/**
780a87f7
JG
1306 * sata_phy_reset - Reset SATA bus.
1307 * @ap: SATA port associated with target SATA PHY.
1da177e4 1308 *
780a87f7
JG
1309 * This function resets the SATA bus, and then probes
1310 * the bus for devices.
1311 *
0cba632b
JG
1312 * LOCKING:
1313 * PCI/etc. bus probe sem.
1da177e4
LT
1314 *
1315 */
1316void sata_phy_reset(struct ata_port *ap)
1317{
1318 __sata_phy_reset(ap);
1319 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1320 return;
1321 ata_bus_reset(ap);
1322}
1323
1324/**
780a87f7
JG
1325 * ata_port_disable - Disable port.
1326 * @ap: Port to be disabled.
1da177e4 1327 *
780a87f7
JG
1328 * Modify @ap data structure such that the system
1329 * thinks that the entire port is disabled, and should
1330 * never attempt to probe or communicate with devices
1331 * on this port.
1332 *
1333 * LOCKING: host_set lock, or some other form of
1334 * serialization.
1da177e4
LT
1335 */
1336
1337void ata_port_disable(struct ata_port *ap)
1338{
1339 ap->device[0].class = ATA_DEV_NONE;
1340 ap->device[1].class = ATA_DEV_NONE;
1341 ap->flags |= ATA_FLAG_PORT_DISABLED;
1342}
1343
1344static struct {
1345 unsigned int shift;
1346 u8 base;
1347} xfer_mode_classes[] = {
1348 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1349 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1350 { ATA_SHIFT_PIO, XFER_PIO_0 },
1351};
1352
1353static inline u8 base_from_shift(unsigned int shift)
1354{
1355 int i;
1356
1357 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1358 if (xfer_mode_classes[i].shift == shift)
1359 return xfer_mode_classes[i].base;
1360
1361 return 0xff;
1362}
1363
1364static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1365{
1366 int ofs, idx;
1367 u8 base;
1368
1369 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1370 return;
1371
1372 if (dev->xfer_shift == ATA_SHIFT_PIO)
1373 dev->flags |= ATA_DFLAG_PIO;
1374
1375 ata_dev_set_xfermode(ap, dev);
1376
1377 base = base_from_shift(dev->xfer_shift);
1378 ofs = dev->xfer_mode - base;
1379 idx = ofs + dev->xfer_shift;
1380 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1381
1382 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1383 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1384
1385 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1386 ap->id, dev->devno, xfer_mode_str[idx]);
1387}
1388
1389static int ata_host_set_pio(struct ata_port *ap)
1390{
1391 unsigned int mask;
1392 int x, i;
1393 u8 base, xfer_mode;
1394
1395 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1396 x = fgb(mask);
1397 if (x < 0) {
1398 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1399 return -1;
1400 }
1401
1402 base = base_from_shift(ATA_SHIFT_PIO);
1403 xfer_mode = base + x;
1404
1405 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1406 (int)base, (int)xfer_mode, mask, x);
1407
1408 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1409 struct ata_device *dev = &ap->device[i];
1410 if (ata_dev_present(dev)) {
1411 dev->pio_mode = xfer_mode;
1412 dev->xfer_mode = xfer_mode;
1413 dev->xfer_shift = ATA_SHIFT_PIO;
1414 if (ap->ops->set_piomode)
1415 ap->ops->set_piomode(ap, dev);
1416 }
1417 }
1418
1419 return 0;
1420}
1421
1422static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1423 unsigned int xfer_shift)
1424{
1425 int i;
1426
1427 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1428 struct ata_device *dev = &ap->device[i];
1429 if (ata_dev_present(dev)) {
1430 dev->dma_mode = xfer_mode;
1431 dev->xfer_mode = xfer_mode;
1432 dev->xfer_shift = xfer_shift;
1433 if (ap->ops->set_dmamode)
1434 ap->ops->set_dmamode(ap, dev);
1435 }
1436 }
1437}
1438
1439/**
1440 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1441 * @ap: port on which timings will be programmed
1442 *
780a87f7
JG
1443 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1444 *
0cba632b
JG
1445 * LOCKING:
1446 * PCI/etc. bus probe sem.
1da177e4
LT
1447 *
1448 */
1449static void ata_set_mode(struct ata_port *ap)
1450{
1451 unsigned int i, xfer_shift;
1452 u8 xfer_mode;
1453 int rc;
1454
1455 /* step 1: always set host PIO timings */
1456 rc = ata_host_set_pio(ap);
1457 if (rc)
1458 goto err_out;
1459
1460 /* step 2: choose the best data xfer mode */
1461 xfer_mode = xfer_shift = 0;
1462 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1463 if (rc)
1464 goto err_out;
1465
1466 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1467 if (xfer_shift != ATA_SHIFT_PIO)
1468 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1469
1470 /* step 4: update devices' xfer mode */
1471 ata_dev_set_mode(ap, &ap->device[0]);
1472 ata_dev_set_mode(ap, &ap->device[1]);
1473
1474 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1475 return;
1476
1477 if (ap->ops->post_set_mode)
1478 ap->ops->post_set_mode(ap);
1479
1480 for (i = 0; i < 2; i++) {
1481 struct ata_device *dev = &ap->device[i];
1482 ata_dev_set_protocol(dev);
1483 }
1484
1485 return;
1486
1487err_out:
1488 ata_port_disable(ap);
1489}
1490
1491/**
1492 * ata_busy_sleep - sleep until BSY clears, or timeout
1493 * @ap: port containing status register to be polled
1494 * @tmout_pat: impatience timeout
1495 * @tmout: overall timeout
1496 *
780a87f7
JG
1497 * Sleep until ATA Status register bit BSY clears,
1498 * or a timeout occurs.
1499 *
1500 * LOCKING: None.
1da177e4
LT
1501 *
1502 */
1503
1504static unsigned int ata_busy_sleep (struct ata_port *ap,
1505 unsigned long tmout_pat,
1506 unsigned long tmout)
1507{
1508 unsigned long timer_start, timeout;
1509 u8 status;
1510
1511 status = ata_busy_wait(ap, ATA_BUSY, 300);
1512 timer_start = jiffies;
1513 timeout = timer_start + tmout_pat;
1514 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1515 msleep(50);
1516 status = ata_busy_wait(ap, ATA_BUSY, 3);
1517 }
1518
1519 if (status & ATA_BUSY)
1520 printk(KERN_WARNING "ata%u is slow to respond, "
1521 "please be patient\n", ap->id);
1522
1523 timeout = timer_start + tmout;
1524 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1525 msleep(50);
1526 status = ata_chk_status(ap);
1527 }
1528
1529 if (status & ATA_BUSY) {
1530 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1531 ap->id, tmout / HZ);
1532 return 1;
1533 }
1534
1535 return 0;
1536}
1537
1538static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1539{
1540 struct ata_ioports *ioaddr = &ap->ioaddr;
1541 unsigned int dev0 = devmask & (1 << 0);
1542 unsigned int dev1 = devmask & (1 << 1);
1543 unsigned long timeout;
1544
1545 /* if device 0 was found in ata_devchk, wait for its
1546 * BSY bit to clear
1547 */
1548 if (dev0)
1549 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1550
1551 /* if device 1 was found in ata_devchk, wait for
1552 * register access, then wait for BSY to clear
1553 */
1554 timeout = jiffies + ATA_TMOUT_BOOT;
1555 while (dev1) {
1556 u8 nsect, lbal;
1557
1558 ap->ops->dev_select(ap, 1);
1559 if (ap->flags & ATA_FLAG_MMIO) {
1560 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1561 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1562 } else {
1563 nsect = inb(ioaddr->nsect_addr);
1564 lbal = inb(ioaddr->lbal_addr);
1565 }
1566 if ((nsect == 1) && (lbal == 1))
1567 break;
1568 if (time_after(jiffies, timeout)) {
1569 dev1 = 0;
1570 break;
1571 }
1572 msleep(50); /* give drive a breather */
1573 }
1574 if (dev1)
1575 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1576
1577 /* is all this really necessary? */
1578 ap->ops->dev_select(ap, 0);
1579 if (dev1)
1580 ap->ops->dev_select(ap, 1);
1581 if (dev0)
1582 ap->ops->dev_select(ap, 0);
1583}
1584
1585/**
0cba632b
JG
1586 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1587 * @ap: Port to reset and probe
1588 *
1589 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1590 * probe the bus. Not often used these days.
1da177e4 1591 *
0cba632b
JG
1592 * LOCKING:
1593 * PCI/etc. bus probe sem.
1da177e4
LT
1594 *
1595 */
1596
1597static unsigned int ata_bus_edd(struct ata_port *ap)
1598{
1599 struct ata_taskfile tf;
1600
1601 /* set up execute-device-diag (bus reset) taskfile */
1602 /* also, take interrupts to a known state (disabled) */
1603 DPRINTK("execute-device-diag\n");
1604 ata_tf_init(ap, &tf, 0);
1605 tf.ctl |= ATA_NIEN;
1606 tf.command = ATA_CMD_EDD;
1607 tf.protocol = ATA_PROT_NODATA;
1608
1609 /* do bus reset */
1610 ata_tf_to_host(ap, &tf);
1611
1612 /* spec says at least 2ms. but who knows with those
1613 * crazy ATAPI devices...
1614 */
1615 msleep(150);
1616
1617 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1618}
1619
1620static unsigned int ata_bus_softreset(struct ata_port *ap,
1621 unsigned int devmask)
1622{
1623 struct ata_ioports *ioaddr = &ap->ioaddr;
1624
1625 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1626
1627 /* software reset. causes dev0 to be selected */
1628 if (ap->flags & ATA_FLAG_MMIO) {
1629 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1630 udelay(20); /* FIXME: flush */
1631 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1632 udelay(20); /* FIXME: flush */
1633 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1634 } else {
1635 outb(ap->ctl, ioaddr->ctl_addr);
1636 udelay(10);
1637 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1638 udelay(10);
1639 outb(ap->ctl, ioaddr->ctl_addr);
1640 }
1641
1642 /* spec mandates ">= 2ms" before checking status.
1643 * We wait 150ms, because that was the magic delay used for
1644 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1645 * between when the ATA command register is written, and then
1646 * status is checked. Because waiting for "a while" before
1647 * checking status is fine, post SRST, we perform this magic
1648 * delay here as well.
1649 */
1650 msleep(150);
1651
1652 ata_bus_post_reset(ap, devmask);
1653
1654 return 0;
1655}
1656
1657/**
1658 * ata_bus_reset - reset host port and associated ATA channel
1659 * @ap: port to reset
1660 *
1661 * This is typically the first time we actually start issuing
1662 * commands to the ATA channel. We wait for BSY to clear, then
1663 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1664 * result. Determine what devices, if any, are on the channel
1665 * by looking at the device 0/1 error register. Look at the signature
1666 * stored in each device's taskfile registers, to determine if
1667 * the device is ATA or ATAPI.
1668 *
1669 * LOCKING:
0cba632b
JG
1670 * PCI/etc. bus probe sem.
1671 * Obtains host_set lock.
1da177e4
LT
1672 *
1673 * SIDE EFFECTS:
1674 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1675 */
1676
1677void ata_bus_reset(struct ata_port *ap)
1678{
1679 struct ata_ioports *ioaddr = &ap->ioaddr;
1680 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1681 u8 err;
1682 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1683
1684 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1685
1686 /* determine if device 0/1 are present */
1687 if (ap->flags & ATA_FLAG_SATA_RESET)
1688 dev0 = 1;
1689 else {
1690 dev0 = ata_devchk(ap, 0);
1691 if (slave_possible)
1692 dev1 = ata_devchk(ap, 1);
1693 }
1694
1695 if (dev0)
1696 devmask |= (1 << 0);
1697 if (dev1)
1698 devmask |= (1 << 1);
1699
1700 /* select device 0 again */
1701 ap->ops->dev_select(ap, 0);
1702
1703 /* issue bus reset */
1704 if (ap->flags & ATA_FLAG_SRST)
1705 rc = ata_bus_softreset(ap, devmask);
1706 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1707 /* set up device control */
1708 if (ap->flags & ATA_FLAG_MMIO)
1709 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1710 else
1711 outb(ap->ctl, ioaddr->ctl_addr);
1712 rc = ata_bus_edd(ap);
1713 }
1714
1715 if (rc)
1716 goto err_out;
1717
1718 /*
1719 * determine by signature whether we have ATA or ATAPI devices
1720 */
1721 err = ata_dev_try_classify(ap, 0);
1722 if ((slave_possible) && (err != 0x81))
1723 ata_dev_try_classify(ap, 1);
1724
1725 /* re-enable interrupts */
1726 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1727 ata_irq_on(ap);
1728
1729 /* is double-select really necessary? */
1730 if (ap->device[1].class != ATA_DEV_NONE)
1731 ap->ops->dev_select(ap, 1);
1732 if (ap->device[0].class != ATA_DEV_NONE)
1733 ap->ops->dev_select(ap, 0);
1734
1735 /* if no devices were detected, disable this port */
1736 if ((ap->device[0].class == ATA_DEV_NONE) &&
1737 (ap->device[1].class == ATA_DEV_NONE))
1738 goto err_out;
1739
1740 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1741 /* set up device control for ATA_FLAG_SATA_RESET */
1742 if (ap->flags & ATA_FLAG_MMIO)
1743 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1744 else
1745 outb(ap->ctl, ioaddr->ctl_addr);
1746 }
1747
1748 DPRINTK("EXIT\n");
1749 return;
1750
1751err_out:
1752 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1753 ap->ops->port_disable(ap);
1754
1755 DPRINTK("EXIT\n");
1756}
1757
1758static void ata_pr_blacklisted(struct ata_port *ap, struct ata_device *dev)
1759{
1760 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1761 ap->id, dev->devno);
1762}
1763
1764static const char * ata_dma_blacklist [] = {
1765 "WDC AC11000H",
1766 "WDC AC22100H",
1767 "WDC AC32500H",
1768 "WDC AC33100H",
1769 "WDC AC31600H",
1770 "WDC AC32100H",
1771 "WDC AC23200L",
1772 "Compaq CRD-8241B",
1773 "CRD-8400B",
1774 "CRD-8480B",
1775 "CRD-8482B",
1776 "CRD-84",
1777 "SanDisk SDP3B",
1778 "SanDisk SDP3B-64",
1779 "SANYO CD-ROM CRD",
1780 "HITACHI CDR-8",
1781 "HITACHI CDR-8335",
1782 "HITACHI CDR-8435",
1783 "Toshiba CD-ROM XM-6202B",
1784 "CD-532E-A",
1785 "E-IDE CD-ROM CR-840",
1786 "CD-ROM Drive/F5A",
1787 "WPI CDD-820",
1788 "SAMSUNG CD-ROM SC-148C",
1789 "SAMSUNG CD-ROM SC",
1790 "SanDisk SDP3B-64",
1791 "SAMSUNG CD-ROM SN-124",
1792 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1793 "_NEC DV5800A",
1794};
1795
1796static int ata_dma_blacklisted(struct ata_port *ap, struct ata_device *dev)
1797{
1798 unsigned char model_num[40];
1799 char *s;
1800 unsigned int len;
1801 int i;
1802
1803 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
1804 sizeof(model_num));
1805 s = &model_num[0];
1806 len = strnlen(s, sizeof(model_num));
1807
1808 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1809 while ((len > 0) && (s[len - 1] == ' ')) {
1810 len--;
1811 s[len] = 0;
1812 }
1813
1814 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
1815 if (!strncmp(ata_dma_blacklist[i], s, len))
1816 return 1;
1817
1818 return 0;
1819}
1820
1821static unsigned int ata_get_mode_mask(struct ata_port *ap, int shift)
1822{
1823 struct ata_device *master, *slave;
1824 unsigned int mask;
1825
1826 master = &ap->device[0];
1827 slave = &ap->device[1];
1828
1829 assert (ata_dev_present(master) || ata_dev_present(slave));
1830
1831 if (shift == ATA_SHIFT_UDMA) {
1832 mask = ap->udma_mask;
1833 if (ata_dev_present(master)) {
1834 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
1835 if (ata_dma_blacklisted(ap, master)) {
1836 mask = 0;
1837 ata_pr_blacklisted(ap, master);
1838 }
1839 }
1840 if (ata_dev_present(slave)) {
1841 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
1842 if (ata_dma_blacklisted(ap, slave)) {
1843 mask = 0;
1844 ata_pr_blacklisted(ap, slave);
1845 }
1846 }
1847 }
1848 else if (shift == ATA_SHIFT_MWDMA) {
1849 mask = ap->mwdma_mask;
1850 if (ata_dev_present(master)) {
1851 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
1852 if (ata_dma_blacklisted(ap, master)) {
1853 mask = 0;
1854 ata_pr_blacklisted(ap, master);
1855 }
1856 }
1857 if (ata_dev_present(slave)) {
1858 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
1859 if (ata_dma_blacklisted(ap, slave)) {
1860 mask = 0;
1861 ata_pr_blacklisted(ap, slave);
1862 }
1863 }
1864 }
1865 else if (shift == ATA_SHIFT_PIO) {
1866 mask = ap->pio_mask;
1867 if (ata_dev_present(master)) {
1868 /* spec doesn't return explicit support for
1869 * PIO0-2, so we fake it
1870 */
1871 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
1872 tmp_mode <<= 3;
1873 tmp_mode |= 0x7;
1874 mask &= tmp_mode;
1875 }
1876 if (ata_dev_present(slave)) {
1877 /* spec doesn't return explicit support for
1878 * PIO0-2, so we fake it
1879 */
1880 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
1881 tmp_mode <<= 3;
1882 tmp_mode |= 0x7;
1883 mask &= tmp_mode;
1884 }
1885 }
1886 else {
1887 mask = 0xffffffff; /* shut up compiler warning */
1888 BUG();
1889 }
1890
1891 return mask;
1892}
1893
1894/* find greatest bit */
1895static int fgb(u32 bitmap)
1896{
1897 unsigned int i;
1898 int x = -1;
1899
1900 for (i = 0; i < 32; i++)
1901 if (bitmap & (1 << i))
1902 x = i;
1903
1904 return x;
1905}
1906
1907/**
1908 * ata_choose_xfer_mode - attempt to find best transfer mode
1909 * @ap: Port for which an xfer mode will be selected
1910 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
1911 * @xfer_shift_out: (output) bit shift that selects this mode
1912 *
0cba632b
JG
1913 * Based on host and device capabilities, determine the
1914 * maximum transfer mode that is amenable to all.
1915 *
1da177e4 1916 * LOCKING:
0cba632b 1917 * PCI/etc. bus probe sem.
1da177e4
LT
1918 *
1919 * RETURNS:
1920 * Zero on success, negative on error.
1921 */
1922
1923static int ata_choose_xfer_mode(struct ata_port *ap,
1924 u8 *xfer_mode_out,
1925 unsigned int *xfer_shift_out)
1926{
1927 unsigned int mask, shift;
1928 int x, i;
1929
1930 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
1931 shift = xfer_mode_classes[i].shift;
1932 mask = ata_get_mode_mask(ap, shift);
1933
1934 x = fgb(mask);
1935 if (x >= 0) {
1936 *xfer_mode_out = xfer_mode_classes[i].base + x;
1937 *xfer_shift_out = shift;
1938 return 0;
1939 }
1940 }
1941
1942 return -1;
1943}
1944
1945/**
1946 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
1947 * @ap: Port associated with device @dev
1948 * @dev: Device to which command will be sent
1949 *
780a87f7
JG
1950 * Issue SET FEATURES - XFER MODE command to device @dev
1951 * on port @ap.
1952 *
0cba632b
JG
1953 * LOCKING:
1954 * PCI/etc. bus probe sem.
1da177e4
LT
1955 */
1956
1957static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
1958{
1959 DECLARE_COMPLETION(wait);
1960 struct ata_queued_cmd *qc;
1961 int rc;
1962 unsigned long flags;
1963
1964 /* set up set-features taskfile */
1965 DPRINTK("set features - xfer mode\n");
1966
1967 qc = ata_qc_new_init(ap, dev);
1968 BUG_ON(qc == NULL);
1969
1970 qc->tf.command = ATA_CMD_SET_FEATURES;
1971 qc->tf.feature = SETFEATURES_XFER;
1972 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1973 qc->tf.protocol = ATA_PROT_NODATA;
1974 qc->tf.nsect = dev->xfer_mode;
1975
1976 qc->waiting = &wait;
1977 qc->complete_fn = ata_qc_complete_noop;
1978
1979 spin_lock_irqsave(&ap->host_set->lock, flags);
1980 rc = ata_qc_issue(qc);
1981 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1982
1983 if (rc)
1984 ata_port_disable(ap);
1985 else
1986 wait_for_completion(&wait);
1987
1988 DPRINTK("EXIT\n");
1989}
1990
1991/**
0cba632b
JG
1992 * ata_sg_clean - Unmap DMA memory associated with command
1993 * @qc: Command containing DMA memory to be released
1994 *
1995 * Unmap all mapped DMA memory associated with this command.
1da177e4
LT
1996 *
1997 * LOCKING:
0cba632b 1998 * spin_lock_irqsave(host_set lock)
1da177e4
LT
1999 */
2000
2001static void ata_sg_clean(struct ata_queued_cmd *qc)
2002{
2003 struct ata_port *ap = qc->ap;
2004 struct scatterlist *sg = qc->sg;
2005 int dir = qc->dma_dir;
2006
2007 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2008 assert(sg != NULL);
2009
2010 if (qc->flags & ATA_QCFLAG_SINGLE)
2011 assert(qc->n_elem == 1);
2012
2013 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2014
2015 if (qc->flags & ATA_QCFLAG_SG)
2016 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2017 else
2018 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2019 sg_dma_len(&sg[0]), dir);
2020
2021 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2022 qc->sg = NULL;
2023}
2024
2025/**
2026 * ata_fill_sg - Fill PCI IDE PRD table
2027 * @qc: Metadata associated with taskfile to be transferred
2028 *
780a87f7
JG
2029 * Fill PCI IDE PRD (scatter-gather) table with segments
2030 * associated with the current disk command.
2031 *
1da177e4 2032 * LOCKING:
780a87f7 2033 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2034 *
2035 */
2036static void ata_fill_sg(struct ata_queued_cmd *qc)
2037{
2038 struct scatterlist *sg = qc->sg;
2039 struct ata_port *ap = qc->ap;
2040 unsigned int idx, nelem;
2041
2042 assert(sg != NULL);
2043 assert(qc->n_elem > 0);
2044
2045 idx = 0;
2046 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2047 u32 addr, offset;
2048 u32 sg_len, len;
2049
2050 /* determine if physical DMA addr spans 64K boundary.
2051 * Note h/w doesn't support 64-bit, so we unconditionally
2052 * truncate dma_addr_t to u32.
2053 */
2054 addr = (u32) sg_dma_address(sg);
2055 sg_len = sg_dma_len(sg);
2056
2057 while (sg_len) {
2058 offset = addr & 0xffff;
2059 len = sg_len;
2060 if ((offset + sg_len) > 0x10000)
2061 len = 0x10000 - offset;
2062
2063 ap->prd[idx].addr = cpu_to_le32(addr);
2064 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2065 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2066
2067 idx++;
2068 sg_len -= len;
2069 addr += len;
2070 }
2071 }
2072
2073 if (idx)
2074 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2075}
2076/**
2077 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2078 * @qc: Metadata associated with taskfile to check
2079 *
780a87f7
JG
2080 * Allow low-level driver to filter ATA PACKET commands, returning
2081 * a status indicating whether or not it is OK to use DMA for the
2082 * supplied PACKET command.
2083 *
1da177e4 2084 * LOCKING:
0cba632b
JG
2085 * spin_lock_irqsave(host_set lock)
2086 *
1da177e4
LT
2087 * RETURNS: 0 when ATAPI DMA can be used
2088 * nonzero otherwise
2089 */
2090int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2091{
2092 struct ata_port *ap = qc->ap;
2093 int rc = 0; /* Assume ATAPI DMA is OK by default */
2094
2095 if (ap->ops->check_atapi_dma)
2096 rc = ap->ops->check_atapi_dma(qc);
2097
2098 return rc;
2099}
2100/**
2101 * ata_qc_prep - Prepare taskfile for submission
2102 * @qc: Metadata associated with taskfile to be prepared
2103 *
780a87f7
JG
2104 * Prepare ATA taskfile for submission.
2105 *
1da177e4
LT
2106 * LOCKING:
2107 * spin_lock_irqsave(host_set lock)
2108 */
2109void ata_qc_prep(struct ata_queued_cmd *qc)
2110{
2111 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2112 return;
2113
2114 ata_fill_sg(qc);
2115}
2116
0cba632b
JG
2117/**
2118 * ata_sg_init_one - Associate command with memory buffer
2119 * @qc: Command to be associated
2120 * @buf: Memory buffer
2121 * @buflen: Length of memory buffer, in bytes.
2122 *
2123 * Initialize the data-related elements of queued_cmd @qc
2124 * to point to a single memory buffer, @buf of byte length @buflen.
2125 *
2126 * LOCKING:
2127 * spin_lock_irqsave(host_set lock)
2128 */
2129
1da177e4
LT
2130void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2131{
2132 struct scatterlist *sg;
2133
2134 qc->flags |= ATA_QCFLAG_SINGLE;
2135
2136 memset(&qc->sgent, 0, sizeof(qc->sgent));
2137 qc->sg = &qc->sgent;
2138 qc->n_elem = 1;
2139 qc->buf_virt = buf;
2140
2141 sg = qc->sg;
2142 sg->page = virt_to_page(buf);
2143 sg->offset = (unsigned long) buf & ~PAGE_MASK;
32529e01 2144 sg->length = buflen;
1da177e4
LT
2145}
2146
0cba632b
JG
2147/**
2148 * ata_sg_init - Associate command with scatter-gather table.
2149 * @qc: Command to be associated
2150 * @sg: Scatter-gather table.
2151 * @n_elem: Number of elements in s/g table.
2152 *
2153 * Initialize the data-related elements of queued_cmd @qc
2154 * to point to a scatter-gather table @sg, containing @n_elem
2155 * elements.
2156 *
2157 * LOCKING:
2158 * spin_lock_irqsave(host_set lock)
2159 */
2160
1da177e4
LT
2161void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2162 unsigned int n_elem)
2163{
2164 qc->flags |= ATA_QCFLAG_SG;
2165 qc->sg = sg;
2166 qc->n_elem = n_elem;
2167}
2168
2169/**
0cba632b
JG
2170 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2171 * @qc: Command with memory buffer to be mapped.
2172 *
2173 * DMA-map the memory buffer associated with queued_cmd @qc.
1da177e4
LT
2174 *
2175 * LOCKING:
2176 * spin_lock_irqsave(host_set lock)
2177 *
2178 * RETURNS:
0cba632b 2179 * Zero on success, negative on error.
1da177e4
LT
2180 */
2181
2182static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2183{
2184 struct ata_port *ap = qc->ap;
2185 int dir = qc->dma_dir;
2186 struct scatterlist *sg = qc->sg;
2187 dma_addr_t dma_address;
2188
2189 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
32529e01 2190 sg->length, dir);
1da177e4
LT
2191 if (dma_mapping_error(dma_address))
2192 return -1;
2193
2194 sg_dma_address(sg) = dma_address;
32529e01 2195 sg_dma_len(sg) = sg->length;
1da177e4
LT
2196
2197 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2198 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2199
2200 return 0;
2201}
2202
2203/**
0cba632b
JG
2204 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2205 * @qc: Command with scatter-gather table to be mapped.
2206 *
2207 * DMA-map the scatter-gather table associated with queued_cmd @qc.
1da177e4
LT
2208 *
2209 * LOCKING:
2210 * spin_lock_irqsave(host_set lock)
2211 *
2212 * RETURNS:
0cba632b 2213 * Zero on success, negative on error.
1da177e4
LT
2214 *
2215 */
2216
2217static int ata_sg_setup(struct ata_queued_cmd *qc)
2218{
2219 struct ata_port *ap = qc->ap;
2220 struct scatterlist *sg = qc->sg;
2221 int n_elem, dir;
2222
2223 VPRINTK("ENTER, ata%u\n", ap->id);
2224 assert(qc->flags & ATA_QCFLAG_SG);
2225
2226 dir = qc->dma_dir;
2227 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2228 if (n_elem < 1)
2229 return -1;
2230
2231 DPRINTK("%d sg elements mapped\n", n_elem);
2232
2233 qc->n_elem = n_elem;
2234
2235 return 0;
2236}
2237
2238/**
2239 * ata_pio_poll -
2240 * @ap:
2241 *
2242 * LOCKING:
0cba632b 2243 * None. (executing in kernel thread context)
1da177e4
LT
2244 *
2245 * RETURNS:
2246 *
2247 */
2248
2249static unsigned long ata_pio_poll(struct ata_port *ap)
2250{
2251 u8 status;
2252 unsigned int poll_state = PIO_ST_UNKNOWN;
2253 unsigned int reg_state = PIO_ST_UNKNOWN;
2254 const unsigned int tmout_state = PIO_ST_TMOUT;
2255
2256 switch (ap->pio_task_state) {
2257 case PIO_ST:
2258 case PIO_ST_POLL:
2259 poll_state = PIO_ST_POLL;
2260 reg_state = PIO_ST;
2261 break;
2262 case PIO_ST_LAST:
2263 case PIO_ST_LAST_POLL:
2264 poll_state = PIO_ST_LAST_POLL;
2265 reg_state = PIO_ST_LAST;
2266 break;
2267 default:
2268 BUG();
2269 break;
2270 }
2271
2272 status = ata_chk_status(ap);
2273 if (status & ATA_BUSY) {
2274 if (time_after(jiffies, ap->pio_task_timeout)) {
2275 ap->pio_task_state = tmout_state;
2276 return 0;
2277 }
2278 ap->pio_task_state = poll_state;
2279 return ATA_SHORT_PAUSE;
2280 }
2281
2282 ap->pio_task_state = reg_state;
2283 return 0;
2284}
2285
2286/**
2287 * ata_pio_complete -
2288 * @ap:
2289 *
2290 * LOCKING:
0cba632b 2291 * None. (executing in kernel thread context)
1da177e4
LT
2292 */
2293
2294static void ata_pio_complete (struct ata_port *ap)
2295{
2296 struct ata_queued_cmd *qc;
2297 u8 drv_stat;
2298
2299 /*
2300 * This is purely hueristic. This is a fast path.
2301 * Sometimes when we enter, BSY will be cleared in
2302 * a chk-status or two. If not, the drive is probably seeking
2303 * or something. Snooze for a couple msecs, then
2304 * chk-status again. If still busy, fall back to
2305 * PIO_ST_POLL state.
2306 */
2307 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2308 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2309 msleep(2);
2310 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2311 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2312 ap->pio_task_state = PIO_ST_LAST_POLL;
2313 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2314 return;
2315 }
2316 }
2317
2318 drv_stat = ata_wait_idle(ap);
2319 if (!ata_ok(drv_stat)) {
2320 ap->pio_task_state = PIO_ST_ERR;
2321 return;
2322 }
2323
2324 qc = ata_qc_from_tag(ap, ap->active_tag);
2325 assert(qc != NULL);
2326
2327 ap->pio_task_state = PIO_ST_IDLE;
2328
2329 ata_irq_on(ap);
2330
2331 ata_qc_complete(qc, drv_stat);
2332}
2333
2334void swap_buf_le16(u16 *buf, unsigned int buf_words)
2335{
2336#ifdef __BIG_ENDIAN
2337 unsigned int i;
2338
2339 for (i = 0; i < buf_words; i++)
2340 buf[i] = le16_to_cpu(buf[i]);
2341#endif /* __BIG_ENDIAN */
2342}
2343
2344static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2345 unsigned int buflen, int write_data)
2346{
2347 unsigned int i;
2348 unsigned int words = buflen >> 1;
2349 u16 *buf16 = (u16 *) buf;
2350 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2351
2352 if (write_data) {
2353 for (i = 0; i < words; i++)
2354 writew(le16_to_cpu(buf16[i]), mmio);
2355 } else {
2356 for (i = 0; i < words; i++)
2357 buf16[i] = cpu_to_le16(readw(mmio));
2358 }
2359}
2360
2361static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2362 unsigned int buflen, int write_data)
2363{
2364 unsigned int dwords = buflen >> 1;
2365
2366 if (write_data)
2367 outsw(ap->ioaddr.data_addr, buf, dwords);
2368 else
2369 insw(ap->ioaddr.data_addr, buf, dwords);
2370}
2371
2372static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2373 unsigned int buflen, int do_write)
2374{
2375 if (ap->flags & ATA_FLAG_MMIO)
2376 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2377 else
2378 ata_pio_data_xfer(ap, buf, buflen, do_write);
2379}
2380
2381static void ata_pio_sector(struct ata_queued_cmd *qc)
2382{
2383 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2384 struct scatterlist *sg = qc->sg;
2385 struct ata_port *ap = qc->ap;
2386 struct page *page;
2387 unsigned int offset;
2388 unsigned char *buf;
2389
2390 if (qc->cursect == (qc->nsect - 1))
2391 ap->pio_task_state = PIO_ST_LAST;
2392
2393 page = sg[qc->cursg].page;
2394 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2395
2396 /* get the current page and offset */
2397 page = nth_page(page, (offset >> PAGE_SHIFT));
2398 offset %= PAGE_SIZE;
2399
2400 buf = kmap(page) + offset;
2401
2402 qc->cursect++;
2403 qc->cursg_ofs++;
2404
32529e01 2405 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
1da177e4
LT
2406 qc->cursg++;
2407 qc->cursg_ofs = 0;
2408 }
2409
2410 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2411
2412 /* do the actual data transfer */
2413 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2414 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2415
2416 kunmap(page);
2417}
2418
2419static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2420{
2421 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2422 struct scatterlist *sg = qc->sg;
2423 struct ata_port *ap = qc->ap;
2424 struct page *page;
2425 unsigned char *buf;
2426 unsigned int offset, count;
2427
2428 if (qc->curbytes == qc->nbytes - bytes)
2429 ap->pio_task_state = PIO_ST_LAST;
2430
2431next_sg:
2432 sg = &qc->sg[qc->cursg];
2433
2434next_page:
2435 page = sg->page;
2436 offset = sg->offset + qc->cursg_ofs;
2437
2438 /* get the current page and offset */
2439 page = nth_page(page, (offset >> PAGE_SHIFT));
2440 offset %= PAGE_SIZE;
2441
32529e01 2442 count = min(sg->length - qc->cursg_ofs, bytes);
1da177e4
LT
2443
2444 /* don't cross page boundaries */
2445 count = min(count, (unsigned int)PAGE_SIZE - offset);
2446
2447 buf = kmap(page) + offset;
2448
2449 bytes -= count;
2450 qc->curbytes += count;
2451 qc->cursg_ofs += count;
2452
32529e01 2453 if (qc->cursg_ofs == sg->length) {
1da177e4
LT
2454 qc->cursg++;
2455 qc->cursg_ofs = 0;
2456 }
2457
2458 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2459
2460 /* do the actual data transfer */
2461 ata_data_xfer(ap, buf, count, do_write);
2462
2463 kunmap(page);
2464
2465 if (bytes) {
32529e01 2466 if (qc->cursg_ofs < sg->length)
1da177e4
LT
2467 goto next_page;
2468 goto next_sg;
2469 }
2470}
2471
2472static void atapi_pio_bytes(struct ata_queued_cmd *qc)
2473{
2474 struct ata_port *ap = qc->ap;
2475 struct ata_device *dev = qc->dev;
2476 unsigned int ireason, bc_lo, bc_hi, bytes;
2477 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
2478
2479 ap->ops->tf_read(ap, &qc->tf);
2480 ireason = qc->tf.nsect;
2481 bc_lo = qc->tf.lbam;
2482 bc_hi = qc->tf.lbah;
2483 bytes = (bc_hi << 8) | bc_lo;
2484
2485 /* shall be cleared to zero, indicating xfer of data */
2486 if (ireason & (1 << 0))
2487 goto err_out;
2488
2489 /* make sure transfer direction matches expected */
2490 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
2491 if (do_write != i_write)
2492 goto err_out;
2493
2494 __atapi_pio_bytes(qc, bytes);
2495
2496 return;
2497
2498err_out:
2499 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
2500 ap->id, dev->devno);
2501 ap->pio_task_state = PIO_ST_ERR;
2502}
2503
2504/**
2505 * ata_pio_sector -
2506 * @ap:
2507 *
2508 * LOCKING:
0cba632b 2509 * None. (executing in kernel thread context)
1da177e4
LT
2510 */
2511
2512static void ata_pio_block(struct ata_port *ap)
2513{
2514 struct ata_queued_cmd *qc;
2515 u8 status;
2516
2517 /*
2518 * This is purely hueristic. This is a fast path.
2519 * Sometimes when we enter, BSY will be cleared in
2520 * a chk-status or two. If not, the drive is probably seeking
2521 * or something. Snooze for a couple msecs, then
2522 * chk-status again. If still busy, fall back to
2523 * PIO_ST_POLL state.
2524 */
2525 status = ata_busy_wait(ap, ATA_BUSY, 5);
2526 if (status & ATA_BUSY) {
2527 msleep(2);
2528 status = ata_busy_wait(ap, ATA_BUSY, 10);
2529 if (status & ATA_BUSY) {
2530 ap->pio_task_state = PIO_ST_POLL;
2531 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2532 return;
2533 }
2534 }
2535
2536 qc = ata_qc_from_tag(ap, ap->active_tag);
2537 assert(qc != NULL);
2538
2539 if (is_atapi_taskfile(&qc->tf)) {
2540 /* no more data to transfer or unsupported ATAPI command */
2541 if ((status & ATA_DRQ) == 0) {
2542 ap->pio_task_state = PIO_ST_IDLE;
2543
2544 ata_irq_on(ap);
2545
2546 ata_qc_complete(qc, status);
2547 return;
2548 }
2549
2550 atapi_pio_bytes(qc);
2551 } else {
2552 /* handle BSY=0, DRQ=0 as error */
2553 if ((status & ATA_DRQ) == 0) {
2554 ap->pio_task_state = PIO_ST_ERR;
2555 return;
2556 }
2557
2558 ata_pio_sector(qc);
2559 }
2560}
2561
2562static void ata_pio_error(struct ata_port *ap)
2563{
2564 struct ata_queued_cmd *qc;
2565 u8 drv_stat;
2566
2567 qc = ata_qc_from_tag(ap, ap->active_tag);
2568 assert(qc != NULL);
2569
2570 drv_stat = ata_chk_status(ap);
2571 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
2572 ap->id, drv_stat);
2573
2574 ap->pio_task_state = PIO_ST_IDLE;
2575
2576 ata_irq_on(ap);
2577
2578 ata_qc_complete(qc, drv_stat | ATA_ERR);
2579}
2580
2581static void ata_pio_task(void *_data)
2582{
2583 struct ata_port *ap = _data;
2584 unsigned long timeout = 0;
2585
2586 switch (ap->pio_task_state) {
2587 case PIO_ST_IDLE:
2588 return;
2589
2590 case PIO_ST:
2591 ata_pio_block(ap);
2592 break;
2593
2594 case PIO_ST_LAST:
2595 ata_pio_complete(ap);
2596 break;
2597
2598 case PIO_ST_POLL:
2599 case PIO_ST_LAST_POLL:
2600 timeout = ata_pio_poll(ap);
2601 break;
2602
2603 case PIO_ST_TMOUT:
2604 case PIO_ST_ERR:
2605 ata_pio_error(ap);
2606 return;
2607 }
2608
2609 if (timeout)
2610 queue_delayed_work(ata_wq, &ap->pio_task,
2611 timeout);
2612 else
2613 queue_work(ata_wq, &ap->pio_task);
2614}
2615
2616static void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
2617 struct scsi_cmnd *cmd)
2618{
2619 DECLARE_COMPLETION(wait);
2620 struct ata_queued_cmd *qc;
2621 unsigned long flags;
2622 int rc;
2623
2624 DPRINTK("ATAPI request sense\n");
2625
2626 qc = ata_qc_new_init(ap, dev);
2627 BUG_ON(qc == NULL);
2628
2629 /* FIXME: is this needed? */
2630 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2631
2632 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2633 qc->dma_dir = DMA_FROM_DEVICE;
2634
21b1ed74 2635 memset(&qc->cdb, 0, ap->cdb_len);
1da177e4
LT
2636 qc->cdb[0] = REQUEST_SENSE;
2637 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2638
2639 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2640 qc->tf.command = ATA_CMD_PACKET;
2641
2642 qc->tf.protocol = ATA_PROT_ATAPI;
2643 qc->tf.lbam = (8 * 1024) & 0xff;
2644 qc->tf.lbah = (8 * 1024) >> 8;
2645 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2646
2647 qc->waiting = &wait;
2648 qc->complete_fn = ata_qc_complete_noop;
2649
2650 spin_lock_irqsave(&ap->host_set->lock, flags);
2651 rc = ata_qc_issue(qc);
2652 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2653
2654 if (rc)
2655 ata_port_disable(ap);
2656 else
2657 wait_for_completion(&wait);
2658
2659 DPRINTK("EXIT\n");
2660}
2661
2662/**
2663 * ata_qc_timeout - Handle timeout of queued command
2664 * @qc: Command that timed out
2665 *
2666 * Some part of the kernel (currently, only the SCSI layer)
2667 * has noticed that the active command on port @ap has not
2668 * completed after a specified length of time. Handle this
2669 * condition by disabling DMA (if necessary) and completing
2670 * transactions, with error if necessary.
2671 *
2672 * This also handles the case of the "lost interrupt", where
2673 * for some reason (possibly hardware bug, possibly driver bug)
2674 * an interrupt was not delivered to the driver, even though the
2675 * transaction completed successfully.
2676 *
2677 * LOCKING:
0cba632b 2678 * Inherited from SCSI layer (none, can sleep)
1da177e4
LT
2679 */
2680
2681static void ata_qc_timeout(struct ata_queued_cmd *qc)
2682{
2683 struct ata_port *ap = qc->ap;
2684 struct ata_device *dev = qc->dev;
2685 u8 host_stat = 0, drv_stat;
2686
2687 DPRINTK("ENTER\n");
2688
2689 /* FIXME: doesn't this conflict with timeout handling? */
2690 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
2691 struct scsi_cmnd *cmd = qc->scsicmd;
2692
2693 if (!scsi_eh_eflags_chk(cmd, SCSI_EH_CANCEL_CMD)) {
2694
2695 /* finish completing original command */
2696 __ata_qc_complete(qc);
2697
2698 atapi_request_sense(ap, dev, cmd);
2699
2700 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
2701 scsi_finish_command(cmd);
2702
2703 goto out;
2704 }
2705 }
2706
2707 /* hack alert! We cannot use the supplied completion
2708 * function from inside the ->eh_strategy_handler() thread.
2709 * libata is the only user of ->eh_strategy_handler() in
2710 * any kernel, so the default scsi_done() assumes it is
2711 * not being called from the SCSI EH.
2712 */
2713 qc->scsidone = scsi_finish_command;
2714
2715 switch (qc->tf.protocol) {
2716
2717 case ATA_PROT_DMA:
2718 case ATA_PROT_ATAPI_DMA:
2719 host_stat = ap->ops->bmdma_status(ap);
2720
2721 /* before we do anything else, clear DMA-Start bit */
2722 ap->ops->bmdma_stop(ap);
2723
2724 /* fall through */
2725
2726 default:
2727 ata_altstatus(ap);
2728 drv_stat = ata_chk_status(ap);
2729
2730 /* ack bmdma irq events */
2731 ap->ops->irq_clear(ap);
2732
2733 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
2734 ap->id, qc->tf.command, drv_stat, host_stat);
2735
2736 /* complete taskfile transaction */
2737 ata_qc_complete(qc, drv_stat);
2738 break;
2739 }
2740out:
2741 DPRINTK("EXIT\n");
2742}
2743
2744/**
2745 * ata_eng_timeout - Handle timeout of queued command
2746 * @ap: Port on which timed-out command is active
2747 *
2748 * Some part of the kernel (currently, only the SCSI layer)
2749 * has noticed that the active command on port @ap has not
2750 * completed after a specified length of time. Handle this
2751 * condition by disabling DMA (if necessary) and completing
2752 * transactions, with error if necessary.
2753 *
2754 * This also handles the case of the "lost interrupt", where
2755 * for some reason (possibly hardware bug, possibly driver bug)
2756 * an interrupt was not delivered to the driver, even though the
2757 * transaction completed successfully.
2758 *
2759 * LOCKING:
2760 * Inherited from SCSI layer (none, can sleep)
2761 */
2762
2763void ata_eng_timeout(struct ata_port *ap)
2764{
2765 struct ata_queued_cmd *qc;
2766
2767 DPRINTK("ENTER\n");
2768
2769 qc = ata_qc_from_tag(ap, ap->active_tag);
2770 if (!qc) {
2771 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
2772 ap->id);
2773 goto out;
2774 }
2775
2776 ata_qc_timeout(qc);
2777
2778out:
2779 DPRINTK("EXIT\n");
2780}
2781
2782/**
2783 * ata_qc_new - Request an available ATA command, for queueing
2784 * @ap: Port associated with device @dev
2785 * @dev: Device from whom we request an available command structure
2786 *
2787 * LOCKING:
0cba632b 2788 * None.
1da177e4
LT
2789 */
2790
2791static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
2792{
2793 struct ata_queued_cmd *qc = NULL;
2794 unsigned int i;
2795
2796 for (i = 0; i < ATA_MAX_QUEUE; i++)
2797 if (!test_and_set_bit(i, &ap->qactive)) {
2798 qc = ata_qc_from_tag(ap, i);
2799 break;
2800 }
2801
2802 if (qc)
2803 qc->tag = i;
2804
2805 return qc;
2806}
2807
2808/**
2809 * ata_qc_new_init - Request an available ATA command, and initialize it
2810 * @ap: Port associated with device @dev
2811 * @dev: Device from whom we request an available command structure
2812 *
2813 * LOCKING:
0cba632b 2814 * None.
1da177e4
LT
2815 */
2816
2817struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
2818 struct ata_device *dev)
2819{
2820 struct ata_queued_cmd *qc;
2821
2822 qc = ata_qc_new(ap);
2823 if (qc) {
2824 qc->sg = NULL;
2825 qc->flags = 0;
2826 qc->scsicmd = NULL;
2827 qc->ap = ap;
2828 qc->dev = dev;
2829 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
2830 qc->nsect = 0;
2831 qc->nbytes = qc->curbytes = 0;
2832
2833 ata_tf_init(ap, &qc->tf, dev->devno);
2834
2835 if (dev->flags & ATA_DFLAG_LBA48)
2836 qc->tf.flags |= ATA_TFLAG_LBA48;
2837 }
2838
2839 return qc;
2840}
2841
2842static int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
2843{
2844 return 0;
2845}
2846
2847static void __ata_qc_complete(struct ata_queued_cmd *qc)
2848{
2849 struct ata_port *ap = qc->ap;
2850 unsigned int tag, do_clear = 0;
2851
2852 qc->flags = 0;
2853 tag = qc->tag;
2854 if (likely(ata_tag_valid(tag))) {
2855 if (tag == ap->active_tag)
2856 ap->active_tag = ATA_TAG_POISON;
2857 qc->tag = ATA_TAG_POISON;
2858 do_clear = 1;
2859 }
2860
2861 if (qc->waiting) {
2862 struct completion *waiting = qc->waiting;
2863 qc->waiting = NULL;
2864 complete(waiting);
2865 }
2866
2867 if (likely(do_clear))
2868 clear_bit(tag, &ap->qactive);
2869}
2870
2871/**
2872 * ata_qc_free - free unused ata_queued_cmd
2873 * @qc: Command to complete
2874 *
2875 * Designed to free unused ata_queued_cmd object
2876 * in case something prevents using it.
2877 *
2878 * LOCKING:
0cba632b 2879 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2880 *
2881 */
2882void ata_qc_free(struct ata_queued_cmd *qc)
2883{
2884 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
2885 assert(qc->waiting == NULL); /* nothing should be waiting */
2886
2887 __ata_qc_complete(qc);
2888}
2889
2890/**
2891 * ata_qc_complete - Complete an active ATA command
2892 * @qc: Command to complete
0cba632b
JG
2893 * @drv_stat: ATA Status register contents
2894 *
2895 * Indicate to the mid and upper layers that an ATA
2896 * command has completed, with either an ok or not-ok status.
1da177e4
LT
2897 *
2898 * LOCKING:
0cba632b 2899 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2900 *
2901 */
2902
2903void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2904{
2905 int rc;
2906
2907 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
2908 assert(qc->flags & ATA_QCFLAG_ACTIVE);
2909
2910 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
2911 ata_sg_clean(qc);
2912
2913 /* call completion callback */
2914 rc = qc->complete_fn(qc, drv_stat);
21b1ed74 2915 qc->flags &= ~ATA_QCFLAG_ACTIVE;
1da177e4
LT
2916
2917 /* if callback indicates not to complete command (non-zero),
2918 * return immediately
2919 */
2920 if (rc != 0)
2921 return;
2922
2923 __ata_qc_complete(qc);
2924
2925 VPRINTK("EXIT\n");
2926}
2927
2928static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
2929{
2930 struct ata_port *ap = qc->ap;
2931
2932 switch (qc->tf.protocol) {
2933 case ATA_PROT_DMA:
2934 case ATA_PROT_ATAPI_DMA:
2935 return 1;
2936
2937 case ATA_PROT_ATAPI:
2938 case ATA_PROT_PIO:
2939 case ATA_PROT_PIO_MULT:
2940 if (ap->flags & ATA_FLAG_PIO_DMA)
2941 return 1;
2942
2943 /* fall through */
2944
2945 default:
2946 return 0;
2947 }
2948
2949 /* never reached */
2950}
2951
2952/**
2953 * ata_qc_issue - issue taskfile to device
2954 * @qc: command to issue to device
2955 *
2956 * Prepare an ATA command to submission to device.
2957 * This includes mapping the data into a DMA-able
2958 * area, filling in the S/G table, and finally
2959 * writing the taskfile to hardware, starting the command.
2960 *
2961 * LOCKING:
2962 * spin_lock_irqsave(host_set lock)
2963 *
2964 * RETURNS:
2965 * Zero on success, negative on error.
2966 */
2967
2968int ata_qc_issue(struct ata_queued_cmd *qc)
2969{
2970 struct ata_port *ap = qc->ap;
2971
2972 if (ata_should_dma_map(qc)) {
2973 if (qc->flags & ATA_QCFLAG_SG) {
2974 if (ata_sg_setup(qc))
2975 goto err_out;
2976 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
2977 if (ata_sg_setup_one(qc))
2978 goto err_out;
2979 }
2980 } else {
2981 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2982 }
2983
2984 ap->ops->qc_prep(qc);
2985
2986 qc->ap->active_tag = qc->tag;
2987 qc->flags |= ATA_QCFLAG_ACTIVE;
2988
2989 return ap->ops->qc_issue(qc);
2990
2991err_out:
2992 return -1;
2993}
2994
2995/**
2996 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
2997 * @qc: command to issue to device
2998 *
2999 * Using various libata functions and hooks, this function
3000 * starts an ATA command. ATA commands are grouped into
3001 * classes called "protocols", and issuing each type of protocol
3002 * is slightly different.
3003 *
3004 * LOCKING:
3005 * spin_lock_irqsave(host_set lock)
3006 *
3007 * RETURNS:
3008 * Zero on success, negative on error.
3009 */
3010
3011int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3012{
3013 struct ata_port *ap = qc->ap;
3014
3015 ata_dev_select(ap, qc->dev->devno, 1, 0);
3016
3017 switch (qc->tf.protocol) {
3018 case ATA_PROT_NODATA:
3019 ata_tf_to_host_nolock(ap, &qc->tf);
3020 break;
3021
3022 case ATA_PROT_DMA:
3023 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3024 ap->ops->bmdma_setup(qc); /* set up bmdma */
3025 ap->ops->bmdma_start(qc); /* initiate bmdma */
3026 break;
3027
3028 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3029 ata_qc_set_polling(qc);
3030 ata_tf_to_host_nolock(ap, &qc->tf);
3031 ap->pio_task_state = PIO_ST;
3032 queue_work(ata_wq, &ap->pio_task);
3033 break;
3034
3035 case ATA_PROT_ATAPI:
3036 ata_qc_set_polling(qc);
3037 ata_tf_to_host_nolock(ap, &qc->tf);
3038 queue_work(ata_wq, &ap->packet_task);
3039 break;
3040
3041 case ATA_PROT_ATAPI_NODATA:
3042 ata_tf_to_host_nolock(ap, &qc->tf);
3043 queue_work(ata_wq, &ap->packet_task);
3044 break;
3045
3046 case ATA_PROT_ATAPI_DMA:
3047 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3048 ap->ops->bmdma_setup(qc); /* set up bmdma */
3049 queue_work(ata_wq, &ap->packet_task);
3050 break;
3051
3052 default:
3053 WARN_ON(1);
3054 return -1;
3055 }
3056
3057 return 0;
3058}
3059
3060/**
3061 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3062 * @qc: Info associated with this ATA transaction.
3063 *
3064 * LOCKING:
3065 * spin_lock_irqsave(host_set lock)
3066 */
3067
3068static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3069{
3070 struct ata_port *ap = qc->ap;
3071 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3072 u8 dmactl;
3073 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3074
3075 /* load PRD table addr. */
3076 mb(); /* make sure PRD table writes are visible to controller */
3077 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3078
3079 /* specify data direction, triple-check start bit is clear */
3080 dmactl = readb(mmio + ATA_DMA_CMD);
3081 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3082 if (!rw)
3083 dmactl |= ATA_DMA_WR;
3084 writeb(dmactl, mmio + ATA_DMA_CMD);
3085
3086 /* issue r/w command */
3087 ap->ops->exec_command(ap, &qc->tf);
3088}
3089
3090/**
3091 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3092 * @qc: Info associated with this ATA transaction.
3093 *
3094 * LOCKING:
3095 * spin_lock_irqsave(host_set lock)
3096 */
3097
3098static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3099{
3100 struct ata_port *ap = qc->ap;
3101 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3102 u8 dmactl;
3103
3104 /* start host DMA transaction */
3105 dmactl = readb(mmio + ATA_DMA_CMD);
3106 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3107
3108 /* Strictly, one may wish to issue a readb() here, to
3109 * flush the mmio write. However, control also passes
3110 * to the hardware at this point, and it will interrupt
3111 * us when we are to resume control. So, in effect,
3112 * we don't care when the mmio write flushes.
3113 * Further, a read of the DMA status register _immediately_
3114 * following the write may not be what certain flaky hardware
3115 * is expected, so I think it is best to not add a readb()
3116 * without first all the MMIO ATA cards/mobos.
3117 * Or maybe I'm just being paranoid.
3118 */
3119}
3120
3121/**
3122 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3123 * @qc: Info associated with this ATA transaction.
3124 *
3125 * LOCKING:
3126 * spin_lock_irqsave(host_set lock)
3127 */
3128
3129static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3130{
3131 struct ata_port *ap = qc->ap;
3132 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3133 u8 dmactl;
3134
3135 /* load PRD table addr. */
3136 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3137
3138 /* specify data direction, triple-check start bit is clear */
3139 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3140 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3141 if (!rw)
3142 dmactl |= ATA_DMA_WR;
3143 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3144
3145 /* issue r/w command */
3146 ap->ops->exec_command(ap, &qc->tf);
3147}
3148
3149/**
3150 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3151 * @qc: Info associated with this ATA transaction.
3152 *
3153 * LOCKING:
3154 * spin_lock_irqsave(host_set lock)
3155 */
3156
3157static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3158{
3159 struct ata_port *ap = qc->ap;
3160 u8 dmactl;
3161
3162 /* start host DMA transaction */
3163 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3164 outb(dmactl | ATA_DMA_START,
3165 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3166}
3167
3168void ata_bmdma_start(struct ata_queued_cmd *qc)
3169{
3170 if (qc->ap->flags & ATA_FLAG_MMIO)
3171 ata_bmdma_start_mmio(qc);
3172 else
3173 ata_bmdma_start_pio(qc);
3174}
3175
3176void ata_bmdma_setup(struct ata_queued_cmd *qc)
3177{
3178 if (qc->ap->flags & ATA_FLAG_MMIO)
3179 ata_bmdma_setup_mmio(qc);
3180 else
3181 ata_bmdma_setup_pio(qc);
3182}
3183
3184void ata_bmdma_irq_clear(struct ata_port *ap)
3185{
3186 if (ap->flags & ATA_FLAG_MMIO) {
3187 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3188 writeb(readb(mmio), mmio);
3189 } else {
3190 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3191 outb(inb(addr), addr);
3192 }
3193
3194}
3195
3196u8 ata_bmdma_status(struct ata_port *ap)
3197{
3198 u8 host_stat;
3199 if (ap->flags & ATA_FLAG_MMIO) {
3200 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3201 host_stat = readb(mmio + ATA_DMA_STATUS);
3202 } else
3203 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3204 return host_stat;
3205}
3206
3207void ata_bmdma_stop(struct ata_port *ap)
3208{
3209 if (ap->flags & ATA_FLAG_MMIO) {
3210 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3211
3212 /* clear start/stop bit */
3213 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3214 mmio + ATA_DMA_CMD);
3215 } else {
3216 /* clear start/stop bit */
3217 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3218 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3219 }
3220
3221 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3222 ata_altstatus(ap); /* dummy read */
3223}
3224
3225/**
3226 * ata_host_intr - Handle host interrupt for given (port, task)
3227 * @ap: Port on which interrupt arrived (possibly...)
3228 * @qc: Taskfile currently active in engine
3229 *
3230 * Handle host interrupt for given queued command. Currently,
3231 * only DMA interrupts are handled. All other commands are
3232 * handled via polling with interrupts disabled (nIEN bit).
3233 *
3234 * LOCKING:
3235 * spin_lock_irqsave(host_set lock)
3236 *
3237 * RETURNS:
3238 * One if interrupt was handled, zero if not (shared irq).
3239 */
3240
3241inline unsigned int ata_host_intr (struct ata_port *ap,
3242 struct ata_queued_cmd *qc)
3243{
3244 u8 status, host_stat;
3245
3246 switch (qc->tf.protocol) {
3247
3248 case ATA_PROT_DMA:
3249 case ATA_PROT_ATAPI_DMA:
3250 case ATA_PROT_ATAPI:
3251 /* check status of DMA engine */
3252 host_stat = ap->ops->bmdma_status(ap);
3253 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3254
3255 /* if it's not our irq... */
3256 if (!(host_stat & ATA_DMA_INTR))
3257 goto idle_irq;
3258
3259 /* before we do anything else, clear DMA-Start bit */
3260 ap->ops->bmdma_stop(ap);
3261
3262 /* fall through */
3263
3264 case ATA_PROT_ATAPI_NODATA:
3265 case ATA_PROT_NODATA:
3266 /* check altstatus */
3267 status = ata_altstatus(ap);
3268 if (status & ATA_BUSY)
3269 goto idle_irq;
3270
3271 /* check main status, clearing INTRQ */
3272 status = ata_chk_status(ap);
3273 if (unlikely(status & ATA_BUSY))
3274 goto idle_irq;
3275 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3276 ap->id, qc->tf.protocol, status);
3277
3278 /* ack bmdma irq events */
3279 ap->ops->irq_clear(ap);
3280
3281 /* complete taskfile transaction */
3282 ata_qc_complete(qc, status);
3283 break;
3284
3285 default:
3286 goto idle_irq;
3287 }
3288
3289 return 1; /* irq handled */
3290
3291idle_irq:
3292 ap->stats.idle_irq++;
3293
3294#ifdef ATA_IRQ_TRAP
3295 if ((ap->stats.idle_irq % 1000) == 0) {
3296 handled = 1;
3297 ata_irq_ack(ap, 0); /* debug trap */
3298 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3299 }
3300#endif
3301 return 0; /* irq not handled */
3302}
3303
3304/**
3305 * ata_interrupt - Default ATA host interrupt handler
0cba632b
JG
3306 * @irq: irq line (unused)
3307 * @dev_instance: pointer to our ata_host_set information structure
1da177e4
LT
3308 * @regs: unused
3309 *
0cba632b
JG
3310 * Default interrupt handler for PCI IDE devices. Calls
3311 * ata_host_intr() for each port that is not disabled.
3312 *
1da177e4 3313 * LOCKING:
0cba632b 3314 * Obtains host_set lock during operation.
1da177e4
LT
3315 *
3316 * RETURNS:
0cba632b 3317 * IRQ_NONE or IRQ_HANDLED.
1da177e4
LT
3318 *
3319 */
3320
3321irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3322{
3323 struct ata_host_set *host_set = dev_instance;
3324 unsigned int i;
3325 unsigned int handled = 0;
3326 unsigned long flags;
3327
3328 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3329 spin_lock_irqsave(&host_set->lock, flags);
3330
3331 for (i = 0; i < host_set->n_ports; i++) {
3332 struct ata_port *ap;
3333
3334 ap = host_set->ports[i];
3335 if (ap && (!(ap->flags & ATA_FLAG_PORT_DISABLED))) {
3336 struct ata_queued_cmd *qc;
3337
3338 qc = ata_qc_from_tag(ap, ap->active_tag);
21b1ed74
AL
3339 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3340 (qc->flags & ATA_QCFLAG_ACTIVE))
1da177e4
LT
3341 handled |= ata_host_intr(ap, qc);
3342 }
3343 }
3344
3345 spin_unlock_irqrestore(&host_set->lock, flags);
3346
3347 return IRQ_RETVAL(handled);
3348}
3349
3350/**
3351 * atapi_packet_task - Write CDB bytes to hardware
3352 * @_data: Port to which ATAPI device is attached.
3353 *
3354 * When device has indicated its readiness to accept
3355 * a CDB, this function is called. Send the CDB.
3356 * If DMA is to be performed, exit immediately.
3357 * Otherwise, we are in polling mode, so poll
3358 * status under operation succeeds or fails.
3359 *
3360 * LOCKING:
3361 * Kernel thread context (may sleep)
3362 */
3363
3364static void atapi_packet_task(void *_data)
3365{
3366 struct ata_port *ap = _data;
3367 struct ata_queued_cmd *qc;
3368 u8 status;
3369
3370 qc = ata_qc_from_tag(ap, ap->active_tag);
3371 assert(qc != NULL);
3372 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3373
3374 /* sleep-wait for BSY to clear */
3375 DPRINTK("busy wait\n");
3376 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
3377 goto err_out;
3378
3379 /* make sure DRQ is set */
3380 status = ata_chk_status(ap);
3381 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
3382 goto err_out;
3383
3384 /* send SCSI cdb */
3385 DPRINTK("send cdb\n");
3386 assert(ap->cdb_len >= 12);
3387 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
3388
3389 /* if we are DMA'ing, irq handler takes over from here */
3390 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3391 ap->ops->bmdma_start(qc); /* initiate bmdma */
3392
3393 /* non-data commands are also handled via irq */
3394 else if (qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3395 /* do nothing */
3396 }
3397
3398 /* PIO commands are handled by polling */
3399 else {
3400 ap->pio_task_state = PIO_ST;
3401 queue_work(ata_wq, &ap->pio_task);
3402 }
3403
3404 return;
3405
3406err_out:
3407 ata_qc_complete(qc, ATA_ERR);
3408}
3409
3410int ata_port_start (struct ata_port *ap)
3411{
3412 struct device *dev = ap->host_set->dev;
3413
3414 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
3415 if (!ap->prd)
3416 return -ENOMEM;
3417
3418 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
3419
3420 return 0;
3421}
3422
3423void ata_port_stop (struct ata_port *ap)
3424{
3425 struct device *dev = ap->host_set->dev;
3426
3427 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
3428}
3429
3430/**
3431 * ata_host_remove - Unregister SCSI host structure with upper layers
3432 * @ap: Port to unregister
3433 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3434 *
3435 * LOCKING:
3436 */
3437
3438static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
3439{
3440 struct Scsi_Host *sh = ap->host;
3441
3442 DPRINTK("ENTER\n");
3443
3444 if (do_unregister)
3445 scsi_remove_host(sh);
3446
3447 ap->ops->port_stop(ap);
3448}
3449
3450/**
3451 * ata_host_init - Initialize an ata_port structure
3452 * @ap: Structure to initialize
3453 * @host: associated SCSI mid-layer structure
3454 * @host_set: Collection of hosts to which @ap belongs
3455 * @ent: Probe information provided by low-level driver
3456 * @port_no: Port number associated with this ata_port
3457 *
0cba632b
JG
3458 * Initialize a new ata_port structure, and its associated
3459 * scsi_host.
3460 *
1da177e4 3461 * LOCKING:
0cba632b 3462 * Inherited from caller.
1da177e4
LT
3463 *
3464 */
3465
3466static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
3467 struct ata_host_set *host_set,
3468 struct ata_probe_ent *ent, unsigned int port_no)
3469{
3470 unsigned int i;
3471
3472 host->max_id = 16;
3473 host->max_lun = 1;
3474 host->max_channel = 1;
3475 host->unique_id = ata_unique_id++;
3476 host->max_cmd_len = 12;
3477 scsi_set_device(host, ent->dev);
3478 scsi_assign_lock(host, &host_set->lock);
3479
3480 ap->flags = ATA_FLAG_PORT_DISABLED;
3481 ap->id = host->unique_id;
3482 ap->host = host;
3483 ap->ctl = ATA_DEVCTL_OBS;
3484 ap->host_set = host_set;
3485 ap->port_no = port_no;
3486 ap->hard_port_no =
3487 ent->legacy_mode ? ent->hard_port_no : port_no;
3488 ap->pio_mask = ent->pio_mask;
3489 ap->mwdma_mask = ent->mwdma_mask;
3490 ap->udma_mask = ent->udma_mask;
3491 ap->flags |= ent->host_flags;
3492 ap->ops = ent->port_ops;
3493 ap->cbl = ATA_CBL_NONE;
3494 ap->active_tag = ATA_TAG_POISON;
3495 ap->last_ctl = 0xFF;
3496
3497 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
3498 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
3499
3500 for (i = 0; i < ATA_MAX_DEVICES; i++)
3501 ap->device[i].devno = i;
3502
3503#ifdef ATA_IRQ_TRAP
3504 ap->stats.unhandled_irq = 1;
3505 ap->stats.idle_irq = 1;
3506#endif
3507
3508 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
3509}
3510
3511/**
3512 * ata_host_add - Attach low-level ATA driver to system
3513 * @ent: Information provided by low-level driver
3514 * @host_set: Collections of ports to which we add
3515 * @port_no: Port number associated with this host
3516 *
0cba632b
JG
3517 * Attach low-level ATA driver to system.
3518 *
1da177e4 3519 * LOCKING:
0cba632b 3520 * PCI/etc. bus probe sem.
1da177e4
LT
3521 *
3522 * RETURNS:
0cba632b 3523 * New ata_port on success, for NULL on error.
1da177e4
LT
3524 *
3525 */
3526
3527static struct ata_port * ata_host_add(struct ata_probe_ent *ent,
3528 struct ata_host_set *host_set,
3529 unsigned int port_no)
3530{
3531 struct Scsi_Host *host;
3532 struct ata_port *ap;
3533 int rc;
3534
3535 DPRINTK("ENTER\n");
3536 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
3537 if (!host)
3538 return NULL;
3539
3540 ap = (struct ata_port *) &host->hostdata[0];
3541
3542 ata_host_init(ap, host, host_set, ent, port_no);
3543
3544 rc = ap->ops->port_start(ap);
3545 if (rc)
3546 goto err_out;
3547
3548 return ap;
3549
3550err_out:
3551 scsi_host_put(host);
3552 return NULL;
3553}
3554
3555/**
0cba632b
JG
3556 * ata_device_add - Register hardware device with ATA and SCSI layers
3557 * @ent: Probe information describing hardware device to be registered
3558 *
3559 * This function processes the information provided in the probe
3560 * information struct @ent, allocates the necessary ATA and SCSI
3561 * host information structures, initializes them, and registers
3562 * everything with requisite kernel subsystems.
3563 *
3564 * This function requests irqs, probes the ATA bus, and probes
3565 * the SCSI bus.
1da177e4
LT
3566 *
3567 * LOCKING:
0cba632b 3568 * PCI/etc. bus probe sem.
1da177e4
LT
3569 *
3570 * RETURNS:
0cba632b 3571 * Number of ports registered. Zero on error (no ports registered).
1da177e4
LT
3572 *
3573 */
3574
3575int ata_device_add(struct ata_probe_ent *ent)
3576{
3577 unsigned int count = 0, i;
3578 struct device *dev = ent->dev;
3579 struct ata_host_set *host_set;
3580
3581 DPRINTK("ENTER\n");
3582 /* alloc a container for our list of ATA ports (buses) */
3583 host_set = kmalloc(sizeof(struct ata_host_set) +
3584 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
3585 if (!host_set)
3586 return 0;
3587 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
3588 spin_lock_init(&host_set->lock);
3589
3590 host_set->dev = dev;
3591 host_set->n_ports = ent->n_ports;
3592 host_set->irq = ent->irq;
3593 host_set->mmio_base = ent->mmio_base;
3594 host_set->private_data = ent->private_data;
3595 host_set->ops = ent->port_ops;
3596
3597 /* register each port bound to this device */
3598 for (i = 0; i < ent->n_ports; i++) {
3599 struct ata_port *ap;
3600 unsigned long xfer_mode_mask;
3601
3602 ap = ata_host_add(ent, host_set, i);
3603 if (!ap)
3604 goto err_out;
3605
3606 host_set->ports[i] = ap;
3607 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
3608 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
3609 (ap->pio_mask << ATA_SHIFT_PIO);
3610
3611 /* print per-port info to dmesg */
3612 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
3613 "bmdma 0x%lX irq %lu\n",
3614 ap->id,
3615 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
3616 ata_mode_string(xfer_mode_mask),
3617 ap->ioaddr.cmd_addr,
3618 ap->ioaddr.ctl_addr,
3619 ap->ioaddr.bmdma_addr,
3620 ent->irq);
3621
3622 ata_chk_status(ap);
3623 host_set->ops->irq_clear(ap);
3624 count++;
3625 }
3626
3627 if (!count) {
3628 kfree(host_set);
3629 return 0;
3630 }
3631
3632 /* obtain irq, that is shared between channels */
3633 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
3634 DRV_NAME, host_set))
3635 goto err_out;
3636
3637 /* perform each probe synchronously */
3638 DPRINTK("probe begin\n");
3639 for (i = 0; i < count; i++) {
3640 struct ata_port *ap;
3641 int rc;
3642
3643 ap = host_set->ports[i];
3644
3645 DPRINTK("ata%u: probe begin\n", ap->id);
3646 rc = ata_bus_probe(ap);
3647 DPRINTK("ata%u: probe end\n", ap->id);
3648
3649 if (rc) {
3650 /* FIXME: do something useful here?
3651 * Current libata behavior will
3652 * tear down everything when
3653 * the module is removed
3654 * or the h/w is unplugged.
3655 */
3656 }
3657
3658 rc = scsi_add_host(ap->host, dev);
3659 if (rc) {
3660 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
3661 ap->id);
3662 /* FIXME: do something useful here */
3663 /* FIXME: handle unconditional calls to
3664 * scsi_scan_host and ata_host_remove, below,
3665 * at the very least
3666 */
3667 }
3668 }
3669
3670 /* probes are done, now scan each port's disk(s) */
3671 DPRINTK("probe begin\n");
3672 for (i = 0; i < count; i++) {
3673 struct ata_port *ap = host_set->ports[i];
3674
3675 scsi_scan_host(ap->host);
3676 }
3677
3678 dev_set_drvdata(dev, host_set);
3679
3680 VPRINTK("EXIT, returning %u\n", ent->n_ports);
3681 return ent->n_ports; /* success */
3682
3683err_out:
3684 for (i = 0; i < count; i++) {
3685 ata_host_remove(host_set->ports[i], 1);
3686 scsi_host_put(host_set->ports[i]->host);
3687 }
3688 kfree(host_set);
3689 VPRINTK("EXIT, returning 0\n");
3690 return 0;
3691}
3692
3693/**
3694 * ata_scsi_release - SCSI layer callback hook for host unload
3695 * @host: libata host to be unloaded
3696 *
3697 * Performs all duties necessary to shut down a libata port...
3698 * Kill port kthread, disable port, and release resources.
3699 *
3700 * LOCKING:
3701 * Inherited from SCSI layer.
3702 *
3703 * RETURNS:
3704 * One.
3705 */
3706
3707int ata_scsi_release(struct Scsi_Host *host)
3708{
3709 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
3710
3711 DPRINTK("ENTER\n");
3712
3713 ap->ops->port_disable(ap);
3714 ata_host_remove(ap, 0);
3715
3716 DPRINTK("EXIT\n");
3717 return 1;
3718}
3719
3720/**
3721 * ata_std_ports - initialize ioaddr with standard port offsets.
3722 * @ioaddr: IO address structure to be initialized
3723 */
3724void ata_std_ports(struct ata_ioports *ioaddr)
3725{
3726 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
3727 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
3728 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
3729 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
3730 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
3731 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
3732 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
3733 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
3734 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
3735 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
3736}
3737
3738static struct ata_probe_ent *
3739ata_probe_ent_alloc(struct device *dev, struct ata_port_info *port)
3740{
3741 struct ata_probe_ent *probe_ent;
3742
3743 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
3744 if (!probe_ent) {
3745 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
3746 kobject_name(&(dev->kobj)));
3747 return NULL;
3748 }
3749
3750 memset(probe_ent, 0, sizeof(*probe_ent));
3751
3752 INIT_LIST_HEAD(&probe_ent->node);
3753 probe_ent->dev = dev;
3754
3755 probe_ent->sht = port->sht;
3756 probe_ent->host_flags = port->host_flags;
3757 probe_ent->pio_mask = port->pio_mask;
3758 probe_ent->mwdma_mask = port->mwdma_mask;
3759 probe_ent->udma_mask = port->udma_mask;
3760 probe_ent->port_ops = port->port_ops;
3761
3762 return probe_ent;
3763}
3764
3765#ifdef CONFIG_PCI
3766struct ata_probe_ent *
3767ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port)
3768{
3769 struct ata_probe_ent *probe_ent =
3770 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
3771 if (!probe_ent)
3772 return NULL;
3773
3774 probe_ent->n_ports = 2;
3775 probe_ent->irq = pdev->irq;
3776 probe_ent->irq_flags = SA_SHIRQ;
3777
3778 probe_ent->port[0].cmd_addr = pci_resource_start(pdev, 0);
3779 probe_ent->port[0].altstatus_addr =
3780 probe_ent->port[0].ctl_addr =
3781 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
3782 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
3783
3784 probe_ent->port[1].cmd_addr = pci_resource_start(pdev, 2);
3785 probe_ent->port[1].altstatus_addr =
3786 probe_ent->port[1].ctl_addr =
3787 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
3788 probe_ent->port[1].bmdma_addr = pci_resource_start(pdev, 4) + 8;
3789
3790 ata_std_ports(&probe_ent->port[0]);
3791 ata_std_ports(&probe_ent->port[1]);
3792
3793 return probe_ent;
3794}
3795
3796static struct ata_probe_ent *
3797ata_pci_init_legacy_mode(struct pci_dev *pdev, struct ata_port_info **port,
3798 struct ata_probe_ent **ppe2)
3799{
3800 struct ata_probe_ent *probe_ent, *probe_ent2;
3801
3802 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
3803 if (!probe_ent)
3804 return NULL;
3805 probe_ent2 = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[1]);
3806 if (!probe_ent2) {
3807 kfree(probe_ent);
3808 return NULL;
3809 }
3810
3811 probe_ent->n_ports = 1;
3812 probe_ent->irq = 14;
3813
3814 probe_ent->hard_port_no = 0;
3815 probe_ent->legacy_mode = 1;
3816
3817 probe_ent2->n_ports = 1;
3818 probe_ent2->irq = 15;
3819
3820 probe_ent2->hard_port_no = 1;
3821 probe_ent2->legacy_mode = 1;
3822
3823 probe_ent->port[0].cmd_addr = 0x1f0;
3824 probe_ent->port[0].altstatus_addr =
3825 probe_ent->port[0].ctl_addr = 0x3f6;
3826 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4);
3827
3828 probe_ent2->port[0].cmd_addr = 0x170;
3829 probe_ent2->port[0].altstatus_addr =
3830 probe_ent2->port[0].ctl_addr = 0x376;
3831 probe_ent2->port[0].bmdma_addr = pci_resource_start(pdev, 4)+8;
3832
3833 ata_std_ports(&probe_ent->port[0]);
3834 ata_std_ports(&probe_ent2->port[0]);
3835
3836 *ppe2 = probe_ent2;
3837 return probe_ent;
3838}
3839
3840/**
3841 * ata_pci_init_one - Initialize/register PCI IDE host controller
3842 * @pdev: Controller to be initialized
3843 * @port_info: Information from low-level host driver
3844 * @n_ports: Number of ports attached to host controller
3845 *
3846 * LOCKING:
3847 * Inherited from PCI layer (may sleep).
3848 *
3849 * RETURNS:
0cba632b 3850 * Zero on success, negative on errno-based value on error.
1da177e4
LT
3851 *
3852 */
3853
3854int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
3855 unsigned int n_ports)
3856{
3857 struct ata_probe_ent *probe_ent, *probe_ent2 = NULL;
3858 struct ata_port_info *port[2];
3859 u8 tmp8, mask;
3860 unsigned int legacy_mode = 0;
3861 int disable_dev_on_err = 1;
3862 int rc;
3863
3864 DPRINTK("ENTER\n");
3865
3866 port[0] = port_info[0];
3867 if (n_ports > 1)
3868 port[1] = port_info[1];
3869 else
3870 port[1] = port[0];
3871
3872 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
3873 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
3874 /* TODO: support transitioning to native mode? */
3875 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
3876 mask = (1 << 2) | (1 << 0);
3877 if ((tmp8 & mask) != mask)
3878 legacy_mode = (1 << 3);
3879 }
3880
3881 /* FIXME... */
3882 if ((!legacy_mode) && (n_ports > 1)) {
3883 printk(KERN_ERR "ata: BUG: native mode, n_ports > 1\n");
3884 return -EINVAL;
3885 }
3886
3887 rc = pci_enable_device(pdev);
3888 if (rc)
3889 return rc;
3890
3891 rc = pci_request_regions(pdev, DRV_NAME);
3892 if (rc) {
3893 disable_dev_on_err = 0;
3894 goto err_out;
3895 }
3896
3897 if (legacy_mode) {
3898 if (!request_region(0x1f0, 8, "libata")) {
3899 struct resource *conflict, res;
3900 res.start = 0x1f0;
3901 res.end = 0x1f0 + 8 - 1;
3902 conflict = ____request_resource(&ioport_resource, &res);
3903 if (!strcmp(conflict->name, "libata"))
3904 legacy_mode |= (1 << 0);
3905 else {
3906 disable_dev_on_err = 0;
3907 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
3908 }
3909 } else
3910 legacy_mode |= (1 << 0);
3911
3912 if (!request_region(0x170, 8, "libata")) {
3913 struct resource *conflict, res;
3914 res.start = 0x170;
3915 res.end = 0x170 + 8 - 1;
3916 conflict = ____request_resource(&ioport_resource, &res);
3917 if (!strcmp(conflict->name, "libata"))
3918 legacy_mode |= (1 << 1);
3919 else {
3920 disable_dev_on_err = 0;
3921 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
3922 }
3923 } else
3924 legacy_mode |= (1 << 1);
3925 }
3926
3927 /* we have legacy mode, but all ports are unavailable */
3928 if (legacy_mode == (1 << 3)) {
3929 rc = -EBUSY;
3930 goto err_out_regions;
3931 }
3932
3933 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
3934 if (rc)
3935 goto err_out_regions;
3936 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
3937 if (rc)
3938 goto err_out_regions;
3939
3940 if (legacy_mode) {
3941 probe_ent = ata_pci_init_legacy_mode(pdev, port, &probe_ent2);
3942 } else
3943 probe_ent = ata_pci_init_native_mode(pdev, port);
3944 if (!probe_ent) {
3945 rc = -ENOMEM;
3946 goto err_out_regions;
3947 }
3948
3949 pci_set_master(pdev);
3950
3951 /* FIXME: check ata_device_add return */
3952 if (legacy_mode) {
3953 if (legacy_mode & (1 << 0))
3954 ata_device_add(probe_ent);
3955 if (legacy_mode & (1 << 1))
3956 ata_device_add(probe_ent2);
3957 } else
3958 ata_device_add(probe_ent);
3959
3960 kfree(probe_ent);
3961 kfree(probe_ent2);
3962
3963 return 0;
3964
3965err_out_regions:
3966 if (legacy_mode & (1 << 0))
3967 release_region(0x1f0, 8);
3968 if (legacy_mode & (1 << 1))
3969 release_region(0x170, 8);
3970 pci_release_regions(pdev);
3971err_out:
3972 if (disable_dev_on_err)
3973 pci_disable_device(pdev);
3974 return rc;
3975}
3976
3977/**
3978 * ata_pci_remove_one - PCI layer callback for device removal
3979 * @pdev: PCI device that was removed
3980 *
3981 * PCI layer indicates to libata via this hook that
3982 * hot-unplug or module unload event has occured.
3983 * Handle this by unregistering all objects associated
3984 * with this PCI device. Free those objects. Then finally
3985 * release PCI resources and disable device.
3986 *
3987 * LOCKING:
3988 * Inherited from PCI layer (may sleep).
3989 */
3990
3991void ata_pci_remove_one (struct pci_dev *pdev)
3992{
3993 struct device *dev = pci_dev_to_dev(pdev);
3994 struct ata_host_set *host_set = dev_get_drvdata(dev);
3995 struct ata_port *ap;
3996 unsigned int i;
3997
3998 for (i = 0; i < host_set->n_ports; i++) {
3999 ap = host_set->ports[i];
4000
4001 scsi_remove_host(ap->host);
4002 }
4003
4004 free_irq(host_set->irq, host_set);
4005 if (host_set->ops->host_stop)
4006 host_set->ops->host_stop(host_set);
4007 if (host_set->mmio_base)
4008 iounmap(host_set->mmio_base);
4009
4010 for (i = 0; i < host_set->n_ports; i++) {
4011 ap = host_set->ports[i];
4012
4013 ata_scsi_release(ap->host);
4014
4015 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4016 struct ata_ioports *ioaddr = &ap->ioaddr;
4017
4018 if (ioaddr->cmd_addr == 0x1f0)
4019 release_region(0x1f0, 8);
4020 else if (ioaddr->cmd_addr == 0x170)
4021 release_region(0x170, 8);
4022 }
4023
4024 scsi_host_put(ap->host);
4025 }
4026
4027 kfree(host_set);
4028
4029 pci_release_regions(pdev);
4030 pci_disable_device(pdev);
4031 dev_set_drvdata(dev, NULL);
4032}
4033
4034/* move to PCI subsystem */
4035int pci_test_config_bits(struct pci_dev *pdev, struct pci_bits *bits)
4036{
4037 unsigned long tmp = 0;
4038
4039 switch (bits->width) {
4040 case 1: {
4041 u8 tmp8 = 0;
4042 pci_read_config_byte(pdev, bits->reg, &tmp8);
4043 tmp = tmp8;
4044 break;
4045 }
4046 case 2: {
4047 u16 tmp16 = 0;
4048 pci_read_config_word(pdev, bits->reg, &tmp16);
4049 tmp = tmp16;
4050 break;
4051 }
4052 case 4: {
4053 u32 tmp32 = 0;
4054 pci_read_config_dword(pdev, bits->reg, &tmp32);
4055 tmp = tmp32;
4056 break;
4057 }
4058
4059 default:
4060 return -EINVAL;
4061 }
4062
4063 tmp &= bits->mask;
4064
4065 return (tmp == bits->val) ? 1 : 0;
4066}
4067#endif /* CONFIG_PCI */
4068
4069
1da177e4
LT
4070static int __init ata_init(void)
4071{
4072 ata_wq = create_workqueue("ata");
4073 if (!ata_wq)
4074 return -ENOMEM;
4075
4076 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4077 return 0;
4078}
4079
4080static void __exit ata_exit(void)
4081{
4082 destroy_workqueue(ata_wq);
4083}
4084
4085module_init(ata_init);
4086module_exit(ata_exit);
4087
4088/*
4089 * libata is essentially a library of internal helper functions for
4090 * low-level ATA host controller drivers. As such, the API/ABI is
4091 * likely to change as new drivers are added and updated.
4092 * Do not depend on ABI/API stability.
4093 */
4094
4095EXPORT_SYMBOL_GPL(ata_std_bios_param);
4096EXPORT_SYMBOL_GPL(ata_std_ports);
4097EXPORT_SYMBOL_GPL(ata_device_add);
4098EXPORT_SYMBOL_GPL(ata_sg_init);
4099EXPORT_SYMBOL_GPL(ata_sg_init_one);
4100EXPORT_SYMBOL_GPL(ata_qc_complete);
4101EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4102EXPORT_SYMBOL_GPL(ata_eng_timeout);
4103EXPORT_SYMBOL_GPL(ata_tf_load);
4104EXPORT_SYMBOL_GPL(ata_tf_read);
4105EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4106EXPORT_SYMBOL_GPL(ata_std_dev_select);
4107EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4108EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4109EXPORT_SYMBOL_GPL(ata_check_status);
4110EXPORT_SYMBOL_GPL(ata_altstatus);
4111EXPORT_SYMBOL_GPL(ata_chk_err);
4112EXPORT_SYMBOL_GPL(ata_exec_command);
4113EXPORT_SYMBOL_GPL(ata_port_start);
4114EXPORT_SYMBOL_GPL(ata_port_stop);
4115EXPORT_SYMBOL_GPL(ata_interrupt);
4116EXPORT_SYMBOL_GPL(ata_qc_prep);
4117EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4118EXPORT_SYMBOL_GPL(ata_bmdma_start);
4119EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4120EXPORT_SYMBOL_GPL(ata_bmdma_status);
4121EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4122EXPORT_SYMBOL_GPL(ata_port_probe);
4123EXPORT_SYMBOL_GPL(sata_phy_reset);
4124EXPORT_SYMBOL_GPL(__sata_phy_reset);
4125EXPORT_SYMBOL_GPL(ata_bus_reset);
4126EXPORT_SYMBOL_GPL(ata_port_disable);
4127EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4128EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4129EXPORT_SYMBOL_GPL(ata_scsi_error);
4130EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4131EXPORT_SYMBOL_GPL(ata_scsi_release);
4132EXPORT_SYMBOL_GPL(ata_host_intr);
4133EXPORT_SYMBOL_GPL(ata_dev_classify);
4134EXPORT_SYMBOL_GPL(ata_dev_id_string);
4135EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4136
4137#ifdef CONFIG_PCI
4138EXPORT_SYMBOL_GPL(pci_test_config_bits);
4139EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4140EXPORT_SYMBOL_GPL(ata_pci_init_one);
4141EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4142#endif /* CONFIG_PCI */