blob: d1530b9fd8e7078f3c16e6cae5f0486c589054fb [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
Tejun Heo0b8efb02006-03-24 15:25:31 +0900397static void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
398{
399 if (ata_dev_present(dev)) {
400 printk(KERN_WARNING "ata%u: dev %u disabled\n",
401 ap->id, dev->devno);
402 dev->class++;
403 }
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/**
407 * ata_pio_devchk - PATA device presence detection
408 * @ap: ATA channel to examine
409 * @device: Device to examine (starting at zero)
410 *
411 * This technique was originally described in
412 * Hale Landis's ATADRVR (www.ata-atapi.com), and
413 * later found its way into the ATA/ATAPI spec.
414 *
415 * Write a pattern to the ATA shadow registers,
416 * and if a device is present, it will respond by
417 * correctly storing and echoing back the
418 * ATA shadow register contents.
419 *
420 * LOCKING:
421 * caller.
422 */
423
424static unsigned int ata_pio_devchk(struct ata_port *ap,
425 unsigned int device)
426{
427 struct ata_ioports *ioaddr = &ap->ioaddr;
428 u8 nsect, lbal;
429
430 ap->ops->dev_select(ap, device);
431
432 outb(0x55, ioaddr->nsect_addr);
433 outb(0xaa, ioaddr->lbal_addr);
434
435 outb(0xaa, ioaddr->nsect_addr);
436 outb(0x55, ioaddr->lbal_addr);
437
438 outb(0x55, ioaddr->nsect_addr);
439 outb(0xaa, ioaddr->lbal_addr);
440
441 nsect = inb(ioaddr->nsect_addr);
442 lbal = inb(ioaddr->lbal_addr);
443
444 if ((nsect == 0x55) && (lbal == 0xaa))
445 return 1; /* we found a device */
446
447 return 0; /* nothing found */
448}
449
450/**
451 * ata_mmio_devchk - PATA device presence detection
452 * @ap: ATA channel to examine
453 * @device: Device to examine (starting at zero)
454 *
455 * This technique was originally described in
456 * Hale Landis's ATADRVR (www.ata-atapi.com), and
457 * later found its way into the ATA/ATAPI spec.
458 *
459 * Write a pattern to the ATA shadow registers,
460 * and if a device is present, it will respond by
461 * correctly storing and echoing back the
462 * ATA shadow register contents.
463 *
464 * LOCKING:
465 * caller.
466 */
467
468static unsigned int ata_mmio_devchk(struct ata_port *ap,
469 unsigned int device)
470{
471 struct ata_ioports *ioaddr = &ap->ioaddr;
472 u8 nsect, lbal;
473
474 ap->ops->dev_select(ap, device);
475
476 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
477 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
478
479 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
480 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
481
482 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
483 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
484
485 nsect = readb((void __iomem *) ioaddr->nsect_addr);
486 lbal = readb((void __iomem *) ioaddr->lbal_addr);
487
488 if ((nsect == 0x55) && (lbal == 0xaa))
489 return 1; /* we found a device */
490
491 return 0; /* nothing found */
492}
493
494/**
495 * ata_devchk - PATA device presence detection
496 * @ap: ATA channel to examine
497 * @device: Device to examine (starting at zero)
498 *
499 * Dispatch ATA device presence detection, depending
500 * on whether we are using PIO or MMIO to talk to the
501 * ATA shadow registers.
502 *
503 * LOCKING:
504 * caller.
505 */
506
507static unsigned int ata_devchk(struct ata_port *ap,
508 unsigned int device)
509{
510 if (ap->flags & ATA_FLAG_MMIO)
511 return ata_mmio_devchk(ap, device);
512 return ata_pio_devchk(ap, device);
513}
514
515/**
516 * ata_dev_classify - determine device type based on ATA-spec signature
517 * @tf: ATA taskfile register set for device to be identified
518 *
519 * Determine from taskfile register contents whether a device is
520 * ATA or ATAPI, as per "Signature and persistence" section
521 * of ATA/PI spec (volume 1, sect 5.14).
522 *
523 * LOCKING:
524 * None.
525 *
526 * RETURNS:
527 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
528 * the event of failure.
529 */
530
Jeff Garzik057ace52005-10-22 14:27:05 -0400531unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 /* Apple's open source Darwin code hints that some devices only
534 * put a proper signature into the LBA mid/high registers,
535 * So, we only check those. It's sufficient for uniqueness.
536 */
537
538 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
539 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
540 DPRINTK("found ATA device by sig\n");
541 return ATA_DEV_ATA;
542 }
543
544 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
545 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
546 DPRINTK("found ATAPI device by sig\n");
547 return ATA_DEV_ATAPI;
548 }
549
550 DPRINTK("unknown device\n");
551 return ATA_DEV_UNKNOWN;
552}
553
554/**
555 * ata_dev_try_classify - Parse returned ATA device signature
556 * @ap: ATA channel to examine
557 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900558 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 *
560 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
561 * an ATA/ATAPI-defined set of values is placed in the ATA
562 * shadow registers, indicating the results of device detection
563 * and diagnostics.
564 *
565 * Select the ATA device, and read the values from the ATA shadow
566 * registers. Then parse according to the Error register value,
567 * and the spec-defined values examined by ata_dev_classify().
568 *
569 * LOCKING:
570 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900571 *
572 * RETURNS:
573 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
575
Tejun Heob4dc7622006-01-24 17:05:22 +0900576static unsigned int
577ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct ata_taskfile tf;
580 unsigned int class;
581 u8 err;
582
583 ap->ops->dev_select(ap, device);
584
585 memset(&tf, 0, sizeof(tf));
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400588 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900589 if (r_err)
590 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* see if device passed diags */
593 if (err == 1)
594 /* do nothing */ ;
595 else if ((device == 0) && (err == 0x81))
596 /* do nothing */ ;
597 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900598 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Tejun Heob4dc7622006-01-24 17:05:22 +0900600 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900604 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900606 return ATA_DEV_NONE;
607 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900611 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * @id: IDENTIFY DEVICE results we will examine
613 * @s: string into which data is output
614 * @ofs: offset into identify device page
615 * @len: length of string to return. must be an even number.
616 *
617 * The strings in the IDENTIFY DEVICE page are broken up into
618 * 16-bit chunks. Run through the string, and output each
619 * 8-bit chunk linearly, regardless of platform.
620 *
621 * LOCKING:
622 * caller.
623 */
624
Tejun Heo6a62a042006-02-13 10:02:46 +0900625void ata_id_string(const u16 *id, unsigned char *s,
626 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 unsigned int c;
629
630 while (len > 0) {
631 c = id[ofs] >> 8;
632 *s = c;
633 s++;
634
635 c = id[ofs] & 0xff;
636 *s = c;
637 s++;
638
639 ofs++;
640 len -= 2;
641 }
642}
643
Tejun Heo0e949ff2006-02-12 22:47:04 +0900644/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900645 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900646 * @id: IDENTIFY DEVICE results we will examine
647 * @s: string into which data is output
648 * @ofs: offset into identify device page
649 * @len: length of string to return. must be an odd number.
650 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900651 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900652 * trims trailing spaces and terminates the resulting string with
653 * null. @len must be actual maximum length (even number) + 1.
654 *
655 * LOCKING:
656 * caller.
657 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900658void ata_id_c_string(const u16 *id, unsigned char *s,
659 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900660{
661 unsigned char *p;
662
663 WARN_ON(!(len & 1));
664
Tejun Heo6a62a042006-02-13 10:02:46 +0900665 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900666
667 p = s + strnlen(s, len - 1);
668 while (p > s && p[-1] == ' ')
669 p--;
670 *p = '\0';
671}
Edward Falk0baab862005-06-02 18:17:13 -0400672
Tejun Heo29407402006-02-12 22:47:04 +0900673static u64 ata_id_n_sectors(const u16 *id)
674{
675 if (ata_id_has_lba(id)) {
676 if (ata_id_has_lba48(id))
677 return ata_id_u64(id, 100);
678 else
679 return ata_id_u32(id, 60);
680 } else {
681 if (ata_id_current_chs_valid(id))
682 return ata_id_u32(id, 57);
683 else
684 return id[1] * id[3] * id[6];
685 }
686}
687
Edward Falk0baab862005-06-02 18:17:13 -0400688/**
689 * ata_noop_dev_select - Select device 0/1 on ATA bus
690 * @ap: ATA channel to manipulate
691 * @device: ATA device (numbered from zero) to select
692 *
693 * This function performs no actual function.
694 *
695 * May be used as the dev_select() entry in ata_port_operations.
696 *
697 * LOCKING:
698 * caller.
699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
701{
702}
703
Edward Falk0baab862005-06-02 18:17:13 -0400704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/**
706 * ata_std_dev_select - Select device 0/1 on ATA bus
707 * @ap: ATA channel to manipulate
708 * @device: ATA device (numbered from zero) to select
709 *
710 * Use the method defined in the ATA specification to
711 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400712 * ATA channel. Works with both PIO and MMIO.
713 *
714 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 *
716 * LOCKING:
717 * caller.
718 */
719
720void ata_std_dev_select (struct ata_port *ap, unsigned int device)
721{
722 u8 tmp;
723
724 if (device == 0)
725 tmp = ATA_DEVICE_OBS;
726 else
727 tmp = ATA_DEVICE_OBS | ATA_DEV1;
728
729 if (ap->flags & ATA_FLAG_MMIO) {
730 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
731 } else {
732 outb(tmp, ap->ioaddr.device_addr);
733 }
734 ata_pause(ap); /* needed; also flushes, for mmio */
735}
736
737/**
738 * ata_dev_select - Select device 0/1 on ATA bus
739 * @ap: ATA channel to manipulate
740 * @device: ATA device (numbered from zero) to select
741 * @wait: non-zero to wait for Status register BSY bit to clear
742 * @can_sleep: non-zero if context allows sleeping
743 *
744 * Use the method defined in the ATA specification to
745 * make either device 0, or device 1, active on the
746 * ATA channel.
747 *
748 * This is a high-level version of ata_std_dev_select(),
749 * which additionally provides the services of inserting
750 * the proper pauses and status polling, where needed.
751 *
752 * LOCKING:
753 * caller.
754 */
755
756void ata_dev_select(struct ata_port *ap, unsigned int device,
757 unsigned int wait, unsigned int can_sleep)
758{
759 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
760 ap->id, device, wait);
761
762 if (wait)
763 ata_wait_idle(ap);
764
765 ap->ops->dev_select(ap, device);
766
767 if (wait) {
768 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
769 msleep(150);
770 ata_wait_idle(ap);
771 }
772}
773
774/**
775 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900776 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900778 * Dump selected 16-bit words from the given IDENTIFY DEVICE
779 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 *
781 * LOCKING:
782 * caller.
783 */
784
Tejun Heo0bd33002006-02-12 22:47:05 +0900785static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 DPRINTK("49==0x%04x "
788 "53==0x%04x "
789 "63==0x%04x "
790 "64==0x%04x "
791 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900792 id[49],
793 id[53],
794 id[63],
795 id[64],
796 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 DPRINTK("80==0x%04x "
798 "81==0x%04x "
799 "82==0x%04x "
800 "83==0x%04x "
801 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900802 id[80],
803 id[81],
804 id[82],
805 id[83],
806 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 DPRINTK("88==0x%04x "
808 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900809 id[88],
810 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Tejun Heocb95d562006-03-06 04:31:56 +0900813/**
814 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
815 * @id: IDENTIFY data to compute xfer mask from
816 *
817 * Compute the xfermask for this device. This is not as trivial
818 * as it seems if we must consider early devices correctly.
819 *
820 * FIXME: pre IDE drive timing (do we care ?).
821 *
822 * LOCKING:
823 * None.
824 *
825 * RETURNS:
826 * Computed xfermask
827 */
828static unsigned int ata_id_xfermask(const u16 *id)
829{
830 unsigned int pio_mask, mwdma_mask, udma_mask;
831
832 /* Usual case. Word 53 indicates word 64 is valid */
833 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
834 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
835 pio_mask <<= 3;
836 pio_mask |= 0x7;
837 } else {
838 /* If word 64 isn't valid then Word 51 high byte holds
839 * the PIO timing number for the maximum. Turn it into
840 * a mask.
841 */
842 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
843
844 /* But wait.. there's more. Design your standards by
845 * committee and you too can get a free iordy field to
846 * process. However its the speeds not the modes that
847 * are supported... Note drivers using the timing API
848 * will get this right anyway
849 */
850 }
851
852 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +0900853
854 udma_mask = 0;
855 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
856 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900857
858 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
859}
860
Tejun Heo86e45b62006-03-05 15:29:09 +0900861/**
862 * ata_port_queue_task - Queue port_task
863 * @ap: The ata_port to queue port_task for
864 *
865 * Schedule @fn(@data) for execution after @delay jiffies using
866 * port_task. There is one port_task per port and it's the
867 * user(low level driver)'s responsibility to make sure that only
868 * one task is active at any given time.
869 *
870 * libata core layer takes care of synchronization between
871 * port_task and EH. ata_port_queue_task() may be ignored for EH
872 * synchronization.
873 *
874 * LOCKING:
875 * Inherited from caller.
876 */
877void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
878 unsigned long delay)
879{
880 int rc;
881
Tejun Heo2e755f62006-03-05 15:29:09 +0900882 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900883 return;
884
885 PREPARE_WORK(&ap->port_task, fn, data);
886
887 if (!delay)
888 rc = queue_work(ata_wq, &ap->port_task);
889 else
890 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
891
892 /* rc == 0 means that another user is using port task */
893 WARN_ON(rc == 0);
894}
895
896/**
897 * ata_port_flush_task - Flush port_task
898 * @ap: The ata_port to flush port_task for
899 *
900 * After this function completes, port_task is guranteed not to
901 * be running or scheduled.
902 *
903 * LOCKING:
904 * Kernel thread context (may sleep)
905 */
906void ata_port_flush_task(struct ata_port *ap)
907{
908 unsigned long flags;
909
910 DPRINTK("ENTER\n");
911
912 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900913 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900914 spin_unlock_irqrestore(&ap->host_set->lock, flags);
915
916 DPRINTK("flush #1\n");
917 flush_workqueue(ata_wq);
918
919 /*
920 * At this point, if a task is running, it's guaranteed to see
921 * the FLUSH flag; thus, it will never queue pio tasks again.
922 * Cancel and flush.
923 */
924 if (!cancel_delayed_work(&ap->port_task)) {
925 DPRINTK("flush #2\n");
926 flush_workqueue(ata_wq);
927 }
928
929 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900930 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900931 spin_unlock_irqrestore(&ap->host_set->lock, flags);
932
933 DPRINTK("EXIT\n");
934}
935
Tejun Heo77853bf2006-01-23 13:09:36 +0900936void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900937{
Tejun Heo77853bf2006-01-23 13:09:36 +0900938 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900939
Tejun Heo77853bf2006-01-23 13:09:36 +0900940 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900941 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900942}
943
944/**
945 * ata_exec_internal - execute libata internal command
946 * @ap: Port to which the command is sent
947 * @dev: Device to which the command is sent
948 * @tf: Taskfile registers for the command and the result
949 * @dma_dir: Data tranfer direction of the command
950 * @buf: Data buffer of the command
951 * @buflen: Length of data buffer
952 *
953 * Executes libata internal command with timeout. @tf contains
954 * command on entry and result on return. Timeout and error
955 * conditions are reported via return value. No recovery action
956 * is taken after a command times out. It's caller's duty to
957 * clean up after timeout.
958 *
959 * LOCKING:
960 * None. Should be called with kernel context, might sleep.
961 */
962
963static unsigned
964ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
965 struct ata_taskfile *tf,
966 int dma_dir, void *buf, unsigned int buflen)
967{
968 u8 command = tf->command;
969 struct ata_queued_cmd *qc;
970 DECLARE_COMPLETION(wait);
971 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900972 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900973
974 spin_lock_irqsave(&ap->host_set->lock, flags);
975
976 qc = ata_qc_new_init(ap, dev);
977 BUG_ON(qc == NULL);
978
979 qc->tf = *tf;
980 qc->dma_dir = dma_dir;
981 if (dma_dir != DMA_NONE) {
982 ata_sg_init_one(qc, buf, buflen);
983 qc->nsect = buflen / ATA_SECT_SIZE;
984 }
985
Tejun Heo77853bf2006-01-23 13:09:36 +0900986 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900987 qc->complete_fn = ata_qc_complete_internal;
988
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900989 qc->err_mask = ata_qc_issue(qc);
990 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900991 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900992
993 spin_unlock_irqrestore(&ap->host_set->lock, flags);
994
995 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
Albert Lee41ade502006-03-14 11:19:04 +0800996 ata_port_flush_task(ap);
997
Tejun Heoa2a7a662005-12-13 14:48:31 +0900998 spin_lock_irqsave(&ap->host_set->lock, flags);
999
1000 /* We're racing with irq here. If we lose, the
1001 * following test prevents us from completing the qc
1002 * again. If completion irq occurs after here but
1003 * before the caller cleans up, it will result in a
1004 * spurious interrupt. We can live with that.
1005 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001006 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001007 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001008 ata_qc_complete(qc);
1009 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1010 ap->id, command);
1011 }
1012
1013 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1014 }
1015
Tejun Heo77853bf2006-01-23 13:09:36 +09001016 *tf = qc->tf;
1017 err_mask = qc->err_mask;
1018
1019 ata_qc_free(qc);
1020
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001021 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1022 * Until those drivers are fixed, we detect the condition
1023 * here, fail the command with AC_ERR_SYSTEM and reenable the
1024 * port.
1025 *
1026 * Note that this doesn't change any behavior as internal
1027 * command failure results in disabling the device in the
1028 * higher layer for LLDDs without new reset/EH callbacks.
1029 *
1030 * Kill the following code as soon as those drivers are fixed.
1031 */
1032 if (ap->flags & ATA_FLAG_PORT_DISABLED) {
1033 err_mask |= AC_ERR_SYSTEM;
1034 ata_port_probe(ap);
1035 }
1036
Tejun Heo77853bf2006-01-23 13:09:36 +09001037 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001041 * ata_pio_need_iordy - check if iordy needed
1042 * @adev: ATA device
1043 *
1044 * Check if the current speed of the device requires IORDY. Used
1045 * by various controllers for chip configuration.
1046 */
1047
1048unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1049{
1050 int pio;
1051 int speed = adev->pio_mode - XFER_PIO_0;
1052
1053 if (speed < 2)
1054 return 0;
1055 if (speed > 2)
1056 return 1;
1057
1058 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1059
1060 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1061 pio = adev->id[ATA_ID_EIDE_PIO];
1062 /* Is the speed faster than the drive allows non IORDY ? */
1063 if (pio) {
1064 /* This is cycle times not frequency - watch the logic! */
1065 if (pio > 240) /* PIO2 is 240nS per cycle */
1066 return 1;
1067 return 0;
1068 }
1069 }
1070 return 0;
1071}
1072
1073/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001074 * ata_dev_read_id - Read ID data from the specified device
1075 * @ap: port on which target device resides
1076 * @dev: target device
1077 * @p_class: pointer to class of the target device (may be changed)
1078 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +09001079 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +09001080 *
1081 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1082 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1083 * devices. This function also takes care of EDD signature
1084 * misreporting (to be removed once EDD support is gone) and
1085 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
1086 *
1087 * LOCKING:
1088 * Kernel thread context (may sleep)
1089 *
1090 * RETURNS:
1091 * 0 on success, -errno otherwise.
1092 */
1093static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +09001094 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001095{
1096 unsigned int class = *p_class;
1097 unsigned int using_edd;
1098 struct ata_taskfile tf;
1099 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +09001100 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001101 const char *reason;
1102 int rc;
1103
1104 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1105
1106 if (ap->ops->probe_reset ||
1107 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1108 using_edd = 0;
1109 else
1110 using_edd = 1;
1111
1112 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1113
Tejun Heod9572b12006-03-01 16:09:35 +09001114 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1115 if (id == NULL) {
1116 rc = -ENOMEM;
1117 reason = "out of memory";
1118 goto err_out;
1119 }
1120
Tejun Heo49016ac2006-02-21 02:12:11 +09001121 retry:
1122 ata_tf_init(ap, &tf, dev->devno);
1123
1124 switch (class) {
1125 case ATA_DEV_ATA:
1126 tf.command = ATA_CMD_ID_ATA;
1127 break;
1128 case ATA_DEV_ATAPI:
1129 tf.command = ATA_CMD_ID_ATAPI;
1130 break;
1131 default:
1132 rc = -ENODEV;
1133 reason = "unsupported class";
1134 goto err_out;
1135 }
1136
1137 tf.protocol = ATA_PROT_PIO;
1138
1139 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1140 id, sizeof(id[0]) * ATA_ID_WORDS);
1141
1142 if (err_mask) {
1143 rc = -EIO;
1144 reason = "I/O error";
1145
1146 if (err_mask & ~AC_ERR_DEV)
1147 goto err_out;
1148
1149 /*
1150 * arg! EDD works for all test cases, but seems to return
1151 * the ATA signature for some ATAPI devices. Until the
1152 * reason for this is found and fixed, we fix up the mess
1153 * here. If IDENTIFY DEVICE returns command aborted
1154 * (as ATAPI devices do), then we issue an
1155 * IDENTIFY PACKET DEVICE.
1156 *
1157 * ATA software reset (SRST, the default) does not appear
1158 * to have this problem.
1159 */
1160 if ((using_edd) && (class == ATA_DEV_ATA)) {
1161 u8 err = tf.feature;
1162 if (err & ATA_ABORTED) {
1163 class = ATA_DEV_ATAPI;
1164 goto retry;
1165 }
1166 }
1167 goto err_out;
1168 }
1169
1170 swap_buf_le16(id, ATA_ID_WORDS);
1171
Tejun Heo49016ac2006-02-21 02:12:11 +09001172 /* sanity check */
1173 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1174 rc = -EINVAL;
1175 reason = "device reports illegal type";
1176 goto err_out;
1177 }
1178
1179 if (post_reset && class == ATA_DEV_ATA) {
1180 /*
1181 * The exact sequence expected by certain pre-ATA4 drives is:
1182 * SRST RESET
1183 * IDENTIFY
1184 * INITIALIZE DEVICE PARAMETERS
1185 * anything else..
1186 * Some drives were very specific about that exact sequence.
1187 */
1188 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1189 err_mask = ata_dev_init_params(ap, dev);
1190 if (err_mask) {
1191 rc = -EIO;
1192 reason = "INIT_DEV_PARAMS failed";
1193 goto err_out;
1194 }
1195
1196 /* current CHS translation info (id[53-58]) might be
1197 * changed. reread the identify device info.
1198 */
1199 post_reset = 0;
1200 goto retry;
1201 }
1202 }
1203
1204 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001205 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001206 return 0;
1207
1208 err_out:
1209 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1210 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001211 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001212 return rc;
1213}
1214
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001215static inline u8 ata_dev_knobble(const struct ata_port *ap,
1216 struct ata_device *dev)
1217{
1218 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1219}
1220
Tejun Heo49016ac2006-02-21 02:12:11 +09001221/**
Tejun Heoffeae412006-03-01 16:09:35 +09001222 * ata_dev_configure - Configure the specified ATA/ATAPI device
1223 * @ap: Port on which target device resides
1224 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001225 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 *
Tejun Heoffeae412006-03-01 16:09:35 +09001227 * Configure @dev according to @dev->id. Generic and low-level
1228 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 *
1230 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001231 * Kernel thread context (may sleep)
1232 *
1233 * RETURNS:
1234 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001236static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1237 int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Tejun Heo1148c3a2006-03-13 19:48:04 +09001239 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001240 unsigned int xfer_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001241 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (!ata_dev_present(dev)) {
1244 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001245 ap->id, dev->devno);
1246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248
Tejun Heoffeae412006-03-01 16:09:35 +09001249 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001251 /* print device capabilities */
1252 if (print_info)
1253 printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
1254 "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1255 ap->id, dev->devno, id[49], id[82], id[83],
1256 id[84], id[85], id[86], id[87], id[88]);
1257
Tejun Heo208a9932006-03-05 17:55:58 +09001258 /* initialize to-be-configured parameters */
1259 dev->flags = 0;
1260 dev->max_sectors = 0;
1261 dev->cdb_len = 0;
1262 dev->n_sectors = 0;
1263 dev->cylinders = 0;
1264 dev->heads = 0;
1265 dev->sectors = 0;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /*
1268 * common ATA, ATAPI feature tests
1269 */
1270
Tejun Heoff8854b2006-03-06 04:31:56 +09001271 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001272 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Tejun Heo1148c3a2006-03-13 19:48:04 +09001274 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* ATA-specific feature tests */
1277 if (dev->class == ATA_DEV_ATA) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001278 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001279
Tejun Heo1148c3a2006-03-13 19:48:04 +09001280 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001281 const char *lba_desc;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001282
Tejun Heo4c2d7212006-03-05 17:55:58 +09001283 lba_desc = "LBA";
1284 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001285 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001286 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001287 lba_desc = "LBA48";
1288 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04001289
1290 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001291 if (print_info)
1292 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1293 "max %s, %Lu sectors: %s\n",
1294 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001295 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001296 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001297 (unsigned long long)dev->n_sectors,
1298 lba_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001299 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001300 /* CHS */
1301
1302 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001303 dev->cylinders = id[1];
1304 dev->heads = id[3];
1305 dev->sectors = id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001306
Tejun Heo1148c3a2006-03-13 19:48:04 +09001307 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001308 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001309 dev->cylinders = id[54];
1310 dev->heads = id[55];
1311 dev->sectors = id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001312 }
1313
1314 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001315 if (print_info)
1316 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1317 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1318 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001319 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001320 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001321 (unsigned long long)dev->n_sectors,
1322 dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
Tejun Heo6e7846e2006-02-12 23:32:58 +09001325 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
1328 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001329 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001330 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1332 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001333 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 goto err_out_nosup;
1335 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001336 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001339 if (print_info)
1340 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoff8854b2006-03-06 04:31:56 +09001341 ap->id, dev->devno, ata_mode_string(xfer_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
Tejun Heo6e7846e2006-02-12 23:32:58 +09001344 ap->host->max_cmd_len = 0;
1345 for (i = 0; i < ATA_MAX_DEVICES; i++)
1346 ap->host->max_cmd_len = max_t(unsigned int,
1347 ap->host->max_cmd_len,
1348 ap->device[i].cdb_len);
1349
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001350 /* limit bridge transfers to udma5, 200 sectors */
1351 if (ata_dev_knobble(ap, dev)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001352 if (print_info)
1353 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1354 ap->id, dev->devno);
Tejun Heo5a529132006-03-24 14:07:50 +09001355 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001356 dev->max_sectors = ATA_MAX_SECTORS;
1357 }
1358
1359 if (ap->ops->dev_config)
1360 ap->ops->dev_config(ap, dev);
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365err_out_nosup:
1366 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001367 ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001369 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
1372/**
1373 * ata_bus_probe - Reset and probe ATA bus
1374 * @ap: Bus to probe
1375 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001376 * Master ATA bus probing function. Initiates a hardware-dependent
1377 * bus reset, then attempts to identify any devices found on
1378 * the bus.
1379 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001381 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 *
1383 * RETURNS:
1384 * Zero on success, non-zero on error.
1385 */
1386
1387static int ata_bus_probe(struct ata_port *ap)
1388{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001389 unsigned int classes[ATA_MAX_DEVICES];
1390 unsigned int i, rc, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Tejun Heo28ca5c52006-03-01 16:09:36 +09001392 ata_port_probe(ap);
1393
Tejun Heo20444702006-03-13 01:57:01 +09001394 /* reset and determine device classes */
1395 for (i = 0; i < ATA_MAX_DEVICES; i++)
1396 classes[i] = ATA_DEV_UNKNOWN;
Tejun Heo2061a472006-03-12 00:57:39 +09001397
Tejun Heo20444702006-03-13 01:57:01 +09001398 if (ap->ops->probe_reset) {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001399 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001400 if (rc) {
1401 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1402 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001403 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001404 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001405 ap->ops->phy_reset(ap);
1406
Tejun Heo20444702006-03-13 01:57:01 +09001407 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1408 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo28ca5c52006-03-01 16:09:36 +09001409 classes[i] = ap->device[i].class;
Tejun Heo20444702006-03-13 01:57:01 +09001410
Tejun Heo28ca5c52006-03-01 16:09:36 +09001411 ata_port_probe(ap);
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Tejun Heo20444702006-03-13 01:57:01 +09001414 for (i = 0; i < ATA_MAX_DEVICES; i++)
1415 if (classes[i] == ATA_DEV_UNKNOWN)
1416 classes[i] = ATA_DEV_NONE;
1417
Tejun Heo28ca5c52006-03-01 16:09:36 +09001418 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoffeae412006-03-01 16:09:35 +09001420 struct ata_device *dev = &ap->device[i];
1421
Tejun Heo28ca5c52006-03-01 16:09:36 +09001422 dev->class = classes[i];
1423
Tejun Heoffeae412006-03-01 16:09:35 +09001424 if (!ata_dev_present(dev))
1425 continue;
1426
1427 WARN_ON(dev->id != NULL);
1428 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1429 dev->class = ATA_DEV_NONE;
1430 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Tejun Heoffeae412006-03-01 16:09:35 +09001432
Tejun Heo4c2d7212006-03-05 17:55:58 +09001433 if (ata_dev_configure(ap, dev, 1)) {
Tejun Heoffeae412006-03-01 16:09:35 +09001434 dev->class++; /* disable device */
1435 continue;
1436 }
1437
Tejun Heoffeae412006-03-01 16:09:35 +09001438 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Tejun Heo28ca5c52006-03-01 16:09:36 +09001441 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 goto err_out_disable;
1443
1444 ata_set_mode(ap);
1445 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1446 goto err_out_disable;
1447
1448 return 0;
1449
1450err_out_disable:
1451 ap->ops->port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -1;
1453}
1454
1455/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001456 * ata_port_probe - Mark port as enabled
1457 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001459 * Modify @ap data structure such that the system
1460 * thinks that the entire port is enabled.
1461 *
1462 * LOCKING: host_set lock, or some other form of
1463 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
1465
1466void ata_port_probe(struct ata_port *ap)
1467{
1468 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1469}
1470
1471/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001472 * sata_print_link_status - Print SATA link status
1473 * @ap: SATA port to printk link status about
1474 *
1475 * This function prints link speed and status of a SATA link.
1476 *
1477 * LOCKING:
1478 * None.
1479 */
1480static void sata_print_link_status(struct ata_port *ap)
1481{
1482 u32 sstatus, tmp;
1483 const char *speed;
1484
1485 if (!ap->ops->scr_read)
1486 return;
1487
1488 sstatus = scr_read(ap, SCR_STATUS);
1489
1490 if (sata_dev_present(ap)) {
1491 tmp = (sstatus >> 4) & 0xf;
1492 if (tmp & (1 << 0))
1493 speed = "1.5";
1494 else if (tmp & (1 << 1))
1495 speed = "3.0";
1496 else
1497 speed = "<unknown>";
1498 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1499 ap->id, speed, sstatus);
1500 } else {
1501 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1502 ap->id, sstatus);
1503 }
1504}
1505
1506/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001507 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1508 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001510 * This function issues commands to standard SATA Sxxx
1511 * PHY registers, to wake up the phy (and device), and
1512 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 *
1514 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001515 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 *
1517 */
1518void __sata_phy_reset(struct ata_port *ap)
1519{
1520 u32 sstatus;
1521 unsigned long timeout = jiffies + (HZ * 5);
1522
1523 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001524 /* issue phy wake/reset */
1525 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001526 /* Couldn't find anything in SATA I/II specs, but
1527 * AHCI-1.1 10.4.2 says at least 1 ms. */
1528 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Brett Russcdcca892005-03-28 15:10:27 -05001530 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 /* wait for phy to become ready, if necessary */
1533 do {
1534 msleep(200);
1535 sstatus = scr_read(ap, SCR_STATUS);
1536 if ((sstatus & 0xf) != 1)
1537 break;
1538 } while (time_before(jiffies, timeout));
1539
Tejun Heo3be680b2005-12-19 22:35:02 +09001540 /* print link status */
1541 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001542
Tejun Heo3be680b2005-12-19 22:35:02 +09001543 /* TODO: phy layer with polling, timeouts, etc. */
1544 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001546 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1550 return;
1551
1552 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1553 ata_port_disable(ap);
1554 return;
1555 }
1556
1557 ap->cbl = ATA_CBL_SATA;
1558}
1559
1560/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001561 * sata_phy_reset - Reset SATA bus.
1562 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001564 * This function resets the SATA bus, and then probes
1565 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 *
1567 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001568 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 *
1570 */
1571void sata_phy_reset(struct ata_port *ap)
1572{
1573 __sata_phy_reset(ap);
1574 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1575 return;
1576 ata_bus_reset(ap);
1577}
1578
1579/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001580 * ata_port_disable - Disable port.
1581 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001583 * Modify @ap data structure such that the system
1584 * thinks that the entire port is disabled, and should
1585 * never attempt to probe or communicate with devices
1586 * on this port.
1587 *
1588 * LOCKING: host_set lock, or some other form of
1589 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 */
1591
1592void ata_port_disable(struct ata_port *ap)
1593{
1594 ap->device[0].class = ATA_DEV_NONE;
1595 ap->device[1].class = ATA_DEV_NONE;
1596 ap->flags |= ATA_FLAG_PORT_DISABLED;
1597}
1598
Alan Cox452503f2005-10-21 19:01:32 -04001599/*
1600 * This mode timing computation functionality is ported over from
1601 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1602 */
1603/*
1604 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1605 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1606 * for PIO 5, which is a nonstandard extension and UDMA6, which
1607 * is currently supported only by Maxtor drives.
1608 */
1609
1610static const struct ata_timing ata_timing[] = {
1611
1612 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1613 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1614 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1615 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1616
1617 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1618 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1619 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1620
1621/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1622
1623 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1624 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1625 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1626
1627 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1628 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1629 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1630
1631/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1632 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1633 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1634
1635 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1636 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1637 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1638
1639/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1640
1641 { 0xFF }
1642};
1643
1644#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1645#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1646
1647static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1648{
1649 q->setup = EZ(t->setup * 1000, T);
1650 q->act8b = EZ(t->act8b * 1000, T);
1651 q->rec8b = EZ(t->rec8b * 1000, T);
1652 q->cyc8b = EZ(t->cyc8b * 1000, T);
1653 q->active = EZ(t->active * 1000, T);
1654 q->recover = EZ(t->recover * 1000, T);
1655 q->cycle = EZ(t->cycle * 1000, T);
1656 q->udma = EZ(t->udma * 1000, UT);
1657}
1658
1659void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1660 struct ata_timing *m, unsigned int what)
1661{
1662 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1663 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1664 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1665 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1666 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1667 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1668 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1669 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1670}
1671
1672static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1673{
1674 const struct ata_timing *t;
1675
1676 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001677 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001678 return NULL;
1679 return t;
1680}
1681
1682int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1683 struct ata_timing *t, int T, int UT)
1684{
1685 const struct ata_timing *s;
1686 struct ata_timing p;
1687
1688 /*
1689 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001690 */
Alan Cox452503f2005-10-21 19:01:32 -04001691
1692 if (!(s = ata_timing_find_mode(speed)))
1693 return -EINVAL;
1694
Albert Lee75b1f2f2005-11-16 17:06:18 +08001695 memcpy(t, s, sizeof(*s));
1696
Alan Cox452503f2005-10-21 19:01:32 -04001697 /*
1698 * If the drive is an EIDE drive, it can tell us it needs extended
1699 * PIO/MW_DMA cycle timing.
1700 */
1701
1702 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1703 memset(&p, 0, sizeof(p));
1704 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1705 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1706 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1707 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1708 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1709 }
1710 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1711 }
1712
1713 /*
1714 * Convert the timing to bus clock counts.
1715 */
1716
Albert Lee75b1f2f2005-11-16 17:06:18 +08001717 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001718
1719 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001720 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1721 * S.M.A.R.T * and some other commands. We have to ensure that the
1722 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001723 */
1724
1725 if (speed > XFER_PIO_4) {
1726 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1727 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1728 }
1729
1730 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001731 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001732 */
1733
1734 if (t->act8b + t->rec8b < t->cyc8b) {
1735 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1736 t->rec8b = t->cyc8b - t->act8b;
1737 }
1738
1739 if (t->active + t->recover < t->cycle) {
1740 t->active += (t->cycle - (t->active + t->recover)) / 2;
1741 t->recover = t->cycle - t->active;
1742 }
1743
1744 return 0;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1750 return;
1751
1752 if (dev->xfer_shift == ATA_SHIFT_PIO)
1753 dev->flags |= ATA_DFLAG_PIO;
1754
1755 ata_dev_set_xfermode(ap, dev);
1756
Tejun Heo48a8a142006-03-05 17:55:58 +09001757 if (ata_dev_revalidate(ap, dev, 0)) {
1758 printk(KERN_ERR "ata%u: failed to revalidate after set "
1759 "xfermode, disabled\n", ap->id);
1760 ata_port_disable(ap);
1761 }
1762
Tejun Heo23e71c32006-03-06 04:31:57 +09001763 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1764 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
Tejun Heo23e71c32006-03-06 04:31:57 +09001767 ap->id, dev->devno,
1768 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
1771static int ata_host_set_pio(struct ata_port *ap)
1772{
Tejun Heoa6d5a512006-03-06 04:31:57 +09001773 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1776 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001777
1778 if (!ata_dev_present(dev))
1779 continue;
1780
1781 if (!dev->pio_mode) {
Alan Cox88f93a32006-03-21 16:04:12 +00001782 printk(KERN_WARNING "ata%u: no PIO support for device %d.\n", ap->id, i);
Tejun Heoa6d5a512006-03-06 04:31:57 +09001783 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09001785
1786 dev->xfer_mode = dev->pio_mode;
1787 dev->xfer_shift = ATA_SHIFT_PIO;
1788 if (ap->ops->set_piomode)
1789 ap->ops->set_piomode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791
1792 return 0;
1793}
1794
Tejun Heoa6d5a512006-03-06 04:31:57 +09001795static void ata_host_set_dma(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 int i;
1798
1799 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1800 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001801
1802 if (!ata_dev_present(dev) || !dev->dma_mode)
1803 continue;
1804
1805 dev->xfer_mode = dev->dma_mode;
1806 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1807 if (ap->ops->set_dmamode)
1808 ap->ops->set_dmamode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810}
1811
1812/**
1813 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1814 * @ap: port on which timings will be programmed
1815 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001816 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1817 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001819 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
1821static void ata_set_mode(struct ata_port *ap)
1822{
Tejun Heoa6d5a512006-03-06 04:31:57 +09001823 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Tejun Heoa6d5a512006-03-06 04:31:57 +09001825 /* step 1: calculate xfer_mask */
1826 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1827 struct ata_device *dev = &ap->device[i];
Tejun Heoacf356b2006-03-24 14:07:50 +09001828 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09001829
1830 if (!ata_dev_present(dev))
1831 continue;
1832
Tejun Heoacf356b2006-03-24 14:07:50 +09001833 ata_dev_xfermask(ap, dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09001834
Tejun Heoacf356b2006-03-24 14:07:50 +09001835 /* TODO: let LLDD filter dev->*_mask here */
1836
1837 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
1838 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
1839 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
1840 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Tejun Heoa6d5a512006-03-06 04:31:57 +09001841 }
1842
1843 /* step 2: always set host PIO timings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 rc = ata_host_set_pio(ap);
1845 if (rc)
1846 goto err_out;
1847
Tejun Heoa6d5a512006-03-06 04:31:57 +09001848 /* step 3: set host DMA timings */
1849 ata_host_set_dma(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* step 4: update devices' xfer mode */
Tejun Heoa6d5a512006-03-06 04:31:57 +09001852 for (i = 0; i < ATA_MAX_DEVICES; i++)
1853 ata_dev_set_mode(ap, &ap->device[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1856 return;
1857
1858 if (ap->ops->post_set_mode)
1859 ap->ops->post_set_mode(ap);
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return;
1862
1863err_out:
1864 ata_port_disable(ap);
1865}
1866
1867/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001868 * ata_tf_to_host - issue ATA taskfile to host controller
1869 * @ap: port to which command is being issued
1870 * @tf: ATA taskfile register set
1871 *
1872 * Issues ATA taskfile register set to ATA host controller,
1873 * with proper synchronization with interrupt handler and
1874 * other threads.
1875 *
1876 * LOCKING:
1877 * spin_lock_irqsave(host_set lock)
1878 */
1879
1880static inline void ata_tf_to_host(struct ata_port *ap,
1881 const struct ata_taskfile *tf)
1882{
1883 ap->ops->tf_load(ap, tf);
1884 ap->ops->exec_command(ap, tf);
1885}
1886
1887/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 * ata_busy_sleep - sleep until BSY clears, or timeout
1889 * @ap: port containing status register to be polled
1890 * @tmout_pat: impatience timeout
1891 * @tmout: overall timeout
1892 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001893 * Sleep until ATA Status register bit BSY clears,
1894 * or a timeout occurs.
1895 *
1896 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 */
1898
Tejun Heo6f8b9952006-01-24 17:05:21 +09001899unsigned int ata_busy_sleep (struct ata_port *ap,
1900 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 unsigned long timer_start, timeout;
1903 u8 status;
1904
1905 status = ata_busy_wait(ap, ATA_BUSY, 300);
1906 timer_start = jiffies;
1907 timeout = timer_start + tmout_pat;
1908 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1909 msleep(50);
1910 status = ata_busy_wait(ap, ATA_BUSY, 3);
1911 }
1912
1913 if (status & ATA_BUSY)
1914 printk(KERN_WARNING "ata%u is slow to respond, "
1915 "please be patient\n", ap->id);
1916
1917 timeout = timer_start + tmout;
1918 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1919 msleep(50);
1920 status = ata_chk_status(ap);
1921 }
1922
1923 if (status & ATA_BUSY) {
1924 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1925 ap->id, tmout / HZ);
1926 return 1;
1927 }
1928
1929 return 0;
1930}
1931
1932static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1933{
1934 struct ata_ioports *ioaddr = &ap->ioaddr;
1935 unsigned int dev0 = devmask & (1 << 0);
1936 unsigned int dev1 = devmask & (1 << 1);
1937 unsigned long timeout;
1938
1939 /* if device 0 was found in ata_devchk, wait for its
1940 * BSY bit to clear
1941 */
1942 if (dev0)
1943 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1944
1945 /* if device 1 was found in ata_devchk, wait for
1946 * register access, then wait for BSY to clear
1947 */
1948 timeout = jiffies + ATA_TMOUT_BOOT;
1949 while (dev1) {
1950 u8 nsect, lbal;
1951
1952 ap->ops->dev_select(ap, 1);
1953 if (ap->flags & ATA_FLAG_MMIO) {
1954 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1955 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1956 } else {
1957 nsect = inb(ioaddr->nsect_addr);
1958 lbal = inb(ioaddr->lbal_addr);
1959 }
1960 if ((nsect == 1) && (lbal == 1))
1961 break;
1962 if (time_after(jiffies, timeout)) {
1963 dev1 = 0;
1964 break;
1965 }
1966 msleep(50); /* give drive a breather */
1967 }
1968 if (dev1)
1969 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1970
1971 /* is all this really necessary? */
1972 ap->ops->dev_select(ap, 0);
1973 if (dev1)
1974 ap->ops->dev_select(ap, 1);
1975 if (dev0)
1976 ap->ops->dev_select(ap, 0);
1977}
1978
1979/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001980 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1981 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001983 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1984 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 *
1986 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001987 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001988 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 *
1990 */
1991
1992static unsigned int ata_bus_edd(struct ata_port *ap)
1993{
1994 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001995 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /* set up execute-device-diag (bus reset) taskfile */
1998 /* also, take interrupts to a known state (disabled) */
1999 DPRINTK("execute-device-diag\n");
2000 ata_tf_init(ap, &tf, 0);
2001 tf.ctl |= ATA_NIEN;
2002 tf.command = ATA_CMD_EDD;
2003 tf.protocol = ATA_PROT_NODATA;
2004
2005 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002006 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002008 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 /* spec says at least 2ms. but who knows with those
2011 * crazy ATAPI devices...
2012 */
2013 msleep(150);
2014
2015 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2016}
2017
2018static unsigned int ata_bus_softreset(struct ata_port *ap,
2019 unsigned int devmask)
2020{
2021 struct ata_ioports *ioaddr = &ap->ioaddr;
2022
2023 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2024
2025 /* software reset. causes dev0 to be selected */
2026 if (ap->flags & ATA_FLAG_MMIO) {
2027 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2028 udelay(20); /* FIXME: flush */
2029 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2030 udelay(20); /* FIXME: flush */
2031 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2032 } else {
2033 outb(ap->ctl, ioaddr->ctl_addr);
2034 udelay(10);
2035 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2036 udelay(10);
2037 outb(ap->ctl, ioaddr->ctl_addr);
2038 }
2039
2040 /* spec mandates ">= 2ms" before checking status.
2041 * We wait 150ms, because that was the magic delay used for
2042 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2043 * between when the ATA command register is written, and then
2044 * status is checked. Because waiting for "a while" before
2045 * checking status is fine, post SRST, we perform this magic
2046 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00002047 *
2048 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 */
2050 msleep(150);
2051
Alan Cox09c7ad72006-03-22 15:52:40 +00002052
2053 /* Before we perform post reset processing we want to see if
2054 the bus shows 0xFF because the odd clown forgets the D7 pulldown
2055 resistor */
2056
2057 if (ata_check_status(ap) == 0xFF)
2058 return 1; /* Positive is failure for some reason */
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 ata_bus_post_reset(ap, devmask);
2061
2062 return 0;
2063}
2064
2065/**
2066 * ata_bus_reset - reset host port and associated ATA channel
2067 * @ap: port to reset
2068 *
2069 * This is typically the first time we actually start issuing
2070 * commands to the ATA channel. We wait for BSY to clear, then
2071 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2072 * result. Determine what devices, if any, are on the channel
2073 * by looking at the device 0/1 error register. Look at the signature
2074 * stored in each device's taskfile registers, to determine if
2075 * the device is ATA or ATAPI.
2076 *
2077 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002078 * PCI/etc. bus probe sem.
2079 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 *
2081 * SIDE EFFECTS:
2082 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2083 */
2084
2085void ata_bus_reset(struct ata_port *ap)
2086{
2087 struct ata_ioports *ioaddr = &ap->ioaddr;
2088 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2089 u8 err;
2090 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2091
2092 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2093
2094 /* determine if device 0/1 are present */
2095 if (ap->flags & ATA_FLAG_SATA_RESET)
2096 dev0 = 1;
2097 else {
2098 dev0 = ata_devchk(ap, 0);
2099 if (slave_possible)
2100 dev1 = ata_devchk(ap, 1);
2101 }
2102
2103 if (dev0)
2104 devmask |= (1 << 0);
2105 if (dev1)
2106 devmask |= (1 << 1);
2107
2108 /* select device 0 again */
2109 ap->ops->dev_select(ap, 0);
2110
2111 /* issue bus reset */
2112 if (ap->flags & ATA_FLAG_SRST)
2113 rc = ata_bus_softreset(ap, devmask);
2114 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2115 /* set up device control */
2116 if (ap->flags & ATA_FLAG_MMIO)
2117 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2118 else
2119 outb(ap->ctl, ioaddr->ctl_addr);
2120 rc = ata_bus_edd(ap);
2121 }
2122
2123 if (rc)
2124 goto err_out;
2125
2126 /*
2127 * determine by signature whether we have ATA or ATAPI devices
2128 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002129 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002131 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 /* re-enable interrupts */
2134 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2135 ata_irq_on(ap);
2136
2137 /* is double-select really necessary? */
2138 if (ap->device[1].class != ATA_DEV_NONE)
2139 ap->ops->dev_select(ap, 1);
2140 if (ap->device[0].class != ATA_DEV_NONE)
2141 ap->ops->dev_select(ap, 0);
2142
2143 /* if no devices were detected, disable this port */
2144 if ((ap->device[0].class == ATA_DEV_NONE) &&
2145 (ap->device[1].class == ATA_DEV_NONE))
2146 goto err_out;
2147
2148 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2149 /* set up device control for ATA_FLAG_SATA_RESET */
2150 if (ap->flags & ATA_FLAG_MMIO)
2151 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2152 else
2153 outb(ap->ctl, ioaddr->ctl_addr);
2154 }
2155
2156 DPRINTK("EXIT\n");
2157 return;
2158
2159err_out:
2160 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2161 ap->ops->port_disable(ap);
2162
2163 DPRINTK("EXIT\n");
2164}
2165
Tejun Heo7a7921e2006-02-02 18:20:00 +09002166static int sata_phy_resume(struct ata_port *ap)
2167{
2168 unsigned long timeout = jiffies + (HZ * 5);
2169 u32 sstatus;
2170
2171 scr_write_flush(ap, SCR_CONTROL, 0x300);
2172
2173 /* Wait for phy to become ready, if necessary. */
2174 do {
2175 msleep(200);
2176 sstatus = scr_read(ap, SCR_STATUS);
2177 if ((sstatus & 0xf) != 1)
2178 return 0;
2179 } while (time_before(jiffies, timeout));
2180
2181 return -1;
2182}
2183
Tejun Heoc2bd5802006-01-24 17:05:22 +09002184/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002185 * ata_std_probeinit - initialize probing
2186 * @ap: port to be probed
2187 *
2188 * @ap is about to be probed. Initialize it. This function is
2189 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002190 *
2191 * NOTE!!! Do not use this function as probeinit if a low level
2192 * driver implements only hardreset. Just pass NULL as probeinit
2193 * in that case. Using this function is probably okay but doing
2194 * so makes reset sequence different from the original
2195 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002196 */
2197extern void ata_std_probeinit(struct ata_port *ap)
2198{
Tejun Heo3a397462006-02-10 23:58:48 +09002199 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002200 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002201 if (sata_dev_present(ap))
2202 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2203 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002204}
2205
2206/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002207 * ata_std_softreset - reset host port via ATA SRST
2208 * @ap: port to reset
2209 * @verbose: fail verbosely
2210 * @classes: resulting classes of attached devices
2211 *
2212 * Reset host port using ATA SRST. This function is to be used
2213 * as standard callback for ata_drive_*_reset() functions.
2214 *
2215 * LOCKING:
2216 * Kernel thread context (may sleep)
2217 *
2218 * RETURNS:
2219 * 0 on success, -errno otherwise.
2220 */
2221int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2222{
2223 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2224 unsigned int devmask = 0, err_mask;
2225 u8 err;
2226
2227 DPRINTK("ENTER\n");
2228
Tejun Heo3a397462006-02-10 23:58:48 +09002229 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2230 classes[0] = ATA_DEV_NONE;
2231 goto out;
2232 }
2233
Tejun Heoc2bd5802006-01-24 17:05:22 +09002234 /* determine if device 0/1 are present */
2235 if (ata_devchk(ap, 0))
2236 devmask |= (1 << 0);
2237 if (slave_possible && ata_devchk(ap, 1))
2238 devmask |= (1 << 1);
2239
Tejun Heoc2bd5802006-01-24 17:05:22 +09002240 /* select device 0 again */
2241 ap->ops->dev_select(ap, 0);
2242
2243 /* issue bus reset */
2244 DPRINTK("about to softreset, devmask=%x\n", devmask);
2245 err_mask = ata_bus_softreset(ap, devmask);
2246 if (err_mask) {
2247 if (verbose)
2248 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2249 ap->id, err_mask);
2250 else
2251 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2252 err_mask);
2253 return -EIO;
2254 }
2255
2256 /* determine by signature whether we have ATA or ATAPI devices */
2257 classes[0] = ata_dev_try_classify(ap, 0, &err);
2258 if (slave_possible && err != 0x81)
2259 classes[1] = ata_dev_try_classify(ap, 1, &err);
2260
Tejun Heo3a397462006-02-10 23:58:48 +09002261 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002262 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2263 return 0;
2264}
2265
2266/**
2267 * sata_std_hardreset - reset host port via SATA phy reset
2268 * @ap: port to reset
2269 * @verbose: fail verbosely
2270 * @class: resulting class of attached device
2271 *
2272 * SATA phy-reset host port using DET bits of SControl register.
2273 * This function is to be used as standard callback for
2274 * ata_drive_*_reset().
2275 *
2276 * LOCKING:
2277 * Kernel thread context (may sleep)
2278 *
2279 * RETURNS:
2280 * 0 on success, -errno otherwise.
2281 */
2282int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2283{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002284 DPRINTK("ENTER\n");
2285
2286 /* Issue phy wake/reset */
2287 scr_write_flush(ap, SCR_CONTROL, 0x301);
2288
2289 /*
2290 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2291 * 10.4.2 says at least 1 ms.
2292 */
2293 msleep(1);
2294
Tejun Heo7a7921e2006-02-02 18:20:00 +09002295 /* Bring phy back */
2296 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002297
Tejun Heoc2bd5802006-01-24 17:05:22 +09002298 /* TODO: phy layer with polling, timeouts, etc. */
2299 if (!sata_dev_present(ap)) {
2300 *class = ATA_DEV_NONE;
2301 DPRINTK("EXIT, link offline\n");
2302 return 0;
2303 }
2304
2305 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2306 if (verbose)
2307 printk(KERN_ERR "ata%u: COMRESET failed "
2308 "(device not ready)\n", ap->id);
2309 else
2310 DPRINTK("EXIT, device not ready\n");
2311 return -EIO;
2312 }
2313
Tejun Heo3a397462006-02-10 23:58:48 +09002314 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2315
Tejun Heoc2bd5802006-01-24 17:05:22 +09002316 *class = ata_dev_try_classify(ap, 0, NULL);
2317
2318 DPRINTK("EXIT, class=%u\n", *class);
2319 return 0;
2320}
2321
2322/**
2323 * ata_std_postreset - standard postreset callback
2324 * @ap: the target ata_port
2325 * @classes: classes of attached devices
2326 *
2327 * This function is invoked after a successful reset. Note that
2328 * the device might have been reset more than once using
2329 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002330 *
2331 * This function is to be used as standard callback for
2332 * ata_drive_*_reset().
2333 *
2334 * LOCKING:
2335 * Kernel thread context (may sleep)
2336 */
2337void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2338{
2339 DPRINTK("ENTER\n");
2340
Tejun Heo56497bd2006-02-15 15:01:42 +09002341 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002342 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2343 ap->cbl = ATA_CBL_SATA;
2344
2345 /* print link status */
2346 if (ap->cbl == ATA_CBL_SATA)
2347 sata_print_link_status(ap);
2348
Tejun Heo3a397462006-02-10 23:58:48 +09002349 /* re-enable interrupts */
2350 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2351 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002352
2353 /* is double-select really necessary? */
2354 if (classes[0] != ATA_DEV_NONE)
2355 ap->ops->dev_select(ap, 1);
2356 if (classes[1] != ATA_DEV_NONE)
2357 ap->ops->dev_select(ap, 0);
2358
Tejun Heo3a397462006-02-10 23:58:48 +09002359 /* bail out if no device is present */
2360 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2361 DPRINTK("EXIT, no device\n");
2362 return;
2363 }
2364
2365 /* set up device control */
2366 if (ap->ioaddr.ctl_addr) {
2367 if (ap->flags & ATA_FLAG_MMIO)
2368 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2369 else
2370 outb(ap->ctl, ap->ioaddr.ctl_addr);
2371 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002372
2373 DPRINTK("EXIT\n");
2374}
2375
2376/**
2377 * ata_std_probe_reset - standard probe reset method
2378 * @ap: prot to perform probe-reset
2379 * @classes: resulting classes of attached devices
2380 *
2381 * The stock off-the-shelf ->probe_reset method.
2382 *
2383 * LOCKING:
2384 * Kernel thread context (may sleep)
2385 *
2386 * RETURNS:
2387 * 0 on success, -errno otherwise.
2388 */
2389int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2390{
2391 ata_reset_fn_t hardreset;
2392
2393 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002394 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002395 hardreset = sata_std_hardreset;
2396
Tejun Heo8a19ac82006-02-02 18:20:00 +09002397 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002398 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002399 ata_std_postreset, classes);
2400}
2401
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002402static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2403 ata_postreset_fn_t postreset,
2404 unsigned int *classes)
2405{
2406 int i, rc;
2407
2408 for (i = 0; i < ATA_MAX_DEVICES; i++)
2409 classes[i] = ATA_DEV_UNKNOWN;
2410
2411 rc = reset(ap, 0, classes);
2412 if (rc)
2413 return rc;
2414
2415 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2416 * is complete and convert all ATA_DEV_UNKNOWN to
2417 * ATA_DEV_NONE.
2418 */
2419 for (i = 0; i < ATA_MAX_DEVICES; i++)
2420 if (classes[i] != ATA_DEV_UNKNOWN)
2421 break;
2422
2423 if (i < ATA_MAX_DEVICES)
2424 for (i = 0; i < ATA_MAX_DEVICES; i++)
2425 if (classes[i] == ATA_DEV_UNKNOWN)
2426 classes[i] = ATA_DEV_NONE;
2427
2428 if (postreset)
2429 postreset(ap, classes);
2430
2431 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2432}
2433
2434/**
2435 * ata_drive_probe_reset - Perform probe reset with given methods
2436 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002437 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002438 * @softreset: softreset method (can be NULL)
2439 * @hardreset: hardreset method (can be NULL)
2440 * @postreset: postreset method (can be NULL)
2441 * @classes: resulting classes of attached devices
2442 *
2443 * Reset the specified port and classify attached devices using
2444 * given methods. This function prefers softreset but tries all
2445 * possible reset sequences to reset and classify devices. This
2446 * function is intended to be used for constructing ->probe_reset
2447 * callback by low level drivers.
2448 *
2449 * Reset methods should follow the following rules.
2450 *
2451 * - Return 0 on sucess, -errno on failure.
2452 * - If classification is supported, fill classes[] with
2453 * recognized class codes.
2454 * - If classification is not supported, leave classes[] alone.
2455 * - If verbose is non-zero, print error message on failure;
2456 * otherwise, shut up.
2457 *
2458 * LOCKING:
2459 * Kernel thread context (may sleep)
2460 *
2461 * RETURNS:
2462 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2463 * if classification fails, and any error code from reset
2464 * methods.
2465 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002466int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002467 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2468 ata_postreset_fn_t postreset, unsigned int *classes)
2469{
2470 int rc = -EINVAL;
2471
Tejun Heo7944ea92006-02-02 18:20:00 +09002472 if (probeinit)
2473 probeinit(ap);
2474
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002475 if (softreset) {
2476 rc = do_probe_reset(ap, softreset, postreset, classes);
2477 if (rc == 0)
2478 return 0;
2479 }
2480
2481 if (!hardreset)
2482 return rc;
2483
2484 rc = do_probe_reset(ap, hardreset, postreset, classes);
2485 if (rc == 0 || rc != -ENODEV)
2486 return rc;
2487
2488 if (softreset)
2489 rc = do_probe_reset(ap, softreset, postreset, classes);
2490
2491 return rc;
2492}
2493
Tejun Heo623a3122006-03-05 17:55:58 +09002494/**
2495 * ata_dev_same_device - Determine whether new ID matches configured device
2496 * @ap: port on which the device to compare against resides
2497 * @dev: device to compare against
2498 * @new_class: class of the new device
2499 * @new_id: IDENTIFY page of the new device
2500 *
2501 * Compare @new_class and @new_id against @dev and determine
2502 * whether @dev is the device indicated by @new_class and
2503 * @new_id.
2504 *
2505 * LOCKING:
2506 * None.
2507 *
2508 * RETURNS:
2509 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2510 */
2511static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2512 unsigned int new_class, const u16 *new_id)
2513{
2514 const u16 *old_id = dev->id;
2515 unsigned char model[2][41], serial[2][21];
2516 u64 new_n_sectors;
2517
2518 if (dev->class != new_class) {
2519 printk(KERN_INFO
2520 "ata%u: dev %u class mismatch %d != %d\n",
2521 ap->id, dev->devno, dev->class, new_class);
2522 return 0;
2523 }
2524
2525 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2526 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2527 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2528 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2529 new_n_sectors = ata_id_n_sectors(new_id);
2530
2531 if (strcmp(model[0], model[1])) {
2532 printk(KERN_INFO
2533 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2534 ap->id, dev->devno, model[0], model[1]);
2535 return 0;
2536 }
2537
2538 if (strcmp(serial[0], serial[1])) {
2539 printk(KERN_INFO
2540 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2541 ap->id, dev->devno, serial[0], serial[1]);
2542 return 0;
2543 }
2544
2545 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2546 printk(KERN_INFO
2547 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2548 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2549 (unsigned long long)new_n_sectors);
2550 return 0;
2551 }
2552
2553 return 1;
2554}
2555
2556/**
2557 * ata_dev_revalidate - Revalidate ATA device
2558 * @ap: port on which the device to revalidate resides
2559 * @dev: device to revalidate
2560 * @post_reset: is this revalidation after reset?
2561 *
2562 * Re-read IDENTIFY page and make sure @dev is still attached to
2563 * the port.
2564 *
2565 * LOCKING:
2566 * Kernel thread context (may sleep)
2567 *
2568 * RETURNS:
2569 * 0 on success, negative errno otherwise
2570 */
2571int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2572 int post_reset)
2573{
2574 unsigned int class;
2575 u16 *id;
2576 int rc;
2577
2578 if (!ata_dev_present(dev))
2579 return -ENODEV;
2580
2581 class = dev->class;
2582 id = NULL;
2583
2584 /* allocate & read ID data */
2585 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2586 if (rc)
2587 goto fail;
2588
2589 /* is the device still there? */
2590 if (!ata_dev_same_device(ap, dev, class, id)) {
2591 rc = -ENODEV;
2592 goto fail;
2593 }
2594
2595 kfree(dev->id);
2596 dev->id = id;
2597
2598 /* configure device according to the new ID */
2599 return ata_dev_configure(ap, dev, 0);
2600
2601 fail:
2602 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2603 ap->id, dev->devno, rc);
2604 kfree(id);
2605 return rc;
2606}
2607
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002608static const char * const ata_dma_blacklist [] = {
Alan Coxf4b15fe2006-03-22 15:54:04 +00002609 "WDC AC11000H", NULL,
2610 "WDC AC22100H", NULL,
2611 "WDC AC32500H", NULL,
2612 "WDC AC33100H", NULL,
2613 "WDC AC31600H", NULL,
2614 "WDC AC32100H", "24.09P07",
2615 "WDC AC23200L", "21.10N21",
2616 "Compaq CRD-8241B", NULL,
2617 "CRD-8400B", NULL,
2618 "CRD-8480B", NULL,
2619 "CRD-8482B", NULL,
2620 "CRD-84", NULL,
2621 "SanDisk SDP3B", NULL,
2622 "SanDisk SDP3B-64", NULL,
2623 "SANYO CD-ROM CRD", NULL,
2624 "HITACHI CDR-8", NULL,
2625 "HITACHI CDR-8335", NULL,
2626 "HITACHI CDR-8435", NULL,
2627 "Toshiba CD-ROM XM-6202B", NULL,
2628 "TOSHIBA CD-ROM XM-1702BC", NULL,
2629 "CD-532E-A", NULL,
2630 "E-IDE CD-ROM CR-840", NULL,
2631 "CD-ROM Drive/F5A", NULL,
2632 "WPI CDD-820", NULL,
2633 "SAMSUNG CD-ROM SC-148C", NULL,
2634 "SAMSUNG CD-ROM SC", NULL,
2635 "SanDisk SDP3B-64", NULL,
2636 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2637 "_NEC DV5800A", NULL,
2638 "SAMSUNG CD-ROM SN-124", "N001"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639};
Alan Coxf4b15fe2006-03-22 15:54:04 +00002640
2641static int ata_strim(char *s, size_t len)
2642{
2643 len = strnlen(s, len);
2644
2645 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2646 while ((len > 0) && (s[len - 1] == ' ')) {
2647 len--;
2648 s[len] = 0;
2649 }
2650 return len;
2651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Jeff Garzik057ace52005-10-22 14:27:05 -04002653static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
Alan Coxf4b15fe2006-03-22 15:54:04 +00002655 unsigned char model_num[40];
2656 unsigned char model_rev[16];
2657 unsigned int nlen, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 int i;
2659
Alan Coxf4b15fe2006-03-22 15:54:04 +00002660 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2661 sizeof(model_num));
2662 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
2663 sizeof(model_rev));
2664 nlen = ata_strim(model_num, sizeof(model_num));
2665 rlen = ata_strim(model_rev, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Alan Coxf4b15fe2006-03-22 15:54:04 +00002667 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
2668 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
2669 if (ata_dma_blacklist[i+1] == NULL)
2670 return 1;
2671 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
2672 return 1;
2673 }
2674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 return 0;
2676}
2677
Tejun Heoa6d5a512006-03-06 04:31:57 +09002678/**
2679 * ata_dev_xfermask - Compute supported xfermask of the given device
2680 * @ap: Port on which the device to compute xfermask for resides
2681 * @dev: Device to compute xfermask for
2682 *
Tejun Heoacf356b2006-03-24 14:07:50 +09002683 * Compute supported xfermask of @dev and store it in
2684 * dev->*_mask. This function is responsible for applying all
2685 * known limits including host controller limits, device
2686 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09002687 *
2688 * LOCKING:
2689 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09002690 */
Tejun Heoacf356b2006-03-24 14:07:50 +09002691static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
Tejun Heoa6d5a512006-03-06 04:31:57 +09002693 unsigned long xfer_mask;
2694 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Tejun Heoa6d5a512006-03-06 04:31:57 +09002696 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2697 ap->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Tejun Heoa6d5a512006-03-06 04:31:57 +09002699 /* use port-wide xfermask for now */
2700 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2701 struct ata_device *d = &ap->device[i];
2702 if (!ata_dev_present(d))
2703 continue;
Tejun Heoacf356b2006-03-24 14:07:50 +09002704 xfer_mask &= ata_pack_xfermask(d->pio_mask, d->mwdma_mask,
2705 d->udma_mask);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002706 xfer_mask &= ata_id_xfermask(d->id);
2707 if (ata_dma_blacklisted(d))
2708 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710
Tejun Heoa6d5a512006-03-06 04:31:57 +09002711 if (ata_dma_blacklisted(dev))
2712 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2713 "disabling DMA\n", ap->id, dev->devno);
2714
Tejun Heoacf356b2006-03-24 14:07:50 +09002715 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2716 &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2721 * @ap: Port associated with device @dev
2722 * @dev: Device to which command will be sent
2723 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002724 * Issue SET FEATURES - XFER MODE command to device @dev
2725 * on port @ap.
2726 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002728 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 */
2730
2731static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2732{
Tejun Heoa0123702005-12-13 14:49:31 +09002733 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735 /* set up set-features taskfile */
2736 DPRINTK("set features - xfer mode\n");
2737
Tejun Heoa0123702005-12-13 14:49:31 +09002738 ata_tf_init(ap, &tf, dev->devno);
2739 tf.command = ATA_CMD_SET_FEATURES;
2740 tf.feature = SETFEATURES_XFER;
2741 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2742 tf.protocol = ATA_PROT_NODATA;
2743 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Tejun Heoa0123702005-12-13 14:49:31 +09002745 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2746 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2747 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 DPRINTK("EXIT\n");
2752}
2753
2754/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002755 * ata_dev_init_params - Issue INIT DEV PARAMS command
2756 * @ap: Port associated with device @dev
2757 * @dev: Device to which command will be sent
2758 *
2759 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002760 * Kernel thread context (may sleep)
2761 *
2762 * RETURNS:
2763 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04002764 */
2765
Tejun Heo6aff8f12006-02-15 18:24:09 +09002766static unsigned int ata_dev_init_params(struct ata_port *ap,
2767 struct ata_device *dev)
Albert Lee8bf62ec2005-05-12 15:29:42 -04002768{
Tejun Heoa0123702005-12-13 14:49:31 +09002769 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002770 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002771 u16 sectors = dev->id[6];
2772 u16 heads = dev->id[3];
2773
2774 /* Number of sectors per track 1-255. Number of heads 1-16 */
2775 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002776 return 0;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002777
2778 /* set up init dev params taskfile */
2779 DPRINTK("init dev params \n");
2780
Tejun Heoa0123702005-12-13 14:49:31 +09002781 ata_tf_init(ap, &tf, dev->devno);
2782 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2783 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2784 tf.protocol = ATA_PROT_NODATA;
2785 tf.nsect = sectors;
2786 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002787
Tejun Heo6aff8f12006-02-15 18:24:09 +09002788 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002789
Tejun Heo6aff8f12006-02-15 18:24:09 +09002790 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2791 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002792}
2793
2794/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002795 * ata_sg_clean - Unmap DMA memory associated with command
2796 * @qc: Command containing DMA memory to be released
2797 *
2798 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 *
2800 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002801 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 */
2803
2804static void ata_sg_clean(struct ata_queued_cmd *qc)
2805{
2806 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002807 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002809 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Tejun Heoa4631472006-02-11 19:11:13 +09002811 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2812 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002815 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002817 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002819 /* if we padded the buffer out to 32-bit bound, and data
2820 * xfer direction is from-device, we must copy from the
2821 * pad buffer back into the supplied buffer
2822 */
2823 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2824 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2825
2826 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002827 if (qc->n_elem)
2828 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002829 /* restore last sg */
2830 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2831 if (pad_buf) {
2832 struct scatterlist *psg = &qc->pad_sgent;
2833 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2834 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002835 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002836 }
2837 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002838 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002839 dma_unmap_single(ap->host_set->dev,
2840 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2841 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002842 /* restore sg */
2843 sg->length += qc->pad_len;
2844 if (pad_buf)
2845 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2846 pad_buf, qc->pad_len);
2847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002850 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
2853/**
2854 * ata_fill_sg - Fill PCI IDE PRD table
2855 * @qc: Metadata associated with taskfile to be transferred
2856 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002857 * Fill PCI IDE PRD (scatter-gather) table with segments
2858 * associated with the current disk command.
2859 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002861 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 *
2863 */
2864static void ata_fill_sg(struct ata_queued_cmd *qc)
2865{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002867 struct scatterlist *sg;
2868 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Tejun Heoa4631472006-02-11 19:11:13 +09002870 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002871 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002874 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 u32 addr, offset;
2876 u32 sg_len, len;
2877
2878 /* determine if physical DMA addr spans 64K boundary.
2879 * Note h/w doesn't support 64-bit, so we unconditionally
2880 * truncate dma_addr_t to u32.
2881 */
2882 addr = (u32) sg_dma_address(sg);
2883 sg_len = sg_dma_len(sg);
2884
2885 while (sg_len) {
2886 offset = addr & 0xffff;
2887 len = sg_len;
2888 if ((offset + sg_len) > 0x10000)
2889 len = 0x10000 - offset;
2890
2891 ap->prd[idx].addr = cpu_to_le32(addr);
2892 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2893 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2894
2895 idx++;
2896 sg_len -= len;
2897 addr += len;
2898 }
2899 }
2900
2901 if (idx)
2902 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2903}
2904/**
2905 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2906 * @qc: Metadata associated with taskfile to check
2907 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002908 * Allow low-level driver to filter ATA PACKET commands, returning
2909 * a status indicating whether or not it is OK to use DMA for the
2910 * supplied PACKET command.
2911 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002913 * spin_lock_irqsave(host_set lock)
2914 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 * RETURNS: 0 when ATAPI DMA can be used
2916 * nonzero otherwise
2917 */
2918int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2919{
2920 struct ata_port *ap = qc->ap;
2921 int rc = 0; /* Assume ATAPI DMA is OK by default */
2922
2923 if (ap->ops->check_atapi_dma)
2924 rc = ap->ops->check_atapi_dma(qc);
2925
2926 return rc;
2927}
2928/**
2929 * ata_qc_prep - Prepare taskfile for submission
2930 * @qc: Metadata associated with taskfile to be prepared
2931 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002932 * Prepare ATA taskfile for submission.
2933 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 * LOCKING:
2935 * spin_lock_irqsave(host_set lock)
2936 */
2937void ata_qc_prep(struct ata_queued_cmd *qc)
2938{
2939 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2940 return;
2941
2942 ata_fill_sg(qc);
2943}
2944
Brian Kinge46834c2006-03-17 17:04:03 -06002945void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
2946
Jeff Garzik0cba6322005-05-30 19:49:12 -04002947/**
2948 * ata_sg_init_one - Associate command with memory buffer
2949 * @qc: Command to be associated
2950 * @buf: Memory buffer
2951 * @buflen: Length of memory buffer, in bytes.
2952 *
2953 * Initialize the data-related elements of queued_cmd @qc
2954 * to point to a single memory buffer, @buf of byte length @buflen.
2955 *
2956 * LOCKING:
2957 * spin_lock_irqsave(host_set lock)
2958 */
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2961{
2962 struct scatterlist *sg;
2963
2964 qc->flags |= ATA_QCFLAG_SINGLE;
2965
2966 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002967 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002969 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 qc->buf_virt = buf;
2971
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002972 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002973 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974}
2975
Jeff Garzik0cba6322005-05-30 19:49:12 -04002976/**
2977 * ata_sg_init - Associate command with scatter-gather table.
2978 * @qc: Command to be associated
2979 * @sg: Scatter-gather table.
2980 * @n_elem: Number of elements in s/g table.
2981 *
2982 * Initialize the data-related elements of queued_cmd @qc
2983 * to point to a scatter-gather table @sg, containing @n_elem
2984 * elements.
2985 *
2986 * LOCKING:
2987 * spin_lock_irqsave(host_set lock)
2988 */
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2991 unsigned int n_elem)
2992{
2993 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002994 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002996 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
2999/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003000 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3001 * @qc: Command with memory buffer to be mapped.
3002 *
3003 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 *
3005 * LOCKING:
3006 * spin_lock_irqsave(host_set lock)
3007 *
3008 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003009 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 */
3011
3012static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3013{
3014 struct ata_port *ap = qc->ap;
3015 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003016 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003018 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003020 /* we must lengthen transfers to end on a 32-bit boundary */
3021 qc->pad_len = sg->length & 3;
3022 if (qc->pad_len) {
3023 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3024 struct scatterlist *psg = &qc->pad_sgent;
3025
Tejun Heoa4631472006-02-11 19:11:13 +09003026 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003027
3028 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3029
3030 if (qc->tf.flags & ATA_TFLAG_WRITE)
3031 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3032 qc->pad_len);
3033
3034 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3035 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3036 /* trim sg */
3037 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003038 if (sg->length == 0)
3039 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003040
3041 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3042 sg->length, qc->pad_len);
3043 }
3044
Tejun Heo2e242fa2006-02-20 23:48:38 +09003045 if (trim_sg) {
3046 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003047 goto skip_map;
3048 }
3049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003051 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003052 if (dma_mapping_error(dma_address)) {
3053 /* restore sg */
3054 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003059 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Tejun Heo2e242fa2006-02-20 23:48:38 +09003061skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3063 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3064
3065 return 0;
3066}
3067
3068/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003069 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3070 * @qc: Command with scatter-gather table to be mapped.
3071 *
3072 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 *
3074 * LOCKING:
3075 * spin_lock_irqsave(host_set lock)
3076 *
3077 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003078 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 *
3080 */
3081
3082static int ata_sg_setup(struct ata_queued_cmd *qc)
3083{
3084 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003085 struct scatterlist *sg = qc->__sg;
3086 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003087 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003090 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003092 /* we must lengthen transfers to end on a 32-bit boundary */
3093 qc->pad_len = lsg->length & 3;
3094 if (qc->pad_len) {
3095 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3096 struct scatterlist *psg = &qc->pad_sgent;
3097 unsigned int offset;
3098
Tejun Heoa4631472006-02-11 19:11:13 +09003099 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003100
3101 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3102
3103 /*
3104 * psg->page/offset are used to copy to-be-written
3105 * data in this function or read data in ata_sg_clean.
3106 */
3107 offset = lsg->offset + lsg->length - qc->pad_len;
3108 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3109 psg->offset = offset_in_page(offset);
3110
3111 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3112 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3113 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003114 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003115 }
3116
3117 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3118 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3119 /* trim last sg */
3120 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003121 if (lsg->length == 0)
3122 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003123
3124 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3125 qc->n_elem - 1, lsg->length, qc->pad_len);
3126 }
3127
Jeff Garzike1410f22005-11-14 14:06:26 -05003128 pre_n_elem = qc->n_elem;
3129 if (trim_sg && pre_n_elem)
3130 pre_n_elem--;
3131
3132 if (!pre_n_elem) {
3133 n_elem = 0;
3134 goto skip_map;
3135 }
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05003138 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003139 if (n_elem < 1) {
3140 /* restore last sg */
3141 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145 DPRINTK("%d sg elements mapped\n", n_elem);
3146
Jeff Garzike1410f22005-11-14 14:06:26 -05003147skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 qc->n_elem = n_elem;
3149
3150 return 0;
3151}
3152
3153/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003154 * ata_poll_qc_complete - turn irq back on and finish qc
3155 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003156 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003157 *
3158 * LOCKING:
3159 * None. (grabs host lock)
3160 */
3161
Albert Leea22e2eb2005-12-05 15:38:02 +08003162void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003163{
3164 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003165 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003166
Jeff Garzikb8f61532005-08-25 22:01:20 -04003167 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003168 ap->flags &= ~ATA_FLAG_NOINTR;
3169 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003170 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003171 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003172}
3173
3174/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003175 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003176 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 *
3178 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003179 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 *
3181 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003182 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 */
3184
3185static unsigned long ata_pio_poll(struct ata_port *ap)
3186{
Albert Leec14b8332005-12-05 15:36:08 +08003187 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003189 unsigned int poll_state = HSM_ST_UNKNOWN;
3190 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Albert Leec14b8332005-12-05 15:36:08 +08003192 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003193 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003194
Albert Lee14be71f2005-09-27 17:36:35 +08003195 switch (ap->hsm_task_state) {
3196 case HSM_ST:
3197 case HSM_ST_POLL:
3198 poll_state = HSM_ST_POLL;
3199 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003201 case HSM_ST_LAST:
3202 case HSM_ST_LAST_POLL:
3203 poll_state = HSM_ST_LAST_POLL;
3204 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 break;
3206 default:
3207 BUG();
3208 break;
3209 }
3210
3211 status = ata_chk_status(ap);
3212 if (status & ATA_BUSY) {
3213 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003214 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003215 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 return 0;
3217 }
Albert Lee14be71f2005-09-27 17:36:35 +08003218 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return ATA_SHORT_PAUSE;
3220 }
3221
Albert Lee14be71f2005-09-27 17:36:35 +08003222 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return 0;
3224}
3225
3226/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003227 * ata_pio_complete - check if drive is busy or idle
3228 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 *
3230 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003231 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003232 *
3233 * RETURNS:
3234 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 */
3236
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003237static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
3239 struct ata_queued_cmd *qc;
3240 u8 drv_stat;
3241
3242 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003243 * This is purely heuristic. This is a fast path. Sometimes when
3244 * we enter, BSY will be cleared in a chk-status or two. If not,
3245 * the drive is probably seeking or something. Snooze for a couple
3246 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003247 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 */
Albert Leefe79e682005-12-06 11:34:59 +08003249 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3250 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003252 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3253 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003254 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258 }
3259
Albert Leec14b8332005-12-05 15:36:08 +08003260 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003261 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 drv_stat = ata_wait_idle(ap);
3264 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003265 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003266 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 }
3269
Albert Lee14be71f2005-09-27 17:36:35 +08003270 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Tejun Heoa4631472006-02-11 19:11:13 +09003272 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003273 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003274
3275 /* another command may start at this point */
3276
3277 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
Edward Falk0baab862005-06-02 18:17:13 -04003280
3281/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003282 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003283 * @buf: Buffer to swap
3284 * @buf_words: Number of 16-bit words in buffer.
3285 *
3286 * Swap halves of 16-bit words if needed to convert from
3287 * little-endian byte order to native cpu byte order, or
3288 * vice-versa.
3289 *
3290 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003291 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293void swap_buf_le16(u16 *buf, unsigned int buf_words)
3294{
3295#ifdef __BIG_ENDIAN
3296 unsigned int i;
3297
3298 for (i = 0; i < buf_words; i++)
3299 buf[i] = le16_to_cpu(buf[i]);
3300#endif /* __BIG_ENDIAN */
3301}
3302
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003303/**
3304 * ata_mmio_data_xfer - Transfer data by MMIO
3305 * @ap: port to read/write
3306 * @buf: data buffer
3307 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003308 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003309 *
3310 * Transfer data from/to the device data register by MMIO.
3311 *
3312 * LOCKING:
3313 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003314 */
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3317 unsigned int buflen, int write_data)
3318{
3319 unsigned int i;
3320 unsigned int words = buflen >> 1;
3321 u16 *buf16 = (u16 *) buf;
3322 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3323
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003324 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 if (write_data) {
3326 for (i = 0; i < words; i++)
3327 writew(le16_to_cpu(buf16[i]), mmio);
3328 } else {
3329 for (i = 0; i < words; i++)
3330 buf16[i] = cpu_to_le16(readw(mmio));
3331 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003332
3333 /* Transfer trailing 1 byte, if any. */
3334 if (unlikely(buflen & 0x01)) {
3335 u16 align_buf[1] = { 0 };
3336 unsigned char *trailing_buf = buf + buflen - 1;
3337
3338 if (write_data) {
3339 memcpy(align_buf, trailing_buf, 1);
3340 writew(le16_to_cpu(align_buf[0]), mmio);
3341 } else {
3342 align_buf[0] = cpu_to_le16(readw(mmio));
3343 memcpy(trailing_buf, align_buf, 1);
3344 }
3345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003348/**
3349 * ata_pio_data_xfer - Transfer data by PIO
3350 * @ap: port to read/write
3351 * @buf: data buffer
3352 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003353 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003354 *
3355 * Transfer data from/to the device data register by PIO.
3356 *
3357 * LOCKING:
3358 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003359 */
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3362 unsigned int buflen, int write_data)
3363{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003364 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003366 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003368 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003370 insw(ap->ioaddr.data_addr, buf, words);
3371
3372 /* Transfer trailing 1 byte, if any. */
3373 if (unlikely(buflen & 0x01)) {
3374 u16 align_buf[1] = { 0 };
3375 unsigned char *trailing_buf = buf + buflen - 1;
3376
3377 if (write_data) {
3378 memcpy(align_buf, trailing_buf, 1);
3379 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3380 } else {
3381 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3382 memcpy(trailing_buf, align_buf, 1);
3383 }
3384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385}
3386
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003387/**
3388 * ata_data_xfer - Transfer data from/to the data register.
3389 * @ap: port to read/write
3390 * @buf: data buffer
3391 * @buflen: buffer length
3392 * @do_write: read/write
3393 *
3394 * Transfer data from/to the device data register.
3395 *
3396 * LOCKING:
3397 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003398 */
3399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3401 unsigned int buflen, int do_write)
3402{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003403 /* Make the crap hardware pay the costs not the good stuff */
3404 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3405 unsigned long flags;
3406 local_irq_save(flags);
3407 if (ap->flags & ATA_FLAG_MMIO)
3408 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3409 else
3410 ata_pio_data_xfer(ap, buf, buflen, do_write);
3411 local_irq_restore(flags);
3412 } else {
3413 if (ap->flags & ATA_FLAG_MMIO)
3414 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3415 else
3416 ata_pio_data_xfer(ap, buf, buflen, do_write);
3417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003420/**
3421 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3422 * @qc: Command on going
3423 *
3424 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3425 *
3426 * LOCKING:
3427 * Inherited from caller.
3428 */
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430static void ata_pio_sector(struct ata_queued_cmd *qc)
3431{
3432 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003433 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 struct ata_port *ap = qc->ap;
3435 struct page *page;
3436 unsigned int offset;
3437 unsigned char *buf;
3438
3439 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003440 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442 page = sg[qc->cursg].page;
3443 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3444
3445 /* get the current page and offset */
3446 page = nth_page(page, (offset >> PAGE_SHIFT));
3447 offset %= PAGE_SIZE;
3448
3449 buf = kmap(page) + offset;
3450
3451 qc->cursect++;
3452 qc->cursg_ofs++;
3453
Albert Lee32529e02005-05-26 03:49:42 -04003454 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 qc->cursg++;
3456 qc->cursg_ofs = 0;
3457 }
3458
3459 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3460
3461 /* do the actual data transfer */
3462 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3463 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3464
3465 kunmap(page);
3466}
3467
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003468/**
3469 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3470 * @qc: Command on going
3471 * @bytes: number of bytes
3472 *
3473 * Transfer Transfer data from/to the ATAPI device.
3474 *
3475 * LOCKING:
3476 * Inherited from caller.
3477 *
3478 */
3479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3481{
3482 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003483 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 struct ata_port *ap = qc->ap;
3485 struct page *page;
3486 unsigned char *buf;
3487 unsigned int offset, count;
3488
Albert Lee563a6e12005-08-12 14:17:50 +08003489 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003490 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003493 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003494 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003495 * The end of qc->sg is reached and the device expects
3496 * more data to transfer. In order not to overrun qc->sg
3497 * and fulfill length specified in the byte count register,
3498 * - for read case, discard trailing data from the device
3499 * - for write case, padding zero data to the device
3500 */
3501 u16 pad_buf[1] = { 0 };
3502 unsigned int words = bytes >> 1;
3503 unsigned int i;
3504
3505 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003506 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003507 ap->id, bytes);
3508
3509 for (i = 0; i < words; i++)
3510 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3511
Albert Lee14be71f2005-09-27 17:36:35 +08003512 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003513 return;
3514 }
3515
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003516 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 page = sg->page;
3519 offset = sg->offset + qc->cursg_ofs;
3520
3521 /* get the current page and offset */
3522 page = nth_page(page, (offset >> PAGE_SHIFT));
3523 offset %= PAGE_SIZE;
3524
Albert Lee6952df02005-06-06 15:56:03 +08003525 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003526 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
3528 /* don't cross page boundaries */
3529 count = min(count, (unsigned int)PAGE_SIZE - offset);
3530
3531 buf = kmap(page) + offset;
3532
3533 bytes -= count;
3534 qc->curbytes += count;
3535 qc->cursg_ofs += count;
3536
Albert Lee32529e02005-05-26 03:49:42 -04003537 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 qc->cursg++;
3539 qc->cursg_ofs = 0;
3540 }
3541
3542 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3543
3544 /* do the actual data transfer */
3545 ata_data_xfer(ap, buf, count, do_write);
3546
3547 kunmap(page);
3548
Albert Lee563a6e12005-08-12 14:17:50 +08003549 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551}
3552
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003553/**
3554 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3555 * @qc: Command on going
3556 *
3557 * Transfer Transfer data from/to the ATAPI device.
3558 *
3559 * LOCKING:
3560 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003561 */
3562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3564{
3565 struct ata_port *ap = qc->ap;
3566 struct ata_device *dev = qc->dev;
3567 unsigned int ireason, bc_lo, bc_hi, bytes;
3568 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3569
3570 ap->ops->tf_read(ap, &qc->tf);
3571 ireason = qc->tf.nsect;
3572 bc_lo = qc->tf.lbam;
3573 bc_hi = qc->tf.lbah;
3574 bytes = (bc_hi << 8) | bc_lo;
3575
3576 /* shall be cleared to zero, indicating xfer of data */
3577 if (ireason & (1 << 0))
3578 goto err_out;
3579
3580 /* make sure transfer direction matches expected */
3581 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3582 if (do_write != i_write)
3583 goto err_out;
3584
3585 __atapi_pio_bytes(qc, bytes);
3586
3587 return;
3588
3589err_out:
3590 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3591 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003592 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003593 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
3596/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003597 * ata_pio_block - start PIO on a block
3598 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 *
3600 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003601 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 */
3603
3604static void ata_pio_block(struct ata_port *ap)
3605{
3606 struct ata_queued_cmd *qc;
3607 u8 status;
3608
3609 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003610 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 * Sometimes when we enter, BSY will be cleared in
3612 * a chk-status or two. If not, the drive is probably seeking
3613 * or something. Snooze for a couple msecs, then
3614 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003615 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 */
3617 status = ata_busy_wait(ap, ATA_BUSY, 5);
3618 if (status & ATA_BUSY) {
3619 msleep(2);
3620 status = ata_busy_wait(ap, ATA_BUSY, 10);
3621 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003622 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3624 return;
3625 }
3626 }
3627
3628 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003629 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Albert Leefe79e682005-12-06 11:34:59 +08003631 /* check error */
3632 if (status & (ATA_ERR | ATA_DF)) {
3633 qc->err_mask |= AC_ERR_DEV;
3634 ap->hsm_task_state = HSM_ST_ERR;
3635 return;
3636 }
3637
3638 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003640 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003642 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 return;
3644 }
3645
3646 atapi_pio_bytes(qc);
3647 } else {
3648 /* handle BSY=0, DRQ=0 as error */
3649 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003650 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003651 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return;
3653 }
3654
3655 ata_pio_sector(qc);
3656 }
3657}
3658
3659static void ata_pio_error(struct ata_port *ap)
3660{
3661 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003664 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Albert Lee0565c262006-02-13 18:55:25 +08003666 if (qc->tf.command != ATA_CMD_PACKET)
3667 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3668
Albert Lee1c848982005-12-05 15:40:15 +08003669 /* make sure qc->err_mask is available to
3670 * know what's wrong and recover
3671 */
Tejun Heoa4631472006-02-11 19:11:13 +09003672 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003673
Albert Lee14be71f2005-09-27 17:36:35 +08003674 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Albert Leea22e2eb2005-12-05 15:38:02 +08003676 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677}
3678
3679static void ata_pio_task(void *_data)
3680{
3681 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003682 unsigned long timeout;
3683 int qc_completed;
3684
3685fsm_start:
3686 timeout = 0;
3687 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Albert Lee14be71f2005-09-27 17:36:35 +08003689 switch (ap->hsm_task_state) {
3690 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 return;
3692
Albert Lee14be71f2005-09-27 17:36:35 +08003693 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 ata_pio_block(ap);
3695 break;
3696
Albert Lee14be71f2005-09-27 17:36:35 +08003697 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003698 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 break;
3700
Albert Lee14be71f2005-09-27 17:36:35 +08003701 case HSM_ST_POLL:
3702 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 timeout = ata_pio_poll(ap);
3704 break;
3705
Albert Lee14be71f2005-09-27 17:36:35 +08003706 case HSM_ST_TMOUT:
3707 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 ata_pio_error(ap);
3709 return;
3710 }
3711
3712 if (timeout)
Tejun Heo8061f5f2006-03-05 15:29:09 +09003713 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003714 else if (!qc_completed)
3715 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716}
3717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718/**
Tejun Heo8061f5f2006-03-05 15:29:09 +09003719 * atapi_packet_task - Write CDB bytes to hardware
3720 * @_data: Port to which ATAPI device is attached.
3721 *
3722 * When device has indicated its readiness to accept
3723 * a CDB, this function is called. Send the CDB.
3724 * If DMA is to be performed, exit immediately.
3725 * Otherwise, we are in polling mode, so poll
3726 * status under operation succeeds or fails.
3727 *
3728 * LOCKING:
3729 * Kernel thread context (may sleep)
3730 */
3731
3732static void atapi_packet_task(void *_data)
3733{
3734 struct ata_port *ap = _data;
3735 struct ata_queued_cmd *qc;
3736 u8 status;
3737
3738 qc = ata_qc_from_tag(ap, ap->active_tag);
3739 WARN_ON(qc == NULL);
3740 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3741
3742 /* sleep-wait for BSY to clear */
3743 DPRINTK("busy wait\n");
3744 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3745 qc->err_mask |= AC_ERR_TIMEOUT;
3746 goto err_out;
3747 }
3748
3749 /* make sure DRQ is set */
3750 status = ata_chk_status(ap);
3751 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3752 qc->err_mask |= AC_ERR_HSM;
3753 goto err_out;
3754 }
3755
3756 /* send SCSI cdb */
3757 DPRINTK("send cdb\n");
3758 WARN_ON(qc->dev->cdb_len < 12);
3759
3760 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3761 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3762 unsigned long flags;
3763
3764 /* Once we're done issuing command and kicking bmdma,
3765 * irq handler takes over. To not lose irq, we need
3766 * to clear NOINTR flag before sending cdb, but
3767 * interrupt handler shouldn't be invoked before we're
3768 * finished. Hence, the following locking.
3769 */
3770 spin_lock_irqsave(&ap->host_set->lock, flags);
3771 ap->flags &= ~ATA_FLAG_NOINTR;
3772 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3773 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3774 ap->ops->bmdma_start(qc); /* initiate bmdma */
3775 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3776 } else {
3777 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3778
3779 /* PIO commands are handled by polling */
3780 ap->hsm_task_state = HSM_ST;
3781 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3782 }
3783
3784 return;
3785
3786err_out:
3787 ata_poll_qc_complete(qc);
3788}
3789
3790/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 * ata_qc_timeout - Handle timeout of queued command
3792 * @qc: Command that timed out
3793 *
3794 * Some part of the kernel (currently, only the SCSI layer)
3795 * has noticed that the active command on port @ap has not
3796 * completed after a specified length of time. Handle this
3797 * condition by disabling DMA (if necessary) and completing
3798 * transactions, with error if necessary.
3799 *
3800 * This also handles the case of the "lost interrupt", where
3801 * for some reason (possibly hardware bug, possibly driver bug)
3802 * an interrupt was not delivered to the driver, even though the
3803 * transaction completed successfully.
3804 *
3805 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003806 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 */
3808
3809static void ata_qc_timeout(struct ata_queued_cmd *qc)
3810{
3811 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003812 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003814 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
3816 DPRINTK("ENTER\n");
3817
Tejun Heoc18d06f2006-02-02 00:56:10 +09003818 ap->hsm_task_state = HSM_ST_IDLE;
3819
Jeff Garzikb8f61532005-08-25 22:01:20 -04003820 spin_lock_irqsave(&host_set->lock, flags);
3821
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 switch (qc->tf.protocol) {
3823
3824 case ATA_PROT_DMA:
3825 case ATA_PROT_ATAPI_DMA:
3826 host_stat = ap->ops->bmdma_status(ap);
3827
3828 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003829 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831 /* fall through */
3832
3833 default:
3834 ata_altstatus(ap);
3835 drv_stat = ata_chk_status(ap);
3836
3837 /* ack bmdma irq events */
3838 ap->ops->irq_clear(ap);
3839
3840 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3841 ap->id, qc->tf.command, drv_stat, host_stat);
3842
3843 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003844 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 break;
3846 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003847
3848 spin_unlock_irqrestore(&host_set->lock, flags);
3849
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003850 ata_eh_qc_complete(qc);
3851
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 DPRINTK("EXIT\n");
3853}
3854
3855/**
3856 * ata_eng_timeout - Handle timeout of queued command
3857 * @ap: Port on which timed-out command is active
3858 *
3859 * Some part of the kernel (currently, only the SCSI layer)
3860 * has noticed that the active command on port @ap has not
3861 * completed after a specified length of time. Handle this
3862 * condition by disabling DMA (if necessary) and completing
3863 * transactions, with error if necessary.
3864 *
3865 * This also handles the case of the "lost interrupt", where
3866 * for some reason (possibly hardware bug, possibly driver bug)
3867 * an interrupt was not delivered to the driver, even though the
3868 * transaction completed successfully.
3869 *
3870 * LOCKING:
3871 * Inherited from SCSI layer (none, can sleep)
3872 */
3873
3874void ata_eng_timeout(struct ata_port *ap)
3875{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 DPRINTK("ENTER\n");
3877
Tejun Heof6379022006-02-10 15:10:48 +09003878 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 DPRINTK("EXIT\n");
3881}
3882
3883/**
3884 * ata_qc_new - Request an available ATA command, for queueing
3885 * @ap: Port associated with device @dev
3886 * @dev: Device from whom we request an available command structure
3887 *
3888 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003889 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 */
3891
3892static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3893{
3894 struct ata_queued_cmd *qc = NULL;
3895 unsigned int i;
3896
3897 for (i = 0; i < ATA_MAX_QUEUE; i++)
3898 if (!test_and_set_bit(i, &ap->qactive)) {
3899 qc = ata_qc_from_tag(ap, i);
3900 break;
3901 }
3902
3903 if (qc)
3904 qc->tag = i;
3905
3906 return qc;
3907}
3908
3909/**
3910 * ata_qc_new_init - Request an available ATA command, and initialize it
3911 * @ap: Port associated with device @dev
3912 * @dev: Device from whom we request an available command structure
3913 *
3914 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003915 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 */
3917
3918struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3919 struct ata_device *dev)
3920{
3921 struct ata_queued_cmd *qc;
3922
3923 qc = ata_qc_new(ap);
3924 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 qc->scsicmd = NULL;
3926 qc->ap = ap;
3927 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003929 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 }
3931
3932 return qc;
3933}
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935/**
3936 * ata_qc_free - free unused ata_queued_cmd
3937 * @qc: Command to complete
3938 *
3939 * Designed to free unused ata_queued_cmd object
3940 * in case something prevents using it.
3941 *
3942 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003943 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 */
3945void ata_qc_free(struct ata_queued_cmd *qc)
3946{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003947 struct ata_port *ap = qc->ap;
3948 unsigned int tag;
3949
Tejun Heoa4631472006-02-11 19:11:13 +09003950 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Tejun Heo4ba946e2006-01-23 13:09:36 +09003952 qc->flags = 0;
3953 tag = qc->tag;
3954 if (likely(ata_tag_valid(tag))) {
3955 if (tag == ap->active_tag)
3956 ap->active_tag = ATA_TAG_POISON;
3957 qc->tag = ATA_TAG_POISON;
3958 clear_bit(tag, &ap->qactive);
3959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960}
3961
Tejun Heo76014422006-02-11 15:13:49 +09003962void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963{
Tejun Heoa4631472006-02-11 19:11:13 +09003964 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3965 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
3967 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3968 ata_sg_clean(qc);
3969
Albert Lee3f3791d2005-08-16 14:25:38 +08003970 /* atapi: mark qc as inactive to prevent the interrupt handler
3971 * from completing the command twice later, before the error handler
3972 * is called. (when rc != 0 and atapi request sense is needed)
3973 */
3974 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003977 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978}
3979
3980static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3981{
3982 struct ata_port *ap = qc->ap;
3983
3984 switch (qc->tf.protocol) {
3985 case ATA_PROT_DMA:
3986 case ATA_PROT_ATAPI_DMA:
3987 return 1;
3988
3989 case ATA_PROT_ATAPI:
3990 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 if (ap->flags & ATA_FLAG_PIO_DMA)
3992 return 1;
3993
3994 /* fall through */
3995
3996 default:
3997 return 0;
3998 }
3999
4000 /* never reached */
4001}
4002
4003/**
4004 * ata_qc_issue - issue taskfile to device
4005 * @qc: command to issue to device
4006 *
4007 * Prepare an ATA command to submission to device.
4008 * This includes mapping the data into a DMA-able
4009 * area, filling in the S/G table, and finally
4010 * writing the taskfile to hardware, starting the command.
4011 *
4012 * LOCKING:
4013 * spin_lock_irqsave(host_set lock)
4014 *
4015 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004016 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 */
4018
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004019unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
4021 struct ata_port *ap = qc->ap;
4022
4023 if (ata_should_dma_map(qc)) {
4024 if (qc->flags & ATA_QCFLAG_SG) {
4025 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004026 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4028 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004029 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 }
4031 } else {
4032 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4033 }
4034
4035 ap->ops->qc_prep(qc);
4036
4037 qc->ap->active_tag = qc->tag;
4038 qc->flags |= ATA_QCFLAG_ACTIVE;
4039
4040 return ap->ops->qc_issue(qc);
4041
Tejun Heo8e436af2006-01-23 13:09:36 +09004042sg_err:
4043 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004044 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045}
4046
Edward Falk0baab862005-06-02 18:17:13 -04004047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048/**
4049 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4050 * @qc: command to issue to device
4051 *
4052 * Using various libata functions and hooks, this function
4053 * starts an ATA command. ATA commands are grouped into
4054 * classes called "protocols", and issuing each type of protocol
4055 * is slightly different.
4056 *
Edward Falk0baab862005-06-02 18:17:13 -04004057 * May be used as the qc_issue() entry in ata_port_operations.
4058 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 * LOCKING:
4060 * spin_lock_irqsave(host_set lock)
4061 *
4062 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004063 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 */
4065
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004066unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
4068 struct ata_port *ap = qc->ap;
4069
4070 ata_dev_select(ap, qc->dev->devno, 1, 0);
4071
4072 switch (qc->tf.protocol) {
4073 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05004074 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 break;
4076
4077 case ATA_PROT_DMA:
4078 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4079 ap->ops->bmdma_setup(qc); /* set up bmdma */
4080 ap->ops->bmdma_start(qc); /* initiate bmdma */
4081 break;
4082
4083 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4084 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004085 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08004086 ap->hsm_task_state = HSM_ST;
Tejun Heo8061f5f2006-03-05 15:29:09 +09004087 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 break;
4089
4090 case ATA_PROT_ATAPI:
4091 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004092 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004093 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 break;
4095
4096 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09004097 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05004098 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004099 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 break;
4101
4102 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09004103 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4105 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo8061f5f2006-03-05 15:29:09 +09004106 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 break;
4108
4109 default:
4110 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004111 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 }
4113
4114 return 0;
4115}
4116
4117/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 * ata_host_intr - Handle host interrupt for given (port, task)
4119 * @ap: Port on which interrupt arrived (possibly...)
4120 * @qc: Taskfile currently active in engine
4121 *
4122 * Handle host interrupt for given queued command. Currently,
4123 * only DMA interrupts are handled. All other commands are
4124 * handled via polling with interrupts disabled (nIEN bit).
4125 *
4126 * LOCKING:
4127 * spin_lock_irqsave(host_set lock)
4128 *
4129 * RETURNS:
4130 * One if interrupt was handled, zero if not (shared irq).
4131 */
4132
4133inline unsigned int ata_host_intr (struct ata_port *ap,
4134 struct ata_queued_cmd *qc)
4135{
4136 u8 status, host_stat;
4137
4138 switch (qc->tf.protocol) {
4139
4140 case ATA_PROT_DMA:
4141 case ATA_PROT_ATAPI_DMA:
4142 case ATA_PROT_ATAPI:
4143 /* check status of DMA engine */
4144 host_stat = ap->ops->bmdma_status(ap);
4145 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4146
4147 /* if it's not our irq... */
4148 if (!(host_stat & ATA_DMA_INTR))
4149 goto idle_irq;
4150
4151 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004152 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
4154 /* fall through */
4155
4156 case ATA_PROT_ATAPI_NODATA:
4157 case ATA_PROT_NODATA:
4158 /* check altstatus */
4159 status = ata_altstatus(ap);
4160 if (status & ATA_BUSY)
4161 goto idle_irq;
4162
4163 /* check main status, clearing INTRQ */
4164 status = ata_chk_status(ap);
4165 if (unlikely(status & ATA_BUSY))
4166 goto idle_irq;
4167 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4168 ap->id, qc->tf.protocol, status);
4169
4170 /* ack bmdma irq events */
4171 ap->ops->irq_clear(ap);
4172
4173 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004174 qc->err_mask |= ac_err_mask(status);
4175 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 break;
4177
4178 default:
4179 goto idle_irq;
4180 }
4181
4182 return 1; /* irq handled */
4183
4184idle_irq:
4185 ap->stats.idle_irq++;
4186
4187#ifdef ATA_IRQ_TRAP
4188 if ((ap->stats.idle_irq % 1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 ata_irq_ack(ap, 0); /* debug trap */
4190 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
Alan Cox23cfce82006-03-21 16:06:53 +00004191 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 }
4193#endif
4194 return 0; /* irq not handled */
4195}
4196
4197/**
4198 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004199 * @irq: irq line (unused)
4200 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 * @regs: unused
4202 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004203 * Default interrupt handler for PCI IDE devices. Calls
4204 * ata_host_intr() for each port that is not disabled.
4205 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004207 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 *
4209 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004210 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 */
4212
4213irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4214{
4215 struct ata_host_set *host_set = dev_instance;
4216 unsigned int i;
4217 unsigned int handled = 0;
4218 unsigned long flags;
4219
4220 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4221 spin_lock_irqsave(&host_set->lock, flags);
4222
4223 for (i = 0; i < host_set->n_ports; i++) {
4224 struct ata_port *ap;
4225
4226 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004227 if (ap &&
4228 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 struct ata_queued_cmd *qc;
4230
4231 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004232 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4233 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 handled |= ata_host_intr(ap, qc);
4235 }
4236 }
4237
4238 spin_unlock_irqrestore(&host_set->lock, flags);
4239
4240 return IRQ_RETVAL(handled);
4241}
4242
Edward Falk0baab862005-06-02 18:17:13 -04004243
Jens Axboe9b847542006-01-06 09:28:07 +01004244/*
4245 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4246 * without filling any other registers
4247 */
4248static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4249 u8 cmd)
4250{
4251 struct ata_taskfile tf;
4252 int err;
4253
4254 ata_tf_init(ap, &tf, dev->devno);
4255
4256 tf.command = cmd;
4257 tf.flags |= ATA_TFLAG_DEVICE;
4258 tf.protocol = ATA_PROT_NODATA;
4259
4260 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4261 if (err)
4262 printk(KERN_ERR "%s: ata command failed: %d\n",
4263 __FUNCTION__, err);
4264
4265 return err;
4266}
4267
4268static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4269{
4270 u8 cmd;
4271
4272 if (!ata_try_flush_cache(dev))
4273 return 0;
4274
4275 if (ata_id_has_flush_ext(dev->id))
4276 cmd = ATA_CMD_FLUSH_EXT;
4277 else
4278 cmd = ATA_CMD_FLUSH;
4279
4280 return ata_do_simple_cmd(ap, dev, cmd);
4281}
4282
4283static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4284{
4285 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4286}
4287
4288static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4289{
4290 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4291}
4292
4293/**
4294 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004295 * @ap: port the device is connected to
4296 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004297 *
4298 * Kick the drive back into action, by sending it an idle immediate
4299 * command and making sure its transfer mode matches between drive
4300 * and host.
4301 *
4302 */
4303int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4304{
4305 if (ap->flags & ATA_FLAG_SUSPENDED) {
4306 ap->flags &= ~ATA_FLAG_SUSPENDED;
4307 ata_set_mode(ap);
4308 }
4309 if (!ata_dev_present(dev))
4310 return 0;
4311 if (dev->class == ATA_DEV_ATA)
4312 ata_start_drive(ap, dev);
4313
4314 return 0;
4315}
4316
4317/**
4318 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004319 * @ap: port the device is connected to
4320 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004321 *
4322 * Flush the cache on the drive, if appropriate, then issue a
4323 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004324 */
4325int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4326{
4327 if (!ata_dev_present(dev))
4328 return 0;
4329 if (dev->class == ATA_DEV_ATA)
4330 ata_flush_cache(ap, dev);
4331
4332 ata_standby_drive(ap, dev);
4333 ap->flags |= ATA_FLAG_SUSPENDED;
4334 return 0;
4335}
4336
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004337/**
4338 * ata_port_start - Set port up for dma.
4339 * @ap: Port to initialize
4340 *
4341 * Called just after data structures for each port are
4342 * initialized. Allocates space for PRD table.
4343 *
4344 * May be used as the port_start() entry in ata_port_operations.
4345 *
4346 * LOCKING:
4347 * Inherited from caller.
4348 */
4349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350int ata_port_start (struct ata_port *ap)
4351{
4352 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004353 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
4355 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4356 if (!ap->prd)
4357 return -ENOMEM;
4358
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004359 rc = ata_pad_alloc(ap, dev);
4360 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004361 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004362 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004363 }
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4366
4367 return 0;
4368}
4369
Edward Falk0baab862005-06-02 18:17:13 -04004370
4371/**
4372 * ata_port_stop - Undo ata_port_start()
4373 * @ap: Port to shut down
4374 *
4375 * Frees the PRD table.
4376 *
4377 * May be used as the port_stop() entry in ata_port_operations.
4378 *
4379 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004380 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004381 */
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383void ata_port_stop (struct ata_port *ap)
4384{
4385 struct device *dev = ap->host_set->dev;
4386
4387 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004388 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389}
4390
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004391void ata_host_stop (struct ata_host_set *host_set)
4392{
4393 if (host_set->mmio_base)
4394 iounmap(host_set->mmio_base);
4395}
4396
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398/**
4399 * ata_host_remove - Unregister SCSI host structure with upper layers
4400 * @ap: Port to unregister
4401 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4402 *
4403 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004404 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 */
4406
4407static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4408{
4409 struct Scsi_Host *sh = ap->host;
4410
4411 DPRINTK("ENTER\n");
4412
4413 if (do_unregister)
4414 scsi_remove_host(sh);
4415
4416 ap->ops->port_stop(ap);
4417}
4418
4419/**
4420 * ata_host_init - Initialize an ata_port structure
4421 * @ap: Structure to initialize
4422 * @host: associated SCSI mid-layer structure
4423 * @host_set: Collection of hosts to which @ap belongs
4424 * @ent: Probe information provided by low-level driver
4425 * @port_no: Port number associated with this ata_port
4426 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004427 * Initialize a new ata_port structure, and its associated
4428 * scsi_host.
4429 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004431 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 */
4433
4434static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4435 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004436 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
4438 unsigned int i;
4439
4440 host->max_id = 16;
4441 host->max_lun = 1;
4442 host->max_channel = 1;
4443 host->unique_id = ata_unique_id++;
4444 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 ap->flags = ATA_FLAG_PORT_DISABLED;
4447 ap->id = host->unique_id;
4448 ap->host = host;
4449 ap->ctl = ATA_DEVCTL_OBS;
4450 ap->host_set = host_set;
4451 ap->port_no = port_no;
4452 ap->hard_port_no =
4453 ent->legacy_mode ? ent->hard_port_no : port_no;
4454 ap->pio_mask = ent->pio_mask;
4455 ap->mwdma_mask = ent->mwdma_mask;
4456 ap->udma_mask = ent->udma_mask;
4457 ap->flags |= ent->host_flags;
4458 ap->ops = ent->port_ops;
4459 ap->cbl = ATA_CBL_NONE;
4460 ap->active_tag = ATA_TAG_POISON;
4461 ap->last_ctl = 0xFF;
4462
Tejun Heo86e45b62006-03-05 15:29:09 +09004463 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004464 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
Tejun Heoacf356b2006-03-24 14:07:50 +09004466 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4467 struct ata_device *dev = &ap->device[i];
4468 dev->devno = i;
4469 dev->pio_mask = UINT_MAX;
4470 dev->mwdma_mask = UINT_MAX;
4471 dev->udma_mask = UINT_MAX;
4472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474#ifdef ATA_IRQ_TRAP
4475 ap->stats.unhandled_irq = 1;
4476 ap->stats.idle_irq = 1;
4477#endif
4478
4479 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4480}
4481
4482/**
4483 * ata_host_add - Attach low-level ATA driver to system
4484 * @ent: Information provided by low-level driver
4485 * @host_set: Collections of ports to which we add
4486 * @port_no: Port number associated with this host
4487 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004488 * Attach low-level ATA driver to system.
4489 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004491 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 *
4493 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004494 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 */
4496
Jeff Garzik057ace52005-10-22 14:27:05 -04004497static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 struct ata_host_set *host_set,
4499 unsigned int port_no)
4500{
4501 struct Scsi_Host *host;
4502 struct ata_port *ap;
4503 int rc;
4504
4505 DPRINTK("ENTER\n");
4506 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4507 if (!host)
4508 return NULL;
4509
Tejun Heo30afc842006-03-18 18:40:14 +09004510 host->transportt = &ata_scsi_transport_template;
4511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 ap = (struct ata_port *) &host->hostdata[0];
4513
4514 ata_host_init(ap, host, host_set, ent, port_no);
4515
4516 rc = ap->ops->port_start(ap);
4517 if (rc)
4518 goto err_out;
4519
4520 return ap;
4521
4522err_out:
4523 scsi_host_put(host);
4524 return NULL;
4525}
4526
4527/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004528 * ata_device_add - Register hardware device with ATA and SCSI layers
4529 * @ent: Probe information describing hardware device to be registered
4530 *
4531 * This function processes the information provided in the probe
4532 * information struct @ent, allocates the necessary ATA and SCSI
4533 * host information structures, initializes them, and registers
4534 * everything with requisite kernel subsystems.
4535 *
4536 * This function requests irqs, probes the ATA bus, and probes
4537 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 *
4539 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004540 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 *
4542 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004543 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 */
4545
Jeff Garzik057ace52005-10-22 14:27:05 -04004546int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
4548 unsigned int count = 0, i;
4549 struct device *dev = ent->dev;
4550 struct ata_host_set *host_set;
4551
4552 DPRINTK("ENTER\n");
4553 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004554 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4556 if (!host_set)
4557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 spin_lock_init(&host_set->lock);
4559
4560 host_set->dev = dev;
4561 host_set->n_ports = ent->n_ports;
4562 host_set->irq = ent->irq;
4563 host_set->mmio_base = ent->mmio_base;
4564 host_set->private_data = ent->private_data;
4565 host_set->ops = ent->port_ops;
4566
4567 /* register each port bound to this device */
4568 for (i = 0; i < ent->n_ports; i++) {
4569 struct ata_port *ap;
4570 unsigned long xfer_mode_mask;
4571
4572 ap = ata_host_add(ent, host_set, i);
4573 if (!ap)
4574 goto err_out;
4575
4576 host_set->ports[i] = ap;
4577 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4578 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4579 (ap->pio_mask << ATA_SHIFT_PIO);
4580
4581 /* print per-port info to dmesg */
4582 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4583 "bmdma 0x%lX irq %lu\n",
4584 ap->id,
4585 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4586 ata_mode_string(xfer_mode_mask),
4587 ap->ioaddr.cmd_addr,
4588 ap->ioaddr.ctl_addr,
4589 ap->ioaddr.bmdma_addr,
4590 ent->irq);
4591
4592 ata_chk_status(ap);
4593 host_set->ops->irq_clear(ap);
4594 count++;
4595 }
4596
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004597 if (!count)
4598 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
4600 /* obtain irq, that is shared between channels */
4601 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4602 DRV_NAME, host_set))
4603 goto err_out;
4604
4605 /* perform each probe synchronously */
4606 DPRINTK("probe begin\n");
4607 for (i = 0; i < count; i++) {
4608 struct ata_port *ap;
4609 int rc;
4610
4611 ap = host_set->ports[i];
4612
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004613 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004615 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 if (rc) {
4618 /* FIXME: do something useful here?
4619 * Current libata behavior will
4620 * tear down everything when
4621 * the module is removed
4622 * or the h/w is unplugged.
4623 */
4624 }
4625
4626 rc = scsi_add_host(ap->host, dev);
4627 if (rc) {
4628 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4629 ap->id);
4630 /* FIXME: do something useful here */
4631 /* FIXME: handle unconditional calls to
4632 * scsi_scan_host and ata_host_remove, below,
4633 * at the very least
4634 */
4635 }
4636 }
4637
4638 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004639 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 for (i = 0; i < count; i++) {
4641 struct ata_port *ap = host_set->ports[i];
4642
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004643 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
4645
4646 dev_set_drvdata(dev, host_set);
4647
4648 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4649 return ent->n_ports; /* success */
4650
4651err_out:
4652 for (i = 0; i < count; i++) {
4653 ata_host_remove(host_set->ports[i], 1);
4654 scsi_host_put(host_set->ports[i]->host);
4655 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004656err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 kfree(host_set);
4658 VPRINTK("EXIT, returning 0\n");
4659 return 0;
4660}
4661
4662/**
Alan Cox17b14452005-09-15 15:44:00 +01004663 * ata_host_set_remove - PCI layer callback for device removal
4664 * @host_set: ATA host set that was removed
4665 *
4666 * Unregister all objects associated with this host set. Free those
4667 * objects.
4668 *
4669 * LOCKING:
4670 * Inherited from calling layer (may sleep).
4671 */
4672
Alan Cox17b14452005-09-15 15:44:00 +01004673void ata_host_set_remove(struct ata_host_set *host_set)
4674{
4675 struct ata_port *ap;
4676 unsigned int i;
4677
4678 for (i = 0; i < host_set->n_ports; i++) {
4679 ap = host_set->ports[i];
4680 scsi_remove_host(ap->host);
4681 }
4682
4683 free_irq(host_set->irq, host_set);
4684
4685 for (i = 0; i < host_set->n_ports; i++) {
4686 ap = host_set->ports[i];
4687
4688 ata_scsi_release(ap->host);
4689
4690 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4691 struct ata_ioports *ioaddr = &ap->ioaddr;
4692
4693 if (ioaddr->cmd_addr == 0x1f0)
4694 release_region(0x1f0, 8);
4695 else if (ioaddr->cmd_addr == 0x170)
4696 release_region(0x170, 8);
4697 }
4698
4699 scsi_host_put(ap->host);
4700 }
4701
4702 if (host_set->ops->host_stop)
4703 host_set->ops->host_stop(host_set);
4704
4705 kfree(host_set);
4706}
4707
4708/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 * ata_scsi_release - SCSI layer callback hook for host unload
4710 * @host: libata host to be unloaded
4711 *
4712 * Performs all duties necessary to shut down a libata port...
4713 * Kill port kthread, disable port, and release resources.
4714 *
4715 * LOCKING:
4716 * Inherited from SCSI layer.
4717 *
4718 * RETURNS:
4719 * One.
4720 */
4721
4722int ata_scsi_release(struct Scsi_Host *host)
4723{
4724 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004725 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
4727 DPRINTK("ENTER\n");
4728
4729 ap->ops->port_disable(ap);
4730 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004731 for (i = 0; i < ATA_MAX_DEVICES; i++)
4732 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734 DPRINTK("EXIT\n");
4735 return 1;
4736}
4737
4738/**
4739 * ata_std_ports - initialize ioaddr with standard port offsets.
4740 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004741 *
4742 * Utility function which initializes data_addr, error_addr,
4743 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4744 * device_addr, status_addr, and command_addr to standard offsets
4745 * relative to cmd_addr.
4746 *
4747 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 */
Edward Falk0baab862005-06-02 18:17:13 -04004749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750void ata_std_ports(struct ata_ioports *ioaddr)
4751{
4752 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4753 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4754 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4755 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4756 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4757 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4758 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4759 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4760 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4761 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4762}
4763
Edward Falk0baab862005-06-02 18:17:13 -04004764
Jeff Garzik374b1872005-08-30 05:42:52 -04004765#ifdef CONFIG_PCI
4766
4767void ata_pci_host_stop (struct ata_host_set *host_set)
4768{
4769 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4770
4771 pci_iounmap(pdev, host_set->mmio_base);
4772}
4773
Edward Falk0baab862005-06-02 18:17:13 -04004774/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 * ata_pci_remove_one - PCI layer callback for device removal
4776 * @pdev: PCI device that was removed
4777 *
4778 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004779 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 * Handle this by unregistering all objects associated
4781 * with this PCI device. Free those objects. Then finally
4782 * release PCI resources and disable device.
4783 *
4784 * LOCKING:
4785 * Inherited from PCI layer (may sleep).
4786 */
4787
4788void ata_pci_remove_one (struct pci_dev *pdev)
4789{
4790 struct device *dev = pci_dev_to_dev(pdev);
4791 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Alan Cox17b14452005-09-15 15:44:00 +01004793 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 pci_release_regions(pdev);
4795 pci_disable_device(pdev);
4796 dev_set_drvdata(dev, NULL);
4797}
4798
4799/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004800int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
4802 unsigned long tmp = 0;
4803
4804 switch (bits->width) {
4805 case 1: {
4806 u8 tmp8 = 0;
4807 pci_read_config_byte(pdev, bits->reg, &tmp8);
4808 tmp = tmp8;
4809 break;
4810 }
4811 case 2: {
4812 u16 tmp16 = 0;
4813 pci_read_config_word(pdev, bits->reg, &tmp16);
4814 tmp = tmp16;
4815 break;
4816 }
4817 case 4: {
4818 u32 tmp32 = 0;
4819 pci_read_config_dword(pdev, bits->reg, &tmp32);
4820 tmp = tmp32;
4821 break;
4822 }
4823
4824 default:
4825 return -EINVAL;
4826 }
4827
4828 tmp &= bits->mask;
4829
4830 return (tmp == bits->val) ? 1 : 0;
4831}
Jens Axboe9b847542006-01-06 09:28:07 +01004832
4833int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4834{
4835 pci_save_state(pdev);
4836 pci_disable_device(pdev);
4837 pci_set_power_state(pdev, PCI_D3hot);
4838 return 0;
4839}
4840
4841int ata_pci_device_resume(struct pci_dev *pdev)
4842{
4843 pci_set_power_state(pdev, PCI_D0);
4844 pci_restore_state(pdev);
4845 pci_enable_device(pdev);
4846 pci_set_master(pdev);
4847 return 0;
4848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849#endif /* CONFIG_PCI */
4850
4851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852static int __init ata_init(void)
4853{
4854 ata_wq = create_workqueue("ata");
4855 if (!ata_wq)
4856 return -ENOMEM;
4857
4858 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4859 return 0;
4860}
4861
4862static void __exit ata_exit(void)
4863{
4864 destroy_workqueue(ata_wq);
4865}
4866
4867module_init(ata_init);
4868module_exit(ata_exit);
4869
Jeff Garzik67846b32005-10-05 02:58:32 -04004870static unsigned long ratelimit_time;
4871static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4872
4873int ata_ratelimit(void)
4874{
4875 int rc;
4876 unsigned long flags;
4877
4878 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4879
4880 if (time_after(jiffies, ratelimit_time)) {
4881 rc = 1;
4882 ratelimit_time = jiffies + (HZ/5);
4883 } else
4884 rc = 0;
4885
4886 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4887
4888 return rc;
4889}
4890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891/*
4892 * libata is essentially a library of internal helper functions for
4893 * low-level ATA host controller drivers. As such, the API/ABI is
4894 * likely to change as new drivers are added and updated.
4895 * Do not depend on ABI/API stability.
4896 */
4897
4898EXPORT_SYMBOL_GPL(ata_std_bios_param);
4899EXPORT_SYMBOL_GPL(ata_std_ports);
4900EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004901EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902EXPORT_SYMBOL_GPL(ata_sg_init);
4903EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09004904EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4906EXPORT_SYMBOL_GPL(ata_eng_timeout);
4907EXPORT_SYMBOL_GPL(ata_tf_load);
4908EXPORT_SYMBOL_GPL(ata_tf_read);
4909EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4910EXPORT_SYMBOL_GPL(ata_std_dev_select);
4911EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4912EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4913EXPORT_SYMBOL_GPL(ata_check_status);
4914EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915EXPORT_SYMBOL_GPL(ata_exec_command);
4916EXPORT_SYMBOL_GPL(ata_port_start);
4917EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004918EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919EXPORT_SYMBOL_GPL(ata_interrupt);
4920EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06004921EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4923EXPORT_SYMBOL_GPL(ata_bmdma_start);
4924EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4925EXPORT_SYMBOL_GPL(ata_bmdma_status);
4926EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4927EXPORT_SYMBOL_GPL(ata_port_probe);
4928EXPORT_SYMBOL_GPL(sata_phy_reset);
4929EXPORT_SYMBOL_GPL(__sata_phy_reset);
4930EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004931EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004932EXPORT_SYMBOL_GPL(ata_std_softreset);
4933EXPORT_SYMBOL_GPL(sata_std_hardreset);
4934EXPORT_SYMBOL_GPL(ata_std_postreset);
4935EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004936EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09004937EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004939EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004940EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09004941EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4943EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4944EXPORT_SYMBOL_GPL(ata_scsi_error);
4945EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4946EXPORT_SYMBOL_GPL(ata_scsi_release);
4947EXPORT_SYMBOL_GPL(ata_host_intr);
4948EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09004949EXPORT_SYMBOL_GPL(ata_id_string);
4950EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004952EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4953EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
Alan Cox1bc4ccf2006-01-09 17:18:14 +00004955EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04004956EXPORT_SYMBOL_GPL(ata_timing_compute);
4957EXPORT_SYMBOL_GPL(ata_timing_merge);
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959#ifdef CONFIG_PCI
4960EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004961EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4963EXPORT_SYMBOL_GPL(ata_pci_init_one);
4964EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01004965EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4966EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Alan Cox67951ad2006-03-22 15:55:54 +00004967EXPORT_SYMBOL_GPL(ata_pci_default_filter);
4968EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01004970
4971EXPORT_SYMBOL_GPL(ata_device_suspend);
4972EXPORT_SYMBOL_GPL(ata_device_resume);
4973EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4974EXPORT_SYMBOL_GPL(ata_scsi_device_resume);