blob: c17df3f22fd15a3c420b1fce6908c510e3881119 [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 -070070
71static unsigned int ata_unique_id = 1;
72static struct workqueue_struct *ata_wq;
73
Jeff Garzik418dc1f2006-03-11 20:50:08 -050074int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040075module_param(atapi_enabled, int, 0444);
76MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
77
Jeff Garzikc3c013a2006-02-27 22:31:19 -050078int libata_fua = 0;
79module_param_named(fua, libata_fua, int, 0444);
80MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082MODULE_AUTHOR("Jeff Garzik");
83MODULE_DESCRIPTION("Library module for ATA devices");
84MODULE_LICENSE("GPL");
85MODULE_VERSION(DRV_VERSION);
86
Edward Falk0baab862005-06-02 18:17:13 -040087
88/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
90 * @tf: Taskfile to convert
91 * @fis: Buffer into which data will output
92 * @pmp: Port multiplier port
93 *
94 * Converts a standard ATA taskfile to a Serial ATA
95 * FIS structure (Register - Host to Device).
96 *
97 * LOCKING:
98 * Inherited from caller.
99 */
100
Jeff Garzik057ace52005-10-22 14:27:05 -0400101void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 fis[0] = 0x27; /* Register - Host to Device FIS */
104 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
105 bit 7 indicates Command FIS */
106 fis[2] = tf->command;
107 fis[3] = tf->feature;
108
109 fis[4] = tf->lbal;
110 fis[5] = tf->lbam;
111 fis[6] = tf->lbah;
112 fis[7] = tf->device;
113
114 fis[8] = tf->hob_lbal;
115 fis[9] = tf->hob_lbam;
116 fis[10] = tf->hob_lbah;
117 fis[11] = tf->hob_feature;
118
119 fis[12] = tf->nsect;
120 fis[13] = tf->hob_nsect;
121 fis[14] = 0;
122 fis[15] = tf->ctl;
123
124 fis[16] = 0;
125 fis[17] = 0;
126 fis[18] = 0;
127 fis[19] = 0;
128}
129
130/**
131 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
132 * @fis: Buffer from which data will be input
133 * @tf: Taskfile to output
134 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500135 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *
137 * LOCKING:
138 * Inherited from caller.
139 */
140
Jeff Garzik057ace52005-10-22 14:27:05 -0400141void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 tf->command = fis[2]; /* status */
144 tf->feature = fis[3]; /* error */
145
146 tf->lbal = fis[4];
147 tf->lbam = fis[5];
148 tf->lbah = fis[6];
149 tf->device = fis[7];
150
151 tf->hob_lbal = fis[8];
152 tf->hob_lbam = fis[9];
153 tf->hob_lbah = fis[10];
154
155 tf->nsect = fis[12];
156 tf->hob_nsect = fis[13];
157}
158
Albert Lee8cbd6df2005-10-12 15:06:27 +0800159static const u8 ata_rw_cmds[] = {
160 /* pio multi */
161 ATA_CMD_READ_MULTI,
162 ATA_CMD_WRITE_MULTI,
163 ATA_CMD_READ_MULTI_EXT,
164 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100165 0,
166 0,
167 0,
168 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800169 /* pio */
170 ATA_CMD_PIO_READ,
171 ATA_CMD_PIO_WRITE,
172 ATA_CMD_PIO_READ_EXT,
173 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100174 0,
175 0,
176 0,
177 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800178 /* dma */
179 ATA_CMD_READ,
180 ATA_CMD_WRITE,
181 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100182 ATA_CMD_WRITE_EXT,
183 0,
184 0,
185 0,
186 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800187};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800190 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
191 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800193 * Examine the device configuration and tf->flags to calculate
194 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * LOCKING:
197 * caller.
198 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100199int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800201 struct ata_taskfile *tf = &qc->tf;
202 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100203 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100205 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800206
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100207 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800208 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
209 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Albert Lee8cbd6df2005-10-12 15:06:27 +0800211 if (dev->flags & ATA_DFLAG_PIO) {
212 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100213 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000214 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
215 /* Unable to use DMA due to host limitation */
216 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800217 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800218 } else {
219 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100220 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100223 cmd = ata_rw_cmds[index + fua + lba48 + write];
224 if (cmd) {
225 tf->command = cmd;
226 return 0;
227 }
228 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Tejun Heocb95d562006-03-06 04:31:56 +0900231/**
232 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
233 * @pio_mask: pio_mask
234 * @mwdma_mask: mwdma_mask
235 * @udma_mask: udma_mask
236 *
237 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
238 * unsigned int xfer_mask.
239 *
240 * LOCKING:
241 * None.
242 *
243 * RETURNS:
244 * Packed xfer_mask.
245 */
246static unsigned int ata_pack_xfermask(unsigned int pio_mask,
247 unsigned int mwdma_mask,
248 unsigned int udma_mask)
249{
250 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
251 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
252 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
253}
254
255static const struct ata_xfer_ent {
256 unsigned int shift, bits;
257 u8 base;
258} ata_xfer_tbl[] = {
259 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
260 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
261 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
262 { -1, },
263};
264
265/**
266 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
267 * @xfer_mask: xfer_mask of interest
268 *
269 * Return matching XFER_* value for @xfer_mask. Only the highest
270 * bit of @xfer_mask is considered.
271 *
272 * LOCKING:
273 * None.
274 *
275 * RETURNS:
276 * Matching XFER_* value, 0 if no match found.
277 */
278static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
279{
280 int highbit = fls(xfer_mask) - 1;
281 const struct ata_xfer_ent *ent;
282
283 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
284 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
285 return ent->base + highbit - ent->shift;
286 return 0;
287}
288
289/**
290 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
291 * @xfer_mode: XFER_* of interest
292 *
293 * Return matching xfer_mask for @xfer_mode.
294 *
295 * LOCKING:
296 * None.
297 *
298 * RETURNS:
299 * Matching xfer_mask, 0 if no match found.
300 */
301static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
302{
303 const struct ata_xfer_ent *ent;
304
305 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
306 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
307 return 1 << (ent->shift + xfer_mode - ent->base);
308 return 0;
309}
310
311/**
312 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
313 * @xfer_mode: XFER_* of interest
314 *
315 * Return matching xfer_shift for @xfer_mode.
316 *
317 * LOCKING:
318 * None.
319 *
320 * RETURNS:
321 * Matching xfer_shift, -1 if no match found.
322 */
323static int ata_xfer_mode2shift(unsigned int xfer_mode)
324{
325 const struct ata_xfer_ent *ent;
326
327 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
328 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
329 return ent->shift;
330 return -1;
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900334 * ata_mode_string - convert xfer_mask to string
335 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
337 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900338 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 *
340 * LOCKING:
341 * None.
342 *
343 * RETURNS:
344 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900345 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900347static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Tejun Heo75f554b2006-03-06 04:31:57 +0900349 static const char * const xfer_mode_str[] = {
350 "PIO0",
351 "PIO1",
352 "PIO2",
353 "PIO3",
354 "PIO4",
355 "MWDMA0",
356 "MWDMA1",
357 "MWDMA2",
358 "UDMA/16",
359 "UDMA/25",
360 "UDMA/33",
361 "UDMA/44",
362 "UDMA/66",
363 "UDMA/100",
364 "UDMA/133",
365 "UDMA7",
366 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900367 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900369 highbit = fls(xfer_mask) - 1;
370 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
371 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/**
376 * ata_pio_devchk - PATA device presence detection
377 * @ap: ATA channel to examine
378 * @device: Device to examine (starting at zero)
379 *
380 * This technique was originally described in
381 * Hale Landis's ATADRVR (www.ata-atapi.com), and
382 * later found its way into the ATA/ATAPI spec.
383 *
384 * Write a pattern to the ATA shadow registers,
385 * and if a device is present, it will respond by
386 * correctly storing and echoing back the
387 * ATA shadow register contents.
388 *
389 * LOCKING:
390 * caller.
391 */
392
393static unsigned int ata_pio_devchk(struct ata_port *ap,
394 unsigned int device)
395{
396 struct ata_ioports *ioaddr = &ap->ioaddr;
397 u8 nsect, lbal;
398
399 ap->ops->dev_select(ap, device);
400
401 outb(0x55, ioaddr->nsect_addr);
402 outb(0xaa, ioaddr->lbal_addr);
403
404 outb(0xaa, ioaddr->nsect_addr);
405 outb(0x55, ioaddr->lbal_addr);
406
407 outb(0x55, ioaddr->nsect_addr);
408 outb(0xaa, ioaddr->lbal_addr);
409
410 nsect = inb(ioaddr->nsect_addr);
411 lbal = inb(ioaddr->lbal_addr);
412
413 if ((nsect == 0x55) && (lbal == 0xaa))
414 return 1; /* we found a device */
415
416 return 0; /* nothing found */
417}
418
419/**
420 * ata_mmio_devchk - PATA device presence detection
421 * @ap: ATA channel to examine
422 * @device: Device to examine (starting at zero)
423 *
424 * This technique was originally described in
425 * Hale Landis's ATADRVR (www.ata-atapi.com), and
426 * later found its way into the ATA/ATAPI spec.
427 *
428 * Write a pattern to the ATA shadow registers,
429 * and if a device is present, it will respond by
430 * correctly storing and echoing back the
431 * ATA shadow register contents.
432 *
433 * LOCKING:
434 * caller.
435 */
436
437static unsigned int ata_mmio_devchk(struct ata_port *ap,
438 unsigned int device)
439{
440 struct ata_ioports *ioaddr = &ap->ioaddr;
441 u8 nsect, lbal;
442
443 ap->ops->dev_select(ap, device);
444
445 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
446 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
447
448 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
449 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
450
451 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
452 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
453
454 nsect = readb((void __iomem *) ioaddr->nsect_addr);
455 lbal = readb((void __iomem *) ioaddr->lbal_addr);
456
457 if ((nsect == 0x55) && (lbal == 0xaa))
458 return 1; /* we found a device */
459
460 return 0; /* nothing found */
461}
462
463/**
464 * ata_devchk - PATA device presence detection
465 * @ap: ATA channel to examine
466 * @device: Device to examine (starting at zero)
467 *
468 * Dispatch ATA device presence detection, depending
469 * on whether we are using PIO or MMIO to talk to the
470 * ATA shadow registers.
471 *
472 * LOCKING:
473 * caller.
474 */
475
476static unsigned int ata_devchk(struct ata_port *ap,
477 unsigned int device)
478{
479 if (ap->flags & ATA_FLAG_MMIO)
480 return ata_mmio_devchk(ap, device);
481 return ata_pio_devchk(ap, device);
482}
483
484/**
485 * ata_dev_classify - determine device type based on ATA-spec signature
486 * @tf: ATA taskfile register set for device to be identified
487 *
488 * Determine from taskfile register contents whether a device is
489 * ATA or ATAPI, as per "Signature and persistence" section
490 * of ATA/PI spec (volume 1, sect 5.14).
491 *
492 * LOCKING:
493 * None.
494 *
495 * RETURNS:
496 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
497 * the event of failure.
498 */
499
Jeff Garzik057ace52005-10-22 14:27:05 -0400500unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 /* Apple's open source Darwin code hints that some devices only
503 * put a proper signature into the LBA mid/high registers,
504 * So, we only check those. It's sufficient for uniqueness.
505 */
506
507 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
508 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
509 DPRINTK("found ATA device by sig\n");
510 return ATA_DEV_ATA;
511 }
512
513 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
514 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
515 DPRINTK("found ATAPI device by sig\n");
516 return ATA_DEV_ATAPI;
517 }
518
519 DPRINTK("unknown device\n");
520 return ATA_DEV_UNKNOWN;
521}
522
523/**
524 * ata_dev_try_classify - Parse returned ATA device signature
525 * @ap: ATA channel to examine
526 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900527 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *
529 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
530 * an ATA/ATAPI-defined set of values is placed in the ATA
531 * shadow registers, indicating the results of device detection
532 * and diagnostics.
533 *
534 * Select the ATA device, and read the values from the ATA shadow
535 * registers. Then parse according to the Error register value,
536 * and the spec-defined values examined by ata_dev_classify().
537 *
538 * LOCKING:
539 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900540 *
541 * RETURNS:
542 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
544
Tejun Heob4dc7622006-01-24 17:05:22 +0900545static unsigned int
546ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct ata_taskfile tf;
549 unsigned int class;
550 u8 err;
551
552 ap->ops->dev_select(ap, device);
553
554 memset(&tf, 0, sizeof(tf));
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400557 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900558 if (r_err)
559 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* see if device passed diags */
562 if (err == 1)
563 /* do nothing */ ;
564 else if ((device == 0) && (err == 0x81))
565 /* do nothing */ ;
566 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900567 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Tejun Heob4dc7622006-01-24 17:05:22 +0900569 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900573 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900575 return ATA_DEV_NONE;
576 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900580 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * @id: IDENTIFY DEVICE results we will examine
582 * @s: string into which data is output
583 * @ofs: offset into identify device page
584 * @len: length of string to return. must be an even number.
585 *
586 * The strings in the IDENTIFY DEVICE page are broken up into
587 * 16-bit chunks. Run through the string, and output each
588 * 8-bit chunk linearly, regardless of platform.
589 *
590 * LOCKING:
591 * caller.
592 */
593
Tejun Heo6a62a042006-02-13 10:02:46 +0900594void ata_id_string(const u16 *id, unsigned char *s,
595 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 unsigned int c;
598
599 while (len > 0) {
600 c = id[ofs] >> 8;
601 *s = c;
602 s++;
603
604 c = id[ofs] & 0xff;
605 *s = c;
606 s++;
607
608 ofs++;
609 len -= 2;
610 }
611}
612
Tejun Heo0e949ff2006-02-12 22:47:04 +0900613/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900614 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900615 * @id: IDENTIFY DEVICE results we will examine
616 * @s: string into which data is output
617 * @ofs: offset into identify device page
618 * @len: length of string to return. must be an odd number.
619 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900620 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900621 * trims trailing spaces and terminates the resulting string with
622 * null. @len must be actual maximum length (even number) + 1.
623 *
624 * LOCKING:
625 * caller.
626 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900627void ata_id_c_string(const u16 *id, unsigned char *s,
628 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900629{
630 unsigned char *p;
631
632 WARN_ON(!(len & 1));
633
Tejun Heo6a62a042006-02-13 10:02:46 +0900634 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900635
636 p = s + strnlen(s, len - 1);
637 while (p > s && p[-1] == ' ')
638 p--;
639 *p = '\0';
640}
Edward Falk0baab862005-06-02 18:17:13 -0400641
Tejun Heo29407402006-02-12 22:47:04 +0900642static u64 ata_id_n_sectors(const u16 *id)
643{
644 if (ata_id_has_lba(id)) {
645 if (ata_id_has_lba48(id))
646 return ata_id_u64(id, 100);
647 else
648 return ata_id_u32(id, 60);
649 } else {
650 if (ata_id_current_chs_valid(id))
651 return ata_id_u32(id, 57);
652 else
653 return id[1] * id[3] * id[6];
654 }
655}
656
Edward Falk0baab862005-06-02 18:17:13 -0400657/**
658 * ata_noop_dev_select - Select device 0/1 on ATA bus
659 * @ap: ATA channel to manipulate
660 * @device: ATA device (numbered from zero) to select
661 *
662 * This function performs no actual function.
663 *
664 * May be used as the dev_select() entry in ata_port_operations.
665 *
666 * LOCKING:
667 * caller.
668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
670{
671}
672
Edward Falk0baab862005-06-02 18:17:13 -0400673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/**
675 * ata_std_dev_select - Select device 0/1 on ATA bus
676 * @ap: ATA channel to manipulate
677 * @device: ATA device (numbered from zero) to select
678 *
679 * Use the method defined in the ATA specification to
680 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400681 * ATA channel. Works with both PIO and MMIO.
682 *
683 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *
685 * LOCKING:
686 * caller.
687 */
688
689void ata_std_dev_select (struct ata_port *ap, unsigned int device)
690{
691 u8 tmp;
692
693 if (device == 0)
694 tmp = ATA_DEVICE_OBS;
695 else
696 tmp = ATA_DEVICE_OBS | ATA_DEV1;
697
698 if (ap->flags & ATA_FLAG_MMIO) {
699 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
700 } else {
701 outb(tmp, ap->ioaddr.device_addr);
702 }
703 ata_pause(ap); /* needed; also flushes, for mmio */
704}
705
706/**
707 * ata_dev_select - Select device 0/1 on ATA bus
708 * @ap: ATA channel to manipulate
709 * @device: ATA device (numbered from zero) to select
710 * @wait: non-zero to wait for Status register BSY bit to clear
711 * @can_sleep: non-zero if context allows sleeping
712 *
713 * Use the method defined in the ATA specification to
714 * make either device 0, or device 1, active on the
715 * ATA channel.
716 *
717 * This is a high-level version of ata_std_dev_select(),
718 * which additionally provides the services of inserting
719 * the proper pauses and status polling, where needed.
720 *
721 * LOCKING:
722 * caller.
723 */
724
725void ata_dev_select(struct ata_port *ap, unsigned int device,
726 unsigned int wait, unsigned int can_sleep)
727{
728 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
729 ap->id, device, wait);
730
731 if (wait)
732 ata_wait_idle(ap);
733
734 ap->ops->dev_select(ap, device);
735
736 if (wait) {
737 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
738 msleep(150);
739 ata_wait_idle(ap);
740 }
741}
742
743/**
744 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900745 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900747 * Dump selected 16-bit words from the given IDENTIFY DEVICE
748 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 *
750 * LOCKING:
751 * caller.
752 */
753
Tejun Heo0bd33002006-02-12 22:47:05 +0900754static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 DPRINTK("49==0x%04x "
757 "53==0x%04x "
758 "63==0x%04x "
759 "64==0x%04x "
760 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900761 id[49],
762 id[53],
763 id[63],
764 id[64],
765 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 DPRINTK("80==0x%04x "
767 "81==0x%04x "
768 "82==0x%04x "
769 "83==0x%04x "
770 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900771 id[80],
772 id[81],
773 id[82],
774 id[83],
775 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 DPRINTK("88==0x%04x "
777 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900778 id[88],
779 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Tejun Heocb95d562006-03-06 04:31:56 +0900782/**
783 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
784 * @id: IDENTIFY data to compute xfer mask from
785 *
786 * Compute the xfermask for this device. This is not as trivial
787 * as it seems if we must consider early devices correctly.
788 *
789 * FIXME: pre IDE drive timing (do we care ?).
790 *
791 * LOCKING:
792 * None.
793 *
794 * RETURNS:
795 * Computed xfermask
796 */
797static unsigned int ata_id_xfermask(const u16 *id)
798{
799 unsigned int pio_mask, mwdma_mask, udma_mask;
800
801 /* Usual case. Word 53 indicates word 64 is valid */
802 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
803 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
804 pio_mask <<= 3;
805 pio_mask |= 0x7;
806 } else {
807 /* If word 64 isn't valid then Word 51 high byte holds
808 * the PIO timing number for the maximum. Turn it into
809 * a mask.
810 */
811 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
812
813 /* But wait.. there's more. Design your standards by
814 * committee and you too can get a free iordy field to
815 * process. However its the speeds not the modes that
816 * are supported... Note drivers using the timing API
817 * will get this right anyway
818 */
819 }
820
821 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
822 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
823
824 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
825}
826
Tejun Heo86e45b62006-03-05 15:29:09 +0900827/**
828 * ata_port_queue_task - Queue port_task
829 * @ap: The ata_port to queue port_task for
830 *
831 * Schedule @fn(@data) for execution after @delay jiffies using
832 * port_task. There is one port_task per port and it's the
833 * user(low level driver)'s responsibility to make sure that only
834 * one task is active at any given time.
835 *
836 * libata core layer takes care of synchronization between
837 * port_task and EH. ata_port_queue_task() may be ignored for EH
838 * synchronization.
839 *
840 * LOCKING:
841 * Inherited from caller.
842 */
843void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
844 unsigned long delay)
845{
846 int rc;
847
Tejun Heo2e755f62006-03-05 15:29:09 +0900848 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900849 return;
850
851 PREPARE_WORK(&ap->port_task, fn, data);
852
853 if (!delay)
854 rc = queue_work(ata_wq, &ap->port_task);
855 else
856 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
857
858 /* rc == 0 means that another user is using port task */
859 WARN_ON(rc == 0);
860}
861
862/**
863 * ata_port_flush_task - Flush port_task
864 * @ap: The ata_port to flush port_task for
865 *
866 * After this function completes, port_task is guranteed not to
867 * be running or scheduled.
868 *
869 * LOCKING:
870 * Kernel thread context (may sleep)
871 */
872void ata_port_flush_task(struct ata_port *ap)
873{
874 unsigned long flags;
875
876 DPRINTK("ENTER\n");
877
878 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900879 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900880 spin_unlock_irqrestore(&ap->host_set->lock, flags);
881
882 DPRINTK("flush #1\n");
883 flush_workqueue(ata_wq);
884
885 /*
886 * At this point, if a task is running, it's guaranteed to see
887 * the FLUSH flag; thus, it will never queue pio tasks again.
888 * Cancel and flush.
889 */
890 if (!cancel_delayed_work(&ap->port_task)) {
891 DPRINTK("flush #2\n");
892 flush_workqueue(ata_wq);
893 }
894
895 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900896 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900897 spin_unlock_irqrestore(&ap->host_set->lock, flags);
898
899 DPRINTK("EXIT\n");
900}
901
Tejun Heo77853bf2006-01-23 13:09:36 +0900902void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900903{
Tejun Heo77853bf2006-01-23 13:09:36 +0900904 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900905
Tejun Heo77853bf2006-01-23 13:09:36 +0900906 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900907 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900908}
909
910/**
911 * ata_exec_internal - execute libata internal command
912 * @ap: Port to which the command is sent
913 * @dev: Device to which the command is sent
914 * @tf: Taskfile registers for the command and the result
915 * @dma_dir: Data tranfer direction of the command
916 * @buf: Data buffer of the command
917 * @buflen: Length of data buffer
918 *
919 * Executes libata internal command with timeout. @tf contains
920 * command on entry and result on return. Timeout and error
921 * conditions are reported via return value. No recovery action
922 * is taken after a command times out. It's caller's duty to
923 * clean up after timeout.
924 *
925 * LOCKING:
926 * None. Should be called with kernel context, might sleep.
927 */
928
929static unsigned
930ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
931 struct ata_taskfile *tf,
932 int dma_dir, void *buf, unsigned int buflen)
933{
934 u8 command = tf->command;
935 struct ata_queued_cmd *qc;
936 DECLARE_COMPLETION(wait);
937 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900938 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900939
940 spin_lock_irqsave(&ap->host_set->lock, flags);
941
942 qc = ata_qc_new_init(ap, dev);
943 BUG_ON(qc == NULL);
944
945 qc->tf = *tf;
946 qc->dma_dir = dma_dir;
947 if (dma_dir != DMA_NONE) {
948 ata_sg_init_one(qc, buf, buflen);
949 qc->nsect = buflen / ATA_SECT_SIZE;
950 }
951
Tejun Heo77853bf2006-01-23 13:09:36 +0900952 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900953 qc->complete_fn = ata_qc_complete_internal;
954
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900955 qc->err_mask = ata_qc_issue(qc);
956 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900957 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900958
959 spin_unlock_irqrestore(&ap->host_set->lock, flags);
960
961 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
962 spin_lock_irqsave(&ap->host_set->lock, flags);
963
964 /* We're racing with irq here. If we lose, the
965 * following test prevents us from completing the qc
966 * again. If completion irq occurs after here but
967 * before the caller cleans up, it will result in a
968 * spurious interrupt. We can live with that.
969 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900970 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900971 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900972 ata_qc_complete(qc);
973 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
974 ap->id, command);
975 }
976
977 spin_unlock_irqrestore(&ap->host_set->lock, flags);
978 }
979
Tejun Heo77853bf2006-01-23 13:09:36 +0900980 *tf = qc->tf;
981 err_mask = qc->err_mask;
982
983 ata_qc_free(qc);
984
985 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +0000989 * ata_pio_need_iordy - check if iordy needed
990 * @adev: ATA device
991 *
992 * Check if the current speed of the device requires IORDY. Used
993 * by various controllers for chip configuration.
994 */
995
996unsigned int ata_pio_need_iordy(const struct ata_device *adev)
997{
998 int pio;
999 int speed = adev->pio_mode - XFER_PIO_0;
1000
1001 if (speed < 2)
1002 return 0;
1003 if (speed > 2)
1004 return 1;
1005
1006 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1007
1008 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1009 pio = adev->id[ATA_ID_EIDE_PIO];
1010 /* Is the speed faster than the drive allows non IORDY ? */
1011 if (pio) {
1012 /* This is cycle times not frequency - watch the logic! */
1013 if (pio > 240) /* PIO2 is 240nS per cycle */
1014 return 1;
1015 return 0;
1016 }
1017 }
1018 return 0;
1019}
1020
1021/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001022 * ata_dev_read_id - Read ID data from the specified device
1023 * @ap: port on which target device resides
1024 * @dev: target device
1025 * @p_class: pointer to class of the target device (may be changed)
1026 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +09001027 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +09001028 *
1029 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1030 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1031 * devices. This function also takes care of EDD signature
1032 * misreporting (to be removed once EDD support is gone) and
1033 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
1034 *
1035 * LOCKING:
1036 * Kernel thread context (may sleep)
1037 *
1038 * RETURNS:
1039 * 0 on success, -errno otherwise.
1040 */
1041static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +09001042 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001043{
1044 unsigned int class = *p_class;
1045 unsigned int using_edd;
1046 struct ata_taskfile tf;
1047 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +09001048 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001049 const char *reason;
1050 int rc;
1051
1052 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1053
1054 if (ap->ops->probe_reset ||
1055 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1056 using_edd = 0;
1057 else
1058 using_edd = 1;
1059
1060 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1061
Tejun Heod9572b12006-03-01 16:09:35 +09001062 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1063 if (id == NULL) {
1064 rc = -ENOMEM;
1065 reason = "out of memory";
1066 goto err_out;
1067 }
1068
Tejun Heo49016ac2006-02-21 02:12:11 +09001069 retry:
1070 ata_tf_init(ap, &tf, dev->devno);
1071
1072 switch (class) {
1073 case ATA_DEV_ATA:
1074 tf.command = ATA_CMD_ID_ATA;
1075 break;
1076 case ATA_DEV_ATAPI:
1077 tf.command = ATA_CMD_ID_ATAPI;
1078 break;
1079 default:
1080 rc = -ENODEV;
1081 reason = "unsupported class";
1082 goto err_out;
1083 }
1084
1085 tf.protocol = ATA_PROT_PIO;
1086
1087 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1088 id, sizeof(id[0]) * ATA_ID_WORDS);
1089
1090 if (err_mask) {
1091 rc = -EIO;
1092 reason = "I/O error";
1093
1094 if (err_mask & ~AC_ERR_DEV)
1095 goto err_out;
1096
1097 /*
1098 * arg! EDD works for all test cases, but seems to return
1099 * the ATA signature for some ATAPI devices. Until the
1100 * reason for this is found and fixed, we fix up the mess
1101 * here. If IDENTIFY DEVICE returns command aborted
1102 * (as ATAPI devices do), then we issue an
1103 * IDENTIFY PACKET DEVICE.
1104 *
1105 * ATA software reset (SRST, the default) does not appear
1106 * to have this problem.
1107 */
1108 if ((using_edd) && (class == ATA_DEV_ATA)) {
1109 u8 err = tf.feature;
1110 if (err & ATA_ABORTED) {
1111 class = ATA_DEV_ATAPI;
1112 goto retry;
1113 }
1114 }
1115 goto err_out;
1116 }
1117
1118 swap_buf_le16(id, ATA_ID_WORDS);
1119
1120 /* print device capabilities */
1121 printk(KERN_DEBUG "ata%u: dev %u cfg "
1122 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1123 ap->id, dev->devno,
1124 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1125
1126 /* sanity check */
1127 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1128 rc = -EINVAL;
1129 reason = "device reports illegal type";
1130 goto err_out;
1131 }
1132
1133 if (post_reset && class == ATA_DEV_ATA) {
1134 /*
1135 * The exact sequence expected by certain pre-ATA4 drives is:
1136 * SRST RESET
1137 * IDENTIFY
1138 * INITIALIZE DEVICE PARAMETERS
1139 * anything else..
1140 * Some drives were very specific about that exact sequence.
1141 */
1142 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1143 err_mask = ata_dev_init_params(ap, dev);
1144 if (err_mask) {
1145 rc = -EIO;
1146 reason = "INIT_DEV_PARAMS failed";
1147 goto err_out;
1148 }
1149
1150 /* current CHS translation info (id[53-58]) might be
1151 * changed. reread the identify device info.
1152 */
1153 post_reset = 0;
1154 goto retry;
1155 }
1156 }
1157
1158 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001159 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001160 return 0;
1161
1162 err_out:
1163 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1164 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001165 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001166 return rc;
1167}
1168
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001169static inline u8 ata_dev_knobble(const struct ata_port *ap,
1170 struct ata_device *dev)
1171{
1172 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1173}
1174
Tejun Heo49016ac2006-02-21 02:12:11 +09001175/**
Tejun Heoffeae412006-03-01 16:09:35 +09001176 * ata_dev_configure - Configure the specified ATA/ATAPI device
1177 * @ap: Port on which target device resides
1178 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001179 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 *
Tejun Heoffeae412006-03-01 16:09:35 +09001181 * Configure @dev according to @dev->id. Generic and low-level
1182 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 *
1184 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001185 * Kernel thread context (may sleep)
1186 *
1187 * RETURNS:
1188 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001190static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1191 int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Tejun Heoff8854b2006-03-06 04:31:56 +09001193 unsigned int xfer_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001194 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (!ata_dev_present(dev)) {
1197 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001198 ap->id, dev->devno);
1199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
Tejun Heoffeae412006-03-01 16:09:35 +09001202 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Tejun Heo208a9932006-03-05 17:55:58 +09001204 /* initialize to-be-configured parameters */
1205 dev->flags = 0;
1206 dev->max_sectors = 0;
1207 dev->cdb_len = 0;
1208 dev->n_sectors = 0;
1209 dev->cylinders = 0;
1210 dev->heads = 0;
1211 dev->sectors = 0;
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /*
1214 * common ATA, ATAPI feature tests
1215 */
1216
Albert Lee8bf62ece2005-05-12 15:29:42 -04001217 /* we require DMA support (bits 8 of word 49) */
1218 if (!ata_id_has_dma(dev->id)) {
1219 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001220 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto err_out_nosup;
1222 }
1223
Tejun Heoff8854b2006-03-06 04:31:56 +09001224 /* find max transfer mode; for printk only */
1225 xfer_mask = ata_id_xfermask(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Tejun Heo0bd33002006-02-12 22:47:05 +09001227 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 /* ATA-specific feature tests */
1230 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001231 dev->n_sectors = ata_id_n_sectors(dev->id);
1232
Albert Lee8bf62ece2005-05-12 15:29:42 -04001233 if (ata_id_has_lba(dev->id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001234 const char *lba_desc;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001235
Tejun Heo4c2d7212006-03-05 17:55:58 +09001236 lba_desc = "LBA";
1237 dev->flags |= ATA_DFLAG_LBA;
1238 if (ata_id_has_lba48(dev->id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001239 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001240 lba_desc = "LBA48";
1241 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04001242
1243 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001244 if (print_info)
1245 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1246 "max %s, %Lu sectors: %s\n",
1247 ap->id, dev->devno,
1248 ata_id_major_version(dev->id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001249 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001250 (unsigned long long)dev->n_sectors,
1251 lba_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001252 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001253 /* CHS */
1254
1255 /* Default translation */
1256 dev->cylinders = dev->id[1];
1257 dev->heads = dev->id[3];
1258 dev->sectors = dev->id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001259
1260 if (ata_id_current_chs_valid(dev->id)) {
1261 /* Current CHS translation is valid. */
1262 dev->cylinders = dev->id[54];
1263 dev->heads = dev->id[55];
1264 dev->sectors = dev->id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001265 }
1266
1267 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001268 if (print_info)
1269 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1270 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1271 ap->id, dev->devno,
1272 ata_id_major_version(dev->id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001273 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001274 (unsigned long long)dev->n_sectors,
1275 dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277
Tejun Heo6e7846e2006-02-12 23:32:58 +09001278 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
1281 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001282 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 rc = atapi_cdb_len(dev->id);
1284 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1285 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001286 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 goto err_out_nosup;
1288 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001289 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001292 if (print_info)
1293 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoff8854b2006-03-06 04:31:56 +09001294 ap->id, dev->devno, ata_mode_string(xfer_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
Tejun Heo6e7846e2006-02-12 23:32:58 +09001297 ap->host->max_cmd_len = 0;
1298 for (i = 0; i < ATA_MAX_DEVICES; i++)
1299 ap->host->max_cmd_len = max_t(unsigned int,
1300 ap->host->max_cmd_len,
1301 ap->device[i].cdb_len);
1302
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001303 /* limit bridge transfers to udma5, 200 sectors */
1304 if (ata_dev_knobble(ap, dev)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001305 if (print_info)
1306 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1307 ap->id, dev->devno);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001308 ap->udma_mask &= ATA_UDMA5;
1309 dev->max_sectors = ATA_MAX_SECTORS;
1310 }
1311
1312 if (ap->ops->dev_config)
1313 ap->ops->dev_config(ap, dev);
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318err_out_nosup:
1319 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001320 ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001322 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325/**
1326 * ata_bus_probe - Reset and probe ATA bus
1327 * @ap: Bus to probe
1328 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001329 * Master ATA bus probing function. Initiates a hardware-dependent
1330 * bus reset, then attempts to identify any devices found on
1331 * the bus.
1332 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001334 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 *
1336 * RETURNS:
1337 * Zero on success, non-zero on error.
1338 */
1339
1340static int ata_bus_probe(struct ata_port *ap)
1341{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001342 unsigned int classes[ATA_MAX_DEVICES];
1343 unsigned int i, rc, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Tejun Heo28ca5c52006-03-01 16:09:36 +09001345 ata_port_probe(ap);
1346
Tejun Heo20444702006-03-13 01:57:01 +09001347 /* reset and determine device classes */
1348 for (i = 0; i < ATA_MAX_DEVICES; i++)
1349 classes[i] = ATA_DEV_UNKNOWN;
Tejun Heo2061a472006-03-12 00:57:39 +09001350
Tejun Heo20444702006-03-13 01:57:01 +09001351 if (ap->ops->probe_reset) {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001352 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001353 if (rc) {
1354 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1355 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001356 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001357 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001358 ap->ops->phy_reset(ap);
1359
Tejun Heo20444702006-03-13 01:57:01 +09001360 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1361 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo28ca5c52006-03-01 16:09:36 +09001362 classes[i] = ap->device[i].class;
Tejun Heo20444702006-03-13 01:57:01 +09001363
Tejun Heo28ca5c52006-03-01 16:09:36 +09001364 ata_port_probe(ap);
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Tejun Heo20444702006-03-13 01:57:01 +09001367 for (i = 0; i < ATA_MAX_DEVICES; i++)
1368 if (classes[i] == ATA_DEV_UNKNOWN)
1369 classes[i] = ATA_DEV_NONE;
1370
Tejun Heo28ca5c52006-03-01 16:09:36 +09001371 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoffeae412006-03-01 16:09:35 +09001373 struct ata_device *dev = &ap->device[i];
1374
Tejun Heo28ca5c52006-03-01 16:09:36 +09001375 dev->class = classes[i];
1376
Tejun Heoffeae412006-03-01 16:09:35 +09001377 if (!ata_dev_present(dev))
1378 continue;
1379
1380 WARN_ON(dev->id != NULL);
1381 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1382 dev->class = ATA_DEV_NONE;
1383 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Tejun Heoffeae412006-03-01 16:09:35 +09001385
Tejun Heo4c2d7212006-03-05 17:55:58 +09001386 if (ata_dev_configure(ap, dev, 1)) {
Tejun Heoffeae412006-03-01 16:09:35 +09001387 dev->class++; /* disable device */
1388 continue;
1389 }
1390
Tejun Heoffeae412006-03-01 16:09:35 +09001391 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Tejun Heo28ca5c52006-03-01 16:09:36 +09001394 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 goto err_out_disable;
1396
1397 ata_set_mode(ap);
1398 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1399 goto err_out_disable;
1400
1401 return 0;
1402
1403err_out_disable:
1404 ap->ops->port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return -1;
1406}
1407
1408/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001409 * ata_port_probe - Mark port as enabled
1410 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001412 * Modify @ap data structure such that the system
1413 * thinks that the entire port is enabled.
1414 *
1415 * LOCKING: host_set lock, or some other form of
1416 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
1418
1419void ata_port_probe(struct ata_port *ap)
1420{
1421 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1422}
1423
1424/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001425 * sata_print_link_status - Print SATA link status
1426 * @ap: SATA port to printk link status about
1427 *
1428 * This function prints link speed and status of a SATA link.
1429 *
1430 * LOCKING:
1431 * None.
1432 */
1433static void sata_print_link_status(struct ata_port *ap)
1434{
1435 u32 sstatus, tmp;
1436 const char *speed;
1437
1438 if (!ap->ops->scr_read)
1439 return;
1440
1441 sstatus = scr_read(ap, SCR_STATUS);
1442
1443 if (sata_dev_present(ap)) {
1444 tmp = (sstatus >> 4) & 0xf;
1445 if (tmp & (1 << 0))
1446 speed = "1.5";
1447 else if (tmp & (1 << 1))
1448 speed = "3.0";
1449 else
1450 speed = "<unknown>";
1451 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1452 ap->id, speed, sstatus);
1453 } else {
1454 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1455 ap->id, sstatus);
1456 }
1457}
1458
1459/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001460 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1461 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001463 * This function issues commands to standard SATA Sxxx
1464 * PHY registers, to wake up the phy (and device), and
1465 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
1467 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001468 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 *
1470 */
1471void __sata_phy_reset(struct ata_port *ap)
1472{
1473 u32 sstatus;
1474 unsigned long timeout = jiffies + (HZ * 5);
1475
1476 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001477 /* issue phy wake/reset */
1478 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001479 /* Couldn't find anything in SATA I/II specs, but
1480 * AHCI-1.1 10.4.2 says at least 1 ms. */
1481 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001483 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 /* wait for phy to become ready, if necessary */
1486 do {
1487 msleep(200);
1488 sstatus = scr_read(ap, SCR_STATUS);
1489 if ((sstatus & 0xf) != 1)
1490 break;
1491 } while (time_before(jiffies, timeout));
1492
Tejun Heo3be680b2005-12-19 22:35:02 +09001493 /* print link status */
1494 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001495
Tejun Heo3be680b2005-12-19 22:35:02 +09001496 /* TODO: phy layer with polling, timeouts, etc. */
1497 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001499 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1503 return;
1504
1505 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1506 ata_port_disable(ap);
1507 return;
1508 }
1509
1510 ap->cbl = ATA_CBL_SATA;
1511}
1512
1513/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001514 * sata_phy_reset - Reset SATA bus.
1515 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001517 * This function resets the SATA bus, and then probes
1518 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 *
1520 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001521 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 *
1523 */
1524void sata_phy_reset(struct ata_port *ap)
1525{
1526 __sata_phy_reset(ap);
1527 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1528 return;
1529 ata_bus_reset(ap);
1530}
1531
1532/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001533 * ata_port_disable - Disable port.
1534 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001536 * Modify @ap data structure such that the system
1537 * thinks that the entire port is disabled, and should
1538 * never attempt to probe or communicate with devices
1539 * on this port.
1540 *
1541 * LOCKING: host_set lock, or some other form of
1542 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 */
1544
1545void ata_port_disable(struct ata_port *ap)
1546{
1547 ap->device[0].class = ATA_DEV_NONE;
1548 ap->device[1].class = ATA_DEV_NONE;
1549 ap->flags |= ATA_FLAG_PORT_DISABLED;
1550}
1551
Alan Cox452503f2005-10-21 19:01:32 -04001552/*
1553 * This mode timing computation functionality is ported over from
1554 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1555 */
1556/*
1557 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1558 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1559 * for PIO 5, which is a nonstandard extension and UDMA6, which
1560 * is currently supported only by Maxtor drives.
1561 */
1562
1563static const struct ata_timing ata_timing[] = {
1564
1565 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1566 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1567 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1568 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1569
1570 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1571 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1572 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1573
1574/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1575
1576 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1577 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1578 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1579
1580 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1581 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1582 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1583
1584/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1585 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1586 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1587
1588 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1589 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1590 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1591
1592/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1593
1594 { 0xFF }
1595};
1596
1597#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1598#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1599
1600static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1601{
1602 q->setup = EZ(t->setup * 1000, T);
1603 q->act8b = EZ(t->act8b * 1000, T);
1604 q->rec8b = EZ(t->rec8b * 1000, T);
1605 q->cyc8b = EZ(t->cyc8b * 1000, T);
1606 q->active = EZ(t->active * 1000, T);
1607 q->recover = EZ(t->recover * 1000, T);
1608 q->cycle = EZ(t->cycle * 1000, T);
1609 q->udma = EZ(t->udma * 1000, UT);
1610}
1611
1612void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1613 struct ata_timing *m, unsigned int what)
1614{
1615 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1616 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1617 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1618 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1619 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1620 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1621 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1622 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1623}
1624
1625static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1626{
1627 const struct ata_timing *t;
1628
1629 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001630 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001631 return NULL;
1632 return t;
1633}
1634
1635int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1636 struct ata_timing *t, int T, int UT)
1637{
1638 const struct ata_timing *s;
1639 struct ata_timing p;
1640
1641 /*
1642 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001643 */
Alan Cox452503f2005-10-21 19:01:32 -04001644
1645 if (!(s = ata_timing_find_mode(speed)))
1646 return -EINVAL;
1647
Albert Lee75b1f2f2005-11-16 17:06:18 +08001648 memcpy(t, s, sizeof(*s));
1649
Alan Cox452503f2005-10-21 19:01:32 -04001650 /*
1651 * If the drive is an EIDE drive, it can tell us it needs extended
1652 * PIO/MW_DMA cycle timing.
1653 */
1654
1655 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1656 memset(&p, 0, sizeof(p));
1657 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1658 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1659 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1660 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1661 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1662 }
1663 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1664 }
1665
1666 /*
1667 * Convert the timing to bus clock counts.
1668 */
1669
Albert Lee75b1f2f2005-11-16 17:06:18 +08001670 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001671
1672 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001673 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1674 * S.M.A.R.T * and some other commands. We have to ensure that the
1675 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001676 */
1677
1678 if (speed > XFER_PIO_4) {
1679 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1680 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1681 }
1682
1683 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001684 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001685 */
1686
1687 if (t->act8b + t->rec8b < t->cyc8b) {
1688 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1689 t->rec8b = t->cyc8b - t->act8b;
1690 }
1691
1692 if (t->active + t->recover < t->cycle) {
1693 t->active += (t->cycle - (t->active + t->recover)) / 2;
1694 t->recover = t->cycle - t->active;
1695 }
1696
1697 return 0;
1698}
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1701{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1703 return;
1704
1705 if (dev->xfer_shift == ATA_SHIFT_PIO)
1706 dev->flags |= ATA_DFLAG_PIO;
1707
1708 ata_dev_set_xfermode(ap, dev);
1709
Tejun Heo48a8a142006-03-05 17:55:58 +09001710 if (ata_dev_revalidate(ap, dev, 0)) {
1711 printk(KERN_ERR "ata%u: failed to revalidate after set "
1712 "xfermode, disabled\n", ap->id);
1713 ata_port_disable(ap);
1714 }
1715
Tejun Heo23e71c32006-03-06 04:31:57 +09001716 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1717 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
Tejun Heo23e71c32006-03-06 04:31:57 +09001720 ap->id, dev->devno,
1721 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724static int ata_host_set_pio(struct ata_port *ap)
1725{
Tejun Heoa6d5a512006-03-06 04:31:57 +09001726 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1729 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001730
1731 if (!ata_dev_present(dev))
1732 continue;
1733
1734 if (!dev->pio_mode) {
1735 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1736 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09001738
1739 dev->xfer_mode = dev->pio_mode;
1740 dev->xfer_shift = ATA_SHIFT_PIO;
1741 if (ap->ops->set_piomode)
1742 ap->ops->set_piomode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
1745 return 0;
1746}
1747
Tejun Heoa6d5a512006-03-06 04:31:57 +09001748static void ata_host_set_dma(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 int i;
1751
1752 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1753 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001754
1755 if (!ata_dev_present(dev) || !dev->dma_mode)
1756 continue;
1757
1758 dev->xfer_mode = dev->dma_mode;
1759 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1760 if (ap->ops->set_dmamode)
1761 ap->ops->set_dmamode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763}
1764
1765/**
1766 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1767 * @ap: port on which timings will be programmed
1768 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001769 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1770 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001772 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
1774static void ata_set_mode(struct ata_port *ap)
1775{
Tejun Heoa6d5a512006-03-06 04:31:57 +09001776 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Tejun Heoa6d5a512006-03-06 04:31:57 +09001778 /* step 1: calculate xfer_mask */
1779 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1780 struct ata_device *dev = &ap->device[i];
1781 unsigned int xfer_mask;
1782
1783 if (!ata_dev_present(dev))
1784 continue;
1785
1786 xfer_mask = ata_dev_xfermask(ap, dev);
1787
1788 dev->pio_mode = ata_xfer_mask2mode(xfer_mask & ATA_MASK_PIO);
1789 dev->dma_mode = ata_xfer_mask2mode(xfer_mask & (ATA_MASK_MWDMA |
1790 ATA_MASK_UDMA));
1791 }
1792
1793 /* step 2: always set host PIO timings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 rc = ata_host_set_pio(ap);
1795 if (rc)
1796 goto err_out;
1797
Tejun Heoa6d5a512006-03-06 04:31:57 +09001798 /* step 3: set host DMA timings */
1799 ata_host_set_dma(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /* step 4: update devices' xfer mode */
Tejun Heoa6d5a512006-03-06 04:31:57 +09001802 for (i = 0; i < ATA_MAX_DEVICES; i++)
1803 ata_dev_set_mode(ap, &ap->device[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1806 return;
1807
1808 if (ap->ops->post_set_mode)
1809 ap->ops->post_set_mode(ap);
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return;
1812
1813err_out:
1814 ata_port_disable(ap);
1815}
1816
1817/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001818 * ata_tf_to_host - issue ATA taskfile to host controller
1819 * @ap: port to which command is being issued
1820 * @tf: ATA taskfile register set
1821 *
1822 * Issues ATA taskfile register set to ATA host controller,
1823 * with proper synchronization with interrupt handler and
1824 * other threads.
1825 *
1826 * LOCKING:
1827 * spin_lock_irqsave(host_set lock)
1828 */
1829
1830static inline void ata_tf_to_host(struct ata_port *ap,
1831 const struct ata_taskfile *tf)
1832{
1833 ap->ops->tf_load(ap, tf);
1834 ap->ops->exec_command(ap, tf);
1835}
1836
1837/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 * ata_busy_sleep - sleep until BSY clears, or timeout
1839 * @ap: port containing status register to be polled
1840 * @tmout_pat: impatience timeout
1841 * @tmout: overall timeout
1842 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001843 * Sleep until ATA Status register bit BSY clears,
1844 * or a timeout occurs.
1845 *
1846 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 */
1848
Tejun Heo6f8b9952006-01-24 17:05:21 +09001849unsigned int ata_busy_sleep (struct ata_port *ap,
1850 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 unsigned long timer_start, timeout;
1853 u8 status;
1854
1855 status = ata_busy_wait(ap, ATA_BUSY, 300);
1856 timer_start = jiffies;
1857 timeout = timer_start + tmout_pat;
1858 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1859 msleep(50);
1860 status = ata_busy_wait(ap, ATA_BUSY, 3);
1861 }
1862
1863 if (status & ATA_BUSY)
1864 printk(KERN_WARNING "ata%u is slow to respond, "
1865 "please be patient\n", ap->id);
1866
1867 timeout = timer_start + tmout;
1868 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1869 msleep(50);
1870 status = ata_chk_status(ap);
1871 }
1872
1873 if (status & ATA_BUSY) {
1874 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1875 ap->id, tmout / HZ);
1876 return 1;
1877 }
1878
1879 return 0;
1880}
1881
1882static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1883{
1884 struct ata_ioports *ioaddr = &ap->ioaddr;
1885 unsigned int dev0 = devmask & (1 << 0);
1886 unsigned int dev1 = devmask & (1 << 1);
1887 unsigned long timeout;
1888
1889 /* if device 0 was found in ata_devchk, wait for its
1890 * BSY bit to clear
1891 */
1892 if (dev0)
1893 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1894
1895 /* if device 1 was found in ata_devchk, wait for
1896 * register access, then wait for BSY to clear
1897 */
1898 timeout = jiffies + ATA_TMOUT_BOOT;
1899 while (dev1) {
1900 u8 nsect, lbal;
1901
1902 ap->ops->dev_select(ap, 1);
1903 if (ap->flags & ATA_FLAG_MMIO) {
1904 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1905 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1906 } else {
1907 nsect = inb(ioaddr->nsect_addr);
1908 lbal = inb(ioaddr->lbal_addr);
1909 }
1910 if ((nsect == 1) && (lbal == 1))
1911 break;
1912 if (time_after(jiffies, timeout)) {
1913 dev1 = 0;
1914 break;
1915 }
1916 msleep(50); /* give drive a breather */
1917 }
1918 if (dev1)
1919 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1920
1921 /* is all this really necessary? */
1922 ap->ops->dev_select(ap, 0);
1923 if (dev1)
1924 ap->ops->dev_select(ap, 1);
1925 if (dev0)
1926 ap->ops->dev_select(ap, 0);
1927}
1928
1929/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001930 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1931 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001933 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1934 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 *
1936 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001937 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001938 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 *
1940 */
1941
1942static unsigned int ata_bus_edd(struct ata_port *ap)
1943{
1944 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001945 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 /* set up execute-device-diag (bus reset) taskfile */
1948 /* also, take interrupts to a known state (disabled) */
1949 DPRINTK("execute-device-diag\n");
1950 ata_tf_init(ap, &tf, 0);
1951 tf.ctl |= ATA_NIEN;
1952 tf.command = ATA_CMD_EDD;
1953 tf.protocol = ATA_PROT_NODATA;
1954
1955 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001956 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001958 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 /* spec says at least 2ms. but who knows with those
1961 * crazy ATAPI devices...
1962 */
1963 msleep(150);
1964
1965 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1966}
1967
1968static unsigned int ata_bus_softreset(struct ata_port *ap,
1969 unsigned int devmask)
1970{
1971 struct ata_ioports *ioaddr = &ap->ioaddr;
1972
1973 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1974
1975 /* software reset. causes dev0 to be selected */
1976 if (ap->flags & ATA_FLAG_MMIO) {
1977 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1978 udelay(20); /* FIXME: flush */
1979 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1980 udelay(20); /* FIXME: flush */
1981 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1982 } else {
1983 outb(ap->ctl, ioaddr->ctl_addr);
1984 udelay(10);
1985 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1986 udelay(10);
1987 outb(ap->ctl, ioaddr->ctl_addr);
1988 }
1989
1990 /* spec mandates ">= 2ms" before checking status.
1991 * We wait 150ms, because that was the magic delay used for
1992 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1993 * between when the ATA command register is written, and then
1994 * status is checked. Because waiting for "a while" before
1995 * checking status is fine, post SRST, we perform this magic
1996 * delay here as well.
1997 */
1998 msleep(150);
1999
2000 ata_bus_post_reset(ap, devmask);
2001
2002 return 0;
2003}
2004
2005/**
2006 * ata_bus_reset - reset host port and associated ATA channel
2007 * @ap: port to reset
2008 *
2009 * This is typically the first time we actually start issuing
2010 * commands to the ATA channel. We wait for BSY to clear, then
2011 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2012 * result. Determine what devices, if any, are on the channel
2013 * by looking at the device 0/1 error register. Look at the signature
2014 * stored in each device's taskfile registers, to determine if
2015 * the device is ATA or ATAPI.
2016 *
2017 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002018 * PCI/etc. bus probe sem.
2019 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 *
2021 * SIDE EFFECTS:
2022 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2023 */
2024
2025void ata_bus_reset(struct ata_port *ap)
2026{
2027 struct ata_ioports *ioaddr = &ap->ioaddr;
2028 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2029 u8 err;
2030 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2031
2032 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2033
2034 /* determine if device 0/1 are present */
2035 if (ap->flags & ATA_FLAG_SATA_RESET)
2036 dev0 = 1;
2037 else {
2038 dev0 = ata_devchk(ap, 0);
2039 if (slave_possible)
2040 dev1 = ata_devchk(ap, 1);
2041 }
2042
2043 if (dev0)
2044 devmask |= (1 << 0);
2045 if (dev1)
2046 devmask |= (1 << 1);
2047
2048 /* select device 0 again */
2049 ap->ops->dev_select(ap, 0);
2050
2051 /* issue bus reset */
2052 if (ap->flags & ATA_FLAG_SRST)
2053 rc = ata_bus_softreset(ap, devmask);
2054 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2055 /* set up device control */
2056 if (ap->flags & ATA_FLAG_MMIO)
2057 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2058 else
2059 outb(ap->ctl, ioaddr->ctl_addr);
2060 rc = ata_bus_edd(ap);
2061 }
2062
2063 if (rc)
2064 goto err_out;
2065
2066 /*
2067 * determine by signature whether we have ATA or ATAPI devices
2068 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002069 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002071 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /* re-enable interrupts */
2074 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2075 ata_irq_on(ap);
2076
2077 /* is double-select really necessary? */
2078 if (ap->device[1].class != ATA_DEV_NONE)
2079 ap->ops->dev_select(ap, 1);
2080 if (ap->device[0].class != ATA_DEV_NONE)
2081 ap->ops->dev_select(ap, 0);
2082
2083 /* if no devices were detected, disable this port */
2084 if ((ap->device[0].class == ATA_DEV_NONE) &&
2085 (ap->device[1].class == ATA_DEV_NONE))
2086 goto err_out;
2087
2088 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2089 /* set up device control for ATA_FLAG_SATA_RESET */
2090 if (ap->flags & ATA_FLAG_MMIO)
2091 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2092 else
2093 outb(ap->ctl, ioaddr->ctl_addr);
2094 }
2095
2096 DPRINTK("EXIT\n");
2097 return;
2098
2099err_out:
2100 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2101 ap->ops->port_disable(ap);
2102
2103 DPRINTK("EXIT\n");
2104}
2105
Tejun Heo7a7921e2006-02-02 18:20:00 +09002106static int sata_phy_resume(struct ata_port *ap)
2107{
2108 unsigned long timeout = jiffies + (HZ * 5);
2109 u32 sstatus;
2110
2111 scr_write_flush(ap, SCR_CONTROL, 0x300);
2112
2113 /* Wait for phy to become ready, if necessary. */
2114 do {
2115 msleep(200);
2116 sstatus = scr_read(ap, SCR_STATUS);
2117 if ((sstatus & 0xf) != 1)
2118 return 0;
2119 } while (time_before(jiffies, timeout));
2120
2121 return -1;
2122}
2123
Tejun Heoc2bd5802006-01-24 17:05:22 +09002124/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002125 * ata_std_probeinit - initialize probing
2126 * @ap: port to be probed
2127 *
2128 * @ap is about to be probed. Initialize it. This function is
2129 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002130 *
2131 * NOTE!!! Do not use this function as probeinit if a low level
2132 * driver implements only hardreset. Just pass NULL as probeinit
2133 * in that case. Using this function is probably okay but doing
2134 * so makes reset sequence different from the original
2135 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002136 */
2137extern void ata_std_probeinit(struct ata_port *ap)
2138{
Tejun Heo3a397462006-02-10 23:58:48 +09002139 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002140 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002141 if (sata_dev_present(ap))
2142 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2143 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002144}
2145
2146/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002147 * ata_std_softreset - reset host port via ATA SRST
2148 * @ap: port to reset
2149 * @verbose: fail verbosely
2150 * @classes: resulting classes of attached devices
2151 *
2152 * Reset host port using ATA SRST. This function is to be used
2153 * as standard callback for ata_drive_*_reset() functions.
2154 *
2155 * LOCKING:
2156 * Kernel thread context (may sleep)
2157 *
2158 * RETURNS:
2159 * 0 on success, -errno otherwise.
2160 */
2161int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2162{
2163 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2164 unsigned int devmask = 0, err_mask;
2165 u8 err;
2166
2167 DPRINTK("ENTER\n");
2168
Tejun Heo3a397462006-02-10 23:58:48 +09002169 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2170 classes[0] = ATA_DEV_NONE;
2171 goto out;
2172 }
2173
Tejun Heoc2bd5802006-01-24 17:05:22 +09002174 /* determine if device 0/1 are present */
2175 if (ata_devchk(ap, 0))
2176 devmask |= (1 << 0);
2177 if (slave_possible && ata_devchk(ap, 1))
2178 devmask |= (1 << 1);
2179
Tejun Heoc2bd5802006-01-24 17:05:22 +09002180 /* select device 0 again */
2181 ap->ops->dev_select(ap, 0);
2182
2183 /* issue bus reset */
2184 DPRINTK("about to softreset, devmask=%x\n", devmask);
2185 err_mask = ata_bus_softreset(ap, devmask);
2186 if (err_mask) {
2187 if (verbose)
2188 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2189 ap->id, err_mask);
2190 else
2191 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2192 err_mask);
2193 return -EIO;
2194 }
2195
2196 /* determine by signature whether we have ATA or ATAPI devices */
2197 classes[0] = ata_dev_try_classify(ap, 0, &err);
2198 if (slave_possible && err != 0x81)
2199 classes[1] = ata_dev_try_classify(ap, 1, &err);
2200
Tejun Heo3a397462006-02-10 23:58:48 +09002201 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002202 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2203 return 0;
2204}
2205
2206/**
2207 * sata_std_hardreset - reset host port via SATA phy reset
2208 * @ap: port to reset
2209 * @verbose: fail verbosely
2210 * @class: resulting class of attached device
2211 *
2212 * SATA phy-reset host port using DET bits of SControl register.
2213 * This function is to be used as standard callback for
2214 * ata_drive_*_reset().
2215 *
2216 * LOCKING:
2217 * Kernel thread context (may sleep)
2218 *
2219 * RETURNS:
2220 * 0 on success, -errno otherwise.
2221 */
2222int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2223{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002224 DPRINTK("ENTER\n");
2225
2226 /* Issue phy wake/reset */
2227 scr_write_flush(ap, SCR_CONTROL, 0x301);
2228
2229 /*
2230 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2231 * 10.4.2 says at least 1 ms.
2232 */
2233 msleep(1);
2234
Tejun Heo7a7921e2006-02-02 18:20:00 +09002235 /* Bring phy back */
2236 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002237
Tejun Heoc2bd5802006-01-24 17:05:22 +09002238 /* TODO: phy layer with polling, timeouts, etc. */
2239 if (!sata_dev_present(ap)) {
2240 *class = ATA_DEV_NONE;
2241 DPRINTK("EXIT, link offline\n");
2242 return 0;
2243 }
2244
2245 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2246 if (verbose)
2247 printk(KERN_ERR "ata%u: COMRESET failed "
2248 "(device not ready)\n", ap->id);
2249 else
2250 DPRINTK("EXIT, device not ready\n");
2251 return -EIO;
2252 }
2253
Tejun Heo3a397462006-02-10 23:58:48 +09002254 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2255
Tejun Heoc2bd5802006-01-24 17:05:22 +09002256 *class = ata_dev_try_classify(ap, 0, NULL);
2257
2258 DPRINTK("EXIT, class=%u\n", *class);
2259 return 0;
2260}
2261
2262/**
2263 * ata_std_postreset - standard postreset callback
2264 * @ap: the target ata_port
2265 * @classes: classes of attached devices
2266 *
2267 * This function is invoked after a successful reset. Note that
2268 * the device might have been reset more than once using
2269 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002270 *
2271 * This function is to be used as standard callback for
2272 * ata_drive_*_reset().
2273 *
2274 * LOCKING:
2275 * Kernel thread context (may sleep)
2276 */
2277void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2278{
2279 DPRINTK("ENTER\n");
2280
Tejun Heo56497bd2006-02-15 15:01:42 +09002281 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002282 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2283 ap->cbl = ATA_CBL_SATA;
2284
2285 /* print link status */
2286 if (ap->cbl == ATA_CBL_SATA)
2287 sata_print_link_status(ap);
2288
Tejun Heo3a397462006-02-10 23:58:48 +09002289 /* re-enable interrupts */
2290 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2291 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002292
2293 /* is double-select really necessary? */
2294 if (classes[0] != ATA_DEV_NONE)
2295 ap->ops->dev_select(ap, 1);
2296 if (classes[1] != ATA_DEV_NONE)
2297 ap->ops->dev_select(ap, 0);
2298
Tejun Heo3a397462006-02-10 23:58:48 +09002299 /* bail out if no device is present */
2300 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2301 DPRINTK("EXIT, no device\n");
2302 return;
2303 }
2304
2305 /* set up device control */
2306 if (ap->ioaddr.ctl_addr) {
2307 if (ap->flags & ATA_FLAG_MMIO)
2308 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2309 else
2310 outb(ap->ctl, ap->ioaddr.ctl_addr);
2311 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002312
2313 DPRINTK("EXIT\n");
2314}
2315
2316/**
2317 * ata_std_probe_reset - standard probe reset method
2318 * @ap: prot to perform probe-reset
2319 * @classes: resulting classes of attached devices
2320 *
2321 * The stock off-the-shelf ->probe_reset method.
2322 *
2323 * LOCKING:
2324 * Kernel thread context (may sleep)
2325 *
2326 * RETURNS:
2327 * 0 on success, -errno otherwise.
2328 */
2329int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2330{
2331 ata_reset_fn_t hardreset;
2332
2333 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002334 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002335 hardreset = sata_std_hardreset;
2336
Tejun Heo8a19ac82006-02-02 18:20:00 +09002337 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002338 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002339 ata_std_postreset, classes);
2340}
2341
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002342static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2343 ata_postreset_fn_t postreset,
2344 unsigned int *classes)
2345{
2346 int i, rc;
2347
2348 for (i = 0; i < ATA_MAX_DEVICES; i++)
2349 classes[i] = ATA_DEV_UNKNOWN;
2350
2351 rc = reset(ap, 0, classes);
2352 if (rc)
2353 return rc;
2354
2355 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2356 * is complete and convert all ATA_DEV_UNKNOWN to
2357 * ATA_DEV_NONE.
2358 */
2359 for (i = 0; i < ATA_MAX_DEVICES; i++)
2360 if (classes[i] != ATA_DEV_UNKNOWN)
2361 break;
2362
2363 if (i < ATA_MAX_DEVICES)
2364 for (i = 0; i < ATA_MAX_DEVICES; i++)
2365 if (classes[i] == ATA_DEV_UNKNOWN)
2366 classes[i] = ATA_DEV_NONE;
2367
2368 if (postreset)
2369 postreset(ap, classes);
2370
2371 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2372}
2373
2374/**
2375 * ata_drive_probe_reset - Perform probe reset with given methods
2376 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002377 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002378 * @softreset: softreset method (can be NULL)
2379 * @hardreset: hardreset method (can be NULL)
2380 * @postreset: postreset method (can be NULL)
2381 * @classes: resulting classes of attached devices
2382 *
2383 * Reset the specified port and classify attached devices using
2384 * given methods. This function prefers softreset but tries all
2385 * possible reset sequences to reset and classify devices. This
2386 * function is intended to be used for constructing ->probe_reset
2387 * callback by low level drivers.
2388 *
2389 * Reset methods should follow the following rules.
2390 *
2391 * - Return 0 on sucess, -errno on failure.
2392 * - If classification is supported, fill classes[] with
2393 * recognized class codes.
2394 * - If classification is not supported, leave classes[] alone.
2395 * - If verbose is non-zero, print error message on failure;
2396 * otherwise, shut up.
2397 *
2398 * LOCKING:
2399 * Kernel thread context (may sleep)
2400 *
2401 * RETURNS:
2402 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2403 * if classification fails, and any error code from reset
2404 * methods.
2405 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002406int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002407 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2408 ata_postreset_fn_t postreset, unsigned int *classes)
2409{
2410 int rc = -EINVAL;
2411
Tejun Heo7944ea92006-02-02 18:20:00 +09002412 if (probeinit)
2413 probeinit(ap);
2414
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002415 if (softreset) {
2416 rc = do_probe_reset(ap, softreset, postreset, classes);
2417 if (rc == 0)
2418 return 0;
2419 }
2420
2421 if (!hardreset)
2422 return rc;
2423
2424 rc = do_probe_reset(ap, hardreset, postreset, classes);
2425 if (rc == 0 || rc != -ENODEV)
2426 return rc;
2427
2428 if (softreset)
2429 rc = do_probe_reset(ap, softreset, postreset, classes);
2430
2431 return rc;
2432}
2433
Tejun Heo623a3122006-03-05 17:55:58 +09002434/**
2435 * ata_dev_same_device - Determine whether new ID matches configured device
2436 * @ap: port on which the device to compare against resides
2437 * @dev: device to compare against
2438 * @new_class: class of the new device
2439 * @new_id: IDENTIFY page of the new device
2440 *
2441 * Compare @new_class and @new_id against @dev and determine
2442 * whether @dev is the device indicated by @new_class and
2443 * @new_id.
2444 *
2445 * LOCKING:
2446 * None.
2447 *
2448 * RETURNS:
2449 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2450 */
2451static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2452 unsigned int new_class, const u16 *new_id)
2453{
2454 const u16 *old_id = dev->id;
2455 unsigned char model[2][41], serial[2][21];
2456 u64 new_n_sectors;
2457
2458 if (dev->class != new_class) {
2459 printk(KERN_INFO
2460 "ata%u: dev %u class mismatch %d != %d\n",
2461 ap->id, dev->devno, dev->class, new_class);
2462 return 0;
2463 }
2464
2465 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2466 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2467 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2468 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2469 new_n_sectors = ata_id_n_sectors(new_id);
2470
2471 if (strcmp(model[0], model[1])) {
2472 printk(KERN_INFO
2473 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2474 ap->id, dev->devno, model[0], model[1]);
2475 return 0;
2476 }
2477
2478 if (strcmp(serial[0], serial[1])) {
2479 printk(KERN_INFO
2480 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2481 ap->id, dev->devno, serial[0], serial[1]);
2482 return 0;
2483 }
2484
2485 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2486 printk(KERN_INFO
2487 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2488 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2489 (unsigned long long)new_n_sectors);
2490 return 0;
2491 }
2492
2493 return 1;
2494}
2495
2496/**
2497 * ata_dev_revalidate - Revalidate ATA device
2498 * @ap: port on which the device to revalidate resides
2499 * @dev: device to revalidate
2500 * @post_reset: is this revalidation after reset?
2501 *
2502 * Re-read IDENTIFY page and make sure @dev is still attached to
2503 * the port.
2504 *
2505 * LOCKING:
2506 * Kernel thread context (may sleep)
2507 *
2508 * RETURNS:
2509 * 0 on success, negative errno otherwise
2510 */
2511int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2512 int post_reset)
2513{
2514 unsigned int class;
2515 u16 *id;
2516 int rc;
2517
2518 if (!ata_dev_present(dev))
2519 return -ENODEV;
2520
2521 class = dev->class;
2522 id = NULL;
2523
2524 /* allocate & read ID data */
2525 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2526 if (rc)
2527 goto fail;
2528
2529 /* is the device still there? */
2530 if (!ata_dev_same_device(ap, dev, class, id)) {
2531 rc = -ENODEV;
2532 goto fail;
2533 }
2534
2535 kfree(dev->id);
2536 dev->id = id;
2537
2538 /* configure device according to the new ID */
2539 return ata_dev_configure(ap, dev, 0);
2540
2541 fail:
2542 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2543 ap->id, dev->devno, rc);
2544 kfree(id);
2545 return rc;
2546}
2547
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002548static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 "WDC AC11000H",
2550 "WDC AC22100H",
2551 "WDC AC32500H",
2552 "WDC AC33100H",
2553 "WDC AC31600H",
2554 "WDC AC32100H",
2555 "WDC AC23200L",
2556 "Compaq CRD-8241B",
2557 "CRD-8400B",
2558 "CRD-8480B",
2559 "CRD-8482B",
2560 "CRD-84",
2561 "SanDisk SDP3B",
2562 "SanDisk SDP3B-64",
2563 "SANYO CD-ROM CRD",
2564 "HITACHI CDR-8",
2565 "HITACHI CDR-8335",
2566 "HITACHI CDR-8435",
2567 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002568 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 "CD-532E-A",
2570 "E-IDE CD-ROM CR-840",
2571 "CD-ROM Drive/F5A",
2572 "WPI CDD-820",
2573 "SAMSUNG CD-ROM SC-148C",
2574 "SAMSUNG CD-ROM SC",
2575 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2577 "_NEC DV5800A",
2578};
2579
Jeff Garzik057ace52005-10-22 14:27:05 -04002580static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Tejun Heo2e026712006-02-12 22:47:04 +09002582 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 int i;
2584
Tejun Heo6a62a042006-02-13 10:02:46 +09002585 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002588 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return 1;
2590
2591 return 0;
2592}
2593
Tejun Heoa6d5a512006-03-06 04:31:57 +09002594/**
2595 * ata_dev_xfermask - Compute supported xfermask of the given device
2596 * @ap: Port on which the device to compute xfermask for resides
2597 * @dev: Device to compute xfermask for
2598 *
2599 * Compute supported xfermask of @dev. This function is
2600 * responsible for applying all known limits including host
2601 * controller limits, device blacklist, etc...
2602 *
2603 * LOCKING:
2604 * None.
2605 *
2606 * RETURNS:
2607 * Computed xfermask.
2608 */
2609static unsigned int ata_dev_xfermask(struct ata_port *ap,
2610 struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Tejun Heoa6d5a512006-03-06 04:31:57 +09002612 unsigned long xfer_mask;
2613 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Tejun Heoa6d5a512006-03-06 04:31:57 +09002615 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2616 ap->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Tejun Heoa6d5a512006-03-06 04:31:57 +09002618 /* use port-wide xfermask for now */
2619 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2620 struct ata_device *d = &ap->device[i];
2621 if (!ata_dev_present(d))
2622 continue;
2623 xfer_mask &= ata_id_xfermask(d->id);
2624 if (ata_dma_blacklisted(d))
2625 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
2627
Tejun Heoa6d5a512006-03-06 04:31:57 +09002628 if (ata_dma_blacklisted(dev))
2629 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2630 "disabling DMA\n", ap->id, dev->devno);
2631
2632 return xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2637 * @ap: Port associated with device @dev
2638 * @dev: Device to which command will be sent
2639 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002640 * Issue SET FEATURES - XFER MODE command to device @dev
2641 * on port @ap.
2642 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002644 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 */
2646
2647static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2648{
Tejun Heoa0123702005-12-13 14:49:31 +09002649 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 /* set up set-features taskfile */
2652 DPRINTK("set features - xfer mode\n");
2653
Tejun Heoa0123702005-12-13 14:49:31 +09002654 ata_tf_init(ap, &tf, dev->devno);
2655 tf.command = ATA_CMD_SET_FEATURES;
2656 tf.feature = SETFEATURES_XFER;
2657 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2658 tf.protocol = ATA_PROT_NODATA;
2659 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Tejun Heoa0123702005-12-13 14:49:31 +09002661 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2662 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2663 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
2667 DPRINTK("EXIT\n");
2668}
2669
2670/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002671 * ata_dev_init_params - Issue INIT DEV PARAMS command
2672 * @ap: Port associated with device @dev
2673 * @dev: Device to which command will be sent
2674 *
2675 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002676 * Kernel thread context (may sleep)
2677 *
2678 * RETURNS:
2679 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04002680 */
2681
Tejun Heo6aff8f12006-02-15 18:24:09 +09002682static unsigned int ata_dev_init_params(struct ata_port *ap,
2683 struct ata_device *dev)
Albert Lee8bf62ece2005-05-12 15:29:42 -04002684{
Tejun Heoa0123702005-12-13 14:49:31 +09002685 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002686 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002687 u16 sectors = dev->id[6];
2688 u16 heads = dev->id[3];
2689
2690 /* Number of sectors per track 1-255. Number of heads 1-16 */
2691 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002692 return 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002693
2694 /* set up init dev params taskfile */
2695 DPRINTK("init dev params \n");
2696
Tejun Heoa0123702005-12-13 14:49:31 +09002697 ata_tf_init(ap, &tf, dev->devno);
2698 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2699 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2700 tf.protocol = ATA_PROT_NODATA;
2701 tf.nsect = sectors;
2702 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04002703
Tejun Heo6aff8f12006-02-15 18:24:09 +09002704 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ece2005-05-12 15:29:42 -04002705
Tejun Heo6aff8f12006-02-15 18:24:09 +09002706 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2707 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002708}
2709
2710/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002711 * ata_sg_clean - Unmap DMA memory associated with command
2712 * @qc: Command containing DMA memory to be released
2713 *
2714 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 *
2716 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002717 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 */
2719
2720static void ata_sg_clean(struct ata_queued_cmd *qc)
2721{
2722 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002723 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002725 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Tejun Heoa46314742006-02-11 19:11:13 +09002727 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2728 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002731 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002733 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002735 /* if we padded the buffer out to 32-bit bound, and data
2736 * xfer direction is from-device, we must copy from the
2737 * pad buffer back into the supplied buffer
2738 */
2739 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2740 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2741
2742 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002743 if (qc->n_elem)
2744 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002745 /* restore last sg */
2746 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2747 if (pad_buf) {
2748 struct scatterlist *psg = &qc->pad_sgent;
2749 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2750 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002751 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002752 }
2753 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002754 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002755 dma_unmap_single(ap->host_set->dev,
2756 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2757 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002758 /* restore sg */
2759 sg->length += qc->pad_len;
2760 if (pad_buf)
2761 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2762 pad_buf, qc->pad_len);
2763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002766 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
2769/**
2770 * ata_fill_sg - Fill PCI IDE PRD table
2771 * @qc: Metadata associated with taskfile to be transferred
2772 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002773 * Fill PCI IDE PRD (scatter-gather) table with segments
2774 * associated with the current disk command.
2775 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002777 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 *
2779 */
2780static void ata_fill_sg(struct ata_queued_cmd *qc)
2781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002783 struct scatterlist *sg;
2784 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Tejun Heoa46314742006-02-11 19:11:13 +09002786 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002787 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002790 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 u32 addr, offset;
2792 u32 sg_len, len;
2793
2794 /* determine if physical DMA addr spans 64K boundary.
2795 * Note h/w doesn't support 64-bit, so we unconditionally
2796 * truncate dma_addr_t to u32.
2797 */
2798 addr = (u32) sg_dma_address(sg);
2799 sg_len = sg_dma_len(sg);
2800
2801 while (sg_len) {
2802 offset = addr & 0xffff;
2803 len = sg_len;
2804 if ((offset + sg_len) > 0x10000)
2805 len = 0x10000 - offset;
2806
2807 ap->prd[idx].addr = cpu_to_le32(addr);
2808 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2809 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2810
2811 idx++;
2812 sg_len -= len;
2813 addr += len;
2814 }
2815 }
2816
2817 if (idx)
2818 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2819}
2820/**
2821 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2822 * @qc: Metadata associated with taskfile to check
2823 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002824 * Allow low-level driver to filter ATA PACKET commands, returning
2825 * a status indicating whether or not it is OK to use DMA for the
2826 * supplied PACKET command.
2827 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002829 * spin_lock_irqsave(host_set lock)
2830 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 * RETURNS: 0 when ATAPI DMA can be used
2832 * nonzero otherwise
2833 */
2834int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2835{
2836 struct ata_port *ap = qc->ap;
2837 int rc = 0; /* Assume ATAPI DMA is OK by default */
2838
2839 if (ap->ops->check_atapi_dma)
2840 rc = ap->ops->check_atapi_dma(qc);
2841
2842 return rc;
2843}
2844/**
2845 * ata_qc_prep - Prepare taskfile for submission
2846 * @qc: Metadata associated with taskfile to be prepared
2847 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002848 * Prepare ATA taskfile for submission.
2849 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 * LOCKING:
2851 * spin_lock_irqsave(host_set lock)
2852 */
2853void ata_qc_prep(struct ata_queued_cmd *qc)
2854{
2855 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2856 return;
2857
2858 ata_fill_sg(qc);
2859}
2860
Jeff Garzik0cba6322005-05-30 19:49:12 -04002861/**
2862 * ata_sg_init_one - Associate command with memory buffer
2863 * @qc: Command to be associated
2864 * @buf: Memory buffer
2865 * @buflen: Length of memory buffer, in bytes.
2866 *
2867 * Initialize the data-related elements of queued_cmd @qc
2868 * to point to a single memory buffer, @buf of byte length @buflen.
2869 *
2870 * LOCKING:
2871 * spin_lock_irqsave(host_set lock)
2872 */
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2875{
2876 struct scatterlist *sg;
2877
2878 qc->flags |= ATA_QCFLAG_SINGLE;
2879
2880 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002881 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002883 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 qc->buf_virt = buf;
2885
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002886 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002887 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
Jeff Garzik0cba6322005-05-30 19:49:12 -04002890/**
2891 * ata_sg_init - Associate command with scatter-gather table.
2892 * @qc: Command to be associated
2893 * @sg: Scatter-gather table.
2894 * @n_elem: Number of elements in s/g table.
2895 *
2896 * Initialize the data-related elements of queued_cmd @qc
2897 * to point to a scatter-gather table @sg, containing @n_elem
2898 * elements.
2899 *
2900 * LOCKING:
2901 * spin_lock_irqsave(host_set lock)
2902 */
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2905 unsigned int n_elem)
2906{
2907 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002908 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002910 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
2912
2913/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002914 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2915 * @qc: Command with memory buffer to be mapped.
2916 *
2917 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 *
2919 * LOCKING:
2920 * spin_lock_irqsave(host_set lock)
2921 *
2922 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002923 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 */
2925
2926static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2927{
2928 struct ata_port *ap = qc->ap;
2929 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002930 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002932 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002934 /* we must lengthen transfers to end on a 32-bit boundary */
2935 qc->pad_len = sg->length & 3;
2936 if (qc->pad_len) {
2937 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2938 struct scatterlist *psg = &qc->pad_sgent;
2939
Tejun Heoa46314742006-02-11 19:11:13 +09002940 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002941
2942 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2943
2944 if (qc->tf.flags & ATA_TFLAG_WRITE)
2945 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2946 qc->pad_len);
2947
2948 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2949 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2950 /* trim sg */
2951 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002952 if (sg->length == 0)
2953 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002954
2955 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2956 sg->length, qc->pad_len);
2957 }
2958
Tejun Heo2e242fa2006-02-20 23:48:38 +09002959 if (trim_sg) {
2960 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05002961 goto skip_map;
2962 }
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002965 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002966 if (dma_mapping_error(dma_address)) {
2967 /* restore sg */
2968 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002973 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Tejun Heo2e242fa2006-02-20 23:48:38 +09002975skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2977 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2978
2979 return 0;
2980}
2981
2982/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002983 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2984 * @qc: Command with scatter-gather table to be mapped.
2985 *
2986 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 *
2988 * LOCKING:
2989 * spin_lock_irqsave(host_set lock)
2990 *
2991 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002992 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 *
2994 */
2995
2996static int ata_sg_setup(struct ata_queued_cmd *qc)
2997{
2998 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002999 struct scatterlist *sg = qc->__sg;
3000 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003001 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa46314742006-02-11 19:11:13 +09003004 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003006 /* we must lengthen transfers to end on a 32-bit boundary */
3007 qc->pad_len = lsg->length & 3;
3008 if (qc->pad_len) {
3009 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3010 struct scatterlist *psg = &qc->pad_sgent;
3011 unsigned int offset;
3012
Tejun Heoa46314742006-02-11 19:11:13 +09003013 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003014
3015 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3016
3017 /*
3018 * psg->page/offset are used to copy to-be-written
3019 * data in this function or read data in ata_sg_clean.
3020 */
3021 offset = lsg->offset + lsg->length - qc->pad_len;
3022 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3023 psg->offset = offset_in_page(offset);
3024
3025 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3026 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3027 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003028 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003029 }
3030
3031 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3032 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3033 /* trim last sg */
3034 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003035 if (lsg->length == 0)
3036 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003037
3038 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3039 qc->n_elem - 1, lsg->length, qc->pad_len);
3040 }
3041
Jeff Garzike1410f22005-11-14 14:06:26 -05003042 pre_n_elem = qc->n_elem;
3043 if (trim_sg && pre_n_elem)
3044 pre_n_elem--;
3045
3046 if (!pre_n_elem) {
3047 n_elem = 0;
3048 goto skip_map;
3049 }
3050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05003052 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003053 if (n_elem < 1) {
3054 /* restore last sg */
3055 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059 DPRINTK("%d sg elements mapped\n", n_elem);
3060
Jeff Garzike1410f22005-11-14 14:06:26 -05003061skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 qc->n_elem = n_elem;
3063
3064 return 0;
3065}
3066
3067/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003068 * ata_poll_qc_complete - turn irq back on and finish qc
3069 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003070 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003071 *
3072 * LOCKING:
3073 * None. (grabs host lock)
3074 */
3075
Albert Leea22e2eb2005-12-05 15:38:02 +08003076void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003077{
3078 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003079 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003080
Jeff Garzikb8f61532005-08-25 22:01:20 -04003081 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003082 ap->flags &= ~ATA_FLAG_NOINTR;
3083 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003084 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003085 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003086}
3087
3088/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003089 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003090 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 *
3092 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003093 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 *
3095 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003096 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 */
3098
3099static unsigned long ata_pio_poll(struct ata_port *ap)
3100{
Albert Leec14b8332005-12-05 15:36:08 +08003101 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003103 unsigned int poll_state = HSM_ST_UNKNOWN;
3104 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Albert Leec14b8332005-12-05 15:36:08 +08003106 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003107 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003108
Albert Lee14be71f2005-09-27 17:36:35 +08003109 switch (ap->hsm_task_state) {
3110 case HSM_ST:
3111 case HSM_ST_POLL:
3112 poll_state = HSM_ST_POLL;
3113 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003115 case HSM_ST_LAST:
3116 case HSM_ST_LAST_POLL:
3117 poll_state = HSM_ST_LAST_POLL;
3118 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 break;
3120 default:
3121 BUG();
3122 break;
3123 }
3124
3125 status = ata_chk_status(ap);
3126 if (status & ATA_BUSY) {
3127 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003128 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003129 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 return 0;
3131 }
Albert Lee14be71f2005-09-27 17:36:35 +08003132 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return ATA_SHORT_PAUSE;
3134 }
3135
Albert Lee14be71f2005-09-27 17:36:35 +08003136 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return 0;
3138}
3139
3140/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003141 * ata_pio_complete - check if drive is busy or idle
3142 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 *
3144 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003145 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003146 *
3147 * RETURNS:
3148 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 */
3150
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003151static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 struct ata_queued_cmd *qc;
3154 u8 drv_stat;
3155
3156 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003157 * This is purely heuristic. This is a fast path. Sometimes when
3158 * we enter, BSY will be cleared in a chk-status or two. If not,
3159 * the drive is probably seeking or something. Snooze for a couple
3160 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003161 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 */
Albert Leefe79e682005-12-06 11:34:59 +08003163 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3164 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003166 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3167 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003168 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
3172 }
3173
Albert Leec14b8332005-12-05 15:36:08 +08003174 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003175 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 drv_stat = ata_wait_idle(ap);
3178 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003179 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003180 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
3183
Albert Lee14be71f2005-09-27 17:36:35 +08003184 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Tejun Heoa46314742006-02-11 19:11:13 +09003186 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003187 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003188
3189 /* another command may start at this point */
3190
3191 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
Edward Falk0baab862005-06-02 18:17:13 -04003194
3195/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003196 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003197 * @buf: Buffer to swap
3198 * @buf_words: Number of 16-bit words in buffer.
3199 *
3200 * Swap halves of 16-bit words if needed to convert from
3201 * little-endian byte order to native cpu byte order, or
3202 * vice-versa.
3203 *
3204 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003205 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207void swap_buf_le16(u16 *buf, unsigned int buf_words)
3208{
3209#ifdef __BIG_ENDIAN
3210 unsigned int i;
3211
3212 for (i = 0; i < buf_words; i++)
3213 buf[i] = le16_to_cpu(buf[i]);
3214#endif /* __BIG_ENDIAN */
3215}
3216
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003217/**
3218 * ata_mmio_data_xfer - Transfer data by MMIO
3219 * @ap: port to read/write
3220 * @buf: data buffer
3221 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003222 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003223 *
3224 * Transfer data from/to the device data register by MMIO.
3225 *
3226 * LOCKING:
3227 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003228 */
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3231 unsigned int buflen, int write_data)
3232{
3233 unsigned int i;
3234 unsigned int words = buflen >> 1;
3235 u16 *buf16 = (u16 *) buf;
3236 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3237
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003238 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 if (write_data) {
3240 for (i = 0; i < words; i++)
3241 writew(le16_to_cpu(buf16[i]), mmio);
3242 } else {
3243 for (i = 0; i < words; i++)
3244 buf16[i] = cpu_to_le16(readw(mmio));
3245 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003246
3247 /* Transfer trailing 1 byte, if any. */
3248 if (unlikely(buflen & 0x01)) {
3249 u16 align_buf[1] = { 0 };
3250 unsigned char *trailing_buf = buf + buflen - 1;
3251
3252 if (write_data) {
3253 memcpy(align_buf, trailing_buf, 1);
3254 writew(le16_to_cpu(align_buf[0]), mmio);
3255 } else {
3256 align_buf[0] = cpu_to_le16(readw(mmio));
3257 memcpy(trailing_buf, align_buf, 1);
3258 }
3259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003262/**
3263 * ata_pio_data_xfer - Transfer data by PIO
3264 * @ap: port to read/write
3265 * @buf: data buffer
3266 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003267 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003268 *
3269 * Transfer data from/to the device data register by PIO.
3270 *
3271 * LOCKING:
3272 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003273 */
3274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3276 unsigned int buflen, int write_data)
3277{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003278 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003280 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003282 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003284 insw(ap->ioaddr.data_addr, buf, words);
3285
3286 /* Transfer trailing 1 byte, if any. */
3287 if (unlikely(buflen & 0x01)) {
3288 u16 align_buf[1] = { 0 };
3289 unsigned char *trailing_buf = buf + buflen - 1;
3290
3291 if (write_data) {
3292 memcpy(align_buf, trailing_buf, 1);
3293 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3294 } else {
3295 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3296 memcpy(trailing_buf, align_buf, 1);
3297 }
3298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299}
3300
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003301/**
3302 * ata_data_xfer - Transfer data from/to the data register.
3303 * @ap: port to read/write
3304 * @buf: data buffer
3305 * @buflen: buffer length
3306 * @do_write: read/write
3307 *
3308 * Transfer data from/to the device data register.
3309 *
3310 * LOCKING:
3311 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003312 */
3313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3315 unsigned int buflen, int do_write)
3316{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003317 /* Make the crap hardware pay the costs not the good stuff */
3318 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3319 unsigned long flags;
3320 local_irq_save(flags);
3321 if (ap->flags & ATA_FLAG_MMIO)
3322 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3323 else
3324 ata_pio_data_xfer(ap, buf, buflen, do_write);
3325 local_irq_restore(flags);
3326 } else {
3327 if (ap->flags & ATA_FLAG_MMIO)
3328 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3329 else
3330 ata_pio_data_xfer(ap, buf, buflen, do_write);
3331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003334/**
3335 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3336 * @qc: Command on going
3337 *
3338 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3339 *
3340 * LOCKING:
3341 * Inherited from caller.
3342 */
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344static void ata_pio_sector(struct ata_queued_cmd *qc)
3345{
3346 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003347 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 struct ata_port *ap = qc->ap;
3349 struct page *page;
3350 unsigned int offset;
3351 unsigned char *buf;
3352
3353 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003354 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356 page = sg[qc->cursg].page;
3357 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3358
3359 /* get the current page and offset */
3360 page = nth_page(page, (offset >> PAGE_SHIFT));
3361 offset %= PAGE_SIZE;
3362
3363 buf = kmap(page) + offset;
3364
3365 qc->cursect++;
3366 qc->cursg_ofs++;
3367
Albert Lee32529e02005-05-26 03:49:42 -04003368 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 qc->cursg++;
3370 qc->cursg_ofs = 0;
3371 }
3372
3373 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3374
3375 /* do the actual data transfer */
3376 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3377 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3378
3379 kunmap(page);
3380}
3381
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003382/**
3383 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3384 * @qc: Command on going
3385 * @bytes: number of bytes
3386 *
3387 * Transfer Transfer data from/to the ATAPI device.
3388 *
3389 * LOCKING:
3390 * Inherited from caller.
3391 *
3392 */
3393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3395{
3396 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003397 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 struct ata_port *ap = qc->ap;
3399 struct page *page;
3400 unsigned char *buf;
3401 unsigned int offset, count;
3402
Albert Lee563a6e12005-08-12 14:17:50 +08003403 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003404 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
3406next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003407 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003408 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003409 * The end of qc->sg is reached and the device expects
3410 * more data to transfer. In order not to overrun qc->sg
3411 * and fulfill length specified in the byte count register,
3412 * - for read case, discard trailing data from the device
3413 * - for write case, padding zero data to the device
3414 */
3415 u16 pad_buf[1] = { 0 };
3416 unsigned int words = bytes >> 1;
3417 unsigned int i;
3418
3419 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003420 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003421 ap->id, bytes);
3422
3423 for (i = 0; i < words; i++)
3424 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3425
Albert Lee14be71f2005-09-27 17:36:35 +08003426 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003427 return;
3428 }
3429
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003430 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 page = sg->page;
3433 offset = sg->offset + qc->cursg_ofs;
3434
3435 /* get the current page and offset */
3436 page = nth_page(page, (offset >> PAGE_SHIFT));
3437 offset %= PAGE_SIZE;
3438
Albert Lee6952df02005-06-06 15:56:03 +08003439 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003440 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442 /* don't cross page boundaries */
3443 count = min(count, (unsigned int)PAGE_SIZE - offset);
3444
3445 buf = kmap(page) + offset;
3446
3447 bytes -= count;
3448 qc->curbytes += count;
3449 qc->cursg_ofs += count;
3450
Albert Lee32529e02005-05-26 03:49:42 -04003451 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 qc->cursg++;
3453 qc->cursg_ofs = 0;
3454 }
3455
3456 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3457
3458 /* do the actual data transfer */
3459 ata_data_xfer(ap, buf, count, do_write);
3460
3461 kunmap(page);
3462
Albert Lee563a6e12005-08-12 14:17:50 +08003463 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465}
3466
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003467/**
3468 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3469 * @qc: Command on going
3470 *
3471 * Transfer Transfer data from/to the ATAPI device.
3472 *
3473 * LOCKING:
3474 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003475 */
3476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3478{
3479 struct ata_port *ap = qc->ap;
3480 struct ata_device *dev = qc->dev;
3481 unsigned int ireason, bc_lo, bc_hi, bytes;
3482 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3483
3484 ap->ops->tf_read(ap, &qc->tf);
3485 ireason = qc->tf.nsect;
3486 bc_lo = qc->tf.lbam;
3487 bc_hi = qc->tf.lbah;
3488 bytes = (bc_hi << 8) | bc_lo;
3489
3490 /* shall be cleared to zero, indicating xfer of data */
3491 if (ireason & (1 << 0))
3492 goto err_out;
3493
3494 /* make sure transfer direction matches expected */
3495 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3496 if (do_write != i_write)
3497 goto err_out;
3498
3499 __atapi_pio_bytes(qc, bytes);
3500
3501 return;
3502
3503err_out:
3504 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3505 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003506 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003507 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508}
3509
3510/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003511 * ata_pio_block - start PIO on a block
3512 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 *
3514 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003515 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 */
3517
3518static void ata_pio_block(struct ata_port *ap)
3519{
3520 struct ata_queued_cmd *qc;
3521 u8 status;
3522
3523 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003524 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 * Sometimes when we enter, BSY will be cleared in
3526 * a chk-status or two. If not, the drive is probably seeking
3527 * or something. Snooze for a couple msecs, then
3528 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003529 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 */
3531 status = ata_busy_wait(ap, ATA_BUSY, 5);
3532 if (status & ATA_BUSY) {
3533 msleep(2);
3534 status = ata_busy_wait(ap, ATA_BUSY, 10);
3535 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003536 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3538 return;
3539 }
3540 }
3541
3542 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003543 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Albert Leefe79e682005-12-06 11:34:59 +08003545 /* check error */
3546 if (status & (ATA_ERR | ATA_DF)) {
3547 qc->err_mask |= AC_ERR_DEV;
3548 ap->hsm_task_state = HSM_ST_ERR;
3549 return;
3550 }
3551
3552 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003554 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003556 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return;
3558 }
3559
3560 atapi_pio_bytes(qc);
3561 } else {
3562 /* handle BSY=0, DRQ=0 as error */
3563 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003564 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003565 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 return;
3567 }
3568
3569 ata_pio_sector(qc);
3570 }
3571}
3572
3573static void ata_pio_error(struct ata_port *ap)
3574{
3575 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003576
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003578 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Albert Lee0565c262006-02-13 18:55:25 +08003580 if (qc->tf.command != ATA_CMD_PACKET)
3581 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3582
Albert Lee1c848982005-12-05 15:40:15 +08003583 /* make sure qc->err_mask is available to
3584 * know what's wrong and recover
3585 */
Tejun Heoa46314742006-02-11 19:11:13 +09003586 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003587
Albert Lee14be71f2005-09-27 17:36:35 +08003588 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Albert Leea22e2eb2005-12-05 15:38:02 +08003590 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
3593static void ata_pio_task(void *_data)
3594{
3595 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003596 unsigned long timeout;
3597 int qc_completed;
3598
3599fsm_start:
3600 timeout = 0;
3601 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Albert Lee14be71f2005-09-27 17:36:35 +08003603 switch (ap->hsm_task_state) {
3604 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return;
3606
Albert Lee14be71f2005-09-27 17:36:35 +08003607 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 ata_pio_block(ap);
3609 break;
3610
Albert Lee14be71f2005-09-27 17:36:35 +08003611 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003612 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 break;
3614
Albert Lee14be71f2005-09-27 17:36:35 +08003615 case HSM_ST_POLL:
3616 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 timeout = ata_pio_poll(ap);
3618 break;
3619
Albert Lee14be71f2005-09-27 17:36:35 +08003620 case HSM_ST_TMOUT:
3621 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 ata_pio_error(ap);
3623 return;
3624 }
3625
3626 if (timeout)
Tejun Heo8061f5f2006-03-05 15:29:09 +09003627 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003628 else if (!qc_completed)
3629 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632/**
Tejun Heo8061f5f2006-03-05 15:29:09 +09003633 * atapi_packet_task - Write CDB bytes to hardware
3634 * @_data: Port to which ATAPI device is attached.
3635 *
3636 * When device has indicated its readiness to accept
3637 * a CDB, this function is called. Send the CDB.
3638 * If DMA is to be performed, exit immediately.
3639 * Otherwise, we are in polling mode, so poll
3640 * status under operation succeeds or fails.
3641 *
3642 * LOCKING:
3643 * Kernel thread context (may sleep)
3644 */
3645
3646static void atapi_packet_task(void *_data)
3647{
3648 struct ata_port *ap = _data;
3649 struct ata_queued_cmd *qc;
3650 u8 status;
3651
3652 qc = ata_qc_from_tag(ap, ap->active_tag);
3653 WARN_ON(qc == NULL);
3654 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3655
3656 /* sleep-wait for BSY to clear */
3657 DPRINTK("busy wait\n");
3658 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3659 qc->err_mask |= AC_ERR_TIMEOUT;
3660 goto err_out;
3661 }
3662
3663 /* make sure DRQ is set */
3664 status = ata_chk_status(ap);
3665 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3666 qc->err_mask |= AC_ERR_HSM;
3667 goto err_out;
3668 }
3669
3670 /* send SCSI cdb */
3671 DPRINTK("send cdb\n");
3672 WARN_ON(qc->dev->cdb_len < 12);
3673
3674 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3675 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3676 unsigned long flags;
3677
3678 /* Once we're done issuing command and kicking bmdma,
3679 * irq handler takes over. To not lose irq, we need
3680 * to clear NOINTR flag before sending cdb, but
3681 * interrupt handler shouldn't be invoked before we're
3682 * finished. Hence, the following locking.
3683 */
3684 spin_lock_irqsave(&ap->host_set->lock, flags);
3685 ap->flags &= ~ATA_FLAG_NOINTR;
3686 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3687 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3688 ap->ops->bmdma_start(qc); /* initiate bmdma */
3689 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3690 } else {
3691 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3692
3693 /* PIO commands are handled by polling */
3694 ap->hsm_task_state = HSM_ST;
3695 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3696 }
3697
3698 return;
3699
3700err_out:
3701 ata_poll_qc_complete(qc);
3702}
3703
3704/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 * ata_qc_timeout - Handle timeout of queued command
3706 * @qc: Command that timed out
3707 *
3708 * Some part of the kernel (currently, only the SCSI layer)
3709 * has noticed that the active command on port @ap has not
3710 * completed after a specified length of time. Handle this
3711 * condition by disabling DMA (if necessary) and completing
3712 * transactions, with error if necessary.
3713 *
3714 * This also handles the case of the "lost interrupt", where
3715 * for some reason (possibly hardware bug, possibly driver bug)
3716 * an interrupt was not delivered to the driver, even though the
3717 * transaction completed successfully.
3718 *
3719 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003720 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 */
3722
3723static void ata_qc_timeout(struct ata_queued_cmd *qc)
3724{
3725 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003726 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003728 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 DPRINTK("ENTER\n");
3731
Tejun Heoc18d06f2006-02-02 00:56:10 +09003732 ap->hsm_task_state = HSM_ST_IDLE;
3733
Jeff Garzikb8f61532005-08-25 22:01:20 -04003734 spin_lock_irqsave(&host_set->lock, flags);
3735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 switch (qc->tf.protocol) {
3737
3738 case ATA_PROT_DMA:
3739 case ATA_PROT_ATAPI_DMA:
3740 host_stat = ap->ops->bmdma_status(ap);
3741
3742 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003743 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
3745 /* fall through */
3746
3747 default:
3748 ata_altstatus(ap);
3749 drv_stat = ata_chk_status(ap);
3750
3751 /* ack bmdma irq events */
3752 ap->ops->irq_clear(ap);
3753
3754 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3755 ap->id, qc->tf.command, drv_stat, host_stat);
3756
3757 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003758 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 break;
3760 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003761
3762 spin_unlock_irqrestore(&host_set->lock, flags);
3763
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003764 ata_eh_qc_complete(qc);
3765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 DPRINTK("EXIT\n");
3767}
3768
3769/**
3770 * ata_eng_timeout - Handle timeout of queued command
3771 * @ap: Port on which timed-out command is active
3772 *
3773 * Some part of the kernel (currently, only the SCSI layer)
3774 * has noticed that the active command on port @ap has not
3775 * completed after a specified length of time. Handle this
3776 * condition by disabling DMA (if necessary) and completing
3777 * transactions, with error if necessary.
3778 *
3779 * This also handles the case of the "lost interrupt", where
3780 * for some reason (possibly hardware bug, possibly driver bug)
3781 * an interrupt was not delivered to the driver, even though the
3782 * transaction completed successfully.
3783 *
3784 * LOCKING:
3785 * Inherited from SCSI layer (none, can sleep)
3786 */
3787
3788void ata_eng_timeout(struct ata_port *ap)
3789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 DPRINTK("ENTER\n");
3791
Tejun Heof6379022006-02-10 15:10:48 +09003792 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 DPRINTK("EXIT\n");
3795}
3796
3797/**
3798 * ata_qc_new - Request an available ATA command, for queueing
3799 * @ap: Port associated with device @dev
3800 * @dev: Device from whom we request an available command structure
3801 *
3802 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003803 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 */
3805
3806static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3807{
3808 struct ata_queued_cmd *qc = NULL;
3809 unsigned int i;
3810
3811 for (i = 0; i < ATA_MAX_QUEUE; i++)
3812 if (!test_and_set_bit(i, &ap->qactive)) {
3813 qc = ata_qc_from_tag(ap, i);
3814 break;
3815 }
3816
3817 if (qc)
3818 qc->tag = i;
3819
3820 return qc;
3821}
3822
3823/**
3824 * ata_qc_new_init - Request an available ATA command, and initialize it
3825 * @ap: Port associated with device @dev
3826 * @dev: Device from whom we request an available command structure
3827 *
3828 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003829 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 */
3831
3832struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3833 struct ata_device *dev)
3834{
3835 struct ata_queued_cmd *qc;
3836
3837 qc = ata_qc_new(ap);
3838 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 qc->scsicmd = NULL;
3840 qc->ap = ap;
3841 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003843 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 }
3845
3846 return qc;
3847}
3848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849/**
3850 * ata_qc_free - free unused ata_queued_cmd
3851 * @qc: Command to complete
3852 *
3853 * Designed to free unused ata_queued_cmd object
3854 * in case something prevents using it.
3855 *
3856 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003857 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 */
3859void ata_qc_free(struct ata_queued_cmd *qc)
3860{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003861 struct ata_port *ap = qc->ap;
3862 unsigned int tag;
3863
Tejun Heoa46314742006-02-11 19:11:13 +09003864 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Tejun Heo4ba946e2006-01-23 13:09:36 +09003866 qc->flags = 0;
3867 tag = qc->tag;
3868 if (likely(ata_tag_valid(tag))) {
3869 if (tag == ap->active_tag)
3870 ap->active_tag = ATA_TAG_POISON;
3871 qc->tag = ATA_TAG_POISON;
3872 clear_bit(tag, &ap->qactive);
3873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874}
3875
Tejun Heo76014422006-02-11 15:13:49 +09003876void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
Tejun Heoa46314742006-02-11 19:11:13 +09003878 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3879 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
3881 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3882 ata_sg_clean(qc);
3883
Albert Lee3f3791d2005-08-16 14:25:38 +08003884 /* atapi: mark qc as inactive to prevent the interrupt handler
3885 * from completing the command twice later, before the error handler
3886 * is called. (when rc != 0 and atapi request sense is needed)
3887 */
3888 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003891 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892}
3893
3894static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3895{
3896 struct ata_port *ap = qc->ap;
3897
3898 switch (qc->tf.protocol) {
3899 case ATA_PROT_DMA:
3900 case ATA_PROT_ATAPI_DMA:
3901 return 1;
3902
3903 case ATA_PROT_ATAPI:
3904 case ATA_PROT_PIO:
3905 case ATA_PROT_PIO_MULT:
3906 if (ap->flags & ATA_FLAG_PIO_DMA)
3907 return 1;
3908
3909 /* fall through */
3910
3911 default:
3912 return 0;
3913 }
3914
3915 /* never reached */
3916}
3917
3918/**
3919 * ata_qc_issue - issue taskfile to device
3920 * @qc: command to issue to device
3921 *
3922 * Prepare an ATA command to submission to device.
3923 * This includes mapping the data into a DMA-able
3924 * area, filling in the S/G table, and finally
3925 * writing the taskfile to hardware, starting the command.
3926 *
3927 * LOCKING:
3928 * spin_lock_irqsave(host_set lock)
3929 *
3930 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003931 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 */
3933
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003934unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
3936 struct ata_port *ap = qc->ap;
3937
3938 if (ata_should_dma_map(qc)) {
3939 if (qc->flags & ATA_QCFLAG_SG) {
3940 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003941 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3943 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003944 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 }
3946 } else {
3947 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3948 }
3949
3950 ap->ops->qc_prep(qc);
3951
3952 qc->ap->active_tag = qc->tag;
3953 qc->flags |= ATA_QCFLAG_ACTIVE;
3954
3955 return ap->ops->qc_issue(qc);
3956
Tejun Heo8e436af2006-01-23 13:09:36 +09003957sg_err:
3958 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003959 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960}
3961
Edward Falk0baab862005-06-02 18:17:13 -04003962
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963/**
3964 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3965 * @qc: command to issue to device
3966 *
3967 * Using various libata functions and hooks, this function
3968 * starts an ATA command. ATA commands are grouped into
3969 * classes called "protocols", and issuing each type of protocol
3970 * is slightly different.
3971 *
Edward Falk0baab862005-06-02 18:17:13 -04003972 * May be used as the qc_issue() entry in ata_port_operations.
3973 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 * LOCKING:
3975 * spin_lock_irqsave(host_set lock)
3976 *
3977 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003978 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 */
3980
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003981unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
3983 struct ata_port *ap = qc->ap;
3984
3985 ata_dev_select(ap, qc->dev->devno, 1, 0);
3986
3987 switch (qc->tf.protocol) {
3988 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003989 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 break;
3991
3992 case ATA_PROT_DMA:
3993 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3994 ap->ops->bmdma_setup(qc); /* set up bmdma */
3995 ap->ops->bmdma_start(qc); /* initiate bmdma */
3996 break;
3997
3998 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3999 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004000 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08004001 ap->hsm_task_state = HSM_ST;
Tejun Heo8061f5f2006-03-05 15:29:09 +09004002 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 break;
4004
4005 case ATA_PROT_ATAPI:
4006 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004007 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004008 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 break;
4010
4011 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09004012 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05004013 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004014 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 break;
4016
4017 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09004018 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4020 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo8061f5f2006-03-05 15:29:09 +09004021 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 break;
4023
4024 default:
4025 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004026 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
4028
4029 return 0;
4030}
4031
4032/**
Edward Falk0baab862005-06-02 18:17:13 -04004033 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 * @qc: Info associated with this ATA transaction.
4035 *
4036 * LOCKING:
4037 * spin_lock_irqsave(host_set lock)
4038 */
4039
4040static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4041{
4042 struct ata_port *ap = qc->ap;
4043 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4044 u8 dmactl;
4045 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4046
4047 /* load PRD table addr. */
4048 mb(); /* make sure PRD table writes are visible to controller */
4049 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4050
4051 /* specify data direction, triple-check start bit is clear */
4052 dmactl = readb(mmio + ATA_DMA_CMD);
4053 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4054 if (!rw)
4055 dmactl |= ATA_DMA_WR;
4056 writeb(dmactl, mmio + ATA_DMA_CMD);
4057
4058 /* issue r/w command */
4059 ap->ops->exec_command(ap, &qc->tf);
4060}
4061
4062/**
Alan Coxb73fc892005-08-26 16:03:19 +01004063 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 * @qc: Info associated with this ATA transaction.
4065 *
4066 * LOCKING:
4067 * spin_lock_irqsave(host_set lock)
4068 */
4069
4070static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4071{
4072 struct ata_port *ap = qc->ap;
4073 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4074 u8 dmactl;
4075
4076 /* start host DMA transaction */
4077 dmactl = readb(mmio + ATA_DMA_CMD);
4078 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4079
4080 /* Strictly, one may wish to issue a readb() here, to
4081 * flush the mmio write. However, control also passes
4082 * to the hardware at this point, and it will interrupt
4083 * us when we are to resume control. So, in effect,
4084 * we don't care when the mmio write flushes.
4085 * Further, a read of the DMA status register _immediately_
4086 * following the write may not be what certain flaky hardware
4087 * is expected, so I think it is best to not add a readb()
4088 * without first all the MMIO ATA cards/mobos.
4089 * Or maybe I'm just being paranoid.
4090 */
4091}
4092
4093/**
4094 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4095 * @qc: Info associated with this ATA transaction.
4096 *
4097 * LOCKING:
4098 * spin_lock_irqsave(host_set lock)
4099 */
4100
4101static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4102{
4103 struct ata_port *ap = qc->ap;
4104 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4105 u8 dmactl;
4106
4107 /* load PRD table addr. */
4108 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4109
4110 /* specify data direction, triple-check start bit is clear */
4111 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4112 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4113 if (!rw)
4114 dmactl |= ATA_DMA_WR;
4115 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4116
4117 /* issue r/w command */
4118 ap->ops->exec_command(ap, &qc->tf);
4119}
4120
4121/**
4122 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4123 * @qc: Info associated with this ATA transaction.
4124 *
4125 * LOCKING:
4126 * spin_lock_irqsave(host_set lock)
4127 */
4128
4129static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4130{
4131 struct ata_port *ap = qc->ap;
4132 u8 dmactl;
4133
4134 /* start host DMA transaction */
4135 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4136 outb(dmactl | ATA_DMA_START,
4137 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4138}
4139
Edward Falk0baab862005-06-02 18:17:13 -04004140
4141/**
4142 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4143 * @qc: Info associated with this ATA transaction.
4144 *
4145 * Writes the ATA_DMA_START flag to the DMA command register.
4146 *
4147 * May be used as the bmdma_start() entry in ata_port_operations.
4148 *
4149 * LOCKING:
4150 * spin_lock_irqsave(host_set lock)
4151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152void ata_bmdma_start(struct ata_queued_cmd *qc)
4153{
4154 if (qc->ap->flags & ATA_FLAG_MMIO)
4155 ata_bmdma_start_mmio(qc);
4156 else
4157 ata_bmdma_start_pio(qc);
4158}
4159
Edward Falk0baab862005-06-02 18:17:13 -04004160
4161/**
4162 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4163 * @qc: Info associated with this ATA transaction.
4164 *
4165 * Writes address of PRD table to device's PRD Table Address
4166 * register, sets the DMA control register, and calls
4167 * ops->exec_command() to start the transfer.
4168 *
4169 * May be used as the bmdma_setup() entry in ata_port_operations.
4170 *
4171 * LOCKING:
4172 * spin_lock_irqsave(host_set lock)
4173 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174void ata_bmdma_setup(struct ata_queued_cmd *qc)
4175{
4176 if (qc->ap->flags & ATA_FLAG_MMIO)
4177 ata_bmdma_setup_mmio(qc);
4178 else
4179 ata_bmdma_setup_pio(qc);
4180}
4181
Edward Falk0baab862005-06-02 18:17:13 -04004182
4183/**
4184 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004185 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004186 *
4187 * Clear interrupt and error flags in DMA status register.
4188 *
4189 * May be used as the irq_clear() entry in ata_port_operations.
4190 *
4191 * LOCKING:
4192 * spin_lock_irqsave(host_set lock)
4193 */
4194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195void ata_bmdma_irq_clear(struct ata_port *ap)
4196{
4197 if (ap->flags & ATA_FLAG_MMIO) {
4198 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4199 writeb(readb(mmio), mmio);
4200 } else {
4201 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4202 outb(inb(addr), addr);
4203 }
4204
4205}
4206
Edward Falk0baab862005-06-02 18:17:13 -04004207
4208/**
4209 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004210 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004211 *
4212 * Read and return BMDMA status register.
4213 *
4214 * May be used as the bmdma_status() entry in ata_port_operations.
4215 *
4216 * LOCKING:
4217 * spin_lock_irqsave(host_set lock)
4218 */
4219
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220u8 ata_bmdma_status(struct ata_port *ap)
4221{
4222 u8 host_stat;
4223 if (ap->flags & ATA_FLAG_MMIO) {
4224 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4225 host_stat = readb(mmio + ATA_DMA_STATUS);
4226 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004227 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 return host_stat;
4229}
4230
Edward Falk0baab862005-06-02 18:17:13 -04004231
4232/**
4233 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004234 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004235 *
4236 * Clears the ATA_DMA_START flag in the dma control register
4237 *
4238 * May be used as the bmdma_stop() entry in ata_port_operations.
4239 *
4240 * LOCKING:
4241 * spin_lock_irqsave(host_set lock)
4242 */
4243
Alan Coxb73fc892005-08-26 16:03:19 +01004244void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Alan Coxb73fc892005-08-26 16:03:19 +01004246 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 if (ap->flags & ATA_FLAG_MMIO) {
4248 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4249
4250 /* clear start/stop bit */
4251 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4252 mmio + ATA_DMA_CMD);
4253 } else {
4254 /* clear start/stop bit */
4255 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4256 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4257 }
4258
4259 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4260 ata_altstatus(ap); /* dummy read */
4261}
4262
4263/**
4264 * ata_host_intr - Handle host interrupt for given (port, task)
4265 * @ap: Port on which interrupt arrived (possibly...)
4266 * @qc: Taskfile currently active in engine
4267 *
4268 * Handle host interrupt for given queued command. Currently,
4269 * only DMA interrupts are handled. All other commands are
4270 * handled via polling with interrupts disabled (nIEN bit).
4271 *
4272 * LOCKING:
4273 * spin_lock_irqsave(host_set lock)
4274 *
4275 * RETURNS:
4276 * One if interrupt was handled, zero if not (shared irq).
4277 */
4278
4279inline unsigned int ata_host_intr (struct ata_port *ap,
4280 struct ata_queued_cmd *qc)
4281{
4282 u8 status, host_stat;
4283
4284 switch (qc->tf.protocol) {
4285
4286 case ATA_PROT_DMA:
4287 case ATA_PROT_ATAPI_DMA:
4288 case ATA_PROT_ATAPI:
4289 /* check status of DMA engine */
4290 host_stat = ap->ops->bmdma_status(ap);
4291 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4292
4293 /* if it's not our irq... */
4294 if (!(host_stat & ATA_DMA_INTR))
4295 goto idle_irq;
4296
4297 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004298 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
4300 /* fall through */
4301
4302 case ATA_PROT_ATAPI_NODATA:
4303 case ATA_PROT_NODATA:
4304 /* check altstatus */
4305 status = ata_altstatus(ap);
4306 if (status & ATA_BUSY)
4307 goto idle_irq;
4308
4309 /* check main status, clearing INTRQ */
4310 status = ata_chk_status(ap);
4311 if (unlikely(status & ATA_BUSY))
4312 goto idle_irq;
4313 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4314 ap->id, qc->tf.protocol, status);
4315
4316 /* ack bmdma irq events */
4317 ap->ops->irq_clear(ap);
4318
4319 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004320 qc->err_mask |= ac_err_mask(status);
4321 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 break;
4323
4324 default:
4325 goto idle_irq;
4326 }
4327
4328 return 1; /* irq handled */
4329
4330idle_irq:
4331 ap->stats.idle_irq++;
4332
4333#ifdef ATA_IRQ_TRAP
4334 if ((ap->stats.idle_irq % 1000) == 0) {
4335 handled = 1;
4336 ata_irq_ack(ap, 0); /* debug trap */
4337 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4338 }
4339#endif
4340 return 0; /* irq not handled */
4341}
4342
4343/**
4344 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004345 * @irq: irq line (unused)
4346 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 * @regs: unused
4348 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004349 * Default interrupt handler for PCI IDE devices. Calls
4350 * ata_host_intr() for each port that is not disabled.
4351 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004353 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 *
4355 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004356 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 */
4358
4359irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4360{
4361 struct ata_host_set *host_set = dev_instance;
4362 unsigned int i;
4363 unsigned int handled = 0;
4364 unsigned long flags;
4365
4366 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4367 spin_lock_irqsave(&host_set->lock, flags);
4368
4369 for (i = 0; i < host_set->n_ports; i++) {
4370 struct ata_port *ap;
4371
4372 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004373 if (ap &&
4374 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 struct ata_queued_cmd *qc;
4376
4377 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004378 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4379 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 handled |= ata_host_intr(ap, qc);
4381 }
4382 }
4383
4384 spin_unlock_irqrestore(&host_set->lock, flags);
4385
4386 return IRQ_RETVAL(handled);
4387}
4388
Edward Falk0baab862005-06-02 18:17:13 -04004389
Jens Axboe9b847542006-01-06 09:28:07 +01004390/*
4391 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4392 * without filling any other registers
4393 */
4394static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4395 u8 cmd)
4396{
4397 struct ata_taskfile tf;
4398 int err;
4399
4400 ata_tf_init(ap, &tf, dev->devno);
4401
4402 tf.command = cmd;
4403 tf.flags |= ATA_TFLAG_DEVICE;
4404 tf.protocol = ATA_PROT_NODATA;
4405
4406 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4407 if (err)
4408 printk(KERN_ERR "%s: ata command failed: %d\n",
4409 __FUNCTION__, err);
4410
4411 return err;
4412}
4413
4414static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4415{
4416 u8 cmd;
4417
4418 if (!ata_try_flush_cache(dev))
4419 return 0;
4420
4421 if (ata_id_has_flush_ext(dev->id))
4422 cmd = ATA_CMD_FLUSH_EXT;
4423 else
4424 cmd = ATA_CMD_FLUSH;
4425
4426 return ata_do_simple_cmd(ap, dev, cmd);
4427}
4428
4429static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4430{
4431 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4432}
4433
4434static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4435{
4436 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4437}
4438
4439/**
4440 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004441 * @ap: port the device is connected to
4442 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004443 *
4444 * Kick the drive back into action, by sending it an idle immediate
4445 * command and making sure its transfer mode matches between drive
4446 * and host.
4447 *
4448 */
4449int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4450{
4451 if (ap->flags & ATA_FLAG_SUSPENDED) {
4452 ap->flags &= ~ATA_FLAG_SUSPENDED;
4453 ata_set_mode(ap);
4454 }
4455 if (!ata_dev_present(dev))
4456 return 0;
4457 if (dev->class == ATA_DEV_ATA)
4458 ata_start_drive(ap, dev);
4459
4460 return 0;
4461}
4462
4463/**
4464 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004465 * @ap: port the device is connected to
4466 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004467 *
4468 * Flush the cache on the drive, if appropriate, then issue a
4469 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004470 */
4471int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4472{
4473 if (!ata_dev_present(dev))
4474 return 0;
4475 if (dev->class == ATA_DEV_ATA)
4476 ata_flush_cache(ap, dev);
4477
4478 ata_standby_drive(ap, dev);
4479 ap->flags |= ATA_FLAG_SUSPENDED;
4480 return 0;
4481}
4482
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004483/**
4484 * ata_port_start - Set port up for dma.
4485 * @ap: Port to initialize
4486 *
4487 * Called just after data structures for each port are
4488 * initialized. Allocates space for PRD table.
4489 *
4490 * May be used as the port_start() entry in ata_port_operations.
4491 *
4492 * LOCKING:
4493 * Inherited from caller.
4494 */
4495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496int ata_port_start (struct ata_port *ap)
4497{
4498 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004499 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
4501 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4502 if (!ap->prd)
4503 return -ENOMEM;
4504
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004505 rc = ata_pad_alloc(ap, dev);
4506 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004507 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004508 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004509 }
4510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4512
4513 return 0;
4514}
4515
Edward Falk0baab862005-06-02 18:17:13 -04004516
4517/**
4518 * ata_port_stop - Undo ata_port_start()
4519 * @ap: Port to shut down
4520 *
4521 * Frees the PRD table.
4522 *
4523 * May be used as the port_stop() entry in ata_port_operations.
4524 *
4525 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004526 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004527 */
4528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529void ata_port_stop (struct ata_port *ap)
4530{
4531 struct device *dev = ap->host_set->dev;
4532
4533 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004534 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535}
4536
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004537void ata_host_stop (struct ata_host_set *host_set)
4538{
4539 if (host_set->mmio_base)
4540 iounmap(host_set->mmio_base);
4541}
4542
4543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544/**
4545 * ata_host_remove - Unregister SCSI host structure with upper layers
4546 * @ap: Port to unregister
4547 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4548 *
4549 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004550 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 */
4552
4553static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4554{
4555 struct Scsi_Host *sh = ap->host;
4556
4557 DPRINTK("ENTER\n");
4558
4559 if (do_unregister)
4560 scsi_remove_host(sh);
4561
4562 ap->ops->port_stop(ap);
4563}
4564
4565/**
4566 * ata_host_init - Initialize an ata_port structure
4567 * @ap: Structure to initialize
4568 * @host: associated SCSI mid-layer structure
4569 * @host_set: Collection of hosts to which @ap belongs
4570 * @ent: Probe information provided by low-level driver
4571 * @port_no: Port number associated with this ata_port
4572 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004573 * Initialize a new ata_port structure, and its associated
4574 * scsi_host.
4575 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004577 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 */
4579
4580static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4581 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004582 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
4584 unsigned int i;
4585
4586 host->max_id = 16;
4587 host->max_lun = 1;
4588 host->max_channel = 1;
4589 host->unique_id = ata_unique_id++;
4590 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 ap->flags = ATA_FLAG_PORT_DISABLED;
4593 ap->id = host->unique_id;
4594 ap->host = host;
4595 ap->ctl = ATA_DEVCTL_OBS;
4596 ap->host_set = host_set;
4597 ap->port_no = port_no;
4598 ap->hard_port_no =
4599 ent->legacy_mode ? ent->hard_port_no : port_no;
4600 ap->pio_mask = ent->pio_mask;
4601 ap->mwdma_mask = ent->mwdma_mask;
4602 ap->udma_mask = ent->udma_mask;
4603 ap->flags |= ent->host_flags;
4604 ap->ops = ent->port_ops;
4605 ap->cbl = ATA_CBL_NONE;
4606 ap->active_tag = ATA_TAG_POISON;
4607 ap->last_ctl = 0xFF;
4608
Tejun Heo86e45b62006-03-05 15:29:09 +09004609 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004610 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
4612 for (i = 0; i < ATA_MAX_DEVICES; i++)
4613 ap->device[i].devno = i;
4614
4615#ifdef ATA_IRQ_TRAP
4616 ap->stats.unhandled_irq = 1;
4617 ap->stats.idle_irq = 1;
4618#endif
4619
4620 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4621}
4622
4623/**
4624 * ata_host_add - Attach low-level ATA driver to system
4625 * @ent: Information provided by low-level driver
4626 * @host_set: Collections of ports to which we add
4627 * @port_no: Port number associated with this host
4628 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004629 * Attach low-level ATA driver to system.
4630 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004632 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 *
4634 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004635 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 */
4637
Jeff Garzik057ace52005-10-22 14:27:05 -04004638static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 struct ata_host_set *host_set,
4640 unsigned int port_no)
4641{
4642 struct Scsi_Host *host;
4643 struct ata_port *ap;
4644 int rc;
4645
4646 DPRINTK("ENTER\n");
4647 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4648 if (!host)
4649 return NULL;
4650
4651 ap = (struct ata_port *) &host->hostdata[0];
4652
4653 ata_host_init(ap, host, host_set, ent, port_no);
4654
4655 rc = ap->ops->port_start(ap);
4656 if (rc)
4657 goto err_out;
4658
4659 return ap;
4660
4661err_out:
4662 scsi_host_put(host);
4663 return NULL;
4664}
4665
4666/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004667 * ata_device_add - Register hardware device with ATA and SCSI layers
4668 * @ent: Probe information describing hardware device to be registered
4669 *
4670 * This function processes the information provided in the probe
4671 * information struct @ent, allocates the necessary ATA and SCSI
4672 * host information structures, initializes them, and registers
4673 * everything with requisite kernel subsystems.
4674 *
4675 * This function requests irqs, probes the ATA bus, and probes
4676 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 *
4678 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004679 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 *
4681 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004682 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 */
4684
Jeff Garzik057ace52005-10-22 14:27:05 -04004685int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686{
4687 unsigned int count = 0, i;
4688 struct device *dev = ent->dev;
4689 struct ata_host_set *host_set;
4690
4691 DPRINTK("ENTER\n");
4692 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004693 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4695 if (!host_set)
4696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 spin_lock_init(&host_set->lock);
4698
4699 host_set->dev = dev;
4700 host_set->n_ports = ent->n_ports;
4701 host_set->irq = ent->irq;
4702 host_set->mmio_base = ent->mmio_base;
4703 host_set->private_data = ent->private_data;
4704 host_set->ops = ent->port_ops;
4705
4706 /* register each port bound to this device */
4707 for (i = 0; i < ent->n_ports; i++) {
4708 struct ata_port *ap;
4709 unsigned long xfer_mode_mask;
4710
4711 ap = ata_host_add(ent, host_set, i);
4712 if (!ap)
4713 goto err_out;
4714
4715 host_set->ports[i] = ap;
4716 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4717 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4718 (ap->pio_mask << ATA_SHIFT_PIO);
4719
4720 /* print per-port info to dmesg */
4721 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4722 "bmdma 0x%lX irq %lu\n",
4723 ap->id,
4724 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4725 ata_mode_string(xfer_mode_mask),
4726 ap->ioaddr.cmd_addr,
4727 ap->ioaddr.ctl_addr,
4728 ap->ioaddr.bmdma_addr,
4729 ent->irq);
4730
4731 ata_chk_status(ap);
4732 host_set->ops->irq_clear(ap);
4733 count++;
4734 }
4735
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004736 if (!count)
4737 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
4739 /* obtain irq, that is shared between channels */
4740 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4741 DRV_NAME, host_set))
4742 goto err_out;
4743
4744 /* perform each probe synchronously */
4745 DPRINTK("probe begin\n");
4746 for (i = 0; i < count; i++) {
4747 struct ata_port *ap;
4748 int rc;
4749
4750 ap = host_set->ports[i];
4751
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004752 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004754 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
4756 if (rc) {
4757 /* FIXME: do something useful here?
4758 * Current libata behavior will
4759 * tear down everything when
4760 * the module is removed
4761 * or the h/w is unplugged.
4762 */
4763 }
4764
4765 rc = scsi_add_host(ap->host, dev);
4766 if (rc) {
4767 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4768 ap->id);
4769 /* FIXME: do something useful here */
4770 /* FIXME: handle unconditional calls to
4771 * scsi_scan_host and ata_host_remove, below,
4772 * at the very least
4773 */
4774 }
4775 }
4776
4777 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004778 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 for (i = 0; i < count; i++) {
4780 struct ata_port *ap = host_set->ports[i];
4781
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004782 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 }
4784
4785 dev_set_drvdata(dev, host_set);
4786
4787 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4788 return ent->n_ports; /* success */
4789
4790err_out:
4791 for (i = 0; i < count; i++) {
4792 ata_host_remove(host_set->ports[i], 1);
4793 scsi_host_put(host_set->ports[i]->host);
4794 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004795err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 kfree(host_set);
4797 VPRINTK("EXIT, returning 0\n");
4798 return 0;
4799}
4800
4801/**
Alan Cox17b14452005-09-15 15:44:00 +01004802 * ata_host_set_remove - PCI layer callback for device removal
4803 * @host_set: ATA host set that was removed
4804 *
4805 * Unregister all objects associated with this host set. Free those
4806 * objects.
4807 *
4808 * LOCKING:
4809 * Inherited from calling layer (may sleep).
4810 */
4811
Alan Cox17b14452005-09-15 15:44:00 +01004812void ata_host_set_remove(struct ata_host_set *host_set)
4813{
4814 struct ata_port *ap;
4815 unsigned int i;
4816
4817 for (i = 0; i < host_set->n_ports; i++) {
4818 ap = host_set->ports[i];
4819 scsi_remove_host(ap->host);
4820 }
4821
4822 free_irq(host_set->irq, host_set);
4823
4824 for (i = 0; i < host_set->n_ports; i++) {
4825 ap = host_set->ports[i];
4826
4827 ata_scsi_release(ap->host);
4828
4829 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4830 struct ata_ioports *ioaddr = &ap->ioaddr;
4831
4832 if (ioaddr->cmd_addr == 0x1f0)
4833 release_region(0x1f0, 8);
4834 else if (ioaddr->cmd_addr == 0x170)
4835 release_region(0x170, 8);
4836 }
4837
4838 scsi_host_put(ap->host);
4839 }
4840
4841 if (host_set->ops->host_stop)
4842 host_set->ops->host_stop(host_set);
4843
4844 kfree(host_set);
4845}
4846
4847/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 * ata_scsi_release - SCSI layer callback hook for host unload
4849 * @host: libata host to be unloaded
4850 *
4851 * Performs all duties necessary to shut down a libata port...
4852 * Kill port kthread, disable port, and release resources.
4853 *
4854 * LOCKING:
4855 * Inherited from SCSI layer.
4856 *
4857 * RETURNS:
4858 * One.
4859 */
4860
4861int ata_scsi_release(struct Scsi_Host *host)
4862{
4863 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004864 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866 DPRINTK("ENTER\n");
4867
4868 ap->ops->port_disable(ap);
4869 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004870 for (i = 0; i < ATA_MAX_DEVICES; i++)
4871 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 DPRINTK("EXIT\n");
4874 return 1;
4875}
4876
4877/**
4878 * ata_std_ports - initialize ioaddr with standard port offsets.
4879 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004880 *
4881 * Utility function which initializes data_addr, error_addr,
4882 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4883 * device_addr, status_addr, and command_addr to standard offsets
4884 * relative to cmd_addr.
4885 *
4886 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 */
Edward Falk0baab862005-06-02 18:17:13 -04004888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889void ata_std_ports(struct ata_ioports *ioaddr)
4890{
4891 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4892 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4893 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4894 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4895 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4896 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4897 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4898 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4899 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4900 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4901}
4902
Edward Falk0baab862005-06-02 18:17:13 -04004903
Jeff Garzik374b1872005-08-30 05:42:52 -04004904#ifdef CONFIG_PCI
4905
4906void ata_pci_host_stop (struct ata_host_set *host_set)
4907{
4908 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4909
4910 pci_iounmap(pdev, host_set->mmio_base);
4911}
4912
Edward Falk0baab862005-06-02 18:17:13 -04004913/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 * ata_pci_remove_one - PCI layer callback for device removal
4915 * @pdev: PCI device that was removed
4916 *
4917 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004918 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 * Handle this by unregistering all objects associated
4920 * with this PCI device. Free those objects. Then finally
4921 * release PCI resources and disable device.
4922 *
4923 * LOCKING:
4924 * Inherited from PCI layer (may sleep).
4925 */
4926
4927void ata_pci_remove_one (struct pci_dev *pdev)
4928{
4929 struct device *dev = pci_dev_to_dev(pdev);
4930 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
Alan Cox17b14452005-09-15 15:44:00 +01004932 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 pci_release_regions(pdev);
4934 pci_disable_device(pdev);
4935 dev_set_drvdata(dev, NULL);
4936}
4937
4938/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004939int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940{
4941 unsigned long tmp = 0;
4942
4943 switch (bits->width) {
4944 case 1: {
4945 u8 tmp8 = 0;
4946 pci_read_config_byte(pdev, bits->reg, &tmp8);
4947 tmp = tmp8;
4948 break;
4949 }
4950 case 2: {
4951 u16 tmp16 = 0;
4952 pci_read_config_word(pdev, bits->reg, &tmp16);
4953 tmp = tmp16;
4954 break;
4955 }
4956 case 4: {
4957 u32 tmp32 = 0;
4958 pci_read_config_dword(pdev, bits->reg, &tmp32);
4959 tmp = tmp32;
4960 break;
4961 }
4962
4963 default:
4964 return -EINVAL;
4965 }
4966
4967 tmp &= bits->mask;
4968
4969 return (tmp == bits->val) ? 1 : 0;
4970}
Jens Axboe9b847542006-01-06 09:28:07 +01004971
4972int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4973{
4974 pci_save_state(pdev);
4975 pci_disable_device(pdev);
4976 pci_set_power_state(pdev, PCI_D3hot);
4977 return 0;
4978}
4979
4980int ata_pci_device_resume(struct pci_dev *pdev)
4981{
4982 pci_set_power_state(pdev, PCI_D0);
4983 pci_restore_state(pdev);
4984 pci_enable_device(pdev);
4985 pci_set_master(pdev);
4986 return 0;
4987}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988#endif /* CONFIG_PCI */
4989
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991static int __init ata_init(void)
4992{
4993 ata_wq = create_workqueue("ata");
4994 if (!ata_wq)
4995 return -ENOMEM;
4996
4997 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4998 return 0;
4999}
5000
5001static void __exit ata_exit(void)
5002{
5003 destroy_workqueue(ata_wq);
5004}
5005
5006module_init(ata_init);
5007module_exit(ata_exit);
5008
Jeff Garzik67846b32005-10-05 02:58:32 -04005009static unsigned long ratelimit_time;
5010static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5011
5012int ata_ratelimit(void)
5013{
5014 int rc;
5015 unsigned long flags;
5016
5017 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5018
5019 if (time_after(jiffies, ratelimit_time)) {
5020 rc = 1;
5021 ratelimit_time = jiffies + (HZ/5);
5022 } else
5023 rc = 0;
5024
5025 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5026
5027 return rc;
5028}
5029
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030/*
5031 * libata is essentially a library of internal helper functions for
5032 * low-level ATA host controller drivers. As such, the API/ABI is
5033 * likely to change as new drivers are added and updated.
5034 * Do not depend on ABI/API stability.
5035 */
5036
5037EXPORT_SYMBOL_GPL(ata_std_bios_param);
5038EXPORT_SYMBOL_GPL(ata_std_ports);
5039EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005040EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041EXPORT_SYMBOL_GPL(ata_sg_init);
5042EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09005043EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5045EXPORT_SYMBOL_GPL(ata_eng_timeout);
5046EXPORT_SYMBOL_GPL(ata_tf_load);
5047EXPORT_SYMBOL_GPL(ata_tf_read);
5048EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5049EXPORT_SYMBOL_GPL(ata_std_dev_select);
5050EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5051EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5052EXPORT_SYMBOL_GPL(ata_check_status);
5053EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054EXPORT_SYMBOL_GPL(ata_exec_command);
5055EXPORT_SYMBOL_GPL(ata_port_start);
5056EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005057EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058EXPORT_SYMBOL_GPL(ata_interrupt);
5059EXPORT_SYMBOL_GPL(ata_qc_prep);
5060EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5061EXPORT_SYMBOL_GPL(ata_bmdma_start);
5062EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5063EXPORT_SYMBOL_GPL(ata_bmdma_status);
5064EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5065EXPORT_SYMBOL_GPL(ata_port_probe);
5066EXPORT_SYMBOL_GPL(sata_phy_reset);
5067EXPORT_SYMBOL_GPL(__sata_phy_reset);
5068EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005069EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005070EXPORT_SYMBOL_GPL(ata_std_softreset);
5071EXPORT_SYMBOL_GPL(sata_std_hardreset);
5072EXPORT_SYMBOL_GPL(ata_std_postreset);
5073EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005074EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09005075EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005077EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005078EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09005079EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5081EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09005082EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083EXPORT_SYMBOL_GPL(ata_scsi_error);
5084EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5085EXPORT_SYMBOL_GPL(ata_scsi_release);
5086EXPORT_SYMBOL_GPL(ata_host_intr);
5087EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09005088EXPORT_SYMBOL_GPL(ata_id_string);
5089EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005091EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5092EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005094EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005095EXPORT_SYMBOL_GPL(ata_timing_compute);
5096EXPORT_SYMBOL_GPL(ata_timing_merge);
5097
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098#ifdef CONFIG_PCI
5099EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005100EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5102EXPORT_SYMBOL_GPL(ata_pci_init_one);
5103EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005104EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5105EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005107
5108EXPORT_SYMBOL_GPL(ata_device_suspend);
5109EXPORT_SYMBOL_GPL(ata_device_resume);
5110EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5111EXPORT_SYMBOL_GPL(ata_scsi_device_resume);