blob: 47fff7bbf3488551a0a2a0c0dcfa7ac0a072f706 [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 Heod7bb4cc2006-05-31 18:27:46 +090064/* debounce timing parameters in msecs { interval, duration, timeout } */
65const unsigned long sata_deb_timing_boot[] = { 5, 100, 2000 };
66const unsigned long sata_deb_timing_eh[] = { 25, 500, 2000 };
67const unsigned long sata_deb_timing_before_fsrst[] = { 100, 2000, 5000 };
68
Tejun Heo3373efd2006-05-15 20:57:53 +090069static unsigned int ata_dev_init_params(struct ata_device *dev,
70 u16 heads, u16 sectors);
71static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
72static void ata_dev_xfermask(struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static unsigned int ata_unique_id = 1;
75static struct workqueue_struct *ata_wq;
76
Tejun Heo453b07a2006-05-31 18:27:42 +090077struct workqueue_struct *ata_aux_wq;
78
Jeff Garzik418dc1f2006-03-11 20:50:08 -050079int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040080module_param(atapi_enabled, int, 0444);
81MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
82
Albert Lee95de7192006-04-04 10:57:18 +080083int atapi_dmadir = 0;
84module_param(atapi_dmadir, int, 0444);
85MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
86
Jeff Garzikc3c013a2006-02-27 22:31:19 -050087int libata_fua = 0;
88module_param_named(fua, libata_fua, int, 0444);
89MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
90
Andrew Mortona8601e52006-06-25 01:36:52 -070091static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
92module_param(ata_probe_timeout, int, 0444);
93MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095MODULE_AUTHOR("Jeff Garzik");
96MODULE_DESCRIPTION("Library module for ATA devices");
97MODULE_LICENSE("GPL");
98MODULE_VERSION(DRV_VERSION);
99
Edward Falk0baab862005-06-02 18:17:13 -0400100
101/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
103 * @tf: Taskfile to convert
104 * @fis: Buffer into which data will output
105 * @pmp: Port multiplier port
106 *
107 * Converts a standard ATA taskfile to a Serial ATA
108 * FIS structure (Register - Host to Device).
109 *
110 * LOCKING:
111 * Inherited from caller.
112 */
113
Jeff Garzik057ace52005-10-22 14:27:05 -0400114void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 fis[0] = 0x27; /* Register - Host to Device FIS */
117 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
118 bit 7 indicates Command FIS */
119 fis[2] = tf->command;
120 fis[3] = tf->feature;
121
122 fis[4] = tf->lbal;
123 fis[5] = tf->lbam;
124 fis[6] = tf->lbah;
125 fis[7] = tf->device;
126
127 fis[8] = tf->hob_lbal;
128 fis[9] = tf->hob_lbam;
129 fis[10] = tf->hob_lbah;
130 fis[11] = tf->hob_feature;
131
132 fis[12] = tf->nsect;
133 fis[13] = tf->hob_nsect;
134 fis[14] = 0;
135 fis[15] = tf->ctl;
136
137 fis[16] = 0;
138 fis[17] = 0;
139 fis[18] = 0;
140 fis[19] = 0;
141}
142
143/**
144 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
145 * @fis: Buffer from which data will be input
146 * @tf: Taskfile to output
147 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500148 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 *
150 * LOCKING:
151 * Inherited from caller.
152 */
153
Jeff Garzik057ace52005-10-22 14:27:05 -0400154void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 tf->command = fis[2]; /* status */
157 tf->feature = fis[3]; /* error */
158
159 tf->lbal = fis[4];
160 tf->lbam = fis[5];
161 tf->lbah = fis[6];
162 tf->device = fis[7];
163
164 tf->hob_lbal = fis[8];
165 tf->hob_lbam = fis[9];
166 tf->hob_lbah = fis[10];
167
168 tf->nsect = fis[12];
169 tf->hob_nsect = fis[13];
170}
171
Albert Lee8cbd6df2005-10-12 15:06:27 +0800172static const u8 ata_rw_cmds[] = {
173 /* pio multi */
174 ATA_CMD_READ_MULTI,
175 ATA_CMD_WRITE_MULTI,
176 ATA_CMD_READ_MULTI_EXT,
177 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100178 0,
179 0,
180 0,
181 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800182 /* pio */
183 ATA_CMD_PIO_READ,
184 ATA_CMD_PIO_WRITE,
185 ATA_CMD_PIO_READ_EXT,
186 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100187 0,
188 0,
189 0,
190 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800191 /* dma */
192 ATA_CMD_READ,
193 ATA_CMD_WRITE,
194 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100195 ATA_CMD_WRITE_EXT,
196 0,
197 0,
198 0,
199 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800200};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800203 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
204 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500206 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800207 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
209 * LOCKING:
210 * caller.
211 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100212int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800214 struct ata_taskfile *tf = &qc->tf;
215 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100216 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100218 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500219
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100220 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800221 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
222 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Albert Lee8cbd6df2005-10-12 15:06:27 +0800224 if (dev->flags & ATA_DFLAG_PIO) {
225 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100226 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000227 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
228 /* Unable to use DMA due to host limitation */
229 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800230 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800231 } else {
232 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100233 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100236 cmd = ata_rw_cmds[index + fua + lba48 + write];
237 if (cmd) {
238 tf->command = cmd;
239 return 0;
240 }
241 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Tejun Heocb95d562006-03-06 04:31:56 +0900244/**
245 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
246 * @pio_mask: pio_mask
247 * @mwdma_mask: mwdma_mask
248 * @udma_mask: udma_mask
249 *
250 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
251 * unsigned int xfer_mask.
252 *
253 * LOCKING:
254 * None.
255 *
256 * RETURNS:
257 * Packed xfer_mask.
258 */
259static unsigned int ata_pack_xfermask(unsigned int pio_mask,
260 unsigned int mwdma_mask,
261 unsigned int udma_mask)
262{
263 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
264 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
265 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
266}
267
Tejun Heoc0489e42006-03-24 14:07:49 +0900268/**
269 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
270 * @xfer_mask: xfer_mask to unpack
271 * @pio_mask: resulting pio_mask
272 * @mwdma_mask: resulting mwdma_mask
273 * @udma_mask: resulting udma_mask
274 *
275 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
276 * Any NULL distination masks will be ignored.
277 */
278static void ata_unpack_xfermask(unsigned int xfer_mask,
279 unsigned int *pio_mask,
280 unsigned int *mwdma_mask,
281 unsigned int *udma_mask)
282{
283 if (pio_mask)
284 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
285 if (mwdma_mask)
286 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
287 if (udma_mask)
288 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
289}
290
Tejun Heocb95d562006-03-06 04:31:56 +0900291static const struct ata_xfer_ent {
Tejun Heobe9a50c82006-03-31 22:48:52 +0900292 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900293 u8 base;
294} ata_xfer_tbl[] = {
295 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
296 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
297 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
298 { -1, },
299};
300
301/**
302 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
303 * @xfer_mask: xfer_mask of interest
304 *
305 * Return matching XFER_* value for @xfer_mask. Only the highest
306 * bit of @xfer_mask is considered.
307 *
308 * LOCKING:
309 * None.
310 *
311 * RETURNS:
312 * Matching XFER_* value, 0 if no match found.
313 */
314static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
315{
316 int highbit = fls(xfer_mask) - 1;
317 const struct ata_xfer_ent *ent;
318
319 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
320 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
321 return ent->base + highbit - ent->shift;
322 return 0;
323}
324
325/**
326 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
327 * @xfer_mode: XFER_* of interest
328 *
329 * Return matching xfer_mask for @xfer_mode.
330 *
331 * LOCKING:
332 * None.
333 *
334 * RETURNS:
335 * Matching xfer_mask, 0 if no match found.
336 */
337static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
338{
339 const struct ata_xfer_ent *ent;
340
341 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
342 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
343 return 1 << (ent->shift + xfer_mode - ent->base);
344 return 0;
345}
346
347/**
348 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
349 * @xfer_mode: XFER_* of interest
350 *
351 * Return matching xfer_shift for @xfer_mode.
352 *
353 * LOCKING:
354 * None.
355 *
356 * RETURNS:
357 * Matching xfer_shift, -1 if no match found.
358 */
359static int ata_xfer_mode2shift(unsigned int xfer_mode)
360{
361 const struct ata_xfer_ent *ent;
362
363 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
364 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
365 return ent->shift;
366 return -1;
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900370 * ata_mode_string - convert xfer_mask to string
371 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 *
373 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900374 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
376 * LOCKING:
377 * None.
378 *
379 * RETURNS:
380 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900381 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900383static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Tejun Heo75f554b2006-03-06 04:31:57 +0900385 static const char * const xfer_mode_str[] = {
386 "PIO0",
387 "PIO1",
388 "PIO2",
389 "PIO3",
390 "PIO4",
391 "MWDMA0",
392 "MWDMA1",
393 "MWDMA2",
394 "UDMA/16",
395 "UDMA/25",
396 "UDMA/33",
397 "UDMA/44",
398 "UDMA/66",
399 "UDMA/100",
400 "UDMA/133",
401 "UDMA7",
402 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900403 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900405 highbit = fls(xfer_mask) - 1;
406 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
407 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Tejun Heo4c360c82006-04-01 01:38:17 +0900411static const char *sata_spd_string(unsigned int spd)
412{
413 static const char * const spd_str[] = {
414 "1.5 Gbps",
415 "3.0 Gbps",
416 };
417
418 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
419 return "<unknown>";
420 return spd_str[spd - 1];
421}
422
Tejun Heo3373efd2006-05-15 20:57:53 +0900423void ata_dev_disable(struct ata_device *dev)
Tejun Heo0b8efb02006-03-24 15:25:31 +0900424{
Borislav Petkov0dd4b212006-06-23 02:29:08 -0400425 if (ata_dev_enabled(dev) && ata_msg_drv(dev->ap)) {
Tejun Heof15a1da2006-05-15 20:57:56 +0900426 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
Tejun Heo0b8efb02006-03-24 15:25:31 +0900427 dev->class++;
428 }
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/**
432 * ata_pio_devchk - PATA device presence detection
433 * @ap: ATA channel to examine
434 * @device: Device to examine (starting at zero)
435 *
436 * This technique was originally described in
437 * Hale Landis's ATADRVR (www.ata-atapi.com), and
438 * later found its way into the ATA/ATAPI spec.
439 *
440 * Write a pattern to the ATA shadow registers,
441 * and if a device is present, it will respond by
442 * correctly storing and echoing back the
443 * ATA shadow register contents.
444 *
445 * LOCKING:
446 * caller.
447 */
448
449static unsigned int ata_pio_devchk(struct ata_port *ap,
450 unsigned int device)
451{
452 struct ata_ioports *ioaddr = &ap->ioaddr;
453 u8 nsect, lbal;
454
455 ap->ops->dev_select(ap, device);
456
457 outb(0x55, ioaddr->nsect_addr);
458 outb(0xaa, ioaddr->lbal_addr);
459
460 outb(0xaa, ioaddr->nsect_addr);
461 outb(0x55, ioaddr->lbal_addr);
462
463 outb(0x55, ioaddr->nsect_addr);
464 outb(0xaa, ioaddr->lbal_addr);
465
466 nsect = inb(ioaddr->nsect_addr);
467 lbal = inb(ioaddr->lbal_addr);
468
469 if ((nsect == 0x55) && (lbal == 0xaa))
470 return 1; /* we found a device */
471
472 return 0; /* nothing found */
473}
474
475/**
476 * ata_mmio_devchk - PATA device presence detection
477 * @ap: ATA channel to examine
478 * @device: Device to examine (starting at zero)
479 *
480 * This technique was originally described in
481 * Hale Landis's ATADRVR (www.ata-atapi.com), and
482 * later found its way into the ATA/ATAPI spec.
483 *
484 * Write a pattern to the ATA shadow registers,
485 * and if a device is present, it will respond by
486 * correctly storing and echoing back the
487 * ATA shadow register contents.
488 *
489 * LOCKING:
490 * caller.
491 */
492
493static unsigned int ata_mmio_devchk(struct ata_port *ap,
494 unsigned int device)
495{
496 struct ata_ioports *ioaddr = &ap->ioaddr;
497 u8 nsect, lbal;
498
499 ap->ops->dev_select(ap, device);
500
501 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
502 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
503
504 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
505 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
506
507 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
508 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
509
510 nsect = readb((void __iomem *) ioaddr->nsect_addr);
511 lbal = readb((void __iomem *) ioaddr->lbal_addr);
512
513 if ((nsect == 0x55) && (lbal == 0xaa))
514 return 1; /* we found a device */
515
516 return 0; /* nothing found */
517}
518
519/**
520 * ata_devchk - PATA device presence detection
521 * @ap: ATA channel to examine
522 * @device: Device to examine (starting at zero)
523 *
524 * Dispatch ATA device presence detection, depending
525 * on whether we are using PIO or MMIO to talk to the
526 * ATA shadow registers.
527 *
528 * LOCKING:
529 * caller.
530 */
531
532static unsigned int ata_devchk(struct ata_port *ap,
533 unsigned int device)
534{
535 if (ap->flags & ATA_FLAG_MMIO)
536 return ata_mmio_devchk(ap, device);
537 return ata_pio_devchk(ap, device);
538}
539
540/**
541 * ata_dev_classify - determine device type based on ATA-spec signature
542 * @tf: ATA taskfile register set for device to be identified
543 *
544 * Determine from taskfile register contents whether a device is
545 * ATA or ATAPI, as per "Signature and persistence" section
546 * of ATA/PI spec (volume 1, sect 5.14).
547 *
548 * LOCKING:
549 * None.
550 *
551 * RETURNS:
552 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
553 * the event of failure.
554 */
555
Jeff Garzik057ace52005-10-22 14:27:05 -0400556unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 /* Apple's open source Darwin code hints that some devices only
559 * put a proper signature into the LBA mid/high registers,
560 * So, we only check those. It's sufficient for uniqueness.
561 */
562
563 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
564 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
565 DPRINTK("found ATA device by sig\n");
566 return ATA_DEV_ATA;
567 }
568
569 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
570 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
571 DPRINTK("found ATAPI device by sig\n");
572 return ATA_DEV_ATAPI;
573 }
574
575 DPRINTK("unknown device\n");
576 return ATA_DEV_UNKNOWN;
577}
578
579/**
580 * ata_dev_try_classify - Parse returned ATA device signature
581 * @ap: ATA channel to examine
582 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900583 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 *
585 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
586 * an ATA/ATAPI-defined set of values is placed in the ATA
587 * shadow registers, indicating the results of device detection
588 * and diagnostics.
589 *
590 * Select the ATA device, and read the values from the ATA shadow
591 * registers. Then parse according to the Error register value,
592 * and the spec-defined values examined by ata_dev_classify().
593 *
594 * LOCKING:
595 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900596 *
597 * RETURNS:
598 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
600
Tejun Heob4dc7622006-01-24 17:05:22 +0900601static unsigned int
602ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct ata_taskfile tf;
605 unsigned int class;
606 u8 err;
607
608 ap->ops->dev_select(ap, device);
609
610 memset(&tf, 0, sizeof(tf));
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400613 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900614 if (r_err)
615 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* see if device passed diags */
618 if (err == 1)
619 /* do nothing */ ;
620 else if ((device == 0) && (err == 0x81))
621 /* do nothing */ ;
622 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900623 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Tejun Heob4dc7622006-01-24 17:05:22 +0900625 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900629 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900631 return ATA_DEV_NONE;
632 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900636 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * @id: IDENTIFY DEVICE results we will examine
638 * @s: string into which data is output
639 * @ofs: offset into identify device page
640 * @len: length of string to return. must be an even number.
641 *
642 * The strings in the IDENTIFY DEVICE page are broken up into
643 * 16-bit chunks. Run through the string, and output each
644 * 8-bit chunk linearly, regardless of platform.
645 *
646 * LOCKING:
647 * caller.
648 */
649
Tejun Heo6a62a042006-02-13 10:02:46 +0900650void ata_id_string(const u16 *id, unsigned char *s,
651 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 unsigned int c;
654
655 while (len > 0) {
656 c = id[ofs] >> 8;
657 *s = c;
658 s++;
659
660 c = id[ofs] & 0xff;
661 *s = c;
662 s++;
663
664 ofs++;
665 len -= 2;
666 }
667}
668
Tejun Heo0e949ff2006-02-12 22:47:04 +0900669/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900670 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900671 * @id: IDENTIFY DEVICE results we will examine
672 * @s: string into which data is output
673 * @ofs: offset into identify device page
674 * @len: length of string to return. must be an odd number.
675 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900676 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900677 * trims trailing spaces and terminates the resulting string with
678 * null. @len must be actual maximum length (even number) + 1.
679 *
680 * LOCKING:
681 * caller.
682 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900683void ata_id_c_string(const u16 *id, unsigned char *s,
684 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900685{
686 unsigned char *p;
687
688 WARN_ON(!(len & 1));
689
Tejun Heo6a62a042006-02-13 10:02:46 +0900690 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900691
692 p = s + strnlen(s, len - 1);
693 while (p > s && p[-1] == ' ')
694 p--;
695 *p = '\0';
696}
Edward Falk0baab862005-06-02 18:17:13 -0400697
Tejun Heo29407402006-02-12 22:47:04 +0900698static u64 ata_id_n_sectors(const u16 *id)
699{
700 if (ata_id_has_lba(id)) {
701 if (ata_id_has_lba48(id))
702 return ata_id_u64(id, 100);
703 else
704 return ata_id_u32(id, 60);
705 } else {
706 if (ata_id_current_chs_valid(id))
707 return ata_id_u32(id, 57);
708 else
709 return id[1] * id[3] * id[6];
710 }
711}
712
Edward Falk0baab862005-06-02 18:17:13 -0400713/**
714 * ata_noop_dev_select - Select device 0/1 on ATA bus
715 * @ap: ATA channel to manipulate
716 * @device: ATA device (numbered from zero) to select
717 *
718 * This function performs no actual function.
719 *
720 * May be used as the dev_select() entry in ata_port_operations.
721 *
722 * LOCKING:
723 * caller.
724 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
726{
727}
728
Edward Falk0baab862005-06-02 18:17:13 -0400729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/**
731 * ata_std_dev_select - Select device 0/1 on ATA bus
732 * @ap: ATA channel to manipulate
733 * @device: ATA device (numbered from zero) to select
734 *
735 * Use the method defined in the ATA specification to
736 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400737 * ATA channel. Works with both PIO and MMIO.
738 *
739 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 *
741 * LOCKING:
742 * caller.
743 */
744
745void ata_std_dev_select (struct ata_port *ap, unsigned int device)
746{
747 u8 tmp;
748
749 if (device == 0)
750 tmp = ATA_DEVICE_OBS;
751 else
752 tmp = ATA_DEVICE_OBS | ATA_DEV1;
753
754 if (ap->flags & ATA_FLAG_MMIO) {
755 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
756 } else {
757 outb(tmp, ap->ioaddr.device_addr);
758 }
759 ata_pause(ap); /* needed; also flushes, for mmio */
760}
761
762/**
763 * ata_dev_select - Select device 0/1 on ATA bus
764 * @ap: ATA channel to manipulate
765 * @device: ATA device (numbered from zero) to select
766 * @wait: non-zero to wait for Status register BSY bit to clear
767 * @can_sleep: non-zero if context allows sleeping
768 *
769 * Use the method defined in the ATA specification to
770 * make either device 0, or device 1, active on the
771 * ATA channel.
772 *
773 * This is a high-level version of ata_std_dev_select(),
774 * which additionally provides the services of inserting
775 * the proper pauses and status polling, where needed.
776 *
777 * LOCKING:
778 * caller.
779 */
780
781void ata_dev_select(struct ata_port *ap, unsigned int device,
782 unsigned int wait, unsigned int can_sleep)
783{
Tejun Heo88574552006-06-25 20:00:35 +0900784 if (ata_msg_probe(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -0400785 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, ata%u: "
Tejun Heo88574552006-06-25 20:00:35 +0900786 "device %u, wait %u\n", ap->id, device, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (wait)
789 ata_wait_idle(ap);
790
791 ap->ops->dev_select(ap, device);
792
793 if (wait) {
794 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
795 msleep(150);
796 ata_wait_idle(ap);
797 }
798}
799
800/**
801 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900802 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900804 * Dump selected 16-bit words from the given IDENTIFY DEVICE
805 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 *
807 * LOCKING:
808 * caller.
809 */
810
Tejun Heo0bd33002006-02-12 22:47:05 +0900811static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 DPRINTK("49==0x%04x "
814 "53==0x%04x "
815 "63==0x%04x "
816 "64==0x%04x "
817 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900818 id[49],
819 id[53],
820 id[63],
821 id[64],
822 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 DPRINTK("80==0x%04x "
824 "81==0x%04x "
825 "82==0x%04x "
826 "83==0x%04x "
827 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900828 id[80],
829 id[81],
830 id[82],
831 id[83],
832 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 DPRINTK("88==0x%04x "
834 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900835 id[88],
836 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Tejun Heocb95d562006-03-06 04:31:56 +0900839/**
840 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
841 * @id: IDENTIFY data to compute xfer mask from
842 *
843 * Compute the xfermask for this device. This is not as trivial
844 * as it seems if we must consider early devices correctly.
845 *
846 * FIXME: pre IDE drive timing (do we care ?).
847 *
848 * LOCKING:
849 * None.
850 *
851 * RETURNS:
852 * Computed xfermask
853 */
854static unsigned int ata_id_xfermask(const u16 *id)
855{
856 unsigned int pio_mask, mwdma_mask, udma_mask;
857
858 /* Usual case. Word 53 indicates word 64 is valid */
859 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
860 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
861 pio_mask <<= 3;
862 pio_mask |= 0x7;
863 } else {
864 /* If word 64 isn't valid then Word 51 high byte holds
865 * the PIO timing number for the maximum. Turn it into
866 * a mask.
867 */
868 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
869
870 /* But wait.. there's more. Design your standards by
871 * committee and you too can get a free iordy field to
872 * process. However its the speeds not the modes that
873 * are supported... Note drivers using the timing API
874 * will get this right anyway
875 */
876 }
877
878 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +0900879
880 udma_mask = 0;
881 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
882 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900883
884 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
885}
886
Tejun Heo86e45b62006-03-05 15:29:09 +0900887/**
888 * ata_port_queue_task - Queue port_task
889 * @ap: The ata_port to queue port_task for
Randy Dunlape2a7f772006-05-18 10:50:18 -0700890 * @fn: workqueue function to be scheduled
891 * @data: data value to pass to workqueue function
892 * @delay: delay time for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +0900893 *
894 * Schedule @fn(@data) for execution after @delay jiffies using
895 * port_task. There is one port_task per port and it's the
896 * user(low level driver)'s responsibility to make sure that only
897 * one task is active at any given time.
898 *
899 * libata core layer takes care of synchronization between
900 * port_task and EH. ata_port_queue_task() may be ignored for EH
901 * synchronization.
902 *
903 * LOCKING:
904 * Inherited from caller.
905 */
906void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
907 unsigned long delay)
908{
909 int rc;
910
Tejun Heo2e755f62006-03-05 15:29:09 +0900911 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900912 return;
913
914 PREPARE_WORK(&ap->port_task, fn, data);
915
916 if (!delay)
917 rc = queue_work(ata_wq, &ap->port_task);
918 else
919 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
920
921 /* rc == 0 means that another user is using port task */
922 WARN_ON(rc == 0);
923}
924
925/**
926 * ata_port_flush_task - Flush port_task
927 * @ap: The ata_port to flush port_task for
928 *
929 * After this function completes, port_task is guranteed not to
930 * be running or scheduled.
931 *
932 * LOCKING:
933 * Kernel thread context (may sleep)
934 */
935void ata_port_flush_task(struct ata_port *ap)
936{
937 unsigned long flags;
938
939 DPRINTK("ENTER\n");
940
Jeff Garzikba6a1302006-06-22 23:46:10 -0400941 spin_lock_irqsave(ap->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900942 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400943 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo86e45b62006-03-05 15:29:09 +0900944
945 DPRINTK("flush #1\n");
946 flush_workqueue(ata_wq);
947
948 /*
949 * At this point, if a task is running, it's guaranteed to see
950 * the FLUSH flag; thus, it will never queue pio tasks again.
951 * Cancel and flush.
952 */
953 if (!cancel_delayed_work(&ap->port_task)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -0400954 if (ata_msg_ctl(ap))
Tejun Heo88574552006-06-25 20:00:35 +0900955 ata_port_printk(ap, KERN_DEBUG, "%s: flush #2\n",
956 __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +0900957 flush_workqueue(ata_wq);
958 }
959
Jeff Garzikba6a1302006-06-22 23:46:10 -0400960 spin_lock_irqsave(ap->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900961 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400962 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo86e45b62006-03-05 15:29:09 +0900963
Borislav Petkov0dd4b212006-06-23 02:29:08 -0400964 if (ata_msg_ctl(ap))
965 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +0900966}
967
Tejun Heo77853bf2006-01-23 13:09:36 +0900968void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900969{
Tejun Heo77853bf2006-01-23 13:09:36 +0900970 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900971
Tejun Heoa2a7a662005-12-13 14:48:31 +0900972 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900973}
974
975/**
976 * ata_exec_internal - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +0900977 * @dev: Device to which the command is sent
978 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +0900979 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +0900980 * @dma_dir: Data tranfer direction of the command
981 * @buf: Data buffer of the command
982 * @buflen: Length of data buffer
983 *
984 * Executes libata internal command with timeout. @tf contains
985 * command on entry and result on return. Timeout and error
986 * conditions are reported via return value. No recovery action
987 * is taken after a command times out. It's caller's duty to
988 * clean up after timeout.
989 *
990 * LOCKING:
991 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +0900992 *
993 * RETURNS:
994 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +0900995 */
Tejun Heo3373efd2006-05-15 20:57:53 +0900996unsigned ata_exec_internal(struct ata_device *dev,
Tejun Heo1ad8e7f2006-04-02 18:51:53 +0900997 struct ata_taskfile *tf, const u8 *cdb,
998 int dma_dir, void *buf, unsigned int buflen)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900999{
Tejun Heo3373efd2006-05-15 20:57:53 +09001000 struct ata_port *ap = dev->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001001 u8 command = tf->command;
1002 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001003 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001004 u32 preempted_sactive, preempted_qc_active;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001005 DECLARE_COMPLETION(wait);
1006 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001007 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001008 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001009
Jeff Garzikba6a1302006-06-22 23:46:10 -04001010 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001011
Tejun Heoe3180492006-05-15 20:58:09 +09001012 /* no internal command while frozen */
1013 if (ap->flags & ATA_FLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001014 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001015 return AC_ERR_SYSTEM;
1016 }
1017
Tejun Heo2ab7db12006-05-15 20:58:02 +09001018 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001019
Tejun Heo2ab7db12006-05-15 20:58:02 +09001020 /* XXX: Tag 0 is used for drivers with legacy EH as some
1021 * drivers choke if any other tag is given. This breaks
1022 * ata_tag_internal() test for those drivers. Don't use new
1023 * EH stuff without converting to it.
1024 */
1025 if (ap->ops->error_handler)
1026 tag = ATA_TAG_INTERNAL;
1027 else
1028 tag = 0;
1029
Tejun Heo6cec4a32006-05-15 21:03:41 +09001030 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001031 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001032 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001033
1034 qc->tag = tag;
1035 qc->scsicmd = NULL;
1036 qc->ap = ap;
1037 qc->dev = dev;
1038 ata_qc_reinit(qc);
1039
1040 preempted_tag = ap->active_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001041 preempted_sactive = ap->sactive;
1042 preempted_qc_active = ap->qc_active;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001043 ap->active_tag = ATA_TAG_POISON;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001044 ap->sactive = 0;
1045 ap->qc_active = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001046
1047 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001048 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001049 if (cdb)
1050 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001051 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001052 qc->dma_dir = dma_dir;
1053 if (dma_dir != DMA_NONE) {
1054 ata_sg_init_one(qc, buf, buflen);
1055 qc->nsect = buflen / ATA_SECT_SIZE;
1056 }
1057
Tejun Heo77853bf2006-01-23 13:09:36 +09001058 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001059 qc->complete_fn = ata_qc_complete_internal;
1060
Tejun Heo8e0e6942006-03-31 20:41:11 +09001061 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001062
Jeff Garzikba6a1302006-06-22 23:46:10 -04001063 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001064
Andrew Mortona8601e52006-06-25 01:36:52 -07001065 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
Albert Lee41ade502006-03-14 11:19:04 +08001066
Tejun Heod95a7172006-05-15 20:58:14 +09001067 ata_port_flush_task(ap);
1068
1069 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001070 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001071
1072 /* We're racing with irq here. If we lose, the
1073 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001074 * twice. If we win, the port is frozen and will be
1075 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001076 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001077 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001078 qc->err_mask |= AC_ERR_TIMEOUT;
1079
1080 if (ap->ops->error_handler)
1081 ata_port_freeze(ap);
1082 else
1083 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001084
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001085 if (ata_msg_warn(ap))
1086 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001087 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001088 }
1089
Jeff Garzikba6a1302006-06-22 23:46:10 -04001090 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001091 }
1092
Tejun Heod95a7172006-05-15 20:58:14 +09001093 /* do post_internal_cmd */
1094 if (ap->ops->post_internal_cmd)
1095 ap->ops->post_internal_cmd(qc);
1096
1097 if (qc->flags & ATA_QCFLAG_FAILED && !qc->err_mask) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001098 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001099 ata_dev_printk(dev, KERN_WARNING,
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001100 "zero err_mask for failed "
Tejun Heo88574552006-06-25 20:00:35 +09001101 "internal command, assuming AC_ERR_OTHER\n");
Tejun Heod95a7172006-05-15 20:58:14 +09001102 qc->err_mask |= AC_ERR_OTHER;
1103 }
1104
Tejun Heo15869302006-05-15 20:57:33 +09001105 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001106 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001107
Tejun Heoe61e0672006-05-15 20:57:40 +09001108 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001109 err_mask = qc->err_mask;
1110
1111 ata_qc_free(qc);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001112 ap->active_tag = preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001113 ap->sactive = preempted_sactive;
1114 ap->qc_active = preempted_qc_active;
Tejun Heo77853bf2006-01-23 13:09:36 +09001115
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001116 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1117 * Until those drivers are fixed, we detect the condition
1118 * here, fail the command with AC_ERR_SYSTEM and reenable the
1119 * port.
1120 *
1121 * Note that this doesn't change any behavior as internal
1122 * command failure results in disabling the device in the
1123 * higher layer for LLDDs without new reset/EH callbacks.
1124 *
1125 * Kill the following code as soon as those drivers are fixed.
1126 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001127 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001128 err_mask |= AC_ERR_SYSTEM;
1129 ata_port_probe(ap);
1130 }
1131
Jeff Garzikba6a1302006-06-22 23:46:10 -04001132 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001133
Tejun Heo77853bf2006-01-23 13:09:36 +09001134 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001135}
1136
Tejun Heo977e6b92006-06-24 20:30:19 +09001137/**
1138 * ata_do_simple_cmd - execute simple internal command
1139 * @dev: Device to which the command is sent
1140 * @cmd: Opcode to execute
1141 *
1142 * Execute a 'simple' command, that only consists of the opcode
1143 * 'cmd' itself, without filling any other registers
1144 *
1145 * LOCKING:
1146 * Kernel thread context (may sleep).
1147 *
1148 * RETURNS:
1149 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001150 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001151unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001152{
1153 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001154
1155 ata_tf_init(dev, &tf);
1156
1157 tf.command = cmd;
1158 tf.flags |= ATA_TFLAG_DEVICE;
1159 tf.protocol = ATA_PROT_NODATA;
1160
Tejun Heo977e6b92006-06-24 20:30:19 +09001161 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001162}
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001165 * ata_pio_need_iordy - check if iordy needed
1166 * @adev: ATA device
1167 *
1168 * Check if the current speed of the device requires IORDY. Used
1169 * by various controllers for chip configuration.
1170 */
1171
1172unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1173{
1174 int pio;
1175 int speed = adev->pio_mode - XFER_PIO_0;
1176
1177 if (speed < 2)
1178 return 0;
1179 if (speed > 2)
1180 return 1;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001181
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001182 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1183
1184 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1185 pio = adev->id[ATA_ID_EIDE_PIO];
1186 /* Is the speed faster than the drive allows non IORDY ? */
1187 if (pio) {
1188 /* This is cycle times not frequency - watch the logic! */
1189 if (pio > 240) /* PIO2 is 240nS per cycle */
1190 return 1;
1191 return 0;
1192 }
1193 }
1194 return 0;
1195}
1196
1197/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001198 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001199 * @dev: target device
1200 * @p_class: pointer to class of the target device (may be changed)
1201 * @post_reset: is this read ID post-reset?
Tejun Heofe635c72006-05-15 20:57:35 +09001202 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001203 *
1204 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1205 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001206 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1207 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001208 *
1209 * LOCKING:
1210 * Kernel thread context (may sleep)
1211 *
1212 * RETURNS:
1213 * 0 on success, -errno otherwise.
1214 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001215int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1216 int post_reset, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001217{
Tejun Heo3373efd2006-05-15 20:57:53 +09001218 struct ata_port *ap = dev->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001219 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001220 struct ata_taskfile tf;
1221 unsigned int err_mask = 0;
1222 const char *reason;
1223 int rc;
1224
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001225 if (ata_msg_ctl(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001226 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
1227 __FUNCTION__, ap->id, dev->devno);
Tejun Heo49016ac2006-02-21 02:12:11 +09001228
Tejun Heo49016ac2006-02-21 02:12:11 +09001229 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1230
1231 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001232 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001233
1234 switch (class) {
1235 case ATA_DEV_ATA:
1236 tf.command = ATA_CMD_ID_ATA;
1237 break;
1238 case ATA_DEV_ATAPI:
1239 tf.command = ATA_CMD_ID_ATAPI;
1240 break;
1241 default:
1242 rc = -ENODEV;
1243 reason = "unsupported class";
1244 goto err_out;
1245 }
1246
1247 tf.protocol = ATA_PROT_PIO;
1248
Tejun Heo3373efd2006-05-15 20:57:53 +09001249 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo49016ac2006-02-21 02:12:11 +09001250 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001251 if (err_mask) {
1252 rc = -EIO;
1253 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001254 goto err_out;
1255 }
1256
1257 swap_buf_le16(id, ATA_ID_WORDS);
1258
Tejun Heo49016ac2006-02-21 02:12:11 +09001259 /* sanity check */
Alan Cox692785e2006-03-27 18:49:19 +01001260 if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001261 rc = -EINVAL;
1262 reason = "device reports illegal type";
1263 goto err_out;
1264 }
1265
1266 if (post_reset && class == ATA_DEV_ATA) {
1267 /*
1268 * The exact sequence expected by certain pre-ATA4 drives is:
1269 * SRST RESET
1270 * IDENTIFY
1271 * INITIALIZE DEVICE PARAMETERS
1272 * anything else..
1273 * Some drives were very specific about that exact sequence.
1274 */
1275 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001276 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001277 if (err_mask) {
1278 rc = -EIO;
1279 reason = "INIT_DEV_PARAMS failed";
1280 goto err_out;
1281 }
1282
1283 /* current CHS translation info (id[53-58]) might be
1284 * changed. reread the identify device info.
1285 */
1286 post_reset = 0;
1287 goto retry;
1288 }
1289 }
1290
1291 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001292
Tejun Heo49016ac2006-02-21 02:12:11 +09001293 return 0;
1294
1295 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001296 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001297 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09001298 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001299 return rc;
1300}
1301
Tejun Heo3373efd2006-05-15 20:57:53 +09001302static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001303{
Tejun Heo3373efd2006-05-15 20:57:53 +09001304 return ((dev->ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001305}
1306
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001307static void ata_dev_config_ncq(struct ata_device *dev,
1308 char *desc, size_t desc_sz)
1309{
1310 struct ata_port *ap = dev->ap;
1311 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1312
1313 if (!ata_id_has_ncq(dev->id)) {
1314 desc[0] = '\0';
1315 return;
1316 }
1317
1318 if (ap->flags & ATA_FLAG_NCQ) {
1319 hdepth = min(ap->host->can_queue, ATA_MAX_QUEUE - 1);
1320 dev->flags |= ATA_DFLAG_NCQ;
1321 }
1322
1323 if (hdepth >= ddepth)
1324 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1325 else
1326 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1327}
1328
Tejun Heo49016ac2006-02-21 02:12:11 +09001329/**
Tejun Heoffeae412006-03-01 16:09:35 +09001330 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09001331 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001332 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
Tejun Heoffeae412006-03-01 16:09:35 +09001334 * Configure @dev according to @dev->id. Generic and low-level
1335 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 *
1337 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001338 * Kernel thread context (may sleep)
1339 *
1340 * RETURNS:
1341 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001343int ata_dev_configure(struct ata_device *dev, int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Tejun Heo3373efd2006-05-15 20:57:53 +09001345 struct ata_port *ap = dev->ap;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001346 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001347 unsigned int xfer_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001348 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001350 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo88574552006-06-25 20:00:35 +09001351 ata_dev_printk(dev, KERN_INFO,
1352 "%s: ENTER/EXIT (host %u, dev %u) -- nodev\n",
1353 __FUNCTION__, ap->id, dev->devno);
Tejun Heoffeae412006-03-01 16:09:35 +09001354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001357 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001358 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
1359 __FUNCTION__, ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001361 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001362 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001363 ata_dev_printk(dev, KERN_DEBUG,
1364 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1365 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001366 __FUNCTION__,
Tejun Heof15a1da2006-05-15 20:57:56 +09001367 id[49], id[82], id[83], id[84],
1368 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001369
Tejun Heo208a9932006-03-05 17:55:58 +09001370 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09001371 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09001372 dev->max_sectors = 0;
1373 dev->cdb_len = 0;
1374 dev->n_sectors = 0;
1375 dev->cylinders = 0;
1376 dev->heads = 0;
1377 dev->sectors = 0;
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /*
1380 * common ATA, ATAPI feature tests
1381 */
1382
Tejun Heoff8854b2006-03-06 04:31:56 +09001383 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001384 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001386 if (ata_msg_probe(ap))
1387 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /* ATA-specific feature tests */
1390 if (dev->class == ATA_DEV_ATA) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001391 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001392
Tejun Heo1148c3a2006-03-13 19:48:04 +09001393 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001394 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001395 char ncq_desc[20];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001396
Tejun Heo4c2d7212006-03-05 17:55:58 +09001397 lba_desc = "LBA";
1398 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001399 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001400 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001401 lba_desc = "LBA48";
1402 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04001403
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001404 /* config NCQ */
1405 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1406
Albert Lee8bf62ec2005-05-12 15:29:42 -04001407 /* print device info to dmesg */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001408 if (ata_msg_info(ap))
Tejun Heof15a1da2006-05-15 20:57:56 +09001409 ata_dev_printk(dev, KERN_INFO, "ATA-%d, "
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001410 "max %s, %Lu sectors: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09001411 ata_id_major_version(id),
1412 ata_mode_string(xfer_mask),
1413 (unsigned long long)dev->n_sectors,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001414 lba_desc, ncq_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001415 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001416 /* CHS */
1417
1418 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001419 dev->cylinders = id[1];
1420 dev->heads = id[3];
1421 dev->sectors = id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001422
Tejun Heo1148c3a2006-03-13 19:48:04 +09001423 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001424 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001425 dev->cylinders = id[54];
1426 dev->heads = id[55];
1427 dev->sectors = id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001428 }
1429
1430 /* print device info to dmesg */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001431 if (ata_msg_info(ap))
Tejun Heof15a1da2006-05-15 20:57:56 +09001432 ata_dev_printk(dev, KERN_INFO, "ATA-%d, "
1433 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1434 ata_id_major_version(id),
1435 ata_mode_string(xfer_mask),
1436 (unsigned long long)dev->n_sectors,
Tejun Heo88574552006-06-25 20:00:35 +09001437 dev->cylinders, dev->heads,
1438 dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Albert Lee07f6f7d2005-11-01 19:33:20 +08001441 if (dev->id[59] & 0x100) {
1442 dev->multi_count = dev->id[59] & 0xff;
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001443 if (ata_msg_info(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001444 ata_dev_printk(dev, KERN_INFO,
1445 "ata%u: dev %u multi count %u\n",
1446 ap->id, dev->devno, dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08001447 }
1448
Tejun Heo6e7846e2006-02-12 23:32:58 +09001449 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
1452 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001453 else if (dev->class == ATA_DEV_ATAPI) {
Albert Lee08a556d2006-03-31 13:29:04 +08001454 char *cdb_intr_string = "";
1455
Tejun Heo1148c3a2006-03-13 19:48:04 +09001456 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001458 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001459 ata_dev_printk(dev, KERN_WARNING,
1460 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001461 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 goto err_out_nosup;
1463 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001464 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Albert Lee08a556d2006-03-31 13:29:04 +08001466 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08001467 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08001468 cdb_intr_string = ", CDB intr";
1469 }
Albert Lee312f7da2005-09-27 17:38:03 +08001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* print device info to dmesg */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001472 if (ata_msg_info(ap))
Tejun Heo12436c32006-05-15 20:59:15 +09001473 ata_dev_printk(dev, KERN_INFO, "ATAPI, max %s%s\n",
1474 ata_mode_string(xfer_mask),
1475 cdb_intr_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477
Tejun Heo6e7846e2006-02-12 23:32:58 +09001478 ap->host->max_cmd_len = 0;
1479 for (i = 0; i < ATA_MAX_DEVICES; i++)
1480 ap->host->max_cmd_len = max_t(unsigned int,
1481 ap->host->max_cmd_len,
1482 ap->device[i].cdb_len);
1483
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001484 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09001485 if (ata_dev_knobble(dev)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001486 if (ata_msg_info(ap))
Tejun Heof15a1da2006-05-15 20:57:56 +09001487 ata_dev_printk(dev, KERN_INFO,
1488 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09001489 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001490 dev->max_sectors = ATA_MAX_SECTORS;
1491 }
1492
1493 if (ap->ops->dev_config)
1494 ap->ops->dev_config(ap, dev);
1495
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001496 if (ata_msg_probe(ap))
1497 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
1498 __FUNCTION__, ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001502 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001503 ata_dev_printk(dev, KERN_DEBUG,
1504 "%s: EXIT, err\n", __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09001505 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
1508/**
1509 * ata_bus_probe - Reset and probe ATA bus
1510 * @ap: Bus to probe
1511 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001512 * Master ATA bus probing function. Initiates a hardware-dependent
1513 * bus reset, then attempts to identify any devices found on
1514 * the bus.
1515 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001517 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 *
1519 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09001520 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 */
1522
1523static int ata_bus_probe(struct ata_port *ap)
1524{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001525 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09001526 int tries[ATA_MAX_DEVICES];
1527 int i, rc, down_xfermask;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001528 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Tejun Heo28ca5c52006-03-01 16:09:36 +09001530 ata_port_probe(ap);
1531
Tejun Heo14d2bac2006-04-02 17:54:46 +09001532 for (i = 0; i < ATA_MAX_DEVICES; i++)
1533 tries[i] = ATA_PROBE_MAX_TRIES;
1534
1535 retry:
1536 down_xfermask = 0;
1537
Tejun Heo20444702006-03-13 01:57:01 +09001538 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09001539 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09001540
Tejun Heo52783c52006-05-31 18:28:22 +09001541 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1542 dev = &ap->device[i];
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001543
Tejun Heo52783c52006-05-31 18:28:22 +09001544 if (!(ap->flags & ATA_FLAG_DISABLED) &&
1545 dev->class != ATA_DEV_UNKNOWN)
1546 classes[dev->devno] = dev->class;
1547 else
1548 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09001549
Tejun Heo52783c52006-05-31 18:28:22 +09001550 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09001551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Tejun Heo52783c52006-05-31 18:28:22 +09001553 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09001554
Alan Coxb6079ca2006-05-22 16:52:06 +01001555 /* after the reset the device state is PIO 0 and the controller
1556 state is undefined. Record the mode */
1557
1558 for (i = 0; i < ATA_MAX_DEVICES; i++)
1559 ap->device[i].pio_mode = XFER_PIO_0;
1560
Tejun Heo28ca5c52006-03-01 16:09:36 +09001561 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001563 dev = &ap->device[i];
Tejun Heo28ca5c52006-03-01 16:09:36 +09001564
Tejun Heoec573752006-04-11 22:26:29 +09001565 if (tries[i])
1566 dev->class = classes[i];
Tejun Heo14d2bac2006-04-02 17:54:46 +09001567
Tejun Heoe1211e32006-04-01 01:38:18 +09001568 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09001569 continue;
1570
Tejun Heo3373efd2006-05-15 20:57:53 +09001571 rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09001572 if (rc)
1573 goto fail;
Tejun Heoffeae412006-03-01 16:09:35 +09001574
Tejun Heo3373efd2006-05-15 20:57:53 +09001575 rc = ata_dev_configure(dev, 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09001576 if (rc)
1577 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001580 /* configure transfer mode */
Tejun Heo3adcebb2006-05-15 20:57:37 +09001581 rc = ata_set_mode(ap, &dev);
Tejun Heo51713d32006-04-11 22:26:29 +09001582 if (rc) {
1583 down_xfermask = 1;
1584 goto fail;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001587 for (i = 0; i < ATA_MAX_DEVICES; i++)
1588 if (ata_dev_enabled(&ap->device[i]))
1589 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01001590
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001591 /* no device present, disable port */
1592 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 ap->ops->port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09001594 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09001595
1596 fail:
1597 switch (rc) {
1598 case -EINVAL:
1599 case -ENODEV:
1600 tries[dev->devno] = 0;
1601 break;
1602 case -EIO:
Tejun Heo3c567b72006-05-15 20:57:23 +09001603 sata_down_spd_limit(ap);
Tejun Heo14d2bac2006-04-02 17:54:46 +09001604 /* fall through */
1605 default:
1606 tries[dev->devno]--;
1607 if (down_xfermask &&
Tejun Heo3373efd2006-05-15 20:57:53 +09001608 ata_down_xfermask_limit(dev, tries[dev->devno] == 1))
Tejun Heo14d2bac2006-04-02 17:54:46 +09001609 tries[dev->devno] = 0;
1610 }
1611
Tejun Heoec573752006-04-11 22:26:29 +09001612 if (!tries[dev->devno]) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001613 ata_down_xfermask_limit(dev, 1);
1614 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09001615 }
1616
Tejun Heo14d2bac2006-04-02 17:54:46 +09001617 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
1620/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001621 * ata_port_probe - Mark port as enabled
1622 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001624 * Modify @ap data structure such that the system
1625 * thinks that the entire port is enabled.
1626 *
1627 * LOCKING: host_set lock, or some other form of
1628 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
1630
1631void ata_port_probe(struct ata_port *ap)
1632{
Tejun Heo198e0fe2006-04-02 18:51:52 +09001633 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001637 * sata_print_link_status - Print SATA link status
1638 * @ap: SATA port to printk link status about
1639 *
1640 * This function prints link speed and status of a SATA link.
1641 *
1642 * LOCKING:
1643 * None.
1644 */
1645static void sata_print_link_status(struct ata_port *ap)
1646{
Tejun Heo6d5f9732006-04-03 00:09:41 +09001647 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09001648
Tejun Heo81952c52006-05-15 20:57:47 +09001649 if (sata_scr_read(ap, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09001650 return;
Tejun Heo81952c52006-05-15 20:57:47 +09001651 sata_scr_read(ap, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001652
Tejun Heo81952c52006-05-15 20:57:47 +09001653 if (ata_port_online(ap)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09001654 tmp = (sstatus >> 4) & 0xf;
Tejun Heof15a1da2006-05-15 20:57:56 +09001655 ata_port_printk(ap, KERN_INFO,
1656 "SATA link up %s (SStatus %X SControl %X)\n",
1657 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001658 } else {
Tejun Heof15a1da2006-05-15 20:57:56 +09001659 ata_port_printk(ap, KERN_INFO,
1660 "SATA link down (SStatus %X SControl %X)\n",
1661 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001662 }
1663}
1664
1665/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001666 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1667 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001669 * This function issues commands to standard SATA Sxxx
1670 * PHY registers, to wake up the phy (and device), and
1671 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 *
1673 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001674 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 *
1676 */
1677void __sata_phy_reset(struct ata_port *ap)
1678{
1679 u32 sstatus;
1680 unsigned long timeout = jiffies + (HZ * 5);
1681
1682 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001683 /* issue phy wake/reset */
Tejun Heo81952c52006-05-15 20:57:47 +09001684 sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001685 /* Couldn't find anything in SATA I/II specs, but
1686 * AHCI-1.1 10.4.2 says at least 1 ms. */
1687 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
Tejun Heo81952c52006-05-15 20:57:47 +09001689 /* phy wake/clear reset */
1690 sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 /* wait for phy to become ready, if necessary */
1693 do {
1694 msleep(200);
Tejun Heo81952c52006-05-15 20:57:47 +09001695 sata_scr_read(ap, SCR_STATUS, &sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if ((sstatus & 0xf) != 1)
1697 break;
1698 } while (time_before(jiffies, timeout));
1699
Tejun Heo3be680b2005-12-19 22:35:02 +09001700 /* print link status */
1701 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001702
Tejun Heo3be680b2005-12-19 22:35:02 +09001703 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo81952c52006-05-15 20:57:47 +09001704 if (!ata_port_offline(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001706 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Tejun Heo198e0fe2006-04-02 18:51:52 +09001709 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return;
1711
1712 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1713 ata_port_disable(ap);
1714 return;
1715 }
1716
1717 ap->cbl = ATA_CBL_SATA;
1718}
1719
1720/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001721 * sata_phy_reset - Reset SATA bus.
1722 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001724 * This function resets the SATA bus, and then probes
1725 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 *
1727 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001728 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 *
1730 */
1731void sata_phy_reset(struct ata_port *ap)
1732{
1733 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09001734 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return;
1736 ata_bus_reset(ap);
1737}
1738
1739/**
Alan Coxebdfca62006-03-23 15:38:34 +00001740 * ata_dev_pair - return other device on cable
Alan Coxebdfca62006-03-23 15:38:34 +00001741 * @adev: device
1742 *
1743 * Obtain the other device on the same cable, or if none is
1744 * present NULL is returned
1745 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001746
Tejun Heo3373efd2006-05-15 20:57:53 +09001747struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca62006-03-23 15:38:34 +00001748{
Tejun Heo3373efd2006-05-15 20:57:53 +09001749 struct ata_port *ap = adev->ap;
Alan Coxebdfca62006-03-23 15:38:34 +00001750 struct ata_device *pair = &ap->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09001751 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00001752 return NULL;
1753 return pair;
1754}
1755
1756/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001757 * ata_port_disable - Disable port.
1758 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001760 * Modify @ap data structure such that the system
1761 * thinks that the entire port is disabled, and should
1762 * never attempt to probe or communicate with devices
1763 * on this port.
1764 *
1765 * LOCKING: host_set lock, or some other form of
1766 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
1768
1769void ata_port_disable(struct ata_port *ap)
1770{
1771 ap->device[0].class = ATA_DEV_NONE;
1772 ap->device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09001773 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Tejun Heo1c3fae42006-04-02 20:53:28 +09001776/**
Tejun Heo3c567b72006-05-15 20:57:23 +09001777 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo1c3fae42006-04-02 20:53:28 +09001778 * @ap: Port to adjust SATA spd limit for
1779 *
1780 * Adjust SATA spd limit of @ap downward. Note that this
1781 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09001782 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09001783 *
1784 * LOCKING:
1785 * Inherited from caller.
1786 *
1787 * RETURNS:
1788 * 0 on success, negative errno on failure
1789 */
Tejun Heo3c567b72006-05-15 20:57:23 +09001790int sata_down_spd_limit(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001791{
Tejun Heo81952c52006-05-15 20:57:47 +09001792 u32 sstatus, spd, mask;
1793 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001794
Tejun Heo81952c52006-05-15 20:57:47 +09001795 rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
1796 if (rc)
1797 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001798
1799 mask = ap->sata_spd_limit;
1800 if (mask <= 1)
1801 return -EINVAL;
1802 highbit = fls(mask) - 1;
1803 mask &= ~(1 << highbit);
1804
Tejun Heo81952c52006-05-15 20:57:47 +09001805 spd = (sstatus >> 4) & 0xf;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001806 if (spd <= 1)
1807 return -EINVAL;
1808 spd--;
1809 mask &= (1 << spd) - 1;
1810 if (!mask)
1811 return -EINVAL;
1812
1813 ap->sata_spd_limit = mask;
1814
Tejun Heof15a1da2006-05-15 20:57:56 +09001815 ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
1816 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09001817
1818 return 0;
1819}
1820
Tejun Heo3c567b72006-05-15 20:57:23 +09001821static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001822{
1823 u32 spd, limit;
1824
1825 if (ap->sata_spd_limit == UINT_MAX)
1826 limit = 0;
1827 else
1828 limit = fls(ap->sata_spd_limit);
1829
1830 spd = (*scontrol >> 4) & 0xf;
1831 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
1832
1833 return spd != limit;
1834}
1835
1836/**
Tejun Heo3c567b72006-05-15 20:57:23 +09001837 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo1c3fae42006-04-02 20:53:28 +09001838 * @ap: Port in question
1839 *
1840 * Test whether the spd limit in SControl matches
1841 * @ap->sata_spd_limit. This function is used to determine
1842 * whether hardreset is necessary to apply SATA spd
1843 * configuration.
1844 *
1845 * LOCKING:
1846 * Inherited from caller.
1847 *
1848 * RETURNS:
1849 * 1 if SATA spd configuration is needed, 0 otherwise.
1850 */
Tejun Heo3c567b72006-05-15 20:57:23 +09001851int sata_set_spd_needed(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001852{
1853 u32 scontrol;
1854
Tejun Heo81952c52006-05-15 20:57:47 +09001855 if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09001856 return 0;
1857
Tejun Heo3c567b72006-05-15 20:57:23 +09001858 return __sata_set_spd_needed(ap, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09001859}
1860
1861/**
Tejun Heo3c567b72006-05-15 20:57:23 +09001862 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09001863 * @ap: Port to set SATA spd for
1864 *
1865 * Set SATA spd of @ap according to sata_spd_limit.
1866 *
1867 * LOCKING:
1868 * Inherited from caller.
1869 *
1870 * RETURNS:
1871 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09001872 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09001873 */
Tejun Heo3c567b72006-05-15 20:57:23 +09001874int sata_set_spd(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001875{
1876 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09001877 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001878
Tejun Heo81952c52006-05-15 20:57:47 +09001879 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
1880 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001881
Tejun Heo3c567b72006-05-15 20:57:23 +09001882 if (!__sata_set_spd_needed(ap, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09001883 return 0;
1884
Tejun Heo81952c52006-05-15 20:57:47 +09001885 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
1886 return rc;
1887
Tejun Heo1c3fae42006-04-02 20:53:28 +09001888 return 1;
1889}
1890
Alan Cox452503f2005-10-21 19:01:32 -04001891/*
1892 * This mode timing computation functionality is ported over from
1893 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1894 */
1895/*
1896 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1897 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1898 * for PIO 5, which is a nonstandard extension and UDMA6, which
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001899 * is currently supported only by Maxtor drives.
Alan Cox452503f2005-10-21 19:01:32 -04001900 */
1901
1902static const struct ata_timing ata_timing[] = {
1903
1904 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1905 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1906 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1907 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1908
1909 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1910 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1911 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1912
1913/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001914
Alan Cox452503f2005-10-21 19:01:32 -04001915 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1916 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1917 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001918
Alan Cox452503f2005-10-21 19:01:32 -04001919 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1920 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1921 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1922
1923/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1924 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1925 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1926
1927 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1928 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1929 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1930
1931/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1932
1933 { 0xFF }
1934};
1935
1936#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1937#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1938
1939static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1940{
1941 q->setup = EZ(t->setup * 1000, T);
1942 q->act8b = EZ(t->act8b * 1000, T);
1943 q->rec8b = EZ(t->rec8b * 1000, T);
1944 q->cyc8b = EZ(t->cyc8b * 1000, T);
1945 q->active = EZ(t->active * 1000, T);
1946 q->recover = EZ(t->recover * 1000, T);
1947 q->cycle = EZ(t->cycle * 1000, T);
1948 q->udma = EZ(t->udma * 1000, UT);
1949}
1950
1951void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1952 struct ata_timing *m, unsigned int what)
1953{
1954 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1955 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1956 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1957 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1958 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1959 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1960 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1961 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1962}
1963
1964static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1965{
1966 const struct ata_timing *t;
1967
1968 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001969 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001970 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001971 return t;
Alan Cox452503f2005-10-21 19:01:32 -04001972}
1973
1974int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1975 struct ata_timing *t, int T, int UT)
1976{
1977 const struct ata_timing *s;
1978 struct ata_timing p;
1979
1980 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001981 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001982 */
Alan Cox452503f2005-10-21 19:01:32 -04001983
1984 if (!(s = ata_timing_find_mode(speed)))
1985 return -EINVAL;
1986
Albert Lee75b1f2f2005-11-16 17:06:18 +08001987 memcpy(t, s, sizeof(*s));
1988
Alan Cox452503f2005-10-21 19:01:32 -04001989 /*
1990 * If the drive is an EIDE drive, it can tell us it needs extended
1991 * PIO/MW_DMA cycle timing.
1992 */
1993
1994 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1995 memset(&p, 0, sizeof(p));
1996 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1997 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1998 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1999 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2000 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2001 }
2002 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2003 }
2004
2005 /*
2006 * Convert the timing to bus clock counts.
2007 */
2008
Albert Lee75b1f2f2005-11-16 17:06:18 +08002009 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04002010
2011 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002012 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2013 * S.M.A.R.T * and some other commands. We have to ensure that the
2014 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04002015 */
2016
2017 if (speed > XFER_PIO_4) {
2018 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2019 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2020 }
2021
2022 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002023 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04002024 */
2025
2026 if (t->act8b + t->rec8b < t->cyc8b) {
2027 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2028 t->rec8b = t->cyc8b - t->act8b;
2029 }
2030
2031 if (t->active + t->recover < t->cycle) {
2032 t->active += (t->cycle - (t->active + t->recover)) / 2;
2033 t->recover = t->cycle - t->active;
2034 }
2035
2036 return 0;
2037}
2038
Tejun Heocf176e12006-04-02 17:54:46 +09002039/**
2040 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09002041 * @dev: Device to adjust xfer masks
2042 * @force_pio0: Force PIO0
2043 *
2044 * Adjust xfer masks of @dev downward. Note that this function
2045 * does not apply the change. Invoking ata_set_mode() afterwards
2046 * will apply the limit.
2047 *
2048 * LOCKING:
2049 * Inherited from caller.
2050 *
2051 * RETURNS:
2052 * 0 on success, negative errno on failure
2053 */
Tejun Heo3373efd2006-05-15 20:57:53 +09002054int ata_down_xfermask_limit(struct ata_device *dev, int force_pio0)
Tejun Heocf176e12006-04-02 17:54:46 +09002055{
2056 unsigned long xfer_mask;
2057 int highbit;
2058
2059 xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
2060 dev->udma_mask);
2061
2062 if (!xfer_mask)
2063 goto fail;
2064 /* don't gear down to MWDMA from UDMA, go directly to PIO */
2065 if (xfer_mask & ATA_MASK_UDMA)
2066 xfer_mask &= ~ATA_MASK_MWDMA;
2067
2068 highbit = fls(xfer_mask) - 1;
2069 xfer_mask &= ~(1 << highbit);
2070 if (force_pio0)
2071 xfer_mask &= 1 << ATA_SHIFT_PIO;
2072 if (!xfer_mask)
2073 goto fail;
2074
2075 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2076 &dev->udma_mask);
2077
Tejun Heof15a1da2006-05-15 20:57:56 +09002078 ata_dev_printk(dev, KERN_WARNING, "limiting speed to %s\n",
2079 ata_mode_string(xfer_mask));
Tejun Heocf176e12006-04-02 17:54:46 +09002080
2081 return 0;
2082
2083 fail:
2084 return -EINVAL;
2085}
2086
Tejun Heo3373efd2006-05-15 20:57:53 +09002087static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Tejun Heo83206a22006-03-24 15:25:31 +09002089 unsigned int err_mask;
2090 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Tejun Heoe8384602006-04-02 18:51:53 +09002092 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (dev->xfer_shift == ATA_SHIFT_PIO)
2094 dev->flags |= ATA_DFLAG_PIO;
2095
Tejun Heo3373efd2006-05-15 20:57:53 +09002096 err_mask = ata_dev_set_xfermode(dev);
Tejun Heo83206a22006-03-24 15:25:31 +09002097 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002098 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2099 "(err_mask=0x%x)\n", err_mask);
Tejun Heo83206a22006-03-24 15:25:31 +09002100 return -EIO;
2101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Tejun Heo3373efd2006-05-15 20:57:53 +09002103 rc = ata_dev_revalidate(dev, 0);
Tejun Heo5eb45c02006-04-02 18:51:52 +09002104 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09002105 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09002106
Tejun Heo23e71c32006-03-06 04:31:57 +09002107 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2108 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Tejun Heof15a1da2006-05-15 20:57:56 +09002110 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2111 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09002112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115/**
2116 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2117 * @ap: port on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002118 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 *
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002120 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2121 * ata_set_mode() fails, pointer to the failing device is
2122 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04002123 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002125 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002126 *
2127 * RETURNS:
2128 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09002130int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Tejun Heoe8e06192006-04-01 01:38:18 +09002132 struct ata_device *dev;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002133 int i, rc = 0, used_dma = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Tejun Heo3adcebb2006-05-15 20:57:37 +09002135 /* has private set_mode? */
2136 if (ap->ops->set_mode) {
2137 /* FIXME: make ->set_mode handle no device case and
2138 * return error code and failing device on failure.
2139 */
2140 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2141 if (ata_dev_enabled(&ap->device[i])) {
2142 ap->ops->set_mode(ap);
2143 break;
2144 }
2145 }
2146 return 0;
2147 }
2148
Tejun Heoa6d5a512006-03-06 04:31:57 +09002149 /* step 1: calculate xfer_mask */
2150 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002151 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002152
Tejun Heoe8e06192006-04-01 01:38:18 +09002153 dev = &ap->device[i];
2154
Tejun Heoe1211e32006-04-01 01:38:18 +09002155 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002156 continue;
2157
Tejun Heo3373efd2006-05-15 20:57:53 +09002158 ata_dev_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002159
Tejun Heoacf356b2006-03-24 14:07:50 +09002160 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2161 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2162 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2163 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002164
Tejun Heo4f659772006-04-01 01:38:18 +09002165 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002166 if (dev->dma_mode)
2167 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002168 }
Tejun Heo4f659772006-04-01 01:38:18 +09002169 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002170 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002171
2172 /* step 2: always set host PIO timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002173 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2174 dev = &ap->device[i];
2175 if (!ata_dev_enabled(dev))
2176 continue;
2177
2178 if (!dev->pio_mode) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002179 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09002180 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002181 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002182 }
2183
2184 dev->xfer_mode = dev->pio_mode;
2185 dev->xfer_shift = ATA_SHIFT_PIO;
2186 if (ap->ops->set_piomode)
2187 ap->ops->set_piomode(ap, dev);
2188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Tejun Heoa6d5a512006-03-06 04:31:57 +09002190 /* step 3: set host DMA timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002191 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2192 dev = &ap->device[i];
2193
2194 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2195 continue;
2196
2197 dev->xfer_mode = dev->dma_mode;
2198 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2199 if (ap->ops->set_dmamode)
2200 ap->ops->set_dmamode(ap, dev);
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* step 4: update devices' xfer mode */
Tejun Heo83206a22006-03-24 15:25:31 +09002204 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002205 dev = &ap->device[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Tejun Heoe1211e32006-04-01 01:38:18 +09002207 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002208 continue;
2209
Tejun Heo3373efd2006-05-15 20:57:53 +09002210 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002211 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002212 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Tejun Heoe8e06192006-04-01 01:38:18 +09002215 /* Record simplex status. If we selected DMA then the other
2216 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002217 */
Alan Cox5444a6f2006-03-27 18:58:20 +01002218 if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
2219 ap->host_set->simplex_claimed = 1;
2220
Tejun Heoe8e06192006-04-01 01:38:18 +09002221 /* step5: chip specific finalisation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (ap->ops->post_set_mode)
2223 ap->ops->post_set_mode(ap);
2224
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002225 out:
2226 if (rc)
2227 *r_failed_dev = dev;
2228 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002232 * ata_tf_to_host - issue ATA taskfile to host controller
2233 * @ap: port to which command is being issued
2234 * @tf: ATA taskfile register set
2235 *
2236 * Issues ATA taskfile register set to ATA host controller,
2237 * with proper synchronization with interrupt handler and
2238 * other threads.
2239 *
2240 * LOCKING:
2241 * spin_lock_irqsave(host_set lock)
2242 */
2243
2244static inline void ata_tf_to_host(struct ata_port *ap,
2245 const struct ata_taskfile *tf)
2246{
2247 ap->ops->tf_load(ap, tf);
2248 ap->ops->exec_command(ap, tf);
2249}
2250
2251/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 * ata_busy_sleep - sleep until BSY clears, or timeout
2253 * @ap: port containing status register to be polled
2254 * @tmout_pat: impatience timeout
2255 * @tmout: overall timeout
2256 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002257 * Sleep until ATA Status register bit BSY clears,
2258 * or a timeout occurs.
2259 *
2260 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 */
2262
Tejun Heo6f8b9952006-01-24 17:05:21 +09002263unsigned int ata_busy_sleep (struct ata_port *ap,
2264 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 unsigned long timer_start, timeout;
2267 u8 status;
2268
2269 status = ata_busy_wait(ap, ATA_BUSY, 300);
2270 timer_start = jiffies;
2271 timeout = timer_start + tmout_pat;
2272 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2273 msleep(50);
2274 status = ata_busy_wait(ap, ATA_BUSY, 3);
2275 }
2276
2277 if (status & ATA_BUSY)
Tejun Heof15a1da2006-05-15 20:57:56 +09002278 ata_port_printk(ap, KERN_WARNING,
2279 "port is slow to respond, please be patient\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 timeout = timer_start + tmout;
2282 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2283 msleep(50);
2284 status = ata_chk_status(ap);
2285 }
2286
2287 if (status & ATA_BUSY) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002288 ata_port_printk(ap, KERN_ERR, "port failed to respond "
2289 "(%lu secs)\n", tmout / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return 1;
2291 }
2292
2293 return 0;
2294}
2295
2296static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2297{
2298 struct ata_ioports *ioaddr = &ap->ioaddr;
2299 unsigned int dev0 = devmask & (1 << 0);
2300 unsigned int dev1 = devmask & (1 << 1);
2301 unsigned long timeout;
2302
2303 /* if device 0 was found in ata_devchk, wait for its
2304 * BSY bit to clear
2305 */
2306 if (dev0)
2307 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2308
2309 /* if device 1 was found in ata_devchk, wait for
2310 * register access, then wait for BSY to clear
2311 */
2312 timeout = jiffies + ATA_TMOUT_BOOT;
2313 while (dev1) {
2314 u8 nsect, lbal;
2315
2316 ap->ops->dev_select(ap, 1);
2317 if (ap->flags & ATA_FLAG_MMIO) {
2318 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2319 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2320 } else {
2321 nsect = inb(ioaddr->nsect_addr);
2322 lbal = inb(ioaddr->lbal_addr);
2323 }
2324 if ((nsect == 1) && (lbal == 1))
2325 break;
2326 if (time_after(jiffies, timeout)) {
2327 dev1 = 0;
2328 break;
2329 }
2330 msleep(50); /* give drive a breather */
2331 }
2332 if (dev1)
2333 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2334
2335 /* is all this really necessary? */
2336 ap->ops->dev_select(ap, 0);
2337 if (dev1)
2338 ap->ops->dev_select(ap, 1);
2339 if (dev0)
2340 ap->ops->dev_select(ap, 0);
2341}
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343static unsigned int ata_bus_softreset(struct ata_port *ap,
2344 unsigned int devmask)
2345{
2346 struct ata_ioports *ioaddr = &ap->ioaddr;
2347
2348 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2349
2350 /* software reset. causes dev0 to be selected */
2351 if (ap->flags & ATA_FLAG_MMIO) {
2352 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2353 udelay(20); /* FIXME: flush */
2354 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2355 udelay(20); /* FIXME: flush */
2356 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2357 } else {
2358 outb(ap->ctl, ioaddr->ctl_addr);
2359 udelay(10);
2360 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2361 udelay(10);
2362 outb(ap->ctl, ioaddr->ctl_addr);
2363 }
2364
2365 /* spec mandates ">= 2ms" before checking status.
2366 * We wait 150ms, because that was the magic delay used for
2367 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2368 * between when the ATA command register is written, and then
2369 * status is checked. Because waiting for "a while" before
2370 * checking status is fine, post SRST, we perform this magic
2371 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00002372 *
2373 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 */
2375 msleep(150);
2376
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002377 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09002378 * the bus shows 0xFF because the odd clown forgets the D7
2379 * pulldown resistor.
2380 */
Tejun Heo987d2f02006-04-11 22:16:45 +09002381 if (ata_check_status(ap) == 0xFF) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002382 ata_port_printk(ap, KERN_ERR, "SRST failed (status 0xFF)\n");
Tejun Heo298a41c2006-03-25 02:58:13 +09002383 return AC_ERR_OTHER;
Tejun Heo987d2f02006-04-11 22:16:45 +09002384 }
Alan Cox09c7ad72006-03-22 15:52:40 +00002385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 ata_bus_post_reset(ap, devmask);
2387
2388 return 0;
2389}
2390
2391/**
2392 * ata_bus_reset - reset host port and associated ATA channel
2393 * @ap: port to reset
2394 *
2395 * This is typically the first time we actually start issuing
2396 * commands to the ATA channel. We wait for BSY to clear, then
2397 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2398 * result. Determine what devices, if any, are on the channel
2399 * by looking at the device 0/1 error register. Look at the signature
2400 * stored in each device's taskfile registers, to determine if
2401 * the device is ATA or ATAPI.
2402 *
2403 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002404 * PCI/etc. bus probe sem.
2405 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 *
2407 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09002408 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 */
2410
2411void ata_bus_reset(struct ata_port *ap)
2412{
2413 struct ata_ioports *ioaddr = &ap->ioaddr;
2414 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2415 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002416 unsigned int dev0, dev1 = 0, devmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2419
2420 /* determine if device 0/1 are present */
2421 if (ap->flags & ATA_FLAG_SATA_RESET)
2422 dev0 = 1;
2423 else {
2424 dev0 = ata_devchk(ap, 0);
2425 if (slave_possible)
2426 dev1 = ata_devchk(ap, 1);
2427 }
2428
2429 if (dev0)
2430 devmask |= (1 << 0);
2431 if (dev1)
2432 devmask |= (1 << 1);
2433
2434 /* select device 0 again */
2435 ap->ops->dev_select(ap, 0);
2436
2437 /* issue bus reset */
2438 if (ap->flags & ATA_FLAG_SRST)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002439 if (ata_bus_softreset(ap, devmask))
2440 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 /*
2443 * determine by signature whether we have ATA or ATAPI devices
2444 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002445 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002447 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 /* re-enable interrupts */
2450 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2451 ata_irq_on(ap);
2452
2453 /* is double-select really necessary? */
2454 if (ap->device[1].class != ATA_DEV_NONE)
2455 ap->ops->dev_select(ap, 1);
2456 if (ap->device[0].class != ATA_DEV_NONE)
2457 ap->ops->dev_select(ap, 0);
2458
2459 /* if no devices were detected, disable this port */
2460 if ((ap->device[0].class == ATA_DEV_NONE) &&
2461 (ap->device[1].class == ATA_DEV_NONE))
2462 goto err_out;
2463
2464 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2465 /* set up device control for ATA_FLAG_SATA_RESET */
2466 if (ap->flags & ATA_FLAG_MMIO)
2467 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2468 else
2469 outb(ap->ctl, ioaddr->ctl_addr);
2470 }
2471
2472 DPRINTK("EXIT\n");
2473 return;
2474
2475err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09002476 ata_port_printk(ap, KERN_ERR, "disabling port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 ap->ops->port_disable(ap);
2478
2479 DPRINTK("EXIT\n");
2480}
2481
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002482/**
2483 * sata_phy_debounce - debounce SATA phy status
2484 * @ap: ATA port to debounce SATA phy status for
2485 * @params: timing parameters { interval, duratinon, timeout } in msec
2486 *
2487 * Make sure SStatus of @ap reaches stable state, determined by
2488 * holding the same value where DET is not 1 for @duration polled
2489 * every @interval, before @timeout. Timeout constraints the
2490 * beginning of the stable state. Because, after hot unplugging,
2491 * DET gets stuck at 1 on some controllers, this functions waits
2492 * until timeout then returns 0 if DET is stable at 1.
2493 *
2494 * LOCKING:
2495 * Kernel thread context (may sleep)
2496 *
2497 * RETURNS:
2498 * 0 on success, -errno on failure.
2499 */
2500int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
Tejun Heo7a7921e2006-02-02 18:20:00 +09002501{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002502 unsigned long interval_msec = params[0];
2503 unsigned long duration = params[1] * HZ / 1000;
2504 unsigned long timeout = jiffies + params[2] * HZ / 1000;
2505 unsigned long last_jiffies;
2506 u32 last, cur;
2507 int rc;
2508
2509 if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
2510 return rc;
2511 cur &= 0xf;
2512
2513 last = cur;
2514 last_jiffies = jiffies;
2515
2516 while (1) {
2517 msleep(interval_msec);
2518 if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
2519 return rc;
2520 cur &= 0xf;
2521
2522 /* DET stable? */
2523 if (cur == last) {
2524 if (cur == 1 && time_before(jiffies, timeout))
2525 continue;
2526 if (time_after(jiffies, last_jiffies + duration))
2527 return 0;
2528 continue;
2529 }
2530
2531 /* unstable, start over */
2532 last = cur;
2533 last_jiffies = jiffies;
2534
2535 /* check timeout */
2536 if (time_after(jiffies, timeout))
2537 return -EBUSY;
2538 }
2539}
2540
2541/**
2542 * sata_phy_resume - resume SATA phy
2543 * @ap: ATA port to resume SATA phy for
2544 * @params: timing parameters { interval, duratinon, timeout } in msec
2545 *
2546 * Resume SATA phy of @ap and debounce it.
2547 *
2548 * LOCKING:
2549 * Kernel thread context (may sleep)
2550 *
2551 * RETURNS:
2552 * 0 on success, -errno on failure.
2553 */
2554int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
2555{
2556 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002557 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09002558
Tejun Heo81952c52006-05-15 20:57:47 +09002559 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2560 return rc;
2561
Tejun Heo852ee162006-04-01 01:38:18 +09002562 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09002563
2564 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
2565 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09002566
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002567 /* Some PHYs react badly if SStatus is pounded immediately
2568 * after resuming. Delay 200ms before debouncing.
2569 */
2570 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09002571
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002572 return sata_phy_debounce(ap, params);
Tejun Heo7a7921e2006-02-02 18:20:00 +09002573}
2574
Tejun Heof5914a42006-05-31 18:27:48 +09002575static void ata_wait_spinup(struct ata_port *ap)
2576{
2577 struct ata_eh_context *ehc = &ap->eh_context;
2578 unsigned long end, secs;
2579 int rc;
2580
2581 /* first, debounce phy if SATA */
2582 if (ap->cbl == ATA_CBL_SATA) {
2583 rc = sata_phy_debounce(ap, sata_deb_timing_eh);
2584
2585 /* if debounced successfully and offline, no need to wait */
2586 if ((rc == 0 || rc == -EOPNOTSUPP) && ata_port_offline(ap))
2587 return;
2588 }
2589
2590 /* okay, let's give the drive time to spin up */
2591 end = ehc->i.hotplug_timestamp + ATA_SPINUP_WAIT * HZ / 1000;
2592 secs = ((end - jiffies) + HZ - 1) / HZ;
2593
2594 if (time_after(jiffies, end))
2595 return;
2596
2597 if (secs > 5)
2598 ata_port_printk(ap, KERN_INFO, "waiting for device to spin up "
2599 "(%lu secs)\n", secs);
2600
2601 schedule_timeout_uninterruptible(end - jiffies);
2602}
2603
2604/**
2605 * ata_std_prereset - prepare for reset
2606 * @ap: ATA port to be reset
2607 *
2608 * @ap is about to be reset. Initialize it.
2609 *
2610 * LOCKING:
2611 * Kernel thread context (may sleep)
2612 *
2613 * RETURNS:
2614 * 0 on success, -errno otherwise.
2615 */
2616int ata_std_prereset(struct ata_port *ap)
2617{
2618 struct ata_eh_context *ehc = &ap->eh_context;
2619 const unsigned long *timing;
2620 int rc;
2621
2622 /* hotplug? */
2623 if (ehc->i.flags & ATA_EHI_HOTPLUGGED) {
2624 if (ap->flags & ATA_FLAG_HRST_TO_RESUME)
2625 ehc->i.action |= ATA_EH_HARDRESET;
2626 if (ap->flags & ATA_FLAG_SKIP_D2H_BSY)
2627 ata_wait_spinup(ap);
2628 }
2629
2630 /* if we're about to do hardreset, nothing more to do */
2631 if (ehc->i.action & ATA_EH_HARDRESET)
2632 return 0;
2633
2634 /* if SATA, resume phy */
2635 if (ap->cbl == ATA_CBL_SATA) {
2636 if (ap->flags & ATA_FLAG_LOADING)
2637 timing = sata_deb_timing_boot;
2638 else
2639 timing = sata_deb_timing_eh;
2640
2641 rc = sata_phy_resume(ap, timing);
2642 if (rc && rc != -EOPNOTSUPP) {
2643 /* phy resume failed */
2644 ata_port_printk(ap, KERN_WARNING, "failed to resume "
2645 "link for reset (errno=%d)\n", rc);
2646 return rc;
2647 }
2648 }
2649
2650 /* Wait for !BSY if the controller can wait for the first D2H
2651 * Reg FIS and we don't know that no device is attached.
2652 */
2653 if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap))
2654 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2655
2656 return 0;
2657}
2658
Tejun Heoc2bd5802006-01-24 17:05:22 +09002659/**
2660 * ata_std_softreset - reset host port via ATA SRST
2661 * @ap: port to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09002662 * @classes: resulting classes of attached devices
2663 *
Tejun Heo52783c52006-05-31 18:28:22 +09002664 * Reset host port using ATA SRST.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002665 *
2666 * LOCKING:
2667 * Kernel thread context (may sleep)
2668 *
2669 * RETURNS:
2670 * 0 on success, -errno otherwise.
2671 */
Tejun Heo2bf2cb22006-04-11 22:16:45 +09002672int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002673{
2674 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2675 unsigned int devmask = 0, err_mask;
2676 u8 err;
2677
2678 DPRINTK("ENTER\n");
2679
Tejun Heo81952c52006-05-15 20:57:47 +09002680 if (ata_port_offline(ap)) {
Tejun Heo3a397462006-02-10 23:58:48 +09002681 classes[0] = ATA_DEV_NONE;
2682 goto out;
2683 }
2684
Tejun Heoc2bd5802006-01-24 17:05:22 +09002685 /* determine if device 0/1 are present */
2686 if (ata_devchk(ap, 0))
2687 devmask |= (1 << 0);
2688 if (slave_possible && ata_devchk(ap, 1))
2689 devmask |= (1 << 1);
2690
Tejun Heoc2bd5802006-01-24 17:05:22 +09002691 /* select device 0 again */
2692 ap->ops->dev_select(ap, 0);
2693
2694 /* issue bus reset */
2695 DPRINTK("about to softreset, devmask=%x\n", devmask);
2696 err_mask = ata_bus_softreset(ap, devmask);
2697 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002698 ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
2699 err_mask);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002700 return -EIO;
2701 }
2702
2703 /* determine by signature whether we have ATA or ATAPI devices */
2704 classes[0] = ata_dev_try_classify(ap, 0, &err);
2705 if (slave_possible && err != 0x81)
2706 classes[1] = ata_dev_try_classify(ap, 1, &err);
2707
Tejun Heo3a397462006-02-10 23:58:48 +09002708 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002709 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2710 return 0;
2711}
2712
2713/**
2714 * sata_std_hardreset - reset host port via SATA phy reset
2715 * @ap: port to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09002716 * @class: resulting class of attached device
2717 *
2718 * SATA phy-reset host port using DET bits of SControl register.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002719 *
2720 * LOCKING:
2721 * Kernel thread context (may sleep)
2722 *
2723 * RETURNS:
2724 * 0 on success, -errno otherwise.
2725 */
Tejun Heo2bf2cb22006-04-11 22:16:45 +09002726int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002727{
Tejun Heo852ee162006-04-01 01:38:18 +09002728 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002729 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09002730
Tejun Heoc2bd5802006-01-24 17:05:22 +09002731 DPRINTK("ENTER\n");
2732
Tejun Heo3c567b72006-05-15 20:57:23 +09002733 if (sata_set_spd_needed(ap)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09002734 /* SATA spec says nothing about how to reconfigure
2735 * spd. To be on the safe side, turn off phy during
2736 * reconfiguration. This works for at least ICH7 AHCI
2737 * and Sil3124.
2738 */
Tejun Heo81952c52006-05-15 20:57:47 +09002739 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2740 return rc;
2741
Tejun Heo1c3fae42006-04-02 20:53:28 +09002742 scontrol = (scontrol & 0x0f0) | 0x302;
Tejun Heo81952c52006-05-15 20:57:47 +09002743
2744 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
2745 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002746
Tejun Heo3c567b72006-05-15 20:57:23 +09002747 sata_set_spd(ap);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002748 }
2749
2750 /* issue phy wake/reset */
Tejun Heo81952c52006-05-15 20:57:47 +09002751 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2752 return rc;
2753
Tejun Heo852ee162006-04-01 01:38:18 +09002754 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09002755
2756 if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
2757 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09002758
Tejun Heo1c3fae42006-04-02 20:53:28 +09002759 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09002760 * 10.4.2 says at least 1 ms.
2761 */
2762 msleep(1);
2763
Tejun Heo1c3fae42006-04-02 20:53:28 +09002764 /* bring phy back */
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002765 sata_phy_resume(ap, sata_deb_timing_eh);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002766
Tejun Heoc2bd5802006-01-24 17:05:22 +09002767 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo81952c52006-05-15 20:57:47 +09002768 if (ata_port_offline(ap)) {
Tejun Heoc2bd5802006-01-24 17:05:22 +09002769 *class = ATA_DEV_NONE;
2770 DPRINTK("EXIT, link offline\n");
2771 return 0;
2772 }
2773
2774 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002775 ata_port_printk(ap, KERN_ERR,
2776 "COMRESET failed (device not ready)\n");
Tejun Heoc2bd5802006-01-24 17:05:22 +09002777 return -EIO;
2778 }
2779
Tejun Heo3a397462006-02-10 23:58:48 +09002780 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2781
Tejun Heoc2bd5802006-01-24 17:05:22 +09002782 *class = ata_dev_try_classify(ap, 0, NULL);
2783
2784 DPRINTK("EXIT, class=%u\n", *class);
2785 return 0;
2786}
2787
2788/**
2789 * ata_std_postreset - standard postreset callback
2790 * @ap: the target ata_port
2791 * @classes: classes of attached devices
2792 *
2793 * This function is invoked after a successful reset. Note that
2794 * the device might have been reset more than once using
2795 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002796 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09002797 * LOCKING:
2798 * Kernel thread context (may sleep)
2799 */
2800void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2801{
Tejun Heodc2b3512006-05-15 20:58:00 +09002802 u32 serror;
2803
Tejun Heoc2bd5802006-01-24 17:05:22 +09002804 DPRINTK("ENTER\n");
2805
Tejun Heoc2bd5802006-01-24 17:05:22 +09002806 /* print link status */
Tejun Heo81952c52006-05-15 20:57:47 +09002807 sata_print_link_status(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002808
Tejun Heodc2b3512006-05-15 20:58:00 +09002809 /* clear SError */
2810 if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
2811 sata_scr_write(ap, SCR_ERROR, serror);
2812
Tejun Heo3a397462006-02-10 23:58:48 +09002813 /* re-enable interrupts */
Tejun Heoe3180492006-05-15 20:58:09 +09002814 if (!ap->ops->error_handler) {
2815 /* FIXME: hack. create a hook instead */
2816 if (ap->ioaddr.ctl_addr)
2817 ata_irq_on(ap);
2818 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002819
2820 /* is double-select really necessary? */
2821 if (classes[0] != ATA_DEV_NONE)
2822 ap->ops->dev_select(ap, 1);
2823 if (classes[1] != ATA_DEV_NONE)
2824 ap->ops->dev_select(ap, 0);
2825
Tejun Heo3a397462006-02-10 23:58:48 +09002826 /* bail out if no device is present */
2827 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2828 DPRINTK("EXIT, no device\n");
2829 return;
2830 }
2831
2832 /* set up device control */
2833 if (ap->ioaddr.ctl_addr) {
2834 if (ap->flags & ATA_FLAG_MMIO)
2835 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2836 else
2837 outb(ap->ctl, ap->ioaddr.ctl_addr);
2838 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002839
2840 DPRINTK("EXIT\n");
2841}
2842
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002843/**
Tejun Heo623a3122006-03-05 17:55:58 +09002844 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09002845 * @dev: device to compare against
2846 * @new_class: class of the new device
2847 * @new_id: IDENTIFY page of the new device
2848 *
2849 * Compare @new_class and @new_id against @dev and determine
2850 * whether @dev is the device indicated by @new_class and
2851 * @new_id.
2852 *
2853 * LOCKING:
2854 * None.
2855 *
2856 * RETURNS:
2857 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2858 */
Tejun Heo3373efd2006-05-15 20:57:53 +09002859static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
2860 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09002861{
2862 const u16 *old_id = dev->id;
2863 unsigned char model[2][41], serial[2][21];
2864 u64 new_n_sectors;
2865
2866 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002867 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
2868 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09002869 return 0;
2870 }
2871
2872 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2873 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2874 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2875 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2876 new_n_sectors = ata_id_n_sectors(new_id);
2877
2878 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002879 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
2880 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09002881 return 0;
2882 }
2883
2884 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002885 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
2886 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09002887 return 0;
2888 }
2889
2890 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002891 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
2892 "%llu != %llu\n",
2893 (unsigned long long)dev->n_sectors,
2894 (unsigned long long)new_n_sectors);
Tejun Heo623a3122006-03-05 17:55:58 +09002895 return 0;
2896 }
2897
2898 return 1;
2899}
2900
2901/**
2902 * ata_dev_revalidate - Revalidate ATA device
Tejun Heo623a3122006-03-05 17:55:58 +09002903 * @dev: device to revalidate
2904 * @post_reset: is this revalidation after reset?
2905 *
2906 * Re-read IDENTIFY page and make sure @dev is still attached to
2907 * the port.
2908 *
2909 * LOCKING:
2910 * Kernel thread context (may sleep)
2911 *
2912 * RETURNS:
2913 * 0 on success, negative errno otherwise
2914 */
Tejun Heo3373efd2006-05-15 20:57:53 +09002915int ata_dev_revalidate(struct ata_device *dev, int post_reset)
Tejun Heo623a3122006-03-05 17:55:58 +09002916{
Tejun Heo5eb45c02006-04-02 18:51:52 +09002917 unsigned int class = dev->class;
Tejun Heof15a1da2006-05-15 20:57:56 +09002918 u16 *id = (void *)dev->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09002919 int rc;
2920
Tejun Heo5eb45c02006-04-02 18:51:52 +09002921 if (!ata_dev_enabled(dev)) {
2922 rc = -ENODEV;
2923 goto fail;
2924 }
Tejun Heo623a3122006-03-05 17:55:58 +09002925
Tejun Heofe635c72006-05-15 20:57:35 +09002926 /* read ID data */
Tejun Heo3373efd2006-05-15 20:57:53 +09002927 rc = ata_dev_read_id(dev, &class, post_reset, id);
Tejun Heo623a3122006-03-05 17:55:58 +09002928 if (rc)
2929 goto fail;
2930
2931 /* is the device still there? */
Tejun Heo3373efd2006-05-15 20:57:53 +09002932 if (!ata_dev_same_device(dev, class, id)) {
Tejun Heo623a3122006-03-05 17:55:58 +09002933 rc = -ENODEV;
2934 goto fail;
2935 }
2936
Tejun Heofe635c72006-05-15 20:57:35 +09002937 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo623a3122006-03-05 17:55:58 +09002938
2939 /* configure device according to the new ID */
Tejun Heo3373efd2006-05-15 20:57:53 +09002940 rc = ata_dev_configure(dev, 0);
Tejun Heo5eb45c02006-04-02 18:51:52 +09002941 if (rc == 0)
2942 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09002943
2944 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09002945 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09002946 return rc;
2947}
2948
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002949static const char * const ata_dma_blacklist [] = {
Alan Coxf4b15fe2006-03-22 15:54:04 +00002950 "WDC AC11000H", NULL,
2951 "WDC AC22100H", NULL,
2952 "WDC AC32500H", NULL,
2953 "WDC AC33100H", NULL,
2954 "WDC AC31600H", NULL,
2955 "WDC AC32100H", "24.09P07",
2956 "WDC AC23200L", "21.10N21",
2957 "Compaq CRD-8241B", NULL,
2958 "CRD-8400B", NULL,
2959 "CRD-8480B", NULL,
2960 "CRD-8482B", NULL,
2961 "CRD-84", NULL,
2962 "SanDisk SDP3B", NULL,
2963 "SanDisk SDP3B-64", NULL,
2964 "SANYO CD-ROM CRD", NULL,
2965 "HITACHI CDR-8", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002966 "HITACHI CDR-8335", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002967 "HITACHI CDR-8435", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002968 "Toshiba CD-ROM XM-6202B", NULL,
2969 "TOSHIBA CD-ROM XM-1702BC", NULL,
2970 "CD-532E-A", NULL,
2971 "E-IDE CD-ROM CR-840", NULL,
2972 "CD-ROM Drive/F5A", NULL,
2973 "WPI CDD-820", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002974 "SAMSUNG CD-ROM SC-148C", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002975 "SAMSUNG CD-ROM SC", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002976 "SanDisk SDP3B-64", NULL,
2977 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2978 "_NEC DV5800A", NULL,
2979 "SAMSUNG CD-ROM SN-124", "N001"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002981
Alan Coxf4b15fe2006-03-22 15:54:04 +00002982static int ata_strim(char *s, size_t len)
2983{
2984 len = strnlen(s, len);
2985
2986 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2987 while ((len > 0) && (s[len - 1] == ' ')) {
2988 len--;
2989 s[len] = 0;
2990 }
2991 return len;
2992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Jeff Garzik057ace52005-10-22 14:27:05 -04002994static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Alan Coxf4b15fe2006-03-22 15:54:04 +00002996 unsigned char model_num[40];
2997 unsigned char model_rev[16];
2998 unsigned int nlen, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 int i;
3000
Albert Lee3a778272006-06-22 13:00:25 +08003001 /* We don't support polling DMA.
3002 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3003 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3004 */
3005 if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
3006 (dev->flags & ATA_DFLAG_CDB_INTR))
3007 return 1;
3008
Alan Coxf4b15fe2006-03-22 15:54:04 +00003009 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
3010 sizeof(model_num));
3011 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
3012 sizeof(model_rev));
3013 nlen = ata_strim(model_num, sizeof(model_num));
3014 rlen = ata_strim(model_rev, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Alan Coxf4b15fe2006-03-22 15:54:04 +00003016 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
3017 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
3018 if (ata_dma_blacklist[i+1] == NULL)
3019 return 1;
3020 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
3021 return 1;
3022 }
3023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return 0;
3025}
3026
Tejun Heoa6d5a512006-03-06 04:31:57 +09003027/**
3028 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09003029 * @dev: Device to compute xfermask for
3030 *
Tejun Heoacf356b2006-03-24 14:07:50 +09003031 * Compute supported xfermask of @dev and store it in
3032 * dev->*_mask. This function is responsible for applying all
3033 * known limits including host controller limits, device
3034 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09003035 *
Tejun Heo600511e2006-03-25 11:14:07 +09003036 * FIXME: The current implementation limits all transfer modes to
3037 * the fastest of the lowested device on the port. This is not
Tejun Heo05c8e0a2006-03-25 14:28:57 +09003038 * required on most controllers.
Tejun Heo600511e2006-03-25 11:14:07 +09003039 *
Tejun Heoa6d5a512006-03-06 04:31:57 +09003040 * LOCKING:
3041 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09003042 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003043static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Tejun Heo3373efd2006-05-15 20:57:53 +09003045 struct ata_port *ap = dev->ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003046 struct ata_host_set *hs = ap->host_set;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003047 unsigned long xfer_mask;
3048 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Tejun Heo565083e2006-04-02 17:54:47 +09003050 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3051 ap->mwdma_mask, ap->udma_mask);
3052
3053 /* Apply cable rule here. Don't apply it early because when
3054 * we handle hot plug the cable type can itself change.
3055 */
3056 if (ap->cbl == ATA_CBL_PATA40)
3057 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Alan Cox5444a6f2006-03-27 18:58:20 +01003059 /* FIXME: Use port-wide xfermask for now */
Tejun Heoa6d5a512006-03-06 04:31:57 +09003060 for (i = 0; i < ATA_MAX_DEVICES; i++) {
3061 struct ata_device *d = &ap->device[i];
Tejun Heo565083e2006-04-02 17:54:47 +09003062
3063 if (ata_dev_absent(d))
Tejun Heoa6d5a512006-03-06 04:31:57 +09003064 continue;
Tejun Heo565083e2006-04-02 17:54:47 +09003065
3066 if (ata_dev_disabled(d)) {
3067 /* to avoid violating device selection timing */
3068 xfer_mask &= ata_pack_xfermask(d->pio_mask,
3069 UINT_MAX, UINT_MAX);
3070 continue;
3071 }
3072
3073 xfer_mask &= ata_pack_xfermask(d->pio_mask,
3074 d->mwdma_mask, d->udma_mask);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003075 xfer_mask &= ata_id_xfermask(d->id);
3076 if (ata_dma_blacklisted(d))
3077 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079
Tejun Heoa6d5a512006-03-06 04:31:57 +09003080 if (ata_dma_blacklisted(dev))
Tejun Heof15a1da2006-05-15 20:57:56 +09003081 ata_dev_printk(dev, KERN_WARNING,
3082 "device is on DMA blacklist, disabling DMA\n");
Tejun Heoa6d5a512006-03-06 04:31:57 +09003083
Alan Cox5444a6f2006-03-27 18:58:20 +01003084 if (hs->flags & ATA_HOST_SIMPLEX) {
3085 if (hs->simplex_claimed)
3086 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3087 }
Tejun Heo565083e2006-04-02 17:54:47 +09003088
Alan Cox5444a6f2006-03-27 18:58:20 +01003089 if (ap->ops->mode_filter)
3090 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
3091
Tejun Heo565083e2006-04-02 17:54:47 +09003092 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
3093 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 * @dev: Device to which command will be sent
3099 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003100 * Issue SET FEATURES - XFER MODE command to device @dev
3101 * on port @ap.
3102 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003104 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09003105 *
3106 * RETURNS:
3107 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 */
3109
Tejun Heo3373efd2006-05-15 20:57:53 +09003110static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Tejun Heoa0123702005-12-13 14:49:31 +09003112 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09003113 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 /* set up set-features taskfile */
3116 DPRINTK("set features - xfer mode\n");
3117
Tejun Heo3373efd2006-05-15 20:57:53 +09003118 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09003119 tf.command = ATA_CMD_SET_FEATURES;
3120 tf.feature = SETFEATURES_XFER;
3121 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3122 tf.protocol = ATA_PROT_NODATA;
3123 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Tejun Heo3373efd2006-05-15 20:57:53 +09003125 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Tejun Heo83206a22006-03-24 15:25:31 +09003127 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3128 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
3131/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04003132 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ec2005-05-12 15:29:42 -04003133 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07003134 * @heads: Number of heads (taskfile parameter)
3135 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ec2005-05-12 15:29:42 -04003136 *
3137 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09003138 * Kernel thread context (may sleep)
3139 *
3140 * RETURNS:
3141 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04003142 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003143static unsigned int ata_dev_init_params(struct ata_device *dev,
3144 u16 heads, u16 sectors)
Albert Lee8bf62ec2005-05-12 15:29:42 -04003145{
Tejun Heoa0123702005-12-13 14:49:31 +09003146 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09003147 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003148
3149 /* Number of sectors per track 1-255. Number of heads 1-16 */
3150 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08003151 return AC_ERR_INVALID;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003152
3153 /* set up init dev params taskfile */
3154 DPRINTK("init dev params \n");
3155
Tejun Heo3373efd2006-05-15 20:57:53 +09003156 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09003157 tf.command = ATA_CMD_INIT_DEV_PARAMS;
3158 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3159 tf.protocol = ATA_PROT_NODATA;
3160 tf.nsect = sectors;
3161 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04003162
Tejun Heo3373efd2006-05-15 20:57:53 +09003163 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04003164
Tejun Heo6aff8f12006-02-15 18:24:09 +09003165 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3166 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003167}
3168
3169/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003170 * ata_sg_clean - Unmap DMA memory associated with command
3171 * @qc: Command containing DMA memory to be released
3172 *
3173 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 *
3175 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003176 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 */
3178
3179static void ata_sg_clean(struct ata_queued_cmd *qc)
3180{
3181 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003182 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003184 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Tejun Heoa4631472006-02-11 19:11:13 +09003186 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
3187 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05003190 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003192 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003194 /* if we padded the buffer out to 32-bit bound, and data
3195 * xfer direction is from-device, we must copy from the
3196 * pad buffer back into the supplied buffer
3197 */
3198 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
3199 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3200
3201 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05003202 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003203 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003204 /* restore last sg */
3205 sg[qc->orig_n_elem - 1].length += qc->pad_len;
3206 if (pad_buf) {
3207 struct scatterlist *psg = &qc->pad_sgent;
3208 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3209 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003210 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003211 }
3212 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09003213 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003214 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05003215 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
3216 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003217 /* restore sg */
3218 sg->length += qc->pad_len;
3219 if (pad_buf)
3220 memcpy(qc->buf_virt + sg->length - qc->pad_len,
3221 pad_buf, qc->pad_len);
3222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003225 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
3228/**
3229 * ata_fill_sg - Fill PCI IDE PRD table
3230 * @qc: Metadata associated with taskfile to be transferred
3231 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003232 * Fill PCI IDE PRD (scatter-gather) table with segments
3233 * associated with the current disk command.
3234 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04003236 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 *
3238 */
3239static void ata_fill_sg(struct ata_queued_cmd *qc)
3240{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003242 struct scatterlist *sg;
3243 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Tejun Heoa4631472006-02-11 19:11:13 +09003245 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05003246 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
3248 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003249 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 u32 addr, offset;
3251 u32 sg_len, len;
3252
3253 /* determine if physical DMA addr spans 64K boundary.
3254 * Note h/w doesn't support 64-bit, so we unconditionally
3255 * truncate dma_addr_t to u32.
3256 */
3257 addr = (u32) sg_dma_address(sg);
3258 sg_len = sg_dma_len(sg);
3259
3260 while (sg_len) {
3261 offset = addr & 0xffff;
3262 len = sg_len;
3263 if ((offset + sg_len) > 0x10000)
3264 len = 0x10000 - offset;
3265
3266 ap->prd[idx].addr = cpu_to_le32(addr);
3267 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
3268 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
3269
3270 idx++;
3271 sg_len -= len;
3272 addr += len;
3273 }
3274 }
3275
3276 if (idx)
3277 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3278}
3279/**
3280 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3281 * @qc: Metadata associated with taskfile to check
3282 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003283 * Allow low-level driver to filter ATA PACKET commands, returning
3284 * a status indicating whether or not it is OK to use DMA for the
3285 * supplied PACKET command.
3286 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003288 * spin_lock_irqsave(host_set lock)
3289 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 * RETURNS: 0 when ATAPI DMA can be used
3291 * nonzero otherwise
3292 */
3293int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3294{
3295 struct ata_port *ap = qc->ap;
3296 int rc = 0; /* Assume ATAPI DMA is OK by default */
3297
3298 if (ap->ops->check_atapi_dma)
3299 rc = ap->ops->check_atapi_dma(qc);
3300
3301 return rc;
3302}
3303/**
3304 * ata_qc_prep - Prepare taskfile for submission
3305 * @qc: Metadata associated with taskfile to be prepared
3306 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003307 * Prepare ATA taskfile for submission.
3308 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 * LOCKING:
3310 * spin_lock_irqsave(host_set lock)
3311 */
3312void ata_qc_prep(struct ata_queued_cmd *qc)
3313{
3314 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3315 return;
3316
3317 ata_fill_sg(qc);
3318}
3319
Brian Kinge46834c2006-03-17 17:04:03 -06003320void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
3321
Jeff Garzik0cba6322005-05-30 19:49:12 -04003322/**
3323 * ata_sg_init_one - Associate command with memory buffer
3324 * @qc: Command to be associated
3325 * @buf: Memory buffer
3326 * @buflen: Length of memory buffer, in bytes.
3327 *
3328 * Initialize the data-related elements of queued_cmd @qc
3329 * to point to a single memory buffer, @buf of byte length @buflen.
3330 *
3331 * LOCKING:
3332 * spin_lock_irqsave(host_set lock)
3333 */
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3336{
3337 struct scatterlist *sg;
3338
3339 qc->flags |= ATA_QCFLAG_SINGLE;
3340
3341 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003342 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003344 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 qc->buf_virt = buf;
Brian King233277c2006-06-07 11:25:31 -05003346 qc->nbytes = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003348 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05003349 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}
3351
Jeff Garzik0cba6322005-05-30 19:49:12 -04003352/**
3353 * ata_sg_init - Associate command with scatter-gather table.
3354 * @qc: Command to be associated
3355 * @sg: Scatter-gather table.
3356 * @n_elem: Number of elements in s/g table.
3357 *
3358 * Initialize the data-related elements of queued_cmd @qc
3359 * to point to a scatter-gather table @sg, containing @n_elem
3360 * elements.
3361 *
3362 * LOCKING:
3363 * spin_lock_irqsave(host_set lock)
3364 */
3365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3367 unsigned int n_elem)
3368{
3369 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003370 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003372 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
3375/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003376 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3377 * @qc: Command with memory buffer to be mapped.
3378 *
3379 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 *
3381 * LOCKING:
3382 * spin_lock_irqsave(host_set lock)
3383 *
3384 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003385 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 */
3387
3388static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3389{
3390 struct ata_port *ap = qc->ap;
3391 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003392 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003394 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003396 /* we must lengthen transfers to end on a 32-bit boundary */
3397 qc->pad_len = sg->length & 3;
3398 if (qc->pad_len) {
3399 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3400 struct scatterlist *psg = &qc->pad_sgent;
3401
Tejun Heoa4631472006-02-11 19:11:13 +09003402 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003403
3404 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3405
3406 if (qc->tf.flags & ATA_TFLAG_WRITE)
3407 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3408 qc->pad_len);
3409
3410 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3411 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3412 /* trim sg */
3413 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003414 if (sg->length == 0)
3415 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003416
3417 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3418 sg->length, qc->pad_len);
3419 }
3420
Tejun Heo2e242fa2006-02-20 23:48:38 +09003421 if (trim_sg) {
3422 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003423 goto skip_map;
3424 }
3425
Brian King2f1f6102006-03-23 17:30:15 -06003426 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003427 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003428 if (dma_mapping_error(dma_address)) {
3429 /* restore sg */
3430 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
3434 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003435 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Tejun Heo2e242fa2006-02-20 23:48:38 +09003437skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3439 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3440
3441 return 0;
3442}
3443
3444/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003445 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3446 * @qc: Command with scatter-gather table to be mapped.
3447 *
3448 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 *
3450 * LOCKING:
3451 * spin_lock_irqsave(host_set lock)
3452 *
3453 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003454 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 *
3456 */
3457
3458static int ata_sg_setup(struct ata_queued_cmd *qc)
3459{
3460 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003461 struct scatterlist *sg = qc->__sg;
3462 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003463 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
3465 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003466 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003468 /* we must lengthen transfers to end on a 32-bit boundary */
3469 qc->pad_len = lsg->length & 3;
3470 if (qc->pad_len) {
3471 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3472 struct scatterlist *psg = &qc->pad_sgent;
3473 unsigned int offset;
3474
Tejun Heoa4631472006-02-11 19:11:13 +09003475 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003476
3477 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3478
3479 /*
3480 * psg->page/offset are used to copy to-be-written
3481 * data in this function or read data in ata_sg_clean.
3482 */
3483 offset = lsg->offset + lsg->length - qc->pad_len;
3484 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3485 psg->offset = offset_in_page(offset);
3486
3487 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3488 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3489 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003490 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003491 }
3492
3493 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3494 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3495 /* trim last sg */
3496 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003497 if (lsg->length == 0)
3498 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003499
3500 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3501 qc->n_elem - 1, lsg->length, qc->pad_len);
3502 }
3503
Jeff Garzike1410f22005-11-14 14:06:26 -05003504 pre_n_elem = qc->n_elem;
3505 if (trim_sg && pre_n_elem)
3506 pre_n_elem--;
3507
3508 if (!pre_n_elem) {
3509 n_elem = 0;
3510 goto skip_map;
3511 }
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06003514 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003515 if (n_elem < 1) {
3516 /* restore last sg */
3517 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
3521 DPRINTK("%d sg elements mapped\n", n_elem);
3522
Jeff Garzike1410f22005-11-14 14:06:26 -05003523skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 qc->n_elem = n_elem;
3525
3526 return 0;
3527}
3528
3529/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003530 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003531 * @buf: Buffer to swap
3532 * @buf_words: Number of 16-bit words in buffer.
3533 *
3534 * Swap halves of 16-bit words if needed to convert from
3535 * little-endian byte order to native cpu byte order, or
3536 * vice-versa.
3537 *
3538 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003539 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541void swap_buf_le16(u16 *buf, unsigned int buf_words)
3542{
3543#ifdef __BIG_ENDIAN
3544 unsigned int i;
3545
3546 for (i = 0; i < buf_words; i++)
3547 buf[i] = le16_to_cpu(buf[i]);
3548#endif /* __BIG_ENDIAN */
3549}
3550
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003551/**
3552 * ata_mmio_data_xfer - Transfer data by MMIO
Jeff Garzikbf717b112006-06-13 20:27:03 -04003553 * @adev: device for this I/O
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003554 * @buf: data buffer
3555 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003556 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003557 *
3558 * Transfer data from/to the device data register by MMIO.
3559 *
3560 * LOCKING:
3561 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003562 */
3563
Tejun Heo88574552006-06-25 20:00:35 +09003564void ata_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003565 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003567 struct ata_port *ap = adev->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 unsigned int i;
3569 unsigned int words = buflen >> 1;
3570 u16 *buf16 = (u16 *) buf;
3571 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3572
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003573 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 if (write_data) {
3575 for (i = 0; i < words; i++)
3576 writew(le16_to_cpu(buf16[i]), mmio);
3577 } else {
3578 for (i = 0; i < words; i++)
3579 buf16[i] = cpu_to_le16(readw(mmio));
3580 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003581
3582 /* Transfer trailing 1 byte, if any. */
3583 if (unlikely(buflen & 0x01)) {
3584 u16 align_buf[1] = { 0 };
3585 unsigned char *trailing_buf = buf + buflen - 1;
3586
3587 if (write_data) {
3588 memcpy(align_buf, trailing_buf, 1);
3589 writew(le16_to_cpu(align_buf[0]), mmio);
3590 } else {
3591 align_buf[0] = cpu_to_le16(readw(mmio));
3592 memcpy(trailing_buf, align_buf, 1);
3593 }
3594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003597/**
3598 * ata_pio_data_xfer - Transfer data by PIO
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003599 * @adev: device to target
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003600 * @buf: data buffer
3601 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003602 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003603 *
3604 * Transfer data from/to the device data register by PIO.
3605 *
3606 * LOCKING:
3607 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003608 */
3609
Tejun Heo88574552006-06-25 20:00:35 +09003610void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf,
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003611 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003613 struct ata_port *ap = adev->ap;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003614 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003616 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003618 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003620 insw(ap->ioaddr.data_addr, buf, words);
3621
3622 /* Transfer trailing 1 byte, if any. */
3623 if (unlikely(buflen & 0x01)) {
3624 u16 align_buf[1] = { 0 };
3625 unsigned char *trailing_buf = buf + buflen - 1;
3626
3627 if (write_data) {
3628 memcpy(align_buf, trailing_buf, 1);
3629 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3630 } else {
3631 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3632 memcpy(trailing_buf, align_buf, 1);
3633 }
3634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635}
3636
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003637/**
Alan Cox75e99582006-05-24 14:14:41 +01003638 * ata_pio_data_xfer_noirq - Transfer data by PIO
3639 * @adev: device to target
3640 * @buf: data buffer
3641 * @buflen: buffer length
3642 * @write_data: read/write
3643 *
Tejun Heo88574552006-06-25 20:00:35 +09003644 * Transfer data from/to the device data register by PIO. Do the
Alan Cox75e99582006-05-24 14:14:41 +01003645 * transfer with interrupts disabled.
3646 *
3647 * LOCKING:
3648 * Inherited from caller.
3649 */
3650
3651void ata_pio_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
3652 unsigned int buflen, int write_data)
3653{
3654 unsigned long flags;
3655 local_irq_save(flags);
3656 ata_pio_data_xfer(adev, buf, buflen, write_data);
3657 local_irq_restore(flags);
3658}
3659
3660
3661/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003662 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3663 * @qc: Command on going
3664 *
3665 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3666 *
3667 * LOCKING:
3668 * Inherited from caller.
3669 */
3670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671static void ata_pio_sector(struct ata_queued_cmd *qc)
3672{
3673 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003674 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 struct ata_port *ap = qc->ap;
3676 struct page *page;
3677 unsigned int offset;
3678 unsigned char *buf;
3679
3680 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003681 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
3683 page = sg[qc->cursg].page;
3684 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3685
3686 /* get the current page and offset */
3687 page = nth_page(page, (offset >> PAGE_SHIFT));
3688 offset %= PAGE_SIZE;
3689
Albert Lee7282aa42005-10-09 09:46:07 -04003690 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3691
Albert Lee91b8b312005-10-09 09:48:44 -04003692 if (PageHighMem(page)) {
3693 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003694
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003695 /* FIXME: use a bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04003696 local_irq_save(flags);
3697 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003698
Albert Lee91b8b312005-10-09 09:48:44 -04003699 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003700 ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04003701
3702 kunmap_atomic(buf, KM_IRQ0);
3703 local_irq_restore(flags);
3704 } else {
3705 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003706 ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04003707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 qc->cursect++;
3710 qc->cursg_ofs++;
3711
Albert Lee32529e02005-05-26 03:49:42 -04003712 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 qc->cursg++;
3714 qc->cursg_ofs = 0;
3715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003718/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003719 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3720 * @qc: Command on going
3721 *
Jeff Garzikc81e29b2006-05-24 01:49:12 -04003722 * Transfer one or many ATA_SECT_SIZE of data from/to the
Albert Lee07f6f7d2005-11-01 19:33:20 +08003723 * ATA device for the DRQ request.
3724 *
3725 * LOCKING:
3726 * Inherited from caller.
3727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Albert Lee07f6f7d2005-11-01 19:33:20 +08003729static void ata_pio_sectors(struct ata_queued_cmd *qc)
3730{
3731 if (is_multi_taskfile(&qc->tf)) {
3732 /* READ/WRITE MULTIPLE */
3733 unsigned int nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Jeff Garzik587005d2006-02-11 18:17:32 -05003735 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08003736
3737 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3738 while (nsect--)
3739 ata_pio_sector(qc);
3740 } else
3741 ata_pio_sector(qc);
3742}
3743
3744/**
Albert Leec71c1852005-10-04 06:03:45 -04003745 * atapi_send_cdb - Write CDB bytes to hardware
3746 * @ap: Port to which ATAPI device is attached.
3747 * @qc: Taskfile currently active
3748 *
3749 * When device has indicated its readiness to accept
3750 * a CDB, this function is called. Send the CDB.
3751 *
3752 * LOCKING:
3753 * caller.
3754 */
3755
3756static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3757{
3758 /* send SCSI cdb */
3759 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05003760 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04003761
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003762 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04003763 ata_altstatus(ap); /* flush */
3764
3765 switch (qc->tf.protocol) {
3766 case ATA_PROT_ATAPI:
3767 ap->hsm_task_state = HSM_ST;
3768 break;
3769 case ATA_PROT_ATAPI_NODATA:
3770 ap->hsm_task_state = HSM_ST_LAST;
3771 break;
3772 case ATA_PROT_ATAPI_DMA:
3773 ap->hsm_task_state = HSM_ST_LAST;
3774 /* initiate bmdma */
3775 ap->ops->bmdma_start(qc);
3776 break;
3777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778}
3779
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003780/**
3781 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3782 * @qc: Command on going
3783 * @bytes: number of bytes
3784 *
3785 * Transfer Transfer data from/to the ATAPI device.
3786 *
3787 * LOCKING:
3788 * Inherited from caller.
3789 *
3790 */
3791
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3793{
3794 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003795 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 struct ata_port *ap = qc->ap;
3797 struct page *page;
3798 unsigned char *buf;
3799 unsigned int offset, count;
3800
Albert Lee563a6e12005-08-12 14:17:50 +08003801 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003802 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
3804next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003805 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003806 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003807 * The end of qc->sg is reached and the device expects
3808 * more data to transfer. In order not to overrun qc->sg
3809 * and fulfill length specified in the byte count register,
3810 * - for read case, discard trailing data from the device
3811 * - for write case, padding zero data to the device
3812 */
3813 u16 pad_buf[1] = { 0 };
3814 unsigned int words = bytes >> 1;
3815 unsigned int i;
3816
3817 if (words) /* warning if bytes > 1 */
Tejun Heof15a1da2006-05-15 20:57:56 +09003818 ata_dev_printk(qc->dev, KERN_WARNING,
3819 "%u bytes trailing data\n", bytes);
Albert Lee563a6e12005-08-12 14:17:50 +08003820
3821 for (i = 0; i < words; i++)
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003822 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
Albert Lee563a6e12005-08-12 14:17:50 +08003823
Albert Lee14be71f2005-09-27 17:36:35 +08003824 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003825 return;
3826 }
3827
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003828 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 page = sg->page;
3831 offset = sg->offset + qc->cursg_ofs;
3832
3833 /* get the current page and offset */
3834 page = nth_page(page, (offset >> PAGE_SHIFT));
3835 offset %= PAGE_SIZE;
3836
Albert Lee6952df02005-06-06 15:56:03 +08003837 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003838 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 /* don't cross page boundaries */
3841 count = min(count, (unsigned int)PAGE_SIZE - offset);
3842
Albert Lee7282aa42005-10-09 09:46:07 -04003843 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3844
Albert Lee91b8b312005-10-09 09:48:44 -04003845 if (PageHighMem(page)) {
3846 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003847
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003848 /* FIXME: use bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04003849 local_irq_save(flags);
3850 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003851
Albert Lee91b8b312005-10-09 09:48:44 -04003852 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003853 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04003854
3855 kunmap_atomic(buf, KM_IRQ0);
3856 local_irq_restore(flags);
3857 } else {
3858 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003859 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04003860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862 bytes -= count;
3863 qc->curbytes += count;
3864 qc->cursg_ofs += count;
3865
Albert Lee32529e02005-05-26 03:49:42 -04003866 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 qc->cursg++;
3868 qc->cursg_ofs = 0;
3869 }
3870
Albert Lee563a6e12005-08-12 14:17:50 +08003871 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003875/**
3876 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3877 * @qc: Command on going
3878 *
3879 * Transfer Transfer data from/to the ATAPI device.
3880 *
3881 * LOCKING:
3882 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003883 */
3884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3886{
3887 struct ata_port *ap = qc->ap;
3888 struct ata_device *dev = qc->dev;
3889 unsigned int ireason, bc_lo, bc_hi, bytes;
3890 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3891
Albert Leeeec4c3f2006-05-18 17:51:10 +08003892 /* Abuse qc->result_tf for temp storage of intermediate TF
3893 * here to save some kernel stack usage.
3894 * For normal completion, qc->result_tf is not relevant. For
3895 * error, qc->result_tf is later overwritten by ata_qc_complete().
3896 * So, the correctness of qc->result_tf is not affected.
3897 */
3898 ap->ops->tf_read(ap, &qc->result_tf);
3899 ireason = qc->result_tf.nsect;
3900 bc_lo = qc->result_tf.lbam;
3901 bc_hi = qc->result_tf.lbah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 bytes = (bc_hi << 8) | bc_lo;
3903
3904 /* shall be cleared to zero, indicating xfer of data */
3905 if (ireason & (1 << 0))
3906 goto err_out;
3907
3908 /* make sure transfer direction matches expected */
3909 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3910 if (do_write != i_write)
3911 goto err_out;
3912
Albert Lee312f7da2005-09-27 17:38:03 +08003913 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 __atapi_pio_bytes(qc, bytes);
3916
3917 return;
3918
3919err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09003920 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
Tejun Heo11a56d22006-01-23 13:09:36 +09003921 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003922 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923}
3924
3925/**
Albert Leec234fb02006-03-25 17:58:38 +08003926 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
3927 * @ap: the target ata_port
3928 * @qc: qc on going
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 *
Albert Leec234fb02006-03-25 17:58:38 +08003930 * RETURNS:
3931 * 1 if ok in workqueue, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 */
Albert Leec234fb02006-03-25 17:58:38 +08003933
3934static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
Albert Leec234fb02006-03-25 17:58:38 +08003936 if (qc->tf.flags & ATA_TFLAG_POLLING)
3937 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
Albert Leec234fb02006-03-25 17:58:38 +08003939 if (ap->hsm_task_state == HSM_ST_FIRST) {
3940 if (qc->tf.protocol == ATA_PROT_PIO &&
3941 (qc->tf.flags & ATA_TFLAG_WRITE))
3942 return 1;
3943
3944 if (is_atapi_taskfile(&qc->tf) &&
3945 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3946 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 }
3948
Albert Leec234fb02006-03-25 17:58:38 +08003949 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
Albert Leec234fb02006-03-25 17:58:38 +08003952/**
Tejun Heoc17ea202006-05-15 20:59:29 +09003953 * ata_hsm_qc_complete - finish a qc running on standard HSM
3954 * @qc: Command to complete
3955 * @in_wq: 1 if called from workqueue, 0 otherwise
3956 *
3957 * Finish @qc which is running on standard HSM.
3958 *
3959 * LOCKING:
3960 * If @in_wq is zero, spin_lock_irqsave(host_set lock).
3961 * Otherwise, none on entry and grabs host lock.
3962 */
3963static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
3964{
3965 struct ata_port *ap = qc->ap;
3966 unsigned long flags;
3967
3968 if (ap->ops->error_handler) {
3969 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04003970 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09003971
3972 /* EH might have kicked in while host_set lock
3973 * is released.
3974 */
3975 qc = ata_qc_from_tag(ap, qc->tag);
3976 if (qc) {
3977 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
3978 ata_irq_on(ap);
3979 ata_qc_complete(qc);
3980 } else
3981 ata_port_freeze(ap);
3982 }
3983
Jeff Garzikba6a1302006-06-22 23:46:10 -04003984 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09003985 } else {
3986 if (likely(!(qc->err_mask & AC_ERR_HSM)))
3987 ata_qc_complete(qc);
3988 else
3989 ata_port_freeze(ap);
3990 }
3991 } else {
3992 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04003993 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09003994 ata_irq_on(ap);
3995 ata_qc_complete(qc);
Jeff Garzikba6a1302006-06-22 23:46:10 -04003996 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09003997 } else
3998 ata_qc_complete(qc);
3999 }
Jeff Garzikc81e29b2006-05-24 01:49:12 -04004000
4001 ata_altstatus(ap); /* flush */
Tejun Heoc17ea202006-05-15 20:59:29 +09004002}
4003
4004/**
Albert Leebb5cb292006-03-25 17:48:02 +08004005 * ata_hsm_move - move the HSM to the next state.
4006 * @ap: the target ata_port
4007 * @qc: qc on going
4008 * @status: current device status
4009 * @in_wq: 1 if called from workqueue, 0 otherwise
4010 *
4011 * RETURNS:
4012 * 1 when poll next status needed, 0 otherwise.
4013 */
Tejun Heo9a1004d2006-05-31 18:27:52 +09004014int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
4015 u8 status, int in_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
Albert Leebb5cb292006-03-25 17:48:02 +08004017 unsigned long flags = 0;
4018 int poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
Albert Lee6912ccd2006-03-25 17:45:49 +08004020 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Lee0565c262006-02-13 18:55:25 +08004021
Albert Leebb5cb292006-03-25 17:48:02 +08004022 /* Make sure ata_qc_issue_prot() does not throw things
4023 * like DMA polling into the workqueue. Notice that
4024 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
Albert Lee1c848982005-12-05 15:40:15 +08004025 */
Albert Leec234fb02006-03-25 17:58:38 +08004026 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Lee1c848982005-12-05 15:40:15 +08004027
Albert Leee2cec772006-03-25 17:43:49 +08004028fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08004029 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4030 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Albert Leee2cec772006-03-25 17:43:49 +08004032 switch (ap->hsm_task_state) {
4033 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08004034 /* Send first data block or PACKET CDB */
4035
4036 /* If polling, we will stay in the work queue after
4037 * sending the data. Otherwise, interrupt handler
4038 * takes over after sending the data.
4039 */
4040 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
4041
Albert Leee2cec772006-03-25 17:43:49 +08004042 /* check device status */
Albert Lee3655d1d2006-05-19 11:43:04 +08004043 if (unlikely((status & ATA_DRQ) == 0)) {
4044 /* handle BSY=0, DRQ=0 as error */
4045 if (likely(status & (ATA_ERR | ATA_DF)))
4046 /* device stops HSM for abort/error */
4047 qc->err_mask |= AC_ERR_DEV;
4048 else
4049 /* HSM violation. Let EH handle this */
4050 qc->err_mask |= AC_ERR_HSM;
4051
Albert Leee2cec772006-03-25 17:43:49 +08004052 ap->hsm_task_state = HSM_ST_ERR;
4053 goto fsm_start;
4054 }
4055
Albert Lee71601952006-03-25 18:11:12 +08004056 /* Device should not ask for data transfer (DRQ=1)
4057 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08004058 * We ignore DRQ here and stop the HSM by
4059 * changing hsm_task_state to HSM_ST_ERR and
4060 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08004061 */
4062 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4063 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
4064 ap->id, status);
Albert Lee3655d1d2006-05-19 11:43:04 +08004065 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08004066 ap->hsm_task_state = HSM_ST_ERR;
4067 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004068 }
4069
Albert Leebb5cb292006-03-25 17:48:02 +08004070 /* Send the CDB (atapi) or the first data block (ata pio out).
4071 * During the state transition, interrupt handler shouldn't
4072 * be invoked before the data transfer is complete and
4073 * hsm_task_state is changed. Hence, the following locking.
4074 */
4075 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04004076 spin_lock_irqsave(ap->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08004077
Albert Leebb5cb292006-03-25 17:48:02 +08004078 if (qc->tf.protocol == ATA_PROT_PIO) {
4079 /* PIO data out protocol.
4080 * send first data block.
4081 */
4082
4083 /* ata_pio_sectors() might change the state
4084 * to HSM_ST_LAST. so, the state is changed here
4085 * before ata_pio_sectors().
4086 */
4087 ap->hsm_task_state = HSM_ST;
4088 ata_pio_sectors(qc);
4089 ata_altstatus(ap); /* flush */
4090 } else
4091 /* send CDB */
4092 atapi_send_cdb(ap, qc);
4093
4094 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04004095 spin_unlock_irqrestore(ap->lock, flags);
Albert Leebb5cb292006-03-25 17:48:02 +08004096
4097 /* if polling, ata_pio_task() handles the rest.
4098 * otherwise, interrupt handler takes over from here.
4099 */
Albert Leee2cec772006-03-25 17:43:49 +08004100 break;
4101
4102 case HSM_ST:
4103 /* complete command or read/write the data register */
4104 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4105 /* ATAPI PIO protocol */
4106 if ((status & ATA_DRQ) == 0) {
Albert Lee3655d1d2006-05-19 11:43:04 +08004107 /* No more data to transfer or device error.
4108 * Device error will be tagged in HSM_ST_LAST.
4109 */
Albert Leee2cec772006-03-25 17:43:49 +08004110 ap->hsm_task_state = HSM_ST_LAST;
4111 goto fsm_start;
4112 }
4113
Albert Lee71601952006-03-25 18:11:12 +08004114 /* Device should not ask for data transfer (DRQ=1)
4115 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08004116 * We ignore DRQ here and stop the HSM by
4117 * changing hsm_task_state to HSM_ST_ERR and
4118 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08004119 */
4120 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4121 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
4122 ap->id, status);
Albert Lee3655d1d2006-05-19 11:43:04 +08004123 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08004124 ap->hsm_task_state = HSM_ST_ERR;
4125 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004126 }
4127
Albert Leee2cec772006-03-25 17:43:49 +08004128 atapi_pio_bytes(qc);
4129
4130 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4131 /* bad ireason reported by device */
4132 goto fsm_start;
4133
4134 } else {
4135 /* ATA PIO protocol */
4136 if (unlikely((status & ATA_DRQ) == 0)) {
4137 /* handle BSY=0, DRQ=0 as error */
Albert Lee3655d1d2006-05-19 11:43:04 +08004138 if (likely(status & (ATA_ERR | ATA_DF)))
4139 /* device stops HSM for abort/error */
4140 qc->err_mask |= AC_ERR_DEV;
4141 else
4142 /* HSM violation. Let EH handle this */
4143 qc->err_mask |= AC_ERR_HSM;
4144
Albert Leee2cec772006-03-25 17:43:49 +08004145 ap->hsm_task_state = HSM_ST_ERR;
4146 goto fsm_start;
4147 }
4148
Albert Leeeee6c322006-04-01 17:38:43 +08004149 /* For PIO reads, some devices may ask for
4150 * data transfer (DRQ=1) alone with ERR=1.
4151 * We respect DRQ here and transfer one
4152 * block of junk data before changing the
4153 * hsm_task_state to HSM_ST_ERR.
4154 *
4155 * For PIO writes, ERR=1 DRQ=1 doesn't make
4156 * sense since the data block has been
4157 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08004158 */
4159 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08004160 /* data might be corrputed */
4161 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08004162
4163 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
4164 ata_pio_sectors(qc);
4165 ata_altstatus(ap);
4166 status = ata_wait_idle(ap);
4167 }
4168
Albert Lee3655d1d2006-05-19 11:43:04 +08004169 if (status & (ATA_BUSY | ATA_DRQ))
4170 qc->err_mask |= AC_ERR_HSM;
4171
Albert Leeeee6c322006-04-01 17:38:43 +08004172 /* ata_pio_sectors() might change the
4173 * state to HSM_ST_LAST. so, the state
4174 * is changed after ata_pio_sectors().
4175 */
4176 ap->hsm_task_state = HSM_ST_ERR;
4177 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004178 }
4179
Albert Leee2cec772006-03-25 17:43:49 +08004180 ata_pio_sectors(qc);
4181
4182 if (ap->hsm_task_state == HSM_ST_LAST &&
4183 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4184 /* all data read */
4185 ata_altstatus(ap);
Albert Lee52a32202006-03-25 18:18:15 +08004186 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08004187 goto fsm_start;
4188 }
4189 }
4190
4191 ata_altstatus(ap); /* flush */
Albert Leebb5cb292006-03-25 17:48:02 +08004192 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08004193 break;
4194
4195 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004196 if (unlikely(!ata_ok(status))) {
4197 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08004198 ap->hsm_task_state = HSM_ST_ERR;
4199 goto fsm_start;
4200 }
4201
4202 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08004203 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
4204 ap->id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08004205
Albert Lee6912ccd2006-03-25 17:45:49 +08004206 WARN_ON(qc->err_mask);
4207
Albert Leee2cec772006-03-25 17:43:49 +08004208 ap->hsm_task_state = HSM_ST_IDLE;
4209
4210 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09004211 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08004212
4213 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08004214 break;
4215
4216 case HSM_ST_ERR:
Albert Leee2cec772006-03-25 17:43:49 +08004217 /* make sure qc->err_mask is available to
4218 * know what's wrong and recover
4219 */
4220 WARN_ON(qc->err_mask == 0);
4221
4222 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08004223
Albert Lee999bb6f2006-03-25 18:07:48 +08004224 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09004225 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08004226
4227 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08004228 break;
4229 default:
Albert Leebb5cb292006-03-25 17:48:02 +08004230 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08004231 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08004232 }
4233
Albert Leebb5cb292006-03-25 17:48:02 +08004234 return poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235}
4236
4237static void ata_pio_task(void *_data)
4238{
Tejun Heoc91af2c2006-04-02 18:51:53 +09004239 struct ata_queued_cmd *qc = _data;
4240 struct ata_port *ap = qc->ap;
Albert Leea1af3732006-03-25 17:50:15 +08004241 u8 status;
4242 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004243
4244fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08004245 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Albert Leea1af3732006-03-25 17:50:15 +08004247 /*
4248 * This is purely heuristic. This is a fast path.
4249 * Sometimes when we enter, BSY will be cleared in
4250 * a chk-status or two. If not, the drive is probably seeking
4251 * or something. Snooze for a couple msecs, then
4252 * chk-status again. If still busy, queue delayed work.
4253 */
4254 status = ata_busy_wait(ap, ATA_BUSY, 5);
4255 if (status & ATA_BUSY) {
4256 msleep(2);
4257 status = ata_busy_wait(ap, ATA_BUSY, 10);
4258 if (status & ATA_BUSY) {
Albert Lee31ce6da2006-04-03 18:31:44 +08004259 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08004260 return;
4261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 }
4263
Albert Leea1af3732006-03-25 17:50:15 +08004264 /* move the HSM */
4265 poll_next = ata_hsm_move(ap, qc, status, 1);
4266
4267 /* another command or interrupt handler
4268 * may be running at this point.
4269 */
4270 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004271 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272}
4273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 * ata_qc_new - Request an available ATA command, for queueing
4276 * @ap: Port associated with device @dev
4277 * @dev: Device from whom we request an available command structure
4278 *
4279 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004280 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 */
4282
4283static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4284{
4285 struct ata_queued_cmd *qc = NULL;
4286 unsigned int i;
4287
Tejun Heoe3180492006-05-15 20:58:09 +09004288 /* no command while frozen */
4289 if (unlikely(ap->flags & ATA_FLAG_FROZEN))
4290 return NULL;
4291
Tejun Heo2ab7db12006-05-15 20:58:02 +09004292 /* the last tag is reserved for internal command. */
4293 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09004294 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09004295 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 break;
4297 }
4298
4299 if (qc)
4300 qc->tag = i;
4301
4302 return qc;
4303}
4304
4305/**
4306 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 * @dev: Device from whom we request an available command structure
4308 *
4309 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004310 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 */
4312
Tejun Heo3373efd2006-05-15 20:57:53 +09004313struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
Tejun Heo3373efd2006-05-15 20:57:53 +09004315 struct ata_port *ap = dev->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 struct ata_queued_cmd *qc;
4317
4318 qc = ata_qc_new(ap);
4319 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 qc->scsicmd = NULL;
4321 qc->ap = ap;
4322 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004324 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 }
4326
4327 return qc;
4328}
4329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330/**
4331 * ata_qc_free - free unused ata_queued_cmd
4332 * @qc: Command to complete
4333 *
4334 * Designed to free unused ata_queued_cmd object
4335 * in case something prevents using it.
4336 *
4337 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004338 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 */
4340void ata_qc_free(struct ata_queued_cmd *qc)
4341{
Tejun Heo4ba946e2006-01-23 13:09:36 +09004342 struct ata_port *ap = qc->ap;
4343 unsigned int tag;
4344
Tejun Heoa4631472006-02-11 19:11:13 +09004345 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Tejun Heo4ba946e2006-01-23 13:09:36 +09004347 qc->flags = 0;
4348 tag = qc->tag;
4349 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09004350 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09004351 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09004352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353}
4354
Tejun Heo76014422006-02-11 15:13:49 +09004355void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356{
Tejun Heodedaf2b2006-05-15 21:03:43 +09004357 struct ata_port *ap = qc->ap;
4358
Tejun Heoa4631472006-02-11 19:11:13 +09004359 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4360 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
4362 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4363 ata_sg_clean(qc);
4364
Tejun Heo7401abf2006-05-15 20:57:32 +09004365 /* command should be marked inactive atomically with qc completion */
Tejun Heodedaf2b2006-05-15 21:03:43 +09004366 if (qc->tf.protocol == ATA_PROT_NCQ)
4367 ap->sactive &= ~(1 << qc->tag);
4368 else
4369 ap->active_tag = ATA_TAG_POISON;
Tejun Heo7401abf2006-05-15 20:57:32 +09004370
Albert Lee3f3791d2005-08-16 14:25:38 +08004371 /* atapi: mark qc as inactive to prevent the interrupt handler
4372 * from completing the command twice later, before the error handler
4373 * is called. (when rc != 0 and atapi request sense is needed)
4374 */
4375 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004376 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08004377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004379 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380}
4381
Tejun Heof686bcb2006-05-15 20:58:05 +09004382/**
4383 * ata_qc_complete - Complete an active ATA command
4384 * @qc: Command to complete
4385 * @err_mask: ATA Status register contents
4386 *
4387 * Indicate to the mid and upper layers that an ATA
4388 * command has completed, with either an ok or not-ok status.
4389 *
4390 * LOCKING:
4391 * spin_lock_irqsave(host_set lock)
4392 */
4393void ata_qc_complete(struct ata_queued_cmd *qc)
4394{
4395 struct ata_port *ap = qc->ap;
4396
4397 /* XXX: New EH and old EH use different mechanisms to
4398 * synchronize EH with regular execution path.
4399 *
4400 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4401 * Normal execution path is responsible for not accessing a
4402 * failed qc. libata core enforces the rule by returning NULL
4403 * from ata_qc_from_tag() for failed qcs.
4404 *
4405 * Old EH depends on ata_qc_complete() nullifying completion
4406 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4407 * not synchronize with interrupt handler. Only PIO task is
4408 * taken care of.
4409 */
4410 if (ap->ops->error_handler) {
4411 WARN_ON(ap->flags & ATA_FLAG_FROZEN);
4412
4413 if (unlikely(qc->err_mask))
4414 qc->flags |= ATA_QCFLAG_FAILED;
4415
4416 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4417 if (!ata_tag_internal(qc->tag)) {
4418 /* always fill result TF for failed qc */
4419 ap->ops->tf_read(ap, &qc->result_tf);
4420 ata_qc_schedule_eh(qc);
4421 return;
4422 }
4423 }
4424
4425 /* read result TF if requested */
4426 if (qc->flags & ATA_QCFLAG_RESULT_TF)
4427 ap->ops->tf_read(ap, &qc->result_tf);
4428
4429 __ata_qc_complete(qc);
4430 } else {
4431 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4432 return;
4433
4434 /* read result TF if failed or requested */
4435 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
4436 ap->ops->tf_read(ap, &qc->result_tf);
4437
4438 __ata_qc_complete(qc);
4439 }
4440}
4441
Tejun Heodedaf2b2006-05-15 21:03:43 +09004442/**
4443 * ata_qc_complete_multiple - Complete multiple qcs successfully
4444 * @ap: port in question
4445 * @qc_active: new qc_active mask
4446 * @finish_qc: LLDD callback invoked before completing a qc
4447 *
4448 * Complete in-flight commands. This functions is meant to be
4449 * called from low-level driver's interrupt routine to complete
4450 * requests normally. ap->qc_active and @qc_active is compared
4451 * and commands are completed accordingly.
4452 *
4453 * LOCKING:
4454 * spin_lock_irqsave(host_set lock)
4455 *
4456 * RETURNS:
4457 * Number of completed commands on success, -errno otherwise.
4458 */
4459int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
4460 void (*finish_qc)(struct ata_queued_cmd *))
4461{
4462 int nr_done = 0;
4463 u32 done_mask;
4464 int i;
4465
4466 done_mask = ap->qc_active ^ qc_active;
4467
4468 if (unlikely(done_mask & qc_active)) {
4469 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
4470 "(%08x->%08x)\n", ap->qc_active, qc_active);
4471 return -EINVAL;
4472 }
4473
4474 for (i = 0; i < ATA_MAX_QUEUE; i++) {
4475 struct ata_queued_cmd *qc;
4476
4477 if (!(done_mask & (1 << i)))
4478 continue;
4479
4480 if ((qc = ata_qc_from_tag(ap, i))) {
4481 if (finish_qc)
4482 finish_qc(qc);
4483 ata_qc_complete(qc);
4484 nr_done++;
4485 }
4486 }
4487
4488 return nr_done;
4489}
4490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4492{
4493 struct ata_port *ap = qc->ap;
4494
4495 switch (qc->tf.protocol) {
Tejun Heo3dc1d882006-05-15 21:03:45 +09004496 case ATA_PROT_NCQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 case ATA_PROT_DMA:
4498 case ATA_PROT_ATAPI_DMA:
4499 return 1;
4500
4501 case ATA_PROT_ATAPI:
4502 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 if (ap->flags & ATA_FLAG_PIO_DMA)
4504 return 1;
4505
4506 /* fall through */
4507
4508 default:
4509 return 0;
4510 }
4511
4512 /* never reached */
4513}
4514
4515/**
4516 * ata_qc_issue - issue taskfile to device
4517 * @qc: command to issue to device
4518 *
4519 * Prepare an ATA command to submission to device.
4520 * This includes mapping the data into a DMA-able
4521 * area, filling in the S/G table, and finally
4522 * writing the taskfile to hardware, starting the command.
4523 *
4524 * LOCKING:
4525 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004527void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528{
4529 struct ata_port *ap = qc->ap;
4530
Tejun Heodedaf2b2006-05-15 21:03:43 +09004531 /* Make sure only one non-NCQ command is outstanding. The
4532 * check is skipped for old EH because it reuses active qc to
4533 * request ATAPI sense.
4534 */
4535 WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
4536
4537 if (qc->tf.protocol == ATA_PROT_NCQ) {
4538 WARN_ON(ap->sactive & (1 << qc->tag));
4539 ap->sactive |= 1 << qc->tag;
4540 } else {
4541 WARN_ON(ap->sactive);
4542 ap->active_tag = qc->tag;
4543 }
4544
Tejun Heoe4a70e72006-03-31 20:36:47 +09004545 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004546 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09004547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 if (ata_should_dma_map(qc)) {
4549 if (qc->flags & ATA_QCFLAG_SG) {
4550 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004551 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4553 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004554 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 }
4556 } else {
4557 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4558 }
4559
4560 ap->ops->qc_prep(qc);
4561
Tejun Heo8e0e6942006-03-31 20:41:11 +09004562 qc->err_mask |= ap->ops->qc_issue(qc);
4563 if (unlikely(qc->err_mask))
4564 goto err;
4565 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Tejun Heo8e436af2006-01-23 13:09:36 +09004567sg_err:
4568 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09004569 qc->err_mask |= AC_ERR_SYSTEM;
4570err:
4571 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572}
4573
4574/**
4575 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4576 * @qc: command to issue to device
4577 *
4578 * Using various libata functions and hooks, this function
4579 * starts an ATA command. ATA commands are grouped into
4580 * classes called "protocols", and issuing each type of protocol
4581 * is slightly different.
4582 *
Edward Falk0baab862005-06-02 18:17:13 -04004583 * May be used as the qc_issue() entry in ata_port_operations.
4584 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 * LOCKING:
4586 * spin_lock_irqsave(host_set lock)
4587 *
4588 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004589 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 */
4591
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004592unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593{
4594 struct ata_port *ap = qc->ap;
4595
Albert Leee50362e2005-09-27 17:39:50 +08004596 /* Use polling pio if the LLD doesn't handle
4597 * interrupt driven pio and atapi CDB interrupt.
4598 */
4599 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4600 switch (qc->tf.protocol) {
4601 case ATA_PROT_PIO:
4602 case ATA_PROT_ATAPI:
4603 case ATA_PROT_ATAPI_NODATA:
4604 qc->tf.flags |= ATA_TFLAG_POLLING;
4605 break;
4606 case ATA_PROT_ATAPI_DMA:
4607 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Lee3a778272006-06-22 13:00:25 +08004608 /* see ata_dma_blacklisted() */
Albert Leee50362e2005-09-27 17:39:50 +08004609 BUG();
4610 break;
4611 default:
4612 break;
4613 }
4614 }
4615
Albert Lee312f7da2005-09-27 17:38:03 +08004616 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 ata_dev_select(ap, qc->dev->devno, 1, 0);
4618
Albert Lee312f7da2005-09-27 17:38:03 +08004619 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 switch (qc->tf.protocol) {
4621 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004622 if (qc->tf.flags & ATA_TFLAG_POLLING)
4623 ata_qc_set_polling(qc);
4624
Jeff Garzike5338252005-10-30 21:37:17 -05004625 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004626 ap->hsm_task_state = HSM_ST_LAST;
4627
4628 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08004629 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004630
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 break;
4632
4633 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004634 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4637 ap->ops->bmdma_setup(qc); /* set up bmdma */
4638 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004639 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 break;
4641
Albert Lee312f7da2005-09-27 17:38:03 +08004642 case ATA_PROT_PIO:
4643 if (qc->tf.flags & ATA_TFLAG_POLLING)
4644 ata_qc_set_polling(qc);
4645
Jeff Garzike5338252005-10-30 21:37:17 -05004646 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004647
Albert Lee54f00382005-09-30 19:14:19 +08004648 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4649 /* PIO data out protocol */
4650 ap->hsm_task_state = HSM_ST_FIRST;
Albert Lee31ce6da2006-04-03 18:31:44 +08004651 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08004652
4653 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08004654 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08004655 */
Albert Lee312f7da2005-09-27 17:38:03 +08004656 } else {
Albert Lee54f00382005-09-30 19:14:19 +08004657 /* PIO data in protocol */
4658 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08004659
Albert Lee54f00382005-09-30 19:14:19 +08004660 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08004661 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004662
Albert Lee54f00382005-09-30 19:14:19 +08004663 /* if polling, ata_pio_task() handles the rest.
4664 * otherwise, interrupt handler takes over from here.
4665 */
Albert Lee312f7da2005-09-27 17:38:03 +08004666 }
4667
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 break;
4669
4670 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004672 if (qc->tf.flags & ATA_TFLAG_POLLING)
4673 ata_qc_set_polling(qc);
4674
Jeff Garzike5338252005-10-30 21:37:17 -05004675 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004676
Albert Lee312f7da2005-09-27 17:38:03 +08004677 ap->hsm_task_state = HSM_ST_FIRST;
4678
4679 /* send cdb by polling if no cdb interrupt */
4680 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4681 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee31ce6da2006-04-03 18:31:44 +08004682 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 break;
4684
4685 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004686 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4689 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004690 ap->hsm_task_state = HSM_ST_FIRST;
4691
4692 /* send cdb by polling if no cdb interrupt */
4693 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee31ce6da2006-04-03 18:31:44 +08004694 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 break;
4696
4697 default:
4698 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004699 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 }
4701
4702 return 0;
4703}
4704
4705/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 * ata_host_intr - Handle host interrupt for given (port, task)
4707 * @ap: Port on which interrupt arrived (possibly...)
4708 * @qc: Taskfile currently active in engine
4709 *
4710 * Handle host interrupt for given queued command. Currently,
4711 * only DMA interrupts are handled. All other commands are
4712 * handled via polling with interrupts disabled (nIEN bit).
4713 *
4714 * LOCKING:
4715 * spin_lock_irqsave(host_set lock)
4716 *
4717 * RETURNS:
4718 * One if interrupt was handled, zero if not (shared irq).
4719 */
4720
4721inline unsigned int ata_host_intr (struct ata_port *ap,
4722 struct ata_queued_cmd *qc)
4723{
Albert Lee312f7da2005-09-27 17:38:03 +08004724 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
Albert Lee312f7da2005-09-27 17:38:03 +08004726 VPRINTK("ata%u: protocol %d task_state %d\n",
4727 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Albert Lee312f7da2005-09-27 17:38:03 +08004729 /* Check whether we are expecting interrupt in this state */
4730 switch (ap->hsm_task_state) {
4731 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004732 /* Some pre-ATAPI-4 devices assert INTRQ
4733 * at this state when ready to receive CDB.
4734 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
Albert Lee312f7da2005-09-27 17:38:03 +08004736 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4737 * The flag was turned on only for atapi devices.
4738 * No need to check is_atapi_taskfile(&qc->tf) again.
4739 */
4740 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 break;
Albert Lee312f7da2005-09-27 17:38:03 +08004743 case HSM_ST_LAST:
4744 if (qc->tf.protocol == ATA_PROT_DMA ||
4745 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4746 /* check status of DMA engine */
4747 host_stat = ap->ops->bmdma_status(ap);
4748 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
Albert Lee312f7da2005-09-27 17:38:03 +08004750 /* if it's not our irq... */
4751 if (!(host_stat & ATA_DMA_INTR))
4752 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Albert Lee312f7da2005-09-27 17:38:03 +08004754 /* before we do anything else, clear DMA-Start bit */
4755 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08004756
4757 if (unlikely(host_stat & ATA_DMA_ERR)) {
4758 /* error when transfering data to/from memory */
4759 qc->err_mask |= AC_ERR_HOST_BUS;
4760 ap->hsm_task_state = HSM_ST_ERR;
4761 }
Albert Lee312f7da2005-09-27 17:38:03 +08004762 }
4763 break;
4764 case HSM_ST:
4765 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 default:
4767 goto idle_irq;
4768 }
4769
Albert Lee312f7da2005-09-27 17:38:03 +08004770 /* check altstatus */
4771 status = ata_altstatus(ap);
4772 if (status & ATA_BUSY)
4773 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Albert Lee312f7da2005-09-27 17:38:03 +08004775 /* check main status, clearing INTRQ */
4776 status = ata_chk_status(ap);
4777 if (unlikely(status & ATA_BUSY))
4778 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Albert Lee312f7da2005-09-27 17:38:03 +08004780 /* ack bmdma irq events */
4781 ap->ops->irq_clear(ap);
4782
Albert Leebb5cb292006-03-25 17:48:02 +08004783 ata_hsm_move(ap, qc, status, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 return 1; /* irq handled */
4785
4786idle_irq:
4787 ap->stats.idle_irq++;
4788
4789#ifdef ATA_IRQ_TRAP
4790 if ((ap->stats.idle_irq % 1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 ata_irq_ack(ap, 0); /* debug trap */
Tejun Heof15a1da2006-05-15 20:57:56 +09004792 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
Alan Cox23cfce82006-03-21 16:06:53 +00004793 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 }
4795#endif
4796 return 0; /* irq not handled */
4797}
4798
4799/**
4800 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004801 * @irq: irq line (unused)
4802 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 * @regs: unused
4804 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004805 * Default interrupt handler for PCI IDE devices. Calls
4806 * ata_host_intr() for each port that is not disabled.
4807 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004809 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 *
4811 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004812 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 */
4814
4815irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4816{
4817 struct ata_host_set *host_set = dev_instance;
4818 unsigned int i;
4819 unsigned int handled = 0;
4820 unsigned long flags;
4821
4822 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4823 spin_lock_irqsave(&host_set->lock, flags);
4824
4825 for (i = 0; i < host_set->n_ports; i++) {
4826 struct ata_port *ap;
4827
4828 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004829 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04004830 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 struct ata_queued_cmd *qc;
4832
4833 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004834 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004835 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 handled |= ata_host_intr(ap, qc);
4837 }
4838 }
4839
4840 spin_unlock_irqrestore(&host_set->lock, flags);
4841
4842 return IRQ_RETVAL(handled);
4843}
4844
Tejun Heo34bf2172006-05-15 20:57:46 +09004845/**
4846 * sata_scr_valid - test whether SCRs are accessible
4847 * @ap: ATA port to test SCR accessibility for
4848 *
4849 * Test whether SCRs are accessible for @ap.
4850 *
4851 * LOCKING:
4852 * None.
4853 *
4854 * RETURNS:
4855 * 1 if SCRs are accessible, 0 otherwise.
4856 */
4857int sata_scr_valid(struct ata_port *ap)
4858{
4859 return ap->cbl == ATA_CBL_SATA && ap->ops->scr_read;
4860}
4861
4862/**
4863 * sata_scr_read - read SCR register of the specified port
4864 * @ap: ATA port to read SCR for
4865 * @reg: SCR to read
4866 * @val: Place to store read value
4867 *
4868 * Read SCR register @reg of @ap into *@val. This function is
4869 * guaranteed to succeed if the cable type of the port is SATA
4870 * and the port implements ->scr_read.
4871 *
4872 * LOCKING:
4873 * None.
4874 *
4875 * RETURNS:
4876 * 0 on success, negative errno on failure.
4877 */
4878int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
4879{
4880 if (sata_scr_valid(ap)) {
4881 *val = ap->ops->scr_read(ap, reg);
4882 return 0;
4883 }
4884 return -EOPNOTSUPP;
4885}
4886
4887/**
4888 * sata_scr_write - write SCR register of the specified port
4889 * @ap: ATA port to write SCR for
4890 * @reg: SCR to write
4891 * @val: value to write
4892 *
4893 * Write @val to SCR register @reg of @ap. This function is
4894 * guaranteed to succeed if the cable type of the port is SATA
4895 * and the port implements ->scr_read.
4896 *
4897 * LOCKING:
4898 * None.
4899 *
4900 * RETURNS:
4901 * 0 on success, negative errno on failure.
4902 */
4903int sata_scr_write(struct ata_port *ap, int reg, u32 val)
4904{
4905 if (sata_scr_valid(ap)) {
4906 ap->ops->scr_write(ap, reg, val);
4907 return 0;
4908 }
4909 return -EOPNOTSUPP;
4910}
4911
4912/**
4913 * sata_scr_write_flush - write SCR register of the specified port and flush
4914 * @ap: ATA port to write SCR for
4915 * @reg: SCR to write
4916 * @val: value to write
4917 *
4918 * This function is identical to sata_scr_write() except that this
4919 * function performs flush after writing to the register.
4920 *
4921 * LOCKING:
4922 * None.
4923 *
4924 * RETURNS:
4925 * 0 on success, negative errno on failure.
4926 */
4927int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
4928{
4929 if (sata_scr_valid(ap)) {
4930 ap->ops->scr_write(ap, reg, val);
4931 ap->ops->scr_read(ap, reg);
4932 return 0;
4933 }
4934 return -EOPNOTSUPP;
4935}
4936
4937/**
4938 * ata_port_online - test whether the given port is online
4939 * @ap: ATA port to test
4940 *
4941 * Test whether @ap is online. Note that this function returns 0
4942 * if online status of @ap cannot be obtained, so
4943 * ata_port_online(ap) != !ata_port_offline(ap).
4944 *
4945 * LOCKING:
4946 * None.
4947 *
4948 * RETURNS:
4949 * 1 if the port online status is available and online.
4950 */
4951int ata_port_online(struct ata_port *ap)
4952{
4953 u32 sstatus;
4954
4955 if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
4956 return 1;
4957 return 0;
4958}
4959
4960/**
4961 * ata_port_offline - test whether the given port is offline
4962 * @ap: ATA port to test
4963 *
4964 * Test whether @ap is offline. Note that this function returns
4965 * 0 if offline status of @ap cannot be obtained, so
4966 * ata_port_online(ap) != !ata_port_offline(ap).
4967 *
4968 * LOCKING:
4969 * None.
4970 *
4971 * RETURNS:
4972 * 1 if the port offline status is available and offline.
4973 */
4974int ata_port_offline(struct ata_port *ap)
4975{
4976 u32 sstatus;
4977
4978 if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
4979 return 1;
4980 return 0;
4981}
Edward Falk0baab862005-06-02 18:17:13 -04004982
Tejun Heo77b08fb2006-06-24 20:30:19 +09004983int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01004984{
Tejun Heo977e6b92006-06-24 20:30:19 +09004985 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01004986 u8 cmd;
4987
4988 if (!ata_try_flush_cache(dev))
4989 return 0;
4990
4991 if (ata_id_has_flush_ext(dev->id))
4992 cmd = ATA_CMD_FLUSH_EXT;
4993 else
4994 cmd = ATA_CMD_FLUSH;
4995
Tejun Heo977e6b92006-06-24 20:30:19 +09004996 err_mask = ata_do_simple_cmd(dev, cmd);
4997 if (err_mask) {
4998 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
4999 return -EIO;
5000 }
5001
5002 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01005003}
5004
Tejun Heo3373efd2006-05-15 20:57:53 +09005005static int ata_standby_drive(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01005006{
Tejun Heo977e6b92006-06-24 20:30:19 +09005007 unsigned int err_mask;
5008
5009 err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
5010 if (err_mask) {
5011 ata_dev_printk(dev, KERN_ERR, "failed to standby drive "
5012 "(err_mask=0x%x)\n", err_mask);
5013 return -EIO;
5014 }
5015
5016 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01005017}
5018
Tejun Heo3373efd2006-05-15 20:57:53 +09005019static int ata_start_drive(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01005020{
Tejun Heo977e6b92006-06-24 20:30:19 +09005021 unsigned int err_mask;
5022
5023 err_mask = ata_do_simple_cmd(dev, ATA_CMD_IDLEIMMEDIATE);
5024 if (err_mask) {
5025 ata_dev_printk(dev, KERN_ERR, "failed to start drive "
5026 "(err_mask=0x%x)\n", err_mask);
5027 return -EIO;
5028 }
5029
5030 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01005031}
5032
5033/**
5034 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005035 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01005036 *
5037 * Kick the drive back into action, by sending it an idle immediate
5038 * command and making sure its transfer mode matches between drive
5039 * and host.
5040 *
5041 */
Tejun Heo3373efd2006-05-15 20:57:53 +09005042int ata_device_resume(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01005043{
Tejun Heo3373efd2006-05-15 20:57:53 +09005044 struct ata_port *ap = dev->ap;
5045
Jens Axboe9b847542006-01-06 09:28:07 +01005046 if (ap->flags & ATA_FLAG_SUSPENDED) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09005047 struct ata_device *failed_dev;
Jeff Garzike42d7be2006-05-28 16:32:59 -04005048
Linus Torvalds1cca0eb2006-06-19 18:01:27 -07005049 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
Mark Lord0737ac82006-05-28 11:28:00 -04005050 ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 200000);
Jeff Garzike42d7be2006-05-28 16:32:59 -04005051
Jens Axboe9b847542006-01-06 09:28:07 +01005052 ap->flags &= ~ATA_FLAG_SUSPENDED;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09005053 while (ata_set_mode(ap, &failed_dev))
Tejun Heo3373efd2006-05-15 20:57:53 +09005054 ata_dev_disable(failed_dev);
Jens Axboe9b847542006-01-06 09:28:07 +01005055 }
Tejun Heoe1211e32006-04-01 01:38:18 +09005056 if (!ata_dev_enabled(dev))
Jens Axboe9b847542006-01-06 09:28:07 +01005057 return 0;
5058 if (dev->class == ATA_DEV_ATA)
Tejun Heo3373efd2006-05-15 20:57:53 +09005059 ata_start_drive(dev);
Jens Axboe9b847542006-01-06 09:28:07 +01005060
5061 return 0;
5062}
5063
5064/**
5065 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005066 * @dev: the device to suspend
Randy Dunlape2a7f772006-05-18 10:50:18 -07005067 * @state: target power management state
Jens Axboe9b847542006-01-06 09:28:07 +01005068 *
5069 * Flush the cache on the drive, if appropriate, then issue a
5070 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01005071 */
Tejun Heo3373efd2006-05-15 20:57:53 +09005072int ata_device_suspend(struct ata_device *dev, pm_message_t state)
Jens Axboe9b847542006-01-06 09:28:07 +01005073{
Tejun Heo3373efd2006-05-15 20:57:53 +09005074 struct ata_port *ap = dev->ap;
5075
Tejun Heoe1211e32006-04-01 01:38:18 +09005076 if (!ata_dev_enabled(dev))
Jens Axboe9b847542006-01-06 09:28:07 +01005077 return 0;
5078 if (dev->class == ATA_DEV_ATA)
Tejun Heo3373efd2006-05-15 20:57:53 +09005079 ata_flush_cache(dev);
Jens Axboe9b847542006-01-06 09:28:07 +01005080
Nigel Cunningham082776e2006-03-23 23:22:16 +10005081 if (state.event != PM_EVENT_FREEZE)
Tejun Heo3373efd2006-05-15 20:57:53 +09005082 ata_standby_drive(dev);
Jens Axboe9b847542006-01-06 09:28:07 +01005083 ap->flags |= ATA_FLAG_SUSPENDED;
5084 return 0;
5085}
5086
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005087/**
5088 * ata_port_start - Set port up for dma.
5089 * @ap: Port to initialize
5090 *
5091 * Called just after data structures for each port are
5092 * initialized. Allocates space for PRD table.
5093 *
5094 * May be used as the port_start() entry in ata_port_operations.
5095 *
5096 * LOCKING:
5097 * Inherited from caller.
5098 */
5099
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100int ata_port_start (struct ata_port *ap)
5101{
Brian King2f1f6102006-03-23 17:30:15 -06005102 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005103 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
5105 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
5106 if (!ap->prd)
5107 return -ENOMEM;
5108
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005109 rc = ata_pad_alloc(ap, dev);
5110 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04005111 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005112 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04005113 }
5114
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
5116
5117 return 0;
5118}
5119
Edward Falk0baab862005-06-02 18:17:13 -04005120
5121/**
5122 * ata_port_stop - Undo ata_port_start()
5123 * @ap: Port to shut down
5124 *
5125 * Frees the PRD table.
5126 *
5127 * May be used as the port_stop() entry in ata_port_operations.
5128 *
5129 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005130 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04005131 */
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133void ata_port_stop (struct ata_port *ap)
5134{
Brian King2f1f6102006-03-23 17:30:15 -06005135 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005138 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139}
5140
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005141void ata_host_stop (struct ata_host_set *host_set)
5142{
5143 if (host_set->mmio_base)
5144 iounmap(host_set->mmio_base);
5145}
5146
5147
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148/**
5149 * ata_host_remove - Unregister SCSI host structure with upper layers
5150 * @ap: Port to unregister
5151 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
5152 *
5153 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005154 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 */
5156
5157static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
5158{
5159 struct Scsi_Host *sh = ap->host;
5160
5161 DPRINTK("ENTER\n");
5162
5163 if (do_unregister)
5164 scsi_remove_host(sh);
5165
5166 ap->ops->port_stop(ap);
5167}
5168
5169/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005170 * ata_dev_init - Initialize an ata_device structure
5171 * @dev: Device structure to initialize
5172 *
5173 * Initialize @dev in preparation for probing.
5174 *
5175 * LOCKING:
5176 * Inherited from caller.
5177 */
5178void ata_dev_init(struct ata_device *dev)
5179{
5180 struct ata_port *ap = dev->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005181 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005182
Tejun Heo5a04bf42006-05-31 18:27:38 +09005183 /* SATA spd limit is bound to the first device */
5184 ap->sata_spd_limit = ap->hw_sata_spd_limit;
5185
Tejun Heo72fa4b72006-05-31 18:27:32 +09005186 /* High bits of dev->flags are used to record warm plug
5187 * requests which occur asynchronously. Synchronize using
5188 * host_set lock.
5189 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005190 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005191 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005192 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005193
5194 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
5195 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005196 dev->pio_mask = UINT_MAX;
5197 dev->mwdma_mask = UINT_MAX;
5198 dev->udma_mask = UINT_MAX;
5199}
5200
5201/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 * ata_host_init - Initialize an ata_port structure
5203 * @ap: Structure to initialize
5204 * @host: associated SCSI mid-layer structure
5205 * @host_set: Collection of hosts to which @ap belongs
5206 * @ent: Probe information provided by low-level driver
5207 * @port_no: Port number associated with this ata_port
5208 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005209 * Initialize a new ata_port structure, and its associated
5210 * scsi_host.
5211 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005213 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
5216 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04005217 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218{
5219 unsigned int i;
5220
5221 host->max_id = 16;
5222 host->max_lun = 1;
5223 host->max_channel = 1;
5224 host->unique_id = ata_unique_id++;
5225 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02005226
Jeff Garzikba6a1302006-06-22 23:46:10 -04005227 ap->lock = &host_set->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09005228 ap->flags = ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 ap->id = host->unique_id;
5230 ap->host = host;
5231 ap->ctl = ATA_DEVCTL_OBS;
5232 ap->host_set = host_set;
Brian King2f1f6102006-03-23 17:30:15 -06005233 ap->dev = ent->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 ap->port_no = port_no;
5235 ap->hard_port_no =
5236 ent->legacy_mode ? ent->hard_port_no : port_no;
5237 ap->pio_mask = ent->pio_mask;
5238 ap->mwdma_mask = ent->mwdma_mask;
5239 ap->udma_mask = ent->udma_mask;
5240 ap->flags |= ent->host_flags;
5241 ap->ops = ent->port_ops;
Tejun Heo5a04bf42006-05-31 18:27:38 +09005242 ap->hw_sata_spd_limit = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 ap->active_tag = ATA_TAG_POISON;
5244 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005245
5246#if defined(ATA_VERBOSE_DEBUG)
5247 /* turn on all debugging levels */
5248 ap->msg_enable = 0x00FF;
5249#elif defined(ATA_DEBUG)
5250 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
Tejun Heo88574552006-06-25 20:00:35 +09005251#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005252 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
Tejun Heo86e45b62006-03-05 15:29:09 +09005255 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heo580b2102006-05-31 18:28:05 +09005256 INIT_WORK(&ap->hotplug_task, ata_scsi_hotplug, ap);
zhao, forrest3057ac32006-06-12 12:01:34 +08005257 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005258 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005259 init_waitqueue_head(&ap->eh_wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
Tejun Heo838df622006-05-15 20:57:44 +09005261 /* set cable type */
5262 ap->cbl = ATA_CBL_NONE;
5263 if (ap->flags & ATA_FLAG_SATA)
5264 ap->cbl = ATA_CBL_SATA;
5265
Tejun Heoacf356b2006-03-24 14:07:50 +09005266 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5267 struct ata_device *dev = &ap->device[i];
Tejun Heo38d87232006-05-15 20:57:51 +09005268 dev->ap = ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005269 dev->devno = i;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005270 ata_dev_init(dev);
Tejun Heoacf356b2006-03-24 14:07:50 +09005271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
5273#ifdef ATA_IRQ_TRAP
5274 ap->stats.unhandled_irq = 1;
5275 ap->stats.idle_irq = 1;
5276#endif
5277
5278 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
5279}
5280
5281/**
5282 * ata_host_add - Attach low-level ATA driver to system
5283 * @ent: Information provided by low-level driver
5284 * @host_set: Collections of ports to which we add
5285 * @port_no: Port number associated with this host
5286 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005287 * Attach low-level ATA driver to system.
5288 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005290 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 *
5292 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005293 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 */
5295
Jeff Garzik057ace52005-10-22 14:27:05 -04005296static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 struct ata_host_set *host_set,
5298 unsigned int port_no)
5299{
5300 struct Scsi_Host *host;
5301 struct ata_port *ap;
5302 int rc;
5303
5304 DPRINTK("ENTER\n");
Tejun Heoaec5c3c2006-03-25 01:33:34 +09005305
Tejun Heo52783c52006-05-31 18:28:22 +09005306 if (!ent->port_ops->error_handler &&
Tejun Heoaec5c3c2006-03-25 01:33:34 +09005307 !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
5308 printk(KERN_ERR "ata%u: no reset mechanism available\n",
5309 port_no);
5310 return NULL;
5311 }
5312
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
5314 if (!host)
5315 return NULL;
5316
Tejun Heo30afc842006-03-18 18:40:14 +09005317 host->transportt = &ata_scsi_transport_template;
5318
Jeff Garzik35bb94b2006-04-11 13:12:34 -04005319 ap = ata_shost_to_port(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
5321 ata_host_init(ap, host, host_set, ent, port_no);
5322
5323 rc = ap->ops->port_start(ap);
5324 if (rc)
5325 goto err_out;
5326
5327 return ap;
5328
5329err_out:
5330 scsi_host_put(host);
5331 return NULL;
5332}
5333
5334/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04005335 * ata_device_add - Register hardware device with ATA and SCSI layers
5336 * @ent: Probe information describing hardware device to be registered
5337 *
5338 * This function processes the information provided in the probe
5339 * information struct @ent, allocates the necessary ATA and SCSI
5340 * host information structures, initializes them, and registers
5341 * everything with requisite kernel subsystems.
5342 *
5343 * This function requests irqs, probes the ATA bus, and probes
5344 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 *
5346 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005347 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 *
5349 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005350 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 */
Jeff Garzik057ace52005-10-22 14:27:05 -04005352int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
5354 unsigned int count = 0, i;
5355 struct device *dev = ent->dev;
5356 struct ata_host_set *host_set;
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005357 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
5359 DPRINTK("ENTER\n");
5360 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005361 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
5363 if (!host_set)
5364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 spin_lock_init(&host_set->lock);
5366
5367 host_set->dev = dev;
5368 host_set->n_ports = ent->n_ports;
5369 host_set->irq = ent->irq;
5370 host_set->mmio_base = ent->mmio_base;
5371 host_set->private_data = ent->private_data;
5372 host_set->ops = ent->port_ops;
Alan Cox5444a6f2006-03-27 18:58:20 +01005373 host_set->flags = ent->host_set_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
5375 /* register each port bound to this device */
5376 for (i = 0; i < ent->n_ports; i++) {
5377 struct ata_port *ap;
5378 unsigned long xfer_mode_mask;
5379
5380 ap = ata_host_add(ent, host_set, i);
5381 if (!ap)
5382 goto err_out;
5383
5384 host_set->ports[i] = ap;
5385 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
5386 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
5387 (ap->pio_mask << ATA_SHIFT_PIO);
5388
5389 /* print per-port info to dmesg */
Tejun Heof15a1da2006-05-15 20:57:56 +09005390 ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%lX "
5391 "ctl 0x%lX bmdma 0x%lX irq %lu\n",
5392 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
5393 ata_mode_string(xfer_mode_mask),
5394 ap->ioaddr.cmd_addr,
5395 ap->ioaddr.ctl_addr,
5396 ap->ioaddr.bmdma_addr,
5397 ent->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
5399 ata_chk_status(ap);
5400 host_set->ops->irq_clear(ap);
Tejun Heoe3180492006-05-15 20:58:09 +09005401 ata_eh_freeze_port(ap); /* freeze port before requesting IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 count++;
5403 }
5404
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005405 if (!count)
5406 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
5408 /* obtain irq, that is shared between channels */
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005409 rc = request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
5410 DRV_NAME, host_set);
5411 if (rc) {
5412 dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
5413 ent->irq, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 goto err_out;
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
5417 /* perform each probe synchronously */
5418 DPRINTK("probe begin\n");
5419 for (i = 0; i < count; i++) {
5420 struct ata_port *ap;
Tejun Heo5a04bf42006-05-31 18:27:38 +09005421 u32 scontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 int rc;
5423
5424 ap = host_set->ports[i];
5425
Tejun Heo5a04bf42006-05-31 18:27:38 +09005426 /* init sata_spd_limit to the current value */
5427 if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
5428 int spd = (scontrol >> 4) & 0xf;
5429 ap->hw_sata_spd_limit &= (1 << spd) - 1;
5430 }
5431 ap->sata_spd_limit = ap->hw_sata_spd_limit;
5432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 rc = scsi_add_host(ap->host, dev);
5434 if (rc) {
Tejun Heof15a1da2006-05-15 20:57:56 +09005435 ata_port_printk(ap, KERN_ERR, "scsi_add_host failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 /* FIXME: do something useful here */
5437 /* FIXME: handle unconditional calls to
5438 * scsi_scan_host and ata_host_remove, below,
5439 * at the very least
5440 */
5441 }
Tejun Heo3e706392006-05-31 18:28:11 +09005442
Tejun Heo52783c52006-05-31 18:28:22 +09005443 if (ap->ops->error_handler) {
Tejun Heo3e706392006-05-31 18:28:11 +09005444 unsigned long flags;
5445
5446 ata_port_probe(ap);
5447
5448 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005449 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09005450
5451 ap->eh_info.probe_mask = (1 << ATA_MAX_DEVICES) - 1;
5452 ap->eh_info.action |= ATA_EH_SOFTRESET;
5453
5454 ap->flags |= ATA_FLAG_LOADING;
5455 ata_port_schedule_eh(ap);
5456
Jeff Garzikba6a1302006-06-22 23:46:10 -04005457 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09005458
5459 /* wait for EH to finish */
5460 ata_port_wait_eh(ap);
5461 } else {
5462 DPRINTK("ata%u: bus probe begin\n", ap->id);
5463 rc = ata_bus_probe(ap);
5464 DPRINTK("ata%u: bus probe end\n", ap->id);
5465
5466 if (rc) {
5467 /* FIXME: do something useful here?
5468 * Current libata behavior will
5469 * tear down everything when
5470 * the module is removed
5471 * or the h/w is unplugged.
5472 */
5473 }
5474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 }
5476
5477 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005478 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 for (i = 0; i < count; i++) {
5480 struct ata_port *ap = host_set->ports[i];
5481
Jeff Garzik644dd0c2005-10-03 15:55:19 -04005482 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 }
5484
5485 dev_set_drvdata(dev, host_set);
5486
5487 VPRINTK("EXIT, returning %u\n", ent->n_ports);
5488 return ent->n_ports; /* success */
5489
5490err_out:
5491 for (i = 0; i < count; i++) {
5492 ata_host_remove(host_set->ports[i], 1);
5493 scsi_host_put(host_set->ports[i]->host);
5494 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07005495err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 kfree(host_set);
5497 VPRINTK("EXIT, returning 0\n");
5498 return 0;
5499}
5500
5501/**
Tejun Heo720ba122006-05-31 18:28:13 +09005502 * ata_port_detach - Detach ATA port in prepration of device removal
5503 * @ap: ATA port to be detached
5504 *
5505 * Detach all ATA devices and the associated SCSI devices of @ap;
5506 * then, remove the associated SCSI host. @ap is guaranteed to
5507 * be quiescent on return from this function.
5508 *
5509 * LOCKING:
5510 * Kernel thread context (may sleep).
5511 */
5512void ata_port_detach(struct ata_port *ap)
5513{
5514 unsigned long flags;
5515 int i;
5516
5517 if (!ap->ops->error_handler)
5518 return;
5519
5520 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005521 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005522 ap->flags |= ATA_FLAG_UNLOADING;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005523 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005524
5525 ata_port_wait_eh(ap);
5526
5527 /* EH is now guaranteed to see UNLOADING, so no new device
5528 * will be attached. Disable all existing devices.
5529 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005530 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005531
5532 for (i = 0; i < ATA_MAX_DEVICES; i++)
5533 ata_dev_disable(&ap->device[i]);
5534
Jeff Garzikba6a1302006-06-22 23:46:10 -04005535 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005536
5537 /* Final freeze & EH. All in-flight commands are aborted. EH
5538 * will be skipped and retrials will be terminated with bad
5539 * target.
5540 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005541 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005542 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005543 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005544
5545 ata_port_wait_eh(ap);
5546
5547 /* Flush hotplug task. The sequence is similar to
5548 * ata_port_flush_task().
5549 */
5550 flush_workqueue(ata_aux_wq);
5551 cancel_delayed_work(&ap->hotplug_task);
5552 flush_workqueue(ata_aux_wq);
5553
5554 /* remove the associated SCSI host */
5555 scsi_remove_host(ap->host);
5556}
5557
5558/**
Alan Cox17b14452005-09-15 15:44:00 +01005559 * ata_host_set_remove - PCI layer callback for device removal
5560 * @host_set: ATA host set that was removed
5561 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005562 * Unregister all objects associated with this host set. Free those
Alan Cox17b14452005-09-15 15:44:00 +01005563 * objects.
5564 *
5565 * LOCKING:
5566 * Inherited from calling layer (may sleep).
5567 */
5568
Alan Cox17b14452005-09-15 15:44:00 +01005569void ata_host_set_remove(struct ata_host_set *host_set)
5570{
Alan Cox17b14452005-09-15 15:44:00 +01005571 unsigned int i;
5572
Tejun Heo720ba122006-05-31 18:28:13 +09005573 for (i = 0; i < host_set->n_ports; i++)
5574 ata_port_detach(host_set->ports[i]);
Alan Cox17b14452005-09-15 15:44:00 +01005575
5576 free_irq(host_set->irq, host_set);
5577
5578 for (i = 0; i < host_set->n_ports; i++) {
Tejun Heo720ba122006-05-31 18:28:13 +09005579 struct ata_port *ap = host_set->ports[i];
Alan Cox17b14452005-09-15 15:44:00 +01005580
5581 ata_scsi_release(ap->host);
5582
5583 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
5584 struct ata_ioports *ioaddr = &ap->ioaddr;
5585
5586 if (ioaddr->cmd_addr == 0x1f0)
5587 release_region(0x1f0, 8);
5588 else if (ioaddr->cmd_addr == 0x170)
5589 release_region(0x170, 8);
5590 }
5591
5592 scsi_host_put(ap->host);
5593 }
5594
5595 if (host_set->ops->host_stop)
5596 host_set->ops->host_stop(host_set);
5597
5598 kfree(host_set);
5599}
5600
5601/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 * ata_scsi_release - SCSI layer callback hook for host unload
5603 * @host: libata host to be unloaded
5604 *
5605 * Performs all duties necessary to shut down a libata port...
5606 * Kill port kthread, disable port, and release resources.
5607 *
5608 * LOCKING:
5609 * Inherited from SCSI layer.
5610 *
5611 * RETURNS:
5612 * One.
5613 */
5614
5615int ata_scsi_release(struct Scsi_Host *host)
5616{
Jeff Garzik35bb94b2006-04-11 13:12:34 -04005617 struct ata_port *ap = ata_shost_to_port(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
5619 DPRINTK("ENTER\n");
5620
5621 ap->ops->port_disable(ap);
5622 ata_host_remove(ap, 0);
5623
5624 DPRINTK("EXIT\n");
5625 return 1;
5626}
5627
5628/**
5629 * ata_std_ports - initialize ioaddr with standard port offsets.
5630 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04005631 *
5632 * Utility function which initializes data_addr, error_addr,
5633 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5634 * device_addr, status_addr, and command_addr to standard offsets
5635 * relative to cmd_addr.
5636 *
5637 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 */
Edward Falk0baab862005-06-02 18:17:13 -04005639
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640void ata_std_ports(struct ata_ioports *ioaddr)
5641{
5642 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5643 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5644 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5645 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5646 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5647 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5648 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5649 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5650 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5651 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5652}
5653
Edward Falk0baab862005-06-02 18:17:13 -04005654
Jeff Garzik374b1872005-08-30 05:42:52 -04005655#ifdef CONFIG_PCI
5656
5657void ata_pci_host_stop (struct ata_host_set *host_set)
5658{
5659 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5660
5661 pci_iounmap(pdev, host_set->mmio_base);
5662}
5663
Edward Falk0baab862005-06-02 18:17:13 -04005664/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 * ata_pci_remove_one - PCI layer callback for device removal
5666 * @pdev: PCI device that was removed
5667 *
5668 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005669 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 * Handle this by unregistering all objects associated
5671 * with this PCI device. Free those objects. Then finally
5672 * release PCI resources and disable device.
5673 *
5674 * LOCKING:
5675 * Inherited from PCI layer (may sleep).
5676 */
5677
5678void ata_pci_remove_one (struct pci_dev *pdev)
5679{
5680 struct device *dev = pci_dev_to_dev(pdev);
5681 struct ata_host_set *host_set = dev_get_drvdata(dev);
Tejun Heof0eb62b2006-06-12 23:05:38 +09005682 struct ata_host_set *host_set2 = host_set->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Alan Cox17b14452005-09-15 15:44:00 +01005684 ata_host_set_remove(host_set);
Tejun Heof0eb62b2006-06-12 23:05:38 +09005685 if (host_set2)
5686 ata_host_set_remove(host_set2);
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 pci_release_regions(pdev);
5689 pci_disable_device(pdev);
5690 dev_set_drvdata(dev, NULL);
5691}
5692
5693/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005694int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695{
5696 unsigned long tmp = 0;
5697
5698 switch (bits->width) {
5699 case 1: {
5700 u8 tmp8 = 0;
5701 pci_read_config_byte(pdev, bits->reg, &tmp8);
5702 tmp = tmp8;
5703 break;
5704 }
5705 case 2: {
5706 u16 tmp16 = 0;
5707 pci_read_config_word(pdev, bits->reg, &tmp16);
5708 tmp = tmp16;
5709 break;
5710 }
5711 case 4: {
5712 u32 tmp32 = 0;
5713 pci_read_config_dword(pdev, bits->reg, &tmp32);
5714 tmp = tmp32;
5715 break;
5716 }
5717
5718 default:
5719 return -EINVAL;
5720 }
5721
5722 tmp &= bits->mask;
5723
5724 return (tmp == bits->val) ? 1 : 0;
5725}
Jens Axboe9b847542006-01-06 09:28:07 +01005726
5727int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5728{
5729 pci_save_state(pdev);
5730 pci_disable_device(pdev);
5731 pci_set_power_state(pdev, PCI_D3hot);
5732 return 0;
5733}
5734
5735int ata_pci_device_resume(struct pci_dev *pdev)
5736{
5737 pci_set_power_state(pdev, PCI_D0);
5738 pci_restore_state(pdev);
5739 pci_enable_device(pdev);
5740 pci_set_master(pdev);
5741 return 0;
5742}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743#endif /* CONFIG_PCI */
5744
5745
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746static int __init ata_init(void)
5747{
Andrew Mortona8601e52006-06-25 01:36:52 -07005748 ata_probe_timeout *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 ata_wq = create_workqueue("ata");
5750 if (!ata_wq)
5751 return -ENOMEM;
5752
Tejun Heo453b07a2006-05-31 18:27:42 +09005753 ata_aux_wq = create_singlethread_workqueue("ata_aux");
5754 if (!ata_aux_wq) {
5755 destroy_workqueue(ata_wq);
5756 return -ENOMEM;
5757 }
5758
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5760 return 0;
5761}
5762
5763static void __exit ata_exit(void)
5764{
5765 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09005766 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767}
5768
5769module_init(ata_init);
5770module_exit(ata_exit);
5771
Jeff Garzik67846b32005-10-05 02:58:32 -04005772static unsigned long ratelimit_time;
5773static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5774
5775int ata_ratelimit(void)
5776{
5777 int rc;
5778 unsigned long flags;
5779
5780 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5781
5782 if (time_after(jiffies, ratelimit_time)) {
5783 rc = 1;
5784 ratelimit_time = jiffies + (HZ/5);
5785 } else
5786 rc = 0;
5787
5788 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5789
5790 return rc;
5791}
5792
Tejun Heoc22daff2006-04-11 22:22:29 +09005793/**
5794 * ata_wait_register - wait until register value changes
5795 * @reg: IO-mapped register
5796 * @mask: Mask to apply to read register value
5797 * @val: Wait condition
5798 * @interval_msec: polling interval in milliseconds
5799 * @timeout_msec: timeout in milliseconds
5800 *
5801 * Waiting for some bits of register to change is a common
5802 * operation for ATA controllers. This function reads 32bit LE
5803 * IO-mapped register @reg and tests for the following condition.
5804 *
5805 * (*@reg & mask) != val
5806 *
5807 * If the condition is met, it returns; otherwise, the process is
5808 * repeated after @interval_msec until timeout.
5809 *
5810 * LOCKING:
5811 * Kernel thread context (may sleep)
5812 *
5813 * RETURNS:
5814 * The final register value.
5815 */
5816u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
5817 unsigned long interval_msec,
5818 unsigned long timeout_msec)
5819{
5820 unsigned long timeout;
5821 u32 tmp;
5822
5823 tmp = ioread32(reg);
5824
5825 /* Calculate timeout _after_ the first read to make sure
5826 * preceding writes reach the controller before starting to
5827 * eat away the timeout.
5828 */
5829 timeout = jiffies + (timeout_msec * HZ) / 1000;
5830
5831 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
5832 msleep(interval_msec);
5833 tmp = ioread32(reg);
5834 }
5835
5836 return tmp;
5837}
5838
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839/*
5840 * libata is essentially a library of internal helper functions for
5841 * low-level ATA host controller drivers. As such, the API/ABI is
5842 * likely to change as new drivers are added and updated.
5843 * Do not depend on ABI/API stability.
5844 */
5845
Tejun Heod7bb4cc2006-05-31 18:27:46 +09005846EXPORT_SYMBOL_GPL(sata_deb_timing_boot);
5847EXPORT_SYMBOL_GPL(sata_deb_timing_eh);
5848EXPORT_SYMBOL_GPL(sata_deb_timing_before_fsrst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849EXPORT_SYMBOL_GPL(ata_std_bios_param);
5850EXPORT_SYMBOL_GPL(ata_std_ports);
5851EXPORT_SYMBOL_GPL(ata_device_add);
Tejun Heo720ba122006-05-31 18:28:13 +09005852EXPORT_SYMBOL_GPL(ata_port_detach);
Alan Cox17b14452005-09-15 15:44:00 +01005853EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854EXPORT_SYMBOL_GPL(ata_sg_init);
5855EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo9a1004d2006-05-31 18:27:52 +09005856EXPORT_SYMBOL_GPL(ata_hsm_move);
Tejun Heof686bcb2006-05-15 20:58:05 +09005857EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005858EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860EXPORT_SYMBOL_GPL(ata_tf_load);
5861EXPORT_SYMBOL_GPL(ata_tf_read);
5862EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5863EXPORT_SYMBOL_GPL(ata_std_dev_select);
5864EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5865EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5866EXPORT_SYMBOL_GPL(ata_check_status);
5867EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868EXPORT_SYMBOL_GPL(ata_exec_command);
5869EXPORT_SYMBOL_GPL(ata_port_start);
5870EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005871EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872EXPORT_SYMBOL_GPL(ata_interrupt);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01005873EXPORT_SYMBOL_GPL(ata_mmio_data_xfer);
5874EXPORT_SYMBOL_GPL(ata_pio_data_xfer);
Alan Cox75e99582006-05-24 14:14:41 +01005875EXPORT_SYMBOL_GPL(ata_pio_data_xfer_noirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06005877EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5879EXPORT_SYMBOL_GPL(ata_bmdma_start);
5880EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5881EXPORT_SYMBOL_GPL(ata_bmdma_status);
5882EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09005883EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
5884EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
5885EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
5886EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
5887EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888EXPORT_SYMBOL_GPL(ata_port_probe);
Tejun Heo3c567b72006-05-15 20:57:23 +09005889EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heod7bb4cc2006-05-31 18:27:46 +09005890EXPORT_SYMBOL_GPL(sata_phy_debounce);
5891EXPORT_SYMBOL_GPL(sata_phy_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892EXPORT_SYMBOL_GPL(sata_phy_reset);
5893EXPORT_SYMBOL_GPL(__sata_phy_reset);
5894EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heof5914a42006-05-31 18:27:48 +09005895EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005896EXPORT_SYMBOL_GPL(ata_std_softreset);
5897EXPORT_SYMBOL_GPL(sata_std_hardreset);
5898EXPORT_SYMBOL_GPL(ata_std_postreset);
Tejun Heo623a3122006-03-05 17:55:58 +09005899EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005900EXPORT_SYMBOL_GPL(ata_dev_classify);
5901EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005903EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09005904EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005905EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09005906EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5908EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09005910EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09005911EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912EXPORT_SYMBOL_GPL(ata_scsi_release);
5913EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo34bf2172006-05-15 20:57:46 +09005914EXPORT_SYMBOL_GPL(sata_scr_valid);
5915EXPORT_SYMBOL_GPL(sata_scr_read);
5916EXPORT_SYMBOL_GPL(sata_scr_write);
5917EXPORT_SYMBOL_GPL(sata_scr_write_flush);
5918EXPORT_SYMBOL_GPL(ata_port_online);
5919EXPORT_SYMBOL_GPL(ata_port_offline);
Tejun Heo6a62a042006-02-13 10:02:46 +09005920EXPORT_SYMBOL_GPL(ata_id_string);
5921EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5923
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005924EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005925EXPORT_SYMBOL_GPL(ata_timing_compute);
5926EXPORT_SYMBOL_GPL(ata_timing_merge);
5927
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928#ifdef CONFIG_PCI
5929EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005930EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5932EXPORT_SYMBOL_GPL(ata_pci_init_one);
5933EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005934EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5935EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Alan Cox67951ad2006-03-22 15:55:54 +00005936EXPORT_SYMBOL_GPL(ata_pci_default_filter);
5937EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005939
5940EXPORT_SYMBOL_GPL(ata_device_suspend);
5941EXPORT_SYMBOL_GPL(ata_device_resume);
5942EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5943EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
Tejun Heoece1d632006-04-02 18:51:53 +09005944
Tejun Heoece1d632006-04-02 18:51:53 +09005945EXPORT_SYMBOL_GPL(ata_eng_timeout);
Tejun Heo7b70fc02006-05-15 20:58:07 +09005946EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
5947EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09005948EXPORT_SYMBOL_GPL(ata_port_freeze);
5949EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
5950EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09005951EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5952EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09005953EXPORT_SYMBOL_GPL(ata_do_eh);