blob: 9de48dd4234a05d0ed55781c2f97e300b6818859 [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{
1520 u32 sstatus, 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);
1526
1527 if (sata_dev_present(ap)) {
1528 tmp = (sstatus >> 4) & 0xf;
Tejun Heo4c360c82006-04-01 01:38:17 +09001529 printk(KERN_INFO "ata%u: SATA link up %s (SStatus %X)\n",
1530 ap->id, sata_spd_string(tmp), sstatus);
Tejun Heo3be680b2005-12-19 22:35:02 +09001531 } else {
1532 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1533 ap->id, sstatus);
1534 }
1535}
1536
1537/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001538 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1539 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001541 * This function issues commands to standard SATA Sxxx
1542 * PHY registers, to wake up the phy (and device), and
1543 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 *
1545 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001546 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 *
1548 */
1549void __sata_phy_reset(struct ata_port *ap)
1550{
1551 u32 sstatus;
1552 unsigned long timeout = jiffies + (HZ * 5);
1553
1554 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001555 /* issue phy wake/reset */
1556 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001557 /* Couldn't find anything in SATA I/II specs, but
1558 * AHCI-1.1 10.4.2 says at least 1 ms. */
1559 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
Brett Russcdcca892005-03-28 15:10:27 -05001561 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 /* wait for phy to become ready, if necessary */
1564 do {
1565 msleep(200);
1566 sstatus = scr_read(ap, SCR_STATUS);
1567 if ((sstatus & 0xf) != 1)
1568 break;
1569 } while (time_before(jiffies, timeout));
1570
Tejun Heo3be680b2005-12-19 22:35:02 +09001571 /* print link status */
1572 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001573
Tejun Heo3be680b2005-12-19 22:35:02 +09001574 /* TODO: phy layer with polling, timeouts, etc. */
1575 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001577 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Tejun Heo198e0fe2006-04-02 18:51:52 +09001580 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return;
1582
1583 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1584 ata_port_disable(ap);
1585 return;
1586 }
1587
1588 ap->cbl = ATA_CBL_SATA;
1589}
1590
1591/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001592 * sata_phy_reset - Reset SATA bus.
1593 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001595 * This function resets the SATA bus, and then probes
1596 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 *
1598 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001599 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 *
1601 */
1602void sata_phy_reset(struct ata_port *ap)
1603{
1604 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09001605 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return;
1607 ata_bus_reset(ap);
1608}
1609
1610/**
Alan Coxebdfca62006-03-23 15:38:34 +00001611 * ata_dev_pair - return other device on cable
1612 * @ap: port
1613 * @adev: device
1614 *
1615 * Obtain the other device on the same cable, or if none is
1616 * present NULL is returned
1617 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001618
Alan Coxebdfca62006-03-23 15:38:34 +00001619struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev)
1620{
1621 struct ata_device *pair = &ap->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09001622 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00001623 return NULL;
1624 return pair;
1625}
1626
1627/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001628 * ata_port_disable - Disable port.
1629 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001631 * Modify @ap data structure such that the system
1632 * thinks that the entire port is disabled, and should
1633 * never attempt to probe or communicate with devices
1634 * on this port.
1635 *
1636 * LOCKING: host_set lock, or some other form of
1637 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 */
1639
1640void ata_port_disable(struct ata_port *ap)
1641{
1642 ap->device[0].class = ATA_DEV_NONE;
1643 ap->device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09001644 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Tejun Heo1c3fae42006-04-02 20:53:28 +09001647/**
1648 * ata_down_sata_spd_limit - adjust SATA spd limit downward
1649 * @ap: Port to adjust SATA spd limit for
1650 *
1651 * Adjust SATA spd limit of @ap downward. Note that this
1652 * function only adjusts the limit. The change must be applied
1653 * using ata_set_sata_spd().
1654 *
1655 * LOCKING:
1656 * Inherited from caller.
1657 *
1658 * RETURNS:
1659 * 0 on success, negative errno on failure
1660 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09001661int ata_down_sata_spd_limit(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001662{
1663 u32 spd, mask;
1664 int highbit;
1665
1666 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1667 return -EOPNOTSUPP;
1668
1669 mask = ap->sata_spd_limit;
1670 if (mask <= 1)
1671 return -EINVAL;
1672 highbit = fls(mask) - 1;
1673 mask &= ~(1 << highbit);
1674
1675 spd = (scr_read(ap, SCR_STATUS) >> 4) & 0xf;
1676 if (spd <= 1)
1677 return -EINVAL;
1678 spd--;
1679 mask &= (1 << spd) - 1;
1680 if (!mask)
1681 return -EINVAL;
1682
1683 ap->sata_spd_limit = mask;
1684
1685 printk(KERN_WARNING "ata%u: limiting SATA link speed to %s\n",
1686 ap->id, sata_spd_string(fls(mask)));
1687
1688 return 0;
1689}
1690
1691static int __ata_set_sata_spd_needed(struct ata_port *ap, u32 *scontrol)
1692{
1693 u32 spd, limit;
1694
1695 if (ap->sata_spd_limit == UINT_MAX)
1696 limit = 0;
1697 else
1698 limit = fls(ap->sata_spd_limit);
1699
1700 spd = (*scontrol >> 4) & 0xf;
1701 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
1702
1703 return spd != limit;
1704}
1705
1706/**
1707 * ata_set_sata_spd_needed - is SATA spd configuration needed
1708 * @ap: Port in question
1709 *
1710 * Test whether the spd limit in SControl matches
1711 * @ap->sata_spd_limit. This function is used to determine
1712 * whether hardreset is necessary to apply SATA spd
1713 * configuration.
1714 *
1715 * LOCKING:
1716 * Inherited from caller.
1717 *
1718 * RETURNS:
1719 * 1 if SATA spd configuration is needed, 0 otherwise.
1720 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09001721int ata_set_sata_spd_needed(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001722{
1723 u32 scontrol;
1724
1725 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1726 return 0;
1727
1728 scontrol = scr_read(ap, SCR_CONTROL);
1729
1730 return __ata_set_sata_spd_needed(ap, &scontrol);
1731}
1732
1733/**
1734 * ata_set_sata_spd - set SATA spd according to spd limit
1735 * @ap: Port to set SATA spd for
1736 *
1737 * Set SATA spd of @ap according to sata_spd_limit.
1738 *
1739 * LOCKING:
1740 * Inherited from caller.
1741 *
1742 * RETURNS:
1743 * 0 if spd doesn't need to be changed, 1 if spd has been
1744 * changed. -EOPNOTSUPP if SCR registers are inaccessible.
1745 */
1746static int ata_set_sata_spd(struct ata_port *ap)
1747{
1748 u32 scontrol;
1749
1750 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1751 return -EOPNOTSUPP;
1752
1753 scontrol = scr_read(ap, SCR_CONTROL);
1754 if (!__ata_set_sata_spd_needed(ap, &scontrol))
1755 return 0;
1756
1757 scr_write(ap, SCR_CONTROL, scontrol);
1758 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Alan Cox452503f2005-10-21 19:01:32 -04001761/*
1762 * This mode timing computation functionality is ported over from
1763 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1764 */
1765/*
1766 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1767 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1768 * for PIO 5, which is a nonstandard extension and UDMA6, which
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001769 * is currently supported only by Maxtor drives.
Alan Cox452503f2005-10-21 19:01:32 -04001770 */
1771
1772static const struct ata_timing ata_timing[] = {
1773
1774 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1775 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1776 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1777 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1778
1779 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1780 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1781 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1782
1783/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001784
Alan Cox452503f2005-10-21 19:01:32 -04001785 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1786 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1787 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001788
Alan Cox452503f2005-10-21 19:01:32 -04001789 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1790 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1791 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1792
1793/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1794 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1795 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1796
1797 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1798 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1799 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1800
1801/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1802
1803 { 0xFF }
1804};
1805
1806#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1807#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1808
1809static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1810{
1811 q->setup = EZ(t->setup * 1000, T);
1812 q->act8b = EZ(t->act8b * 1000, T);
1813 q->rec8b = EZ(t->rec8b * 1000, T);
1814 q->cyc8b = EZ(t->cyc8b * 1000, T);
1815 q->active = EZ(t->active * 1000, T);
1816 q->recover = EZ(t->recover * 1000, T);
1817 q->cycle = EZ(t->cycle * 1000, T);
1818 q->udma = EZ(t->udma * 1000, UT);
1819}
1820
1821void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1822 struct ata_timing *m, unsigned int what)
1823{
1824 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1825 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1826 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1827 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1828 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1829 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1830 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1831 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1832}
1833
1834static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1835{
1836 const struct ata_timing *t;
1837
1838 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001839 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001840 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001841 return t;
Alan Cox452503f2005-10-21 19:01:32 -04001842}
1843
1844int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1845 struct ata_timing *t, int T, int UT)
1846{
1847 const struct ata_timing *s;
1848 struct ata_timing p;
1849
1850 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001851 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001852 */
Alan Cox452503f2005-10-21 19:01:32 -04001853
1854 if (!(s = ata_timing_find_mode(speed)))
1855 return -EINVAL;
1856
Albert Lee75b1f2f2005-11-16 17:06:18 +08001857 memcpy(t, s, sizeof(*s));
1858
Alan Cox452503f2005-10-21 19:01:32 -04001859 /*
1860 * If the drive is an EIDE drive, it can tell us it needs extended
1861 * PIO/MW_DMA cycle timing.
1862 */
1863
1864 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1865 memset(&p, 0, sizeof(p));
1866 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1867 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1868 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1869 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1870 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1871 }
1872 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1873 }
1874
1875 /*
1876 * Convert the timing to bus clock counts.
1877 */
1878
Albert Lee75b1f2f2005-11-16 17:06:18 +08001879 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001880
1881 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001882 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1883 * S.M.A.R.T * and some other commands. We have to ensure that the
1884 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001885 */
1886
1887 if (speed > XFER_PIO_4) {
1888 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1889 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1890 }
1891
1892 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001893 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001894 */
1895
1896 if (t->act8b + t->rec8b < t->cyc8b) {
1897 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1898 t->rec8b = t->cyc8b - t->act8b;
1899 }
1900
1901 if (t->active + t->recover < t->cycle) {
1902 t->active += (t->cycle - (t->active + t->recover)) / 2;
1903 t->recover = t->cycle - t->active;
1904 }
1905
1906 return 0;
1907}
1908
Tejun Heocf176e12006-04-02 17:54:46 +09001909/**
1910 * ata_down_xfermask_limit - adjust dev xfer masks downward
1911 * @ap: Port associated with device @dev
1912 * @dev: Device to adjust xfer masks
1913 * @force_pio0: Force PIO0
1914 *
1915 * Adjust xfer masks of @dev downward. Note that this function
1916 * does not apply the change. Invoking ata_set_mode() afterwards
1917 * will apply the limit.
1918 *
1919 * LOCKING:
1920 * Inherited from caller.
1921 *
1922 * RETURNS:
1923 * 0 on success, negative errno on failure
1924 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09001925int ata_down_xfermask_limit(struct ata_port *ap, struct ata_device *dev,
1926 int force_pio0)
Tejun Heocf176e12006-04-02 17:54:46 +09001927{
1928 unsigned long xfer_mask;
1929 int highbit;
1930
1931 xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
1932 dev->udma_mask);
1933
1934 if (!xfer_mask)
1935 goto fail;
1936 /* don't gear down to MWDMA from UDMA, go directly to PIO */
1937 if (xfer_mask & ATA_MASK_UDMA)
1938 xfer_mask &= ~ATA_MASK_MWDMA;
1939
1940 highbit = fls(xfer_mask) - 1;
1941 xfer_mask &= ~(1 << highbit);
1942 if (force_pio0)
1943 xfer_mask &= 1 << ATA_SHIFT_PIO;
1944 if (!xfer_mask)
1945 goto fail;
1946
1947 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
1948 &dev->udma_mask);
1949
1950 printk(KERN_WARNING "ata%u: dev %u limiting speed to %s\n",
1951 ap->id, dev->devno, ata_mode_string(xfer_mask));
1952
1953 return 0;
1954
1955 fail:
1956 return -EINVAL;
1957}
1958
Tejun Heo83206a22006-03-24 15:25:31 +09001959static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Tejun Heo83206a22006-03-24 15:25:31 +09001961 unsigned int err_mask;
1962 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Tejun Heoe8384602006-04-02 18:51:53 +09001964 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (dev->xfer_shift == ATA_SHIFT_PIO)
1966 dev->flags |= ATA_DFLAG_PIO;
1967
Tejun Heo83206a22006-03-24 15:25:31 +09001968 err_mask = ata_dev_set_xfermode(ap, dev);
1969 if (err_mask) {
1970 printk(KERN_ERR
1971 "ata%u: failed to set xfermode (err_mask=0x%x)\n",
1972 ap->id, err_mask);
1973 return -EIO;
1974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Tejun Heo83206a22006-03-24 15:25:31 +09001976 rc = ata_dev_revalidate(ap, dev, 0);
Tejun Heo5eb45c02006-04-02 18:51:52 +09001977 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09001978 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Tejun Heo23e71c32006-03-06 04:31:57 +09001980 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1981 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
Tejun Heo23e71c32006-03-06 04:31:57 +09001984 ap->id, dev->devno,
1985 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09001986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989/**
1990 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1991 * @ap: port on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001992 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 *
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001994 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
1995 * ata_set_mode() fails, pointer to the failing device is
1996 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04001997 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001999 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002000 *
2001 * RETURNS:
2002 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09002004int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Tejun Heoe8e06192006-04-01 01:38:18 +09002006 struct ata_device *dev;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002007 int i, rc = 0, used_dma = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Tejun Heoa6d5a512006-03-06 04:31:57 +09002009 /* step 1: calculate xfer_mask */
2010 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002011 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002012
Tejun Heoe8e06192006-04-01 01:38:18 +09002013 dev = &ap->device[i];
2014
Tejun Heoe1211e32006-04-01 01:38:18 +09002015 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002016 continue;
2017
Tejun Heoacf356b2006-03-24 14:07:50 +09002018 ata_dev_xfermask(ap, dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002019
Tejun Heoacf356b2006-03-24 14:07:50 +09002020 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2021 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2022 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2023 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002024
Tejun Heo4f659772006-04-01 01:38:18 +09002025 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002026 if (dev->dma_mode)
2027 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002028 }
Tejun Heo4f659772006-04-01 01:38:18 +09002029 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002030 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002031
2032 /* step 2: always set host PIO timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002033 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2034 dev = &ap->device[i];
2035 if (!ata_dev_enabled(dev))
2036 continue;
2037
2038 if (!dev->pio_mode) {
2039 printk(KERN_WARNING "ata%u: dev %u no PIO support\n",
2040 ap->id, dev->devno);
2041 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002042 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002043 }
2044
2045 dev->xfer_mode = dev->pio_mode;
2046 dev->xfer_shift = ATA_SHIFT_PIO;
2047 if (ap->ops->set_piomode)
2048 ap->ops->set_piomode(ap, dev);
2049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Tejun Heoa6d5a512006-03-06 04:31:57 +09002051 /* step 3: set host DMA timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002052 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2053 dev = &ap->device[i];
2054
2055 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2056 continue;
2057
2058 dev->xfer_mode = dev->dma_mode;
2059 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2060 if (ap->ops->set_dmamode)
2061 ap->ops->set_dmamode(ap, dev);
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 /* step 4: update devices' xfer mode */
Tejun Heo83206a22006-03-24 15:25:31 +09002065 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002066 dev = &ap->device[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Tejun Heoe1211e32006-04-01 01:38:18 +09002068 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002069 continue;
2070
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002071 rc = ata_dev_set_mode(ap, dev);
2072 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002073 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Tejun Heoe8e06192006-04-01 01:38:18 +09002076 /* Record simplex status. If we selected DMA then the other
2077 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002078 */
Alan Cox5444a6f2006-03-27 18:58:20 +01002079 if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
2080 ap->host_set->simplex_claimed = 1;
2081
Tejun Heoe8e06192006-04-01 01:38:18 +09002082 /* step5: chip specific finalisation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (ap->ops->post_set_mode)
2084 ap->ops->post_set_mode(ap);
2085
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002086 out:
2087 if (rc)
2088 *r_failed_dev = dev;
2089 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
2092/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002093 * ata_tf_to_host - issue ATA taskfile to host controller
2094 * @ap: port to which command is being issued
2095 * @tf: ATA taskfile register set
2096 *
2097 * Issues ATA taskfile register set to ATA host controller,
2098 * with proper synchronization with interrupt handler and
2099 * other threads.
2100 *
2101 * LOCKING:
2102 * spin_lock_irqsave(host_set lock)
2103 */
2104
2105static inline void ata_tf_to_host(struct ata_port *ap,
2106 const struct ata_taskfile *tf)
2107{
2108 ap->ops->tf_load(ap, tf);
2109 ap->ops->exec_command(ap, tf);
2110}
2111
2112/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 * ata_busy_sleep - sleep until BSY clears, or timeout
2114 * @ap: port containing status register to be polled
2115 * @tmout_pat: impatience timeout
2116 * @tmout: overall timeout
2117 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002118 * Sleep until ATA Status register bit BSY clears,
2119 * or a timeout occurs.
2120 *
2121 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 */
2123
Tejun Heo6f8b9952006-01-24 17:05:21 +09002124unsigned int ata_busy_sleep (struct ata_port *ap,
2125 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 unsigned long timer_start, timeout;
2128 u8 status;
2129
2130 status = ata_busy_wait(ap, ATA_BUSY, 300);
2131 timer_start = jiffies;
2132 timeout = timer_start + tmout_pat;
2133 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2134 msleep(50);
2135 status = ata_busy_wait(ap, ATA_BUSY, 3);
2136 }
2137
2138 if (status & ATA_BUSY)
2139 printk(KERN_WARNING "ata%u is slow to respond, "
2140 "please be patient\n", ap->id);
2141
2142 timeout = timer_start + tmout;
2143 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2144 msleep(50);
2145 status = ata_chk_status(ap);
2146 }
2147
2148 if (status & ATA_BUSY) {
2149 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
2150 ap->id, tmout / HZ);
2151 return 1;
2152 }
2153
2154 return 0;
2155}
2156
2157static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2158{
2159 struct ata_ioports *ioaddr = &ap->ioaddr;
2160 unsigned int dev0 = devmask & (1 << 0);
2161 unsigned int dev1 = devmask & (1 << 1);
2162 unsigned long timeout;
2163
2164 /* if device 0 was found in ata_devchk, wait for its
2165 * BSY bit to clear
2166 */
2167 if (dev0)
2168 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2169
2170 /* if device 1 was found in ata_devchk, wait for
2171 * register access, then wait for BSY to clear
2172 */
2173 timeout = jiffies + ATA_TMOUT_BOOT;
2174 while (dev1) {
2175 u8 nsect, lbal;
2176
2177 ap->ops->dev_select(ap, 1);
2178 if (ap->flags & ATA_FLAG_MMIO) {
2179 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2180 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2181 } else {
2182 nsect = inb(ioaddr->nsect_addr);
2183 lbal = inb(ioaddr->lbal_addr);
2184 }
2185 if ((nsect == 1) && (lbal == 1))
2186 break;
2187 if (time_after(jiffies, timeout)) {
2188 dev1 = 0;
2189 break;
2190 }
2191 msleep(50); /* give drive a breather */
2192 }
2193 if (dev1)
2194 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2195
2196 /* is all this really necessary? */
2197 ap->ops->dev_select(ap, 0);
2198 if (dev1)
2199 ap->ops->dev_select(ap, 1);
2200 if (dev0)
2201 ap->ops->dev_select(ap, 0);
2202}
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204static unsigned int ata_bus_softreset(struct ata_port *ap,
2205 unsigned int devmask)
2206{
2207 struct ata_ioports *ioaddr = &ap->ioaddr;
2208
2209 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2210
2211 /* software reset. causes dev0 to be selected */
2212 if (ap->flags & ATA_FLAG_MMIO) {
2213 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2214 udelay(20); /* FIXME: flush */
2215 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2216 udelay(20); /* FIXME: flush */
2217 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2218 } else {
2219 outb(ap->ctl, ioaddr->ctl_addr);
2220 udelay(10);
2221 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2222 udelay(10);
2223 outb(ap->ctl, ioaddr->ctl_addr);
2224 }
2225
2226 /* spec mandates ">= 2ms" before checking status.
2227 * We wait 150ms, because that was the magic delay used for
2228 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2229 * between when the ATA command register is written, and then
2230 * status is checked. Because waiting for "a while" before
2231 * checking status is fine, post SRST, we perform this magic
2232 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00002233 *
2234 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 */
2236 msleep(150);
2237
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002238 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09002239 * the bus shows 0xFF because the odd clown forgets the D7
2240 * pulldown resistor.
2241 */
Alan Cox09c7ad72006-03-22 15:52:40 +00002242 if (ata_check_status(ap) == 0xFF)
Tejun Heo298a41c2006-03-25 02:58:13 +09002243 return AC_ERR_OTHER;
Alan Cox09c7ad72006-03-22 15:52:40 +00002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 ata_bus_post_reset(ap, devmask);
2246
2247 return 0;
2248}
2249
2250/**
2251 * ata_bus_reset - reset host port and associated ATA channel
2252 * @ap: port to reset
2253 *
2254 * This is typically the first time we actually start issuing
2255 * commands to the ATA channel. We wait for BSY to clear, then
2256 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2257 * result. Determine what devices, if any, are on the channel
2258 * by looking at the device 0/1 error register. Look at the signature
2259 * stored in each device's taskfile registers, to determine if
2260 * the device is ATA or ATAPI.
2261 *
2262 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002263 * PCI/etc. bus probe sem.
2264 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 *
2266 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09002267 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 */
2269
2270void ata_bus_reset(struct ata_port *ap)
2271{
2272 struct ata_ioports *ioaddr = &ap->ioaddr;
2273 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2274 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002275 unsigned int dev0, dev1 = 0, devmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2278
2279 /* determine if device 0/1 are present */
2280 if (ap->flags & ATA_FLAG_SATA_RESET)
2281 dev0 = 1;
2282 else {
2283 dev0 = ata_devchk(ap, 0);
2284 if (slave_possible)
2285 dev1 = ata_devchk(ap, 1);
2286 }
2287
2288 if (dev0)
2289 devmask |= (1 << 0);
2290 if (dev1)
2291 devmask |= (1 << 1);
2292
2293 /* select device 0 again */
2294 ap->ops->dev_select(ap, 0);
2295
2296 /* issue bus reset */
2297 if (ap->flags & ATA_FLAG_SRST)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002298 if (ata_bus_softreset(ap, devmask))
2299 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 /*
2302 * determine by signature whether we have ATA or ATAPI devices
2303 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002304 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002306 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 /* re-enable interrupts */
2309 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2310 ata_irq_on(ap);
2311
2312 /* is double-select really necessary? */
2313 if (ap->device[1].class != ATA_DEV_NONE)
2314 ap->ops->dev_select(ap, 1);
2315 if (ap->device[0].class != ATA_DEV_NONE)
2316 ap->ops->dev_select(ap, 0);
2317
2318 /* if no devices were detected, disable this port */
2319 if ((ap->device[0].class == ATA_DEV_NONE) &&
2320 (ap->device[1].class == ATA_DEV_NONE))
2321 goto err_out;
2322
2323 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2324 /* set up device control for ATA_FLAG_SATA_RESET */
2325 if (ap->flags & ATA_FLAG_MMIO)
2326 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2327 else
2328 outb(ap->ctl, ioaddr->ctl_addr);
2329 }
2330
2331 DPRINTK("EXIT\n");
2332 return;
2333
2334err_out:
2335 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2336 ap->ops->port_disable(ap);
2337
2338 DPRINTK("EXIT\n");
2339}
2340
Tejun Heo7a7921e2006-02-02 18:20:00 +09002341static int sata_phy_resume(struct ata_port *ap)
2342{
2343 unsigned long timeout = jiffies + (HZ * 5);
Tejun Heo852ee162006-04-01 01:38:18 +09002344 u32 scontrol, sstatus;
Tejun Heo7a7921e2006-02-02 18:20:00 +09002345
Tejun Heo852ee162006-04-01 01:38:18 +09002346 scontrol = scr_read(ap, SCR_CONTROL);
2347 scontrol = (scontrol & 0x0f0) | 0x300;
2348 scr_write_flush(ap, SCR_CONTROL, scontrol);
Tejun Heo7a7921e2006-02-02 18:20:00 +09002349
2350 /* Wait for phy to become ready, if necessary. */
2351 do {
2352 msleep(200);
2353 sstatus = scr_read(ap, SCR_STATUS);
2354 if ((sstatus & 0xf) != 1)
2355 return 0;
2356 } while (time_before(jiffies, timeout));
2357
2358 return -1;
2359}
2360
Tejun Heoc2bd5802006-01-24 17:05:22 +09002361/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002362 * ata_std_probeinit - initialize probing
2363 * @ap: port to be probed
2364 *
2365 * @ap is about to be probed. Initialize it. This function is
2366 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002367 *
2368 * NOTE!!! Do not use this function as probeinit if a low level
2369 * driver implements only hardreset. Just pass NULL as probeinit
2370 * in that case. Using this function is probably okay but doing
2371 * so makes reset sequence different from the original
2372 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002373 */
Alan Cox17efc5f2006-03-27 19:01:32 +01002374void ata_std_probeinit(struct ata_port *ap)
Tejun Heo8a19ac82006-02-02 18:20:00 +09002375{
Alan Cox17efc5f2006-03-27 19:01:32 +01002376 if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09002377 u32 spd;
2378
Tejun Heo8a19ac82006-02-02 18:20:00 +09002379 sata_phy_resume(ap);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002380
2381 spd = (scr_read(ap, SCR_CONTROL) & 0xf0) >> 4;
2382 if (spd)
2383 ap->sata_spd_limit &= (1 << spd) - 1;
2384
Tejun Heo3a397462006-02-10 23:58:48 +09002385 if (sata_dev_present(ap))
2386 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2387 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002388}
2389
2390/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002391 * ata_std_softreset - reset host port via ATA SRST
2392 * @ap: port to reset
2393 * @verbose: fail verbosely
2394 * @classes: resulting classes of attached devices
2395 *
2396 * Reset host port using ATA SRST. This function is to be used
2397 * as standard callback for ata_drive_*_reset() functions.
2398 *
2399 * LOCKING:
2400 * Kernel thread context (may sleep)
2401 *
2402 * RETURNS:
2403 * 0 on success, -errno otherwise.
2404 */
2405int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2406{
2407 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2408 unsigned int devmask = 0, err_mask;
2409 u8 err;
2410
2411 DPRINTK("ENTER\n");
2412
Tejun Heo3a397462006-02-10 23:58:48 +09002413 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2414 classes[0] = ATA_DEV_NONE;
2415 goto out;
2416 }
2417
Tejun Heoc2bd5802006-01-24 17:05:22 +09002418 /* determine if device 0/1 are present */
2419 if (ata_devchk(ap, 0))
2420 devmask |= (1 << 0);
2421 if (slave_possible && ata_devchk(ap, 1))
2422 devmask |= (1 << 1);
2423
Tejun Heoc2bd5802006-01-24 17:05:22 +09002424 /* select device 0 again */
2425 ap->ops->dev_select(ap, 0);
2426
2427 /* issue bus reset */
2428 DPRINTK("about to softreset, devmask=%x\n", devmask);
2429 err_mask = ata_bus_softreset(ap, devmask);
2430 if (err_mask) {
2431 if (verbose)
2432 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2433 ap->id, err_mask);
2434 else
2435 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2436 err_mask);
2437 return -EIO;
2438 }
2439
2440 /* determine by signature whether we have ATA or ATAPI devices */
2441 classes[0] = ata_dev_try_classify(ap, 0, &err);
2442 if (slave_possible && err != 0x81)
2443 classes[1] = ata_dev_try_classify(ap, 1, &err);
2444
Tejun Heo3a397462006-02-10 23:58:48 +09002445 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002446 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2447 return 0;
2448}
2449
2450/**
2451 * sata_std_hardreset - reset host port via SATA phy reset
2452 * @ap: port to reset
2453 * @verbose: fail verbosely
2454 * @class: resulting class of attached device
2455 *
2456 * SATA phy-reset host port using DET bits of SControl register.
2457 * This function is to be used as standard callback for
2458 * ata_drive_*_reset().
2459 *
2460 * LOCKING:
2461 * Kernel thread context (may sleep)
2462 *
2463 * RETURNS:
2464 * 0 on success, -errno otherwise.
2465 */
2466int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2467{
Tejun Heo852ee162006-04-01 01:38:18 +09002468 u32 scontrol;
2469
Tejun Heoc2bd5802006-01-24 17:05:22 +09002470 DPRINTK("ENTER\n");
2471
Tejun Heo1c3fae42006-04-02 20:53:28 +09002472 if (ata_set_sata_spd_needed(ap)) {
2473 /* SATA spec says nothing about how to reconfigure
2474 * spd. To be on the safe side, turn off phy during
2475 * reconfiguration. This works for at least ICH7 AHCI
2476 * and Sil3124.
2477 */
2478 scontrol = scr_read(ap, SCR_CONTROL);
2479 scontrol = (scontrol & 0x0f0) | 0x302;
2480 scr_write_flush(ap, SCR_CONTROL, scontrol);
2481
2482 ata_set_sata_spd(ap);
2483 }
2484
2485 /* issue phy wake/reset */
Tejun Heo852ee162006-04-01 01:38:18 +09002486 scontrol = scr_read(ap, SCR_CONTROL);
2487 scontrol = (scontrol & 0x0f0) | 0x301;
2488 scr_write_flush(ap, SCR_CONTROL, scontrol);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002489
Tejun Heo1c3fae42006-04-02 20:53:28 +09002490 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09002491 * 10.4.2 says at least 1 ms.
2492 */
2493 msleep(1);
2494
Tejun Heo1c3fae42006-04-02 20:53:28 +09002495 /* bring phy back */
Tejun Heo7a7921e2006-02-02 18:20:00 +09002496 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002497
Tejun Heoc2bd5802006-01-24 17:05:22 +09002498 /* TODO: phy layer with polling, timeouts, etc. */
2499 if (!sata_dev_present(ap)) {
2500 *class = ATA_DEV_NONE;
2501 DPRINTK("EXIT, link offline\n");
2502 return 0;
2503 }
2504
2505 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2506 if (verbose)
2507 printk(KERN_ERR "ata%u: COMRESET failed "
2508 "(device not ready)\n", ap->id);
2509 else
2510 DPRINTK("EXIT, device not ready\n");
2511 return -EIO;
2512 }
2513
Tejun Heo3a397462006-02-10 23:58:48 +09002514 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2515
Tejun Heoc2bd5802006-01-24 17:05:22 +09002516 *class = ata_dev_try_classify(ap, 0, NULL);
2517
2518 DPRINTK("EXIT, class=%u\n", *class);
2519 return 0;
2520}
2521
2522/**
2523 * ata_std_postreset - standard postreset callback
2524 * @ap: the target ata_port
2525 * @classes: classes of attached devices
2526 *
2527 * This function is invoked after a successful reset. Note that
2528 * the device might have been reset more than once using
2529 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002530 *
2531 * This function is to be used as standard callback for
2532 * ata_drive_*_reset().
2533 *
2534 * LOCKING:
2535 * Kernel thread context (may sleep)
2536 */
2537void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2538{
2539 DPRINTK("ENTER\n");
2540
Tejun Heo56497bd2006-02-15 15:01:42 +09002541 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002542 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2543 ap->cbl = ATA_CBL_SATA;
2544
2545 /* print link status */
2546 if (ap->cbl == ATA_CBL_SATA)
2547 sata_print_link_status(ap);
2548
Tejun Heo3a397462006-02-10 23:58:48 +09002549 /* re-enable interrupts */
2550 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2551 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002552
2553 /* is double-select really necessary? */
2554 if (classes[0] != ATA_DEV_NONE)
2555 ap->ops->dev_select(ap, 1);
2556 if (classes[1] != ATA_DEV_NONE)
2557 ap->ops->dev_select(ap, 0);
2558
Tejun Heo3a397462006-02-10 23:58:48 +09002559 /* bail out if no device is present */
2560 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2561 DPRINTK("EXIT, no device\n");
2562 return;
2563 }
2564
2565 /* set up device control */
2566 if (ap->ioaddr.ctl_addr) {
2567 if (ap->flags & ATA_FLAG_MMIO)
2568 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2569 else
2570 outb(ap->ctl, ap->ioaddr.ctl_addr);
2571 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002572
2573 DPRINTK("EXIT\n");
2574}
2575
2576/**
2577 * ata_std_probe_reset - standard probe reset method
2578 * @ap: prot to perform probe-reset
2579 * @classes: resulting classes of attached devices
2580 *
2581 * The stock off-the-shelf ->probe_reset method.
2582 *
2583 * LOCKING:
2584 * Kernel thread context (may sleep)
2585 *
2586 * RETURNS:
2587 * 0 on success, -errno otherwise.
2588 */
2589int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2590{
2591 ata_reset_fn_t hardreset;
2592
2593 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002594 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002595 hardreset = sata_std_hardreset;
2596
Tejun Heo8a19ac82006-02-02 18:20:00 +09002597 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002598 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002599 ata_std_postreset, classes);
2600}
2601
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09002602int ata_do_reset(struct ata_port *ap,
2603 ata_reset_fn_t reset, ata_postreset_fn_t postreset,
2604 int verbose, unsigned int *classes)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002605{
2606 int i, rc;
2607
2608 for (i = 0; i < ATA_MAX_DEVICES; i++)
2609 classes[i] = ATA_DEV_UNKNOWN;
2610
Tejun Heo9974e7c2006-04-01 01:38:17 +09002611 rc = reset(ap, verbose, classes);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002612 if (rc)
2613 return rc;
2614
2615 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2616 * is complete and convert all ATA_DEV_UNKNOWN to
2617 * ATA_DEV_NONE.
2618 */
2619 for (i = 0; i < ATA_MAX_DEVICES; i++)
2620 if (classes[i] != ATA_DEV_UNKNOWN)
2621 break;
2622
2623 if (i < ATA_MAX_DEVICES)
2624 for (i = 0; i < ATA_MAX_DEVICES; i++)
2625 if (classes[i] == ATA_DEV_UNKNOWN)
2626 classes[i] = ATA_DEV_NONE;
2627
2628 if (postreset)
2629 postreset(ap, classes);
2630
Tejun Heo9974e7c2006-04-01 01:38:17 +09002631 return 0;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002632}
2633
2634/**
2635 * ata_drive_probe_reset - Perform probe reset with given methods
2636 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002637 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002638 * @softreset: softreset method (can be NULL)
2639 * @hardreset: hardreset method (can be NULL)
2640 * @postreset: postreset method (can be NULL)
2641 * @classes: resulting classes of attached devices
2642 *
2643 * Reset the specified port and classify attached devices using
2644 * given methods. This function prefers softreset but tries all
2645 * possible reset sequences to reset and classify devices. This
2646 * function is intended to be used for constructing ->probe_reset
2647 * callback by low level drivers.
2648 *
2649 * Reset methods should follow the following rules.
2650 *
2651 * - Return 0 on sucess, -errno on failure.
2652 * - If classification is supported, fill classes[] with
2653 * recognized class codes.
2654 * - If classification is not supported, leave classes[] alone.
2655 * - If verbose is non-zero, print error message on failure;
2656 * otherwise, shut up.
2657 *
2658 * LOCKING:
2659 * Kernel thread context (may sleep)
2660 *
2661 * RETURNS:
2662 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2663 * if classification fails, and any error code from reset
2664 * methods.
2665 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002666int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002667 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2668 ata_postreset_fn_t postreset, unsigned int *classes)
2669{
2670 int rc = -EINVAL;
2671
Tejun Heo7944ea92006-02-02 18:20:00 +09002672 if (probeinit)
2673 probeinit(ap);
2674
Tejun Heo90dac022006-04-02 17:54:46 +09002675 if (softreset && !ata_set_sata_spd_needed(ap)) {
Tejun Heo9974e7c2006-04-01 01:38:17 +09002676 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
2677 if (rc == 0 && classes[0] != ATA_DEV_UNKNOWN)
2678 goto done;
Tejun Heoedbabd82006-04-02 20:55:02 +09002679 printk(KERN_INFO "ata%u: softreset failed, will try "
2680 "hardreset in 5 secs\n", ap->id);
2681 ssleep(5);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002682 }
2683
2684 if (!hardreset)
Tejun Heo9974e7c2006-04-01 01:38:17 +09002685 goto done;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002686
Tejun Heo90dac022006-04-02 17:54:46 +09002687 while (1) {
2688 rc = ata_do_reset(ap, hardreset, postreset, 0, classes);
2689 if (rc == 0) {
2690 if (classes[0] != ATA_DEV_UNKNOWN)
2691 goto done;
2692 break;
2693 }
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002694
Tejun Heo90dac022006-04-02 17:54:46 +09002695 if (ata_down_sata_spd_limit(ap))
2696 goto done;
Tejun Heoedbabd82006-04-02 20:55:02 +09002697
2698 printk(KERN_INFO "ata%u: hardreset failed, will retry "
2699 "in 5 secs\n", ap->id);
2700 ssleep(5);
Tejun Heo90dac022006-04-02 17:54:46 +09002701 }
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002702
Tejun Heoedbabd82006-04-02 20:55:02 +09002703 if (softreset) {
2704 printk(KERN_INFO "ata%u: hardreset succeeded without "
2705 "classification, will retry softreset in 5 secs\n",
2706 ap->id);
2707 ssleep(5);
2708
Tejun Heo9974e7c2006-04-01 01:38:17 +09002709 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
Tejun Heoedbabd82006-04-02 20:55:02 +09002710 }
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002711
Tejun Heo9974e7c2006-04-01 01:38:17 +09002712 done:
2713 if (rc == 0 && classes[0] == ATA_DEV_UNKNOWN)
2714 rc = -ENODEV;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002715 return rc;
2716}
2717
Tejun Heo623a3122006-03-05 17:55:58 +09002718/**
2719 * ata_dev_same_device - Determine whether new ID matches configured device
2720 * @ap: port on which the device to compare against resides
2721 * @dev: device to compare against
2722 * @new_class: class of the new device
2723 * @new_id: IDENTIFY page of the new device
2724 *
2725 * Compare @new_class and @new_id against @dev and determine
2726 * whether @dev is the device indicated by @new_class and
2727 * @new_id.
2728 *
2729 * LOCKING:
2730 * None.
2731 *
2732 * RETURNS:
2733 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2734 */
2735static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2736 unsigned int new_class, const u16 *new_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Tejun Heo623a3122006-03-05 17:55:58 +09002738 const u16 *old_id = dev->id;
2739 unsigned char model[2][41], serial[2][21];
2740 u64 new_n_sectors;
2741
2742 if (dev->class != new_class) {
2743 printk(KERN_INFO
2744 "ata%u: dev %u class mismatch %d != %d\n",
2745 ap->id, dev->devno, dev->class, new_class);
2746 return 0;
2747 }
2748
2749 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2750 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2751 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2752 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2753 new_n_sectors = ata_id_n_sectors(new_id);
2754
2755 if (strcmp(model[0], model[1])) {
2756 printk(KERN_INFO
2757 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2758 ap->id, dev->devno, model[0], model[1]);
2759 return 0;
2760 }
2761
2762 if (strcmp(serial[0], serial[1])) {
2763 printk(KERN_INFO
2764 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2765 ap->id, dev->devno, serial[0], serial[1]);
2766 return 0;
2767 }
2768
2769 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2770 printk(KERN_INFO
2771 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2772 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2773 (unsigned long long)new_n_sectors);
2774 return 0;
2775 }
2776
2777 return 1;
2778}
2779
2780/**
2781 * ata_dev_revalidate - Revalidate ATA device
2782 * @ap: port on which the device to revalidate resides
2783 * @dev: device to revalidate
2784 * @post_reset: is this revalidation after reset?
2785 *
2786 * Re-read IDENTIFY page and make sure @dev is still attached to
2787 * the port.
2788 *
2789 * LOCKING:
2790 * Kernel thread context (may sleep)
2791 *
2792 * RETURNS:
2793 * 0 on success, negative errno otherwise
2794 */
2795int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2796 int post_reset)
2797{
Tejun Heo5eb45c02006-04-02 18:51:52 +09002798 unsigned int class = dev->class;
2799 u16 *id = NULL;
Tejun Heo623a3122006-03-05 17:55:58 +09002800 int rc;
2801
Tejun Heo5eb45c02006-04-02 18:51:52 +09002802 if (!ata_dev_enabled(dev)) {
2803 rc = -ENODEV;
2804 goto fail;
2805 }
Tejun Heo623a3122006-03-05 17:55:58 +09002806
2807 /* allocate & read ID data */
2808 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2809 if (rc)
2810 goto fail;
2811
2812 /* is the device still there? */
2813 if (!ata_dev_same_device(ap, dev, class, id)) {
2814 rc = -ENODEV;
2815 goto fail;
2816 }
2817
2818 kfree(dev->id);
2819 dev->id = id;
2820
2821 /* configure device according to the new ID */
Tejun Heo5eb45c02006-04-02 18:51:52 +09002822 rc = ata_dev_configure(ap, dev, 0);
2823 if (rc == 0)
2824 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09002825
2826 fail:
2827 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2828 ap->id, dev->devno, rc);
2829 kfree(id);
2830 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002833static const char * const ata_dma_blacklist [] = {
Alan Coxf4b15fe2006-03-22 15:54:04 +00002834 "WDC AC11000H", NULL,
2835 "WDC AC22100H", NULL,
2836 "WDC AC32500H", NULL,
2837 "WDC AC33100H", NULL,
2838 "WDC AC31600H", NULL,
2839 "WDC AC32100H", "24.09P07",
2840 "WDC AC23200L", "21.10N21",
2841 "Compaq CRD-8241B", NULL,
2842 "CRD-8400B", NULL,
2843 "CRD-8480B", NULL,
2844 "CRD-8482B", NULL,
2845 "CRD-84", NULL,
2846 "SanDisk SDP3B", NULL,
2847 "SanDisk SDP3B-64", NULL,
2848 "SANYO CD-ROM CRD", NULL,
2849 "HITACHI CDR-8", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002850 "HITACHI CDR-8335", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002851 "HITACHI CDR-8435", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002852 "Toshiba CD-ROM XM-6202B", NULL,
2853 "TOSHIBA CD-ROM XM-1702BC", NULL,
2854 "CD-532E-A", NULL,
2855 "E-IDE CD-ROM CR-840", NULL,
2856 "CD-ROM Drive/F5A", NULL,
2857 "WPI CDD-820", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002858 "SAMSUNG CD-ROM SC-148C", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002859 "SAMSUNG CD-ROM SC", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002860 "SanDisk SDP3B-64", NULL,
2861 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2862 "_NEC DV5800A", NULL,
2863 "SAMSUNG CD-ROM SN-124", "N001"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002865
Alan Coxf4b15fe2006-03-22 15:54:04 +00002866static int ata_strim(char *s, size_t len)
2867{
2868 len = strnlen(s, len);
2869
2870 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2871 while ((len > 0) && (s[len - 1] == ' ')) {
2872 len--;
2873 s[len] = 0;
2874 }
2875 return len;
2876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Jeff Garzik057ace52005-10-22 14:27:05 -04002878static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Alan Coxf4b15fe2006-03-22 15:54:04 +00002880 unsigned char model_num[40];
2881 unsigned char model_rev[16];
2882 unsigned int nlen, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 int i;
2884
Alan Coxf4b15fe2006-03-22 15:54:04 +00002885 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2886 sizeof(model_num));
2887 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
2888 sizeof(model_rev));
2889 nlen = ata_strim(model_num, sizeof(model_num));
2890 rlen = ata_strim(model_rev, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Alan Coxf4b15fe2006-03-22 15:54:04 +00002892 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
2893 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
2894 if (ata_dma_blacklist[i+1] == NULL)
2895 return 1;
2896 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
2897 return 1;
2898 }
2899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return 0;
2901}
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09002904 * ata_dev_xfermask - Compute supported xfermask of the given device
2905 * @ap: Port on which the device to compute xfermask for resides
2906 * @dev: Device to compute xfermask for
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 *
Tejun Heoacf356b2006-03-24 14:07:50 +09002908 * Compute supported xfermask of @dev and store it in
2909 * dev->*_mask. This function is responsible for applying all
2910 * known limits including host controller limits, device
2911 * blacklist, etc...
Jeff Garzik0cba6322005-05-30 19:49:12 -04002912 *
Tejun Heo600511e2006-03-25 11:14:07 +09002913 * FIXME: The current implementation limits all transfer modes to
2914 * the fastest of the lowested device on the port. This is not
Tejun Heo05c8e0a2006-03-25 14:28:57 +09002915 * required on most controllers.
Tejun Heo600511e2006-03-25 11:14:07 +09002916 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 * LOCKING:
Tejun Heoa6d5a512006-03-06 04:31:57 +09002918 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 */
Tejun Heoacf356b2006-03-24 14:07:50 +09002920static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
Alan Cox5444a6f2006-03-27 18:58:20 +01002922 struct ata_host_set *hs = ap->host_set;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002923 unsigned long xfer_mask;
2924 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Tejun Heo565083e2006-04-02 17:54:47 +09002926 xfer_mask = ata_pack_xfermask(ap->pio_mask,
2927 ap->mwdma_mask, ap->udma_mask);
2928
2929 /* Apply cable rule here. Don't apply it early because when
2930 * we handle hot plug the cable type can itself change.
2931 */
2932 if (ap->cbl == ATA_CBL_PATA40)
2933 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Alan Cox5444a6f2006-03-27 18:58:20 +01002935 /* FIXME: Use port-wide xfermask for now */
Tejun Heoa6d5a512006-03-06 04:31:57 +09002936 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2937 struct ata_device *d = &ap->device[i];
Tejun Heo565083e2006-04-02 17:54:47 +09002938
2939 if (ata_dev_absent(d))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002940 continue;
Tejun Heo565083e2006-04-02 17:54:47 +09002941
2942 if (ata_dev_disabled(d)) {
2943 /* to avoid violating device selection timing */
2944 xfer_mask &= ata_pack_xfermask(d->pio_mask,
2945 UINT_MAX, UINT_MAX);
2946 continue;
2947 }
2948
2949 xfer_mask &= ata_pack_xfermask(d->pio_mask,
2950 d->mwdma_mask, d->udma_mask);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002951 xfer_mask &= ata_id_xfermask(d->id);
2952 if (ata_dma_blacklisted(d))
2953 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
2955
Tejun Heoa6d5a512006-03-06 04:31:57 +09002956 if (ata_dma_blacklisted(dev))
2957 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2958 "disabling DMA\n", ap->id, dev->devno);
2959
Alan Cox5444a6f2006-03-27 18:58:20 +01002960 if (hs->flags & ATA_HOST_SIMPLEX) {
2961 if (hs->simplex_claimed)
2962 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2963 }
Tejun Heo565083e2006-04-02 17:54:47 +09002964
Alan Cox5444a6f2006-03-27 18:58:20 +01002965 if (ap->ops->mode_filter)
2966 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
2967
Tejun Heo565083e2006-04-02 17:54:47 +09002968 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
2969 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
2972/**
2973 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2974 * @ap: Port associated with device @dev
2975 * @dev: Device to which command will be sent
2976 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002977 * Issue SET FEATURES - XFER MODE command to device @dev
2978 * on port @ap.
2979 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002981 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09002982 *
2983 * RETURNS:
2984 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 */
2986
Tejun Heo83206a22006-03-24 15:25:31 +09002987static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
2988 struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Tejun Heoa0123702005-12-13 14:49:31 +09002990 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09002991 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993 /* set up set-features taskfile */
2994 DPRINTK("set features - xfer mode\n");
2995
Tejun Heoa0123702005-12-13 14:49:31 +09002996 ata_tf_init(ap, &tf, dev->devno);
2997 tf.command = ATA_CMD_SET_FEATURES;
2998 tf.feature = SETFEATURES_XFER;
2999 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3000 tf.protocol = ATA_PROT_NODATA;
3001 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Tejun Heod69cf372006-04-02 18:51:53 +09003003 err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Tejun Heo83206a22006-03-24 15:25:31 +09003005 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3006 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008
3009/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04003010 * ata_dev_init_params - Issue INIT DEV PARAMS command
3011 * @ap: Port associated with device @dev
3012 * @dev: Device to which command will be sent
3013 *
3014 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09003015 * Kernel thread context (may sleep)
3016 *
3017 * RETURNS:
3018 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04003019 */
3020
Tejun Heo6aff8f12006-02-15 18:24:09 +09003021static unsigned int ata_dev_init_params(struct ata_port *ap,
Albert Lee00b6f5e2006-03-27 16:39:18 +08003022 struct ata_device *dev,
3023 u16 heads,
3024 u16 sectors)
Albert Lee8bf62ec2005-05-12 15:29:42 -04003025{
Tejun Heoa0123702005-12-13 14:49:31 +09003026 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09003027 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003028
3029 /* Number of sectors per track 1-255. Number of heads 1-16 */
3030 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08003031 return AC_ERR_INVALID;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003032
3033 /* set up init dev params taskfile */
3034 DPRINTK("init dev params \n");
3035
Tejun Heoa0123702005-12-13 14:49:31 +09003036 ata_tf_init(ap, &tf, dev->devno);
3037 tf.command = ATA_CMD_INIT_DEV_PARAMS;
3038 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3039 tf.protocol = ATA_PROT_NODATA;
3040 tf.nsect = sectors;
3041 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04003042
Tejun Heod69cf372006-04-02 18:51:53 +09003043 err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04003044
Tejun Heo6aff8f12006-02-15 18:24:09 +09003045 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3046 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003047}
3048
3049/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003050 * ata_sg_clean - Unmap DMA memory associated with command
3051 * @qc: Command containing DMA memory to be released
3052 *
3053 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 *
3055 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003056 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 */
3058
3059static void ata_sg_clean(struct ata_queued_cmd *qc)
3060{
3061 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003062 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003064 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Tejun Heoa4631472006-02-11 19:11:13 +09003066 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
3067 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05003070 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003072 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003074 /* if we padded the buffer out to 32-bit bound, and data
3075 * xfer direction is from-device, we must copy from the
3076 * pad buffer back into the supplied buffer
3077 */
3078 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
3079 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3080
3081 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05003082 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003083 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003084 /* restore last sg */
3085 sg[qc->orig_n_elem - 1].length += qc->pad_len;
3086 if (pad_buf) {
3087 struct scatterlist *psg = &qc->pad_sgent;
3088 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3089 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003090 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003091 }
3092 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09003093 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003094 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05003095 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
3096 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003097 /* restore sg */
3098 sg->length += qc->pad_len;
3099 if (pad_buf)
3100 memcpy(qc->buf_virt + sg->length - qc->pad_len,
3101 pad_buf, qc->pad_len);
3102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003105 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
3108/**
3109 * ata_fill_sg - Fill PCI IDE PRD table
3110 * @qc: Metadata associated with taskfile to be transferred
3111 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003112 * Fill PCI IDE PRD (scatter-gather) table with segments
3113 * associated with the current disk command.
3114 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04003116 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 *
3118 */
3119static void ata_fill_sg(struct ata_queued_cmd *qc)
3120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003122 struct scatterlist *sg;
3123 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Tejun Heoa4631472006-02-11 19:11:13 +09003125 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05003126 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003129 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 u32 addr, offset;
3131 u32 sg_len, len;
3132
3133 /* determine if physical DMA addr spans 64K boundary.
3134 * Note h/w doesn't support 64-bit, so we unconditionally
3135 * truncate dma_addr_t to u32.
3136 */
3137 addr = (u32) sg_dma_address(sg);
3138 sg_len = sg_dma_len(sg);
3139
3140 while (sg_len) {
3141 offset = addr & 0xffff;
3142 len = sg_len;
3143 if ((offset + sg_len) > 0x10000)
3144 len = 0x10000 - offset;
3145
3146 ap->prd[idx].addr = cpu_to_le32(addr);
3147 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
3148 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
3149
3150 idx++;
3151 sg_len -= len;
3152 addr += len;
3153 }
3154 }
3155
3156 if (idx)
3157 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3158}
3159/**
3160 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3161 * @qc: Metadata associated with taskfile to check
3162 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003163 * Allow low-level driver to filter ATA PACKET commands, returning
3164 * a status indicating whether or not it is OK to use DMA for the
3165 * supplied PACKET command.
3166 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003168 * spin_lock_irqsave(host_set lock)
3169 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 * RETURNS: 0 when ATAPI DMA can be used
3171 * nonzero otherwise
3172 */
3173int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3174{
3175 struct ata_port *ap = qc->ap;
3176 int rc = 0; /* Assume ATAPI DMA is OK by default */
3177
3178 if (ap->ops->check_atapi_dma)
3179 rc = ap->ops->check_atapi_dma(qc);
3180
Albert Leec2bbc552006-03-25 17:56:55 +08003181 /* We don't support polling DMA.
3182 * Use PIO if the LLDD handles only interrupts in
3183 * the HSM_ST_LAST state and the ATAPI device
3184 * generates CDB interrupts.
3185 */
3186 if ((ap->flags & ATA_FLAG_PIO_POLLING) &&
3187 (qc->dev->flags & ATA_DFLAG_CDB_INTR))
3188 rc = 1;
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return rc;
3191}
3192/**
3193 * ata_qc_prep - Prepare taskfile for submission
3194 * @qc: Metadata associated with taskfile to be prepared
3195 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003196 * Prepare ATA taskfile for submission.
3197 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 * LOCKING:
3199 * spin_lock_irqsave(host_set lock)
3200 */
3201void ata_qc_prep(struct ata_queued_cmd *qc)
3202{
3203 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3204 return;
3205
3206 ata_fill_sg(qc);
3207}
3208
Brian Kinge46834c2006-03-17 17:04:03 -06003209void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
3210
Jeff Garzik0cba6322005-05-30 19:49:12 -04003211/**
3212 * ata_sg_init_one - Associate command with memory buffer
3213 * @qc: Command to be associated
3214 * @buf: Memory buffer
3215 * @buflen: Length of memory buffer, in bytes.
3216 *
3217 * Initialize the data-related elements of queued_cmd @qc
3218 * to point to a single memory buffer, @buf of byte length @buflen.
3219 *
3220 * LOCKING:
3221 * spin_lock_irqsave(host_set lock)
3222 */
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3225{
3226 struct scatterlist *sg;
3227
3228 qc->flags |= ATA_QCFLAG_SINGLE;
3229
3230 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003231 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003233 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 qc->buf_virt = buf;
3235
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003236 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05003237 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
Jeff Garzik0cba6322005-05-30 19:49:12 -04003240/**
3241 * ata_sg_init - Associate command with scatter-gather table.
3242 * @qc: Command to be associated
3243 * @sg: Scatter-gather table.
3244 * @n_elem: Number of elements in s/g table.
3245 *
3246 * Initialize the data-related elements of queued_cmd @qc
3247 * to point to a scatter-gather table @sg, containing @n_elem
3248 * elements.
3249 *
3250 * LOCKING:
3251 * spin_lock_irqsave(host_set lock)
3252 */
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3255 unsigned int n_elem)
3256{
3257 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003258 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003260 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
3263/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003264 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3265 * @qc: Command with memory buffer to be mapped.
3266 *
3267 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 *
3269 * LOCKING:
3270 * spin_lock_irqsave(host_set lock)
3271 *
3272 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003273 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 */
3275
3276static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3277{
3278 struct ata_port *ap = qc->ap;
3279 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003280 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003282 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003284 /* we must lengthen transfers to end on a 32-bit boundary */
3285 qc->pad_len = sg->length & 3;
3286 if (qc->pad_len) {
3287 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3288 struct scatterlist *psg = &qc->pad_sgent;
3289
Tejun Heoa4631472006-02-11 19:11:13 +09003290 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003291
3292 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3293
3294 if (qc->tf.flags & ATA_TFLAG_WRITE)
3295 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3296 qc->pad_len);
3297
3298 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3299 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3300 /* trim sg */
3301 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003302 if (sg->length == 0)
3303 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003304
3305 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3306 sg->length, qc->pad_len);
3307 }
3308
Tejun Heo2e242fa2006-02-20 23:48:38 +09003309 if (trim_sg) {
3310 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003311 goto skip_map;
3312 }
3313
Brian King2f1f6102006-03-23 17:30:15 -06003314 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003315 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003316 if (dma_mapping_error(dma_address)) {
3317 /* restore sg */
3318 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003323 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Tejun Heo2e242fa2006-02-20 23:48:38 +09003325skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3327 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3328
3329 return 0;
3330}
3331
3332/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003333 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3334 * @qc: Command with scatter-gather table to be mapped.
3335 *
3336 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 *
3338 * LOCKING:
3339 * spin_lock_irqsave(host_set lock)
3340 *
3341 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003342 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 *
3344 */
3345
3346static int ata_sg_setup(struct ata_queued_cmd *qc)
3347{
3348 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003349 struct scatterlist *sg = qc->__sg;
3350 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003351 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
3353 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003354 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003356 /* we must lengthen transfers to end on a 32-bit boundary */
3357 qc->pad_len = lsg->length & 3;
3358 if (qc->pad_len) {
3359 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3360 struct scatterlist *psg = &qc->pad_sgent;
3361 unsigned int offset;
3362
Tejun Heoa4631472006-02-11 19:11:13 +09003363 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003364
3365 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3366
3367 /*
3368 * psg->page/offset are used to copy to-be-written
3369 * data in this function or read data in ata_sg_clean.
3370 */
3371 offset = lsg->offset + lsg->length - qc->pad_len;
3372 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3373 psg->offset = offset_in_page(offset);
3374
3375 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3376 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3377 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003378 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003379 }
3380
3381 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3382 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3383 /* trim last sg */
3384 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003385 if (lsg->length == 0)
3386 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003387
3388 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3389 qc->n_elem - 1, lsg->length, qc->pad_len);
3390 }
3391
Jeff Garzike1410f22005-11-14 14:06:26 -05003392 pre_n_elem = qc->n_elem;
3393 if (trim_sg && pre_n_elem)
3394 pre_n_elem--;
3395
3396 if (!pre_n_elem) {
3397 n_elem = 0;
3398 goto skip_map;
3399 }
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06003402 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003403 if (n_elem < 1) {
3404 /* restore last sg */
3405 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 DPRINTK("%d sg elements mapped\n", n_elem);
3410
Jeff Garzike1410f22005-11-14 14:06:26 -05003411skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 qc->n_elem = n_elem;
3413
3414 return 0;
3415}
3416
3417/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003418 * ata_poll_qc_complete - turn irq back on and finish qc
3419 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003420 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003421 *
3422 * LOCKING:
3423 * None. (grabs host lock)
3424 */
3425
Albert Leea22e2eb2005-12-05 15:38:02 +08003426void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003427{
3428 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003429 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003430
Jeff Garzikb8f61532005-08-25 22:01:20 -04003431 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003432 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003433 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003434 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003435}
3436
3437/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003438 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003439 * @buf: Buffer to swap
3440 * @buf_words: Number of 16-bit words in buffer.
3441 *
3442 * Swap halves of 16-bit words if needed to convert from
3443 * little-endian byte order to native cpu byte order, or
3444 * vice-versa.
3445 *
3446 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003447 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449void swap_buf_le16(u16 *buf, unsigned int buf_words)
3450{
3451#ifdef __BIG_ENDIAN
3452 unsigned int i;
3453
3454 for (i = 0; i < buf_words; i++)
3455 buf[i] = le16_to_cpu(buf[i]);
3456#endif /* __BIG_ENDIAN */
3457}
3458
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003459/**
3460 * ata_mmio_data_xfer - Transfer data by MMIO
3461 * @ap: port to read/write
3462 * @buf: data buffer
3463 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003464 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003465 *
3466 * Transfer data from/to the device data register by MMIO.
3467 *
3468 * LOCKING:
3469 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003470 */
3471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3473 unsigned int buflen, int write_data)
3474{
3475 unsigned int i;
3476 unsigned int words = buflen >> 1;
3477 u16 *buf16 = (u16 *) buf;
3478 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3479
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003480 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 if (write_data) {
3482 for (i = 0; i < words; i++)
3483 writew(le16_to_cpu(buf16[i]), mmio);
3484 } else {
3485 for (i = 0; i < words; i++)
3486 buf16[i] = cpu_to_le16(readw(mmio));
3487 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003488
3489 /* Transfer trailing 1 byte, if any. */
3490 if (unlikely(buflen & 0x01)) {
3491 u16 align_buf[1] = { 0 };
3492 unsigned char *trailing_buf = buf + buflen - 1;
3493
3494 if (write_data) {
3495 memcpy(align_buf, trailing_buf, 1);
3496 writew(le16_to_cpu(align_buf[0]), mmio);
3497 } else {
3498 align_buf[0] = cpu_to_le16(readw(mmio));
3499 memcpy(trailing_buf, align_buf, 1);
3500 }
3501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003504/**
3505 * ata_pio_data_xfer - Transfer data by PIO
3506 * @ap: port to read/write
3507 * @buf: data buffer
3508 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003509 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003510 *
3511 * Transfer data from/to the device data register by PIO.
3512 *
3513 * LOCKING:
3514 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003515 */
3516
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3518 unsigned int buflen, int write_data)
3519{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003520 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003522 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003524 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003526 insw(ap->ioaddr.data_addr, buf, words);
3527
3528 /* Transfer trailing 1 byte, if any. */
3529 if (unlikely(buflen & 0x01)) {
3530 u16 align_buf[1] = { 0 };
3531 unsigned char *trailing_buf = buf + buflen - 1;
3532
3533 if (write_data) {
3534 memcpy(align_buf, trailing_buf, 1);
3535 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3536 } else {
3537 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3538 memcpy(trailing_buf, align_buf, 1);
3539 }
3540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541}
3542
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003543/**
3544 * ata_data_xfer - Transfer data from/to the data register.
3545 * @ap: port to read/write
3546 * @buf: data buffer
3547 * @buflen: buffer length
3548 * @do_write: read/write
3549 *
3550 * Transfer data from/to the device data register.
3551 *
3552 * LOCKING:
3553 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003554 */
3555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3557 unsigned int buflen, int do_write)
3558{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003559 /* Make the crap hardware pay the costs not the good stuff */
3560 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3561 unsigned long flags;
3562 local_irq_save(flags);
3563 if (ap->flags & ATA_FLAG_MMIO)
3564 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3565 else
3566 ata_pio_data_xfer(ap, buf, buflen, do_write);
3567 local_irq_restore(flags);
3568 } else {
3569 if (ap->flags & ATA_FLAG_MMIO)
3570 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3571 else
3572 ata_pio_data_xfer(ap, buf, buflen, do_write);
3573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003576/**
3577 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3578 * @qc: Command on going
3579 *
3580 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3581 *
3582 * LOCKING:
3583 * Inherited from caller.
3584 */
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586static void ata_pio_sector(struct ata_queued_cmd *qc)
3587{
3588 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003589 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 struct ata_port *ap = qc->ap;
3591 struct page *page;
3592 unsigned int offset;
3593 unsigned char *buf;
3594
3595 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003596 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
3598 page = sg[qc->cursg].page;
3599 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3600
3601 /* get the current page and offset */
3602 page = nth_page(page, (offset >> PAGE_SHIFT));
3603 offset %= PAGE_SIZE;
3604
Albert Lee7282aa42005-10-09 09:46:07 -04003605 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3606
Albert Lee91b8b312005-10-09 09:48:44 -04003607 if (PageHighMem(page)) {
3608 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003609
Albert Lee91b8b312005-10-09 09:48:44 -04003610 local_irq_save(flags);
3611 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003612
Albert Lee91b8b312005-10-09 09:48:44 -04003613 /* do the actual data transfer */
3614 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3615
3616 kunmap_atomic(buf, KM_IRQ0);
3617 local_irq_restore(flags);
3618 } else {
3619 buf = page_address(page);
3620 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3621 }
Albert Lee7282aa42005-10-09 09:46:07 -04003622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 qc->cursect++;
3624 qc->cursg_ofs++;
3625
Albert Lee32529e02005-05-26 03:49:42 -04003626 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 qc->cursg++;
3628 qc->cursg_ofs = 0;
3629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003632/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003633 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3634 * @qc: Command on going
3635 *
3636 * Transfer one or many ATA_SECT_SIZE of data from/to the
3637 * ATA device for the DRQ request.
3638 *
3639 * LOCKING:
3640 * Inherited from caller.
3641 */
3642
3643static void ata_pio_sectors(struct ata_queued_cmd *qc)
3644{
3645 if (is_multi_taskfile(&qc->tf)) {
3646 /* READ/WRITE MULTIPLE */
3647 unsigned int nsect;
3648
Jeff Garzik587005d2006-02-11 18:17:32 -05003649 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08003650
3651 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3652 while (nsect--)
3653 ata_pio_sector(qc);
3654 } else
3655 ata_pio_sector(qc);
3656}
3657
3658/**
Albert Leec71c1852005-10-04 06:03:45 -04003659 * atapi_send_cdb - Write CDB bytes to hardware
3660 * @ap: Port to which ATAPI device is attached.
3661 * @qc: Taskfile currently active
3662 *
3663 * When device has indicated its readiness to accept
3664 * a CDB, this function is called. Send the CDB.
3665 *
3666 * LOCKING:
3667 * caller.
3668 */
3669
3670static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3671{
3672 /* send SCSI cdb */
3673 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05003674 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04003675
Jeff Garzikdb024d52006-02-13 00:23:57 -05003676 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04003677 ata_altstatus(ap); /* flush */
3678
3679 switch (qc->tf.protocol) {
3680 case ATA_PROT_ATAPI:
3681 ap->hsm_task_state = HSM_ST;
3682 break;
3683 case ATA_PROT_ATAPI_NODATA:
3684 ap->hsm_task_state = HSM_ST_LAST;
3685 break;
3686 case ATA_PROT_ATAPI_DMA:
3687 ap->hsm_task_state = HSM_ST_LAST;
3688 /* initiate bmdma */
3689 ap->ops->bmdma_start(qc);
3690 break;
3691 }
3692}
3693
3694/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003695 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3696 * @qc: Command on going
3697 * @bytes: number of bytes
3698 *
3699 * Transfer Transfer data from/to the ATAPI device.
3700 *
3701 * LOCKING:
3702 * Inherited from caller.
3703 *
3704 */
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3707{
3708 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003709 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 struct ata_port *ap = qc->ap;
3711 struct page *page;
3712 unsigned char *buf;
3713 unsigned int offset, count;
3714
Albert Lee563a6e12005-08-12 14:17:50 +08003715 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003716 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
3718next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003719 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003720 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003721 * The end of qc->sg is reached and the device expects
3722 * more data to transfer. In order not to overrun qc->sg
3723 * and fulfill length specified in the byte count register,
3724 * - for read case, discard trailing data from the device
3725 * - for write case, padding zero data to the device
3726 */
3727 u16 pad_buf[1] = { 0 };
3728 unsigned int words = bytes >> 1;
3729 unsigned int i;
3730
3731 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003732 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003733 ap->id, bytes);
3734
3735 for (i = 0; i < words; i++)
3736 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3737
Albert Lee14be71f2005-09-27 17:36:35 +08003738 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003739 return;
3740 }
3741
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003742 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 page = sg->page;
3745 offset = sg->offset + qc->cursg_ofs;
3746
3747 /* get the current page and offset */
3748 page = nth_page(page, (offset >> PAGE_SHIFT));
3749 offset %= PAGE_SIZE;
3750
Albert Lee6952df02005-06-06 15:56:03 +08003751 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003752 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
3754 /* don't cross page boundaries */
3755 count = min(count, (unsigned int)PAGE_SIZE - offset);
3756
Albert Lee7282aa42005-10-09 09:46:07 -04003757 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3758
Albert Lee91b8b312005-10-09 09:48:44 -04003759 if (PageHighMem(page)) {
3760 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003761
Albert Lee91b8b312005-10-09 09:48:44 -04003762 local_irq_save(flags);
3763 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003764
Albert Lee91b8b312005-10-09 09:48:44 -04003765 /* do the actual data transfer */
3766 ata_data_xfer(ap, buf + offset, count, do_write);
3767
3768 kunmap_atomic(buf, KM_IRQ0);
3769 local_irq_restore(flags);
3770 } else {
3771 buf = page_address(page);
3772 ata_data_xfer(ap, buf + offset, count, do_write);
3773 }
Albert Lee7282aa42005-10-09 09:46:07 -04003774
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 bytes -= count;
3776 qc->curbytes += count;
3777 qc->cursg_ofs += count;
3778
Albert Lee32529e02005-05-26 03:49:42 -04003779 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 qc->cursg++;
3781 qc->cursg_ofs = 0;
3782 }
3783
Albert Lee563a6e12005-08-12 14:17:50 +08003784 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003788/**
3789 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3790 * @qc: Command on going
3791 *
3792 * Transfer Transfer data from/to the ATAPI device.
3793 *
3794 * LOCKING:
3795 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003796 */
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3799{
3800 struct ata_port *ap = qc->ap;
3801 struct ata_device *dev = qc->dev;
3802 unsigned int ireason, bc_lo, bc_hi, bytes;
3803 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3804
3805 ap->ops->tf_read(ap, &qc->tf);
3806 ireason = qc->tf.nsect;
3807 bc_lo = qc->tf.lbam;
3808 bc_hi = qc->tf.lbah;
3809 bytes = (bc_hi << 8) | bc_lo;
3810
3811 /* shall be cleared to zero, indicating xfer of data */
3812 if (ireason & (1 << 0))
3813 goto err_out;
3814
3815 /* make sure transfer direction matches expected */
3816 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3817 if (do_write != i_write)
3818 goto err_out;
3819
Albert Lee312f7da2005-09-27 17:38:03 +08003820 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3821
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 __atapi_pio_bytes(qc, bytes);
3823
3824 return;
3825
3826err_out:
3827 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3828 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003829 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003830 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
3833/**
Albert Leec234fb02006-03-25 17:58:38 +08003834 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
3835 * @ap: the target ata_port
3836 * @qc: qc on going
3837 *
3838 * RETURNS:
3839 * 1 if ok in workqueue, 0 otherwise.
3840 */
3841
3842static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
3843{
3844 if (qc->tf.flags & ATA_TFLAG_POLLING)
3845 return 1;
3846
3847 if (ap->hsm_task_state == HSM_ST_FIRST) {
3848 if (qc->tf.protocol == ATA_PROT_PIO &&
3849 (qc->tf.flags & ATA_TFLAG_WRITE))
3850 return 1;
3851
3852 if (is_atapi_taskfile(&qc->tf) &&
3853 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3854 return 1;
3855 }
3856
3857 return 0;
3858}
3859
3860/**
Albert Leebb5cb292006-03-25 17:48:02 +08003861 * ata_hsm_move - move the HSM to the next state.
3862 * @ap: the target ata_port
3863 * @qc: qc on going
3864 * @status: current device status
3865 * @in_wq: 1 if called from workqueue, 0 otherwise
3866 *
3867 * RETURNS:
3868 * 1 when poll next status needed, 0 otherwise.
3869 */
3870
3871static int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
3872 u8 status, int in_wq)
Albert Leee2cec772006-03-25 17:43:49 +08003873{
Albert Leebb5cb292006-03-25 17:48:02 +08003874 unsigned long flags = 0;
3875 int poll_next;
3876
Albert Lee6912ccd2006-03-25 17:45:49 +08003877 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
3878
Albert Leebb5cb292006-03-25 17:48:02 +08003879 /* Make sure ata_qc_issue_prot() does not throw things
3880 * like DMA polling into the workqueue. Notice that
3881 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
3882 */
Albert Leec234fb02006-03-25 17:58:38 +08003883 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Leebb5cb292006-03-25 17:48:02 +08003884
Albert Leee2cec772006-03-25 17:43:49 +08003885fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08003886 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
3887 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
3888
Albert Leee2cec772006-03-25 17:43:49 +08003889 switch (ap->hsm_task_state) {
3890 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08003891 /* Send first data block or PACKET CDB */
3892
3893 /* If polling, we will stay in the work queue after
3894 * sending the data. Otherwise, interrupt handler
3895 * takes over after sending the data.
3896 */
3897 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3898
Albert Leee2cec772006-03-25 17:43:49 +08003899 /* check device status */
3900 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
3901 /* Wrong status. Let EH handle this */
3902 qc->err_mask |= AC_ERR_HSM;
3903 ap->hsm_task_state = HSM_ST_ERR;
3904 goto fsm_start;
3905 }
3906
Albert Lee71601952006-03-25 18:11:12 +08003907 /* Device should not ask for data transfer (DRQ=1)
3908 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08003909 * We ignore DRQ here and stop the HSM by
3910 * changing hsm_task_state to HSM_ST_ERR and
3911 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08003912 */
3913 if (unlikely(status & (ATA_ERR | ATA_DF))) {
3914 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
3915 ap->id, status);
3916 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08003917 ap->hsm_task_state = HSM_ST_ERR;
3918 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08003919 }
3920
Albert Leebb5cb292006-03-25 17:48:02 +08003921 /* Send the CDB (atapi) or the first data block (ata pio out).
3922 * During the state transition, interrupt handler shouldn't
3923 * be invoked before the data transfer is complete and
3924 * hsm_task_state is changed. Hence, the following locking.
3925 */
3926 if (in_wq)
3927 spin_lock_irqsave(&ap->host_set->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08003928
Albert Leebb5cb292006-03-25 17:48:02 +08003929 if (qc->tf.protocol == ATA_PROT_PIO) {
3930 /* PIO data out protocol.
3931 * send first data block.
3932 */
3933
3934 /* ata_pio_sectors() might change the state
3935 * to HSM_ST_LAST. so, the state is changed here
3936 * before ata_pio_sectors().
3937 */
3938 ap->hsm_task_state = HSM_ST;
3939 ata_pio_sectors(qc);
3940 ata_altstatus(ap); /* flush */
3941 } else
3942 /* send CDB */
3943 atapi_send_cdb(ap, qc);
3944
3945 if (in_wq)
3946 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3947
3948 /* if polling, ata_pio_task() handles the rest.
3949 * otherwise, interrupt handler takes over from here.
3950 */
Albert Leee2cec772006-03-25 17:43:49 +08003951 break;
3952
3953 case HSM_ST:
3954 /* complete command or read/write the data register */
3955 if (qc->tf.protocol == ATA_PROT_ATAPI) {
3956 /* ATAPI PIO protocol */
3957 if ((status & ATA_DRQ) == 0) {
3958 /* no more data to transfer */
3959 ap->hsm_task_state = HSM_ST_LAST;
3960 goto fsm_start;
3961 }
3962
Albert Lee71601952006-03-25 18:11:12 +08003963 /* Device should not ask for data transfer (DRQ=1)
3964 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08003965 * We ignore DRQ here and stop the HSM by
3966 * changing hsm_task_state to HSM_ST_ERR and
3967 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08003968 */
3969 if (unlikely(status & (ATA_ERR | ATA_DF))) {
3970 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
3971 ap->id, status);
3972 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08003973 ap->hsm_task_state = HSM_ST_ERR;
3974 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08003975 }
3976
Albert Leee2cec772006-03-25 17:43:49 +08003977 atapi_pio_bytes(qc);
3978
3979 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
3980 /* bad ireason reported by device */
3981 goto fsm_start;
3982
3983 } else {
3984 /* ATA PIO protocol */
3985 if (unlikely((status & ATA_DRQ) == 0)) {
3986 /* handle BSY=0, DRQ=0 as error */
3987 qc->err_mask |= AC_ERR_HSM;
3988 ap->hsm_task_state = HSM_ST_ERR;
3989 goto fsm_start;
3990 }
3991
Albert Leeeee6c322006-04-01 17:38:43 +08003992 /* For PIO reads, some devices may ask for
3993 * data transfer (DRQ=1) alone with ERR=1.
3994 * We respect DRQ here and transfer one
3995 * block of junk data before changing the
3996 * hsm_task_state to HSM_ST_ERR.
3997 *
3998 * For PIO writes, ERR=1 DRQ=1 doesn't make
3999 * sense since the data block has been
4000 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08004001 */
4002 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08004003 /* data might be corrputed */
4004 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08004005
4006 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
4007 ata_pio_sectors(qc);
4008 ata_altstatus(ap);
4009 status = ata_wait_idle(ap);
4010 }
4011
4012 /* ata_pio_sectors() might change the
4013 * state to HSM_ST_LAST. so, the state
4014 * is changed after ata_pio_sectors().
4015 */
4016 ap->hsm_task_state = HSM_ST_ERR;
4017 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004018 }
4019
Albert Leee2cec772006-03-25 17:43:49 +08004020 ata_pio_sectors(qc);
4021
4022 if (ap->hsm_task_state == HSM_ST_LAST &&
4023 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4024 /* all data read */
4025 ata_altstatus(ap);
Albert Lee52a32202006-03-25 18:18:15 +08004026 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08004027 goto fsm_start;
4028 }
4029 }
4030
4031 ata_altstatus(ap); /* flush */
Albert Leebb5cb292006-03-25 17:48:02 +08004032 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08004033 break;
4034
4035 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004036 if (unlikely(!ata_ok(status))) {
4037 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08004038 ap->hsm_task_state = HSM_ST_ERR;
4039 goto fsm_start;
4040 }
4041
4042 /* no more data to transfer */
4043 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4044 ap->id, status);
4045
Albert Lee6912ccd2006-03-25 17:45:49 +08004046 WARN_ON(qc->err_mask);
4047
Albert Leee2cec772006-03-25 17:43:49 +08004048 ap->hsm_task_state = HSM_ST_IDLE;
4049
4050 /* complete taskfile transaction */
Albert Leebb5cb292006-03-25 17:48:02 +08004051 if (in_wq)
4052 ata_poll_qc_complete(qc);
4053 else
4054 ata_qc_complete(qc);
4055
4056 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08004057 break;
4058
4059 case HSM_ST_ERR:
4060 if (qc->tf.command != ATA_CMD_PACKET)
Albert Lee6912ccd2006-03-25 17:45:49 +08004061 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x\n",
4062 ap->id, status);
Albert Leee2cec772006-03-25 17:43:49 +08004063
4064 /* make sure qc->err_mask is available to
4065 * know what's wrong and recover
4066 */
4067 WARN_ON(qc->err_mask == 0);
4068
4069 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08004070
Albert Lee999bb6f2006-03-25 18:07:48 +08004071 /* complete taskfile transaction */
Albert Leebb5cb292006-03-25 17:48:02 +08004072 if (in_wq)
4073 ata_poll_qc_complete(qc);
4074 else
4075 ata_qc_complete(qc);
4076
4077 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08004078 break;
4079 default:
Albert Leebb5cb292006-03-25 17:48:02 +08004080 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08004081 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08004082 }
4083
Albert Leebb5cb292006-03-25 17:48:02 +08004084 return poll_next;
Albert Leee2cec772006-03-25 17:43:49 +08004085}
4086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087static void ata_pio_task(void *_data)
4088{
Tejun Heoc91af2c2006-04-02 18:51:53 +09004089 struct ata_queued_cmd *qc = _data;
4090 struct ata_port *ap = qc->ap;
Albert Leea1af3732006-03-25 17:50:15 +08004091 u8 status;
4092 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004093
4094fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08004095 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Albert Leea1af3732006-03-25 17:50:15 +08004097 qc = ata_qc_from_tag(ap, ap->active_tag);
4098 WARN_ON(qc == NULL);
Albert Leee27486d2005-11-01 19:24:49 +08004099
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) {
4112 ata_port_queue_task(ap, ata_pio_task, ap, ATA_SHORT_PAUSE);
4113 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/**
4128 * ata_qc_timeout - Handle timeout of queued command
4129 * @qc: Command that timed out
4130 *
4131 * Some part of the kernel (currently, only the SCSI layer)
4132 * has noticed that the active command on port @ap has not
4133 * completed after a specified length of time. Handle this
4134 * condition by disabling DMA (if necessary) and completing
4135 * transactions, with error if necessary.
4136 *
4137 * This also handles the case of the "lost interrupt", where
4138 * for some reason (possibly hardware bug, possibly driver bug)
4139 * an interrupt was not delivered to the driver, even though the
4140 * transaction completed successfully.
4141 *
4142 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004143 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 */
4145
4146static void ata_qc_timeout(struct ata_queued_cmd *qc)
4147{
4148 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04004149 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04004151 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
4153 DPRINTK("ENTER\n");
4154
Tejun Heoc18d06f2006-02-02 00:56:10 +09004155 ap->hsm_task_state = HSM_ST_IDLE;
4156
Jeff Garzikb8f61532005-08-25 22:01:20 -04004157 spin_lock_irqsave(&host_set->lock, flags);
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 switch (qc->tf.protocol) {
4160
4161 case ATA_PROT_DMA:
4162 case ATA_PROT_ATAPI_DMA:
4163 host_stat = ap->ops->bmdma_status(ap);
4164
4165 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004166 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
4168 /* fall through */
4169
4170 default:
4171 ata_altstatus(ap);
4172 drv_stat = ata_chk_status(ap);
4173
4174 /* ack bmdma irq events */
4175 ap->ops->irq_clear(ap);
4176
4177 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
4178 ap->id, qc->tf.command, drv_stat, host_stat);
4179
Albert Lee312f7da2005-09-27 17:38:03 +08004180 ap->hsm_task_state = HSM_ST_IDLE;
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 /* complete taskfile transaction */
Albert Lee555a8962006-02-08 16:50:29 +08004183 qc->err_mask |= AC_ERR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 break;
4185 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04004186
4187 spin_unlock_irqrestore(&host_set->lock, flags);
4188
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004189 ata_eh_qc_complete(qc);
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 DPRINTK("EXIT\n");
4192}
4193
4194/**
4195 * ata_eng_timeout - Handle timeout of queued command
4196 * @ap: Port on which timed-out command is active
4197 *
4198 * Some part of the kernel (currently, only the SCSI layer)
4199 * has noticed that the active command on port @ap has not
4200 * completed after a specified length of time. Handle this
4201 * condition by disabling DMA (if necessary) and completing
4202 * transactions, with error if necessary.
4203 *
4204 * This also handles the case of the "lost interrupt", where
4205 * for some reason (possibly hardware bug, possibly driver bug)
4206 * an interrupt was not delivered to the driver, even though the
4207 * transaction completed successfully.
4208 *
4209 * LOCKING:
4210 * Inherited from SCSI layer (none, can sleep)
4211 */
4212
4213void ata_eng_timeout(struct ata_port *ap)
4214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 DPRINTK("ENTER\n");
4216
Tejun Heof6379022006-02-10 15:10:48 +09004217 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 DPRINTK("EXIT\n");
4220}
4221
4222/**
4223 * ata_qc_new - Request an available ATA command, for queueing
4224 * @ap: Port associated with device @dev
4225 * @dev: Device from whom we request an available command structure
4226 *
4227 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004228 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 */
4230
4231static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4232{
4233 struct ata_queued_cmd *qc = NULL;
4234 unsigned int i;
4235
4236 for (i = 0; i < ATA_MAX_QUEUE; i++)
4237 if (!test_and_set_bit(i, &ap->qactive)) {
4238 qc = ata_qc_from_tag(ap, i);
4239 break;
4240 }
4241
4242 if (qc)
4243 qc->tag = i;
4244
4245 return qc;
4246}
4247
4248/**
4249 * ata_qc_new_init - Request an available ATA command, and initialize it
4250 * @ap: Port associated with device @dev
4251 * @dev: Device from whom we request an available command structure
4252 *
4253 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004254 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 */
4256
4257struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
4258 struct ata_device *dev)
4259{
4260 struct ata_queued_cmd *qc;
4261
4262 qc = ata_qc_new(ap);
4263 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 qc->scsicmd = NULL;
4265 qc->ap = ap;
4266 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004268 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 }
4270
4271 return qc;
4272}
4273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274/**
4275 * ata_qc_free - free unused ata_queued_cmd
4276 * @qc: Command to complete
4277 *
4278 * Designed to free unused ata_queued_cmd object
4279 * in case something prevents using it.
4280 *
4281 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004282 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 */
4284void ata_qc_free(struct ata_queued_cmd *qc)
4285{
Tejun Heo4ba946e2006-01-23 13:09:36 +09004286 struct ata_port *ap = qc->ap;
4287 unsigned int tag;
4288
Tejun Heoa4631472006-02-11 19:11:13 +09004289 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Tejun Heo4ba946e2006-01-23 13:09:36 +09004291 qc->flags = 0;
4292 tag = qc->tag;
4293 if (likely(ata_tag_valid(tag))) {
4294 if (tag == ap->active_tag)
4295 ap->active_tag = ATA_TAG_POISON;
4296 qc->tag = ATA_TAG_POISON;
4297 clear_bit(tag, &ap->qactive);
4298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300
Tejun Heo76014422006-02-11 15:13:49 +09004301void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
Tejun Heoa4631472006-02-11 19:11:13 +09004303 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4304 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
4306 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4307 ata_sg_clean(qc);
4308
Albert Lee3f3791d2005-08-16 14:25:38 +08004309 /* atapi: mark qc as inactive to prevent the interrupt handler
4310 * from completing the command twice later, before the error handler
4311 * is called. (when rc != 0 and atapi request sense is needed)
4312 */
4313 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004316 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317}
4318
4319static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4320{
4321 struct ata_port *ap = qc->ap;
4322
4323 switch (qc->tf.protocol) {
4324 case ATA_PROT_DMA:
4325 case ATA_PROT_ATAPI_DMA:
4326 return 1;
4327
4328 case ATA_PROT_ATAPI:
4329 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 if (ap->flags & ATA_FLAG_PIO_DMA)
4331 return 1;
4332
4333 /* fall through */
4334
4335 default:
4336 return 0;
4337 }
4338
4339 /* never reached */
4340}
4341
4342/**
4343 * ata_qc_issue - issue taskfile to device
4344 * @qc: command to issue to device
4345 *
4346 * Prepare an ATA command to submission to device.
4347 * This includes mapping the data into a DMA-able
4348 * area, filling in the S/G table, and finally
4349 * writing the taskfile to hardware, starting the command.
4350 *
4351 * LOCKING:
4352 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004354void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
4356 struct ata_port *ap = qc->ap;
4357
Tejun Heoe4a70e72006-03-31 20:36:47 +09004358 qc->ap->active_tag = qc->tag;
4359 qc->flags |= ATA_QCFLAG_ACTIVE;
4360
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 if (ata_should_dma_map(qc)) {
4362 if (qc->flags & ATA_QCFLAG_SG) {
4363 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004364 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4366 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004367 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 }
4369 } else {
4370 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4371 }
4372
4373 ap->ops->qc_prep(qc);
4374
Tejun Heo8e0e6942006-03-31 20:41:11 +09004375 qc->err_mask |= ap->ops->qc_issue(qc);
4376 if (unlikely(qc->err_mask))
4377 goto err;
4378 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Tejun Heo8e436af2006-01-23 13:09:36 +09004380sg_err:
4381 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09004382 qc->err_mask |= AC_ERR_SYSTEM;
4383err:
4384 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385}
4386
4387/**
4388 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4389 * @qc: command to issue to device
4390 *
4391 * Using various libata functions and hooks, this function
4392 * starts an ATA command. ATA commands are grouped into
4393 * classes called "protocols", and issuing each type of protocol
4394 * is slightly different.
4395 *
Edward Falk0baab862005-06-02 18:17:13 -04004396 * May be used as the qc_issue() entry in ata_port_operations.
4397 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 * LOCKING:
4399 * spin_lock_irqsave(host_set lock)
4400 *
4401 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004402 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 */
4404
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004405unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
4407 struct ata_port *ap = qc->ap;
4408
Albert Leee50362e2005-09-27 17:39:50 +08004409 /* Use polling pio if the LLD doesn't handle
4410 * interrupt driven pio and atapi CDB interrupt.
4411 */
4412 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4413 switch (qc->tf.protocol) {
4414 case ATA_PROT_PIO:
4415 case ATA_PROT_ATAPI:
4416 case ATA_PROT_ATAPI_NODATA:
4417 qc->tf.flags |= ATA_TFLAG_POLLING;
4418 break;
4419 case ATA_PROT_ATAPI_DMA:
4420 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Leec2bbc552006-03-25 17:56:55 +08004421 /* see ata_check_atapi_dma() */
Albert Leee50362e2005-09-27 17:39:50 +08004422 BUG();
4423 break;
4424 default:
4425 break;
4426 }
4427 }
4428
Albert Lee312f7da2005-09-27 17:38:03 +08004429 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 ata_dev_select(ap, qc->dev->devno, 1, 0);
4431
Albert Lee312f7da2005-09-27 17:38:03 +08004432 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 switch (qc->tf.protocol) {
4434 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004435 if (qc->tf.flags & ATA_TFLAG_POLLING)
4436 ata_qc_set_polling(qc);
4437
Jeff Garzike5338252005-10-30 21:37:17 -05004438 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004439 ap->hsm_task_state = HSM_ST_LAST;
4440
4441 if (qc->tf.flags & ATA_TFLAG_POLLING)
Jeff Garzik46e202e2006-03-11 19:25:47 -05004442 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004443
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 break;
4445
4446 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004447 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4450 ap->ops->bmdma_setup(qc); /* set up bmdma */
4451 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004452 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 break;
4454
Albert Lee312f7da2005-09-27 17:38:03 +08004455 case ATA_PROT_PIO:
4456 if (qc->tf.flags & ATA_TFLAG_POLLING)
4457 ata_qc_set_polling(qc);
4458
Jeff Garzike5338252005-10-30 21:37:17 -05004459 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004460
Albert Lee54f00382005-09-30 19:14:19 +08004461 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4462 /* PIO data out protocol */
4463 ap->hsm_task_state = HSM_ST_FIRST;
Jeff Garzikce1e7a22006-03-11 19:21:17 -05004464 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Albert Lee54f00382005-09-30 19:14:19 +08004465
4466 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08004467 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08004468 */
Albert Lee312f7da2005-09-27 17:38:03 +08004469 } else {
Albert Lee54f00382005-09-30 19:14:19 +08004470 /* PIO data in protocol */
4471 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08004472
Albert Lee54f00382005-09-30 19:14:19 +08004473 if (qc->tf.flags & ATA_TFLAG_POLLING)
Jeff Garzikce1e7a22006-03-11 19:21:17 -05004474 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004475
Albert Lee54f00382005-09-30 19:14:19 +08004476 /* if polling, ata_pio_task() handles the rest.
4477 * otherwise, interrupt handler takes over from here.
4478 */
Albert Lee312f7da2005-09-27 17:38:03 +08004479 }
4480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 break;
4482
4483 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004485 if (qc->tf.flags & ATA_TFLAG_POLLING)
4486 ata_qc_set_polling(qc);
4487
Jeff Garzike5338252005-10-30 21:37:17 -05004488 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004489
Albert Lee312f7da2005-09-27 17:38:03 +08004490 ap->hsm_task_state = HSM_ST_FIRST;
4491
4492 /* send cdb by polling if no cdb interrupt */
4493 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4494 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee13ee4622006-03-25 17:39:34 +08004495 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 break;
4497
4498 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004499 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4502 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004503 ap->hsm_task_state = HSM_ST_FIRST;
4504
4505 /* send cdb by polling if no cdb interrupt */
4506 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee13ee4622006-03-25 17:39:34 +08004507 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 break;
4509
4510 default:
4511 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004512 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 }
4514
4515 return 0;
4516}
4517
4518/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 * ata_host_intr - Handle host interrupt for given (port, task)
4520 * @ap: Port on which interrupt arrived (possibly...)
4521 * @qc: Taskfile currently active in engine
4522 *
4523 * Handle host interrupt for given queued command. Currently,
4524 * only DMA interrupts are handled. All other commands are
4525 * handled via polling with interrupts disabled (nIEN bit).
4526 *
4527 * LOCKING:
4528 * spin_lock_irqsave(host_set lock)
4529 *
4530 * RETURNS:
4531 * One if interrupt was handled, zero if not (shared irq).
4532 */
4533
4534inline unsigned int ata_host_intr (struct ata_port *ap,
4535 struct ata_queued_cmd *qc)
4536{
Albert Lee312f7da2005-09-27 17:38:03 +08004537 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
Albert Lee312f7da2005-09-27 17:38:03 +08004539 VPRINTK("ata%u: protocol %d task_state %d\n",
4540 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Albert Lee312f7da2005-09-27 17:38:03 +08004542 /* Check whether we are expecting interrupt in this state */
4543 switch (ap->hsm_task_state) {
4544 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004545 /* Some pre-ATAPI-4 devices assert INTRQ
4546 * at this state when ready to receive CDB.
4547 */
4548
Albert Lee312f7da2005-09-27 17:38:03 +08004549 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4550 * The flag was turned on only for atapi devices.
4551 * No need to check is_atapi_taskfile(&qc->tf) again.
4552 */
4553 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08004555 break;
4556 case HSM_ST_LAST:
4557 if (qc->tf.protocol == ATA_PROT_DMA ||
4558 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4559 /* check status of DMA engine */
4560 host_stat = ap->ops->bmdma_status(ap);
4561 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Albert Lee312f7da2005-09-27 17:38:03 +08004563 /* if it's not our irq... */
4564 if (!(host_stat & ATA_DMA_INTR))
4565 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Albert Lee312f7da2005-09-27 17:38:03 +08004567 /* before we do anything else, clear DMA-Start bit */
4568 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08004569
4570 if (unlikely(host_stat & ATA_DMA_ERR)) {
4571 /* error when transfering data to/from memory */
4572 qc->err_mask |= AC_ERR_HOST_BUS;
4573 ap->hsm_task_state = HSM_ST_ERR;
4574 }
Albert Lee312f7da2005-09-27 17:38:03 +08004575 }
4576 break;
4577 case HSM_ST:
4578 break;
4579 default:
4580 goto idle_irq;
4581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
Albert Lee312f7da2005-09-27 17:38:03 +08004583 /* check altstatus */
4584 status = ata_altstatus(ap);
4585 if (status & ATA_BUSY)
4586 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Albert Lee312f7da2005-09-27 17:38:03 +08004588 /* check main status, clearing INTRQ */
4589 status = ata_chk_status(ap);
4590 if (unlikely(status & ATA_BUSY))
4591 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
Albert Lee312f7da2005-09-27 17:38:03 +08004593 /* ack bmdma irq events */
4594 ap->ops->irq_clear(ap);
4595
Albert Leebb5cb292006-03-25 17:48:02 +08004596 ata_hsm_move(ap, qc, status, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return 1; /* irq handled */
4598
4599idle_irq:
4600 ap->stats.idle_irq++;
4601
4602#ifdef ATA_IRQ_TRAP
4603 if ((ap->stats.idle_irq % 1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 ata_irq_ack(ap, 0); /* debug trap */
4605 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
Alan Cox23cfce82006-03-21 16:06:53 +00004606 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 }
4608#endif
4609 return 0; /* irq not handled */
4610}
4611
4612/**
4613 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004614 * @irq: irq line (unused)
4615 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 * @regs: unused
4617 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004618 * Default interrupt handler for PCI IDE devices. Calls
4619 * ata_host_intr() for each port that is not disabled.
4620 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004622 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 *
4624 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004625 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 */
4627
4628irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4629{
4630 struct ata_host_set *host_set = dev_instance;
4631 unsigned int i;
4632 unsigned int handled = 0;
4633 unsigned long flags;
4634
4635 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4636 spin_lock_irqsave(&host_set->lock, flags);
4637
4638 for (i = 0; i < host_set->n_ports; i++) {
4639 struct ata_port *ap;
4640
4641 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004642 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04004643 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 struct ata_queued_cmd *qc;
4645
4646 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004647 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004648 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 handled |= ata_host_intr(ap, qc);
4650 }
4651 }
4652
4653 spin_unlock_irqrestore(&host_set->lock, flags);
4654
4655 return IRQ_RETVAL(handled);
4656}
4657
Edward Falk0baab862005-06-02 18:17:13 -04004658
Jens Axboe9b847542006-01-06 09:28:07 +01004659/*
4660 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4661 * without filling any other registers
4662 */
4663static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4664 u8 cmd)
4665{
4666 struct ata_taskfile tf;
4667 int err;
4668
4669 ata_tf_init(ap, &tf, dev->devno);
4670
4671 tf.command = cmd;
4672 tf.flags |= ATA_TFLAG_DEVICE;
4673 tf.protocol = ATA_PROT_NODATA;
4674
Tejun Heod69cf372006-04-02 18:51:53 +09004675 err = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
Jens Axboe9b847542006-01-06 09:28:07 +01004676 if (err)
4677 printk(KERN_ERR "%s: ata command failed: %d\n",
4678 __FUNCTION__, err);
4679
4680 return err;
4681}
4682
4683static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4684{
4685 u8 cmd;
4686
4687 if (!ata_try_flush_cache(dev))
4688 return 0;
4689
4690 if (ata_id_has_flush_ext(dev->id))
4691 cmd = ATA_CMD_FLUSH_EXT;
4692 else
4693 cmd = ATA_CMD_FLUSH;
4694
4695 return ata_do_simple_cmd(ap, dev, cmd);
4696}
4697
4698static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4699{
4700 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4701}
4702
4703static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4704{
4705 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4706}
4707
4708/**
4709 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004710 * @ap: port the device is connected to
4711 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004712 *
4713 * Kick the drive back into action, by sending it an idle immediate
4714 * command and making sure its transfer mode matches between drive
4715 * and host.
4716 *
4717 */
4718int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4719{
4720 if (ap->flags & ATA_FLAG_SUSPENDED) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09004721 struct ata_device *failed_dev;
Jens Axboe9b847542006-01-06 09:28:07 +01004722 ap->flags &= ~ATA_FLAG_SUSPENDED;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09004723 while (ata_set_mode(ap, &failed_dev))
4724 ata_dev_disable(ap, failed_dev);
Jens Axboe9b847542006-01-06 09:28:07 +01004725 }
Tejun Heoe1211e32006-04-01 01:38:18 +09004726 if (!ata_dev_enabled(dev))
Jens Axboe9b847542006-01-06 09:28:07 +01004727 return 0;
4728 if (dev->class == ATA_DEV_ATA)
4729 ata_start_drive(ap, dev);
4730
4731 return 0;
4732}
4733
4734/**
4735 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004736 * @ap: port the device is connected to
4737 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004738 *
4739 * Flush the cache on the drive, if appropriate, then issue a
4740 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004741 */
Nigel Cunningham082776e2006-03-23 23:22:16 +10004742int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
Jens Axboe9b847542006-01-06 09:28:07 +01004743{
Tejun Heoe1211e32006-04-01 01:38:18 +09004744 if (!ata_dev_enabled(dev))
Jens Axboe9b847542006-01-06 09:28:07 +01004745 return 0;
4746 if (dev->class == ATA_DEV_ATA)
4747 ata_flush_cache(ap, dev);
4748
Nigel Cunningham082776e2006-03-23 23:22:16 +10004749 if (state.event != PM_EVENT_FREEZE)
4750 ata_standby_drive(ap, dev);
Jens Axboe9b847542006-01-06 09:28:07 +01004751 ap->flags |= ATA_FLAG_SUSPENDED;
4752 return 0;
4753}
4754
Albert Lee332b5a52006-02-08 16:51:34 +08004755/**
4756 * ata_port_start - Set port up for dma.
4757 * @ap: Port to initialize
4758 *
4759 * Called just after data structures for each port are
4760 * initialized. Allocates space for PRD table.
4761 *
4762 * May be used as the port_start() entry in ata_port_operations.
4763 *
4764 * LOCKING:
4765 * Inherited from caller.
4766 */
4767
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768int ata_port_start (struct ata_port *ap)
4769{
Brian King2f1f6102006-03-23 17:30:15 -06004770 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004771 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
4773 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4774 if (!ap->prd)
4775 return -ENOMEM;
4776
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004777 rc = ata_pad_alloc(ap, dev);
4778 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004779 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004780 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004781 }
4782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4784
4785 return 0;
4786}
4787
Edward Falk0baab862005-06-02 18:17:13 -04004788
4789/**
4790 * ata_port_stop - Undo ata_port_start()
4791 * @ap: Port to shut down
4792 *
4793 * Frees the PRD table.
4794 *
4795 * May be used as the port_stop() entry in ata_port_operations.
4796 *
4797 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004798 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004799 */
4800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801void ata_port_stop (struct ata_port *ap)
4802{
Brian King2f1f6102006-03-23 17:30:15 -06004803 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004806 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807}
4808
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004809void ata_host_stop (struct ata_host_set *host_set)
4810{
4811 if (host_set->mmio_base)
4812 iounmap(host_set->mmio_base);
4813}
4814
4815
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816/**
4817 * ata_host_remove - Unregister SCSI host structure with upper layers
4818 * @ap: Port to unregister
4819 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4820 *
4821 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004822 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 */
4824
4825static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4826{
4827 struct Scsi_Host *sh = ap->host;
4828
4829 DPRINTK("ENTER\n");
4830
4831 if (do_unregister)
4832 scsi_remove_host(sh);
4833
4834 ap->ops->port_stop(ap);
4835}
4836
4837/**
4838 * ata_host_init - Initialize an ata_port structure
4839 * @ap: Structure to initialize
4840 * @host: associated SCSI mid-layer structure
4841 * @host_set: Collection of hosts to which @ap belongs
4842 * @ent: Probe information provided by low-level driver
4843 * @port_no: Port number associated with this ata_port
4844 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004845 * Initialize a new ata_port structure, and its associated
4846 * scsi_host.
4847 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004849 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 */
4851
4852static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4853 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004854 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855{
4856 unsigned int i;
4857
4858 host->max_id = 16;
4859 host->max_lun = 1;
4860 host->max_channel = 1;
4861 host->unique_id = ata_unique_id++;
4862 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004863
Tejun Heo198e0fe2006-04-02 18:51:52 +09004864 ap->flags = ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 ap->id = host->unique_id;
4866 ap->host = host;
4867 ap->ctl = ATA_DEVCTL_OBS;
4868 ap->host_set = host_set;
Brian King2f1f6102006-03-23 17:30:15 -06004869 ap->dev = ent->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 ap->port_no = port_no;
4871 ap->hard_port_no =
4872 ent->legacy_mode ? ent->hard_port_no : port_no;
4873 ap->pio_mask = ent->pio_mask;
4874 ap->mwdma_mask = ent->mwdma_mask;
4875 ap->udma_mask = ent->udma_mask;
4876 ap->flags |= ent->host_flags;
4877 ap->ops = ent->port_ops;
4878 ap->cbl = ATA_CBL_NONE;
Tejun Heo1c3fae42006-04-02 20:53:28 +09004879 ap->sata_spd_limit = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 ap->active_tag = ATA_TAG_POISON;
4881 ap->last_ctl = 0xFF;
4882
Tejun Heo86e45b62006-03-05 15:29:09 +09004883 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004884 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
Tejun Heoacf356b2006-03-24 14:07:50 +09004886 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4887 struct ata_device *dev = &ap->device[i];
4888 dev->devno = i;
4889 dev->pio_mask = UINT_MAX;
4890 dev->mwdma_mask = UINT_MAX;
4891 dev->udma_mask = UINT_MAX;
4892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
4894#ifdef ATA_IRQ_TRAP
4895 ap->stats.unhandled_irq = 1;
4896 ap->stats.idle_irq = 1;
4897#endif
4898
4899 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4900}
4901
4902/**
4903 * ata_host_add - Attach low-level ATA driver to system
4904 * @ent: Information provided by low-level driver
4905 * @host_set: Collections of ports to which we add
4906 * @port_no: Port number associated with this host
4907 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004908 * Attach low-level ATA driver to system.
4909 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004911 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 *
4913 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004914 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 */
4916
Jeff Garzik057ace52005-10-22 14:27:05 -04004917static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 struct ata_host_set *host_set,
4919 unsigned int port_no)
4920{
4921 struct Scsi_Host *host;
4922 struct ata_port *ap;
4923 int rc;
4924
4925 DPRINTK("ENTER\n");
Tejun Heoaec5c3c2006-03-25 01:33:34 +09004926
4927 if (!ent->port_ops->probe_reset &&
4928 !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
4929 printk(KERN_ERR "ata%u: no reset mechanism available\n",
4930 port_no);
4931 return NULL;
4932 }
4933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4935 if (!host)
4936 return NULL;
4937
Tejun Heo30afc842006-03-18 18:40:14 +09004938 host->transportt = &ata_scsi_transport_template;
4939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 ap = (struct ata_port *) &host->hostdata[0];
4941
4942 ata_host_init(ap, host, host_set, ent, port_no);
4943
4944 rc = ap->ops->port_start(ap);
4945 if (rc)
4946 goto err_out;
4947
4948 return ap;
4949
4950err_out:
4951 scsi_host_put(host);
4952 return NULL;
4953}
4954
4955/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004956 * ata_device_add - Register hardware device with ATA and SCSI layers
4957 * @ent: Probe information describing hardware device to be registered
4958 *
4959 * This function processes the information provided in the probe
4960 * information struct @ent, allocates the necessary ATA and SCSI
4961 * host information structures, initializes them, and registers
4962 * everything with requisite kernel subsystems.
4963 *
4964 * This function requests irqs, probes the ATA bus, and probes
4965 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 *
4967 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004968 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 *
4970 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004971 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 */
4973
Jeff Garzik057ace52005-10-22 14:27:05 -04004974int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975{
4976 unsigned int count = 0, i;
4977 struct device *dev = ent->dev;
4978 struct ata_host_set *host_set;
4979
4980 DPRINTK("ENTER\n");
4981 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004982 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4984 if (!host_set)
4985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 spin_lock_init(&host_set->lock);
4987
4988 host_set->dev = dev;
4989 host_set->n_ports = ent->n_ports;
4990 host_set->irq = ent->irq;
4991 host_set->mmio_base = ent->mmio_base;
4992 host_set->private_data = ent->private_data;
4993 host_set->ops = ent->port_ops;
Alan Cox5444a6f2006-03-27 18:58:20 +01004994 host_set->flags = ent->host_set_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
4996 /* register each port bound to this device */
4997 for (i = 0; i < ent->n_ports; i++) {
4998 struct ata_port *ap;
4999 unsigned long xfer_mode_mask;
5000
5001 ap = ata_host_add(ent, host_set, i);
5002 if (!ap)
5003 goto err_out;
5004
5005 host_set->ports[i] = ap;
5006 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
5007 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
5008 (ap->pio_mask << ATA_SHIFT_PIO);
5009
5010 /* print per-port info to dmesg */
5011 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
5012 "bmdma 0x%lX irq %lu\n",
5013 ap->id,
5014 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
5015 ata_mode_string(xfer_mode_mask),
5016 ap->ioaddr.cmd_addr,
5017 ap->ioaddr.ctl_addr,
5018 ap->ioaddr.bmdma_addr,
5019 ent->irq);
5020
5021 ata_chk_status(ap);
5022 host_set->ops->irq_clear(ap);
5023 count++;
5024 }
5025
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005026 if (!count)
5027 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
5029 /* obtain irq, that is shared between channels */
5030 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
5031 DRV_NAME, host_set))
5032 goto err_out;
5033
5034 /* perform each probe synchronously */
5035 DPRINTK("probe begin\n");
5036 for (i = 0; i < count; i++) {
5037 struct ata_port *ap;
5038 int rc;
5039
5040 ap = host_set->ports[i];
5041
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005042 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005044 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
5046 if (rc) {
5047 /* FIXME: do something useful here?
5048 * Current libata behavior will
5049 * tear down everything when
5050 * the module is removed
5051 * or the h/w is unplugged.
5052 */
5053 }
5054
5055 rc = scsi_add_host(ap->host, dev);
5056 if (rc) {
5057 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
5058 ap->id);
5059 /* FIXME: do something useful here */
5060 /* FIXME: handle unconditional calls to
5061 * scsi_scan_host and ata_host_remove, below,
5062 * at the very least
5063 */
5064 }
5065 }
5066
5067 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005068 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 for (i = 0; i < count; i++) {
5070 struct ata_port *ap = host_set->ports[i];
5071
Jeff Garzik644dd0c2005-10-03 15:55:19 -04005072 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 }
5074
5075 dev_set_drvdata(dev, host_set);
5076
5077 VPRINTK("EXIT, returning %u\n", ent->n_ports);
5078 return ent->n_ports; /* success */
5079
5080err_out:
5081 for (i = 0; i < count; i++) {
5082 ata_host_remove(host_set->ports[i], 1);
5083 scsi_host_put(host_set->ports[i]->host);
5084 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005085err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 kfree(host_set);
5087 VPRINTK("EXIT, returning 0\n");
5088 return 0;
5089}
5090
5091/**
Alan Cox17b14452005-09-15 15:44:00 +01005092 * ata_host_set_remove - PCI layer callback for device removal
5093 * @host_set: ATA host set that was removed
5094 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005095 * Unregister all objects associated with this host set. Free those
Alan Cox17b14452005-09-15 15:44:00 +01005096 * objects.
5097 *
5098 * LOCKING:
5099 * Inherited from calling layer (may sleep).
5100 */
5101
Alan Cox17b14452005-09-15 15:44:00 +01005102void ata_host_set_remove(struct ata_host_set *host_set)
5103{
5104 struct ata_port *ap;
5105 unsigned int i;
5106
5107 for (i = 0; i < host_set->n_ports; i++) {
5108 ap = host_set->ports[i];
5109 scsi_remove_host(ap->host);
5110 }
5111
5112 free_irq(host_set->irq, host_set);
5113
5114 for (i = 0; i < host_set->n_ports; i++) {
5115 ap = host_set->ports[i];
5116
5117 ata_scsi_release(ap->host);
5118
5119 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
5120 struct ata_ioports *ioaddr = &ap->ioaddr;
5121
5122 if (ioaddr->cmd_addr == 0x1f0)
5123 release_region(0x1f0, 8);
5124 else if (ioaddr->cmd_addr == 0x170)
5125 release_region(0x170, 8);
5126 }
5127
5128 scsi_host_put(ap->host);
5129 }
5130
5131 if (host_set->ops->host_stop)
5132 host_set->ops->host_stop(host_set);
5133
5134 kfree(host_set);
5135}
5136
5137/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 * ata_scsi_release - SCSI layer callback hook for host unload
5139 * @host: libata host to be unloaded
5140 *
5141 * Performs all duties necessary to shut down a libata port...
5142 * Kill port kthread, disable port, and release resources.
5143 *
5144 * LOCKING:
5145 * Inherited from SCSI layer.
5146 *
5147 * RETURNS:
5148 * One.
5149 */
5150
5151int ata_scsi_release(struct Scsi_Host *host)
5152{
5153 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09005154 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
5156 DPRINTK("ENTER\n");
5157
5158 ap->ops->port_disable(ap);
5159 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09005160 for (i = 0; i < ATA_MAX_DEVICES; i++)
5161 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
5163 DPRINTK("EXIT\n");
5164 return 1;
5165}
5166
5167/**
5168 * ata_std_ports - initialize ioaddr with standard port offsets.
5169 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04005170 *
5171 * Utility function which initializes data_addr, error_addr,
5172 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5173 * device_addr, status_addr, and command_addr to standard offsets
5174 * relative to cmd_addr.
5175 *
5176 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 */
Edward Falk0baab862005-06-02 18:17:13 -04005178
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179void ata_std_ports(struct ata_ioports *ioaddr)
5180{
5181 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5182 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5183 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5184 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5185 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5186 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5187 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5188 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5189 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5190 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5191}
5192
Edward Falk0baab862005-06-02 18:17:13 -04005193
Jeff Garzik374b1872005-08-30 05:42:52 -04005194#ifdef CONFIG_PCI
5195
5196void ata_pci_host_stop (struct ata_host_set *host_set)
5197{
5198 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5199
5200 pci_iounmap(pdev, host_set->mmio_base);
5201}
5202
Edward Falk0baab862005-06-02 18:17:13 -04005203/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 * ata_pci_remove_one - PCI layer callback for device removal
5205 * @pdev: PCI device that was removed
5206 *
5207 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005208 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 * Handle this by unregistering all objects associated
5210 * with this PCI device. Free those objects. Then finally
5211 * release PCI resources and disable device.
5212 *
5213 * LOCKING:
5214 * Inherited from PCI layer (may sleep).
5215 */
5216
5217void ata_pci_remove_one (struct pci_dev *pdev)
5218{
5219 struct device *dev = pci_dev_to_dev(pdev);
5220 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
Alan Cox17b14452005-09-15 15:44:00 +01005222 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 pci_release_regions(pdev);
5224 pci_disable_device(pdev);
5225 dev_set_drvdata(dev, NULL);
5226}
5227
5228/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005229int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230{
5231 unsigned long tmp = 0;
5232
5233 switch (bits->width) {
5234 case 1: {
5235 u8 tmp8 = 0;
5236 pci_read_config_byte(pdev, bits->reg, &tmp8);
5237 tmp = tmp8;
5238 break;
5239 }
5240 case 2: {
5241 u16 tmp16 = 0;
5242 pci_read_config_word(pdev, bits->reg, &tmp16);
5243 tmp = tmp16;
5244 break;
5245 }
5246 case 4: {
5247 u32 tmp32 = 0;
5248 pci_read_config_dword(pdev, bits->reg, &tmp32);
5249 tmp = tmp32;
5250 break;
5251 }
5252
5253 default:
5254 return -EINVAL;
5255 }
5256
5257 tmp &= bits->mask;
5258
5259 return (tmp == bits->val) ? 1 : 0;
5260}
Jens Axboe9b847542006-01-06 09:28:07 +01005261
5262int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5263{
5264 pci_save_state(pdev);
5265 pci_disable_device(pdev);
5266 pci_set_power_state(pdev, PCI_D3hot);
5267 return 0;
5268}
5269
5270int ata_pci_device_resume(struct pci_dev *pdev)
5271{
5272 pci_set_power_state(pdev, PCI_D0);
5273 pci_restore_state(pdev);
5274 pci_enable_device(pdev);
5275 pci_set_master(pdev);
5276 return 0;
5277}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278#endif /* CONFIG_PCI */
5279
5280
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281static int __init ata_init(void)
5282{
5283 ata_wq = create_workqueue("ata");
5284 if (!ata_wq)
5285 return -ENOMEM;
5286
5287 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5288 return 0;
5289}
5290
5291static void __exit ata_exit(void)
5292{
5293 destroy_workqueue(ata_wq);
5294}
5295
5296module_init(ata_init);
5297module_exit(ata_exit);
5298
Jeff Garzik67846b32005-10-05 02:58:32 -04005299static unsigned long ratelimit_time;
5300static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5301
5302int ata_ratelimit(void)
5303{
5304 int rc;
5305 unsigned long flags;
5306
5307 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5308
5309 if (time_after(jiffies, ratelimit_time)) {
5310 rc = 1;
5311 ratelimit_time = jiffies + (HZ/5);
5312 } else
5313 rc = 0;
5314
5315 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5316
5317 return rc;
5318}
5319
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320/*
5321 * libata is essentially a library of internal helper functions for
5322 * low-level ATA host controller drivers. As such, the API/ABI is
5323 * likely to change as new drivers are added and updated.
5324 * Do not depend on ABI/API stability.
5325 */
5326
5327EXPORT_SYMBOL_GPL(ata_std_bios_param);
5328EXPORT_SYMBOL_GPL(ata_std_ports);
5329EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005330EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331EXPORT_SYMBOL_GPL(ata_sg_init);
5332EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09005333EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335EXPORT_SYMBOL_GPL(ata_tf_load);
5336EXPORT_SYMBOL_GPL(ata_tf_read);
5337EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5338EXPORT_SYMBOL_GPL(ata_std_dev_select);
5339EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5340EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5341EXPORT_SYMBOL_GPL(ata_check_status);
5342EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343EXPORT_SYMBOL_GPL(ata_exec_command);
5344EXPORT_SYMBOL_GPL(ata_port_start);
5345EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005346EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347EXPORT_SYMBOL_GPL(ata_interrupt);
5348EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06005349EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5351EXPORT_SYMBOL_GPL(ata_bmdma_start);
5352EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5353EXPORT_SYMBOL_GPL(ata_bmdma_status);
5354EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5355EXPORT_SYMBOL_GPL(ata_port_probe);
5356EXPORT_SYMBOL_GPL(sata_phy_reset);
5357EXPORT_SYMBOL_GPL(__sata_phy_reset);
5358EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005359EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005360EXPORT_SYMBOL_GPL(ata_std_softreset);
5361EXPORT_SYMBOL_GPL(sata_std_hardreset);
5362EXPORT_SYMBOL_GPL(ata_std_postreset);
5363EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005364EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09005365EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005366EXPORT_SYMBOL_GPL(ata_dev_classify);
5367EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005369EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005370EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09005371EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5373EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5375EXPORT_SYMBOL_GPL(ata_scsi_release);
5376EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo6a62a042006-02-13 10:02:46 +09005377EXPORT_SYMBOL_GPL(ata_id_string);
5378EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5380
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005381EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005382EXPORT_SYMBOL_GPL(ata_timing_compute);
5383EXPORT_SYMBOL_GPL(ata_timing_merge);
5384
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385#ifdef CONFIG_PCI
5386EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005387EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5389EXPORT_SYMBOL_GPL(ata_pci_init_one);
5390EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005391EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5392EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Alan Cox67951ad2006-03-22 15:55:54 +00005393EXPORT_SYMBOL_GPL(ata_pci_default_filter);
5394EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005396
5397EXPORT_SYMBOL_GPL(ata_device_suspend);
5398EXPORT_SYMBOL_GPL(ata_device_resume);
5399EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5400EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
Tejun Heoece1d632006-04-02 18:51:53 +09005401
5402EXPORT_SYMBOL_GPL(ata_scsi_error);
5403EXPORT_SYMBOL_GPL(ata_eng_timeout);
5404EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5405EXPORT_SYMBOL_GPL(ata_eh_qc_retry);