Merge branch 'master'
[linux-2.6-block.git] / drivers / scsi / libata-core.c
CommitLineData
1da177e4 1/*
af36d7f0
JG
2 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
1da177e4
LT
33 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
67846b30 51#include <linux/jiffies.h>
378f058c 52#include <linux/scatterlist.h>
1da177e4 53#include <scsi/scsi.h>
1da177e4 54#include "scsi_priv.h"
193515d5 55#include <scsi/scsi_cmnd.h>
1da177e4
LT
56#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
59a10b17 64static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
8bf62ece 65static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
1da177e4
LT
66static void ata_set_mode(struct ata_port *ap);
67static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
057ace5e 68static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
1da177e4 69static int fgb(u32 bitmap);
057ace5e 70static int ata_choose_xfer_mode(const struct ata_port *ap,
1da177e4
LT
71 u8 *xfer_mode_out,
72 unsigned int *xfer_shift_out);
1da177e4
LT
73
74static unsigned int ata_unique_id = 1;
75static struct workqueue_struct *ata_wq;
76
1623c81e
JG
77int atapi_enabled = 0;
78module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
1da177e4
LT
81MODULE_AUTHOR("Jeff Garzik");
82MODULE_DESCRIPTION("Library module for ATA devices");
83MODULE_LICENSE("GPL");
84MODULE_VERSION(DRV_VERSION);
85
0baab86b 86
1da177e4
LT
87/**
88 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
89 * @tf: Taskfile to convert
90 * @fis: Buffer into which data will output
91 * @pmp: Port multiplier port
92 *
93 * Converts a standard ATA taskfile to a Serial ATA
94 * FIS structure (Register - Host to Device).
95 *
96 * LOCKING:
97 * Inherited from caller.
98 */
99
057ace5e 100void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
1da177e4
LT
101{
102 fis[0] = 0x27; /* Register - Host to Device FIS */
103 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
104 bit 7 indicates Command FIS */
105 fis[2] = tf->command;
106 fis[3] = tf->feature;
107
108 fis[4] = tf->lbal;
109 fis[5] = tf->lbam;
110 fis[6] = tf->lbah;
111 fis[7] = tf->device;
112
113 fis[8] = tf->hob_lbal;
114 fis[9] = tf->hob_lbam;
115 fis[10] = tf->hob_lbah;
116 fis[11] = tf->hob_feature;
117
118 fis[12] = tf->nsect;
119 fis[13] = tf->hob_nsect;
120 fis[14] = 0;
121 fis[15] = tf->ctl;
122
123 fis[16] = 0;
124 fis[17] = 0;
125 fis[18] = 0;
126 fis[19] = 0;
127}
128
129/**
130 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
131 * @fis: Buffer from which data will be input
132 * @tf: Taskfile to output
133 *
e12a1be6 134 * Converts a serial ATA FIS structure to a standard ATA taskfile.
1da177e4
LT
135 *
136 * LOCKING:
137 * Inherited from caller.
138 */
139
057ace5e 140void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
1da177e4
LT
141{
142 tf->command = fis[2]; /* status */
143 tf->feature = fis[3]; /* error */
144
145 tf->lbal = fis[4];
146 tf->lbam = fis[5];
147 tf->lbah = fis[6];
148 tf->device = fis[7];
149
150 tf->hob_lbal = fis[8];
151 tf->hob_lbam = fis[9];
152 tf->hob_lbah = fis[10];
153
154 tf->nsect = fis[12];
155 tf->hob_nsect = fis[13];
156}
157
8cbd6df1
AL
158static const u8 ata_rw_cmds[] = {
159 /* pio multi */
160 ATA_CMD_READ_MULTI,
161 ATA_CMD_WRITE_MULTI,
162 ATA_CMD_READ_MULTI_EXT,
163 ATA_CMD_WRITE_MULTI_EXT,
9a3dccc4
TH
164 0,
165 0,
166 0,
167 ATA_CMD_WRITE_MULTI_FUA_EXT,
8cbd6df1
AL
168 /* pio */
169 ATA_CMD_PIO_READ,
170 ATA_CMD_PIO_WRITE,
171 ATA_CMD_PIO_READ_EXT,
172 ATA_CMD_PIO_WRITE_EXT,
9a3dccc4
TH
173 0,
174 0,
175 0,
176 0,
8cbd6df1
AL
177 /* dma */
178 ATA_CMD_READ,
179 ATA_CMD_WRITE,
180 ATA_CMD_READ_EXT,
9a3dccc4
TH
181 ATA_CMD_WRITE_EXT,
182 0,
183 0,
184 0,
185 ATA_CMD_WRITE_FUA_EXT
8cbd6df1 186};
1da177e4
LT
187
188/**
8cbd6df1
AL
189 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
190 * @qc: command to examine and configure
1da177e4 191 *
8cbd6df1
AL
192 * Examine the device configuration and tf->flags to calculate
193 * the proper read/write commands and protocol to use.
1da177e4
LT
194 *
195 * LOCKING:
196 * caller.
197 */
9a3dccc4 198int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
1da177e4 199{
8cbd6df1
AL
200 struct ata_taskfile *tf = &qc->tf;
201 struct ata_device *dev = qc->dev;
9a3dccc4 202 u8 cmd;
1da177e4 203
9a3dccc4 204 int index, fua, lba48, write;
8cbd6df1 205
9a3dccc4 206 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
8cbd6df1
AL
207 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
208 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
1da177e4 209
8cbd6df1
AL
210 if (dev->flags & ATA_DFLAG_PIO) {
211 tf->protocol = ATA_PROT_PIO;
9a3dccc4 212 index = dev->multi_count ? 0 : 8;
8d238e01
AC
213 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
214 /* Unable to use DMA due to host limitation */
215 tf->protocol = ATA_PROT_PIO;
0565c26d 216 index = dev->multi_count ? 0 : 8;
8cbd6df1
AL
217 } else {
218 tf->protocol = ATA_PROT_DMA;
9a3dccc4 219 index = 16;
8cbd6df1 220 }
1da177e4 221
9a3dccc4
TH
222 cmd = ata_rw_cmds[index + fua + lba48 + write];
223 if (cmd) {
224 tf->command = cmd;
225 return 0;
226 }
227 return -1;
1da177e4
LT
228}
229
98ac62de 230static const char * const xfer_mode_str[] = {
1da177e4
LT
231 "UDMA/16",
232 "UDMA/25",
233 "UDMA/33",
234 "UDMA/44",
235 "UDMA/66",
236 "UDMA/100",
237 "UDMA/133",
238 "UDMA7",
239 "MWDMA0",
240 "MWDMA1",
241 "MWDMA2",
242 "PIO0",
243 "PIO1",
244 "PIO2",
245 "PIO3",
246 "PIO4",
247};
248
249/**
250 * ata_udma_string - convert UDMA bit offset to string
251 * @mask: mask of bits supported; only highest bit counts.
252 *
253 * Determine string which represents the highest speed
254 * (highest bit in @udma_mask).
255 *
256 * LOCKING:
257 * None.
258 *
259 * RETURNS:
260 * Constant C string representing highest speed listed in
261 * @udma_mask, or the constant C string "<n/a>".
262 */
263
264static const char *ata_mode_string(unsigned int mask)
265{
266 int i;
267
268 for (i = 7; i >= 0; i--)
269 if (mask & (1 << i))
270 goto out;
271 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
272 if (mask & (1 << i))
273 goto out;
274 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
275 if (mask & (1 << i))
276 goto out;
277
278 return "<n/a>";
279
280out:
281 return xfer_mode_str[i];
282}
283
284/**
285 * ata_pio_devchk - PATA device presence detection
286 * @ap: ATA channel to examine
287 * @device: Device to examine (starting at zero)
288 *
289 * This technique was originally described in
290 * Hale Landis's ATADRVR (www.ata-atapi.com), and
291 * later found its way into the ATA/ATAPI spec.
292 *
293 * Write a pattern to the ATA shadow registers,
294 * and if a device is present, it will respond by
295 * correctly storing and echoing back the
296 * ATA shadow register contents.
297 *
298 * LOCKING:
299 * caller.
300 */
301
302static unsigned int ata_pio_devchk(struct ata_port *ap,
303 unsigned int device)
304{
305 struct ata_ioports *ioaddr = &ap->ioaddr;
306 u8 nsect, lbal;
307
308 ap->ops->dev_select(ap, device);
309
310 outb(0x55, ioaddr->nsect_addr);
311 outb(0xaa, ioaddr->lbal_addr);
312
313 outb(0xaa, ioaddr->nsect_addr);
314 outb(0x55, ioaddr->lbal_addr);
315
316 outb(0x55, ioaddr->nsect_addr);
317 outb(0xaa, ioaddr->lbal_addr);
318
319 nsect = inb(ioaddr->nsect_addr);
320 lbal = inb(ioaddr->lbal_addr);
321
322 if ((nsect == 0x55) && (lbal == 0xaa))
323 return 1; /* we found a device */
324
325 return 0; /* nothing found */
326}
327
328/**
329 * ata_mmio_devchk - PATA device presence detection
330 * @ap: ATA channel to examine
331 * @device: Device to examine (starting at zero)
332 *
333 * This technique was originally described in
334 * Hale Landis's ATADRVR (www.ata-atapi.com), and
335 * later found its way into the ATA/ATAPI spec.
336 *
337 * Write a pattern to the ATA shadow registers,
338 * and if a device is present, it will respond by
339 * correctly storing and echoing back the
340 * ATA shadow register contents.
341 *
342 * LOCKING:
343 * caller.
344 */
345
346static unsigned int ata_mmio_devchk(struct ata_port *ap,
347 unsigned int device)
348{
349 struct ata_ioports *ioaddr = &ap->ioaddr;
350 u8 nsect, lbal;
351
352 ap->ops->dev_select(ap, device);
353
354 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
355 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
356
357 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
358 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
359
360 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
361 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
362
363 nsect = readb((void __iomem *) ioaddr->nsect_addr);
364 lbal = readb((void __iomem *) ioaddr->lbal_addr);
365
366 if ((nsect == 0x55) && (lbal == 0xaa))
367 return 1; /* we found a device */
368
369 return 0; /* nothing found */
370}
371
372/**
373 * ata_devchk - PATA device presence detection
374 * @ap: ATA channel to examine
375 * @device: Device to examine (starting at zero)
376 *
377 * Dispatch ATA device presence detection, depending
378 * on whether we are using PIO or MMIO to talk to the
379 * ATA shadow registers.
380 *
381 * LOCKING:
382 * caller.
383 */
384
385static unsigned int ata_devchk(struct ata_port *ap,
386 unsigned int device)
387{
388 if (ap->flags & ATA_FLAG_MMIO)
389 return ata_mmio_devchk(ap, device);
390 return ata_pio_devchk(ap, device);
391}
392
393/**
394 * ata_dev_classify - determine device type based on ATA-spec signature
395 * @tf: ATA taskfile register set for device to be identified
396 *
397 * Determine from taskfile register contents whether a device is
398 * ATA or ATAPI, as per "Signature and persistence" section
399 * of ATA/PI spec (volume 1, sect 5.14).
400 *
401 * LOCKING:
402 * None.
403 *
404 * RETURNS:
405 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
406 * the event of failure.
407 */
408
057ace5e 409unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1da177e4
LT
410{
411 /* Apple's open source Darwin code hints that some devices only
412 * put a proper signature into the LBA mid/high registers,
413 * So, we only check those. It's sufficient for uniqueness.
414 */
415
416 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
417 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
418 DPRINTK("found ATA device by sig\n");
419 return ATA_DEV_ATA;
420 }
421
422 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
423 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
424 DPRINTK("found ATAPI device by sig\n");
425 return ATA_DEV_ATAPI;
426 }
427
428 DPRINTK("unknown device\n");
429 return ATA_DEV_UNKNOWN;
430}
431
432/**
433 * ata_dev_try_classify - Parse returned ATA device signature
434 * @ap: ATA channel to examine
435 * @device: Device to examine (starting at zero)
b4dc7623 436 * @r_err: Value of error register on completion
1da177e4
LT
437 *
438 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
439 * an ATA/ATAPI-defined set of values is placed in the ATA
440 * shadow registers, indicating the results of device detection
441 * and diagnostics.
442 *
443 * Select the ATA device, and read the values from the ATA shadow
444 * registers. Then parse according to the Error register value,
445 * and the spec-defined values examined by ata_dev_classify().
446 *
447 * LOCKING:
448 * caller.
b4dc7623
TH
449 *
450 * RETURNS:
451 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1da177e4
LT
452 */
453
b4dc7623
TH
454static unsigned int
455ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
1da177e4 456{
1da177e4
LT
457 struct ata_taskfile tf;
458 unsigned int class;
459 u8 err;
460
461 ap->ops->dev_select(ap, device);
462
463 memset(&tf, 0, sizeof(tf));
464
1da177e4 465 ap->ops->tf_read(ap, &tf);
0169e284 466 err = tf.feature;
b4dc7623
TH
467 if (r_err)
468 *r_err = err;
1da177e4
LT
469
470 /* see if device passed diags */
471 if (err == 1)
472 /* do nothing */ ;
473 else if ((device == 0) && (err == 0x81))
474 /* do nothing */ ;
475 else
b4dc7623 476 return ATA_DEV_NONE;
1da177e4 477
b4dc7623 478 /* determine if device is ATA or ATAPI */
1da177e4 479 class = ata_dev_classify(&tf);
b4dc7623 480
1da177e4 481 if (class == ATA_DEV_UNKNOWN)
b4dc7623 482 return ATA_DEV_NONE;
1da177e4 483 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
b4dc7623
TH
484 return ATA_DEV_NONE;
485 return class;
1da177e4
LT
486}
487
488/**
489 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
490 * @id: IDENTIFY DEVICE results we will examine
491 * @s: string into which data is output
492 * @ofs: offset into identify device page
493 * @len: length of string to return. must be an even number.
494 *
495 * The strings in the IDENTIFY DEVICE page are broken up into
496 * 16-bit chunks. Run through the string, and output each
497 * 8-bit chunk linearly, regardless of platform.
498 *
499 * LOCKING:
500 * caller.
501 */
502
057ace5e 503void ata_dev_id_string(const u16 *id, unsigned char *s,
1da177e4
LT
504 unsigned int ofs, unsigned int len)
505{
506 unsigned int c;
507
508 while (len > 0) {
509 c = id[ofs] >> 8;
510 *s = c;
511 s++;
512
513 c = id[ofs] & 0xff;
514 *s = c;
515 s++;
516
517 ofs++;
518 len -= 2;
519 }
520}
521
0e949ff3
TH
522/**
523 * ata_dev_id_c_string - Convert IDENTIFY DEVICE page into C string
524 * @id: IDENTIFY DEVICE results we will examine
525 * @s: string into which data is output
526 * @ofs: offset into identify device page
527 * @len: length of string to return. must be an odd number.
528 *
529 * This function is identical to ata_dev_id_string except that it
530 * trims trailing spaces and terminates the resulting string with
531 * null. @len must be actual maximum length (even number) + 1.
532 *
533 * LOCKING:
534 * caller.
535 */
536void ata_dev_id_c_string(const u16 *id, unsigned char *s,
537 unsigned int ofs, unsigned int len)
538{
539 unsigned char *p;
540
541 WARN_ON(!(len & 1));
542
543 ata_dev_id_string(id, s, ofs, len - 1);
544
545 p = s + strnlen(s, len - 1);
546 while (p > s && p[-1] == ' ')
547 p--;
548 *p = '\0';
549}
0baab86b 550
2940740b
TH
551static u64 ata_id_n_sectors(const u16 *id)
552{
553 if (ata_id_has_lba(id)) {
554 if (ata_id_has_lba48(id))
555 return ata_id_u64(id, 100);
556 else
557 return ata_id_u32(id, 60);
558 } else {
559 if (ata_id_current_chs_valid(id))
560 return ata_id_u32(id, 57);
561 else
562 return id[1] * id[3] * id[6];
563 }
564}
565
0baab86b
EF
566/**
567 * ata_noop_dev_select - Select device 0/1 on ATA bus
568 * @ap: ATA channel to manipulate
569 * @device: ATA device (numbered from zero) to select
570 *
571 * This function performs no actual function.
572 *
573 * May be used as the dev_select() entry in ata_port_operations.
574 *
575 * LOCKING:
576 * caller.
577 */
1da177e4
LT
578void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
579{
580}
581
0baab86b 582
1da177e4
LT
583/**
584 * ata_std_dev_select - Select device 0/1 on ATA bus
585 * @ap: ATA channel to manipulate
586 * @device: ATA device (numbered from zero) to select
587 *
588 * Use the method defined in the ATA specification to
589 * make either device 0, or device 1, active on the
0baab86b
EF
590 * ATA channel. Works with both PIO and MMIO.
591 *
592 * May be used as the dev_select() entry in ata_port_operations.
1da177e4
LT
593 *
594 * LOCKING:
595 * caller.
596 */
597
598void ata_std_dev_select (struct ata_port *ap, unsigned int device)
599{
600 u8 tmp;
601
602 if (device == 0)
603 tmp = ATA_DEVICE_OBS;
604 else
605 tmp = ATA_DEVICE_OBS | ATA_DEV1;
606
607 if (ap->flags & ATA_FLAG_MMIO) {
608 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
609 } else {
610 outb(tmp, ap->ioaddr.device_addr);
611 }
612 ata_pause(ap); /* needed; also flushes, for mmio */
613}
614
615/**
616 * ata_dev_select - Select device 0/1 on ATA bus
617 * @ap: ATA channel to manipulate
618 * @device: ATA device (numbered from zero) to select
619 * @wait: non-zero to wait for Status register BSY bit to clear
620 * @can_sleep: non-zero if context allows sleeping
621 *
622 * Use the method defined in the ATA specification to
623 * make either device 0, or device 1, active on the
624 * ATA channel.
625 *
626 * This is a high-level version of ata_std_dev_select(),
627 * which additionally provides the services of inserting
628 * the proper pauses and status polling, where needed.
629 *
630 * LOCKING:
631 * caller.
632 */
633
634void ata_dev_select(struct ata_port *ap, unsigned int device,
635 unsigned int wait, unsigned int can_sleep)
636{
637 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
638 ap->id, device, wait);
639
640 if (wait)
641 ata_wait_idle(ap);
642
643 ap->ops->dev_select(ap, device);
644
645 if (wait) {
646 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
647 msleep(150);
648 ata_wait_idle(ap);
649 }
650}
651
652/**
653 * ata_dump_id - IDENTIFY DEVICE info debugging output
0bd3300a 654 * @id: IDENTIFY DEVICE page to dump
1da177e4 655 *
0bd3300a
TH
656 * Dump selected 16-bit words from the given IDENTIFY DEVICE
657 * page.
1da177e4
LT
658 *
659 * LOCKING:
660 * caller.
661 */
662
0bd3300a 663static inline void ata_dump_id(const u16 *id)
1da177e4
LT
664{
665 DPRINTK("49==0x%04x "
666 "53==0x%04x "
667 "63==0x%04x "
668 "64==0x%04x "
669 "75==0x%04x \n",
0bd3300a
TH
670 id[49],
671 id[53],
672 id[63],
673 id[64],
674 id[75]);
1da177e4
LT
675 DPRINTK("80==0x%04x "
676 "81==0x%04x "
677 "82==0x%04x "
678 "83==0x%04x "
679 "84==0x%04x \n",
0bd3300a
TH
680 id[80],
681 id[81],
682 id[82],
683 id[83],
684 id[84]);
1da177e4
LT
685 DPRINTK("88==0x%04x "
686 "93==0x%04x\n",
0bd3300a
TH
687 id[88],
688 id[93]);
1da177e4
LT
689}
690
11e29e21
AC
691/*
692 * Compute the PIO modes available for this device. This is not as
693 * trivial as it seems if we must consider early devices correctly.
694 *
695 * FIXME: pre IDE drive timing (do we care ?).
696 */
697
057ace5e 698static unsigned int ata_pio_modes(const struct ata_device *adev)
11e29e21
AC
699{
700 u16 modes;
701
ffa29456
AC
702 /* Usual case. Word 53 indicates word 64 is valid */
703 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
11e29e21
AC
704 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
705 modes <<= 3;
706 modes |= 0x7;
707 return modes;
708 }
709
ffa29456
AC
710 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
711 number for the maximum. Turn it into a mask and return it */
712 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
11e29e21 713 return modes;
ffa29456
AC
714 /* But wait.. there's more. Design your standards by committee and
715 you too can get a free iordy field to process. However its the
716 speeds not the modes that are supported... Note drivers using the
717 timing API will get this right anyway */
11e29e21
AC
718}
719
95064379
TH
720static inline void
721ata_queue_packet_task(struct ata_port *ap)
722{
c18d06f8
TH
723 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
724 queue_work(ata_wq, &ap->packet_task);
95064379
TH
725}
726
727static inline void
728ata_queue_pio_task(struct ata_port *ap)
729{
c18d06f8
TH
730 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
731 queue_work(ata_wq, &ap->pio_task);
95064379
TH
732}
733
734static inline void
735ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
736{
c18d06f8
TH
737 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
738 queue_delayed_work(ata_wq, &ap->pio_task, delay);
739}
740
741/**
742 * ata_flush_pio_tasks - Flush pio_task and packet_task
743 * @ap: the target ata_port
744 *
745 * After this function completes, pio_task and packet_task are
746 * guranteed not to be running or scheduled.
747 *
748 * LOCKING:
749 * Kernel thread context (may sleep)
750 */
751
752static void ata_flush_pio_tasks(struct ata_port *ap)
753{
754 int tmp = 0;
755 unsigned long flags;
756
757 DPRINTK("ENTER\n");
758
759 spin_lock_irqsave(&ap->host_set->lock, flags);
760 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
761 spin_unlock_irqrestore(&ap->host_set->lock, flags);
762
763 DPRINTK("flush #1\n");
764 flush_workqueue(ata_wq);
765
766 /*
767 * At this point, if a task is running, it's guaranteed to see
768 * the FLUSH flag; thus, it will never queue pio tasks again.
769 * Cancel and flush.
770 */
771 tmp |= cancel_delayed_work(&ap->pio_task);
772 tmp |= cancel_delayed_work(&ap->packet_task);
773 if (!tmp) {
774 DPRINTK("flush #2\n");
775 flush_workqueue(ata_wq);
776 }
777
778 spin_lock_irqsave(&ap->host_set->lock, flags);
779 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
780 spin_unlock_irqrestore(&ap->host_set->lock, flags);
781
782 DPRINTK("EXIT\n");
95064379
TH
783}
784
77853bf2 785void ata_qc_complete_internal(struct ata_queued_cmd *qc)
a2a7a662 786{
77853bf2 787 struct completion *waiting = qc->private_data;
a2a7a662 788
77853bf2 789 qc->ap->ops->tf_read(qc->ap, &qc->tf);
a2a7a662 790 complete(waiting);
a2a7a662
TH
791}
792
793/**
794 * ata_exec_internal - execute libata internal command
795 * @ap: Port to which the command is sent
796 * @dev: Device to which the command is sent
797 * @tf: Taskfile registers for the command and the result
798 * @dma_dir: Data tranfer direction of the command
799 * @buf: Data buffer of the command
800 * @buflen: Length of data buffer
801 *
802 * Executes libata internal command with timeout. @tf contains
803 * command on entry and result on return. Timeout and error
804 * conditions are reported via return value. No recovery action
805 * is taken after a command times out. It's caller's duty to
806 * clean up after timeout.
807 *
808 * LOCKING:
809 * None. Should be called with kernel context, might sleep.
810 */
811
812static unsigned
813ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
814 struct ata_taskfile *tf,
815 int dma_dir, void *buf, unsigned int buflen)
816{
817 u8 command = tf->command;
818 struct ata_queued_cmd *qc;
819 DECLARE_COMPLETION(wait);
820 unsigned long flags;
77853bf2 821 unsigned int err_mask;
a2a7a662
TH
822
823 spin_lock_irqsave(&ap->host_set->lock, flags);
824
825 qc = ata_qc_new_init(ap, dev);
826 BUG_ON(qc == NULL);
827
828 qc->tf = *tf;
829 qc->dma_dir = dma_dir;
830 if (dma_dir != DMA_NONE) {
831 ata_sg_init_one(qc, buf, buflen);
832 qc->nsect = buflen / ATA_SECT_SIZE;
833 }
834
77853bf2 835 qc->private_data = &wait;
a2a7a662
TH
836 qc->complete_fn = ata_qc_complete_internal;
837
9a3d9eb0
TH
838 qc->err_mask = ata_qc_issue(qc);
839 if (qc->err_mask)
8e436af9 840 ata_qc_complete(qc);
a2a7a662
TH
841
842 spin_unlock_irqrestore(&ap->host_set->lock, flags);
843
844 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
845 spin_lock_irqsave(&ap->host_set->lock, flags);
846
847 /* We're racing with irq here. If we lose, the
848 * following test prevents us from completing the qc
849 * again. If completion irq occurs after here but
850 * before the caller cleans up, it will result in a
851 * spurious interrupt. We can live with that.
852 */
77853bf2 853 if (qc->flags & ATA_QCFLAG_ACTIVE) {
11a56d24 854 qc->err_mask = AC_ERR_TIMEOUT;
a2a7a662
TH
855 ata_qc_complete(qc);
856 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
857 ap->id, command);
858 }
859
860 spin_unlock_irqrestore(&ap->host_set->lock, flags);
861 }
862
77853bf2
TH
863 *tf = qc->tf;
864 err_mask = qc->err_mask;
865
866 ata_qc_free(qc);
867
868 return err_mask;
a2a7a662
TH
869}
870
1bc4ccff
AC
871/**
872 * ata_pio_need_iordy - check if iordy needed
873 * @adev: ATA device
874 *
875 * Check if the current speed of the device requires IORDY. Used
876 * by various controllers for chip configuration.
877 */
878
879unsigned int ata_pio_need_iordy(const struct ata_device *adev)
880{
881 int pio;
882 int speed = adev->pio_mode - XFER_PIO_0;
883
884 if (speed < 2)
885 return 0;
886 if (speed > 2)
887 return 1;
888
889 /* If we have no drive specific rule, then PIO 2 is non IORDY */
890
891 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
892 pio = adev->id[ATA_ID_EIDE_PIO];
893 /* Is the speed faster than the drive allows non IORDY ? */
894 if (pio) {
895 /* This is cycle times not frequency - watch the logic! */
896 if (pio > 240) /* PIO2 is 240nS per cycle */
897 return 1;
898 return 0;
899 }
900 }
901 return 0;
902}
903
1da177e4
LT
904/**
905 * ata_dev_identify - obtain IDENTIFY x DEVICE page
906 * @ap: port on which device we wish to probe resides
907 * @device: device bus address, starting at zero
908 *
909 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
910 * command, and read back the 512-byte device information page.
911 * The device information page is fed to us via the standard
912 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
913 * using standard PIO-IN paths)
914 *
915 * After reading the device information page, we use several
916 * bits of information from it to initialize data structures
917 * that will be used during the lifetime of the ata_device.
918 * Other data from the info page is used to disqualify certain
919 * older ATA devices we do not wish to support.
920 *
921 * LOCKING:
922 * Inherited from caller. Some functions called by this function
923 * obtain the host_set lock.
924 */
925
926static void ata_dev_identify(struct ata_port *ap, unsigned int device)
927{
928 struct ata_device *dev = &ap->device[device];
8bf62ece 929 unsigned int major_version;
1da177e4 930 unsigned long xfer_modes;
1da177e4 931 unsigned int using_edd;
a0123703
TH
932 struct ata_taskfile tf;
933 unsigned int err_mask;
6e7846e9 934 int i, rc;
1da177e4
LT
935
936 if (!ata_dev_present(dev)) {
937 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
938 ap->id, device);
939 return;
940 }
941
61eb066a
TH
942 if (ap->ops->probe_reset ||
943 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1da177e4
LT
944 using_edd = 0;
945 else
946 using_edd = 1;
947
948 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
949
a4631474
TH
950 WARN_ON(dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ATAPI &&
951 dev->class != ATA_DEV_NONE);
1da177e4
LT
952
953 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
954
1da177e4 955retry:
a0123703
TH
956 ata_tf_init(ap, &tf, device);
957
1da177e4 958 if (dev->class == ATA_DEV_ATA) {
a0123703 959 tf.command = ATA_CMD_ID_ATA;
1da177e4
LT
960 DPRINTK("do ATA identify\n");
961 } else {
a0123703 962 tf.command = ATA_CMD_ID_ATAPI;
1da177e4
LT
963 DPRINTK("do ATAPI identify\n");
964 }
965
a0123703 966 tf.protocol = ATA_PROT_PIO;
1da177e4 967
a0123703
TH
968 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
969 dev->id, sizeof(dev->id));
1da177e4 970
a0123703
TH
971 if (err_mask) {
972 if (err_mask & ~AC_ERR_DEV)
973 goto err_out;
0169e284 974
1da177e4
LT
975 /*
976 * arg! EDD works for all test cases, but seems to return
977 * the ATA signature for some ATAPI devices. Until the
978 * reason for this is found and fixed, we fix up the mess
979 * here. If IDENTIFY DEVICE returns command aborted
980 * (as ATAPI devices do), then we issue an
981 * IDENTIFY PACKET DEVICE.
982 *
983 * ATA software reset (SRST, the default) does not appear
984 * to have this problem.
985 */
7c398335 986 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
a0123703 987 u8 err = tf.feature;
1da177e4
LT
988 if (err & ATA_ABORTED) {
989 dev->class = ATA_DEV_ATAPI;
1da177e4
LT
990 goto retry;
991 }
992 }
993 goto err_out;
994 }
995
996 swap_buf_le16(dev->id, ATA_ID_WORDS);
997
998 /* print device capabilities */
999 printk(KERN_DEBUG "ata%u: dev %u cfg "
1000 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1001 ap->id, device, dev->id[49],
1002 dev->id[82], dev->id[83], dev->id[84],
1003 dev->id[85], dev->id[86], dev->id[87],
1004 dev->id[88]);
1005
1006 /*
1007 * common ATA, ATAPI feature tests
1008 */
1009
8bf62ece
AL
1010 /* we require DMA support (bits 8 of word 49) */
1011 if (!ata_id_has_dma(dev->id)) {
1012 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1da177e4
LT
1013 goto err_out_nosup;
1014 }
1015
1016 /* quick-n-dirty find max transfer mode; for printk only */
1017 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1018 if (!xfer_modes)
1019 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
11e29e21
AC
1020 if (!xfer_modes)
1021 xfer_modes = ata_pio_modes(dev);
1da177e4 1022
0bd3300a 1023 ata_dump_id(dev->id);
1da177e4
LT
1024
1025 /* ATA-specific feature tests */
1026 if (dev->class == ATA_DEV_ATA) {
2940740b
TH
1027 dev->n_sectors = ata_id_n_sectors(dev->id);
1028
1da177e4
LT
1029 if (!ata_id_is_ata(dev->id)) /* sanity check */
1030 goto err_out_nosup;
1031
8bf62ece 1032 /* get major version */
3d2ca910 1033 major_version = ata_id_major_version(dev->id);
1da177e4 1034
8bf62ece
AL
1035 /*
1036 * The exact sequence expected by certain pre-ATA4 drives is:
1037 * SRST RESET
1038 * IDENTIFY
1039 * INITIALIZE DEVICE PARAMETERS
1040 * anything else..
1041 * Some drives were very specific about that exact sequence.
1042 */
59a10b17 1043 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
8bf62ece
AL
1044 ata_dev_init_params(ap, dev);
1045
59a10b17
AL
1046 /* current CHS translation info (id[53-58]) might be
1047 * changed. reread the identify device info.
1048 */
1049 ata_dev_reread_id(ap, dev);
1050 }
1051
8bf62ece
AL
1052 if (ata_id_has_lba(dev->id)) {
1053 dev->flags |= ATA_DFLAG_LBA;
1054
2940740b 1055 if (ata_id_has_lba48(dev->id))
8bf62ece 1056 dev->flags |= ATA_DFLAG_LBA48;
8bf62ece
AL
1057
1058 /* print device info to dmesg */
1059 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1060 ap->id, device,
1061 major_version,
1062 ata_mode_string(xfer_modes),
1063 (unsigned long long)dev->n_sectors,
1064 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1065 } else {
1066 /* CHS */
1067
1068 /* Default translation */
1069 dev->cylinders = dev->id[1];
1070 dev->heads = dev->id[3];
1071 dev->sectors = dev->id[6];
8bf62ece
AL
1072
1073 if (ata_id_current_chs_valid(dev->id)) {
1074 /* Current CHS translation is valid. */
1075 dev->cylinders = dev->id[54];
1076 dev->heads = dev->id[55];
1077 dev->sectors = dev->id[56];
8bf62ece
AL
1078 }
1079
1080 /* print device info to dmesg */
1081 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1082 ap->id, device,
1083 major_version,
1084 ata_mode_string(xfer_modes),
1085 (unsigned long long)dev->n_sectors,
1086 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1da177e4 1087
1da177e4
LT
1088 }
1089
6e7846e9 1090 dev->cdb_len = 16;
1da177e4
LT
1091 }
1092
1093 /* ATAPI-specific feature tests */
2c13b7ce 1094 else if (dev->class == ATA_DEV_ATAPI) {
1da177e4
LT
1095 if (ata_id_is_ata(dev->id)) /* sanity check */
1096 goto err_out_nosup;
1097
1098 rc = atapi_cdb_len(dev->id);
1099 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1100 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1101 goto err_out_nosup;
1102 }
6e7846e9 1103 dev->cdb_len = (unsigned int) rc;
1da177e4
LT
1104
1105 /* print device info to dmesg */
1106 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1107 ap->id, device,
1108 ata_mode_string(xfer_modes));
1109 }
1110
6e7846e9
TH
1111 ap->host->max_cmd_len = 0;
1112 for (i = 0; i < ATA_MAX_DEVICES; i++)
1113 ap->host->max_cmd_len = max_t(unsigned int,
1114 ap->host->max_cmd_len,
1115 ap->device[i].cdb_len);
1116
1da177e4
LT
1117 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1118 return;
1119
1120err_out_nosup:
1121 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1122 ap->id, device);
1123err_out:
1124 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1125 DPRINTK("EXIT, err\n");
1126}
1127
6f2f3812 1128
8eabd02c
TH
1129static inline u8 ata_dev_knobble(const struct ata_port *ap,
1130 struct ata_device *dev)
6f2f3812 1131{
8eabd02c 1132 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
6f2f3812
BC
1133}
1134
1135/**
c893a3ae
RD
1136 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1137 * @ap: Bus
1138 * @i: Device
6f2f3812 1139 *
c893a3ae 1140 * LOCKING:
6f2f3812 1141 */
8a60a071 1142
6f2f3812
BC
1143void ata_dev_config(struct ata_port *ap, unsigned int i)
1144{
1145 /* limit bridge transfers to udma5, 200 sectors */
8eabd02c 1146 if (ata_dev_knobble(ap, &ap->device[i])) {
6f2f3812 1147 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
8eabd02c 1148 ap->id, i);
6f2f3812 1149 ap->udma_mask &= ATA_UDMA5;
b00eec1d 1150 ap->device[i].max_sectors = ATA_MAX_SECTORS;
6f2f3812
BC
1151 }
1152
1153 if (ap->ops->dev_config)
1154 ap->ops->dev_config(ap, &ap->device[i]);
1155}
1156
1da177e4
LT
1157/**
1158 * ata_bus_probe - Reset and probe ATA bus
1159 * @ap: Bus to probe
1160 *
0cba632b
JG
1161 * Master ATA bus probing function. Initiates a hardware-dependent
1162 * bus reset, then attempts to identify any devices found on
1163 * the bus.
1164 *
1da177e4 1165 * LOCKING:
0cba632b 1166 * PCI/etc. bus probe sem.
1da177e4
LT
1167 *
1168 * RETURNS:
1169 * Zero on success, non-zero on error.
1170 */
1171
1172static int ata_bus_probe(struct ata_port *ap)
1173{
1174 unsigned int i, found = 0;
1175
c19ba8af
TH
1176 if (ap->ops->probe_reset) {
1177 unsigned int classes[ATA_MAX_DEVICES];
1178 int rc;
1179
1180 ata_port_probe(ap);
1181
1182 rc = ap->ops->probe_reset(ap, classes);
1183 if (rc == 0) {
06ab7822
TH
1184 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1185 if (classes[i] == ATA_DEV_UNKNOWN)
1186 classes[i] = ATA_DEV_NONE;
c19ba8af 1187 ap->device[i].class = classes[i];
06ab7822 1188 }
c19ba8af
TH
1189 } else {
1190 printk(KERN_ERR "ata%u: probe reset failed, "
1191 "disabling port\n", ap->id);
1192 ata_port_disable(ap);
1193 }
1194 } else
1195 ap->ops->phy_reset(ap);
1196
1da177e4
LT
1197 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1198 goto err_out;
1199
1200 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1201 ata_dev_identify(ap, i);
1202 if (ata_dev_present(&ap->device[i])) {
1203 found = 1;
6f2f3812 1204 ata_dev_config(ap,i);
1da177e4
LT
1205 }
1206 }
1207
1208 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1209 goto err_out_disable;
1210
1211 ata_set_mode(ap);
1212 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1213 goto err_out_disable;
1214
1215 return 0;
1216
1217err_out_disable:
1218 ap->ops->port_disable(ap);
1219err_out:
1220 return -1;
1221}
1222
1223/**
0cba632b
JG
1224 * ata_port_probe - Mark port as enabled
1225 * @ap: Port for which we indicate enablement
1da177e4 1226 *
0cba632b
JG
1227 * Modify @ap data structure such that the system
1228 * thinks that the entire port is enabled.
1229 *
1230 * LOCKING: host_set lock, or some other form of
1231 * serialization.
1da177e4
LT
1232 */
1233
1234void ata_port_probe(struct ata_port *ap)
1235{
1236 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1237}
1238
3be680b7
TH
1239/**
1240 * sata_print_link_status - Print SATA link status
1241 * @ap: SATA port to printk link status about
1242 *
1243 * This function prints link speed and status of a SATA link.
1244 *
1245 * LOCKING:
1246 * None.
1247 */
1248static void sata_print_link_status(struct ata_port *ap)
1249{
1250 u32 sstatus, tmp;
1251 const char *speed;
1252
1253 if (!ap->ops->scr_read)
1254 return;
1255
1256 sstatus = scr_read(ap, SCR_STATUS);
1257
1258 if (sata_dev_present(ap)) {
1259 tmp = (sstatus >> 4) & 0xf;
1260 if (tmp & (1 << 0))
1261 speed = "1.5";
1262 else if (tmp & (1 << 1))
1263 speed = "3.0";
1264 else
1265 speed = "<unknown>";
1266 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1267 ap->id, speed, sstatus);
1268 } else {
1269 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1270 ap->id, sstatus);
1271 }
1272}
1273
1da177e4 1274/**
780a87f7
JG
1275 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1276 * @ap: SATA port associated with target SATA PHY.
1da177e4 1277 *
780a87f7
JG
1278 * This function issues commands to standard SATA Sxxx
1279 * PHY registers, to wake up the phy (and device), and
1280 * clear any reset condition.
1da177e4
LT
1281 *
1282 * LOCKING:
0cba632b 1283 * PCI/etc. bus probe sem.
1da177e4
LT
1284 *
1285 */
1286void __sata_phy_reset(struct ata_port *ap)
1287{
1288 u32 sstatus;
1289 unsigned long timeout = jiffies + (HZ * 5);
1290
1291 if (ap->flags & ATA_FLAG_SATA_RESET) {
cdcca89e
BR
1292 /* issue phy wake/reset */
1293 scr_write_flush(ap, SCR_CONTROL, 0x301);
62ba2841
TH
1294 /* Couldn't find anything in SATA I/II specs, but
1295 * AHCI-1.1 10.4.2 says at least 1 ms. */
1296 mdelay(1);
1da177e4 1297 }
cdcca89e 1298 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1da177e4
LT
1299
1300 /* wait for phy to become ready, if necessary */
1301 do {
1302 msleep(200);
1303 sstatus = scr_read(ap, SCR_STATUS);
1304 if ((sstatus & 0xf) != 1)
1305 break;
1306 } while (time_before(jiffies, timeout));
1307
3be680b7
TH
1308 /* print link status */
1309 sata_print_link_status(ap);
656563e3 1310
3be680b7
TH
1311 /* TODO: phy layer with polling, timeouts, etc. */
1312 if (sata_dev_present(ap))
1da177e4 1313 ata_port_probe(ap);
3be680b7 1314 else
1da177e4 1315 ata_port_disable(ap);
1da177e4
LT
1316
1317 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1318 return;
1319
1320 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1321 ata_port_disable(ap);
1322 return;
1323 }
1324
1325 ap->cbl = ATA_CBL_SATA;
1326}
1327
1328/**
780a87f7
JG
1329 * sata_phy_reset - Reset SATA bus.
1330 * @ap: SATA port associated with target SATA PHY.
1da177e4 1331 *
780a87f7
JG
1332 * This function resets the SATA bus, and then probes
1333 * the bus for devices.
1da177e4
LT
1334 *
1335 * LOCKING:
0cba632b 1336 * PCI/etc. bus probe sem.
1da177e4
LT
1337 *
1338 */
1339void sata_phy_reset(struct ata_port *ap)
1340{
1341 __sata_phy_reset(ap);
1342 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1343 return;
1344 ata_bus_reset(ap);
1345}
1346
1347/**
780a87f7
JG
1348 * ata_port_disable - Disable port.
1349 * @ap: Port to be disabled.
1da177e4 1350 *
780a87f7
JG
1351 * Modify @ap data structure such that the system
1352 * thinks that the entire port is disabled, and should
1353 * never attempt to probe or communicate with devices
1354 * on this port.
1355 *
1356 * LOCKING: host_set lock, or some other form of
1357 * serialization.
1da177e4
LT
1358 */
1359
1360void ata_port_disable(struct ata_port *ap)
1361{
1362 ap->device[0].class = ATA_DEV_NONE;
1363 ap->device[1].class = ATA_DEV_NONE;
1364 ap->flags |= ATA_FLAG_PORT_DISABLED;
1365}
1366
452503f9
AC
1367/*
1368 * This mode timing computation functionality is ported over from
1369 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1370 */
1371/*
1372 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1373 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1374 * for PIO 5, which is a nonstandard extension and UDMA6, which
1375 * is currently supported only by Maxtor drives.
1376 */
1377
1378static const struct ata_timing ata_timing[] = {
1379
1380 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1381 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1382 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1383 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1384
1385 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1386 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1387 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1388
1389/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1390
1391 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1392 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1393 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1394
1395 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1396 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1397 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1398
1399/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1400 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1401 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1402
1403 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1404 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1405 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1406
1407/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1408
1409 { 0xFF }
1410};
1411
1412#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1413#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1414
1415static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1416{
1417 q->setup = EZ(t->setup * 1000, T);
1418 q->act8b = EZ(t->act8b * 1000, T);
1419 q->rec8b = EZ(t->rec8b * 1000, T);
1420 q->cyc8b = EZ(t->cyc8b * 1000, T);
1421 q->active = EZ(t->active * 1000, T);
1422 q->recover = EZ(t->recover * 1000, T);
1423 q->cycle = EZ(t->cycle * 1000, T);
1424 q->udma = EZ(t->udma * 1000, UT);
1425}
1426
1427void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1428 struct ata_timing *m, unsigned int what)
1429{
1430 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1431 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1432 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1433 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1434 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1435 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1436 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1437 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1438}
1439
1440static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1441{
1442 const struct ata_timing *t;
1443
1444 for (t = ata_timing; t->mode != speed; t++)
91190758 1445 if (t->mode == 0xFF)
452503f9
AC
1446 return NULL;
1447 return t;
1448}
1449
1450int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1451 struct ata_timing *t, int T, int UT)
1452{
1453 const struct ata_timing *s;
1454 struct ata_timing p;
1455
1456 /*
1457 * Find the mode.
75b1f2f8 1458 */
452503f9
AC
1459
1460 if (!(s = ata_timing_find_mode(speed)))
1461 return -EINVAL;
1462
75b1f2f8
AL
1463 memcpy(t, s, sizeof(*s));
1464
452503f9
AC
1465 /*
1466 * If the drive is an EIDE drive, it can tell us it needs extended
1467 * PIO/MW_DMA cycle timing.
1468 */
1469
1470 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1471 memset(&p, 0, sizeof(p));
1472 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1473 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1474 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1475 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1476 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1477 }
1478 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1479 }
1480
1481 /*
1482 * Convert the timing to bus clock counts.
1483 */
1484
75b1f2f8 1485 ata_timing_quantize(t, t, T, UT);
452503f9
AC
1486
1487 /*
c893a3ae
RD
1488 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1489 * S.M.A.R.T * and some other commands. We have to ensure that the
1490 * DMA cycle timing is slower/equal than the fastest PIO timing.
452503f9
AC
1491 */
1492
1493 if (speed > XFER_PIO_4) {
1494 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1495 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1496 }
1497
1498 /*
c893a3ae 1499 * Lengthen active & recovery time so that cycle time is correct.
452503f9
AC
1500 */
1501
1502 if (t->act8b + t->rec8b < t->cyc8b) {
1503 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1504 t->rec8b = t->cyc8b - t->act8b;
1505 }
1506
1507 if (t->active + t->recover < t->cycle) {
1508 t->active += (t->cycle - (t->active + t->recover)) / 2;
1509 t->recover = t->cycle - t->active;
1510 }
1511
1512 return 0;
1513}
1514
057ace5e 1515static const struct {
1da177e4
LT
1516 unsigned int shift;
1517 u8 base;
1518} xfer_mode_classes[] = {
1519 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1520 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1521 { ATA_SHIFT_PIO, XFER_PIO_0 },
1522};
1523
858119e1 1524static u8 base_from_shift(unsigned int shift)
1da177e4
LT
1525{
1526 int i;
1527
1528 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1529 if (xfer_mode_classes[i].shift == shift)
1530 return xfer_mode_classes[i].base;
1531
1532 return 0xff;
1533}
1534
1535static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1536{
1537 int ofs, idx;
1538 u8 base;
1539
1540 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1541 return;
1542
1543 if (dev->xfer_shift == ATA_SHIFT_PIO)
1544 dev->flags |= ATA_DFLAG_PIO;
1545
1546 ata_dev_set_xfermode(ap, dev);
1547
1548 base = base_from_shift(dev->xfer_shift);
1549 ofs = dev->xfer_mode - base;
1550 idx = ofs + dev->xfer_shift;
1551 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1552
1553 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1554 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1555
1556 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1557 ap->id, dev->devno, xfer_mode_str[idx]);
1558}
1559
1560static int ata_host_set_pio(struct ata_port *ap)
1561{
1562 unsigned int mask;
1563 int x, i;
1564 u8 base, xfer_mode;
1565
1566 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1567 x = fgb(mask);
1568 if (x < 0) {
1569 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1570 return -1;
1571 }
1572
1573 base = base_from_shift(ATA_SHIFT_PIO);
1574 xfer_mode = base + x;
1575
1576 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1577 (int)base, (int)xfer_mode, mask, x);
1578
1579 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1580 struct ata_device *dev = &ap->device[i];
1581 if (ata_dev_present(dev)) {
1582 dev->pio_mode = xfer_mode;
1583 dev->xfer_mode = xfer_mode;
1584 dev->xfer_shift = ATA_SHIFT_PIO;
1585 if (ap->ops->set_piomode)
1586 ap->ops->set_piomode(ap, dev);
1587 }
1588 }
1589
1590 return 0;
1591}
1592
1593static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1594 unsigned int xfer_shift)
1595{
1596 int i;
1597
1598 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1599 struct ata_device *dev = &ap->device[i];
1600 if (ata_dev_present(dev)) {
1601 dev->dma_mode = xfer_mode;
1602 dev->xfer_mode = xfer_mode;
1603 dev->xfer_shift = xfer_shift;
1604 if (ap->ops->set_dmamode)
1605 ap->ops->set_dmamode(ap, dev);
1606 }
1607 }
1608}
1609
1610/**
1611 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1612 * @ap: port on which timings will be programmed
1613 *
780a87f7
JG
1614 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1615 *
1da177e4 1616 * LOCKING:
0cba632b 1617 * PCI/etc. bus probe sem.
1da177e4
LT
1618 */
1619static void ata_set_mode(struct ata_port *ap)
1620{
8cbd6df1 1621 unsigned int xfer_shift;
1da177e4
LT
1622 u8 xfer_mode;
1623 int rc;
1624
1625 /* step 1: always set host PIO timings */
1626 rc = ata_host_set_pio(ap);
1627 if (rc)
1628 goto err_out;
1629
1630 /* step 2: choose the best data xfer mode */
1631 xfer_mode = xfer_shift = 0;
1632 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1633 if (rc)
1634 goto err_out;
1635
1636 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1637 if (xfer_shift != ATA_SHIFT_PIO)
1638 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1639
1640 /* step 4: update devices' xfer mode */
1641 ata_dev_set_mode(ap, &ap->device[0]);
1642 ata_dev_set_mode(ap, &ap->device[1]);
1643
1644 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1645 return;
1646
1647 if (ap->ops->post_set_mode)
1648 ap->ops->post_set_mode(ap);
1649
1da177e4
LT
1650 return;
1651
1652err_out:
1653 ata_port_disable(ap);
1654}
1655
1fdffbce
JG
1656/**
1657 * ata_tf_to_host - issue ATA taskfile to host controller
1658 * @ap: port to which command is being issued
1659 * @tf: ATA taskfile register set
1660 *
1661 * Issues ATA taskfile register set to ATA host controller,
1662 * with proper synchronization with interrupt handler and
1663 * other threads.
1664 *
1665 * LOCKING:
1666 * spin_lock_irqsave(host_set lock)
1667 */
1668
1669static inline void ata_tf_to_host(struct ata_port *ap,
1670 const struct ata_taskfile *tf)
1671{
1672 ap->ops->tf_load(ap, tf);
1673 ap->ops->exec_command(ap, tf);
1674}
1675
1da177e4
LT
1676/**
1677 * ata_busy_sleep - sleep until BSY clears, or timeout
1678 * @ap: port containing status register to be polled
1679 * @tmout_pat: impatience timeout
1680 * @tmout: overall timeout
1681 *
780a87f7
JG
1682 * Sleep until ATA Status register bit BSY clears,
1683 * or a timeout occurs.
1684 *
1685 * LOCKING: None.
1da177e4
LT
1686 */
1687
6f8b9958
TH
1688unsigned int ata_busy_sleep (struct ata_port *ap,
1689 unsigned long tmout_pat, unsigned long tmout)
1da177e4
LT
1690{
1691 unsigned long timer_start, timeout;
1692 u8 status;
1693
1694 status = ata_busy_wait(ap, ATA_BUSY, 300);
1695 timer_start = jiffies;
1696 timeout = timer_start + tmout_pat;
1697 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1698 msleep(50);
1699 status = ata_busy_wait(ap, ATA_BUSY, 3);
1700 }
1701
1702 if (status & ATA_BUSY)
1703 printk(KERN_WARNING "ata%u is slow to respond, "
1704 "please be patient\n", ap->id);
1705
1706 timeout = timer_start + tmout;
1707 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1708 msleep(50);
1709 status = ata_chk_status(ap);
1710 }
1711
1712 if (status & ATA_BUSY) {
1713 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1714 ap->id, tmout / HZ);
1715 return 1;
1716 }
1717
1718 return 0;
1719}
1720
1721static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1722{
1723 struct ata_ioports *ioaddr = &ap->ioaddr;
1724 unsigned int dev0 = devmask & (1 << 0);
1725 unsigned int dev1 = devmask & (1 << 1);
1726 unsigned long timeout;
1727
1728 /* if device 0 was found in ata_devchk, wait for its
1729 * BSY bit to clear
1730 */
1731 if (dev0)
1732 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1733
1734 /* if device 1 was found in ata_devchk, wait for
1735 * register access, then wait for BSY to clear
1736 */
1737 timeout = jiffies + ATA_TMOUT_BOOT;
1738 while (dev1) {
1739 u8 nsect, lbal;
1740
1741 ap->ops->dev_select(ap, 1);
1742 if (ap->flags & ATA_FLAG_MMIO) {
1743 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1744 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1745 } else {
1746 nsect = inb(ioaddr->nsect_addr);
1747 lbal = inb(ioaddr->lbal_addr);
1748 }
1749 if ((nsect == 1) && (lbal == 1))
1750 break;
1751 if (time_after(jiffies, timeout)) {
1752 dev1 = 0;
1753 break;
1754 }
1755 msleep(50); /* give drive a breather */
1756 }
1757 if (dev1)
1758 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1759
1760 /* is all this really necessary? */
1761 ap->ops->dev_select(ap, 0);
1762 if (dev1)
1763 ap->ops->dev_select(ap, 1);
1764 if (dev0)
1765 ap->ops->dev_select(ap, 0);
1766}
1767
1768/**
0cba632b
JG
1769 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1770 * @ap: Port to reset and probe
1771 *
1772 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1773 * probe the bus. Not often used these days.
1da177e4
LT
1774 *
1775 * LOCKING:
0cba632b 1776 * PCI/etc. bus probe sem.
e5338254 1777 * Obtains host_set lock.
1da177e4
LT
1778 *
1779 */
1780
1781static unsigned int ata_bus_edd(struct ata_port *ap)
1782{
1783 struct ata_taskfile tf;
e5338254 1784 unsigned long flags;
1da177e4
LT
1785
1786 /* set up execute-device-diag (bus reset) taskfile */
1787 /* also, take interrupts to a known state (disabled) */
1788 DPRINTK("execute-device-diag\n");
1789 ata_tf_init(ap, &tf, 0);
1790 tf.ctl |= ATA_NIEN;
1791 tf.command = ATA_CMD_EDD;
1792 tf.protocol = ATA_PROT_NODATA;
1793
1794 /* do bus reset */
e5338254 1795 spin_lock_irqsave(&ap->host_set->lock, flags);
1da177e4 1796 ata_tf_to_host(ap, &tf);
e5338254 1797 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1da177e4
LT
1798
1799 /* spec says at least 2ms. but who knows with those
1800 * crazy ATAPI devices...
1801 */
1802 msleep(150);
1803
1804 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1805}
1806
1807static unsigned int ata_bus_softreset(struct ata_port *ap,
1808 unsigned int devmask)
1809{
1810 struct ata_ioports *ioaddr = &ap->ioaddr;
1811
1812 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1813
1814 /* software reset. causes dev0 to be selected */
1815 if (ap->flags & ATA_FLAG_MMIO) {
1816 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1817 udelay(20); /* FIXME: flush */
1818 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1819 udelay(20); /* FIXME: flush */
1820 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1821 } else {
1822 outb(ap->ctl, ioaddr->ctl_addr);
1823 udelay(10);
1824 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1825 udelay(10);
1826 outb(ap->ctl, ioaddr->ctl_addr);
1827 }
1828
1829 /* spec mandates ">= 2ms" before checking status.
1830 * We wait 150ms, because that was the magic delay used for
1831 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1832 * between when the ATA command register is written, and then
1833 * status is checked. Because waiting for "a while" before
1834 * checking status is fine, post SRST, we perform this magic
1835 * delay here as well.
1836 */
1837 msleep(150);
1838
1839 ata_bus_post_reset(ap, devmask);
1840
1841 return 0;
1842}
1843
1844/**
1845 * ata_bus_reset - reset host port and associated ATA channel
1846 * @ap: port to reset
1847 *
1848 * This is typically the first time we actually start issuing
1849 * commands to the ATA channel. We wait for BSY to clear, then
1850 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1851 * result. Determine what devices, if any, are on the channel
1852 * by looking at the device 0/1 error register. Look at the signature
1853 * stored in each device's taskfile registers, to determine if
1854 * the device is ATA or ATAPI.
1855 *
1856 * LOCKING:
0cba632b
JG
1857 * PCI/etc. bus probe sem.
1858 * Obtains host_set lock.
1da177e4
LT
1859 *
1860 * SIDE EFFECTS:
1861 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1862 */
1863
1864void ata_bus_reset(struct ata_port *ap)
1865{
1866 struct ata_ioports *ioaddr = &ap->ioaddr;
1867 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1868 u8 err;
1869 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1870
1871 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1872
1873 /* determine if device 0/1 are present */
1874 if (ap->flags & ATA_FLAG_SATA_RESET)
1875 dev0 = 1;
1876 else {
1877 dev0 = ata_devchk(ap, 0);
1878 if (slave_possible)
1879 dev1 = ata_devchk(ap, 1);
1880 }
1881
1882 if (dev0)
1883 devmask |= (1 << 0);
1884 if (dev1)
1885 devmask |= (1 << 1);
1886
1887 /* select device 0 again */
1888 ap->ops->dev_select(ap, 0);
1889
1890 /* issue bus reset */
1891 if (ap->flags & ATA_FLAG_SRST)
1892 rc = ata_bus_softreset(ap, devmask);
1893 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1894 /* set up device control */
1895 if (ap->flags & ATA_FLAG_MMIO)
1896 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1897 else
1898 outb(ap->ctl, ioaddr->ctl_addr);
1899 rc = ata_bus_edd(ap);
1900 }
1901
1902 if (rc)
1903 goto err_out;
1904
1905 /*
1906 * determine by signature whether we have ATA or ATAPI devices
1907 */
b4dc7623 1908 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
1da177e4 1909 if ((slave_possible) && (err != 0x81))
b4dc7623 1910 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
1da177e4
LT
1911
1912 /* re-enable interrupts */
1913 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1914 ata_irq_on(ap);
1915
1916 /* is double-select really necessary? */
1917 if (ap->device[1].class != ATA_DEV_NONE)
1918 ap->ops->dev_select(ap, 1);
1919 if (ap->device[0].class != ATA_DEV_NONE)
1920 ap->ops->dev_select(ap, 0);
1921
1922 /* if no devices were detected, disable this port */
1923 if ((ap->device[0].class == ATA_DEV_NONE) &&
1924 (ap->device[1].class == ATA_DEV_NONE))
1925 goto err_out;
1926
1927 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1928 /* set up device control for ATA_FLAG_SATA_RESET */
1929 if (ap->flags & ATA_FLAG_MMIO)
1930 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1931 else
1932 outb(ap->ctl, ioaddr->ctl_addr);
1933 }
1934
1935 DPRINTK("EXIT\n");
1936 return;
1937
1938err_out:
1939 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1940 ap->ops->port_disable(ap);
1941
1942 DPRINTK("EXIT\n");
1943}
1944
7a7921e8
TH
1945static int sata_phy_resume(struct ata_port *ap)
1946{
1947 unsigned long timeout = jiffies + (HZ * 5);
1948 u32 sstatus;
1949
1950 scr_write_flush(ap, SCR_CONTROL, 0x300);
1951
1952 /* Wait for phy to become ready, if necessary. */
1953 do {
1954 msleep(200);
1955 sstatus = scr_read(ap, SCR_STATUS);
1956 if ((sstatus & 0xf) != 1)
1957 return 0;
1958 } while (time_before(jiffies, timeout));
1959
1960 return -1;
1961}
1962
8a19ac89
TH
1963/**
1964 * ata_std_probeinit - initialize probing
1965 * @ap: port to be probed
1966 *
1967 * @ap is about to be probed. Initialize it. This function is
1968 * to be used as standard callback for ata_drive_probe_reset().
3a39746a
TH
1969 *
1970 * NOTE!!! Do not use this function as probeinit if a low level
1971 * driver implements only hardreset. Just pass NULL as probeinit
1972 * in that case. Using this function is probably okay but doing
1973 * so makes reset sequence different from the original
1974 * ->phy_reset implementation and Jeff nervous. :-P
8a19ac89
TH
1975 */
1976extern void ata_std_probeinit(struct ata_port *ap)
1977{
3a39746a 1978 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
8a19ac89 1979 sata_phy_resume(ap);
3a39746a
TH
1980 if (sata_dev_present(ap))
1981 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1982 }
8a19ac89
TH
1983}
1984
c2bd5804
TH
1985/**
1986 * ata_std_softreset - reset host port via ATA SRST
1987 * @ap: port to reset
1988 * @verbose: fail verbosely
1989 * @classes: resulting classes of attached devices
1990 *
1991 * Reset host port using ATA SRST. This function is to be used
1992 * as standard callback for ata_drive_*_reset() functions.
1993 *
1994 * LOCKING:
1995 * Kernel thread context (may sleep)
1996 *
1997 * RETURNS:
1998 * 0 on success, -errno otherwise.
1999 */
2000int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2001{
2002 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2003 unsigned int devmask = 0, err_mask;
2004 u8 err;
2005
2006 DPRINTK("ENTER\n");
2007
3a39746a
TH
2008 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2009 classes[0] = ATA_DEV_NONE;
2010 goto out;
2011 }
2012
c2bd5804
TH
2013 /* determine if device 0/1 are present */
2014 if (ata_devchk(ap, 0))
2015 devmask |= (1 << 0);
2016 if (slave_possible && ata_devchk(ap, 1))
2017 devmask |= (1 << 1);
2018
c2bd5804
TH
2019 /* select device 0 again */
2020 ap->ops->dev_select(ap, 0);
2021
2022 /* issue bus reset */
2023 DPRINTK("about to softreset, devmask=%x\n", devmask);
2024 err_mask = ata_bus_softreset(ap, devmask);
2025 if (err_mask) {
2026 if (verbose)
2027 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2028 ap->id, err_mask);
2029 else
2030 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2031 err_mask);
2032 return -EIO;
2033 }
2034
2035 /* determine by signature whether we have ATA or ATAPI devices */
2036 classes[0] = ata_dev_try_classify(ap, 0, &err);
2037 if (slave_possible && err != 0x81)
2038 classes[1] = ata_dev_try_classify(ap, 1, &err);
2039
3a39746a 2040 out:
c2bd5804
TH
2041 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2042 return 0;
2043}
2044
2045/**
2046 * sata_std_hardreset - reset host port via SATA phy reset
2047 * @ap: port to reset
2048 * @verbose: fail verbosely
2049 * @class: resulting class of attached device
2050 *
2051 * SATA phy-reset host port using DET bits of SControl register.
2052 * This function is to be used as standard callback for
2053 * ata_drive_*_reset().
2054 *
2055 * LOCKING:
2056 * Kernel thread context (may sleep)
2057 *
2058 * RETURNS:
2059 * 0 on success, -errno otherwise.
2060 */
2061int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2062{
c2bd5804
TH
2063 DPRINTK("ENTER\n");
2064
2065 /* Issue phy wake/reset */
2066 scr_write_flush(ap, SCR_CONTROL, 0x301);
2067
2068 /*
2069 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2070 * 10.4.2 says at least 1 ms.
2071 */
2072 msleep(1);
2073
7a7921e8
TH
2074 /* Bring phy back */
2075 sata_phy_resume(ap);
c2bd5804 2076
c2bd5804
TH
2077 /* TODO: phy layer with polling, timeouts, etc. */
2078 if (!sata_dev_present(ap)) {
2079 *class = ATA_DEV_NONE;
2080 DPRINTK("EXIT, link offline\n");
2081 return 0;
2082 }
2083
2084 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2085 if (verbose)
2086 printk(KERN_ERR "ata%u: COMRESET failed "
2087 "(device not ready)\n", ap->id);
2088 else
2089 DPRINTK("EXIT, device not ready\n");
2090 return -EIO;
2091 }
2092
3a39746a
TH
2093 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2094
c2bd5804
TH
2095 *class = ata_dev_try_classify(ap, 0, NULL);
2096
2097 DPRINTK("EXIT, class=%u\n", *class);
2098 return 0;
2099}
2100
2101/**
2102 * ata_std_postreset - standard postreset callback
2103 * @ap: the target ata_port
2104 * @classes: classes of attached devices
2105 *
2106 * This function is invoked after a successful reset. Note that
2107 * the device might have been reset more than once using
2108 * different reset methods before postreset is invoked.
2109 * postreset is also reponsible for setting cable type.
2110 *
2111 * This function is to be used as standard callback for
2112 * ata_drive_*_reset().
2113 *
2114 * LOCKING:
2115 * Kernel thread context (may sleep)
2116 */
2117void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2118{
2119 DPRINTK("ENTER\n");
2120
2121 /* set cable type */
2122 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2123 ap->cbl = ATA_CBL_SATA;
2124
2125 /* print link status */
2126 if (ap->cbl == ATA_CBL_SATA)
2127 sata_print_link_status(ap);
2128
3a39746a
TH
2129 /* re-enable interrupts */
2130 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2131 ata_irq_on(ap);
c2bd5804
TH
2132
2133 /* is double-select really necessary? */
2134 if (classes[0] != ATA_DEV_NONE)
2135 ap->ops->dev_select(ap, 1);
2136 if (classes[1] != ATA_DEV_NONE)
2137 ap->ops->dev_select(ap, 0);
2138
3a39746a
TH
2139 /* bail out if no device is present */
2140 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2141 DPRINTK("EXIT, no device\n");
2142 return;
2143 }
2144
2145 /* set up device control */
2146 if (ap->ioaddr.ctl_addr) {
2147 if (ap->flags & ATA_FLAG_MMIO)
2148 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2149 else
2150 outb(ap->ctl, ap->ioaddr.ctl_addr);
2151 }
c2bd5804
TH
2152
2153 DPRINTK("EXIT\n");
2154}
2155
2156/**
2157 * ata_std_probe_reset - standard probe reset method
2158 * @ap: prot to perform probe-reset
2159 * @classes: resulting classes of attached devices
2160 *
2161 * The stock off-the-shelf ->probe_reset method.
2162 *
2163 * LOCKING:
2164 * Kernel thread context (may sleep)
2165 *
2166 * RETURNS:
2167 * 0 on success, -errno otherwise.
2168 */
2169int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2170{
2171 ata_reset_fn_t hardreset;
2172
2173 hardreset = NULL;
b911fc3a 2174 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
c2bd5804
TH
2175 hardreset = sata_std_hardreset;
2176
8a19ac89 2177 return ata_drive_probe_reset(ap, ata_std_probeinit,
7944ea95 2178 ata_std_softreset, hardreset,
c2bd5804
TH
2179 ata_std_postreset, classes);
2180}
2181
a62c0fc5
TH
2182static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2183 ata_postreset_fn_t postreset,
2184 unsigned int *classes)
2185{
2186 int i, rc;
2187
2188 for (i = 0; i < ATA_MAX_DEVICES; i++)
2189 classes[i] = ATA_DEV_UNKNOWN;
2190
2191 rc = reset(ap, 0, classes);
2192 if (rc)
2193 return rc;
2194
2195 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2196 * is complete and convert all ATA_DEV_UNKNOWN to
2197 * ATA_DEV_NONE.
2198 */
2199 for (i = 0; i < ATA_MAX_DEVICES; i++)
2200 if (classes[i] != ATA_DEV_UNKNOWN)
2201 break;
2202
2203 if (i < ATA_MAX_DEVICES)
2204 for (i = 0; i < ATA_MAX_DEVICES; i++)
2205 if (classes[i] == ATA_DEV_UNKNOWN)
2206 classes[i] = ATA_DEV_NONE;
2207
2208 if (postreset)
2209 postreset(ap, classes);
2210
2211 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2212}
2213
2214/**
2215 * ata_drive_probe_reset - Perform probe reset with given methods
2216 * @ap: port to reset
7944ea95 2217 * @probeinit: probeinit method (can be NULL)
a62c0fc5
TH
2218 * @softreset: softreset method (can be NULL)
2219 * @hardreset: hardreset method (can be NULL)
2220 * @postreset: postreset method (can be NULL)
2221 * @classes: resulting classes of attached devices
2222 *
2223 * Reset the specified port and classify attached devices using
2224 * given methods. This function prefers softreset but tries all
2225 * possible reset sequences to reset and classify devices. This
2226 * function is intended to be used for constructing ->probe_reset
2227 * callback by low level drivers.
2228 *
2229 * Reset methods should follow the following rules.
2230 *
2231 * - Return 0 on sucess, -errno on failure.
2232 * - If classification is supported, fill classes[] with
2233 * recognized class codes.
2234 * - If classification is not supported, leave classes[] alone.
2235 * - If verbose is non-zero, print error message on failure;
2236 * otherwise, shut up.
2237 *
2238 * LOCKING:
2239 * Kernel thread context (may sleep)
2240 *
2241 * RETURNS:
2242 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2243 * if classification fails, and any error code from reset
2244 * methods.
2245 */
7944ea95 2246int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
a62c0fc5
TH
2247 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2248 ata_postreset_fn_t postreset, unsigned int *classes)
2249{
2250 int rc = -EINVAL;
2251
7944ea95
TH
2252 if (probeinit)
2253 probeinit(ap);
2254
a62c0fc5
TH
2255 if (softreset) {
2256 rc = do_probe_reset(ap, softreset, postreset, classes);
2257 if (rc == 0)
2258 return 0;
2259 }
2260
2261 if (!hardreset)
2262 return rc;
2263
2264 rc = do_probe_reset(ap, hardreset, postreset, classes);
2265 if (rc == 0 || rc != -ENODEV)
2266 return rc;
2267
2268 if (softreset)
2269 rc = do_probe_reset(ap, softreset, postreset, classes);
2270
2271 return rc;
2272}
2273
057ace5e
JG
2274static void ata_pr_blacklisted(const struct ata_port *ap,
2275 const struct ata_device *dev)
1da177e4
LT
2276{
2277 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2278 ap->id, dev->devno);
2279}
2280
98ac62de 2281static const char * const ata_dma_blacklist [] = {
1da177e4
LT
2282 "WDC AC11000H",
2283 "WDC AC22100H",
2284 "WDC AC32500H",
2285 "WDC AC33100H",
2286 "WDC AC31600H",
2287 "WDC AC32100H",
2288 "WDC AC23200L",
2289 "Compaq CRD-8241B",
2290 "CRD-8400B",
2291 "CRD-8480B",
2292 "CRD-8482B",
2293 "CRD-84",
2294 "SanDisk SDP3B",
2295 "SanDisk SDP3B-64",
2296 "SANYO CD-ROM CRD",
2297 "HITACHI CDR-8",
2298 "HITACHI CDR-8335",
2299 "HITACHI CDR-8435",
2300 "Toshiba CD-ROM XM-6202B",
e922256a 2301 "TOSHIBA CD-ROM XM-1702BC",
1da177e4
LT
2302 "CD-532E-A",
2303 "E-IDE CD-ROM CR-840",
2304 "CD-ROM Drive/F5A",
2305 "WPI CDD-820",
2306 "SAMSUNG CD-ROM SC-148C",
2307 "SAMSUNG CD-ROM SC",
2308 "SanDisk SDP3B-64",
1da177e4
LT
2309 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2310 "_NEC DV5800A",
2311};
2312
057ace5e 2313static int ata_dma_blacklisted(const struct ata_device *dev)
1da177e4 2314{
2e02671d 2315 unsigned char model_num[41];
1da177e4
LT
2316 int i;
2317
2e02671d
TH
2318 ata_dev_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS,
2319 sizeof(model_num));
1da177e4
LT
2320
2321 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2e02671d 2322 if (!strcmp(ata_dma_blacklist[i], model_num))
1da177e4
LT
2323 return 1;
2324
2325 return 0;
2326}
2327
057ace5e 2328static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
1da177e4 2329{
057ace5e 2330 const struct ata_device *master, *slave;
1da177e4
LT
2331 unsigned int mask;
2332
2333 master = &ap->device[0];
2334 slave = &ap->device[1];
2335
a4631474 2336 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
1da177e4
LT
2337
2338 if (shift == ATA_SHIFT_UDMA) {
2339 mask = ap->udma_mask;
2340 if (ata_dev_present(master)) {
2341 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
057ace5e 2342 if (ata_dma_blacklisted(master)) {
1da177e4
LT
2343 mask = 0;
2344 ata_pr_blacklisted(ap, master);
2345 }
2346 }
2347 if (ata_dev_present(slave)) {
2348 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
057ace5e 2349 if (ata_dma_blacklisted(slave)) {
1da177e4
LT
2350 mask = 0;
2351 ata_pr_blacklisted(ap, slave);
2352 }
2353 }
2354 }
2355 else if (shift == ATA_SHIFT_MWDMA) {
2356 mask = ap->mwdma_mask;
2357 if (ata_dev_present(master)) {
2358 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
057ace5e 2359 if (ata_dma_blacklisted(master)) {
1da177e4
LT
2360 mask = 0;
2361 ata_pr_blacklisted(ap, master);
2362 }
2363 }
2364 if (ata_dev_present(slave)) {
2365 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
057ace5e 2366 if (ata_dma_blacklisted(slave)) {
1da177e4
LT
2367 mask = 0;
2368 ata_pr_blacklisted(ap, slave);
2369 }
2370 }
2371 }
2372 else if (shift == ATA_SHIFT_PIO) {
2373 mask = ap->pio_mask;
2374 if (ata_dev_present(master)) {
2375 /* spec doesn't return explicit support for
2376 * PIO0-2, so we fake it
2377 */
2378 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2379 tmp_mode <<= 3;
2380 tmp_mode |= 0x7;
2381 mask &= tmp_mode;
2382 }
2383 if (ata_dev_present(slave)) {
2384 /* spec doesn't return explicit support for
2385 * PIO0-2, so we fake it
2386 */
2387 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2388 tmp_mode <<= 3;
2389 tmp_mode |= 0x7;
2390 mask &= tmp_mode;
2391 }
2392 }
2393 else {
2394 mask = 0xffffffff; /* shut up compiler warning */
2395 BUG();
2396 }
2397
2398 return mask;
2399}
2400
2401/* find greatest bit */
2402static int fgb(u32 bitmap)
2403{
2404 unsigned int i;
2405 int x = -1;
2406
2407 for (i = 0; i < 32; i++)
2408 if (bitmap & (1 << i))
2409 x = i;
2410
2411 return x;
2412}
2413
2414/**
2415 * ata_choose_xfer_mode - attempt to find best transfer mode
2416 * @ap: Port for which an xfer mode will be selected
2417 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2418 * @xfer_shift_out: (output) bit shift that selects this mode
2419 *
0cba632b
JG
2420 * Based on host and device capabilities, determine the
2421 * maximum transfer mode that is amenable to all.
2422 *
1da177e4 2423 * LOCKING:
0cba632b 2424 * PCI/etc. bus probe sem.
1da177e4
LT
2425 *
2426 * RETURNS:
2427 * Zero on success, negative on error.
2428 */
2429
057ace5e 2430static int ata_choose_xfer_mode(const struct ata_port *ap,
1da177e4
LT
2431 u8 *xfer_mode_out,
2432 unsigned int *xfer_shift_out)
2433{
2434 unsigned int mask, shift;
2435 int x, i;
2436
2437 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2438 shift = xfer_mode_classes[i].shift;
2439 mask = ata_get_mode_mask(ap, shift);
2440
2441 x = fgb(mask);
2442 if (x >= 0) {
2443 *xfer_mode_out = xfer_mode_classes[i].base + x;
2444 *xfer_shift_out = shift;
2445 return 0;
2446 }
2447 }
2448
2449 return -1;
2450}
2451
2452/**
2453 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2454 * @ap: Port associated with device @dev
2455 * @dev: Device to which command will be sent
2456 *
780a87f7
JG
2457 * Issue SET FEATURES - XFER MODE command to device @dev
2458 * on port @ap.
2459 *
1da177e4 2460 * LOCKING:
0cba632b 2461 * PCI/etc. bus probe sem.
1da177e4
LT
2462 */
2463
2464static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2465{
a0123703 2466 struct ata_taskfile tf;
1da177e4
LT
2467
2468 /* set up set-features taskfile */
2469 DPRINTK("set features - xfer mode\n");
2470
a0123703
TH
2471 ata_tf_init(ap, &tf, dev->devno);
2472 tf.command = ATA_CMD_SET_FEATURES;
2473 tf.feature = SETFEATURES_XFER;
2474 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2475 tf.protocol = ATA_PROT_NODATA;
2476 tf.nsect = dev->xfer_mode;
1da177e4 2477
a0123703
TH
2478 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2479 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2480 ap->id);
1da177e4 2481 ata_port_disable(ap);
a0123703 2482 }
1da177e4
LT
2483
2484 DPRINTK("EXIT\n");
2485}
2486
59a10b17
AL
2487/**
2488 * ata_dev_reread_id - Reread the device identify device info
2489 * @ap: port where the device is
2490 * @dev: device to reread the identify device info
2491 *
2492 * LOCKING:
2493 */
2494
2495static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2496{
a0123703 2497 struct ata_taskfile tf;
59a10b17 2498
a0123703 2499 ata_tf_init(ap, &tf, dev->devno);
59a10b17
AL
2500
2501 if (dev->class == ATA_DEV_ATA) {
a0123703 2502 tf.command = ATA_CMD_ID_ATA;
59a10b17
AL
2503 DPRINTK("do ATA identify\n");
2504 } else {
a0123703 2505 tf.command = ATA_CMD_ID_ATAPI;
59a10b17
AL
2506 DPRINTK("do ATAPI identify\n");
2507 }
2508
a0123703
TH
2509 tf.flags |= ATA_TFLAG_DEVICE;
2510 tf.protocol = ATA_PROT_PIO;
59a10b17 2511
a0123703
TH
2512 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2513 dev->id, sizeof(dev->id)))
59a10b17
AL
2514 goto err_out;
2515
59a10b17
AL
2516 swap_buf_le16(dev->id, ATA_ID_WORDS);
2517
0bd3300a 2518 ata_dump_id(dev->id);
59a10b17
AL
2519
2520 DPRINTK("EXIT\n");
2521
2522 return;
2523err_out:
a0123703 2524 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
59a10b17
AL
2525 ata_port_disable(ap);
2526}
2527
8bf62ece
AL
2528/**
2529 * ata_dev_init_params - Issue INIT DEV PARAMS command
2530 * @ap: Port associated with device @dev
2531 * @dev: Device to which command will be sent
2532 *
2533 * LOCKING:
2534 */
2535
2536static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2537{
a0123703 2538 struct ata_taskfile tf;
8bf62ece
AL
2539 u16 sectors = dev->id[6];
2540 u16 heads = dev->id[3];
2541
2542 /* Number of sectors per track 1-255. Number of heads 1-16 */
2543 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2544 return;
2545
2546 /* set up init dev params taskfile */
2547 DPRINTK("init dev params \n");
2548
a0123703
TH
2549 ata_tf_init(ap, &tf, dev->devno);
2550 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2551 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2552 tf.protocol = ATA_PROT_NODATA;
2553 tf.nsect = sectors;
2554 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
8bf62ece 2555
a0123703
TH
2556 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2557 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2558 ap->id);
8bf62ece 2559 ata_port_disable(ap);
a0123703 2560 }
8bf62ece
AL
2561
2562 DPRINTK("EXIT\n");
2563}
2564
1da177e4 2565/**
0cba632b
JG
2566 * ata_sg_clean - Unmap DMA memory associated with command
2567 * @qc: Command containing DMA memory to be released
2568 *
2569 * Unmap all mapped DMA memory associated with this command.
1da177e4
LT
2570 *
2571 * LOCKING:
0cba632b 2572 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2573 */
2574
2575static void ata_sg_clean(struct ata_queued_cmd *qc)
2576{
2577 struct ata_port *ap = qc->ap;
cedc9a47 2578 struct scatterlist *sg = qc->__sg;
1da177e4 2579 int dir = qc->dma_dir;
cedc9a47 2580 void *pad_buf = NULL;
1da177e4 2581
a4631474
TH
2582 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2583 WARN_ON(sg == NULL);
1da177e4
LT
2584
2585 if (qc->flags & ATA_QCFLAG_SINGLE)
a4631474 2586 WARN_ON(qc->n_elem != 1);
1da177e4 2587
2c13b7ce 2588 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
1da177e4 2589
cedc9a47
JG
2590 /* if we padded the buffer out to 32-bit bound, and data
2591 * xfer direction is from-device, we must copy from the
2592 * pad buffer back into the supplied buffer
2593 */
2594 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2595 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2596
2597 if (qc->flags & ATA_QCFLAG_SG) {
e1410f2d
JG
2598 if (qc->n_elem)
2599 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
cedc9a47
JG
2600 /* restore last sg */
2601 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2602 if (pad_buf) {
2603 struct scatterlist *psg = &qc->pad_sgent;
2604 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2605 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
dfa15988 2606 kunmap_atomic(addr, KM_IRQ0);
cedc9a47
JG
2607 }
2608 } else {
e1410f2d
JG
2609 if (sg_dma_len(&sg[0]) > 0)
2610 dma_unmap_single(ap->host_set->dev,
2611 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2612 dir);
cedc9a47
JG
2613 /* restore sg */
2614 sg->length += qc->pad_len;
2615 if (pad_buf)
2616 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2617 pad_buf, qc->pad_len);
2618 }
1da177e4
LT
2619
2620 qc->flags &= ~ATA_QCFLAG_DMAMAP;
cedc9a47 2621 qc->__sg = NULL;
1da177e4
LT
2622}
2623
2624/**
2625 * ata_fill_sg - Fill PCI IDE PRD table
2626 * @qc: Metadata associated with taskfile to be transferred
2627 *
780a87f7
JG
2628 * Fill PCI IDE PRD (scatter-gather) table with segments
2629 * associated with the current disk command.
2630 *
1da177e4 2631 * LOCKING:
780a87f7 2632 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2633 *
2634 */
2635static void ata_fill_sg(struct ata_queued_cmd *qc)
2636{
1da177e4 2637 struct ata_port *ap = qc->ap;
cedc9a47
JG
2638 struct scatterlist *sg;
2639 unsigned int idx;
1da177e4 2640
a4631474
TH
2641 WARN_ON(qc->__sg == NULL);
2642 WARN_ON(qc->n_elem == 0);
1da177e4
LT
2643
2644 idx = 0;
cedc9a47 2645 ata_for_each_sg(sg, qc) {
1da177e4
LT
2646 u32 addr, offset;
2647 u32 sg_len, len;
2648
2649 /* determine if physical DMA addr spans 64K boundary.
2650 * Note h/w doesn't support 64-bit, so we unconditionally
2651 * truncate dma_addr_t to u32.
2652 */
2653 addr = (u32) sg_dma_address(sg);
2654 sg_len = sg_dma_len(sg);
2655
2656 while (sg_len) {
2657 offset = addr & 0xffff;
2658 len = sg_len;
2659 if ((offset + sg_len) > 0x10000)
2660 len = 0x10000 - offset;
2661
2662 ap->prd[idx].addr = cpu_to_le32(addr);
2663 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2664 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2665
2666 idx++;
2667 sg_len -= len;
2668 addr += len;
2669 }
2670 }
2671
2672 if (idx)
2673 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2674}
2675/**
2676 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2677 * @qc: Metadata associated with taskfile to check
2678 *
780a87f7
JG
2679 * Allow low-level driver to filter ATA PACKET commands, returning
2680 * a status indicating whether or not it is OK to use DMA for the
2681 * supplied PACKET command.
2682 *
1da177e4 2683 * LOCKING:
0cba632b
JG
2684 * spin_lock_irqsave(host_set lock)
2685 *
1da177e4
LT
2686 * RETURNS: 0 when ATAPI DMA can be used
2687 * nonzero otherwise
2688 */
2689int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2690{
2691 struct ata_port *ap = qc->ap;
2692 int rc = 0; /* Assume ATAPI DMA is OK by default */
2693
2694 if (ap->ops->check_atapi_dma)
2695 rc = ap->ops->check_atapi_dma(qc);
2696
2697 return rc;
2698}
2699/**
2700 * ata_qc_prep - Prepare taskfile for submission
2701 * @qc: Metadata associated with taskfile to be prepared
2702 *
780a87f7
JG
2703 * Prepare ATA taskfile for submission.
2704 *
1da177e4
LT
2705 * LOCKING:
2706 * spin_lock_irqsave(host_set lock)
2707 */
2708void ata_qc_prep(struct ata_queued_cmd *qc)
2709{
2710 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2711 return;
2712
2713 ata_fill_sg(qc);
2714}
2715
0cba632b
JG
2716/**
2717 * ata_sg_init_one - Associate command with memory buffer
2718 * @qc: Command to be associated
2719 * @buf: Memory buffer
2720 * @buflen: Length of memory buffer, in bytes.
2721 *
2722 * Initialize the data-related elements of queued_cmd @qc
2723 * to point to a single memory buffer, @buf of byte length @buflen.
2724 *
2725 * LOCKING:
2726 * spin_lock_irqsave(host_set lock)
2727 */
2728
1da177e4
LT
2729void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2730{
2731 struct scatterlist *sg;
2732
2733 qc->flags |= ATA_QCFLAG_SINGLE;
2734
2735 memset(&qc->sgent, 0, sizeof(qc->sgent));
cedc9a47 2736 qc->__sg = &qc->sgent;
1da177e4 2737 qc->n_elem = 1;
cedc9a47 2738 qc->orig_n_elem = 1;
1da177e4
LT
2739 qc->buf_virt = buf;
2740
cedc9a47 2741 sg = qc->__sg;
f0612bbc 2742 sg_init_one(sg, buf, buflen);
1da177e4
LT
2743}
2744
0cba632b
JG
2745/**
2746 * ata_sg_init - Associate command with scatter-gather table.
2747 * @qc: Command to be associated
2748 * @sg: Scatter-gather table.
2749 * @n_elem: Number of elements in s/g table.
2750 *
2751 * Initialize the data-related elements of queued_cmd @qc
2752 * to point to a scatter-gather table @sg, containing @n_elem
2753 * elements.
2754 *
2755 * LOCKING:
2756 * spin_lock_irqsave(host_set lock)
2757 */
2758
1da177e4
LT
2759void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2760 unsigned int n_elem)
2761{
2762 qc->flags |= ATA_QCFLAG_SG;
cedc9a47 2763 qc->__sg = sg;
1da177e4 2764 qc->n_elem = n_elem;
cedc9a47 2765 qc->orig_n_elem = n_elem;
1da177e4
LT
2766}
2767
2768/**
0cba632b
JG
2769 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2770 * @qc: Command with memory buffer to be mapped.
2771 *
2772 * DMA-map the memory buffer associated with queued_cmd @qc.
1da177e4
LT
2773 *
2774 * LOCKING:
2775 * spin_lock_irqsave(host_set lock)
2776 *
2777 * RETURNS:
0cba632b 2778 * Zero on success, negative on error.
1da177e4
LT
2779 */
2780
2781static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2782{
2783 struct ata_port *ap = qc->ap;
2784 int dir = qc->dma_dir;
cedc9a47 2785 struct scatterlist *sg = qc->__sg;
1da177e4
LT
2786 dma_addr_t dma_address;
2787
cedc9a47
JG
2788 /* we must lengthen transfers to end on a 32-bit boundary */
2789 qc->pad_len = sg->length & 3;
2790 if (qc->pad_len) {
2791 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2792 struct scatterlist *psg = &qc->pad_sgent;
2793
a4631474 2794 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
cedc9a47
JG
2795
2796 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2797
2798 if (qc->tf.flags & ATA_TFLAG_WRITE)
2799 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2800 qc->pad_len);
2801
2802 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2803 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2804 /* trim sg */
2805 sg->length -= qc->pad_len;
2806
2807 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2808 sg->length, qc->pad_len);
2809 }
2810
e1410f2d
JG
2811 if (!sg->length) {
2812 sg_dma_address(sg) = 0;
2813 goto skip_map;
2814 }
2815
1da177e4 2816 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
32529e01 2817 sg->length, dir);
537a95d9
TH
2818 if (dma_mapping_error(dma_address)) {
2819 /* restore sg */
2820 sg->length += qc->pad_len;
1da177e4 2821 return -1;
537a95d9 2822 }
1da177e4
LT
2823
2824 sg_dma_address(sg) = dma_address;
e1410f2d 2825skip_map:
32529e01 2826 sg_dma_len(sg) = sg->length;
1da177e4
LT
2827
2828 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2829 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2830
2831 return 0;
2832}
2833
2834/**
0cba632b
JG
2835 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2836 * @qc: Command with scatter-gather table to be mapped.
2837 *
2838 * DMA-map the scatter-gather table associated with queued_cmd @qc.
1da177e4
LT
2839 *
2840 * LOCKING:
2841 * spin_lock_irqsave(host_set lock)
2842 *
2843 * RETURNS:
0cba632b 2844 * Zero on success, negative on error.
1da177e4
LT
2845 *
2846 */
2847
2848static int ata_sg_setup(struct ata_queued_cmd *qc)
2849{
2850 struct ata_port *ap = qc->ap;
cedc9a47
JG
2851 struct scatterlist *sg = qc->__sg;
2852 struct scatterlist *lsg = &sg[qc->n_elem - 1];
e1410f2d 2853 int n_elem, pre_n_elem, dir, trim_sg = 0;
1da177e4
LT
2854
2855 VPRINTK("ENTER, ata%u\n", ap->id);
a4631474 2856 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
1da177e4 2857
cedc9a47
JG
2858 /* we must lengthen transfers to end on a 32-bit boundary */
2859 qc->pad_len = lsg->length & 3;
2860 if (qc->pad_len) {
2861 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2862 struct scatterlist *psg = &qc->pad_sgent;
2863 unsigned int offset;
2864
a4631474 2865 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
cedc9a47
JG
2866
2867 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2868
2869 /*
2870 * psg->page/offset are used to copy to-be-written
2871 * data in this function or read data in ata_sg_clean.
2872 */
2873 offset = lsg->offset + lsg->length - qc->pad_len;
2874 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2875 psg->offset = offset_in_page(offset);
2876
2877 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2878 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2879 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
dfa15988 2880 kunmap_atomic(addr, KM_IRQ0);
cedc9a47
JG
2881 }
2882
2883 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2884 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2885 /* trim last sg */
2886 lsg->length -= qc->pad_len;
e1410f2d
JG
2887 if (lsg->length == 0)
2888 trim_sg = 1;
cedc9a47
JG
2889
2890 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2891 qc->n_elem - 1, lsg->length, qc->pad_len);
2892 }
2893
e1410f2d
JG
2894 pre_n_elem = qc->n_elem;
2895 if (trim_sg && pre_n_elem)
2896 pre_n_elem--;
2897
2898 if (!pre_n_elem) {
2899 n_elem = 0;
2900 goto skip_map;
2901 }
2902
1da177e4 2903 dir = qc->dma_dir;
e1410f2d 2904 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
537a95d9
TH
2905 if (n_elem < 1) {
2906 /* restore last sg */
2907 lsg->length += qc->pad_len;
1da177e4 2908 return -1;
537a95d9 2909 }
1da177e4
LT
2910
2911 DPRINTK("%d sg elements mapped\n", n_elem);
2912
e1410f2d 2913skip_map:
1da177e4
LT
2914 qc->n_elem = n_elem;
2915
2916 return 0;
2917}
2918
40e8c82c
TH
2919/**
2920 * ata_poll_qc_complete - turn irq back on and finish qc
2921 * @qc: Command to complete
8e8b77dd 2922 * @err_mask: ATA status register content
40e8c82c
TH
2923 *
2924 * LOCKING:
2925 * None. (grabs host lock)
2926 */
2927
a22e2eb0 2928void ata_poll_qc_complete(struct ata_queued_cmd *qc)
40e8c82c
TH
2929{
2930 struct ata_port *ap = qc->ap;
b8f6153e 2931 unsigned long flags;
40e8c82c 2932
b8f6153e 2933 spin_lock_irqsave(&ap->host_set->lock, flags);
40e8c82c
TH
2934 ap->flags &= ~ATA_FLAG_NOINTR;
2935 ata_irq_on(ap);
a22e2eb0 2936 ata_qc_complete(qc);
b8f6153e 2937 spin_unlock_irqrestore(&ap->host_set->lock, flags);
40e8c82c
TH
2938}
2939
1da177e4 2940/**
c893a3ae 2941 * ata_pio_poll - poll using PIO, depending on current state
6f0ef4fa 2942 * @ap: the target ata_port
1da177e4
LT
2943 *
2944 * LOCKING:
0cba632b 2945 * None. (executing in kernel thread context)
1da177e4
LT
2946 *
2947 * RETURNS:
6f0ef4fa 2948 * timeout value to use
1da177e4
LT
2949 */
2950
2951static unsigned long ata_pio_poll(struct ata_port *ap)
2952{
c14b8331 2953 struct ata_queued_cmd *qc;
1da177e4 2954 u8 status;
14be71f4
AL
2955 unsigned int poll_state = HSM_ST_UNKNOWN;
2956 unsigned int reg_state = HSM_ST_UNKNOWN;
14be71f4 2957
c14b8331 2958 qc = ata_qc_from_tag(ap, ap->active_tag);
a4631474 2959 WARN_ON(qc == NULL);
c14b8331 2960
14be71f4
AL
2961 switch (ap->hsm_task_state) {
2962 case HSM_ST:
2963 case HSM_ST_POLL:
2964 poll_state = HSM_ST_POLL;
2965 reg_state = HSM_ST;
1da177e4 2966 break;
14be71f4
AL
2967 case HSM_ST_LAST:
2968 case HSM_ST_LAST_POLL:
2969 poll_state = HSM_ST_LAST_POLL;
2970 reg_state = HSM_ST_LAST;
1da177e4
LT
2971 break;
2972 default:
2973 BUG();
2974 break;
2975 }
2976
2977 status = ata_chk_status(ap);
2978 if (status & ATA_BUSY) {
2979 if (time_after(jiffies, ap->pio_task_timeout)) {
11a56d24 2980 qc->err_mask |= AC_ERR_TIMEOUT;
7c398335 2981 ap->hsm_task_state = HSM_ST_TMOUT;
1da177e4
LT
2982 return 0;
2983 }
14be71f4 2984 ap->hsm_task_state = poll_state;
1da177e4
LT
2985 return ATA_SHORT_PAUSE;
2986 }
2987
14be71f4 2988 ap->hsm_task_state = reg_state;
1da177e4
LT
2989 return 0;
2990}
2991
2992/**
6f0ef4fa
RD
2993 * ata_pio_complete - check if drive is busy or idle
2994 * @ap: the target ata_port
1da177e4
LT
2995 *
2996 * LOCKING:
0cba632b 2997 * None. (executing in kernel thread context)
7fb6ec28
JG
2998 *
2999 * RETURNS:
3000 * Non-zero if qc completed, zero otherwise.
1da177e4
LT
3001 */
3002
7fb6ec28 3003static int ata_pio_complete (struct ata_port *ap)
1da177e4
LT
3004{
3005 struct ata_queued_cmd *qc;
3006 u8 drv_stat;
3007
3008 /*
31433ea3
AC
3009 * This is purely heuristic. This is a fast path. Sometimes when
3010 * we enter, BSY will be cleared in a chk-status or two. If not,
3011 * the drive is probably seeking or something. Snooze for a couple
3012 * msecs, then chk-status again. If still busy, fall back to
14be71f4 3013 * HSM_ST_POLL state.
1da177e4 3014 */
fe79e683
AL
3015 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3016 if (drv_stat & ATA_BUSY) {
1da177e4 3017 msleep(2);
fe79e683
AL
3018 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3019 if (drv_stat & ATA_BUSY) {
14be71f4 3020 ap->hsm_task_state = HSM_ST_LAST_POLL;
1da177e4 3021 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
7fb6ec28 3022 return 0;
1da177e4
LT
3023 }
3024 }
3025
c14b8331 3026 qc = ata_qc_from_tag(ap, ap->active_tag);
a4631474 3027 WARN_ON(qc == NULL);
c14b8331 3028
1da177e4
LT
3029 drv_stat = ata_wait_idle(ap);
3030 if (!ata_ok(drv_stat)) {
1c848984 3031 qc->err_mask |= __ac_err_mask(drv_stat);
14be71f4 3032 ap->hsm_task_state = HSM_ST_ERR;
7fb6ec28 3033 return 0;
1da177e4
LT
3034 }
3035
14be71f4 3036 ap->hsm_task_state = HSM_ST_IDLE;
1da177e4 3037
a4631474 3038 WARN_ON(qc->err_mask);
a22e2eb0 3039 ata_poll_qc_complete(qc);
7fb6ec28
JG
3040
3041 /* another command may start at this point */
3042
3043 return 1;
1da177e4
LT
3044}
3045
0baab86b
EF
3046
3047/**
c893a3ae 3048 * swap_buf_le16 - swap halves of 16-bit words in place
0baab86b
EF
3049 * @buf: Buffer to swap
3050 * @buf_words: Number of 16-bit words in buffer.
3051 *
3052 * Swap halves of 16-bit words if needed to convert from
3053 * little-endian byte order to native cpu byte order, or
3054 * vice-versa.
3055 *
3056 * LOCKING:
6f0ef4fa 3057 * Inherited from caller.
0baab86b 3058 */
1da177e4
LT
3059void swap_buf_le16(u16 *buf, unsigned int buf_words)
3060{
3061#ifdef __BIG_ENDIAN
3062 unsigned int i;
3063
3064 for (i = 0; i < buf_words; i++)
3065 buf[i] = le16_to_cpu(buf[i]);
3066#endif /* __BIG_ENDIAN */
3067}
3068
6ae4cfb5
AL
3069/**
3070 * ata_mmio_data_xfer - Transfer data by MMIO
3071 * @ap: port to read/write
3072 * @buf: data buffer
3073 * @buflen: buffer length
344babaa 3074 * @write_data: read/write
6ae4cfb5
AL
3075 *
3076 * Transfer data from/to the device data register by MMIO.
3077 *
3078 * LOCKING:
3079 * Inherited from caller.
6ae4cfb5
AL
3080 */
3081
1da177e4
LT
3082static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3083 unsigned int buflen, int write_data)
3084{
3085 unsigned int i;
3086 unsigned int words = buflen >> 1;
3087 u16 *buf16 = (u16 *) buf;
3088 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3089
6ae4cfb5 3090 /* Transfer multiple of 2 bytes */
1da177e4
LT
3091 if (write_data) {
3092 for (i = 0; i < words; i++)
3093 writew(le16_to_cpu(buf16[i]), mmio);
3094 } else {
3095 for (i = 0; i < words; i++)
3096 buf16[i] = cpu_to_le16(readw(mmio));
3097 }
6ae4cfb5
AL
3098
3099 /* Transfer trailing 1 byte, if any. */
3100 if (unlikely(buflen & 0x01)) {
3101 u16 align_buf[1] = { 0 };
3102 unsigned char *trailing_buf = buf + buflen - 1;
3103
3104 if (write_data) {
3105 memcpy(align_buf, trailing_buf, 1);
3106 writew(le16_to_cpu(align_buf[0]), mmio);
3107 } else {
3108 align_buf[0] = cpu_to_le16(readw(mmio));
3109 memcpy(trailing_buf, align_buf, 1);
3110 }
3111 }
1da177e4
LT
3112}
3113
6ae4cfb5
AL
3114/**
3115 * ata_pio_data_xfer - Transfer data by PIO
3116 * @ap: port to read/write
3117 * @buf: data buffer
3118 * @buflen: buffer length
344babaa 3119 * @write_data: read/write
6ae4cfb5
AL
3120 *
3121 * Transfer data from/to the device data register by PIO.
3122 *
3123 * LOCKING:
3124 * Inherited from caller.
6ae4cfb5
AL
3125 */
3126
1da177e4
LT
3127static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3128 unsigned int buflen, int write_data)
3129{
6ae4cfb5 3130 unsigned int words = buflen >> 1;
1da177e4 3131
6ae4cfb5 3132 /* Transfer multiple of 2 bytes */
1da177e4 3133 if (write_data)
6ae4cfb5 3134 outsw(ap->ioaddr.data_addr, buf, words);
1da177e4 3135 else
6ae4cfb5
AL
3136 insw(ap->ioaddr.data_addr, buf, words);
3137
3138 /* Transfer trailing 1 byte, if any. */
3139 if (unlikely(buflen & 0x01)) {
3140 u16 align_buf[1] = { 0 };
3141 unsigned char *trailing_buf = buf + buflen - 1;
3142
3143 if (write_data) {
3144 memcpy(align_buf, trailing_buf, 1);
3145 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3146 } else {
3147 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3148 memcpy(trailing_buf, align_buf, 1);
3149 }
3150 }
1da177e4
LT
3151}
3152
6ae4cfb5
AL
3153/**
3154 * ata_data_xfer - Transfer data from/to the data register.
3155 * @ap: port to read/write
3156 * @buf: data buffer
3157 * @buflen: buffer length
3158 * @do_write: read/write
3159 *
3160 * Transfer data from/to the device data register.
3161 *
3162 * LOCKING:
3163 * Inherited from caller.
6ae4cfb5
AL
3164 */
3165
1da177e4
LT
3166static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3167 unsigned int buflen, int do_write)
3168{
a1bd9e68
AC
3169 /* Make the crap hardware pay the costs not the good stuff */
3170 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3171 unsigned long flags;
3172 local_irq_save(flags);
3173 if (ap->flags & ATA_FLAG_MMIO)
3174 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3175 else
3176 ata_pio_data_xfer(ap, buf, buflen, do_write);
3177 local_irq_restore(flags);
3178 } else {
3179 if (ap->flags & ATA_FLAG_MMIO)
3180 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3181 else
3182 ata_pio_data_xfer(ap, buf, buflen, do_write);
3183 }
1da177e4
LT
3184}
3185
6ae4cfb5
AL
3186/**
3187 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3188 * @qc: Command on going
3189 *
3190 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3191 *
3192 * LOCKING:
3193 * Inherited from caller.
3194 */
3195
1da177e4
LT
3196static void ata_pio_sector(struct ata_queued_cmd *qc)
3197{
3198 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
cedc9a47 3199 struct scatterlist *sg = qc->__sg;
1da177e4
LT
3200 struct ata_port *ap = qc->ap;
3201 struct page *page;
3202 unsigned int offset;
3203 unsigned char *buf;
3204
3205 if (qc->cursect == (qc->nsect - 1))
14be71f4 3206 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
3207
3208 page = sg[qc->cursg].page;
3209 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3210
3211 /* get the current page and offset */
3212 page = nth_page(page, (offset >> PAGE_SHIFT));
3213 offset %= PAGE_SIZE;
3214
3215 buf = kmap(page) + offset;
3216
3217 qc->cursect++;
3218 qc->cursg_ofs++;
3219
32529e01 3220 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
1da177e4
LT
3221 qc->cursg++;
3222 qc->cursg_ofs = 0;
3223 }
3224
3225 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3226
3227 /* do the actual data transfer */
3228 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3229 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3230
3231 kunmap(page);
3232}
3233
6ae4cfb5
AL
3234/**
3235 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3236 * @qc: Command on going
3237 * @bytes: number of bytes
3238 *
3239 * Transfer Transfer data from/to the ATAPI device.
3240 *
3241 * LOCKING:
3242 * Inherited from caller.
3243 *
3244 */
3245
1da177e4
LT
3246static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3247{
3248 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
cedc9a47 3249 struct scatterlist *sg = qc->__sg;
1da177e4
LT
3250 struct ata_port *ap = qc->ap;
3251 struct page *page;
3252 unsigned char *buf;
3253 unsigned int offset, count;
3254
563a6e1f 3255 if (qc->curbytes + bytes >= qc->nbytes)
14be71f4 3256 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
3257
3258next_sg:
563a6e1f 3259 if (unlikely(qc->cursg >= qc->n_elem)) {
7fb6ec28 3260 /*
563a6e1f
AL
3261 * The end of qc->sg is reached and the device expects
3262 * more data to transfer. In order not to overrun qc->sg
3263 * and fulfill length specified in the byte count register,
3264 * - for read case, discard trailing data from the device
3265 * - for write case, padding zero data to the device
3266 */
3267 u16 pad_buf[1] = { 0 };
3268 unsigned int words = bytes >> 1;
3269 unsigned int i;
3270
3271 if (words) /* warning if bytes > 1 */
7fb6ec28 3272 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
563a6e1f
AL
3273 ap->id, bytes);
3274
3275 for (i = 0; i < words; i++)
3276 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3277
14be71f4 3278 ap->hsm_task_state = HSM_ST_LAST;
563a6e1f
AL
3279 return;
3280 }
3281
cedc9a47 3282 sg = &qc->__sg[qc->cursg];
1da177e4 3283
1da177e4
LT
3284 page = sg->page;
3285 offset = sg->offset + qc->cursg_ofs;
3286
3287 /* get the current page and offset */
3288 page = nth_page(page, (offset >> PAGE_SHIFT));
3289 offset %= PAGE_SIZE;
3290
6952df03 3291 /* don't overrun current sg */
32529e01 3292 count = min(sg->length - qc->cursg_ofs, bytes);
1da177e4
LT
3293
3294 /* don't cross page boundaries */
3295 count = min(count, (unsigned int)PAGE_SIZE - offset);
3296
3297 buf = kmap(page) + offset;
3298
3299 bytes -= count;
3300 qc->curbytes += count;
3301 qc->cursg_ofs += count;
3302
32529e01 3303 if (qc->cursg_ofs == sg->length) {
1da177e4
LT
3304 qc->cursg++;
3305 qc->cursg_ofs = 0;
3306 }
3307
3308 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3309
3310 /* do the actual data transfer */
3311 ata_data_xfer(ap, buf, count, do_write);
3312
3313 kunmap(page);
3314
563a6e1f 3315 if (bytes)
1da177e4 3316 goto next_sg;
1da177e4
LT
3317}
3318
6ae4cfb5
AL
3319/**
3320 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3321 * @qc: Command on going
3322 *
3323 * Transfer Transfer data from/to the ATAPI device.
3324 *
3325 * LOCKING:
3326 * Inherited from caller.
6ae4cfb5
AL
3327 */
3328
1da177e4
LT
3329static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3330{
3331 struct ata_port *ap = qc->ap;
3332 struct ata_device *dev = qc->dev;
3333 unsigned int ireason, bc_lo, bc_hi, bytes;
3334 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3335
3336 ap->ops->tf_read(ap, &qc->tf);
3337 ireason = qc->tf.nsect;
3338 bc_lo = qc->tf.lbam;
3339 bc_hi = qc->tf.lbah;
3340 bytes = (bc_hi << 8) | bc_lo;
3341
3342 /* shall be cleared to zero, indicating xfer of data */
3343 if (ireason & (1 << 0))
3344 goto err_out;
3345
3346 /* make sure transfer direction matches expected */
3347 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3348 if (do_write != i_write)
3349 goto err_out;
3350
3351 __atapi_pio_bytes(qc, bytes);
3352
3353 return;
3354
3355err_out:
3356 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3357 ap->id, dev->devno);
11a56d24 3358 qc->err_mask |= AC_ERR_HSM;
14be71f4 3359 ap->hsm_task_state = HSM_ST_ERR;
1da177e4
LT
3360}
3361
3362/**
6f0ef4fa
RD
3363 * ata_pio_block - start PIO on a block
3364 * @ap: the target ata_port
1da177e4
LT
3365 *
3366 * LOCKING:
0cba632b 3367 * None. (executing in kernel thread context)
1da177e4
LT
3368 */
3369
3370static void ata_pio_block(struct ata_port *ap)
3371{
3372 struct ata_queued_cmd *qc;
3373 u8 status;
3374
3375 /*
6f0ef4fa 3376 * This is purely heuristic. This is a fast path.
1da177e4
LT
3377 * Sometimes when we enter, BSY will be cleared in
3378 * a chk-status or two. If not, the drive is probably seeking
3379 * or something. Snooze for a couple msecs, then
3380 * chk-status again. If still busy, fall back to
14be71f4 3381 * HSM_ST_POLL state.
1da177e4
LT
3382 */
3383 status = ata_busy_wait(ap, ATA_BUSY, 5);
3384 if (status & ATA_BUSY) {
3385 msleep(2);
3386 status = ata_busy_wait(ap, ATA_BUSY, 10);
3387 if (status & ATA_BUSY) {
14be71f4 3388 ap->hsm_task_state = HSM_ST_POLL;
1da177e4
LT
3389 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3390 return;
3391 }
3392 }
3393
3394 qc = ata_qc_from_tag(ap, ap->active_tag);
a4631474 3395 WARN_ON(qc == NULL);
1da177e4 3396
fe79e683
AL
3397 /* check error */
3398 if (status & (ATA_ERR | ATA_DF)) {
3399 qc->err_mask |= AC_ERR_DEV;
3400 ap->hsm_task_state = HSM_ST_ERR;
3401 return;
3402 }
3403
3404 /* transfer data if any */
1da177e4 3405 if (is_atapi_taskfile(&qc->tf)) {
fe79e683 3406 /* DRQ=0 means no more data to transfer */
1da177e4 3407 if ((status & ATA_DRQ) == 0) {
14be71f4 3408 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
3409 return;
3410 }
3411
3412 atapi_pio_bytes(qc);
3413 } else {
3414 /* handle BSY=0, DRQ=0 as error */
3415 if ((status & ATA_DRQ) == 0) {
11a56d24 3416 qc->err_mask |= AC_ERR_HSM;
14be71f4 3417 ap->hsm_task_state = HSM_ST_ERR;
1da177e4
LT
3418 return;
3419 }
3420
3421 ata_pio_sector(qc);
3422 }
3423}
3424
3425static void ata_pio_error(struct ata_port *ap)
3426{
3427 struct ata_queued_cmd *qc;
a7dac447 3428
1da177e4 3429 qc = ata_qc_from_tag(ap, ap->active_tag);
a4631474 3430 WARN_ON(qc == NULL);
1da177e4 3431
0565c26d
AL
3432 if (qc->tf.command != ATA_CMD_PACKET)
3433 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3434
1c848984
AL
3435 /* make sure qc->err_mask is available to
3436 * know what's wrong and recover
3437 */
a4631474 3438 WARN_ON(qc->err_mask == 0);
1c848984 3439
14be71f4 3440 ap->hsm_task_state = HSM_ST_IDLE;
1da177e4 3441
a22e2eb0 3442 ata_poll_qc_complete(qc);
1da177e4
LT
3443}
3444
3445static void ata_pio_task(void *_data)
3446{
3447 struct ata_port *ap = _data;
7fb6ec28
JG
3448 unsigned long timeout;
3449 int qc_completed;
3450
3451fsm_start:
3452 timeout = 0;
3453 qc_completed = 0;
1da177e4 3454
14be71f4
AL
3455 switch (ap->hsm_task_state) {
3456 case HSM_ST_IDLE:
1da177e4
LT
3457 return;
3458
14be71f4 3459 case HSM_ST:
1da177e4
LT
3460 ata_pio_block(ap);
3461 break;
3462
14be71f4 3463 case HSM_ST_LAST:
7fb6ec28 3464 qc_completed = ata_pio_complete(ap);
1da177e4
LT
3465 break;
3466
14be71f4
AL
3467 case HSM_ST_POLL:
3468 case HSM_ST_LAST_POLL:
1da177e4
LT
3469 timeout = ata_pio_poll(ap);
3470 break;
3471
14be71f4
AL
3472 case HSM_ST_TMOUT:
3473 case HSM_ST_ERR:
1da177e4
LT
3474 ata_pio_error(ap);
3475 return;
3476 }
3477
3478 if (timeout)
95064379 3479 ata_queue_delayed_pio_task(ap, timeout);
7fb6ec28
JG
3480 else if (!qc_completed)
3481 goto fsm_start;
1da177e4
LT
3482}
3483
1da177e4
LT
3484/**
3485 * ata_qc_timeout - Handle timeout of queued command
3486 * @qc: Command that timed out
3487 *
3488 * Some part of the kernel (currently, only the SCSI layer)
3489 * has noticed that the active command on port @ap has not
3490 * completed after a specified length of time. Handle this
3491 * condition by disabling DMA (if necessary) and completing
3492 * transactions, with error if necessary.
3493 *
3494 * This also handles the case of the "lost interrupt", where
3495 * for some reason (possibly hardware bug, possibly driver bug)
3496 * an interrupt was not delivered to the driver, even though the
3497 * transaction completed successfully.
3498 *
3499 * LOCKING:
0cba632b 3500 * Inherited from SCSI layer (none, can sleep)
1da177e4
LT
3501 */
3502
3503static void ata_qc_timeout(struct ata_queued_cmd *qc)
3504{
3505 struct ata_port *ap = qc->ap;
b8f6153e 3506 struct ata_host_set *host_set = ap->host_set;
1da177e4 3507 u8 host_stat = 0, drv_stat;
b8f6153e 3508 unsigned long flags;
1da177e4
LT
3509
3510 DPRINTK("ENTER\n");
3511
c18d06f8
TH
3512 ata_flush_pio_tasks(ap);
3513 ap->hsm_task_state = HSM_ST_IDLE;
3514
b8f6153e
JG
3515 spin_lock_irqsave(&host_set->lock, flags);
3516
1da177e4
LT
3517 switch (qc->tf.protocol) {
3518
3519 case ATA_PROT_DMA:
3520 case ATA_PROT_ATAPI_DMA:
3521 host_stat = ap->ops->bmdma_status(ap);
3522
3523 /* before we do anything else, clear DMA-Start bit */
b73fc89f 3524 ap->ops->bmdma_stop(qc);
1da177e4
LT
3525
3526 /* fall through */
3527
3528 default:
3529 ata_altstatus(ap);
3530 drv_stat = ata_chk_status(ap);
3531
3532 /* ack bmdma irq events */
3533 ap->ops->irq_clear(ap);
3534
3535 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3536 ap->id, qc->tf.command, drv_stat, host_stat);
3537
3538 /* complete taskfile transaction */
a22e2eb0 3539 qc->err_mask |= ac_err_mask(drv_stat);
1da177e4
LT
3540 break;
3541 }
b8f6153e
JG
3542
3543 spin_unlock_irqrestore(&host_set->lock, flags);
3544
a72ec4ce
TH
3545 ata_eh_qc_complete(qc);
3546
1da177e4
LT
3547 DPRINTK("EXIT\n");
3548}
3549
3550/**
3551 * ata_eng_timeout - Handle timeout of queued command
3552 * @ap: Port on which timed-out command is active
3553 *
3554 * Some part of the kernel (currently, only the SCSI layer)
3555 * has noticed that the active command on port @ap has not
3556 * completed after a specified length of time. Handle this
3557 * condition by disabling DMA (if necessary) and completing
3558 * transactions, with error if necessary.
3559 *
3560 * This also handles the case of the "lost interrupt", where
3561 * for some reason (possibly hardware bug, possibly driver bug)
3562 * an interrupt was not delivered to the driver, even though the
3563 * transaction completed successfully.
3564 *
3565 * LOCKING:
3566 * Inherited from SCSI layer (none, can sleep)
3567 */
3568
3569void ata_eng_timeout(struct ata_port *ap)
3570{
1da177e4
LT
3571 DPRINTK("ENTER\n");
3572
f6379020 3573 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
1da177e4 3574
1da177e4
LT
3575 DPRINTK("EXIT\n");
3576}
3577
3578/**
3579 * ata_qc_new - Request an available ATA command, for queueing
3580 * @ap: Port associated with device @dev
3581 * @dev: Device from whom we request an available command structure
3582 *
3583 * LOCKING:
0cba632b 3584 * None.
1da177e4
LT
3585 */
3586
3587static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3588{
3589 struct ata_queued_cmd *qc = NULL;
3590 unsigned int i;
3591
3592 for (i = 0; i < ATA_MAX_QUEUE; i++)
3593 if (!test_and_set_bit(i, &ap->qactive)) {
3594 qc = ata_qc_from_tag(ap, i);
3595 break;
3596 }
3597
3598 if (qc)
3599 qc->tag = i;
3600
3601 return qc;
3602}
3603
3604/**
3605 * ata_qc_new_init - Request an available ATA command, and initialize it
3606 * @ap: Port associated with device @dev
3607 * @dev: Device from whom we request an available command structure
3608 *
3609 * LOCKING:
0cba632b 3610 * None.
1da177e4
LT
3611 */
3612
3613struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3614 struct ata_device *dev)
3615{
3616 struct ata_queued_cmd *qc;
3617
3618 qc = ata_qc_new(ap);
3619 if (qc) {
1da177e4
LT
3620 qc->scsicmd = NULL;
3621 qc->ap = ap;
3622 qc->dev = dev;
1da177e4 3623
2c13b7ce 3624 ata_qc_reinit(qc);
1da177e4
LT
3625 }
3626
3627 return qc;
3628}
3629
1da177e4
LT
3630/**
3631 * ata_qc_free - free unused ata_queued_cmd
3632 * @qc: Command to complete
3633 *
3634 * Designed to free unused ata_queued_cmd object
3635 * in case something prevents using it.
3636 *
3637 * LOCKING:
0cba632b 3638 * spin_lock_irqsave(host_set lock)
1da177e4
LT
3639 */
3640void ata_qc_free(struct ata_queued_cmd *qc)
3641{
4ba946e9
TH
3642 struct ata_port *ap = qc->ap;
3643 unsigned int tag;
3644
a4631474 3645 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
1da177e4 3646
4ba946e9
TH
3647 qc->flags = 0;
3648 tag = qc->tag;
3649 if (likely(ata_tag_valid(tag))) {
3650 if (tag == ap->active_tag)
3651 ap->active_tag = ATA_TAG_POISON;
3652 qc->tag = ATA_TAG_POISON;
3653 clear_bit(tag, &ap->qactive);
3654 }
1da177e4
LT
3655}
3656
76014427 3657void __ata_qc_complete(struct ata_queued_cmd *qc)
1da177e4 3658{
a4631474
TH
3659 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3660 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
1da177e4
LT
3661
3662 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3663 ata_sg_clean(qc);
3664
3f3791d3
AL
3665 /* atapi: mark qc as inactive to prevent the interrupt handler
3666 * from completing the command twice later, before the error handler
3667 * is called. (when rc != 0 and atapi request sense is needed)
3668 */
3669 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3670
1da177e4 3671 /* call completion callback */
77853bf2 3672 qc->complete_fn(qc);
1da177e4
LT
3673}
3674
3675static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3676{
3677 struct ata_port *ap = qc->ap;
3678
3679 switch (qc->tf.protocol) {
3680 case ATA_PROT_DMA:
3681 case ATA_PROT_ATAPI_DMA:
3682 return 1;
3683
3684 case ATA_PROT_ATAPI:
3685 case ATA_PROT_PIO:
3686 case ATA_PROT_PIO_MULT:
3687 if (ap->flags & ATA_FLAG_PIO_DMA)
3688 return 1;
3689
3690 /* fall through */
3691
3692 default:
3693 return 0;
3694 }
3695
3696 /* never reached */
3697}
3698
3699/**
3700 * ata_qc_issue - issue taskfile to device
3701 * @qc: command to issue to device
3702 *
3703 * Prepare an ATA command to submission to device.
3704 * This includes mapping the data into a DMA-able
3705 * area, filling in the S/G table, and finally
3706 * writing the taskfile to hardware, starting the command.
3707 *
3708 * LOCKING:
3709 * spin_lock_irqsave(host_set lock)
3710 *
3711 * RETURNS:
9a3d9eb0 3712 * Zero on success, AC_ERR_* mask on failure
1da177e4
LT
3713 */
3714
9a3d9eb0 3715unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
1da177e4
LT
3716{
3717 struct ata_port *ap = qc->ap;
3718
3719 if (ata_should_dma_map(qc)) {
3720 if (qc->flags & ATA_QCFLAG_SG) {
3721 if (ata_sg_setup(qc))
8e436af9 3722 goto sg_err;
1da177e4
LT
3723 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3724 if (ata_sg_setup_one(qc))
8e436af9 3725 goto sg_err;
1da177e4
LT
3726 }
3727 } else {
3728 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3729 }
3730
3731 ap->ops->qc_prep(qc);
3732
3733 qc->ap->active_tag = qc->tag;
3734 qc->flags |= ATA_QCFLAG_ACTIVE;
3735
3736 return ap->ops->qc_issue(qc);
3737
8e436af9
TH
3738sg_err:
3739 qc->flags &= ~ATA_QCFLAG_DMAMAP;
9a3d9eb0 3740 return AC_ERR_SYSTEM;
1da177e4
LT
3741}
3742
0baab86b 3743
1da177e4
LT
3744/**
3745 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3746 * @qc: command to issue to device
3747 *
3748 * Using various libata functions and hooks, this function
3749 * starts an ATA command. ATA commands are grouped into
3750 * classes called "protocols", and issuing each type of protocol
3751 * is slightly different.
3752 *
0baab86b
EF
3753 * May be used as the qc_issue() entry in ata_port_operations.
3754 *
1da177e4
LT
3755 * LOCKING:
3756 * spin_lock_irqsave(host_set lock)
3757 *
3758 * RETURNS:
9a3d9eb0 3759 * Zero on success, AC_ERR_* mask on failure
1da177e4
LT
3760 */
3761
9a3d9eb0 3762unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
1da177e4
LT
3763{
3764 struct ata_port *ap = qc->ap;
3765
3766 ata_dev_select(ap, qc->dev->devno, 1, 0);
3767
3768 switch (qc->tf.protocol) {
3769 case ATA_PROT_NODATA:
e5338254 3770 ata_tf_to_host(ap, &qc->tf);
1da177e4
LT
3771 break;
3772
3773 case ATA_PROT_DMA:
3774 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3775 ap->ops->bmdma_setup(qc); /* set up bmdma */
3776 ap->ops->bmdma_start(qc); /* initiate bmdma */
3777 break;
3778
3779 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3780 ata_qc_set_polling(qc);
e5338254 3781 ata_tf_to_host(ap, &qc->tf);
14be71f4 3782 ap->hsm_task_state = HSM_ST;
95064379 3783 ata_queue_pio_task(ap);
1da177e4
LT
3784 break;
3785
3786 case ATA_PROT_ATAPI:
3787 ata_qc_set_polling(qc);
e5338254 3788 ata_tf_to_host(ap, &qc->tf);
95064379 3789 ata_queue_packet_task(ap);
1da177e4
LT
3790 break;
3791
3792 case ATA_PROT_ATAPI_NODATA:
c1389503 3793 ap->flags |= ATA_FLAG_NOINTR;
e5338254 3794 ata_tf_to_host(ap, &qc->tf);
95064379 3795 ata_queue_packet_task(ap);
1da177e4
LT
3796 break;
3797
3798 case ATA_PROT_ATAPI_DMA:
c1389503 3799 ap->flags |= ATA_FLAG_NOINTR;
1da177e4
LT
3800 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3801 ap->ops->bmdma_setup(qc); /* set up bmdma */
95064379 3802 ata_queue_packet_task(ap);
1da177e4
LT
3803 break;
3804
3805 default:
3806 WARN_ON(1);
9a3d9eb0 3807 return AC_ERR_SYSTEM;
1da177e4
LT
3808 }
3809
3810 return 0;
3811}
3812
3813/**
0baab86b 3814 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
1da177e4
LT
3815 * @qc: Info associated with this ATA transaction.
3816 *
3817 * LOCKING:
3818 * spin_lock_irqsave(host_set lock)
3819 */
3820
3821static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3822{
3823 struct ata_port *ap = qc->ap;
3824 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3825 u8 dmactl;
3826 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3827
3828 /* load PRD table addr. */
3829 mb(); /* make sure PRD table writes are visible to controller */
3830 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3831
3832 /* specify data direction, triple-check start bit is clear */
3833 dmactl = readb(mmio + ATA_DMA_CMD);
3834 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3835 if (!rw)
3836 dmactl |= ATA_DMA_WR;
3837 writeb(dmactl, mmio + ATA_DMA_CMD);
3838
3839 /* issue r/w command */
3840 ap->ops->exec_command(ap, &qc->tf);
3841}
3842
3843/**
b73fc89f 3844 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
1da177e4
LT
3845 * @qc: Info associated with this ATA transaction.
3846 *
3847 * LOCKING:
3848 * spin_lock_irqsave(host_set lock)
3849 */
3850
3851static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3852{
3853 struct ata_port *ap = qc->ap;
3854 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3855 u8 dmactl;
3856
3857 /* start host DMA transaction */
3858 dmactl = readb(mmio + ATA_DMA_CMD);
3859 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3860
3861 /* Strictly, one may wish to issue a readb() here, to
3862 * flush the mmio write. However, control also passes
3863 * to the hardware at this point, and it will interrupt
3864 * us when we are to resume control. So, in effect,
3865 * we don't care when the mmio write flushes.
3866 * Further, a read of the DMA status register _immediately_
3867 * following the write may not be what certain flaky hardware
3868 * is expected, so I think it is best to not add a readb()
3869 * without first all the MMIO ATA cards/mobos.
3870 * Or maybe I'm just being paranoid.
3871 */
3872}
3873
3874/**
3875 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3876 * @qc: Info associated with this ATA transaction.
3877 *
3878 * LOCKING:
3879 * spin_lock_irqsave(host_set lock)
3880 */
3881
3882static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3883{
3884 struct ata_port *ap = qc->ap;
3885 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3886 u8 dmactl;
3887
3888 /* load PRD table addr. */
3889 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3890
3891 /* specify data direction, triple-check start bit is clear */
3892 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3893 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3894 if (!rw)
3895 dmactl |= ATA_DMA_WR;
3896 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3897
3898 /* issue r/w command */
3899 ap->ops->exec_command(ap, &qc->tf);
3900}
3901
3902/**
3903 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3904 * @qc: Info associated with this ATA transaction.
3905 *
3906 * LOCKING:
3907 * spin_lock_irqsave(host_set lock)
3908 */
3909
3910static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3911{
3912 struct ata_port *ap = qc->ap;
3913 u8 dmactl;
3914
3915 /* start host DMA transaction */
3916 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3917 outb(dmactl | ATA_DMA_START,
3918 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3919}
3920
0baab86b
EF
3921
3922/**
3923 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3924 * @qc: Info associated with this ATA transaction.
3925 *
3926 * Writes the ATA_DMA_START flag to the DMA command register.
3927 *
3928 * May be used as the bmdma_start() entry in ata_port_operations.
3929 *
3930 * LOCKING:
3931 * spin_lock_irqsave(host_set lock)
3932 */
1da177e4
LT
3933void ata_bmdma_start(struct ata_queued_cmd *qc)
3934{
3935 if (qc->ap->flags & ATA_FLAG_MMIO)
3936 ata_bmdma_start_mmio(qc);
3937 else
3938 ata_bmdma_start_pio(qc);
3939}
3940
0baab86b
EF
3941
3942/**
3943 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3944 * @qc: Info associated with this ATA transaction.
3945 *
3946 * Writes address of PRD table to device's PRD Table Address
3947 * register, sets the DMA control register, and calls
3948 * ops->exec_command() to start the transfer.
3949 *
3950 * May be used as the bmdma_setup() entry in ata_port_operations.
3951 *
3952 * LOCKING:
3953 * spin_lock_irqsave(host_set lock)
3954 */
1da177e4
LT
3955void ata_bmdma_setup(struct ata_queued_cmd *qc)
3956{
3957 if (qc->ap->flags & ATA_FLAG_MMIO)
3958 ata_bmdma_setup_mmio(qc);
3959 else
3960 ata_bmdma_setup_pio(qc);
3961}
3962
0baab86b
EF
3963
3964/**
3965 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
decc6d0b 3966 * @ap: Port associated with this ATA transaction.
0baab86b
EF
3967 *
3968 * Clear interrupt and error flags in DMA status register.
3969 *
3970 * May be used as the irq_clear() entry in ata_port_operations.
3971 *
3972 * LOCKING:
3973 * spin_lock_irqsave(host_set lock)
3974 */
3975
1da177e4
LT
3976void ata_bmdma_irq_clear(struct ata_port *ap)
3977{
3978 if (ap->flags & ATA_FLAG_MMIO) {
3979 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3980 writeb(readb(mmio), mmio);
3981 } else {
3982 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3983 outb(inb(addr), addr);
3984 }
3985
3986}
3987
0baab86b
EF
3988
3989/**
3990 * ata_bmdma_status - Read PCI IDE BMDMA status
decc6d0b 3991 * @ap: Port associated with this ATA transaction.
0baab86b
EF
3992 *
3993 * Read and return BMDMA status register.
3994 *
3995 * May be used as the bmdma_status() entry in ata_port_operations.
3996 *
3997 * LOCKING:
3998 * spin_lock_irqsave(host_set lock)
3999 */
4000
1da177e4
LT
4001u8 ata_bmdma_status(struct ata_port *ap)
4002{
4003 u8 host_stat;
4004 if (ap->flags & ATA_FLAG_MMIO) {
4005 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4006 host_stat = readb(mmio + ATA_DMA_STATUS);
4007 } else
ee500aab 4008 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
1da177e4
LT
4009 return host_stat;
4010}
4011
0baab86b
EF
4012
4013/**
4014 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
b73fc89f 4015 * @qc: Command we are ending DMA for
0baab86b
EF
4016 *
4017 * Clears the ATA_DMA_START flag in the dma control register
4018 *
4019 * May be used as the bmdma_stop() entry in ata_port_operations.
4020 *
4021 * LOCKING:
4022 * spin_lock_irqsave(host_set lock)
4023 */
4024
b73fc89f 4025void ata_bmdma_stop(struct ata_queued_cmd *qc)
1da177e4 4026{
b73fc89f 4027 struct ata_port *ap = qc->ap;
1da177e4
LT
4028 if (ap->flags & ATA_FLAG_MMIO) {
4029 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4030
4031 /* clear start/stop bit */
4032 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4033 mmio + ATA_DMA_CMD);
4034 } else {
4035 /* clear start/stop bit */
4036 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4037 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4038 }
4039
4040 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4041 ata_altstatus(ap); /* dummy read */
4042}
4043
4044/**
4045 * ata_host_intr - Handle host interrupt for given (port, task)
4046 * @ap: Port on which interrupt arrived (possibly...)
4047 * @qc: Taskfile currently active in engine
4048 *
4049 * Handle host interrupt for given queued command. Currently,
4050 * only DMA interrupts are handled. All other commands are
4051 * handled via polling with interrupts disabled (nIEN bit).
4052 *
4053 * LOCKING:
4054 * spin_lock_irqsave(host_set lock)
4055 *
4056 * RETURNS:
4057 * One if interrupt was handled, zero if not (shared irq).
4058 */
4059
4060inline unsigned int ata_host_intr (struct ata_port *ap,
4061 struct ata_queued_cmd *qc)
4062{
4063 u8 status, host_stat;
4064
4065 switch (qc->tf.protocol) {
4066
4067 case ATA_PROT_DMA:
4068 case ATA_PROT_ATAPI_DMA:
4069 case ATA_PROT_ATAPI:
4070 /* check status of DMA engine */
4071 host_stat = ap->ops->bmdma_status(ap);
4072 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4073
4074 /* if it's not our irq... */
4075 if (!(host_stat & ATA_DMA_INTR))
4076 goto idle_irq;
4077
4078 /* before we do anything else, clear DMA-Start bit */
b73fc89f 4079 ap->ops->bmdma_stop(qc);
1da177e4
LT
4080
4081 /* fall through */
4082
4083 case ATA_PROT_ATAPI_NODATA:
4084 case ATA_PROT_NODATA:
4085 /* check altstatus */
4086 status = ata_altstatus(ap);
4087 if (status & ATA_BUSY)
4088 goto idle_irq;
4089
4090 /* check main status, clearing INTRQ */
4091 status = ata_chk_status(ap);
4092 if (unlikely(status & ATA_BUSY))
4093 goto idle_irq;
4094 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4095 ap->id, qc->tf.protocol, status);
4096
4097 /* ack bmdma irq events */
4098 ap->ops->irq_clear(ap);
4099
4100 /* complete taskfile transaction */
a22e2eb0
AL
4101 qc->err_mask |= ac_err_mask(status);
4102 ata_qc_complete(qc);
1da177e4
LT
4103 break;
4104
4105 default:
4106 goto idle_irq;
4107 }
4108
4109 return 1; /* irq handled */
4110
4111idle_irq:
4112 ap->stats.idle_irq++;
4113
4114#ifdef ATA_IRQ_TRAP
4115 if ((ap->stats.idle_irq % 1000) == 0) {
4116 handled = 1;
4117 ata_irq_ack(ap, 0); /* debug trap */
4118 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4119 }
4120#endif
4121 return 0; /* irq not handled */
4122}
4123
4124/**
4125 * ata_interrupt - Default ATA host interrupt handler
0cba632b
JG
4126 * @irq: irq line (unused)
4127 * @dev_instance: pointer to our ata_host_set information structure
1da177e4
LT
4128 * @regs: unused
4129 *
0cba632b
JG
4130 * Default interrupt handler for PCI IDE devices. Calls
4131 * ata_host_intr() for each port that is not disabled.
4132 *
1da177e4 4133 * LOCKING:
0cba632b 4134 * Obtains host_set lock during operation.
1da177e4
LT
4135 *
4136 * RETURNS:
0cba632b 4137 * IRQ_NONE or IRQ_HANDLED.
1da177e4
LT
4138 */
4139
4140irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4141{
4142 struct ata_host_set *host_set = dev_instance;
4143 unsigned int i;
4144 unsigned int handled = 0;
4145 unsigned long flags;
4146
4147 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4148 spin_lock_irqsave(&host_set->lock, flags);
4149
4150 for (i = 0; i < host_set->n_ports; i++) {
4151 struct ata_port *ap;
4152
4153 ap = host_set->ports[i];
c1389503
TH
4154 if (ap &&
4155 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
1da177e4
LT
4156 struct ata_queued_cmd *qc;
4157
4158 qc = ata_qc_from_tag(ap, ap->active_tag);
21b1ed74
AL
4159 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4160 (qc->flags & ATA_QCFLAG_ACTIVE))
1da177e4
LT
4161 handled |= ata_host_intr(ap, qc);
4162 }
4163 }
4164
4165 spin_unlock_irqrestore(&host_set->lock, flags);
4166
4167 return IRQ_RETVAL(handled);
4168}
4169
4170/**
4171 * atapi_packet_task - Write CDB bytes to hardware
4172 * @_data: Port to which ATAPI device is attached.
4173 *
4174 * When device has indicated its readiness to accept
4175 * a CDB, this function is called. Send the CDB.
4176 * If DMA is to be performed, exit immediately.
4177 * Otherwise, we are in polling mode, so poll
4178 * status under operation succeeds or fails.
4179 *
4180 * LOCKING:
4181 * Kernel thread context (may sleep)
4182 */
4183
4184static void atapi_packet_task(void *_data)
4185{
4186 struct ata_port *ap = _data;
4187 struct ata_queued_cmd *qc;
4188 u8 status;
4189
4190 qc = ata_qc_from_tag(ap, ap->active_tag);
a4631474
TH
4191 WARN_ON(qc == NULL);
4192 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
1da177e4
LT
4193
4194 /* sleep-wait for BSY to clear */
4195 DPRINTK("busy wait\n");
d8fe452b 4196 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
11a56d24 4197 qc->err_mask |= AC_ERR_TIMEOUT;
d8fe452b
AL
4198 goto err_out;
4199 }
1da177e4
LT
4200
4201 /* make sure DRQ is set */
4202 status = ata_chk_status(ap);
d8fe452b 4203 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
11a56d24 4204 qc->err_mask |= AC_ERR_HSM;
1da177e4 4205 goto err_out;
d8fe452b 4206 }
1da177e4
LT
4207
4208 /* send SCSI cdb */
4209 DPRINTK("send cdb\n");
6e7846e9 4210 WARN_ON(qc->dev->cdb_len < 12);
1da177e4 4211
c1389503
TH
4212 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4213 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4214 unsigned long flags;
1da177e4 4215
c1389503
TH
4216 /* Once we're done issuing command and kicking bmdma,
4217 * irq handler takes over. To not lose irq, we need
4218 * to clear NOINTR flag before sending cdb, but
4219 * interrupt handler shouldn't be invoked before we're
4220 * finished. Hence, the following locking.
4221 */
4222 spin_lock_irqsave(&ap->host_set->lock, flags);
4223 ap->flags &= ~ATA_FLAG_NOINTR;
6e7846e9 4224 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
c1389503
TH
4225 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4226 ap->ops->bmdma_start(qc); /* initiate bmdma */
4227 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4228 } else {
6e7846e9 4229 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
1da177e4 4230
c1389503 4231 /* PIO commands are handled by polling */
14be71f4 4232 ap->hsm_task_state = HSM_ST;
95064379 4233 ata_queue_pio_task(ap);
1da177e4
LT
4234 }
4235
4236 return;
4237
4238err_out:
a22e2eb0 4239 ata_poll_qc_complete(qc);
1da177e4
LT
4240}
4241
0baab86b 4242
9b847548
JA
4243/*
4244 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4245 * without filling any other registers
4246 */
4247static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4248 u8 cmd)
4249{
4250 struct ata_taskfile tf;
4251 int err;
4252
4253 ata_tf_init(ap, &tf, dev->devno);
4254
4255 tf.command = cmd;
4256 tf.flags |= ATA_TFLAG_DEVICE;
4257 tf.protocol = ATA_PROT_NODATA;
4258
4259 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4260 if (err)
4261 printk(KERN_ERR "%s: ata command failed: %d\n",
4262 __FUNCTION__, err);
4263
4264 return err;
4265}
4266
4267static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4268{
4269 u8 cmd;
4270
4271 if (!ata_try_flush_cache(dev))
4272 return 0;
4273
4274 if (ata_id_has_flush_ext(dev->id))
4275 cmd = ATA_CMD_FLUSH_EXT;
4276 else
4277 cmd = ATA_CMD_FLUSH;
4278
4279 return ata_do_simple_cmd(ap, dev, cmd);
4280}
4281
4282static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4283{
4284 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4285}
4286
4287static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4288{
4289 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4290}
4291
4292/**
4293 * ata_device_resume - wakeup a previously suspended devices
c893a3ae
RD
4294 * @ap: port the device is connected to
4295 * @dev: the device to resume
9b847548
JA
4296 *
4297 * Kick the drive back into action, by sending it an idle immediate
4298 * command and making sure its transfer mode matches between drive
4299 * and host.
4300 *
4301 */
4302int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4303{
4304 if (ap->flags & ATA_FLAG_SUSPENDED) {
4305 ap->flags &= ~ATA_FLAG_SUSPENDED;
4306 ata_set_mode(ap);
4307 }
4308 if (!ata_dev_present(dev))
4309 return 0;
4310 if (dev->class == ATA_DEV_ATA)
4311 ata_start_drive(ap, dev);
4312
4313 return 0;
4314}
4315
4316/**
4317 * ata_device_suspend - prepare a device for suspend
c893a3ae
RD
4318 * @ap: port the device is connected to
4319 * @dev: the device to suspend
9b847548
JA
4320 *
4321 * Flush the cache on the drive, if appropriate, then issue a
4322 * standbynow command.
9b847548
JA
4323 */
4324int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4325{
4326 if (!ata_dev_present(dev))
4327 return 0;
4328 if (dev->class == ATA_DEV_ATA)
4329 ata_flush_cache(ap, dev);
4330
4331 ata_standby_drive(ap, dev);
4332 ap->flags |= ATA_FLAG_SUSPENDED;
4333 return 0;
4334}
4335
c893a3ae
RD
4336/**
4337 * ata_port_start - Set port up for dma.
4338 * @ap: Port to initialize
4339 *
4340 * Called just after data structures for each port are
4341 * initialized. Allocates space for PRD table.
4342 *
4343 * May be used as the port_start() entry in ata_port_operations.
4344 *
4345 * LOCKING:
4346 * Inherited from caller.
4347 */
4348
1da177e4
LT
4349int ata_port_start (struct ata_port *ap)
4350{
4351 struct device *dev = ap->host_set->dev;
6037d6bb 4352 int rc;
1da177e4
LT
4353
4354 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4355 if (!ap->prd)
4356 return -ENOMEM;
4357
6037d6bb
JG
4358 rc = ata_pad_alloc(ap, dev);
4359 if (rc) {
cedc9a47 4360 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
6037d6bb 4361 return rc;
cedc9a47
JG
4362 }
4363
1da177e4
LT
4364 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4365
4366 return 0;
4367}
4368
0baab86b
EF
4369
4370/**
4371 * ata_port_stop - Undo ata_port_start()
4372 * @ap: Port to shut down
4373 *
4374 * Frees the PRD table.
4375 *
4376 * May be used as the port_stop() entry in ata_port_operations.
4377 *
4378 * LOCKING:
6f0ef4fa 4379 * Inherited from caller.
0baab86b
EF
4380 */
4381
1da177e4
LT
4382void ata_port_stop (struct ata_port *ap)
4383{
4384 struct device *dev = ap->host_set->dev;
4385
4386 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
6037d6bb 4387 ata_pad_free(ap, dev);
1da177e4
LT
4388}
4389
aa8f0dc6
JG
4390void ata_host_stop (struct ata_host_set *host_set)
4391{
4392 if (host_set->mmio_base)
4393 iounmap(host_set->mmio_base);
4394}
4395
4396
1da177e4
LT
4397/**
4398 * ata_host_remove - Unregister SCSI host structure with upper layers
4399 * @ap: Port to unregister
4400 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4401 *
4402 * LOCKING:
6f0ef4fa 4403 * Inherited from caller.
1da177e4
LT
4404 */
4405
4406static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4407{
4408 struct Scsi_Host *sh = ap->host;
4409
4410 DPRINTK("ENTER\n");
4411
4412 if (do_unregister)
4413 scsi_remove_host(sh);
4414
4415 ap->ops->port_stop(ap);
4416}
4417
4418/**
4419 * ata_host_init - Initialize an ata_port structure
4420 * @ap: Structure to initialize
4421 * @host: associated SCSI mid-layer structure
4422 * @host_set: Collection of hosts to which @ap belongs
4423 * @ent: Probe information provided by low-level driver
4424 * @port_no: Port number associated with this ata_port
4425 *
0cba632b
JG
4426 * Initialize a new ata_port structure, and its associated
4427 * scsi_host.
4428 *
1da177e4 4429 * LOCKING:
0cba632b 4430 * Inherited from caller.
1da177e4
LT
4431 */
4432
4433static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4434 struct ata_host_set *host_set,
057ace5e 4435 const struct ata_probe_ent *ent, unsigned int port_no)
1da177e4
LT
4436{
4437 unsigned int i;
4438
4439 host->max_id = 16;
4440 host->max_lun = 1;
4441 host->max_channel = 1;
4442 host->unique_id = ata_unique_id++;
4443 host->max_cmd_len = 12;
12413197 4444
1da177e4
LT
4445 ap->flags = ATA_FLAG_PORT_DISABLED;
4446 ap->id = host->unique_id;
4447 ap->host = host;
4448 ap->ctl = ATA_DEVCTL_OBS;
4449 ap->host_set = host_set;
4450 ap->port_no = port_no;
4451 ap->hard_port_no =
4452 ent->legacy_mode ? ent->hard_port_no : port_no;
4453 ap->pio_mask = ent->pio_mask;
4454 ap->mwdma_mask = ent->mwdma_mask;
4455 ap->udma_mask = ent->udma_mask;
4456 ap->flags |= ent->host_flags;
4457 ap->ops = ent->port_ops;
4458 ap->cbl = ATA_CBL_NONE;
4459 ap->active_tag = ATA_TAG_POISON;
4460 ap->last_ctl = 0xFF;
4461
4462 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4463 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
a72ec4ce 4464 INIT_LIST_HEAD(&ap->eh_done_q);
1da177e4
LT
4465
4466 for (i = 0; i < ATA_MAX_DEVICES; i++)
4467 ap->device[i].devno = i;
4468
4469#ifdef ATA_IRQ_TRAP
4470 ap->stats.unhandled_irq = 1;
4471 ap->stats.idle_irq = 1;
4472#endif
4473
4474 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4475}
4476
4477/**
4478 * ata_host_add - Attach low-level ATA driver to system
4479 * @ent: Information provided by low-level driver
4480 * @host_set: Collections of ports to which we add
4481 * @port_no: Port number associated with this host
4482 *
0cba632b
JG
4483 * Attach low-level ATA driver to system.
4484 *
1da177e4 4485 * LOCKING:
0cba632b 4486 * PCI/etc. bus probe sem.
1da177e4
LT
4487 *
4488 * RETURNS:
0cba632b 4489 * New ata_port on success, for NULL on error.
1da177e4
LT
4490 */
4491
057ace5e 4492static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
1da177e4
LT
4493 struct ata_host_set *host_set,
4494 unsigned int port_no)
4495{
4496 struct Scsi_Host *host;
4497 struct ata_port *ap;
4498 int rc;
4499
4500 DPRINTK("ENTER\n");
4501 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4502 if (!host)
4503 return NULL;
4504
4505 ap = (struct ata_port *) &host->hostdata[0];
4506
4507 ata_host_init(ap, host, host_set, ent, port_no);
4508
4509 rc = ap->ops->port_start(ap);
4510 if (rc)
4511 goto err_out;
4512
4513 return ap;
4514
4515err_out:
4516 scsi_host_put(host);
4517 return NULL;
4518}
4519
4520/**
0cba632b
JG
4521 * ata_device_add - Register hardware device with ATA and SCSI layers
4522 * @ent: Probe information describing hardware device to be registered
4523 *
4524 * This function processes the information provided in the probe
4525 * information struct @ent, allocates the necessary ATA and SCSI
4526 * host information structures, initializes them, and registers
4527 * everything with requisite kernel subsystems.
4528 *
4529 * This function requests irqs, probes the ATA bus, and probes
4530 * the SCSI bus.
1da177e4
LT
4531 *
4532 * LOCKING:
0cba632b 4533 * PCI/etc. bus probe sem.
1da177e4
LT
4534 *
4535 * RETURNS:
0cba632b 4536 * Number of ports registered. Zero on error (no ports registered).
1da177e4
LT
4537 */
4538
057ace5e 4539int ata_device_add(const struct ata_probe_ent *ent)
1da177e4
LT
4540{
4541 unsigned int count = 0, i;
4542 struct device *dev = ent->dev;
4543 struct ata_host_set *host_set;
4544
4545 DPRINTK("ENTER\n");
4546 /* alloc a container for our list of ATA ports (buses) */
57f3bda8 4547 host_set = kzalloc(sizeof(struct ata_host_set) +
1da177e4
LT
4548 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4549 if (!host_set)
4550 return 0;
1da177e4
LT
4551 spin_lock_init(&host_set->lock);
4552
4553 host_set->dev = dev;
4554 host_set->n_ports = ent->n_ports;
4555 host_set->irq = ent->irq;
4556 host_set->mmio_base = ent->mmio_base;
4557 host_set->private_data = ent->private_data;
4558 host_set->ops = ent->port_ops;
4559
4560 /* register each port bound to this device */
4561 for (i = 0; i < ent->n_ports; i++) {
4562 struct ata_port *ap;
4563 unsigned long xfer_mode_mask;
4564
4565 ap = ata_host_add(ent, host_set, i);
4566 if (!ap)
4567 goto err_out;
4568
4569 host_set->ports[i] = ap;
4570 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4571 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4572 (ap->pio_mask << ATA_SHIFT_PIO);
4573
4574 /* print per-port info to dmesg */
4575 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4576 "bmdma 0x%lX irq %lu\n",
4577 ap->id,
4578 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4579 ata_mode_string(xfer_mode_mask),
4580 ap->ioaddr.cmd_addr,
4581 ap->ioaddr.ctl_addr,
4582 ap->ioaddr.bmdma_addr,
4583 ent->irq);
4584
4585 ata_chk_status(ap);
4586 host_set->ops->irq_clear(ap);
4587 count++;
4588 }
4589
57f3bda8
RD
4590 if (!count)
4591 goto err_free_ret;
1da177e4
LT
4592
4593 /* obtain irq, that is shared between channels */
4594 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4595 DRV_NAME, host_set))
4596 goto err_out;
4597
4598 /* perform each probe synchronously */
4599 DPRINTK("probe begin\n");
4600 for (i = 0; i < count; i++) {
4601 struct ata_port *ap;
4602 int rc;
4603
4604 ap = host_set->ports[i];
4605
c893a3ae 4606 DPRINTK("ata%u: bus probe begin\n", ap->id);
1da177e4 4607 rc = ata_bus_probe(ap);
c893a3ae 4608 DPRINTK("ata%u: bus probe end\n", ap->id);
1da177e4
LT
4609
4610 if (rc) {
4611 /* FIXME: do something useful here?
4612 * Current libata behavior will
4613 * tear down everything when
4614 * the module is removed
4615 * or the h/w is unplugged.
4616 */
4617 }
4618
4619 rc = scsi_add_host(ap->host, dev);
4620 if (rc) {
4621 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4622 ap->id);
4623 /* FIXME: do something useful here */
4624 /* FIXME: handle unconditional calls to
4625 * scsi_scan_host and ata_host_remove, below,
4626 * at the very least
4627 */
4628 }
4629 }
4630
4631 /* probes are done, now scan each port's disk(s) */
c893a3ae 4632 DPRINTK("host probe begin\n");
1da177e4
LT
4633 for (i = 0; i < count; i++) {
4634 struct ata_port *ap = host_set->ports[i];
4635
644dd0cc 4636 ata_scsi_scan_host(ap);
1da177e4
LT
4637 }
4638
4639 dev_set_drvdata(dev, host_set);
4640
4641 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4642 return ent->n_ports; /* success */
4643
4644err_out:
4645 for (i = 0; i < count; i++) {
4646 ata_host_remove(host_set->ports[i], 1);
4647 scsi_host_put(host_set->ports[i]->host);
4648 }
57f3bda8 4649err_free_ret:
1da177e4
LT
4650 kfree(host_set);
4651 VPRINTK("EXIT, returning 0\n");
4652 return 0;
4653}
4654
17b14451
AC
4655/**
4656 * ata_host_set_remove - PCI layer callback for device removal
4657 * @host_set: ATA host set that was removed
4658 *
4659 * Unregister all objects associated with this host set. Free those
4660 * objects.
4661 *
4662 * LOCKING:
4663 * Inherited from calling layer (may sleep).
4664 */
4665
17b14451
AC
4666void ata_host_set_remove(struct ata_host_set *host_set)
4667{
4668 struct ata_port *ap;
4669 unsigned int i;
4670
4671 for (i = 0; i < host_set->n_ports; i++) {
4672 ap = host_set->ports[i];
4673 scsi_remove_host(ap->host);
4674 }
4675
4676 free_irq(host_set->irq, host_set);
4677
4678 for (i = 0; i < host_set->n_ports; i++) {
4679 ap = host_set->ports[i];
4680
4681 ata_scsi_release(ap->host);
4682
4683 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4684 struct ata_ioports *ioaddr = &ap->ioaddr;
4685
4686 if (ioaddr->cmd_addr == 0x1f0)
4687 release_region(0x1f0, 8);
4688 else if (ioaddr->cmd_addr == 0x170)
4689 release_region(0x170, 8);
4690 }
4691
4692 scsi_host_put(ap->host);
4693 }
4694
4695 if (host_set->ops->host_stop)
4696 host_set->ops->host_stop(host_set);
4697
4698 kfree(host_set);
4699}
4700
1da177e4
LT
4701/**
4702 * ata_scsi_release - SCSI layer callback hook for host unload
4703 * @host: libata host to be unloaded
4704 *
4705 * Performs all duties necessary to shut down a libata port...
4706 * Kill port kthread, disable port, and release resources.
4707 *
4708 * LOCKING:
4709 * Inherited from SCSI layer.
4710 *
4711 * RETURNS:
4712 * One.
4713 */
4714
4715int ata_scsi_release(struct Scsi_Host *host)
4716{
4717 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4718
4719 DPRINTK("ENTER\n");
4720
4721 ap->ops->port_disable(ap);
4722 ata_host_remove(ap, 0);
4723
4724 DPRINTK("EXIT\n");
4725 return 1;
4726}
4727
4728/**
4729 * ata_std_ports - initialize ioaddr with standard port offsets.
4730 * @ioaddr: IO address structure to be initialized
0baab86b
EF
4731 *
4732 * Utility function which initializes data_addr, error_addr,
4733 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4734 * device_addr, status_addr, and command_addr to standard offsets
4735 * relative to cmd_addr.
4736 *
4737 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
1da177e4 4738 */
0baab86b 4739
1da177e4
LT
4740void ata_std_ports(struct ata_ioports *ioaddr)
4741{
4742 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4743 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4744 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4745 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4746 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4747 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4748 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4749 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4750 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4751 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4752}
4753
0baab86b 4754
374b1873
JG
4755#ifdef CONFIG_PCI
4756
4757void ata_pci_host_stop (struct ata_host_set *host_set)
4758{
4759 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4760
4761 pci_iounmap(pdev, host_set->mmio_base);
4762}
4763
1da177e4
LT
4764/**
4765 * ata_pci_remove_one - PCI layer callback for device removal
4766 * @pdev: PCI device that was removed
4767 *
4768 * PCI layer indicates to libata via this hook that
6f0ef4fa 4769 * hot-unplug or module unload event has occurred.
1da177e4
LT
4770 * Handle this by unregistering all objects associated
4771 * with this PCI device. Free those objects. Then finally
4772 * release PCI resources and disable device.
4773 *
4774 * LOCKING:
4775 * Inherited from PCI layer (may sleep).
4776 */
4777
4778void ata_pci_remove_one (struct pci_dev *pdev)
4779{
4780 struct device *dev = pci_dev_to_dev(pdev);
4781 struct ata_host_set *host_set = dev_get_drvdata(dev);
1da177e4 4782
17b14451 4783 ata_host_set_remove(host_set);
1da177e4
LT
4784 pci_release_regions(pdev);
4785 pci_disable_device(pdev);
4786 dev_set_drvdata(dev, NULL);
4787}
4788
4789/* move to PCI subsystem */
057ace5e 4790int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
1da177e4
LT
4791{
4792 unsigned long tmp = 0;
4793
4794 switch (bits->width) {
4795 case 1: {
4796 u8 tmp8 = 0;
4797 pci_read_config_byte(pdev, bits->reg, &tmp8);
4798 tmp = tmp8;
4799 break;
4800 }
4801 case 2: {
4802 u16 tmp16 = 0;
4803 pci_read_config_word(pdev, bits->reg, &tmp16);
4804 tmp = tmp16;
4805 break;
4806 }
4807 case 4: {
4808 u32 tmp32 = 0;
4809 pci_read_config_dword(pdev, bits->reg, &tmp32);
4810 tmp = tmp32;
4811 break;
4812 }
4813
4814 default:
4815 return -EINVAL;
4816 }
4817
4818 tmp &= bits->mask;
4819
4820 return (tmp == bits->val) ? 1 : 0;
4821}
9b847548
JA
4822
4823int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4824{
4825 pci_save_state(pdev);
4826 pci_disable_device(pdev);
4827 pci_set_power_state(pdev, PCI_D3hot);
4828 return 0;
4829}
4830
4831int ata_pci_device_resume(struct pci_dev *pdev)
4832{
4833 pci_set_power_state(pdev, PCI_D0);
4834 pci_restore_state(pdev);
4835 pci_enable_device(pdev);
4836 pci_set_master(pdev);
4837 return 0;
4838}
1da177e4
LT
4839#endif /* CONFIG_PCI */
4840
4841
1da177e4
LT
4842static int __init ata_init(void)
4843{
4844 ata_wq = create_workqueue("ata");
4845 if (!ata_wq)
4846 return -ENOMEM;
4847
4848 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4849 return 0;
4850}
4851
4852static void __exit ata_exit(void)
4853{
4854 destroy_workqueue(ata_wq);
4855}
4856
4857module_init(ata_init);
4858module_exit(ata_exit);
4859
67846b30
JG
4860static unsigned long ratelimit_time;
4861static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4862
4863int ata_ratelimit(void)
4864{
4865 int rc;
4866 unsigned long flags;
4867
4868 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4869
4870 if (time_after(jiffies, ratelimit_time)) {
4871 rc = 1;
4872 ratelimit_time = jiffies + (HZ/5);
4873 } else
4874 rc = 0;
4875
4876 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4877
4878 return rc;
4879}
4880
1da177e4
LT
4881/*
4882 * libata is essentially a library of internal helper functions for
4883 * low-level ATA host controller drivers. As such, the API/ABI is
4884 * likely to change as new drivers are added and updated.
4885 * Do not depend on ABI/API stability.
4886 */
4887
4888EXPORT_SYMBOL_GPL(ata_std_bios_param);
4889EXPORT_SYMBOL_GPL(ata_std_ports);
4890EXPORT_SYMBOL_GPL(ata_device_add);
17b14451 4891EXPORT_SYMBOL_GPL(ata_host_set_remove);
1da177e4
LT
4892EXPORT_SYMBOL_GPL(ata_sg_init);
4893EXPORT_SYMBOL_GPL(ata_sg_init_one);
76014427 4894EXPORT_SYMBOL_GPL(__ata_qc_complete);
1da177e4
LT
4895EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4896EXPORT_SYMBOL_GPL(ata_eng_timeout);
4897EXPORT_SYMBOL_GPL(ata_tf_load);
4898EXPORT_SYMBOL_GPL(ata_tf_read);
4899EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4900EXPORT_SYMBOL_GPL(ata_std_dev_select);
4901EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4902EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4903EXPORT_SYMBOL_GPL(ata_check_status);
4904EXPORT_SYMBOL_GPL(ata_altstatus);
1da177e4
LT
4905EXPORT_SYMBOL_GPL(ata_exec_command);
4906EXPORT_SYMBOL_GPL(ata_port_start);
4907EXPORT_SYMBOL_GPL(ata_port_stop);
aa8f0dc6 4908EXPORT_SYMBOL_GPL(ata_host_stop);
1da177e4
LT
4909EXPORT_SYMBOL_GPL(ata_interrupt);
4910EXPORT_SYMBOL_GPL(ata_qc_prep);
4911EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4912EXPORT_SYMBOL_GPL(ata_bmdma_start);
4913EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4914EXPORT_SYMBOL_GPL(ata_bmdma_status);
4915EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4916EXPORT_SYMBOL_GPL(ata_port_probe);
4917EXPORT_SYMBOL_GPL(sata_phy_reset);
4918EXPORT_SYMBOL_GPL(__sata_phy_reset);
4919EXPORT_SYMBOL_GPL(ata_bus_reset);
8a19ac89 4920EXPORT_SYMBOL_GPL(ata_std_probeinit);
c2bd5804
TH
4921EXPORT_SYMBOL_GPL(ata_std_softreset);
4922EXPORT_SYMBOL_GPL(sata_std_hardreset);
4923EXPORT_SYMBOL_GPL(ata_std_postreset);
4924EXPORT_SYMBOL_GPL(ata_std_probe_reset);
a62c0fc5 4925EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
1da177e4 4926EXPORT_SYMBOL_GPL(ata_port_disable);
67846b30 4927EXPORT_SYMBOL_GPL(ata_ratelimit);
6f8b9958 4928EXPORT_SYMBOL_GPL(ata_busy_sleep);
1da177e4
LT
4929EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4930EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
f29841e0 4931EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
1da177e4
LT
4932EXPORT_SYMBOL_GPL(ata_scsi_error);
4933EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4934EXPORT_SYMBOL_GPL(ata_scsi_release);
4935EXPORT_SYMBOL_GPL(ata_host_intr);
4936EXPORT_SYMBOL_GPL(ata_dev_classify);
4937EXPORT_SYMBOL_GPL(ata_dev_id_string);
0e949ff3 4938EXPORT_SYMBOL_GPL(ata_dev_id_c_string);
6f2f3812 4939EXPORT_SYMBOL_GPL(ata_dev_config);
1da177e4 4940EXPORT_SYMBOL_GPL(ata_scsi_simulate);
a72ec4ce
TH
4941EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4942EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
1da177e4 4943
1bc4ccff 4944EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
452503f9
AC
4945EXPORT_SYMBOL_GPL(ata_timing_compute);
4946EXPORT_SYMBOL_GPL(ata_timing_merge);
4947
1da177e4
LT
4948#ifdef CONFIG_PCI
4949EXPORT_SYMBOL_GPL(pci_test_config_bits);
374b1873 4950EXPORT_SYMBOL_GPL(ata_pci_host_stop);
1da177e4
LT
4951EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4952EXPORT_SYMBOL_GPL(ata_pci_init_one);
4953EXPORT_SYMBOL_GPL(ata_pci_remove_one);
9b847548
JA
4954EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4955EXPORT_SYMBOL_GPL(ata_pci_device_resume);
1da177e4 4956#endif /* CONFIG_PCI */
9b847548
JA
4957
4958EXPORT_SYMBOL_GPL(ata_device_suspend);
4959EXPORT_SYMBOL_GPL(ata_device_resume);
4960EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4961EXPORT_SYMBOL_GPL(ata_scsi_device_resume);