blob: 9cd9053e78fe0898c8f2277ab14824f5dd0058ab [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);
Tejun Heoacf356b2006-03-24 14:07:50 +090068static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70static unsigned int ata_unique_id = 1;
71static struct workqueue_struct *ata_wq;
72
Jeff Garzik418dc1f2006-03-11 20:50:08 -050073int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040074module_param(atapi_enabled, int, 0444);
75MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
76
Jeff Garzikc3c013a2006-02-27 22:31:19 -050077int libata_fua = 0;
78module_param_named(fua, libata_fua, int, 0444);
79MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081MODULE_AUTHOR("Jeff Garzik");
82MODULE_DESCRIPTION("Library module for ATA devices");
83MODULE_LICENSE("GPL");
84MODULE_VERSION(DRV_VERSION);
85
Edward Falk0baab862005-06-02 18:17:13 -040086
87/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
89 * @tf: Taskfile to convert
90 * @fis: Buffer into which data will output
91 * @pmp: Port multiplier port
92 *
93 * Converts a standard ATA taskfile to a Serial ATA
94 * FIS structure (Register - Host to Device).
95 *
96 * LOCKING:
97 * Inherited from caller.
98 */
99
Jeff Garzik057ace52005-10-22 14:27:05 -0400100void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 fis[0] = 0x27; /* Register - Host to Device FIS */
103 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
104 bit 7 indicates Command FIS */
105 fis[2] = tf->command;
106 fis[3] = tf->feature;
107
108 fis[4] = tf->lbal;
109 fis[5] = tf->lbam;
110 fis[6] = tf->lbah;
111 fis[7] = tf->device;
112
113 fis[8] = tf->hob_lbal;
114 fis[9] = tf->hob_lbam;
115 fis[10] = tf->hob_lbah;
116 fis[11] = tf->hob_feature;
117
118 fis[12] = tf->nsect;
119 fis[13] = tf->hob_nsect;
120 fis[14] = 0;
121 fis[15] = tf->ctl;
122
123 fis[16] = 0;
124 fis[17] = 0;
125 fis[18] = 0;
126 fis[19] = 0;
127}
128
129/**
130 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
131 * @fis: Buffer from which data will be input
132 * @tf: Taskfile to output
133 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500134 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
136 * LOCKING:
137 * Inherited from caller.
138 */
139
Jeff Garzik057ace52005-10-22 14:27:05 -0400140void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 tf->command = fis[2]; /* status */
143 tf->feature = fis[3]; /* error */
144
145 tf->lbal = fis[4];
146 tf->lbam = fis[5];
147 tf->lbah = fis[6];
148 tf->device = fis[7];
149
150 tf->hob_lbal = fis[8];
151 tf->hob_lbam = fis[9];
152 tf->hob_lbah = fis[10];
153
154 tf->nsect = fis[12];
155 tf->hob_nsect = fis[13];
156}
157
Albert Lee8cbd6df2005-10-12 15:06:27 +0800158static const u8 ata_rw_cmds[] = {
159 /* pio multi */
160 ATA_CMD_READ_MULTI,
161 ATA_CMD_WRITE_MULTI,
162 ATA_CMD_READ_MULTI_EXT,
163 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100164 0,
165 0,
166 0,
167 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800168 /* pio */
169 ATA_CMD_PIO_READ,
170 ATA_CMD_PIO_WRITE,
171 ATA_CMD_PIO_READ_EXT,
172 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100173 0,
174 0,
175 0,
176 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800177 /* dma */
178 ATA_CMD_READ,
179 ATA_CMD_WRITE,
180 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100181 ATA_CMD_WRITE_EXT,
182 0,
183 0,
184 0,
185 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800186};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800189 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
190 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800192 * Examine the device configuration and tf->flags to calculate
193 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * LOCKING:
196 * caller.
197 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100198int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800200 struct ata_taskfile *tf = &qc->tf;
201 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100202 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100204 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800205
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800207 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
208 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Albert Lee8cbd6df2005-10-12 15:06:27 +0800210 if (dev->flags & ATA_DFLAG_PIO) {
211 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100212 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000213 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
214 /* Unable to use DMA due to host limitation */
215 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800216 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800217 } else {
218 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100219 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100222 cmd = ata_rw_cmds[index + fua + lba48 + write];
223 if (cmd) {
224 tf->command = cmd;
225 return 0;
226 }
227 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Tejun Heocb95d562006-03-06 04:31:56 +0900230/**
231 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
232 * @pio_mask: pio_mask
233 * @mwdma_mask: mwdma_mask
234 * @udma_mask: udma_mask
235 *
236 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
237 * unsigned int xfer_mask.
238 *
239 * LOCKING:
240 * None.
241 *
242 * RETURNS:
243 * Packed xfer_mask.
244 */
245static unsigned int ata_pack_xfermask(unsigned int pio_mask,
246 unsigned int mwdma_mask,
247 unsigned int udma_mask)
248{
249 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
250 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
251 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
252}
253
Tejun Heoc0489e42006-03-24 14:07:49 +0900254/**
255 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
256 * @xfer_mask: xfer_mask to unpack
257 * @pio_mask: resulting pio_mask
258 * @mwdma_mask: resulting mwdma_mask
259 * @udma_mask: resulting udma_mask
260 *
261 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
262 * Any NULL distination masks will be ignored.
263 */
264static void ata_unpack_xfermask(unsigned int xfer_mask,
265 unsigned int *pio_mask,
266 unsigned int *mwdma_mask,
267 unsigned int *udma_mask)
268{
269 if (pio_mask)
270 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
271 if (mwdma_mask)
272 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
273 if (udma_mask)
274 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
275}
276
Tejun Heocb95d562006-03-06 04:31:56 +0900277static const struct ata_xfer_ent {
278 unsigned int shift, bits;
279 u8 base;
280} ata_xfer_tbl[] = {
281 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
282 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
283 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
284 { -1, },
285};
286
287/**
288 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
289 * @xfer_mask: xfer_mask of interest
290 *
291 * Return matching XFER_* value for @xfer_mask. Only the highest
292 * bit of @xfer_mask is considered.
293 *
294 * LOCKING:
295 * None.
296 *
297 * RETURNS:
298 * Matching XFER_* value, 0 if no match found.
299 */
300static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
301{
302 int highbit = fls(xfer_mask) - 1;
303 const struct ata_xfer_ent *ent;
304
305 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
306 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
307 return ent->base + highbit - ent->shift;
308 return 0;
309}
310
311/**
312 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
313 * @xfer_mode: XFER_* of interest
314 *
315 * Return matching xfer_mask for @xfer_mode.
316 *
317 * LOCKING:
318 * None.
319 *
320 * RETURNS:
321 * Matching xfer_mask, 0 if no match found.
322 */
323static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
324{
325 const struct ata_xfer_ent *ent;
326
327 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
328 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
329 return 1 << (ent->shift + xfer_mode - ent->base);
330 return 0;
331}
332
333/**
334 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
335 * @xfer_mode: XFER_* of interest
336 *
337 * Return matching xfer_shift for @xfer_mode.
338 *
339 * LOCKING:
340 * None.
341 *
342 * RETURNS:
343 * Matching xfer_shift, -1 if no match found.
344 */
345static int ata_xfer_mode2shift(unsigned int xfer_mode)
346{
347 const struct ata_xfer_ent *ent;
348
349 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
350 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
351 return ent->shift;
352 return -1;
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900356 * ata_mode_string - convert xfer_mask to string
357 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 *
359 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900360 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *
362 * LOCKING:
363 * None.
364 *
365 * RETURNS:
366 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900367 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900369static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Tejun Heo75f554b2006-03-06 04:31:57 +0900371 static const char * const xfer_mode_str[] = {
372 "PIO0",
373 "PIO1",
374 "PIO2",
375 "PIO3",
376 "PIO4",
377 "MWDMA0",
378 "MWDMA1",
379 "MWDMA2",
380 "UDMA/16",
381 "UDMA/25",
382 "UDMA/33",
383 "UDMA/44",
384 "UDMA/66",
385 "UDMA/100",
386 "UDMA/133",
387 "UDMA7",
388 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900389 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900391 highbit = fls(xfer_mask) - 1;
392 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
393 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/**
398 * ata_pio_devchk - PATA device presence detection
399 * @ap: ATA channel to examine
400 * @device: Device to examine (starting at zero)
401 *
402 * This technique was originally described in
403 * Hale Landis's ATADRVR (www.ata-atapi.com), and
404 * later found its way into the ATA/ATAPI spec.
405 *
406 * Write a pattern to the ATA shadow registers,
407 * and if a device is present, it will respond by
408 * correctly storing and echoing back the
409 * ATA shadow register contents.
410 *
411 * LOCKING:
412 * caller.
413 */
414
415static unsigned int ata_pio_devchk(struct ata_port *ap,
416 unsigned int device)
417{
418 struct ata_ioports *ioaddr = &ap->ioaddr;
419 u8 nsect, lbal;
420
421 ap->ops->dev_select(ap, device);
422
423 outb(0x55, ioaddr->nsect_addr);
424 outb(0xaa, ioaddr->lbal_addr);
425
426 outb(0xaa, ioaddr->nsect_addr);
427 outb(0x55, ioaddr->lbal_addr);
428
429 outb(0x55, ioaddr->nsect_addr);
430 outb(0xaa, ioaddr->lbal_addr);
431
432 nsect = inb(ioaddr->nsect_addr);
433 lbal = inb(ioaddr->lbal_addr);
434
435 if ((nsect == 0x55) && (lbal == 0xaa))
436 return 1; /* we found a device */
437
438 return 0; /* nothing found */
439}
440
441/**
442 * ata_mmio_devchk - PATA device presence detection
443 * @ap: ATA channel to examine
444 * @device: Device to examine (starting at zero)
445 *
446 * This technique was originally described in
447 * Hale Landis's ATADRVR (www.ata-atapi.com), and
448 * later found its way into the ATA/ATAPI spec.
449 *
450 * Write a pattern to the ATA shadow registers,
451 * and if a device is present, it will respond by
452 * correctly storing and echoing back the
453 * ATA shadow register contents.
454 *
455 * LOCKING:
456 * caller.
457 */
458
459static unsigned int ata_mmio_devchk(struct ata_port *ap,
460 unsigned int device)
461{
462 struct ata_ioports *ioaddr = &ap->ioaddr;
463 u8 nsect, lbal;
464
465 ap->ops->dev_select(ap, device);
466
467 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
468 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
469
470 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
471 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
472
473 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
474 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
475
476 nsect = readb((void __iomem *) ioaddr->nsect_addr);
477 lbal = readb((void __iomem *) ioaddr->lbal_addr);
478
479 if ((nsect == 0x55) && (lbal == 0xaa))
480 return 1; /* we found a device */
481
482 return 0; /* nothing found */
483}
484
485/**
486 * ata_devchk - PATA device presence detection
487 * @ap: ATA channel to examine
488 * @device: Device to examine (starting at zero)
489 *
490 * Dispatch ATA device presence detection, depending
491 * on whether we are using PIO or MMIO to talk to the
492 * ATA shadow registers.
493 *
494 * LOCKING:
495 * caller.
496 */
497
498static unsigned int ata_devchk(struct ata_port *ap,
499 unsigned int device)
500{
501 if (ap->flags & ATA_FLAG_MMIO)
502 return ata_mmio_devchk(ap, device);
503 return ata_pio_devchk(ap, device);
504}
505
506/**
507 * ata_dev_classify - determine device type based on ATA-spec signature
508 * @tf: ATA taskfile register set for device to be identified
509 *
510 * Determine from taskfile register contents whether a device is
511 * ATA or ATAPI, as per "Signature and persistence" section
512 * of ATA/PI spec (volume 1, sect 5.14).
513 *
514 * LOCKING:
515 * None.
516 *
517 * RETURNS:
518 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
519 * the event of failure.
520 */
521
Jeff Garzik057ace52005-10-22 14:27:05 -0400522unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 /* Apple's open source Darwin code hints that some devices only
525 * put a proper signature into the LBA mid/high registers,
526 * So, we only check those. It's sufficient for uniqueness.
527 */
528
529 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
530 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
531 DPRINTK("found ATA device by sig\n");
532 return ATA_DEV_ATA;
533 }
534
535 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
536 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
537 DPRINTK("found ATAPI device by sig\n");
538 return ATA_DEV_ATAPI;
539 }
540
541 DPRINTK("unknown device\n");
542 return ATA_DEV_UNKNOWN;
543}
544
545/**
546 * ata_dev_try_classify - Parse returned ATA device signature
547 * @ap: ATA channel to examine
548 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900549 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 *
551 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
552 * an ATA/ATAPI-defined set of values is placed in the ATA
553 * shadow registers, indicating the results of device detection
554 * and diagnostics.
555 *
556 * Select the ATA device, and read the values from the ATA shadow
557 * registers. Then parse according to the Error register value,
558 * and the spec-defined values examined by ata_dev_classify().
559 *
560 * LOCKING:
561 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900562 *
563 * RETURNS:
564 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
566
Tejun Heob4dc7622006-01-24 17:05:22 +0900567static unsigned int
568ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct ata_taskfile tf;
571 unsigned int class;
572 u8 err;
573
574 ap->ops->dev_select(ap, device);
575
576 memset(&tf, 0, sizeof(tf));
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400579 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900580 if (r_err)
581 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* see if device passed diags */
584 if (err == 1)
585 /* do nothing */ ;
586 else if ((device == 0) && (err == 0x81))
587 /* do nothing */ ;
588 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900589 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Tejun Heob4dc7622006-01-24 17:05:22 +0900591 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900595 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900597 return ATA_DEV_NONE;
598 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900602 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * @id: IDENTIFY DEVICE results we will examine
604 * @s: string into which data is output
605 * @ofs: offset into identify device page
606 * @len: length of string to return. must be an even number.
607 *
608 * The strings in the IDENTIFY DEVICE page are broken up into
609 * 16-bit chunks. Run through the string, and output each
610 * 8-bit chunk linearly, regardless of platform.
611 *
612 * LOCKING:
613 * caller.
614 */
615
Tejun Heo6a62a042006-02-13 10:02:46 +0900616void ata_id_string(const u16 *id, unsigned char *s,
617 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 unsigned int c;
620
621 while (len > 0) {
622 c = id[ofs] >> 8;
623 *s = c;
624 s++;
625
626 c = id[ofs] & 0xff;
627 *s = c;
628 s++;
629
630 ofs++;
631 len -= 2;
632 }
633}
634
Tejun Heo0e949ff2006-02-12 22:47:04 +0900635/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900636 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900637 * @id: IDENTIFY DEVICE results we will examine
638 * @s: string into which data is output
639 * @ofs: offset into identify device page
640 * @len: length of string to return. must be an odd number.
641 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900642 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900643 * trims trailing spaces and terminates the resulting string with
644 * null. @len must be actual maximum length (even number) + 1.
645 *
646 * LOCKING:
647 * caller.
648 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900649void ata_id_c_string(const u16 *id, unsigned char *s,
650 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900651{
652 unsigned char *p;
653
654 WARN_ON(!(len & 1));
655
Tejun Heo6a62a042006-02-13 10:02:46 +0900656 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900657
658 p = s + strnlen(s, len - 1);
659 while (p > s && p[-1] == ' ')
660 p--;
661 *p = '\0';
662}
Edward Falk0baab862005-06-02 18:17:13 -0400663
Tejun Heo29407402006-02-12 22:47:04 +0900664static u64 ata_id_n_sectors(const u16 *id)
665{
666 if (ata_id_has_lba(id)) {
667 if (ata_id_has_lba48(id))
668 return ata_id_u64(id, 100);
669 else
670 return ata_id_u32(id, 60);
671 } else {
672 if (ata_id_current_chs_valid(id))
673 return ata_id_u32(id, 57);
674 else
675 return id[1] * id[3] * id[6];
676 }
677}
678
Edward Falk0baab862005-06-02 18:17:13 -0400679/**
680 * ata_noop_dev_select - Select device 0/1 on ATA bus
681 * @ap: ATA channel to manipulate
682 * @device: ATA device (numbered from zero) to select
683 *
684 * This function performs no actual function.
685 *
686 * May be used as the dev_select() entry in ata_port_operations.
687 *
688 * LOCKING:
689 * caller.
690 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
692{
693}
694
Edward Falk0baab862005-06-02 18:17:13 -0400695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/**
697 * ata_std_dev_select - Select device 0/1 on ATA bus
698 * @ap: ATA channel to manipulate
699 * @device: ATA device (numbered from zero) to select
700 *
701 * Use the method defined in the ATA specification to
702 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400703 * ATA channel. Works with both PIO and MMIO.
704 *
705 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 *
707 * LOCKING:
708 * caller.
709 */
710
711void ata_std_dev_select (struct ata_port *ap, unsigned int device)
712{
713 u8 tmp;
714
715 if (device == 0)
716 tmp = ATA_DEVICE_OBS;
717 else
718 tmp = ATA_DEVICE_OBS | ATA_DEV1;
719
720 if (ap->flags & ATA_FLAG_MMIO) {
721 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
722 } else {
723 outb(tmp, ap->ioaddr.device_addr);
724 }
725 ata_pause(ap); /* needed; also flushes, for mmio */
726}
727
728/**
729 * ata_dev_select - Select device 0/1 on ATA bus
730 * @ap: ATA channel to manipulate
731 * @device: ATA device (numbered from zero) to select
732 * @wait: non-zero to wait for Status register BSY bit to clear
733 * @can_sleep: non-zero if context allows sleeping
734 *
735 * Use the method defined in the ATA specification to
736 * make either device 0, or device 1, active on the
737 * ATA channel.
738 *
739 * This is a high-level version of ata_std_dev_select(),
740 * which additionally provides the services of inserting
741 * the proper pauses and status polling, where needed.
742 *
743 * LOCKING:
744 * caller.
745 */
746
747void ata_dev_select(struct ata_port *ap, unsigned int device,
748 unsigned int wait, unsigned int can_sleep)
749{
750 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
751 ap->id, device, wait);
752
753 if (wait)
754 ata_wait_idle(ap);
755
756 ap->ops->dev_select(ap, device);
757
758 if (wait) {
759 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
760 msleep(150);
761 ata_wait_idle(ap);
762 }
763}
764
765/**
766 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900767 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900769 * Dump selected 16-bit words from the given IDENTIFY DEVICE
770 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 *
772 * LOCKING:
773 * caller.
774 */
775
Tejun Heo0bd33002006-02-12 22:47:05 +0900776static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 DPRINTK("49==0x%04x "
779 "53==0x%04x "
780 "63==0x%04x "
781 "64==0x%04x "
782 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900783 id[49],
784 id[53],
785 id[63],
786 id[64],
787 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 DPRINTK("80==0x%04x "
789 "81==0x%04x "
790 "82==0x%04x "
791 "83==0x%04x "
792 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900793 id[80],
794 id[81],
795 id[82],
796 id[83],
797 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 DPRINTK("88==0x%04x "
799 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900800 id[88],
801 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Tejun Heocb95d562006-03-06 04:31:56 +0900804/**
805 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
806 * @id: IDENTIFY data to compute xfer mask from
807 *
808 * Compute the xfermask for this device. This is not as trivial
809 * as it seems if we must consider early devices correctly.
810 *
811 * FIXME: pre IDE drive timing (do we care ?).
812 *
813 * LOCKING:
814 * None.
815 *
816 * RETURNS:
817 * Computed xfermask
818 */
819static unsigned int ata_id_xfermask(const u16 *id)
820{
821 unsigned int pio_mask, mwdma_mask, udma_mask;
822
823 /* Usual case. Word 53 indicates word 64 is valid */
824 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
825 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
826 pio_mask <<= 3;
827 pio_mask |= 0x7;
828 } else {
829 /* If word 64 isn't valid then Word 51 high byte holds
830 * the PIO timing number for the maximum. Turn it into
831 * a mask.
832 */
833 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
834
835 /* But wait.. there's more. Design your standards by
836 * committee and you too can get a free iordy field to
837 * process. However its the speeds not the modes that
838 * are supported... Note drivers using the timing API
839 * will get this right anyway
840 */
841 }
842
843 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +0900844
845 udma_mask = 0;
846 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
847 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900848
849 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
850}
851
Tejun Heo86e45b62006-03-05 15:29:09 +0900852/**
853 * ata_port_queue_task - Queue port_task
854 * @ap: The ata_port to queue port_task for
855 *
856 * Schedule @fn(@data) for execution after @delay jiffies using
857 * port_task. There is one port_task per port and it's the
858 * user(low level driver)'s responsibility to make sure that only
859 * one task is active at any given time.
860 *
861 * libata core layer takes care of synchronization between
862 * port_task and EH. ata_port_queue_task() may be ignored for EH
863 * synchronization.
864 *
865 * LOCKING:
866 * Inherited from caller.
867 */
868void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
869 unsigned long delay)
870{
871 int rc;
872
Tejun Heo2e755f62006-03-05 15:29:09 +0900873 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900874 return;
875
876 PREPARE_WORK(&ap->port_task, fn, data);
877
878 if (!delay)
879 rc = queue_work(ata_wq, &ap->port_task);
880 else
881 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
882
883 /* rc == 0 means that another user is using port task */
884 WARN_ON(rc == 0);
885}
886
887/**
888 * ata_port_flush_task - Flush port_task
889 * @ap: The ata_port to flush port_task for
890 *
891 * After this function completes, port_task is guranteed not to
892 * be running or scheduled.
893 *
894 * LOCKING:
895 * Kernel thread context (may sleep)
896 */
897void ata_port_flush_task(struct ata_port *ap)
898{
899 unsigned long flags;
900
901 DPRINTK("ENTER\n");
902
903 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900904 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900905 spin_unlock_irqrestore(&ap->host_set->lock, flags);
906
907 DPRINTK("flush #1\n");
908 flush_workqueue(ata_wq);
909
910 /*
911 * At this point, if a task is running, it's guaranteed to see
912 * the FLUSH flag; thus, it will never queue pio tasks again.
913 * Cancel and flush.
914 */
915 if (!cancel_delayed_work(&ap->port_task)) {
916 DPRINTK("flush #2\n");
917 flush_workqueue(ata_wq);
918 }
919
920 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900921 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900922 spin_unlock_irqrestore(&ap->host_set->lock, flags);
923
924 DPRINTK("EXIT\n");
925}
926
Tejun Heo77853bf2006-01-23 13:09:36 +0900927void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900928{
Tejun Heo77853bf2006-01-23 13:09:36 +0900929 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900930
Tejun Heo77853bf2006-01-23 13:09:36 +0900931 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900932 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900933}
934
935/**
936 * ata_exec_internal - execute libata internal command
937 * @ap: Port to which the command is sent
938 * @dev: Device to which the command is sent
939 * @tf: Taskfile registers for the command and the result
940 * @dma_dir: Data tranfer direction of the command
941 * @buf: Data buffer of the command
942 * @buflen: Length of data buffer
943 *
944 * Executes libata internal command with timeout. @tf contains
945 * command on entry and result on return. Timeout and error
946 * conditions are reported via return value. No recovery action
947 * is taken after a command times out. It's caller's duty to
948 * clean up after timeout.
949 *
950 * LOCKING:
951 * None. Should be called with kernel context, might sleep.
952 */
953
954static unsigned
955ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
956 struct ata_taskfile *tf,
957 int dma_dir, void *buf, unsigned int buflen)
958{
959 u8 command = tf->command;
960 struct ata_queued_cmd *qc;
961 DECLARE_COMPLETION(wait);
962 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900963 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900964
965 spin_lock_irqsave(&ap->host_set->lock, flags);
966
967 qc = ata_qc_new_init(ap, dev);
968 BUG_ON(qc == NULL);
969
970 qc->tf = *tf;
971 qc->dma_dir = dma_dir;
972 if (dma_dir != DMA_NONE) {
973 ata_sg_init_one(qc, buf, buflen);
974 qc->nsect = buflen / ATA_SECT_SIZE;
975 }
976
Tejun Heo77853bf2006-01-23 13:09:36 +0900977 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900978 qc->complete_fn = ata_qc_complete_internal;
979
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900980 qc->err_mask = ata_qc_issue(qc);
981 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900982 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900983
984 spin_unlock_irqrestore(&ap->host_set->lock, flags);
985
986 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
Albert Lee41ade502006-03-14 11:19:04 +0800987 ata_port_flush_task(ap);
988
Tejun Heoa2a7a662005-12-13 14:48:31 +0900989 spin_lock_irqsave(&ap->host_set->lock, flags);
990
991 /* We're racing with irq here. If we lose, the
992 * following test prevents us from completing the qc
993 * again. If completion irq occurs after here but
994 * before the caller cleans up, it will result in a
995 * spurious interrupt. We can live with that.
996 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900997 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900998 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900999 ata_qc_complete(qc);
1000 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1001 ap->id, command);
1002 }
1003
1004 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1005 }
1006
Tejun Heo77853bf2006-01-23 13:09:36 +09001007 *tf = qc->tf;
1008 err_mask = qc->err_mask;
1009
1010 ata_qc_free(qc);
1011
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001012 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1013 * Until those drivers are fixed, we detect the condition
1014 * here, fail the command with AC_ERR_SYSTEM and reenable the
1015 * port.
1016 *
1017 * Note that this doesn't change any behavior as internal
1018 * command failure results in disabling the device in the
1019 * higher layer for LLDDs without new reset/EH callbacks.
1020 *
1021 * Kill the following code as soon as those drivers are fixed.
1022 */
1023 if (ap->flags & ATA_FLAG_PORT_DISABLED) {
1024 err_mask |= AC_ERR_SYSTEM;
1025 ata_port_probe(ap);
1026 }
1027
Tejun Heo77853bf2006-01-23 13:09:36 +09001028 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001032 * ata_pio_need_iordy - check if iordy needed
1033 * @adev: ATA device
1034 *
1035 * Check if the current speed of the device requires IORDY. Used
1036 * by various controllers for chip configuration.
1037 */
1038
1039unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1040{
1041 int pio;
1042 int speed = adev->pio_mode - XFER_PIO_0;
1043
1044 if (speed < 2)
1045 return 0;
1046 if (speed > 2)
1047 return 1;
1048
1049 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1050
1051 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1052 pio = adev->id[ATA_ID_EIDE_PIO];
1053 /* Is the speed faster than the drive allows non IORDY ? */
1054 if (pio) {
1055 /* This is cycle times not frequency - watch the logic! */
1056 if (pio > 240) /* PIO2 is 240nS per cycle */
1057 return 1;
1058 return 0;
1059 }
1060 }
1061 return 0;
1062}
1063
1064/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001065 * ata_dev_read_id - Read ID data from the specified device
1066 * @ap: port on which target device resides
1067 * @dev: target device
1068 * @p_class: pointer to class of the target device (may be changed)
1069 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +09001070 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +09001071 *
1072 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1073 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1074 * devices. This function also takes care of EDD signature
1075 * misreporting (to be removed once EDD support is gone) and
1076 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
1077 *
1078 * LOCKING:
1079 * Kernel thread context (may sleep)
1080 *
1081 * RETURNS:
1082 * 0 on success, -errno otherwise.
1083 */
1084static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +09001085 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001086{
1087 unsigned int class = *p_class;
1088 unsigned int using_edd;
1089 struct ata_taskfile tf;
1090 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +09001091 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001092 const char *reason;
1093 int rc;
1094
1095 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1096
1097 if (ap->ops->probe_reset ||
1098 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1099 using_edd = 0;
1100 else
1101 using_edd = 1;
1102
1103 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1104
Tejun Heod9572b12006-03-01 16:09:35 +09001105 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1106 if (id == NULL) {
1107 rc = -ENOMEM;
1108 reason = "out of memory";
1109 goto err_out;
1110 }
1111
Tejun Heo49016ac2006-02-21 02:12:11 +09001112 retry:
1113 ata_tf_init(ap, &tf, dev->devno);
1114
1115 switch (class) {
1116 case ATA_DEV_ATA:
1117 tf.command = ATA_CMD_ID_ATA;
1118 break;
1119 case ATA_DEV_ATAPI:
1120 tf.command = ATA_CMD_ID_ATAPI;
1121 break;
1122 default:
1123 rc = -ENODEV;
1124 reason = "unsupported class";
1125 goto err_out;
1126 }
1127
1128 tf.protocol = ATA_PROT_PIO;
1129
1130 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1131 id, sizeof(id[0]) * ATA_ID_WORDS);
1132
1133 if (err_mask) {
1134 rc = -EIO;
1135 reason = "I/O error";
1136
1137 if (err_mask & ~AC_ERR_DEV)
1138 goto err_out;
1139
1140 /*
1141 * arg! EDD works for all test cases, but seems to return
1142 * the ATA signature for some ATAPI devices. Until the
1143 * reason for this is found and fixed, we fix up the mess
1144 * here. If IDENTIFY DEVICE returns command aborted
1145 * (as ATAPI devices do), then we issue an
1146 * IDENTIFY PACKET DEVICE.
1147 *
1148 * ATA software reset (SRST, the default) does not appear
1149 * to have this problem.
1150 */
1151 if ((using_edd) && (class == ATA_DEV_ATA)) {
1152 u8 err = tf.feature;
1153 if (err & ATA_ABORTED) {
1154 class = ATA_DEV_ATAPI;
1155 goto retry;
1156 }
1157 }
1158 goto err_out;
1159 }
1160
1161 swap_buf_le16(id, ATA_ID_WORDS);
1162
Tejun Heo49016ac2006-02-21 02:12:11 +09001163 /* sanity check */
1164 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1165 rc = -EINVAL;
1166 reason = "device reports illegal type";
1167 goto err_out;
1168 }
1169
1170 if (post_reset && class == ATA_DEV_ATA) {
1171 /*
1172 * The exact sequence expected by certain pre-ATA4 drives is:
1173 * SRST RESET
1174 * IDENTIFY
1175 * INITIALIZE DEVICE PARAMETERS
1176 * anything else..
1177 * Some drives were very specific about that exact sequence.
1178 */
1179 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1180 err_mask = ata_dev_init_params(ap, dev);
1181 if (err_mask) {
1182 rc = -EIO;
1183 reason = "INIT_DEV_PARAMS failed";
1184 goto err_out;
1185 }
1186
1187 /* current CHS translation info (id[53-58]) might be
1188 * changed. reread the identify device info.
1189 */
1190 post_reset = 0;
1191 goto retry;
1192 }
1193 }
1194
1195 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001196 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001197 return 0;
1198
1199 err_out:
1200 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1201 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001202 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001203 return rc;
1204}
1205
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001206static inline u8 ata_dev_knobble(const struct ata_port *ap,
1207 struct ata_device *dev)
1208{
1209 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1210}
1211
Tejun Heo49016ac2006-02-21 02:12:11 +09001212/**
Tejun Heoffeae412006-03-01 16:09:35 +09001213 * ata_dev_configure - Configure the specified ATA/ATAPI device
1214 * @ap: Port on which target device resides
1215 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001216 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 *
Tejun Heoffeae412006-03-01 16:09:35 +09001218 * Configure @dev according to @dev->id. Generic and low-level
1219 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 *
1221 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001222 * Kernel thread context (may sleep)
1223 *
1224 * RETURNS:
1225 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001227static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1228 int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Tejun Heo1148c3a2006-03-13 19:48:04 +09001230 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001231 unsigned int xfer_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001232 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (!ata_dev_present(dev)) {
1235 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001236 ap->id, dev->devno);
1237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
Tejun Heoffeae412006-03-01 16:09:35 +09001240 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001242 /* print device capabilities */
1243 if (print_info)
1244 printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
1245 "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1246 ap->id, dev->devno, id[49], id[82], id[83],
1247 id[84], id[85], id[86], id[87], id[88]);
1248
Tejun Heo208a9932006-03-05 17:55:58 +09001249 /* initialize to-be-configured parameters */
1250 dev->flags = 0;
1251 dev->max_sectors = 0;
1252 dev->cdb_len = 0;
1253 dev->n_sectors = 0;
1254 dev->cylinders = 0;
1255 dev->heads = 0;
1256 dev->sectors = 0;
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /*
1259 * common ATA, ATAPI feature tests
1260 */
1261
Tejun Heoff8854b2006-03-06 04:31:56 +09001262 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001263 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Tejun Heo1148c3a2006-03-13 19:48:04 +09001265 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /* ATA-specific feature tests */
1268 if (dev->class == ATA_DEV_ATA) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001269 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001270
Tejun Heo1148c3a2006-03-13 19:48:04 +09001271 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001272 const char *lba_desc;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001273
Tejun Heo4c2d7212006-03-05 17:55:58 +09001274 lba_desc = "LBA";
1275 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001276 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001277 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001278 lba_desc = "LBA48";
1279 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04001280
1281 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001282 if (print_info)
1283 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1284 "max %s, %Lu sectors: %s\n",
1285 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001286 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001287 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001288 (unsigned long long)dev->n_sectors,
1289 lba_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001290 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001291 /* CHS */
1292
1293 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001294 dev->cylinders = id[1];
1295 dev->heads = id[3];
1296 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001297
Tejun Heo1148c3a2006-03-13 19:48:04 +09001298 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001299 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001300 dev->cylinders = id[54];
1301 dev->heads = id[55];
1302 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001303 }
1304
1305 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001306 if (print_info)
1307 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1308 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1309 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001310 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001311 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001312 (unsigned long long)dev->n_sectors,
1313 dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315
Tejun Heo6e7846e2006-02-12 23:32:58 +09001316 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318
1319 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001320 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001321 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1323 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001324 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 goto err_out_nosup;
1326 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001327 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001330 if (print_info)
1331 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoff8854b2006-03-06 04:31:56 +09001332 ap->id, dev->devno, ata_mode_string(xfer_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334
Tejun Heo6e7846e2006-02-12 23:32:58 +09001335 ap->host->max_cmd_len = 0;
1336 for (i = 0; i < ATA_MAX_DEVICES; i++)
1337 ap->host->max_cmd_len = max_t(unsigned int,
1338 ap->host->max_cmd_len,
1339 ap->device[i].cdb_len);
1340
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001341 /* limit bridge transfers to udma5, 200 sectors */
1342 if (ata_dev_knobble(ap, dev)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001343 if (print_info)
1344 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1345 ap->id, dev->devno);
Tejun Heo5a529132006-03-24 14:07:50 +09001346 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001347 dev->max_sectors = ATA_MAX_SECTORS;
1348 }
1349
1350 if (ap->ops->dev_config)
1351 ap->ops->dev_config(ap, dev);
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356err_out_nosup:
1357 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001358 ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001360 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363/**
1364 * ata_bus_probe - Reset and probe ATA bus
1365 * @ap: Bus to probe
1366 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001367 * Master ATA bus probing function. Initiates a hardware-dependent
1368 * bus reset, then attempts to identify any devices found on
1369 * the bus.
1370 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001372 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 *
1374 * RETURNS:
1375 * Zero on success, non-zero on error.
1376 */
1377
1378static int ata_bus_probe(struct ata_port *ap)
1379{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001380 unsigned int classes[ATA_MAX_DEVICES];
1381 unsigned int i, rc, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Tejun Heo28ca5c52006-03-01 16:09:36 +09001383 ata_port_probe(ap);
1384
Tejun Heo20444702006-03-13 01:57:01 +09001385 /* reset and determine device classes */
1386 for (i = 0; i < ATA_MAX_DEVICES; i++)
1387 classes[i] = ATA_DEV_UNKNOWN;
Tejun Heo2061a472006-03-12 00:57:39 +09001388
Tejun Heo20444702006-03-13 01:57:01 +09001389 if (ap->ops->probe_reset) {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001390 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001391 if (rc) {
1392 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1393 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001394 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001395 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001396 ap->ops->phy_reset(ap);
1397
Tejun Heo20444702006-03-13 01:57:01 +09001398 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1399 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo28ca5c52006-03-01 16:09:36 +09001400 classes[i] = ap->device[i].class;
Tejun Heo20444702006-03-13 01:57:01 +09001401
Tejun Heo28ca5c52006-03-01 16:09:36 +09001402 ata_port_probe(ap);
1403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Tejun Heo20444702006-03-13 01:57:01 +09001405 for (i = 0; i < ATA_MAX_DEVICES; i++)
1406 if (classes[i] == ATA_DEV_UNKNOWN)
1407 classes[i] = ATA_DEV_NONE;
1408
Tejun Heo28ca5c52006-03-01 16:09:36 +09001409 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoffeae412006-03-01 16:09:35 +09001411 struct ata_device *dev = &ap->device[i];
1412
Tejun Heo28ca5c52006-03-01 16:09:36 +09001413 dev->class = classes[i];
1414
Tejun Heoffeae412006-03-01 16:09:35 +09001415 if (!ata_dev_present(dev))
1416 continue;
1417
1418 WARN_ON(dev->id != NULL);
1419 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1420 dev->class = ATA_DEV_NONE;
1421 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Tejun Heoffeae412006-03-01 16:09:35 +09001423
Tejun Heo4c2d7212006-03-05 17:55:58 +09001424 if (ata_dev_configure(ap, dev, 1)) {
Tejun Heoffeae412006-03-01 16:09:35 +09001425 dev->class++; /* disable device */
1426 continue;
1427 }
1428
Tejun Heoffeae412006-03-01 16:09:35 +09001429 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
Tejun Heo28ca5c52006-03-01 16:09:36 +09001432 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 goto err_out_disable;
1434
1435 ata_set_mode(ap);
1436 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1437 goto err_out_disable;
1438
1439 return 0;
1440
1441err_out_disable:
1442 ap->ops->port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return -1;
1444}
1445
1446/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001447 * ata_port_probe - Mark port as enabled
1448 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001450 * Modify @ap data structure such that the system
1451 * thinks that the entire port is enabled.
1452 *
1453 * LOCKING: host_set lock, or some other form of
1454 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
1456
1457void ata_port_probe(struct ata_port *ap)
1458{
1459 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1460}
1461
1462/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001463 * sata_print_link_status - Print SATA link status
1464 * @ap: SATA port to printk link status about
1465 *
1466 * This function prints link speed and status of a SATA link.
1467 *
1468 * LOCKING:
1469 * None.
1470 */
1471static void sata_print_link_status(struct ata_port *ap)
1472{
1473 u32 sstatus, tmp;
1474 const char *speed;
1475
1476 if (!ap->ops->scr_read)
1477 return;
1478
1479 sstatus = scr_read(ap, SCR_STATUS);
1480
1481 if (sata_dev_present(ap)) {
1482 tmp = (sstatus >> 4) & 0xf;
1483 if (tmp & (1 << 0))
1484 speed = "1.5";
1485 else if (tmp & (1 << 1))
1486 speed = "3.0";
1487 else
1488 speed = "<unknown>";
1489 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1490 ap->id, speed, sstatus);
1491 } else {
1492 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1493 ap->id, sstatus);
1494 }
1495}
1496
1497/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001498 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1499 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001501 * This function issues commands to standard SATA Sxxx
1502 * PHY registers, to wake up the phy (and device), and
1503 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 *
1505 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001506 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 *
1508 */
1509void __sata_phy_reset(struct ata_port *ap)
1510{
1511 u32 sstatus;
1512 unsigned long timeout = jiffies + (HZ * 5);
1513
1514 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001515 /* issue phy wake/reset */
1516 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001517 /* Couldn't find anything in SATA I/II specs, but
1518 * AHCI-1.1 10.4.2 says at least 1 ms. */
1519 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001521 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 /* wait for phy to become ready, if necessary */
1524 do {
1525 msleep(200);
1526 sstatus = scr_read(ap, SCR_STATUS);
1527 if ((sstatus & 0xf) != 1)
1528 break;
1529 } while (time_before(jiffies, timeout));
1530
Tejun Heo3be680b2005-12-19 22:35:02 +09001531 /* print link status */
1532 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001533
Tejun Heo3be680b2005-12-19 22:35:02 +09001534 /* TODO: phy layer with polling, timeouts, etc. */
1535 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001537 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1541 return;
1542
1543 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1544 ata_port_disable(ap);
1545 return;
1546 }
1547
1548 ap->cbl = ATA_CBL_SATA;
1549}
1550
1551/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001552 * sata_phy_reset - Reset SATA bus.
1553 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001555 * This function resets the SATA bus, and then probes
1556 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 *
1558 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001559 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 *
1561 */
1562void sata_phy_reset(struct ata_port *ap)
1563{
1564 __sata_phy_reset(ap);
1565 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1566 return;
1567 ata_bus_reset(ap);
1568}
1569
1570/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001571 * ata_port_disable - Disable port.
1572 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001574 * Modify @ap data structure such that the system
1575 * thinks that the entire port is disabled, and should
1576 * never attempt to probe or communicate with devices
1577 * on this port.
1578 *
1579 * LOCKING: host_set lock, or some other form of
1580 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 */
1582
1583void ata_port_disable(struct ata_port *ap)
1584{
1585 ap->device[0].class = ATA_DEV_NONE;
1586 ap->device[1].class = ATA_DEV_NONE;
1587 ap->flags |= ATA_FLAG_PORT_DISABLED;
1588}
1589
Alan Cox452503f2005-10-21 19:01:32 -04001590/*
1591 * This mode timing computation functionality is ported over from
1592 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1593 */
1594/*
1595 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1596 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1597 * for PIO 5, which is a nonstandard extension and UDMA6, which
1598 * is currently supported only by Maxtor drives.
1599 */
1600
1601static const struct ata_timing ata_timing[] = {
1602
1603 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1604 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1605 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1606 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1607
1608 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1609 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1610 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1611
1612/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1613
1614 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1615 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1616 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1617
1618 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1619 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1620 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1621
1622/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1623 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1624 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1625
1626 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1627 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1628 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1629
1630/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1631
1632 { 0xFF }
1633};
1634
1635#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1636#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1637
1638static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1639{
1640 q->setup = EZ(t->setup * 1000, T);
1641 q->act8b = EZ(t->act8b * 1000, T);
1642 q->rec8b = EZ(t->rec8b * 1000, T);
1643 q->cyc8b = EZ(t->cyc8b * 1000, T);
1644 q->active = EZ(t->active * 1000, T);
1645 q->recover = EZ(t->recover * 1000, T);
1646 q->cycle = EZ(t->cycle * 1000, T);
1647 q->udma = EZ(t->udma * 1000, UT);
1648}
1649
1650void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1651 struct ata_timing *m, unsigned int what)
1652{
1653 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1654 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1655 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1656 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1657 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1658 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1659 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1660 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1661}
1662
1663static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1664{
1665 const struct ata_timing *t;
1666
1667 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001668 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001669 return NULL;
1670 return t;
1671}
1672
1673int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1674 struct ata_timing *t, int T, int UT)
1675{
1676 const struct ata_timing *s;
1677 struct ata_timing p;
1678
1679 /*
1680 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001681 */
Alan Cox452503f2005-10-21 19:01:32 -04001682
1683 if (!(s = ata_timing_find_mode(speed)))
1684 return -EINVAL;
1685
Albert Lee75b1f2f2005-11-16 17:06:18 +08001686 memcpy(t, s, sizeof(*s));
1687
Alan Cox452503f2005-10-21 19:01:32 -04001688 /*
1689 * If the drive is an EIDE drive, it can tell us it needs extended
1690 * PIO/MW_DMA cycle timing.
1691 */
1692
1693 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1694 memset(&p, 0, sizeof(p));
1695 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1696 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1697 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1698 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1699 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1700 }
1701 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1702 }
1703
1704 /*
1705 * Convert the timing to bus clock counts.
1706 */
1707
Albert Lee75b1f2f2005-11-16 17:06:18 +08001708 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001709
1710 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001711 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1712 * S.M.A.R.T * and some other commands. We have to ensure that the
1713 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001714 */
1715
1716 if (speed > XFER_PIO_4) {
1717 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1718 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1719 }
1720
1721 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001722 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001723 */
1724
1725 if (t->act8b + t->rec8b < t->cyc8b) {
1726 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1727 t->rec8b = t->cyc8b - t->act8b;
1728 }
1729
1730 if (t->active + t->recover < t->cycle) {
1731 t->active += (t->cycle - (t->active + t->recover)) / 2;
1732 t->recover = t->cycle - t->active;
1733 }
1734
1735 return 0;
1736}
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1739{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1741 return;
1742
1743 if (dev->xfer_shift == ATA_SHIFT_PIO)
1744 dev->flags |= ATA_DFLAG_PIO;
1745
1746 ata_dev_set_xfermode(ap, dev);
1747
Tejun Heo48a8a142006-03-05 17:55:58 +09001748 if (ata_dev_revalidate(ap, dev, 0)) {
1749 printk(KERN_ERR "ata%u: failed to revalidate after set "
1750 "xfermode, disabled\n", ap->id);
1751 ata_port_disable(ap);
1752 }
1753
Tejun Heo23e71c32006-03-06 04:31:57 +09001754 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1755 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
Tejun Heo23e71c32006-03-06 04:31:57 +09001758 ap->id, dev->devno,
1759 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
1762static int ata_host_set_pio(struct ata_port *ap)
1763{
Tejun Heoa6d5a512006-03-06 04:31:57 +09001764 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1767 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001768
1769 if (!ata_dev_present(dev))
1770 continue;
1771
1772 if (!dev->pio_mode) {
Alan Cox88f93a32006-03-21 16:04:12 +00001773 printk(KERN_WARNING "ata%u: no PIO support for device %d.\n", ap->id, i);
Tejun Heoa6d5a512006-03-06 04:31:57 +09001774 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09001776
1777 dev->xfer_mode = dev->pio_mode;
1778 dev->xfer_shift = ATA_SHIFT_PIO;
1779 if (ap->ops->set_piomode)
1780 ap->ops->set_piomode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782
1783 return 0;
1784}
1785
Tejun Heoa6d5a512006-03-06 04:31:57 +09001786static void ata_host_set_dma(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 int i;
1789
1790 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1791 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001792
1793 if (!ata_dev_present(dev) || !dev->dma_mode)
1794 continue;
1795
1796 dev->xfer_mode = dev->dma_mode;
1797 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1798 if (ap->ops->set_dmamode)
1799 ap->ops->set_dmamode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801}
1802
1803/**
1804 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1805 * @ap: port on which timings will be programmed
1806 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001807 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1808 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001810 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 */
1812static void ata_set_mode(struct ata_port *ap)
1813{
Tejun Heoa6d5a512006-03-06 04:31:57 +09001814 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Tejun Heoa6d5a512006-03-06 04:31:57 +09001816 /* step 1: calculate xfer_mask */
1817 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1818 struct ata_device *dev = &ap->device[i];
Tejun Heoacf356b2006-03-24 14:07:50 +09001819 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09001820
1821 if (!ata_dev_present(dev))
1822 continue;
1823
Tejun Heoacf356b2006-03-24 14:07:50 +09001824 ata_dev_xfermask(ap, dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09001825
Tejun Heoacf356b2006-03-24 14:07:50 +09001826 /* TODO: let LLDD filter dev->*_mask here */
1827
1828 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
1829 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
1830 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
1831 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Tejun Heoa6d5a512006-03-06 04:31:57 +09001832 }
1833
1834 /* step 2: always set host PIO timings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 rc = ata_host_set_pio(ap);
1836 if (rc)
1837 goto err_out;
1838
Tejun Heoa6d5a512006-03-06 04:31:57 +09001839 /* step 3: set host DMA timings */
1840 ata_host_set_dma(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /* step 4: update devices' xfer mode */
Tejun Heoa6d5a512006-03-06 04:31:57 +09001843 for (i = 0; i < ATA_MAX_DEVICES; i++)
1844 ata_dev_set_mode(ap, &ap->device[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1847 return;
1848
1849 if (ap->ops->post_set_mode)
1850 ap->ops->post_set_mode(ap);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return;
1853
1854err_out:
1855 ata_port_disable(ap);
1856}
1857
1858/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001859 * ata_tf_to_host - issue ATA taskfile to host controller
1860 * @ap: port to which command is being issued
1861 * @tf: ATA taskfile register set
1862 *
1863 * Issues ATA taskfile register set to ATA host controller,
1864 * with proper synchronization with interrupt handler and
1865 * other threads.
1866 *
1867 * LOCKING:
1868 * spin_lock_irqsave(host_set lock)
1869 */
1870
1871static inline void ata_tf_to_host(struct ata_port *ap,
1872 const struct ata_taskfile *tf)
1873{
1874 ap->ops->tf_load(ap, tf);
1875 ap->ops->exec_command(ap, tf);
1876}
1877
1878/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 * ata_busy_sleep - sleep until BSY clears, or timeout
1880 * @ap: port containing status register to be polled
1881 * @tmout_pat: impatience timeout
1882 * @tmout: overall timeout
1883 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001884 * Sleep until ATA Status register bit BSY clears,
1885 * or a timeout occurs.
1886 *
1887 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
1889
Tejun Heo6f8b9952006-01-24 17:05:21 +09001890unsigned int ata_busy_sleep (struct ata_port *ap,
1891 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 unsigned long timer_start, timeout;
1894 u8 status;
1895
1896 status = ata_busy_wait(ap, ATA_BUSY, 300);
1897 timer_start = jiffies;
1898 timeout = timer_start + tmout_pat;
1899 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1900 msleep(50);
1901 status = ata_busy_wait(ap, ATA_BUSY, 3);
1902 }
1903
1904 if (status & ATA_BUSY)
1905 printk(KERN_WARNING "ata%u is slow to respond, "
1906 "please be patient\n", ap->id);
1907
1908 timeout = timer_start + tmout;
1909 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1910 msleep(50);
1911 status = ata_chk_status(ap);
1912 }
1913
1914 if (status & ATA_BUSY) {
1915 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1916 ap->id, tmout / HZ);
1917 return 1;
1918 }
1919
1920 return 0;
1921}
1922
1923static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1924{
1925 struct ata_ioports *ioaddr = &ap->ioaddr;
1926 unsigned int dev0 = devmask & (1 << 0);
1927 unsigned int dev1 = devmask & (1 << 1);
1928 unsigned long timeout;
1929
1930 /* if device 0 was found in ata_devchk, wait for its
1931 * BSY bit to clear
1932 */
1933 if (dev0)
1934 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1935
1936 /* if device 1 was found in ata_devchk, wait for
1937 * register access, then wait for BSY to clear
1938 */
1939 timeout = jiffies + ATA_TMOUT_BOOT;
1940 while (dev1) {
1941 u8 nsect, lbal;
1942
1943 ap->ops->dev_select(ap, 1);
1944 if (ap->flags & ATA_FLAG_MMIO) {
1945 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1946 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1947 } else {
1948 nsect = inb(ioaddr->nsect_addr);
1949 lbal = inb(ioaddr->lbal_addr);
1950 }
1951 if ((nsect == 1) && (lbal == 1))
1952 break;
1953 if (time_after(jiffies, timeout)) {
1954 dev1 = 0;
1955 break;
1956 }
1957 msleep(50); /* give drive a breather */
1958 }
1959 if (dev1)
1960 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1961
1962 /* is all this really necessary? */
1963 ap->ops->dev_select(ap, 0);
1964 if (dev1)
1965 ap->ops->dev_select(ap, 1);
1966 if (dev0)
1967 ap->ops->dev_select(ap, 0);
1968}
1969
1970/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001971 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1972 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001974 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1975 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 *
1977 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001978 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001979 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 *
1981 */
1982
1983static unsigned int ata_bus_edd(struct ata_port *ap)
1984{
1985 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001986 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 /* set up execute-device-diag (bus reset) taskfile */
1989 /* also, take interrupts to a known state (disabled) */
1990 DPRINTK("execute-device-diag\n");
1991 ata_tf_init(ap, &tf, 0);
1992 tf.ctl |= ATA_NIEN;
1993 tf.command = ATA_CMD_EDD;
1994 tf.protocol = ATA_PROT_NODATA;
1995
1996 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001997 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001999 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /* spec says at least 2ms. but who knows with those
2002 * crazy ATAPI devices...
2003 */
2004 msleep(150);
2005
2006 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2007}
2008
2009static unsigned int ata_bus_softreset(struct ata_port *ap,
2010 unsigned int devmask)
2011{
2012 struct ata_ioports *ioaddr = &ap->ioaddr;
2013
2014 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2015
2016 /* software reset. causes dev0 to be selected */
2017 if (ap->flags & ATA_FLAG_MMIO) {
2018 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2019 udelay(20); /* FIXME: flush */
2020 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2021 udelay(20); /* FIXME: flush */
2022 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2023 } else {
2024 outb(ap->ctl, ioaddr->ctl_addr);
2025 udelay(10);
2026 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2027 udelay(10);
2028 outb(ap->ctl, ioaddr->ctl_addr);
2029 }
2030
2031 /* spec mandates ">= 2ms" before checking status.
2032 * We wait 150ms, because that was the magic delay used for
2033 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2034 * between when the ATA command register is written, and then
2035 * status is checked. Because waiting for "a while" before
2036 * checking status is fine, post SRST, we perform this magic
2037 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00002038 *
2039 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 */
2041 msleep(150);
2042
Alan Cox09c7ad72006-03-22 15:52:40 +00002043
2044 /* Before we perform post reset processing we want to see if
2045 the bus shows 0xFF because the odd clown forgets the D7 pulldown
2046 resistor */
2047
2048 if (ata_check_status(ap) == 0xFF)
2049 return 1; /* Positive is failure for some reason */
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 ata_bus_post_reset(ap, devmask);
2052
2053 return 0;
2054}
2055
2056/**
2057 * ata_bus_reset - reset host port and associated ATA channel
2058 * @ap: port to reset
2059 *
2060 * This is typically the first time we actually start issuing
2061 * commands to the ATA channel. We wait for BSY to clear, then
2062 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2063 * result. Determine what devices, if any, are on the channel
2064 * by looking at the device 0/1 error register. Look at the signature
2065 * stored in each device's taskfile registers, to determine if
2066 * the device is ATA or ATAPI.
2067 *
2068 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002069 * PCI/etc. bus probe sem.
2070 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 *
2072 * SIDE EFFECTS:
2073 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2074 */
2075
2076void ata_bus_reset(struct ata_port *ap)
2077{
2078 struct ata_ioports *ioaddr = &ap->ioaddr;
2079 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2080 u8 err;
2081 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2082
2083 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2084
2085 /* determine if device 0/1 are present */
2086 if (ap->flags & ATA_FLAG_SATA_RESET)
2087 dev0 = 1;
2088 else {
2089 dev0 = ata_devchk(ap, 0);
2090 if (slave_possible)
2091 dev1 = ata_devchk(ap, 1);
2092 }
2093
2094 if (dev0)
2095 devmask |= (1 << 0);
2096 if (dev1)
2097 devmask |= (1 << 1);
2098
2099 /* select device 0 again */
2100 ap->ops->dev_select(ap, 0);
2101
2102 /* issue bus reset */
2103 if (ap->flags & ATA_FLAG_SRST)
2104 rc = ata_bus_softreset(ap, devmask);
2105 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2106 /* set up device control */
2107 if (ap->flags & ATA_FLAG_MMIO)
2108 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2109 else
2110 outb(ap->ctl, ioaddr->ctl_addr);
2111 rc = ata_bus_edd(ap);
2112 }
2113
2114 if (rc)
2115 goto err_out;
2116
2117 /*
2118 * determine by signature whether we have ATA or ATAPI devices
2119 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002120 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002122 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 /* re-enable interrupts */
2125 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2126 ata_irq_on(ap);
2127
2128 /* is double-select really necessary? */
2129 if (ap->device[1].class != ATA_DEV_NONE)
2130 ap->ops->dev_select(ap, 1);
2131 if (ap->device[0].class != ATA_DEV_NONE)
2132 ap->ops->dev_select(ap, 0);
2133
2134 /* if no devices were detected, disable this port */
2135 if ((ap->device[0].class == ATA_DEV_NONE) &&
2136 (ap->device[1].class == ATA_DEV_NONE))
2137 goto err_out;
2138
2139 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2140 /* set up device control for ATA_FLAG_SATA_RESET */
2141 if (ap->flags & ATA_FLAG_MMIO)
2142 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2143 else
2144 outb(ap->ctl, ioaddr->ctl_addr);
2145 }
2146
2147 DPRINTK("EXIT\n");
2148 return;
2149
2150err_out:
2151 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2152 ap->ops->port_disable(ap);
2153
2154 DPRINTK("EXIT\n");
2155}
2156
Tejun Heo7a7921e2006-02-02 18:20:00 +09002157static int sata_phy_resume(struct ata_port *ap)
2158{
2159 unsigned long timeout = jiffies + (HZ * 5);
2160 u32 sstatus;
2161
2162 scr_write_flush(ap, SCR_CONTROL, 0x300);
2163
2164 /* Wait for phy to become ready, if necessary. */
2165 do {
2166 msleep(200);
2167 sstatus = scr_read(ap, SCR_STATUS);
2168 if ((sstatus & 0xf) != 1)
2169 return 0;
2170 } while (time_before(jiffies, timeout));
2171
2172 return -1;
2173}
2174
Tejun Heoc2bd5802006-01-24 17:05:22 +09002175/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002176 * ata_std_probeinit - initialize probing
2177 * @ap: port to be probed
2178 *
2179 * @ap is about to be probed. Initialize it. This function is
2180 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002181 *
2182 * NOTE!!! Do not use this function as probeinit if a low level
2183 * driver implements only hardreset. Just pass NULL as probeinit
2184 * in that case. Using this function is probably okay but doing
2185 * so makes reset sequence different from the original
2186 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002187 */
2188extern void ata_std_probeinit(struct ata_port *ap)
2189{
Tejun Heo3a397462006-02-10 23:58:48 +09002190 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002191 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002192 if (sata_dev_present(ap))
2193 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2194 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002195}
2196
2197/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002198 * ata_std_softreset - reset host port via ATA SRST
2199 * @ap: port to reset
2200 * @verbose: fail verbosely
2201 * @classes: resulting classes of attached devices
2202 *
2203 * Reset host port using ATA SRST. This function is to be used
2204 * as standard callback for ata_drive_*_reset() functions.
2205 *
2206 * LOCKING:
2207 * Kernel thread context (may sleep)
2208 *
2209 * RETURNS:
2210 * 0 on success, -errno otherwise.
2211 */
2212int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2213{
2214 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2215 unsigned int devmask = 0, err_mask;
2216 u8 err;
2217
2218 DPRINTK("ENTER\n");
2219
Tejun Heo3a397462006-02-10 23:58:48 +09002220 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2221 classes[0] = ATA_DEV_NONE;
2222 goto out;
2223 }
2224
Tejun Heoc2bd5802006-01-24 17:05:22 +09002225 /* determine if device 0/1 are present */
2226 if (ata_devchk(ap, 0))
2227 devmask |= (1 << 0);
2228 if (slave_possible && ata_devchk(ap, 1))
2229 devmask |= (1 << 1);
2230
Tejun Heoc2bd5802006-01-24 17:05:22 +09002231 /* select device 0 again */
2232 ap->ops->dev_select(ap, 0);
2233
2234 /* issue bus reset */
2235 DPRINTK("about to softreset, devmask=%x\n", devmask);
2236 err_mask = ata_bus_softreset(ap, devmask);
2237 if (err_mask) {
2238 if (verbose)
2239 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2240 ap->id, err_mask);
2241 else
2242 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2243 err_mask);
2244 return -EIO;
2245 }
2246
2247 /* determine by signature whether we have ATA or ATAPI devices */
2248 classes[0] = ata_dev_try_classify(ap, 0, &err);
2249 if (slave_possible && err != 0x81)
2250 classes[1] = ata_dev_try_classify(ap, 1, &err);
2251
Tejun Heo3a397462006-02-10 23:58:48 +09002252 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002253 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2254 return 0;
2255}
2256
2257/**
2258 * sata_std_hardreset - reset host port via SATA phy reset
2259 * @ap: port to reset
2260 * @verbose: fail verbosely
2261 * @class: resulting class of attached device
2262 *
2263 * SATA phy-reset host port using DET bits of SControl register.
2264 * This function is to be used as standard callback for
2265 * ata_drive_*_reset().
2266 *
2267 * LOCKING:
2268 * Kernel thread context (may sleep)
2269 *
2270 * RETURNS:
2271 * 0 on success, -errno otherwise.
2272 */
2273int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2274{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002275 DPRINTK("ENTER\n");
2276
2277 /* Issue phy wake/reset */
2278 scr_write_flush(ap, SCR_CONTROL, 0x301);
2279
2280 /*
2281 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2282 * 10.4.2 says at least 1 ms.
2283 */
2284 msleep(1);
2285
Tejun Heo7a7921e2006-02-02 18:20:00 +09002286 /* Bring phy back */
2287 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002288
Tejun Heoc2bd5802006-01-24 17:05:22 +09002289 /* TODO: phy layer with polling, timeouts, etc. */
2290 if (!sata_dev_present(ap)) {
2291 *class = ATA_DEV_NONE;
2292 DPRINTK("EXIT, link offline\n");
2293 return 0;
2294 }
2295
2296 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2297 if (verbose)
2298 printk(KERN_ERR "ata%u: COMRESET failed "
2299 "(device not ready)\n", ap->id);
2300 else
2301 DPRINTK("EXIT, device not ready\n");
2302 return -EIO;
2303 }
2304
Tejun Heo3a397462006-02-10 23:58:48 +09002305 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2306
Tejun Heoc2bd5802006-01-24 17:05:22 +09002307 *class = ata_dev_try_classify(ap, 0, NULL);
2308
2309 DPRINTK("EXIT, class=%u\n", *class);
2310 return 0;
2311}
2312
2313/**
2314 * ata_std_postreset - standard postreset callback
2315 * @ap: the target ata_port
2316 * @classes: classes of attached devices
2317 *
2318 * This function is invoked after a successful reset. Note that
2319 * the device might have been reset more than once using
2320 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002321 *
2322 * This function is to be used as standard callback for
2323 * ata_drive_*_reset().
2324 *
2325 * LOCKING:
2326 * Kernel thread context (may sleep)
2327 */
2328void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2329{
2330 DPRINTK("ENTER\n");
2331
Tejun Heo56497bd2006-02-15 15:01:42 +09002332 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002333 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2334 ap->cbl = ATA_CBL_SATA;
2335
2336 /* print link status */
2337 if (ap->cbl == ATA_CBL_SATA)
2338 sata_print_link_status(ap);
2339
Tejun Heo3a397462006-02-10 23:58:48 +09002340 /* re-enable interrupts */
2341 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2342 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002343
2344 /* is double-select really necessary? */
2345 if (classes[0] != ATA_DEV_NONE)
2346 ap->ops->dev_select(ap, 1);
2347 if (classes[1] != ATA_DEV_NONE)
2348 ap->ops->dev_select(ap, 0);
2349
Tejun Heo3a397462006-02-10 23:58:48 +09002350 /* bail out if no device is present */
2351 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2352 DPRINTK("EXIT, no device\n");
2353 return;
2354 }
2355
2356 /* set up device control */
2357 if (ap->ioaddr.ctl_addr) {
2358 if (ap->flags & ATA_FLAG_MMIO)
2359 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2360 else
2361 outb(ap->ctl, ap->ioaddr.ctl_addr);
2362 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002363
2364 DPRINTK("EXIT\n");
2365}
2366
2367/**
2368 * ata_std_probe_reset - standard probe reset method
2369 * @ap: prot to perform probe-reset
2370 * @classes: resulting classes of attached devices
2371 *
2372 * The stock off-the-shelf ->probe_reset method.
2373 *
2374 * LOCKING:
2375 * Kernel thread context (may sleep)
2376 *
2377 * RETURNS:
2378 * 0 on success, -errno otherwise.
2379 */
2380int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2381{
2382 ata_reset_fn_t hardreset;
2383
2384 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002385 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002386 hardreset = sata_std_hardreset;
2387
Tejun Heo8a19ac82006-02-02 18:20:00 +09002388 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002389 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002390 ata_std_postreset, classes);
2391}
2392
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002393static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2394 ata_postreset_fn_t postreset,
2395 unsigned int *classes)
2396{
2397 int i, rc;
2398
2399 for (i = 0; i < ATA_MAX_DEVICES; i++)
2400 classes[i] = ATA_DEV_UNKNOWN;
2401
2402 rc = reset(ap, 0, classes);
2403 if (rc)
2404 return rc;
2405
2406 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2407 * is complete and convert all ATA_DEV_UNKNOWN to
2408 * ATA_DEV_NONE.
2409 */
2410 for (i = 0; i < ATA_MAX_DEVICES; i++)
2411 if (classes[i] != ATA_DEV_UNKNOWN)
2412 break;
2413
2414 if (i < ATA_MAX_DEVICES)
2415 for (i = 0; i < ATA_MAX_DEVICES; i++)
2416 if (classes[i] == ATA_DEV_UNKNOWN)
2417 classes[i] = ATA_DEV_NONE;
2418
2419 if (postreset)
2420 postreset(ap, classes);
2421
2422 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2423}
2424
2425/**
2426 * ata_drive_probe_reset - Perform probe reset with given methods
2427 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002428 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002429 * @softreset: softreset method (can be NULL)
2430 * @hardreset: hardreset method (can be NULL)
2431 * @postreset: postreset method (can be NULL)
2432 * @classes: resulting classes of attached devices
2433 *
2434 * Reset the specified port and classify attached devices using
2435 * given methods. This function prefers softreset but tries all
2436 * possible reset sequences to reset and classify devices. This
2437 * function is intended to be used for constructing ->probe_reset
2438 * callback by low level drivers.
2439 *
2440 * Reset methods should follow the following rules.
2441 *
2442 * - Return 0 on sucess, -errno on failure.
2443 * - If classification is supported, fill classes[] with
2444 * recognized class codes.
2445 * - If classification is not supported, leave classes[] alone.
2446 * - If verbose is non-zero, print error message on failure;
2447 * otherwise, shut up.
2448 *
2449 * LOCKING:
2450 * Kernel thread context (may sleep)
2451 *
2452 * RETURNS:
2453 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2454 * if classification fails, and any error code from reset
2455 * methods.
2456 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002457int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002458 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2459 ata_postreset_fn_t postreset, unsigned int *classes)
2460{
2461 int rc = -EINVAL;
2462
Tejun Heo7944ea92006-02-02 18:20:00 +09002463 if (probeinit)
2464 probeinit(ap);
2465
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002466 if (softreset) {
2467 rc = do_probe_reset(ap, softreset, postreset, classes);
2468 if (rc == 0)
2469 return 0;
2470 }
2471
2472 if (!hardreset)
2473 return rc;
2474
2475 rc = do_probe_reset(ap, hardreset, postreset, classes);
2476 if (rc == 0 || rc != -ENODEV)
2477 return rc;
2478
2479 if (softreset)
2480 rc = do_probe_reset(ap, softreset, postreset, classes);
2481
2482 return rc;
2483}
2484
Tejun Heo623a3122006-03-05 17:55:58 +09002485/**
2486 * ata_dev_same_device - Determine whether new ID matches configured device
2487 * @ap: port on which the device to compare against resides
2488 * @dev: device to compare against
2489 * @new_class: class of the new device
2490 * @new_id: IDENTIFY page of the new device
2491 *
2492 * Compare @new_class and @new_id against @dev and determine
2493 * whether @dev is the device indicated by @new_class and
2494 * @new_id.
2495 *
2496 * LOCKING:
2497 * None.
2498 *
2499 * RETURNS:
2500 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2501 */
2502static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2503 unsigned int new_class, const u16 *new_id)
2504{
2505 const u16 *old_id = dev->id;
2506 unsigned char model[2][41], serial[2][21];
2507 u64 new_n_sectors;
2508
2509 if (dev->class != new_class) {
2510 printk(KERN_INFO
2511 "ata%u: dev %u class mismatch %d != %d\n",
2512 ap->id, dev->devno, dev->class, new_class);
2513 return 0;
2514 }
2515
2516 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2517 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2518 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2519 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2520 new_n_sectors = ata_id_n_sectors(new_id);
2521
2522 if (strcmp(model[0], model[1])) {
2523 printk(KERN_INFO
2524 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2525 ap->id, dev->devno, model[0], model[1]);
2526 return 0;
2527 }
2528
2529 if (strcmp(serial[0], serial[1])) {
2530 printk(KERN_INFO
2531 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2532 ap->id, dev->devno, serial[0], serial[1]);
2533 return 0;
2534 }
2535
2536 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2537 printk(KERN_INFO
2538 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2539 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2540 (unsigned long long)new_n_sectors);
2541 return 0;
2542 }
2543
2544 return 1;
2545}
2546
2547/**
2548 * ata_dev_revalidate - Revalidate ATA device
2549 * @ap: port on which the device to revalidate resides
2550 * @dev: device to revalidate
2551 * @post_reset: is this revalidation after reset?
2552 *
2553 * Re-read IDENTIFY page and make sure @dev is still attached to
2554 * the port.
2555 *
2556 * LOCKING:
2557 * Kernel thread context (may sleep)
2558 *
2559 * RETURNS:
2560 * 0 on success, negative errno otherwise
2561 */
2562int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2563 int post_reset)
2564{
2565 unsigned int class;
2566 u16 *id;
2567 int rc;
2568
2569 if (!ata_dev_present(dev))
2570 return -ENODEV;
2571
2572 class = dev->class;
2573 id = NULL;
2574
2575 /* allocate & read ID data */
2576 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2577 if (rc)
2578 goto fail;
2579
2580 /* is the device still there? */
2581 if (!ata_dev_same_device(ap, dev, class, id)) {
2582 rc = -ENODEV;
2583 goto fail;
2584 }
2585
2586 kfree(dev->id);
2587 dev->id = id;
2588
2589 /* configure device according to the new ID */
2590 return ata_dev_configure(ap, dev, 0);
2591
2592 fail:
2593 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2594 ap->id, dev->devno, rc);
2595 kfree(id);
2596 return rc;
2597}
2598
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002599static const char * const ata_dma_blacklist [] = {
Alan Coxf4b15fe2006-03-22 15:54:04 +00002600 "WDC AC11000H", NULL,
2601 "WDC AC22100H", NULL,
2602 "WDC AC32500H", NULL,
2603 "WDC AC33100H", NULL,
2604 "WDC AC31600H", NULL,
2605 "WDC AC32100H", "24.09P07",
2606 "WDC AC23200L", "21.10N21",
2607 "Compaq CRD-8241B", NULL,
2608 "CRD-8400B", NULL,
2609 "CRD-8480B", NULL,
2610 "CRD-8482B", NULL,
2611 "CRD-84", NULL,
2612 "SanDisk SDP3B", NULL,
2613 "SanDisk SDP3B-64", NULL,
2614 "SANYO CD-ROM CRD", NULL,
2615 "HITACHI CDR-8", NULL,
2616 "HITACHI CDR-8335", NULL,
2617 "HITACHI CDR-8435", NULL,
2618 "Toshiba CD-ROM XM-6202B", NULL,
2619 "TOSHIBA CD-ROM XM-1702BC", NULL,
2620 "CD-532E-A", NULL,
2621 "E-IDE CD-ROM CR-840", NULL,
2622 "CD-ROM Drive/F5A", NULL,
2623 "WPI CDD-820", NULL,
2624 "SAMSUNG CD-ROM SC-148C", NULL,
2625 "SAMSUNG CD-ROM SC", NULL,
2626 "SanDisk SDP3B-64", NULL,
2627 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2628 "_NEC DV5800A", NULL,
2629 "SAMSUNG CD-ROM SN-124", "N001"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630};
Alan Coxf4b15fe2006-03-22 15:54:04 +00002631
2632static int ata_strim(char *s, size_t len)
2633{
2634 len = strnlen(s, len);
2635
2636 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2637 while ((len > 0) && (s[len - 1] == ' ')) {
2638 len--;
2639 s[len] = 0;
2640 }
2641 return len;
2642}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Jeff Garzik057ace52005-10-22 14:27:05 -04002644static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
Alan Coxf4b15fe2006-03-22 15:54:04 +00002646 unsigned char model_num[40];
2647 unsigned char model_rev[16];
2648 unsigned int nlen, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 int i;
2650
Alan Coxf4b15fe2006-03-22 15:54:04 +00002651 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2652 sizeof(model_num));
2653 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
2654 sizeof(model_rev));
2655 nlen = ata_strim(model_num, sizeof(model_num));
2656 rlen = ata_strim(model_rev, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Alan Coxf4b15fe2006-03-22 15:54:04 +00002658 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
2659 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
2660 if (ata_dma_blacklist[i+1] == NULL)
2661 return 1;
2662 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
2663 return 1;
2664 }
2665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return 0;
2667}
2668
Tejun Heoa6d5a512006-03-06 04:31:57 +09002669/**
2670 * ata_dev_xfermask - Compute supported xfermask of the given device
2671 * @ap: Port on which the device to compute xfermask for resides
2672 * @dev: Device to compute xfermask for
2673 *
Tejun Heoacf356b2006-03-24 14:07:50 +09002674 * Compute supported xfermask of @dev and store it in
2675 * dev->*_mask. This function is responsible for applying all
2676 * known limits including host controller limits, device
2677 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09002678 *
2679 * LOCKING:
2680 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09002681 */
Tejun Heoacf356b2006-03-24 14:07:50 +09002682static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Tejun Heoa6d5a512006-03-06 04:31:57 +09002684 unsigned long xfer_mask;
2685 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Tejun Heoa6d5a512006-03-06 04:31:57 +09002687 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2688 ap->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Tejun Heoa6d5a512006-03-06 04:31:57 +09002690 /* use port-wide xfermask for now */
2691 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2692 struct ata_device *d = &ap->device[i];
2693 if (!ata_dev_present(d))
2694 continue;
Tejun Heoacf356b2006-03-24 14:07:50 +09002695 xfer_mask &= ata_pack_xfermask(d->pio_mask, d->mwdma_mask,
2696 d->udma_mask);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002697 xfer_mask &= ata_id_xfermask(d->id);
2698 if (ata_dma_blacklisted(d))
2699 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
2701
Tejun Heoa6d5a512006-03-06 04:31:57 +09002702 if (ata_dma_blacklisted(dev))
2703 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2704 "disabling DMA\n", ap->id, dev->devno);
2705
Tejun Heoacf356b2006-03-24 14:07:50 +09002706 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2707 &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2712 * @ap: Port associated with device @dev
2713 * @dev: Device to which command will be sent
2714 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002715 * Issue SET FEATURES - XFER MODE command to device @dev
2716 * on port @ap.
2717 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002719 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 */
2721
2722static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2723{
Tejun Heoa0123702005-12-13 14:49:31 +09002724 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 /* set up set-features taskfile */
2727 DPRINTK("set features - xfer mode\n");
2728
Tejun Heoa0123702005-12-13 14:49:31 +09002729 ata_tf_init(ap, &tf, dev->devno);
2730 tf.command = ATA_CMD_SET_FEATURES;
2731 tf.feature = SETFEATURES_XFER;
2732 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2733 tf.protocol = ATA_PROT_NODATA;
2734 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Tejun Heoa0123702005-12-13 14:49:31 +09002736 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2737 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2738 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 DPRINTK("EXIT\n");
2743}
2744
2745/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002746 * ata_dev_init_params - Issue INIT DEV PARAMS command
2747 * @ap: Port associated with device @dev
2748 * @dev: Device to which command will be sent
2749 *
2750 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002751 * Kernel thread context (may sleep)
2752 *
2753 * RETURNS:
2754 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04002755 */
2756
Tejun Heo6aff8f12006-02-15 18:24:09 +09002757static unsigned int ata_dev_init_params(struct ata_port *ap,
2758 struct ata_device *dev)
Albert Lee8bf62ece2005-05-12 15:29:42 -04002759{
Tejun Heoa0123702005-12-13 14:49:31 +09002760 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002761 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002762 u16 sectors = dev->id[6];
2763 u16 heads = dev->id[3];
2764
2765 /* Number of sectors per track 1-255. Number of heads 1-16 */
2766 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002767 return 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002768
2769 /* set up init dev params taskfile */
2770 DPRINTK("init dev params \n");
2771
Tejun Heoa0123702005-12-13 14:49:31 +09002772 ata_tf_init(ap, &tf, dev->devno);
2773 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2774 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2775 tf.protocol = ATA_PROT_NODATA;
2776 tf.nsect = sectors;
2777 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04002778
Tejun Heo6aff8f12006-02-15 18:24:09 +09002779 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ece2005-05-12 15:29:42 -04002780
Tejun Heo6aff8f12006-02-15 18:24:09 +09002781 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2782 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002783}
2784
2785/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002786 * ata_sg_clean - Unmap DMA memory associated with command
2787 * @qc: Command containing DMA memory to be released
2788 *
2789 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 *
2791 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002792 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 */
2794
2795static void ata_sg_clean(struct ata_queued_cmd *qc)
2796{
2797 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002798 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002800 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Tejun Heoa46314742006-02-11 19:11:13 +09002802 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2803 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002806 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002808 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002810 /* if we padded the buffer out to 32-bit bound, and data
2811 * xfer direction is from-device, we must copy from the
2812 * pad buffer back into the supplied buffer
2813 */
2814 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2815 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2816
2817 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002818 if (qc->n_elem)
2819 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002820 /* restore last sg */
2821 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2822 if (pad_buf) {
2823 struct scatterlist *psg = &qc->pad_sgent;
2824 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2825 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002826 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002827 }
2828 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002829 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002830 dma_unmap_single(ap->host_set->dev,
2831 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2832 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002833 /* restore sg */
2834 sg->length += qc->pad_len;
2835 if (pad_buf)
2836 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2837 pad_buf, qc->pad_len);
2838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002841 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
2844/**
2845 * ata_fill_sg - Fill PCI IDE PRD table
2846 * @qc: Metadata associated with taskfile to be transferred
2847 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002848 * Fill PCI IDE PRD (scatter-gather) table with segments
2849 * associated with the current disk command.
2850 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002852 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 *
2854 */
2855static void ata_fill_sg(struct ata_queued_cmd *qc)
2856{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002858 struct scatterlist *sg;
2859 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Tejun Heoa46314742006-02-11 19:11:13 +09002861 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002862 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002865 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 u32 addr, offset;
2867 u32 sg_len, len;
2868
2869 /* determine if physical DMA addr spans 64K boundary.
2870 * Note h/w doesn't support 64-bit, so we unconditionally
2871 * truncate dma_addr_t to u32.
2872 */
2873 addr = (u32) sg_dma_address(sg);
2874 sg_len = sg_dma_len(sg);
2875
2876 while (sg_len) {
2877 offset = addr & 0xffff;
2878 len = sg_len;
2879 if ((offset + sg_len) > 0x10000)
2880 len = 0x10000 - offset;
2881
2882 ap->prd[idx].addr = cpu_to_le32(addr);
2883 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2884 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2885
2886 idx++;
2887 sg_len -= len;
2888 addr += len;
2889 }
2890 }
2891
2892 if (idx)
2893 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2894}
2895/**
2896 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2897 * @qc: Metadata associated with taskfile to check
2898 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002899 * Allow low-level driver to filter ATA PACKET commands, returning
2900 * a status indicating whether or not it is OK to use DMA for the
2901 * supplied PACKET command.
2902 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002904 * spin_lock_irqsave(host_set lock)
2905 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 * RETURNS: 0 when ATAPI DMA can be used
2907 * nonzero otherwise
2908 */
2909int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2910{
2911 struct ata_port *ap = qc->ap;
2912 int rc = 0; /* Assume ATAPI DMA is OK by default */
2913
2914 if (ap->ops->check_atapi_dma)
2915 rc = ap->ops->check_atapi_dma(qc);
2916
2917 return rc;
2918}
2919/**
2920 * ata_qc_prep - Prepare taskfile for submission
2921 * @qc: Metadata associated with taskfile to be prepared
2922 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002923 * Prepare ATA taskfile for submission.
2924 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 * LOCKING:
2926 * spin_lock_irqsave(host_set lock)
2927 */
2928void ata_qc_prep(struct ata_queued_cmd *qc)
2929{
2930 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2931 return;
2932
2933 ata_fill_sg(qc);
2934}
2935
Brian Kinge46834c2006-03-17 17:04:03 -06002936void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
2937
Jeff Garzik0cba6322005-05-30 19:49:12 -04002938/**
2939 * ata_sg_init_one - Associate command with memory buffer
2940 * @qc: Command to be associated
2941 * @buf: Memory buffer
2942 * @buflen: Length of memory buffer, in bytes.
2943 *
2944 * Initialize the data-related elements of queued_cmd @qc
2945 * to point to a single memory buffer, @buf of byte length @buflen.
2946 *
2947 * LOCKING:
2948 * spin_lock_irqsave(host_set lock)
2949 */
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2952{
2953 struct scatterlist *sg;
2954
2955 qc->flags |= ATA_QCFLAG_SINGLE;
2956
2957 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002958 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002960 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 qc->buf_virt = buf;
2962
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002963 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002964 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Jeff Garzik0cba6322005-05-30 19:49:12 -04002967/**
2968 * ata_sg_init - Associate command with scatter-gather table.
2969 * @qc: Command to be associated
2970 * @sg: Scatter-gather table.
2971 * @n_elem: Number of elements in s/g table.
2972 *
2973 * Initialize the data-related elements of queued_cmd @qc
2974 * to point to a scatter-gather table @sg, containing @n_elem
2975 * elements.
2976 *
2977 * LOCKING:
2978 * spin_lock_irqsave(host_set lock)
2979 */
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2982 unsigned int n_elem)
2983{
2984 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002985 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002987 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
2989
2990/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002991 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2992 * @qc: Command with memory buffer to be mapped.
2993 *
2994 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 *
2996 * LOCKING:
2997 * spin_lock_irqsave(host_set lock)
2998 *
2999 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003000 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 */
3002
3003static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3004{
3005 struct ata_port *ap = qc->ap;
3006 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003007 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003009 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003011 /* we must lengthen transfers to end on a 32-bit boundary */
3012 qc->pad_len = sg->length & 3;
3013 if (qc->pad_len) {
3014 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3015 struct scatterlist *psg = &qc->pad_sgent;
3016
Tejun Heoa46314742006-02-11 19:11:13 +09003017 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003018
3019 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3020
3021 if (qc->tf.flags & ATA_TFLAG_WRITE)
3022 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3023 qc->pad_len);
3024
3025 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3026 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3027 /* trim sg */
3028 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003029 if (sg->length == 0)
3030 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003031
3032 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3033 sg->length, qc->pad_len);
3034 }
3035
Tejun Heo2e242fa2006-02-20 23:48:38 +09003036 if (trim_sg) {
3037 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003038 goto skip_map;
3039 }
3040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003042 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003043 if (dma_mapping_error(dma_address)) {
3044 /* restore sg */
3045 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003050 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Tejun Heo2e242fa2006-02-20 23:48:38 +09003052skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3054 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3055
3056 return 0;
3057}
3058
3059/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003060 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3061 * @qc: Command with scatter-gather table to be mapped.
3062 *
3063 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 *
3065 * LOCKING:
3066 * spin_lock_irqsave(host_set lock)
3067 *
3068 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003069 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 *
3071 */
3072
3073static int ata_sg_setup(struct ata_queued_cmd *qc)
3074{
3075 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003076 struct scatterlist *sg = qc->__sg;
3077 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003078 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa46314742006-02-11 19:11:13 +09003081 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003083 /* we must lengthen transfers to end on a 32-bit boundary */
3084 qc->pad_len = lsg->length & 3;
3085 if (qc->pad_len) {
3086 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3087 struct scatterlist *psg = &qc->pad_sgent;
3088 unsigned int offset;
3089
Tejun Heoa46314742006-02-11 19:11:13 +09003090 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003091
3092 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3093
3094 /*
3095 * psg->page/offset are used to copy to-be-written
3096 * data in this function or read data in ata_sg_clean.
3097 */
3098 offset = lsg->offset + lsg->length - qc->pad_len;
3099 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3100 psg->offset = offset_in_page(offset);
3101
3102 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3103 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3104 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003105 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003106 }
3107
3108 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3109 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3110 /* trim last sg */
3111 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003112 if (lsg->length == 0)
3113 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003114
3115 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3116 qc->n_elem - 1, lsg->length, qc->pad_len);
3117 }
3118
Jeff Garzike1410f22005-11-14 14:06:26 -05003119 pre_n_elem = qc->n_elem;
3120 if (trim_sg && pre_n_elem)
3121 pre_n_elem--;
3122
3123 if (!pre_n_elem) {
3124 n_elem = 0;
3125 goto skip_map;
3126 }
3127
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05003129 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003130 if (n_elem < 1) {
3131 /* restore last sg */
3132 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 DPRINTK("%d sg elements mapped\n", n_elem);
3137
Jeff Garzike1410f22005-11-14 14:06:26 -05003138skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 qc->n_elem = n_elem;
3140
3141 return 0;
3142}
3143
3144/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003145 * ata_poll_qc_complete - turn irq back on and finish qc
3146 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003147 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003148 *
3149 * LOCKING:
3150 * None. (grabs host lock)
3151 */
3152
Albert Leea22e2eb2005-12-05 15:38:02 +08003153void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003154{
3155 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003156 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003157
Jeff Garzikb8f61532005-08-25 22:01:20 -04003158 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003159 ap->flags &= ~ATA_FLAG_NOINTR;
3160 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003161 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003162 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003163}
3164
3165/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003166 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003167 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 *
3169 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003170 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 *
3172 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003173 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 */
3175
3176static unsigned long ata_pio_poll(struct ata_port *ap)
3177{
Albert Leec14b8332005-12-05 15:36:08 +08003178 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003180 unsigned int poll_state = HSM_ST_UNKNOWN;
3181 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Albert Leec14b8332005-12-05 15:36:08 +08003183 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003184 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003185
Albert Lee14be71f2005-09-27 17:36:35 +08003186 switch (ap->hsm_task_state) {
3187 case HSM_ST:
3188 case HSM_ST_POLL:
3189 poll_state = HSM_ST_POLL;
3190 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003192 case HSM_ST_LAST:
3193 case HSM_ST_LAST_POLL:
3194 poll_state = HSM_ST_LAST_POLL;
3195 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 break;
3197 default:
3198 BUG();
3199 break;
3200 }
3201
3202 status = ata_chk_status(ap);
3203 if (status & ATA_BUSY) {
3204 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003205 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003206 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return 0;
3208 }
Albert Lee14be71f2005-09-27 17:36:35 +08003209 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return ATA_SHORT_PAUSE;
3211 }
3212
Albert Lee14be71f2005-09-27 17:36:35 +08003213 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return 0;
3215}
3216
3217/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003218 * ata_pio_complete - check if drive is busy or idle
3219 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 *
3221 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003222 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003223 *
3224 * RETURNS:
3225 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 */
3227
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003228static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 struct ata_queued_cmd *qc;
3231 u8 drv_stat;
3232
3233 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003234 * This is purely heuristic. This is a fast path. Sometimes when
3235 * we enter, BSY will be cleared in a chk-status or two. If not,
3236 * the drive is probably seeking or something. Snooze for a couple
3237 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003238 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 */
Albert Leefe79e682005-12-06 11:34:59 +08003240 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3241 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003243 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3244 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003245 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 }
3249 }
3250
Albert Leec14b8332005-12-05 15:36:08 +08003251 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003252 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 drv_stat = ata_wait_idle(ap);
3255 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003256 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003257 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 }
3260
Albert Lee14be71f2005-09-27 17:36:35 +08003261 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Tejun Heoa46314742006-02-11 19:11:13 +09003263 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003264 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003265
3266 /* another command may start at this point */
3267
3268 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
3270
Edward Falk0baab862005-06-02 18:17:13 -04003271
3272/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003273 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003274 * @buf: Buffer to swap
3275 * @buf_words: Number of 16-bit words in buffer.
3276 *
3277 * Swap halves of 16-bit words if needed to convert from
3278 * little-endian byte order to native cpu byte order, or
3279 * vice-versa.
3280 *
3281 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003282 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284void swap_buf_le16(u16 *buf, unsigned int buf_words)
3285{
3286#ifdef __BIG_ENDIAN
3287 unsigned int i;
3288
3289 for (i = 0; i < buf_words; i++)
3290 buf[i] = le16_to_cpu(buf[i]);
3291#endif /* __BIG_ENDIAN */
3292}
3293
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003294/**
3295 * ata_mmio_data_xfer - Transfer data by MMIO
3296 * @ap: port to read/write
3297 * @buf: data buffer
3298 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003299 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003300 *
3301 * Transfer data from/to the device data register by MMIO.
3302 *
3303 * LOCKING:
3304 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003305 */
3306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3308 unsigned int buflen, int write_data)
3309{
3310 unsigned int i;
3311 unsigned int words = buflen >> 1;
3312 u16 *buf16 = (u16 *) buf;
3313 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3314
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003315 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (write_data) {
3317 for (i = 0; i < words; i++)
3318 writew(le16_to_cpu(buf16[i]), mmio);
3319 } else {
3320 for (i = 0; i < words; i++)
3321 buf16[i] = cpu_to_le16(readw(mmio));
3322 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003323
3324 /* Transfer trailing 1 byte, if any. */
3325 if (unlikely(buflen & 0x01)) {
3326 u16 align_buf[1] = { 0 };
3327 unsigned char *trailing_buf = buf + buflen - 1;
3328
3329 if (write_data) {
3330 memcpy(align_buf, trailing_buf, 1);
3331 writew(le16_to_cpu(align_buf[0]), mmio);
3332 } else {
3333 align_buf[0] = cpu_to_le16(readw(mmio));
3334 memcpy(trailing_buf, align_buf, 1);
3335 }
3336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337}
3338
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003339/**
3340 * ata_pio_data_xfer - Transfer data by PIO
3341 * @ap: port to read/write
3342 * @buf: data buffer
3343 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003344 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003345 *
3346 * Transfer data from/to the device data register by PIO.
3347 *
3348 * LOCKING:
3349 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003350 */
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3353 unsigned int buflen, int write_data)
3354{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003355 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003357 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003359 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003361 insw(ap->ioaddr.data_addr, buf, words);
3362
3363 /* Transfer trailing 1 byte, if any. */
3364 if (unlikely(buflen & 0x01)) {
3365 u16 align_buf[1] = { 0 };
3366 unsigned char *trailing_buf = buf + buflen - 1;
3367
3368 if (write_data) {
3369 memcpy(align_buf, trailing_buf, 1);
3370 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3371 } else {
3372 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3373 memcpy(trailing_buf, align_buf, 1);
3374 }
3375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003378/**
3379 * ata_data_xfer - Transfer data from/to the data register.
3380 * @ap: port to read/write
3381 * @buf: data buffer
3382 * @buflen: buffer length
3383 * @do_write: read/write
3384 *
3385 * Transfer data from/to the device data register.
3386 *
3387 * LOCKING:
3388 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003389 */
3390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3392 unsigned int buflen, int do_write)
3393{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003394 /* Make the crap hardware pay the costs not the good stuff */
3395 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3396 unsigned long flags;
3397 local_irq_save(flags);
3398 if (ap->flags & ATA_FLAG_MMIO)
3399 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3400 else
3401 ata_pio_data_xfer(ap, buf, buflen, do_write);
3402 local_irq_restore(flags);
3403 } else {
3404 if (ap->flags & ATA_FLAG_MMIO)
3405 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3406 else
3407 ata_pio_data_xfer(ap, buf, buflen, do_write);
3408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003411/**
3412 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3413 * @qc: Command on going
3414 *
3415 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3416 *
3417 * LOCKING:
3418 * Inherited from caller.
3419 */
3420
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421static void ata_pio_sector(struct ata_queued_cmd *qc)
3422{
3423 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003424 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 struct ata_port *ap = qc->ap;
3426 struct page *page;
3427 unsigned int offset;
3428 unsigned char *buf;
3429
3430 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003431 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
3433 page = sg[qc->cursg].page;
3434 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3435
3436 /* get the current page and offset */
3437 page = nth_page(page, (offset >> PAGE_SHIFT));
3438 offset %= PAGE_SIZE;
3439
3440 buf = kmap(page) + offset;
3441
3442 qc->cursect++;
3443 qc->cursg_ofs++;
3444
Albert Lee32529e02005-05-26 03:49:42 -04003445 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 qc->cursg++;
3447 qc->cursg_ofs = 0;
3448 }
3449
3450 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3451
3452 /* do the actual data transfer */
3453 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3454 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3455
3456 kunmap(page);
3457}
3458
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003459/**
3460 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3461 * @qc: Command on going
3462 * @bytes: number of bytes
3463 *
3464 * Transfer Transfer data from/to the ATAPI device.
3465 *
3466 * LOCKING:
3467 * Inherited from caller.
3468 *
3469 */
3470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3472{
3473 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003474 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 struct ata_port *ap = qc->ap;
3476 struct page *page;
3477 unsigned char *buf;
3478 unsigned int offset, count;
3479
Albert Lee563a6e12005-08-12 14:17:50 +08003480 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003481 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
3483next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003484 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003485 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003486 * The end of qc->sg is reached and the device expects
3487 * more data to transfer. In order not to overrun qc->sg
3488 * and fulfill length specified in the byte count register,
3489 * - for read case, discard trailing data from the device
3490 * - for write case, padding zero data to the device
3491 */
3492 u16 pad_buf[1] = { 0 };
3493 unsigned int words = bytes >> 1;
3494 unsigned int i;
3495
3496 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003497 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003498 ap->id, bytes);
3499
3500 for (i = 0; i < words; i++)
3501 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3502
Albert Lee14be71f2005-09-27 17:36:35 +08003503 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003504 return;
3505 }
3506
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003507 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 page = sg->page;
3510 offset = sg->offset + qc->cursg_ofs;
3511
3512 /* get the current page and offset */
3513 page = nth_page(page, (offset >> PAGE_SHIFT));
3514 offset %= PAGE_SIZE;
3515
Albert Lee6952df02005-06-06 15:56:03 +08003516 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003517 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
3519 /* don't cross page boundaries */
3520 count = min(count, (unsigned int)PAGE_SIZE - offset);
3521
3522 buf = kmap(page) + offset;
3523
3524 bytes -= count;
3525 qc->curbytes += count;
3526 qc->cursg_ofs += count;
3527
Albert Lee32529e02005-05-26 03:49:42 -04003528 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 qc->cursg++;
3530 qc->cursg_ofs = 0;
3531 }
3532
3533 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3534
3535 /* do the actual data transfer */
3536 ata_data_xfer(ap, buf, count, do_write);
3537
3538 kunmap(page);
3539
Albert Lee563a6e12005-08-12 14:17:50 +08003540 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
3543
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003544/**
3545 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3546 * @qc: Command on going
3547 *
3548 * Transfer Transfer data from/to the ATAPI device.
3549 *
3550 * LOCKING:
3551 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003552 */
3553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3555{
3556 struct ata_port *ap = qc->ap;
3557 struct ata_device *dev = qc->dev;
3558 unsigned int ireason, bc_lo, bc_hi, bytes;
3559 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3560
3561 ap->ops->tf_read(ap, &qc->tf);
3562 ireason = qc->tf.nsect;
3563 bc_lo = qc->tf.lbam;
3564 bc_hi = qc->tf.lbah;
3565 bytes = (bc_hi << 8) | bc_lo;
3566
3567 /* shall be cleared to zero, indicating xfer of data */
3568 if (ireason & (1 << 0))
3569 goto err_out;
3570
3571 /* make sure transfer direction matches expected */
3572 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3573 if (do_write != i_write)
3574 goto err_out;
3575
3576 __atapi_pio_bytes(qc, bytes);
3577
3578 return;
3579
3580err_out:
3581 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3582 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003583 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003584 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585}
3586
3587/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003588 * ata_pio_block - start PIO on a block
3589 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 *
3591 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003592 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 */
3594
3595static void ata_pio_block(struct ata_port *ap)
3596{
3597 struct ata_queued_cmd *qc;
3598 u8 status;
3599
3600 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003601 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 * Sometimes when we enter, BSY will be cleared in
3603 * a chk-status or two. If not, the drive is probably seeking
3604 * or something. Snooze for a couple msecs, then
3605 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003606 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 */
3608 status = ata_busy_wait(ap, ATA_BUSY, 5);
3609 if (status & ATA_BUSY) {
3610 msleep(2);
3611 status = ata_busy_wait(ap, ATA_BUSY, 10);
3612 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003613 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3615 return;
3616 }
3617 }
3618
3619 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003620 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Albert Leefe79e682005-12-06 11:34:59 +08003622 /* check error */
3623 if (status & (ATA_ERR | ATA_DF)) {
3624 qc->err_mask |= AC_ERR_DEV;
3625 ap->hsm_task_state = HSM_ST_ERR;
3626 return;
3627 }
3628
3629 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003631 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003633 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 return;
3635 }
3636
3637 atapi_pio_bytes(qc);
3638 } else {
3639 /* handle BSY=0, DRQ=0 as error */
3640 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003641 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003642 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 return;
3644 }
3645
3646 ata_pio_sector(qc);
3647 }
3648}
3649
3650static void ata_pio_error(struct ata_port *ap)
3651{
3652 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003655 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Albert Lee0565c262006-02-13 18:55:25 +08003657 if (qc->tf.command != ATA_CMD_PACKET)
3658 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3659
Albert Lee1c848982005-12-05 15:40:15 +08003660 /* make sure qc->err_mask is available to
3661 * know what's wrong and recover
3662 */
Tejun Heoa46314742006-02-11 19:11:13 +09003663 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003664
Albert Lee14be71f2005-09-27 17:36:35 +08003665 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Albert Leea22e2eb2005-12-05 15:38:02 +08003667 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
3669
3670static void ata_pio_task(void *_data)
3671{
3672 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003673 unsigned long timeout;
3674 int qc_completed;
3675
3676fsm_start:
3677 timeout = 0;
3678 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Albert Lee14be71f2005-09-27 17:36:35 +08003680 switch (ap->hsm_task_state) {
3681 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 return;
3683
Albert Lee14be71f2005-09-27 17:36:35 +08003684 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 ata_pio_block(ap);
3686 break;
3687
Albert Lee14be71f2005-09-27 17:36:35 +08003688 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003689 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 break;
3691
Albert Lee14be71f2005-09-27 17:36:35 +08003692 case HSM_ST_POLL:
3693 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 timeout = ata_pio_poll(ap);
3695 break;
3696
Albert Lee14be71f2005-09-27 17:36:35 +08003697 case HSM_ST_TMOUT:
3698 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 ata_pio_error(ap);
3700 return;
3701 }
3702
3703 if (timeout)
Tejun Heo8061f5f2006-03-05 15:29:09 +09003704 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003705 else if (!qc_completed)
3706 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709/**
Tejun Heo8061f5f2006-03-05 15:29:09 +09003710 * atapi_packet_task - Write CDB bytes to hardware
3711 * @_data: Port to which ATAPI device is attached.
3712 *
3713 * When device has indicated its readiness to accept
3714 * a CDB, this function is called. Send the CDB.
3715 * If DMA is to be performed, exit immediately.
3716 * Otherwise, we are in polling mode, so poll
3717 * status under operation succeeds or fails.
3718 *
3719 * LOCKING:
3720 * Kernel thread context (may sleep)
3721 */
3722
3723static void atapi_packet_task(void *_data)
3724{
3725 struct ata_port *ap = _data;
3726 struct ata_queued_cmd *qc;
3727 u8 status;
3728
3729 qc = ata_qc_from_tag(ap, ap->active_tag);
3730 WARN_ON(qc == NULL);
3731 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3732
3733 /* sleep-wait for BSY to clear */
3734 DPRINTK("busy wait\n");
3735 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3736 qc->err_mask |= AC_ERR_TIMEOUT;
3737 goto err_out;
3738 }
3739
3740 /* make sure DRQ is set */
3741 status = ata_chk_status(ap);
3742 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3743 qc->err_mask |= AC_ERR_HSM;
3744 goto err_out;
3745 }
3746
3747 /* send SCSI cdb */
3748 DPRINTK("send cdb\n");
3749 WARN_ON(qc->dev->cdb_len < 12);
3750
3751 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3752 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3753 unsigned long flags;
3754
3755 /* Once we're done issuing command and kicking bmdma,
3756 * irq handler takes over. To not lose irq, we need
3757 * to clear NOINTR flag before sending cdb, but
3758 * interrupt handler shouldn't be invoked before we're
3759 * finished. Hence, the following locking.
3760 */
3761 spin_lock_irqsave(&ap->host_set->lock, flags);
3762 ap->flags &= ~ATA_FLAG_NOINTR;
3763 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3764 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3765 ap->ops->bmdma_start(qc); /* initiate bmdma */
3766 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3767 } else {
3768 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3769
3770 /* PIO commands are handled by polling */
3771 ap->hsm_task_state = HSM_ST;
3772 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3773 }
3774
3775 return;
3776
3777err_out:
3778 ata_poll_qc_complete(qc);
3779}
3780
3781/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 * ata_qc_timeout - Handle timeout of queued command
3783 * @qc: Command that timed out
3784 *
3785 * Some part of the kernel (currently, only the SCSI layer)
3786 * has noticed that the active command on port @ap has not
3787 * completed after a specified length of time. Handle this
3788 * condition by disabling DMA (if necessary) and completing
3789 * transactions, with error if necessary.
3790 *
3791 * This also handles the case of the "lost interrupt", where
3792 * for some reason (possibly hardware bug, possibly driver bug)
3793 * an interrupt was not delivered to the driver, even though the
3794 * transaction completed successfully.
3795 *
3796 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003797 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 */
3799
3800static void ata_qc_timeout(struct ata_queued_cmd *qc)
3801{
3802 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003803 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003805 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807 DPRINTK("ENTER\n");
3808
Tejun Heoc18d06f2006-02-02 00:56:10 +09003809 ap->hsm_task_state = HSM_ST_IDLE;
3810
Jeff Garzikb8f61532005-08-25 22:01:20 -04003811 spin_lock_irqsave(&host_set->lock, flags);
3812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 switch (qc->tf.protocol) {
3814
3815 case ATA_PROT_DMA:
3816 case ATA_PROT_ATAPI_DMA:
3817 host_stat = ap->ops->bmdma_status(ap);
3818
3819 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003820 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822 /* fall through */
3823
3824 default:
3825 ata_altstatus(ap);
3826 drv_stat = ata_chk_status(ap);
3827
3828 /* ack bmdma irq events */
3829 ap->ops->irq_clear(ap);
3830
3831 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3832 ap->id, qc->tf.command, drv_stat, host_stat);
3833
3834 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003835 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 break;
3837 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003838
3839 spin_unlock_irqrestore(&host_set->lock, flags);
3840
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003841 ata_eh_qc_complete(qc);
3842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 DPRINTK("EXIT\n");
3844}
3845
3846/**
3847 * ata_eng_timeout - Handle timeout of queued command
3848 * @ap: Port on which timed-out command is active
3849 *
3850 * Some part of the kernel (currently, only the SCSI layer)
3851 * has noticed that the active command on port @ap has not
3852 * completed after a specified length of time. Handle this
3853 * condition by disabling DMA (if necessary) and completing
3854 * transactions, with error if necessary.
3855 *
3856 * This also handles the case of the "lost interrupt", where
3857 * for some reason (possibly hardware bug, possibly driver bug)
3858 * an interrupt was not delivered to the driver, even though the
3859 * transaction completed successfully.
3860 *
3861 * LOCKING:
3862 * Inherited from SCSI layer (none, can sleep)
3863 */
3864
3865void ata_eng_timeout(struct ata_port *ap)
3866{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 DPRINTK("ENTER\n");
3868
Tejun Heof6379022006-02-10 15:10:48 +09003869 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 DPRINTK("EXIT\n");
3872}
3873
3874/**
3875 * ata_qc_new - Request an available ATA command, for queueing
3876 * @ap: Port associated with device @dev
3877 * @dev: Device from whom we request an available command structure
3878 *
3879 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003880 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 */
3882
3883static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3884{
3885 struct ata_queued_cmd *qc = NULL;
3886 unsigned int i;
3887
3888 for (i = 0; i < ATA_MAX_QUEUE; i++)
3889 if (!test_and_set_bit(i, &ap->qactive)) {
3890 qc = ata_qc_from_tag(ap, i);
3891 break;
3892 }
3893
3894 if (qc)
3895 qc->tag = i;
3896
3897 return qc;
3898}
3899
3900/**
3901 * ata_qc_new_init - Request an available ATA command, and initialize it
3902 * @ap: Port associated with device @dev
3903 * @dev: Device from whom we request an available command structure
3904 *
3905 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003906 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 */
3908
3909struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3910 struct ata_device *dev)
3911{
3912 struct ata_queued_cmd *qc;
3913
3914 qc = ata_qc_new(ap);
3915 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 qc->scsicmd = NULL;
3917 qc->ap = ap;
3918 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003920 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 }
3922
3923 return qc;
3924}
3925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926/**
3927 * ata_qc_free - free unused ata_queued_cmd
3928 * @qc: Command to complete
3929 *
3930 * Designed to free unused ata_queued_cmd object
3931 * in case something prevents using it.
3932 *
3933 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003934 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 */
3936void ata_qc_free(struct ata_queued_cmd *qc)
3937{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003938 struct ata_port *ap = qc->ap;
3939 unsigned int tag;
3940
Tejun Heoa46314742006-02-11 19:11:13 +09003941 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
Tejun Heo4ba946e2006-01-23 13:09:36 +09003943 qc->flags = 0;
3944 tag = qc->tag;
3945 if (likely(ata_tag_valid(tag))) {
3946 if (tag == ap->active_tag)
3947 ap->active_tag = ATA_TAG_POISON;
3948 qc->tag = ATA_TAG_POISON;
3949 clear_bit(tag, &ap->qactive);
3950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951}
3952
Tejun Heo76014422006-02-11 15:13:49 +09003953void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
Tejun Heoa46314742006-02-11 19:11:13 +09003955 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3956 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3959 ata_sg_clean(qc);
3960
Albert Lee3f3791d2005-08-16 14:25:38 +08003961 /* atapi: mark qc as inactive to prevent the interrupt handler
3962 * from completing the command twice later, before the error handler
3963 * is called. (when rc != 0 and atapi request sense is needed)
3964 */
3965 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003968 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969}
3970
3971static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3972{
3973 struct ata_port *ap = qc->ap;
3974
3975 switch (qc->tf.protocol) {
3976 case ATA_PROT_DMA:
3977 case ATA_PROT_ATAPI_DMA:
3978 return 1;
3979
3980 case ATA_PROT_ATAPI:
3981 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 if (ap->flags & ATA_FLAG_PIO_DMA)
3983 return 1;
3984
3985 /* fall through */
3986
3987 default:
3988 return 0;
3989 }
3990
3991 /* never reached */
3992}
3993
3994/**
3995 * ata_qc_issue - issue taskfile to device
3996 * @qc: command to issue to device
3997 *
3998 * Prepare an ATA command to submission to device.
3999 * This includes mapping the data into a DMA-able
4000 * area, filling in the S/G table, and finally
4001 * writing the taskfile to hardware, starting the command.
4002 *
4003 * LOCKING:
4004 * spin_lock_irqsave(host_set lock)
4005 *
4006 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004007 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 */
4009
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004010unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
4012 struct ata_port *ap = qc->ap;
4013
4014 if (ata_should_dma_map(qc)) {
4015 if (qc->flags & ATA_QCFLAG_SG) {
4016 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004017 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4019 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004020 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
4022 } else {
4023 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4024 }
4025
4026 ap->ops->qc_prep(qc);
4027
4028 qc->ap->active_tag = qc->tag;
4029 qc->flags |= ATA_QCFLAG_ACTIVE;
4030
4031 return ap->ops->qc_issue(qc);
4032
Tejun Heo8e436af2006-01-23 13:09:36 +09004033sg_err:
4034 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004035 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036}
4037
Edward Falk0baab862005-06-02 18:17:13 -04004038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039/**
4040 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4041 * @qc: command to issue to device
4042 *
4043 * Using various libata functions and hooks, this function
4044 * starts an ATA command. ATA commands are grouped into
4045 * classes called "protocols", and issuing each type of protocol
4046 * is slightly different.
4047 *
Edward Falk0baab862005-06-02 18:17:13 -04004048 * May be used as the qc_issue() entry in ata_port_operations.
4049 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 * LOCKING:
4051 * spin_lock_irqsave(host_set lock)
4052 *
4053 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004054 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 */
4056
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004057unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
4059 struct ata_port *ap = qc->ap;
4060
4061 ata_dev_select(ap, qc->dev->devno, 1, 0);
4062
4063 switch (qc->tf.protocol) {
4064 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05004065 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 break;
4067
4068 case ATA_PROT_DMA:
4069 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4070 ap->ops->bmdma_setup(qc); /* set up bmdma */
4071 ap->ops->bmdma_start(qc); /* initiate bmdma */
4072 break;
4073
4074 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4075 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004076 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08004077 ap->hsm_task_state = HSM_ST;
Tejun Heo8061f5f2006-03-05 15:29:09 +09004078 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 break;
4080
4081 case ATA_PROT_ATAPI:
4082 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004083 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004084 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 break;
4086
4087 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09004088 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05004089 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004090 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 break;
4092
4093 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09004094 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4096 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo8061f5f2006-03-05 15:29:09 +09004097 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 break;
4099
4100 default:
4101 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004102 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 }
4104
4105 return 0;
4106}
4107
4108/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 * ata_host_intr - Handle host interrupt for given (port, task)
4110 * @ap: Port on which interrupt arrived (possibly...)
4111 * @qc: Taskfile currently active in engine
4112 *
4113 * Handle host interrupt for given queued command. Currently,
4114 * only DMA interrupts are handled. All other commands are
4115 * handled via polling with interrupts disabled (nIEN bit).
4116 *
4117 * LOCKING:
4118 * spin_lock_irqsave(host_set lock)
4119 *
4120 * RETURNS:
4121 * One if interrupt was handled, zero if not (shared irq).
4122 */
4123
4124inline unsigned int ata_host_intr (struct ata_port *ap,
4125 struct ata_queued_cmd *qc)
4126{
4127 u8 status, host_stat;
4128
4129 switch (qc->tf.protocol) {
4130
4131 case ATA_PROT_DMA:
4132 case ATA_PROT_ATAPI_DMA:
4133 case ATA_PROT_ATAPI:
4134 /* check status of DMA engine */
4135 host_stat = ap->ops->bmdma_status(ap);
4136 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4137
4138 /* if it's not our irq... */
4139 if (!(host_stat & ATA_DMA_INTR))
4140 goto idle_irq;
4141
4142 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004143 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
4145 /* fall through */
4146
4147 case ATA_PROT_ATAPI_NODATA:
4148 case ATA_PROT_NODATA:
4149 /* check altstatus */
4150 status = ata_altstatus(ap);
4151 if (status & ATA_BUSY)
4152 goto idle_irq;
4153
4154 /* check main status, clearing INTRQ */
4155 status = ata_chk_status(ap);
4156 if (unlikely(status & ATA_BUSY))
4157 goto idle_irq;
4158 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4159 ap->id, qc->tf.protocol, status);
4160
4161 /* ack bmdma irq events */
4162 ap->ops->irq_clear(ap);
4163
4164 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004165 qc->err_mask |= ac_err_mask(status);
4166 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 break;
4168
4169 default:
4170 goto idle_irq;
4171 }
4172
4173 return 1; /* irq handled */
4174
4175idle_irq:
4176 ap->stats.idle_irq++;
4177
4178#ifdef ATA_IRQ_TRAP
4179 if ((ap->stats.idle_irq % 1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 ata_irq_ack(ap, 0); /* debug trap */
4181 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
Alan Cox23cfce82006-03-21 16:06:53 +00004182 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 }
4184#endif
4185 return 0; /* irq not handled */
4186}
4187
4188/**
4189 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004190 * @irq: irq line (unused)
4191 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 * @regs: unused
4193 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004194 * Default interrupt handler for PCI IDE devices. Calls
4195 * ata_host_intr() for each port that is not disabled.
4196 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004198 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 *
4200 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004201 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 */
4203
4204irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4205{
4206 struct ata_host_set *host_set = dev_instance;
4207 unsigned int i;
4208 unsigned int handled = 0;
4209 unsigned long flags;
4210
4211 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4212 spin_lock_irqsave(&host_set->lock, flags);
4213
4214 for (i = 0; i < host_set->n_ports; i++) {
4215 struct ata_port *ap;
4216
4217 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004218 if (ap &&
4219 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 struct ata_queued_cmd *qc;
4221
4222 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004223 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4224 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 handled |= ata_host_intr(ap, qc);
4226 }
4227 }
4228
4229 spin_unlock_irqrestore(&host_set->lock, flags);
4230
4231 return IRQ_RETVAL(handled);
4232}
4233
Edward Falk0baab862005-06-02 18:17:13 -04004234
Jens Axboe9b847542006-01-06 09:28:07 +01004235/*
4236 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4237 * without filling any other registers
4238 */
4239static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4240 u8 cmd)
4241{
4242 struct ata_taskfile tf;
4243 int err;
4244
4245 ata_tf_init(ap, &tf, dev->devno);
4246
4247 tf.command = cmd;
4248 tf.flags |= ATA_TFLAG_DEVICE;
4249 tf.protocol = ATA_PROT_NODATA;
4250
4251 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4252 if (err)
4253 printk(KERN_ERR "%s: ata command failed: %d\n",
4254 __FUNCTION__, err);
4255
4256 return err;
4257}
4258
4259static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4260{
4261 u8 cmd;
4262
4263 if (!ata_try_flush_cache(dev))
4264 return 0;
4265
4266 if (ata_id_has_flush_ext(dev->id))
4267 cmd = ATA_CMD_FLUSH_EXT;
4268 else
4269 cmd = ATA_CMD_FLUSH;
4270
4271 return ata_do_simple_cmd(ap, dev, cmd);
4272}
4273
4274static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4275{
4276 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4277}
4278
4279static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4280{
4281 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4282}
4283
4284/**
4285 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004286 * @ap: port the device is connected to
4287 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004288 *
4289 * Kick the drive back into action, by sending it an idle immediate
4290 * command and making sure its transfer mode matches between drive
4291 * and host.
4292 *
4293 */
4294int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4295{
4296 if (ap->flags & ATA_FLAG_SUSPENDED) {
4297 ap->flags &= ~ATA_FLAG_SUSPENDED;
4298 ata_set_mode(ap);
4299 }
4300 if (!ata_dev_present(dev))
4301 return 0;
4302 if (dev->class == ATA_DEV_ATA)
4303 ata_start_drive(ap, dev);
4304
4305 return 0;
4306}
4307
4308/**
4309 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004310 * @ap: port the device is connected to
4311 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004312 *
4313 * Flush the cache on the drive, if appropriate, then issue a
4314 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004315 */
4316int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4317{
4318 if (!ata_dev_present(dev))
4319 return 0;
4320 if (dev->class == ATA_DEV_ATA)
4321 ata_flush_cache(ap, dev);
4322
4323 ata_standby_drive(ap, dev);
4324 ap->flags |= ATA_FLAG_SUSPENDED;
4325 return 0;
4326}
4327
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004328/**
4329 * ata_port_start - Set port up for dma.
4330 * @ap: Port to initialize
4331 *
4332 * Called just after data structures for each port are
4333 * initialized. Allocates space for PRD table.
4334 *
4335 * May be used as the port_start() entry in ata_port_operations.
4336 *
4337 * LOCKING:
4338 * Inherited from caller.
4339 */
4340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341int ata_port_start (struct ata_port *ap)
4342{
4343 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004344 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
4346 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4347 if (!ap->prd)
4348 return -ENOMEM;
4349
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004350 rc = ata_pad_alloc(ap, dev);
4351 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004352 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004353 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004354 }
4355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4357
4358 return 0;
4359}
4360
Edward Falk0baab862005-06-02 18:17:13 -04004361
4362/**
4363 * ata_port_stop - Undo ata_port_start()
4364 * @ap: Port to shut down
4365 *
4366 * Frees the PRD table.
4367 *
4368 * May be used as the port_stop() entry in ata_port_operations.
4369 *
4370 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004371 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004372 */
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374void ata_port_stop (struct ata_port *ap)
4375{
4376 struct device *dev = ap->host_set->dev;
4377
4378 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004379 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380}
4381
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004382void ata_host_stop (struct ata_host_set *host_set)
4383{
4384 if (host_set->mmio_base)
4385 iounmap(host_set->mmio_base);
4386}
4387
4388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389/**
4390 * ata_host_remove - Unregister SCSI host structure with upper layers
4391 * @ap: Port to unregister
4392 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4393 *
4394 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004395 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 */
4397
4398static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4399{
4400 struct Scsi_Host *sh = ap->host;
4401
4402 DPRINTK("ENTER\n");
4403
4404 if (do_unregister)
4405 scsi_remove_host(sh);
4406
4407 ap->ops->port_stop(ap);
4408}
4409
4410/**
4411 * ata_host_init - Initialize an ata_port structure
4412 * @ap: Structure to initialize
4413 * @host: associated SCSI mid-layer structure
4414 * @host_set: Collection of hosts to which @ap belongs
4415 * @ent: Probe information provided by low-level driver
4416 * @port_no: Port number associated with this ata_port
4417 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004418 * Initialize a new ata_port structure, and its associated
4419 * scsi_host.
4420 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004422 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 */
4424
4425static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4426 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004427 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
4429 unsigned int i;
4430
4431 host->max_id = 16;
4432 host->max_lun = 1;
4433 host->max_channel = 1;
4434 host->unique_id = ata_unique_id++;
4435 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 ap->flags = ATA_FLAG_PORT_DISABLED;
4438 ap->id = host->unique_id;
4439 ap->host = host;
4440 ap->ctl = ATA_DEVCTL_OBS;
4441 ap->host_set = host_set;
4442 ap->port_no = port_no;
4443 ap->hard_port_no =
4444 ent->legacy_mode ? ent->hard_port_no : port_no;
4445 ap->pio_mask = ent->pio_mask;
4446 ap->mwdma_mask = ent->mwdma_mask;
4447 ap->udma_mask = ent->udma_mask;
4448 ap->flags |= ent->host_flags;
4449 ap->ops = ent->port_ops;
4450 ap->cbl = ATA_CBL_NONE;
4451 ap->active_tag = ATA_TAG_POISON;
4452 ap->last_ctl = 0xFF;
4453
Tejun Heo86e45b62006-03-05 15:29:09 +09004454 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004455 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Tejun Heoacf356b2006-03-24 14:07:50 +09004457 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4458 struct ata_device *dev = &ap->device[i];
4459 dev->devno = i;
4460 dev->pio_mask = UINT_MAX;
4461 dev->mwdma_mask = UINT_MAX;
4462 dev->udma_mask = UINT_MAX;
4463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
4465#ifdef ATA_IRQ_TRAP
4466 ap->stats.unhandled_irq = 1;
4467 ap->stats.idle_irq = 1;
4468#endif
4469
4470 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4471}
4472
4473/**
4474 * ata_host_add - Attach low-level ATA driver to system
4475 * @ent: Information provided by low-level driver
4476 * @host_set: Collections of ports to which we add
4477 * @port_no: Port number associated with this host
4478 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004479 * Attach low-level ATA driver to system.
4480 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004482 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 *
4484 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004485 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 */
4487
Jeff Garzik057ace52005-10-22 14:27:05 -04004488static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 struct ata_host_set *host_set,
4490 unsigned int port_no)
4491{
4492 struct Scsi_Host *host;
4493 struct ata_port *ap;
4494 int rc;
4495
4496 DPRINTK("ENTER\n");
4497 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4498 if (!host)
4499 return NULL;
4500
Tejun Heo30afc842006-03-18 18:40:14 +09004501 host->transportt = &ata_scsi_transport_template;
4502
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 ap = (struct ata_port *) &host->hostdata[0];
4504
4505 ata_host_init(ap, host, host_set, ent, port_no);
4506
4507 rc = ap->ops->port_start(ap);
4508 if (rc)
4509 goto err_out;
4510
4511 return ap;
4512
4513err_out:
4514 scsi_host_put(host);
4515 return NULL;
4516}
4517
4518/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004519 * ata_device_add - Register hardware device with ATA and SCSI layers
4520 * @ent: Probe information describing hardware device to be registered
4521 *
4522 * This function processes the information provided in the probe
4523 * information struct @ent, allocates the necessary ATA and SCSI
4524 * host information structures, initializes them, and registers
4525 * everything with requisite kernel subsystems.
4526 *
4527 * This function requests irqs, probes the ATA bus, and probes
4528 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 *
4530 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004531 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 *
4533 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004534 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 */
4536
Jeff Garzik057ace52005-10-22 14:27:05 -04004537int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538{
4539 unsigned int count = 0, i;
4540 struct device *dev = ent->dev;
4541 struct ata_host_set *host_set;
4542
4543 DPRINTK("ENTER\n");
4544 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004545 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4547 if (!host_set)
4548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 spin_lock_init(&host_set->lock);
4550
4551 host_set->dev = dev;
4552 host_set->n_ports = ent->n_ports;
4553 host_set->irq = ent->irq;
4554 host_set->mmio_base = ent->mmio_base;
4555 host_set->private_data = ent->private_data;
4556 host_set->ops = ent->port_ops;
4557
4558 /* register each port bound to this device */
4559 for (i = 0; i < ent->n_ports; i++) {
4560 struct ata_port *ap;
4561 unsigned long xfer_mode_mask;
4562
4563 ap = ata_host_add(ent, host_set, i);
4564 if (!ap)
4565 goto err_out;
4566
4567 host_set->ports[i] = ap;
4568 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4569 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4570 (ap->pio_mask << ATA_SHIFT_PIO);
4571
4572 /* print per-port info to dmesg */
4573 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4574 "bmdma 0x%lX irq %lu\n",
4575 ap->id,
4576 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4577 ata_mode_string(xfer_mode_mask),
4578 ap->ioaddr.cmd_addr,
4579 ap->ioaddr.ctl_addr,
4580 ap->ioaddr.bmdma_addr,
4581 ent->irq);
4582
4583 ata_chk_status(ap);
4584 host_set->ops->irq_clear(ap);
4585 count++;
4586 }
4587
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004588 if (!count)
4589 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
4591 /* obtain irq, that is shared between channels */
4592 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4593 DRV_NAME, host_set))
4594 goto err_out;
4595
4596 /* perform each probe synchronously */
4597 DPRINTK("probe begin\n");
4598 for (i = 0; i < count; i++) {
4599 struct ata_port *ap;
4600 int rc;
4601
4602 ap = host_set->ports[i];
4603
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004604 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004606 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
4608 if (rc) {
4609 /* FIXME: do something useful here?
4610 * Current libata behavior will
4611 * tear down everything when
4612 * the module is removed
4613 * or the h/w is unplugged.
4614 */
4615 }
4616
4617 rc = scsi_add_host(ap->host, dev);
4618 if (rc) {
4619 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4620 ap->id);
4621 /* FIXME: do something useful here */
4622 /* FIXME: handle unconditional calls to
4623 * scsi_scan_host and ata_host_remove, below,
4624 * at the very least
4625 */
4626 }
4627 }
4628
4629 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004630 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 for (i = 0; i < count; i++) {
4632 struct ata_port *ap = host_set->ports[i];
4633
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004634 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 }
4636
4637 dev_set_drvdata(dev, host_set);
4638
4639 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4640 return ent->n_ports; /* success */
4641
4642err_out:
4643 for (i = 0; i < count; i++) {
4644 ata_host_remove(host_set->ports[i], 1);
4645 scsi_host_put(host_set->ports[i]->host);
4646 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004647err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 kfree(host_set);
4649 VPRINTK("EXIT, returning 0\n");
4650 return 0;
4651}
4652
4653/**
Alan Cox17b14452005-09-15 15:44:00 +01004654 * ata_host_set_remove - PCI layer callback for device removal
4655 * @host_set: ATA host set that was removed
4656 *
4657 * Unregister all objects associated with this host set. Free those
4658 * objects.
4659 *
4660 * LOCKING:
4661 * Inherited from calling layer (may sleep).
4662 */
4663
Alan Cox17b14452005-09-15 15:44:00 +01004664void ata_host_set_remove(struct ata_host_set *host_set)
4665{
4666 struct ata_port *ap;
4667 unsigned int i;
4668
4669 for (i = 0; i < host_set->n_ports; i++) {
4670 ap = host_set->ports[i];
4671 scsi_remove_host(ap->host);
4672 }
4673
4674 free_irq(host_set->irq, host_set);
4675
4676 for (i = 0; i < host_set->n_ports; i++) {
4677 ap = host_set->ports[i];
4678
4679 ata_scsi_release(ap->host);
4680
4681 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4682 struct ata_ioports *ioaddr = &ap->ioaddr;
4683
4684 if (ioaddr->cmd_addr == 0x1f0)
4685 release_region(0x1f0, 8);
4686 else if (ioaddr->cmd_addr == 0x170)
4687 release_region(0x170, 8);
4688 }
4689
4690 scsi_host_put(ap->host);
4691 }
4692
4693 if (host_set->ops->host_stop)
4694 host_set->ops->host_stop(host_set);
4695
4696 kfree(host_set);
4697}
4698
4699/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 * ata_scsi_release - SCSI layer callback hook for host unload
4701 * @host: libata host to be unloaded
4702 *
4703 * Performs all duties necessary to shut down a libata port...
4704 * Kill port kthread, disable port, and release resources.
4705 *
4706 * LOCKING:
4707 * Inherited from SCSI layer.
4708 *
4709 * RETURNS:
4710 * One.
4711 */
4712
4713int ata_scsi_release(struct Scsi_Host *host)
4714{
4715 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004716 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
4718 DPRINTK("ENTER\n");
4719
4720 ap->ops->port_disable(ap);
4721 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004722 for (i = 0; i < ATA_MAX_DEVICES; i++)
4723 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
4725 DPRINTK("EXIT\n");
4726 return 1;
4727}
4728
4729/**
4730 * ata_std_ports - initialize ioaddr with standard port offsets.
4731 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004732 *
4733 * Utility function which initializes data_addr, error_addr,
4734 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4735 * device_addr, status_addr, and command_addr to standard offsets
4736 * relative to cmd_addr.
4737 *
4738 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 */
Edward Falk0baab862005-06-02 18:17:13 -04004740
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741void ata_std_ports(struct ata_ioports *ioaddr)
4742{
4743 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4744 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4745 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4746 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4747 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4748 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4749 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4750 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4751 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4752 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4753}
4754
Edward Falk0baab862005-06-02 18:17:13 -04004755
Jeff Garzik374b1872005-08-30 05:42:52 -04004756#ifdef CONFIG_PCI
4757
4758void ata_pci_host_stop (struct ata_host_set *host_set)
4759{
4760 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4761
4762 pci_iounmap(pdev, host_set->mmio_base);
4763}
4764
Edward Falk0baab862005-06-02 18:17:13 -04004765/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 * ata_pci_remove_one - PCI layer callback for device removal
4767 * @pdev: PCI device that was removed
4768 *
4769 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004770 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 * Handle this by unregistering all objects associated
4772 * with this PCI device. Free those objects. Then finally
4773 * release PCI resources and disable device.
4774 *
4775 * LOCKING:
4776 * Inherited from PCI layer (may sleep).
4777 */
4778
4779void ata_pci_remove_one (struct pci_dev *pdev)
4780{
4781 struct device *dev = pci_dev_to_dev(pdev);
4782 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Alan Cox17b14452005-09-15 15:44:00 +01004784 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 pci_release_regions(pdev);
4786 pci_disable_device(pdev);
4787 dev_set_drvdata(dev, NULL);
4788}
4789
4790/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004791int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
4793 unsigned long tmp = 0;
4794
4795 switch (bits->width) {
4796 case 1: {
4797 u8 tmp8 = 0;
4798 pci_read_config_byte(pdev, bits->reg, &tmp8);
4799 tmp = tmp8;
4800 break;
4801 }
4802 case 2: {
4803 u16 tmp16 = 0;
4804 pci_read_config_word(pdev, bits->reg, &tmp16);
4805 tmp = tmp16;
4806 break;
4807 }
4808 case 4: {
4809 u32 tmp32 = 0;
4810 pci_read_config_dword(pdev, bits->reg, &tmp32);
4811 tmp = tmp32;
4812 break;
4813 }
4814
4815 default:
4816 return -EINVAL;
4817 }
4818
4819 tmp &= bits->mask;
4820
4821 return (tmp == bits->val) ? 1 : 0;
4822}
Jens Axboe9b847542006-01-06 09:28:07 +01004823
4824int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4825{
4826 pci_save_state(pdev);
4827 pci_disable_device(pdev);
4828 pci_set_power_state(pdev, PCI_D3hot);
4829 return 0;
4830}
4831
4832int ata_pci_device_resume(struct pci_dev *pdev)
4833{
4834 pci_set_power_state(pdev, PCI_D0);
4835 pci_restore_state(pdev);
4836 pci_enable_device(pdev);
4837 pci_set_master(pdev);
4838 return 0;
4839}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840#endif /* CONFIG_PCI */
4841
4842
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843static int __init ata_init(void)
4844{
4845 ata_wq = create_workqueue("ata");
4846 if (!ata_wq)
4847 return -ENOMEM;
4848
4849 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4850 return 0;
4851}
4852
4853static void __exit ata_exit(void)
4854{
4855 destroy_workqueue(ata_wq);
4856}
4857
4858module_init(ata_init);
4859module_exit(ata_exit);
4860
Jeff Garzik67846b32005-10-05 02:58:32 -04004861static unsigned long ratelimit_time;
4862static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4863
4864int ata_ratelimit(void)
4865{
4866 int rc;
4867 unsigned long flags;
4868
4869 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4870
4871 if (time_after(jiffies, ratelimit_time)) {
4872 rc = 1;
4873 ratelimit_time = jiffies + (HZ/5);
4874 } else
4875 rc = 0;
4876
4877 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4878
4879 return rc;
4880}
4881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882/*
4883 * libata is essentially a library of internal helper functions for
4884 * low-level ATA host controller drivers. As such, the API/ABI is
4885 * likely to change as new drivers are added and updated.
4886 * Do not depend on ABI/API stability.
4887 */
4888
4889EXPORT_SYMBOL_GPL(ata_std_bios_param);
4890EXPORT_SYMBOL_GPL(ata_std_ports);
4891EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004892EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893EXPORT_SYMBOL_GPL(ata_sg_init);
4894EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09004895EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4897EXPORT_SYMBOL_GPL(ata_eng_timeout);
4898EXPORT_SYMBOL_GPL(ata_tf_load);
4899EXPORT_SYMBOL_GPL(ata_tf_read);
4900EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4901EXPORT_SYMBOL_GPL(ata_std_dev_select);
4902EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4903EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4904EXPORT_SYMBOL_GPL(ata_check_status);
4905EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906EXPORT_SYMBOL_GPL(ata_exec_command);
4907EXPORT_SYMBOL_GPL(ata_port_start);
4908EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004909EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910EXPORT_SYMBOL_GPL(ata_interrupt);
4911EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06004912EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4914EXPORT_SYMBOL_GPL(ata_bmdma_start);
4915EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4916EXPORT_SYMBOL_GPL(ata_bmdma_status);
4917EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4918EXPORT_SYMBOL_GPL(ata_port_probe);
4919EXPORT_SYMBOL_GPL(sata_phy_reset);
4920EXPORT_SYMBOL_GPL(__sata_phy_reset);
4921EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004922EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004923EXPORT_SYMBOL_GPL(ata_std_softreset);
4924EXPORT_SYMBOL_GPL(sata_std_hardreset);
4925EXPORT_SYMBOL_GPL(ata_std_postreset);
4926EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004927EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09004928EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004930EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004931EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09004932EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4934EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4935EXPORT_SYMBOL_GPL(ata_scsi_error);
4936EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4937EXPORT_SYMBOL_GPL(ata_scsi_release);
4938EXPORT_SYMBOL_GPL(ata_host_intr);
4939EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09004940EXPORT_SYMBOL_GPL(ata_id_string);
4941EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004943EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4944EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
Alan Cox1bc4ccf2006-01-09 17:18:14 +00004946EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04004947EXPORT_SYMBOL_GPL(ata_timing_compute);
4948EXPORT_SYMBOL_GPL(ata_timing_merge);
4949
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950#ifdef CONFIG_PCI
4951EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004952EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4954EXPORT_SYMBOL_GPL(ata_pci_init_one);
4955EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01004956EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4957EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Alan Cox67951ad2006-03-22 15:55:54 +00004958EXPORT_SYMBOL_GPL(ata_pci_default_filter);
4959EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01004961
4962EXPORT_SYMBOL_GPL(ata_device_suspend);
4963EXPORT_SYMBOL_GPL(ata_device_resume);
4964EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4965EXPORT_SYMBOL_GPL(ata_scsi_device_resume);