blob: d211f0b1d72129e8d3e57617d77b73afa5f66d26 [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 Heoa6d5a512006-03-06 04:31:57 +090068static unsigned int ata_dev_xfermask(struct ata_port *ap,
69 struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static int fgb(u32 bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static unsigned int ata_unique_id = 1;
73static struct workqueue_struct *ata_wq;
74
Jeff Garzik1623c812005-08-30 03:37:42 -040075int atapi_enabled = 0;
76module_param(atapi_enabled, int, 0444);
77MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
78
Jeff Garzikc3c013a2006-02-27 22:31:19 -050079int libata_fua = 0;
80module_param_named(fua, libata_fua, int, 0444);
81MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083MODULE_AUTHOR("Jeff Garzik");
84MODULE_DESCRIPTION("Library module for ATA devices");
85MODULE_LICENSE("GPL");
86MODULE_VERSION(DRV_VERSION);
87
Edward Falk0baab862005-06-02 18:17:13 -040088
89/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
91 * @tf: Taskfile to convert
92 * @fis: Buffer into which data will output
93 * @pmp: Port multiplier port
94 *
95 * Converts a standard ATA taskfile to a Serial ATA
96 * FIS structure (Register - Host to Device).
97 *
98 * LOCKING:
99 * Inherited from caller.
100 */
101
Jeff Garzik057ace52005-10-22 14:27:05 -0400102void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 fis[0] = 0x27; /* Register - Host to Device FIS */
105 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
106 bit 7 indicates Command FIS */
107 fis[2] = tf->command;
108 fis[3] = tf->feature;
109
110 fis[4] = tf->lbal;
111 fis[5] = tf->lbam;
112 fis[6] = tf->lbah;
113 fis[7] = tf->device;
114
115 fis[8] = tf->hob_lbal;
116 fis[9] = tf->hob_lbam;
117 fis[10] = tf->hob_lbah;
118 fis[11] = tf->hob_feature;
119
120 fis[12] = tf->nsect;
121 fis[13] = tf->hob_nsect;
122 fis[14] = 0;
123 fis[15] = tf->ctl;
124
125 fis[16] = 0;
126 fis[17] = 0;
127 fis[18] = 0;
128 fis[19] = 0;
129}
130
131/**
132 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
133 * @fis: Buffer from which data will be input
134 * @tf: Taskfile to output
135 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500136 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * LOCKING:
139 * Inherited from caller.
140 */
141
Jeff Garzik057ace52005-10-22 14:27:05 -0400142void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 tf->command = fis[2]; /* status */
145 tf->feature = fis[3]; /* error */
146
147 tf->lbal = fis[4];
148 tf->lbam = fis[5];
149 tf->lbah = fis[6];
150 tf->device = fis[7];
151
152 tf->hob_lbal = fis[8];
153 tf->hob_lbam = fis[9];
154 tf->hob_lbah = fis[10];
155
156 tf->nsect = fis[12];
157 tf->hob_nsect = fis[13];
158}
159
Albert Lee8cbd6df2005-10-12 15:06:27 +0800160static const u8 ata_rw_cmds[] = {
161 /* pio multi */
162 ATA_CMD_READ_MULTI,
163 ATA_CMD_WRITE_MULTI,
164 ATA_CMD_READ_MULTI_EXT,
165 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100166 0,
167 0,
168 0,
169 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800170 /* pio */
171 ATA_CMD_PIO_READ,
172 ATA_CMD_PIO_WRITE,
173 ATA_CMD_PIO_READ_EXT,
174 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100175 0,
176 0,
177 0,
178 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800179 /* dma */
180 ATA_CMD_READ,
181 ATA_CMD_WRITE,
182 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100183 ATA_CMD_WRITE_EXT,
184 0,
185 0,
186 0,
187 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800188};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800191 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
192 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800194 * Examine the device configuration and tf->flags to calculate
195 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
197 * LOCKING:
198 * caller.
199 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100200int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800202 struct ata_taskfile *tf = &qc->tf;
203 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100204 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800207
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100208 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800209 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
210 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Albert Lee8cbd6df2005-10-12 15:06:27 +0800212 if (dev->flags & ATA_DFLAG_PIO) {
213 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100214 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000215 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
216 /* Unable to use DMA due to host limitation */
217 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800218 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800219 } else {
220 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100221 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100224 cmd = ata_rw_cmds[index + fua + lba48 + write];
225 if (cmd) {
226 tf->command = cmd;
227 return 0;
228 }
229 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Tejun Heocb95d562006-03-06 04:31:56 +0900232/**
233 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
234 * @pio_mask: pio_mask
235 * @mwdma_mask: mwdma_mask
236 * @udma_mask: udma_mask
237 *
238 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
239 * unsigned int xfer_mask.
240 *
241 * LOCKING:
242 * None.
243 *
244 * RETURNS:
245 * Packed xfer_mask.
246 */
247static unsigned int ata_pack_xfermask(unsigned int pio_mask,
248 unsigned int mwdma_mask,
249 unsigned int udma_mask)
250{
251 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
252 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
253 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
254}
255
256static const struct ata_xfer_ent {
257 unsigned int shift, bits;
258 u8 base;
259} ata_xfer_tbl[] = {
260 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
261 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
262 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
263 { -1, },
264};
265
266/**
267 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
268 * @xfer_mask: xfer_mask of interest
269 *
270 * Return matching XFER_* value for @xfer_mask. Only the highest
271 * bit of @xfer_mask is considered.
272 *
273 * LOCKING:
274 * None.
275 *
276 * RETURNS:
277 * Matching XFER_* value, 0 if no match found.
278 */
279static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
280{
281 int highbit = fls(xfer_mask) - 1;
282 const struct ata_xfer_ent *ent;
283
284 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
285 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
286 return ent->base + highbit - ent->shift;
287 return 0;
288}
289
290/**
291 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
292 * @xfer_mode: XFER_* of interest
293 *
294 * Return matching xfer_mask for @xfer_mode.
295 *
296 * LOCKING:
297 * None.
298 *
299 * RETURNS:
300 * Matching xfer_mask, 0 if no match found.
301 */
302static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
303{
304 const struct ata_xfer_ent *ent;
305
306 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
307 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
308 return 1 << (ent->shift + xfer_mode - ent->base);
309 return 0;
310}
311
312/**
313 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
314 * @xfer_mode: XFER_* of interest
315 *
316 * Return matching xfer_shift for @xfer_mode.
317 *
318 * LOCKING:
319 * None.
320 *
321 * RETURNS:
322 * Matching xfer_shift, -1 if no match found.
323 */
324static int ata_xfer_mode2shift(unsigned int xfer_mode)
325{
326 const struct ata_xfer_ent *ent;
327
328 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
329 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
330 return ent->shift;
331 return -1;
332}
333
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100334static const char * const xfer_mode_str[] = {
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900335 "PIO0",
336 "PIO1",
337 "PIO2",
338 "PIO3",
339 "PIO4",
340 "MWDMA0",
341 "MWDMA1",
342 "MWDMA2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 "UDMA/16",
344 "UDMA/25",
345 "UDMA/33",
346 "UDMA/44",
347 "UDMA/66",
348 "UDMA/100",
349 "UDMA/133",
350 "UDMA7",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351};
352
353/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900354 * ata_mode_string - convert xfer_mask to string
355 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *
357 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900358 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 *
360 * LOCKING:
361 * None.
362 *
363 * RETURNS:
364 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900365 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
367
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900368static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900370 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900372 highbit = fls(xfer_mask) - 1;
373 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
374 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378/**
379 * ata_pio_devchk - PATA device presence detection
380 * @ap: ATA channel to examine
381 * @device: Device to examine (starting at zero)
382 *
383 * This technique was originally described in
384 * Hale Landis's ATADRVR (www.ata-atapi.com), and
385 * later found its way into the ATA/ATAPI spec.
386 *
387 * Write a pattern to the ATA shadow registers,
388 * and if a device is present, it will respond by
389 * correctly storing and echoing back the
390 * ATA shadow register contents.
391 *
392 * LOCKING:
393 * caller.
394 */
395
396static unsigned int ata_pio_devchk(struct ata_port *ap,
397 unsigned int device)
398{
399 struct ata_ioports *ioaddr = &ap->ioaddr;
400 u8 nsect, lbal;
401
402 ap->ops->dev_select(ap, device);
403
404 outb(0x55, ioaddr->nsect_addr);
405 outb(0xaa, ioaddr->lbal_addr);
406
407 outb(0xaa, ioaddr->nsect_addr);
408 outb(0x55, ioaddr->lbal_addr);
409
410 outb(0x55, ioaddr->nsect_addr);
411 outb(0xaa, ioaddr->lbal_addr);
412
413 nsect = inb(ioaddr->nsect_addr);
414 lbal = inb(ioaddr->lbal_addr);
415
416 if ((nsect == 0x55) && (lbal == 0xaa))
417 return 1; /* we found a device */
418
419 return 0; /* nothing found */
420}
421
422/**
423 * ata_mmio_devchk - PATA device presence detection
424 * @ap: ATA channel to examine
425 * @device: Device to examine (starting at zero)
426 *
427 * This technique was originally described in
428 * Hale Landis's ATADRVR (www.ata-atapi.com), and
429 * later found its way into the ATA/ATAPI spec.
430 *
431 * Write a pattern to the ATA shadow registers,
432 * and if a device is present, it will respond by
433 * correctly storing and echoing back the
434 * ATA shadow register contents.
435 *
436 * LOCKING:
437 * caller.
438 */
439
440static unsigned int ata_mmio_devchk(struct ata_port *ap,
441 unsigned int device)
442{
443 struct ata_ioports *ioaddr = &ap->ioaddr;
444 u8 nsect, lbal;
445
446 ap->ops->dev_select(ap, device);
447
448 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
449 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
450
451 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
452 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
453
454 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
455 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
456
457 nsect = readb((void __iomem *) ioaddr->nsect_addr);
458 lbal = readb((void __iomem *) ioaddr->lbal_addr);
459
460 if ((nsect == 0x55) && (lbal == 0xaa))
461 return 1; /* we found a device */
462
463 return 0; /* nothing found */
464}
465
466/**
467 * ata_devchk - PATA device presence detection
468 * @ap: ATA channel to examine
469 * @device: Device to examine (starting at zero)
470 *
471 * Dispatch ATA device presence detection, depending
472 * on whether we are using PIO or MMIO to talk to the
473 * ATA shadow registers.
474 *
475 * LOCKING:
476 * caller.
477 */
478
479static unsigned int ata_devchk(struct ata_port *ap,
480 unsigned int device)
481{
482 if (ap->flags & ATA_FLAG_MMIO)
483 return ata_mmio_devchk(ap, device);
484 return ata_pio_devchk(ap, device);
485}
486
487/**
488 * ata_dev_classify - determine device type based on ATA-spec signature
489 * @tf: ATA taskfile register set for device to be identified
490 *
491 * Determine from taskfile register contents whether a device is
492 * ATA or ATAPI, as per "Signature and persistence" section
493 * of ATA/PI spec (volume 1, sect 5.14).
494 *
495 * LOCKING:
496 * None.
497 *
498 * RETURNS:
499 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
500 * the event of failure.
501 */
502
Jeff Garzik057ace52005-10-22 14:27:05 -0400503unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 /* Apple's open source Darwin code hints that some devices only
506 * put a proper signature into the LBA mid/high registers,
507 * So, we only check those. It's sufficient for uniqueness.
508 */
509
510 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
511 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
512 DPRINTK("found ATA device by sig\n");
513 return ATA_DEV_ATA;
514 }
515
516 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
517 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
518 DPRINTK("found ATAPI device by sig\n");
519 return ATA_DEV_ATAPI;
520 }
521
522 DPRINTK("unknown device\n");
523 return ATA_DEV_UNKNOWN;
524}
525
526/**
527 * ata_dev_try_classify - Parse returned ATA device signature
528 * @ap: ATA channel to examine
529 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900530 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *
532 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
533 * an ATA/ATAPI-defined set of values is placed in the ATA
534 * shadow registers, indicating the results of device detection
535 * and diagnostics.
536 *
537 * Select the ATA device, and read the values from the ATA shadow
538 * registers. Then parse according to the Error register value,
539 * and the spec-defined values examined by ata_dev_classify().
540 *
541 * LOCKING:
542 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900543 *
544 * RETURNS:
545 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
547
Tejun Heob4dc7622006-01-24 17:05:22 +0900548static unsigned int
549ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct ata_taskfile tf;
552 unsigned int class;
553 u8 err;
554
555 ap->ops->dev_select(ap, device);
556
557 memset(&tf, 0, sizeof(tf));
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400560 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900561 if (r_err)
562 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* see if device passed diags */
565 if (err == 1)
566 /* do nothing */ ;
567 else if ((device == 0) && (err == 0x81))
568 /* do nothing */ ;
569 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900570 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Tejun Heob4dc7622006-01-24 17:05:22 +0900572 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900576 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900578 return ATA_DEV_NONE;
579 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900583 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * @id: IDENTIFY DEVICE results we will examine
585 * @s: string into which data is output
586 * @ofs: offset into identify device page
587 * @len: length of string to return. must be an even number.
588 *
589 * The strings in the IDENTIFY DEVICE page are broken up into
590 * 16-bit chunks. Run through the string, and output each
591 * 8-bit chunk linearly, regardless of platform.
592 *
593 * LOCKING:
594 * caller.
595 */
596
Tejun Heo6a62a042006-02-13 10:02:46 +0900597void ata_id_string(const u16 *id, unsigned char *s,
598 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 unsigned int c;
601
602 while (len > 0) {
603 c = id[ofs] >> 8;
604 *s = c;
605 s++;
606
607 c = id[ofs] & 0xff;
608 *s = c;
609 s++;
610
611 ofs++;
612 len -= 2;
613 }
614}
615
Tejun Heo0e949ff2006-02-12 22:47:04 +0900616/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900617 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900618 * @id: IDENTIFY DEVICE results we will examine
619 * @s: string into which data is output
620 * @ofs: offset into identify device page
621 * @len: length of string to return. must be an odd number.
622 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900623 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900624 * trims trailing spaces and terminates the resulting string with
625 * null. @len must be actual maximum length (even number) + 1.
626 *
627 * LOCKING:
628 * caller.
629 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900630void ata_id_c_string(const u16 *id, unsigned char *s,
631 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900632{
633 unsigned char *p;
634
635 WARN_ON(!(len & 1));
636
Tejun Heo6a62a042006-02-13 10:02:46 +0900637 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900638
639 p = s + strnlen(s, len - 1);
640 while (p > s && p[-1] == ' ')
641 p--;
642 *p = '\0';
643}
Edward Falk0baab862005-06-02 18:17:13 -0400644
Tejun Heo29407402006-02-12 22:47:04 +0900645static u64 ata_id_n_sectors(const u16 *id)
646{
647 if (ata_id_has_lba(id)) {
648 if (ata_id_has_lba48(id))
649 return ata_id_u64(id, 100);
650 else
651 return ata_id_u32(id, 60);
652 } else {
653 if (ata_id_current_chs_valid(id))
654 return ata_id_u32(id, 57);
655 else
656 return id[1] * id[3] * id[6];
657 }
658}
659
Edward Falk0baab862005-06-02 18:17:13 -0400660/**
661 * ata_noop_dev_select - Select device 0/1 on ATA bus
662 * @ap: ATA channel to manipulate
663 * @device: ATA device (numbered from zero) to select
664 *
665 * This function performs no actual function.
666 *
667 * May be used as the dev_select() entry in ata_port_operations.
668 *
669 * LOCKING:
670 * caller.
671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
673{
674}
675
Edward Falk0baab862005-06-02 18:17:13 -0400676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/**
678 * ata_std_dev_select - Select device 0/1 on ATA bus
679 * @ap: ATA channel to manipulate
680 * @device: ATA device (numbered from zero) to select
681 *
682 * Use the method defined in the ATA specification to
683 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400684 * ATA channel. Works with both PIO and MMIO.
685 *
686 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 *
688 * LOCKING:
689 * caller.
690 */
691
692void ata_std_dev_select (struct ata_port *ap, unsigned int device)
693{
694 u8 tmp;
695
696 if (device == 0)
697 tmp = ATA_DEVICE_OBS;
698 else
699 tmp = ATA_DEVICE_OBS | ATA_DEV1;
700
701 if (ap->flags & ATA_FLAG_MMIO) {
702 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
703 } else {
704 outb(tmp, ap->ioaddr.device_addr);
705 }
706 ata_pause(ap); /* needed; also flushes, for mmio */
707}
708
709/**
710 * ata_dev_select - Select device 0/1 on ATA bus
711 * @ap: ATA channel to manipulate
712 * @device: ATA device (numbered from zero) to select
713 * @wait: non-zero to wait for Status register BSY bit to clear
714 * @can_sleep: non-zero if context allows sleeping
715 *
716 * Use the method defined in the ATA specification to
717 * make either device 0, or device 1, active on the
718 * ATA channel.
719 *
720 * This is a high-level version of ata_std_dev_select(),
721 * which additionally provides the services of inserting
722 * the proper pauses and status polling, where needed.
723 *
724 * LOCKING:
725 * caller.
726 */
727
728void ata_dev_select(struct ata_port *ap, unsigned int device,
729 unsigned int wait, unsigned int can_sleep)
730{
731 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
732 ap->id, device, wait);
733
734 if (wait)
735 ata_wait_idle(ap);
736
737 ap->ops->dev_select(ap, device);
738
739 if (wait) {
740 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
741 msleep(150);
742 ata_wait_idle(ap);
743 }
744}
745
746/**
747 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900748 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900750 * Dump selected 16-bit words from the given IDENTIFY DEVICE
751 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 *
753 * LOCKING:
754 * caller.
755 */
756
Tejun Heo0bd33002006-02-12 22:47:05 +0900757static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 DPRINTK("49==0x%04x "
760 "53==0x%04x "
761 "63==0x%04x "
762 "64==0x%04x "
763 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900764 id[49],
765 id[53],
766 id[63],
767 id[64],
768 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 DPRINTK("80==0x%04x "
770 "81==0x%04x "
771 "82==0x%04x "
772 "83==0x%04x "
773 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900774 id[80],
775 id[81],
776 id[82],
777 id[83],
778 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 DPRINTK("88==0x%04x "
780 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900781 id[88],
782 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Tejun Heocb95d562006-03-06 04:31:56 +0900785/**
786 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
787 * @id: IDENTIFY data to compute xfer mask from
788 *
789 * Compute the xfermask for this device. This is not as trivial
790 * as it seems if we must consider early devices correctly.
791 *
792 * FIXME: pre IDE drive timing (do we care ?).
793 *
794 * LOCKING:
795 * None.
796 *
797 * RETURNS:
798 * Computed xfermask
799 */
800static unsigned int ata_id_xfermask(const u16 *id)
801{
802 unsigned int pio_mask, mwdma_mask, udma_mask;
803
804 /* Usual case. Word 53 indicates word 64 is valid */
805 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
806 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
807 pio_mask <<= 3;
808 pio_mask |= 0x7;
809 } else {
810 /* If word 64 isn't valid then Word 51 high byte holds
811 * the PIO timing number for the maximum. Turn it into
812 * a mask.
813 */
814 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
815
816 /* But wait.. there's more. Design your standards by
817 * committee and you too can get a free iordy field to
818 * process. However its the speeds not the modes that
819 * are supported... Note drivers using the timing API
820 * will get this right anyway
821 */
822 }
823
824 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
825 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
826
827 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
828}
829
Alan Cox11e29e22005-10-21 18:46:32 -0400830/*
831 * Compute the PIO modes available for this device. This is not as
832 * trivial as it seems if we must consider early devices correctly.
833 *
834 * FIXME: pre IDE drive timing (do we care ?).
835 */
836
Jeff Garzik057ace52005-10-22 14:27:05 -0400837static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -0400838{
839 u16 modes;
840
Alan Coxffa29452006-01-09 17:14:40 +0000841 /* Usual case. Word 53 indicates word 64 is valid */
842 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -0400843 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
844 modes <<= 3;
845 modes |= 0x7;
846 return modes;
847 }
848
Alan Coxffa29452006-01-09 17:14:40 +0000849 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
850 number for the maximum. Turn it into a mask and return it */
851 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -0400852 return modes;
Alan Coxffa29452006-01-09 17:14:40 +0000853 /* But wait.. there's more. Design your standards by committee and
854 you too can get a free iordy field to process. However its the
855 speeds not the modes that are supported... Note drivers using the
856 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -0400857}
858
Tejun Heo86e45b62006-03-05 15:29:09 +0900859/**
860 * ata_port_queue_task - Queue port_task
861 * @ap: The ata_port to queue port_task for
862 *
863 * Schedule @fn(@data) for execution after @delay jiffies using
864 * port_task. There is one port_task per port and it's the
865 * user(low level driver)'s responsibility to make sure that only
866 * one task is active at any given time.
867 *
868 * libata core layer takes care of synchronization between
869 * port_task and EH. ata_port_queue_task() may be ignored for EH
870 * synchronization.
871 *
872 * LOCKING:
873 * Inherited from caller.
874 */
875void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
876 unsigned long delay)
877{
878 int rc;
879
Tejun Heo2e755f62006-03-05 15:29:09 +0900880 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900881 return;
882
883 PREPARE_WORK(&ap->port_task, fn, data);
884
885 if (!delay)
886 rc = queue_work(ata_wq, &ap->port_task);
887 else
888 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
889
890 /* rc == 0 means that another user is using port task */
891 WARN_ON(rc == 0);
892}
893
894/**
895 * ata_port_flush_task - Flush port_task
896 * @ap: The ata_port to flush port_task for
897 *
898 * After this function completes, port_task is guranteed not to
899 * be running or scheduled.
900 *
901 * LOCKING:
902 * Kernel thread context (may sleep)
903 */
904void ata_port_flush_task(struct ata_port *ap)
905{
906 unsigned long flags;
907
908 DPRINTK("ENTER\n");
909
910 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900911 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900912 spin_unlock_irqrestore(&ap->host_set->lock, flags);
913
914 DPRINTK("flush #1\n");
915 flush_workqueue(ata_wq);
916
917 /*
918 * At this point, if a task is running, it's guaranteed to see
919 * the FLUSH flag; thus, it will never queue pio tasks again.
920 * Cancel and flush.
921 */
922 if (!cancel_delayed_work(&ap->port_task)) {
923 DPRINTK("flush #2\n");
924 flush_workqueue(ata_wq);
925 }
926
927 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900928 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900929 spin_unlock_irqrestore(&ap->host_set->lock, flags);
930
931 DPRINTK("EXIT\n");
932}
933
Tejun Heo77853bf2006-01-23 13:09:36 +0900934void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900935{
Tejun Heo77853bf2006-01-23 13:09:36 +0900936 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900937
Tejun Heo77853bf2006-01-23 13:09:36 +0900938 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900939 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900940}
941
942/**
943 * ata_exec_internal - execute libata internal command
944 * @ap: Port to which the command is sent
945 * @dev: Device to which the command is sent
946 * @tf: Taskfile registers for the command and the result
947 * @dma_dir: Data tranfer direction of the command
948 * @buf: Data buffer of the command
949 * @buflen: Length of data buffer
950 *
951 * Executes libata internal command with timeout. @tf contains
952 * command on entry and result on return. Timeout and error
953 * conditions are reported via return value. No recovery action
954 * is taken after a command times out. It's caller's duty to
955 * clean up after timeout.
956 *
957 * LOCKING:
958 * None. Should be called with kernel context, might sleep.
959 */
960
961static unsigned
962ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
963 struct ata_taskfile *tf,
964 int dma_dir, void *buf, unsigned int buflen)
965{
966 u8 command = tf->command;
967 struct ata_queued_cmd *qc;
968 DECLARE_COMPLETION(wait);
969 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900970 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900971
972 spin_lock_irqsave(&ap->host_set->lock, flags);
973
974 qc = ata_qc_new_init(ap, dev);
975 BUG_ON(qc == NULL);
976
977 qc->tf = *tf;
978 qc->dma_dir = dma_dir;
979 if (dma_dir != DMA_NONE) {
980 ata_sg_init_one(qc, buf, buflen);
981 qc->nsect = buflen / ATA_SECT_SIZE;
982 }
983
Tejun Heo77853bf2006-01-23 13:09:36 +0900984 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900985 qc->complete_fn = ata_qc_complete_internal;
986
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900987 qc->err_mask = ata_qc_issue(qc);
988 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900989 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900990
991 spin_unlock_irqrestore(&ap->host_set->lock, flags);
992
993 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
994 spin_lock_irqsave(&ap->host_set->lock, flags);
995
996 /* We're racing with irq here. If we lose, the
997 * following test prevents us from completing the qc
998 * again. If completion irq occurs after here but
999 * before the caller cleans up, it will result in a
1000 * spurious interrupt. We can live with that.
1001 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001002 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001003 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001004 ata_qc_complete(qc);
1005 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1006 ap->id, command);
1007 }
1008
1009 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1010 }
1011
Tejun Heo77853bf2006-01-23 13:09:36 +09001012 *tf = qc->tf;
1013 err_mask = qc->err_mask;
1014
1015 ata_qc_free(qc);
1016
1017 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001021 * ata_pio_need_iordy - check if iordy needed
1022 * @adev: ATA device
1023 *
1024 * Check if the current speed of the device requires IORDY. Used
1025 * by various controllers for chip configuration.
1026 */
1027
1028unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1029{
1030 int pio;
1031 int speed = adev->pio_mode - XFER_PIO_0;
1032
1033 if (speed < 2)
1034 return 0;
1035 if (speed > 2)
1036 return 1;
1037
1038 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1039
1040 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1041 pio = adev->id[ATA_ID_EIDE_PIO];
1042 /* Is the speed faster than the drive allows non IORDY ? */
1043 if (pio) {
1044 /* This is cycle times not frequency - watch the logic! */
1045 if (pio > 240) /* PIO2 is 240nS per cycle */
1046 return 1;
1047 return 0;
1048 }
1049 }
1050 return 0;
1051}
1052
1053/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001054 * ata_dev_read_id - Read ID data from the specified device
1055 * @ap: port on which target device resides
1056 * @dev: target device
1057 * @p_class: pointer to class of the target device (may be changed)
1058 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +09001059 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +09001060 *
1061 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1062 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1063 * devices. This function also takes care of EDD signature
1064 * misreporting (to be removed once EDD support is gone) and
1065 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
1066 *
1067 * LOCKING:
1068 * Kernel thread context (may sleep)
1069 *
1070 * RETURNS:
1071 * 0 on success, -errno otherwise.
1072 */
1073static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +09001074 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001075{
1076 unsigned int class = *p_class;
1077 unsigned int using_edd;
1078 struct ata_taskfile tf;
1079 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +09001080 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001081 const char *reason;
1082 int rc;
1083
1084 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1085
1086 if (ap->ops->probe_reset ||
1087 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1088 using_edd = 0;
1089 else
1090 using_edd = 1;
1091
1092 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1093
Tejun Heod9572b12006-03-01 16:09:35 +09001094 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1095 if (id == NULL) {
1096 rc = -ENOMEM;
1097 reason = "out of memory";
1098 goto err_out;
1099 }
1100
Tejun Heo49016ac2006-02-21 02:12:11 +09001101 retry:
1102 ata_tf_init(ap, &tf, dev->devno);
1103
1104 switch (class) {
1105 case ATA_DEV_ATA:
1106 tf.command = ATA_CMD_ID_ATA;
1107 break;
1108 case ATA_DEV_ATAPI:
1109 tf.command = ATA_CMD_ID_ATAPI;
1110 break;
1111 default:
1112 rc = -ENODEV;
1113 reason = "unsupported class";
1114 goto err_out;
1115 }
1116
1117 tf.protocol = ATA_PROT_PIO;
1118
1119 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1120 id, sizeof(id[0]) * ATA_ID_WORDS);
1121
1122 if (err_mask) {
1123 rc = -EIO;
1124 reason = "I/O error";
1125
1126 if (err_mask & ~AC_ERR_DEV)
1127 goto err_out;
1128
1129 /*
1130 * arg! EDD works for all test cases, but seems to return
1131 * the ATA signature for some ATAPI devices. Until the
1132 * reason for this is found and fixed, we fix up the mess
1133 * here. If IDENTIFY DEVICE returns command aborted
1134 * (as ATAPI devices do), then we issue an
1135 * IDENTIFY PACKET DEVICE.
1136 *
1137 * ATA software reset (SRST, the default) does not appear
1138 * to have this problem.
1139 */
1140 if ((using_edd) && (class == ATA_DEV_ATA)) {
1141 u8 err = tf.feature;
1142 if (err & ATA_ABORTED) {
1143 class = ATA_DEV_ATAPI;
1144 goto retry;
1145 }
1146 }
1147 goto err_out;
1148 }
1149
1150 swap_buf_le16(id, ATA_ID_WORDS);
1151
1152 /* print device capabilities */
1153 printk(KERN_DEBUG "ata%u: dev %u cfg "
1154 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1155 ap->id, dev->devno,
1156 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1157
1158 /* sanity check */
1159 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1160 rc = -EINVAL;
1161 reason = "device reports illegal type";
1162 goto err_out;
1163 }
1164
1165 if (post_reset && class == ATA_DEV_ATA) {
1166 /*
1167 * The exact sequence expected by certain pre-ATA4 drives is:
1168 * SRST RESET
1169 * IDENTIFY
1170 * INITIALIZE DEVICE PARAMETERS
1171 * anything else..
1172 * Some drives were very specific about that exact sequence.
1173 */
1174 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1175 err_mask = ata_dev_init_params(ap, dev);
1176 if (err_mask) {
1177 rc = -EIO;
1178 reason = "INIT_DEV_PARAMS failed";
1179 goto err_out;
1180 }
1181
1182 /* current CHS translation info (id[53-58]) might be
1183 * changed. reread the identify device info.
1184 */
1185 post_reset = 0;
1186 goto retry;
1187 }
1188 }
1189
1190 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001191 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001192 return 0;
1193
1194 err_out:
1195 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1196 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001197 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001198 return rc;
1199}
1200
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001201static inline u8 ata_dev_knobble(const struct ata_port *ap,
1202 struct ata_device *dev)
1203{
1204 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1205}
1206
Tejun Heo49016ac2006-02-21 02:12:11 +09001207/**
Tejun Heoffeae412006-03-01 16:09:35 +09001208 * ata_dev_configure - Configure the specified ATA/ATAPI device
1209 * @ap: Port on which target device resides
1210 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001211 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 *
Tejun Heoffeae412006-03-01 16:09:35 +09001213 * Configure @dev according to @dev->id. Generic and low-level
1214 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 *
1216 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001217 * Kernel thread context (may sleep)
1218 *
1219 * RETURNS:
1220 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001222static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1223 int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Tejun Heoff8854b2006-03-06 04:31:56 +09001225 unsigned int xfer_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001226 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (!ata_dev_present(dev)) {
1229 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001230 ap->id, dev->devno);
1231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
Tejun Heoffeae412006-03-01 16:09:35 +09001234 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Tejun Heo208a9932006-03-05 17:55:58 +09001236 /* initialize to-be-configured parameters */
1237 dev->flags = 0;
1238 dev->max_sectors = 0;
1239 dev->cdb_len = 0;
1240 dev->n_sectors = 0;
1241 dev->cylinders = 0;
1242 dev->heads = 0;
1243 dev->sectors = 0;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /*
1246 * common ATA, ATAPI feature tests
1247 */
1248
Albert Lee8bf62ece2005-05-12 15:29:42 -04001249 /* we require DMA support (bits 8 of word 49) */
1250 if (!ata_id_has_dma(dev->id)) {
1251 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001252 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 goto err_out_nosup;
1254 }
1255
Tejun Heoff8854b2006-03-06 04:31:56 +09001256 /* find max transfer mode; for printk only */
1257 xfer_mask = ata_id_xfermask(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Tejun Heo0bd33002006-02-12 22:47:05 +09001259 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 /* ATA-specific feature tests */
1262 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001263 dev->n_sectors = ata_id_n_sectors(dev->id);
1264
Albert Lee8bf62ece2005-05-12 15:29:42 -04001265 if (ata_id_has_lba(dev->id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001266 const char *lba_desc;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001267
Tejun Heo4c2d7212006-03-05 17:55:58 +09001268 lba_desc = "LBA";
1269 dev->flags |= ATA_DFLAG_LBA;
1270 if (ata_id_has_lba48(dev->id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001271 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001272 lba_desc = "LBA48";
1273 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04001274
1275 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001276 if (print_info)
1277 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1278 "max %s, %Lu sectors: %s\n",
1279 ap->id, dev->devno,
1280 ata_id_major_version(dev->id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001281 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001282 (unsigned long long)dev->n_sectors,
1283 lba_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001284 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001285 /* CHS */
1286
1287 /* Default translation */
1288 dev->cylinders = dev->id[1];
1289 dev->heads = dev->id[3];
1290 dev->sectors = dev->id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001291
1292 if (ata_id_current_chs_valid(dev->id)) {
1293 /* Current CHS translation is valid. */
1294 dev->cylinders = dev->id[54];
1295 dev->heads = dev->id[55];
1296 dev->sectors = dev->id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001297 }
1298
1299 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001300 if (print_info)
1301 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1302 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1303 ap->id, dev->devno,
1304 ata_id_major_version(dev->id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001305 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001306 (unsigned long long)dev->n_sectors,
1307 dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
Tejun Heo6e7846e2006-02-12 23:32:58 +09001310 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
1313 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001314 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 rc = atapi_cdb_len(dev->id);
1316 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1317 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001318 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 goto err_out_nosup;
1320 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001321 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001324 if (print_info)
1325 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoff8854b2006-03-06 04:31:56 +09001326 ap->id, dev->devno, ata_mode_string(xfer_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
Tejun Heo6e7846e2006-02-12 23:32:58 +09001329 ap->host->max_cmd_len = 0;
1330 for (i = 0; i < ATA_MAX_DEVICES; i++)
1331 ap->host->max_cmd_len = max_t(unsigned int,
1332 ap->host->max_cmd_len,
1333 ap->device[i].cdb_len);
1334
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001335 /* limit bridge transfers to udma5, 200 sectors */
1336 if (ata_dev_knobble(ap, dev)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001337 if (print_info)
1338 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1339 ap->id, dev->devno);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001340 ap->udma_mask &= ATA_UDMA5;
1341 dev->max_sectors = ATA_MAX_SECTORS;
1342 }
1343
1344 if (ap->ops->dev_config)
1345 ap->ops->dev_config(ap, dev);
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350err_out_nosup:
1351 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001352 ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001354 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357/**
1358 * ata_bus_probe - Reset and probe ATA bus
1359 * @ap: Bus to probe
1360 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001361 * Master ATA bus probing function. Initiates a hardware-dependent
1362 * bus reset, then attempts to identify any devices found on
1363 * the bus.
1364 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001366 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 *
1368 * RETURNS:
1369 * Zero on success, non-zero on error.
1370 */
1371
1372static int ata_bus_probe(struct ata_port *ap)
1373{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001374 unsigned int classes[ATA_MAX_DEVICES];
1375 unsigned int i, rc, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Tejun Heo28ca5c52006-03-01 16:09:36 +09001377 ata_port_probe(ap);
1378
1379 /* reset */
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001380 if (ap->ops->probe_reset) {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001381 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001382 if (rc) {
1383 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1384 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001385 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001386
1387 for (i = 0; i < ATA_MAX_DEVICES; i++)
1388 if (classes[i] == ATA_DEV_UNKNOWN)
1389 classes[i] = ATA_DEV_NONE;
1390 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001391 ap->ops->phy_reset(ap);
1392
Tejun Heo28ca5c52006-03-01 16:09:36 +09001393 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1394 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1395 classes[i] = ap->device[i].class;
1396 else
1397 ap->device[i].class = ATA_DEV_UNKNOWN;
1398 }
1399 ata_port_probe(ap);
1400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Tejun Heo28ca5c52006-03-01 16:09:36 +09001402 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoffeae412006-03-01 16:09:35 +09001404 struct ata_device *dev = &ap->device[i];
1405
Tejun Heo28ca5c52006-03-01 16:09:36 +09001406 dev->class = classes[i];
1407
Tejun Heoffeae412006-03-01 16:09:35 +09001408 if (!ata_dev_present(dev))
1409 continue;
1410
1411 WARN_ON(dev->id != NULL);
1412 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1413 dev->class = ATA_DEV_NONE;
1414 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
Tejun Heoffeae412006-03-01 16:09:35 +09001416
Tejun Heo4c2d7212006-03-05 17:55:58 +09001417 if (ata_dev_configure(ap, dev, 1)) {
Tejun Heoffeae412006-03-01 16:09:35 +09001418 dev->class++; /* disable device */
1419 continue;
1420 }
1421
Tejun Heoffeae412006-03-01 16:09:35 +09001422 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
Tejun Heo28ca5c52006-03-01 16:09:36 +09001425 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 goto err_out_disable;
1427
1428 ata_set_mode(ap);
1429 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1430 goto err_out_disable;
1431
1432 return 0;
1433
1434err_out_disable:
1435 ap->ops->port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return -1;
1437}
1438
1439/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001440 * ata_port_probe - Mark port as enabled
1441 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001443 * Modify @ap data structure such that the system
1444 * thinks that the entire port is enabled.
1445 *
1446 * LOCKING: host_set lock, or some other form of
1447 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
1449
1450void ata_port_probe(struct ata_port *ap)
1451{
1452 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1453}
1454
1455/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001456 * sata_print_link_status - Print SATA link status
1457 * @ap: SATA port to printk link status about
1458 *
1459 * This function prints link speed and status of a SATA link.
1460 *
1461 * LOCKING:
1462 * None.
1463 */
1464static void sata_print_link_status(struct ata_port *ap)
1465{
1466 u32 sstatus, tmp;
1467 const char *speed;
1468
1469 if (!ap->ops->scr_read)
1470 return;
1471
1472 sstatus = scr_read(ap, SCR_STATUS);
1473
1474 if (sata_dev_present(ap)) {
1475 tmp = (sstatus >> 4) & 0xf;
1476 if (tmp & (1 << 0))
1477 speed = "1.5";
1478 else if (tmp & (1 << 1))
1479 speed = "3.0";
1480 else
1481 speed = "<unknown>";
1482 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1483 ap->id, speed, sstatus);
1484 } else {
1485 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1486 ap->id, sstatus);
1487 }
1488}
1489
1490/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001491 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1492 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001494 * This function issues commands to standard SATA Sxxx
1495 * PHY registers, to wake up the phy (and device), and
1496 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 *
1498 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001499 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 *
1501 */
1502void __sata_phy_reset(struct ata_port *ap)
1503{
1504 u32 sstatus;
1505 unsigned long timeout = jiffies + (HZ * 5);
1506
1507 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001508 /* issue phy wake/reset */
1509 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001510 /* Couldn't find anything in SATA I/II specs, but
1511 * AHCI-1.1 10.4.2 says at least 1 ms. */
1512 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001514 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 /* wait for phy to become ready, if necessary */
1517 do {
1518 msleep(200);
1519 sstatus = scr_read(ap, SCR_STATUS);
1520 if ((sstatus & 0xf) != 1)
1521 break;
1522 } while (time_before(jiffies, timeout));
1523
Tejun Heo3be680b2005-12-19 22:35:02 +09001524 /* print link status */
1525 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001526
Tejun Heo3be680b2005-12-19 22:35:02 +09001527 /* TODO: phy layer with polling, timeouts, etc. */
1528 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001530 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1534 return;
1535
1536 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1537 ata_port_disable(ap);
1538 return;
1539 }
1540
1541 ap->cbl = ATA_CBL_SATA;
1542}
1543
1544/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001545 * sata_phy_reset - Reset SATA bus.
1546 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001548 * This function resets the SATA bus, and then probes
1549 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 *
1551 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001552 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 *
1554 */
1555void sata_phy_reset(struct ata_port *ap)
1556{
1557 __sata_phy_reset(ap);
1558 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1559 return;
1560 ata_bus_reset(ap);
1561}
1562
1563/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001564 * ata_port_disable - Disable port.
1565 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001567 * Modify @ap data structure such that the system
1568 * thinks that the entire port is disabled, and should
1569 * never attempt to probe or communicate with devices
1570 * on this port.
1571 *
1572 * LOCKING: host_set lock, or some other form of
1573 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 */
1575
1576void ata_port_disable(struct ata_port *ap)
1577{
1578 ap->device[0].class = ATA_DEV_NONE;
1579 ap->device[1].class = ATA_DEV_NONE;
1580 ap->flags |= ATA_FLAG_PORT_DISABLED;
1581}
1582
Alan Cox452503f2005-10-21 19:01:32 -04001583/*
1584 * This mode timing computation functionality is ported over from
1585 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1586 */
1587/*
1588 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1589 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1590 * for PIO 5, which is a nonstandard extension and UDMA6, which
1591 * is currently supported only by Maxtor drives.
1592 */
1593
1594static const struct ata_timing ata_timing[] = {
1595
1596 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1597 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1598 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1599 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1600
1601 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1602 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1603 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1604
1605/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1606
1607 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1608 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1609 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1610
1611 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1612 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1613 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1614
1615/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1616 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1617 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1618
1619 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1620 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1621 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1622
1623/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1624
1625 { 0xFF }
1626};
1627
1628#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1629#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1630
1631static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1632{
1633 q->setup = EZ(t->setup * 1000, T);
1634 q->act8b = EZ(t->act8b * 1000, T);
1635 q->rec8b = EZ(t->rec8b * 1000, T);
1636 q->cyc8b = EZ(t->cyc8b * 1000, T);
1637 q->active = EZ(t->active * 1000, T);
1638 q->recover = EZ(t->recover * 1000, T);
1639 q->cycle = EZ(t->cycle * 1000, T);
1640 q->udma = EZ(t->udma * 1000, UT);
1641}
1642
1643void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1644 struct ata_timing *m, unsigned int what)
1645{
1646 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1647 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1648 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1649 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1650 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1651 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1652 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1653 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1654}
1655
1656static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1657{
1658 const struct ata_timing *t;
1659
1660 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001661 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001662 return NULL;
1663 return t;
1664}
1665
1666int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1667 struct ata_timing *t, int T, int UT)
1668{
1669 const struct ata_timing *s;
1670 struct ata_timing p;
1671
1672 /*
1673 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001674 */
Alan Cox452503f2005-10-21 19:01:32 -04001675
1676 if (!(s = ata_timing_find_mode(speed)))
1677 return -EINVAL;
1678
Albert Lee75b1f2f2005-11-16 17:06:18 +08001679 memcpy(t, s, sizeof(*s));
1680
Alan Cox452503f2005-10-21 19:01:32 -04001681 /*
1682 * If the drive is an EIDE drive, it can tell us it needs extended
1683 * PIO/MW_DMA cycle timing.
1684 */
1685
1686 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1687 memset(&p, 0, sizeof(p));
1688 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1689 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1690 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1691 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1692 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1693 }
1694 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1695 }
1696
1697 /*
1698 * Convert the timing to bus clock counts.
1699 */
1700
Albert Lee75b1f2f2005-11-16 17:06:18 +08001701 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001702
1703 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001704 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1705 * S.M.A.R.T * and some other commands. We have to ensure that the
1706 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001707 */
1708
1709 if (speed > XFER_PIO_4) {
1710 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1711 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1712 }
1713
1714 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001715 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001716 */
1717
1718 if (t->act8b + t->rec8b < t->cyc8b) {
1719 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1720 t->rec8b = t->cyc8b - t->act8b;
1721 }
1722
1723 if (t->active + t->recover < t->cycle) {
1724 t->active += (t->cycle - (t->active + t->recover)) / 2;
1725 t->recover = t->cycle - t->active;
1726 }
1727
1728 return 0;
1729}
1730
Jeff Garzik057ace52005-10-22 14:27:05 -04001731static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 unsigned int shift;
1733 u8 base;
1734} xfer_mode_classes[] = {
1735 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1736 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1737 { ATA_SHIFT_PIO, XFER_PIO_0 },
1738};
1739
Arjan van de Ven858119e2006-01-14 13:20:43 -08001740static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
1742 int i;
1743
1744 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1745 if (xfer_mode_classes[i].shift == shift)
1746 return xfer_mode_classes[i].base;
1747
1748 return 0xff;
1749}
1750
1751static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1754 return;
1755
1756 if (dev->xfer_shift == ATA_SHIFT_PIO)
1757 dev->flags |= ATA_DFLAG_PIO;
1758
1759 ata_dev_set_xfermode(ap, dev);
1760
Tejun Heo48a8a142006-03-05 17:55:58 +09001761 if (ata_dev_revalidate(ap, dev, 0)) {
1762 printk(KERN_ERR "ata%u: failed to revalidate after set "
1763 "xfermode, disabled\n", ap->id);
1764 ata_port_disable(ap);
1765 }
1766
Tejun Heo23e71c32006-03-06 04:31:57 +09001767 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1768 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
Tejun Heo23e71c32006-03-06 04:31:57 +09001771 ap->id, dev->devno,
1772 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
1775static int ata_host_set_pio(struct ata_port *ap)
1776{
Tejun Heoa6d5a512006-03-06 04:31:57 +09001777 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1780 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001781
1782 if (!ata_dev_present(dev))
1783 continue;
1784
1785 if (!dev->pio_mode) {
1786 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1787 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09001789
1790 dev->xfer_mode = dev->pio_mode;
1791 dev->xfer_shift = ATA_SHIFT_PIO;
1792 if (ap->ops->set_piomode)
1793 ap->ops->set_piomode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795
1796 return 0;
1797}
1798
Tejun Heoa6d5a512006-03-06 04:31:57 +09001799static void ata_host_set_dma(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 int i;
1802
1803 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1804 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001805
1806 if (!ata_dev_present(dev) || !dev->dma_mode)
1807 continue;
1808
1809 dev->xfer_mode = dev->dma_mode;
1810 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1811 if (ap->ops->set_dmamode)
1812 ap->ops->set_dmamode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814}
1815
1816/**
1817 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1818 * @ap: port on which timings will be programmed
1819 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001820 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1821 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001823 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
1825static void ata_set_mode(struct ata_port *ap)
1826{
Tejun Heoa6d5a512006-03-06 04:31:57 +09001827 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Tejun Heoa6d5a512006-03-06 04:31:57 +09001829 /* step 1: calculate xfer_mask */
1830 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1831 struct ata_device *dev = &ap->device[i];
1832 unsigned int xfer_mask;
1833
1834 if (!ata_dev_present(dev))
1835 continue;
1836
1837 xfer_mask = ata_dev_xfermask(ap, dev);
1838
1839 dev->pio_mode = ata_xfer_mask2mode(xfer_mask & ATA_MASK_PIO);
1840 dev->dma_mode = ata_xfer_mask2mode(xfer_mask & (ATA_MASK_MWDMA |
1841 ATA_MASK_UDMA));
1842 }
1843
1844 /* step 2: always set host PIO timings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 rc = ata_host_set_pio(ap);
1846 if (rc)
1847 goto err_out;
1848
Tejun Heoa6d5a512006-03-06 04:31:57 +09001849 /* step 3: set host DMA timings */
1850 ata_host_set_dma(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /* step 4: update devices' xfer mode */
Tejun Heoa6d5a512006-03-06 04:31:57 +09001853 for (i = 0; i < ATA_MAX_DEVICES; i++)
1854 ata_dev_set_mode(ap, &ap->device[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1857 return;
1858
1859 if (ap->ops->post_set_mode)
1860 ap->ops->post_set_mode(ap);
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return;
1863
1864err_out:
1865 ata_port_disable(ap);
1866}
1867
1868/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001869 * ata_tf_to_host - issue ATA taskfile to host controller
1870 * @ap: port to which command is being issued
1871 * @tf: ATA taskfile register set
1872 *
1873 * Issues ATA taskfile register set to ATA host controller,
1874 * with proper synchronization with interrupt handler and
1875 * other threads.
1876 *
1877 * LOCKING:
1878 * spin_lock_irqsave(host_set lock)
1879 */
1880
1881static inline void ata_tf_to_host(struct ata_port *ap,
1882 const struct ata_taskfile *tf)
1883{
1884 ap->ops->tf_load(ap, tf);
1885 ap->ops->exec_command(ap, tf);
1886}
1887
1888/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 * ata_busy_sleep - sleep until BSY clears, or timeout
1890 * @ap: port containing status register to be polled
1891 * @tmout_pat: impatience timeout
1892 * @tmout: overall timeout
1893 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001894 * Sleep until ATA Status register bit BSY clears,
1895 * or a timeout occurs.
1896 *
1897 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 */
1899
Tejun Heo6f8b9952006-01-24 17:05:21 +09001900unsigned int ata_busy_sleep (struct ata_port *ap,
1901 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 unsigned long timer_start, timeout;
1904 u8 status;
1905
1906 status = ata_busy_wait(ap, ATA_BUSY, 300);
1907 timer_start = jiffies;
1908 timeout = timer_start + tmout_pat;
1909 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1910 msleep(50);
1911 status = ata_busy_wait(ap, ATA_BUSY, 3);
1912 }
1913
1914 if (status & ATA_BUSY)
1915 printk(KERN_WARNING "ata%u is slow to respond, "
1916 "please be patient\n", ap->id);
1917
1918 timeout = timer_start + tmout;
1919 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1920 msleep(50);
1921 status = ata_chk_status(ap);
1922 }
1923
1924 if (status & ATA_BUSY) {
1925 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1926 ap->id, tmout / HZ);
1927 return 1;
1928 }
1929
1930 return 0;
1931}
1932
1933static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1934{
1935 struct ata_ioports *ioaddr = &ap->ioaddr;
1936 unsigned int dev0 = devmask & (1 << 0);
1937 unsigned int dev1 = devmask & (1 << 1);
1938 unsigned long timeout;
1939
1940 /* if device 0 was found in ata_devchk, wait for its
1941 * BSY bit to clear
1942 */
1943 if (dev0)
1944 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1945
1946 /* if device 1 was found in ata_devchk, wait for
1947 * register access, then wait for BSY to clear
1948 */
1949 timeout = jiffies + ATA_TMOUT_BOOT;
1950 while (dev1) {
1951 u8 nsect, lbal;
1952
1953 ap->ops->dev_select(ap, 1);
1954 if (ap->flags & ATA_FLAG_MMIO) {
1955 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1956 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1957 } else {
1958 nsect = inb(ioaddr->nsect_addr);
1959 lbal = inb(ioaddr->lbal_addr);
1960 }
1961 if ((nsect == 1) && (lbal == 1))
1962 break;
1963 if (time_after(jiffies, timeout)) {
1964 dev1 = 0;
1965 break;
1966 }
1967 msleep(50); /* give drive a breather */
1968 }
1969 if (dev1)
1970 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1971
1972 /* is all this really necessary? */
1973 ap->ops->dev_select(ap, 0);
1974 if (dev1)
1975 ap->ops->dev_select(ap, 1);
1976 if (dev0)
1977 ap->ops->dev_select(ap, 0);
1978}
1979
1980/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001981 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1982 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001984 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1985 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 *
1987 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001988 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001989 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 *
1991 */
1992
1993static unsigned int ata_bus_edd(struct ata_port *ap)
1994{
1995 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001996 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* set up execute-device-diag (bus reset) taskfile */
1999 /* also, take interrupts to a known state (disabled) */
2000 DPRINTK("execute-device-diag\n");
2001 ata_tf_init(ap, &tf, 0);
2002 tf.ctl |= ATA_NIEN;
2003 tf.command = ATA_CMD_EDD;
2004 tf.protocol = ATA_PROT_NODATA;
2005
2006 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002007 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002009 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 /* spec says at least 2ms. but who knows with those
2012 * crazy ATAPI devices...
2013 */
2014 msleep(150);
2015
2016 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2017}
2018
2019static unsigned int ata_bus_softreset(struct ata_port *ap,
2020 unsigned int devmask)
2021{
2022 struct ata_ioports *ioaddr = &ap->ioaddr;
2023
2024 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2025
2026 /* software reset. causes dev0 to be selected */
2027 if (ap->flags & ATA_FLAG_MMIO) {
2028 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2029 udelay(20); /* FIXME: flush */
2030 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2031 udelay(20); /* FIXME: flush */
2032 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2033 } else {
2034 outb(ap->ctl, ioaddr->ctl_addr);
2035 udelay(10);
2036 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2037 udelay(10);
2038 outb(ap->ctl, ioaddr->ctl_addr);
2039 }
2040
2041 /* spec mandates ">= 2ms" before checking status.
2042 * We wait 150ms, because that was the magic delay used for
2043 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2044 * between when the ATA command register is written, and then
2045 * status is checked. Because waiting for "a while" before
2046 * checking status is fine, post SRST, we perform this magic
2047 * delay here as well.
2048 */
2049 msleep(150);
2050
2051 ata_bus_post_reset(ap, devmask);
2052
2053 return 0;
2054}
2055
2056/**
2057 * ata_bus_reset - reset host port and associated ATA channel
2058 * @ap: port to reset
2059 *
2060 * This is typically the first time we actually start issuing
2061 * commands to the ATA channel. We wait for BSY to clear, then
2062 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2063 * result. Determine what devices, if any, are on the channel
2064 * by looking at the device 0/1 error register. Look at the signature
2065 * stored in each device's taskfile registers, to determine if
2066 * the device is ATA or ATAPI.
2067 *
2068 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002069 * PCI/etc. bus probe sem.
2070 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 *
2072 * SIDE EFFECTS:
2073 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2074 */
2075
2076void ata_bus_reset(struct ata_port *ap)
2077{
2078 struct ata_ioports *ioaddr = &ap->ioaddr;
2079 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2080 u8 err;
2081 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2082
2083 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2084
2085 /* determine if device 0/1 are present */
2086 if (ap->flags & ATA_FLAG_SATA_RESET)
2087 dev0 = 1;
2088 else {
2089 dev0 = ata_devchk(ap, 0);
2090 if (slave_possible)
2091 dev1 = ata_devchk(ap, 1);
2092 }
2093
2094 if (dev0)
2095 devmask |= (1 << 0);
2096 if (dev1)
2097 devmask |= (1 << 1);
2098
2099 /* select device 0 again */
2100 ap->ops->dev_select(ap, 0);
2101
2102 /* issue bus reset */
2103 if (ap->flags & ATA_FLAG_SRST)
2104 rc = ata_bus_softreset(ap, devmask);
2105 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2106 /* set up device control */
2107 if (ap->flags & ATA_FLAG_MMIO)
2108 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2109 else
2110 outb(ap->ctl, ioaddr->ctl_addr);
2111 rc = ata_bus_edd(ap);
2112 }
2113
2114 if (rc)
2115 goto err_out;
2116
2117 /*
2118 * determine by signature whether we have ATA or ATAPI devices
2119 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002120 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002122 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 /* re-enable interrupts */
2125 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2126 ata_irq_on(ap);
2127
2128 /* is double-select really necessary? */
2129 if (ap->device[1].class != ATA_DEV_NONE)
2130 ap->ops->dev_select(ap, 1);
2131 if (ap->device[0].class != ATA_DEV_NONE)
2132 ap->ops->dev_select(ap, 0);
2133
2134 /* if no devices were detected, disable this port */
2135 if ((ap->device[0].class == ATA_DEV_NONE) &&
2136 (ap->device[1].class == ATA_DEV_NONE))
2137 goto err_out;
2138
2139 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2140 /* set up device control for ATA_FLAG_SATA_RESET */
2141 if (ap->flags & ATA_FLAG_MMIO)
2142 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2143 else
2144 outb(ap->ctl, ioaddr->ctl_addr);
2145 }
2146
2147 DPRINTK("EXIT\n");
2148 return;
2149
2150err_out:
2151 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2152 ap->ops->port_disable(ap);
2153
2154 DPRINTK("EXIT\n");
2155}
2156
Tejun Heo7a7921e2006-02-02 18:20:00 +09002157static int sata_phy_resume(struct ata_port *ap)
2158{
2159 unsigned long timeout = jiffies + (HZ * 5);
2160 u32 sstatus;
2161
2162 scr_write_flush(ap, SCR_CONTROL, 0x300);
2163
2164 /* Wait for phy to become ready, if necessary. */
2165 do {
2166 msleep(200);
2167 sstatus = scr_read(ap, SCR_STATUS);
2168 if ((sstatus & 0xf) != 1)
2169 return 0;
2170 } while (time_before(jiffies, timeout));
2171
2172 return -1;
2173}
2174
Tejun Heoc2bd5802006-01-24 17:05:22 +09002175/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002176 * ata_std_probeinit - initialize probing
2177 * @ap: port to be probed
2178 *
2179 * @ap is about to be probed. Initialize it. This function is
2180 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002181 *
2182 * NOTE!!! Do not use this function as probeinit if a low level
2183 * driver implements only hardreset. Just pass NULL as probeinit
2184 * in that case. Using this function is probably okay but doing
2185 * so makes reset sequence different from the original
2186 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002187 */
2188extern void ata_std_probeinit(struct ata_port *ap)
2189{
Tejun Heo3a397462006-02-10 23:58:48 +09002190 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002191 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002192 if (sata_dev_present(ap))
2193 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2194 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002195}
2196
2197/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002198 * ata_std_softreset - reset host port via ATA SRST
2199 * @ap: port to reset
2200 * @verbose: fail verbosely
2201 * @classes: resulting classes of attached devices
2202 *
2203 * Reset host port using ATA SRST. This function is to be used
2204 * as standard callback for ata_drive_*_reset() functions.
2205 *
2206 * LOCKING:
2207 * Kernel thread context (may sleep)
2208 *
2209 * RETURNS:
2210 * 0 on success, -errno otherwise.
2211 */
2212int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2213{
2214 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2215 unsigned int devmask = 0, err_mask;
2216 u8 err;
2217
2218 DPRINTK("ENTER\n");
2219
Tejun Heo3a397462006-02-10 23:58:48 +09002220 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2221 classes[0] = ATA_DEV_NONE;
2222 goto out;
2223 }
2224
Tejun Heoc2bd5802006-01-24 17:05:22 +09002225 /* determine if device 0/1 are present */
2226 if (ata_devchk(ap, 0))
2227 devmask |= (1 << 0);
2228 if (slave_possible && ata_devchk(ap, 1))
2229 devmask |= (1 << 1);
2230
Tejun Heoc2bd5802006-01-24 17:05:22 +09002231 /* select device 0 again */
2232 ap->ops->dev_select(ap, 0);
2233
2234 /* issue bus reset */
2235 DPRINTK("about to softreset, devmask=%x\n", devmask);
2236 err_mask = ata_bus_softreset(ap, devmask);
2237 if (err_mask) {
2238 if (verbose)
2239 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2240 ap->id, err_mask);
2241 else
2242 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2243 err_mask);
2244 return -EIO;
2245 }
2246
2247 /* determine by signature whether we have ATA or ATAPI devices */
2248 classes[0] = ata_dev_try_classify(ap, 0, &err);
2249 if (slave_possible && err != 0x81)
2250 classes[1] = ata_dev_try_classify(ap, 1, &err);
2251
Tejun Heo3a397462006-02-10 23:58:48 +09002252 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002253 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2254 return 0;
2255}
2256
2257/**
2258 * sata_std_hardreset - reset host port via SATA phy reset
2259 * @ap: port to reset
2260 * @verbose: fail verbosely
2261 * @class: resulting class of attached device
2262 *
2263 * SATA phy-reset host port using DET bits of SControl register.
2264 * This function is to be used as standard callback for
2265 * ata_drive_*_reset().
2266 *
2267 * LOCKING:
2268 * Kernel thread context (may sleep)
2269 *
2270 * RETURNS:
2271 * 0 on success, -errno otherwise.
2272 */
2273int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2274{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002275 DPRINTK("ENTER\n");
2276
2277 /* Issue phy wake/reset */
2278 scr_write_flush(ap, SCR_CONTROL, 0x301);
2279
2280 /*
2281 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2282 * 10.4.2 says at least 1 ms.
2283 */
2284 msleep(1);
2285
Tejun Heo7a7921e2006-02-02 18:20:00 +09002286 /* Bring phy back */
2287 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002288
Tejun Heoc2bd5802006-01-24 17:05:22 +09002289 /* TODO: phy layer with polling, timeouts, etc. */
2290 if (!sata_dev_present(ap)) {
2291 *class = ATA_DEV_NONE;
2292 DPRINTK("EXIT, link offline\n");
2293 return 0;
2294 }
2295
2296 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2297 if (verbose)
2298 printk(KERN_ERR "ata%u: COMRESET failed "
2299 "(device not ready)\n", ap->id);
2300 else
2301 DPRINTK("EXIT, device not ready\n");
2302 return -EIO;
2303 }
2304
Tejun Heo3a397462006-02-10 23:58:48 +09002305 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2306
Tejun Heoc2bd5802006-01-24 17:05:22 +09002307 *class = ata_dev_try_classify(ap, 0, NULL);
2308
2309 DPRINTK("EXIT, class=%u\n", *class);
2310 return 0;
2311}
2312
2313/**
2314 * ata_std_postreset - standard postreset callback
2315 * @ap: the target ata_port
2316 * @classes: classes of attached devices
2317 *
2318 * This function is invoked after a successful reset. Note that
2319 * the device might have been reset more than once using
2320 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002321 *
2322 * This function is to be used as standard callback for
2323 * ata_drive_*_reset().
2324 *
2325 * LOCKING:
2326 * Kernel thread context (may sleep)
2327 */
2328void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2329{
2330 DPRINTK("ENTER\n");
2331
Tejun Heo56497bd2006-02-15 15:01:42 +09002332 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002333 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2334 ap->cbl = ATA_CBL_SATA;
2335
2336 /* print link status */
2337 if (ap->cbl == ATA_CBL_SATA)
2338 sata_print_link_status(ap);
2339
Tejun Heo3a397462006-02-10 23:58:48 +09002340 /* re-enable interrupts */
2341 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2342 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002343
2344 /* is double-select really necessary? */
2345 if (classes[0] != ATA_DEV_NONE)
2346 ap->ops->dev_select(ap, 1);
2347 if (classes[1] != ATA_DEV_NONE)
2348 ap->ops->dev_select(ap, 0);
2349
Tejun Heo3a397462006-02-10 23:58:48 +09002350 /* bail out if no device is present */
2351 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2352 DPRINTK("EXIT, no device\n");
2353 return;
2354 }
2355
2356 /* set up device control */
2357 if (ap->ioaddr.ctl_addr) {
2358 if (ap->flags & ATA_FLAG_MMIO)
2359 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2360 else
2361 outb(ap->ctl, ap->ioaddr.ctl_addr);
2362 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002363
2364 DPRINTK("EXIT\n");
2365}
2366
2367/**
2368 * ata_std_probe_reset - standard probe reset method
2369 * @ap: prot to perform probe-reset
2370 * @classes: resulting classes of attached devices
2371 *
2372 * The stock off-the-shelf ->probe_reset method.
2373 *
2374 * LOCKING:
2375 * Kernel thread context (may sleep)
2376 *
2377 * RETURNS:
2378 * 0 on success, -errno otherwise.
2379 */
2380int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2381{
2382 ata_reset_fn_t hardreset;
2383
2384 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002385 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002386 hardreset = sata_std_hardreset;
2387
Tejun Heo8a19ac82006-02-02 18:20:00 +09002388 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002389 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002390 ata_std_postreset, classes);
2391}
2392
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002393static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2394 ata_postreset_fn_t postreset,
2395 unsigned int *classes)
2396{
2397 int i, rc;
2398
2399 for (i = 0; i < ATA_MAX_DEVICES; i++)
2400 classes[i] = ATA_DEV_UNKNOWN;
2401
2402 rc = reset(ap, 0, classes);
2403 if (rc)
2404 return rc;
2405
2406 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2407 * is complete and convert all ATA_DEV_UNKNOWN to
2408 * ATA_DEV_NONE.
2409 */
2410 for (i = 0; i < ATA_MAX_DEVICES; i++)
2411 if (classes[i] != ATA_DEV_UNKNOWN)
2412 break;
2413
2414 if (i < ATA_MAX_DEVICES)
2415 for (i = 0; i < ATA_MAX_DEVICES; i++)
2416 if (classes[i] == ATA_DEV_UNKNOWN)
2417 classes[i] = ATA_DEV_NONE;
2418
2419 if (postreset)
2420 postreset(ap, classes);
2421
2422 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2423}
2424
2425/**
2426 * ata_drive_probe_reset - Perform probe reset with given methods
2427 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002428 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002429 * @softreset: softreset method (can be NULL)
2430 * @hardreset: hardreset method (can be NULL)
2431 * @postreset: postreset method (can be NULL)
2432 * @classes: resulting classes of attached devices
2433 *
2434 * Reset the specified port and classify attached devices using
2435 * given methods. This function prefers softreset but tries all
2436 * possible reset sequences to reset and classify devices. This
2437 * function is intended to be used for constructing ->probe_reset
2438 * callback by low level drivers.
2439 *
2440 * Reset methods should follow the following rules.
2441 *
2442 * - Return 0 on sucess, -errno on failure.
2443 * - If classification is supported, fill classes[] with
2444 * recognized class codes.
2445 * - If classification is not supported, leave classes[] alone.
2446 * - If verbose is non-zero, print error message on failure;
2447 * otherwise, shut up.
2448 *
2449 * LOCKING:
2450 * Kernel thread context (may sleep)
2451 *
2452 * RETURNS:
2453 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2454 * if classification fails, and any error code from reset
2455 * methods.
2456 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002457int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002458 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2459 ata_postreset_fn_t postreset, unsigned int *classes)
2460{
2461 int rc = -EINVAL;
2462
Tejun Heo7944ea92006-02-02 18:20:00 +09002463 if (probeinit)
2464 probeinit(ap);
2465
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002466 if (softreset) {
2467 rc = do_probe_reset(ap, softreset, postreset, classes);
2468 if (rc == 0)
2469 return 0;
2470 }
2471
2472 if (!hardreset)
2473 return rc;
2474
2475 rc = do_probe_reset(ap, hardreset, postreset, classes);
2476 if (rc == 0 || rc != -ENODEV)
2477 return rc;
2478
2479 if (softreset)
2480 rc = do_probe_reset(ap, softreset, postreset, classes);
2481
2482 return rc;
2483}
2484
Tejun Heo623a3122006-03-05 17:55:58 +09002485/**
2486 * ata_dev_same_device - Determine whether new ID matches configured device
2487 * @ap: port on which the device to compare against resides
2488 * @dev: device to compare against
2489 * @new_class: class of the new device
2490 * @new_id: IDENTIFY page of the new device
2491 *
2492 * Compare @new_class and @new_id against @dev and determine
2493 * whether @dev is the device indicated by @new_class and
2494 * @new_id.
2495 *
2496 * LOCKING:
2497 * None.
2498 *
2499 * RETURNS:
2500 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2501 */
2502static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2503 unsigned int new_class, const u16 *new_id)
2504{
2505 const u16 *old_id = dev->id;
2506 unsigned char model[2][41], serial[2][21];
2507 u64 new_n_sectors;
2508
2509 if (dev->class != new_class) {
2510 printk(KERN_INFO
2511 "ata%u: dev %u class mismatch %d != %d\n",
2512 ap->id, dev->devno, dev->class, new_class);
2513 return 0;
2514 }
2515
2516 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2517 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2518 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2519 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2520 new_n_sectors = ata_id_n_sectors(new_id);
2521
2522 if (strcmp(model[0], model[1])) {
2523 printk(KERN_INFO
2524 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2525 ap->id, dev->devno, model[0], model[1]);
2526 return 0;
2527 }
2528
2529 if (strcmp(serial[0], serial[1])) {
2530 printk(KERN_INFO
2531 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2532 ap->id, dev->devno, serial[0], serial[1]);
2533 return 0;
2534 }
2535
2536 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2537 printk(KERN_INFO
2538 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2539 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2540 (unsigned long long)new_n_sectors);
2541 return 0;
2542 }
2543
2544 return 1;
2545}
2546
2547/**
2548 * ata_dev_revalidate - Revalidate ATA device
2549 * @ap: port on which the device to revalidate resides
2550 * @dev: device to revalidate
2551 * @post_reset: is this revalidation after reset?
2552 *
2553 * Re-read IDENTIFY page and make sure @dev is still attached to
2554 * the port.
2555 *
2556 * LOCKING:
2557 * Kernel thread context (may sleep)
2558 *
2559 * RETURNS:
2560 * 0 on success, negative errno otherwise
2561 */
2562int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2563 int post_reset)
2564{
2565 unsigned int class;
2566 u16 *id;
2567 int rc;
2568
2569 if (!ata_dev_present(dev))
2570 return -ENODEV;
2571
2572 class = dev->class;
2573 id = NULL;
2574
2575 /* allocate & read ID data */
2576 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2577 if (rc)
2578 goto fail;
2579
2580 /* is the device still there? */
2581 if (!ata_dev_same_device(ap, dev, class, id)) {
2582 rc = -ENODEV;
2583 goto fail;
2584 }
2585
2586 kfree(dev->id);
2587 dev->id = id;
2588
2589 /* configure device according to the new ID */
2590 return ata_dev_configure(ap, dev, 0);
2591
2592 fail:
2593 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2594 ap->id, dev->devno, rc);
2595 kfree(id);
2596 return rc;
2597}
2598
Jeff Garzik057ace52005-10-22 14:27:05 -04002599static void ata_pr_blacklisted(const struct ata_port *ap,
2600 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
2602 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2603 ap->id, dev->devno);
2604}
2605
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002606static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 "WDC AC11000H",
2608 "WDC AC22100H",
2609 "WDC AC32500H",
2610 "WDC AC33100H",
2611 "WDC AC31600H",
2612 "WDC AC32100H",
2613 "WDC AC23200L",
2614 "Compaq CRD-8241B",
2615 "CRD-8400B",
2616 "CRD-8480B",
2617 "CRD-8482B",
2618 "CRD-84",
2619 "SanDisk SDP3B",
2620 "SanDisk SDP3B-64",
2621 "SANYO CD-ROM CRD",
2622 "HITACHI CDR-8",
2623 "HITACHI CDR-8335",
2624 "HITACHI CDR-8435",
2625 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002626 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 "CD-532E-A",
2628 "E-IDE CD-ROM CR-840",
2629 "CD-ROM Drive/F5A",
2630 "WPI CDD-820",
2631 "SAMSUNG CD-ROM SC-148C",
2632 "SAMSUNG CD-ROM SC",
2633 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2635 "_NEC DV5800A",
2636};
2637
Jeff Garzik057ace52005-10-22 14:27:05 -04002638static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Tejun Heo2e026712006-02-12 22:47:04 +09002640 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 int i;
2642
Tejun Heo6a62a042006-02-13 10:02:46 +09002643 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
2645 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002646 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 return 1;
2648
2649 return 0;
2650}
2651
Tejun Heoa6d5a512006-03-06 04:31:57 +09002652/**
2653 * ata_dev_xfermask - Compute supported xfermask of the given device
2654 * @ap: Port on which the device to compute xfermask for resides
2655 * @dev: Device to compute xfermask for
2656 *
2657 * Compute supported xfermask of @dev. This function is
2658 * responsible for applying all known limits including host
2659 * controller limits, device blacklist, etc...
2660 *
2661 * LOCKING:
2662 * None.
2663 *
2664 * RETURNS:
2665 * Computed xfermask.
2666 */
2667static unsigned int ata_dev_xfermask(struct ata_port *ap,
2668 struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Tejun Heoa6d5a512006-03-06 04:31:57 +09002670 unsigned long xfer_mask;
2671 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Tejun Heoa6d5a512006-03-06 04:31:57 +09002673 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2674 ap->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Tejun Heoa6d5a512006-03-06 04:31:57 +09002676 /* use port-wide xfermask for now */
2677 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2678 struct ata_device *d = &ap->device[i];
2679 if (!ata_dev_present(d))
2680 continue;
2681 xfer_mask &= ata_id_xfermask(d->id);
2682 if (ata_dma_blacklisted(d))
2683 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 }
2685
Tejun Heoa6d5a512006-03-06 04:31:57 +09002686 if (ata_dma_blacklisted(dev))
2687 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2688 "disabling DMA\n", ap->id, dev->devno);
2689
2690 return xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692
2693/* find greatest bit */
2694static int fgb(u32 bitmap)
2695{
2696 unsigned int i;
2697 int x = -1;
2698
2699 for (i = 0; i < 32; i++)
2700 if (bitmap & (1 << i))
2701 x = i;
2702
2703 return x;
2704}
2705
2706/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2708 * @ap: Port associated with device @dev
2709 * @dev: Device to which command will be sent
2710 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002711 * Issue SET FEATURES - XFER MODE command to device @dev
2712 * on port @ap.
2713 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002715 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 */
2717
2718static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2719{
Tejun Heoa0123702005-12-13 14:49:31 +09002720 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 /* set up set-features taskfile */
2723 DPRINTK("set features - xfer mode\n");
2724
Tejun Heoa0123702005-12-13 14:49:31 +09002725 ata_tf_init(ap, &tf, dev->devno);
2726 tf.command = ATA_CMD_SET_FEATURES;
2727 tf.feature = SETFEATURES_XFER;
2728 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2729 tf.protocol = ATA_PROT_NODATA;
2730 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Tejun Heoa0123702005-12-13 14:49:31 +09002732 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2733 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2734 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 DPRINTK("EXIT\n");
2739}
2740
2741/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002742 * ata_dev_init_params - Issue INIT DEV PARAMS command
2743 * @ap: Port associated with device @dev
2744 * @dev: Device to which command will be sent
2745 *
2746 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002747 * Kernel thread context (may sleep)
2748 *
2749 * RETURNS:
2750 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04002751 */
2752
Tejun Heo6aff8f12006-02-15 18:24:09 +09002753static unsigned int ata_dev_init_params(struct ata_port *ap,
2754 struct ata_device *dev)
Albert Lee8bf62ece2005-05-12 15:29:42 -04002755{
Tejun Heoa0123702005-12-13 14:49:31 +09002756 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002757 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002758 u16 sectors = dev->id[6];
2759 u16 heads = dev->id[3];
2760
2761 /* Number of sectors per track 1-255. Number of heads 1-16 */
2762 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002763 return 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002764
2765 /* set up init dev params taskfile */
2766 DPRINTK("init dev params \n");
2767
Tejun Heoa0123702005-12-13 14:49:31 +09002768 ata_tf_init(ap, &tf, dev->devno);
2769 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2770 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2771 tf.protocol = ATA_PROT_NODATA;
2772 tf.nsect = sectors;
2773 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04002774
Tejun Heo6aff8f12006-02-15 18:24:09 +09002775 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ece2005-05-12 15:29:42 -04002776
Tejun Heo6aff8f12006-02-15 18:24:09 +09002777 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2778 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002779}
2780
2781/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002782 * ata_sg_clean - Unmap DMA memory associated with command
2783 * @qc: Command containing DMA memory to be released
2784 *
2785 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 *
2787 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002788 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 */
2790
2791static void ata_sg_clean(struct ata_queued_cmd *qc)
2792{
2793 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002794 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002796 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Tejun Heoa46314742006-02-11 19:11:13 +09002798 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2799 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002802 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002804 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002806 /* if we padded the buffer out to 32-bit bound, and data
2807 * xfer direction is from-device, we must copy from the
2808 * pad buffer back into the supplied buffer
2809 */
2810 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2811 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2812
2813 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002814 if (qc->n_elem)
2815 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002816 /* restore last sg */
2817 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2818 if (pad_buf) {
2819 struct scatterlist *psg = &qc->pad_sgent;
2820 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2821 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002822 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002823 }
2824 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002825 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002826 dma_unmap_single(ap->host_set->dev,
2827 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2828 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002829 /* restore sg */
2830 sg->length += qc->pad_len;
2831 if (pad_buf)
2832 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2833 pad_buf, qc->pad_len);
2834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002837 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
2840/**
2841 * ata_fill_sg - Fill PCI IDE PRD table
2842 * @qc: Metadata associated with taskfile to be transferred
2843 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002844 * Fill PCI IDE PRD (scatter-gather) table with segments
2845 * associated with the current disk command.
2846 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002848 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 *
2850 */
2851static void ata_fill_sg(struct ata_queued_cmd *qc)
2852{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002854 struct scatterlist *sg;
2855 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Tejun Heoa46314742006-02-11 19:11:13 +09002857 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002858 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002861 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 u32 addr, offset;
2863 u32 sg_len, len;
2864
2865 /* determine if physical DMA addr spans 64K boundary.
2866 * Note h/w doesn't support 64-bit, so we unconditionally
2867 * truncate dma_addr_t to u32.
2868 */
2869 addr = (u32) sg_dma_address(sg);
2870 sg_len = sg_dma_len(sg);
2871
2872 while (sg_len) {
2873 offset = addr & 0xffff;
2874 len = sg_len;
2875 if ((offset + sg_len) > 0x10000)
2876 len = 0x10000 - offset;
2877
2878 ap->prd[idx].addr = cpu_to_le32(addr);
2879 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2880 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2881
2882 idx++;
2883 sg_len -= len;
2884 addr += len;
2885 }
2886 }
2887
2888 if (idx)
2889 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2890}
2891/**
2892 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2893 * @qc: Metadata associated with taskfile to check
2894 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002895 * Allow low-level driver to filter ATA PACKET commands, returning
2896 * a status indicating whether or not it is OK to use DMA for the
2897 * supplied PACKET command.
2898 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002900 * spin_lock_irqsave(host_set lock)
2901 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 * RETURNS: 0 when ATAPI DMA can be used
2903 * nonzero otherwise
2904 */
2905int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2906{
2907 struct ata_port *ap = qc->ap;
2908 int rc = 0; /* Assume ATAPI DMA is OK by default */
2909
2910 if (ap->ops->check_atapi_dma)
2911 rc = ap->ops->check_atapi_dma(qc);
2912
2913 return rc;
2914}
2915/**
2916 * ata_qc_prep - Prepare taskfile for submission
2917 * @qc: Metadata associated with taskfile to be prepared
2918 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002919 * Prepare ATA taskfile for submission.
2920 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 * LOCKING:
2922 * spin_lock_irqsave(host_set lock)
2923 */
2924void ata_qc_prep(struct ata_queued_cmd *qc)
2925{
2926 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2927 return;
2928
2929 ata_fill_sg(qc);
2930}
2931
Jeff Garzik0cba6322005-05-30 19:49:12 -04002932/**
2933 * ata_sg_init_one - Associate command with memory buffer
2934 * @qc: Command to be associated
2935 * @buf: Memory buffer
2936 * @buflen: Length of memory buffer, in bytes.
2937 *
2938 * Initialize the data-related elements of queued_cmd @qc
2939 * to point to a single memory buffer, @buf of byte length @buflen.
2940 *
2941 * LOCKING:
2942 * spin_lock_irqsave(host_set lock)
2943 */
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2946{
2947 struct scatterlist *sg;
2948
2949 qc->flags |= ATA_QCFLAG_SINGLE;
2950
2951 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002952 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002954 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 qc->buf_virt = buf;
2956
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002957 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002958 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960
Jeff Garzik0cba6322005-05-30 19:49:12 -04002961/**
2962 * ata_sg_init - Associate command with scatter-gather table.
2963 * @qc: Command to be associated
2964 * @sg: Scatter-gather table.
2965 * @n_elem: Number of elements in s/g table.
2966 *
2967 * Initialize the data-related elements of queued_cmd @qc
2968 * to point to a scatter-gather table @sg, containing @n_elem
2969 * elements.
2970 *
2971 * LOCKING:
2972 * spin_lock_irqsave(host_set lock)
2973 */
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2976 unsigned int n_elem)
2977{
2978 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002979 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002981 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
2984/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002985 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2986 * @qc: Command with memory buffer to be mapped.
2987 *
2988 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 *
2990 * LOCKING:
2991 * spin_lock_irqsave(host_set lock)
2992 *
2993 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002994 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 */
2996
2997static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2998{
2999 struct ata_port *ap = qc->ap;
3000 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003001 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003003 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003005 /* we must lengthen transfers to end on a 32-bit boundary */
3006 qc->pad_len = sg->length & 3;
3007 if (qc->pad_len) {
3008 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3009 struct scatterlist *psg = &qc->pad_sgent;
3010
Tejun Heoa46314742006-02-11 19:11:13 +09003011 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003012
3013 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3014
3015 if (qc->tf.flags & ATA_TFLAG_WRITE)
3016 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3017 qc->pad_len);
3018
3019 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3020 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3021 /* trim sg */
3022 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003023 if (sg->length == 0)
3024 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003025
3026 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3027 sg->length, qc->pad_len);
3028 }
3029
Tejun Heo2e242fa2006-02-20 23:48:38 +09003030 if (trim_sg) {
3031 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003032 goto skip_map;
3033 }
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003036 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003037 if (dma_mapping_error(dma_address)) {
3038 /* restore sg */
3039 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
3043 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003044 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Tejun Heo2e242fa2006-02-20 23:48:38 +09003046skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3048 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3049
3050 return 0;
3051}
3052
3053/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003054 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3055 * @qc: Command with scatter-gather table to be mapped.
3056 *
3057 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 *
3059 * LOCKING:
3060 * spin_lock_irqsave(host_set lock)
3061 *
3062 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003063 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 *
3065 */
3066
3067static int ata_sg_setup(struct ata_queued_cmd *qc)
3068{
3069 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003070 struct scatterlist *sg = qc->__sg;
3071 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003072 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa46314742006-02-11 19:11:13 +09003075 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003077 /* we must lengthen transfers to end on a 32-bit boundary */
3078 qc->pad_len = lsg->length & 3;
3079 if (qc->pad_len) {
3080 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3081 struct scatterlist *psg = &qc->pad_sgent;
3082 unsigned int offset;
3083
Tejun Heoa46314742006-02-11 19:11:13 +09003084 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003085
3086 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3087
3088 /*
3089 * psg->page/offset are used to copy to-be-written
3090 * data in this function or read data in ata_sg_clean.
3091 */
3092 offset = lsg->offset + lsg->length - qc->pad_len;
3093 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3094 psg->offset = offset_in_page(offset);
3095
3096 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3097 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3098 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003099 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003100 }
3101
3102 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3103 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3104 /* trim last sg */
3105 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003106 if (lsg->length == 0)
3107 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003108
3109 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3110 qc->n_elem - 1, lsg->length, qc->pad_len);
3111 }
3112
Jeff Garzike1410f22005-11-14 14:06:26 -05003113 pre_n_elem = qc->n_elem;
3114 if (trim_sg && pre_n_elem)
3115 pre_n_elem--;
3116
3117 if (!pre_n_elem) {
3118 n_elem = 0;
3119 goto skip_map;
3120 }
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05003123 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003124 if (n_elem < 1) {
3125 /* restore last sg */
3126 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 DPRINTK("%d sg elements mapped\n", n_elem);
3131
Jeff Garzike1410f22005-11-14 14:06:26 -05003132skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 qc->n_elem = n_elem;
3134
3135 return 0;
3136}
3137
3138/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003139 * ata_poll_qc_complete - turn irq back on and finish qc
3140 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003141 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003142 *
3143 * LOCKING:
3144 * None. (grabs host lock)
3145 */
3146
Albert Leea22e2eb2005-12-05 15:38:02 +08003147void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003148{
3149 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003150 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003151
Jeff Garzikb8f61532005-08-25 22:01:20 -04003152 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003153 ap->flags &= ~ATA_FLAG_NOINTR;
3154 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003155 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003156 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003157}
3158
3159/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003160 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003161 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 *
3163 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003164 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 *
3166 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003167 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 */
3169
3170static unsigned long ata_pio_poll(struct ata_port *ap)
3171{
Albert Leec14b8332005-12-05 15:36:08 +08003172 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003174 unsigned int poll_state = HSM_ST_UNKNOWN;
3175 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Albert Leec14b8332005-12-05 15:36:08 +08003177 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003178 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003179
Albert Lee14be71f2005-09-27 17:36:35 +08003180 switch (ap->hsm_task_state) {
3181 case HSM_ST:
3182 case HSM_ST_POLL:
3183 poll_state = HSM_ST_POLL;
3184 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003186 case HSM_ST_LAST:
3187 case HSM_ST_LAST_POLL:
3188 poll_state = HSM_ST_LAST_POLL;
3189 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 break;
3191 default:
3192 BUG();
3193 break;
3194 }
3195
3196 status = ata_chk_status(ap);
3197 if (status & ATA_BUSY) {
3198 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003199 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003200 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 return 0;
3202 }
Albert Lee14be71f2005-09-27 17:36:35 +08003203 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 return ATA_SHORT_PAUSE;
3205 }
3206
Albert Lee14be71f2005-09-27 17:36:35 +08003207 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return 0;
3209}
3210
3211/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003212 * ata_pio_complete - check if drive is busy or idle
3213 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 *
3215 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003216 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003217 *
3218 * RETURNS:
3219 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 */
3221
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003222static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
3224 struct ata_queued_cmd *qc;
3225 u8 drv_stat;
3226
3227 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003228 * This is purely heuristic. This is a fast path. Sometimes when
3229 * we enter, BSY will be cleared in a chk-status or two. If not,
3230 * the drive is probably seeking or something. Snooze for a couple
3231 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003232 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 */
Albert Leefe79e682005-12-06 11:34:59 +08003234 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3235 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003237 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3238 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003239 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
3243 }
3244
Albert Leec14b8332005-12-05 15:36:08 +08003245 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003246 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 drv_stat = ata_wait_idle(ap);
3249 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003250 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003251 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
3254
Albert Lee14be71f2005-09-27 17:36:35 +08003255 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Tejun Heoa46314742006-02-11 19:11:13 +09003257 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003258 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003259
3260 /* another command may start at this point */
3261
3262 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
Edward Falk0baab862005-06-02 18:17:13 -04003265
3266/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003267 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003268 * @buf: Buffer to swap
3269 * @buf_words: Number of 16-bit words in buffer.
3270 *
3271 * Swap halves of 16-bit words if needed to convert from
3272 * little-endian byte order to native cpu byte order, or
3273 * vice-versa.
3274 *
3275 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003276 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278void swap_buf_le16(u16 *buf, unsigned int buf_words)
3279{
3280#ifdef __BIG_ENDIAN
3281 unsigned int i;
3282
3283 for (i = 0; i < buf_words; i++)
3284 buf[i] = le16_to_cpu(buf[i]);
3285#endif /* __BIG_ENDIAN */
3286}
3287
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003288/**
3289 * ata_mmio_data_xfer - Transfer data by MMIO
3290 * @ap: port to read/write
3291 * @buf: data buffer
3292 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003293 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003294 *
3295 * Transfer data from/to the device data register by MMIO.
3296 *
3297 * LOCKING:
3298 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003299 */
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3302 unsigned int buflen, int write_data)
3303{
3304 unsigned int i;
3305 unsigned int words = buflen >> 1;
3306 u16 *buf16 = (u16 *) buf;
3307 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3308
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003309 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 if (write_data) {
3311 for (i = 0; i < words; i++)
3312 writew(le16_to_cpu(buf16[i]), mmio);
3313 } else {
3314 for (i = 0; i < words; i++)
3315 buf16[i] = cpu_to_le16(readw(mmio));
3316 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003317
3318 /* Transfer trailing 1 byte, if any. */
3319 if (unlikely(buflen & 0x01)) {
3320 u16 align_buf[1] = { 0 };
3321 unsigned char *trailing_buf = buf + buflen - 1;
3322
3323 if (write_data) {
3324 memcpy(align_buf, trailing_buf, 1);
3325 writew(le16_to_cpu(align_buf[0]), mmio);
3326 } else {
3327 align_buf[0] = cpu_to_le16(readw(mmio));
3328 memcpy(trailing_buf, align_buf, 1);
3329 }
3330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003333/**
3334 * ata_pio_data_xfer - Transfer data by PIO
3335 * @ap: port to read/write
3336 * @buf: data buffer
3337 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003338 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003339 *
3340 * Transfer data from/to the device data register by PIO.
3341 *
3342 * LOCKING:
3343 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003344 */
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3347 unsigned int buflen, int write_data)
3348{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003349 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003351 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003353 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003355 insw(ap->ioaddr.data_addr, buf, words);
3356
3357 /* Transfer trailing 1 byte, if any. */
3358 if (unlikely(buflen & 0x01)) {
3359 u16 align_buf[1] = { 0 };
3360 unsigned char *trailing_buf = buf + buflen - 1;
3361
3362 if (write_data) {
3363 memcpy(align_buf, trailing_buf, 1);
3364 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3365 } else {
3366 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3367 memcpy(trailing_buf, align_buf, 1);
3368 }
3369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003372/**
3373 * ata_data_xfer - Transfer data from/to the data register.
3374 * @ap: port to read/write
3375 * @buf: data buffer
3376 * @buflen: buffer length
3377 * @do_write: read/write
3378 *
3379 * Transfer data from/to the device data register.
3380 *
3381 * LOCKING:
3382 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003383 */
3384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3386 unsigned int buflen, int do_write)
3387{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003388 /* Make the crap hardware pay the costs not the good stuff */
3389 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3390 unsigned long flags;
3391 local_irq_save(flags);
3392 if (ap->flags & ATA_FLAG_MMIO)
3393 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3394 else
3395 ata_pio_data_xfer(ap, buf, buflen, do_write);
3396 local_irq_restore(flags);
3397 } else {
3398 if (ap->flags & ATA_FLAG_MMIO)
3399 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3400 else
3401 ata_pio_data_xfer(ap, buf, buflen, do_write);
3402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003405/**
3406 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3407 * @qc: Command on going
3408 *
3409 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3410 *
3411 * LOCKING:
3412 * Inherited from caller.
3413 */
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415static void ata_pio_sector(struct ata_queued_cmd *qc)
3416{
3417 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003418 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 struct ata_port *ap = qc->ap;
3420 struct page *page;
3421 unsigned int offset;
3422 unsigned char *buf;
3423
3424 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003425 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 page = sg[qc->cursg].page;
3428 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3429
3430 /* get the current page and offset */
3431 page = nth_page(page, (offset >> PAGE_SHIFT));
3432 offset %= PAGE_SIZE;
3433
3434 buf = kmap(page) + offset;
3435
3436 qc->cursect++;
3437 qc->cursg_ofs++;
3438
Albert Lee32529e02005-05-26 03:49:42 -04003439 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 qc->cursg++;
3441 qc->cursg_ofs = 0;
3442 }
3443
3444 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3445
3446 /* do the actual data transfer */
3447 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3448 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3449
3450 kunmap(page);
3451}
3452
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003453/**
3454 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3455 * @qc: Command on going
3456 * @bytes: number of bytes
3457 *
3458 * Transfer Transfer data from/to the ATAPI device.
3459 *
3460 * LOCKING:
3461 * Inherited from caller.
3462 *
3463 */
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3466{
3467 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003468 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 struct ata_port *ap = qc->ap;
3470 struct page *page;
3471 unsigned char *buf;
3472 unsigned int offset, count;
3473
Albert Lee563a6e12005-08-12 14:17:50 +08003474 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003475 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003478 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003479 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003480 * The end of qc->sg is reached and the device expects
3481 * more data to transfer. In order not to overrun qc->sg
3482 * and fulfill length specified in the byte count register,
3483 * - for read case, discard trailing data from the device
3484 * - for write case, padding zero data to the device
3485 */
3486 u16 pad_buf[1] = { 0 };
3487 unsigned int words = bytes >> 1;
3488 unsigned int i;
3489
3490 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003491 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003492 ap->id, bytes);
3493
3494 for (i = 0; i < words; i++)
3495 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3496
Albert Lee14be71f2005-09-27 17:36:35 +08003497 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003498 return;
3499 }
3500
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003501 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 page = sg->page;
3504 offset = sg->offset + qc->cursg_ofs;
3505
3506 /* get the current page and offset */
3507 page = nth_page(page, (offset >> PAGE_SHIFT));
3508 offset %= PAGE_SIZE;
3509
Albert Lee6952df02005-06-06 15:56:03 +08003510 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003511 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 /* don't cross page boundaries */
3514 count = min(count, (unsigned int)PAGE_SIZE - offset);
3515
3516 buf = kmap(page) + offset;
3517
3518 bytes -= count;
3519 qc->curbytes += count;
3520 qc->cursg_ofs += count;
3521
Albert Lee32529e02005-05-26 03:49:42 -04003522 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 qc->cursg++;
3524 qc->cursg_ofs = 0;
3525 }
3526
3527 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3528
3529 /* do the actual data transfer */
3530 ata_data_xfer(ap, buf, count, do_write);
3531
3532 kunmap(page);
3533
Albert Lee563a6e12005-08-12 14:17:50 +08003534 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536}
3537
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003538/**
3539 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3540 * @qc: Command on going
3541 *
3542 * Transfer Transfer data from/to the ATAPI device.
3543 *
3544 * LOCKING:
3545 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003546 */
3547
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3549{
3550 struct ata_port *ap = qc->ap;
3551 struct ata_device *dev = qc->dev;
3552 unsigned int ireason, bc_lo, bc_hi, bytes;
3553 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3554
3555 ap->ops->tf_read(ap, &qc->tf);
3556 ireason = qc->tf.nsect;
3557 bc_lo = qc->tf.lbam;
3558 bc_hi = qc->tf.lbah;
3559 bytes = (bc_hi << 8) | bc_lo;
3560
3561 /* shall be cleared to zero, indicating xfer of data */
3562 if (ireason & (1 << 0))
3563 goto err_out;
3564
3565 /* make sure transfer direction matches expected */
3566 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3567 if (do_write != i_write)
3568 goto err_out;
3569
3570 __atapi_pio_bytes(qc, bytes);
3571
3572 return;
3573
3574err_out:
3575 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3576 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003577 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003578 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
3581/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003582 * ata_pio_block - start PIO on a block
3583 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 *
3585 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003586 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 */
3588
3589static void ata_pio_block(struct ata_port *ap)
3590{
3591 struct ata_queued_cmd *qc;
3592 u8 status;
3593
3594 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003595 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 * Sometimes when we enter, BSY will be cleared in
3597 * a chk-status or two. If not, the drive is probably seeking
3598 * or something. Snooze for a couple msecs, then
3599 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003600 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 */
3602 status = ata_busy_wait(ap, ATA_BUSY, 5);
3603 if (status & ATA_BUSY) {
3604 msleep(2);
3605 status = ata_busy_wait(ap, ATA_BUSY, 10);
3606 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003607 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3609 return;
3610 }
3611 }
3612
3613 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003614 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Albert Leefe79e682005-12-06 11:34:59 +08003616 /* check error */
3617 if (status & (ATA_ERR | ATA_DF)) {
3618 qc->err_mask |= AC_ERR_DEV;
3619 ap->hsm_task_state = HSM_ST_ERR;
3620 return;
3621 }
3622
3623 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003625 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003627 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 return;
3629 }
3630
3631 atapi_pio_bytes(qc);
3632 } else {
3633 /* handle BSY=0, DRQ=0 as error */
3634 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003635 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003636 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 return;
3638 }
3639
3640 ata_pio_sector(qc);
3641 }
3642}
3643
3644static void ata_pio_error(struct ata_port *ap)
3645{
3646 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003649 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Albert Lee0565c262006-02-13 18:55:25 +08003651 if (qc->tf.command != ATA_CMD_PACKET)
3652 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3653
Albert Lee1c848982005-12-05 15:40:15 +08003654 /* make sure qc->err_mask is available to
3655 * know what's wrong and recover
3656 */
Tejun Heoa46314742006-02-11 19:11:13 +09003657 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003658
Albert Lee14be71f2005-09-27 17:36:35 +08003659 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Albert Leea22e2eb2005-12-05 15:38:02 +08003661 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
3664static void ata_pio_task(void *_data)
3665{
3666 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003667 unsigned long timeout;
3668 int qc_completed;
3669
3670fsm_start:
3671 timeout = 0;
3672 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Albert Lee14be71f2005-09-27 17:36:35 +08003674 switch (ap->hsm_task_state) {
3675 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return;
3677
Albert Lee14be71f2005-09-27 17:36:35 +08003678 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 ata_pio_block(ap);
3680 break;
3681
Albert Lee14be71f2005-09-27 17:36:35 +08003682 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003683 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 break;
3685
Albert Lee14be71f2005-09-27 17:36:35 +08003686 case HSM_ST_POLL:
3687 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 timeout = ata_pio_poll(ap);
3689 break;
3690
Albert Lee14be71f2005-09-27 17:36:35 +08003691 case HSM_ST_TMOUT:
3692 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 ata_pio_error(ap);
3694 return;
3695 }
3696
3697 if (timeout)
Tejun Heo8061f5f2006-03-05 15:29:09 +09003698 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003699 else if (!qc_completed)
3700 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703/**
Tejun Heo8061f5f2006-03-05 15:29:09 +09003704 * atapi_packet_task - Write CDB bytes to hardware
3705 * @_data: Port to which ATAPI device is attached.
3706 *
3707 * When device has indicated its readiness to accept
3708 * a CDB, this function is called. Send the CDB.
3709 * If DMA is to be performed, exit immediately.
3710 * Otherwise, we are in polling mode, so poll
3711 * status under operation succeeds or fails.
3712 *
3713 * LOCKING:
3714 * Kernel thread context (may sleep)
3715 */
3716
3717static void atapi_packet_task(void *_data)
3718{
3719 struct ata_port *ap = _data;
3720 struct ata_queued_cmd *qc;
3721 u8 status;
3722
3723 qc = ata_qc_from_tag(ap, ap->active_tag);
3724 WARN_ON(qc == NULL);
3725 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3726
3727 /* sleep-wait for BSY to clear */
3728 DPRINTK("busy wait\n");
3729 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3730 qc->err_mask |= AC_ERR_TIMEOUT;
3731 goto err_out;
3732 }
3733
3734 /* make sure DRQ is set */
3735 status = ata_chk_status(ap);
3736 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3737 qc->err_mask |= AC_ERR_HSM;
3738 goto err_out;
3739 }
3740
3741 /* send SCSI cdb */
3742 DPRINTK("send cdb\n");
3743 WARN_ON(qc->dev->cdb_len < 12);
3744
3745 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3746 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3747 unsigned long flags;
3748
3749 /* Once we're done issuing command and kicking bmdma,
3750 * irq handler takes over. To not lose irq, we need
3751 * to clear NOINTR flag before sending cdb, but
3752 * interrupt handler shouldn't be invoked before we're
3753 * finished. Hence, the following locking.
3754 */
3755 spin_lock_irqsave(&ap->host_set->lock, flags);
3756 ap->flags &= ~ATA_FLAG_NOINTR;
3757 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3758 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3759 ap->ops->bmdma_start(qc); /* initiate bmdma */
3760 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3761 } else {
3762 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3763
3764 /* PIO commands are handled by polling */
3765 ap->hsm_task_state = HSM_ST;
3766 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3767 }
3768
3769 return;
3770
3771err_out:
3772 ata_poll_qc_complete(qc);
3773}
3774
3775/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 * ata_qc_timeout - Handle timeout of queued command
3777 * @qc: Command that timed out
3778 *
3779 * Some part of the kernel (currently, only the SCSI layer)
3780 * has noticed that the active command on port @ap has not
3781 * completed after a specified length of time. Handle this
3782 * condition by disabling DMA (if necessary) and completing
3783 * transactions, with error if necessary.
3784 *
3785 * This also handles the case of the "lost interrupt", where
3786 * for some reason (possibly hardware bug, possibly driver bug)
3787 * an interrupt was not delivered to the driver, even though the
3788 * transaction completed successfully.
3789 *
3790 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003791 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 */
3793
3794static void ata_qc_timeout(struct ata_queued_cmd *qc)
3795{
3796 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003797 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003799 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
3801 DPRINTK("ENTER\n");
3802
Tejun Heoc18d06f2006-02-02 00:56:10 +09003803 ap->hsm_task_state = HSM_ST_IDLE;
3804
Jeff Garzikb8f61532005-08-25 22:01:20 -04003805 spin_lock_irqsave(&host_set->lock, flags);
3806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 switch (qc->tf.protocol) {
3808
3809 case ATA_PROT_DMA:
3810 case ATA_PROT_ATAPI_DMA:
3811 host_stat = ap->ops->bmdma_status(ap);
3812
3813 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003814 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
3816 /* fall through */
3817
3818 default:
3819 ata_altstatus(ap);
3820 drv_stat = ata_chk_status(ap);
3821
3822 /* ack bmdma irq events */
3823 ap->ops->irq_clear(ap);
3824
3825 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3826 ap->id, qc->tf.command, drv_stat, host_stat);
3827
3828 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003829 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 break;
3831 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003832
3833 spin_unlock_irqrestore(&host_set->lock, flags);
3834
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003835 ata_eh_qc_complete(qc);
3836
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 DPRINTK("EXIT\n");
3838}
3839
3840/**
3841 * ata_eng_timeout - Handle timeout of queued command
3842 * @ap: Port on which timed-out command is active
3843 *
3844 * Some part of the kernel (currently, only the SCSI layer)
3845 * has noticed that the active command on port @ap has not
3846 * completed after a specified length of time. Handle this
3847 * condition by disabling DMA (if necessary) and completing
3848 * transactions, with error if necessary.
3849 *
3850 * This also handles the case of the "lost interrupt", where
3851 * for some reason (possibly hardware bug, possibly driver bug)
3852 * an interrupt was not delivered to the driver, even though the
3853 * transaction completed successfully.
3854 *
3855 * LOCKING:
3856 * Inherited from SCSI layer (none, can sleep)
3857 */
3858
3859void ata_eng_timeout(struct ata_port *ap)
3860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 DPRINTK("ENTER\n");
3862
Tejun Heof6379022006-02-10 15:10:48 +09003863 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 DPRINTK("EXIT\n");
3866}
3867
3868/**
3869 * ata_qc_new - Request an available ATA command, for queueing
3870 * @ap: Port associated with device @dev
3871 * @dev: Device from whom we request an available command structure
3872 *
3873 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003874 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 */
3876
3877static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3878{
3879 struct ata_queued_cmd *qc = NULL;
3880 unsigned int i;
3881
3882 for (i = 0; i < ATA_MAX_QUEUE; i++)
3883 if (!test_and_set_bit(i, &ap->qactive)) {
3884 qc = ata_qc_from_tag(ap, i);
3885 break;
3886 }
3887
3888 if (qc)
3889 qc->tag = i;
3890
3891 return qc;
3892}
3893
3894/**
3895 * ata_qc_new_init - Request an available ATA command, and initialize it
3896 * @ap: Port associated with device @dev
3897 * @dev: Device from whom we request an available command structure
3898 *
3899 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003900 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 */
3902
3903struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3904 struct ata_device *dev)
3905{
3906 struct ata_queued_cmd *qc;
3907
3908 qc = ata_qc_new(ap);
3909 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 qc->scsicmd = NULL;
3911 qc->ap = ap;
3912 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003914 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 }
3916
3917 return qc;
3918}
3919
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920/**
3921 * ata_qc_free - free unused ata_queued_cmd
3922 * @qc: Command to complete
3923 *
3924 * Designed to free unused ata_queued_cmd object
3925 * in case something prevents using it.
3926 *
3927 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003928 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 */
3930void ata_qc_free(struct ata_queued_cmd *qc)
3931{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003932 struct ata_port *ap = qc->ap;
3933 unsigned int tag;
3934
Tejun Heoa46314742006-02-11 19:11:13 +09003935 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Tejun Heo4ba946e2006-01-23 13:09:36 +09003937 qc->flags = 0;
3938 tag = qc->tag;
3939 if (likely(ata_tag_valid(tag))) {
3940 if (tag == ap->active_tag)
3941 ap->active_tag = ATA_TAG_POISON;
3942 qc->tag = ATA_TAG_POISON;
3943 clear_bit(tag, &ap->qactive);
3944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945}
3946
Tejun Heo76014422006-02-11 15:13:49 +09003947void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
Tejun Heoa46314742006-02-11 19:11:13 +09003949 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3950 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
3952 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3953 ata_sg_clean(qc);
3954
Albert Lee3f3791d2005-08-16 14:25:38 +08003955 /* atapi: mark qc as inactive to prevent the interrupt handler
3956 * from completing the command twice later, before the error handler
3957 * is called. (when rc != 0 and atapi request sense is needed)
3958 */
3959 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3960
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003962 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963}
3964
3965static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3966{
3967 struct ata_port *ap = qc->ap;
3968
3969 switch (qc->tf.protocol) {
3970 case ATA_PROT_DMA:
3971 case ATA_PROT_ATAPI_DMA:
3972 return 1;
3973
3974 case ATA_PROT_ATAPI:
3975 case ATA_PROT_PIO:
3976 case ATA_PROT_PIO_MULT:
3977 if (ap->flags & ATA_FLAG_PIO_DMA)
3978 return 1;
3979
3980 /* fall through */
3981
3982 default:
3983 return 0;
3984 }
3985
3986 /* never reached */
3987}
3988
3989/**
3990 * ata_qc_issue - issue taskfile to device
3991 * @qc: command to issue to device
3992 *
3993 * Prepare an ATA command to submission to device.
3994 * This includes mapping the data into a DMA-able
3995 * area, filling in the S/G table, and finally
3996 * writing the taskfile to hardware, starting the command.
3997 *
3998 * LOCKING:
3999 * spin_lock_irqsave(host_set lock)
4000 *
4001 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004002 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 */
4004
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004005unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
4007 struct ata_port *ap = qc->ap;
4008
4009 if (ata_should_dma_map(qc)) {
4010 if (qc->flags & ATA_QCFLAG_SG) {
4011 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004012 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4014 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004015 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 }
4017 } else {
4018 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4019 }
4020
4021 ap->ops->qc_prep(qc);
4022
4023 qc->ap->active_tag = qc->tag;
4024 qc->flags |= ATA_QCFLAG_ACTIVE;
4025
4026 return ap->ops->qc_issue(qc);
4027
Tejun Heo8e436af2006-01-23 13:09:36 +09004028sg_err:
4029 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004030 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031}
4032
Edward Falk0baab862005-06-02 18:17:13 -04004033
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034/**
4035 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4036 * @qc: command to issue to device
4037 *
4038 * Using various libata functions and hooks, this function
4039 * starts an ATA command. ATA commands are grouped into
4040 * classes called "protocols", and issuing each type of protocol
4041 * is slightly different.
4042 *
Edward Falk0baab862005-06-02 18:17:13 -04004043 * May be used as the qc_issue() entry in ata_port_operations.
4044 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 * LOCKING:
4046 * spin_lock_irqsave(host_set lock)
4047 *
4048 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004049 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 */
4051
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004052unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053{
4054 struct ata_port *ap = qc->ap;
4055
4056 ata_dev_select(ap, qc->dev->devno, 1, 0);
4057
4058 switch (qc->tf.protocol) {
4059 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05004060 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 break;
4062
4063 case ATA_PROT_DMA:
4064 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4065 ap->ops->bmdma_setup(qc); /* set up bmdma */
4066 ap->ops->bmdma_start(qc); /* initiate bmdma */
4067 break;
4068
4069 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4070 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004071 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08004072 ap->hsm_task_state = HSM_ST;
Tejun Heo8061f5f2006-03-05 15:29:09 +09004073 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 break;
4075
4076 case ATA_PROT_ATAPI:
4077 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004078 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004079 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 break;
4081
4082 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09004083 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05004084 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004085 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 break;
4087
4088 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09004089 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4091 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo8061f5f2006-03-05 15:29:09 +09004092 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 break;
4094
4095 default:
4096 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004097 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 }
4099
4100 return 0;
4101}
4102
4103/**
Edward Falk0baab862005-06-02 18:17:13 -04004104 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 * @qc: Info associated with this ATA transaction.
4106 *
4107 * LOCKING:
4108 * spin_lock_irqsave(host_set lock)
4109 */
4110
4111static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4112{
4113 struct ata_port *ap = qc->ap;
4114 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4115 u8 dmactl;
4116 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4117
4118 /* load PRD table addr. */
4119 mb(); /* make sure PRD table writes are visible to controller */
4120 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4121
4122 /* specify data direction, triple-check start bit is clear */
4123 dmactl = readb(mmio + ATA_DMA_CMD);
4124 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4125 if (!rw)
4126 dmactl |= ATA_DMA_WR;
4127 writeb(dmactl, mmio + ATA_DMA_CMD);
4128
4129 /* issue r/w command */
4130 ap->ops->exec_command(ap, &qc->tf);
4131}
4132
4133/**
Alan Coxb73fc892005-08-26 16:03:19 +01004134 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 * @qc: Info associated with this ATA transaction.
4136 *
4137 * LOCKING:
4138 * spin_lock_irqsave(host_set lock)
4139 */
4140
4141static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4142{
4143 struct ata_port *ap = qc->ap;
4144 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4145 u8 dmactl;
4146
4147 /* start host DMA transaction */
4148 dmactl = readb(mmio + ATA_DMA_CMD);
4149 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4150
4151 /* Strictly, one may wish to issue a readb() here, to
4152 * flush the mmio write. However, control also passes
4153 * to the hardware at this point, and it will interrupt
4154 * us when we are to resume control. So, in effect,
4155 * we don't care when the mmio write flushes.
4156 * Further, a read of the DMA status register _immediately_
4157 * following the write may not be what certain flaky hardware
4158 * is expected, so I think it is best to not add a readb()
4159 * without first all the MMIO ATA cards/mobos.
4160 * Or maybe I'm just being paranoid.
4161 */
4162}
4163
4164/**
4165 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4166 * @qc: Info associated with this ATA transaction.
4167 *
4168 * LOCKING:
4169 * spin_lock_irqsave(host_set lock)
4170 */
4171
4172static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4173{
4174 struct ata_port *ap = qc->ap;
4175 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4176 u8 dmactl;
4177
4178 /* load PRD table addr. */
4179 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4180
4181 /* specify data direction, triple-check start bit is clear */
4182 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4183 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4184 if (!rw)
4185 dmactl |= ATA_DMA_WR;
4186 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4187
4188 /* issue r/w command */
4189 ap->ops->exec_command(ap, &qc->tf);
4190}
4191
4192/**
4193 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4194 * @qc: Info associated with this ATA transaction.
4195 *
4196 * LOCKING:
4197 * spin_lock_irqsave(host_set lock)
4198 */
4199
4200static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4201{
4202 struct ata_port *ap = qc->ap;
4203 u8 dmactl;
4204
4205 /* start host DMA transaction */
4206 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4207 outb(dmactl | ATA_DMA_START,
4208 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4209}
4210
Edward Falk0baab862005-06-02 18:17:13 -04004211
4212/**
4213 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4214 * @qc: Info associated with this ATA transaction.
4215 *
4216 * Writes the ATA_DMA_START flag to the DMA command register.
4217 *
4218 * May be used as the bmdma_start() entry in ata_port_operations.
4219 *
4220 * LOCKING:
4221 * spin_lock_irqsave(host_set lock)
4222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223void ata_bmdma_start(struct ata_queued_cmd *qc)
4224{
4225 if (qc->ap->flags & ATA_FLAG_MMIO)
4226 ata_bmdma_start_mmio(qc);
4227 else
4228 ata_bmdma_start_pio(qc);
4229}
4230
Edward Falk0baab862005-06-02 18:17:13 -04004231
4232/**
4233 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4234 * @qc: Info associated with this ATA transaction.
4235 *
4236 * Writes address of PRD table to device's PRD Table Address
4237 * register, sets the DMA control register, and calls
4238 * ops->exec_command() to start the transfer.
4239 *
4240 * May be used as the bmdma_setup() entry in ata_port_operations.
4241 *
4242 * LOCKING:
4243 * spin_lock_irqsave(host_set lock)
4244 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245void ata_bmdma_setup(struct ata_queued_cmd *qc)
4246{
4247 if (qc->ap->flags & ATA_FLAG_MMIO)
4248 ata_bmdma_setup_mmio(qc);
4249 else
4250 ata_bmdma_setup_pio(qc);
4251}
4252
Edward Falk0baab862005-06-02 18:17:13 -04004253
4254/**
4255 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004256 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004257 *
4258 * Clear interrupt and error flags in DMA status register.
4259 *
4260 * May be used as the irq_clear() entry in ata_port_operations.
4261 *
4262 * LOCKING:
4263 * spin_lock_irqsave(host_set lock)
4264 */
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266void ata_bmdma_irq_clear(struct ata_port *ap)
4267{
4268 if (ap->flags & ATA_FLAG_MMIO) {
4269 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4270 writeb(readb(mmio), mmio);
4271 } else {
4272 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4273 outb(inb(addr), addr);
4274 }
4275
4276}
4277
Edward Falk0baab862005-06-02 18:17:13 -04004278
4279/**
4280 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004281 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004282 *
4283 * Read and return BMDMA status register.
4284 *
4285 * May be used as the bmdma_status() entry in ata_port_operations.
4286 *
4287 * LOCKING:
4288 * spin_lock_irqsave(host_set lock)
4289 */
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291u8 ata_bmdma_status(struct ata_port *ap)
4292{
4293 u8 host_stat;
4294 if (ap->flags & ATA_FLAG_MMIO) {
4295 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4296 host_stat = readb(mmio + ATA_DMA_STATUS);
4297 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004298 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 return host_stat;
4300}
4301
Edward Falk0baab862005-06-02 18:17:13 -04004302
4303/**
4304 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004305 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004306 *
4307 * Clears the ATA_DMA_START flag in the dma control register
4308 *
4309 * May be used as the bmdma_stop() entry in ata_port_operations.
4310 *
4311 * LOCKING:
4312 * spin_lock_irqsave(host_set lock)
4313 */
4314
Alan Coxb73fc892005-08-26 16:03:19 +01004315void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
Alan Coxb73fc892005-08-26 16:03:19 +01004317 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 if (ap->flags & ATA_FLAG_MMIO) {
4319 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4320
4321 /* clear start/stop bit */
4322 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4323 mmio + ATA_DMA_CMD);
4324 } else {
4325 /* clear start/stop bit */
4326 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4327 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4328 }
4329
4330 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4331 ata_altstatus(ap); /* dummy read */
4332}
4333
4334/**
4335 * ata_host_intr - Handle host interrupt for given (port, task)
4336 * @ap: Port on which interrupt arrived (possibly...)
4337 * @qc: Taskfile currently active in engine
4338 *
4339 * Handle host interrupt for given queued command. Currently,
4340 * only DMA interrupts are handled. All other commands are
4341 * handled via polling with interrupts disabled (nIEN bit).
4342 *
4343 * LOCKING:
4344 * spin_lock_irqsave(host_set lock)
4345 *
4346 * RETURNS:
4347 * One if interrupt was handled, zero if not (shared irq).
4348 */
4349
4350inline unsigned int ata_host_intr (struct ata_port *ap,
4351 struct ata_queued_cmd *qc)
4352{
4353 u8 status, host_stat;
4354
4355 switch (qc->tf.protocol) {
4356
4357 case ATA_PROT_DMA:
4358 case ATA_PROT_ATAPI_DMA:
4359 case ATA_PROT_ATAPI:
4360 /* check status of DMA engine */
4361 host_stat = ap->ops->bmdma_status(ap);
4362 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4363
4364 /* if it's not our irq... */
4365 if (!(host_stat & ATA_DMA_INTR))
4366 goto idle_irq;
4367
4368 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004369 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
4371 /* fall through */
4372
4373 case ATA_PROT_ATAPI_NODATA:
4374 case ATA_PROT_NODATA:
4375 /* check altstatus */
4376 status = ata_altstatus(ap);
4377 if (status & ATA_BUSY)
4378 goto idle_irq;
4379
4380 /* check main status, clearing INTRQ */
4381 status = ata_chk_status(ap);
4382 if (unlikely(status & ATA_BUSY))
4383 goto idle_irq;
4384 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4385 ap->id, qc->tf.protocol, status);
4386
4387 /* ack bmdma irq events */
4388 ap->ops->irq_clear(ap);
4389
4390 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004391 qc->err_mask |= ac_err_mask(status);
4392 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 break;
4394
4395 default:
4396 goto idle_irq;
4397 }
4398
4399 return 1; /* irq handled */
4400
4401idle_irq:
4402 ap->stats.idle_irq++;
4403
4404#ifdef ATA_IRQ_TRAP
4405 if ((ap->stats.idle_irq % 1000) == 0) {
4406 handled = 1;
4407 ata_irq_ack(ap, 0); /* debug trap */
4408 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4409 }
4410#endif
4411 return 0; /* irq not handled */
4412}
4413
4414/**
4415 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004416 * @irq: irq line (unused)
4417 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 * @regs: unused
4419 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004420 * Default interrupt handler for PCI IDE devices. Calls
4421 * ata_host_intr() for each port that is not disabled.
4422 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004424 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 *
4426 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004427 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 */
4429
4430irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4431{
4432 struct ata_host_set *host_set = dev_instance;
4433 unsigned int i;
4434 unsigned int handled = 0;
4435 unsigned long flags;
4436
4437 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4438 spin_lock_irqsave(&host_set->lock, flags);
4439
4440 for (i = 0; i < host_set->n_ports; i++) {
4441 struct ata_port *ap;
4442
4443 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004444 if (ap &&
4445 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 struct ata_queued_cmd *qc;
4447
4448 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004449 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4450 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 handled |= ata_host_intr(ap, qc);
4452 }
4453 }
4454
4455 spin_unlock_irqrestore(&host_set->lock, flags);
4456
4457 return IRQ_RETVAL(handled);
4458}
4459
Edward Falk0baab862005-06-02 18:17:13 -04004460
Jens Axboe9b847542006-01-06 09:28:07 +01004461/*
4462 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4463 * without filling any other registers
4464 */
4465static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4466 u8 cmd)
4467{
4468 struct ata_taskfile tf;
4469 int err;
4470
4471 ata_tf_init(ap, &tf, dev->devno);
4472
4473 tf.command = cmd;
4474 tf.flags |= ATA_TFLAG_DEVICE;
4475 tf.protocol = ATA_PROT_NODATA;
4476
4477 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4478 if (err)
4479 printk(KERN_ERR "%s: ata command failed: %d\n",
4480 __FUNCTION__, err);
4481
4482 return err;
4483}
4484
4485static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4486{
4487 u8 cmd;
4488
4489 if (!ata_try_flush_cache(dev))
4490 return 0;
4491
4492 if (ata_id_has_flush_ext(dev->id))
4493 cmd = ATA_CMD_FLUSH_EXT;
4494 else
4495 cmd = ATA_CMD_FLUSH;
4496
4497 return ata_do_simple_cmd(ap, dev, cmd);
4498}
4499
4500static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4501{
4502 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4503}
4504
4505static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4506{
4507 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4508}
4509
4510/**
4511 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004512 * @ap: port the device is connected to
4513 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004514 *
4515 * Kick the drive back into action, by sending it an idle immediate
4516 * command and making sure its transfer mode matches between drive
4517 * and host.
4518 *
4519 */
4520int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4521{
4522 if (ap->flags & ATA_FLAG_SUSPENDED) {
4523 ap->flags &= ~ATA_FLAG_SUSPENDED;
4524 ata_set_mode(ap);
4525 }
4526 if (!ata_dev_present(dev))
4527 return 0;
4528 if (dev->class == ATA_DEV_ATA)
4529 ata_start_drive(ap, dev);
4530
4531 return 0;
4532}
4533
4534/**
4535 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004536 * @ap: port the device is connected to
4537 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004538 *
4539 * Flush the cache on the drive, if appropriate, then issue a
4540 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004541 */
4542int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4543{
4544 if (!ata_dev_present(dev))
4545 return 0;
4546 if (dev->class == ATA_DEV_ATA)
4547 ata_flush_cache(ap, dev);
4548
4549 ata_standby_drive(ap, dev);
4550 ap->flags |= ATA_FLAG_SUSPENDED;
4551 return 0;
4552}
4553
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004554/**
4555 * ata_port_start - Set port up for dma.
4556 * @ap: Port to initialize
4557 *
4558 * Called just after data structures for each port are
4559 * initialized. Allocates space for PRD table.
4560 *
4561 * May be used as the port_start() entry in ata_port_operations.
4562 *
4563 * LOCKING:
4564 * Inherited from caller.
4565 */
4566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567int ata_port_start (struct ata_port *ap)
4568{
4569 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004570 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
4572 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4573 if (!ap->prd)
4574 return -ENOMEM;
4575
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004576 rc = ata_pad_alloc(ap, dev);
4577 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004578 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004579 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004580 }
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4583
4584 return 0;
4585}
4586
Edward Falk0baab862005-06-02 18:17:13 -04004587
4588/**
4589 * ata_port_stop - Undo ata_port_start()
4590 * @ap: Port to shut down
4591 *
4592 * Frees the PRD table.
4593 *
4594 * May be used as the port_stop() entry in ata_port_operations.
4595 *
4596 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004597 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004598 */
4599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600void ata_port_stop (struct ata_port *ap)
4601{
4602 struct device *dev = ap->host_set->dev;
4603
4604 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004605 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606}
4607
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004608void ata_host_stop (struct ata_host_set *host_set)
4609{
4610 if (host_set->mmio_base)
4611 iounmap(host_set->mmio_base);
4612}
4613
4614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615/**
4616 * ata_host_remove - Unregister SCSI host structure with upper layers
4617 * @ap: Port to unregister
4618 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4619 *
4620 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004621 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 */
4623
4624static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4625{
4626 struct Scsi_Host *sh = ap->host;
4627
4628 DPRINTK("ENTER\n");
4629
4630 if (do_unregister)
4631 scsi_remove_host(sh);
4632
4633 ap->ops->port_stop(ap);
4634}
4635
4636/**
4637 * ata_host_init - Initialize an ata_port structure
4638 * @ap: Structure to initialize
4639 * @host: associated SCSI mid-layer structure
4640 * @host_set: Collection of hosts to which @ap belongs
4641 * @ent: Probe information provided by low-level driver
4642 * @port_no: Port number associated with this ata_port
4643 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004644 * Initialize a new ata_port structure, and its associated
4645 * scsi_host.
4646 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004648 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 */
4650
4651static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4652 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004653 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654{
4655 unsigned int i;
4656
4657 host->max_id = 16;
4658 host->max_lun = 1;
4659 host->max_channel = 1;
4660 host->unique_id = ata_unique_id++;
4661 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 ap->flags = ATA_FLAG_PORT_DISABLED;
4664 ap->id = host->unique_id;
4665 ap->host = host;
4666 ap->ctl = ATA_DEVCTL_OBS;
4667 ap->host_set = host_set;
4668 ap->port_no = port_no;
4669 ap->hard_port_no =
4670 ent->legacy_mode ? ent->hard_port_no : port_no;
4671 ap->pio_mask = ent->pio_mask;
4672 ap->mwdma_mask = ent->mwdma_mask;
4673 ap->udma_mask = ent->udma_mask;
4674 ap->flags |= ent->host_flags;
4675 ap->ops = ent->port_ops;
4676 ap->cbl = ATA_CBL_NONE;
4677 ap->active_tag = ATA_TAG_POISON;
4678 ap->last_ctl = 0xFF;
4679
Tejun Heo86e45b62006-03-05 15:29:09 +09004680 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004681 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
4683 for (i = 0; i < ATA_MAX_DEVICES; i++)
4684 ap->device[i].devno = i;
4685
4686#ifdef ATA_IRQ_TRAP
4687 ap->stats.unhandled_irq = 1;
4688 ap->stats.idle_irq = 1;
4689#endif
4690
4691 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4692}
4693
4694/**
4695 * ata_host_add - Attach low-level ATA driver to system
4696 * @ent: Information provided by low-level driver
4697 * @host_set: Collections of ports to which we add
4698 * @port_no: Port number associated with this host
4699 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004700 * Attach low-level ATA driver to system.
4701 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004703 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 *
4705 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004706 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 */
4708
Jeff Garzik057ace52005-10-22 14:27:05 -04004709static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 struct ata_host_set *host_set,
4711 unsigned int port_no)
4712{
4713 struct Scsi_Host *host;
4714 struct ata_port *ap;
4715 int rc;
4716
4717 DPRINTK("ENTER\n");
4718 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4719 if (!host)
4720 return NULL;
4721
4722 ap = (struct ata_port *) &host->hostdata[0];
4723
4724 ata_host_init(ap, host, host_set, ent, port_no);
4725
4726 rc = ap->ops->port_start(ap);
4727 if (rc)
4728 goto err_out;
4729
4730 return ap;
4731
4732err_out:
4733 scsi_host_put(host);
4734 return NULL;
4735}
4736
4737/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004738 * ata_device_add - Register hardware device with ATA and SCSI layers
4739 * @ent: Probe information describing hardware device to be registered
4740 *
4741 * This function processes the information provided in the probe
4742 * information struct @ent, allocates the necessary ATA and SCSI
4743 * host information structures, initializes them, and registers
4744 * everything with requisite kernel subsystems.
4745 *
4746 * This function requests irqs, probes the ATA bus, and probes
4747 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 *
4749 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004750 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 *
4752 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004753 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 */
4755
Jeff Garzik057ace52005-10-22 14:27:05 -04004756int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757{
4758 unsigned int count = 0, i;
4759 struct device *dev = ent->dev;
4760 struct ata_host_set *host_set;
4761
4762 DPRINTK("ENTER\n");
4763 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004764 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4766 if (!host_set)
4767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 spin_lock_init(&host_set->lock);
4769
4770 host_set->dev = dev;
4771 host_set->n_ports = ent->n_ports;
4772 host_set->irq = ent->irq;
4773 host_set->mmio_base = ent->mmio_base;
4774 host_set->private_data = ent->private_data;
4775 host_set->ops = ent->port_ops;
4776
4777 /* register each port bound to this device */
4778 for (i = 0; i < ent->n_ports; i++) {
4779 struct ata_port *ap;
4780 unsigned long xfer_mode_mask;
4781
4782 ap = ata_host_add(ent, host_set, i);
4783 if (!ap)
4784 goto err_out;
4785
4786 host_set->ports[i] = ap;
4787 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4788 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4789 (ap->pio_mask << ATA_SHIFT_PIO);
4790
4791 /* print per-port info to dmesg */
4792 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4793 "bmdma 0x%lX irq %lu\n",
4794 ap->id,
4795 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4796 ata_mode_string(xfer_mode_mask),
4797 ap->ioaddr.cmd_addr,
4798 ap->ioaddr.ctl_addr,
4799 ap->ioaddr.bmdma_addr,
4800 ent->irq);
4801
4802 ata_chk_status(ap);
4803 host_set->ops->irq_clear(ap);
4804 count++;
4805 }
4806
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004807 if (!count)
4808 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
4810 /* obtain irq, that is shared between channels */
4811 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4812 DRV_NAME, host_set))
4813 goto err_out;
4814
4815 /* perform each probe synchronously */
4816 DPRINTK("probe begin\n");
4817 for (i = 0; i < count; i++) {
4818 struct ata_port *ap;
4819 int rc;
4820
4821 ap = host_set->ports[i];
4822
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004823 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004825 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
4827 if (rc) {
4828 /* FIXME: do something useful here?
4829 * Current libata behavior will
4830 * tear down everything when
4831 * the module is removed
4832 * or the h/w is unplugged.
4833 */
4834 }
4835
4836 rc = scsi_add_host(ap->host, dev);
4837 if (rc) {
4838 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4839 ap->id);
4840 /* FIXME: do something useful here */
4841 /* FIXME: handle unconditional calls to
4842 * scsi_scan_host and ata_host_remove, below,
4843 * at the very least
4844 */
4845 }
4846 }
4847
4848 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004849 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 for (i = 0; i < count; i++) {
4851 struct ata_port *ap = host_set->ports[i];
4852
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004853 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 }
4855
4856 dev_set_drvdata(dev, host_set);
4857
4858 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4859 return ent->n_ports; /* success */
4860
4861err_out:
4862 for (i = 0; i < count; i++) {
4863 ata_host_remove(host_set->ports[i], 1);
4864 scsi_host_put(host_set->ports[i]->host);
4865 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004866err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 kfree(host_set);
4868 VPRINTK("EXIT, returning 0\n");
4869 return 0;
4870}
4871
4872/**
Alan Cox17b14452005-09-15 15:44:00 +01004873 * ata_host_set_remove - PCI layer callback for device removal
4874 * @host_set: ATA host set that was removed
4875 *
4876 * Unregister all objects associated with this host set. Free those
4877 * objects.
4878 *
4879 * LOCKING:
4880 * Inherited from calling layer (may sleep).
4881 */
4882
Alan Cox17b14452005-09-15 15:44:00 +01004883void ata_host_set_remove(struct ata_host_set *host_set)
4884{
4885 struct ata_port *ap;
4886 unsigned int i;
4887
4888 for (i = 0; i < host_set->n_ports; i++) {
4889 ap = host_set->ports[i];
4890 scsi_remove_host(ap->host);
4891 }
4892
4893 free_irq(host_set->irq, host_set);
4894
4895 for (i = 0; i < host_set->n_ports; i++) {
4896 ap = host_set->ports[i];
4897
4898 ata_scsi_release(ap->host);
4899
4900 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4901 struct ata_ioports *ioaddr = &ap->ioaddr;
4902
4903 if (ioaddr->cmd_addr == 0x1f0)
4904 release_region(0x1f0, 8);
4905 else if (ioaddr->cmd_addr == 0x170)
4906 release_region(0x170, 8);
4907 }
4908
4909 scsi_host_put(ap->host);
4910 }
4911
4912 if (host_set->ops->host_stop)
4913 host_set->ops->host_stop(host_set);
4914
4915 kfree(host_set);
4916}
4917
4918/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 * ata_scsi_release - SCSI layer callback hook for host unload
4920 * @host: libata host to be unloaded
4921 *
4922 * Performs all duties necessary to shut down a libata port...
4923 * Kill port kthread, disable port, and release resources.
4924 *
4925 * LOCKING:
4926 * Inherited from SCSI layer.
4927 *
4928 * RETURNS:
4929 * One.
4930 */
4931
4932int ata_scsi_release(struct Scsi_Host *host)
4933{
4934 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004935 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
4937 DPRINTK("ENTER\n");
4938
4939 ap->ops->port_disable(ap);
4940 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004941 for (i = 0; i < ATA_MAX_DEVICES; i++)
4942 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
4944 DPRINTK("EXIT\n");
4945 return 1;
4946}
4947
4948/**
4949 * ata_std_ports - initialize ioaddr with standard port offsets.
4950 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004951 *
4952 * Utility function which initializes data_addr, error_addr,
4953 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4954 * device_addr, status_addr, and command_addr to standard offsets
4955 * relative to cmd_addr.
4956 *
4957 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 */
Edward Falk0baab862005-06-02 18:17:13 -04004959
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960void ata_std_ports(struct ata_ioports *ioaddr)
4961{
4962 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4963 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4964 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4965 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4966 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4967 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4968 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4969 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4970 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4971 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4972}
4973
Edward Falk0baab862005-06-02 18:17:13 -04004974
Jeff Garzik374b1872005-08-30 05:42:52 -04004975#ifdef CONFIG_PCI
4976
4977void ata_pci_host_stop (struct ata_host_set *host_set)
4978{
4979 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4980
4981 pci_iounmap(pdev, host_set->mmio_base);
4982}
4983
Edward Falk0baab862005-06-02 18:17:13 -04004984/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 * ata_pci_remove_one - PCI layer callback for device removal
4986 * @pdev: PCI device that was removed
4987 *
4988 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004989 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 * Handle this by unregistering all objects associated
4991 * with this PCI device. Free those objects. Then finally
4992 * release PCI resources and disable device.
4993 *
4994 * LOCKING:
4995 * Inherited from PCI layer (may sleep).
4996 */
4997
4998void ata_pci_remove_one (struct pci_dev *pdev)
4999{
5000 struct device *dev = pci_dev_to_dev(pdev);
5001 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Alan Cox17b14452005-09-15 15:44:00 +01005003 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 pci_release_regions(pdev);
5005 pci_disable_device(pdev);
5006 dev_set_drvdata(dev, NULL);
5007}
5008
5009/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005010int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011{
5012 unsigned long tmp = 0;
5013
5014 switch (bits->width) {
5015 case 1: {
5016 u8 tmp8 = 0;
5017 pci_read_config_byte(pdev, bits->reg, &tmp8);
5018 tmp = tmp8;
5019 break;
5020 }
5021 case 2: {
5022 u16 tmp16 = 0;
5023 pci_read_config_word(pdev, bits->reg, &tmp16);
5024 tmp = tmp16;
5025 break;
5026 }
5027 case 4: {
5028 u32 tmp32 = 0;
5029 pci_read_config_dword(pdev, bits->reg, &tmp32);
5030 tmp = tmp32;
5031 break;
5032 }
5033
5034 default:
5035 return -EINVAL;
5036 }
5037
5038 tmp &= bits->mask;
5039
5040 return (tmp == bits->val) ? 1 : 0;
5041}
Jens Axboe9b847542006-01-06 09:28:07 +01005042
5043int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5044{
5045 pci_save_state(pdev);
5046 pci_disable_device(pdev);
5047 pci_set_power_state(pdev, PCI_D3hot);
5048 return 0;
5049}
5050
5051int ata_pci_device_resume(struct pci_dev *pdev)
5052{
5053 pci_set_power_state(pdev, PCI_D0);
5054 pci_restore_state(pdev);
5055 pci_enable_device(pdev);
5056 pci_set_master(pdev);
5057 return 0;
5058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059#endif /* CONFIG_PCI */
5060
5061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062static int __init ata_init(void)
5063{
5064 ata_wq = create_workqueue("ata");
5065 if (!ata_wq)
5066 return -ENOMEM;
5067
5068 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5069 return 0;
5070}
5071
5072static void __exit ata_exit(void)
5073{
5074 destroy_workqueue(ata_wq);
5075}
5076
5077module_init(ata_init);
5078module_exit(ata_exit);
5079
Jeff Garzik67846b32005-10-05 02:58:32 -04005080static unsigned long ratelimit_time;
5081static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5082
5083int ata_ratelimit(void)
5084{
5085 int rc;
5086 unsigned long flags;
5087
5088 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5089
5090 if (time_after(jiffies, ratelimit_time)) {
5091 rc = 1;
5092 ratelimit_time = jiffies + (HZ/5);
5093 } else
5094 rc = 0;
5095
5096 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5097
5098 return rc;
5099}
5100
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101/*
5102 * libata is essentially a library of internal helper functions for
5103 * low-level ATA host controller drivers. As such, the API/ABI is
5104 * likely to change as new drivers are added and updated.
5105 * Do not depend on ABI/API stability.
5106 */
5107
5108EXPORT_SYMBOL_GPL(ata_std_bios_param);
5109EXPORT_SYMBOL_GPL(ata_std_ports);
5110EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005111EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112EXPORT_SYMBOL_GPL(ata_sg_init);
5113EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09005114EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5116EXPORT_SYMBOL_GPL(ata_eng_timeout);
5117EXPORT_SYMBOL_GPL(ata_tf_load);
5118EXPORT_SYMBOL_GPL(ata_tf_read);
5119EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5120EXPORT_SYMBOL_GPL(ata_std_dev_select);
5121EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5122EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5123EXPORT_SYMBOL_GPL(ata_check_status);
5124EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125EXPORT_SYMBOL_GPL(ata_exec_command);
5126EXPORT_SYMBOL_GPL(ata_port_start);
5127EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005128EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129EXPORT_SYMBOL_GPL(ata_interrupt);
5130EXPORT_SYMBOL_GPL(ata_qc_prep);
5131EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5132EXPORT_SYMBOL_GPL(ata_bmdma_start);
5133EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5134EXPORT_SYMBOL_GPL(ata_bmdma_status);
5135EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5136EXPORT_SYMBOL_GPL(ata_port_probe);
5137EXPORT_SYMBOL_GPL(sata_phy_reset);
5138EXPORT_SYMBOL_GPL(__sata_phy_reset);
5139EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005140EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005141EXPORT_SYMBOL_GPL(ata_std_softreset);
5142EXPORT_SYMBOL_GPL(sata_std_hardreset);
5143EXPORT_SYMBOL_GPL(ata_std_postreset);
5144EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005145EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09005146EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005148EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005149EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09005150EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5152EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09005153EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154EXPORT_SYMBOL_GPL(ata_scsi_error);
5155EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5156EXPORT_SYMBOL_GPL(ata_scsi_release);
5157EXPORT_SYMBOL_GPL(ata_host_intr);
5158EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09005159EXPORT_SYMBOL_GPL(ata_id_string);
5160EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005162EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5163EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005165EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005166EXPORT_SYMBOL_GPL(ata_timing_compute);
5167EXPORT_SYMBOL_GPL(ata_timing_merge);
5168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169#ifdef CONFIG_PCI
5170EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005171EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5173EXPORT_SYMBOL_GPL(ata_pci_init_one);
5174EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005175EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5176EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005178
5179EXPORT_SYMBOL_GPL(ata_device_suspend);
5180EXPORT_SYMBOL_GPL(ata_device_resume);
5181EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5182EXPORT_SYMBOL_GPL(ata_scsi_device_resume);