blob: 57b3240ee867f294496ce88dd020325439f1b0bf [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,
Albert Lee00b6f5e2006-03-27 16:39:18 +080065 struct ata_device *dev,
66 u16 heads,
67 u16 sectors);
Tejun Heo83206a22006-03-24 15:25:31 +090068static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
69 struct ata_device *dev);
Tejun Heoacf356b2006-03-24 14:07:50 +090070static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static unsigned int ata_unique_id = 1;
73static struct workqueue_struct *ata_wq;
74
Jeff Garzik418dc1f2006-03-11 20:50:08 -050075int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040076module_param(atapi_enabled, int, 0444);
77MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
78
Jeff Garzikc3c013a2006-02-27 22:31:19 -050079int libata_fua = 0;
80module_param_named(fua, libata_fua, int, 0444);
81MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083MODULE_AUTHOR("Jeff Garzik");
84MODULE_DESCRIPTION("Library module for ATA devices");
85MODULE_LICENSE("GPL");
86MODULE_VERSION(DRV_VERSION);
87
Edward Falk0baab862005-06-02 18:17:13 -040088
89/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
91 * @tf: Taskfile to convert
92 * @fis: Buffer into which data will output
93 * @pmp: Port multiplier port
94 *
95 * Converts a standard ATA taskfile to a Serial ATA
96 * FIS structure (Register - Host to Device).
97 *
98 * LOCKING:
99 * Inherited from caller.
100 */
101
Jeff Garzik057ace52005-10-22 14:27:05 -0400102void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 fis[0] = 0x27; /* Register - Host to Device FIS */
105 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
106 bit 7 indicates Command FIS */
107 fis[2] = tf->command;
108 fis[3] = tf->feature;
109
110 fis[4] = tf->lbal;
111 fis[5] = tf->lbam;
112 fis[6] = tf->lbah;
113 fis[7] = tf->device;
114
115 fis[8] = tf->hob_lbal;
116 fis[9] = tf->hob_lbam;
117 fis[10] = tf->hob_lbah;
118 fis[11] = tf->hob_feature;
119
120 fis[12] = tf->nsect;
121 fis[13] = tf->hob_nsect;
122 fis[14] = 0;
123 fis[15] = tf->ctl;
124
125 fis[16] = 0;
126 fis[17] = 0;
127 fis[18] = 0;
128 fis[19] = 0;
129}
130
131/**
132 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
133 * @fis: Buffer from which data will be input
134 * @tf: Taskfile to output
135 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500136 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * LOCKING:
139 * Inherited from caller.
140 */
141
Jeff Garzik057ace52005-10-22 14:27:05 -0400142void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 tf->command = fis[2]; /* status */
145 tf->feature = fis[3]; /* error */
146
147 tf->lbal = fis[4];
148 tf->lbam = fis[5];
149 tf->lbah = fis[6];
150 tf->device = fis[7];
151
152 tf->hob_lbal = fis[8];
153 tf->hob_lbam = fis[9];
154 tf->hob_lbah = fis[10];
155
156 tf->nsect = fis[12];
157 tf->hob_nsect = fis[13];
158}
159
Albert Lee8cbd6df2005-10-12 15:06:27 +0800160static const u8 ata_rw_cmds[] = {
161 /* pio multi */
162 ATA_CMD_READ_MULTI,
163 ATA_CMD_WRITE_MULTI,
164 ATA_CMD_READ_MULTI_EXT,
165 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100166 0,
167 0,
168 0,
169 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800170 /* pio */
171 ATA_CMD_PIO_READ,
172 ATA_CMD_PIO_WRITE,
173 ATA_CMD_PIO_READ_EXT,
174 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100175 0,
176 0,
177 0,
178 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800179 /* dma */
180 ATA_CMD_READ,
181 ATA_CMD_WRITE,
182 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100183 ATA_CMD_WRITE_EXT,
184 0,
185 0,
186 0,
187 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800188};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800191 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
192 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500194 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800195 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
197 * LOCKING:
198 * caller.
199 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100200int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800202 struct ata_taskfile *tf = &qc->tf;
203 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100204 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500207
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100208 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800209 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
210 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Albert Lee8cbd6df2005-10-12 15:06:27 +0800212 if (dev->flags & ATA_DFLAG_PIO) {
213 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100214 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000215 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
216 /* Unable to use DMA due to host limitation */
217 tf->protocol = ATA_PROT_PIO;
Albert Leeaef9d532006-02-08 16:37:43 +0800218 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800219 } else {
220 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100221 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100224 cmd = ata_rw_cmds[index + fua + lba48 + write];
225 if (cmd) {
226 tf->command = cmd;
227 return 0;
228 }
229 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Tejun Heocb95d562006-03-06 04:31:56 +0900232/**
233 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
234 * @pio_mask: pio_mask
235 * @mwdma_mask: mwdma_mask
236 * @udma_mask: udma_mask
237 *
238 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
239 * unsigned int xfer_mask.
240 *
241 * LOCKING:
242 * None.
243 *
244 * RETURNS:
245 * Packed xfer_mask.
246 */
247static unsigned int ata_pack_xfermask(unsigned int pio_mask,
248 unsigned int mwdma_mask,
249 unsigned int udma_mask)
250{
251 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
252 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
253 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
254}
255
Tejun Heoc0489e42006-03-24 14:07:49 +0900256/**
257 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
258 * @xfer_mask: xfer_mask to unpack
259 * @pio_mask: resulting pio_mask
260 * @mwdma_mask: resulting mwdma_mask
261 * @udma_mask: resulting udma_mask
262 *
263 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
264 * Any NULL distination masks will be ignored.
265 */
266static void ata_unpack_xfermask(unsigned int xfer_mask,
267 unsigned int *pio_mask,
268 unsigned int *mwdma_mask,
269 unsigned int *udma_mask)
270{
271 if (pio_mask)
272 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
273 if (mwdma_mask)
274 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
275 if (udma_mask)
276 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
277}
278
Tejun Heocb95d562006-03-06 04:31:56 +0900279static const struct ata_xfer_ent {
Tejun Heobe9a50c82006-03-31 22:48:52 +0900280 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900281 u8 base;
282} ata_xfer_tbl[] = {
283 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
284 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
285 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
286 { -1, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287};
288
289/**
Tejun Heocb95d562006-03-06 04:31:56 +0900290 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
291 * @xfer_mask: xfer_mask of interest
292 *
293 * Return matching XFER_* value for @xfer_mask. Only the highest
294 * bit of @xfer_mask is considered.
295 *
296 * LOCKING:
297 * None.
298 *
299 * RETURNS:
300 * Matching XFER_* value, 0 if no match found.
301 */
302static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
303{
304 int highbit = fls(xfer_mask) - 1;
305 const struct ata_xfer_ent *ent;
306
307 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
308 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
309 return ent->base + highbit - ent->shift;
310 return 0;
311}
312
313/**
314 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
315 * @xfer_mode: XFER_* of interest
316 *
317 * Return matching xfer_mask for @xfer_mode.
318 *
319 * LOCKING:
320 * None.
321 *
322 * RETURNS:
323 * Matching xfer_mask, 0 if no match found.
324 */
325static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
326{
327 const struct ata_xfer_ent *ent;
328
329 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
330 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
331 return 1 << (ent->shift + xfer_mode - ent->base);
332 return 0;
333}
334
335/**
336 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
337 * @xfer_mode: XFER_* of interest
338 *
339 * Return matching xfer_shift for @xfer_mode.
340 *
341 * LOCKING:
342 * None.
343 *
344 * RETURNS:
345 * Matching xfer_shift, -1 if no match found.
346 */
347static int ata_xfer_mode2shift(unsigned int xfer_mode)
348{
349 const struct ata_xfer_ent *ent;
350
351 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
352 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
353 return ent->shift;
354 return -1;
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900358 * ata_mode_string - convert xfer_mask to string
359 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
361 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900362 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *
364 * LOCKING:
365 * None.
366 *
367 * RETURNS:
368 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900369 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900371static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Tejun Heo75f554b2006-03-06 04:31:57 +0900373 static const char * const xfer_mode_str[] = {
374 "PIO0",
375 "PIO1",
376 "PIO2",
377 "PIO3",
378 "PIO4",
379 "MWDMA0",
380 "MWDMA1",
381 "MWDMA2",
382 "UDMA/16",
383 "UDMA/25",
384 "UDMA/33",
385 "UDMA/44",
386 "UDMA/66",
387 "UDMA/100",
388 "UDMA/133",
389 "UDMA7",
390 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900391 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900393 highbit = fls(xfer_mask) - 1;
394 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
395 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Tejun Heo4c360c82006-04-01 01:38:17 +0900399static const char *sata_spd_string(unsigned int spd)
400{
401 static const char * const spd_str[] = {
402 "1.5 Gbps",
403 "3.0 Gbps",
404 };
405
406 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
407 return "<unknown>";
408 return spd_str[spd - 1];
409}
410
Tejun Heo1ad8e7f2006-04-02 18:51:53 +0900411void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
Tejun Heo0b8efb02006-03-24 15:25:31 +0900412{
Tejun Heoe1211e32006-04-01 01:38:18 +0900413 if (ata_dev_enabled(dev)) {
Tejun Heo0b8efb02006-03-24 15:25:31 +0900414 printk(KERN_WARNING "ata%u: dev %u disabled\n",
415 ap->id, dev->devno);
416 dev->class++;
417 }
418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/**
421 * ata_pio_devchk - PATA device presence detection
422 * @ap: ATA channel to examine
423 * @device: Device to examine (starting at zero)
424 *
425 * This technique was originally described in
426 * Hale Landis's ATADRVR (www.ata-atapi.com), and
427 * later found its way into the ATA/ATAPI spec.
428 *
429 * Write a pattern to the ATA shadow registers,
430 * and if a device is present, it will respond by
431 * correctly storing and echoing back the
432 * ATA shadow register contents.
433 *
434 * LOCKING:
435 * caller.
436 */
437
438static unsigned int ata_pio_devchk(struct ata_port *ap,
439 unsigned int device)
440{
441 struct ata_ioports *ioaddr = &ap->ioaddr;
442 u8 nsect, lbal;
443
444 ap->ops->dev_select(ap, device);
445
446 outb(0x55, ioaddr->nsect_addr);
447 outb(0xaa, ioaddr->lbal_addr);
448
449 outb(0xaa, ioaddr->nsect_addr);
450 outb(0x55, ioaddr->lbal_addr);
451
452 outb(0x55, ioaddr->nsect_addr);
453 outb(0xaa, ioaddr->lbal_addr);
454
455 nsect = inb(ioaddr->nsect_addr);
456 lbal = inb(ioaddr->lbal_addr);
457
458 if ((nsect == 0x55) && (lbal == 0xaa))
459 return 1; /* we found a device */
460
461 return 0; /* nothing found */
462}
463
464/**
465 * ata_mmio_devchk - PATA device presence detection
466 * @ap: ATA channel to examine
467 * @device: Device to examine (starting at zero)
468 *
469 * This technique was originally described in
470 * Hale Landis's ATADRVR (www.ata-atapi.com), and
471 * later found its way into the ATA/ATAPI spec.
472 *
473 * Write a pattern to the ATA shadow registers,
474 * and if a device is present, it will respond by
475 * correctly storing and echoing back the
476 * ATA shadow register contents.
477 *
478 * LOCKING:
479 * caller.
480 */
481
482static unsigned int ata_mmio_devchk(struct ata_port *ap,
483 unsigned int device)
484{
485 struct ata_ioports *ioaddr = &ap->ioaddr;
486 u8 nsect, lbal;
487
488 ap->ops->dev_select(ap, device);
489
490 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
491 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
492
493 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
494 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
495
496 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
497 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
498
499 nsect = readb((void __iomem *) ioaddr->nsect_addr);
500 lbal = readb((void __iomem *) ioaddr->lbal_addr);
501
502 if ((nsect == 0x55) && (lbal == 0xaa))
503 return 1; /* we found a device */
504
505 return 0; /* nothing found */
506}
507
508/**
509 * ata_devchk - PATA device presence detection
510 * @ap: ATA channel to examine
511 * @device: Device to examine (starting at zero)
512 *
513 * Dispatch ATA device presence detection, depending
514 * on whether we are using PIO or MMIO to talk to the
515 * ATA shadow registers.
516 *
517 * LOCKING:
518 * caller.
519 */
520
521static unsigned int ata_devchk(struct ata_port *ap,
522 unsigned int device)
523{
524 if (ap->flags & ATA_FLAG_MMIO)
525 return ata_mmio_devchk(ap, device);
526 return ata_pio_devchk(ap, device);
527}
528
529/**
530 * ata_dev_classify - determine device type based on ATA-spec signature
531 * @tf: ATA taskfile register set for device to be identified
532 *
533 * Determine from taskfile register contents whether a device is
534 * ATA or ATAPI, as per "Signature and persistence" section
535 * of ATA/PI spec (volume 1, sect 5.14).
536 *
537 * LOCKING:
538 * None.
539 *
540 * RETURNS:
541 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
542 * the event of failure.
543 */
544
Jeff Garzik057ace52005-10-22 14:27:05 -0400545unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 /* Apple's open source Darwin code hints that some devices only
548 * put a proper signature into the LBA mid/high registers,
549 * So, we only check those. It's sufficient for uniqueness.
550 */
551
552 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
553 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
554 DPRINTK("found ATA device by sig\n");
555 return ATA_DEV_ATA;
556 }
557
558 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
559 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
560 DPRINTK("found ATAPI device by sig\n");
561 return ATA_DEV_ATAPI;
562 }
563
564 DPRINTK("unknown device\n");
565 return ATA_DEV_UNKNOWN;
566}
567
568/**
569 * ata_dev_try_classify - Parse returned ATA device signature
570 * @ap: ATA channel to examine
571 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900572 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
574 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
575 * an ATA/ATAPI-defined set of values is placed in the ATA
576 * shadow registers, indicating the results of device detection
577 * and diagnostics.
578 *
579 * Select the ATA device, and read the values from the ATA shadow
580 * registers. Then parse according to the Error register value,
581 * and the spec-defined values examined by ata_dev_classify().
582 *
583 * LOCKING:
584 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900585 *
586 * RETURNS:
587 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
589
Tejun Heob4dc7622006-01-24 17:05:22 +0900590static unsigned int
591ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct ata_taskfile tf;
594 unsigned int class;
595 u8 err;
596
597 ap->ops->dev_select(ap, device);
598
599 memset(&tf, 0, sizeof(tf));
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400602 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900603 if (r_err)
604 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* see if device passed diags */
607 if (err == 1)
608 /* do nothing */ ;
609 else if ((device == 0) && (err == 0x81))
610 /* do nothing */ ;
611 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900612 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Tejun Heob4dc7622006-01-24 17:05:22 +0900614 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900618 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900620 return ATA_DEV_NONE;
621 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900625 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * @id: IDENTIFY DEVICE results we will examine
627 * @s: string into which data is output
628 * @ofs: offset into identify device page
629 * @len: length of string to return. must be an even number.
630 *
631 * The strings in the IDENTIFY DEVICE page are broken up into
632 * 16-bit chunks. Run through the string, and output each
633 * 8-bit chunk linearly, regardless of platform.
634 *
635 * LOCKING:
636 * caller.
637 */
638
Tejun Heo6a62a042006-02-13 10:02:46 +0900639void ata_id_string(const u16 *id, unsigned char *s,
640 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 unsigned int c;
643
644 while (len > 0) {
645 c = id[ofs] >> 8;
646 *s = c;
647 s++;
648
649 c = id[ofs] & 0xff;
650 *s = c;
651 s++;
652
653 ofs++;
654 len -= 2;
655 }
656}
657
Tejun Heo0e949ff2006-02-12 22:47:04 +0900658/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900659 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900660 * @id: IDENTIFY DEVICE results we will examine
661 * @s: string into which data is output
662 * @ofs: offset into identify device page
663 * @len: length of string to return. must be an odd number.
664 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900665 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900666 * trims trailing spaces and terminates the resulting string with
667 * null. @len must be actual maximum length (even number) + 1.
668 *
669 * LOCKING:
670 * caller.
671 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900672void ata_id_c_string(const u16 *id, unsigned char *s,
673 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900674{
675 unsigned char *p;
676
677 WARN_ON(!(len & 1));
678
Tejun Heo6a62a042006-02-13 10:02:46 +0900679 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900680
681 p = s + strnlen(s, len - 1);
682 while (p > s && p[-1] == ' ')
683 p--;
684 *p = '\0';
685}
Edward Falk0baab862005-06-02 18:17:13 -0400686
Tejun Heo29407402006-02-12 22:47:04 +0900687static u64 ata_id_n_sectors(const u16 *id)
688{
689 if (ata_id_has_lba(id)) {
690 if (ata_id_has_lba48(id))
691 return ata_id_u64(id, 100);
692 else
693 return ata_id_u32(id, 60);
694 } else {
695 if (ata_id_current_chs_valid(id))
696 return ata_id_u32(id, 57);
697 else
698 return id[1] * id[3] * id[6];
699 }
700}
Edward Falk0baab862005-06-02 18:17:13 -0400701
702/**
703 * ata_noop_dev_select - Select device 0/1 on ATA bus
704 * @ap: ATA channel to manipulate
705 * @device: ATA device (numbered from zero) to select
706 *
707 * This function performs no actual function.
708 *
709 * May be used as the dev_select() entry in ata_port_operations.
710 *
711 * LOCKING:
712 * caller.
713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
715{
716}
717
Edward Falk0baab862005-06-02 18:17:13 -0400718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/**
720 * ata_std_dev_select - Select device 0/1 on ATA bus
721 * @ap: ATA channel to manipulate
722 * @device: ATA device (numbered from zero) to select
723 *
724 * Use the method defined in the ATA specification to
725 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400726 * ATA channel. Works with both PIO and MMIO.
727 *
728 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *
730 * LOCKING:
731 * caller.
732 */
733
734void ata_std_dev_select (struct ata_port *ap, unsigned int device)
735{
736 u8 tmp;
737
738 if (device == 0)
739 tmp = ATA_DEVICE_OBS;
740 else
741 tmp = ATA_DEVICE_OBS | ATA_DEV1;
742
743 if (ap->flags & ATA_FLAG_MMIO) {
744 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
745 } else {
746 outb(tmp, ap->ioaddr.device_addr);
747 }
748 ata_pause(ap); /* needed; also flushes, for mmio */
749}
750
751/**
752 * ata_dev_select - Select device 0/1 on ATA bus
753 * @ap: ATA channel to manipulate
754 * @device: ATA device (numbered from zero) to select
755 * @wait: non-zero to wait for Status register BSY bit to clear
756 * @can_sleep: non-zero if context allows sleeping
757 *
758 * Use the method defined in the ATA specification to
759 * make either device 0, or device 1, active on the
760 * ATA channel.
761 *
762 * This is a high-level version of ata_std_dev_select(),
763 * which additionally provides the services of inserting
764 * the proper pauses and status polling, where needed.
765 *
766 * LOCKING:
767 * caller.
768 */
769
770void ata_dev_select(struct ata_port *ap, unsigned int device,
771 unsigned int wait, unsigned int can_sleep)
772{
773 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
774 ap->id, device, wait);
775
776 if (wait)
777 ata_wait_idle(ap);
778
779 ap->ops->dev_select(ap, device);
780
781 if (wait) {
782 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
783 msleep(150);
784 ata_wait_idle(ap);
785 }
786}
787
788/**
789 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900790 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900792 * Dump selected 16-bit words from the given IDENTIFY DEVICE
793 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 *
795 * LOCKING:
796 * caller.
797 */
798
Tejun Heo0bd33002006-02-12 22:47:05 +0900799static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 DPRINTK("49==0x%04x "
802 "53==0x%04x "
803 "63==0x%04x "
804 "64==0x%04x "
805 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900806 id[49],
807 id[53],
808 id[63],
809 id[64],
810 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 DPRINTK("80==0x%04x "
812 "81==0x%04x "
813 "82==0x%04x "
814 "83==0x%04x "
815 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900816 id[80],
817 id[81],
818 id[82],
819 id[83],
820 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 DPRINTK("88==0x%04x "
822 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900823 id[88],
824 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Tejun Heocb95d562006-03-06 04:31:56 +0900827/**
828 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
829 * @id: IDENTIFY data to compute xfer mask from
Alan Cox11e29e22005-10-21 18:46:32 -0400830 *
Tejun Heocb95d562006-03-06 04:31:56 +0900831 * Compute the xfermask for this device. This is not as trivial
832 * as it seems if we must consider early devices correctly.
833 *
834 * FIXME: pre IDE drive timing (do we care ?).
835 *
836 * LOCKING:
837 * None.
838 *
839 * RETURNS:
840 * Computed xfermask
Alan Cox11e29e22005-10-21 18:46:32 -0400841 */
Tejun Heocb95d562006-03-06 04:31:56 +0900842static unsigned int ata_id_xfermask(const u16 *id)
Alan Cox11e29e22005-10-21 18:46:32 -0400843{
Tejun Heocb95d562006-03-06 04:31:56 +0900844 unsigned int pio_mask, mwdma_mask, udma_mask;
Alan Cox11e29e22005-10-21 18:46:32 -0400845
Alan Coxffa29452006-01-09 17:14:40 +0000846 /* Usual case. Word 53 indicates word 64 is valid */
Tejun Heocb95d562006-03-06 04:31:56 +0900847 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
848 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
849 pio_mask <<= 3;
850 pio_mask |= 0x7;
851 } else {
852 /* If word 64 isn't valid then Word 51 high byte holds
853 * the PIO timing number for the maximum. Turn it into
854 * a mask.
855 */
856 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
857
858 /* But wait.. there's more. Design your standards by
859 * committee and you too can get a free iordy field to
860 * process. However its the speeds not the modes that
861 * are supported... Note drivers using the timing API
862 * will get this right anyway
863 */
Alan Cox11e29e22005-10-21 18:46:32 -0400864 }
865
Tejun Heocb95d562006-03-06 04:31:56 +0900866 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +0900867
868 udma_mask = 0;
869 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
870 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Alan Cox11e29e22005-10-21 18:46:32 -0400871
Tejun Heocb95d562006-03-06 04:31:56 +0900872 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
Tejun Heoc18d06f2006-02-02 00:56:10 +0900873}
874
875/**
Tejun Heo86e45b62006-03-05 15:29:09 +0900876 * ata_port_queue_task - Queue port_task
877 * @ap: The ata_port to queue port_task for
Tejun Heoc18d06f2006-02-02 00:56:10 +0900878 *
Tejun Heo86e45b62006-03-05 15:29:09 +0900879 * Schedule @fn(@data) for execution after @delay jiffies using
880 * port_task. There is one port_task per port and it's the
881 * user(low level driver)'s responsibility to make sure that only
882 * one task is active at any given time.
883 *
884 * libata core layer takes care of synchronization between
885 * port_task and EH. ata_port_queue_task() may be ignored for EH
886 * synchronization.
887 *
888 * LOCKING:
889 * Inherited from caller.
890 */
891void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
892 unsigned long delay)
893{
894 int rc;
895
Tejun Heo2e755f62006-03-05 15:29:09 +0900896 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900897 return;
898
899 PREPARE_WORK(&ap->port_task, fn, data);
900
901 if (!delay)
902 rc = queue_work(ata_wq, &ap->port_task);
903 else
904 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
905
906 /* rc == 0 means that another user is using port task */
907 WARN_ON(rc == 0);
908}
909
910/**
911 * ata_port_flush_task - Flush port_task
912 * @ap: The ata_port to flush port_task for
913 *
914 * After this function completes, port_task is guranteed not to
915 * be running or scheduled.
Tejun Heoc18d06f2006-02-02 00:56:10 +0900916 *
917 * LOCKING:
918 * Kernel thread context (may sleep)
919 */
Tejun Heo86e45b62006-03-05 15:29:09 +0900920void ata_port_flush_task(struct ata_port *ap)
Tejun Heoc18d06f2006-02-02 00:56:10 +0900921{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900922 unsigned long flags;
923
924 DPRINTK("ENTER\n");
925
926 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900927 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heoc18d06f2006-02-02 00:56:10 +0900928 spin_unlock_irqrestore(&ap->host_set->lock, flags);
929
930 DPRINTK("flush #1\n");
931 flush_workqueue(ata_wq);
932
933 /*
934 * At this point, if a task is running, it's guaranteed to see
935 * the FLUSH flag; thus, it will never queue pio tasks again.
936 * Cancel and flush.
937 */
Tejun Heo86e45b62006-03-05 15:29:09 +0900938 if (!cancel_delayed_work(&ap->port_task)) {
Tejun Heoc18d06f2006-02-02 00:56:10 +0900939 DPRINTK("flush #2\n");
940 flush_workqueue(ata_wq);
941 }
942
943 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900944 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heoc18d06f2006-02-02 00:56:10 +0900945 spin_unlock_irqrestore(&ap->host_set->lock, flags);
946
947 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +0900948}
949
Tejun Heo77853bf2006-01-23 13:09:36 +0900950void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Jeff Garzik64f043d2005-11-17 10:50:01 -0500951{
Tejun Heo77853bf2006-01-23 13:09:36 +0900952 struct completion *waiting = qc->private_data;
Jeff Garzik64f043d2005-11-17 10:50:01 -0500953
Tejun Heo77853bf2006-01-23 13:09:36 +0900954 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900955 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900956}
Jeff Garzik64f043d2005-11-17 10:50:01 -0500957
Tejun Heoa2a7a662005-12-13 14:48:31 +0900958/**
959 * ata_exec_internal - execute libata internal command
960 * @ap: Port to which the command is sent
961 * @dev: Device to which the command is sent
962 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +0900963 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +0900964 * @dma_dir: Data tranfer direction of the command
965 * @buf: Data buffer of the command
966 * @buflen: Length of data buffer
967 *
968 * Executes libata internal command with timeout. @tf contains
969 * command on entry and result on return. Timeout and error
970 * conditions are reported via return value. No recovery action
971 * is taken after a command times out. It's caller's duty to
972 * clean up after timeout.
973 *
974 * LOCKING:
975 * None. Should be called with kernel context, might sleep.
976 */
977
Tejun Heo1ad8e7f2006-04-02 18:51:53 +0900978unsigned ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
979 struct ata_taskfile *tf, const u8 *cdb,
980 int dma_dir, void *buf, unsigned int buflen)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900981{
982 u8 command = tf->command;
983 struct ata_queued_cmd *qc;
984 DECLARE_COMPLETION(wait);
985 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900986 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900987
988 spin_lock_irqsave(&ap->host_set->lock, flags);
989
990 qc = ata_qc_new_init(ap, dev);
991 BUG_ON(qc == NULL);
992
993 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +0900994 if (cdb)
995 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900996 qc->dma_dir = dma_dir;
997 if (dma_dir != DMA_NONE) {
998 ata_sg_init_one(qc, buf, buflen);
999 qc->nsect = buflen / ATA_SECT_SIZE;
Jeff Garzik64f043d2005-11-17 10:50:01 -05001000 }
1001
Tejun Heo77853bf2006-01-23 13:09:36 +09001002 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001003 qc->complete_fn = ata_qc_complete_internal;
1004
Tejun Heo8e0e6942006-03-31 20:41:11 +09001005 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001006
1007 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1008
1009 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
Albert Lee41ade502006-03-14 11:19:04 +08001010 ata_port_flush_task(ap);
1011
Tejun Heoa2a7a662005-12-13 14:48:31 +09001012 spin_lock_irqsave(&ap->host_set->lock, flags);
1013
1014 /* We're racing with irq here. If we lose, the
1015 * following test prevents us from completing the qc
1016 * again. If completion irq occurs after here but
1017 * before the caller cleans up, it will result in a
1018 * spurious interrupt. We can live with that.
1019 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001020 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001021 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001022 ata_qc_complete(qc);
1023 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1024 ap->id, command);
1025 }
1026
1027 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1028 }
1029
Tejun Heo77853bf2006-01-23 13:09:36 +09001030 *tf = qc->tf;
1031 err_mask = qc->err_mask;
1032
1033 ata_qc_free(qc);
1034
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001035 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1036 * Until those drivers are fixed, we detect the condition
1037 * here, fail the command with AC_ERR_SYSTEM and reenable the
1038 * port.
1039 *
1040 * Note that this doesn't change any behavior as internal
1041 * command failure results in disabling the device in the
1042 * higher layer for LLDDs without new reset/EH callbacks.
1043 *
1044 * Kill the following code as soon as those drivers are fixed.
1045 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001046 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001047 err_mask |= AC_ERR_SYSTEM;
1048 ata_port_probe(ap);
1049 }
1050
Tejun Heo77853bf2006-01-23 13:09:36 +09001051 return err_mask;
Jeff Garzik64f043d2005-11-17 10:50:01 -05001052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001055 * ata_pio_need_iordy - check if iordy needed
1056 * @adev: ATA device
1057 *
1058 * Check if the current speed of the device requires IORDY. Used
1059 * by various controllers for chip configuration.
1060 */
1061
1062unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1063{
1064 int pio;
1065 int speed = adev->pio_mode - XFER_PIO_0;
1066
1067 if (speed < 2)
1068 return 0;
1069 if (speed > 2)
1070 return 1;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001071
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001072 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1073
1074 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1075 pio = adev->id[ATA_ID_EIDE_PIO];
1076 /* Is the speed faster than the drive allows non IORDY ? */
1077 if (pio) {
1078 /* This is cycle times not frequency - watch the logic! */
1079 if (pio > 240) /* PIO2 is 240nS per cycle */
1080 return 1;
1081 return 0;
1082 }
1083 }
1084 return 0;
1085}
1086
1087/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001088 * ata_dev_read_id - Read ID data from the specified device
1089 * @ap: port on which target device resides
1090 * @dev: target device
1091 * @p_class: pointer to class of the target device (may be changed)
1092 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +09001093 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +09001094 *
1095 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1096 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001097 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1098 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001099 *
1100 * LOCKING:
1101 * Kernel thread context (may sleep)
1102 *
1103 * RETURNS:
1104 * 0 on success, -errno otherwise.
1105 */
1106static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +09001107 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001108{
1109 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001110 struct ata_taskfile tf;
1111 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +09001112 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001113 const char *reason;
1114 int rc;
1115
1116 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1117
Tejun Heo49016ac2006-02-21 02:12:11 +09001118 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1119
Tejun Heod9572b12006-03-01 16:09:35 +09001120 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1121 if (id == NULL) {
1122 rc = -ENOMEM;
1123 reason = "out of memory";
1124 goto err_out;
1125 }
1126
Tejun Heo49016ac2006-02-21 02:12:11 +09001127 retry:
1128 ata_tf_init(ap, &tf, dev->devno);
1129
1130 switch (class) {
1131 case ATA_DEV_ATA:
1132 tf.command = ATA_CMD_ID_ATA;
1133 break;
1134 case ATA_DEV_ATAPI:
1135 tf.command = ATA_CMD_ID_ATAPI;
1136 break;
1137 default:
1138 rc = -ENODEV;
1139 reason = "unsupported class";
1140 goto err_out;
1141 }
1142
1143 tf.protocol = ATA_PROT_PIO;
1144
Tejun Heod69cf372006-04-02 18:51:53 +09001145 err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo49016ac2006-02-21 02:12:11 +09001146 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001147 if (err_mask) {
1148 rc = -EIO;
1149 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001150 goto err_out;
1151 }
1152
1153 swap_buf_le16(id, ATA_ID_WORDS);
1154
Tejun Heo49016ac2006-02-21 02:12:11 +09001155 /* sanity check */
Alan Cox692785e2006-03-27 18:49:19 +01001156 if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001157 rc = -EINVAL;
1158 reason = "device reports illegal type";
1159 goto err_out;
1160 }
1161
1162 if (post_reset && class == ATA_DEV_ATA) {
1163 /*
1164 * The exact sequence expected by certain pre-ATA4 drives is:
1165 * SRST RESET
1166 * IDENTIFY
1167 * INITIALIZE DEVICE PARAMETERS
1168 * anything else..
1169 * Some drives were very specific about that exact sequence.
1170 */
1171 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Albert Lee00b6f5e2006-03-27 16:39:18 +08001172 err_mask = ata_dev_init_params(ap, dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001173 if (err_mask) {
1174 rc = -EIO;
1175 reason = "INIT_DEV_PARAMS failed";
1176 goto err_out;
1177 }
1178
1179 /* current CHS translation info (id[53-58]) might be
1180 * changed. reread the identify device info.
1181 */
1182 post_reset = 0;
1183 goto retry;
1184 }
1185 }
1186
1187 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001188 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001189 return 0;
1190
1191 err_out:
1192 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1193 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001194 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001195 return rc;
1196}
1197
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001198static inline u8 ata_dev_knobble(const struct ata_port *ap,
1199 struct ata_device *dev)
1200{
1201 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1202}
1203
Tejun Heo49016ac2006-02-21 02:12:11 +09001204/**
Tejun Heoffeae412006-03-01 16:09:35 +09001205 * ata_dev_configure - Configure the specified ATA/ATAPI device
1206 * @ap: Port on which target device resides
1207 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001208 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 *
Tejun Heoffeae412006-03-01 16:09:35 +09001210 * Configure @dev according to @dev->id. Generic and low-level
1211 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 *
1213 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001214 * Kernel thread context (may sleep)
1215 *
1216 * RETURNS:
1217 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001219static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1220 int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Tejun Heo1148c3a2006-03-13 19:48:04 +09001222 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001223 unsigned int xfer_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001224 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Tejun Heoe1211e32006-04-01 01:38:18 +09001226 if (!ata_dev_enabled(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001228 ap->id, dev->devno);
1229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
Tejun Heoffeae412006-03-01 16:09:35 +09001232 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001234 /* print device capabilities */
1235 if (print_info)
1236 printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
1237 "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1238 ap->id, dev->devno, id[49], id[82], id[83],
1239 id[84], id[85], id[86], id[87], id[88]);
1240
Tejun Heo208a9932006-03-05 17:55:58 +09001241 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09001242 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09001243 dev->max_sectors = 0;
1244 dev->cdb_len = 0;
1245 dev->n_sectors = 0;
1246 dev->cylinders = 0;
1247 dev->heads = 0;
1248 dev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /*
1251 * common ATA, ATAPI feature tests
1252 */
1253
Tejun Heoff8854b2006-03-06 04:31:56 +09001254 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001255 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Tejun Heo1148c3a2006-03-13 19:48:04 +09001257 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /* ATA-specific feature tests */
1260 if (dev->class == ATA_DEV_ATA) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001261 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001262
Tejun Heo1148c3a2006-03-13 19:48:04 +09001263 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001264 const char *lba_desc;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001265
Tejun Heo4c2d7212006-03-05 17:55:58 +09001266 lba_desc = "LBA";
1267 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001268 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001269 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001270 lba_desc = "LBA48";
1271 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04001272
1273 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001274 if (print_info)
1275 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1276 "max %s, %Lu sectors: %s\n",
1277 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001278 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001279 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001280 (unsigned long long)dev->n_sectors,
1281 lba_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001282 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001283 /* CHS */
1284
1285 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001286 dev->cylinders = id[1];
1287 dev->heads = id[3];
1288 dev->sectors = id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001289
Tejun Heo1148c3a2006-03-13 19:48:04 +09001290 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001291 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001292 dev->cylinders = id[54];
1293 dev->heads = id[55];
1294 dev->sectors = id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001295 }
1296
1297 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001298 if (print_info)
1299 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1300 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1301 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001302 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001303 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001304 (unsigned long long)dev->n_sectors,
1305 dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
Albert Lee07f6f7d2005-11-01 19:33:20 +08001308 if (dev->id[59] & 0x100) {
1309 dev->multi_count = dev->id[59] & 0xff;
1310 DPRINTK("ata%u: dev %u multi count %u\n",
Albert Lee999bb6f2006-03-25 18:07:48 +08001311 ap->id, dev->devno, dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08001312 }
1313
Albert Lee13ee4622006-03-25 17:39:34 +08001314 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316
1317 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001318 else if (dev->class == ATA_DEV_ATAPI) {
Albert Lee08a556d2006-03-31 13:29:04 +08001319 char *cdb_intr_string = "";
1320
Tejun Heo1148c3a2006-03-13 19:48:04 +09001321 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1323 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001324 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 goto err_out_nosup;
1326 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001327 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Albert Lee08a556d2006-03-31 13:29:04 +08001329 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08001330 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08001331 cdb_intr_string = ", CDB intr";
1332 }
Albert Lee312f7da2005-09-27 17:38:03 +08001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001335 if (print_info)
Albert Lee08a556d2006-03-31 13:29:04 +08001336 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s%s\n",
1337 ap->id, dev->devno, ata_mode_string(xfer_mask),
1338 cdb_intr_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340
Tejun Heo6e7846e2006-02-12 23:32:58 +09001341 ap->host->max_cmd_len = 0;
1342 for (i = 0; i < ATA_MAX_DEVICES; i++)
1343 ap->host->max_cmd_len = max_t(unsigned int,
1344 ap->host->max_cmd_len,
1345 ap->device[i].cdb_len);
1346
Brad Campbell6f2f3812005-05-12 15:07:47 -04001347 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001348 if (ata_dev_knobble(ap, dev)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001349 if (print_info)
1350 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1351 ap->id, dev->devno);
Tejun Heo5a529132006-03-24 14:07:50 +09001352 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001353 dev->max_sectors = ATA_MAX_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001354 }
1355
1356 if (ap->ops->dev_config)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001357 ap->ops->dev_config(ap, dev);
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362err_out_nosup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001364 return rc;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001365}
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367/**
1368 * ata_bus_probe - Reset and probe ATA bus
1369 * @ap: Bus to probe
1370 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001371 * Master ATA bus probing function. Initiates a hardware-dependent
1372 * bus reset, then attempts to identify any devices found on
1373 * the bus.
1374 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001376 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 *
1378 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09001379 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 */
1381
1382static int ata_bus_probe(struct ata_port *ap)
1383{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001384 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09001385 int tries[ATA_MAX_DEVICES];
1386 int i, rc, down_xfermask;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001387 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Tejun Heo28ca5c52006-03-01 16:09:36 +09001389 ata_port_probe(ap);
1390
Tejun Heo14d2bac2006-04-02 17:54:46 +09001391 for (i = 0; i < ATA_MAX_DEVICES; i++)
1392 tries[i] = ATA_PROBE_MAX_TRIES;
1393
1394 retry:
1395 down_xfermask = 0;
1396
Tejun Heo20444702006-03-13 01:57:01 +09001397 /* reset and determine device classes */
1398 for (i = 0; i < ATA_MAX_DEVICES; i++)
1399 classes[i] = ATA_DEV_UNKNOWN;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001400
Tejun Heo20444702006-03-13 01:57:01 +09001401 if (ap->ops->probe_reset) {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001402 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001403 if (rc) {
1404 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1405 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001406 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001407 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001408 ap->ops->phy_reset(ap);
1409
Tejun Heo198e0fe2006-04-02 18:51:52 +09001410 if (!(ap->flags & ATA_FLAG_DISABLED))
Tejun Heo20444702006-03-13 01:57:01 +09001411 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo28ca5c52006-03-01 16:09:36 +09001412 classes[i] = ap->device[i].class;
Tejun Heo20444702006-03-13 01:57:01 +09001413
Tejun Heo28ca5c52006-03-01 16:09:36 +09001414 ata_port_probe(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Tejun Heo20444702006-03-13 01:57:01 +09001417 for (i = 0; i < ATA_MAX_DEVICES; i++)
1418 if (classes[i] == ATA_DEV_UNKNOWN)
1419 classes[i] = ATA_DEV_NONE;
1420
Tejun Heo28ca5c52006-03-01 16:09:36 +09001421 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001423 dev = &ap->device[i];
Tejun Heo28ca5c52006-03-01 16:09:36 +09001424 dev->class = classes[i];
1425
Tejun Heo14d2bac2006-04-02 17:54:46 +09001426 if (!tries[i]) {
1427 ata_down_xfermask_limit(ap, dev, 1);
1428 ata_dev_disable(ap, dev);
1429 }
1430
Tejun Heoe1211e32006-04-01 01:38:18 +09001431 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09001432 continue;
1433
Tejun Heo14d2bac2006-04-02 17:54:46 +09001434 kfree(dev->id);
1435 dev->id = NULL;
1436 rc = ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id);
1437 if (rc)
1438 goto fail;
Tejun Heoffeae412006-03-01 16:09:35 +09001439
Tejun Heo14d2bac2006-04-02 17:54:46 +09001440 rc = ata_dev_configure(ap, dev, 1);
1441 if (rc)
1442 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001445 /* configure transfer mode */
1446 if (ap->ops->set_mode) {
1447 /* FIXME: make ->set_mode handle no device case and
1448 * return error code and failing device on failure as
1449 * ata_set_mode() does.
1450 */
Tejun Heo14d2bac2006-04-02 17:54:46 +09001451 for (i = 0; i < ATA_MAX_DEVICES; i++)
1452 if (ata_dev_enabled(&ap->device[i])) {
1453 ap->ops->set_mode(ap);
1454 break;
1455 }
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001456 rc = 0;
1457 } else {
Tejun Heo14d2bac2006-04-02 17:54:46 +09001458 rc = ata_set_mode(ap, &dev);
1459 if (rc) {
1460 down_xfermask = 1;
1461 goto fail;
1462 }
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001465 for (i = 0; i < ATA_MAX_DEVICES; i++)
1466 if (ata_dev_enabled(&ap->device[i]))
1467 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01001468
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001469 /* no device present, disable port */
1470 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ap->ops->port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09001472 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09001473
1474 fail:
1475 switch (rc) {
1476 case -EINVAL:
1477 case -ENODEV:
1478 tries[dev->devno] = 0;
1479 break;
1480 case -EIO:
1481 ata_down_sata_spd_limit(ap);
1482 /* fall through */
1483 default:
1484 tries[dev->devno]--;
1485 if (down_xfermask &&
1486 ata_down_xfermask_limit(ap, dev, tries[dev->devno] == 1))
1487 tries[dev->devno] = 0;
1488 }
1489
1490 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
1493/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001494 * ata_port_probe - Mark port as enabled
1495 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001497 * Modify @ap data structure such that the system
1498 * thinks that the entire port is enabled.
1499 *
1500 * LOCKING: host_set lock, or some other form of
1501 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 */
1503
1504void ata_port_probe(struct ata_port *ap)
1505{
Tejun Heo198e0fe2006-04-02 18:51:52 +09001506 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
1509/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001510 * sata_print_link_status - Print SATA link status
1511 * @ap: SATA port to printk link status about
1512 *
1513 * This function prints link speed and status of a SATA link.
1514 *
1515 * LOCKING:
1516 * None.
1517 */
1518static void sata_print_link_status(struct ata_port *ap)
1519{
Tejun Heo6d5f9732006-04-03 00:09:41 +09001520 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09001521
1522 if (!ap->ops->scr_read)
1523 return;
1524
1525 sstatus = scr_read(ap, SCR_STATUS);
Tejun Heo6d5f9732006-04-03 00:09:41 +09001526 scontrol = scr_read(ap, SCR_CONTROL);
Tejun Heo3be680b2005-12-19 22:35:02 +09001527
1528 if (sata_dev_present(ap)) {
1529 tmp = (sstatus >> 4) & 0xf;
Tejun Heo6d5f9732006-04-03 00:09:41 +09001530 printk(KERN_INFO
1531 "ata%u: SATA link up %s (SStatus %X SControl %X)\n",
1532 ap->id, sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001533 } else {
Tejun Heo6d5f9732006-04-03 00:09:41 +09001534 printk(KERN_INFO
1535 "ata%u: SATA link down (SStatus %X SControl %X)\n",
1536 ap->id, sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001537 }
1538}
1539
1540/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001541 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1542 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001544 * This function issues commands to standard SATA Sxxx
1545 * PHY registers, to wake up the phy (and device), and
1546 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 *
1548 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001549 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 *
1551 */
1552void __sata_phy_reset(struct ata_port *ap)
1553{
1554 u32 sstatus;
1555 unsigned long timeout = jiffies + (HZ * 5);
1556
1557 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001558 /* issue phy wake/reset */
1559 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001560 /* Couldn't find anything in SATA I/II specs, but
1561 * AHCI-1.1 10.4.2 says at least 1 ms. */
1562 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
Brett Russcdcca892005-03-28 15:10:27 -05001564 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /* wait for phy to become ready, if necessary */
1567 do {
1568 msleep(200);
1569 sstatus = scr_read(ap, SCR_STATUS);
1570 if ((sstatus & 0xf) != 1)
1571 break;
1572 } while (time_before(jiffies, timeout));
1573
Tejun Heo3be680b2005-12-19 22:35:02 +09001574 /* print link status */
1575 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001576
Tejun Heo3be680b2005-12-19 22:35:02 +09001577 /* TODO: phy layer with polling, timeouts, etc. */
1578 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001580 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Tejun Heo198e0fe2006-04-02 18:51:52 +09001583 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return;
1585
1586 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1587 ata_port_disable(ap);
1588 return;
1589 }
1590
1591 ap->cbl = ATA_CBL_SATA;
1592}
1593
1594/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001595 * sata_phy_reset - Reset SATA bus.
1596 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001598 * This function resets the SATA bus, and then probes
1599 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 *
1601 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001602 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 *
1604 */
1605void sata_phy_reset(struct ata_port *ap)
1606{
1607 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09001608 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return;
1610 ata_bus_reset(ap);
1611}
1612
1613/**
Alan Coxebdfca62006-03-23 15:38:34 +00001614 * ata_dev_pair - return other device on cable
1615 * @ap: port
1616 * @adev: device
1617 *
1618 * Obtain the other device on the same cable, or if none is
1619 * present NULL is returned
1620 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001621
Alan Coxebdfca62006-03-23 15:38:34 +00001622struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev)
1623{
1624 struct ata_device *pair = &ap->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09001625 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00001626 return NULL;
1627 return pair;
1628}
1629
1630/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001631 * ata_port_disable - Disable port.
1632 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001634 * Modify @ap data structure such that the system
1635 * thinks that the entire port is disabled, and should
1636 * never attempt to probe or communicate with devices
1637 * on this port.
1638 *
1639 * LOCKING: host_set lock, or some other form of
1640 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 */
1642
1643void ata_port_disable(struct ata_port *ap)
1644{
1645 ap->device[0].class = ATA_DEV_NONE;
1646 ap->device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09001647 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
Tejun Heo1c3fae42006-04-02 20:53:28 +09001650/**
1651 * ata_down_sata_spd_limit - adjust SATA spd limit downward
1652 * @ap: Port to adjust SATA spd limit for
1653 *
1654 * Adjust SATA spd limit of @ap downward. Note that this
1655 * function only adjusts the limit. The change must be applied
1656 * using ata_set_sata_spd().
1657 *
1658 * LOCKING:
1659 * Inherited from caller.
1660 *
1661 * RETURNS:
1662 * 0 on success, negative errno on failure
1663 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09001664int ata_down_sata_spd_limit(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001665{
1666 u32 spd, mask;
1667 int highbit;
1668
1669 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1670 return -EOPNOTSUPP;
1671
1672 mask = ap->sata_spd_limit;
1673 if (mask <= 1)
1674 return -EINVAL;
1675 highbit = fls(mask) - 1;
1676 mask &= ~(1 << highbit);
1677
1678 spd = (scr_read(ap, SCR_STATUS) >> 4) & 0xf;
1679 if (spd <= 1)
1680 return -EINVAL;
1681 spd--;
1682 mask &= (1 << spd) - 1;
1683 if (!mask)
1684 return -EINVAL;
1685
1686 ap->sata_spd_limit = mask;
1687
1688 printk(KERN_WARNING "ata%u: limiting SATA link speed to %s\n",
1689 ap->id, sata_spd_string(fls(mask)));
1690
1691 return 0;
1692}
1693
1694static int __ata_set_sata_spd_needed(struct ata_port *ap, u32 *scontrol)
1695{
1696 u32 spd, limit;
1697
1698 if (ap->sata_spd_limit == UINT_MAX)
1699 limit = 0;
1700 else
1701 limit = fls(ap->sata_spd_limit);
1702
1703 spd = (*scontrol >> 4) & 0xf;
1704 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
1705
1706 return spd != limit;
1707}
1708
1709/**
1710 * ata_set_sata_spd_needed - is SATA spd configuration needed
1711 * @ap: Port in question
1712 *
1713 * Test whether the spd limit in SControl matches
1714 * @ap->sata_spd_limit. This function is used to determine
1715 * whether hardreset is necessary to apply SATA spd
1716 * configuration.
1717 *
1718 * LOCKING:
1719 * Inherited from caller.
1720 *
1721 * RETURNS:
1722 * 1 if SATA spd configuration is needed, 0 otherwise.
1723 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09001724int ata_set_sata_spd_needed(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001725{
1726 u32 scontrol;
1727
1728 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1729 return 0;
1730
1731 scontrol = scr_read(ap, SCR_CONTROL);
1732
1733 return __ata_set_sata_spd_needed(ap, &scontrol);
1734}
1735
1736/**
1737 * ata_set_sata_spd - set SATA spd according to spd limit
1738 * @ap: Port to set SATA spd for
1739 *
1740 * Set SATA spd of @ap according to sata_spd_limit.
1741 *
1742 * LOCKING:
1743 * Inherited from caller.
1744 *
1745 * RETURNS:
1746 * 0 if spd doesn't need to be changed, 1 if spd has been
1747 * changed. -EOPNOTSUPP if SCR registers are inaccessible.
1748 */
1749static int ata_set_sata_spd(struct ata_port *ap)
1750{
1751 u32 scontrol;
1752
1753 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1754 return -EOPNOTSUPP;
1755
1756 scontrol = scr_read(ap, SCR_CONTROL);
1757 if (!__ata_set_sata_spd_needed(ap, &scontrol))
1758 return 0;
1759
1760 scr_write(ap, SCR_CONTROL, scontrol);
1761 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Alan Cox452503f2005-10-21 19:01:32 -04001764/*
1765 * This mode timing computation functionality is ported over from
1766 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1767 */
1768/*
1769 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1770 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1771 * for PIO 5, which is a nonstandard extension and UDMA6, which
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001772 * is currently supported only by Maxtor drives.
Alan Cox452503f2005-10-21 19:01:32 -04001773 */
1774
1775static const struct ata_timing ata_timing[] = {
1776
1777 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1778 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1779 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1780 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1781
1782 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1783 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1784 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1785
1786/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001787
Alan Cox452503f2005-10-21 19:01:32 -04001788 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1789 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1790 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001791
Alan Cox452503f2005-10-21 19:01:32 -04001792 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1793 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1794 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1795
1796/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1797 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1798 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1799
1800 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1801 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1802 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1803
1804/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1805
1806 { 0xFF }
1807};
1808
1809#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1810#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1811
1812static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1813{
1814 q->setup = EZ(t->setup * 1000, T);
1815 q->act8b = EZ(t->act8b * 1000, T);
1816 q->rec8b = EZ(t->rec8b * 1000, T);
1817 q->cyc8b = EZ(t->cyc8b * 1000, T);
1818 q->active = EZ(t->active * 1000, T);
1819 q->recover = EZ(t->recover * 1000, T);
1820 q->cycle = EZ(t->cycle * 1000, T);
1821 q->udma = EZ(t->udma * 1000, UT);
1822}
1823
1824void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1825 struct ata_timing *m, unsigned int what)
1826{
1827 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1828 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1829 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1830 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1831 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1832 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1833 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1834 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1835}
1836
1837static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1838{
1839 const struct ata_timing *t;
1840
1841 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001842 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001843 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001844 return t;
Alan Cox452503f2005-10-21 19:01:32 -04001845}
1846
1847int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1848 struct ata_timing *t, int T, int UT)
1849{
1850 const struct ata_timing *s;
1851 struct ata_timing p;
1852
1853 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001854 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001855 */
Alan Cox452503f2005-10-21 19:01:32 -04001856
1857 if (!(s = ata_timing_find_mode(speed)))
1858 return -EINVAL;
1859
Albert Lee75b1f2f2005-11-16 17:06:18 +08001860 memcpy(t, s, sizeof(*s));
1861
Alan Cox452503f2005-10-21 19:01:32 -04001862 /*
1863 * If the drive is an EIDE drive, it can tell us it needs extended
1864 * PIO/MW_DMA cycle timing.
1865 */
1866
1867 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1868 memset(&p, 0, sizeof(p));
1869 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1870 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1871 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1872 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1873 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1874 }
1875 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1876 }
1877
1878 /*
1879 * Convert the timing to bus clock counts.
1880 */
1881
Albert Lee75b1f2f2005-11-16 17:06:18 +08001882 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001883
1884 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001885 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1886 * S.M.A.R.T * and some other commands. We have to ensure that the
1887 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001888 */
1889
1890 if (speed > XFER_PIO_4) {
1891 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1892 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1893 }
1894
1895 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001896 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001897 */
1898
1899 if (t->act8b + t->rec8b < t->cyc8b) {
1900 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1901 t->rec8b = t->cyc8b - t->act8b;
1902 }
1903
1904 if (t->active + t->recover < t->cycle) {
1905 t->active += (t->cycle - (t->active + t->recover)) / 2;
1906 t->recover = t->cycle - t->active;
1907 }
1908
1909 return 0;
1910}
1911
Tejun Heocf176e12006-04-02 17:54:46 +09001912/**
1913 * ata_down_xfermask_limit - adjust dev xfer masks downward
1914 * @ap: Port associated with device @dev
1915 * @dev: Device to adjust xfer masks
1916 * @force_pio0: Force PIO0
1917 *
1918 * Adjust xfer masks of @dev downward. Note that this function
1919 * does not apply the change. Invoking ata_set_mode() afterwards
1920 * will apply the limit.
1921 *
1922 * LOCKING:
1923 * Inherited from caller.
1924 *
1925 * RETURNS:
1926 * 0 on success, negative errno on failure
1927 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09001928int ata_down_xfermask_limit(struct ata_port *ap, struct ata_device *dev,
1929 int force_pio0)
Tejun Heocf176e12006-04-02 17:54:46 +09001930{
1931 unsigned long xfer_mask;
1932 int highbit;
1933
1934 xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
1935 dev->udma_mask);
1936
1937 if (!xfer_mask)
1938 goto fail;
1939 /* don't gear down to MWDMA from UDMA, go directly to PIO */
1940 if (xfer_mask & ATA_MASK_UDMA)
1941 xfer_mask &= ~ATA_MASK_MWDMA;
1942
1943 highbit = fls(xfer_mask) - 1;
1944 xfer_mask &= ~(1 << highbit);
1945 if (force_pio0)
1946 xfer_mask &= 1 << ATA_SHIFT_PIO;
1947 if (!xfer_mask)
1948 goto fail;
1949
1950 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
1951 &dev->udma_mask);
1952
1953 printk(KERN_WARNING "ata%u: dev %u limiting speed to %s\n",
1954 ap->id, dev->devno, ata_mode_string(xfer_mask));
1955
1956 return 0;
1957
1958 fail:
1959 return -EINVAL;
1960}
1961
Tejun Heo83206a22006-03-24 15:25:31 +09001962static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Tejun Heo83206a22006-03-24 15:25:31 +09001964 unsigned int err_mask;
1965 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Tejun Heoe8384602006-04-02 18:51:53 +09001967 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (dev->xfer_shift == ATA_SHIFT_PIO)
1969 dev->flags |= ATA_DFLAG_PIO;
1970
Tejun Heo83206a22006-03-24 15:25:31 +09001971 err_mask = ata_dev_set_xfermode(ap, dev);
1972 if (err_mask) {
1973 printk(KERN_ERR
1974 "ata%u: failed to set xfermode (err_mask=0x%x)\n",
1975 ap->id, err_mask);
1976 return -EIO;
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Tejun Heo83206a22006-03-24 15:25:31 +09001979 rc = ata_dev_revalidate(ap, dev, 0);
Tejun Heo5eb45c02006-04-02 18:51:52 +09001980 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09001981 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Tejun Heo23e71c32006-03-06 04:31:57 +09001983 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1984 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
Tejun Heo23e71c32006-03-06 04:31:57 +09001987 ap->id, dev->devno,
1988 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09001989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992/**
1993 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1994 * @ap: port on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001995 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 *
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001997 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
1998 * ata_set_mode() fails, pointer to the failing device is
1999 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04002000 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002002 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002003 *
2004 * RETURNS:
2005 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09002007int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Tejun Heoe8e06192006-04-01 01:38:18 +09002009 struct ata_device *dev;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002010 int i, rc = 0, used_dma = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Tejun Heoa6d5a512006-03-06 04:31:57 +09002012 /* step 1: calculate xfer_mask */
2013 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002014 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002015
Tejun Heoe8e06192006-04-01 01:38:18 +09002016 dev = &ap->device[i];
2017
Tejun Heoe1211e32006-04-01 01:38:18 +09002018 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002019 continue;
2020
Tejun Heoacf356b2006-03-24 14:07:50 +09002021 ata_dev_xfermask(ap, dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002022
Tejun Heoacf356b2006-03-24 14:07:50 +09002023 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2024 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2025 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2026 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002027
Tejun Heo4f659772006-04-01 01:38:18 +09002028 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002029 if (dev->dma_mode)
2030 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002031 }
Tejun Heo4f659772006-04-01 01:38:18 +09002032 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002033 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002034
2035 /* step 2: always set host PIO timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002036 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2037 dev = &ap->device[i];
2038 if (!ata_dev_enabled(dev))
2039 continue;
2040
2041 if (!dev->pio_mode) {
2042 printk(KERN_WARNING "ata%u: dev %u no PIO support\n",
2043 ap->id, dev->devno);
2044 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002045 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002046 }
2047
2048 dev->xfer_mode = dev->pio_mode;
2049 dev->xfer_shift = ATA_SHIFT_PIO;
2050 if (ap->ops->set_piomode)
2051 ap->ops->set_piomode(ap, dev);
2052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Tejun Heoa6d5a512006-03-06 04:31:57 +09002054 /* step 3: set host DMA timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002055 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2056 dev = &ap->device[i];
2057
2058 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2059 continue;
2060
2061 dev->xfer_mode = dev->dma_mode;
2062 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2063 if (ap->ops->set_dmamode)
2064 ap->ops->set_dmamode(ap, dev);
2065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /* step 4: update devices' xfer mode */
Tejun Heo83206a22006-03-24 15:25:31 +09002068 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002069 dev = &ap->device[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Tejun Heoe1211e32006-04-01 01:38:18 +09002071 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002072 continue;
2073
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002074 rc = ata_dev_set_mode(ap, dev);
2075 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002076 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Tejun Heoe8e06192006-04-01 01:38:18 +09002079 /* Record simplex status. If we selected DMA then the other
2080 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002081 */
Alan Cox5444a6f2006-03-27 18:58:20 +01002082 if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
2083 ap->host_set->simplex_claimed = 1;
2084
Tejun Heoe8e06192006-04-01 01:38:18 +09002085 /* step5: chip specific finalisation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (ap->ops->post_set_mode)
2087 ap->ops->post_set_mode(ap);
2088
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002089 out:
2090 if (rc)
2091 *r_failed_dev = dev;
2092 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
2095/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002096 * ata_tf_to_host - issue ATA taskfile to host controller
2097 * @ap: port to which command is being issued
2098 * @tf: ATA taskfile register set
2099 *
2100 * Issues ATA taskfile register set to ATA host controller,
2101 * with proper synchronization with interrupt handler and
2102 * other threads.
2103 *
2104 * LOCKING:
2105 * spin_lock_irqsave(host_set lock)
2106 */
2107
2108static inline void ata_tf_to_host(struct ata_port *ap,
2109 const struct ata_taskfile *tf)
2110{
2111 ap->ops->tf_load(ap, tf);
2112 ap->ops->exec_command(ap, tf);
2113}
2114
2115/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 * ata_busy_sleep - sleep until BSY clears, or timeout
2117 * @ap: port containing status register to be polled
2118 * @tmout_pat: impatience timeout
2119 * @tmout: overall timeout
2120 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002121 * Sleep until ATA Status register bit BSY clears,
2122 * or a timeout occurs.
2123 *
2124 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 */
2126
Tejun Heo6f8b9952006-01-24 17:05:21 +09002127unsigned int ata_busy_sleep (struct ata_port *ap,
2128 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 unsigned long timer_start, timeout;
2131 u8 status;
2132
2133 status = ata_busy_wait(ap, ATA_BUSY, 300);
2134 timer_start = jiffies;
2135 timeout = timer_start + tmout_pat;
2136 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2137 msleep(50);
2138 status = ata_busy_wait(ap, ATA_BUSY, 3);
2139 }
2140
2141 if (status & ATA_BUSY)
2142 printk(KERN_WARNING "ata%u is slow to respond, "
2143 "please be patient\n", ap->id);
2144
2145 timeout = timer_start + tmout;
2146 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2147 msleep(50);
2148 status = ata_chk_status(ap);
2149 }
2150
2151 if (status & ATA_BUSY) {
2152 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
2153 ap->id, tmout / HZ);
2154 return 1;
2155 }
2156
2157 return 0;
2158}
2159
2160static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2161{
2162 struct ata_ioports *ioaddr = &ap->ioaddr;
2163 unsigned int dev0 = devmask & (1 << 0);
2164 unsigned int dev1 = devmask & (1 << 1);
2165 unsigned long timeout;
2166
2167 /* if device 0 was found in ata_devchk, wait for its
2168 * BSY bit to clear
2169 */
2170 if (dev0)
2171 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2172
2173 /* if device 1 was found in ata_devchk, wait for
2174 * register access, then wait for BSY to clear
2175 */
2176 timeout = jiffies + ATA_TMOUT_BOOT;
2177 while (dev1) {
2178 u8 nsect, lbal;
2179
2180 ap->ops->dev_select(ap, 1);
2181 if (ap->flags & ATA_FLAG_MMIO) {
2182 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2183 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2184 } else {
2185 nsect = inb(ioaddr->nsect_addr);
2186 lbal = inb(ioaddr->lbal_addr);
2187 }
2188 if ((nsect == 1) && (lbal == 1))
2189 break;
2190 if (time_after(jiffies, timeout)) {
2191 dev1 = 0;
2192 break;
2193 }
2194 msleep(50); /* give drive a breather */
2195 }
2196 if (dev1)
2197 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2198
2199 /* is all this really necessary? */
2200 ap->ops->dev_select(ap, 0);
2201 if (dev1)
2202 ap->ops->dev_select(ap, 1);
2203 if (dev0)
2204 ap->ops->dev_select(ap, 0);
2205}
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207static unsigned int ata_bus_softreset(struct ata_port *ap,
2208 unsigned int devmask)
2209{
2210 struct ata_ioports *ioaddr = &ap->ioaddr;
2211
2212 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2213
2214 /* software reset. causes dev0 to be selected */
2215 if (ap->flags & ATA_FLAG_MMIO) {
2216 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2217 udelay(20); /* FIXME: flush */
2218 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2219 udelay(20); /* FIXME: flush */
2220 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2221 } else {
2222 outb(ap->ctl, ioaddr->ctl_addr);
2223 udelay(10);
2224 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2225 udelay(10);
2226 outb(ap->ctl, ioaddr->ctl_addr);
2227 }
2228
2229 /* spec mandates ">= 2ms" before checking status.
2230 * We wait 150ms, because that was the magic delay used for
2231 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2232 * between when the ATA command register is written, and then
2233 * status is checked. Because waiting for "a while" before
2234 * checking status is fine, post SRST, we perform this magic
2235 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00002236 *
2237 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 */
2239 msleep(150);
2240
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002241 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09002242 * the bus shows 0xFF because the odd clown forgets the D7
2243 * pulldown resistor.
2244 */
Alan Cox09c7ad72006-03-22 15:52:40 +00002245 if (ata_check_status(ap) == 0xFF)
Tejun Heo298a41c2006-03-25 02:58:13 +09002246 return AC_ERR_OTHER;
Alan Cox09c7ad72006-03-22 15:52:40 +00002247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 ata_bus_post_reset(ap, devmask);
2249
2250 return 0;
2251}
2252
2253/**
2254 * ata_bus_reset - reset host port and associated ATA channel
2255 * @ap: port to reset
2256 *
2257 * This is typically the first time we actually start issuing
2258 * commands to the ATA channel. We wait for BSY to clear, then
2259 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2260 * result. Determine what devices, if any, are on the channel
2261 * by looking at the device 0/1 error register. Look at the signature
2262 * stored in each device's taskfile registers, to determine if
2263 * the device is ATA or ATAPI.
2264 *
2265 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002266 * PCI/etc. bus probe sem.
2267 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 *
2269 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09002270 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 */
2272
2273void ata_bus_reset(struct ata_port *ap)
2274{
2275 struct ata_ioports *ioaddr = &ap->ioaddr;
2276 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2277 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002278 unsigned int dev0, dev1 = 0, devmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2281
2282 /* determine if device 0/1 are present */
2283 if (ap->flags & ATA_FLAG_SATA_RESET)
2284 dev0 = 1;
2285 else {
2286 dev0 = ata_devchk(ap, 0);
2287 if (slave_possible)
2288 dev1 = ata_devchk(ap, 1);
2289 }
2290
2291 if (dev0)
2292 devmask |= (1 << 0);
2293 if (dev1)
2294 devmask |= (1 << 1);
2295
2296 /* select device 0 again */
2297 ap->ops->dev_select(ap, 0);
2298
2299 /* issue bus reset */
2300 if (ap->flags & ATA_FLAG_SRST)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002301 if (ata_bus_softreset(ap, devmask))
2302 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 /*
2305 * determine by signature whether we have ATA or ATAPI devices
2306 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002307 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002309 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 /* re-enable interrupts */
2312 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2313 ata_irq_on(ap);
2314
2315 /* is double-select really necessary? */
2316 if (ap->device[1].class != ATA_DEV_NONE)
2317 ap->ops->dev_select(ap, 1);
2318 if (ap->device[0].class != ATA_DEV_NONE)
2319 ap->ops->dev_select(ap, 0);
2320
2321 /* if no devices were detected, disable this port */
2322 if ((ap->device[0].class == ATA_DEV_NONE) &&
2323 (ap->device[1].class == ATA_DEV_NONE))
2324 goto err_out;
2325
2326 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2327 /* set up device control for ATA_FLAG_SATA_RESET */
2328 if (ap->flags & ATA_FLAG_MMIO)
2329 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2330 else
2331 outb(ap->ctl, ioaddr->ctl_addr);
2332 }
2333
2334 DPRINTK("EXIT\n");
2335 return;
2336
2337err_out:
2338 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2339 ap->ops->port_disable(ap);
2340
2341 DPRINTK("EXIT\n");
2342}
2343
Tejun Heo7a7921e2006-02-02 18:20:00 +09002344static int sata_phy_resume(struct ata_port *ap)
2345{
2346 unsigned long timeout = jiffies + (HZ * 5);
Tejun Heo852ee162006-04-01 01:38:18 +09002347 u32 scontrol, sstatus;
Tejun Heo7a7921e2006-02-02 18:20:00 +09002348
Tejun Heo852ee162006-04-01 01:38:18 +09002349 scontrol = scr_read(ap, SCR_CONTROL);
2350 scontrol = (scontrol & 0x0f0) | 0x300;
2351 scr_write_flush(ap, SCR_CONTROL, scontrol);
Tejun Heo7a7921e2006-02-02 18:20:00 +09002352
2353 /* Wait for phy to become ready, if necessary. */
2354 do {
2355 msleep(200);
2356 sstatus = scr_read(ap, SCR_STATUS);
2357 if ((sstatus & 0xf) != 1)
2358 return 0;
2359 } while (time_before(jiffies, timeout));
2360
2361 return -1;
2362}
2363
Tejun Heoc2bd5802006-01-24 17:05:22 +09002364/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002365 * ata_std_probeinit - initialize probing
2366 * @ap: port to be probed
2367 *
2368 * @ap is about to be probed. Initialize it. This function is
2369 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002370 *
2371 * NOTE!!! Do not use this function as probeinit if a low level
2372 * driver implements only hardreset. Just pass NULL as probeinit
2373 * in that case. Using this function is probably okay but doing
2374 * so makes reset sequence different from the original
2375 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002376 */
Alan Cox17efc5f2006-03-27 19:01:32 +01002377void ata_std_probeinit(struct ata_port *ap)
Tejun Heo8a19ac82006-02-02 18:20:00 +09002378{
Alan Cox17efc5f2006-03-27 19:01:32 +01002379 if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09002380 u32 spd;
2381
Tejun Heo8a19ac82006-02-02 18:20:00 +09002382 sata_phy_resume(ap);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002383
2384 spd = (scr_read(ap, SCR_CONTROL) & 0xf0) >> 4;
2385 if (spd)
2386 ap->sata_spd_limit &= (1 << spd) - 1;
2387
Tejun Heo3a397462006-02-10 23:58:48 +09002388 if (sata_dev_present(ap))
2389 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2390 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002391}
2392
2393/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002394 * ata_std_softreset - reset host port via ATA SRST
2395 * @ap: port to reset
2396 * @verbose: fail verbosely
2397 * @classes: resulting classes of attached devices
2398 *
2399 * Reset host port using ATA SRST. This function is to be used
2400 * as standard callback for ata_drive_*_reset() functions.
2401 *
2402 * LOCKING:
2403 * Kernel thread context (may sleep)
2404 *
2405 * RETURNS:
2406 * 0 on success, -errno otherwise.
2407 */
2408int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2409{
2410 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2411 unsigned int devmask = 0, err_mask;
2412 u8 err;
2413
2414 DPRINTK("ENTER\n");
2415
Tejun Heo3a397462006-02-10 23:58:48 +09002416 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2417 classes[0] = ATA_DEV_NONE;
2418 goto out;
2419 }
2420
Tejun Heoc2bd5802006-01-24 17:05:22 +09002421 /* determine if device 0/1 are present */
2422 if (ata_devchk(ap, 0))
2423 devmask |= (1 << 0);
2424 if (slave_possible && ata_devchk(ap, 1))
2425 devmask |= (1 << 1);
2426
Tejun Heoc2bd5802006-01-24 17:05:22 +09002427 /* select device 0 again */
2428 ap->ops->dev_select(ap, 0);
2429
2430 /* issue bus reset */
2431 DPRINTK("about to softreset, devmask=%x\n", devmask);
2432 err_mask = ata_bus_softreset(ap, devmask);
2433 if (err_mask) {
2434 if (verbose)
2435 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2436 ap->id, err_mask);
2437 else
2438 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2439 err_mask);
2440 return -EIO;
2441 }
2442
2443 /* determine by signature whether we have ATA or ATAPI devices */
2444 classes[0] = ata_dev_try_classify(ap, 0, &err);
2445 if (slave_possible && err != 0x81)
2446 classes[1] = ata_dev_try_classify(ap, 1, &err);
2447
Tejun Heo3a397462006-02-10 23:58:48 +09002448 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002449 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2450 return 0;
2451}
2452
2453/**
2454 * sata_std_hardreset - reset host port via SATA phy reset
2455 * @ap: port to reset
2456 * @verbose: fail verbosely
2457 * @class: resulting class of attached device
2458 *
2459 * SATA phy-reset host port using DET bits of SControl register.
2460 * This function is to be used as standard callback for
2461 * ata_drive_*_reset().
2462 *
2463 * LOCKING:
2464 * Kernel thread context (may sleep)
2465 *
2466 * RETURNS:
2467 * 0 on success, -errno otherwise.
2468 */
2469int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2470{
Tejun Heo852ee162006-04-01 01:38:18 +09002471 u32 scontrol;
2472
Tejun Heoc2bd5802006-01-24 17:05:22 +09002473 DPRINTK("ENTER\n");
2474
Tejun Heo1c3fae42006-04-02 20:53:28 +09002475 if (ata_set_sata_spd_needed(ap)) {
2476 /* SATA spec says nothing about how to reconfigure
2477 * spd. To be on the safe side, turn off phy during
2478 * reconfiguration. This works for at least ICH7 AHCI
2479 * and Sil3124.
2480 */
2481 scontrol = scr_read(ap, SCR_CONTROL);
2482 scontrol = (scontrol & 0x0f0) | 0x302;
2483 scr_write_flush(ap, SCR_CONTROL, scontrol);
2484
2485 ata_set_sata_spd(ap);
2486 }
2487
2488 /* issue phy wake/reset */
Tejun Heo852ee162006-04-01 01:38:18 +09002489 scontrol = scr_read(ap, SCR_CONTROL);
2490 scontrol = (scontrol & 0x0f0) | 0x301;
2491 scr_write_flush(ap, SCR_CONTROL, scontrol);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002492
Tejun Heo1c3fae42006-04-02 20:53:28 +09002493 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09002494 * 10.4.2 says at least 1 ms.
2495 */
2496 msleep(1);
2497
Tejun Heo1c3fae42006-04-02 20:53:28 +09002498 /* bring phy back */
Tejun Heo7a7921e2006-02-02 18:20:00 +09002499 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002500
Tejun Heoc2bd5802006-01-24 17:05:22 +09002501 /* TODO: phy layer with polling, timeouts, etc. */
2502 if (!sata_dev_present(ap)) {
2503 *class = ATA_DEV_NONE;
2504 DPRINTK("EXIT, link offline\n");
2505 return 0;
2506 }
2507
2508 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2509 if (verbose)
2510 printk(KERN_ERR "ata%u: COMRESET failed "
2511 "(device not ready)\n", ap->id);
2512 else
2513 DPRINTK("EXIT, device not ready\n");
2514 return -EIO;
2515 }
2516
Tejun Heo3a397462006-02-10 23:58:48 +09002517 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2518
Tejun Heoc2bd5802006-01-24 17:05:22 +09002519 *class = ata_dev_try_classify(ap, 0, NULL);
2520
2521 DPRINTK("EXIT, class=%u\n", *class);
2522 return 0;
2523}
2524
2525/**
2526 * ata_std_postreset - standard postreset callback
2527 * @ap: the target ata_port
2528 * @classes: classes of attached devices
2529 *
2530 * This function is invoked after a successful reset. Note that
2531 * the device might have been reset more than once using
2532 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002533 *
2534 * This function is to be used as standard callback for
2535 * ata_drive_*_reset().
2536 *
2537 * LOCKING:
2538 * Kernel thread context (may sleep)
2539 */
2540void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2541{
2542 DPRINTK("ENTER\n");
2543
Tejun Heo56497bd2006-02-15 15:01:42 +09002544 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002545 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2546 ap->cbl = ATA_CBL_SATA;
2547
2548 /* print link status */
2549 if (ap->cbl == ATA_CBL_SATA)
2550 sata_print_link_status(ap);
2551
Tejun Heo3a397462006-02-10 23:58:48 +09002552 /* re-enable interrupts */
2553 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2554 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002555
2556 /* is double-select really necessary? */
2557 if (classes[0] != ATA_DEV_NONE)
2558 ap->ops->dev_select(ap, 1);
2559 if (classes[1] != ATA_DEV_NONE)
2560 ap->ops->dev_select(ap, 0);
2561
Tejun Heo3a397462006-02-10 23:58:48 +09002562 /* bail out if no device is present */
2563 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2564 DPRINTK("EXIT, no device\n");
2565 return;
2566 }
2567
2568 /* set up device control */
2569 if (ap->ioaddr.ctl_addr) {
2570 if (ap->flags & ATA_FLAG_MMIO)
2571 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2572 else
2573 outb(ap->ctl, ap->ioaddr.ctl_addr);
2574 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002575
2576 DPRINTK("EXIT\n");
2577}
2578
2579/**
2580 * ata_std_probe_reset - standard probe reset method
2581 * @ap: prot to perform probe-reset
2582 * @classes: resulting classes of attached devices
2583 *
2584 * The stock off-the-shelf ->probe_reset method.
2585 *
2586 * LOCKING:
2587 * Kernel thread context (may sleep)
2588 *
2589 * RETURNS:
2590 * 0 on success, -errno otherwise.
2591 */
2592int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2593{
2594 ata_reset_fn_t hardreset;
2595
2596 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002597 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002598 hardreset = sata_std_hardreset;
2599
Tejun Heo8a19ac82006-02-02 18:20:00 +09002600 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002601 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002602 ata_std_postreset, classes);
2603}
2604
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09002605int ata_do_reset(struct ata_port *ap,
2606 ata_reset_fn_t reset, ata_postreset_fn_t postreset,
2607 int verbose, unsigned int *classes)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002608{
2609 int i, rc;
2610
2611 for (i = 0; i < ATA_MAX_DEVICES; i++)
2612 classes[i] = ATA_DEV_UNKNOWN;
2613
Tejun Heo9974e7c2006-04-01 01:38:17 +09002614 rc = reset(ap, verbose, classes);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002615 if (rc)
2616 return rc;
2617
2618 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2619 * is complete and convert all ATA_DEV_UNKNOWN to
2620 * ATA_DEV_NONE.
2621 */
2622 for (i = 0; i < ATA_MAX_DEVICES; i++)
2623 if (classes[i] != ATA_DEV_UNKNOWN)
2624 break;
2625
2626 if (i < ATA_MAX_DEVICES)
2627 for (i = 0; i < ATA_MAX_DEVICES; i++)
2628 if (classes[i] == ATA_DEV_UNKNOWN)
2629 classes[i] = ATA_DEV_NONE;
2630
2631 if (postreset)
2632 postreset(ap, classes);
2633
Tejun Heo9974e7c2006-04-01 01:38:17 +09002634 return 0;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002635}
2636
2637/**
2638 * ata_drive_probe_reset - Perform probe reset with given methods
2639 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002640 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002641 * @softreset: softreset method (can be NULL)
2642 * @hardreset: hardreset method (can be NULL)
2643 * @postreset: postreset method (can be NULL)
2644 * @classes: resulting classes of attached devices
2645 *
2646 * Reset the specified port and classify attached devices using
2647 * given methods. This function prefers softreset but tries all
2648 * possible reset sequences to reset and classify devices. This
2649 * function is intended to be used for constructing ->probe_reset
2650 * callback by low level drivers.
2651 *
2652 * Reset methods should follow the following rules.
2653 *
2654 * - Return 0 on sucess, -errno on failure.
2655 * - If classification is supported, fill classes[] with
2656 * recognized class codes.
2657 * - If classification is not supported, leave classes[] alone.
2658 * - If verbose is non-zero, print error message on failure;
2659 * otherwise, shut up.
2660 *
2661 * LOCKING:
2662 * Kernel thread context (may sleep)
2663 *
2664 * RETURNS:
2665 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2666 * if classification fails, and any error code from reset
2667 * methods.
2668 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002669int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002670 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2671 ata_postreset_fn_t postreset, unsigned int *classes)
2672{
2673 int rc = -EINVAL;
2674
Tejun Heo7944ea92006-02-02 18:20:00 +09002675 if (probeinit)
2676 probeinit(ap);
2677
Tejun Heo90dac022006-04-02 17:54:46 +09002678 if (softreset && !ata_set_sata_spd_needed(ap)) {
Tejun Heo9974e7c2006-04-01 01:38:17 +09002679 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
2680 if (rc == 0 && classes[0] != ATA_DEV_UNKNOWN)
2681 goto done;
Tejun Heoedbabd82006-04-02 20:55:02 +09002682 printk(KERN_INFO "ata%u: softreset failed, will try "
2683 "hardreset in 5 secs\n", ap->id);
2684 ssleep(5);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002685 }
2686
2687 if (!hardreset)
Tejun Heo9974e7c2006-04-01 01:38:17 +09002688 goto done;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002689
Tejun Heo90dac022006-04-02 17:54:46 +09002690 while (1) {
2691 rc = ata_do_reset(ap, hardreset, postreset, 0, classes);
2692 if (rc == 0) {
2693 if (classes[0] != ATA_DEV_UNKNOWN)
2694 goto done;
2695 break;
2696 }
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002697
Tejun Heo90dac022006-04-02 17:54:46 +09002698 if (ata_down_sata_spd_limit(ap))
2699 goto done;
Tejun Heoedbabd82006-04-02 20:55:02 +09002700
2701 printk(KERN_INFO "ata%u: hardreset failed, will retry "
2702 "in 5 secs\n", ap->id);
2703 ssleep(5);
Tejun Heo90dac022006-04-02 17:54:46 +09002704 }
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002705
Tejun Heoedbabd82006-04-02 20:55:02 +09002706 if (softreset) {
2707 printk(KERN_INFO "ata%u: hardreset succeeded without "
2708 "classification, will retry softreset in 5 secs\n",
2709 ap->id);
2710 ssleep(5);
2711
Tejun Heo9974e7c2006-04-01 01:38:17 +09002712 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
Tejun Heoedbabd82006-04-02 20:55:02 +09002713 }
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002714
Tejun Heo9974e7c2006-04-01 01:38:17 +09002715 done:
2716 if (rc == 0 && classes[0] == ATA_DEV_UNKNOWN)
2717 rc = -ENODEV;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002718 return rc;
2719}
2720
Tejun Heo623a3122006-03-05 17:55:58 +09002721/**
2722 * ata_dev_same_device - Determine whether new ID matches configured device
2723 * @ap: port on which the device to compare against resides
2724 * @dev: device to compare against
2725 * @new_class: class of the new device
2726 * @new_id: IDENTIFY page of the new device
2727 *
2728 * Compare @new_class and @new_id against @dev and determine
2729 * whether @dev is the device indicated by @new_class and
2730 * @new_id.
2731 *
2732 * LOCKING:
2733 * None.
2734 *
2735 * RETURNS:
2736 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2737 */
2738static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2739 unsigned int new_class, const u16 *new_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Tejun Heo623a3122006-03-05 17:55:58 +09002741 const u16 *old_id = dev->id;
2742 unsigned char model[2][41], serial[2][21];
2743 u64 new_n_sectors;
2744
2745 if (dev->class != new_class) {
2746 printk(KERN_INFO
2747 "ata%u: dev %u class mismatch %d != %d\n",
2748 ap->id, dev->devno, dev->class, new_class);
2749 return 0;
2750 }
2751
2752 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2753 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2754 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2755 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2756 new_n_sectors = ata_id_n_sectors(new_id);
2757
2758 if (strcmp(model[0], model[1])) {
2759 printk(KERN_INFO
2760 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2761 ap->id, dev->devno, model[0], model[1]);
2762 return 0;
2763 }
2764
2765 if (strcmp(serial[0], serial[1])) {
2766 printk(KERN_INFO
2767 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2768 ap->id, dev->devno, serial[0], serial[1]);
2769 return 0;
2770 }
2771
2772 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2773 printk(KERN_INFO
2774 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2775 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2776 (unsigned long long)new_n_sectors);
2777 return 0;
2778 }
2779
2780 return 1;
2781}
2782
2783/**
2784 * ata_dev_revalidate - Revalidate ATA device
2785 * @ap: port on which the device to revalidate resides
2786 * @dev: device to revalidate
2787 * @post_reset: is this revalidation after reset?
2788 *
2789 * Re-read IDENTIFY page and make sure @dev is still attached to
2790 * the port.
2791 *
2792 * LOCKING:
2793 * Kernel thread context (may sleep)
2794 *
2795 * RETURNS:
2796 * 0 on success, negative errno otherwise
2797 */
2798int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2799 int post_reset)
2800{
Tejun Heo5eb45c02006-04-02 18:51:52 +09002801 unsigned int class = dev->class;
2802 u16 *id = NULL;
Tejun Heo623a3122006-03-05 17:55:58 +09002803 int rc;
2804
Tejun Heo5eb45c02006-04-02 18:51:52 +09002805 if (!ata_dev_enabled(dev)) {
2806 rc = -ENODEV;
2807 goto fail;
2808 }
Tejun Heo623a3122006-03-05 17:55:58 +09002809
2810 /* allocate & read ID data */
2811 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2812 if (rc)
2813 goto fail;
2814
2815 /* is the device still there? */
2816 if (!ata_dev_same_device(ap, dev, class, id)) {
2817 rc = -ENODEV;
2818 goto fail;
2819 }
2820
2821 kfree(dev->id);
2822 dev->id = id;
2823
2824 /* configure device according to the new ID */
Tejun Heo5eb45c02006-04-02 18:51:52 +09002825 rc = ata_dev_configure(ap, dev, 0);
2826 if (rc == 0)
2827 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09002828
2829 fail:
2830 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2831 ap->id, dev->devno, rc);
2832 kfree(id);
2833 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002836static const char * const ata_dma_blacklist [] = {
Alan Coxf4b15fe2006-03-22 15:54:04 +00002837 "WDC AC11000H", NULL,
2838 "WDC AC22100H", NULL,
2839 "WDC AC32500H", NULL,
2840 "WDC AC33100H", NULL,
2841 "WDC AC31600H", NULL,
2842 "WDC AC32100H", "24.09P07",
2843 "WDC AC23200L", "21.10N21",
2844 "Compaq CRD-8241B", NULL,
2845 "CRD-8400B", NULL,
2846 "CRD-8480B", NULL,
2847 "CRD-8482B", NULL,
2848 "CRD-84", NULL,
2849 "SanDisk SDP3B", NULL,
2850 "SanDisk SDP3B-64", NULL,
2851 "SANYO CD-ROM CRD", NULL,
2852 "HITACHI CDR-8", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002853 "HITACHI CDR-8335", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002854 "HITACHI CDR-8435", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002855 "Toshiba CD-ROM XM-6202B", NULL,
2856 "TOSHIBA CD-ROM XM-1702BC", NULL,
2857 "CD-532E-A", NULL,
2858 "E-IDE CD-ROM CR-840", NULL,
2859 "CD-ROM Drive/F5A", NULL,
2860 "WPI CDD-820", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002861 "SAMSUNG CD-ROM SC-148C", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002862 "SAMSUNG CD-ROM SC", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002863 "SanDisk SDP3B-64", NULL,
2864 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2865 "_NEC DV5800A", NULL,
2866 "SAMSUNG CD-ROM SN-124", "N001"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002868
Alan Coxf4b15fe2006-03-22 15:54:04 +00002869static int ata_strim(char *s, size_t len)
2870{
2871 len = strnlen(s, len);
2872
2873 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2874 while ((len > 0) && (s[len - 1] == ' ')) {
2875 len--;
2876 s[len] = 0;
2877 }
2878 return len;
2879}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Jeff Garzik057ace52005-10-22 14:27:05 -04002881static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
Alan Coxf4b15fe2006-03-22 15:54:04 +00002883 unsigned char model_num[40];
2884 unsigned char model_rev[16];
2885 unsigned int nlen, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 int i;
2887
Alan Coxf4b15fe2006-03-22 15:54:04 +00002888 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2889 sizeof(model_num));
2890 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
2891 sizeof(model_rev));
2892 nlen = ata_strim(model_num, sizeof(model_num));
2893 rlen = ata_strim(model_rev, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Alan Coxf4b15fe2006-03-22 15:54:04 +00002895 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
2896 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
2897 if (ata_dma_blacklist[i+1] == NULL)
2898 return 1;
2899 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
2900 return 1;
2901 }
2902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return 0;
2904}
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09002907 * ata_dev_xfermask - Compute supported xfermask of the given device
2908 * @ap: Port on which the device to compute xfermask for resides
2909 * @dev: Device to compute xfermask for
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 *
Tejun Heoacf356b2006-03-24 14:07:50 +09002911 * Compute supported xfermask of @dev and store it in
2912 * dev->*_mask. This function is responsible for applying all
2913 * known limits including host controller limits, device
2914 * blacklist, etc...
Jeff Garzik0cba6322005-05-30 19:49:12 -04002915 *
Tejun Heo600511e2006-03-25 11:14:07 +09002916 * FIXME: The current implementation limits all transfer modes to
2917 * the fastest of the lowested device on the port. This is not
Tejun Heo05c8e0a2006-03-25 14:28:57 +09002918 * required on most controllers.
Tejun Heo600511e2006-03-25 11:14:07 +09002919 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 * LOCKING:
Tejun Heoa6d5a512006-03-06 04:31:57 +09002921 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 */
Tejun Heoacf356b2006-03-24 14:07:50 +09002923static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
Alan Cox5444a6f2006-03-27 18:58:20 +01002925 struct ata_host_set *hs = ap->host_set;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002926 unsigned long xfer_mask;
2927 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Tejun Heo565083e2006-04-02 17:54:47 +09002929 xfer_mask = ata_pack_xfermask(ap->pio_mask,
2930 ap->mwdma_mask, ap->udma_mask);
2931
2932 /* Apply cable rule here. Don't apply it early because when
2933 * we handle hot plug the cable type can itself change.
2934 */
2935 if (ap->cbl == ATA_CBL_PATA40)
2936 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Alan Cox5444a6f2006-03-27 18:58:20 +01002938 /* FIXME: Use port-wide xfermask for now */
Tejun Heoa6d5a512006-03-06 04:31:57 +09002939 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2940 struct ata_device *d = &ap->device[i];
Tejun Heo565083e2006-04-02 17:54:47 +09002941
2942 if (ata_dev_absent(d))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002943 continue;
Tejun Heo565083e2006-04-02 17:54:47 +09002944
2945 if (ata_dev_disabled(d)) {
2946 /* to avoid violating device selection timing */
2947 xfer_mask &= ata_pack_xfermask(d->pio_mask,
2948 UINT_MAX, UINT_MAX);
2949 continue;
2950 }
2951
2952 xfer_mask &= ata_pack_xfermask(d->pio_mask,
2953 d->mwdma_mask, d->udma_mask);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002954 xfer_mask &= ata_id_xfermask(d->id);
2955 if (ata_dma_blacklisted(d))
2956 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958
Tejun Heoa6d5a512006-03-06 04:31:57 +09002959 if (ata_dma_blacklisted(dev))
2960 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2961 "disabling DMA\n", ap->id, dev->devno);
2962
Alan Cox5444a6f2006-03-27 18:58:20 +01002963 if (hs->flags & ATA_HOST_SIMPLEX) {
2964 if (hs->simplex_claimed)
2965 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2966 }
Tejun Heo565083e2006-04-02 17:54:47 +09002967
Alan Cox5444a6f2006-03-27 18:58:20 +01002968 if (ap->ops->mode_filter)
2969 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
2970
Tejun Heo565083e2006-04-02 17:54:47 +09002971 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
2972 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
2975/**
2976 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2977 * @ap: Port associated with device @dev
2978 * @dev: Device to which command will be sent
2979 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002980 * Issue SET FEATURES - XFER MODE command to device @dev
2981 * on port @ap.
2982 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002984 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09002985 *
2986 * RETURNS:
2987 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 */
2989
Tejun Heo83206a22006-03-24 15:25:31 +09002990static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
2991 struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Tejun Heoa0123702005-12-13 14:49:31 +09002993 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09002994 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 /* set up set-features taskfile */
2997 DPRINTK("set features - xfer mode\n");
2998
Tejun Heoa0123702005-12-13 14:49:31 +09002999 ata_tf_init(ap, &tf, dev->devno);
3000 tf.command = ATA_CMD_SET_FEATURES;
3001 tf.feature = SETFEATURES_XFER;
3002 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3003 tf.protocol = ATA_PROT_NODATA;
3004 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Tejun Heod69cf372006-04-02 18:51:53 +09003006 err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Tejun Heo83206a22006-03-24 15:25:31 +09003008 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3009 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
3012/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04003013 * ata_dev_init_params - Issue INIT DEV PARAMS command
3014 * @ap: Port associated with device @dev
3015 * @dev: Device to which command will be sent
3016 *
3017 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09003018 * Kernel thread context (may sleep)
3019 *
3020 * RETURNS:
3021 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04003022 */
3023
Tejun Heo6aff8f12006-02-15 18:24:09 +09003024static unsigned int ata_dev_init_params(struct ata_port *ap,
Albert Lee00b6f5e2006-03-27 16:39:18 +08003025 struct ata_device *dev,
3026 u16 heads,
3027 u16 sectors)
Albert Lee8bf62ec2005-05-12 15:29:42 -04003028{
Tejun Heoa0123702005-12-13 14:49:31 +09003029 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09003030 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003031
3032 /* Number of sectors per track 1-255. Number of heads 1-16 */
3033 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08003034 return AC_ERR_INVALID;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003035
3036 /* set up init dev params taskfile */
3037 DPRINTK("init dev params \n");
3038
Tejun Heoa0123702005-12-13 14:49:31 +09003039 ata_tf_init(ap, &tf, dev->devno);
3040 tf.command = ATA_CMD_INIT_DEV_PARAMS;
3041 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3042 tf.protocol = ATA_PROT_NODATA;
3043 tf.nsect = sectors;
3044 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04003045
Tejun Heod69cf372006-04-02 18:51:53 +09003046 err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04003047
Tejun Heo6aff8f12006-02-15 18:24:09 +09003048 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3049 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003050}
3051
3052/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003053 * ata_sg_clean - Unmap DMA memory associated with command
3054 * @qc: Command containing DMA memory to be released
3055 *
3056 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 *
3058 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003059 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 */
3061
3062static void ata_sg_clean(struct ata_queued_cmd *qc)
3063{
3064 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003065 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003067 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Tejun Heoa4631472006-02-11 19:11:13 +09003069 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
3070 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05003073 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003075 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003077 /* if we padded the buffer out to 32-bit bound, and data
3078 * xfer direction is from-device, we must copy from the
3079 * pad buffer back into the supplied buffer
3080 */
3081 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
3082 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3083
3084 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05003085 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003086 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003087 /* restore last sg */
3088 sg[qc->orig_n_elem - 1].length += qc->pad_len;
3089 if (pad_buf) {
3090 struct scatterlist *psg = &qc->pad_sgent;
3091 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3092 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003093 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003094 }
3095 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09003096 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003097 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05003098 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
3099 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003100 /* restore sg */
3101 sg->length += qc->pad_len;
3102 if (pad_buf)
3103 memcpy(qc->buf_virt + sg->length - qc->pad_len,
3104 pad_buf, qc->pad_len);
3105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003108 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109}
3110
3111/**
3112 * ata_fill_sg - Fill PCI IDE PRD table
3113 * @qc: Metadata associated with taskfile to be transferred
3114 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003115 * Fill PCI IDE PRD (scatter-gather) table with segments
3116 * associated with the current disk command.
3117 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04003119 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 *
3121 */
3122static void ata_fill_sg(struct ata_queued_cmd *qc)
3123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003125 struct scatterlist *sg;
3126 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Tejun Heoa4631472006-02-11 19:11:13 +09003128 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05003129 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003132 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 u32 addr, offset;
3134 u32 sg_len, len;
3135
3136 /* determine if physical DMA addr spans 64K boundary.
3137 * Note h/w doesn't support 64-bit, so we unconditionally
3138 * truncate dma_addr_t to u32.
3139 */
3140 addr = (u32) sg_dma_address(sg);
3141 sg_len = sg_dma_len(sg);
3142
3143 while (sg_len) {
3144 offset = addr & 0xffff;
3145 len = sg_len;
3146 if ((offset + sg_len) > 0x10000)
3147 len = 0x10000 - offset;
3148
3149 ap->prd[idx].addr = cpu_to_le32(addr);
3150 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
3151 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
3152
3153 idx++;
3154 sg_len -= len;
3155 addr += len;
3156 }
3157 }
3158
3159 if (idx)
3160 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3161}
3162/**
3163 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3164 * @qc: Metadata associated with taskfile to check
3165 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003166 * Allow low-level driver to filter ATA PACKET commands, returning
3167 * a status indicating whether or not it is OK to use DMA for the
3168 * supplied PACKET command.
3169 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003171 * spin_lock_irqsave(host_set lock)
3172 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 * RETURNS: 0 when ATAPI DMA can be used
3174 * nonzero otherwise
3175 */
3176int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3177{
3178 struct ata_port *ap = qc->ap;
3179 int rc = 0; /* Assume ATAPI DMA is OK by default */
3180
3181 if (ap->ops->check_atapi_dma)
3182 rc = ap->ops->check_atapi_dma(qc);
3183
Albert Leec2bbc552006-03-25 17:56:55 +08003184 /* We don't support polling DMA.
3185 * Use PIO if the LLDD handles only interrupts in
3186 * the HSM_ST_LAST state and the ATAPI device
3187 * generates CDB interrupts.
3188 */
3189 if ((ap->flags & ATA_FLAG_PIO_POLLING) &&
3190 (qc->dev->flags & ATA_DFLAG_CDB_INTR))
3191 rc = 1;
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return rc;
3194}
3195/**
3196 * ata_qc_prep - Prepare taskfile for submission
3197 * @qc: Metadata associated with taskfile to be prepared
3198 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003199 * Prepare ATA taskfile for submission.
3200 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 * LOCKING:
3202 * spin_lock_irqsave(host_set lock)
3203 */
3204void ata_qc_prep(struct ata_queued_cmd *qc)
3205{
3206 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3207 return;
3208
3209 ata_fill_sg(qc);
3210}
3211
Brian Kinge46834c2006-03-17 17:04:03 -06003212void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
3213
Jeff Garzik0cba6322005-05-30 19:49:12 -04003214/**
3215 * ata_sg_init_one - Associate command with memory buffer
3216 * @qc: Command to be associated
3217 * @buf: Memory buffer
3218 * @buflen: Length of memory buffer, in bytes.
3219 *
3220 * Initialize the data-related elements of queued_cmd @qc
3221 * to point to a single memory buffer, @buf of byte length @buflen.
3222 *
3223 * LOCKING:
3224 * spin_lock_irqsave(host_set lock)
3225 */
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3228{
3229 struct scatterlist *sg;
3230
3231 qc->flags |= ATA_QCFLAG_SINGLE;
3232
3233 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003234 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003236 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 qc->buf_virt = buf;
3238
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003239 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05003240 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
Jeff Garzik0cba6322005-05-30 19:49:12 -04003243/**
3244 * ata_sg_init - Associate command with scatter-gather table.
3245 * @qc: Command to be associated
3246 * @sg: Scatter-gather table.
3247 * @n_elem: Number of elements in s/g table.
3248 *
3249 * Initialize the data-related elements of queued_cmd @qc
3250 * to point to a scatter-gather table @sg, containing @n_elem
3251 * elements.
3252 *
3253 * LOCKING:
3254 * spin_lock_irqsave(host_set lock)
3255 */
3256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3258 unsigned int n_elem)
3259{
3260 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003261 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003263 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
3266/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003267 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3268 * @qc: Command with memory buffer to be mapped.
3269 *
3270 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 *
3272 * LOCKING:
3273 * spin_lock_irqsave(host_set lock)
3274 *
3275 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003276 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 */
3278
3279static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3280{
3281 struct ata_port *ap = qc->ap;
3282 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003283 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003285 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003287 /* we must lengthen transfers to end on a 32-bit boundary */
3288 qc->pad_len = sg->length & 3;
3289 if (qc->pad_len) {
3290 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3291 struct scatterlist *psg = &qc->pad_sgent;
3292
Tejun Heoa4631472006-02-11 19:11:13 +09003293 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003294
3295 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3296
3297 if (qc->tf.flags & ATA_TFLAG_WRITE)
3298 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3299 qc->pad_len);
3300
3301 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3302 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3303 /* trim sg */
3304 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003305 if (sg->length == 0)
3306 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003307
3308 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3309 sg->length, qc->pad_len);
3310 }
3311
Tejun Heo2e242fa2006-02-20 23:48:38 +09003312 if (trim_sg) {
3313 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003314 goto skip_map;
3315 }
3316
Brian King2f1f6102006-03-23 17:30:15 -06003317 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003318 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003319 if (dma_mapping_error(dma_address)) {
3320 /* restore sg */
3321 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
3325 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003326 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Tejun Heo2e242fa2006-02-20 23:48:38 +09003328skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3330 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3331
3332 return 0;
3333}
3334
3335/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003336 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3337 * @qc: Command with scatter-gather table to be mapped.
3338 *
3339 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 *
3341 * LOCKING:
3342 * spin_lock_irqsave(host_set lock)
3343 *
3344 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003345 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 *
3347 */
3348
3349static int ata_sg_setup(struct ata_queued_cmd *qc)
3350{
3351 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003352 struct scatterlist *sg = qc->__sg;
3353 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003354 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003357 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003359 /* we must lengthen transfers to end on a 32-bit boundary */
3360 qc->pad_len = lsg->length & 3;
3361 if (qc->pad_len) {
3362 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3363 struct scatterlist *psg = &qc->pad_sgent;
3364 unsigned int offset;
3365
Tejun Heoa4631472006-02-11 19:11:13 +09003366 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003367
3368 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3369
3370 /*
3371 * psg->page/offset are used to copy to-be-written
3372 * data in this function or read data in ata_sg_clean.
3373 */
3374 offset = lsg->offset + lsg->length - qc->pad_len;
3375 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3376 psg->offset = offset_in_page(offset);
3377
3378 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3379 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3380 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003381 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003382 }
3383
3384 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3385 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3386 /* trim last sg */
3387 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003388 if (lsg->length == 0)
3389 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003390
3391 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3392 qc->n_elem - 1, lsg->length, qc->pad_len);
3393 }
3394
Jeff Garzike1410f22005-11-14 14:06:26 -05003395 pre_n_elem = qc->n_elem;
3396 if (trim_sg && pre_n_elem)
3397 pre_n_elem--;
3398
3399 if (!pre_n_elem) {
3400 n_elem = 0;
3401 goto skip_map;
3402 }
3403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06003405 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003406 if (n_elem < 1) {
3407 /* restore last sg */
3408 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
3412 DPRINTK("%d sg elements mapped\n", n_elem);
3413
Jeff Garzike1410f22005-11-14 14:06:26 -05003414skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 qc->n_elem = n_elem;
3416
3417 return 0;
3418}
3419
3420/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003421 * ata_poll_qc_complete - turn irq back on and finish qc
3422 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003423 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003424 *
3425 * LOCKING:
3426 * None. (grabs host lock)
3427 */
3428
Albert Leea22e2eb2005-12-05 15:38:02 +08003429void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003430{
3431 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003432 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003433
Jeff Garzikb8f61532005-08-25 22:01:20 -04003434 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003435 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003436 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003437 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003438}
3439
3440/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003441 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003442 * @buf: Buffer to swap
3443 * @buf_words: Number of 16-bit words in buffer.
3444 *
3445 * Swap halves of 16-bit words if needed to convert from
3446 * little-endian byte order to native cpu byte order, or
3447 * vice-versa.
3448 *
3449 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003450 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452void swap_buf_le16(u16 *buf, unsigned int buf_words)
3453{
3454#ifdef __BIG_ENDIAN
3455 unsigned int i;
3456
3457 for (i = 0; i < buf_words; i++)
3458 buf[i] = le16_to_cpu(buf[i]);
3459#endif /* __BIG_ENDIAN */
3460}
3461
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003462/**
3463 * ata_mmio_data_xfer - Transfer data by MMIO
3464 * @ap: port to read/write
3465 * @buf: data buffer
3466 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003467 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003468 *
3469 * Transfer data from/to the device data register by MMIO.
3470 *
3471 * LOCKING:
3472 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003473 */
3474
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3476 unsigned int buflen, int write_data)
3477{
3478 unsigned int i;
3479 unsigned int words = buflen >> 1;
3480 u16 *buf16 = (u16 *) buf;
3481 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3482
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003483 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 if (write_data) {
3485 for (i = 0; i < words; i++)
3486 writew(le16_to_cpu(buf16[i]), mmio);
3487 } else {
3488 for (i = 0; i < words; i++)
3489 buf16[i] = cpu_to_le16(readw(mmio));
3490 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003491
3492 /* Transfer trailing 1 byte, if any. */
3493 if (unlikely(buflen & 0x01)) {
3494 u16 align_buf[1] = { 0 };
3495 unsigned char *trailing_buf = buf + buflen - 1;
3496
3497 if (write_data) {
3498 memcpy(align_buf, trailing_buf, 1);
3499 writew(le16_to_cpu(align_buf[0]), mmio);
3500 } else {
3501 align_buf[0] = cpu_to_le16(readw(mmio));
3502 memcpy(trailing_buf, align_buf, 1);
3503 }
3504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505}
3506
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003507/**
3508 * ata_pio_data_xfer - Transfer data by PIO
3509 * @ap: port to read/write
3510 * @buf: data buffer
3511 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003512 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003513 *
3514 * Transfer data from/to the device data register by PIO.
3515 *
3516 * LOCKING:
3517 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003518 */
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3521 unsigned int buflen, int write_data)
3522{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003523 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003525 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003527 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003529 insw(ap->ioaddr.data_addr, buf, words);
3530
3531 /* Transfer trailing 1 byte, if any. */
3532 if (unlikely(buflen & 0x01)) {
3533 u16 align_buf[1] = { 0 };
3534 unsigned char *trailing_buf = buf + buflen - 1;
3535
3536 if (write_data) {
3537 memcpy(align_buf, trailing_buf, 1);
3538 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3539 } else {
3540 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3541 memcpy(trailing_buf, align_buf, 1);
3542 }
3543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003546/**
3547 * ata_data_xfer - Transfer data from/to the data register.
3548 * @ap: port to read/write
3549 * @buf: data buffer
3550 * @buflen: buffer length
3551 * @do_write: read/write
3552 *
3553 * Transfer data from/to the device data register.
3554 *
3555 * LOCKING:
3556 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003557 */
3558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3560 unsigned int buflen, int do_write)
3561{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003562 /* Make the crap hardware pay the costs not the good stuff */
3563 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3564 unsigned long flags;
3565 local_irq_save(flags);
3566 if (ap->flags & ATA_FLAG_MMIO)
3567 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3568 else
3569 ata_pio_data_xfer(ap, buf, buflen, do_write);
3570 local_irq_restore(flags);
3571 } else {
3572 if (ap->flags & ATA_FLAG_MMIO)
3573 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3574 else
3575 ata_pio_data_xfer(ap, buf, buflen, do_write);
3576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003579/**
3580 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3581 * @qc: Command on going
3582 *
3583 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3584 *
3585 * LOCKING:
3586 * Inherited from caller.
3587 */
3588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589static void ata_pio_sector(struct ata_queued_cmd *qc)
3590{
3591 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003592 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 struct ata_port *ap = qc->ap;
3594 struct page *page;
3595 unsigned int offset;
3596 unsigned char *buf;
3597
3598 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003599 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
3601 page = sg[qc->cursg].page;
3602 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3603
3604 /* get the current page and offset */
3605 page = nth_page(page, (offset >> PAGE_SHIFT));
3606 offset %= PAGE_SIZE;
3607
Albert Lee7282aa42005-10-09 09:46:07 -04003608 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3609
Albert Lee91b8b312005-10-09 09:48:44 -04003610 if (PageHighMem(page)) {
3611 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003612
Albert Lee91b8b312005-10-09 09:48:44 -04003613 local_irq_save(flags);
3614 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003615
Albert Lee91b8b312005-10-09 09:48:44 -04003616 /* do the actual data transfer */
3617 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3618
3619 kunmap_atomic(buf, KM_IRQ0);
3620 local_irq_restore(flags);
3621 } else {
3622 buf = page_address(page);
3623 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3624 }
Albert Lee7282aa42005-10-09 09:46:07 -04003625
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 qc->cursect++;
3627 qc->cursg_ofs++;
3628
Albert Lee32529e02005-05-26 03:49:42 -04003629 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 qc->cursg++;
3631 qc->cursg_ofs = 0;
3632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
3634
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003635/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003636 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3637 * @qc: Command on going
3638 *
3639 * Transfer one or many ATA_SECT_SIZE of data from/to the
3640 * ATA device for the DRQ request.
3641 *
3642 * LOCKING:
3643 * Inherited from caller.
3644 */
3645
3646static void ata_pio_sectors(struct ata_queued_cmd *qc)
3647{
3648 if (is_multi_taskfile(&qc->tf)) {
3649 /* READ/WRITE MULTIPLE */
3650 unsigned int nsect;
3651
Jeff Garzik587005d2006-02-11 18:17:32 -05003652 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08003653
3654 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3655 while (nsect--)
3656 ata_pio_sector(qc);
3657 } else
3658 ata_pio_sector(qc);
3659}
3660
3661/**
Albert Leec71c1852005-10-04 06:03:45 -04003662 * atapi_send_cdb - Write CDB bytes to hardware
3663 * @ap: Port to which ATAPI device is attached.
3664 * @qc: Taskfile currently active
3665 *
3666 * When device has indicated its readiness to accept
3667 * a CDB, this function is called. Send the CDB.
3668 *
3669 * LOCKING:
3670 * caller.
3671 */
3672
3673static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3674{
3675 /* send SCSI cdb */
3676 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05003677 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04003678
Jeff Garzikdb024d52006-02-13 00:23:57 -05003679 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04003680 ata_altstatus(ap); /* flush */
3681
3682 switch (qc->tf.protocol) {
3683 case ATA_PROT_ATAPI:
3684 ap->hsm_task_state = HSM_ST;
3685 break;
3686 case ATA_PROT_ATAPI_NODATA:
3687 ap->hsm_task_state = HSM_ST_LAST;
3688 break;
3689 case ATA_PROT_ATAPI_DMA:
3690 ap->hsm_task_state = HSM_ST_LAST;
3691 /* initiate bmdma */
3692 ap->ops->bmdma_start(qc);
3693 break;
3694 }
3695}
3696
3697/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003698 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3699 * @qc: Command on going
3700 * @bytes: number of bytes
3701 *
3702 * Transfer Transfer data from/to the ATAPI device.
3703 *
3704 * LOCKING:
3705 * Inherited from caller.
3706 *
3707 */
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3710{
3711 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003712 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 struct ata_port *ap = qc->ap;
3714 struct page *page;
3715 unsigned char *buf;
3716 unsigned int offset, count;
3717
Albert Lee563a6e12005-08-12 14:17:50 +08003718 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003719 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003722 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003723 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003724 * The end of qc->sg is reached and the device expects
3725 * more data to transfer. In order not to overrun qc->sg
3726 * and fulfill length specified in the byte count register,
3727 * - for read case, discard trailing data from the device
3728 * - for write case, padding zero data to the device
3729 */
3730 u16 pad_buf[1] = { 0 };
3731 unsigned int words = bytes >> 1;
3732 unsigned int i;
3733
3734 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003735 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003736 ap->id, bytes);
3737
3738 for (i = 0; i < words; i++)
3739 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3740
Albert Lee14be71f2005-09-27 17:36:35 +08003741 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003742 return;
3743 }
3744
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003745 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 page = sg->page;
3748 offset = sg->offset + qc->cursg_ofs;
3749
3750 /* get the current page and offset */
3751 page = nth_page(page, (offset >> PAGE_SHIFT));
3752 offset %= PAGE_SIZE;
3753
Albert Lee6952df02005-06-06 15:56:03 +08003754 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003755 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
3757 /* don't cross page boundaries */
3758 count = min(count, (unsigned int)PAGE_SIZE - offset);
3759
Albert Lee7282aa42005-10-09 09:46:07 -04003760 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3761
Albert Lee91b8b312005-10-09 09:48:44 -04003762 if (PageHighMem(page)) {
3763 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003764
Albert Lee91b8b312005-10-09 09:48:44 -04003765 local_irq_save(flags);
3766 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003767
Albert Lee91b8b312005-10-09 09:48:44 -04003768 /* do the actual data transfer */
3769 ata_data_xfer(ap, buf + offset, count, do_write);
3770
3771 kunmap_atomic(buf, KM_IRQ0);
3772 local_irq_restore(flags);
3773 } else {
3774 buf = page_address(page);
3775 ata_data_xfer(ap, buf + offset, count, do_write);
3776 }
Albert Lee7282aa42005-10-09 09:46:07 -04003777
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 bytes -= count;
3779 qc->curbytes += count;
3780 qc->cursg_ofs += count;
3781
Albert Lee32529e02005-05-26 03:49:42 -04003782 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 qc->cursg++;
3784 qc->cursg_ofs = 0;
3785 }
3786
Albert Lee563a6e12005-08-12 14:17:50 +08003787 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789}
3790
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003791/**
3792 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3793 * @qc: Command on going
3794 *
3795 * Transfer Transfer data from/to the ATAPI device.
3796 *
3797 * LOCKING:
3798 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003799 */
3800
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3802{
3803 struct ata_port *ap = qc->ap;
3804 struct ata_device *dev = qc->dev;
3805 unsigned int ireason, bc_lo, bc_hi, bytes;
3806 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3807
3808 ap->ops->tf_read(ap, &qc->tf);
3809 ireason = qc->tf.nsect;
3810 bc_lo = qc->tf.lbam;
3811 bc_hi = qc->tf.lbah;
3812 bytes = (bc_hi << 8) | bc_lo;
3813
3814 /* shall be cleared to zero, indicating xfer of data */
3815 if (ireason & (1 << 0))
3816 goto err_out;
3817
3818 /* make sure transfer direction matches expected */
3819 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3820 if (do_write != i_write)
3821 goto err_out;
3822
Albert Lee312f7da2005-09-27 17:38:03 +08003823 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3824
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 __atapi_pio_bytes(qc, bytes);
3826
3827 return;
3828
3829err_out:
3830 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3831 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003832 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003833 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834}
3835
3836/**
Albert Leec234fb02006-03-25 17:58:38 +08003837 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
3838 * @ap: the target ata_port
3839 * @qc: qc on going
3840 *
3841 * RETURNS:
3842 * 1 if ok in workqueue, 0 otherwise.
3843 */
3844
3845static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
3846{
3847 if (qc->tf.flags & ATA_TFLAG_POLLING)
3848 return 1;
3849
3850 if (ap->hsm_task_state == HSM_ST_FIRST) {
3851 if (qc->tf.protocol == ATA_PROT_PIO &&
3852 (qc->tf.flags & ATA_TFLAG_WRITE))
3853 return 1;
3854
3855 if (is_atapi_taskfile(&qc->tf) &&
3856 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3857 return 1;
3858 }
3859
3860 return 0;
3861}
3862
3863/**
Albert Leebb5cb292006-03-25 17:48:02 +08003864 * ata_hsm_move - move the HSM to the next state.
3865 * @ap: the target ata_port
3866 * @qc: qc on going
3867 * @status: current device status
3868 * @in_wq: 1 if called from workqueue, 0 otherwise
3869 *
3870 * RETURNS:
3871 * 1 when poll next status needed, 0 otherwise.
3872 */
3873
3874static int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
3875 u8 status, int in_wq)
Albert Leee2cec772006-03-25 17:43:49 +08003876{
Albert Leebb5cb292006-03-25 17:48:02 +08003877 unsigned long flags = 0;
3878 int poll_next;
3879
Albert Lee6912ccd2006-03-25 17:45:49 +08003880 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
3881
Albert Leebb5cb292006-03-25 17:48:02 +08003882 /* Make sure ata_qc_issue_prot() does not throw things
3883 * like DMA polling into the workqueue. Notice that
3884 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
3885 */
Albert Leec234fb02006-03-25 17:58:38 +08003886 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Leebb5cb292006-03-25 17:48:02 +08003887
Albert Leee2cec772006-03-25 17:43:49 +08003888fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08003889 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
3890 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
3891
Albert Leee2cec772006-03-25 17:43:49 +08003892 switch (ap->hsm_task_state) {
3893 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08003894 /* Send first data block or PACKET CDB */
3895
3896 /* If polling, we will stay in the work queue after
3897 * sending the data. Otherwise, interrupt handler
3898 * takes over after sending the data.
3899 */
3900 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3901
Albert Leee2cec772006-03-25 17:43:49 +08003902 /* check device status */
3903 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
3904 /* Wrong status. Let EH handle this */
3905 qc->err_mask |= AC_ERR_HSM;
3906 ap->hsm_task_state = HSM_ST_ERR;
3907 goto fsm_start;
3908 }
3909
Albert Lee71601952006-03-25 18:11:12 +08003910 /* Device should not ask for data transfer (DRQ=1)
3911 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08003912 * We ignore DRQ here and stop the HSM by
3913 * changing hsm_task_state to HSM_ST_ERR and
3914 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08003915 */
3916 if (unlikely(status & (ATA_ERR | ATA_DF))) {
3917 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
3918 ap->id, status);
3919 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08003920 ap->hsm_task_state = HSM_ST_ERR;
3921 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08003922 }
3923
Albert Leebb5cb292006-03-25 17:48:02 +08003924 /* Send the CDB (atapi) or the first data block (ata pio out).
3925 * During the state transition, interrupt handler shouldn't
3926 * be invoked before the data transfer is complete and
3927 * hsm_task_state is changed. Hence, the following locking.
3928 */
3929 if (in_wq)
3930 spin_lock_irqsave(&ap->host_set->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08003931
Albert Leebb5cb292006-03-25 17:48:02 +08003932 if (qc->tf.protocol == ATA_PROT_PIO) {
3933 /* PIO data out protocol.
3934 * send first data block.
3935 */
3936
3937 /* ata_pio_sectors() might change the state
3938 * to HSM_ST_LAST. so, the state is changed here
3939 * before ata_pio_sectors().
3940 */
3941 ap->hsm_task_state = HSM_ST;
3942 ata_pio_sectors(qc);
3943 ata_altstatus(ap); /* flush */
3944 } else
3945 /* send CDB */
3946 atapi_send_cdb(ap, qc);
3947
3948 if (in_wq)
3949 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3950
3951 /* if polling, ata_pio_task() handles the rest.
3952 * otherwise, interrupt handler takes over from here.
3953 */
Albert Leee2cec772006-03-25 17:43:49 +08003954 break;
3955
3956 case HSM_ST:
3957 /* complete command or read/write the data register */
3958 if (qc->tf.protocol == ATA_PROT_ATAPI) {
3959 /* ATAPI PIO protocol */
3960 if ((status & ATA_DRQ) == 0) {
3961 /* no more data to transfer */
3962 ap->hsm_task_state = HSM_ST_LAST;
3963 goto fsm_start;
3964 }
3965
Albert Lee71601952006-03-25 18:11:12 +08003966 /* Device should not ask for data transfer (DRQ=1)
3967 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08003968 * We ignore DRQ here and stop the HSM by
3969 * changing hsm_task_state to HSM_ST_ERR and
3970 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08003971 */
3972 if (unlikely(status & (ATA_ERR | ATA_DF))) {
3973 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
3974 ap->id, status);
3975 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08003976 ap->hsm_task_state = HSM_ST_ERR;
3977 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08003978 }
3979
Albert Leee2cec772006-03-25 17:43:49 +08003980 atapi_pio_bytes(qc);
3981
3982 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
3983 /* bad ireason reported by device */
3984 goto fsm_start;
3985
3986 } else {
3987 /* ATA PIO protocol */
3988 if (unlikely((status & ATA_DRQ) == 0)) {
3989 /* handle BSY=0, DRQ=0 as error */
3990 qc->err_mask |= AC_ERR_HSM;
3991 ap->hsm_task_state = HSM_ST_ERR;
3992 goto fsm_start;
3993 }
3994
Albert Leeeee6c322006-04-01 17:38:43 +08003995 /* For PIO reads, some devices may ask for
3996 * data transfer (DRQ=1) alone with ERR=1.
3997 * We respect DRQ here and transfer one
3998 * block of junk data before changing the
3999 * hsm_task_state to HSM_ST_ERR.
4000 *
4001 * For PIO writes, ERR=1 DRQ=1 doesn't make
4002 * sense since the data block has been
4003 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08004004 */
4005 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08004006 /* data might be corrputed */
4007 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08004008
4009 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
4010 ata_pio_sectors(qc);
4011 ata_altstatus(ap);
4012 status = ata_wait_idle(ap);
4013 }
4014
4015 /* ata_pio_sectors() might change the
4016 * state to HSM_ST_LAST. so, the state
4017 * is changed after ata_pio_sectors().
4018 */
4019 ap->hsm_task_state = HSM_ST_ERR;
4020 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004021 }
4022
Albert Leee2cec772006-03-25 17:43:49 +08004023 ata_pio_sectors(qc);
4024
4025 if (ap->hsm_task_state == HSM_ST_LAST &&
4026 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4027 /* all data read */
4028 ata_altstatus(ap);
Albert Lee52a32202006-03-25 18:18:15 +08004029 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08004030 goto fsm_start;
4031 }
4032 }
4033
4034 ata_altstatus(ap); /* flush */
Albert Leebb5cb292006-03-25 17:48:02 +08004035 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08004036 break;
4037
4038 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004039 if (unlikely(!ata_ok(status))) {
4040 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08004041 ap->hsm_task_state = HSM_ST_ERR;
4042 goto fsm_start;
4043 }
4044
4045 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08004046 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
4047 ap->id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08004048
Albert Lee6912ccd2006-03-25 17:45:49 +08004049 WARN_ON(qc->err_mask);
4050
Albert Leee2cec772006-03-25 17:43:49 +08004051 ap->hsm_task_state = HSM_ST_IDLE;
4052
4053 /* complete taskfile transaction */
Albert Leebb5cb292006-03-25 17:48:02 +08004054 if (in_wq)
4055 ata_poll_qc_complete(qc);
4056 else
4057 ata_qc_complete(qc);
4058
4059 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08004060 break;
4061
4062 case HSM_ST_ERR:
4063 if (qc->tf.command != ATA_CMD_PACKET)
Albert Lee4332a772006-04-03 17:43:24 +08004064 printk(KERN_ERR "ata%u: dev %u command error, drv_stat 0x%x\n",
4065 ap->id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08004066
4067 /* make sure qc->err_mask is available to
4068 * know what's wrong and recover
4069 */
4070 WARN_ON(qc->err_mask == 0);
4071
4072 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08004073
Albert Lee999bb6f2006-03-25 18:07:48 +08004074 /* complete taskfile transaction */
Albert Leebb5cb292006-03-25 17:48:02 +08004075 if (in_wq)
4076 ata_poll_qc_complete(qc);
4077 else
4078 ata_qc_complete(qc);
4079
4080 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08004081 break;
4082 default:
Albert Leebb5cb292006-03-25 17:48:02 +08004083 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08004084 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08004085 }
4086
Albert Leebb5cb292006-03-25 17:48:02 +08004087 return poll_next;
Albert Leee2cec772006-03-25 17:43:49 +08004088}
4089
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090static void ata_pio_task(void *_data)
4091{
Tejun Heoc91af2c2006-04-02 18:51:53 +09004092 struct ata_queued_cmd *qc = _data;
4093 struct ata_port *ap = qc->ap;
Albert Leea1af3732006-03-25 17:50:15 +08004094 u8 status;
4095 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004096
4097fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08004098 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Albert Leea1af3732006-03-25 17:50:15 +08004100 /*
4101 * This is purely heuristic. This is a fast path.
4102 * Sometimes when we enter, BSY will be cleared in
4103 * a chk-status or two. If not, the drive is probably seeking
4104 * or something. Snooze for a couple msecs, then
4105 * chk-status again. If still busy, queue delayed work.
4106 */
4107 status = ata_busy_wait(ap, ATA_BUSY, 5);
4108 if (status & ATA_BUSY) {
4109 msleep(2);
4110 status = ata_busy_wait(ap, ATA_BUSY, 10);
4111 if (status & ATA_BUSY) {
Albert Lee31ce6da2006-04-03 18:31:44 +08004112 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08004113 return;
4114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 }
4116
Albert Leea1af3732006-03-25 17:50:15 +08004117 /* move the HSM */
4118 poll_next = ata_hsm_move(ap, qc, status, 1);
4119
4120 /* another command or interrupt handler
4121 * may be running at this point.
4122 */
4123 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004124 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125}
4126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 * ata_qc_new - Request an available ATA command, for queueing
4129 * @ap: Port associated with device @dev
4130 * @dev: Device from whom we request an available command structure
4131 *
4132 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004133 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 */
4135
4136static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4137{
4138 struct ata_queued_cmd *qc = NULL;
4139 unsigned int i;
4140
4141 for (i = 0; i < ATA_MAX_QUEUE; i++)
4142 if (!test_and_set_bit(i, &ap->qactive)) {
4143 qc = ata_qc_from_tag(ap, i);
4144 break;
4145 }
4146
4147 if (qc)
4148 qc->tag = i;
4149
4150 return qc;
4151}
4152
4153/**
4154 * ata_qc_new_init - Request an available ATA command, and initialize it
4155 * @ap: Port associated with device @dev
4156 * @dev: Device from whom we request an available command structure
4157 *
4158 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004159 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 */
4161
4162struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
4163 struct ata_device *dev)
4164{
4165 struct ata_queued_cmd *qc;
4166
4167 qc = ata_qc_new(ap);
4168 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 qc->scsicmd = NULL;
4170 qc->ap = ap;
4171 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004173 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 }
4175
4176 return qc;
4177}
4178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179/**
4180 * ata_qc_free - free unused ata_queued_cmd
4181 * @qc: Command to complete
4182 *
4183 * Designed to free unused ata_queued_cmd object
4184 * in case something prevents using it.
4185 *
4186 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004187 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 */
4189void ata_qc_free(struct ata_queued_cmd *qc)
4190{
Tejun Heo4ba946e2006-01-23 13:09:36 +09004191 struct ata_port *ap = qc->ap;
4192 unsigned int tag;
4193
Tejun Heoa4631472006-02-11 19:11:13 +09004194 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Tejun Heo4ba946e2006-01-23 13:09:36 +09004196 qc->flags = 0;
4197 tag = qc->tag;
4198 if (likely(ata_tag_valid(tag))) {
4199 if (tag == ap->active_tag)
4200 ap->active_tag = ATA_TAG_POISON;
4201 qc->tag = ATA_TAG_POISON;
4202 clear_bit(tag, &ap->qactive);
4203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
Tejun Heo76014422006-02-11 15:13:49 +09004206void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207{
Tejun Heoa4631472006-02-11 19:11:13 +09004208 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4209 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4212 ata_sg_clean(qc);
4213
Albert Lee3f3791d2005-08-16 14:25:38 +08004214 /* atapi: mark qc as inactive to prevent the interrupt handler
4215 * from completing the command twice later, before the error handler
4216 * is called. (when rc != 0 and atapi request sense is needed)
4217 */
4218 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4219
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004221 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222}
4223
4224static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4225{
4226 struct ata_port *ap = qc->ap;
4227
4228 switch (qc->tf.protocol) {
4229 case ATA_PROT_DMA:
4230 case ATA_PROT_ATAPI_DMA:
4231 return 1;
4232
4233 case ATA_PROT_ATAPI:
4234 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 if (ap->flags & ATA_FLAG_PIO_DMA)
4236 return 1;
4237
4238 /* fall through */
4239
4240 default:
4241 return 0;
4242 }
4243
4244 /* never reached */
4245}
4246
4247/**
4248 * ata_qc_issue - issue taskfile to device
4249 * @qc: command to issue to device
4250 *
4251 * Prepare an ATA command to submission to device.
4252 * This includes mapping the data into a DMA-able
4253 * area, filling in the S/G table, and finally
4254 * writing the taskfile to hardware, starting the command.
4255 *
4256 * LOCKING:
4257 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004259void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
4261 struct ata_port *ap = qc->ap;
4262
Tejun Heoe4a70e72006-03-31 20:36:47 +09004263 qc->ap->active_tag = qc->tag;
4264 qc->flags |= ATA_QCFLAG_ACTIVE;
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 if (ata_should_dma_map(qc)) {
4267 if (qc->flags & ATA_QCFLAG_SG) {
4268 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004269 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4271 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004272 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 }
4274 } else {
4275 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4276 }
4277
4278 ap->ops->qc_prep(qc);
4279
Tejun Heo8e0e6942006-03-31 20:41:11 +09004280 qc->err_mask |= ap->ops->qc_issue(qc);
4281 if (unlikely(qc->err_mask))
4282 goto err;
4283 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Tejun Heo8e436af2006-01-23 13:09:36 +09004285sg_err:
4286 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09004287 qc->err_mask |= AC_ERR_SYSTEM;
4288err:
4289 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290}
4291
4292/**
4293 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4294 * @qc: command to issue to device
4295 *
4296 * Using various libata functions and hooks, this function
4297 * starts an ATA command. ATA commands are grouped into
4298 * classes called "protocols", and issuing each type of protocol
4299 * is slightly different.
4300 *
Edward Falk0baab862005-06-02 18:17:13 -04004301 * May be used as the qc_issue() entry in ata_port_operations.
4302 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 * LOCKING:
4304 * spin_lock_irqsave(host_set lock)
4305 *
4306 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004307 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 */
4309
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004310unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{
4312 struct ata_port *ap = qc->ap;
4313
Albert Leee50362e2005-09-27 17:39:50 +08004314 /* Use polling pio if the LLD doesn't handle
4315 * interrupt driven pio and atapi CDB interrupt.
4316 */
4317 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4318 switch (qc->tf.protocol) {
4319 case ATA_PROT_PIO:
4320 case ATA_PROT_ATAPI:
4321 case ATA_PROT_ATAPI_NODATA:
4322 qc->tf.flags |= ATA_TFLAG_POLLING;
4323 break;
4324 case ATA_PROT_ATAPI_DMA:
4325 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Leec2bbc552006-03-25 17:56:55 +08004326 /* see ata_check_atapi_dma() */
Albert Leee50362e2005-09-27 17:39:50 +08004327 BUG();
4328 break;
4329 default:
4330 break;
4331 }
4332 }
4333
Albert Lee312f7da2005-09-27 17:38:03 +08004334 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 ata_dev_select(ap, qc->dev->devno, 1, 0);
4336
Albert Lee312f7da2005-09-27 17:38:03 +08004337 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 switch (qc->tf.protocol) {
4339 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004340 if (qc->tf.flags & ATA_TFLAG_POLLING)
4341 ata_qc_set_polling(qc);
4342
Jeff Garzike5338252005-10-30 21:37:17 -05004343 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004344 ap->hsm_task_state = HSM_ST_LAST;
4345
4346 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08004347 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 break;
4350
4351 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004352 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4355 ap->ops->bmdma_setup(qc); /* set up bmdma */
4356 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004357 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 break;
4359
Albert Lee312f7da2005-09-27 17:38:03 +08004360 case ATA_PROT_PIO:
4361 if (qc->tf.flags & ATA_TFLAG_POLLING)
4362 ata_qc_set_polling(qc);
4363
Jeff Garzike5338252005-10-30 21:37:17 -05004364 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004365
Albert Lee54f00382005-09-30 19:14:19 +08004366 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4367 /* PIO data out protocol */
4368 ap->hsm_task_state = HSM_ST_FIRST;
Albert Lee31ce6da2006-04-03 18:31:44 +08004369 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08004370
4371 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08004372 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08004373 */
Albert Lee312f7da2005-09-27 17:38:03 +08004374 } else {
Albert Lee54f00382005-09-30 19:14:19 +08004375 /* PIO data in protocol */
4376 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08004377
Albert Lee54f00382005-09-30 19:14:19 +08004378 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08004379 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004380
Albert Lee54f00382005-09-30 19:14:19 +08004381 /* if polling, ata_pio_task() handles the rest.
4382 * otherwise, interrupt handler takes over from here.
4383 */
Albert Lee312f7da2005-09-27 17:38:03 +08004384 }
4385
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 break;
4387
4388 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004390 if (qc->tf.flags & ATA_TFLAG_POLLING)
4391 ata_qc_set_polling(qc);
4392
Jeff Garzike5338252005-10-30 21:37:17 -05004393 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004394
Albert Lee312f7da2005-09-27 17:38:03 +08004395 ap->hsm_task_state = HSM_ST_FIRST;
4396
4397 /* send cdb by polling if no cdb interrupt */
4398 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4399 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee31ce6da2006-04-03 18:31:44 +08004400 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 break;
4402
4403 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004404 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004405
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4407 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004408 ap->hsm_task_state = HSM_ST_FIRST;
4409
4410 /* send cdb by polling if no cdb interrupt */
4411 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee31ce6da2006-04-03 18:31:44 +08004412 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 break;
4414
4415 default:
4416 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004417 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 }
4419
4420 return 0;
4421}
4422
4423/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 * ata_host_intr - Handle host interrupt for given (port, task)
4425 * @ap: Port on which interrupt arrived (possibly...)
4426 * @qc: Taskfile currently active in engine
4427 *
4428 * Handle host interrupt for given queued command. Currently,
4429 * only DMA interrupts are handled. All other commands are
4430 * handled via polling with interrupts disabled (nIEN bit).
4431 *
4432 * LOCKING:
4433 * spin_lock_irqsave(host_set lock)
4434 *
4435 * RETURNS:
4436 * One if interrupt was handled, zero if not (shared irq).
4437 */
4438
4439inline unsigned int ata_host_intr (struct ata_port *ap,
4440 struct ata_queued_cmd *qc)
4441{
Albert Lee312f7da2005-09-27 17:38:03 +08004442 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Albert Lee312f7da2005-09-27 17:38:03 +08004444 VPRINTK("ata%u: protocol %d task_state %d\n",
4445 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Albert Lee312f7da2005-09-27 17:38:03 +08004447 /* Check whether we are expecting interrupt in this state */
4448 switch (ap->hsm_task_state) {
4449 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004450 /* Some pre-ATAPI-4 devices assert INTRQ
4451 * at this state when ready to receive CDB.
4452 */
4453
Albert Lee312f7da2005-09-27 17:38:03 +08004454 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4455 * The flag was turned on only for atapi devices.
4456 * No need to check is_atapi_taskfile(&qc->tf) again.
4457 */
4458 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08004460 break;
4461 case HSM_ST_LAST:
4462 if (qc->tf.protocol == ATA_PROT_DMA ||
4463 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4464 /* check status of DMA engine */
4465 host_stat = ap->ops->bmdma_status(ap);
4466 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Albert Lee312f7da2005-09-27 17:38:03 +08004468 /* if it's not our irq... */
4469 if (!(host_stat & ATA_DMA_INTR))
4470 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
Albert Lee312f7da2005-09-27 17:38:03 +08004472 /* before we do anything else, clear DMA-Start bit */
4473 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08004474
4475 if (unlikely(host_stat & ATA_DMA_ERR)) {
4476 /* error when transfering data to/from memory */
4477 qc->err_mask |= AC_ERR_HOST_BUS;
4478 ap->hsm_task_state = HSM_ST_ERR;
4479 }
Albert Lee312f7da2005-09-27 17:38:03 +08004480 }
4481 break;
4482 case HSM_ST:
4483 break;
4484 default:
4485 goto idle_irq;
4486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Albert Lee312f7da2005-09-27 17:38:03 +08004488 /* check altstatus */
4489 status = ata_altstatus(ap);
4490 if (status & ATA_BUSY)
4491 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Albert Lee312f7da2005-09-27 17:38:03 +08004493 /* check main status, clearing INTRQ */
4494 status = ata_chk_status(ap);
4495 if (unlikely(status & ATA_BUSY))
4496 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Albert Lee312f7da2005-09-27 17:38:03 +08004498 /* ack bmdma irq events */
4499 ap->ops->irq_clear(ap);
4500
Albert Leebb5cb292006-03-25 17:48:02 +08004501 ata_hsm_move(ap, qc, status, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 return 1; /* irq handled */
4503
4504idle_irq:
4505 ap->stats.idle_irq++;
4506
4507#ifdef ATA_IRQ_TRAP
4508 if ((ap->stats.idle_irq % 1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 ata_irq_ack(ap, 0); /* debug trap */
4510 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
Alan Cox23cfce82006-03-21 16:06:53 +00004511 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 }
4513#endif
4514 return 0; /* irq not handled */
4515}
4516
4517/**
4518 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004519 * @irq: irq line (unused)
4520 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 * @regs: unused
4522 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004523 * Default interrupt handler for PCI IDE devices. Calls
4524 * ata_host_intr() for each port that is not disabled.
4525 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004527 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 *
4529 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004530 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 */
4532
4533irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4534{
4535 struct ata_host_set *host_set = dev_instance;
4536 unsigned int i;
4537 unsigned int handled = 0;
4538 unsigned long flags;
4539
4540 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4541 spin_lock_irqsave(&host_set->lock, flags);
4542
4543 for (i = 0; i < host_set->n_ports; i++) {
4544 struct ata_port *ap;
4545
4546 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004547 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04004548 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 struct ata_queued_cmd *qc;
4550
4551 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004552 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004553 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 handled |= ata_host_intr(ap, qc);
4555 }
4556 }
4557
4558 spin_unlock_irqrestore(&host_set->lock, flags);
4559
4560 return IRQ_RETVAL(handled);
4561}
4562
Edward Falk0baab862005-06-02 18:17:13 -04004563
Jens Axboe9b847542006-01-06 09:28:07 +01004564/*
4565 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4566 * without filling any other registers
4567 */
4568static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4569 u8 cmd)
4570{
4571 struct ata_taskfile tf;
4572 int err;
4573
4574 ata_tf_init(ap, &tf, dev->devno);
4575
4576 tf.command = cmd;
4577 tf.flags |= ATA_TFLAG_DEVICE;
4578 tf.protocol = ATA_PROT_NODATA;
4579
Tejun Heod69cf372006-04-02 18:51:53 +09004580 err = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
Jens Axboe9b847542006-01-06 09:28:07 +01004581 if (err)
4582 printk(KERN_ERR "%s: ata command failed: %d\n",
4583 __FUNCTION__, err);
4584
4585 return err;
4586}
4587
4588static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4589{
4590 u8 cmd;
4591
4592 if (!ata_try_flush_cache(dev))
4593 return 0;
4594
4595 if (ata_id_has_flush_ext(dev->id))
4596 cmd = ATA_CMD_FLUSH_EXT;
4597 else
4598 cmd = ATA_CMD_FLUSH;
4599
4600 return ata_do_simple_cmd(ap, dev, cmd);
4601}
4602
4603static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4604{
4605 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4606}
4607
4608static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4609{
4610 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4611}
4612
4613/**
4614 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004615 * @ap: port the device is connected to
4616 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004617 *
4618 * Kick the drive back into action, by sending it an idle immediate
4619 * command and making sure its transfer mode matches between drive
4620 * and host.
4621 *
4622 */
4623int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4624{
4625 if (ap->flags & ATA_FLAG_SUSPENDED) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09004626 struct ata_device *failed_dev;
Jens Axboe9b847542006-01-06 09:28:07 +01004627 ap->flags &= ~ATA_FLAG_SUSPENDED;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09004628 while (ata_set_mode(ap, &failed_dev))
4629 ata_dev_disable(ap, failed_dev);
Jens Axboe9b847542006-01-06 09:28:07 +01004630 }
Tejun Heoe1211e32006-04-01 01:38:18 +09004631 if (!ata_dev_enabled(dev))
Jens Axboe9b847542006-01-06 09:28:07 +01004632 return 0;
4633 if (dev->class == ATA_DEV_ATA)
4634 ata_start_drive(ap, dev);
4635
4636 return 0;
4637}
4638
4639/**
4640 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004641 * @ap: port the device is connected to
4642 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004643 *
4644 * Flush the cache on the drive, if appropriate, then issue a
4645 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004646 */
Nigel Cunningham082776e2006-03-23 23:22:16 +10004647int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
Jens Axboe9b847542006-01-06 09:28:07 +01004648{
Tejun Heoe1211e32006-04-01 01:38:18 +09004649 if (!ata_dev_enabled(dev))
Jens Axboe9b847542006-01-06 09:28:07 +01004650 return 0;
4651 if (dev->class == ATA_DEV_ATA)
4652 ata_flush_cache(ap, dev);
4653
Nigel Cunningham082776e2006-03-23 23:22:16 +10004654 if (state.event != PM_EVENT_FREEZE)
4655 ata_standby_drive(ap, dev);
Jens Axboe9b847542006-01-06 09:28:07 +01004656 ap->flags |= ATA_FLAG_SUSPENDED;
4657 return 0;
4658}
4659
Albert Lee332b5a52006-02-08 16:51:34 +08004660/**
4661 * ata_port_start - Set port up for dma.
4662 * @ap: Port to initialize
4663 *
4664 * Called just after data structures for each port are
4665 * initialized. Allocates space for PRD table.
4666 *
4667 * May be used as the port_start() entry in ata_port_operations.
4668 *
4669 * LOCKING:
4670 * Inherited from caller.
4671 */
4672
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673int ata_port_start (struct ata_port *ap)
4674{
Brian King2f1f6102006-03-23 17:30:15 -06004675 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004676 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
4678 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4679 if (!ap->prd)
4680 return -ENOMEM;
4681
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004682 rc = ata_pad_alloc(ap, dev);
4683 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004684 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004685 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004686 }
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4689
4690 return 0;
4691}
4692
Edward Falk0baab862005-06-02 18:17:13 -04004693
4694/**
4695 * ata_port_stop - Undo ata_port_start()
4696 * @ap: Port to shut down
4697 *
4698 * Frees the PRD table.
4699 *
4700 * May be used as the port_stop() entry in ata_port_operations.
4701 *
4702 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004703 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004704 */
4705
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706void ata_port_stop (struct ata_port *ap)
4707{
Brian King2f1f6102006-03-23 17:30:15 -06004708 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004711 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004714void ata_host_stop (struct ata_host_set *host_set)
4715{
4716 if (host_set->mmio_base)
4717 iounmap(host_set->mmio_base);
4718}
4719
4720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721/**
4722 * ata_host_remove - Unregister SCSI host structure with upper layers
4723 * @ap: Port to unregister
4724 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4725 *
4726 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004727 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 */
4729
4730static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4731{
4732 struct Scsi_Host *sh = ap->host;
4733
4734 DPRINTK("ENTER\n");
4735
4736 if (do_unregister)
4737 scsi_remove_host(sh);
4738
4739 ap->ops->port_stop(ap);
4740}
4741
4742/**
4743 * ata_host_init - Initialize an ata_port structure
4744 * @ap: Structure to initialize
4745 * @host: associated SCSI mid-layer structure
4746 * @host_set: Collection of hosts to which @ap belongs
4747 * @ent: Probe information provided by low-level driver
4748 * @port_no: Port number associated with this ata_port
4749 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004750 * Initialize a new ata_port structure, and its associated
4751 * scsi_host.
4752 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004754 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 */
4756
4757static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4758 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004759 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
4761 unsigned int i;
4762
4763 host->max_id = 16;
4764 host->max_lun = 1;
4765 host->max_channel = 1;
4766 host->unique_id = ata_unique_id++;
4767 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004768
Tejun Heo198e0fe2006-04-02 18:51:52 +09004769 ap->flags = ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 ap->id = host->unique_id;
4771 ap->host = host;
4772 ap->ctl = ATA_DEVCTL_OBS;
4773 ap->host_set = host_set;
Brian King2f1f6102006-03-23 17:30:15 -06004774 ap->dev = ent->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 ap->port_no = port_no;
4776 ap->hard_port_no =
4777 ent->legacy_mode ? ent->hard_port_no : port_no;
4778 ap->pio_mask = ent->pio_mask;
4779 ap->mwdma_mask = ent->mwdma_mask;
4780 ap->udma_mask = ent->udma_mask;
4781 ap->flags |= ent->host_flags;
4782 ap->ops = ent->port_ops;
4783 ap->cbl = ATA_CBL_NONE;
Tejun Heo1c3fae42006-04-02 20:53:28 +09004784 ap->sata_spd_limit = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 ap->active_tag = ATA_TAG_POISON;
4786 ap->last_ctl = 0xFF;
4787
Tejun Heo86e45b62006-03-05 15:29:09 +09004788 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004789 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
Tejun Heoacf356b2006-03-24 14:07:50 +09004791 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4792 struct ata_device *dev = &ap->device[i];
4793 dev->devno = i;
4794 dev->pio_mask = UINT_MAX;
4795 dev->mwdma_mask = UINT_MAX;
4796 dev->udma_mask = UINT_MAX;
4797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
4799#ifdef ATA_IRQ_TRAP
4800 ap->stats.unhandled_irq = 1;
4801 ap->stats.idle_irq = 1;
4802#endif
4803
4804 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4805}
4806
4807/**
4808 * ata_host_add - Attach low-level ATA driver to system
4809 * @ent: Information provided by low-level driver
4810 * @host_set: Collections of ports to which we add
4811 * @port_no: Port number associated with this host
4812 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004813 * Attach low-level ATA driver to system.
4814 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004816 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 *
4818 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004819 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 */
4821
Jeff Garzik057ace52005-10-22 14:27:05 -04004822static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 struct ata_host_set *host_set,
4824 unsigned int port_no)
4825{
4826 struct Scsi_Host *host;
4827 struct ata_port *ap;
4828 int rc;
4829
4830 DPRINTK("ENTER\n");
Tejun Heoaec5c3c2006-03-25 01:33:34 +09004831
4832 if (!ent->port_ops->probe_reset &&
4833 !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
4834 printk(KERN_ERR "ata%u: no reset mechanism available\n",
4835 port_no);
4836 return NULL;
4837 }
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4840 if (!host)
4841 return NULL;
4842
Tejun Heo30afc842006-03-18 18:40:14 +09004843 host->transportt = &ata_scsi_transport_template;
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 ap = (struct ata_port *) &host->hostdata[0];
4846
4847 ata_host_init(ap, host, host_set, ent, port_no);
4848
4849 rc = ap->ops->port_start(ap);
4850 if (rc)
4851 goto err_out;
4852
4853 return ap;
4854
4855err_out:
4856 scsi_host_put(host);
4857 return NULL;
4858}
4859
4860/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004861 * ata_device_add - Register hardware device with ATA and SCSI layers
4862 * @ent: Probe information describing hardware device to be registered
4863 *
4864 * This function processes the information provided in the probe
4865 * information struct @ent, allocates the necessary ATA and SCSI
4866 * host information structures, initializes them, and registers
4867 * everything with requisite kernel subsystems.
4868 *
4869 * This function requests irqs, probes the ATA bus, and probes
4870 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 *
4872 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004873 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 *
4875 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004876 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 */
4878
Jeff Garzik057ace52005-10-22 14:27:05 -04004879int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880{
4881 unsigned int count = 0, i;
4882 struct device *dev = ent->dev;
4883 struct ata_host_set *host_set;
4884
4885 DPRINTK("ENTER\n");
4886 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004887 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4889 if (!host_set)
4890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 spin_lock_init(&host_set->lock);
4892
4893 host_set->dev = dev;
4894 host_set->n_ports = ent->n_ports;
4895 host_set->irq = ent->irq;
4896 host_set->mmio_base = ent->mmio_base;
4897 host_set->private_data = ent->private_data;
4898 host_set->ops = ent->port_ops;
Alan Cox5444a6f2006-03-27 18:58:20 +01004899 host_set->flags = ent->host_set_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901 /* register each port bound to this device */
4902 for (i = 0; i < ent->n_ports; i++) {
4903 struct ata_port *ap;
4904 unsigned long xfer_mode_mask;
4905
4906 ap = ata_host_add(ent, host_set, i);
4907 if (!ap)
4908 goto err_out;
4909
4910 host_set->ports[i] = ap;
4911 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4912 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4913 (ap->pio_mask << ATA_SHIFT_PIO);
4914
4915 /* print per-port info to dmesg */
4916 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4917 "bmdma 0x%lX irq %lu\n",
4918 ap->id,
4919 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4920 ata_mode_string(xfer_mode_mask),
4921 ap->ioaddr.cmd_addr,
4922 ap->ioaddr.ctl_addr,
4923 ap->ioaddr.bmdma_addr,
4924 ent->irq);
4925
4926 ata_chk_status(ap);
4927 host_set->ops->irq_clear(ap);
4928 count++;
4929 }
4930
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004931 if (!count)
4932 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
4934 /* obtain irq, that is shared between channels */
4935 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4936 DRV_NAME, host_set))
4937 goto err_out;
4938
4939 /* perform each probe synchronously */
4940 DPRINTK("probe begin\n");
4941 for (i = 0; i < count; i++) {
4942 struct ata_port *ap;
4943 int rc;
4944
4945 ap = host_set->ports[i];
4946
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004947 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004949 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950
4951 if (rc) {
4952 /* FIXME: do something useful here?
4953 * Current libata behavior will
4954 * tear down everything when
4955 * the module is removed
4956 * or the h/w is unplugged.
4957 */
4958 }
4959
4960 rc = scsi_add_host(ap->host, dev);
4961 if (rc) {
4962 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4963 ap->id);
4964 /* FIXME: do something useful here */
4965 /* FIXME: handle unconditional calls to
4966 * scsi_scan_host and ata_host_remove, below,
4967 * at the very least
4968 */
4969 }
4970 }
4971
4972 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004973 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 for (i = 0; i < count; i++) {
4975 struct ata_port *ap = host_set->ports[i];
4976
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004977 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 }
4979
4980 dev_set_drvdata(dev, host_set);
4981
4982 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4983 return ent->n_ports; /* success */
4984
4985err_out:
4986 for (i = 0; i < count; i++) {
4987 ata_host_remove(host_set->ports[i], 1);
4988 scsi_host_put(host_set->ports[i]->host);
4989 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004990err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 kfree(host_set);
4992 VPRINTK("EXIT, returning 0\n");
4993 return 0;
4994}
4995
4996/**
Alan Cox17b14452005-09-15 15:44:00 +01004997 * ata_host_set_remove - PCI layer callback for device removal
4998 * @host_set: ATA host set that was removed
4999 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005000 * Unregister all objects associated with this host set. Free those
Alan Cox17b14452005-09-15 15:44:00 +01005001 * objects.
5002 *
5003 * LOCKING:
5004 * Inherited from calling layer (may sleep).
5005 */
5006
Alan Cox17b14452005-09-15 15:44:00 +01005007void ata_host_set_remove(struct ata_host_set *host_set)
5008{
5009 struct ata_port *ap;
5010 unsigned int i;
5011
5012 for (i = 0; i < host_set->n_ports; i++) {
5013 ap = host_set->ports[i];
5014 scsi_remove_host(ap->host);
5015 }
5016
5017 free_irq(host_set->irq, host_set);
5018
5019 for (i = 0; i < host_set->n_ports; i++) {
5020 ap = host_set->ports[i];
5021
5022 ata_scsi_release(ap->host);
5023
5024 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
5025 struct ata_ioports *ioaddr = &ap->ioaddr;
5026
5027 if (ioaddr->cmd_addr == 0x1f0)
5028 release_region(0x1f0, 8);
5029 else if (ioaddr->cmd_addr == 0x170)
5030 release_region(0x170, 8);
5031 }
5032
5033 scsi_host_put(ap->host);
5034 }
5035
5036 if (host_set->ops->host_stop)
5037 host_set->ops->host_stop(host_set);
5038
5039 kfree(host_set);
5040}
5041
5042/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 * ata_scsi_release - SCSI layer callback hook for host unload
5044 * @host: libata host to be unloaded
5045 *
5046 * Performs all duties necessary to shut down a libata port...
5047 * Kill port kthread, disable port, and release resources.
5048 *
5049 * LOCKING:
5050 * Inherited from SCSI layer.
5051 *
5052 * RETURNS:
5053 * One.
5054 */
5055
5056int ata_scsi_release(struct Scsi_Host *host)
5057{
5058 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09005059 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
5061 DPRINTK("ENTER\n");
5062
5063 ap->ops->port_disable(ap);
5064 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09005065 for (i = 0; i < ATA_MAX_DEVICES; i++)
5066 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068 DPRINTK("EXIT\n");
5069 return 1;
5070}
5071
5072/**
5073 * ata_std_ports - initialize ioaddr with standard port offsets.
5074 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04005075 *
5076 * Utility function which initializes data_addr, error_addr,
5077 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5078 * device_addr, status_addr, and command_addr to standard offsets
5079 * relative to cmd_addr.
5080 *
5081 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 */
Edward Falk0baab862005-06-02 18:17:13 -04005083
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084void ata_std_ports(struct ata_ioports *ioaddr)
5085{
5086 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5087 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5088 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5089 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5090 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5091 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5092 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5093 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5094 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5095 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5096}
5097
Edward Falk0baab862005-06-02 18:17:13 -04005098
Jeff Garzik374b1872005-08-30 05:42:52 -04005099#ifdef CONFIG_PCI
5100
5101void ata_pci_host_stop (struct ata_host_set *host_set)
5102{
5103 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5104
5105 pci_iounmap(pdev, host_set->mmio_base);
5106}
5107
Edward Falk0baab862005-06-02 18:17:13 -04005108/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 * ata_pci_remove_one - PCI layer callback for device removal
5110 * @pdev: PCI device that was removed
5111 *
5112 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005113 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 * Handle this by unregistering all objects associated
5115 * with this PCI device. Free those objects. Then finally
5116 * release PCI resources and disable device.
5117 *
5118 * LOCKING:
5119 * Inherited from PCI layer (may sleep).
5120 */
5121
5122void ata_pci_remove_one (struct pci_dev *pdev)
5123{
5124 struct device *dev = pci_dev_to_dev(pdev);
5125 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
Alan Cox17b14452005-09-15 15:44:00 +01005127 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 pci_release_regions(pdev);
5129 pci_disable_device(pdev);
5130 dev_set_drvdata(dev, NULL);
5131}
5132
5133/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005134int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135{
5136 unsigned long tmp = 0;
5137
5138 switch (bits->width) {
5139 case 1: {
5140 u8 tmp8 = 0;
5141 pci_read_config_byte(pdev, bits->reg, &tmp8);
5142 tmp = tmp8;
5143 break;
5144 }
5145 case 2: {
5146 u16 tmp16 = 0;
5147 pci_read_config_word(pdev, bits->reg, &tmp16);
5148 tmp = tmp16;
5149 break;
5150 }
5151 case 4: {
5152 u32 tmp32 = 0;
5153 pci_read_config_dword(pdev, bits->reg, &tmp32);
5154 tmp = tmp32;
5155 break;
5156 }
5157
5158 default:
5159 return -EINVAL;
5160 }
5161
5162 tmp &= bits->mask;
5163
5164 return (tmp == bits->val) ? 1 : 0;
5165}
Jens Axboe9b847542006-01-06 09:28:07 +01005166
5167int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5168{
5169 pci_save_state(pdev);
5170 pci_disable_device(pdev);
5171 pci_set_power_state(pdev, PCI_D3hot);
5172 return 0;
5173}
5174
5175int ata_pci_device_resume(struct pci_dev *pdev)
5176{
5177 pci_set_power_state(pdev, PCI_D0);
5178 pci_restore_state(pdev);
5179 pci_enable_device(pdev);
5180 pci_set_master(pdev);
5181 return 0;
5182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183#endif /* CONFIG_PCI */
5184
5185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186static int __init ata_init(void)
5187{
5188 ata_wq = create_workqueue("ata");
5189 if (!ata_wq)
5190 return -ENOMEM;
5191
5192 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5193 return 0;
5194}
5195
5196static void __exit ata_exit(void)
5197{
5198 destroy_workqueue(ata_wq);
5199}
5200
5201module_init(ata_init);
5202module_exit(ata_exit);
5203
Jeff Garzik67846b32005-10-05 02:58:32 -04005204static unsigned long ratelimit_time;
5205static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5206
5207int ata_ratelimit(void)
5208{
5209 int rc;
5210 unsigned long flags;
5211
5212 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5213
5214 if (time_after(jiffies, ratelimit_time)) {
5215 rc = 1;
5216 ratelimit_time = jiffies + (HZ/5);
5217 } else
5218 rc = 0;
5219
5220 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5221
5222 return rc;
5223}
5224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225/*
5226 * libata is essentially a library of internal helper functions for
5227 * low-level ATA host controller drivers. As such, the API/ABI is
5228 * likely to change as new drivers are added and updated.
5229 * Do not depend on ABI/API stability.
5230 */
5231
5232EXPORT_SYMBOL_GPL(ata_std_bios_param);
5233EXPORT_SYMBOL_GPL(ata_std_ports);
5234EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005235EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236EXPORT_SYMBOL_GPL(ata_sg_init);
5237EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09005238EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240EXPORT_SYMBOL_GPL(ata_tf_load);
5241EXPORT_SYMBOL_GPL(ata_tf_read);
5242EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5243EXPORT_SYMBOL_GPL(ata_std_dev_select);
5244EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5245EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5246EXPORT_SYMBOL_GPL(ata_check_status);
5247EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248EXPORT_SYMBOL_GPL(ata_exec_command);
5249EXPORT_SYMBOL_GPL(ata_port_start);
5250EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005251EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252EXPORT_SYMBOL_GPL(ata_interrupt);
5253EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06005254EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5256EXPORT_SYMBOL_GPL(ata_bmdma_start);
5257EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5258EXPORT_SYMBOL_GPL(ata_bmdma_status);
5259EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5260EXPORT_SYMBOL_GPL(ata_port_probe);
5261EXPORT_SYMBOL_GPL(sata_phy_reset);
5262EXPORT_SYMBOL_GPL(__sata_phy_reset);
5263EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005264EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005265EXPORT_SYMBOL_GPL(ata_std_softreset);
5266EXPORT_SYMBOL_GPL(sata_std_hardreset);
5267EXPORT_SYMBOL_GPL(ata_std_postreset);
5268EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005269EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09005270EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005271EXPORT_SYMBOL_GPL(ata_dev_classify);
5272EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005274EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005275EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09005276EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5278EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5280EXPORT_SYMBOL_GPL(ata_scsi_release);
5281EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo6a62a042006-02-13 10:02:46 +09005282EXPORT_SYMBOL_GPL(ata_id_string);
5283EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5285
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005286EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005287EXPORT_SYMBOL_GPL(ata_timing_compute);
5288EXPORT_SYMBOL_GPL(ata_timing_merge);
5289
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290#ifdef CONFIG_PCI
5291EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005292EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5294EXPORT_SYMBOL_GPL(ata_pci_init_one);
5295EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005296EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5297EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Alan Cox67951ad2006-03-22 15:55:54 +00005298EXPORT_SYMBOL_GPL(ata_pci_default_filter);
5299EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005301
5302EXPORT_SYMBOL_GPL(ata_device_suspend);
5303EXPORT_SYMBOL_GPL(ata_device_resume);
5304EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5305EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
Tejun Heoece1d632006-04-02 18:51:53 +09005306
5307EXPORT_SYMBOL_GPL(ata_scsi_error);
5308EXPORT_SYMBOL_GPL(ata_eng_timeout);
5309EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5310EXPORT_SYMBOL_GPL(ata_eh_qc_retry);