blob: 6d8aa86f2f6b0eac7827491d2218ad8d8bc43c62 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040051#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100052#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "scsi_priv.h"
Jeff Garzik193515d2005-11-07 00:59:37 -050055#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#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
Tejun Heo6aff8f12006-02-15 18:24:09 +090064static unsigned int ata_dev_init_params(struct ata_port *ap,
65 struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ata_set_mode(struct ata_port *ap);
67static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040068static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040070static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u8 *xfer_mode_out,
72 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static unsigned int ata_unique_id = 1;
75static struct workqueue_struct *ata_wq;
76
Jeff Garzik1623c812005-08-30 03:37:42 -040077int atapi_enabled = 0;
78module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
Jeff Garzikc3c013a2006-02-27 22:31:19 -050081int libata_fua = 0;
82module_param_named(fua, libata_fua, int, 0444);
83MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_AUTHOR("Jeff Garzik");
86MODULE_DESCRIPTION("Library module for ATA devices");
87MODULE_LICENSE("GPL");
88MODULE_VERSION(DRV_VERSION);
89
Edward Falk0baab862005-06-02 18:17:13 -040090
91/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
93 * @tf: Taskfile to convert
94 * @fis: Buffer into which data will output
95 * @pmp: Port multiplier port
96 *
97 * Converts a standard ATA taskfile to a Serial ATA
98 * FIS structure (Register - Host to Device).
99 *
100 * LOCKING:
101 * Inherited from caller.
102 */
103
Jeff Garzik057ace52005-10-22 14:27:05 -0400104void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 fis[0] = 0x27; /* Register - Host to Device FIS */
107 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
108 bit 7 indicates Command FIS */
109 fis[2] = tf->command;
110 fis[3] = tf->feature;
111
112 fis[4] = tf->lbal;
113 fis[5] = tf->lbam;
114 fis[6] = tf->lbah;
115 fis[7] = tf->device;
116
117 fis[8] = tf->hob_lbal;
118 fis[9] = tf->hob_lbam;
119 fis[10] = tf->hob_lbah;
120 fis[11] = tf->hob_feature;
121
122 fis[12] = tf->nsect;
123 fis[13] = tf->hob_nsect;
124 fis[14] = 0;
125 fis[15] = tf->ctl;
126
127 fis[16] = 0;
128 fis[17] = 0;
129 fis[18] = 0;
130 fis[19] = 0;
131}
132
133/**
134 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
135 * @fis: Buffer from which data will be input
136 * @tf: Taskfile to output
137 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500138 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
140 * LOCKING:
141 * Inherited from caller.
142 */
143
Jeff Garzik057ace52005-10-22 14:27:05 -0400144void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 tf->command = fis[2]; /* status */
147 tf->feature = fis[3]; /* error */
148
149 tf->lbal = fis[4];
150 tf->lbam = fis[5];
151 tf->lbah = fis[6];
152 tf->device = fis[7];
153
154 tf->hob_lbal = fis[8];
155 tf->hob_lbam = fis[9];
156 tf->hob_lbah = fis[10];
157
158 tf->nsect = fis[12];
159 tf->hob_nsect = fis[13];
160}
161
Albert Lee8cbd6df2005-10-12 15:06:27 +0800162static const u8 ata_rw_cmds[] = {
163 /* pio multi */
164 ATA_CMD_READ_MULTI,
165 ATA_CMD_WRITE_MULTI,
166 ATA_CMD_READ_MULTI_EXT,
167 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100168 0,
169 0,
170 0,
171 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800172 /* pio */
173 ATA_CMD_PIO_READ,
174 ATA_CMD_PIO_WRITE,
175 ATA_CMD_PIO_READ_EXT,
176 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100177 0,
178 0,
179 0,
180 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800181 /* dma */
182 ATA_CMD_READ,
183 ATA_CMD_WRITE,
184 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100185 ATA_CMD_WRITE_EXT,
186 0,
187 0,
188 0,
189 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800190};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800193 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
194 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800196 * Examine the device configuration and tf->flags to calculate
197 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * LOCKING:
200 * caller.
201 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100202int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800204 struct ata_taskfile *tf = &qc->tf;
205 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100208 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800209
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100210 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800211 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
212 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Albert Lee8cbd6df2005-10-12 15:06:27 +0800214 if (dev->flags & ATA_DFLAG_PIO) {
215 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100216 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000217 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
218 /* Unable to use DMA due to host limitation */
219 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800220 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800221 } else {
222 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100223 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100226 cmd = ata_rw_cmds[index + fua + lba48 + write];
227 if (cmd) {
228 tf->command = cmd;
229 return 0;
230 }
231 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100234static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 "UDMA/16",
236 "UDMA/25",
237 "UDMA/33",
238 "UDMA/44",
239 "UDMA/66",
240 "UDMA/100",
241 "UDMA/133",
242 "UDMA7",
243 "MWDMA0",
244 "MWDMA1",
245 "MWDMA2",
246 "PIO0",
247 "PIO1",
248 "PIO2",
249 "PIO3",
250 "PIO4",
251};
252
253/**
254 * ata_udma_string - convert UDMA bit offset to string
255 * @mask: mask of bits supported; only highest bit counts.
256 *
257 * Determine string which represents the highest speed
258 * (highest bit in @udma_mask).
259 *
260 * LOCKING:
261 * None.
262 *
263 * RETURNS:
264 * Constant C string representing highest speed listed in
265 * @udma_mask, or the constant C string "<n/a>".
266 */
267
268static const char *ata_mode_string(unsigned int mask)
269{
270 int i;
271
272 for (i = 7; i >= 0; i--)
273 if (mask & (1 << i))
274 goto out;
275 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
276 if (mask & (1 << i))
277 goto out;
278 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
279 if (mask & (1 << i))
280 goto out;
281
282 return "<n/a>";
283
284out:
285 return xfer_mode_str[i];
286}
287
288/**
289 * ata_pio_devchk - PATA device presence detection
290 * @ap: ATA channel to examine
291 * @device: Device to examine (starting at zero)
292 *
293 * This technique was originally described in
294 * Hale Landis's ATADRVR (www.ata-atapi.com), and
295 * later found its way into the ATA/ATAPI spec.
296 *
297 * Write a pattern to the ATA shadow registers,
298 * and if a device is present, it will respond by
299 * correctly storing and echoing back the
300 * ATA shadow register contents.
301 *
302 * LOCKING:
303 * caller.
304 */
305
306static unsigned int ata_pio_devchk(struct ata_port *ap,
307 unsigned int device)
308{
309 struct ata_ioports *ioaddr = &ap->ioaddr;
310 u8 nsect, lbal;
311
312 ap->ops->dev_select(ap, device);
313
314 outb(0x55, ioaddr->nsect_addr);
315 outb(0xaa, ioaddr->lbal_addr);
316
317 outb(0xaa, ioaddr->nsect_addr);
318 outb(0x55, ioaddr->lbal_addr);
319
320 outb(0x55, ioaddr->nsect_addr);
321 outb(0xaa, ioaddr->lbal_addr);
322
323 nsect = inb(ioaddr->nsect_addr);
324 lbal = inb(ioaddr->lbal_addr);
325
326 if ((nsect == 0x55) && (lbal == 0xaa))
327 return 1; /* we found a device */
328
329 return 0; /* nothing found */
330}
331
332/**
333 * ata_mmio_devchk - PATA device presence detection
334 * @ap: ATA channel to examine
335 * @device: Device to examine (starting at zero)
336 *
337 * This technique was originally described in
338 * Hale Landis's ATADRVR (www.ata-atapi.com), and
339 * later found its way into the ATA/ATAPI spec.
340 *
341 * Write a pattern to the ATA shadow registers,
342 * and if a device is present, it will respond by
343 * correctly storing and echoing back the
344 * ATA shadow register contents.
345 *
346 * LOCKING:
347 * caller.
348 */
349
350static unsigned int ata_mmio_devchk(struct ata_port *ap,
351 unsigned int device)
352{
353 struct ata_ioports *ioaddr = &ap->ioaddr;
354 u8 nsect, lbal;
355
356 ap->ops->dev_select(ap, device);
357
358 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
359 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
360
361 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
362 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
363
364 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
365 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
366
367 nsect = readb((void __iomem *) ioaddr->nsect_addr);
368 lbal = readb((void __iomem *) ioaddr->lbal_addr);
369
370 if ((nsect == 0x55) && (lbal == 0xaa))
371 return 1; /* we found a device */
372
373 return 0; /* nothing found */
374}
375
376/**
377 * ata_devchk - PATA device presence detection
378 * @ap: ATA channel to examine
379 * @device: Device to examine (starting at zero)
380 *
381 * Dispatch ATA device presence detection, depending
382 * on whether we are using PIO or MMIO to talk to the
383 * ATA shadow registers.
384 *
385 * LOCKING:
386 * caller.
387 */
388
389static unsigned int ata_devchk(struct ata_port *ap,
390 unsigned int device)
391{
392 if (ap->flags & ATA_FLAG_MMIO)
393 return ata_mmio_devchk(ap, device);
394 return ata_pio_devchk(ap, device);
395}
396
397/**
398 * ata_dev_classify - determine device type based on ATA-spec signature
399 * @tf: ATA taskfile register set for device to be identified
400 *
401 * Determine from taskfile register contents whether a device is
402 * ATA or ATAPI, as per "Signature and persistence" section
403 * of ATA/PI spec (volume 1, sect 5.14).
404 *
405 * LOCKING:
406 * None.
407 *
408 * RETURNS:
409 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
410 * the event of failure.
411 */
412
Jeff Garzik057ace52005-10-22 14:27:05 -0400413unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 /* Apple's open source Darwin code hints that some devices only
416 * put a proper signature into the LBA mid/high registers,
417 * So, we only check those. It's sufficient for uniqueness.
418 */
419
420 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
421 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
422 DPRINTK("found ATA device by sig\n");
423 return ATA_DEV_ATA;
424 }
425
426 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
427 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
428 DPRINTK("found ATAPI device by sig\n");
429 return ATA_DEV_ATAPI;
430 }
431
432 DPRINTK("unknown device\n");
433 return ATA_DEV_UNKNOWN;
434}
435
436/**
437 * ata_dev_try_classify - Parse returned ATA device signature
438 * @ap: ATA channel to examine
439 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900440 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *
442 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
443 * an ATA/ATAPI-defined set of values is placed in the ATA
444 * shadow registers, indicating the results of device detection
445 * and diagnostics.
446 *
447 * Select the ATA device, and read the values from the ATA shadow
448 * registers. Then parse according to the Error register value,
449 * and the spec-defined values examined by ata_dev_classify().
450 *
451 * LOCKING:
452 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900453 *
454 * RETURNS:
455 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
457
Tejun Heob4dc7622006-01-24 17:05:22 +0900458static unsigned int
459ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct ata_taskfile tf;
462 unsigned int class;
463 u8 err;
464
465 ap->ops->dev_select(ap, device);
466
467 memset(&tf, 0, sizeof(tf));
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400470 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900471 if (r_err)
472 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* see if device passed diags */
475 if (err == 1)
476 /* do nothing */ ;
477 else if ((device == 0) && (err == 0x81))
478 /* do nothing */ ;
479 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900480 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Tejun Heob4dc7622006-01-24 17:05:22 +0900482 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900486 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900488 return ATA_DEV_NONE;
489 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900493 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * @id: IDENTIFY DEVICE results we will examine
495 * @s: string into which data is output
496 * @ofs: offset into identify device page
497 * @len: length of string to return. must be an even number.
498 *
499 * The strings in the IDENTIFY DEVICE page are broken up into
500 * 16-bit chunks. Run through the string, and output each
501 * 8-bit chunk linearly, regardless of platform.
502 *
503 * LOCKING:
504 * caller.
505 */
506
Tejun Heo6a62a042006-02-13 10:02:46 +0900507void ata_id_string(const u16 *id, unsigned char *s,
508 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 unsigned int c;
511
512 while (len > 0) {
513 c = id[ofs] >> 8;
514 *s = c;
515 s++;
516
517 c = id[ofs] & 0xff;
518 *s = c;
519 s++;
520
521 ofs++;
522 len -= 2;
523 }
524}
525
Tejun Heo0e949ff2006-02-12 22:47:04 +0900526/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900527 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900528 * @id: IDENTIFY DEVICE results we will examine
529 * @s: string into which data is output
530 * @ofs: offset into identify device page
531 * @len: length of string to return. must be an odd number.
532 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900533 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900534 * trims trailing spaces and terminates the resulting string with
535 * null. @len must be actual maximum length (even number) + 1.
536 *
537 * LOCKING:
538 * caller.
539 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900540void ata_id_c_string(const u16 *id, unsigned char *s,
541 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900542{
543 unsigned char *p;
544
545 WARN_ON(!(len & 1));
546
Tejun Heo6a62a042006-02-13 10:02:46 +0900547 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900548
549 p = s + strnlen(s, len - 1);
550 while (p > s && p[-1] == ' ')
551 p--;
552 *p = '\0';
553}
Edward Falk0baab862005-06-02 18:17:13 -0400554
Tejun Heo29407402006-02-12 22:47:04 +0900555static u64 ata_id_n_sectors(const u16 *id)
556{
557 if (ata_id_has_lba(id)) {
558 if (ata_id_has_lba48(id))
559 return ata_id_u64(id, 100);
560 else
561 return ata_id_u32(id, 60);
562 } else {
563 if (ata_id_current_chs_valid(id))
564 return ata_id_u32(id, 57);
565 else
566 return id[1] * id[3] * id[6];
567 }
568}
569
Edward Falk0baab862005-06-02 18:17:13 -0400570/**
571 * ata_noop_dev_select - Select device 0/1 on ATA bus
572 * @ap: ATA channel to manipulate
573 * @device: ATA device (numbered from zero) to select
574 *
575 * This function performs no actual function.
576 *
577 * May be used as the dev_select() entry in ata_port_operations.
578 *
579 * LOCKING:
580 * caller.
581 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
583{
584}
585
Edward Falk0baab862005-06-02 18:17:13 -0400586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/**
588 * ata_std_dev_select - Select device 0/1 on ATA bus
589 * @ap: ATA channel to manipulate
590 * @device: ATA device (numbered from zero) to select
591 *
592 * Use the method defined in the ATA specification to
593 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400594 * ATA channel. Works with both PIO and MMIO.
595 *
596 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 *
598 * LOCKING:
599 * caller.
600 */
601
602void ata_std_dev_select (struct ata_port *ap, unsigned int device)
603{
604 u8 tmp;
605
606 if (device == 0)
607 tmp = ATA_DEVICE_OBS;
608 else
609 tmp = ATA_DEVICE_OBS | ATA_DEV1;
610
611 if (ap->flags & ATA_FLAG_MMIO) {
612 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
613 } else {
614 outb(tmp, ap->ioaddr.device_addr);
615 }
616 ata_pause(ap); /* needed; also flushes, for mmio */
617}
618
619/**
620 * ata_dev_select - Select device 0/1 on ATA bus
621 * @ap: ATA channel to manipulate
622 * @device: ATA device (numbered from zero) to select
623 * @wait: non-zero to wait for Status register BSY bit to clear
624 * @can_sleep: non-zero if context allows sleeping
625 *
626 * Use the method defined in the ATA specification to
627 * make either device 0, or device 1, active on the
628 * ATA channel.
629 *
630 * This is a high-level version of ata_std_dev_select(),
631 * which additionally provides the services of inserting
632 * the proper pauses and status polling, where needed.
633 *
634 * LOCKING:
635 * caller.
636 */
637
638void ata_dev_select(struct ata_port *ap, unsigned int device,
639 unsigned int wait, unsigned int can_sleep)
640{
641 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
642 ap->id, device, wait);
643
644 if (wait)
645 ata_wait_idle(ap);
646
647 ap->ops->dev_select(ap, device);
648
649 if (wait) {
650 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
651 msleep(150);
652 ata_wait_idle(ap);
653 }
654}
655
656/**
657 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900658 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900660 * Dump selected 16-bit words from the given IDENTIFY DEVICE
661 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 *
663 * LOCKING:
664 * caller.
665 */
666
Tejun Heo0bd33002006-02-12 22:47:05 +0900667static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 DPRINTK("49==0x%04x "
670 "53==0x%04x "
671 "63==0x%04x "
672 "64==0x%04x "
673 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900674 id[49],
675 id[53],
676 id[63],
677 id[64],
678 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 DPRINTK("80==0x%04x "
680 "81==0x%04x "
681 "82==0x%04x "
682 "83==0x%04x "
683 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900684 id[80],
685 id[81],
686 id[82],
687 id[83],
688 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 DPRINTK("88==0x%04x "
690 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900691 id[88],
692 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Alan Cox11e29e22005-10-21 18:46:32 -0400695/*
696 * Compute the PIO modes available for this device. This is not as
697 * trivial as it seems if we must consider early devices correctly.
698 *
699 * FIXME: pre IDE drive timing (do we care ?).
700 */
701
Jeff Garzik057ace52005-10-22 14:27:05 -0400702static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -0400703{
704 u16 modes;
705
Alan Coxffa29452006-01-09 17:14:40 +0000706 /* Usual case. Word 53 indicates word 64 is valid */
707 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -0400708 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
709 modes <<= 3;
710 modes |= 0x7;
711 return modes;
712 }
713
Alan Coxffa29452006-01-09 17:14:40 +0000714 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
715 number for the maximum. Turn it into a mask and return it */
716 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -0400717 return modes;
Alan Coxffa29452006-01-09 17:14:40 +0000718 /* But wait.. there's more. Design your standards by committee and
719 you too can get a free iordy field to process. However its the
720 speeds not the modes that are supported... Note drivers using the
721 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -0400722}
723
Tejun Heo86e45b62006-03-05 15:29:09 +0900724/**
725 * ata_port_queue_task - Queue port_task
726 * @ap: The ata_port to queue port_task for
727 *
728 * Schedule @fn(@data) for execution after @delay jiffies using
729 * port_task. There is one port_task per port and it's the
730 * user(low level driver)'s responsibility to make sure that only
731 * one task is active at any given time.
732 *
733 * libata core layer takes care of synchronization between
734 * port_task and EH. ata_port_queue_task() may be ignored for EH
735 * synchronization.
736 *
737 * LOCKING:
738 * Inherited from caller.
739 */
740void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
741 unsigned long delay)
742{
743 int rc;
744
Tejun Heo2e755f62006-03-05 15:29:09 +0900745 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900746 return;
747
748 PREPARE_WORK(&ap->port_task, fn, data);
749
750 if (!delay)
751 rc = queue_work(ata_wq, &ap->port_task);
752 else
753 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
754
755 /* rc == 0 means that another user is using port task */
756 WARN_ON(rc == 0);
757}
758
759/**
760 * ata_port_flush_task - Flush port_task
761 * @ap: The ata_port to flush port_task for
762 *
763 * After this function completes, port_task is guranteed not to
764 * be running or scheduled.
765 *
766 * LOCKING:
767 * Kernel thread context (may sleep)
768 */
769void ata_port_flush_task(struct ata_port *ap)
770{
771 unsigned long flags;
772
773 DPRINTK("ENTER\n");
774
775 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900776 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900777 spin_unlock_irqrestore(&ap->host_set->lock, flags);
778
779 DPRINTK("flush #1\n");
780 flush_workqueue(ata_wq);
781
782 /*
783 * At this point, if a task is running, it's guaranteed to see
784 * the FLUSH flag; thus, it will never queue pio tasks again.
785 * Cancel and flush.
786 */
787 if (!cancel_delayed_work(&ap->port_task)) {
788 DPRINTK("flush #2\n");
789 flush_workqueue(ata_wq);
790 }
791
792 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900793 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900794 spin_unlock_irqrestore(&ap->host_set->lock, flags);
795
796 DPRINTK("EXIT\n");
797}
798
Tejun Heo77853bf2006-01-23 13:09:36 +0900799void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900800{
Tejun Heo77853bf2006-01-23 13:09:36 +0900801 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900802
Tejun Heo77853bf2006-01-23 13:09:36 +0900803 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900804 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900805}
806
807/**
808 * ata_exec_internal - execute libata internal command
809 * @ap: Port to which the command is sent
810 * @dev: Device to which the command is sent
811 * @tf: Taskfile registers for the command and the result
812 * @dma_dir: Data tranfer direction of the command
813 * @buf: Data buffer of the command
814 * @buflen: Length of data buffer
815 *
816 * Executes libata internal command with timeout. @tf contains
817 * command on entry and result on return. Timeout and error
818 * conditions are reported via return value. No recovery action
819 * is taken after a command times out. It's caller's duty to
820 * clean up after timeout.
821 *
822 * LOCKING:
823 * None. Should be called with kernel context, might sleep.
824 */
825
826static unsigned
827ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
828 struct ata_taskfile *tf,
829 int dma_dir, void *buf, unsigned int buflen)
830{
831 u8 command = tf->command;
832 struct ata_queued_cmd *qc;
833 DECLARE_COMPLETION(wait);
834 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900835 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900836
837 spin_lock_irqsave(&ap->host_set->lock, flags);
838
839 qc = ata_qc_new_init(ap, dev);
840 BUG_ON(qc == NULL);
841
842 qc->tf = *tf;
843 qc->dma_dir = dma_dir;
844 if (dma_dir != DMA_NONE) {
845 ata_sg_init_one(qc, buf, buflen);
846 qc->nsect = buflen / ATA_SECT_SIZE;
847 }
848
Tejun Heo77853bf2006-01-23 13:09:36 +0900849 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900850 qc->complete_fn = ata_qc_complete_internal;
851
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900852 qc->err_mask = ata_qc_issue(qc);
853 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900854 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900855
856 spin_unlock_irqrestore(&ap->host_set->lock, flags);
857
858 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
859 spin_lock_irqsave(&ap->host_set->lock, flags);
860
861 /* We're racing with irq here. If we lose, the
862 * following test prevents us from completing the qc
863 * again. If completion irq occurs after here but
864 * before the caller cleans up, it will result in a
865 * spurious interrupt. We can live with that.
866 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900867 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900868 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900869 ata_qc_complete(qc);
870 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
871 ap->id, command);
872 }
873
874 spin_unlock_irqrestore(&ap->host_set->lock, flags);
875 }
876
Tejun Heo77853bf2006-01-23 13:09:36 +0900877 *tf = qc->tf;
878 err_mask = qc->err_mask;
879
880 ata_qc_free(qc);
881
882 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +0000886 * ata_pio_need_iordy - check if iordy needed
887 * @adev: ATA device
888 *
889 * Check if the current speed of the device requires IORDY. Used
890 * by various controllers for chip configuration.
891 */
892
893unsigned int ata_pio_need_iordy(const struct ata_device *adev)
894{
895 int pio;
896 int speed = adev->pio_mode - XFER_PIO_0;
897
898 if (speed < 2)
899 return 0;
900 if (speed > 2)
901 return 1;
902
903 /* If we have no drive specific rule, then PIO 2 is non IORDY */
904
905 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
906 pio = adev->id[ATA_ID_EIDE_PIO];
907 /* Is the speed faster than the drive allows non IORDY ? */
908 if (pio) {
909 /* This is cycle times not frequency - watch the logic! */
910 if (pio > 240) /* PIO2 is 240nS per cycle */
911 return 1;
912 return 0;
913 }
914 }
915 return 0;
916}
917
918/**
Tejun Heo49016ac2006-02-21 02:12:11 +0900919 * ata_dev_read_id - Read ID data from the specified device
920 * @ap: port on which target device resides
921 * @dev: target device
922 * @p_class: pointer to class of the target device (may be changed)
923 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +0900924 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +0900925 *
926 * Read ID data from the specified device. ATA_CMD_ID_ATA is
927 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
928 * devices. This function also takes care of EDD signature
929 * misreporting (to be removed once EDD support is gone) and
930 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
931 *
932 * LOCKING:
933 * Kernel thread context (may sleep)
934 *
935 * RETURNS:
936 * 0 on success, -errno otherwise.
937 */
938static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +0900939 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +0900940{
941 unsigned int class = *p_class;
942 unsigned int using_edd;
943 struct ata_taskfile tf;
944 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +0900945 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +0900946 const char *reason;
947 int rc;
948
949 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
950
951 if (ap->ops->probe_reset ||
952 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
953 using_edd = 0;
954 else
955 using_edd = 1;
956
957 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
958
Tejun Heod9572b12006-03-01 16:09:35 +0900959 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
960 if (id == NULL) {
961 rc = -ENOMEM;
962 reason = "out of memory";
963 goto err_out;
964 }
965
Tejun Heo49016ac2006-02-21 02:12:11 +0900966 retry:
967 ata_tf_init(ap, &tf, dev->devno);
968
969 switch (class) {
970 case ATA_DEV_ATA:
971 tf.command = ATA_CMD_ID_ATA;
972 break;
973 case ATA_DEV_ATAPI:
974 tf.command = ATA_CMD_ID_ATAPI;
975 break;
976 default:
977 rc = -ENODEV;
978 reason = "unsupported class";
979 goto err_out;
980 }
981
982 tf.protocol = ATA_PROT_PIO;
983
984 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
985 id, sizeof(id[0]) * ATA_ID_WORDS);
986
987 if (err_mask) {
988 rc = -EIO;
989 reason = "I/O error";
990
991 if (err_mask & ~AC_ERR_DEV)
992 goto err_out;
993
994 /*
995 * arg! EDD works for all test cases, but seems to return
996 * the ATA signature for some ATAPI devices. Until the
997 * reason for this is found and fixed, we fix up the mess
998 * here. If IDENTIFY DEVICE returns command aborted
999 * (as ATAPI devices do), then we issue an
1000 * IDENTIFY PACKET DEVICE.
1001 *
1002 * ATA software reset (SRST, the default) does not appear
1003 * to have this problem.
1004 */
1005 if ((using_edd) && (class == ATA_DEV_ATA)) {
1006 u8 err = tf.feature;
1007 if (err & ATA_ABORTED) {
1008 class = ATA_DEV_ATAPI;
1009 goto retry;
1010 }
1011 }
1012 goto err_out;
1013 }
1014
1015 swap_buf_le16(id, ATA_ID_WORDS);
1016
1017 /* print device capabilities */
1018 printk(KERN_DEBUG "ata%u: dev %u cfg "
1019 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1020 ap->id, dev->devno,
1021 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1022
1023 /* sanity check */
1024 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1025 rc = -EINVAL;
1026 reason = "device reports illegal type";
1027 goto err_out;
1028 }
1029
1030 if (post_reset && class == ATA_DEV_ATA) {
1031 /*
1032 * The exact sequence expected by certain pre-ATA4 drives is:
1033 * SRST RESET
1034 * IDENTIFY
1035 * INITIALIZE DEVICE PARAMETERS
1036 * anything else..
1037 * Some drives were very specific about that exact sequence.
1038 */
1039 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1040 err_mask = ata_dev_init_params(ap, dev);
1041 if (err_mask) {
1042 rc = -EIO;
1043 reason = "INIT_DEV_PARAMS failed";
1044 goto err_out;
1045 }
1046
1047 /* current CHS translation info (id[53-58]) might be
1048 * changed. reread the identify device info.
1049 */
1050 post_reset = 0;
1051 goto retry;
1052 }
1053 }
1054
1055 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001056 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001057 return 0;
1058
1059 err_out:
1060 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1061 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001062 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001063 return rc;
1064}
1065
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001066static inline u8 ata_dev_knobble(const struct ata_port *ap,
1067 struct ata_device *dev)
1068{
1069 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1070}
1071
Tejun Heo49016ac2006-02-21 02:12:11 +09001072/**
Tejun Heoffeae412006-03-01 16:09:35 +09001073 * ata_dev_configure - Configure the specified ATA/ATAPI device
1074 * @ap: Port on which target device resides
1075 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001076 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 *
Tejun Heoffeae412006-03-01 16:09:35 +09001078 * Configure @dev according to @dev->id. Generic and low-level
1079 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *
1081 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001082 * Kernel thread context (may sleep)
1083 *
1084 * RETURNS:
1085 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001087static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1088 int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 unsigned long xfer_modes;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001091 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (!ata_dev_present(dev)) {
1094 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001095 ap->id, dev->devno);
1096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
Tejun Heoffeae412006-03-01 16:09:35 +09001099 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Tejun Heo208a9932006-03-05 17:55:58 +09001101 /* initialize to-be-configured parameters */
1102 dev->flags = 0;
1103 dev->max_sectors = 0;
1104 dev->cdb_len = 0;
1105 dev->n_sectors = 0;
1106 dev->cylinders = 0;
1107 dev->heads = 0;
1108 dev->sectors = 0;
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /*
1111 * common ATA, ATAPI feature tests
1112 */
1113
Albert Lee8bf62ec2005-05-12 15:29:42 -04001114 /* we require DMA support (bits 8 of word 49) */
1115 if (!ata_id_has_dma(dev->id)) {
1116 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001117 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 goto err_out_nosup;
1119 }
1120
1121 /* quick-n-dirty find max transfer mode; for printk only */
1122 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1123 if (!xfer_modes)
1124 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001125 if (!xfer_modes)
1126 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Tejun Heo0bd33002006-02-12 22:47:05 +09001128 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /* ATA-specific feature tests */
1131 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001132 dev->n_sectors = ata_id_n_sectors(dev->id);
1133
Albert Lee8bf62ec2005-05-12 15:29:42 -04001134 if (ata_id_has_lba(dev->id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001135 const char *lba_desc;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001136
Tejun Heo4c2d7212006-03-05 17:55:58 +09001137 lba_desc = "LBA";
1138 dev->flags |= ATA_DFLAG_LBA;
1139 if (ata_id_has_lba48(dev->id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001140 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001141 lba_desc = "LBA48";
1142 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04001143
1144 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001145 if (print_info)
1146 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1147 "max %s, %Lu sectors: %s\n",
1148 ap->id, dev->devno,
1149 ata_id_major_version(dev->id),
1150 ata_mode_string(xfer_modes),
1151 (unsigned long long)dev->n_sectors,
1152 lba_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001153 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001154 /* CHS */
1155
1156 /* Default translation */
1157 dev->cylinders = dev->id[1];
1158 dev->heads = dev->id[3];
1159 dev->sectors = dev->id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001160
1161 if (ata_id_current_chs_valid(dev->id)) {
1162 /* Current CHS translation is valid. */
1163 dev->cylinders = dev->id[54];
1164 dev->heads = dev->id[55];
1165 dev->sectors = dev->id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001166 }
1167
1168 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001169 if (print_info)
1170 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1171 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1172 ap->id, dev->devno,
1173 ata_id_major_version(dev->id),
1174 ata_mode_string(xfer_modes),
1175 (unsigned long long)dev->n_sectors,
1176 dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178
Tejun Heo6e7846e2006-02-12 23:32:58 +09001179 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
1182 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001183 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 rc = atapi_cdb_len(dev->id);
1185 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1186 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001187 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 goto err_out_nosup;
1189 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001190 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001193 if (print_info)
1194 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1195 ap->id, dev->devno, ata_mode_string(xfer_modes));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197
Tejun Heo6e7846e2006-02-12 23:32:58 +09001198 ap->host->max_cmd_len = 0;
1199 for (i = 0; i < ATA_MAX_DEVICES; i++)
1200 ap->host->max_cmd_len = max_t(unsigned int,
1201 ap->host->max_cmd_len,
1202 ap->device[i].cdb_len);
1203
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001204 /* limit bridge transfers to udma5, 200 sectors */
1205 if (ata_dev_knobble(ap, dev)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001206 if (print_info)
1207 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1208 ap->id, dev->devno);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001209 ap->udma_mask &= ATA_UDMA5;
1210 dev->max_sectors = ATA_MAX_SECTORS;
1211 }
1212
1213 if (ap->ops->dev_config)
1214 ap->ops->dev_config(ap, dev);
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219err_out_nosup:
1220 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001221 ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001223 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226/**
1227 * ata_bus_probe - Reset and probe ATA bus
1228 * @ap: Bus to probe
1229 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001230 * Master ATA bus probing function. Initiates a hardware-dependent
1231 * bus reset, then attempts to identify any devices found on
1232 * the bus.
1233 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001235 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 *
1237 * RETURNS:
1238 * Zero on success, non-zero on error.
1239 */
1240
1241static int ata_bus_probe(struct ata_port *ap)
1242{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001243 unsigned int classes[ATA_MAX_DEVICES];
1244 unsigned int i, rc, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Tejun Heo28ca5c52006-03-01 16:09:36 +09001246 ata_port_probe(ap);
1247
1248 /* reset */
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001249 if (ap->ops->probe_reset) {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001250 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001251 if (rc) {
1252 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1253 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001254 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001255
1256 for (i = 0; i < ATA_MAX_DEVICES; i++)
1257 if (classes[i] == ATA_DEV_UNKNOWN)
1258 classes[i] = ATA_DEV_NONE;
1259 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001260 ap->ops->phy_reset(ap);
1261
Tejun Heo28ca5c52006-03-01 16:09:36 +09001262 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1263 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1264 classes[i] = ap->device[i].class;
1265 else
1266 ap->device[i].class = ATA_DEV_UNKNOWN;
1267 }
1268 ata_port_probe(ap);
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Tejun Heo28ca5c52006-03-01 16:09:36 +09001271 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoffeae412006-03-01 16:09:35 +09001273 struct ata_device *dev = &ap->device[i];
1274
Tejun Heo28ca5c52006-03-01 16:09:36 +09001275 dev->class = classes[i];
1276
Tejun Heoffeae412006-03-01 16:09:35 +09001277 if (!ata_dev_present(dev))
1278 continue;
1279
1280 WARN_ON(dev->id != NULL);
1281 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1282 dev->class = ATA_DEV_NONE;
1283 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
Tejun Heoffeae412006-03-01 16:09:35 +09001285
Tejun Heo4c2d7212006-03-05 17:55:58 +09001286 if (ata_dev_configure(ap, dev, 1)) {
Tejun Heoffeae412006-03-01 16:09:35 +09001287 dev->class++; /* disable device */
1288 continue;
1289 }
1290
Tejun Heoffeae412006-03-01 16:09:35 +09001291 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293
Tejun Heo28ca5c52006-03-01 16:09:36 +09001294 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 goto err_out_disable;
1296
1297 ata_set_mode(ap);
1298 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1299 goto err_out_disable;
1300
1301 return 0;
1302
1303err_out_disable:
1304 ap->ops->port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return -1;
1306}
1307
1308/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001309 * ata_port_probe - Mark port as enabled
1310 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001312 * Modify @ap data structure such that the system
1313 * thinks that the entire port is enabled.
1314 *
1315 * LOCKING: host_set lock, or some other form of
1316 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
1318
1319void ata_port_probe(struct ata_port *ap)
1320{
1321 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1322}
1323
1324/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001325 * sata_print_link_status - Print SATA link status
1326 * @ap: SATA port to printk link status about
1327 *
1328 * This function prints link speed and status of a SATA link.
1329 *
1330 * LOCKING:
1331 * None.
1332 */
1333static void sata_print_link_status(struct ata_port *ap)
1334{
1335 u32 sstatus, tmp;
1336 const char *speed;
1337
1338 if (!ap->ops->scr_read)
1339 return;
1340
1341 sstatus = scr_read(ap, SCR_STATUS);
1342
1343 if (sata_dev_present(ap)) {
1344 tmp = (sstatus >> 4) & 0xf;
1345 if (tmp & (1 << 0))
1346 speed = "1.5";
1347 else if (tmp & (1 << 1))
1348 speed = "3.0";
1349 else
1350 speed = "<unknown>";
1351 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1352 ap->id, speed, sstatus);
1353 } else {
1354 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1355 ap->id, sstatus);
1356 }
1357}
1358
1359/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001360 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1361 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001363 * This function issues commands to standard SATA Sxxx
1364 * PHY registers, to wake up the phy (and device), and
1365 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 *
1367 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001368 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 *
1370 */
1371void __sata_phy_reset(struct ata_port *ap)
1372{
1373 u32 sstatus;
1374 unsigned long timeout = jiffies + (HZ * 5);
1375
1376 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001377 /* issue phy wake/reset */
1378 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001379 /* Couldn't find anything in SATA I/II specs, but
1380 * AHCI-1.1 10.4.2 says at least 1 ms. */
1381 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Brett Russcdcca892005-03-28 15:10:27 -05001383 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* wait for phy to become ready, if necessary */
1386 do {
1387 msleep(200);
1388 sstatus = scr_read(ap, SCR_STATUS);
1389 if ((sstatus & 0xf) != 1)
1390 break;
1391 } while (time_before(jiffies, timeout));
1392
Tejun Heo3be680b2005-12-19 22:35:02 +09001393 /* print link status */
1394 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001395
Tejun Heo3be680b2005-12-19 22:35:02 +09001396 /* TODO: phy layer with polling, timeouts, etc. */
1397 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001399 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1403 return;
1404
1405 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1406 ata_port_disable(ap);
1407 return;
1408 }
1409
1410 ap->cbl = ATA_CBL_SATA;
1411}
1412
1413/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001414 * sata_phy_reset - Reset SATA bus.
1415 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001417 * This function resets the SATA bus, and then probes
1418 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 *
1420 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001421 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 *
1423 */
1424void sata_phy_reset(struct ata_port *ap)
1425{
1426 __sata_phy_reset(ap);
1427 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1428 return;
1429 ata_bus_reset(ap);
1430}
1431
1432/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001433 * ata_port_disable - Disable port.
1434 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001436 * Modify @ap data structure such that the system
1437 * thinks that the entire port is disabled, and should
1438 * never attempt to probe or communicate with devices
1439 * on this port.
1440 *
1441 * LOCKING: host_set lock, or some other form of
1442 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
1444
1445void ata_port_disable(struct ata_port *ap)
1446{
1447 ap->device[0].class = ATA_DEV_NONE;
1448 ap->device[1].class = ATA_DEV_NONE;
1449 ap->flags |= ATA_FLAG_PORT_DISABLED;
1450}
1451
Alan Cox452503f2005-10-21 19:01:32 -04001452/*
1453 * This mode timing computation functionality is ported over from
1454 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1455 */
1456/*
1457 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1458 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1459 * for PIO 5, which is a nonstandard extension and UDMA6, which
1460 * is currently supported only by Maxtor drives.
1461 */
1462
1463static const struct ata_timing ata_timing[] = {
1464
1465 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1466 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1467 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1468 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1469
1470 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1471 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1472 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1473
1474/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1475
1476 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1477 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1478 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1479
1480 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1481 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1482 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1483
1484/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1485 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1486 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1487
1488 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1489 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1490 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1491
1492/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1493
1494 { 0xFF }
1495};
1496
1497#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1498#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1499
1500static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1501{
1502 q->setup = EZ(t->setup * 1000, T);
1503 q->act8b = EZ(t->act8b * 1000, T);
1504 q->rec8b = EZ(t->rec8b * 1000, T);
1505 q->cyc8b = EZ(t->cyc8b * 1000, T);
1506 q->active = EZ(t->active * 1000, T);
1507 q->recover = EZ(t->recover * 1000, T);
1508 q->cycle = EZ(t->cycle * 1000, T);
1509 q->udma = EZ(t->udma * 1000, UT);
1510}
1511
1512void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1513 struct ata_timing *m, unsigned int what)
1514{
1515 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1516 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1517 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1518 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1519 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1520 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1521 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1522 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1523}
1524
1525static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1526{
1527 const struct ata_timing *t;
1528
1529 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001530 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001531 return NULL;
1532 return t;
1533}
1534
1535int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1536 struct ata_timing *t, int T, int UT)
1537{
1538 const struct ata_timing *s;
1539 struct ata_timing p;
1540
1541 /*
1542 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001543 */
Alan Cox452503f2005-10-21 19:01:32 -04001544
1545 if (!(s = ata_timing_find_mode(speed)))
1546 return -EINVAL;
1547
Albert Lee75b1f2f2005-11-16 17:06:18 +08001548 memcpy(t, s, sizeof(*s));
1549
Alan Cox452503f2005-10-21 19:01:32 -04001550 /*
1551 * If the drive is an EIDE drive, it can tell us it needs extended
1552 * PIO/MW_DMA cycle timing.
1553 */
1554
1555 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1556 memset(&p, 0, sizeof(p));
1557 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1558 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1559 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1560 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1561 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1562 }
1563 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1564 }
1565
1566 /*
1567 * Convert the timing to bus clock counts.
1568 */
1569
Albert Lee75b1f2f2005-11-16 17:06:18 +08001570 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001571
1572 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001573 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1574 * S.M.A.R.T * and some other commands. We have to ensure that the
1575 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001576 */
1577
1578 if (speed > XFER_PIO_4) {
1579 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1580 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1581 }
1582
1583 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001584 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001585 */
1586
1587 if (t->act8b + t->rec8b < t->cyc8b) {
1588 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1589 t->rec8b = t->cyc8b - t->act8b;
1590 }
1591
1592 if (t->active + t->recover < t->cycle) {
1593 t->active += (t->cycle - (t->active + t->recover)) / 2;
1594 t->recover = t->cycle - t->active;
1595 }
1596
1597 return 0;
1598}
1599
Jeff Garzik057ace52005-10-22 14:27:05 -04001600static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 unsigned int shift;
1602 u8 base;
1603} xfer_mode_classes[] = {
1604 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1605 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1606 { ATA_SHIFT_PIO, XFER_PIO_0 },
1607};
1608
Arjan van de Ven858119e2006-01-14 13:20:43 -08001609static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 int i;
1612
1613 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1614 if (xfer_mode_classes[i].shift == shift)
1615 return xfer_mode_classes[i].base;
1616
1617 return 0xff;
1618}
1619
1620static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1621{
1622 int ofs, idx;
1623 u8 base;
1624
1625 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1626 return;
1627
1628 if (dev->xfer_shift == ATA_SHIFT_PIO)
1629 dev->flags |= ATA_DFLAG_PIO;
1630
1631 ata_dev_set_xfermode(ap, dev);
1632
1633 base = base_from_shift(dev->xfer_shift);
1634 ofs = dev->xfer_mode - base;
1635 idx = ofs + dev->xfer_shift;
1636 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1637
Tejun Heo48a8a142006-03-05 17:55:58 +09001638 if (ata_dev_revalidate(ap, dev, 0)) {
1639 printk(KERN_ERR "ata%u: failed to revalidate after set "
1640 "xfermode, disabled\n", ap->id);
1641 ata_port_disable(ap);
1642 }
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1645 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1646
1647 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1648 ap->id, dev->devno, xfer_mode_str[idx]);
1649}
1650
1651static int ata_host_set_pio(struct ata_port *ap)
1652{
1653 unsigned int mask;
1654 int x, i;
1655 u8 base, xfer_mode;
1656
1657 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1658 x = fgb(mask);
1659 if (x < 0) {
1660 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1661 return -1;
1662 }
1663
1664 base = base_from_shift(ATA_SHIFT_PIO);
1665 xfer_mode = base + x;
1666
1667 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1668 (int)base, (int)xfer_mode, mask, x);
1669
1670 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1671 struct ata_device *dev = &ap->device[i];
1672 if (ata_dev_present(dev)) {
1673 dev->pio_mode = xfer_mode;
1674 dev->xfer_mode = xfer_mode;
1675 dev->xfer_shift = ATA_SHIFT_PIO;
1676 if (ap->ops->set_piomode)
1677 ap->ops->set_piomode(ap, dev);
1678 }
1679 }
1680
1681 return 0;
1682}
1683
1684static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1685 unsigned int xfer_shift)
1686{
1687 int i;
1688
1689 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1690 struct ata_device *dev = &ap->device[i];
1691 if (ata_dev_present(dev)) {
1692 dev->dma_mode = xfer_mode;
1693 dev->xfer_mode = xfer_mode;
1694 dev->xfer_shift = xfer_shift;
1695 if (ap->ops->set_dmamode)
1696 ap->ops->set_dmamode(ap, dev);
1697 }
1698 }
1699}
1700
1701/**
1702 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1703 * @ap: port on which timings will be programmed
1704 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001705 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1706 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001708 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 */
1710static void ata_set_mode(struct ata_port *ap)
1711{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001712 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 u8 xfer_mode;
1714 int rc;
1715
1716 /* step 1: always set host PIO timings */
1717 rc = ata_host_set_pio(ap);
1718 if (rc)
1719 goto err_out;
1720
1721 /* step 2: choose the best data xfer mode */
1722 xfer_mode = xfer_shift = 0;
1723 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1724 if (rc)
1725 goto err_out;
1726
1727 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1728 if (xfer_shift != ATA_SHIFT_PIO)
1729 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1730
1731 /* step 4: update devices' xfer mode */
1732 ata_dev_set_mode(ap, &ap->device[0]);
1733 ata_dev_set_mode(ap, &ap->device[1]);
1734
1735 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1736 return;
1737
1738 if (ap->ops->post_set_mode)
1739 ap->ops->post_set_mode(ap);
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return;
1742
1743err_out:
1744 ata_port_disable(ap);
1745}
1746
1747/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001748 * ata_tf_to_host - issue ATA taskfile to host controller
1749 * @ap: port to which command is being issued
1750 * @tf: ATA taskfile register set
1751 *
1752 * Issues ATA taskfile register set to ATA host controller,
1753 * with proper synchronization with interrupt handler and
1754 * other threads.
1755 *
1756 * LOCKING:
1757 * spin_lock_irqsave(host_set lock)
1758 */
1759
1760static inline void ata_tf_to_host(struct ata_port *ap,
1761 const struct ata_taskfile *tf)
1762{
1763 ap->ops->tf_load(ap, tf);
1764 ap->ops->exec_command(ap, tf);
1765}
1766
1767/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * ata_busy_sleep - sleep until BSY clears, or timeout
1769 * @ap: port containing status register to be polled
1770 * @tmout_pat: impatience timeout
1771 * @tmout: overall timeout
1772 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001773 * Sleep until ATA Status register bit BSY clears,
1774 * or a timeout occurs.
1775 *
1776 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 */
1778
Tejun Heo6f8b9952006-01-24 17:05:21 +09001779unsigned int ata_busy_sleep (struct ata_port *ap,
1780 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 unsigned long timer_start, timeout;
1783 u8 status;
1784
1785 status = ata_busy_wait(ap, ATA_BUSY, 300);
1786 timer_start = jiffies;
1787 timeout = timer_start + tmout_pat;
1788 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1789 msleep(50);
1790 status = ata_busy_wait(ap, ATA_BUSY, 3);
1791 }
1792
1793 if (status & ATA_BUSY)
1794 printk(KERN_WARNING "ata%u is slow to respond, "
1795 "please be patient\n", ap->id);
1796
1797 timeout = timer_start + tmout;
1798 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1799 msleep(50);
1800 status = ata_chk_status(ap);
1801 }
1802
1803 if (status & ATA_BUSY) {
1804 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1805 ap->id, tmout / HZ);
1806 return 1;
1807 }
1808
1809 return 0;
1810}
1811
1812static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1813{
1814 struct ata_ioports *ioaddr = &ap->ioaddr;
1815 unsigned int dev0 = devmask & (1 << 0);
1816 unsigned int dev1 = devmask & (1 << 1);
1817 unsigned long timeout;
1818
1819 /* if device 0 was found in ata_devchk, wait for its
1820 * BSY bit to clear
1821 */
1822 if (dev0)
1823 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1824
1825 /* if device 1 was found in ata_devchk, wait for
1826 * register access, then wait for BSY to clear
1827 */
1828 timeout = jiffies + ATA_TMOUT_BOOT;
1829 while (dev1) {
1830 u8 nsect, lbal;
1831
1832 ap->ops->dev_select(ap, 1);
1833 if (ap->flags & ATA_FLAG_MMIO) {
1834 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1835 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1836 } else {
1837 nsect = inb(ioaddr->nsect_addr);
1838 lbal = inb(ioaddr->lbal_addr);
1839 }
1840 if ((nsect == 1) && (lbal == 1))
1841 break;
1842 if (time_after(jiffies, timeout)) {
1843 dev1 = 0;
1844 break;
1845 }
1846 msleep(50); /* give drive a breather */
1847 }
1848 if (dev1)
1849 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1850
1851 /* is all this really necessary? */
1852 ap->ops->dev_select(ap, 0);
1853 if (dev1)
1854 ap->ops->dev_select(ap, 1);
1855 if (dev0)
1856 ap->ops->dev_select(ap, 0);
1857}
1858
1859/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001860 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1861 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001863 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1864 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 *
1866 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001867 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001868 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 *
1870 */
1871
1872static unsigned int ata_bus_edd(struct ata_port *ap)
1873{
1874 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001875 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /* set up execute-device-diag (bus reset) taskfile */
1878 /* also, take interrupts to a known state (disabled) */
1879 DPRINTK("execute-device-diag\n");
1880 ata_tf_init(ap, &tf, 0);
1881 tf.ctl |= ATA_NIEN;
1882 tf.command = ATA_CMD_EDD;
1883 tf.protocol = ATA_PROT_NODATA;
1884
1885 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001886 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001888 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 /* spec says at least 2ms. but who knows with those
1891 * crazy ATAPI devices...
1892 */
1893 msleep(150);
1894
1895 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1896}
1897
1898static unsigned int ata_bus_softreset(struct ata_port *ap,
1899 unsigned int devmask)
1900{
1901 struct ata_ioports *ioaddr = &ap->ioaddr;
1902
1903 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1904
1905 /* software reset. causes dev0 to be selected */
1906 if (ap->flags & ATA_FLAG_MMIO) {
1907 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1908 udelay(20); /* FIXME: flush */
1909 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1910 udelay(20); /* FIXME: flush */
1911 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1912 } else {
1913 outb(ap->ctl, ioaddr->ctl_addr);
1914 udelay(10);
1915 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1916 udelay(10);
1917 outb(ap->ctl, ioaddr->ctl_addr);
1918 }
1919
1920 /* spec mandates ">= 2ms" before checking status.
1921 * We wait 150ms, because that was the magic delay used for
1922 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1923 * between when the ATA command register is written, and then
1924 * status is checked. Because waiting for "a while" before
1925 * checking status is fine, post SRST, we perform this magic
1926 * delay here as well.
1927 */
1928 msleep(150);
1929
1930 ata_bus_post_reset(ap, devmask);
1931
1932 return 0;
1933}
1934
1935/**
1936 * ata_bus_reset - reset host port and associated ATA channel
1937 * @ap: port to reset
1938 *
1939 * This is typically the first time we actually start issuing
1940 * commands to the ATA channel. We wait for BSY to clear, then
1941 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1942 * result. Determine what devices, if any, are on the channel
1943 * by looking at the device 0/1 error register. Look at the signature
1944 * stored in each device's taskfile registers, to determine if
1945 * the device is ATA or ATAPI.
1946 *
1947 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001948 * PCI/etc. bus probe sem.
1949 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 *
1951 * SIDE EFFECTS:
1952 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1953 */
1954
1955void ata_bus_reset(struct ata_port *ap)
1956{
1957 struct ata_ioports *ioaddr = &ap->ioaddr;
1958 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1959 u8 err;
1960 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1961
1962 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1963
1964 /* determine if device 0/1 are present */
1965 if (ap->flags & ATA_FLAG_SATA_RESET)
1966 dev0 = 1;
1967 else {
1968 dev0 = ata_devchk(ap, 0);
1969 if (slave_possible)
1970 dev1 = ata_devchk(ap, 1);
1971 }
1972
1973 if (dev0)
1974 devmask |= (1 << 0);
1975 if (dev1)
1976 devmask |= (1 << 1);
1977
1978 /* select device 0 again */
1979 ap->ops->dev_select(ap, 0);
1980
1981 /* issue bus reset */
1982 if (ap->flags & ATA_FLAG_SRST)
1983 rc = ata_bus_softreset(ap, devmask);
1984 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1985 /* set up device control */
1986 if (ap->flags & ATA_FLAG_MMIO)
1987 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1988 else
1989 outb(ap->ctl, ioaddr->ctl_addr);
1990 rc = ata_bus_edd(ap);
1991 }
1992
1993 if (rc)
1994 goto err_out;
1995
1996 /*
1997 * determine by signature whether we have ATA or ATAPI devices
1998 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001999 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002001 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 /* re-enable interrupts */
2004 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2005 ata_irq_on(ap);
2006
2007 /* is double-select really necessary? */
2008 if (ap->device[1].class != ATA_DEV_NONE)
2009 ap->ops->dev_select(ap, 1);
2010 if (ap->device[0].class != ATA_DEV_NONE)
2011 ap->ops->dev_select(ap, 0);
2012
2013 /* if no devices were detected, disable this port */
2014 if ((ap->device[0].class == ATA_DEV_NONE) &&
2015 (ap->device[1].class == ATA_DEV_NONE))
2016 goto err_out;
2017
2018 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2019 /* set up device control for ATA_FLAG_SATA_RESET */
2020 if (ap->flags & ATA_FLAG_MMIO)
2021 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2022 else
2023 outb(ap->ctl, ioaddr->ctl_addr);
2024 }
2025
2026 DPRINTK("EXIT\n");
2027 return;
2028
2029err_out:
2030 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2031 ap->ops->port_disable(ap);
2032
2033 DPRINTK("EXIT\n");
2034}
2035
Tejun Heo7a7921e2006-02-02 18:20:00 +09002036static int sata_phy_resume(struct ata_port *ap)
2037{
2038 unsigned long timeout = jiffies + (HZ * 5);
2039 u32 sstatus;
2040
2041 scr_write_flush(ap, SCR_CONTROL, 0x300);
2042
2043 /* Wait for phy to become ready, if necessary. */
2044 do {
2045 msleep(200);
2046 sstatus = scr_read(ap, SCR_STATUS);
2047 if ((sstatus & 0xf) != 1)
2048 return 0;
2049 } while (time_before(jiffies, timeout));
2050
2051 return -1;
2052}
2053
Tejun Heoc2bd5802006-01-24 17:05:22 +09002054/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002055 * ata_std_probeinit - initialize probing
2056 * @ap: port to be probed
2057 *
2058 * @ap is about to be probed. Initialize it. This function is
2059 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002060 *
2061 * NOTE!!! Do not use this function as probeinit if a low level
2062 * driver implements only hardreset. Just pass NULL as probeinit
2063 * in that case. Using this function is probably okay but doing
2064 * so makes reset sequence different from the original
2065 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002066 */
2067extern void ata_std_probeinit(struct ata_port *ap)
2068{
Tejun Heo3a397462006-02-10 23:58:48 +09002069 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002070 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002071 if (sata_dev_present(ap))
2072 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2073 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002074}
2075
2076/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002077 * ata_std_softreset - reset host port via ATA SRST
2078 * @ap: port to reset
2079 * @verbose: fail verbosely
2080 * @classes: resulting classes of attached devices
2081 *
2082 * Reset host port using ATA SRST. This function is to be used
2083 * as standard callback for ata_drive_*_reset() functions.
2084 *
2085 * LOCKING:
2086 * Kernel thread context (may sleep)
2087 *
2088 * RETURNS:
2089 * 0 on success, -errno otherwise.
2090 */
2091int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2092{
2093 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2094 unsigned int devmask = 0, err_mask;
2095 u8 err;
2096
2097 DPRINTK("ENTER\n");
2098
Tejun Heo3a397462006-02-10 23:58:48 +09002099 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2100 classes[0] = ATA_DEV_NONE;
2101 goto out;
2102 }
2103
Tejun Heoc2bd5802006-01-24 17:05:22 +09002104 /* determine if device 0/1 are present */
2105 if (ata_devchk(ap, 0))
2106 devmask |= (1 << 0);
2107 if (slave_possible && ata_devchk(ap, 1))
2108 devmask |= (1 << 1);
2109
Tejun Heoc2bd5802006-01-24 17:05:22 +09002110 /* select device 0 again */
2111 ap->ops->dev_select(ap, 0);
2112
2113 /* issue bus reset */
2114 DPRINTK("about to softreset, devmask=%x\n", devmask);
2115 err_mask = ata_bus_softreset(ap, devmask);
2116 if (err_mask) {
2117 if (verbose)
2118 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2119 ap->id, err_mask);
2120 else
2121 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2122 err_mask);
2123 return -EIO;
2124 }
2125
2126 /* determine by signature whether we have ATA or ATAPI devices */
2127 classes[0] = ata_dev_try_classify(ap, 0, &err);
2128 if (slave_possible && err != 0x81)
2129 classes[1] = ata_dev_try_classify(ap, 1, &err);
2130
Tejun Heo3a397462006-02-10 23:58:48 +09002131 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002132 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2133 return 0;
2134}
2135
2136/**
2137 * sata_std_hardreset - reset host port via SATA phy reset
2138 * @ap: port to reset
2139 * @verbose: fail verbosely
2140 * @class: resulting class of attached device
2141 *
2142 * SATA phy-reset host port using DET bits of SControl register.
2143 * This function is to be used as standard callback for
2144 * ata_drive_*_reset().
2145 *
2146 * LOCKING:
2147 * Kernel thread context (may sleep)
2148 *
2149 * RETURNS:
2150 * 0 on success, -errno otherwise.
2151 */
2152int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2153{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002154 DPRINTK("ENTER\n");
2155
2156 /* Issue phy wake/reset */
2157 scr_write_flush(ap, SCR_CONTROL, 0x301);
2158
2159 /*
2160 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2161 * 10.4.2 says at least 1 ms.
2162 */
2163 msleep(1);
2164
Tejun Heo7a7921e2006-02-02 18:20:00 +09002165 /* Bring phy back */
2166 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002167
Tejun Heoc2bd5802006-01-24 17:05:22 +09002168 /* TODO: phy layer with polling, timeouts, etc. */
2169 if (!sata_dev_present(ap)) {
2170 *class = ATA_DEV_NONE;
2171 DPRINTK("EXIT, link offline\n");
2172 return 0;
2173 }
2174
2175 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2176 if (verbose)
2177 printk(KERN_ERR "ata%u: COMRESET failed "
2178 "(device not ready)\n", ap->id);
2179 else
2180 DPRINTK("EXIT, device not ready\n");
2181 return -EIO;
2182 }
2183
Tejun Heo3a397462006-02-10 23:58:48 +09002184 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2185
Tejun Heoc2bd5802006-01-24 17:05:22 +09002186 *class = ata_dev_try_classify(ap, 0, NULL);
2187
2188 DPRINTK("EXIT, class=%u\n", *class);
2189 return 0;
2190}
2191
2192/**
2193 * ata_std_postreset - standard postreset callback
2194 * @ap: the target ata_port
2195 * @classes: classes of attached devices
2196 *
2197 * This function is invoked after a successful reset. Note that
2198 * the device might have been reset more than once using
2199 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002200 *
2201 * This function is to be used as standard callback for
2202 * ata_drive_*_reset().
2203 *
2204 * LOCKING:
2205 * Kernel thread context (may sleep)
2206 */
2207void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2208{
2209 DPRINTK("ENTER\n");
2210
Tejun Heo56497bd2006-02-15 15:01:42 +09002211 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002212 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2213 ap->cbl = ATA_CBL_SATA;
2214
2215 /* print link status */
2216 if (ap->cbl == ATA_CBL_SATA)
2217 sata_print_link_status(ap);
2218
Tejun Heo3a397462006-02-10 23:58:48 +09002219 /* re-enable interrupts */
2220 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2221 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002222
2223 /* is double-select really necessary? */
2224 if (classes[0] != ATA_DEV_NONE)
2225 ap->ops->dev_select(ap, 1);
2226 if (classes[1] != ATA_DEV_NONE)
2227 ap->ops->dev_select(ap, 0);
2228
Tejun Heo3a397462006-02-10 23:58:48 +09002229 /* bail out if no device is present */
2230 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2231 DPRINTK("EXIT, no device\n");
2232 return;
2233 }
2234
2235 /* set up device control */
2236 if (ap->ioaddr.ctl_addr) {
2237 if (ap->flags & ATA_FLAG_MMIO)
2238 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2239 else
2240 outb(ap->ctl, ap->ioaddr.ctl_addr);
2241 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002242
2243 DPRINTK("EXIT\n");
2244}
2245
2246/**
2247 * ata_std_probe_reset - standard probe reset method
2248 * @ap: prot to perform probe-reset
2249 * @classes: resulting classes of attached devices
2250 *
2251 * The stock off-the-shelf ->probe_reset method.
2252 *
2253 * LOCKING:
2254 * Kernel thread context (may sleep)
2255 *
2256 * RETURNS:
2257 * 0 on success, -errno otherwise.
2258 */
2259int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2260{
2261 ata_reset_fn_t hardreset;
2262
2263 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002264 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002265 hardreset = sata_std_hardreset;
2266
Tejun Heo8a19ac82006-02-02 18:20:00 +09002267 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002268 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002269 ata_std_postreset, classes);
2270}
2271
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002272static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2273 ata_postreset_fn_t postreset,
2274 unsigned int *classes)
2275{
2276 int i, rc;
2277
2278 for (i = 0; i < ATA_MAX_DEVICES; i++)
2279 classes[i] = ATA_DEV_UNKNOWN;
2280
2281 rc = reset(ap, 0, classes);
2282 if (rc)
2283 return rc;
2284
2285 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2286 * is complete and convert all ATA_DEV_UNKNOWN to
2287 * ATA_DEV_NONE.
2288 */
2289 for (i = 0; i < ATA_MAX_DEVICES; i++)
2290 if (classes[i] != ATA_DEV_UNKNOWN)
2291 break;
2292
2293 if (i < ATA_MAX_DEVICES)
2294 for (i = 0; i < ATA_MAX_DEVICES; i++)
2295 if (classes[i] == ATA_DEV_UNKNOWN)
2296 classes[i] = ATA_DEV_NONE;
2297
2298 if (postreset)
2299 postreset(ap, classes);
2300
2301 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2302}
2303
2304/**
2305 * ata_drive_probe_reset - Perform probe reset with given methods
2306 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002307 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002308 * @softreset: softreset method (can be NULL)
2309 * @hardreset: hardreset method (can be NULL)
2310 * @postreset: postreset method (can be NULL)
2311 * @classes: resulting classes of attached devices
2312 *
2313 * Reset the specified port and classify attached devices using
2314 * given methods. This function prefers softreset but tries all
2315 * possible reset sequences to reset and classify devices. This
2316 * function is intended to be used for constructing ->probe_reset
2317 * callback by low level drivers.
2318 *
2319 * Reset methods should follow the following rules.
2320 *
2321 * - Return 0 on sucess, -errno on failure.
2322 * - If classification is supported, fill classes[] with
2323 * recognized class codes.
2324 * - If classification is not supported, leave classes[] alone.
2325 * - If verbose is non-zero, print error message on failure;
2326 * otherwise, shut up.
2327 *
2328 * LOCKING:
2329 * Kernel thread context (may sleep)
2330 *
2331 * RETURNS:
2332 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2333 * if classification fails, and any error code from reset
2334 * methods.
2335 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002336int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002337 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2338 ata_postreset_fn_t postreset, unsigned int *classes)
2339{
2340 int rc = -EINVAL;
2341
Tejun Heo7944ea92006-02-02 18:20:00 +09002342 if (probeinit)
2343 probeinit(ap);
2344
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002345 if (softreset) {
2346 rc = do_probe_reset(ap, softreset, postreset, classes);
2347 if (rc == 0)
2348 return 0;
2349 }
2350
2351 if (!hardreset)
2352 return rc;
2353
2354 rc = do_probe_reset(ap, hardreset, postreset, classes);
2355 if (rc == 0 || rc != -ENODEV)
2356 return rc;
2357
2358 if (softreset)
2359 rc = do_probe_reset(ap, softreset, postreset, classes);
2360
2361 return rc;
2362}
2363
Tejun Heo623a3122006-03-05 17:55:58 +09002364/**
2365 * ata_dev_same_device - Determine whether new ID matches configured device
2366 * @ap: port on which the device to compare against resides
2367 * @dev: device to compare against
2368 * @new_class: class of the new device
2369 * @new_id: IDENTIFY page of the new device
2370 *
2371 * Compare @new_class and @new_id against @dev and determine
2372 * whether @dev is the device indicated by @new_class and
2373 * @new_id.
2374 *
2375 * LOCKING:
2376 * None.
2377 *
2378 * RETURNS:
2379 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2380 */
2381static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2382 unsigned int new_class, const u16 *new_id)
2383{
2384 const u16 *old_id = dev->id;
2385 unsigned char model[2][41], serial[2][21];
2386 u64 new_n_sectors;
2387
2388 if (dev->class != new_class) {
2389 printk(KERN_INFO
2390 "ata%u: dev %u class mismatch %d != %d\n",
2391 ap->id, dev->devno, dev->class, new_class);
2392 return 0;
2393 }
2394
2395 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2396 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2397 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2398 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2399 new_n_sectors = ata_id_n_sectors(new_id);
2400
2401 if (strcmp(model[0], model[1])) {
2402 printk(KERN_INFO
2403 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2404 ap->id, dev->devno, model[0], model[1]);
2405 return 0;
2406 }
2407
2408 if (strcmp(serial[0], serial[1])) {
2409 printk(KERN_INFO
2410 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2411 ap->id, dev->devno, serial[0], serial[1]);
2412 return 0;
2413 }
2414
2415 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2416 printk(KERN_INFO
2417 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2418 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2419 (unsigned long long)new_n_sectors);
2420 return 0;
2421 }
2422
2423 return 1;
2424}
2425
2426/**
2427 * ata_dev_revalidate - Revalidate ATA device
2428 * @ap: port on which the device to revalidate resides
2429 * @dev: device to revalidate
2430 * @post_reset: is this revalidation after reset?
2431 *
2432 * Re-read IDENTIFY page and make sure @dev is still attached to
2433 * the port.
2434 *
2435 * LOCKING:
2436 * Kernel thread context (may sleep)
2437 *
2438 * RETURNS:
2439 * 0 on success, negative errno otherwise
2440 */
2441int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2442 int post_reset)
2443{
2444 unsigned int class;
2445 u16 *id;
2446 int rc;
2447
2448 if (!ata_dev_present(dev))
2449 return -ENODEV;
2450
2451 class = dev->class;
2452 id = NULL;
2453
2454 /* allocate & read ID data */
2455 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2456 if (rc)
2457 goto fail;
2458
2459 /* is the device still there? */
2460 if (!ata_dev_same_device(ap, dev, class, id)) {
2461 rc = -ENODEV;
2462 goto fail;
2463 }
2464
2465 kfree(dev->id);
2466 dev->id = id;
2467
2468 /* configure device according to the new ID */
2469 return ata_dev_configure(ap, dev, 0);
2470
2471 fail:
2472 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2473 ap->id, dev->devno, rc);
2474 kfree(id);
2475 return rc;
2476}
2477
Jeff Garzik057ace52005-10-22 14:27:05 -04002478static void ata_pr_blacklisted(const struct ata_port *ap,
2479 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2482 ap->id, dev->devno);
2483}
2484
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002485static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 "WDC AC11000H",
2487 "WDC AC22100H",
2488 "WDC AC32500H",
2489 "WDC AC33100H",
2490 "WDC AC31600H",
2491 "WDC AC32100H",
2492 "WDC AC23200L",
2493 "Compaq CRD-8241B",
2494 "CRD-8400B",
2495 "CRD-8480B",
2496 "CRD-8482B",
2497 "CRD-84",
2498 "SanDisk SDP3B",
2499 "SanDisk SDP3B-64",
2500 "SANYO CD-ROM CRD",
2501 "HITACHI CDR-8",
2502 "HITACHI CDR-8335",
2503 "HITACHI CDR-8435",
2504 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002505 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 "CD-532E-A",
2507 "E-IDE CD-ROM CR-840",
2508 "CD-ROM Drive/F5A",
2509 "WPI CDD-820",
2510 "SAMSUNG CD-ROM SC-148C",
2511 "SAMSUNG CD-ROM SC",
2512 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2514 "_NEC DV5800A",
2515};
2516
Jeff Garzik057ace52005-10-22 14:27:05 -04002517static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Tejun Heo2e026712006-02-12 22:47:04 +09002519 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 int i;
2521
Tejun Heo6a62a042006-02-13 10:02:46 +09002522 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002525 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return 1;
2527
2528 return 0;
2529}
2530
Jeff Garzik057ace52005-10-22 14:27:05 -04002531static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Jeff Garzik057ace52005-10-22 14:27:05 -04002533 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 unsigned int mask;
2535
2536 master = &ap->device[0];
2537 slave = &ap->device[1];
2538
Tejun Heoa4631472006-02-11 19:11:13 +09002539 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 if (shift == ATA_SHIFT_UDMA) {
2542 mask = ap->udma_mask;
2543 if (ata_dev_present(master)) {
2544 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002545 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 mask = 0;
2547 ata_pr_blacklisted(ap, master);
2548 }
2549 }
2550 if (ata_dev_present(slave)) {
2551 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002552 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 mask = 0;
2554 ata_pr_blacklisted(ap, slave);
2555 }
2556 }
2557 }
2558 else if (shift == ATA_SHIFT_MWDMA) {
2559 mask = ap->mwdma_mask;
2560 if (ata_dev_present(master)) {
2561 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002562 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 mask = 0;
2564 ata_pr_blacklisted(ap, master);
2565 }
2566 }
2567 if (ata_dev_present(slave)) {
2568 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002569 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 mask = 0;
2571 ata_pr_blacklisted(ap, slave);
2572 }
2573 }
2574 }
2575 else if (shift == ATA_SHIFT_PIO) {
2576 mask = ap->pio_mask;
2577 if (ata_dev_present(master)) {
2578 /* spec doesn't return explicit support for
2579 * PIO0-2, so we fake it
2580 */
2581 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2582 tmp_mode <<= 3;
2583 tmp_mode |= 0x7;
2584 mask &= tmp_mode;
2585 }
2586 if (ata_dev_present(slave)) {
2587 /* spec doesn't return explicit support for
2588 * PIO0-2, so we fake it
2589 */
2590 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2591 tmp_mode <<= 3;
2592 tmp_mode |= 0x7;
2593 mask &= tmp_mode;
2594 }
2595 }
2596 else {
2597 mask = 0xffffffff; /* shut up compiler warning */
2598 BUG();
2599 }
2600
2601 return mask;
2602}
2603
2604/* find greatest bit */
2605static int fgb(u32 bitmap)
2606{
2607 unsigned int i;
2608 int x = -1;
2609
2610 for (i = 0; i < 32; i++)
2611 if (bitmap & (1 << i))
2612 x = i;
2613
2614 return x;
2615}
2616
2617/**
2618 * ata_choose_xfer_mode - attempt to find best transfer mode
2619 * @ap: Port for which an xfer mode will be selected
2620 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2621 * @xfer_shift_out: (output) bit shift that selects this mode
2622 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002623 * Based on host and device capabilities, determine the
2624 * maximum transfer mode that is amenable to all.
2625 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002627 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 *
2629 * RETURNS:
2630 * Zero on success, negative on error.
2631 */
2632
Jeff Garzik057ace52005-10-22 14:27:05 -04002633static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 u8 *xfer_mode_out,
2635 unsigned int *xfer_shift_out)
2636{
2637 unsigned int mask, shift;
2638 int x, i;
2639
2640 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2641 shift = xfer_mode_classes[i].shift;
2642 mask = ata_get_mode_mask(ap, shift);
2643
2644 x = fgb(mask);
2645 if (x >= 0) {
2646 *xfer_mode_out = xfer_mode_classes[i].base + x;
2647 *xfer_shift_out = shift;
2648 return 0;
2649 }
2650 }
2651
2652 return -1;
2653}
2654
2655/**
2656 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2657 * @ap: Port associated with device @dev
2658 * @dev: Device to which command will be sent
2659 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002660 * Issue SET FEATURES - XFER MODE command to device @dev
2661 * on port @ap.
2662 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002664 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 */
2666
2667static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2668{
Tejun Heoa0123702005-12-13 14:49:31 +09002669 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 /* set up set-features taskfile */
2672 DPRINTK("set features - xfer mode\n");
2673
Tejun Heoa0123702005-12-13 14:49:31 +09002674 ata_tf_init(ap, &tf, dev->devno);
2675 tf.command = ATA_CMD_SET_FEATURES;
2676 tf.feature = SETFEATURES_XFER;
2677 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2678 tf.protocol = ATA_PROT_NODATA;
2679 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Tejun Heoa0123702005-12-13 14:49:31 +09002681 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2682 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2683 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 DPRINTK("EXIT\n");
2688}
2689
2690/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002691 * ata_dev_init_params - Issue INIT DEV PARAMS command
2692 * @ap: Port associated with device @dev
2693 * @dev: Device to which command will be sent
2694 *
2695 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002696 * Kernel thread context (may sleep)
2697 *
2698 * RETURNS:
2699 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04002700 */
2701
Tejun Heo6aff8f12006-02-15 18:24:09 +09002702static unsigned int ata_dev_init_params(struct ata_port *ap,
2703 struct ata_device *dev)
Albert Lee8bf62ec2005-05-12 15:29:42 -04002704{
Tejun Heoa0123702005-12-13 14:49:31 +09002705 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002706 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002707 u16 sectors = dev->id[6];
2708 u16 heads = dev->id[3];
2709
2710 /* Number of sectors per track 1-255. Number of heads 1-16 */
2711 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002712 return 0;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002713
2714 /* set up init dev params taskfile */
2715 DPRINTK("init dev params \n");
2716
Tejun Heoa0123702005-12-13 14:49:31 +09002717 ata_tf_init(ap, &tf, dev->devno);
2718 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2719 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2720 tf.protocol = ATA_PROT_NODATA;
2721 tf.nsect = sectors;
2722 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002723
Tejun Heo6aff8f12006-02-15 18:24:09 +09002724 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002725
Tejun Heo6aff8f12006-02-15 18:24:09 +09002726 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2727 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002728}
2729
2730/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002731 * ata_sg_clean - Unmap DMA memory associated with command
2732 * @qc: Command containing DMA memory to be released
2733 *
2734 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 *
2736 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002737 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 */
2739
2740static void ata_sg_clean(struct ata_queued_cmd *qc)
2741{
2742 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002743 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002745 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Tejun Heoa4631472006-02-11 19:11:13 +09002747 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2748 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002751 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002753 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002755 /* if we padded the buffer out to 32-bit bound, and data
2756 * xfer direction is from-device, we must copy from the
2757 * pad buffer back into the supplied buffer
2758 */
2759 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2760 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2761
2762 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002763 if (qc->n_elem)
2764 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002765 /* restore last sg */
2766 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2767 if (pad_buf) {
2768 struct scatterlist *psg = &qc->pad_sgent;
2769 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2770 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002771 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002772 }
2773 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002774 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002775 dma_unmap_single(ap->host_set->dev,
2776 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2777 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002778 /* restore sg */
2779 sg->length += qc->pad_len;
2780 if (pad_buf)
2781 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2782 pad_buf, qc->pad_len);
2783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002786 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789/**
2790 * ata_fill_sg - Fill PCI IDE PRD table
2791 * @qc: Metadata associated with taskfile to be transferred
2792 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002793 * Fill PCI IDE PRD (scatter-gather) table with segments
2794 * associated with the current disk command.
2795 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002797 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 *
2799 */
2800static void ata_fill_sg(struct ata_queued_cmd *qc)
2801{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002803 struct scatterlist *sg;
2804 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Tejun Heoa4631472006-02-11 19:11:13 +09002806 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002807 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002810 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 u32 addr, offset;
2812 u32 sg_len, len;
2813
2814 /* determine if physical DMA addr spans 64K boundary.
2815 * Note h/w doesn't support 64-bit, so we unconditionally
2816 * truncate dma_addr_t to u32.
2817 */
2818 addr = (u32) sg_dma_address(sg);
2819 sg_len = sg_dma_len(sg);
2820
2821 while (sg_len) {
2822 offset = addr & 0xffff;
2823 len = sg_len;
2824 if ((offset + sg_len) > 0x10000)
2825 len = 0x10000 - offset;
2826
2827 ap->prd[idx].addr = cpu_to_le32(addr);
2828 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2829 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2830
2831 idx++;
2832 sg_len -= len;
2833 addr += len;
2834 }
2835 }
2836
2837 if (idx)
2838 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2839}
2840/**
2841 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2842 * @qc: Metadata associated with taskfile to check
2843 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002844 * Allow low-level driver to filter ATA PACKET commands, returning
2845 * a status indicating whether or not it is OK to use DMA for the
2846 * supplied PACKET command.
2847 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002849 * spin_lock_irqsave(host_set lock)
2850 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 * RETURNS: 0 when ATAPI DMA can be used
2852 * nonzero otherwise
2853 */
2854int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2855{
2856 struct ata_port *ap = qc->ap;
2857 int rc = 0; /* Assume ATAPI DMA is OK by default */
2858
2859 if (ap->ops->check_atapi_dma)
2860 rc = ap->ops->check_atapi_dma(qc);
2861
2862 return rc;
2863}
2864/**
2865 * ata_qc_prep - Prepare taskfile for submission
2866 * @qc: Metadata associated with taskfile to be prepared
2867 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002868 * Prepare ATA taskfile for submission.
2869 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 * LOCKING:
2871 * spin_lock_irqsave(host_set lock)
2872 */
2873void ata_qc_prep(struct ata_queued_cmd *qc)
2874{
2875 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2876 return;
2877
2878 ata_fill_sg(qc);
2879}
2880
Jeff Garzik0cba6322005-05-30 19:49:12 -04002881/**
2882 * ata_sg_init_one - Associate command with memory buffer
2883 * @qc: Command to be associated
2884 * @buf: Memory buffer
2885 * @buflen: Length of memory buffer, in bytes.
2886 *
2887 * Initialize the data-related elements of queued_cmd @qc
2888 * to point to a single memory buffer, @buf of byte length @buflen.
2889 *
2890 * LOCKING:
2891 * spin_lock_irqsave(host_set lock)
2892 */
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2895{
2896 struct scatterlist *sg;
2897
2898 qc->flags |= ATA_QCFLAG_SINGLE;
2899
2900 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002901 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002903 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 qc->buf_virt = buf;
2905
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002906 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002907 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908}
2909
Jeff Garzik0cba6322005-05-30 19:49:12 -04002910/**
2911 * ata_sg_init - Associate command with scatter-gather table.
2912 * @qc: Command to be associated
2913 * @sg: Scatter-gather table.
2914 * @n_elem: Number of elements in s/g table.
2915 *
2916 * Initialize the data-related elements of queued_cmd @qc
2917 * to point to a scatter-gather table @sg, containing @n_elem
2918 * elements.
2919 *
2920 * LOCKING:
2921 * spin_lock_irqsave(host_set lock)
2922 */
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2925 unsigned int n_elem)
2926{
2927 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002928 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002930 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
2933/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002934 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2935 * @qc: Command with memory buffer to be mapped.
2936 *
2937 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 *
2939 * LOCKING:
2940 * spin_lock_irqsave(host_set lock)
2941 *
2942 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002943 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 */
2945
2946static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2947{
2948 struct ata_port *ap = qc->ap;
2949 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002950 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002952 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002954 /* we must lengthen transfers to end on a 32-bit boundary */
2955 qc->pad_len = sg->length & 3;
2956 if (qc->pad_len) {
2957 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2958 struct scatterlist *psg = &qc->pad_sgent;
2959
Tejun Heoa4631472006-02-11 19:11:13 +09002960 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002961
2962 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2963
2964 if (qc->tf.flags & ATA_TFLAG_WRITE)
2965 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2966 qc->pad_len);
2967
2968 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2969 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2970 /* trim sg */
2971 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002972 if (sg->length == 0)
2973 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002974
2975 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2976 sg->length, qc->pad_len);
2977 }
2978
Tejun Heo2e242fa2006-02-20 23:48:38 +09002979 if (trim_sg) {
2980 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05002981 goto skip_map;
2982 }
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002985 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002986 if (dma_mapping_error(dma_address)) {
2987 /* restore sg */
2988 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002993 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Tejun Heo2e242fa2006-02-20 23:48:38 +09002995skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2997 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2998
2999 return 0;
3000}
3001
3002/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003003 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3004 * @qc: Command with scatter-gather table to be mapped.
3005 *
3006 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 *
3008 * LOCKING:
3009 * spin_lock_irqsave(host_set lock)
3010 *
3011 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003012 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 *
3014 */
3015
3016static int ata_sg_setup(struct ata_queued_cmd *qc)
3017{
3018 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003019 struct scatterlist *sg = qc->__sg;
3020 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003021 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003024 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003026 /* we must lengthen transfers to end on a 32-bit boundary */
3027 qc->pad_len = lsg->length & 3;
3028 if (qc->pad_len) {
3029 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3030 struct scatterlist *psg = &qc->pad_sgent;
3031 unsigned int offset;
3032
Tejun Heoa4631472006-02-11 19:11:13 +09003033 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003034
3035 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3036
3037 /*
3038 * psg->page/offset are used to copy to-be-written
3039 * data in this function or read data in ata_sg_clean.
3040 */
3041 offset = lsg->offset + lsg->length - qc->pad_len;
3042 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3043 psg->offset = offset_in_page(offset);
3044
3045 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3046 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3047 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003048 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003049 }
3050
3051 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3052 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3053 /* trim last sg */
3054 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003055 if (lsg->length == 0)
3056 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003057
3058 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3059 qc->n_elem - 1, lsg->length, qc->pad_len);
3060 }
3061
Jeff Garzike1410f22005-11-14 14:06:26 -05003062 pre_n_elem = qc->n_elem;
3063 if (trim_sg && pre_n_elem)
3064 pre_n_elem--;
3065
3066 if (!pre_n_elem) {
3067 n_elem = 0;
3068 goto skip_map;
3069 }
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05003072 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003073 if (n_elem < 1) {
3074 /* restore last sg */
3075 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 DPRINTK("%d sg elements mapped\n", n_elem);
3080
Jeff Garzike1410f22005-11-14 14:06:26 -05003081skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 qc->n_elem = n_elem;
3083
3084 return 0;
3085}
3086
3087/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003088 * ata_poll_qc_complete - turn irq back on and finish qc
3089 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003090 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003091 *
3092 * LOCKING:
3093 * None. (grabs host lock)
3094 */
3095
Albert Leea22e2eb2005-12-05 15:38:02 +08003096void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003097{
3098 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003099 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003100
Jeff Garzikb8f61532005-08-25 22:01:20 -04003101 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003102 ap->flags &= ~ATA_FLAG_NOINTR;
3103 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003104 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003105 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003106}
3107
3108/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003109 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003110 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 *
3112 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003113 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 *
3115 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003116 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 */
3118
3119static unsigned long ata_pio_poll(struct ata_port *ap)
3120{
Albert Leec14b8332005-12-05 15:36:08 +08003121 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003123 unsigned int poll_state = HSM_ST_UNKNOWN;
3124 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Albert Leec14b8332005-12-05 15:36:08 +08003126 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003127 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003128
Albert Lee14be71f2005-09-27 17:36:35 +08003129 switch (ap->hsm_task_state) {
3130 case HSM_ST:
3131 case HSM_ST_POLL:
3132 poll_state = HSM_ST_POLL;
3133 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003135 case HSM_ST_LAST:
3136 case HSM_ST_LAST_POLL:
3137 poll_state = HSM_ST_LAST_POLL;
3138 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 break;
3140 default:
3141 BUG();
3142 break;
3143 }
3144
3145 status = ata_chk_status(ap);
3146 if (status & ATA_BUSY) {
3147 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003148 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003149 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return 0;
3151 }
Albert Lee14be71f2005-09-27 17:36:35 +08003152 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return ATA_SHORT_PAUSE;
3154 }
3155
Albert Lee14be71f2005-09-27 17:36:35 +08003156 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 return 0;
3158}
3159
3160/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003161 * ata_pio_complete - check if drive is busy or idle
3162 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 *
3164 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003165 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003166 *
3167 * RETURNS:
3168 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 */
3170
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003171static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
3173 struct ata_queued_cmd *qc;
3174 u8 drv_stat;
3175
3176 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003177 * This is purely heuristic. This is a fast path. Sometimes when
3178 * we enter, BSY will be cleared in a chk-status or two. If not,
3179 * the drive is probably seeking or something. Snooze for a couple
3180 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003181 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 */
Albert Leefe79e682005-12-06 11:34:59 +08003183 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3184 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003186 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3187 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003188 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192 }
3193
Albert Leec14b8332005-12-05 15:36:08 +08003194 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003195 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 drv_stat = ata_wait_idle(ap);
3198 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003199 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003200 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
3203
Albert Lee14be71f2005-09-27 17:36:35 +08003204 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Tejun Heoa4631472006-02-11 19:11:13 +09003206 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003207 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003208
3209 /* another command may start at this point */
3210
3211 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212}
3213
Edward Falk0baab862005-06-02 18:17:13 -04003214
3215/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003216 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003217 * @buf: Buffer to swap
3218 * @buf_words: Number of 16-bit words in buffer.
3219 *
3220 * Swap halves of 16-bit words if needed to convert from
3221 * little-endian byte order to native cpu byte order, or
3222 * vice-versa.
3223 *
3224 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003225 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227void swap_buf_le16(u16 *buf, unsigned int buf_words)
3228{
3229#ifdef __BIG_ENDIAN
3230 unsigned int i;
3231
3232 for (i = 0; i < buf_words; i++)
3233 buf[i] = le16_to_cpu(buf[i]);
3234#endif /* __BIG_ENDIAN */
3235}
3236
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003237/**
3238 * ata_mmio_data_xfer - Transfer data by MMIO
3239 * @ap: port to read/write
3240 * @buf: data buffer
3241 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003242 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003243 *
3244 * Transfer data from/to the device data register by MMIO.
3245 *
3246 * LOCKING:
3247 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003248 */
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3251 unsigned int buflen, int write_data)
3252{
3253 unsigned int i;
3254 unsigned int words = buflen >> 1;
3255 u16 *buf16 = (u16 *) buf;
3256 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3257
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003258 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (write_data) {
3260 for (i = 0; i < words; i++)
3261 writew(le16_to_cpu(buf16[i]), mmio);
3262 } else {
3263 for (i = 0; i < words; i++)
3264 buf16[i] = cpu_to_le16(readw(mmio));
3265 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003266
3267 /* Transfer trailing 1 byte, if any. */
3268 if (unlikely(buflen & 0x01)) {
3269 u16 align_buf[1] = { 0 };
3270 unsigned char *trailing_buf = buf + buflen - 1;
3271
3272 if (write_data) {
3273 memcpy(align_buf, trailing_buf, 1);
3274 writew(le16_to_cpu(align_buf[0]), mmio);
3275 } else {
3276 align_buf[0] = cpu_to_le16(readw(mmio));
3277 memcpy(trailing_buf, align_buf, 1);
3278 }
3279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
3281
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003282/**
3283 * ata_pio_data_xfer - Transfer data by PIO
3284 * @ap: port to read/write
3285 * @buf: data buffer
3286 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003287 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003288 *
3289 * Transfer data from/to the device data register by PIO.
3290 *
3291 * LOCKING:
3292 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003293 */
3294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3296 unsigned int buflen, int write_data)
3297{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003298 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003300 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003302 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003304 insw(ap->ioaddr.data_addr, buf, words);
3305
3306 /* Transfer trailing 1 byte, if any. */
3307 if (unlikely(buflen & 0x01)) {
3308 u16 align_buf[1] = { 0 };
3309 unsigned char *trailing_buf = buf + buflen - 1;
3310
3311 if (write_data) {
3312 memcpy(align_buf, trailing_buf, 1);
3313 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3314 } else {
3315 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3316 memcpy(trailing_buf, align_buf, 1);
3317 }
3318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319}
3320
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003321/**
3322 * ata_data_xfer - Transfer data from/to the data register.
3323 * @ap: port to read/write
3324 * @buf: data buffer
3325 * @buflen: buffer length
3326 * @do_write: read/write
3327 *
3328 * Transfer data from/to the device data register.
3329 *
3330 * LOCKING:
3331 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003332 */
3333
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3335 unsigned int buflen, int do_write)
3336{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003337 /* Make the crap hardware pay the costs not the good stuff */
3338 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3339 unsigned long flags;
3340 local_irq_save(flags);
3341 if (ap->flags & ATA_FLAG_MMIO)
3342 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3343 else
3344 ata_pio_data_xfer(ap, buf, buflen, do_write);
3345 local_irq_restore(flags);
3346 } else {
3347 if (ap->flags & ATA_FLAG_MMIO)
3348 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3349 else
3350 ata_pio_data_xfer(ap, buf, buflen, do_write);
3351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003354/**
3355 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3356 * @qc: Command on going
3357 *
3358 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3359 *
3360 * LOCKING:
3361 * Inherited from caller.
3362 */
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364static void ata_pio_sector(struct ata_queued_cmd *qc)
3365{
3366 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003367 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 struct ata_port *ap = qc->ap;
3369 struct page *page;
3370 unsigned int offset;
3371 unsigned char *buf;
3372
3373 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003374 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
3376 page = sg[qc->cursg].page;
3377 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3378
3379 /* get the current page and offset */
3380 page = nth_page(page, (offset >> PAGE_SHIFT));
3381 offset %= PAGE_SIZE;
3382
3383 buf = kmap(page) + offset;
3384
3385 qc->cursect++;
3386 qc->cursg_ofs++;
3387
Albert Lee32529e02005-05-26 03:49:42 -04003388 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 qc->cursg++;
3390 qc->cursg_ofs = 0;
3391 }
3392
3393 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3394
3395 /* do the actual data transfer */
3396 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3397 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3398
3399 kunmap(page);
3400}
3401
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003402/**
3403 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3404 * @qc: Command on going
3405 * @bytes: number of bytes
3406 *
3407 * Transfer Transfer data from/to the ATAPI device.
3408 *
3409 * LOCKING:
3410 * Inherited from caller.
3411 *
3412 */
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3415{
3416 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003417 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 struct ata_port *ap = qc->ap;
3419 struct page *page;
3420 unsigned char *buf;
3421 unsigned int offset, count;
3422
Albert Lee563a6e12005-08-12 14:17:50 +08003423 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003424 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003427 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003428 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003429 * The end of qc->sg is reached and the device expects
3430 * more data to transfer. In order not to overrun qc->sg
3431 * and fulfill length specified in the byte count register,
3432 * - for read case, discard trailing data from the device
3433 * - for write case, padding zero data to the device
3434 */
3435 u16 pad_buf[1] = { 0 };
3436 unsigned int words = bytes >> 1;
3437 unsigned int i;
3438
3439 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003440 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003441 ap->id, bytes);
3442
3443 for (i = 0; i < words; i++)
3444 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3445
Albert Lee14be71f2005-09-27 17:36:35 +08003446 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003447 return;
3448 }
3449
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003450 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 page = sg->page;
3453 offset = sg->offset + qc->cursg_ofs;
3454
3455 /* get the current page and offset */
3456 page = nth_page(page, (offset >> PAGE_SHIFT));
3457 offset %= PAGE_SIZE;
3458
Albert Lee6952df02005-06-06 15:56:03 +08003459 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003460 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 /* don't cross page boundaries */
3463 count = min(count, (unsigned int)PAGE_SIZE - offset);
3464
3465 buf = kmap(page) + offset;
3466
3467 bytes -= count;
3468 qc->curbytes += count;
3469 qc->cursg_ofs += count;
3470
Albert Lee32529e02005-05-26 03:49:42 -04003471 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 qc->cursg++;
3473 qc->cursg_ofs = 0;
3474 }
3475
3476 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3477
3478 /* do the actual data transfer */
3479 ata_data_xfer(ap, buf, count, do_write);
3480
3481 kunmap(page);
3482
Albert Lee563a6e12005-08-12 14:17:50 +08003483 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003487/**
3488 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3489 * @qc: Command on going
3490 *
3491 * Transfer Transfer data from/to the ATAPI device.
3492 *
3493 * LOCKING:
3494 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003495 */
3496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3498{
3499 struct ata_port *ap = qc->ap;
3500 struct ata_device *dev = qc->dev;
3501 unsigned int ireason, bc_lo, bc_hi, bytes;
3502 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3503
3504 ap->ops->tf_read(ap, &qc->tf);
3505 ireason = qc->tf.nsect;
3506 bc_lo = qc->tf.lbam;
3507 bc_hi = qc->tf.lbah;
3508 bytes = (bc_hi << 8) | bc_lo;
3509
3510 /* shall be cleared to zero, indicating xfer of data */
3511 if (ireason & (1 << 0))
3512 goto err_out;
3513
3514 /* make sure transfer direction matches expected */
3515 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3516 if (do_write != i_write)
3517 goto err_out;
3518
3519 __atapi_pio_bytes(qc, bytes);
3520
3521 return;
3522
3523err_out:
3524 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3525 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003526 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003527 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
3530/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003531 * ata_pio_block - start PIO on a block
3532 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 *
3534 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003535 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 */
3537
3538static void ata_pio_block(struct ata_port *ap)
3539{
3540 struct ata_queued_cmd *qc;
3541 u8 status;
3542
3543 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003544 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 * Sometimes when we enter, BSY will be cleared in
3546 * a chk-status or two. If not, the drive is probably seeking
3547 * or something. Snooze for a couple msecs, then
3548 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003549 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 */
3551 status = ata_busy_wait(ap, ATA_BUSY, 5);
3552 if (status & ATA_BUSY) {
3553 msleep(2);
3554 status = ata_busy_wait(ap, ATA_BUSY, 10);
3555 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003556 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3558 return;
3559 }
3560 }
3561
3562 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003563 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Albert Leefe79e682005-12-06 11:34:59 +08003565 /* check error */
3566 if (status & (ATA_ERR | ATA_DF)) {
3567 qc->err_mask |= AC_ERR_DEV;
3568 ap->hsm_task_state = HSM_ST_ERR;
3569 return;
3570 }
3571
3572 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003574 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003576 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 return;
3578 }
3579
3580 atapi_pio_bytes(qc);
3581 } else {
3582 /* handle BSY=0, DRQ=0 as error */
3583 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003584 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003585 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 return;
3587 }
3588
3589 ata_pio_sector(qc);
3590 }
3591}
3592
3593static void ata_pio_error(struct ata_port *ap)
3594{
3595 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003598 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Albert Lee0565c262006-02-13 18:55:25 +08003600 if (qc->tf.command != ATA_CMD_PACKET)
3601 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3602
Albert Lee1c848982005-12-05 15:40:15 +08003603 /* make sure qc->err_mask is available to
3604 * know what's wrong and recover
3605 */
Tejun Heoa4631472006-02-11 19:11:13 +09003606 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003607
Albert Lee14be71f2005-09-27 17:36:35 +08003608 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Albert Leea22e2eb2005-12-05 15:38:02 +08003610 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
3613static void ata_pio_task(void *_data)
3614{
3615 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003616 unsigned long timeout;
3617 int qc_completed;
3618
3619fsm_start:
3620 timeout = 0;
3621 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Albert Lee14be71f2005-09-27 17:36:35 +08003623 switch (ap->hsm_task_state) {
3624 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 return;
3626
Albert Lee14be71f2005-09-27 17:36:35 +08003627 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 ata_pio_block(ap);
3629 break;
3630
Albert Lee14be71f2005-09-27 17:36:35 +08003631 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003632 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 break;
3634
Albert Lee14be71f2005-09-27 17:36:35 +08003635 case HSM_ST_POLL:
3636 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 timeout = ata_pio_poll(ap);
3638 break;
3639
Albert Lee14be71f2005-09-27 17:36:35 +08003640 case HSM_ST_TMOUT:
3641 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 ata_pio_error(ap);
3643 return;
3644 }
3645
3646 if (timeout)
Tejun Heo8061f5f2006-03-05 15:29:09 +09003647 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003648 else if (!qc_completed)
3649 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650}
3651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652/**
Tejun Heo8061f5f2006-03-05 15:29:09 +09003653 * atapi_packet_task - Write CDB bytes to hardware
3654 * @_data: Port to which ATAPI device is attached.
3655 *
3656 * When device has indicated its readiness to accept
3657 * a CDB, this function is called. Send the CDB.
3658 * If DMA is to be performed, exit immediately.
3659 * Otherwise, we are in polling mode, so poll
3660 * status under operation succeeds or fails.
3661 *
3662 * LOCKING:
3663 * Kernel thread context (may sleep)
3664 */
3665
3666static void atapi_packet_task(void *_data)
3667{
3668 struct ata_port *ap = _data;
3669 struct ata_queued_cmd *qc;
3670 u8 status;
3671
3672 qc = ata_qc_from_tag(ap, ap->active_tag);
3673 WARN_ON(qc == NULL);
3674 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3675
3676 /* sleep-wait for BSY to clear */
3677 DPRINTK("busy wait\n");
3678 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3679 qc->err_mask |= AC_ERR_TIMEOUT;
3680 goto err_out;
3681 }
3682
3683 /* make sure DRQ is set */
3684 status = ata_chk_status(ap);
3685 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3686 qc->err_mask |= AC_ERR_HSM;
3687 goto err_out;
3688 }
3689
3690 /* send SCSI cdb */
3691 DPRINTK("send cdb\n");
3692 WARN_ON(qc->dev->cdb_len < 12);
3693
3694 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3695 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3696 unsigned long flags;
3697
3698 /* Once we're done issuing command and kicking bmdma,
3699 * irq handler takes over. To not lose irq, we need
3700 * to clear NOINTR flag before sending cdb, but
3701 * interrupt handler shouldn't be invoked before we're
3702 * finished. Hence, the following locking.
3703 */
3704 spin_lock_irqsave(&ap->host_set->lock, flags);
3705 ap->flags &= ~ATA_FLAG_NOINTR;
3706 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3707 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3708 ap->ops->bmdma_start(qc); /* initiate bmdma */
3709 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3710 } else {
3711 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3712
3713 /* PIO commands are handled by polling */
3714 ap->hsm_task_state = HSM_ST;
3715 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3716 }
3717
3718 return;
3719
3720err_out:
3721 ata_poll_qc_complete(qc);
3722}
3723
3724/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 * ata_qc_timeout - Handle timeout of queued command
3726 * @qc: Command that timed out
3727 *
3728 * Some part of the kernel (currently, only the SCSI layer)
3729 * has noticed that the active command on port @ap has not
3730 * completed after a specified length of time. Handle this
3731 * condition by disabling DMA (if necessary) and completing
3732 * transactions, with error if necessary.
3733 *
3734 * This also handles the case of the "lost interrupt", where
3735 * for some reason (possibly hardware bug, possibly driver bug)
3736 * an interrupt was not delivered to the driver, even though the
3737 * transaction completed successfully.
3738 *
3739 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003740 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 */
3742
3743static void ata_qc_timeout(struct ata_queued_cmd *qc)
3744{
3745 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003746 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003748 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 DPRINTK("ENTER\n");
3751
Tejun Heoc18d06f2006-02-02 00:56:10 +09003752 ap->hsm_task_state = HSM_ST_IDLE;
3753
Jeff Garzikb8f61532005-08-25 22:01:20 -04003754 spin_lock_irqsave(&host_set->lock, flags);
3755
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 switch (qc->tf.protocol) {
3757
3758 case ATA_PROT_DMA:
3759 case ATA_PROT_ATAPI_DMA:
3760 host_stat = ap->ops->bmdma_status(ap);
3761
3762 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003763 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
3765 /* fall through */
3766
3767 default:
3768 ata_altstatus(ap);
3769 drv_stat = ata_chk_status(ap);
3770
3771 /* ack bmdma irq events */
3772 ap->ops->irq_clear(ap);
3773
3774 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3775 ap->id, qc->tf.command, drv_stat, host_stat);
3776
3777 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003778 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 break;
3780 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003781
3782 spin_unlock_irqrestore(&host_set->lock, flags);
3783
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003784 ata_eh_qc_complete(qc);
3785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 DPRINTK("EXIT\n");
3787}
3788
3789/**
3790 * ata_eng_timeout - Handle timeout of queued command
3791 * @ap: Port on which timed-out command is active
3792 *
3793 * Some part of the kernel (currently, only the SCSI layer)
3794 * has noticed that the active command on port @ap has not
3795 * completed after a specified length of time. Handle this
3796 * condition by disabling DMA (if necessary) and completing
3797 * transactions, with error if necessary.
3798 *
3799 * This also handles the case of the "lost interrupt", where
3800 * for some reason (possibly hardware bug, possibly driver bug)
3801 * an interrupt was not delivered to the driver, even though the
3802 * transaction completed successfully.
3803 *
3804 * LOCKING:
3805 * Inherited from SCSI layer (none, can sleep)
3806 */
3807
3808void ata_eng_timeout(struct ata_port *ap)
3809{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 DPRINTK("ENTER\n");
3811
Tejun Heof6379022006-02-10 15:10:48 +09003812 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 DPRINTK("EXIT\n");
3815}
3816
3817/**
3818 * ata_qc_new - Request an available ATA command, for queueing
3819 * @ap: Port associated with device @dev
3820 * @dev: Device from whom we request an available command structure
3821 *
3822 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003823 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 */
3825
3826static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3827{
3828 struct ata_queued_cmd *qc = NULL;
3829 unsigned int i;
3830
3831 for (i = 0; i < ATA_MAX_QUEUE; i++)
3832 if (!test_and_set_bit(i, &ap->qactive)) {
3833 qc = ata_qc_from_tag(ap, i);
3834 break;
3835 }
3836
3837 if (qc)
3838 qc->tag = i;
3839
3840 return qc;
3841}
3842
3843/**
3844 * ata_qc_new_init - Request an available ATA command, and initialize it
3845 * @ap: Port associated with device @dev
3846 * @dev: Device from whom we request an available command structure
3847 *
3848 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003849 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 */
3851
3852struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3853 struct ata_device *dev)
3854{
3855 struct ata_queued_cmd *qc;
3856
3857 qc = ata_qc_new(ap);
3858 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 qc->scsicmd = NULL;
3860 qc->ap = ap;
3861 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003863 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 }
3865
3866 return qc;
3867}
3868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869/**
3870 * ata_qc_free - free unused ata_queued_cmd
3871 * @qc: Command to complete
3872 *
3873 * Designed to free unused ata_queued_cmd object
3874 * in case something prevents using it.
3875 *
3876 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003877 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 */
3879void ata_qc_free(struct ata_queued_cmd *qc)
3880{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003881 struct ata_port *ap = qc->ap;
3882 unsigned int tag;
3883
Tejun Heoa4631472006-02-11 19:11:13 +09003884 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Tejun Heo4ba946e2006-01-23 13:09:36 +09003886 qc->flags = 0;
3887 tag = qc->tag;
3888 if (likely(ata_tag_valid(tag))) {
3889 if (tag == ap->active_tag)
3890 ap->active_tag = ATA_TAG_POISON;
3891 qc->tag = ATA_TAG_POISON;
3892 clear_bit(tag, &ap->qactive);
3893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894}
3895
Tejun Heo76014422006-02-11 15:13:49 +09003896void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
Tejun Heoa4631472006-02-11 19:11:13 +09003898 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3899 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
3901 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3902 ata_sg_clean(qc);
3903
Albert Lee3f3791d2005-08-16 14:25:38 +08003904 /* atapi: mark qc as inactive to prevent the interrupt handler
3905 * from completing the command twice later, before the error handler
3906 * is called. (when rc != 0 and atapi request sense is needed)
3907 */
3908 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003911 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912}
3913
3914static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3915{
3916 struct ata_port *ap = qc->ap;
3917
3918 switch (qc->tf.protocol) {
3919 case ATA_PROT_DMA:
3920 case ATA_PROT_ATAPI_DMA:
3921 return 1;
3922
3923 case ATA_PROT_ATAPI:
3924 case ATA_PROT_PIO:
3925 case ATA_PROT_PIO_MULT:
3926 if (ap->flags & ATA_FLAG_PIO_DMA)
3927 return 1;
3928
3929 /* fall through */
3930
3931 default:
3932 return 0;
3933 }
3934
3935 /* never reached */
3936}
3937
3938/**
3939 * ata_qc_issue - issue taskfile to device
3940 * @qc: command to issue to device
3941 *
3942 * Prepare an ATA command to submission to device.
3943 * This includes mapping the data into a DMA-able
3944 * area, filling in the S/G table, and finally
3945 * writing the taskfile to hardware, starting the command.
3946 *
3947 * LOCKING:
3948 * spin_lock_irqsave(host_set lock)
3949 *
3950 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003951 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 */
3953
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003954unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955{
3956 struct ata_port *ap = qc->ap;
3957
3958 if (ata_should_dma_map(qc)) {
3959 if (qc->flags & ATA_QCFLAG_SG) {
3960 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003961 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3963 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003964 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
3966 } else {
3967 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3968 }
3969
3970 ap->ops->qc_prep(qc);
3971
3972 qc->ap->active_tag = qc->tag;
3973 qc->flags |= ATA_QCFLAG_ACTIVE;
3974
3975 return ap->ops->qc_issue(qc);
3976
Tejun Heo8e436af2006-01-23 13:09:36 +09003977sg_err:
3978 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003979 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980}
3981
Edward Falk0baab862005-06-02 18:17:13 -04003982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983/**
3984 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3985 * @qc: command to issue to device
3986 *
3987 * Using various libata functions and hooks, this function
3988 * starts an ATA command. ATA commands are grouped into
3989 * classes called "protocols", and issuing each type of protocol
3990 * is slightly different.
3991 *
Edward Falk0baab862005-06-02 18:17:13 -04003992 * May be used as the qc_issue() entry in ata_port_operations.
3993 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 * LOCKING:
3995 * spin_lock_irqsave(host_set lock)
3996 *
3997 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003998 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 */
4000
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004001unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
4003 struct ata_port *ap = qc->ap;
4004
4005 ata_dev_select(ap, qc->dev->devno, 1, 0);
4006
4007 switch (qc->tf.protocol) {
4008 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05004009 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 break;
4011
4012 case ATA_PROT_DMA:
4013 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4014 ap->ops->bmdma_setup(qc); /* set up bmdma */
4015 ap->ops->bmdma_start(qc); /* initiate bmdma */
4016 break;
4017
4018 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4019 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004020 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08004021 ap->hsm_task_state = HSM_ST;
Tejun Heo8061f5f2006-03-05 15:29:09 +09004022 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 break;
4024
4025 case ATA_PROT_ATAPI:
4026 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004027 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004028 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 break;
4030
4031 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09004032 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05004033 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004034 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 break;
4036
4037 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09004038 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4040 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo8061f5f2006-03-05 15:29:09 +09004041 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 break;
4043
4044 default:
4045 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004046 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 }
4048
4049 return 0;
4050}
4051
4052/**
Edward Falk0baab862005-06-02 18:17:13 -04004053 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 * @qc: Info associated with this ATA transaction.
4055 *
4056 * LOCKING:
4057 * spin_lock_irqsave(host_set lock)
4058 */
4059
4060static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4061{
4062 struct ata_port *ap = qc->ap;
4063 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4064 u8 dmactl;
4065 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4066
4067 /* load PRD table addr. */
4068 mb(); /* make sure PRD table writes are visible to controller */
4069 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4070
4071 /* specify data direction, triple-check start bit is clear */
4072 dmactl = readb(mmio + ATA_DMA_CMD);
4073 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4074 if (!rw)
4075 dmactl |= ATA_DMA_WR;
4076 writeb(dmactl, mmio + ATA_DMA_CMD);
4077
4078 /* issue r/w command */
4079 ap->ops->exec_command(ap, &qc->tf);
4080}
4081
4082/**
Alan Coxb73fc892005-08-26 16:03:19 +01004083 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 * @qc: Info associated with this ATA transaction.
4085 *
4086 * LOCKING:
4087 * spin_lock_irqsave(host_set lock)
4088 */
4089
4090static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4091{
4092 struct ata_port *ap = qc->ap;
4093 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4094 u8 dmactl;
4095
4096 /* start host DMA transaction */
4097 dmactl = readb(mmio + ATA_DMA_CMD);
4098 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4099
4100 /* Strictly, one may wish to issue a readb() here, to
4101 * flush the mmio write. However, control also passes
4102 * to the hardware at this point, and it will interrupt
4103 * us when we are to resume control. So, in effect,
4104 * we don't care when the mmio write flushes.
4105 * Further, a read of the DMA status register _immediately_
4106 * following the write may not be what certain flaky hardware
4107 * is expected, so I think it is best to not add a readb()
4108 * without first all the MMIO ATA cards/mobos.
4109 * Or maybe I'm just being paranoid.
4110 */
4111}
4112
4113/**
4114 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4115 * @qc: Info associated with this ATA transaction.
4116 *
4117 * LOCKING:
4118 * spin_lock_irqsave(host_set lock)
4119 */
4120
4121static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4122{
4123 struct ata_port *ap = qc->ap;
4124 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4125 u8 dmactl;
4126
4127 /* load PRD table addr. */
4128 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4129
4130 /* specify data direction, triple-check start bit is clear */
4131 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4132 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4133 if (!rw)
4134 dmactl |= ATA_DMA_WR;
4135 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4136
4137 /* issue r/w command */
4138 ap->ops->exec_command(ap, &qc->tf);
4139}
4140
4141/**
4142 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4143 * @qc: Info associated with this ATA transaction.
4144 *
4145 * LOCKING:
4146 * spin_lock_irqsave(host_set lock)
4147 */
4148
4149static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4150{
4151 struct ata_port *ap = qc->ap;
4152 u8 dmactl;
4153
4154 /* start host DMA transaction */
4155 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4156 outb(dmactl | ATA_DMA_START,
4157 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4158}
4159
Edward Falk0baab862005-06-02 18:17:13 -04004160
4161/**
4162 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4163 * @qc: Info associated with this ATA transaction.
4164 *
4165 * Writes the ATA_DMA_START flag to the DMA command register.
4166 *
4167 * May be used as the bmdma_start() entry in ata_port_operations.
4168 *
4169 * LOCKING:
4170 * spin_lock_irqsave(host_set lock)
4171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172void ata_bmdma_start(struct ata_queued_cmd *qc)
4173{
4174 if (qc->ap->flags & ATA_FLAG_MMIO)
4175 ata_bmdma_start_mmio(qc);
4176 else
4177 ata_bmdma_start_pio(qc);
4178}
4179
Edward Falk0baab862005-06-02 18:17:13 -04004180
4181/**
4182 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4183 * @qc: Info associated with this ATA transaction.
4184 *
4185 * Writes address of PRD table to device's PRD Table Address
4186 * register, sets the DMA control register, and calls
4187 * ops->exec_command() to start the transfer.
4188 *
4189 * May be used as the bmdma_setup() entry in ata_port_operations.
4190 *
4191 * LOCKING:
4192 * spin_lock_irqsave(host_set lock)
4193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194void ata_bmdma_setup(struct ata_queued_cmd *qc)
4195{
4196 if (qc->ap->flags & ATA_FLAG_MMIO)
4197 ata_bmdma_setup_mmio(qc);
4198 else
4199 ata_bmdma_setup_pio(qc);
4200}
4201
Edward Falk0baab862005-06-02 18:17:13 -04004202
4203/**
4204 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004205 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004206 *
4207 * Clear interrupt and error flags in DMA status register.
4208 *
4209 * May be used as the irq_clear() entry in ata_port_operations.
4210 *
4211 * LOCKING:
4212 * spin_lock_irqsave(host_set lock)
4213 */
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215void ata_bmdma_irq_clear(struct ata_port *ap)
4216{
4217 if (ap->flags & ATA_FLAG_MMIO) {
4218 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4219 writeb(readb(mmio), mmio);
4220 } else {
4221 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4222 outb(inb(addr), addr);
4223 }
4224
4225}
4226
Edward Falk0baab862005-06-02 18:17:13 -04004227
4228/**
4229 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004230 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004231 *
4232 * Read and return BMDMA status register.
4233 *
4234 * May be used as the bmdma_status() entry in ata_port_operations.
4235 *
4236 * LOCKING:
4237 * spin_lock_irqsave(host_set lock)
4238 */
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240u8 ata_bmdma_status(struct ata_port *ap)
4241{
4242 u8 host_stat;
4243 if (ap->flags & ATA_FLAG_MMIO) {
4244 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4245 host_stat = readb(mmio + ATA_DMA_STATUS);
4246 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004247 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 return host_stat;
4249}
4250
Edward Falk0baab862005-06-02 18:17:13 -04004251
4252/**
4253 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004254 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004255 *
4256 * Clears the ATA_DMA_START flag in the dma control register
4257 *
4258 * May be used as the bmdma_stop() entry in ata_port_operations.
4259 *
4260 * LOCKING:
4261 * spin_lock_irqsave(host_set lock)
4262 */
4263
Alan Coxb73fc892005-08-26 16:03:19 +01004264void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
Alan Coxb73fc892005-08-26 16:03:19 +01004266 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 if (ap->flags & ATA_FLAG_MMIO) {
4268 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4269
4270 /* clear start/stop bit */
4271 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4272 mmio + ATA_DMA_CMD);
4273 } else {
4274 /* clear start/stop bit */
4275 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4276 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4277 }
4278
4279 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4280 ata_altstatus(ap); /* dummy read */
4281}
4282
4283/**
4284 * ata_host_intr - Handle host interrupt for given (port, task)
4285 * @ap: Port on which interrupt arrived (possibly...)
4286 * @qc: Taskfile currently active in engine
4287 *
4288 * Handle host interrupt for given queued command. Currently,
4289 * only DMA interrupts are handled. All other commands are
4290 * handled via polling with interrupts disabled (nIEN bit).
4291 *
4292 * LOCKING:
4293 * spin_lock_irqsave(host_set lock)
4294 *
4295 * RETURNS:
4296 * One if interrupt was handled, zero if not (shared irq).
4297 */
4298
4299inline unsigned int ata_host_intr (struct ata_port *ap,
4300 struct ata_queued_cmd *qc)
4301{
4302 u8 status, host_stat;
4303
4304 switch (qc->tf.protocol) {
4305
4306 case ATA_PROT_DMA:
4307 case ATA_PROT_ATAPI_DMA:
4308 case ATA_PROT_ATAPI:
4309 /* check status of DMA engine */
4310 host_stat = ap->ops->bmdma_status(ap);
4311 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4312
4313 /* if it's not our irq... */
4314 if (!(host_stat & ATA_DMA_INTR))
4315 goto idle_irq;
4316
4317 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004318 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
4320 /* fall through */
4321
4322 case ATA_PROT_ATAPI_NODATA:
4323 case ATA_PROT_NODATA:
4324 /* check altstatus */
4325 status = ata_altstatus(ap);
4326 if (status & ATA_BUSY)
4327 goto idle_irq;
4328
4329 /* check main status, clearing INTRQ */
4330 status = ata_chk_status(ap);
4331 if (unlikely(status & ATA_BUSY))
4332 goto idle_irq;
4333 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4334 ap->id, qc->tf.protocol, status);
4335
4336 /* ack bmdma irq events */
4337 ap->ops->irq_clear(ap);
4338
4339 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004340 qc->err_mask |= ac_err_mask(status);
4341 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 break;
4343
4344 default:
4345 goto idle_irq;
4346 }
4347
4348 return 1; /* irq handled */
4349
4350idle_irq:
4351 ap->stats.idle_irq++;
4352
4353#ifdef ATA_IRQ_TRAP
4354 if ((ap->stats.idle_irq % 1000) == 0) {
4355 handled = 1;
4356 ata_irq_ack(ap, 0); /* debug trap */
4357 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4358 }
4359#endif
4360 return 0; /* irq not handled */
4361}
4362
4363/**
4364 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004365 * @irq: irq line (unused)
4366 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 * @regs: unused
4368 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004369 * Default interrupt handler for PCI IDE devices. Calls
4370 * ata_host_intr() for each port that is not disabled.
4371 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004373 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 *
4375 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004376 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 */
4378
4379irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4380{
4381 struct ata_host_set *host_set = dev_instance;
4382 unsigned int i;
4383 unsigned int handled = 0;
4384 unsigned long flags;
4385
4386 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4387 spin_lock_irqsave(&host_set->lock, flags);
4388
4389 for (i = 0; i < host_set->n_ports; i++) {
4390 struct ata_port *ap;
4391
4392 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004393 if (ap &&
4394 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 struct ata_queued_cmd *qc;
4396
4397 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004398 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4399 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 handled |= ata_host_intr(ap, qc);
4401 }
4402 }
4403
4404 spin_unlock_irqrestore(&host_set->lock, flags);
4405
4406 return IRQ_RETVAL(handled);
4407}
4408
Edward Falk0baab862005-06-02 18:17:13 -04004409
Jens Axboe9b847542006-01-06 09:28:07 +01004410/*
4411 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4412 * without filling any other registers
4413 */
4414static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4415 u8 cmd)
4416{
4417 struct ata_taskfile tf;
4418 int err;
4419
4420 ata_tf_init(ap, &tf, dev->devno);
4421
4422 tf.command = cmd;
4423 tf.flags |= ATA_TFLAG_DEVICE;
4424 tf.protocol = ATA_PROT_NODATA;
4425
4426 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4427 if (err)
4428 printk(KERN_ERR "%s: ata command failed: %d\n",
4429 __FUNCTION__, err);
4430
4431 return err;
4432}
4433
4434static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4435{
4436 u8 cmd;
4437
4438 if (!ata_try_flush_cache(dev))
4439 return 0;
4440
4441 if (ata_id_has_flush_ext(dev->id))
4442 cmd = ATA_CMD_FLUSH_EXT;
4443 else
4444 cmd = ATA_CMD_FLUSH;
4445
4446 return ata_do_simple_cmd(ap, dev, cmd);
4447}
4448
4449static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4450{
4451 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4452}
4453
4454static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4455{
4456 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4457}
4458
4459/**
4460 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004461 * @ap: port the device is connected to
4462 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004463 *
4464 * Kick the drive back into action, by sending it an idle immediate
4465 * command and making sure its transfer mode matches between drive
4466 * and host.
4467 *
4468 */
4469int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4470{
4471 if (ap->flags & ATA_FLAG_SUSPENDED) {
4472 ap->flags &= ~ATA_FLAG_SUSPENDED;
4473 ata_set_mode(ap);
4474 }
4475 if (!ata_dev_present(dev))
4476 return 0;
4477 if (dev->class == ATA_DEV_ATA)
4478 ata_start_drive(ap, dev);
4479
4480 return 0;
4481}
4482
4483/**
4484 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004485 * @ap: port the device is connected to
4486 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004487 *
4488 * Flush the cache on the drive, if appropriate, then issue a
4489 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004490 */
4491int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4492{
4493 if (!ata_dev_present(dev))
4494 return 0;
4495 if (dev->class == ATA_DEV_ATA)
4496 ata_flush_cache(ap, dev);
4497
4498 ata_standby_drive(ap, dev);
4499 ap->flags |= ATA_FLAG_SUSPENDED;
4500 return 0;
4501}
4502
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004503/**
4504 * ata_port_start - Set port up for dma.
4505 * @ap: Port to initialize
4506 *
4507 * Called just after data structures for each port are
4508 * initialized. Allocates space for PRD table.
4509 *
4510 * May be used as the port_start() entry in ata_port_operations.
4511 *
4512 * LOCKING:
4513 * Inherited from caller.
4514 */
4515
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516int ata_port_start (struct ata_port *ap)
4517{
4518 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004519 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
4521 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4522 if (!ap->prd)
4523 return -ENOMEM;
4524
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004525 rc = ata_pad_alloc(ap, dev);
4526 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004527 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004528 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004529 }
4530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4532
4533 return 0;
4534}
4535
Edward Falk0baab862005-06-02 18:17:13 -04004536
4537/**
4538 * ata_port_stop - Undo ata_port_start()
4539 * @ap: Port to shut down
4540 *
4541 * Frees the PRD table.
4542 *
4543 * May be used as the port_stop() entry in ata_port_operations.
4544 *
4545 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004546 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004547 */
4548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549void ata_port_stop (struct ata_port *ap)
4550{
4551 struct device *dev = ap->host_set->dev;
4552
4553 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004554 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555}
4556
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004557void ata_host_stop (struct ata_host_set *host_set)
4558{
4559 if (host_set->mmio_base)
4560 iounmap(host_set->mmio_base);
4561}
4562
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564/**
4565 * ata_host_remove - Unregister SCSI host structure with upper layers
4566 * @ap: Port to unregister
4567 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4568 *
4569 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004570 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 */
4572
4573static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4574{
4575 struct Scsi_Host *sh = ap->host;
4576
4577 DPRINTK("ENTER\n");
4578
4579 if (do_unregister)
4580 scsi_remove_host(sh);
4581
4582 ap->ops->port_stop(ap);
4583}
4584
4585/**
4586 * ata_host_init - Initialize an ata_port structure
4587 * @ap: Structure to initialize
4588 * @host: associated SCSI mid-layer structure
4589 * @host_set: Collection of hosts to which @ap belongs
4590 * @ent: Probe information provided by low-level driver
4591 * @port_no: Port number associated with this ata_port
4592 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004593 * Initialize a new ata_port structure, and its associated
4594 * scsi_host.
4595 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004597 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 */
4599
4600static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4601 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004602 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603{
4604 unsigned int i;
4605
4606 host->max_id = 16;
4607 host->max_lun = 1;
4608 host->max_channel = 1;
4609 host->unique_id = ata_unique_id++;
4610 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 ap->flags = ATA_FLAG_PORT_DISABLED;
4613 ap->id = host->unique_id;
4614 ap->host = host;
4615 ap->ctl = ATA_DEVCTL_OBS;
4616 ap->host_set = host_set;
4617 ap->port_no = port_no;
4618 ap->hard_port_no =
4619 ent->legacy_mode ? ent->hard_port_no : port_no;
4620 ap->pio_mask = ent->pio_mask;
4621 ap->mwdma_mask = ent->mwdma_mask;
4622 ap->udma_mask = ent->udma_mask;
4623 ap->flags |= ent->host_flags;
4624 ap->ops = ent->port_ops;
4625 ap->cbl = ATA_CBL_NONE;
4626 ap->active_tag = ATA_TAG_POISON;
4627 ap->last_ctl = 0xFF;
4628
Tejun Heo86e45b62006-03-05 15:29:09 +09004629 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004630 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
4632 for (i = 0; i < ATA_MAX_DEVICES; i++)
4633 ap->device[i].devno = i;
4634
4635#ifdef ATA_IRQ_TRAP
4636 ap->stats.unhandled_irq = 1;
4637 ap->stats.idle_irq = 1;
4638#endif
4639
4640 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4641}
4642
4643/**
4644 * ata_host_add - Attach low-level ATA driver to system
4645 * @ent: Information provided by low-level driver
4646 * @host_set: Collections of ports to which we add
4647 * @port_no: Port number associated with this host
4648 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004649 * Attach low-level ATA driver to system.
4650 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004652 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 *
4654 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004655 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 */
4657
Jeff Garzik057ace52005-10-22 14:27:05 -04004658static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 struct ata_host_set *host_set,
4660 unsigned int port_no)
4661{
4662 struct Scsi_Host *host;
4663 struct ata_port *ap;
4664 int rc;
4665
4666 DPRINTK("ENTER\n");
4667 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4668 if (!host)
4669 return NULL;
4670
4671 ap = (struct ata_port *) &host->hostdata[0];
4672
4673 ata_host_init(ap, host, host_set, ent, port_no);
4674
4675 rc = ap->ops->port_start(ap);
4676 if (rc)
4677 goto err_out;
4678
4679 return ap;
4680
4681err_out:
4682 scsi_host_put(host);
4683 return NULL;
4684}
4685
4686/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004687 * ata_device_add - Register hardware device with ATA and SCSI layers
4688 * @ent: Probe information describing hardware device to be registered
4689 *
4690 * This function processes the information provided in the probe
4691 * information struct @ent, allocates the necessary ATA and SCSI
4692 * host information structures, initializes them, and registers
4693 * everything with requisite kernel subsystems.
4694 *
4695 * This function requests irqs, probes the ATA bus, and probes
4696 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 *
4698 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004699 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 *
4701 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004702 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 */
4704
Jeff Garzik057ace52005-10-22 14:27:05 -04004705int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706{
4707 unsigned int count = 0, i;
4708 struct device *dev = ent->dev;
4709 struct ata_host_set *host_set;
4710
4711 DPRINTK("ENTER\n");
4712 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004713 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4715 if (!host_set)
4716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 spin_lock_init(&host_set->lock);
4718
4719 host_set->dev = dev;
4720 host_set->n_ports = ent->n_ports;
4721 host_set->irq = ent->irq;
4722 host_set->mmio_base = ent->mmio_base;
4723 host_set->private_data = ent->private_data;
4724 host_set->ops = ent->port_ops;
4725
4726 /* register each port bound to this device */
4727 for (i = 0; i < ent->n_ports; i++) {
4728 struct ata_port *ap;
4729 unsigned long xfer_mode_mask;
4730
4731 ap = ata_host_add(ent, host_set, i);
4732 if (!ap)
4733 goto err_out;
4734
4735 host_set->ports[i] = ap;
4736 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4737 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4738 (ap->pio_mask << ATA_SHIFT_PIO);
4739
4740 /* print per-port info to dmesg */
4741 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4742 "bmdma 0x%lX irq %lu\n",
4743 ap->id,
4744 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4745 ata_mode_string(xfer_mode_mask),
4746 ap->ioaddr.cmd_addr,
4747 ap->ioaddr.ctl_addr,
4748 ap->ioaddr.bmdma_addr,
4749 ent->irq);
4750
4751 ata_chk_status(ap);
4752 host_set->ops->irq_clear(ap);
4753 count++;
4754 }
4755
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004756 if (!count)
4757 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
4759 /* obtain irq, that is shared between channels */
4760 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4761 DRV_NAME, host_set))
4762 goto err_out;
4763
4764 /* perform each probe synchronously */
4765 DPRINTK("probe begin\n");
4766 for (i = 0; i < count; i++) {
4767 struct ata_port *ap;
4768 int rc;
4769
4770 ap = host_set->ports[i];
4771
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004772 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004774 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
4776 if (rc) {
4777 /* FIXME: do something useful here?
4778 * Current libata behavior will
4779 * tear down everything when
4780 * the module is removed
4781 * or the h/w is unplugged.
4782 */
4783 }
4784
4785 rc = scsi_add_host(ap->host, dev);
4786 if (rc) {
4787 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4788 ap->id);
4789 /* FIXME: do something useful here */
4790 /* FIXME: handle unconditional calls to
4791 * scsi_scan_host and ata_host_remove, below,
4792 * at the very least
4793 */
4794 }
4795 }
4796
4797 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004798 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 for (i = 0; i < count; i++) {
4800 struct ata_port *ap = host_set->ports[i];
4801
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004802 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 }
4804
4805 dev_set_drvdata(dev, host_set);
4806
4807 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4808 return ent->n_ports; /* success */
4809
4810err_out:
4811 for (i = 0; i < count; i++) {
4812 ata_host_remove(host_set->ports[i], 1);
4813 scsi_host_put(host_set->ports[i]->host);
4814 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004815err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 kfree(host_set);
4817 VPRINTK("EXIT, returning 0\n");
4818 return 0;
4819}
4820
4821/**
Alan Cox17b14452005-09-15 15:44:00 +01004822 * ata_host_set_remove - PCI layer callback for device removal
4823 * @host_set: ATA host set that was removed
4824 *
4825 * Unregister all objects associated with this host set. Free those
4826 * objects.
4827 *
4828 * LOCKING:
4829 * Inherited from calling layer (may sleep).
4830 */
4831
Alan Cox17b14452005-09-15 15:44:00 +01004832void ata_host_set_remove(struct ata_host_set *host_set)
4833{
4834 struct ata_port *ap;
4835 unsigned int i;
4836
4837 for (i = 0; i < host_set->n_ports; i++) {
4838 ap = host_set->ports[i];
4839 scsi_remove_host(ap->host);
4840 }
4841
4842 free_irq(host_set->irq, host_set);
4843
4844 for (i = 0; i < host_set->n_ports; i++) {
4845 ap = host_set->ports[i];
4846
4847 ata_scsi_release(ap->host);
4848
4849 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4850 struct ata_ioports *ioaddr = &ap->ioaddr;
4851
4852 if (ioaddr->cmd_addr == 0x1f0)
4853 release_region(0x1f0, 8);
4854 else if (ioaddr->cmd_addr == 0x170)
4855 release_region(0x170, 8);
4856 }
4857
4858 scsi_host_put(ap->host);
4859 }
4860
4861 if (host_set->ops->host_stop)
4862 host_set->ops->host_stop(host_set);
4863
4864 kfree(host_set);
4865}
4866
4867/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 * ata_scsi_release - SCSI layer callback hook for host unload
4869 * @host: libata host to be unloaded
4870 *
4871 * Performs all duties necessary to shut down a libata port...
4872 * Kill port kthread, disable port, and release resources.
4873 *
4874 * LOCKING:
4875 * Inherited from SCSI layer.
4876 *
4877 * RETURNS:
4878 * One.
4879 */
4880
4881int ata_scsi_release(struct Scsi_Host *host)
4882{
4883 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004884 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
4886 DPRINTK("ENTER\n");
4887
4888 ap->ops->port_disable(ap);
4889 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004890 for (i = 0; i < ATA_MAX_DEVICES; i++)
4891 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
4893 DPRINTK("EXIT\n");
4894 return 1;
4895}
4896
4897/**
4898 * ata_std_ports - initialize ioaddr with standard port offsets.
4899 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004900 *
4901 * Utility function which initializes data_addr, error_addr,
4902 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4903 * device_addr, status_addr, and command_addr to standard offsets
4904 * relative to cmd_addr.
4905 *
4906 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 */
Edward Falk0baab862005-06-02 18:17:13 -04004908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909void ata_std_ports(struct ata_ioports *ioaddr)
4910{
4911 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4912 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4913 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4914 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4915 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4916 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4917 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4918 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4919 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4920 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4921}
4922
Edward Falk0baab862005-06-02 18:17:13 -04004923
Jeff Garzik374b1872005-08-30 05:42:52 -04004924#ifdef CONFIG_PCI
4925
4926void ata_pci_host_stop (struct ata_host_set *host_set)
4927{
4928 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4929
4930 pci_iounmap(pdev, host_set->mmio_base);
4931}
4932
Edward Falk0baab862005-06-02 18:17:13 -04004933/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 * ata_pci_remove_one - PCI layer callback for device removal
4935 * @pdev: PCI device that was removed
4936 *
4937 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004938 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 * Handle this by unregistering all objects associated
4940 * with this PCI device. Free those objects. Then finally
4941 * release PCI resources and disable device.
4942 *
4943 * LOCKING:
4944 * Inherited from PCI layer (may sleep).
4945 */
4946
4947void ata_pci_remove_one (struct pci_dev *pdev)
4948{
4949 struct device *dev = pci_dev_to_dev(pdev);
4950 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Alan Cox17b14452005-09-15 15:44:00 +01004952 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 pci_release_regions(pdev);
4954 pci_disable_device(pdev);
4955 dev_set_drvdata(dev, NULL);
4956}
4957
4958/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004959int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960{
4961 unsigned long tmp = 0;
4962
4963 switch (bits->width) {
4964 case 1: {
4965 u8 tmp8 = 0;
4966 pci_read_config_byte(pdev, bits->reg, &tmp8);
4967 tmp = tmp8;
4968 break;
4969 }
4970 case 2: {
4971 u16 tmp16 = 0;
4972 pci_read_config_word(pdev, bits->reg, &tmp16);
4973 tmp = tmp16;
4974 break;
4975 }
4976 case 4: {
4977 u32 tmp32 = 0;
4978 pci_read_config_dword(pdev, bits->reg, &tmp32);
4979 tmp = tmp32;
4980 break;
4981 }
4982
4983 default:
4984 return -EINVAL;
4985 }
4986
4987 tmp &= bits->mask;
4988
4989 return (tmp == bits->val) ? 1 : 0;
4990}
Jens Axboe9b847542006-01-06 09:28:07 +01004991
4992int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4993{
4994 pci_save_state(pdev);
4995 pci_disable_device(pdev);
4996 pci_set_power_state(pdev, PCI_D3hot);
4997 return 0;
4998}
4999
5000int ata_pci_device_resume(struct pci_dev *pdev)
5001{
5002 pci_set_power_state(pdev, PCI_D0);
5003 pci_restore_state(pdev);
5004 pci_enable_device(pdev);
5005 pci_set_master(pdev);
5006 return 0;
5007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008#endif /* CONFIG_PCI */
5009
5010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011static int __init ata_init(void)
5012{
5013 ata_wq = create_workqueue("ata");
5014 if (!ata_wq)
5015 return -ENOMEM;
5016
5017 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5018 return 0;
5019}
5020
5021static void __exit ata_exit(void)
5022{
5023 destroy_workqueue(ata_wq);
5024}
5025
5026module_init(ata_init);
5027module_exit(ata_exit);
5028
Jeff Garzik67846b32005-10-05 02:58:32 -04005029static unsigned long ratelimit_time;
5030static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5031
5032int ata_ratelimit(void)
5033{
5034 int rc;
5035 unsigned long flags;
5036
5037 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5038
5039 if (time_after(jiffies, ratelimit_time)) {
5040 rc = 1;
5041 ratelimit_time = jiffies + (HZ/5);
5042 } else
5043 rc = 0;
5044
5045 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5046
5047 return rc;
5048}
5049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050/*
5051 * libata is essentially a library of internal helper functions for
5052 * low-level ATA host controller drivers. As such, the API/ABI is
5053 * likely to change as new drivers are added and updated.
5054 * Do not depend on ABI/API stability.
5055 */
5056
5057EXPORT_SYMBOL_GPL(ata_std_bios_param);
5058EXPORT_SYMBOL_GPL(ata_std_ports);
5059EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005060EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061EXPORT_SYMBOL_GPL(ata_sg_init);
5062EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09005063EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5065EXPORT_SYMBOL_GPL(ata_eng_timeout);
5066EXPORT_SYMBOL_GPL(ata_tf_load);
5067EXPORT_SYMBOL_GPL(ata_tf_read);
5068EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5069EXPORT_SYMBOL_GPL(ata_std_dev_select);
5070EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5071EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5072EXPORT_SYMBOL_GPL(ata_check_status);
5073EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074EXPORT_SYMBOL_GPL(ata_exec_command);
5075EXPORT_SYMBOL_GPL(ata_port_start);
5076EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005077EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078EXPORT_SYMBOL_GPL(ata_interrupt);
5079EXPORT_SYMBOL_GPL(ata_qc_prep);
5080EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5081EXPORT_SYMBOL_GPL(ata_bmdma_start);
5082EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5083EXPORT_SYMBOL_GPL(ata_bmdma_status);
5084EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5085EXPORT_SYMBOL_GPL(ata_port_probe);
5086EXPORT_SYMBOL_GPL(sata_phy_reset);
5087EXPORT_SYMBOL_GPL(__sata_phy_reset);
5088EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005089EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005090EXPORT_SYMBOL_GPL(ata_std_softreset);
5091EXPORT_SYMBOL_GPL(sata_std_hardreset);
5092EXPORT_SYMBOL_GPL(ata_std_postreset);
5093EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005094EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09005095EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005097EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005098EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09005099EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5101EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09005102EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103EXPORT_SYMBOL_GPL(ata_scsi_error);
5104EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5105EXPORT_SYMBOL_GPL(ata_scsi_release);
5106EXPORT_SYMBOL_GPL(ata_host_intr);
5107EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09005108EXPORT_SYMBOL_GPL(ata_id_string);
5109EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005111EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5112EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005114EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005115EXPORT_SYMBOL_GPL(ata_timing_compute);
5116EXPORT_SYMBOL_GPL(ata_timing_merge);
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118#ifdef CONFIG_PCI
5119EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005120EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5122EXPORT_SYMBOL_GPL(ata_pci_init_one);
5123EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005124EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5125EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005127
5128EXPORT_SYMBOL_GPL(ata_device_suspend);
5129EXPORT_SYMBOL_GPL(ata_device_resume);
5130EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5131EXPORT_SYMBOL_GPL(ata_scsi_device_resume);