blob: 1a00c80b96d449328ee5c436d150888ebf645b40 [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 Garzike33b9df2005-10-09 09:51:46 -040068static void ata_pio_error(struct ata_port *ap);
Tejun Heoa6d5a512006-03-06 04:31:57 +090069static unsigned int ata_dev_xfermask(struct ata_port *ap,
70 struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static unsigned int ata_unique_id = 1;
73static struct workqueue_struct *ata_wq;
74
Jeff Garzik1623c812005-08-30 03:37:42 -040075int atapi_enabled = 0;
76module_param(atapi_enabled, int, 0444);
77MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
78
Jeff Garzikc3c013a2006-02-27 22:31:19 -050079int libata_fua = 0;
80module_param_named(fua, libata_fua, int, 0444);
81MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083MODULE_AUTHOR("Jeff Garzik");
84MODULE_DESCRIPTION("Library module for ATA devices");
85MODULE_LICENSE("GPL");
86MODULE_VERSION(DRV_VERSION);
87
Edward Falk0baab862005-06-02 18:17:13 -040088
89/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
91 * @tf: Taskfile to convert
92 * @fis: Buffer into which data will output
93 * @pmp: Port multiplier port
94 *
95 * Converts a standard ATA taskfile to a Serial ATA
96 * FIS structure (Register - Host to Device).
97 *
98 * LOCKING:
99 * Inherited from caller.
100 */
101
Jeff Garzik057ace52005-10-22 14:27:05 -0400102void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 fis[0] = 0x27; /* Register - Host to Device FIS */
105 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
106 bit 7 indicates Command FIS */
107 fis[2] = tf->command;
108 fis[3] = tf->feature;
109
110 fis[4] = tf->lbal;
111 fis[5] = tf->lbam;
112 fis[6] = tf->lbah;
113 fis[7] = tf->device;
114
115 fis[8] = tf->hob_lbal;
116 fis[9] = tf->hob_lbam;
117 fis[10] = tf->hob_lbah;
118 fis[11] = tf->hob_feature;
119
120 fis[12] = tf->nsect;
121 fis[13] = tf->hob_nsect;
122 fis[14] = 0;
123 fis[15] = tf->ctl;
124
125 fis[16] = 0;
126 fis[17] = 0;
127 fis[18] = 0;
128 fis[19] = 0;
129}
130
131/**
132 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
133 * @fis: Buffer from which data will be input
134 * @tf: Taskfile to output
135 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500136 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * LOCKING:
139 * Inherited from caller.
140 */
141
Jeff Garzik057ace52005-10-22 14:27:05 -0400142void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 tf->command = fis[2]; /* status */
145 tf->feature = fis[3]; /* error */
146
147 tf->lbal = fis[4];
148 tf->lbam = fis[5];
149 tf->lbah = fis[6];
150 tf->device = fis[7];
151
152 tf->hob_lbal = fis[8];
153 tf->hob_lbam = fis[9];
154 tf->hob_lbah = fis[10];
155
156 tf->nsect = fis[12];
157 tf->hob_nsect = fis[13];
158}
159
Albert Lee8cbd6df2005-10-12 15:06:27 +0800160static const u8 ata_rw_cmds[] = {
161 /* pio multi */
162 ATA_CMD_READ_MULTI,
163 ATA_CMD_WRITE_MULTI,
164 ATA_CMD_READ_MULTI_EXT,
165 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100166 0,
167 0,
168 0,
169 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800170 /* pio */
171 ATA_CMD_PIO_READ,
172 ATA_CMD_PIO_WRITE,
173 ATA_CMD_PIO_READ_EXT,
174 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100175 0,
176 0,
177 0,
178 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800179 /* dma */
180 ATA_CMD_READ,
181 ATA_CMD_WRITE,
182 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100183 ATA_CMD_WRITE_EXT,
184 0,
185 0,
186 0,
187 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800188};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800191 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
192 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800194 * Examine the device configuration and tf->flags to calculate
195 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
197 * LOCKING:
198 * caller.
199 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100200int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800202 struct ata_taskfile *tf = &qc->tf;
203 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100204 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800207
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100208 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800209 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
210 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Albert Lee8cbd6df2005-10-12 15:06:27 +0800212 if (dev->flags & ATA_DFLAG_PIO) {
213 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100214 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000215 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
216 /* Unable to use DMA due to host limitation */
217 tf->protocol = ATA_PROT_PIO;
Albert Leeaef9d532006-02-08 16:37:43 +0800218 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800219 } else {
220 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100221 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100224 cmd = ata_rw_cmds[index + fua + lba48 + write];
225 if (cmd) {
226 tf->command = cmd;
227 return 0;
228 }
229 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Tejun Heocb95d562006-03-06 04:31:56 +0900232/**
233 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
234 * @pio_mask: pio_mask
235 * @mwdma_mask: mwdma_mask
236 * @udma_mask: udma_mask
237 *
238 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
239 * unsigned int xfer_mask.
240 *
241 * LOCKING:
242 * None.
243 *
244 * RETURNS:
245 * Packed xfer_mask.
246 */
247static unsigned int ata_pack_xfermask(unsigned int pio_mask,
248 unsigned int mwdma_mask,
249 unsigned int udma_mask)
250{
251 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
252 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
253 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
254}
255
256static const struct ata_xfer_ent {
257 unsigned int shift, bits;
258 u8 base;
259} ata_xfer_tbl[] = {
260 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
261 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
262 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
263 { -1, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264};
265
266/**
Tejun Heocb95d562006-03-06 04:31:56 +0900267 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
268 * @xfer_mask: xfer_mask of interest
269 *
270 * Return matching XFER_* value for @xfer_mask. Only the highest
271 * bit of @xfer_mask is considered.
272 *
273 * LOCKING:
274 * None.
275 *
276 * RETURNS:
277 * Matching XFER_* value, 0 if no match found.
278 */
279static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
280{
281 int highbit = fls(xfer_mask) - 1;
282 const struct ata_xfer_ent *ent;
283
284 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
285 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
286 return ent->base + highbit - ent->shift;
287 return 0;
288}
289
290/**
291 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
292 * @xfer_mode: XFER_* of interest
293 *
294 * Return matching xfer_mask for @xfer_mode.
295 *
296 * LOCKING:
297 * None.
298 *
299 * RETURNS:
300 * Matching xfer_mask, 0 if no match found.
301 */
302static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
303{
304 const struct ata_xfer_ent *ent;
305
306 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
307 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
308 return 1 << (ent->shift + xfer_mode - ent->base);
309 return 0;
310}
311
312/**
313 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
314 * @xfer_mode: XFER_* of interest
315 *
316 * Return matching xfer_shift for @xfer_mode.
317 *
318 * LOCKING:
319 * None.
320 *
321 * RETURNS:
322 * Matching xfer_shift, -1 if no match found.
323 */
324static int ata_xfer_mode2shift(unsigned int xfer_mode)
325{
326 const struct ata_xfer_ent *ent;
327
328 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
329 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
330 return ent->shift;
331 return -1;
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900335 * ata_mode_string - convert xfer_mask to string
336 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 *
338 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900339 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 *
341 * LOCKING:
342 * None.
343 *
344 * RETURNS:
345 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900346 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900348static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Tejun Heo75f554b2006-03-06 04:31:57 +0900350 static const char * const xfer_mode_str[] = {
351 "PIO0",
352 "PIO1",
353 "PIO2",
354 "PIO3",
355 "PIO4",
356 "MWDMA0",
357 "MWDMA1",
358 "MWDMA2",
359 "UDMA/16",
360 "UDMA/25",
361 "UDMA/33",
362 "UDMA/44",
363 "UDMA/66",
364 "UDMA/100",
365 "UDMA/133",
366 "UDMA7",
367 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900368 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900370 highbit = fls(xfer_mask) - 1;
371 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
372 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/**
377 * ata_pio_devchk - PATA device presence detection
378 * @ap: ATA channel to examine
379 * @device: Device to examine (starting at zero)
380 *
381 * This technique was originally described in
382 * Hale Landis's ATADRVR (www.ata-atapi.com), and
383 * later found its way into the ATA/ATAPI spec.
384 *
385 * Write a pattern to the ATA shadow registers,
386 * and if a device is present, it will respond by
387 * correctly storing and echoing back the
388 * ATA shadow register contents.
389 *
390 * LOCKING:
391 * caller.
392 */
393
394static unsigned int ata_pio_devchk(struct ata_port *ap,
395 unsigned int device)
396{
397 struct ata_ioports *ioaddr = &ap->ioaddr;
398 u8 nsect, lbal;
399
400 ap->ops->dev_select(ap, device);
401
402 outb(0x55, ioaddr->nsect_addr);
403 outb(0xaa, ioaddr->lbal_addr);
404
405 outb(0xaa, ioaddr->nsect_addr);
406 outb(0x55, ioaddr->lbal_addr);
407
408 outb(0x55, ioaddr->nsect_addr);
409 outb(0xaa, ioaddr->lbal_addr);
410
411 nsect = inb(ioaddr->nsect_addr);
412 lbal = inb(ioaddr->lbal_addr);
413
414 if ((nsect == 0x55) && (lbal == 0xaa))
415 return 1; /* we found a device */
416
417 return 0; /* nothing found */
418}
419
420/**
421 * ata_mmio_devchk - PATA device presence detection
422 * @ap: ATA channel to examine
423 * @device: Device to examine (starting at zero)
424 *
425 * This technique was originally described in
426 * Hale Landis's ATADRVR (www.ata-atapi.com), and
427 * later found its way into the ATA/ATAPI spec.
428 *
429 * Write a pattern to the ATA shadow registers,
430 * and if a device is present, it will respond by
431 * correctly storing and echoing back the
432 * ATA shadow register contents.
433 *
434 * LOCKING:
435 * caller.
436 */
437
438static unsigned int ata_mmio_devchk(struct ata_port *ap,
439 unsigned int device)
440{
441 struct ata_ioports *ioaddr = &ap->ioaddr;
442 u8 nsect, lbal;
443
444 ap->ops->dev_select(ap, device);
445
446 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
447 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
448
449 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
450 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
451
452 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
453 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
454
455 nsect = readb((void __iomem *) ioaddr->nsect_addr);
456 lbal = readb((void __iomem *) ioaddr->lbal_addr);
457
458 if ((nsect == 0x55) && (lbal == 0xaa))
459 return 1; /* we found a device */
460
461 return 0; /* nothing found */
462}
463
464/**
465 * ata_devchk - PATA device presence detection
466 * @ap: ATA channel to examine
467 * @device: Device to examine (starting at zero)
468 *
469 * Dispatch ATA device presence detection, depending
470 * on whether we are using PIO or MMIO to talk to the
471 * ATA shadow registers.
472 *
473 * LOCKING:
474 * caller.
475 */
476
477static unsigned int ata_devchk(struct ata_port *ap,
478 unsigned int device)
479{
480 if (ap->flags & ATA_FLAG_MMIO)
481 return ata_mmio_devchk(ap, device);
482 return ata_pio_devchk(ap, device);
483}
484
485/**
486 * ata_dev_classify - determine device type based on ATA-spec signature
487 * @tf: ATA taskfile register set for device to be identified
488 *
489 * Determine from taskfile register contents whether a device is
490 * ATA or ATAPI, as per "Signature and persistence" section
491 * of ATA/PI spec (volume 1, sect 5.14).
492 *
493 * LOCKING:
494 * None.
495 *
496 * RETURNS:
497 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
498 * the event of failure.
499 */
500
Jeff Garzik057ace52005-10-22 14:27:05 -0400501unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 /* Apple's open source Darwin code hints that some devices only
504 * put a proper signature into the LBA mid/high registers,
505 * So, we only check those. It's sufficient for uniqueness.
506 */
507
508 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
509 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
510 DPRINTK("found ATA device by sig\n");
511 return ATA_DEV_ATA;
512 }
513
514 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
515 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
516 DPRINTK("found ATAPI device by sig\n");
517 return ATA_DEV_ATAPI;
518 }
519
520 DPRINTK("unknown device\n");
521 return ATA_DEV_UNKNOWN;
522}
523
524/**
525 * ata_dev_try_classify - Parse returned ATA device signature
526 * @ap: ATA channel to examine
527 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900528 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 *
530 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
531 * an ATA/ATAPI-defined set of values is placed in the ATA
532 * shadow registers, indicating the results of device detection
533 * and diagnostics.
534 *
535 * Select the ATA device, and read the values from the ATA shadow
536 * registers. Then parse according to the Error register value,
537 * and the spec-defined values examined by ata_dev_classify().
538 *
539 * LOCKING:
540 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900541 *
542 * RETURNS:
543 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
545
Tejun Heob4dc7622006-01-24 17:05:22 +0900546static unsigned int
547ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct ata_taskfile tf;
550 unsigned int class;
551 u8 err;
552
553 ap->ops->dev_select(ap, device);
554
555 memset(&tf, 0, sizeof(tf));
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400558 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900559 if (r_err)
560 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /* see if device passed diags */
563 if (err == 1)
564 /* do nothing */ ;
565 else if ((device == 0) && (err == 0x81))
566 /* do nothing */ ;
567 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900568 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Tejun Heob4dc7622006-01-24 17:05:22 +0900570 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900574 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900576 return ATA_DEV_NONE;
577 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900581 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * @id: IDENTIFY DEVICE results we will examine
583 * @s: string into which data is output
584 * @ofs: offset into identify device page
585 * @len: length of string to return. must be an even number.
586 *
587 * The strings in the IDENTIFY DEVICE page are broken up into
588 * 16-bit chunks. Run through the string, and output each
589 * 8-bit chunk linearly, regardless of platform.
590 *
591 * LOCKING:
592 * caller.
593 */
594
Tejun Heo6a62a042006-02-13 10:02:46 +0900595void ata_id_string(const u16 *id, unsigned char *s,
596 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 unsigned int c;
599
600 while (len > 0) {
601 c = id[ofs] >> 8;
602 *s = c;
603 s++;
604
605 c = id[ofs] & 0xff;
606 *s = c;
607 s++;
608
609 ofs++;
610 len -= 2;
611 }
612}
613
Tejun Heo0e949ff2006-02-12 22:47:04 +0900614/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900615 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900616 * @id: IDENTIFY DEVICE results we will examine
617 * @s: string into which data is output
618 * @ofs: offset into identify device page
619 * @len: length of string to return. must be an odd number.
620 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900621 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900622 * trims trailing spaces and terminates the resulting string with
623 * null. @len must be actual maximum length (even number) + 1.
624 *
625 * LOCKING:
626 * caller.
627 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900628void ata_id_c_string(const u16 *id, unsigned char *s,
629 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900630{
631 unsigned char *p;
632
633 WARN_ON(!(len & 1));
634
Tejun Heo6a62a042006-02-13 10:02:46 +0900635 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900636
637 p = s + strnlen(s, len - 1);
638 while (p > s && p[-1] == ' ')
639 p--;
640 *p = '\0';
641}
Edward Falk0baab862005-06-02 18:17:13 -0400642
Tejun Heo29407402006-02-12 22:47:04 +0900643static u64 ata_id_n_sectors(const u16 *id)
644{
645 if (ata_id_has_lba(id)) {
646 if (ata_id_has_lba48(id))
647 return ata_id_u64(id, 100);
648 else
649 return ata_id_u32(id, 60);
650 } else {
651 if (ata_id_current_chs_valid(id))
652 return ata_id_u32(id, 57);
653 else
654 return id[1] * id[3] * id[6];
655 }
656}
Edward Falk0baab862005-06-02 18:17:13 -0400657
658/**
659 * ata_noop_dev_select - Select device 0/1 on ATA bus
660 * @ap: ATA channel to manipulate
661 * @device: ATA device (numbered from zero) to select
662 *
663 * This function performs no actual function.
664 *
665 * May be used as the dev_select() entry in ata_port_operations.
666 *
667 * LOCKING:
668 * caller.
669 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
671{
672}
673
Edward Falk0baab862005-06-02 18:17:13 -0400674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/**
676 * ata_std_dev_select - Select device 0/1 on ATA bus
677 * @ap: ATA channel to manipulate
678 * @device: ATA device (numbered from zero) to select
679 *
680 * Use the method defined in the ATA specification to
681 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400682 * ATA channel. Works with both PIO and MMIO.
683 *
684 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
686 * LOCKING:
687 * caller.
688 */
689
690void ata_std_dev_select (struct ata_port *ap, unsigned int device)
691{
692 u8 tmp;
693
694 if (device == 0)
695 tmp = ATA_DEVICE_OBS;
696 else
697 tmp = ATA_DEVICE_OBS | ATA_DEV1;
698
699 if (ap->flags & ATA_FLAG_MMIO) {
700 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
701 } else {
702 outb(tmp, ap->ioaddr.device_addr);
703 }
704 ata_pause(ap); /* needed; also flushes, for mmio */
705}
706
707/**
708 * ata_dev_select - Select device 0/1 on ATA bus
709 * @ap: ATA channel to manipulate
710 * @device: ATA device (numbered from zero) to select
711 * @wait: non-zero to wait for Status register BSY bit to clear
712 * @can_sleep: non-zero if context allows sleeping
713 *
714 * Use the method defined in the ATA specification to
715 * make either device 0, or device 1, active on the
716 * ATA channel.
717 *
718 * This is a high-level version of ata_std_dev_select(),
719 * which additionally provides the services of inserting
720 * the proper pauses and status polling, where needed.
721 *
722 * LOCKING:
723 * caller.
724 */
725
726void ata_dev_select(struct ata_port *ap, unsigned int device,
727 unsigned int wait, unsigned int can_sleep)
728{
729 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
730 ap->id, device, wait);
731
732 if (wait)
733 ata_wait_idle(ap);
734
735 ap->ops->dev_select(ap, device);
736
737 if (wait) {
738 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
739 msleep(150);
740 ata_wait_idle(ap);
741 }
742}
743
744/**
745 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900746 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900748 * Dump selected 16-bit words from the given IDENTIFY DEVICE
749 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 *
751 * LOCKING:
752 * caller.
753 */
754
Tejun Heo0bd33002006-02-12 22:47:05 +0900755static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 DPRINTK("49==0x%04x "
758 "53==0x%04x "
759 "63==0x%04x "
760 "64==0x%04x "
761 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900762 id[49],
763 id[53],
764 id[63],
765 id[64],
766 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 DPRINTK("80==0x%04x "
768 "81==0x%04x "
769 "82==0x%04x "
770 "83==0x%04x "
771 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900772 id[80],
773 id[81],
774 id[82],
775 id[83],
776 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 DPRINTK("88==0x%04x "
778 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900779 id[88],
780 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Tejun Heocb95d562006-03-06 04:31:56 +0900783/**
784 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
785 * @id: IDENTIFY data to compute xfer mask from
Alan Cox11e29e22005-10-21 18:46:32 -0400786 *
Tejun Heocb95d562006-03-06 04:31:56 +0900787 * Compute the xfermask for this device. This is not as trivial
788 * as it seems if we must consider early devices correctly.
789 *
790 * FIXME: pre IDE drive timing (do we care ?).
791 *
792 * LOCKING:
793 * None.
794 *
795 * RETURNS:
796 * Computed xfermask
Alan Cox11e29e22005-10-21 18:46:32 -0400797 */
Tejun Heocb95d562006-03-06 04:31:56 +0900798static unsigned int ata_id_xfermask(const u16 *id)
Alan Cox11e29e22005-10-21 18:46:32 -0400799{
Tejun Heocb95d562006-03-06 04:31:56 +0900800 unsigned int pio_mask, mwdma_mask, udma_mask;
Alan Cox11e29e22005-10-21 18:46:32 -0400801
Alan Coxffa29452006-01-09 17:14:40 +0000802 /* Usual case. Word 53 indicates word 64 is valid */
Tejun Heocb95d562006-03-06 04:31:56 +0900803 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
804 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
805 pio_mask <<= 3;
806 pio_mask |= 0x7;
807 } else {
808 /* If word 64 isn't valid then Word 51 high byte holds
809 * the PIO timing number for the maximum. Turn it into
810 * a mask.
811 */
812 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
813
814 /* But wait.. there's more. Design your standards by
815 * committee and you too can get a free iordy field to
816 * process. However its the speeds not the modes that
817 * are supported... Note drivers using the timing API
818 * will get this right anyway
819 */
Alan Cox11e29e22005-10-21 18:46:32 -0400820 }
821
Tejun Heocb95d562006-03-06 04:31:56 +0900822 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
823 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Alan Cox11e29e22005-10-21 18:46:32 -0400824
Tejun Heocb95d562006-03-06 04:31:56 +0900825 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
Tejun Heoc18d06f2006-02-02 00:56:10 +0900826}
827
828/**
Tejun Heo86e45b62006-03-05 15:29:09 +0900829 * ata_port_queue_task - Queue port_task
830 * @ap: The ata_port to queue port_task for
Tejun Heoc18d06f2006-02-02 00:56:10 +0900831 *
Tejun Heo86e45b62006-03-05 15:29:09 +0900832 * Schedule @fn(@data) for execution after @delay jiffies using
833 * port_task. There is one port_task per port and it's the
834 * user(low level driver)'s responsibility to make sure that only
835 * one task is active at any given time.
836 *
837 * libata core layer takes care of synchronization between
838 * port_task and EH. ata_port_queue_task() may be ignored for EH
839 * synchronization.
840 *
841 * LOCKING:
842 * Inherited from caller.
843 */
844void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
845 unsigned long delay)
846{
847 int rc;
848
Tejun Heo2e755f62006-03-05 15:29:09 +0900849 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900850 return;
851
852 PREPARE_WORK(&ap->port_task, fn, data);
853
854 if (!delay)
855 rc = queue_work(ata_wq, &ap->port_task);
856 else
857 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
858
859 /* rc == 0 means that another user is using port task */
860 WARN_ON(rc == 0);
861}
862
863/**
864 * ata_port_flush_task - Flush port_task
865 * @ap: The ata_port to flush port_task for
866 *
867 * After this function completes, port_task is guranteed not to
868 * be running or scheduled.
Tejun Heoc18d06f2006-02-02 00:56:10 +0900869 *
870 * LOCKING:
871 * Kernel thread context (may sleep)
872 */
Tejun Heo86e45b62006-03-05 15:29:09 +0900873void ata_port_flush_task(struct ata_port *ap)
Tejun Heoc18d06f2006-02-02 00:56:10 +0900874{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900875 unsigned long flags;
876
877 DPRINTK("ENTER\n");
878
879 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900880 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heoc18d06f2006-02-02 00:56:10 +0900881 spin_unlock_irqrestore(&ap->host_set->lock, flags);
882
883 DPRINTK("flush #1\n");
884 flush_workqueue(ata_wq);
885
886 /*
887 * At this point, if a task is running, it's guaranteed to see
888 * the FLUSH flag; thus, it will never queue pio tasks again.
889 * Cancel and flush.
890 */
Tejun Heo86e45b62006-03-05 15:29:09 +0900891 if (!cancel_delayed_work(&ap->port_task)) {
Tejun Heoc18d06f2006-02-02 00:56:10 +0900892 DPRINTK("flush #2\n");
893 flush_workqueue(ata_wq);
894 }
895
896 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900897 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heoc18d06f2006-02-02 00:56:10 +0900898 spin_unlock_irqrestore(&ap->host_set->lock, flags);
899
900 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +0900901}
902
Tejun Heo77853bf2006-01-23 13:09:36 +0900903void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Jeff Garzik64f043d2005-11-17 10:50:01 -0500904{
Tejun Heo77853bf2006-01-23 13:09:36 +0900905 struct completion *waiting = qc->private_data;
Jeff Garzik64f043d2005-11-17 10:50:01 -0500906
Tejun Heo77853bf2006-01-23 13:09:36 +0900907 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900908 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900909}
Jeff Garzik64f043d2005-11-17 10:50:01 -0500910
Tejun Heoa2a7a662005-12-13 14:48:31 +0900911/**
912 * ata_exec_internal - execute libata internal command
913 * @ap: Port to which the command is sent
914 * @dev: Device to which the command is sent
915 * @tf: Taskfile registers for the command and the result
916 * @dma_dir: Data tranfer direction of the command
917 * @buf: Data buffer of the command
918 * @buflen: Length of data buffer
919 *
920 * Executes libata internal command with timeout. @tf contains
921 * command on entry and result on return. Timeout and error
922 * conditions are reported via return value. No recovery action
923 * is taken after a command times out. It's caller's duty to
924 * clean up after timeout.
925 *
926 * LOCKING:
927 * None. Should be called with kernel context, might sleep.
928 */
929
930static unsigned
931ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
932 struct ata_taskfile *tf,
933 int dma_dir, void *buf, unsigned int buflen)
934{
935 u8 command = tf->command;
936 struct ata_queued_cmd *qc;
937 DECLARE_COMPLETION(wait);
938 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900939 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900940
941 spin_lock_irqsave(&ap->host_set->lock, flags);
942
943 qc = ata_qc_new_init(ap, dev);
944 BUG_ON(qc == NULL);
945
946 qc->tf = *tf;
947 qc->dma_dir = dma_dir;
948 if (dma_dir != DMA_NONE) {
949 ata_sg_init_one(qc, buf, buflen);
950 qc->nsect = buflen / ATA_SECT_SIZE;
Jeff Garzik64f043d2005-11-17 10:50:01 -0500951 }
952
Tejun Heo77853bf2006-01-23 13:09:36 +0900953 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900954 qc->complete_fn = ata_qc_complete_internal;
955
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900956 qc->err_mask = ata_qc_issue(qc);
957 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900958 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900959
960 spin_unlock_irqrestore(&ap->host_set->lock, flags);
961
962 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
963 spin_lock_irqsave(&ap->host_set->lock, flags);
964
965 /* We're racing with irq here. If we lose, the
966 * following test prevents us from completing the qc
967 * again. If completion irq occurs after here but
968 * before the caller cleans up, it will result in a
969 * spurious interrupt. We can live with that.
970 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900971 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900972 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900973 ata_qc_complete(qc);
974 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
975 ap->id, command);
976 }
977
978 spin_unlock_irqrestore(&ap->host_set->lock, flags);
979 }
980
Tejun Heo77853bf2006-01-23 13:09:36 +0900981 *tf = qc->tf;
982 err_mask = qc->err_mask;
983
984 ata_qc_free(qc);
985
986 return err_mask;
Jeff Garzik64f043d2005-11-17 10:50:01 -0500987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +0000990 * ata_pio_need_iordy - check if iordy needed
991 * @adev: ATA device
992 *
993 * Check if the current speed of the device requires IORDY. Used
994 * by various controllers for chip configuration.
995 */
996
997unsigned int ata_pio_need_iordy(const struct ata_device *adev)
998{
999 int pio;
1000 int speed = adev->pio_mode - XFER_PIO_0;
1001
1002 if (speed < 2)
1003 return 0;
1004 if (speed > 2)
1005 return 1;
1006
1007 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1008
1009 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1010 pio = adev->id[ATA_ID_EIDE_PIO];
1011 /* Is the speed faster than the drive allows non IORDY ? */
1012 if (pio) {
1013 /* This is cycle times not frequency - watch the logic! */
1014 if (pio > 240) /* PIO2 is 240nS per cycle */
1015 return 1;
1016 return 0;
1017 }
1018 }
1019 return 0;
1020}
1021
1022/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001023 * ata_dev_read_id - Read ID data from the specified device
1024 * @ap: port on which target device resides
1025 * @dev: target device
1026 * @p_class: pointer to class of the target device (may be changed)
1027 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +09001028 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +09001029 *
1030 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1031 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1032 * devices. This function also takes care of EDD signature
1033 * misreporting (to be removed once EDD support is gone) and
1034 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
1035 *
1036 * LOCKING:
1037 * Kernel thread context (may sleep)
1038 *
1039 * RETURNS:
1040 * 0 on success, -errno otherwise.
1041 */
1042static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +09001043 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001044{
1045 unsigned int class = *p_class;
1046 unsigned int using_edd;
1047 struct ata_taskfile tf;
1048 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +09001049 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001050 const char *reason;
1051 int rc;
1052
1053 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1054
1055 if (ap->ops->probe_reset ||
1056 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1057 using_edd = 0;
1058 else
1059 using_edd = 1;
1060
1061 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1062
Tejun Heod9572b12006-03-01 16:09:35 +09001063 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1064 if (id == NULL) {
1065 rc = -ENOMEM;
1066 reason = "out of memory";
1067 goto err_out;
1068 }
1069
Tejun Heo49016ac2006-02-21 02:12:11 +09001070 retry:
1071 ata_tf_init(ap, &tf, dev->devno);
1072
1073 switch (class) {
1074 case ATA_DEV_ATA:
1075 tf.command = ATA_CMD_ID_ATA;
1076 break;
1077 case ATA_DEV_ATAPI:
1078 tf.command = ATA_CMD_ID_ATAPI;
1079 break;
1080 default:
1081 rc = -ENODEV;
1082 reason = "unsupported class";
1083 goto err_out;
1084 }
1085
1086 tf.protocol = ATA_PROT_PIO;
1087
1088 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1089 id, sizeof(id[0]) * ATA_ID_WORDS);
1090
1091 if (err_mask) {
1092 rc = -EIO;
1093 reason = "I/O error";
1094
1095 if (err_mask & ~AC_ERR_DEV)
1096 goto err_out;
1097
1098 /*
1099 * arg! EDD works for all test cases, but seems to return
1100 * the ATA signature for some ATAPI devices. Until the
1101 * reason for this is found and fixed, we fix up the mess
1102 * here. If IDENTIFY DEVICE returns command aborted
1103 * (as ATAPI devices do), then we issue an
1104 * IDENTIFY PACKET DEVICE.
1105 *
1106 * ATA software reset (SRST, the default) does not appear
1107 * to have this problem.
1108 */
1109 if ((using_edd) && (class == ATA_DEV_ATA)) {
1110 u8 err = tf.feature;
1111 if (err & ATA_ABORTED) {
1112 class = ATA_DEV_ATAPI;
1113 goto retry;
1114 }
1115 }
1116 goto err_out;
1117 }
1118
1119 swap_buf_le16(id, ATA_ID_WORDS);
1120
1121 /* print device capabilities */
1122 printk(KERN_DEBUG "ata%u: dev %u cfg "
1123 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1124 ap->id, dev->devno,
1125 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1126
1127 /* sanity check */
1128 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1129 rc = -EINVAL;
1130 reason = "device reports illegal type";
1131 goto err_out;
1132 }
1133
1134 if (post_reset && class == ATA_DEV_ATA) {
1135 /*
1136 * The exact sequence expected by certain pre-ATA4 drives is:
1137 * SRST RESET
1138 * IDENTIFY
1139 * INITIALIZE DEVICE PARAMETERS
1140 * anything else..
1141 * Some drives were very specific about that exact sequence.
1142 */
1143 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1144 err_mask = ata_dev_init_params(ap, dev);
1145 if (err_mask) {
1146 rc = -EIO;
1147 reason = "INIT_DEV_PARAMS failed";
1148 goto err_out;
1149 }
1150
1151 /* current CHS translation info (id[53-58]) might be
1152 * changed. reread the identify device info.
1153 */
1154 post_reset = 0;
1155 goto retry;
1156 }
1157 }
1158
1159 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001160 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001161 return 0;
1162
1163 err_out:
1164 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1165 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001166 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001167 return rc;
1168}
1169
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001170static inline u8 ata_dev_knobble(const struct ata_port *ap,
1171 struct ata_device *dev)
1172{
1173 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1174}
1175
Tejun Heo49016ac2006-02-21 02:12:11 +09001176/**
Tejun Heoffeae412006-03-01 16:09:35 +09001177 * ata_dev_configure - Configure the specified ATA/ATAPI device
1178 * @ap: Port on which target device resides
1179 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001180 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 *
Tejun Heoffeae412006-03-01 16:09:35 +09001182 * Configure @dev according to @dev->id. Generic and low-level
1183 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 *
1185 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001186 * Kernel thread context (may sleep)
1187 *
1188 * RETURNS:
1189 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001191static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1192 int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Tejun Heoff8854b2006-03-06 04:31:56 +09001194 unsigned int xfer_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001195 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (!ata_dev_present(dev)) {
1198 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001199 ap->id, dev->devno);
1200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
Tejun Heoffeae412006-03-01 16:09:35 +09001203 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Tejun Heo208a9932006-03-05 17:55:58 +09001205 /* initialize to-be-configured parameters */
1206 dev->flags = 0;
1207 dev->max_sectors = 0;
1208 dev->cdb_len = 0;
1209 dev->n_sectors = 0;
1210 dev->cylinders = 0;
1211 dev->heads = 0;
1212 dev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /*
1215 * common ATA, ATAPI feature tests
1216 */
1217
Albert Lee8bf62ec2005-05-12 15:29:42 -04001218 /* we require DMA support (bits 8 of word 49) */
1219 if (!ata_id_has_dma(dev->id)) {
1220 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001221 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 goto err_out_nosup;
1223 }
1224
Tejun Heoff8854b2006-03-06 04:31:56 +09001225 /* find max transfer mode; for printk only */
1226 xfer_mask = ata_id_xfermask(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Tejun Heo0bd33002006-02-12 22:47:05 +09001228 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /* ATA-specific feature tests */
1231 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001232 dev->n_sectors = ata_id_n_sectors(dev->id);
1233
Albert Lee8bf62ec2005-05-12 15:29:42 -04001234 if (ata_id_has_lba(dev->id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001235 const char *lba_desc;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001236
Tejun Heo4c2d7212006-03-05 17:55:58 +09001237 lba_desc = "LBA";
1238 dev->flags |= ATA_DFLAG_LBA;
1239 if (ata_id_has_lba48(dev->id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001240 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001241 lba_desc = "LBA48";
1242 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04001243
1244 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001245 if (print_info)
1246 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1247 "max %s, %Lu sectors: %s\n",
1248 ap->id, dev->devno,
1249 ata_id_major_version(dev->id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001250 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001251 (unsigned long long)dev->n_sectors,
1252 lba_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001253 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001254 /* CHS */
1255
1256 /* Default translation */
1257 dev->cylinders = dev->id[1];
1258 dev->heads = dev->id[3];
1259 dev->sectors = dev->id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001260
1261 if (ata_id_current_chs_valid(dev->id)) {
1262 /* Current CHS translation is valid. */
1263 dev->cylinders = dev->id[54];
1264 dev->heads = dev->id[55];
1265 dev->sectors = dev->id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001266 }
1267
1268 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001269 if (print_info)
1270 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1271 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1272 ap->id, dev->devno,
1273 ata_id_major_version(dev->id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001274 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001275 (unsigned long long)dev->n_sectors,
1276 dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
Albert Lee07f6f7d2005-11-01 19:33:20 +08001279 if (dev->id[59] & 0x100) {
1280 dev->multi_count = dev->id[59] & 0xff;
1281 DPRINTK("ata%u: dev %u multi count %u\n",
1282 ap->id, device, dev->multi_count);
1283 }
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286
1287 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001288 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 rc = atapi_cdb_len(dev->id);
1290 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1291 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001292 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 goto err_out_nosup;
1294 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001295 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Albert Lee312f7da2005-09-27 17:38:03 +08001297 if (ata_id_cdb_intr(dev->id))
1298 dev->flags |= ATA_DFLAG_CDB_INTR;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001301 if (print_info)
1302 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoff8854b2006-03-06 04:31:56 +09001303 ap->id, dev->devno, ata_mode_string(xfer_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
Tejun Heo6e7846e2006-02-12 23:32:58 +09001306 ap->host->max_cmd_len = 0;
1307 for (i = 0; i < ATA_MAX_DEVICES; i++)
1308 ap->host->max_cmd_len = max_t(unsigned int,
1309 ap->host->max_cmd_len,
1310 ap->device[i].cdb_len);
1311
Brad Campbell6f2f3812005-05-12 15:07:47 -04001312 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001313 if (ata_dev_knobble(ap, dev)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001314 if (print_info)
1315 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1316 ap->id, dev->devno);
Brad Campbell6f2f3812005-05-12 15:07:47 -04001317 ap->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001318 dev->max_sectors = ATA_MAX_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001319 }
1320
1321 if (ap->ops->dev_config)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001322 ap->ops->dev_config(ap, dev);
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327err_out_nosup:
1328 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001329 ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001331 return rc;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/**
1335 * ata_bus_probe - Reset and probe ATA bus
1336 * @ap: Bus to probe
1337 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001338 * Master ATA bus probing function. Initiates a hardware-dependent
1339 * bus reset, then attempts to identify any devices found on
1340 * the bus.
1341 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001343 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 *
1345 * RETURNS:
1346 * Zero on success, non-zero on error.
1347 */
1348
1349static int ata_bus_probe(struct ata_port *ap)
1350{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001351 unsigned int classes[ATA_MAX_DEVICES];
1352 unsigned int i, rc, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Tejun Heo28ca5c52006-03-01 16:09:36 +09001354 ata_port_probe(ap);
1355
1356 /* reset */
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001357 if (ap->ops->probe_reset) {
Tejun Heo2061a472006-03-12 00:57:39 +09001358 for (i = 0; i < ATA_MAX_DEVICES; i++)
1359 classes[i] = ATA_DEV_UNKNOWN;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001360
1361 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001362 if (rc) {
1363 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1364 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001365 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001366
1367 for (i = 0; i < ATA_MAX_DEVICES; i++)
1368 if (classes[i] == ATA_DEV_UNKNOWN)
1369 classes[i] = ATA_DEV_NONE;
1370 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001371 ap->ops->phy_reset(ap);
1372
Tejun Heo28ca5c52006-03-01 16:09:36 +09001373 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1374 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1375 classes[i] = ap->device[i].class;
1376 else
1377 ap->device[i].class = ATA_DEV_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001379 ata_port_probe(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
Tejun Heo28ca5c52006-03-01 16:09:36 +09001382 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoffeae412006-03-01 16:09:35 +09001384 struct ata_device *dev = &ap->device[i];
1385
Tejun Heo28ca5c52006-03-01 16:09:36 +09001386 dev->class = classes[i];
1387
Tejun Heoffeae412006-03-01 16:09:35 +09001388 if (!ata_dev_present(dev))
1389 continue;
1390
1391 WARN_ON(dev->id != NULL);
1392 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1393 dev->class = ATA_DEV_NONE;
1394 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Tejun Heoffeae412006-03-01 16:09:35 +09001396
Tejun Heo4c2d7212006-03-05 17:55:58 +09001397 if (ata_dev_configure(ap, dev, 1)) {
Tejun Heoffeae412006-03-01 16:09:35 +09001398 dev->class++; /* disable device */
1399 continue;
1400 }
1401
Tejun Heoffeae412006-03-01 16:09:35 +09001402 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
Tejun Heo28ca5c52006-03-01 16:09:36 +09001405 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 goto err_out_disable;
1407
1408 ata_set_mode(ap);
1409 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1410 goto err_out_disable;
1411
1412 return 0;
1413
1414err_out_disable:
1415 ap->ops->port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -1;
1417}
1418
1419/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001420 * ata_port_probe - Mark port as enabled
1421 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001423 * Modify @ap data structure such that the system
1424 * thinks that the entire port is enabled.
1425 *
1426 * LOCKING: host_set lock, or some other form of
1427 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
1429
1430void ata_port_probe(struct ata_port *ap)
1431{
1432 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1433}
1434
1435/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001436 * sata_print_link_status - Print SATA link status
1437 * @ap: SATA port to printk link status about
1438 *
1439 * This function prints link speed and status of a SATA link.
1440 *
1441 * LOCKING:
1442 * None.
1443 */
1444static void sata_print_link_status(struct ata_port *ap)
1445{
1446 u32 sstatus, tmp;
1447 const char *speed;
1448
1449 if (!ap->ops->scr_read)
1450 return;
1451
1452 sstatus = scr_read(ap, SCR_STATUS);
1453
1454 if (sata_dev_present(ap)) {
1455 tmp = (sstatus >> 4) & 0xf;
1456 if (tmp & (1 << 0))
1457 speed = "1.5";
1458 else if (tmp & (1 << 1))
1459 speed = "3.0";
1460 else
1461 speed = "<unknown>";
1462 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1463 ap->id, speed, sstatus);
1464 } else {
1465 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1466 ap->id, sstatus);
1467 }
1468}
1469
1470/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001471 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1472 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001474 * This function issues commands to standard SATA Sxxx
1475 * PHY registers, to wake up the phy (and device), and
1476 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 *
1478 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001479 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 *
1481 */
1482void __sata_phy_reset(struct ata_port *ap)
1483{
1484 u32 sstatus;
1485 unsigned long timeout = jiffies + (HZ * 5);
1486
1487 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001488 /* issue phy wake/reset */
1489 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001490 /* Couldn't find anything in SATA I/II specs, but
1491 * AHCI-1.1 10.4.2 says at least 1 ms. */
1492 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Brett Russcdcca892005-03-28 15:10:27 -05001494 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 /* wait for phy to become ready, if necessary */
1497 do {
1498 msleep(200);
1499 sstatus = scr_read(ap, SCR_STATUS);
1500 if ((sstatus & 0xf) != 1)
1501 break;
1502 } while (time_before(jiffies, timeout));
1503
Tejun Heo3be680b2005-12-19 22:35:02 +09001504 /* print link status */
1505 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001506
Tejun Heo3be680b2005-12-19 22:35:02 +09001507 /* TODO: phy layer with polling, timeouts, etc. */
1508 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001510 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1514 return;
1515
1516 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1517 ata_port_disable(ap);
1518 return;
1519 }
1520
1521 ap->cbl = ATA_CBL_SATA;
1522}
1523
1524/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001525 * sata_phy_reset - Reset SATA bus.
1526 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001528 * This function resets the SATA bus, and then probes
1529 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 *
1531 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001532 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 *
1534 */
1535void sata_phy_reset(struct ata_port *ap)
1536{
1537 __sata_phy_reset(ap);
1538 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1539 return;
1540 ata_bus_reset(ap);
1541}
1542
1543/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001544 * ata_port_disable - Disable port.
1545 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001547 * Modify @ap data structure such that the system
1548 * thinks that the entire port is disabled, and should
1549 * never attempt to probe or communicate with devices
1550 * on this port.
1551 *
1552 * LOCKING: host_set lock, or some other form of
1553 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 */
1555
1556void ata_port_disable(struct ata_port *ap)
1557{
1558 ap->device[0].class = ATA_DEV_NONE;
1559 ap->device[1].class = ATA_DEV_NONE;
1560 ap->flags |= ATA_FLAG_PORT_DISABLED;
1561}
1562
Alan Cox452503f2005-10-21 19:01:32 -04001563/*
1564 * This mode timing computation functionality is ported over from
1565 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1566 */
1567/*
1568 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1569 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1570 * for PIO 5, which is a nonstandard extension and UDMA6, which
1571 * is currently supported only by Maxtor drives.
1572 */
1573
1574static const struct ata_timing ata_timing[] = {
1575
1576 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1577 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1578 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1579 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1580
1581 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1582 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1583 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1584
1585/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1586
1587 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1588 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1589 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1590
1591 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1592 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1593 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1594
1595/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1596 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1597 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1598
1599 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1600 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1601 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1602
1603/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1604
1605 { 0xFF }
1606};
1607
1608#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1609#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1610
1611static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1612{
1613 q->setup = EZ(t->setup * 1000, T);
1614 q->act8b = EZ(t->act8b * 1000, T);
1615 q->rec8b = EZ(t->rec8b * 1000, T);
1616 q->cyc8b = EZ(t->cyc8b * 1000, T);
1617 q->active = EZ(t->active * 1000, T);
1618 q->recover = EZ(t->recover * 1000, T);
1619 q->cycle = EZ(t->cycle * 1000, T);
1620 q->udma = EZ(t->udma * 1000, UT);
1621}
1622
1623void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1624 struct ata_timing *m, unsigned int what)
1625{
1626 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1627 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1628 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1629 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1630 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1631 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1632 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1633 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1634}
1635
1636static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1637{
1638 const struct ata_timing *t;
1639
1640 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001641 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001642 return NULL;
1643 return t;
1644}
1645
1646int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1647 struct ata_timing *t, int T, int UT)
1648{
1649 const struct ata_timing *s;
1650 struct ata_timing p;
1651
1652 /*
1653 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001654 */
Alan Cox452503f2005-10-21 19:01:32 -04001655
1656 if (!(s = ata_timing_find_mode(speed)))
1657 return -EINVAL;
1658
Albert Lee75b1f2f2005-11-16 17:06:18 +08001659 memcpy(t, s, sizeof(*s));
1660
Alan Cox452503f2005-10-21 19:01:32 -04001661 /*
1662 * If the drive is an EIDE drive, it can tell us it needs extended
1663 * PIO/MW_DMA cycle timing.
1664 */
1665
1666 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1667 memset(&p, 0, sizeof(p));
1668 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1669 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1670 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1671 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1672 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1673 }
1674 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1675 }
1676
1677 /*
1678 * Convert the timing to bus clock counts.
1679 */
1680
Albert Lee75b1f2f2005-11-16 17:06:18 +08001681 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001682
1683 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001684 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1685 * S.M.A.R.T * and some other commands. We have to ensure that the
1686 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001687 */
1688
1689 if (speed > XFER_PIO_4) {
1690 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1691 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1692 }
1693
1694 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001695 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001696 */
1697
1698 if (t->act8b + t->rec8b < t->cyc8b) {
1699 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1700 t->rec8b = t->cyc8b - t->act8b;
1701 }
1702
1703 if (t->active + t->recover < t->cycle) {
1704 t->active += (t->cycle - (t->active + t->recover)) / 2;
1705 t->recover = t->cycle - t->active;
1706 }
1707
1708 return 0;
1709}
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1714 return;
1715
1716 if (dev->xfer_shift == ATA_SHIFT_PIO)
1717 dev->flags |= ATA_DFLAG_PIO;
1718
1719 ata_dev_set_xfermode(ap, dev);
1720
Tejun Heo48a8a142006-03-05 17:55:58 +09001721 if (ata_dev_revalidate(ap, dev, 0)) {
1722 printk(KERN_ERR "ata%u: failed to revalidate after set "
1723 "xfermode, disabled\n", ap->id);
1724 ata_port_disable(ap);
1725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Tejun Heo23e71c32006-03-06 04:31:57 +09001727 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1728 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
Tejun Heo23e71c32006-03-06 04:31:57 +09001731 ap->id, dev->devno,
1732 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
1735static int ata_host_set_pio(struct ata_port *ap)
1736{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int i;
1738
1739 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1740 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001741
1742 if (!ata_dev_present(dev))
1743 continue;
1744
1745 if (!dev->pio_mode) {
1746 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1747 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09001749
1750 dev->xfer_mode = dev->pio_mode;
1751 dev->xfer_shift = ATA_SHIFT_PIO;
1752 if (ap->ops->set_piomode)
1753 ap->ops->set_piomode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
1756 return 0;
1757}
1758
Tejun Heoa6d5a512006-03-06 04:31:57 +09001759static void ata_host_set_dma(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 int i;
1762
1763 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1764 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001765
1766 if (!ata_dev_present(dev) || !dev->dma_mode)
1767 continue;
1768
1769 dev->xfer_mode = dev->dma_mode;
1770 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1771 if (ap->ops->set_dmamode)
1772 ap->ops->set_dmamode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774}
1775
1776/**
1777 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1778 * @ap: port on which timings will be programmed
1779 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001780 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1781 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001783 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
1785static void ata_set_mode(struct ata_port *ap)
1786{
Tejun Heoa6d5a512006-03-06 04:31:57 +09001787 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Tejun Heoa6d5a512006-03-06 04:31:57 +09001789 /* step 1: calculate xfer_mask */
1790 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1791 struct ata_device *dev = &ap->device[i];
1792 unsigned int xfer_mask;
1793
1794 if (!ata_dev_present(dev))
1795 continue;
1796
1797 xfer_mask = ata_dev_xfermask(ap, dev);
1798
1799 dev->pio_mode = ata_xfer_mask2mode(xfer_mask & ATA_MASK_PIO);
1800 dev->dma_mode = ata_xfer_mask2mode(xfer_mask & (ATA_MASK_MWDMA |
1801 ATA_MASK_UDMA));
1802 }
1803
1804 /* step 2: always set host PIO timings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 rc = ata_host_set_pio(ap);
1806 if (rc)
1807 goto err_out;
1808
Tejun Heoa6d5a512006-03-06 04:31:57 +09001809 /* step 3: set host DMA timings */
1810 ata_host_set_dma(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 /* step 4: update devices' xfer mode */
Tejun Heoa6d5a512006-03-06 04:31:57 +09001813 for (i = 0; i < ATA_MAX_DEVICES; i++)
1814 ata_dev_set_mode(ap, &ap->device[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1817 return;
1818
1819 if (ap->ops->post_set_mode)
1820 ap->ops->post_set_mode(ap);
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return;
1823
1824err_out:
1825 ata_port_disable(ap);
1826}
1827
1828/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001829 * ata_tf_to_host - issue ATA taskfile to host controller
1830 * @ap: port to which command is being issued
1831 * @tf: ATA taskfile register set
1832 *
1833 * Issues ATA taskfile register set to ATA host controller,
1834 * with proper synchronization with interrupt handler and
1835 * other threads.
1836 *
1837 * LOCKING:
1838 * spin_lock_irqsave(host_set lock)
1839 */
1840
1841static inline void ata_tf_to_host(struct ata_port *ap,
1842 const struct ata_taskfile *tf)
1843{
1844 ap->ops->tf_load(ap, tf);
1845 ap->ops->exec_command(ap, tf);
1846}
1847
1848/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 * ata_busy_sleep - sleep until BSY clears, or timeout
1850 * @ap: port containing status register to be polled
1851 * @tmout_pat: impatience timeout
1852 * @tmout: overall timeout
1853 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001854 * Sleep until ATA Status register bit BSY clears,
1855 * or a timeout occurs.
1856 *
1857 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 */
1859
Tejun Heo6f8b9952006-01-24 17:05:21 +09001860unsigned int ata_busy_sleep (struct ata_port *ap,
1861 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 unsigned long timer_start, timeout;
1864 u8 status;
1865
1866 status = ata_busy_wait(ap, ATA_BUSY, 300);
1867 timer_start = jiffies;
1868 timeout = timer_start + tmout_pat;
1869 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1870 msleep(50);
1871 status = ata_busy_wait(ap, ATA_BUSY, 3);
1872 }
1873
1874 if (status & ATA_BUSY)
1875 printk(KERN_WARNING "ata%u is slow to respond, "
1876 "please be patient\n", ap->id);
1877
1878 timeout = timer_start + tmout;
1879 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1880 msleep(50);
1881 status = ata_chk_status(ap);
1882 }
1883
1884 if (status & ATA_BUSY) {
1885 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1886 ap->id, tmout / HZ);
1887 return 1;
1888 }
1889
1890 return 0;
1891}
1892
1893static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1894{
1895 struct ata_ioports *ioaddr = &ap->ioaddr;
1896 unsigned int dev0 = devmask & (1 << 0);
1897 unsigned int dev1 = devmask & (1 << 1);
1898 unsigned long timeout;
1899
1900 /* if device 0 was found in ata_devchk, wait for its
1901 * BSY bit to clear
1902 */
1903 if (dev0)
1904 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1905
1906 /* if device 1 was found in ata_devchk, wait for
1907 * register access, then wait for BSY to clear
1908 */
1909 timeout = jiffies + ATA_TMOUT_BOOT;
1910 while (dev1) {
1911 u8 nsect, lbal;
1912
1913 ap->ops->dev_select(ap, 1);
1914 if (ap->flags & ATA_FLAG_MMIO) {
1915 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1916 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1917 } else {
1918 nsect = inb(ioaddr->nsect_addr);
1919 lbal = inb(ioaddr->lbal_addr);
1920 }
1921 if ((nsect == 1) && (lbal == 1))
1922 break;
1923 if (time_after(jiffies, timeout)) {
1924 dev1 = 0;
1925 break;
1926 }
1927 msleep(50); /* give drive a breather */
1928 }
1929 if (dev1)
1930 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1931
1932 /* is all this really necessary? */
1933 ap->ops->dev_select(ap, 0);
1934 if (dev1)
1935 ap->ops->dev_select(ap, 1);
1936 if (dev0)
1937 ap->ops->dev_select(ap, 0);
1938}
1939
1940/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001941 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1942 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001944 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1945 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 *
1947 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001948 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001949 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 *
1951 */
1952
1953static unsigned int ata_bus_edd(struct ata_port *ap)
1954{
1955 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001956 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /* set up execute-device-diag (bus reset) taskfile */
1959 /* also, take interrupts to a known state (disabled) */
1960 DPRINTK("execute-device-diag\n");
1961 ata_tf_init(ap, &tf, 0);
1962 tf.ctl |= ATA_NIEN;
1963 tf.command = ATA_CMD_EDD;
1964 tf.protocol = ATA_PROT_NODATA;
1965
1966 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001967 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001969 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 /* spec says at least 2ms. but who knows with those
1972 * crazy ATAPI devices...
1973 */
1974 msleep(150);
1975
1976 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1977}
1978
1979static unsigned int ata_bus_softreset(struct ata_port *ap,
1980 unsigned int devmask)
1981{
1982 struct ata_ioports *ioaddr = &ap->ioaddr;
1983
1984 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1985
1986 /* software reset. causes dev0 to be selected */
1987 if (ap->flags & ATA_FLAG_MMIO) {
1988 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1989 udelay(20); /* FIXME: flush */
1990 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1991 udelay(20); /* FIXME: flush */
1992 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1993 } else {
1994 outb(ap->ctl, ioaddr->ctl_addr);
1995 udelay(10);
1996 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1997 udelay(10);
1998 outb(ap->ctl, ioaddr->ctl_addr);
1999 }
2000
2001 /* spec mandates ">= 2ms" before checking status.
2002 * We wait 150ms, because that was the magic delay used for
2003 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2004 * between when the ATA command register is written, and then
2005 * status is checked. Because waiting for "a while" before
2006 * checking status is fine, post SRST, we perform this magic
2007 * delay here as well.
2008 */
2009 msleep(150);
2010
2011 ata_bus_post_reset(ap, devmask);
2012
2013 return 0;
2014}
2015
2016/**
2017 * ata_bus_reset - reset host port and associated ATA channel
2018 * @ap: port to reset
2019 *
2020 * This is typically the first time we actually start issuing
2021 * commands to the ATA channel. We wait for BSY to clear, then
2022 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2023 * result. Determine what devices, if any, are on the channel
2024 * by looking at the device 0/1 error register. Look at the signature
2025 * stored in each device's taskfile registers, to determine if
2026 * the device is ATA or ATAPI.
2027 *
2028 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002029 * PCI/etc. bus probe sem.
2030 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 *
2032 * SIDE EFFECTS:
2033 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2034 */
2035
2036void ata_bus_reset(struct ata_port *ap)
2037{
2038 struct ata_ioports *ioaddr = &ap->ioaddr;
2039 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2040 u8 err;
2041 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2042
2043 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2044
2045 /* determine if device 0/1 are present */
2046 if (ap->flags & ATA_FLAG_SATA_RESET)
2047 dev0 = 1;
2048 else {
2049 dev0 = ata_devchk(ap, 0);
2050 if (slave_possible)
2051 dev1 = ata_devchk(ap, 1);
2052 }
2053
2054 if (dev0)
2055 devmask |= (1 << 0);
2056 if (dev1)
2057 devmask |= (1 << 1);
2058
2059 /* select device 0 again */
2060 ap->ops->dev_select(ap, 0);
2061
2062 /* issue bus reset */
2063 if (ap->flags & ATA_FLAG_SRST)
2064 rc = ata_bus_softreset(ap, devmask);
2065 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2066 /* set up device control */
2067 if (ap->flags & ATA_FLAG_MMIO)
2068 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2069 else
2070 outb(ap->ctl, ioaddr->ctl_addr);
2071 rc = ata_bus_edd(ap);
2072 }
2073
2074 if (rc)
2075 goto err_out;
2076
2077 /*
2078 * determine by signature whether we have ATA or ATAPI devices
2079 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002080 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002082 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 /* re-enable interrupts */
2085 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2086 ata_irq_on(ap);
2087
2088 /* is double-select really necessary? */
2089 if (ap->device[1].class != ATA_DEV_NONE)
2090 ap->ops->dev_select(ap, 1);
2091 if (ap->device[0].class != ATA_DEV_NONE)
2092 ap->ops->dev_select(ap, 0);
2093
2094 /* if no devices were detected, disable this port */
2095 if ((ap->device[0].class == ATA_DEV_NONE) &&
2096 (ap->device[1].class == ATA_DEV_NONE))
2097 goto err_out;
2098
2099 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2100 /* set up device control for ATA_FLAG_SATA_RESET */
2101 if (ap->flags & ATA_FLAG_MMIO)
2102 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2103 else
2104 outb(ap->ctl, ioaddr->ctl_addr);
2105 }
2106
2107 DPRINTK("EXIT\n");
2108 return;
2109
2110err_out:
2111 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2112 ap->ops->port_disable(ap);
2113
2114 DPRINTK("EXIT\n");
2115}
2116
Tejun Heo7a7921e2006-02-02 18:20:00 +09002117static int sata_phy_resume(struct ata_port *ap)
2118{
2119 unsigned long timeout = jiffies + (HZ * 5);
2120 u32 sstatus;
2121
2122 scr_write_flush(ap, SCR_CONTROL, 0x300);
2123
2124 /* Wait for phy to become ready, if necessary. */
2125 do {
2126 msleep(200);
2127 sstatus = scr_read(ap, SCR_STATUS);
2128 if ((sstatus & 0xf) != 1)
2129 return 0;
2130 } while (time_before(jiffies, timeout));
2131
2132 return -1;
2133}
2134
Tejun Heoc2bd5802006-01-24 17:05:22 +09002135/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002136 * ata_std_probeinit - initialize probing
2137 * @ap: port to be probed
2138 *
2139 * @ap is about to be probed. Initialize it. This function is
2140 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002141 *
2142 * NOTE!!! Do not use this function as probeinit if a low level
2143 * driver implements only hardreset. Just pass NULL as probeinit
2144 * in that case. Using this function is probably okay but doing
2145 * so makes reset sequence different from the original
2146 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002147 */
2148extern void ata_std_probeinit(struct ata_port *ap)
2149{
Tejun Heo3a397462006-02-10 23:58:48 +09002150 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002151 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002152 if (sata_dev_present(ap))
2153 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2154 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002155}
2156
2157/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002158 * ata_std_softreset - reset host port via ATA SRST
2159 * @ap: port to reset
2160 * @verbose: fail verbosely
2161 * @classes: resulting classes of attached devices
2162 *
2163 * Reset host port using ATA SRST. This function is to be used
2164 * as standard callback for ata_drive_*_reset() functions.
2165 *
2166 * LOCKING:
2167 * Kernel thread context (may sleep)
2168 *
2169 * RETURNS:
2170 * 0 on success, -errno otherwise.
2171 */
2172int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2173{
2174 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2175 unsigned int devmask = 0, err_mask;
2176 u8 err;
2177
2178 DPRINTK("ENTER\n");
2179
Tejun Heo3a397462006-02-10 23:58:48 +09002180 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2181 classes[0] = ATA_DEV_NONE;
2182 goto out;
2183 }
2184
Tejun Heoc2bd5802006-01-24 17:05:22 +09002185 /* determine if device 0/1 are present */
2186 if (ata_devchk(ap, 0))
2187 devmask |= (1 << 0);
2188 if (slave_possible && ata_devchk(ap, 1))
2189 devmask |= (1 << 1);
2190
Tejun Heoc2bd5802006-01-24 17:05:22 +09002191 /* select device 0 again */
2192 ap->ops->dev_select(ap, 0);
2193
2194 /* issue bus reset */
2195 DPRINTK("about to softreset, devmask=%x\n", devmask);
2196 err_mask = ata_bus_softreset(ap, devmask);
2197 if (err_mask) {
2198 if (verbose)
2199 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2200 ap->id, err_mask);
2201 else
2202 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2203 err_mask);
2204 return -EIO;
2205 }
2206
2207 /* determine by signature whether we have ATA or ATAPI devices */
2208 classes[0] = ata_dev_try_classify(ap, 0, &err);
2209 if (slave_possible && err != 0x81)
2210 classes[1] = ata_dev_try_classify(ap, 1, &err);
2211
Tejun Heo3a397462006-02-10 23:58:48 +09002212 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002213 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2214 return 0;
2215}
2216
2217/**
2218 * sata_std_hardreset - reset host port via SATA phy reset
2219 * @ap: port to reset
2220 * @verbose: fail verbosely
2221 * @class: resulting class of attached device
2222 *
2223 * SATA phy-reset host port using DET bits of SControl register.
2224 * This function is to be used as standard callback for
2225 * ata_drive_*_reset().
2226 *
2227 * LOCKING:
2228 * Kernel thread context (may sleep)
2229 *
2230 * RETURNS:
2231 * 0 on success, -errno otherwise.
2232 */
2233int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2234{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002235 DPRINTK("ENTER\n");
2236
2237 /* Issue phy wake/reset */
2238 scr_write_flush(ap, SCR_CONTROL, 0x301);
2239
2240 /*
2241 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2242 * 10.4.2 says at least 1 ms.
2243 */
2244 msleep(1);
2245
Tejun Heo7a7921e2006-02-02 18:20:00 +09002246 /* Bring phy back */
2247 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002248
Tejun Heoc2bd5802006-01-24 17:05:22 +09002249 /* TODO: phy layer with polling, timeouts, etc. */
2250 if (!sata_dev_present(ap)) {
2251 *class = ATA_DEV_NONE;
2252 DPRINTK("EXIT, link offline\n");
2253 return 0;
2254 }
2255
2256 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2257 if (verbose)
2258 printk(KERN_ERR "ata%u: COMRESET failed "
2259 "(device not ready)\n", ap->id);
2260 else
2261 DPRINTK("EXIT, device not ready\n");
2262 return -EIO;
2263 }
2264
Tejun Heo3a397462006-02-10 23:58:48 +09002265 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2266
Tejun Heoc2bd5802006-01-24 17:05:22 +09002267 *class = ata_dev_try_classify(ap, 0, NULL);
2268
2269 DPRINTK("EXIT, class=%u\n", *class);
2270 return 0;
2271}
2272
2273/**
2274 * ata_std_postreset - standard postreset callback
2275 * @ap: the target ata_port
2276 * @classes: classes of attached devices
2277 *
2278 * This function is invoked after a successful reset. Note that
2279 * the device might have been reset more than once using
2280 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002281 *
2282 * This function is to be used as standard callback for
2283 * ata_drive_*_reset().
2284 *
2285 * LOCKING:
2286 * Kernel thread context (may sleep)
2287 */
2288void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2289{
2290 DPRINTK("ENTER\n");
2291
Tejun Heo56497bd2006-02-15 15:01:42 +09002292 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002293 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2294 ap->cbl = ATA_CBL_SATA;
2295
2296 /* print link status */
2297 if (ap->cbl == ATA_CBL_SATA)
2298 sata_print_link_status(ap);
2299
Tejun Heo3a397462006-02-10 23:58:48 +09002300 /* re-enable interrupts */
2301 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2302 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002303
2304 /* is double-select really necessary? */
2305 if (classes[0] != ATA_DEV_NONE)
2306 ap->ops->dev_select(ap, 1);
2307 if (classes[1] != ATA_DEV_NONE)
2308 ap->ops->dev_select(ap, 0);
2309
Tejun Heo3a397462006-02-10 23:58:48 +09002310 /* bail out if no device is present */
2311 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2312 DPRINTK("EXIT, no device\n");
2313 return;
2314 }
2315
2316 /* set up device control */
2317 if (ap->ioaddr.ctl_addr) {
2318 if (ap->flags & ATA_FLAG_MMIO)
2319 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2320 else
2321 outb(ap->ctl, ap->ioaddr.ctl_addr);
2322 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002323
2324 DPRINTK("EXIT\n");
2325}
2326
2327/**
2328 * ata_std_probe_reset - standard probe reset method
2329 * @ap: prot to perform probe-reset
2330 * @classes: resulting classes of attached devices
2331 *
2332 * The stock off-the-shelf ->probe_reset method.
2333 *
2334 * LOCKING:
2335 * Kernel thread context (may sleep)
2336 *
2337 * RETURNS:
2338 * 0 on success, -errno otherwise.
2339 */
2340int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2341{
2342 ata_reset_fn_t hardreset;
2343
2344 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002345 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002346 hardreset = sata_std_hardreset;
2347
Tejun Heo8a19ac82006-02-02 18:20:00 +09002348 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002349 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002350 ata_std_postreset, classes);
2351}
2352
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002353static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2354 ata_postreset_fn_t postreset,
2355 unsigned int *classes)
2356{
2357 int i, rc;
2358
2359 for (i = 0; i < ATA_MAX_DEVICES; i++)
2360 classes[i] = ATA_DEV_UNKNOWN;
2361
2362 rc = reset(ap, 0, classes);
2363 if (rc)
2364 return rc;
2365
2366 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2367 * is complete and convert all ATA_DEV_UNKNOWN to
2368 * ATA_DEV_NONE.
2369 */
2370 for (i = 0; i < ATA_MAX_DEVICES; i++)
2371 if (classes[i] != ATA_DEV_UNKNOWN)
2372 break;
2373
2374 if (i < ATA_MAX_DEVICES)
2375 for (i = 0; i < ATA_MAX_DEVICES; i++)
2376 if (classes[i] == ATA_DEV_UNKNOWN)
2377 classes[i] = ATA_DEV_NONE;
2378
2379 if (postreset)
2380 postreset(ap, classes);
2381
2382 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2383}
2384
2385/**
2386 * ata_drive_probe_reset - Perform probe reset with given methods
2387 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002388 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002389 * @softreset: softreset method (can be NULL)
2390 * @hardreset: hardreset method (can be NULL)
2391 * @postreset: postreset method (can be NULL)
2392 * @classes: resulting classes of attached devices
2393 *
2394 * Reset the specified port and classify attached devices using
2395 * given methods. This function prefers softreset but tries all
2396 * possible reset sequences to reset and classify devices. This
2397 * function is intended to be used for constructing ->probe_reset
2398 * callback by low level drivers.
2399 *
2400 * Reset methods should follow the following rules.
2401 *
2402 * - Return 0 on sucess, -errno on failure.
2403 * - If classification is supported, fill classes[] with
2404 * recognized class codes.
2405 * - If classification is not supported, leave classes[] alone.
2406 * - If verbose is non-zero, print error message on failure;
2407 * otherwise, shut up.
2408 *
2409 * LOCKING:
2410 * Kernel thread context (may sleep)
2411 *
2412 * RETURNS:
2413 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2414 * if classification fails, and any error code from reset
2415 * methods.
2416 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002417int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002418 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2419 ata_postreset_fn_t postreset, unsigned int *classes)
2420{
2421 int rc = -EINVAL;
2422
Tejun Heo7944ea92006-02-02 18:20:00 +09002423 if (probeinit)
2424 probeinit(ap);
2425
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002426 if (softreset) {
2427 rc = do_probe_reset(ap, softreset, postreset, classes);
2428 if (rc == 0)
2429 return 0;
2430 }
2431
2432 if (!hardreset)
2433 return rc;
2434
2435 rc = do_probe_reset(ap, hardreset, postreset, classes);
2436 if (rc == 0 || rc != -ENODEV)
2437 return rc;
2438
2439 if (softreset)
2440 rc = do_probe_reset(ap, softreset, postreset, classes);
2441
2442 return rc;
2443}
2444
Tejun Heo623a3122006-03-05 17:55:58 +09002445/**
2446 * ata_dev_same_device - Determine whether new ID matches configured device
2447 * @ap: port on which the device to compare against resides
2448 * @dev: device to compare against
2449 * @new_class: class of the new device
2450 * @new_id: IDENTIFY page of the new device
2451 *
2452 * Compare @new_class and @new_id against @dev and determine
2453 * whether @dev is the device indicated by @new_class and
2454 * @new_id.
2455 *
2456 * LOCKING:
2457 * None.
2458 *
2459 * RETURNS:
2460 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2461 */
2462static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2463 unsigned int new_class, const u16 *new_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Tejun Heo623a3122006-03-05 17:55:58 +09002465 const u16 *old_id = dev->id;
2466 unsigned char model[2][41], serial[2][21];
2467 u64 new_n_sectors;
2468
2469 if (dev->class != new_class) {
2470 printk(KERN_INFO
2471 "ata%u: dev %u class mismatch %d != %d\n",
2472 ap->id, dev->devno, dev->class, new_class);
2473 return 0;
2474 }
2475
2476 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2477 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2478 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2479 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2480 new_n_sectors = ata_id_n_sectors(new_id);
2481
2482 if (strcmp(model[0], model[1])) {
2483 printk(KERN_INFO
2484 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2485 ap->id, dev->devno, model[0], model[1]);
2486 return 0;
2487 }
2488
2489 if (strcmp(serial[0], serial[1])) {
2490 printk(KERN_INFO
2491 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2492 ap->id, dev->devno, serial[0], serial[1]);
2493 return 0;
2494 }
2495
2496 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2497 printk(KERN_INFO
2498 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2499 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2500 (unsigned long long)new_n_sectors);
2501 return 0;
2502 }
2503
2504 return 1;
2505}
2506
2507/**
2508 * ata_dev_revalidate - Revalidate ATA device
2509 * @ap: port on which the device to revalidate resides
2510 * @dev: device to revalidate
2511 * @post_reset: is this revalidation after reset?
2512 *
2513 * Re-read IDENTIFY page and make sure @dev is still attached to
2514 * the port.
2515 *
2516 * LOCKING:
2517 * Kernel thread context (may sleep)
2518 *
2519 * RETURNS:
2520 * 0 on success, negative errno otherwise
2521 */
2522int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2523 int post_reset)
2524{
2525 unsigned int class;
2526 u16 *id;
2527 int rc;
2528
2529 if (!ata_dev_present(dev))
2530 return -ENODEV;
2531
2532 class = dev->class;
2533 id = NULL;
2534
2535 /* allocate & read ID data */
2536 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2537 if (rc)
2538 goto fail;
2539
2540 /* is the device still there? */
2541 if (!ata_dev_same_device(ap, dev, class, id)) {
2542 rc = -ENODEV;
2543 goto fail;
2544 }
2545
2546 kfree(dev->id);
2547 dev->id = id;
2548
2549 /* configure device according to the new ID */
2550 return ata_dev_configure(ap, dev, 0);
2551
2552 fail:
2553 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2554 ap->id, dev->devno, rc);
2555 kfree(id);
2556 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002559static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 "WDC AC11000H",
2561 "WDC AC22100H",
2562 "WDC AC32500H",
2563 "WDC AC33100H",
2564 "WDC AC31600H",
2565 "WDC AC32100H",
2566 "WDC AC23200L",
2567 "Compaq CRD-8241B",
2568 "CRD-8400B",
2569 "CRD-8480B",
2570 "CRD-8482B",
2571 "CRD-84",
2572 "SanDisk SDP3B",
2573 "SanDisk SDP3B-64",
2574 "SANYO CD-ROM CRD",
2575 "HITACHI CDR-8",
2576 "HITACHI CDR-8335",
2577 "HITACHI CDR-8435",
2578 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002579 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 "CD-532E-A",
2581 "E-IDE CD-ROM CR-840",
2582 "CD-ROM Drive/F5A",
2583 "WPI CDD-820",
2584 "SAMSUNG CD-ROM SC-148C",
2585 "SAMSUNG CD-ROM SC",
2586 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2588 "_NEC DV5800A",
2589};
2590
Jeff Garzik057ace52005-10-22 14:27:05 -04002591static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
Tejun Heo2e026712006-02-12 22:47:04 +09002593 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 int i;
2595
Tejun Heo6a62a042006-02-13 10:02:46 +09002596 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002599 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return 1;
2601
2602 return 0;
2603}
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09002606 * ata_dev_xfermask - Compute supported xfermask of the given device
2607 * @ap: Port on which the device to compute xfermask for resides
2608 * @dev: Device to compute xfermask for
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 *
Tejun Heoa6d5a512006-03-06 04:31:57 +09002610 * Compute supported xfermask of @dev. This function is
2611 * responsible for applying all known limits including host
2612 * controller limits, device blacklist, etc...
Jeff Garzik0cba6322005-05-30 19:49:12 -04002613 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 * LOCKING:
Tejun Heoa6d5a512006-03-06 04:31:57 +09002615 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 *
2617 * RETURNS:
Tejun Heoa6d5a512006-03-06 04:31:57 +09002618 * Computed xfermask.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 */
Tejun Heoa6d5a512006-03-06 04:31:57 +09002620static unsigned int ata_dev_xfermask(struct ata_port *ap,
2621 struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
Tejun Heoa6d5a512006-03-06 04:31:57 +09002623 unsigned long xfer_mask;
2624 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Tejun Heoa6d5a512006-03-06 04:31:57 +09002626 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2627 ap->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Tejun Heoa6d5a512006-03-06 04:31:57 +09002629 /* use port-wide xfermask for now */
2630 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2631 struct ata_device *d = &ap->device[i];
2632 if (!ata_dev_present(d))
2633 continue;
2634 xfer_mask &= ata_id_xfermask(d->id);
2635 if (ata_dma_blacklisted(d))
2636 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
2638
Tejun Heoa6d5a512006-03-06 04:31:57 +09002639 if (ata_dma_blacklisted(dev))
2640 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2641 "disabling DMA\n", ap->id, dev->devno);
2642
2643 return xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
2646/**
2647 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2648 * @ap: Port associated with device @dev
2649 * @dev: Device to which command will be sent
2650 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002651 * Issue SET FEATURES - XFER MODE command to device @dev
2652 * on port @ap.
2653 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002655 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 */
2657
2658static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2659{
Tejun Heoa0123702005-12-13 14:49:31 +09002660 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 /* set up set-features taskfile */
2663 DPRINTK("set features - xfer mode\n");
2664
Tejun Heoa0123702005-12-13 14:49:31 +09002665 ata_tf_init(ap, &tf, dev->devno);
2666 tf.command = ATA_CMD_SET_FEATURES;
2667 tf.feature = SETFEATURES_XFER;
2668 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2669 tf.protocol = ATA_PROT_NODATA;
2670 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Tejun Heoa0123702005-12-13 14:49:31 +09002672 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2673 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2674 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 DPRINTK("EXIT\n");
2679}
2680
2681/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002682 * ata_dev_init_params - Issue INIT DEV PARAMS command
2683 * @ap: Port associated with device @dev
2684 * @dev: Device to which command will be sent
2685 *
2686 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002687 * Kernel thread context (may sleep)
2688 *
2689 * RETURNS:
2690 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04002691 */
2692
Tejun Heo6aff8f12006-02-15 18:24:09 +09002693static unsigned int ata_dev_init_params(struct ata_port *ap,
2694 struct ata_device *dev)
Albert Lee8bf62ec2005-05-12 15:29:42 -04002695{
Tejun Heoa0123702005-12-13 14:49:31 +09002696 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002697 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002698 u16 sectors = dev->id[6];
2699 u16 heads = dev->id[3];
2700
2701 /* Number of sectors per track 1-255. Number of heads 1-16 */
2702 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002703 return 0;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002704
2705 /* set up init dev params taskfile */
2706 DPRINTK("init dev params \n");
2707
Tejun Heoa0123702005-12-13 14:49:31 +09002708 ata_tf_init(ap, &tf, dev->devno);
2709 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2710 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2711 tf.protocol = ATA_PROT_NODATA;
2712 tf.nsect = sectors;
2713 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002714
Tejun Heo6aff8f12006-02-15 18:24:09 +09002715 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002716
Tejun Heo6aff8f12006-02-15 18:24:09 +09002717 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2718 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002719}
2720
2721/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002722 * ata_sg_clean - Unmap DMA memory associated with command
2723 * @qc: Command containing DMA memory to be released
2724 *
2725 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 *
2727 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002728 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 */
2730
2731static void ata_sg_clean(struct ata_queued_cmd *qc)
2732{
2733 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002734 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002736 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Tejun Heoa4631472006-02-11 19:11:13 +09002738 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2739 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002742 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002744 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002746 /* if we padded the buffer out to 32-bit bound, and data
2747 * xfer direction is from-device, we must copy from the
2748 * pad buffer back into the supplied buffer
2749 */
2750 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2751 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2752
2753 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002754 if (qc->n_elem)
2755 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002756 /* restore last sg */
2757 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2758 if (pad_buf) {
2759 struct scatterlist *psg = &qc->pad_sgent;
2760 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2761 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002762 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002763 }
2764 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002765 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002766 dma_unmap_single(ap->host_set->dev,
2767 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2768 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002769 /* restore sg */
2770 sg->length += qc->pad_len;
2771 if (pad_buf)
2772 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2773 pad_buf, qc->pad_len);
2774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002777 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
2780/**
2781 * ata_fill_sg - Fill PCI IDE PRD table
2782 * @qc: Metadata associated with taskfile to be transferred
2783 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002784 * Fill PCI IDE PRD (scatter-gather) table with segments
2785 * associated with the current disk command.
2786 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002788 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 *
2790 */
2791static void ata_fill_sg(struct ata_queued_cmd *qc)
2792{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002794 struct scatterlist *sg;
2795 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Tejun Heoa4631472006-02-11 19:11:13 +09002797 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002798 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002801 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 u32 addr, offset;
2803 u32 sg_len, len;
2804
2805 /* determine if physical DMA addr spans 64K boundary.
2806 * Note h/w doesn't support 64-bit, so we unconditionally
2807 * truncate dma_addr_t to u32.
2808 */
2809 addr = (u32) sg_dma_address(sg);
2810 sg_len = sg_dma_len(sg);
2811
2812 while (sg_len) {
2813 offset = addr & 0xffff;
2814 len = sg_len;
2815 if ((offset + sg_len) > 0x10000)
2816 len = 0x10000 - offset;
2817
2818 ap->prd[idx].addr = cpu_to_le32(addr);
2819 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2820 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2821
2822 idx++;
2823 sg_len -= len;
2824 addr += len;
2825 }
2826 }
2827
2828 if (idx)
2829 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2830}
2831/**
2832 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2833 * @qc: Metadata associated with taskfile to check
2834 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002835 * Allow low-level driver to filter ATA PACKET commands, returning
2836 * a status indicating whether or not it is OK to use DMA for the
2837 * supplied PACKET command.
2838 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002840 * spin_lock_irqsave(host_set lock)
2841 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 * RETURNS: 0 when ATAPI DMA can be used
2843 * nonzero otherwise
2844 */
2845int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2846{
2847 struct ata_port *ap = qc->ap;
2848 int rc = 0; /* Assume ATAPI DMA is OK by default */
2849
2850 if (ap->ops->check_atapi_dma)
2851 rc = ap->ops->check_atapi_dma(qc);
2852
2853 return rc;
2854}
2855/**
2856 * ata_qc_prep - Prepare taskfile for submission
2857 * @qc: Metadata associated with taskfile to be prepared
2858 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002859 * Prepare ATA taskfile for submission.
2860 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 * LOCKING:
2862 * spin_lock_irqsave(host_set lock)
2863 */
2864void ata_qc_prep(struct ata_queued_cmd *qc)
2865{
2866 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2867 return;
2868
2869 ata_fill_sg(qc);
2870}
2871
Jeff Garzik0cba6322005-05-30 19:49:12 -04002872/**
2873 * ata_sg_init_one - Associate command with memory buffer
2874 * @qc: Command to be associated
2875 * @buf: Memory buffer
2876 * @buflen: Length of memory buffer, in bytes.
2877 *
2878 * Initialize the data-related elements of queued_cmd @qc
2879 * to point to a single memory buffer, @buf of byte length @buflen.
2880 *
2881 * LOCKING:
2882 * spin_lock_irqsave(host_set lock)
2883 */
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2886{
2887 struct scatterlist *sg;
2888
2889 qc->flags |= ATA_QCFLAG_SINGLE;
2890
2891 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002892 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002894 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 qc->buf_virt = buf;
2896
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002897 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002898 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
Jeff Garzik0cba6322005-05-30 19:49:12 -04002901/**
2902 * ata_sg_init - Associate command with scatter-gather table.
2903 * @qc: Command to be associated
2904 * @sg: Scatter-gather table.
2905 * @n_elem: Number of elements in s/g table.
2906 *
2907 * Initialize the data-related elements of queued_cmd @qc
2908 * to point to a scatter-gather table @sg, containing @n_elem
2909 * elements.
2910 *
2911 * LOCKING:
2912 * spin_lock_irqsave(host_set lock)
2913 */
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2916 unsigned int n_elem)
2917{
2918 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002919 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002921 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
2924/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002925 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2926 * @qc: Command with memory buffer to be mapped.
2927 *
2928 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 *
2930 * LOCKING:
2931 * spin_lock_irqsave(host_set lock)
2932 *
2933 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002934 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 */
2936
2937static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2938{
2939 struct ata_port *ap = qc->ap;
2940 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002941 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002943 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002945 /* we must lengthen transfers to end on a 32-bit boundary */
2946 qc->pad_len = sg->length & 3;
2947 if (qc->pad_len) {
2948 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2949 struct scatterlist *psg = &qc->pad_sgent;
2950
Tejun Heoa4631472006-02-11 19:11:13 +09002951 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002952
2953 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2954
2955 if (qc->tf.flags & ATA_TFLAG_WRITE)
2956 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2957 qc->pad_len);
2958
2959 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2960 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2961 /* trim sg */
2962 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002963 if (sg->length == 0)
2964 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002965
2966 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2967 sg->length, qc->pad_len);
2968 }
2969
Tejun Heo2e242fa2006-02-20 23:48:38 +09002970 if (trim_sg) {
2971 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05002972 goto skip_map;
2973 }
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002976 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002977 if (dma_mapping_error(dma_address)) {
2978 /* restore sg */
2979 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002984 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Tejun Heo2e242fa2006-02-20 23:48:38 +09002986skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2988 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2989
2990 return 0;
2991}
2992
2993/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002994 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2995 * @qc: Command with scatter-gather table to be mapped.
2996 *
2997 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 *
2999 * LOCKING:
3000 * spin_lock_irqsave(host_set lock)
3001 *
3002 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003003 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 *
3005 */
3006
3007static int ata_sg_setup(struct ata_queued_cmd *qc)
3008{
3009 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003010 struct scatterlist *sg = qc->__sg;
3011 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003012 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003015 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003017 /* we must lengthen transfers to end on a 32-bit boundary */
3018 qc->pad_len = lsg->length & 3;
3019 if (qc->pad_len) {
3020 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3021 struct scatterlist *psg = &qc->pad_sgent;
3022 unsigned int offset;
3023
Tejun Heoa4631472006-02-11 19:11:13 +09003024 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003025
3026 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3027
3028 /*
3029 * psg->page/offset are used to copy to-be-written
3030 * data in this function or read data in ata_sg_clean.
3031 */
3032 offset = lsg->offset + lsg->length - qc->pad_len;
3033 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3034 psg->offset = offset_in_page(offset);
3035
3036 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3037 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3038 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003039 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003040 }
3041
3042 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3043 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3044 /* trim last sg */
3045 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003046 if (lsg->length == 0)
3047 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003048
3049 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3050 qc->n_elem - 1, lsg->length, qc->pad_len);
3051 }
3052
Jeff Garzike1410f22005-11-14 14:06:26 -05003053 pre_n_elem = qc->n_elem;
3054 if (trim_sg && pre_n_elem)
3055 pre_n_elem--;
3056
3057 if (!pre_n_elem) {
3058 n_elem = 0;
3059 goto skip_map;
3060 }
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05003063 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003064 if (n_elem < 1) {
3065 /* restore last sg */
3066 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 DPRINTK("%d sg elements mapped\n", n_elem);
3071
Jeff Garzike1410f22005-11-14 14:06:26 -05003072skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 qc->n_elem = n_elem;
3074
3075 return 0;
3076}
3077
3078/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003079 * ata_poll_qc_complete - turn irq back on and finish qc
3080 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003081 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003082 *
3083 * LOCKING:
3084 * None. (grabs host lock)
3085 */
3086
Albert Leea22e2eb2005-12-05 15:38:02 +08003087void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003088{
3089 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003090 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003091
Jeff Garzikb8f61532005-08-25 22:01:20 -04003092 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003093 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003094 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003095 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003096}
3097
3098/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003099 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003100 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 *
3102 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003103 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 *
3105 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003106 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 */
3108
3109static unsigned long ata_pio_poll(struct ata_port *ap)
3110{
Albert Leec14b8332005-12-05 15:36:08 +08003111 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003113 unsigned int poll_state = HSM_ST_UNKNOWN;
3114 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Albert Leec14b8332005-12-05 15:36:08 +08003116 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003117 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003118
Albert Lee14be71f2005-09-27 17:36:35 +08003119 switch (ap->hsm_task_state) {
3120 case HSM_ST:
3121 case HSM_ST_POLL:
3122 poll_state = HSM_ST_POLL;
3123 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003125 case HSM_ST_LAST:
3126 case HSM_ST_LAST_POLL:
3127 poll_state = HSM_ST_LAST_POLL;
3128 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 break;
3130 default:
3131 BUG();
3132 break;
3133 }
3134
3135 status = ata_chk_status(ap);
3136 if (status & ATA_BUSY) {
3137 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003138 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003139 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return 0;
3141 }
Albert Lee14be71f2005-09-27 17:36:35 +08003142 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return ATA_SHORT_PAUSE;
3144 }
3145
Albert Lee14be71f2005-09-27 17:36:35 +08003146 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return 0;
3148}
3149
3150/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003151 * ata_pio_complete - check if drive is busy or idle
3152 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 *
3154 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003155 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003156 *
3157 * RETURNS:
Albert Leefbcdd802005-11-01 19:30:05 +08003158 * Zero if qc completed.
3159 * Non-zero if has next.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 */
3161
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003162static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
3164 struct ata_queued_cmd *qc;
3165 u8 drv_stat;
3166
3167 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003168 * This is purely heuristic. This is a fast path. Sometimes when
3169 * we enter, BSY will be cleared in a chk-status or two. If not,
3170 * the drive is probably seeking or something. Snooze for a couple
3171 * msecs, then chk-status again. If still busy, fall back to
Albert Lee07f6f7d2005-11-01 19:33:20 +08003172 * HSM_ST_LAST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 */
Albert Leefe79e682005-12-06 11:34:59 +08003174 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3175 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003177 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3178 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003179 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Albert Leefbcdd802005-11-01 19:30:05 +08003181 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
3183 }
3184
Albert Leec14b8332005-12-05 15:36:08 +08003185 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003186 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 drv_stat = ata_wait_idle(ap);
3189 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003190 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003191 ap->hsm_task_state = HSM_ST_ERR;
Albert Leefbcdd802005-11-01 19:30:05 +08003192 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
3194
Albert Lee14be71f2005-09-27 17:36:35 +08003195 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Tejun Heoa4631472006-02-11 19:11:13 +09003197 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003198 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003199
3200 /* another command may start at this point */
3201
Albert Leefbcdd802005-11-01 19:30:05 +08003202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Edward Falk0baab862005-06-02 18:17:13 -04003205
3206/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003207 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003208 * @buf: Buffer to swap
3209 * @buf_words: Number of 16-bit words in buffer.
3210 *
3211 * Swap halves of 16-bit words if needed to convert from
3212 * little-endian byte order to native cpu byte order, or
3213 * vice-versa.
3214 *
3215 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003216 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218void swap_buf_le16(u16 *buf, unsigned int buf_words)
3219{
3220#ifdef __BIG_ENDIAN
3221 unsigned int i;
3222
3223 for (i = 0; i < buf_words; i++)
3224 buf[i] = le16_to_cpu(buf[i]);
3225#endif /* __BIG_ENDIAN */
3226}
3227
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003228/**
3229 * ata_mmio_data_xfer - Transfer data by MMIO
3230 * @ap: port to read/write
3231 * @buf: data buffer
3232 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003233 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003234 *
3235 * Transfer data from/to the device data register by MMIO.
3236 *
3237 * LOCKING:
3238 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003239 */
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3242 unsigned int buflen, int write_data)
3243{
3244 unsigned int i;
3245 unsigned int words = buflen >> 1;
3246 u16 *buf16 = (u16 *) buf;
3247 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3248
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003249 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 if (write_data) {
3251 for (i = 0; i < words; i++)
3252 writew(le16_to_cpu(buf16[i]), mmio);
3253 } else {
3254 for (i = 0; i < words; i++)
3255 buf16[i] = cpu_to_le16(readw(mmio));
3256 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003257
3258 /* Transfer trailing 1 byte, if any. */
3259 if (unlikely(buflen & 0x01)) {
3260 u16 align_buf[1] = { 0 };
3261 unsigned char *trailing_buf = buf + buflen - 1;
3262
3263 if (write_data) {
3264 memcpy(align_buf, trailing_buf, 1);
3265 writew(le16_to_cpu(align_buf[0]), mmio);
3266 } else {
3267 align_buf[0] = cpu_to_le16(readw(mmio));
3268 memcpy(trailing_buf, align_buf, 1);
3269 }
3270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271}
3272
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003273/**
3274 * ata_pio_data_xfer - Transfer data by PIO
3275 * @ap: port to read/write
3276 * @buf: data buffer
3277 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003278 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003279 *
3280 * Transfer data from/to the device data register by PIO.
3281 *
3282 * LOCKING:
3283 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003284 */
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3287 unsigned int buflen, int write_data)
3288{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003289 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003291 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003293 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003295 insw(ap->ioaddr.data_addr, buf, words);
3296
3297 /* Transfer trailing 1 byte, if any. */
3298 if (unlikely(buflen & 0x01)) {
3299 u16 align_buf[1] = { 0 };
3300 unsigned char *trailing_buf = buf + buflen - 1;
3301
3302 if (write_data) {
3303 memcpy(align_buf, trailing_buf, 1);
3304 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3305 } else {
3306 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3307 memcpy(trailing_buf, align_buf, 1);
3308 }
3309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003312/**
3313 * ata_data_xfer - Transfer data from/to the data register.
3314 * @ap: port to read/write
3315 * @buf: data buffer
3316 * @buflen: buffer length
3317 * @do_write: read/write
3318 *
3319 * Transfer data from/to the device data register.
3320 *
3321 * LOCKING:
3322 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003323 */
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3326 unsigned int buflen, int do_write)
3327{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003328 /* Make the crap hardware pay the costs not the good stuff */
3329 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3330 unsigned long flags;
3331 local_irq_save(flags);
3332 if (ap->flags & ATA_FLAG_MMIO)
3333 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3334 else
3335 ata_pio_data_xfer(ap, buf, buflen, do_write);
3336 local_irq_restore(flags);
3337 } else {
3338 if (ap->flags & ATA_FLAG_MMIO)
3339 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3340 else
3341 ata_pio_data_xfer(ap, buf, buflen, do_write);
3342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003345/**
3346 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3347 * @qc: Command on going
3348 *
3349 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3350 *
3351 * LOCKING:
3352 * Inherited from caller.
3353 */
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355static void ata_pio_sector(struct ata_queued_cmd *qc)
3356{
3357 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003358 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 struct ata_port *ap = qc->ap;
3360 struct page *page;
3361 unsigned int offset;
3362 unsigned char *buf;
3363
3364 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003365 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
3367 page = sg[qc->cursg].page;
3368 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3369
3370 /* get the current page and offset */
3371 page = nth_page(page, (offset >> PAGE_SHIFT));
3372 offset %= PAGE_SIZE;
3373
Albert Lee7282aa42005-10-09 09:46:07 -04003374 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3375
Albert Lee91b8b312005-10-09 09:48:44 -04003376 if (PageHighMem(page)) {
3377 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003378
Albert Lee91b8b312005-10-09 09:48:44 -04003379 local_irq_save(flags);
3380 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003381
Albert Lee91b8b312005-10-09 09:48:44 -04003382 /* do the actual data transfer */
3383 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3384
3385 kunmap_atomic(buf, KM_IRQ0);
3386 local_irq_restore(flags);
3387 } else {
3388 buf = page_address(page);
3389 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3390 }
Albert Lee7282aa42005-10-09 09:46:07 -04003391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 qc->cursect++;
3393 qc->cursg_ofs++;
3394
Albert Lee32529e02005-05-26 03:49:42 -04003395 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 qc->cursg++;
3397 qc->cursg_ofs = 0;
3398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003401/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003402 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3403 * @qc: Command on going
3404 *
3405 * Transfer one or many ATA_SECT_SIZE of data from/to the
3406 * ATA device for the DRQ request.
3407 *
3408 * LOCKING:
3409 * Inherited from caller.
3410 */
3411
3412static void ata_pio_sectors(struct ata_queued_cmd *qc)
3413{
3414 if (is_multi_taskfile(&qc->tf)) {
3415 /* READ/WRITE MULTIPLE */
3416 unsigned int nsect;
3417
Jeff Garzik587005d2006-02-11 18:17:32 -05003418 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08003419
3420 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3421 while (nsect--)
3422 ata_pio_sector(qc);
3423 } else
3424 ata_pio_sector(qc);
3425}
3426
3427/**
Albert Leec71c1852005-10-04 06:03:45 -04003428 * atapi_send_cdb - Write CDB bytes to hardware
3429 * @ap: Port to which ATAPI device is attached.
3430 * @qc: Taskfile currently active
3431 *
3432 * When device has indicated its readiness to accept
3433 * a CDB, this function is called. Send the CDB.
3434 *
3435 * LOCKING:
3436 * caller.
3437 */
3438
3439static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3440{
3441 /* send SCSI cdb */
3442 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05003443 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04003444
Jeff Garzikdb024d52006-02-13 00:23:57 -05003445 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04003446 ata_altstatus(ap); /* flush */
3447
3448 switch (qc->tf.protocol) {
3449 case ATA_PROT_ATAPI:
3450 ap->hsm_task_state = HSM_ST;
3451 break;
3452 case ATA_PROT_ATAPI_NODATA:
3453 ap->hsm_task_state = HSM_ST_LAST;
3454 break;
3455 case ATA_PROT_ATAPI_DMA:
3456 ap->hsm_task_state = HSM_ST_LAST;
3457 /* initiate bmdma */
3458 ap->ops->bmdma_start(qc);
3459 break;
3460 }
3461}
3462
3463/**
Albert Leee27486d2005-11-01 19:24:49 +08003464 * ata_pio_first_block - Write first data block to hardware
3465 * @ap: Port to which ATA/ATAPI device is attached.
Albert Leec71c1852005-10-04 06:03:45 -04003466 *
3467 * When device has indicated its readiness to accept
3468 * the data, this function sends out the CDB or
3469 * the first data block by PIO.
3470 * After this,
3471 * - If polling, ata_pio_task() handles the rest.
3472 * - Otherwise, interrupt handler takes over.
3473 *
3474 * LOCKING:
3475 * Kernel thread context (may sleep)
Albert Leefbcdd802005-11-01 19:30:05 +08003476 *
3477 * RETURNS:
3478 * Zero if irq handler takes over
3479 * Non-zero if has next (polling).
Albert Leec71c1852005-10-04 06:03:45 -04003480 */
3481
Albert Leefbcdd802005-11-01 19:30:05 +08003482static int ata_pio_first_block(struct ata_port *ap)
Albert Leec71c1852005-10-04 06:03:45 -04003483{
Albert Leec71c1852005-10-04 06:03:45 -04003484 struct ata_queued_cmd *qc;
3485 u8 status;
3486 unsigned long flags;
Albert Leefbcdd802005-11-01 19:30:05 +08003487 int has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003488
3489 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzik587005d2006-02-11 18:17:32 -05003490 WARN_ON(qc == NULL);
3491 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Leec71c1852005-10-04 06:03:45 -04003492
Albert Leefbcdd802005-11-01 19:30:05 +08003493 /* if polling, we will stay in the work queue after sending the data.
3494 * otherwise, interrupt handler takes over after sending the data.
3495 */
3496 has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3497
Albert Leec71c1852005-10-04 06:03:45 -04003498 /* sleep-wait for BSY to clear */
3499 DPRINTK("busy wait\n");
Albert Leefbcdd802005-11-01 19:30:05 +08003500 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
Albert Lee555a8962006-02-08 16:50:29 +08003501 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leefbcdd802005-11-01 19:30:05 +08003502 ap->hsm_task_state = HSM_ST_TMOUT;
Albert Leec71c1852005-10-04 06:03:45 -04003503 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003504 }
Albert Leec71c1852005-10-04 06:03:45 -04003505
3506 /* make sure DRQ is set */
3507 status = ata_chk_status(ap);
Albert Leefbcdd802005-11-01 19:30:05 +08003508 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3509 /* device status error */
Albert Lee555a8962006-02-08 16:50:29 +08003510 qc->err_mask |= AC_ERR_HSM;
Albert Leefbcdd802005-11-01 19:30:05 +08003511 ap->hsm_task_state = HSM_ST_ERR;
Albert Leec71c1852005-10-04 06:03:45 -04003512 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003513 }
Albert Leec71c1852005-10-04 06:03:45 -04003514
3515 /* Send the CDB (atapi) or the first data block (ata pio out).
3516 * During the state transition, interrupt handler shouldn't
3517 * be invoked before the data transfer is complete and
3518 * hsm_task_state is changed. Hence, the following locking.
3519 */
3520 spin_lock_irqsave(&ap->host_set->lock, flags);
3521
3522 if (qc->tf.protocol == ATA_PROT_PIO) {
3523 /* PIO data out protocol.
3524 * send first data block.
3525 */
3526
Albert Lee07f6f7d2005-11-01 19:33:20 +08003527 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3528 * so, the state is changed here before ata_pio_sectors().
Albert Leec71c1852005-10-04 06:03:45 -04003529 */
3530 ap->hsm_task_state = HSM_ST;
Albert Lee07f6f7d2005-11-01 19:33:20 +08003531 ata_pio_sectors(qc);
Albert Leec71c1852005-10-04 06:03:45 -04003532 ata_altstatus(ap); /* flush */
3533 } else
3534 /* send CDB */
3535 atapi_send_cdb(ap, qc);
3536
Albert Leefbcdd802005-11-01 19:30:05 +08003537 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3538
Albert Leec71c1852005-10-04 06:03:45 -04003539 /* if polling, ata_pio_task() handles the rest.
3540 * otherwise, interrupt handler takes over from here.
3541 */
Albert Leefbcdd802005-11-01 19:30:05 +08003542 return has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003543
3544err_out:
Albert Leefbcdd802005-11-01 19:30:05 +08003545 return 1; /* has next */
Albert Leec71c1852005-10-04 06:03:45 -04003546}
3547
3548/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003549 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3550 * @qc: Command on going
3551 * @bytes: number of bytes
3552 *
3553 * Transfer Transfer data from/to the ATAPI device.
3554 *
3555 * LOCKING:
3556 * Inherited from caller.
3557 *
3558 */
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3561{
3562 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003563 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 struct ata_port *ap = qc->ap;
3565 struct page *page;
3566 unsigned char *buf;
3567 unsigned int offset, count;
3568
Albert Lee563a6e12005-08-12 14:17:50 +08003569 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003570 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003573 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003574 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003575 * The end of qc->sg is reached and the device expects
3576 * more data to transfer. In order not to overrun qc->sg
3577 * and fulfill length specified in the byte count register,
3578 * - for read case, discard trailing data from the device
3579 * - for write case, padding zero data to the device
3580 */
3581 u16 pad_buf[1] = { 0 };
3582 unsigned int words = bytes >> 1;
3583 unsigned int i;
3584
3585 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003586 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003587 ap->id, bytes);
3588
3589 for (i = 0; i < words; i++)
3590 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3591
Albert Lee14be71f2005-09-27 17:36:35 +08003592 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003593 return;
3594 }
3595
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003596 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 page = sg->page;
3599 offset = sg->offset + qc->cursg_ofs;
3600
3601 /* get the current page and offset */
3602 page = nth_page(page, (offset >> PAGE_SHIFT));
3603 offset %= PAGE_SIZE;
3604
Albert Lee6952df02005-06-06 15:56:03 +08003605 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003606 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
3608 /* don't cross page boundaries */
3609 count = min(count, (unsigned int)PAGE_SIZE - offset);
3610
Albert Lee7282aa42005-10-09 09:46:07 -04003611 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3612
Albert Lee91b8b312005-10-09 09:48:44 -04003613 if (PageHighMem(page)) {
3614 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003615
Albert Lee91b8b312005-10-09 09:48:44 -04003616 local_irq_save(flags);
3617 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003618
Albert Lee91b8b312005-10-09 09:48:44 -04003619 /* do the actual data transfer */
3620 ata_data_xfer(ap, buf + offset, count, do_write);
3621
3622 kunmap_atomic(buf, KM_IRQ0);
3623 local_irq_restore(flags);
3624 } else {
3625 buf = page_address(page);
3626 ata_data_xfer(ap, buf + offset, count, do_write);
3627 }
Albert Lee7282aa42005-10-09 09:46:07 -04003628
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 bytes -= count;
3630 qc->curbytes += count;
3631 qc->cursg_ofs += count;
3632
Albert Lee32529e02005-05-26 03:49:42 -04003633 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 qc->cursg++;
3635 qc->cursg_ofs = 0;
3636 }
3637
Albert Lee563a6e12005-08-12 14:17:50 +08003638 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640}
3641
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003642/**
3643 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3644 * @qc: Command on going
3645 *
3646 * Transfer Transfer data from/to the ATAPI device.
3647 *
3648 * LOCKING:
3649 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003650 */
3651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3653{
3654 struct ata_port *ap = qc->ap;
3655 struct ata_device *dev = qc->dev;
3656 unsigned int ireason, bc_lo, bc_hi, bytes;
3657 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3658
3659 ap->ops->tf_read(ap, &qc->tf);
3660 ireason = qc->tf.nsect;
3661 bc_lo = qc->tf.lbam;
3662 bc_hi = qc->tf.lbah;
3663 bytes = (bc_hi << 8) | bc_lo;
3664
3665 /* shall be cleared to zero, indicating xfer of data */
3666 if (ireason & (1 << 0))
3667 goto err_out;
3668
3669 /* make sure transfer direction matches expected */
3670 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3671 if (do_write != i_write)
3672 goto err_out;
3673
Albert Lee312f7da2005-09-27 17:38:03 +08003674 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 __atapi_pio_bytes(qc, bytes);
3677
3678 return;
3679
3680err_out:
3681 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3682 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003683 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003684 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685}
3686
3687/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003688 * ata_pio_block - start PIO on a block
3689 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 *
3691 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003692 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 */
3694
3695static void ata_pio_block(struct ata_port *ap)
3696{
3697 struct ata_queued_cmd *qc;
3698 u8 status;
3699
3700 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003701 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 * Sometimes when we enter, BSY will be cleared in
3703 * a chk-status or two. If not, the drive is probably seeking
3704 * or something. Snooze for a couple msecs, then
3705 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003706 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 */
3708 status = ata_busy_wait(ap, ATA_BUSY, 5);
3709 if (status & ATA_BUSY) {
3710 msleep(2);
3711 status = ata_busy_wait(ap, ATA_BUSY, 10);
3712 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003713 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3715 return;
3716 }
3717 }
3718
3719 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003720 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Albert Leefe79e682005-12-06 11:34:59 +08003722 /* check error */
3723 if (status & (ATA_ERR | ATA_DF)) {
3724 qc->err_mask |= AC_ERR_DEV;
3725 ap->hsm_task_state = HSM_ST_ERR;
3726 return;
3727 }
3728
3729 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003731 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003733 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 return;
3735 }
3736
3737 atapi_pio_bytes(qc);
3738 } else {
3739 /* handle BSY=0, DRQ=0 as error */
3740 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003741 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003742 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return;
3744 }
3745
Albert Lee07f6f7d2005-11-01 19:33:20 +08003746 ata_pio_sectors(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 }
Albert Lee467b16d2005-11-01 19:19:01 +08003748
3749 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750}
3751
3752static void ata_pio_error(struct ata_port *ap)
3753{
3754 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003755
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003757 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Albert Lee000080c2005-12-26 16:48:00 +08003759 if (qc->tf.command != ATA_CMD_PACKET)
3760 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3761
Albert Lee1c848982005-12-05 15:40:15 +08003762 /* make sure qc->err_mask is available to
3763 * know what's wrong and recover
3764 */
Tejun Heoa4631472006-02-11 19:11:13 +09003765 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003766
Albert Lee14be71f2005-09-27 17:36:35 +08003767 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Albert Leea22e2eb2005-12-05 15:38:02 +08003769 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770}
3771
3772static void ata_pio_task(void *_data)
3773{
3774 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003775 unsigned long timeout;
Albert Leefbcdd802005-11-01 19:30:05 +08003776 int has_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003777
3778fsm_start:
3779 timeout = 0;
Albert Leefbcdd802005-11-01 19:30:05 +08003780 has_next = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Albert Lee14be71f2005-09-27 17:36:35 +08003782 switch (ap->hsm_task_state) {
Albert Leee27486d2005-11-01 19:24:49 +08003783 case HSM_ST_FIRST:
Albert Leefbcdd802005-11-01 19:30:05 +08003784 has_next = ata_pio_first_block(ap);
3785 break;
Albert Leee27486d2005-11-01 19:24:49 +08003786
Albert Lee14be71f2005-09-27 17:36:35 +08003787 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 ata_pio_block(ap);
3789 break;
3790
Albert Lee14be71f2005-09-27 17:36:35 +08003791 case HSM_ST_LAST:
Albert Leefbcdd802005-11-01 19:30:05 +08003792 has_next = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 break;
3794
Albert Lee14be71f2005-09-27 17:36:35 +08003795 case HSM_ST_POLL:
3796 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 timeout = ata_pio_poll(ap);
3798 break;
3799
Albert Lee14be71f2005-09-27 17:36:35 +08003800 case HSM_ST_TMOUT:
3801 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 ata_pio_error(ap);
3803 return;
Albert Lee467b16d2005-11-01 19:19:01 +08003804
3805 default:
3806 BUG();
3807 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
3809
3810 if (timeout)
Tejun Heo8061f5f2006-03-05 15:29:09 +09003811 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
Jeff Garzik46e202e2006-03-11 19:25:47 -05003812 else if (has_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003813 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814}
3815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816/**
Tejun Heo8061f5f2006-03-05 15:29:09 +09003817 * atapi_packet_task - Write CDB bytes to hardware
3818 * @_data: Port to which ATAPI device is attached.
3819 *
3820 * When device has indicated its readiness to accept
3821 * a CDB, this function is called. Send the CDB.
3822 * If DMA is to be performed, exit immediately.
3823 * Otherwise, we are in polling mode, so poll
3824 * status under operation succeeds or fails.
3825 *
3826 * LOCKING:
3827 * Kernel thread context (may sleep)
3828 */
3829
3830static void atapi_packet_task(void *_data)
3831{
3832 struct ata_port *ap = _data;
3833 struct ata_queued_cmd *qc;
3834 u8 status;
3835
3836 qc = ata_qc_from_tag(ap, ap->active_tag);
3837 WARN_ON(qc == NULL);
3838 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3839
3840 /* sleep-wait for BSY to clear */
3841 DPRINTK("busy wait\n");
3842 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3843 qc->err_mask |= AC_ERR_TIMEOUT;
3844 goto err_out;
3845 }
3846
3847 /* make sure DRQ is set */
3848 status = ata_chk_status(ap);
3849 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3850 qc->err_mask |= AC_ERR_HSM;
3851 goto err_out;
3852 }
3853
3854 /* send SCSI cdb */
3855 DPRINTK("send cdb\n");
3856 WARN_ON(qc->dev->cdb_len < 12);
3857
3858 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3859 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3860 unsigned long flags;
3861
3862 /* Once we're done issuing command and kicking bmdma,
3863 * irq handler takes over. To not lose irq, we need
3864 * to clear NOINTR flag before sending cdb, but
3865 * interrupt handler shouldn't be invoked before we're
3866 * finished. Hence, the following locking.
3867 */
3868 spin_lock_irqsave(&ap->host_set->lock, flags);
Jeff Garzik46e202e2006-03-11 19:25:47 -05003869#warning FIXME
3870 /* ap->flags &= ~ATA_FLAG_NOINTR; */
Tejun Heo8061f5f2006-03-05 15:29:09 +09003871 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3872 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3873 ap->ops->bmdma_start(qc); /* initiate bmdma */
3874 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3875 } else {
3876 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3877
3878 /* PIO commands are handled by polling */
3879 ap->hsm_task_state = HSM_ST;
3880 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3881 }
3882
3883 return;
3884
3885err_out:
3886 ata_poll_qc_complete(qc);
3887}
3888
3889/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 * ata_qc_timeout - Handle timeout of queued command
3891 * @qc: Command that timed out
3892 *
3893 * Some part of the kernel (currently, only the SCSI layer)
3894 * has noticed that the active command on port @ap has not
3895 * completed after a specified length of time. Handle this
3896 * condition by disabling DMA (if necessary) and completing
3897 * transactions, with error if necessary.
3898 *
3899 * This also handles the case of the "lost interrupt", where
3900 * for some reason (possibly hardware bug, possibly driver bug)
3901 * an interrupt was not delivered to the driver, even though the
3902 * transaction completed successfully.
3903 *
3904 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003905 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 */
3907
3908static void ata_qc_timeout(struct ata_queued_cmd *qc)
3909{
3910 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003911 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003913 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
3915 DPRINTK("ENTER\n");
3916
Tejun Heoc18d06f2006-02-02 00:56:10 +09003917 ap->hsm_task_state = HSM_ST_IDLE;
3918
Jeff Garzikb8f61532005-08-25 22:01:20 -04003919 spin_lock_irqsave(&host_set->lock, flags);
3920
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 switch (qc->tf.protocol) {
3922
3923 case ATA_PROT_DMA:
3924 case ATA_PROT_ATAPI_DMA:
3925 host_stat = ap->ops->bmdma_status(ap);
3926
3927 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003928 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 /* fall through */
3931
3932 default:
3933 ata_altstatus(ap);
3934 drv_stat = ata_chk_status(ap);
3935
3936 /* ack bmdma irq events */
3937 ap->ops->irq_clear(ap);
3938
3939 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3940 ap->id, qc->tf.command, drv_stat, host_stat);
3941
Albert Lee312f7da2005-09-27 17:38:03 +08003942 ap->hsm_task_state = HSM_ST_IDLE;
3943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 /* complete taskfile transaction */
Albert Lee555a8962006-02-08 16:50:29 +08003945 qc->err_mask |= AC_ERR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 break;
3947 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003948
3949 spin_unlock_irqrestore(&host_set->lock, flags);
3950
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003951 ata_eh_qc_complete(qc);
3952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 DPRINTK("EXIT\n");
3954}
3955
3956/**
3957 * ata_eng_timeout - Handle timeout of queued command
3958 * @ap: Port on which timed-out command is active
3959 *
3960 * Some part of the kernel (currently, only the SCSI layer)
3961 * has noticed that the active command on port @ap has not
3962 * completed after a specified length of time. Handle this
3963 * condition by disabling DMA (if necessary) and completing
3964 * transactions, with error if necessary.
3965 *
3966 * This also handles the case of the "lost interrupt", where
3967 * for some reason (possibly hardware bug, possibly driver bug)
3968 * an interrupt was not delivered to the driver, even though the
3969 * transaction completed successfully.
3970 *
3971 * LOCKING:
3972 * Inherited from SCSI layer (none, can sleep)
3973 */
3974
3975void ata_eng_timeout(struct ata_port *ap)
3976{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 DPRINTK("ENTER\n");
3978
Tejun Heof6379022006-02-10 15:10:48 +09003979 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 DPRINTK("EXIT\n");
3982}
3983
3984/**
3985 * ata_qc_new - Request an available ATA command, for queueing
3986 * @ap: Port associated with device @dev
3987 * @dev: Device from whom we request an available command structure
3988 *
3989 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003990 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 */
3992
3993static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3994{
3995 struct ata_queued_cmd *qc = NULL;
3996 unsigned int i;
3997
3998 for (i = 0; i < ATA_MAX_QUEUE; i++)
3999 if (!test_and_set_bit(i, &ap->qactive)) {
4000 qc = ata_qc_from_tag(ap, i);
4001 break;
4002 }
4003
4004 if (qc)
4005 qc->tag = i;
4006
4007 return qc;
4008}
4009
4010/**
4011 * ata_qc_new_init - Request an available ATA command, and initialize it
4012 * @ap: Port associated with device @dev
4013 * @dev: Device from whom we request an available command structure
4014 *
4015 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004016 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 */
4018
4019struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
4020 struct ata_device *dev)
4021{
4022 struct ata_queued_cmd *qc;
4023
4024 qc = ata_qc_new(ap);
4025 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 qc->scsicmd = NULL;
4027 qc->ap = ap;
4028 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004030 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 }
4032
4033 return qc;
4034}
4035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036/**
4037 * ata_qc_free - free unused ata_queued_cmd
4038 * @qc: Command to complete
4039 *
4040 * Designed to free unused ata_queued_cmd object
4041 * in case something prevents using it.
4042 *
4043 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004044 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 */
4046void ata_qc_free(struct ata_queued_cmd *qc)
4047{
Tejun Heo4ba946e2006-01-23 13:09:36 +09004048 struct ata_port *ap = qc->ap;
4049 unsigned int tag;
4050
Tejun Heoa4631472006-02-11 19:11:13 +09004051 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Tejun Heo4ba946e2006-01-23 13:09:36 +09004053 qc->flags = 0;
4054 tag = qc->tag;
4055 if (likely(ata_tag_valid(tag))) {
4056 if (tag == ap->active_tag)
4057 ap->active_tag = ATA_TAG_POISON;
4058 qc->tag = ATA_TAG_POISON;
4059 clear_bit(tag, &ap->qactive);
4060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
4062
Tejun Heo76014422006-02-11 15:13:49 +09004063void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064{
Tejun Heoa4631472006-02-11 19:11:13 +09004065 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4066 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
4068 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4069 ata_sg_clean(qc);
4070
Albert Lee3f3791d2005-08-16 14:25:38 +08004071 /* atapi: mark qc as inactive to prevent the interrupt handler
4072 * from completing the command twice later, before the error handler
4073 * is called. (when rc != 0 and atapi request sense is needed)
4074 */
4075 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004078 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079}
4080
4081static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4082{
4083 struct ata_port *ap = qc->ap;
4084
4085 switch (qc->tf.protocol) {
4086 case ATA_PROT_DMA:
4087 case ATA_PROT_ATAPI_DMA:
4088 return 1;
4089
4090 case ATA_PROT_ATAPI:
4091 case ATA_PROT_PIO:
4092 case ATA_PROT_PIO_MULT:
4093 if (ap->flags & ATA_FLAG_PIO_DMA)
4094 return 1;
4095
4096 /* fall through */
4097
4098 default:
4099 return 0;
4100 }
4101
4102 /* never reached */
4103}
4104
4105/**
4106 * ata_qc_issue - issue taskfile to device
4107 * @qc: command to issue to device
4108 *
4109 * Prepare an ATA command to submission to device.
4110 * This includes mapping the data into a DMA-able
4111 * area, filling in the S/G table, and finally
4112 * writing the taskfile to hardware, starting the command.
4113 *
4114 * LOCKING:
4115 * spin_lock_irqsave(host_set lock)
4116 *
4117 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004118 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 */
4120
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004121unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{
4123 struct ata_port *ap = qc->ap;
4124
4125 if (ata_should_dma_map(qc)) {
4126 if (qc->flags & ATA_QCFLAG_SG) {
4127 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004128 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4130 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004131 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 }
4133 } else {
4134 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4135 }
4136
4137 ap->ops->qc_prep(qc);
4138
4139 qc->ap->active_tag = qc->tag;
4140 qc->flags |= ATA_QCFLAG_ACTIVE;
4141
4142 return ap->ops->qc_issue(qc);
4143
Tejun Heo8e436af2006-01-23 13:09:36 +09004144sg_err:
4145 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004146 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
4148
Edward Falk0baab862005-06-02 18:17:13 -04004149
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150/**
4151 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4152 * @qc: command to issue to device
4153 *
4154 * Using various libata functions and hooks, this function
4155 * starts an ATA command. ATA commands are grouped into
4156 * classes called "protocols", and issuing each type of protocol
4157 * is slightly different.
4158 *
Edward Falk0baab862005-06-02 18:17:13 -04004159 * May be used as the qc_issue() entry in ata_port_operations.
4160 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 * LOCKING:
4162 * spin_lock_irqsave(host_set lock)
4163 *
4164 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004165 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 */
4167
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004168unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
4170 struct ata_port *ap = qc->ap;
4171
Albert Leee50362e2005-09-27 17:39:50 +08004172 /* Use polling pio if the LLD doesn't handle
4173 * interrupt driven pio and atapi CDB interrupt.
4174 */
4175 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4176 switch (qc->tf.protocol) {
4177 case ATA_PROT_PIO:
4178 case ATA_PROT_ATAPI:
4179 case ATA_PROT_ATAPI_NODATA:
4180 qc->tf.flags |= ATA_TFLAG_POLLING;
4181 break;
4182 case ATA_PROT_ATAPI_DMA:
4183 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
4184 BUG();
4185 break;
4186 default:
4187 break;
4188 }
4189 }
4190
Albert Lee312f7da2005-09-27 17:38:03 +08004191 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 ata_dev_select(ap, qc->dev->devno, 1, 0);
4193
Albert Lee312f7da2005-09-27 17:38:03 +08004194 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 switch (qc->tf.protocol) {
4196 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004197 if (qc->tf.flags & ATA_TFLAG_POLLING)
4198 ata_qc_set_polling(qc);
4199
Jeff Garzike5338252005-10-30 21:37:17 -05004200 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004201 ap->hsm_task_state = HSM_ST_LAST;
4202
4203 if (qc->tf.flags & ATA_TFLAG_POLLING)
Jeff Garzik46e202e2006-03-11 19:25:47 -05004204 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004205
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 break;
4207
4208 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004209 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4212 ap->ops->bmdma_setup(qc); /* set up bmdma */
4213 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004214 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 break;
4216
Albert Lee312f7da2005-09-27 17:38:03 +08004217 case ATA_PROT_PIO:
4218 if (qc->tf.flags & ATA_TFLAG_POLLING)
4219 ata_qc_set_polling(qc);
4220
Jeff Garzike5338252005-10-30 21:37:17 -05004221 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004222
Albert Lee54f00382005-09-30 19:14:19 +08004223 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4224 /* PIO data out protocol */
4225 ap->hsm_task_state = HSM_ST_FIRST;
Jeff Garzikce1e7a22006-03-11 19:21:17 -05004226 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Albert Lee54f00382005-09-30 19:14:19 +08004227
4228 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08004229 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08004230 */
Albert Lee312f7da2005-09-27 17:38:03 +08004231 } else {
Albert Lee54f00382005-09-30 19:14:19 +08004232 /* PIO data in protocol */
4233 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08004234
Albert Lee54f00382005-09-30 19:14:19 +08004235 if (qc->tf.flags & ATA_TFLAG_POLLING)
Jeff Garzikce1e7a22006-03-11 19:21:17 -05004236 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004237
Albert Lee54f00382005-09-30 19:14:19 +08004238 /* if polling, ata_pio_task() handles the rest.
4239 * otherwise, interrupt handler takes over from here.
4240 */
Albert Lee312f7da2005-09-27 17:38:03 +08004241 }
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 break;
4244
4245 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004247 if (qc->tf.flags & ATA_TFLAG_POLLING)
4248 ata_qc_set_polling(qc);
4249
Jeff Garzike5338252005-10-30 21:37:17 -05004250 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004251
Albert Lee312f7da2005-09-27 17:38:03 +08004252 ap->hsm_task_state = HSM_ST_FIRST;
4253
4254 /* send cdb by polling if no cdb interrupt */
4255 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4256 (qc->tf.flags & ATA_TFLAG_POLLING))
Jeff Garzikce1e7a22006-03-11 19:21:17 -05004257 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 break;
4259
4260 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004261 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4264 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004265 ap->hsm_task_state = HSM_ST_FIRST;
4266
4267 /* send cdb by polling if no cdb interrupt */
4268 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Jeff Garzikce1e7a22006-03-11 19:21:17 -05004269 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 break;
4271
4272 default:
4273 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004274 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 }
4276
4277 return 0;
4278}
4279
4280/**
Edward Falk0baab862005-06-02 18:17:13 -04004281 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 * @qc: Info associated with this ATA transaction.
4283 *
4284 * LOCKING:
4285 * spin_lock_irqsave(host_set lock)
4286 */
4287
4288static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4289{
4290 struct ata_port *ap = qc->ap;
4291 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4292 u8 dmactl;
4293 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4294
4295 /* load PRD table addr. */
4296 mb(); /* make sure PRD table writes are visible to controller */
4297 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4298
4299 /* specify data direction, triple-check start bit is clear */
4300 dmactl = readb(mmio + ATA_DMA_CMD);
4301 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4302 if (!rw)
4303 dmactl |= ATA_DMA_WR;
4304 writeb(dmactl, mmio + ATA_DMA_CMD);
4305
4306 /* issue r/w command */
4307 ap->ops->exec_command(ap, &qc->tf);
4308}
4309
4310/**
Alan Coxb73fc892005-08-26 16:03:19 +01004311 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 * @qc: Info associated with this ATA transaction.
4313 *
4314 * LOCKING:
4315 * spin_lock_irqsave(host_set lock)
4316 */
4317
4318static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4319{
4320 struct ata_port *ap = qc->ap;
4321 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4322 u8 dmactl;
4323
4324 /* start host DMA transaction */
4325 dmactl = readb(mmio + ATA_DMA_CMD);
4326 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4327
4328 /* Strictly, one may wish to issue a readb() here, to
4329 * flush the mmio write. However, control also passes
4330 * to the hardware at this point, and it will interrupt
4331 * us when we are to resume control. So, in effect,
4332 * we don't care when the mmio write flushes.
4333 * Further, a read of the DMA status register _immediately_
4334 * following the write may not be what certain flaky hardware
4335 * is expected, so I think it is best to not add a readb()
4336 * without first all the MMIO ATA cards/mobos.
4337 * Or maybe I'm just being paranoid.
4338 */
4339}
4340
4341/**
4342 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4343 * @qc: Info associated with this ATA transaction.
4344 *
4345 * LOCKING:
4346 * spin_lock_irqsave(host_set lock)
4347 */
4348
4349static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4350{
4351 struct ata_port *ap = qc->ap;
4352 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4353 u8 dmactl;
4354
4355 /* load PRD table addr. */
4356 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4357
4358 /* specify data direction, triple-check start bit is clear */
4359 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4360 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4361 if (!rw)
4362 dmactl |= ATA_DMA_WR;
4363 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4364
4365 /* issue r/w command */
4366 ap->ops->exec_command(ap, &qc->tf);
4367}
4368
4369/**
4370 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4371 * @qc: Info associated with this ATA transaction.
4372 *
4373 * LOCKING:
4374 * spin_lock_irqsave(host_set lock)
4375 */
4376
4377static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4378{
4379 struct ata_port *ap = qc->ap;
4380 u8 dmactl;
4381
4382 /* start host DMA transaction */
4383 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4384 outb(dmactl | ATA_DMA_START,
4385 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4386}
4387
Edward Falk0baab862005-06-02 18:17:13 -04004388
4389/**
4390 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4391 * @qc: Info associated with this ATA transaction.
4392 *
4393 * Writes the ATA_DMA_START flag to the DMA command register.
4394 *
4395 * May be used as the bmdma_start() entry in ata_port_operations.
4396 *
4397 * LOCKING:
4398 * spin_lock_irqsave(host_set lock)
4399 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400void ata_bmdma_start(struct ata_queued_cmd *qc)
4401{
4402 if (qc->ap->flags & ATA_FLAG_MMIO)
4403 ata_bmdma_start_mmio(qc);
4404 else
4405 ata_bmdma_start_pio(qc);
4406}
4407
Edward Falk0baab862005-06-02 18:17:13 -04004408
4409/**
4410 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4411 * @qc: Info associated with this ATA transaction.
4412 *
4413 * Writes address of PRD table to device's PRD Table Address
4414 * register, sets the DMA control register, and calls
4415 * ops->exec_command() to start the transfer.
4416 *
4417 * May be used as the bmdma_setup() entry in ata_port_operations.
4418 *
4419 * LOCKING:
4420 * spin_lock_irqsave(host_set lock)
4421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422void ata_bmdma_setup(struct ata_queued_cmd *qc)
4423{
4424 if (qc->ap->flags & ATA_FLAG_MMIO)
4425 ata_bmdma_setup_mmio(qc);
4426 else
4427 ata_bmdma_setup_pio(qc);
4428}
4429
Edward Falk0baab862005-06-02 18:17:13 -04004430
4431/**
4432 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004433 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004434 *
4435 * Clear interrupt and error flags in DMA status register.
4436 *
4437 * May be used as the irq_clear() entry in ata_port_operations.
4438 *
4439 * LOCKING:
4440 * spin_lock_irqsave(host_set lock)
4441 */
4442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443void ata_bmdma_irq_clear(struct ata_port *ap)
4444{
4445 if (ap->flags & ATA_FLAG_MMIO) {
4446 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4447 writeb(readb(mmio), mmio);
4448 } else {
4449 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4450 outb(inb(addr), addr);
4451 }
4452
4453}
4454
Edward Falk0baab862005-06-02 18:17:13 -04004455
4456/**
4457 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004458 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004459 *
4460 * Read and return BMDMA status register.
4461 *
4462 * May be used as the bmdma_status() entry in ata_port_operations.
4463 *
4464 * LOCKING:
4465 * spin_lock_irqsave(host_set lock)
4466 */
4467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468u8 ata_bmdma_status(struct ata_port *ap)
4469{
4470 u8 host_stat;
4471 if (ap->flags & ATA_FLAG_MMIO) {
4472 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4473 host_stat = readb(mmio + ATA_DMA_STATUS);
4474 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004475 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 return host_stat;
4477}
4478
Edward Falk0baab862005-06-02 18:17:13 -04004479
4480/**
4481 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004482 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004483 *
4484 * Clears the ATA_DMA_START flag in the dma control register
4485 *
4486 * May be used as the bmdma_stop() entry in ata_port_operations.
4487 *
4488 * LOCKING:
4489 * spin_lock_irqsave(host_set lock)
4490 */
4491
Alan Coxb73fc892005-08-26 16:03:19 +01004492void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
Alan Coxb73fc892005-08-26 16:03:19 +01004494 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 if (ap->flags & ATA_FLAG_MMIO) {
4496 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4497
4498 /* clear start/stop bit */
4499 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4500 mmio + ATA_DMA_CMD);
4501 } else {
4502 /* clear start/stop bit */
4503 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4504 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4505 }
4506
4507 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4508 ata_altstatus(ap); /* dummy read */
4509}
4510
4511/**
4512 * ata_host_intr - Handle host interrupt for given (port, task)
4513 * @ap: Port on which interrupt arrived (possibly...)
4514 * @qc: Taskfile currently active in engine
4515 *
4516 * Handle host interrupt for given queued command. Currently,
4517 * only DMA interrupts are handled. All other commands are
4518 * handled via polling with interrupts disabled (nIEN bit).
4519 *
4520 * LOCKING:
4521 * spin_lock_irqsave(host_set lock)
4522 *
4523 * RETURNS:
4524 * One if interrupt was handled, zero if not (shared irq).
4525 */
4526
4527inline unsigned int ata_host_intr (struct ata_port *ap,
4528 struct ata_queued_cmd *qc)
4529{
Albert Lee312f7da2005-09-27 17:38:03 +08004530 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
Albert Lee312f7da2005-09-27 17:38:03 +08004532 VPRINTK("ata%u: protocol %d task_state %d\n",
4533 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Albert Lee312f7da2005-09-27 17:38:03 +08004535 /* Check whether we are expecting interrupt in this state */
4536 switch (ap->hsm_task_state) {
4537 case HSM_ST_FIRST:
4538 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4539 * The flag was turned on only for atapi devices.
4540 * No need to check is_atapi_taskfile(&qc->tf) again.
4541 */
4542 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08004544 break;
4545 case HSM_ST_LAST:
4546 if (qc->tf.protocol == ATA_PROT_DMA ||
4547 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4548 /* check status of DMA engine */
4549 host_stat = ap->ops->bmdma_status(ap);
4550 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Albert Lee312f7da2005-09-27 17:38:03 +08004552 /* if it's not our irq... */
4553 if (!(host_stat & ATA_DMA_INTR))
4554 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Albert Lee312f7da2005-09-27 17:38:03 +08004556 /* before we do anything else, clear DMA-Start bit */
4557 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08004558
4559 if (unlikely(host_stat & ATA_DMA_ERR)) {
4560 /* error when transfering data to/from memory */
4561 qc->err_mask |= AC_ERR_HOST_BUS;
4562 ap->hsm_task_state = HSM_ST_ERR;
4563 }
Albert Lee312f7da2005-09-27 17:38:03 +08004564 }
4565 break;
4566 case HSM_ST:
4567 break;
4568 default:
4569 goto idle_irq;
4570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
Albert Lee312f7da2005-09-27 17:38:03 +08004572 /* check altstatus */
4573 status = ata_altstatus(ap);
4574 if (status & ATA_BUSY)
4575 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Albert Lee312f7da2005-09-27 17:38:03 +08004577 /* check main status, clearing INTRQ */
4578 status = ata_chk_status(ap);
4579 if (unlikely(status & ATA_BUSY))
4580 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Albert Lee312f7da2005-09-27 17:38:03 +08004582 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4583 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4584
4585 /* ack bmdma irq events */
4586 ap->ops->irq_clear(ap);
4587
4588 /* check error */
Albert Leea4f16612005-12-26 16:40:53 +08004589 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4590 qc->err_mask |= AC_ERR_DEV;
Albert Lee312f7da2005-09-27 17:38:03 +08004591 ap->hsm_task_state = HSM_ST_ERR;
Albert Leea4f16612005-12-26 16:40:53 +08004592 }
Albert Lee312f7da2005-09-27 17:38:03 +08004593
4594fsm_start:
4595 switch (ap->hsm_task_state) {
4596 case HSM_ST_FIRST:
4597 /* Some pre-ATAPI-4 devices assert INTRQ
4598 * at this state when ready to receive CDB.
4599 */
4600
4601 /* check device status */
4602 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4603 /* Wrong status. Let EH handle this */
Albert Lee555a8962006-02-08 16:50:29 +08004604 qc->err_mask |= AC_ERR_HSM;
Albert Lee312f7da2005-09-27 17:38:03 +08004605 ap->hsm_task_state = HSM_ST_ERR;
4606 goto fsm_start;
4607 }
4608
4609 atapi_send_cdb(ap, qc);
4610
4611 break;
4612
4613 case HSM_ST:
4614 /* complete command or read/write the data register */
4615 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4616 /* ATAPI PIO protocol */
4617 if ((status & ATA_DRQ) == 0) {
4618 /* no more data to transfer */
4619 ap->hsm_task_state = HSM_ST_LAST;
4620 goto fsm_start;
4621 }
4622
4623 atapi_pio_bytes(qc);
4624
4625 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4626 /* bad ireason reported by device */
4627 goto fsm_start;
4628
4629 } else {
4630 /* ATA PIO protocol */
4631 if (unlikely((status & ATA_DRQ) == 0)) {
4632 /* handle BSY=0, DRQ=0 as error */
Albert Lee555a8962006-02-08 16:50:29 +08004633 qc->err_mask |= AC_ERR_HSM;
Albert Lee312f7da2005-09-27 17:38:03 +08004634 ap->hsm_task_state = HSM_ST_ERR;
4635 goto fsm_start;
4636 }
4637
Albert Lee07f6f7d2005-11-01 19:33:20 +08004638 ata_pio_sectors(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004639
4640 if (ap->hsm_task_state == HSM_ST_LAST &&
4641 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4642 /* all data read */
4643 ata_altstatus(ap);
4644 status = ata_chk_status(ap);
4645 goto fsm_start;
4646 }
4647 }
4648
4649 ata_altstatus(ap); /* flush */
4650 break;
4651
4652 case HSM_ST_LAST:
4653 if (unlikely(status & ATA_DRQ)) {
4654 /* handle DRQ=1 as error */
Albert Lee555a8962006-02-08 16:50:29 +08004655 qc->err_mask |= AC_ERR_HSM;
Albert Lee312f7da2005-09-27 17:38:03 +08004656 ap->hsm_task_state = HSM_ST_ERR;
4657 goto fsm_start;
4658 }
4659
4660 /* no more data to transfer */
4661 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4662 ap->id, status);
4663
4664 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004667 qc->err_mask |= ac_err_mask(status);
4668 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 break;
4670
Albert Lee312f7da2005-09-27 17:38:03 +08004671 case HSM_ST_ERR:
Albert Lee000080c2005-12-26 16:48:00 +08004672 if (qc->tf.command != ATA_CMD_PACKET)
4673 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4674 ap->id, status, host_stat);
Albert Lee312f7da2005-09-27 17:38:03 +08004675
Albert Leea4f16612005-12-26 16:40:53 +08004676 /* make sure qc->err_mask is available to
4677 * know what's wrong and recover
4678 */
Jeff Garzik587005d2006-02-11 18:17:32 -05004679 WARN_ON(qc->err_mask == 0);
Albert Leea4f16612005-12-26 16:40:53 +08004680
Albert Lee312f7da2005-09-27 17:38:03 +08004681 ap->hsm_task_state = HSM_ST_IDLE;
Jeff Garzik278efe92005-12-06 05:01:27 -05004682 ata_qc_complete(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 default:
4685 goto idle_irq;
4686 }
4687
4688 return 1; /* irq handled */
4689
4690idle_irq:
4691 ap->stats.idle_irq++;
4692
4693#ifdef ATA_IRQ_TRAP
4694 if ((ap->stats.idle_irq % 1000) == 0) {
4695 handled = 1;
4696 ata_irq_ack(ap, 0); /* debug trap */
4697 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4698 }
4699#endif
4700 return 0; /* irq not handled */
4701}
4702
4703/**
4704 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004705 * @irq: irq line (unused)
4706 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 * @regs: unused
4708 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004709 * Default interrupt handler for PCI IDE devices. Calls
4710 * ata_host_intr() for each port that is not disabled.
4711 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004713 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 *
4715 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004716 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 */
4718
4719irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4720{
4721 struct ata_host_set *host_set = dev_instance;
4722 unsigned int i;
4723 unsigned int handled = 0;
4724 unsigned long flags;
4725
4726 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4727 spin_lock_irqsave(&host_set->lock, flags);
4728
4729 for (i = 0; i < host_set->n_ports; i++) {
4730 struct ata_port *ap;
4731
4732 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004733 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08004734 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 struct ata_queued_cmd *qc;
4736
4737 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004738 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004739 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 handled |= ata_host_intr(ap, qc);
4741 }
4742 }
4743
4744 spin_unlock_irqrestore(&host_set->lock, flags);
4745
4746 return IRQ_RETVAL(handled);
4747}
4748
Edward Falk0baab862005-06-02 18:17:13 -04004749
Jens Axboe9b847542006-01-06 09:28:07 +01004750/*
4751 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4752 * without filling any other registers
4753 */
4754static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4755 u8 cmd)
4756{
4757 struct ata_taskfile tf;
4758 int err;
4759
4760 ata_tf_init(ap, &tf, dev->devno);
4761
4762 tf.command = cmd;
4763 tf.flags |= ATA_TFLAG_DEVICE;
4764 tf.protocol = ATA_PROT_NODATA;
4765
4766 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4767 if (err)
4768 printk(KERN_ERR "%s: ata command failed: %d\n",
4769 __FUNCTION__, err);
4770
4771 return err;
4772}
4773
4774static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4775{
4776 u8 cmd;
4777
4778 if (!ata_try_flush_cache(dev))
4779 return 0;
4780
4781 if (ata_id_has_flush_ext(dev->id))
4782 cmd = ATA_CMD_FLUSH_EXT;
4783 else
4784 cmd = ATA_CMD_FLUSH;
4785
4786 return ata_do_simple_cmd(ap, dev, cmd);
4787}
4788
4789static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4790{
4791 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4792}
4793
4794static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4795{
4796 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4797}
4798
4799/**
4800 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004801 * @ap: port the device is connected to
4802 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004803 *
4804 * Kick the drive back into action, by sending it an idle immediate
4805 * command and making sure its transfer mode matches between drive
4806 * and host.
4807 *
4808 */
4809int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4810{
4811 if (ap->flags & ATA_FLAG_SUSPENDED) {
4812 ap->flags &= ~ATA_FLAG_SUSPENDED;
4813 ata_set_mode(ap);
4814 }
4815 if (!ata_dev_present(dev))
4816 return 0;
4817 if (dev->class == ATA_DEV_ATA)
4818 ata_start_drive(ap, dev);
4819
4820 return 0;
4821}
4822
4823/**
4824 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004825 * @ap: port the device is connected to
4826 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004827 *
4828 * Flush the cache on the drive, if appropriate, then issue a
4829 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004830 */
4831int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4832{
4833 if (!ata_dev_present(dev))
4834 return 0;
4835 if (dev->class == ATA_DEV_ATA)
4836 ata_flush_cache(ap, dev);
4837
4838 ata_standby_drive(ap, dev);
4839 ap->flags |= ATA_FLAG_SUSPENDED;
4840 return 0;
4841}
4842
Albert Lee332b5a52006-02-08 16:51:34 +08004843/**
4844 * ata_port_start - Set port up for dma.
4845 * @ap: Port to initialize
4846 *
4847 * Called just after data structures for each port are
4848 * initialized. Allocates space for PRD table.
4849 *
4850 * May be used as the port_start() entry in ata_port_operations.
4851 *
4852 * LOCKING:
4853 * Inherited from caller.
4854 */
4855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856int ata_port_start (struct ata_port *ap)
4857{
4858 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004859 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
4861 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4862 if (!ap->prd)
4863 return -ENOMEM;
4864
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004865 rc = ata_pad_alloc(ap, dev);
4866 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004867 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004868 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004869 }
4870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4872
4873 return 0;
4874}
4875
Edward Falk0baab862005-06-02 18:17:13 -04004876
4877/**
4878 * ata_port_stop - Undo ata_port_start()
4879 * @ap: Port to shut down
4880 *
4881 * Frees the PRD table.
4882 *
4883 * May be used as the port_stop() entry in ata_port_operations.
4884 *
4885 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004886 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004887 */
4888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889void ata_port_stop (struct ata_port *ap)
4890{
4891 struct device *dev = ap->host_set->dev;
4892
4893 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004894 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895}
4896
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004897void ata_host_stop (struct ata_host_set *host_set)
4898{
4899 if (host_set->mmio_base)
4900 iounmap(host_set->mmio_base);
4901}
4902
4903
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904/**
4905 * ata_host_remove - Unregister SCSI host structure with upper layers
4906 * @ap: Port to unregister
4907 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4908 *
4909 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004910 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 */
4912
4913static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4914{
4915 struct Scsi_Host *sh = ap->host;
4916
4917 DPRINTK("ENTER\n");
4918
4919 if (do_unregister)
4920 scsi_remove_host(sh);
4921
4922 ap->ops->port_stop(ap);
4923}
4924
4925/**
4926 * ata_host_init - Initialize an ata_port structure
4927 * @ap: Structure to initialize
4928 * @host: associated SCSI mid-layer structure
4929 * @host_set: Collection of hosts to which @ap belongs
4930 * @ent: Probe information provided by low-level driver
4931 * @port_no: Port number associated with this ata_port
4932 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004933 * Initialize a new ata_port structure, and its associated
4934 * scsi_host.
4935 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004937 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 */
4939
4940static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4941 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004942 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943{
4944 unsigned int i;
4945
4946 host->max_id = 16;
4947 host->max_lun = 1;
4948 host->max_channel = 1;
4949 host->unique_id = ata_unique_id++;
4950 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 ap->flags = ATA_FLAG_PORT_DISABLED;
4953 ap->id = host->unique_id;
4954 ap->host = host;
4955 ap->ctl = ATA_DEVCTL_OBS;
4956 ap->host_set = host_set;
4957 ap->port_no = port_no;
4958 ap->hard_port_no =
4959 ent->legacy_mode ? ent->hard_port_no : port_no;
4960 ap->pio_mask = ent->pio_mask;
4961 ap->mwdma_mask = ent->mwdma_mask;
4962 ap->udma_mask = ent->udma_mask;
4963 ap->flags |= ent->host_flags;
4964 ap->ops = ent->port_ops;
4965 ap->cbl = ATA_CBL_NONE;
4966 ap->active_tag = ATA_TAG_POISON;
4967 ap->last_ctl = 0xFF;
4968
Tejun Heo86e45b62006-03-05 15:29:09 +09004969 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004970 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
4972 for (i = 0; i < ATA_MAX_DEVICES; i++)
4973 ap->device[i].devno = i;
4974
4975#ifdef ATA_IRQ_TRAP
4976 ap->stats.unhandled_irq = 1;
4977 ap->stats.idle_irq = 1;
4978#endif
4979
4980 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4981}
4982
4983/**
4984 * ata_host_add - Attach low-level ATA driver to system
4985 * @ent: Information provided by low-level driver
4986 * @host_set: Collections of ports to which we add
4987 * @port_no: Port number associated with this host
4988 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004989 * Attach low-level ATA driver to system.
4990 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004992 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 *
4994 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004995 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 */
4997
Jeff Garzik057ace52005-10-22 14:27:05 -04004998static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 struct ata_host_set *host_set,
5000 unsigned int port_no)
5001{
5002 struct Scsi_Host *host;
5003 struct ata_port *ap;
5004 int rc;
5005
5006 DPRINTK("ENTER\n");
5007 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
5008 if (!host)
5009 return NULL;
5010
5011 ap = (struct ata_port *) &host->hostdata[0];
5012
5013 ata_host_init(ap, host, host_set, ent, port_no);
5014
5015 rc = ap->ops->port_start(ap);
5016 if (rc)
5017 goto err_out;
5018
5019 return ap;
5020
5021err_out:
5022 scsi_host_put(host);
5023 return NULL;
5024}
5025
5026/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04005027 * ata_device_add - Register hardware device with ATA and SCSI layers
5028 * @ent: Probe information describing hardware device to be registered
5029 *
5030 * This function processes the information provided in the probe
5031 * information struct @ent, allocates the necessary ATA and SCSI
5032 * host information structures, initializes them, and registers
5033 * everything with requisite kernel subsystems.
5034 *
5035 * This function requests irqs, probes the ATA bus, and probes
5036 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 *
5038 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005039 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 *
5041 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005042 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 */
5044
Jeff Garzik057ace52005-10-22 14:27:05 -04005045int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046{
5047 unsigned int count = 0, i;
5048 struct device *dev = ent->dev;
5049 struct ata_host_set *host_set;
5050
5051 DPRINTK("ENTER\n");
5052 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005053 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
5055 if (!host_set)
5056 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 spin_lock_init(&host_set->lock);
5058
5059 host_set->dev = dev;
5060 host_set->n_ports = ent->n_ports;
5061 host_set->irq = ent->irq;
5062 host_set->mmio_base = ent->mmio_base;
5063 host_set->private_data = ent->private_data;
5064 host_set->ops = ent->port_ops;
5065
5066 /* register each port bound to this device */
5067 for (i = 0; i < ent->n_ports; i++) {
5068 struct ata_port *ap;
5069 unsigned long xfer_mode_mask;
5070
5071 ap = ata_host_add(ent, host_set, i);
5072 if (!ap)
5073 goto err_out;
5074
5075 host_set->ports[i] = ap;
5076 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
5077 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
5078 (ap->pio_mask << ATA_SHIFT_PIO);
5079
5080 /* print per-port info to dmesg */
5081 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
5082 "bmdma 0x%lX irq %lu\n",
5083 ap->id,
5084 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
5085 ata_mode_string(xfer_mode_mask),
5086 ap->ioaddr.cmd_addr,
5087 ap->ioaddr.ctl_addr,
5088 ap->ioaddr.bmdma_addr,
5089 ent->irq);
5090
5091 ata_chk_status(ap);
5092 host_set->ops->irq_clear(ap);
5093 count++;
5094 }
5095
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005096 if (!count)
5097 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
5099 /* obtain irq, that is shared between channels */
5100 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
5101 DRV_NAME, host_set))
5102 goto err_out;
5103
5104 /* perform each probe synchronously */
5105 DPRINTK("probe begin\n");
5106 for (i = 0; i < count; i++) {
5107 struct ata_port *ap;
5108 int rc;
5109
5110 ap = host_set->ports[i];
5111
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005112 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005114 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
5116 if (rc) {
5117 /* FIXME: do something useful here?
5118 * Current libata behavior will
5119 * tear down everything when
5120 * the module is removed
5121 * or the h/w is unplugged.
5122 */
5123 }
5124
5125 rc = scsi_add_host(ap->host, dev);
5126 if (rc) {
5127 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
5128 ap->id);
5129 /* FIXME: do something useful here */
5130 /* FIXME: handle unconditional calls to
5131 * scsi_scan_host and ata_host_remove, below,
5132 * at the very least
5133 */
5134 }
5135 }
5136
5137 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005138 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 for (i = 0; i < count; i++) {
5140 struct ata_port *ap = host_set->ports[i];
5141
Jeff Garzik644dd0c2005-10-03 15:55:19 -04005142 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 }
5144
5145 dev_set_drvdata(dev, host_set);
5146
5147 VPRINTK("EXIT, returning %u\n", ent->n_ports);
5148 return ent->n_ports; /* success */
5149
5150err_out:
5151 for (i = 0; i < count; i++) {
5152 ata_host_remove(host_set->ports[i], 1);
5153 scsi_host_put(host_set->ports[i]->host);
5154 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005155err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 kfree(host_set);
5157 VPRINTK("EXIT, returning 0\n");
5158 return 0;
5159}
5160
5161/**
Alan Cox17b14452005-09-15 15:44:00 +01005162 * ata_host_set_remove - PCI layer callback for device removal
5163 * @host_set: ATA host set that was removed
5164 *
5165 * Unregister all objects associated with this host set. Free those
5166 * objects.
5167 *
5168 * LOCKING:
5169 * Inherited from calling layer (may sleep).
5170 */
5171
Alan Cox17b14452005-09-15 15:44:00 +01005172void ata_host_set_remove(struct ata_host_set *host_set)
5173{
5174 struct ata_port *ap;
5175 unsigned int i;
5176
5177 for (i = 0; i < host_set->n_ports; i++) {
5178 ap = host_set->ports[i];
5179 scsi_remove_host(ap->host);
5180 }
5181
5182 free_irq(host_set->irq, host_set);
5183
5184 for (i = 0; i < host_set->n_ports; i++) {
5185 ap = host_set->ports[i];
5186
5187 ata_scsi_release(ap->host);
5188
5189 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
5190 struct ata_ioports *ioaddr = &ap->ioaddr;
5191
5192 if (ioaddr->cmd_addr == 0x1f0)
5193 release_region(0x1f0, 8);
5194 else if (ioaddr->cmd_addr == 0x170)
5195 release_region(0x170, 8);
5196 }
5197
5198 scsi_host_put(ap->host);
5199 }
5200
5201 if (host_set->ops->host_stop)
5202 host_set->ops->host_stop(host_set);
5203
5204 kfree(host_set);
5205}
5206
5207/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 * ata_scsi_release - SCSI layer callback hook for host unload
5209 * @host: libata host to be unloaded
5210 *
5211 * Performs all duties necessary to shut down a libata port...
5212 * Kill port kthread, disable port, and release resources.
5213 *
5214 * LOCKING:
5215 * Inherited from SCSI layer.
5216 *
5217 * RETURNS:
5218 * One.
5219 */
5220
5221int ata_scsi_release(struct Scsi_Host *host)
5222{
5223 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09005224 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
5226 DPRINTK("ENTER\n");
5227
5228 ap->ops->port_disable(ap);
5229 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09005230 for (i = 0; i < ATA_MAX_DEVICES; i++)
5231 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
5233 DPRINTK("EXIT\n");
5234 return 1;
5235}
5236
5237/**
5238 * ata_std_ports - initialize ioaddr with standard port offsets.
5239 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04005240 *
5241 * Utility function which initializes data_addr, error_addr,
5242 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5243 * device_addr, status_addr, and command_addr to standard offsets
5244 * relative to cmd_addr.
5245 *
5246 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 */
Edward Falk0baab862005-06-02 18:17:13 -04005248
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249void ata_std_ports(struct ata_ioports *ioaddr)
5250{
5251 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5252 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5253 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5254 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5255 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5256 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5257 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5258 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5259 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5260 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5261}
5262
Edward Falk0baab862005-06-02 18:17:13 -04005263
Jeff Garzik374b1872005-08-30 05:42:52 -04005264#ifdef CONFIG_PCI
5265
5266void ata_pci_host_stop (struct ata_host_set *host_set)
5267{
5268 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5269
5270 pci_iounmap(pdev, host_set->mmio_base);
5271}
5272
Edward Falk0baab862005-06-02 18:17:13 -04005273/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 * ata_pci_remove_one - PCI layer callback for device removal
5275 * @pdev: PCI device that was removed
5276 *
5277 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005278 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 * Handle this by unregistering all objects associated
5280 * with this PCI device. Free those objects. Then finally
5281 * release PCI resources and disable device.
5282 *
5283 * LOCKING:
5284 * Inherited from PCI layer (may sleep).
5285 */
5286
5287void ata_pci_remove_one (struct pci_dev *pdev)
5288{
5289 struct device *dev = pci_dev_to_dev(pdev);
5290 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291
Alan Cox17b14452005-09-15 15:44:00 +01005292 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 pci_release_regions(pdev);
5294 pci_disable_device(pdev);
5295 dev_set_drvdata(dev, NULL);
5296}
5297
5298/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005299int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300{
5301 unsigned long tmp = 0;
5302
5303 switch (bits->width) {
5304 case 1: {
5305 u8 tmp8 = 0;
5306 pci_read_config_byte(pdev, bits->reg, &tmp8);
5307 tmp = tmp8;
5308 break;
5309 }
5310 case 2: {
5311 u16 tmp16 = 0;
5312 pci_read_config_word(pdev, bits->reg, &tmp16);
5313 tmp = tmp16;
5314 break;
5315 }
5316 case 4: {
5317 u32 tmp32 = 0;
5318 pci_read_config_dword(pdev, bits->reg, &tmp32);
5319 tmp = tmp32;
5320 break;
5321 }
5322
5323 default:
5324 return -EINVAL;
5325 }
5326
5327 tmp &= bits->mask;
5328
5329 return (tmp == bits->val) ? 1 : 0;
5330}
Jens Axboe9b847542006-01-06 09:28:07 +01005331
5332int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5333{
5334 pci_save_state(pdev);
5335 pci_disable_device(pdev);
5336 pci_set_power_state(pdev, PCI_D3hot);
5337 return 0;
5338}
5339
5340int ata_pci_device_resume(struct pci_dev *pdev)
5341{
5342 pci_set_power_state(pdev, PCI_D0);
5343 pci_restore_state(pdev);
5344 pci_enable_device(pdev);
5345 pci_set_master(pdev);
5346 return 0;
5347}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348#endif /* CONFIG_PCI */
5349
5350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351static int __init ata_init(void)
5352{
5353 ata_wq = create_workqueue("ata");
5354 if (!ata_wq)
5355 return -ENOMEM;
5356
5357 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5358 return 0;
5359}
5360
5361static void __exit ata_exit(void)
5362{
5363 destroy_workqueue(ata_wq);
5364}
5365
5366module_init(ata_init);
5367module_exit(ata_exit);
5368
Jeff Garzik67846b32005-10-05 02:58:32 -04005369static unsigned long ratelimit_time;
5370static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5371
5372int ata_ratelimit(void)
5373{
5374 int rc;
5375 unsigned long flags;
5376
5377 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5378
5379 if (time_after(jiffies, ratelimit_time)) {
5380 rc = 1;
5381 ratelimit_time = jiffies + (HZ/5);
5382 } else
5383 rc = 0;
5384
5385 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5386
5387 return rc;
5388}
5389
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390/*
5391 * libata is essentially a library of internal helper functions for
5392 * low-level ATA host controller drivers. As such, the API/ABI is
5393 * likely to change as new drivers are added and updated.
5394 * Do not depend on ABI/API stability.
5395 */
5396
5397EXPORT_SYMBOL_GPL(ata_std_bios_param);
5398EXPORT_SYMBOL_GPL(ata_std_ports);
5399EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005400EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401EXPORT_SYMBOL_GPL(ata_sg_init);
5402EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09005403EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5405EXPORT_SYMBOL_GPL(ata_eng_timeout);
5406EXPORT_SYMBOL_GPL(ata_tf_load);
5407EXPORT_SYMBOL_GPL(ata_tf_read);
5408EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5409EXPORT_SYMBOL_GPL(ata_std_dev_select);
5410EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5411EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5412EXPORT_SYMBOL_GPL(ata_check_status);
5413EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414EXPORT_SYMBOL_GPL(ata_exec_command);
5415EXPORT_SYMBOL_GPL(ata_port_start);
5416EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005417EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418EXPORT_SYMBOL_GPL(ata_interrupt);
5419EXPORT_SYMBOL_GPL(ata_qc_prep);
5420EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5421EXPORT_SYMBOL_GPL(ata_bmdma_start);
5422EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5423EXPORT_SYMBOL_GPL(ata_bmdma_status);
5424EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5425EXPORT_SYMBOL_GPL(ata_port_probe);
5426EXPORT_SYMBOL_GPL(sata_phy_reset);
5427EXPORT_SYMBOL_GPL(__sata_phy_reset);
5428EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005429EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005430EXPORT_SYMBOL_GPL(ata_std_softreset);
5431EXPORT_SYMBOL_GPL(sata_std_hardreset);
5432EXPORT_SYMBOL_GPL(ata_std_postreset);
5433EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005434EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09005435EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005437EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005438EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09005439EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5441EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09005442EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443EXPORT_SYMBOL_GPL(ata_scsi_error);
5444EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5445EXPORT_SYMBOL_GPL(ata_scsi_release);
5446EXPORT_SYMBOL_GPL(ata_host_intr);
5447EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09005448EXPORT_SYMBOL_GPL(ata_id_string);
5449EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005451EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5452EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005454EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005455EXPORT_SYMBOL_GPL(ata_timing_compute);
5456EXPORT_SYMBOL_GPL(ata_timing_merge);
5457
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458#ifdef CONFIG_PCI
5459EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005460EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5462EXPORT_SYMBOL_GPL(ata_pci_init_one);
5463EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005464EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5465EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005467
5468EXPORT_SYMBOL_GPL(ata_device_suspend);
5469EXPORT_SYMBOL_GPL(ata_device_resume);
5470EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5471EXPORT_SYMBOL_GPL(ata_scsi_device_resume);