blob: 68fa64d24721a908e980e2ac23787b0519a1ceb3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040051#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100052#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "scsi_priv.h"
Jeff Garzik193515d2005-11-07 00:59:37 -050055#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
Tejun Heo6aff8f12006-02-15 18:24:09 +090064static unsigned int ata_dev_init_params(struct ata_port *ap,
Albert Lee00b6f5e2006-03-27 16:39:18 +080065 struct ata_device *dev,
66 u16 heads,
67 u16 sectors);
Tejun Heocf176e12006-04-02 17:54:46 +090068static int ata_down_xfermask_limit(struct ata_port *ap, struct ata_device *dev,
69 int force_pio0);
Tejun Heo1c3fae42006-04-02 20:53:28 +090070static int ata_down_sata_spd_limit(struct ata_port *ap);
Tejun Heoe82cbdb2006-04-01 01:38:18 +090071static int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev);
Tejun Heo83206a22006-03-24 15:25:31 +090072static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
73 struct ata_device *dev);
Tejun Heoacf356b2006-03-24 14:07:50 +090074static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76static unsigned int ata_unique_id = 1;
77static struct workqueue_struct *ata_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
Jeff Garzikc3c013a2006-02-27 22:31:19 -050083int libata_fua = 0;
84module_param_named(fua, libata_fua, int, 0444);
85MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087MODULE_AUTHOR("Jeff Garzik");
88MODULE_DESCRIPTION("Library module for ATA devices");
89MODULE_LICENSE("GPL");
90MODULE_VERSION(DRV_VERSION);
91
Edward Falk0baab862005-06-02 18:17:13 -040092
93/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
95 * @tf: Taskfile to convert
96 * @fis: Buffer into which data will output
97 * @pmp: Port multiplier port
98 *
99 * Converts a standard ATA taskfile to a Serial ATA
100 * FIS structure (Register - Host to Device).
101 *
102 * LOCKING:
103 * Inherited from caller.
104 */
105
Jeff Garzik057ace52005-10-22 14:27:05 -0400106void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 fis[0] = 0x27; /* Register - Host to Device FIS */
109 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
110 bit 7 indicates Command FIS */
111 fis[2] = tf->command;
112 fis[3] = tf->feature;
113
114 fis[4] = tf->lbal;
115 fis[5] = tf->lbam;
116 fis[6] = tf->lbah;
117 fis[7] = tf->device;
118
119 fis[8] = tf->hob_lbal;
120 fis[9] = tf->hob_lbam;
121 fis[10] = tf->hob_lbah;
122 fis[11] = tf->hob_feature;
123
124 fis[12] = tf->nsect;
125 fis[13] = tf->hob_nsect;
126 fis[14] = 0;
127 fis[15] = tf->ctl;
128
129 fis[16] = 0;
130 fis[17] = 0;
131 fis[18] = 0;
132 fis[19] = 0;
133}
134
135/**
136 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
137 * @fis: Buffer from which data will be input
138 * @tf: Taskfile to output
139 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500140 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 *
142 * LOCKING:
143 * Inherited from caller.
144 */
145
Jeff Garzik057ace52005-10-22 14:27:05 -0400146void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 tf->command = fis[2]; /* status */
149 tf->feature = fis[3]; /* error */
150
151 tf->lbal = fis[4];
152 tf->lbam = fis[5];
153 tf->lbah = fis[6];
154 tf->device = fis[7];
155
156 tf->hob_lbal = fis[8];
157 tf->hob_lbam = fis[9];
158 tf->hob_lbah = fis[10];
159
160 tf->nsect = fis[12];
161 tf->hob_nsect = fis[13];
162}
163
Albert Lee8cbd6df2005-10-12 15:06:27 +0800164static const u8 ata_rw_cmds[] = {
165 /* pio multi */
166 ATA_CMD_READ_MULTI,
167 ATA_CMD_WRITE_MULTI,
168 ATA_CMD_READ_MULTI_EXT,
169 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100170 0,
171 0,
172 0,
173 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800174 /* pio */
175 ATA_CMD_PIO_READ,
176 ATA_CMD_PIO_WRITE,
177 ATA_CMD_PIO_READ_EXT,
178 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100179 0,
180 0,
181 0,
182 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800183 /* dma */
184 ATA_CMD_READ,
185 ATA_CMD_WRITE,
186 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100187 ATA_CMD_WRITE_EXT,
188 0,
189 0,
190 0,
191 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800192};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800195 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
196 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500198 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800199 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
201 * LOCKING:
202 * caller.
203 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100204int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800206 struct ata_taskfile *tf = &qc->tf;
207 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100208 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100210 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500211
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100212 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800213 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
214 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Albert Lee8cbd6df2005-10-12 15:06:27 +0800216 if (dev->flags & ATA_DFLAG_PIO) {
217 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100218 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000219 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
220 /* Unable to use DMA due to host limitation */
221 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800222 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800223 } else {
224 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100225 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100228 cmd = ata_rw_cmds[index + fua + lba48 + write];
229 if (cmd) {
230 tf->command = cmd;
231 return 0;
232 }
233 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Tejun Heocb95d562006-03-06 04:31:56 +0900236/**
237 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
238 * @pio_mask: pio_mask
239 * @mwdma_mask: mwdma_mask
240 * @udma_mask: udma_mask
241 *
242 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
243 * unsigned int xfer_mask.
244 *
245 * LOCKING:
246 * None.
247 *
248 * RETURNS:
249 * Packed xfer_mask.
250 */
251static unsigned int ata_pack_xfermask(unsigned int pio_mask,
252 unsigned int mwdma_mask,
253 unsigned int udma_mask)
254{
255 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
256 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
257 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
258}
259
Tejun Heoc0489e42006-03-24 14:07:49 +0900260/**
261 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
262 * @xfer_mask: xfer_mask to unpack
263 * @pio_mask: resulting pio_mask
264 * @mwdma_mask: resulting mwdma_mask
265 * @udma_mask: resulting udma_mask
266 *
267 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
268 * Any NULL distination masks will be ignored.
269 */
270static void ata_unpack_xfermask(unsigned int xfer_mask,
271 unsigned int *pio_mask,
272 unsigned int *mwdma_mask,
273 unsigned int *udma_mask)
274{
275 if (pio_mask)
276 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
277 if (mwdma_mask)
278 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
279 if (udma_mask)
280 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
281}
282
Tejun Heocb95d562006-03-06 04:31:56 +0900283static const struct ata_xfer_ent {
Tejun Heobe9a50c82006-03-31 22:48:52 +0900284 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900285 u8 base;
286} ata_xfer_tbl[] = {
287 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
288 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
289 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
290 { -1, },
291};
292
293/**
294 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
295 * @xfer_mask: xfer_mask of interest
296 *
297 * Return matching XFER_* value for @xfer_mask. Only the highest
298 * bit of @xfer_mask is considered.
299 *
300 * LOCKING:
301 * None.
302 *
303 * RETURNS:
304 * Matching XFER_* value, 0 if no match found.
305 */
306static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
307{
308 int highbit = fls(xfer_mask) - 1;
309 const struct ata_xfer_ent *ent;
310
311 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
312 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
313 return ent->base + highbit - ent->shift;
314 return 0;
315}
316
317/**
318 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
319 * @xfer_mode: XFER_* of interest
320 *
321 * Return matching xfer_mask for @xfer_mode.
322 *
323 * LOCKING:
324 * None.
325 *
326 * RETURNS:
327 * Matching xfer_mask, 0 if no match found.
328 */
329static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
330{
331 const struct ata_xfer_ent *ent;
332
333 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
334 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
335 return 1 << (ent->shift + xfer_mode - ent->base);
336 return 0;
337}
338
339/**
340 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
341 * @xfer_mode: XFER_* of interest
342 *
343 * Return matching xfer_shift for @xfer_mode.
344 *
345 * LOCKING:
346 * None.
347 *
348 * RETURNS:
349 * Matching xfer_shift, -1 if no match found.
350 */
351static int ata_xfer_mode2shift(unsigned int xfer_mode)
352{
353 const struct ata_xfer_ent *ent;
354
355 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
356 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
357 return ent->shift;
358 return -1;
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900362 * ata_mode_string - convert xfer_mask to string
363 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 *
365 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900366 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
368 * LOCKING:
369 * None.
370 *
371 * RETURNS:
372 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900373 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900375static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Tejun Heo75f554b2006-03-06 04:31:57 +0900377 static const char * const xfer_mode_str[] = {
378 "PIO0",
379 "PIO1",
380 "PIO2",
381 "PIO3",
382 "PIO4",
383 "MWDMA0",
384 "MWDMA1",
385 "MWDMA2",
386 "UDMA/16",
387 "UDMA/25",
388 "UDMA/33",
389 "UDMA/44",
390 "UDMA/66",
391 "UDMA/100",
392 "UDMA/133",
393 "UDMA7",
394 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900395 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900397 highbit = fls(xfer_mask) - 1;
398 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
399 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Tejun Heo4c360c82006-04-01 01:38:17 +0900403static const char *sata_spd_string(unsigned int spd)
404{
405 static const char * const spd_str[] = {
406 "1.5 Gbps",
407 "3.0 Gbps",
408 };
409
410 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
411 return "<unknown>";
412 return spd_str[spd - 1];
413}
414
Tejun Heo0b8efb02006-03-24 15:25:31 +0900415static void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
416{
Tejun Heoe1211e32006-04-01 01:38:18 +0900417 if (ata_dev_enabled(dev)) {
Tejun Heo0b8efb02006-03-24 15:25:31 +0900418 printk(KERN_WARNING "ata%u: dev %u disabled\n",
419 ap->id, dev->devno);
420 dev->class++;
421 }
422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/**
425 * ata_pio_devchk - PATA device presence detection
426 * @ap: ATA channel to examine
427 * @device: Device to examine (starting at zero)
428 *
429 * This technique was originally described in
430 * Hale Landis's ATADRVR (www.ata-atapi.com), and
431 * later found its way into the ATA/ATAPI spec.
432 *
433 * Write a pattern to the ATA shadow registers,
434 * and if a device is present, it will respond by
435 * correctly storing and echoing back the
436 * ATA shadow register contents.
437 *
438 * LOCKING:
439 * caller.
440 */
441
442static unsigned int ata_pio_devchk(struct ata_port *ap,
443 unsigned int device)
444{
445 struct ata_ioports *ioaddr = &ap->ioaddr;
446 u8 nsect, lbal;
447
448 ap->ops->dev_select(ap, device);
449
450 outb(0x55, ioaddr->nsect_addr);
451 outb(0xaa, ioaddr->lbal_addr);
452
453 outb(0xaa, ioaddr->nsect_addr);
454 outb(0x55, ioaddr->lbal_addr);
455
456 outb(0x55, ioaddr->nsect_addr);
457 outb(0xaa, ioaddr->lbal_addr);
458
459 nsect = inb(ioaddr->nsect_addr);
460 lbal = inb(ioaddr->lbal_addr);
461
462 if ((nsect == 0x55) && (lbal == 0xaa))
463 return 1; /* we found a device */
464
465 return 0; /* nothing found */
466}
467
468/**
469 * ata_mmio_devchk - PATA device presence detection
470 * @ap: ATA channel to examine
471 * @device: Device to examine (starting at zero)
472 *
473 * This technique was originally described in
474 * Hale Landis's ATADRVR (www.ata-atapi.com), and
475 * later found its way into the ATA/ATAPI spec.
476 *
477 * Write a pattern to the ATA shadow registers,
478 * and if a device is present, it will respond by
479 * correctly storing and echoing back the
480 * ATA shadow register contents.
481 *
482 * LOCKING:
483 * caller.
484 */
485
486static unsigned int ata_mmio_devchk(struct ata_port *ap,
487 unsigned int device)
488{
489 struct ata_ioports *ioaddr = &ap->ioaddr;
490 u8 nsect, lbal;
491
492 ap->ops->dev_select(ap, device);
493
494 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
495 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
496
497 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
498 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
499
500 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
501 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
502
503 nsect = readb((void __iomem *) ioaddr->nsect_addr);
504 lbal = readb((void __iomem *) ioaddr->lbal_addr);
505
506 if ((nsect == 0x55) && (lbal == 0xaa))
507 return 1; /* we found a device */
508
509 return 0; /* nothing found */
510}
511
512/**
513 * ata_devchk - PATA device presence detection
514 * @ap: ATA channel to examine
515 * @device: Device to examine (starting at zero)
516 *
517 * Dispatch ATA device presence detection, depending
518 * on whether we are using PIO or MMIO to talk to the
519 * ATA shadow registers.
520 *
521 * LOCKING:
522 * caller.
523 */
524
525static unsigned int ata_devchk(struct ata_port *ap,
526 unsigned int device)
527{
528 if (ap->flags & ATA_FLAG_MMIO)
529 return ata_mmio_devchk(ap, device);
530 return ata_pio_devchk(ap, device);
531}
532
533/**
534 * ata_dev_classify - determine device type based on ATA-spec signature
535 * @tf: ATA taskfile register set for device to be identified
536 *
537 * Determine from taskfile register contents whether a device is
538 * ATA or ATAPI, as per "Signature and persistence" section
539 * of ATA/PI spec (volume 1, sect 5.14).
540 *
541 * LOCKING:
542 * None.
543 *
544 * RETURNS:
545 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
546 * the event of failure.
547 */
548
Jeff Garzik057ace52005-10-22 14:27:05 -0400549unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 /* Apple's open source Darwin code hints that some devices only
552 * put a proper signature into the LBA mid/high registers,
553 * So, we only check those. It's sufficient for uniqueness.
554 */
555
556 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
557 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
558 DPRINTK("found ATA device by sig\n");
559 return ATA_DEV_ATA;
560 }
561
562 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
563 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
564 DPRINTK("found ATAPI device by sig\n");
565 return ATA_DEV_ATAPI;
566 }
567
568 DPRINTK("unknown device\n");
569 return ATA_DEV_UNKNOWN;
570}
571
572/**
573 * ata_dev_try_classify - Parse returned ATA device signature
574 * @ap: ATA channel to examine
575 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900576 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *
578 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
579 * an ATA/ATAPI-defined set of values is placed in the ATA
580 * shadow registers, indicating the results of device detection
581 * and diagnostics.
582 *
583 * Select the ATA device, and read the values from the ATA shadow
584 * registers. Then parse according to the Error register value,
585 * and the spec-defined values examined by ata_dev_classify().
586 *
587 * LOCKING:
588 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900589 *
590 * RETURNS:
591 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
593
Tejun Heob4dc7622006-01-24 17:05:22 +0900594static unsigned int
595ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct ata_taskfile tf;
598 unsigned int class;
599 u8 err;
600
601 ap->ops->dev_select(ap, device);
602
603 memset(&tf, 0, sizeof(tf));
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400606 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900607 if (r_err)
608 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* see if device passed diags */
611 if (err == 1)
612 /* do nothing */ ;
613 else if ((device == 0) && (err == 0x81))
614 /* do nothing */ ;
615 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900616 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Tejun Heob4dc7622006-01-24 17:05:22 +0900618 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900622 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900624 return ATA_DEV_NONE;
625 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900629 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * @id: IDENTIFY DEVICE results we will examine
631 * @s: string into which data is output
632 * @ofs: offset into identify device page
633 * @len: length of string to return. must be an even number.
634 *
635 * The strings in the IDENTIFY DEVICE page are broken up into
636 * 16-bit chunks. Run through the string, and output each
637 * 8-bit chunk linearly, regardless of platform.
638 *
639 * LOCKING:
640 * caller.
641 */
642
Tejun Heo6a62a042006-02-13 10:02:46 +0900643void ata_id_string(const u16 *id, unsigned char *s,
644 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 unsigned int c;
647
648 while (len > 0) {
649 c = id[ofs] >> 8;
650 *s = c;
651 s++;
652
653 c = id[ofs] & 0xff;
654 *s = c;
655 s++;
656
657 ofs++;
658 len -= 2;
659 }
660}
661
Tejun Heo0e949ff2006-02-12 22:47:04 +0900662/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900663 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900664 * @id: IDENTIFY DEVICE results we will examine
665 * @s: string into which data is output
666 * @ofs: offset into identify device page
667 * @len: length of string to return. must be an odd number.
668 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900669 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900670 * trims trailing spaces and terminates the resulting string with
671 * null. @len must be actual maximum length (even number) + 1.
672 *
673 * LOCKING:
674 * caller.
675 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900676void ata_id_c_string(const u16 *id, unsigned char *s,
677 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900678{
679 unsigned char *p;
680
681 WARN_ON(!(len & 1));
682
Tejun Heo6a62a042006-02-13 10:02:46 +0900683 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900684
685 p = s + strnlen(s, len - 1);
686 while (p > s && p[-1] == ' ')
687 p--;
688 *p = '\0';
689}
Edward Falk0baab862005-06-02 18:17:13 -0400690
Tejun Heo29407402006-02-12 22:47:04 +0900691static u64 ata_id_n_sectors(const u16 *id)
692{
693 if (ata_id_has_lba(id)) {
694 if (ata_id_has_lba48(id))
695 return ata_id_u64(id, 100);
696 else
697 return ata_id_u32(id, 60);
698 } else {
699 if (ata_id_current_chs_valid(id))
700 return ata_id_u32(id, 57);
701 else
702 return id[1] * id[3] * id[6];
703 }
704}
705
Edward Falk0baab862005-06-02 18:17:13 -0400706/**
707 * ata_noop_dev_select - Select device 0/1 on ATA bus
708 * @ap: ATA channel to manipulate
709 * @device: ATA device (numbered from zero) to select
710 *
711 * This function performs no actual function.
712 *
713 * May be used as the dev_select() entry in ata_port_operations.
714 *
715 * LOCKING:
716 * caller.
717 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
719{
720}
721
Edward Falk0baab862005-06-02 18:17:13 -0400722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723/**
724 * ata_std_dev_select - Select device 0/1 on ATA bus
725 * @ap: ATA channel to manipulate
726 * @device: ATA device (numbered from zero) to select
727 *
728 * Use the method defined in the ATA specification to
729 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400730 * ATA channel. Works with both PIO and MMIO.
731 *
732 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *
734 * LOCKING:
735 * caller.
736 */
737
738void ata_std_dev_select (struct ata_port *ap, unsigned int device)
739{
740 u8 tmp;
741
742 if (device == 0)
743 tmp = ATA_DEVICE_OBS;
744 else
745 tmp = ATA_DEVICE_OBS | ATA_DEV1;
746
747 if (ap->flags & ATA_FLAG_MMIO) {
748 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
749 } else {
750 outb(tmp, ap->ioaddr.device_addr);
751 }
752 ata_pause(ap); /* needed; also flushes, for mmio */
753}
754
755/**
756 * ata_dev_select - Select device 0/1 on ATA bus
757 * @ap: ATA channel to manipulate
758 * @device: ATA device (numbered from zero) to select
759 * @wait: non-zero to wait for Status register BSY bit to clear
760 * @can_sleep: non-zero if context allows sleeping
761 *
762 * Use the method defined in the ATA specification to
763 * make either device 0, or device 1, active on the
764 * ATA channel.
765 *
766 * This is a high-level version of ata_std_dev_select(),
767 * which additionally provides the services of inserting
768 * the proper pauses and status polling, where needed.
769 *
770 * LOCKING:
771 * caller.
772 */
773
774void ata_dev_select(struct ata_port *ap, unsigned int device,
775 unsigned int wait, unsigned int can_sleep)
776{
777 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
778 ap->id, device, wait);
779
780 if (wait)
781 ata_wait_idle(ap);
782
783 ap->ops->dev_select(ap, device);
784
785 if (wait) {
786 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
787 msleep(150);
788 ata_wait_idle(ap);
789 }
790}
791
792/**
793 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900794 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900796 * Dump selected 16-bit words from the given IDENTIFY DEVICE
797 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 *
799 * LOCKING:
800 * caller.
801 */
802
Tejun Heo0bd33002006-02-12 22:47:05 +0900803static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 DPRINTK("49==0x%04x "
806 "53==0x%04x "
807 "63==0x%04x "
808 "64==0x%04x "
809 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900810 id[49],
811 id[53],
812 id[63],
813 id[64],
814 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 DPRINTK("80==0x%04x "
816 "81==0x%04x "
817 "82==0x%04x "
818 "83==0x%04x "
819 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900820 id[80],
821 id[81],
822 id[82],
823 id[83],
824 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 DPRINTK("88==0x%04x "
826 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900827 id[88],
828 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Tejun Heocb95d562006-03-06 04:31:56 +0900831/**
832 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
833 * @id: IDENTIFY data to compute xfer mask from
834 *
835 * Compute the xfermask for this device. This is not as trivial
836 * as it seems if we must consider early devices correctly.
837 *
838 * FIXME: pre IDE drive timing (do we care ?).
839 *
840 * LOCKING:
841 * None.
842 *
843 * RETURNS:
844 * Computed xfermask
845 */
846static unsigned int ata_id_xfermask(const u16 *id)
847{
848 unsigned int pio_mask, mwdma_mask, udma_mask;
849
850 /* Usual case. Word 53 indicates word 64 is valid */
851 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
852 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
853 pio_mask <<= 3;
854 pio_mask |= 0x7;
855 } else {
856 /* If word 64 isn't valid then Word 51 high byte holds
857 * the PIO timing number for the maximum. Turn it into
858 * a mask.
859 */
860 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
861
862 /* But wait.. there's more. Design your standards by
863 * committee and you too can get a free iordy field to
864 * process. However its the speeds not the modes that
865 * are supported... Note drivers using the timing API
866 * will get this right anyway
867 */
868 }
869
870 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +0900871
872 udma_mask = 0;
873 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
874 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900875
876 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
877}
878
Tejun Heo86e45b62006-03-05 15:29:09 +0900879/**
880 * ata_port_queue_task - Queue port_task
881 * @ap: The ata_port to queue port_task for
882 *
883 * Schedule @fn(@data) for execution after @delay jiffies using
884 * port_task. There is one port_task per port and it's the
885 * user(low level driver)'s responsibility to make sure that only
886 * one task is active at any given time.
887 *
888 * libata core layer takes care of synchronization between
889 * port_task and EH. ata_port_queue_task() may be ignored for EH
890 * synchronization.
891 *
892 * LOCKING:
893 * Inherited from caller.
894 */
895void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
896 unsigned long delay)
897{
898 int rc;
899
Tejun Heo2e755f62006-03-05 15:29:09 +0900900 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900901 return;
902
903 PREPARE_WORK(&ap->port_task, fn, data);
904
905 if (!delay)
906 rc = queue_work(ata_wq, &ap->port_task);
907 else
908 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
909
910 /* rc == 0 means that another user is using port task */
911 WARN_ON(rc == 0);
912}
913
914/**
915 * ata_port_flush_task - Flush port_task
916 * @ap: The ata_port to flush port_task for
917 *
918 * After this function completes, port_task is guranteed not to
919 * be running or scheduled.
920 *
921 * LOCKING:
922 * Kernel thread context (may sleep)
923 */
924void ata_port_flush_task(struct ata_port *ap)
925{
926 unsigned long flags;
927
928 DPRINTK("ENTER\n");
929
930 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900931 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900932 spin_unlock_irqrestore(&ap->host_set->lock, flags);
933
934 DPRINTK("flush #1\n");
935 flush_workqueue(ata_wq);
936
937 /*
938 * At this point, if a task is running, it's guaranteed to see
939 * the FLUSH flag; thus, it will never queue pio tasks again.
940 * Cancel and flush.
941 */
942 if (!cancel_delayed_work(&ap->port_task)) {
943 DPRINTK("flush #2\n");
944 flush_workqueue(ata_wq);
945 }
946
947 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900948 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900949 spin_unlock_irqrestore(&ap->host_set->lock, flags);
950
951 DPRINTK("EXIT\n");
952}
953
Tejun Heo77853bf2006-01-23 13:09:36 +0900954void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900955{
Tejun Heo77853bf2006-01-23 13:09:36 +0900956 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900957
Tejun Heo77853bf2006-01-23 13:09:36 +0900958 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900959 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900960}
961
962/**
963 * ata_exec_internal - execute libata internal command
964 * @ap: Port to which the command is sent
965 * @dev: Device to which the command is sent
966 * @tf: Taskfile registers for the command and the result
967 * @dma_dir: Data tranfer direction of the command
968 * @buf: Data buffer of the command
969 * @buflen: Length of data buffer
970 *
971 * Executes libata internal command with timeout. @tf contains
972 * command on entry and result on return. Timeout and error
973 * conditions are reported via return value. No recovery action
974 * is taken after a command times out. It's caller's duty to
975 * clean up after timeout.
976 *
977 * LOCKING:
978 * None. Should be called with kernel context, might sleep.
979 */
980
981static unsigned
982ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
983 struct ata_taskfile *tf,
984 int dma_dir, void *buf, unsigned int buflen)
985{
986 u8 command = tf->command;
987 struct ata_queued_cmd *qc;
988 DECLARE_COMPLETION(wait);
989 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900990 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900991
992 spin_lock_irqsave(&ap->host_set->lock, flags);
993
994 qc = ata_qc_new_init(ap, dev);
995 BUG_ON(qc == NULL);
996
997 qc->tf = *tf;
998 qc->dma_dir = dma_dir;
999 if (dma_dir != DMA_NONE) {
1000 ata_sg_init_one(qc, buf, buflen);
1001 qc->nsect = buflen / ATA_SECT_SIZE;
1002 }
1003
Tejun Heo77853bf2006-01-23 13:09:36 +09001004 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001005 qc->complete_fn = ata_qc_complete_internal;
1006
Tejun Heo8e0e6942006-03-31 20:41:11 +09001007 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001008
1009 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1010
1011 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
Albert Lee41ade502006-03-14 11:19:04 +08001012 ata_port_flush_task(ap);
1013
Tejun Heoa2a7a662005-12-13 14:48:31 +09001014 spin_lock_irqsave(&ap->host_set->lock, flags);
1015
1016 /* We're racing with irq here. If we lose, the
1017 * following test prevents us from completing the qc
1018 * again. If completion irq occurs after here but
1019 * before the caller cleans up, it will result in a
1020 * spurious interrupt. We can live with that.
1021 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001022 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001023 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001024 ata_qc_complete(qc);
1025 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1026 ap->id, command);
1027 }
1028
1029 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1030 }
1031
Tejun Heo77853bf2006-01-23 13:09:36 +09001032 *tf = qc->tf;
1033 err_mask = qc->err_mask;
1034
1035 ata_qc_free(qc);
1036
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001037 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1038 * Until those drivers are fixed, we detect the condition
1039 * here, fail the command with AC_ERR_SYSTEM and reenable the
1040 * port.
1041 *
1042 * Note that this doesn't change any behavior as internal
1043 * command failure results in disabling the device in the
1044 * higher layer for LLDDs without new reset/EH callbacks.
1045 *
1046 * Kill the following code as soon as those drivers are fixed.
1047 */
1048 if (ap->flags & ATA_FLAG_PORT_DISABLED) {
1049 err_mask |= AC_ERR_SYSTEM;
1050 ata_port_probe(ap);
1051 }
1052
Tejun Heo77853bf2006-01-23 13:09:36 +09001053 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001057 * ata_pio_need_iordy - check if iordy needed
1058 * @adev: ATA device
1059 *
1060 * Check if the current speed of the device requires IORDY. Used
1061 * by various controllers for chip configuration.
1062 */
1063
1064unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1065{
1066 int pio;
1067 int speed = adev->pio_mode - XFER_PIO_0;
1068
1069 if (speed < 2)
1070 return 0;
1071 if (speed > 2)
1072 return 1;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001073
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001074 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1075
1076 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1077 pio = adev->id[ATA_ID_EIDE_PIO];
1078 /* Is the speed faster than the drive allows non IORDY ? */
1079 if (pio) {
1080 /* This is cycle times not frequency - watch the logic! */
1081 if (pio > 240) /* PIO2 is 240nS per cycle */
1082 return 1;
1083 return 0;
1084 }
1085 }
1086 return 0;
1087}
1088
1089/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001090 * ata_dev_read_id - Read ID data from the specified device
1091 * @ap: port on which target device resides
1092 * @dev: target device
1093 * @p_class: pointer to class of the target device (may be changed)
1094 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +09001095 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +09001096 *
1097 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1098 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001099 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1100 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001101 *
1102 * LOCKING:
1103 * Kernel thread context (may sleep)
1104 *
1105 * RETURNS:
1106 * 0 on success, -errno otherwise.
1107 */
1108static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +09001109 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001110{
1111 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001112 struct ata_taskfile tf;
1113 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +09001114 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001115 const char *reason;
1116 int rc;
1117
1118 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1119
Tejun Heo49016ac2006-02-21 02:12:11 +09001120 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1121
Tejun Heod9572b12006-03-01 16:09:35 +09001122 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1123 if (id == NULL) {
1124 rc = -ENOMEM;
1125 reason = "out of memory";
1126 goto err_out;
1127 }
1128
Tejun Heo49016ac2006-02-21 02:12:11 +09001129 retry:
1130 ata_tf_init(ap, &tf, dev->devno);
1131
1132 switch (class) {
1133 case ATA_DEV_ATA:
1134 tf.command = ATA_CMD_ID_ATA;
1135 break;
1136 case ATA_DEV_ATAPI:
1137 tf.command = ATA_CMD_ID_ATAPI;
1138 break;
1139 default:
1140 rc = -ENODEV;
1141 reason = "unsupported class";
1142 goto err_out;
1143 }
1144
1145 tf.protocol = ATA_PROT_PIO;
1146
1147 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1148 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001149 if (err_mask) {
1150 rc = -EIO;
1151 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001152 goto err_out;
1153 }
1154
1155 swap_buf_le16(id, ATA_ID_WORDS);
1156
Tejun Heo49016ac2006-02-21 02:12:11 +09001157 /* sanity check */
Alan Cox692785e2006-03-27 18:49:19 +01001158 if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001159 rc = -EINVAL;
1160 reason = "device reports illegal type";
1161 goto err_out;
1162 }
1163
1164 if (post_reset && class == ATA_DEV_ATA) {
1165 /*
1166 * The exact sequence expected by certain pre-ATA4 drives is:
1167 * SRST RESET
1168 * IDENTIFY
1169 * INITIALIZE DEVICE PARAMETERS
1170 * anything else..
1171 * Some drives were very specific about that exact sequence.
1172 */
1173 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Albert Lee00b6f5e2006-03-27 16:39:18 +08001174 err_mask = ata_dev_init_params(ap, dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001175 if (err_mask) {
1176 rc = -EIO;
1177 reason = "INIT_DEV_PARAMS failed";
1178 goto err_out;
1179 }
1180
1181 /* current CHS translation info (id[53-58]) might be
1182 * changed. reread the identify device info.
1183 */
1184 post_reset = 0;
1185 goto retry;
1186 }
1187 }
1188
1189 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001190 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001191 return 0;
1192
1193 err_out:
1194 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1195 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001196 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001197 return rc;
1198}
1199
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001200static inline u8 ata_dev_knobble(const struct ata_port *ap,
1201 struct ata_device *dev)
1202{
1203 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1204}
1205
Tejun Heo49016ac2006-02-21 02:12:11 +09001206/**
Tejun Heoffeae412006-03-01 16:09:35 +09001207 * ata_dev_configure - Configure the specified ATA/ATAPI device
1208 * @ap: Port on which target device resides
1209 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001210 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 *
Tejun Heoffeae412006-03-01 16:09:35 +09001212 * Configure @dev according to @dev->id. Generic and low-level
1213 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 *
1215 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001216 * Kernel thread context (may sleep)
1217 *
1218 * RETURNS:
1219 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001221static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1222 int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Tejun Heo1148c3a2006-03-13 19:48:04 +09001224 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001225 unsigned int xfer_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001226 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Tejun Heoe1211e32006-04-01 01:38:18 +09001228 if (!ata_dev_enabled(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001230 ap->id, dev->devno);
1231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
Tejun Heoffeae412006-03-01 16:09:35 +09001234 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001236 /* print device capabilities */
1237 if (print_info)
1238 printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
1239 "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1240 ap->id, dev->devno, id[49], id[82], id[83],
1241 id[84], id[85], id[86], id[87], id[88]);
1242
Tejun Heo208a9932006-03-05 17:55:58 +09001243 /* initialize to-be-configured parameters */
1244 dev->flags = 0;
1245 dev->max_sectors = 0;
1246 dev->cdb_len = 0;
1247 dev->n_sectors = 0;
1248 dev->cylinders = 0;
1249 dev->heads = 0;
1250 dev->sectors = 0;
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /*
1253 * common ATA, ATAPI feature tests
1254 */
1255
Tejun Heoff8854b2006-03-06 04:31:56 +09001256 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001257 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Tejun Heo1148c3a2006-03-13 19:48:04 +09001259 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 /* ATA-specific feature tests */
1262 if (dev->class == ATA_DEV_ATA) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001263 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001264
Tejun Heo1148c3a2006-03-13 19:48:04 +09001265 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001266 const char *lba_desc;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001267
Tejun Heo4c2d7212006-03-05 17:55:58 +09001268 lba_desc = "LBA";
1269 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001270 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001271 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001272 lba_desc = "LBA48";
1273 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04001274
1275 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001276 if (print_info)
1277 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1278 "max %s, %Lu sectors: %s\n",
1279 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001280 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001281 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001282 (unsigned long long)dev->n_sectors,
1283 lba_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001284 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001285 /* CHS */
1286
1287 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001288 dev->cylinders = id[1];
1289 dev->heads = id[3];
1290 dev->sectors = id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001291
Tejun Heo1148c3a2006-03-13 19:48:04 +09001292 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001293 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001294 dev->cylinders = id[54];
1295 dev->heads = id[55];
1296 dev->sectors = id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001297 }
1298
1299 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001300 if (print_info)
1301 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1302 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1303 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001304 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001305 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001306 (unsigned long long)dev->n_sectors,
1307 dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
Tejun Heo6e7846e2006-02-12 23:32:58 +09001310 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
1313 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001314 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001315 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1317 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001318 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 goto err_out_nosup;
1320 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001321 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001324 if (print_info)
1325 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoff8854b2006-03-06 04:31:56 +09001326 ap->id, dev->devno, ata_mode_string(xfer_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
Tejun Heo6e7846e2006-02-12 23:32:58 +09001329 ap->host->max_cmd_len = 0;
1330 for (i = 0; i < ATA_MAX_DEVICES; i++)
1331 ap->host->max_cmd_len = max_t(unsigned int,
1332 ap->host->max_cmd_len,
1333 ap->device[i].cdb_len);
1334
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001335 /* limit bridge transfers to udma5, 200 sectors */
1336 if (ata_dev_knobble(ap, dev)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001337 if (print_info)
1338 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1339 ap->id, dev->devno);
Tejun Heo5a529132006-03-24 14:07:50 +09001340 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001341 dev->max_sectors = ATA_MAX_SECTORS;
1342 }
1343
1344 if (ap->ops->dev_config)
1345 ap->ops->dev_config(ap, dev);
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350err_out_nosup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001352 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355/**
1356 * ata_bus_probe - Reset and probe ATA bus
1357 * @ap: Bus to probe
1358 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001359 * Master ATA bus probing function. Initiates a hardware-dependent
1360 * bus reset, then attempts to identify any devices found on
1361 * the bus.
1362 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001364 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 *
1366 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09001367 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
1369
1370static int ata_bus_probe(struct ata_port *ap)
1371{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001372 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo96072e62006-04-01 01:38:17 +09001373 int i, rc, found = 0;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001374 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Tejun Heo28ca5c52006-03-01 16:09:36 +09001376 ata_port_probe(ap);
1377
Tejun Heo20444702006-03-13 01:57:01 +09001378 /* reset and determine device classes */
1379 for (i = 0; i < ATA_MAX_DEVICES; i++)
1380 classes[i] = ATA_DEV_UNKNOWN;
Tejun Heo2061a472006-03-12 00:57:39 +09001381
Tejun Heo20444702006-03-13 01:57:01 +09001382 if (ap->ops->probe_reset) {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001383 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001384 if (rc) {
1385 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1386 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001387 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001388 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001389 ap->ops->phy_reset(ap);
1390
Tejun Heo20444702006-03-13 01:57:01 +09001391 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1392 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo28ca5c52006-03-01 16:09:36 +09001393 classes[i] = ap->device[i].class;
Tejun Heo20444702006-03-13 01:57:01 +09001394
Tejun Heo28ca5c52006-03-01 16:09:36 +09001395 ata_port_probe(ap);
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Tejun Heo20444702006-03-13 01:57:01 +09001398 for (i = 0; i < ATA_MAX_DEVICES; i++)
1399 if (classes[i] == ATA_DEV_UNKNOWN)
1400 classes[i] = ATA_DEV_NONE;
1401
Tejun Heo28ca5c52006-03-01 16:09:36 +09001402 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001404 dev = &ap->device[i];
Tejun Heo28ca5c52006-03-01 16:09:36 +09001405 dev->class = classes[i];
1406
Tejun Heoe1211e32006-04-01 01:38:18 +09001407 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09001408 continue;
1409
1410 WARN_ON(dev->id != NULL);
1411 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1412 dev->class = ATA_DEV_NONE;
1413 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Tejun Heoffeae412006-03-01 16:09:35 +09001415
Tejun Heo4c2d7212006-03-05 17:55:58 +09001416 if (ata_dev_configure(ap, dev, 1)) {
Tejun Heofcef9782006-03-24 15:25:31 +09001417 ata_dev_disable(ap, dev);
Tejun Heoffeae412006-03-01 16:09:35 +09001418 continue;
1419 }
1420
Tejun Heoffeae412006-03-01 16:09:35 +09001421 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001424 /* configure transfer mode */
1425 if (ap->ops->set_mode) {
1426 /* FIXME: make ->set_mode handle no device case and
1427 * return error code and failing device on failure as
1428 * ata_set_mode() does.
1429 */
1430 if (found)
1431 ap->ops->set_mode(ap);
1432 rc = 0;
1433 } else {
1434 while (ata_set_mode(ap, &dev))
1435 ata_dev_disable(ap, dev);
1436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001438 for (i = 0; i < ATA_MAX_DEVICES; i++)
1439 if (ata_dev_enabled(&ap->device[i]))
1440 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01001441
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001442 /* no device present, disable port */
1443 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 ap->ops->port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09001445 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001449 * ata_port_probe - Mark port as enabled
1450 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001452 * Modify @ap data structure such that the system
1453 * thinks that the entire port is enabled.
1454 *
1455 * LOCKING: host_set lock, or some other form of
1456 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 */
1458
1459void ata_port_probe(struct ata_port *ap)
1460{
1461 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1462}
1463
1464/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001465 * sata_print_link_status - Print SATA link status
1466 * @ap: SATA port to printk link status about
1467 *
1468 * This function prints link speed and status of a SATA link.
1469 *
1470 * LOCKING:
1471 * None.
1472 */
1473static void sata_print_link_status(struct ata_port *ap)
1474{
1475 u32 sstatus, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09001476
1477 if (!ap->ops->scr_read)
1478 return;
1479
1480 sstatus = scr_read(ap, SCR_STATUS);
1481
1482 if (sata_dev_present(ap)) {
1483 tmp = (sstatus >> 4) & 0xf;
Tejun Heo4c360c82006-04-01 01:38:17 +09001484 printk(KERN_INFO "ata%u: SATA link up %s (SStatus %X)\n",
1485 ap->id, sata_spd_string(tmp), sstatus);
Tejun Heo3be680b2005-12-19 22:35:02 +09001486 } else {
1487 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1488 ap->id, sstatus);
1489 }
1490}
1491
1492/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001493 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1494 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001496 * This function issues commands to standard SATA Sxxx
1497 * PHY registers, to wake up the phy (and device), and
1498 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 *
1500 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001501 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 *
1503 */
1504void __sata_phy_reset(struct ata_port *ap)
1505{
1506 u32 sstatus;
1507 unsigned long timeout = jiffies + (HZ * 5);
1508
1509 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001510 /* issue phy wake/reset */
1511 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001512 /* Couldn't find anything in SATA I/II specs, but
1513 * AHCI-1.1 10.4.2 says at least 1 ms. */
1514 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
Brett Russcdcca892005-03-28 15:10:27 -05001516 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 /* wait for phy to become ready, if necessary */
1519 do {
1520 msleep(200);
1521 sstatus = scr_read(ap, SCR_STATUS);
1522 if ((sstatus & 0xf) != 1)
1523 break;
1524 } while (time_before(jiffies, timeout));
1525
Tejun Heo3be680b2005-12-19 22:35:02 +09001526 /* print link status */
1527 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001528
Tejun Heo3be680b2005-12-19 22:35:02 +09001529 /* TODO: phy layer with polling, timeouts, etc. */
1530 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001532 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1536 return;
1537
1538 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1539 ata_port_disable(ap);
1540 return;
1541 }
1542
1543 ap->cbl = ATA_CBL_SATA;
1544}
1545
1546/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001547 * sata_phy_reset - Reset SATA bus.
1548 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001550 * This function resets the SATA bus, and then probes
1551 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 *
1553 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001554 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 *
1556 */
1557void sata_phy_reset(struct ata_port *ap)
1558{
1559 __sata_phy_reset(ap);
1560 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1561 return;
1562 ata_bus_reset(ap);
1563}
1564
1565/**
Alan Coxebdfca62006-03-23 15:38:34 +00001566 * ata_dev_pair - return other device on cable
1567 * @ap: port
1568 * @adev: device
1569 *
1570 * Obtain the other device on the same cable, or if none is
1571 * present NULL is returned
1572 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001573
Alan Coxebdfca62006-03-23 15:38:34 +00001574struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev)
1575{
1576 struct ata_device *pair = &ap->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09001577 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00001578 return NULL;
1579 return pair;
1580}
1581
1582/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001583 * ata_port_disable - Disable port.
1584 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001586 * Modify @ap data structure such that the system
1587 * thinks that the entire port is disabled, and should
1588 * never attempt to probe or communicate with devices
1589 * on this port.
1590 *
1591 * LOCKING: host_set lock, or some other form of
1592 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 */
1594
1595void ata_port_disable(struct ata_port *ap)
1596{
1597 ap->device[0].class = ATA_DEV_NONE;
1598 ap->device[1].class = ATA_DEV_NONE;
1599 ap->flags |= ATA_FLAG_PORT_DISABLED;
1600}
1601
Tejun Heo1c3fae42006-04-02 20:53:28 +09001602/**
1603 * ata_down_sata_spd_limit - adjust SATA spd limit downward
1604 * @ap: Port to adjust SATA spd limit for
1605 *
1606 * Adjust SATA spd limit of @ap downward. Note that this
1607 * function only adjusts the limit. The change must be applied
1608 * using ata_set_sata_spd().
1609 *
1610 * LOCKING:
1611 * Inherited from caller.
1612 *
1613 * RETURNS:
1614 * 0 on success, negative errno on failure
1615 */
1616static int ata_down_sata_spd_limit(struct ata_port *ap)
1617{
1618 u32 spd, mask;
1619 int highbit;
1620
1621 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1622 return -EOPNOTSUPP;
1623
1624 mask = ap->sata_spd_limit;
1625 if (mask <= 1)
1626 return -EINVAL;
1627 highbit = fls(mask) - 1;
1628 mask &= ~(1 << highbit);
1629
1630 spd = (scr_read(ap, SCR_STATUS) >> 4) & 0xf;
1631 if (spd <= 1)
1632 return -EINVAL;
1633 spd--;
1634 mask &= (1 << spd) - 1;
1635 if (!mask)
1636 return -EINVAL;
1637
1638 ap->sata_spd_limit = mask;
1639
1640 printk(KERN_WARNING "ata%u: limiting SATA link speed to %s\n",
1641 ap->id, sata_spd_string(fls(mask)));
1642
1643 return 0;
1644}
1645
1646static int __ata_set_sata_spd_needed(struct ata_port *ap, u32 *scontrol)
1647{
1648 u32 spd, limit;
1649
1650 if (ap->sata_spd_limit == UINT_MAX)
1651 limit = 0;
1652 else
1653 limit = fls(ap->sata_spd_limit);
1654
1655 spd = (*scontrol >> 4) & 0xf;
1656 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
1657
1658 return spd != limit;
1659}
1660
1661/**
1662 * ata_set_sata_spd_needed - is SATA spd configuration needed
1663 * @ap: Port in question
1664 *
1665 * Test whether the spd limit in SControl matches
1666 * @ap->sata_spd_limit. This function is used to determine
1667 * whether hardreset is necessary to apply SATA spd
1668 * configuration.
1669 *
1670 * LOCKING:
1671 * Inherited from caller.
1672 *
1673 * RETURNS:
1674 * 1 if SATA spd configuration is needed, 0 otherwise.
1675 */
1676static int ata_set_sata_spd_needed(struct ata_port *ap)
1677{
1678 u32 scontrol;
1679
1680 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1681 return 0;
1682
1683 scontrol = scr_read(ap, SCR_CONTROL);
1684
1685 return __ata_set_sata_spd_needed(ap, &scontrol);
1686}
1687
1688/**
1689 * ata_set_sata_spd - set SATA spd according to spd limit
1690 * @ap: Port to set SATA spd for
1691 *
1692 * Set SATA spd of @ap according to sata_spd_limit.
1693 *
1694 * LOCKING:
1695 * Inherited from caller.
1696 *
1697 * RETURNS:
1698 * 0 if spd doesn't need to be changed, 1 if spd has been
1699 * changed. -EOPNOTSUPP if SCR registers are inaccessible.
1700 */
1701static int ata_set_sata_spd(struct ata_port *ap)
1702{
1703 u32 scontrol;
1704
1705 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1706 return -EOPNOTSUPP;
1707
1708 scontrol = scr_read(ap, SCR_CONTROL);
1709 if (!__ata_set_sata_spd_needed(ap, &scontrol))
1710 return 0;
1711
1712 scr_write(ap, SCR_CONTROL, scontrol);
1713 return 1;
1714}
1715
Alan Cox452503f2005-10-21 19:01:32 -04001716/*
1717 * This mode timing computation functionality is ported over from
1718 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1719 */
1720/*
1721 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1722 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1723 * for PIO 5, which is a nonstandard extension and UDMA6, which
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001724 * is currently supported only by Maxtor drives.
Alan Cox452503f2005-10-21 19:01:32 -04001725 */
1726
1727static const struct ata_timing ata_timing[] = {
1728
1729 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1730 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1731 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1732 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1733
1734 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1735 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1736 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1737
1738/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001739
Alan Cox452503f2005-10-21 19:01:32 -04001740 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1741 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1742 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001743
Alan Cox452503f2005-10-21 19:01:32 -04001744 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1745 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1746 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1747
1748/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1749 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1750 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1751
1752 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1753 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1754 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1755
1756/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1757
1758 { 0xFF }
1759};
1760
1761#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1762#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1763
1764static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1765{
1766 q->setup = EZ(t->setup * 1000, T);
1767 q->act8b = EZ(t->act8b * 1000, T);
1768 q->rec8b = EZ(t->rec8b * 1000, T);
1769 q->cyc8b = EZ(t->cyc8b * 1000, T);
1770 q->active = EZ(t->active * 1000, T);
1771 q->recover = EZ(t->recover * 1000, T);
1772 q->cycle = EZ(t->cycle * 1000, T);
1773 q->udma = EZ(t->udma * 1000, UT);
1774}
1775
1776void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1777 struct ata_timing *m, unsigned int what)
1778{
1779 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1780 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1781 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1782 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1783 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1784 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1785 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1786 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1787}
1788
1789static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1790{
1791 const struct ata_timing *t;
1792
1793 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001794 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001795 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001796 return t;
Alan Cox452503f2005-10-21 19:01:32 -04001797}
1798
1799int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1800 struct ata_timing *t, int T, int UT)
1801{
1802 const struct ata_timing *s;
1803 struct ata_timing p;
1804
1805 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001806 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001807 */
Alan Cox452503f2005-10-21 19:01:32 -04001808
1809 if (!(s = ata_timing_find_mode(speed)))
1810 return -EINVAL;
1811
Albert Lee75b1f2f2005-11-16 17:06:18 +08001812 memcpy(t, s, sizeof(*s));
1813
Alan Cox452503f2005-10-21 19:01:32 -04001814 /*
1815 * If the drive is an EIDE drive, it can tell us it needs extended
1816 * PIO/MW_DMA cycle timing.
1817 */
1818
1819 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1820 memset(&p, 0, sizeof(p));
1821 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1822 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1823 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1824 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1825 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1826 }
1827 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1828 }
1829
1830 /*
1831 * Convert the timing to bus clock counts.
1832 */
1833
Albert Lee75b1f2f2005-11-16 17:06:18 +08001834 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001835
1836 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001837 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1838 * S.M.A.R.T * and some other commands. We have to ensure that the
1839 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001840 */
1841
1842 if (speed > XFER_PIO_4) {
1843 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1844 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1845 }
1846
1847 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001848 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001849 */
1850
1851 if (t->act8b + t->rec8b < t->cyc8b) {
1852 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1853 t->rec8b = t->cyc8b - t->act8b;
1854 }
1855
1856 if (t->active + t->recover < t->cycle) {
1857 t->active += (t->cycle - (t->active + t->recover)) / 2;
1858 t->recover = t->cycle - t->active;
1859 }
1860
1861 return 0;
1862}
1863
Tejun Heocf176e12006-04-02 17:54:46 +09001864/**
1865 * ata_down_xfermask_limit - adjust dev xfer masks downward
1866 * @ap: Port associated with device @dev
1867 * @dev: Device to adjust xfer masks
1868 * @force_pio0: Force PIO0
1869 *
1870 * Adjust xfer masks of @dev downward. Note that this function
1871 * does not apply the change. Invoking ata_set_mode() afterwards
1872 * will apply the limit.
1873 *
1874 * LOCKING:
1875 * Inherited from caller.
1876 *
1877 * RETURNS:
1878 * 0 on success, negative errno on failure
1879 */
1880static int ata_down_xfermask_limit(struct ata_port *ap, struct ata_device *dev,
1881 int force_pio0)
1882{
1883 unsigned long xfer_mask;
1884 int highbit;
1885
1886 xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
1887 dev->udma_mask);
1888
1889 if (!xfer_mask)
1890 goto fail;
1891 /* don't gear down to MWDMA from UDMA, go directly to PIO */
1892 if (xfer_mask & ATA_MASK_UDMA)
1893 xfer_mask &= ~ATA_MASK_MWDMA;
1894
1895 highbit = fls(xfer_mask) - 1;
1896 xfer_mask &= ~(1 << highbit);
1897 if (force_pio0)
1898 xfer_mask &= 1 << ATA_SHIFT_PIO;
1899 if (!xfer_mask)
1900 goto fail;
1901
1902 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
1903 &dev->udma_mask);
1904
1905 printk(KERN_WARNING "ata%u: dev %u limiting speed to %s\n",
1906 ap->id, dev->devno, ata_mode_string(xfer_mask));
1907
1908 return 0;
1909
1910 fail:
1911 return -EINVAL;
1912}
1913
Tejun Heo83206a22006-03-24 15:25:31 +09001914static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Tejun Heo83206a22006-03-24 15:25:31 +09001916 unsigned int err_mask;
1917 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 if (dev->xfer_shift == ATA_SHIFT_PIO)
1920 dev->flags |= ATA_DFLAG_PIO;
1921
Tejun Heo83206a22006-03-24 15:25:31 +09001922 err_mask = ata_dev_set_xfermode(ap, dev);
1923 if (err_mask) {
1924 printk(KERN_ERR
1925 "ata%u: failed to set xfermode (err_mask=0x%x)\n",
1926 ap->id, err_mask);
1927 return -EIO;
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Tejun Heo83206a22006-03-24 15:25:31 +09001930 rc = ata_dev_revalidate(ap, dev, 0);
1931 if (rc) {
1932 printk(KERN_ERR
1933 "ata%u: failed to revalidate after set xfermode\n",
1934 ap->id);
1935 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09001936 }
1937
Tejun Heo23e71c32006-03-06 04:31:57 +09001938 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1939 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
Tejun Heo23e71c32006-03-06 04:31:57 +09001942 ap->id, dev->devno,
1943 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09001944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947/**
1948 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1949 * @ap: port on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001950 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 *
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001952 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
1953 * ata_set_mode() fails, pointer to the failing device is
1954 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04001955 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001957 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001958 *
1959 * RETURNS:
1960 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 */
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001962static int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Tejun Heoe8e06192006-04-01 01:38:18 +09001964 struct ata_device *dev;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001965 int i, rc = 0, used_dma = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Tejun Heoa6d5a512006-03-06 04:31:57 +09001967 /* step 1: calculate xfer_mask */
1968 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoacf356b2006-03-24 14:07:50 +09001969 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09001970
Tejun Heoe8e06192006-04-01 01:38:18 +09001971 dev = &ap->device[i];
1972
Tejun Heoe1211e32006-04-01 01:38:18 +09001973 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09001974 continue;
1975
Tejun Heoacf356b2006-03-24 14:07:50 +09001976 ata_dev_xfermask(ap, dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09001977
Tejun Heoacf356b2006-03-24 14:07:50 +09001978 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
1979 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
1980 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
1981 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01001982
Tejun Heo4f659772006-04-01 01:38:18 +09001983 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01001984 if (dev->dma_mode)
1985 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09001986 }
Tejun Heo4f659772006-04-01 01:38:18 +09001987 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001988 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09001989
1990 /* step 2: always set host PIO timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09001991 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1992 dev = &ap->device[i];
1993 if (!ata_dev_enabled(dev))
1994 continue;
1995
1996 if (!dev->pio_mode) {
1997 printk(KERN_WARNING "ata%u: dev %u no PIO support\n",
1998 ap->id, dev->devno);
1999 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002000 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002001 }
2002
2003 dev->xfer_mode = dev->pio_mode;
2004 dev->xfer_shift = ATA_SHIFT_PIO;
2005 if (ap->ops->set_piomode)
2006 ap->ops->set_piomode(ap, dev);
2007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Tejun Heoa6d5a512006-03-06 04:31:57 +09002009 /* step 3: set host DMA timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002010 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2011 dev = &ap->device[i];
2012
2013 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2014 continue;
2015
2016 dev->xfer_mode = dev->dma_mode;
2017 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2018 if (ap->ops->set_dmamode)
2019 ap->ops->set_dmamode(ap, dev);
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 /* step 4: update devices' xfer mode */
Tejun Heo83206a22006-03-24 15:25:31 +09002023 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002024 dev = &ap->device[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Tejun Heoe1211e32006-04-01 01:38:18 +09002026 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002027 continue;
2028
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002029 rc = ata_dev_set_mode(ap, dev);
2030 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002031 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Tejun Heoe8e06192006-04-01 01:38:18 +09002034 /* Record simplex status. If we selected DMA then the other
2035 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002036 */
Alan Cox5444a6f2006-03-27 18:58:20 +01002037 if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
2038 ap->host_set->simplex_claimed = 1;
2039
Tejun Heoe8e06192006-04-01 01:38:18 +09002040 /* step5: chip specific finalisation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (ap->ops->post_set_mode)
2042 ap->ops->post_set_mode(ap);
2043
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002044 out:
2045 if (rc)
2046 *r_failed_dev = dev;
2047 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002051 * ata_tf_to_host - issue ATA taskfile to host controller
2052 * @ap: port to which command is being issued
2053 * @tf: ATA taskfile register set
2054 *
2055 * Issues ATA taskfile register set to ATA host controller,
2056 * with proper synchronization with interrupt handler and
2057 * other threads.
2058 *
2059 * LOCKING:
2060 * spin_lock_irqsave(host_set lock)
2061 */
2062
2063static inline void ata_tf_to_host(struct ata_port *ap,
2064 const struct ata_taskfile *tf)
2065{
2066 ap->ops->tf_load(ap, tf);
2067 ap->ops->exec_command(ap, tf);
2068}
2069
2070/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 * ata_busy_sleep - sleep until BSY clears, or timeout
2072 * @ap: port containing status register to be polled
2073 * @tmout_pat: impatience timeout
2074 * @tmout: overall timeout
2075 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002076 * Sleep until ATA Status register bit BSY clears,
2077 * or a timeout occurs.
2078 *
2079 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 */
2081
Tejun Heo6f8b9952006-01-24 17:05:21 +09002082unsigned int ata_busy_sleep (struct ata_port *ap,
2083 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 unsigned long timer_start, timeout;
2086 u8 status;
2087
2088 status = ata_busy_wait(ap, ATA_BUSY, 300);
2089 timer_start = jiffies;
2090 timeout = timer_start + tmout_pat;
2091 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2092 msleep(50);
2093 status = ata_busy_wait(ap, ATA_BUSY, 3);
2094 }
2095
2096 if (status & ATA_BUSY)
2097 printk(KERN_WARNING "ata%u is slow to respond, "
2098 "please be patient\n", ap->id);
2099
2100 timeout = timer_start + tmout;
2101 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2102 msleep(50);
2103 status = ata_chk_status(ap);
2104 }
2105
2106 if (status & ATA_BUSY) {
2107 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
2108 ap->id, tmout / HZ);
2109 return 1;
2110 }
2111
2112 return 0;
2113}
2114
2115static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2116{
2117 struct ata_ioports *ioaddr = &ap->ioaddr;
2118 unsigned int dev0 = devmask & (1 << 0);
2119 unsigned int dev1 = devmask & (1 << 1);
2120 unsigned long timeout;
2121
2122 /* if device 0 was found in ata_devchk, wait for its
2123 * BSY bit to clear
2124 */
2125 if (dev0)
2126 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2127
2128 /* if device 1 was found in ata_devchk, wait for
2129 * register access, then wait for BSY to clear
2130 */
2131 timeout = jiffies + ATA_TMOUT_BOOT;
2132 while (dev1) {
2133 u8 nsect, lbal;
2134
2135 ap->ops->dev_select(ap, 1);
2136 if (ap->flags & ATA_FLAG_MMIO) {
2137 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2138 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2139 } else {
2140 nsect = inb(ioaddr->nsect_addr);
2141 lbal = inb(ioaddr->lbal_addr);
2142 }
2143 if ((nsect == 1) && (lbal == 1))
2144 break;
2145 if (time_after(jiffies, timeout)) {
2146 dev1 = 0;
2147 break;
2148 }
2149 msleep(50); /* give drive a breather */
2150 }
2151 if (dev1)
2152 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2153
2154 /* is all this really necessary? */
2155 ap->ops->dev_select(ap, 0);
2156 if (dev1)
2157 ap->ops->dev_select(ap, 1);
2158 if (dev0)
2159 ap->ops->dev_select(ap, 0);
2160}
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162static unsigned int ata_bus_softreset(struct ata_port *ap,
2163 unsigned int devmask)
2164{
2165 struct ata_ioports *ioaddr = &ap->ioaddr;
2166
2167 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2168
2169 /* software reset. causes dev0 to be selected */
2170 if (ap->flags & ATA_FLAG_MMIO) {
2171 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2172 udelay(20); /* FIXME: flush */
2173 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2174 udelay(20); /* FIXME: flush */
2175 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2176 } else {
2177 outb(ap->ctl, ioaddr->ctl_addr);
2178 udelay(10);
2179 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2180 udelay(10);
2181 outb(ap->ctl, ioaddr->ctl_addr);
2182 }
2183
2184 /* spec mandates ">= 2ms" before checking status.
2185 * We wait 150ms, because that was the magic delay used for
2186 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2187 * between when the ATA command register is written, and then
2188 * status is checked. Because waiting for "a while" before
2189 * checking status is fine, post SRST, we perform this magic
2190 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00002191 *
2192 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 */
2194 msleep(150);
2195
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002196 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09002197 * the bus shows 0xFF because the odd clown forgets the D7
2198 * pulldown resistor.
2199 */
Alan Cox09c7ad72006-03-22 15:52:40 +00002200 if (ata_check_status(ap) == 0xFF)
Tejun Heo298a41c2006-03-25 02:58:13 +09002201 return AC_ERR_OTHER;
Alan Cox09c7ad72006-03-22 15:52:40 +00002202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 ata_bus_post_reset(ap, devmask);
2204
2205 return 0;
2206}
2207
2208/**
2209 * ata_bus_reset - reset host port and associated ATA channel
2210 * @ap: port to reset
2211 *
2212 * This is typically the first time we actually start issuing
2213 * commands to the ATA channel. We wait for BSY to clear, then
2214 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2215 * result. Determine what devices, if any, are on the channel
2216 * by looking at the device 0/1 error register. Look at the signature
2217 * stored in each device's taskfile registers, to determine if
2218 * the device is ATA or ATAPI.
2219 *
2220 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002221 * PCI/etc. bus probe sem.
2222 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 *
2224 * SIDE EFFECTS:
2225 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2226 */
2227
2228void ata_bus_reset(struct ata_port *ap)
2229{
2230 struct ata_ioports *ioaddr = &ap->ioaddr;
2231 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2232 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002233 unsigned int dev0, dev1 = 0, devmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2236
2237 /* determine if device 0/1 are present */
2238 if (ap->flags & ATA_FLAG_SATA_RESET)
2239 dev0 = 1;
2240 else {
2241 dev0 = ata_devchk(ap, 0);
2242 if (slave_possible)
2243 dev1 = ata_devchk(ap, 1);
2244 }
2245
2246 if (dev0)
2247 devmask |= (1 << 0);
2248 if (dev1)
2249 devmask |= (1 << 1);
2250
2251 /* select device 0 again */
2252 ap->ops->dev_select(ap, 0);
2253
2254 /* issue bus reset */
2255 if (ap->flags & ATA_FLAG_SRST)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002256 if (ata_bus_softreset(ap, devmask))
2257 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 /*
2260 * determine by signature whether we have ATA or ATAPI devices
2261 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002262 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002264 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 /* re-enable interrupts */
2267 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2268 ata_irq_on(ap);
2269
2270 /* is double-select really necessary? */
2271 if (ap->device[1].class != ATA_DEV_NONE)
2272 ap->ops->dev_select(ap, 1);
2273 if (ap->device[0].class != ATA_DEV_NONE)
2274 ap->ops->dev_select(ap, 0);
2275
2276 /* if no devices were detected, disable this port */
2277 if ((ap->device[0].class == ATA_DEV_NONE) &&
2278 (ap->device[1].class == ATA_DEV_NONE))
2279 goto err_out;
2280
2281 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2282 /* set up device control for ATA_FLAG_SATA_RESET */
2283 if (ap->flags & ATA_FLAG_MMIO)
2284 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2285 else
2286 outb(ap->ctl, ioaddr->ctl_addr);
2287 }
2288
2289 DPRINTK("EXIT\n");
2290 return;
2291
2292err_out:
2293 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2294 ap->ops->port_disable(ap);
2295
2296 DPRINTK("EXIT\n");
2297}
2298
Tejun Heo7a7921e2006-02-02 18:20:00 +09002299static int sata_phy_resume(struct ata_port *ap)
2300{
2301 unsigned long timeout = jiffies + (HZ * 5);
Tejun Heo852ee162006-04-01 01:38:18 +09002302 u32 scontrol, sstatus;
Tejun Heo7a7921e2006-02-02 18:20:00 +09002303
Tejun Heo852ee162006-04-01 01:38:18 +09002304 scontrol = scr_read(ap, SCR_CONTROL);
2305 scontrol = (scontrol & 0x0f0) | 0x300;
2306 scr_write_flush(ap, SCR_CONTROL, scontrol);
Tejun Heo7a7921e2006-02-02 18:20:00 +09002307
2308 /* Wait for phy to become ready, if necessary. */
2309 do {
2310 msleep(200);
2311 sstatus = scr_read(ap, SCR_STATUS);
2312 if ((sstatus & 0xf) != 1)
2313 return 0;
2314 } while (time_before(jiffies, timeout));
2315
2316 return -1;
2317}
2318
Tejun Heoc2bd5802006-01-24 17:05:22 +09002319/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002320 * ata_std_probeinit - initialize probing
2321 * @ap: port to be probed
2322 *
2323 * @ap is about to be probed. Initialize it. This function is
2324 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002325 *
2326 * NOTE!!! Do not use this function as probeinit if a low level
2327 * driver implements only hardreset. Just pass NULL as probeinit
2328 * in that case. Using this function is probably okay but doing
2329 * so makes reset sequence different from the original
2330 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002331 */
Alan Cox17efc5f2006-03-27 19:01:32 +01002332void ata_std_probeinit(struct ata_port *ap)
Tejun Heo8a19ac82006-02-02 18:20:00 +09002333{
Alan Cox17efc5f2006-03-27 19:01:32 +01002334 if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09002335 u32 spd;
2336
Tejun Heo8a19ac82006-02-02 18:20:00 +09002337 sata_phy_resume(ap);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002338
2339 spd = (scr_read(ap, SCR_CONTROL) & 0xf0) >> 4;
2340 if (spd)
2341 ap->sata_spd_limit &= (1 << spd) - 1;
2342
Tejun Heo3a397462006-02-10 23:58:48 +09002343 if (sata_dev_present(ap))
2344 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2345 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002346}
2347
2348/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002349 * ata_std_softreset - reset host port via ATA SRST
2350 * @ap: port to reset
2351 * @verbose: fail verbosely
2352 * @classes: resulting classes of attached devices
2353 *
2354 * Reset host port using ATA SRST. This function is to be used
2355 * as standard callback for ata_drive_*_reset() functions.
2356 *
2357 * LOCKING:
2358 * Kernel thread context (may sleep)
2359 *
2360 * RETURNS:
2361 * 0 on success, -errno otherwise.
2362 */
2363int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2364{
2365 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2366 unsigned int devmask = 0, err_mask;
2367 u8 err;
2368
2369 DPRINTK("ENTER\n");
2370
Tejun Heo3a397462006-02-10 23:58:48 +09002371 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2372 classes[0] = ATA_DEV_NONE;
2373 goto out;
2374 }
2375
Tejun Heoc2bd5802006-01-24 17:05:22 +09002376 /* determine if device 0/1 are present */
2377 if (ata_devchk(ap, 0))
2378 devmask |= (1 << 0);
2379 if (slave_possible && ata_devchk(ap, 1))
2380 devmask |= (1 << 1);
2381
Tejun Heoc2bd5802006-01-24 17:05:22 +09002382 /* select device 0 again */
2383 ap->ops->dev_select(ap, 0);
2384
2385 /* issue bus reset */
2386 DPRINTK("about to softreset, devmask=%x\n", devmask);
2387 err_mask = ata_bus_softreset(ap, devmask);
2388 if (err_mask) {
2389 if (verbose)
2390 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2391 ap->id, err_mask);
2392 else
2393 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2394 err_mask);
2395 return -EIO;
2396 }
2397
2398 /* determine by signature whether we have ATA or ATAPI devices */
2399 classes[0] = ata_dev_try_classify(ap, 0, &err);
2400 if (slave_possible && err != 0x81)
2401 classes[1] = ata_dev_try_classify(ap, 1, &err);
2402
Tejun Heo3a397462006-02-10 23:58:48 +09002403 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002404 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2405 return 0;
2406}
2407
2408/**
2409 * sata_std_hardreset - reset host port via SATA phy reset
2410 * @ap: port to reset
2411 * @verbose: fail verbosely
2412 * @class: resulting class of attached device
2413 *
2414 * SATA phy-reset host port using DET bits of SControl register.
2415 * This function is to be used as standard callback for
2416 * ata_drive_*_reset().
2417 *
2418 * LOCKING:
2419 * Kernel thread context (may sleep)
2420 *
2421 * RETURNS:
2422 * 0 on success, -errno otherwise.
2423 */
2424int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2425{
Tejun Heo852ee162006-04-01 01:38:18 +09002426 u32 scontrol;
2427
Tejun Heoc2bd5802006-01-24 17:05:22 +09002428 DPRINTK("ENTER\n");
2429
Tejun Heo1c3fae42006-04-02 20:53:28 +09002430 if (ata_set_sata_spd_needed(ap)) {
2431 /* SATA spec says nothing about how to reconfigure
2432 * spd. To be on the safe side, turn off phy during
2433 * reconfiguration. This works for at least ICH7 AHCI
2434 * and Sil3124.
2435 */
2436 scontrol = scr_read(ap, SCR_CONTROL);
2437 scontrol = (scontrol & 0x0f0) | 0x302;
2438 scr_write_flush(ap, SCR_CONTROL, scontrol);
2439
2440 ata_set_sata_spd(ap);
2441 }
2442
2443 /* issue phy wake/reset */
Tejun Heo852ee162006-04-01 01:38:18 +09002444 scontrol = scr_read(ap, SCR_CONTROL);
2445 scontrol = (scontrol & 0x0f0) | 0x301;
2446 scr_write_flush(ap, SCR_CONTROL, scontrol);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002447
Tejun Heo1c3fae42006-04-02 20:53:28 +09002448 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09002449 * 10.4.2 says at least 1 ms.
2450 */
2451 msleep(1);
2452
Tejun Heo1c3fae42006-04-02 20:53:28 +09002453 /* bring phy back */
Tejun Heo7a7921e2006-02-02 18:20:00 +09002454 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002455
Tejun Heoc2bd5802006-01-24 17:05:22 +09002456 /* TODO: phy layer with polling, timeouts, etc. */
2457 if (!sata_dev_present(ap)) {
2458 *class = ATA_DEV_NONE;
2459 DPRINTK("EXIT, link offline\n");
2460 return 0;
2461 }
2462
2463 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2464 if (verbose)
2465 printk(KERN_ERR "ata%u: COMRESET failed "
2466 "(device not ready)\n", ap->id);
2467 else
2468 DPRINTK("EXIT, device not ready\n");
2469 return -EIO;
2470 }
2471
Tejun Heo3a397462006-02-10 23:58:48 +09002472 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2473
Tejun Heoc2bd5802006-01-24 17:05:22 +09002474 *class = ata_dev_try_classify(ap, 0, NULL);
2475
2476 DPRINTK("EXIT, class=%u\n", *class);
2477 return 0;
2478}
2479
2480/**
2481 * ata_std_postreset - standard postreset callback
2482 * @ap: the target ata_port
2483 * @classes: classes of attached devices
2484 *
2485 * This function is invoked after a successful reset. Note that
2486 * the device might have been reset more than once using
2487 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002488 *
2489 * This function is to be used as standard callback for
2490 * ata_drive_*_reset().
2491 *
2492 * LOCKING:
2493 * Kernel thread context (may sleep)
2494 */
2495void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2496{
2497 DPRINTK("ENTER\n");
2498
Tejun Heo56497bd2006-02-15 15:01:42 +09002499 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002500 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2501 ap->cbl = ATA_CBL_SATA;
2502
2503 /* print link status */
2504 if (ap->cbl == ATA_CBL_SATA)
2505 sata_print_link_status(ap);
2506
Tejun Heo3a397462006-02-10 23:58:48 +09002507 /* re-enable interrupts */
2508 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2509 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002510
2511 /* is double-select really necessary? */
2512 if (classes[0] != ATA_DEV_NONE)
2513 ap->ops->dev_select(ap, 1);
2514 if (classes[1] != ATA_DEV_NONE)
2515 ap->ops->dev_select(ap, 0);
2516
Tejun Heo3a397462006-02-10 23:58:48 +09002517 /* bail out if no device is present */
2518 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2519 DPRINTK("EXIT, no device\n");
2520 return;
2521 }
2522
2523 /* set up device control */
2524 if (ap->ioaddr.ctl_addr) {
2525 if (ap->flags & ATA_FLAG_MMIO)
2526 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2527 else
2528 outb(ap->ctl, ap->ioaddr.ctl_addr);
2529 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002530
2531 DPRINTK("EXIT\n");
2532}
2533
2534/**
2535 * ata_std_probe_reset - standard probe reset method
2536 * @ap: prot to perform probe-reset
2537 * @classes: resulting classes of attached devices
2538 *
2539 * The stock off-the-shelf ->probe_reset method.
2540 *
2541 * LOCKING:
2542 * Kernel thread context (may sleep)
2543 *
2544 * RETURNS:
2545 * 0 on success, -errno otherwise.
2546 */
2547int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2548{
2549 ata_reset_fn_t hardreset;
2550
2551 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002552 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002553 hardreset = sata_std_hardreset;
2554
Tejun Heo8a19ac82006-02-02 18:20:00 +09002555 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002556 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002557 ata_std_postreset, classes);
2558}
2559
Tejun Heo9974e7c2006-04-01 01:38:17 +09002560static int ata_do_reset(struct ata_port *ap,
2561 ata_reset_fn_t reset, ata_postreset_fn_t postreset,
2562 int verbose, unsigned int *classes)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002563{
2564 int i, rc;
2565
2566 for (i = 0; i < ATA_MAX_DEVICES; i++)
2567 classes[i] = ATA_DEV_UNKNOWN;
2568
Tejun Heo9974e7c2006-04-01 01:38:17 +09002569 rc = reset(ap, verbose, classes);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002570 if (rc)
2571 return rc;
2572
2573 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2574 * is complete and convert all ATA_DEV_UNKNOWN to
2575 * ATA_DEV_NONE.
2576 */
2577 for (i = 0; i < ATA_MAX_DEVICES; i++)
2578 if (classes[i] != ATA_DEV_UNKNOWN)
2579 break;
2580
2581 if (i < ATA_MAX_DEVICES)
2582 for (i = 0; i < ATA_MAX_DEVICES; i++)
2583 if (classes[i] == ATA_DEV_UNKNOWN)
2584 classes[i] = ATA_DEV_NONE;
2585
2586 if (postreset)
2587 postreset(ap, classes);
2588
Tejun Heo9974e7c2006-04-01 01:38:17 +09002589 return 0;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002590}
2591
2592/**
2593 * ata_drive_probe_reset - Perform probe reset with given methods
2594 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002595 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002596 * @softreset: softreset method (can be NULL)
2597 * @hardreset: hardreset method (can be NULL)
2598 * @postreset: postreset method (can be NULL)
2599 * @classes: resulting classes of attached devices
2600 *
2601 * Reset the specified port and classify attached devices using
2602 * given methods. This function prefers softreset but tries all
2603 * possible reset sequences to reset and classify devices. This
2604 * function is intended to be used for constructing ->probe_reset
2605 * callback by low level drivers.
2606 *
2607 * Reset methods should follow the following rules.
2608 *
2609 * - Return 0 on sucess, -errno on failure.
2610 * - If classification is supported, fill classes[] with
2611 * recognized class codes.
2612 * - If classification is not supported, leave classes[] alone.
2613 * - If verbose is non-zero, print error message on failure;
2614 * otherwise, shut up.
2615 *
2616 * LOCKING:
2617 * Kernel thread context (may sleep)
2618 *
2619 * RETURNS:
2620 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2621 * if classification fails, and any error code from reset
2622 * methods.
2623 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002624int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002625 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2626 ata_postreset_fn_t postreset, unsigned int *classes)
2627{
2628 int rc = -EINVAL;
2629
Tejun Heo7944ea92006-02-02 18:20:00 +09002630 if (probeinit)
2631 probeinit(ap);
2632
Tejun Heo90dac022006-04-02 17:54:46 +09002633 if (softreset && !ata_set_sata_spd_needed(ap)) {
Tejun Heo9974e7c2006-04-01 01:38:17 +09002634 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
2635 if (rc == 0 && classes[0] != ATA_DEV_UNKNOWN)
2636 goto done;
Tejun Heoedbabd82006-04-02 20:55:02 +09002637 printk(KERN_INFO "ata%u: softreset failed, will try "
2638 "hardreset in 5 secs\n", ap->id);
2639 ssleep(5);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002640 }
2641
2642 if (!hardreset)
Tejun Heo9974e7c2006-04-01 01:38:17 +09002643 goto done;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002644
Tejun Heo90dac022006-04-02 17:54:46 +09002645 while (1) {
2646 rc = ata_do_reset(ap, hardreset, postreset, 0, classes);
2647 if (rc == 0) {
2648 if (classes[0] != ATA_DEV_UNKNOWN)
2649 goto done;
2650 break;
2651 }
2652
2653 if (ata_down_sata_spd_limit(ap))
2654 goto done;
Tejun Heoedbabd82006-04-02 20:55:02 +09002655
2656 printk(KERN_INFO "ata%u: hardreset failed, will retry "
2657 "in 5 secs\n", ap->id);
2658 ssleep(5);
Tejun Heo90dac022006-04-02 17:54:46 +09002659 }
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002660
Tejun Heoedbabd82006-04-02 20:55:02 +09002661 if (softreset) {
2662 printk(KERN_INFO "ata%u: hardreset succeeded without "
2663 "classification, will retry softreset in 5 secs\n",
2664 ap->id);
2665 ssleep(5);
2666
Tejun Heo9974e7c2006-04-01 01:38:17 +09002667 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
Tejun Heoedbabd82006-04-02 20:55:02 +09002668 }
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002669
Tejun Heo9974e7c2006-04-01 01:38:17 +09002670 done:
2671 if (rc == 0 && classes[0] == ATA_DEV_UNKNOWN)
2672 rc = -ENODEV;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002673 return rc;
2674}
2675
Tejun Heo623a3122006-03-05 17:55:58 +09002676/**
2677 * ata_dev_same_device - Determine whether new ID matches configured device
2678 * @ap: port on which the device to compare against resides
2679 * @dev: device to compare against
2680 * @new_class: class of the new device
2681 * @new_id: IDENTIFY page of the new device
2682 *
2683 * Compare @new_class and @new_id against @dev and determine
2684 * whether @dev is the device indicated by @new_class and
2685 * @new_id.
2686 *
2687 * LOCKING:
2688 * None.
2689 *
2690 * RETURNS:
2691 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2692 */
2693static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2694 unsigned int new_class, const u16 *new_id)
2695{
2696 const u16 *old_id = dev->id;
2697 unsigned char model[2][41], serial[2][21];
2698 u64 new_n_sectors;
2699
2700 if (dev->class != new_class) {
2701 printk(KERN_INFO
2702 "ata%u: dev %u class mismatch %d != %d\n",
2703 ap->id, dev->devno, dev->class, new_class);
2704 return 0;
2705 }
2706
2707 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2708 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2709 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2710 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2711 new_n_sectors = ata_id_n_sectors(new_id);
2712
2713 if (strcmp(model[0], model[1])) {
2714 printk(KERN_INFO
2715 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2716 ap->id, dev->devno, model[0], model[1]);
2717 return 0;
2718 }
2719
2720 if (strcmp(serial[0], serial[1])) {
2721 printk(KERN_INFO
2722 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2723 ap->id, dev->devno, serial[0], serial[1]);
2724 return 0;
2725 }
2726
2727 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2728 printk(KERN_INFO
2729 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2730 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2731 (unsigned long long)new_n_sectors);
2732 return 0;
2733 }
2734
2735 return 1;
2736}
2737
2738/**
2739 * ata_dev_revalidate - Revalidate ATA device
2740 * @ap: port on which the device to revalidate resides
2741 * @dev: device to revalidate
2742 * @post_reset: is this revalidation after reset?
2743 *
2744 * Re-read IDENTIFY page and make sure @dev is still attached to
2745 * the port.
2746 *
2747 * LOCKING:
2748 * Kernel thread context (may sleep)
2749 *
2750 * RETURNS:
2751 * 0 on success, negative errno otherwise
2752 */
2753int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2754 int post_reset)
2755{
2756 unsigned int class;
2757 u16 *id;
2758 int rc;
2759
Tejun Heoe1211e32006-04-01 01:38:18 +09002760 if (!ata_dev_enabled(dev))
Tejun Heo623a3122006-03-05 17:55:58 +09002761 return -ENODEV;
2762
2763 class = dev->class;
2764 id = NULL;
2765
2766 /* allocate & read ID data */
2767 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2768 if (rc)
2769 goto fail;
2770
2771 /* is the device still there? */
2772 if (!ata_dev_same_device(ap, dev, class, id)) {
2773 rc = -ENODEV;
2774 goto fail;
2775 }
2776
2777 kfree(dev->id);
2778 dev->id = id;
2779
2780 /* configure device according to the new ID */
2781 return ata_dev_configure(ap, dev, 0);
2782
2783 fail:
2784 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2785 ap->id, dev->devno, rc);
2786 kfree(id);
2787 return rc;
2788}
2789
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002790static const char * const ata_dma_blacklist [] = {
Alan Coxf4b15fe2006-03-22 15:54:04 +00002791 "WDC AC11000H", NULL,
2792 "WDC AC22100H", NULL,
2793 "WDC AC32500H", NULL,
2794 "WDC AC33100H", NULL,
2795 "WDC AC31600H", NULL,
2796 "WDC AC32100H", "24.09P07",
2797 "WDC AC23200L", "21.10N21",
2798 "Compaq CRD-8241B", NULL,
2799 "CRD-8400B", NULL,
2800 "CRD-8480B", NULL,
2801 "CRD-8482B", NULL,
2802 "CRD-84", NULL,
2803 "SanDisk SDP3B", NULL,
2804 "SanDisk SDP3B-64", NULL,
2805 "SANYO CD-ROM CRD", NULL,
2806 "HITACHI CDR-8", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002807 "HITACHI CDR-8335", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002808 "HITACHI CDR-8435", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002809 "Toshiba CD-ROM XM-6202B", NULL,
2810 "TOSHIBA CD-ROM XM-1702BC", NULL,
2811 "CD-532E-A", NULL,
2812 "E-IDE CD-ROM CR-840", NULL,
2813 "CD-ROM Drive/F5A", NULL,
2814 "WPI CDD-820", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002815 "SAMSUNG CD-ROM SC-148C", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002816 "SAMSUNG CD-ROM SC", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002817 "SanDisk SDP3B-64", NULL,
2818 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2819 "_NEC DV5800A", NULL,
2820 "SAMSUNG CD-ROM SN-124", "N001"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002822
Alan Coxf4b15fe2006-03-22 15:54:04 +00002823static int ata_strim(char *s, size_t len)
2824{
2825 len = strnlen(s, len);
2826
2827 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2828 while ((len > 0) && (s[len - 1] == ' ')) {
2829 len--;
2830 s[len] = 0;
2831 }
2832 return len;
2833}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Jeff Garzik057ace52005-10-22 14:27:05 -04002835static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Alan Coxf4b15fe2006-03-22 15:54:04 +00002837 unsigned char model_num[40];
2838 unsigned char model_rev[16];
2839 unsigned int nlen, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 int i;
2841
Alan Coxf4b15fe2006-03-22 15:54:04 +00002842 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2843 sizeof(model_num));
2844 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
2845 sizeof(model_rev));
2846 nlen = ata_strim(model_num, sizeof(model_num));
2847 rlen = ata_strim(model_rev, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Alan Coxf4b15fe2006-03-22 15:54:04 +00002849 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
2850 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
2851 if (ata_dma_blacklist[i+1] == NULL)
2852 return 1;
2853 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
2854 return 1;
2855 }
2856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return 0;
2858}
2859
Tejun Heoa6d5a512006-03-06 04:31:57 +09002860/**
2861 * ata_dev_xfermask - Compute supported xfermask of the given device
2862 * @ap: Port on which the device to compute xfermask for resides
2863 * @dev: Device to compute xfermask for
2864 *
Tejun Heoacf356b2006-03-24 14:07:50 +09002865 * Compute supported xfermask of @dev and store it in
2866 * dev->*_mask. This function is responsible for applying all
2867 * known limits including host controller limits, device
2868 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09002869 *
Tejun Heo600511e2006-03-25 11:14:07 +09002870 * FIXME: The current implementation limits all transfer modes to
2871 * the fastest of the lowested device on the port. This is not
Tejun Heo05c8e0a2006-03-25 14:28:57 +09002872 * required on most controllers.
Tejun Heo600511e2006-03-25 11:14:07 +09002873 *
Tejun Heoa6d5a512006-03-06 04:31:57 +09002874 * LOCKING:
2875 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09002876 */
Tejun Heoacf356b2006-03-24 14:07:50 +09002877static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Alan Cox5444a6f2006-03-27 18:58:20 +01002879 struct ata_host_set *hs = ap->host_set;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002880 unsigned long xfer_mask;
2881 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Tejun Heoa6d5a512006-03-06 04:31:57 +09002883 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2884 ap->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Alan Cox5444a6f2006-03-27 18:58:20 +01002886 /* FIXME: Use port-wide xfermask for now */
Tejun Heoa6d5a512006-03-06 04:31:57 +09002887 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2888 struct ata_device *d = &ap->device[i];
Tejun Heoe1211e32006-04-01 01:38:18 +09002889 if (!ata_dev_enabled(d))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002890 continue;
Tejun Heoacf356b2006-03-24 14:07:50 +09002891 xfer_mask &= ata_pack_xfermask(d->pio_mask, d->mwdma_mask,
2892 d->udma_mask);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002893 xfer_mask &= ata_id_xfermask(d->id);
2894 if (ata_dma_blacklisted(d))
2895 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Alan Cox5444a6f2006-03-27 18:58:20 +01002896 /* Apply cable rule here. Don't apply it early because when
2897 we handle hot plug the cable type can itself change */
2898 if (ap->cbl == ATA_CBL_PATA40)
2899 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
2901
Tejun Heoa6d5a512006-03-06 04:31:57 +09002902 if (ata_dma_blacklisted(dev))
2903 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2904 "disabling DMA\n", ap->id, dev->devno);
2905
Alan Cox5444a6f2006-03-27 18:58:20 +01002906 if (hs->flags & ATA_HOST_SIMPLEX) {
2907 if (hs->simplex_claimed)
2908 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2909 }
2910 if (ap->ops->mode_filter)
2911 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
2912
Tejun Heoacf356b2006-03-24 14:07:50 +09002913 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2914 &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2919 * @ap: Port associated with device @dev
2920 * @dev: Device to which command will be sent
2921 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002922 * Issue SET FEATURES - XFER MODE command to device @dev
2923 * on port @ap.
2924 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002926 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09002927 *
2928 * RETURNS:
2929 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 */
2931
Tejun Heo83206a22006-03-24 15:25:31 +09002932static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
2933 struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Tejun Heoa0123702005-12-13 14:49:31 +09002935 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09002936 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938 /* set up set-features taskfile */
2939 DPRINTK("set features - xfer mode\n");
2940
Tejun Heoa0123702005-12-13 14:49:31 +09002941 ata_tf_init(ap, &tf, dev->devno);
2942 tf.command = ATA_CMD_SET_FEATURES;
2943 tf.feature = SETFEATURES_XFER;
2944 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2945 tf.protocol = ATA_PROT_NODATA;
2946 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Tejun Heo83206a22006-03-24 15:25:31 +09002948 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Tejun Heo83206a22006-03-24 15:25:31 +09002950 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2951 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953
2954/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002955 * ata_dev_init_params - Issue INIT DEV PARAMS command
2956 * @ap: Port associated with device @dev
2957 * @dev: Device to which command will be sent
2958 *
2959 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002960 * Kernel thread context (may sleep)
2961 *
2962 * RETURNS:
2963 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04002964 */
2965
Tejun Heo6aff8f12006-02-15 18:24:09 +09002966static unsigned int ata_dev_init_params(struct ata_port *ap,
Albert Lee00b6f5e2006-03-27 16:39:18 +08002967 struct ata_device *dev,
2968 u16 heads,
2969 u16 sectors)
Albert Lee8bf62ec2005-05-12 15:29:42 -04002970{
Tejun Heoa0123702005-12-13 14:49:31 +09002971 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002972 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002973
2974 /* Number of sectors per track 1-255. Number of heads 1-16 */
2975 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08002976 return AC_ERR_INVALID;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002977
2978 /* set up init dev params taskfile */
2979 DPRINTK("init dev params \n");
2980
Tejun Heoa0123702005-12-13 14:49:31 +09002981 ata_tf_init(ap, &tf, dev->devno);
2982 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2983 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2984 tf.protocol = ATA_PROT_NODATA;
2985 tf.nsect = sectors;
2986 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002987
Tejun Heo6aff8f12006-02-15 18:24:09 +09002988 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002989
Tejun Heo6aff8f12006-02-15 18:24:09 +09002990 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2991 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002992}
2993
2994/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002995 * ata_sg_clean - Unmap DMA memory associated with command
2996 * @qc: Command containing DMA memory to be released
2997 *
2998 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 *
3000 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003001 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 */
3003
3004static void ata_sg_clean(struct ata_queued_cmd *qc)
3005{
3006 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003007 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003009 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Tejun Heoa4631472006-02-11 19:11:13 +09003011 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
3012 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05003015 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003017 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003019 /* if we padded the buffer out to 32-bit bound, and data
3020 * xfer direction is from-device, we must copy from the
3021 * pad buffer back into the supplied buffer
3022 */
3023 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
3024 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3025
3026 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05003027 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003028 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003029 /* restore last sg */
3030 sg[qc->orig_n_elem - 1].length += qc->pad_len;
3031 if (pad_buf) {
3032 struct scatterlist *psg = &qc->pad_sgent;
3033 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3034 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003035 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003036 }
3037 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09003038 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003039 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05003040 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
3041 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003042 /* restore sg */
3043 sg->length += qc->pad_len;
3044 if (pad_buf)
3045 memcpy(qc->buf_virt + sg->length - qc->pad_len,
3046 pad_buf, qc->pad_len);
3047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003050 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
3053/**
3054 * ata_fill_sg - Fill PCI IDE PRD table
3055 * @qc: Metadata associated with taskfile to be transferred
3056 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003057 * Fill PCI IDE PRD (scatter-gather) table with segments
3058 * associated with the current disk command.
3059 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04003061 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 *
3063 */
3064static void ata_fill_sg(struct ata_queued_cmd *qc)
3065{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003067 struct scatterlist *sg;
3068 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Tejun Heoa4631472006-02-11 19:11:13 +09003070 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05003071 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003074 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 u32 addr, offset;
3076 u32 sg_len, len;
3077
3078 /* determine if physical DMA addr spans 64K boundary.
3079 * Note h/w doesn't support 64-bit, so we unconditionally
3080 * truncate dma_addr_t to u32.
3081 */
3082 addr = (u32) sg_dma_address(sg);
3083 sg_len = sg_dma_len(sg);
3084
3085 while (sg_len) {
3086 offset = addr & 0xffff;
3087 len = sg_len;
3088 if ((offset + sg_len) > 0x10000)
3089 len = 0x10000 - offset;
3090
3091 ap->prd[idx].addr = cpu_to_le32(addr);
3092 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
3093 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
3094
3095 idx++;
3096 sg_len -= len;
3097 addr += len;
3098 }
3099 }
3100
3101 if (idx)
3102 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3103}
3104/**
3105 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3106 * @qc: Metadata associated with taskfile to check
3107 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003108 * Allow low-level driver to filter ATA PACKET commands, returning
3109 * a status indicating whether or not it is OK to use DMA for the
3110 * supplied PACKET command.
3111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003113 * spin_lock_irqsave(host_set lock)
3114 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 * RETURNS: 0 when ATAPI DMA can be used
3116 * nonzero otherwise
3117 */
3118int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3119{
3120 struct ata_port *ap = qc->ap;
3121 int rc = 0; /* Assume ATAPI DMA is OK by default */
3122
3123 if (ap->ops->check_atapi_dma)
3124 rc = ap->ops->check_atapi_dma(qc);
3125
3126 return rc;
3127}
3128/**
3129 * ata_qc_prep - Prepare taskfile for submission
3130 * @qc: Metadata associated with taskfile to be prepared
3131 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003132 * Prepare ATA taskfile for submission.
3133 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 * LOCKING:
3135 * spin_lock_irqsave(host_set lock)
3136 */
3137void ata_qc_prep(struct ata_queued_cmd *qc)
3138{
3139 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3140 return;
3141
3142 ata_fill_sg(qc);
3143}
3144
Brian Kinge46834c2006-03-17 17:04:03 -06003145void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
3146
Jeff Garzik0cba6322005-05-30 19:49:12 -04003147/**
3148 * ata_sg_init_one - Associate command with memory buffer
3149 * @qc: Command to be associated
3150 * @buf: Memory buffer
3151 * @buflen: Length of memory buffer, in bytes.
3152 *
3153 * Initialize the data-related elements of queued_cmd @qc
3154 * to point to a single memory buffer, @buf of byte length @buflen.
3155 *
3156 * LOCKING:
3157 * spin_lock_irqsave(host_set lock)
3158 */
3159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3161{
3162 struct scatterlist *sg;
3163
3164 qc->flags |= ATA_QCFLAG_SINGLE;
3165
3166 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003167 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003169 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 qc->buf_virt = buf;
3171
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003172 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05003173 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
Jeff Garzik0cba6322005-05-30 19:49:12 -04003176/**
3177 * ata_sg_init - Associate command with scatter-gather table.
3178 * @qc: Command to be associated
3179 * @sg: Scatter-gather table.
3180 * @n_elem: Number of elements in s/g table.
3181 *
3182 * Initialize the data-related elements of queued_cmd @qc
3183 * to point to a scatter-gather table @sg, containing @n_elem
3184 * elements.
3185 *
3186 * LOCKING:
3187 * spin_lock_irqsave(host_set lock)
3188 */
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3191 unsigned int n_elem)
3192{
3193 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003194 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003196 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
3199/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003200 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3201 * @qc: Command with memory buffer to be mapped.
3202 *
3203 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 *
3205 * LOCKING:
3206 * spin_lock_irqsave(host_set lock)
3207 *
3208 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003209 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 */
3211
3212static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3213{
3214 struct ata_port *ap = qc->ap;
3215 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003216 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003218 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003220 /* we must lengthen transfers to end on a 32-bit boundary */
3221 qc->pad_len = sg->length & 3;
3222 if (qc->pad_len) {
3223 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3224 struct scatterlist *psg = &qc->pad_sgent;
3225
Tejun Heoa4631472006-02-11 19:11:13 +09003226 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003227
3228 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3229
3230 if (qc->tf.flags & ATA_TFLAG_WRITE)
3231 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3232 qc->pad_len);
3233
3234 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3235 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3236 /* trim sg */
3237 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003238 if (sg->length == 0)
3239 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003240
3241 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3242 sg->length, qc->pad_len);
3243 }
3244
Tejun Heo2e242fa2006-02-20 23:48:38 +09003245 if (trim_sg) {
3246 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003247 goto skip_map;
3248 }
3249
Brian King2f1f6102006-03-23 17:30:15 -06003250 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003251 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003252 if (dma_mapping_error(dma_address)) {
3253 /* restore sg */
3254 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003259 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Tejun Heo2e242fa2006-02-20 23:48:38 +09003261skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3263 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3264
3265 return 0;
3266}
3267
3268/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003269 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3270 * @qc: Command with scatter-gather table to be mapped.
3271 *
3272 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 *
3274 * LOCKING:
3275 * spin_lock_irqsave(host_set lock)
3276 *
3277 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003278 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 *
3280 */
3281
3282static int ata_sg_setup(struct ata_queued_cmd *qc)
3283{
3284 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003285 struct scatterlist *sg = qc->__sg;
3286 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003287 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003290 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003292 /* we must lengthen transfers to end on a 32-bit boundary */
3293 qc->pad_len = lsg->length & 3;
3294 if (qc->pad_len) {
3295 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3296 struct scatterlist *psg = &qc->pad_sgent;
3297 unsigned int offset;
3298
Tejun Heoa4631472006-02-11 19:11:13 +09003299 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003300
3301 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3302
3303 /*
3304 * psg->page/offset are used to copy to-be-written
3305 * data in this function or read data in ata_sg_clean.
3306 */
3307 offset = lsg->offset + lsg->length - qc->pad_len;
3308 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3309 psg->offset = offset_in_page(offset);
3310
3311 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3312 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3313 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003314 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003315 }
3316
3317 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3318 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3319 /* trim last sg */
3320 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003321 if (lsg->length == 0)
3322 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003323
3324 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3325 qc->n_elem - 1, lsg->length, qc->pad_len);
3326 }
3327
Jeff Garzike1410f22005-11-14 14:06:26 -05003328 pre_n_elem = qc->n_elem;
3329 if (trim_sg && pre_n_elem)
3330 pre_n_elem--;
3331
3332 if (!pre_n_elem) {
3333 n_elem = 0;
3334 goto skip_map;
3335 }
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06003338 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003339 if (n_elem < 1) {
3340 /* restore last sg */
3341 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
3345 DPRINTK("%d sg elements mapped\n", n_elem);
3346
Jeff Garzike1410f22005-11-14 14:06:26 -05003347skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 qc->n_elem = n_elem;
3349
3350 return 0;
3351}
3352
3353/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003354 * ata_poll_qc_complete - turn irq back on and finish qc
3355 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003356 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003357 *
3358 * LOCKING:
3359 * None. (grabs host lock)
3360 */
3361
Albert Leea22e2eb2005-12-05 15:38:02 +08003362void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003363{
3364 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003365 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003366
Jeff Garzikb8f61532005-08-25 22:01:20 -04003367 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003368 ap->flags &= ~ATA_FLAG_NOINTR;
3369 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003370 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003371 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003372}
3373
3374/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003375 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003376 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 *
3378 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003379 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 *
3381 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003382 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 */
3384
3385static unsigned long ata_pio_poll(struct ata_port *ap)
3386{
Albert Leec14b8332005-12-05 15:36:08 +08003387 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003389 unsigned int poll_state = HSM_ST_UNKNOWN;
3390 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Albert Leec14b8332005-12-05 15:36:08 +08003392 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003393 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003394
Albert Lee14be71f2005-09-27 17:36:35 +08003395 switch (ap->hsm_task_state) {
3396 case HSM_ST:
3397 case HSM_ST_POLL:
3398 poll_state = HSM_ST_POLL;
3399 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003401 case HSM_ST_LAST:
3402 case HSM_ST_LAST_POLL:
3403 poll_state = HSM_ST_LAST_POLL;
3404 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 break;
3406 default:
3407 BUG();
3408 break;
3409 }
3410
3411 status = ata_chk_status(ap);
3412 if (status & ATA_BUSY) {
3413 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003414 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003415 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 return 0;
3417 }
Albert Lee14be71f2005-09-27 17:36:35 +08003418 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return ATA_SHORT_PAUSE;
3420 }
3421
Albert Lee14be71f2005-09-27 17:36:35 +08003422 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 return 0;
3424}
3425
3426/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003427 * ata_pio_complete - check if drive is busy or idle
3428 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 *
3430 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003431 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003432 *
3433 * RETURNS:
3434 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 */
3436
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003437static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
3439 struct ata_queued_cmd *qc;
3440 u8 drv_stat;
3441
3442 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003443 * This is purely heuristic. This is a fast path. Sometimes when
3444 * we enter, BSY will be cleared in a chk-status or two. If not,
3445 * the drive is probably seeking or something. Snooze for a couple
3446 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003447 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 */
Albert Leefe79e682005-12-06 11:34:59 +08003449 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3450 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003452 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3453 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003454 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
3458 }
3459
Albert Leec14b8332005-12-05 15:36:08 +08003460 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003461 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 drv_stat = ata_wait_idle(ap);
3464 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003465 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003466 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
3469
Albert Lee14be71f2005-09-27 17:36:35 +08003470 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Tejun Heoa4631472006-02-11 19:11:13 +09003472 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003473 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003474
3475 /* another command may start at this point */
3476
3477 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
Edward Falk0baab862005-06-02 18:17:13 -04003480
3481/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003482 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003483 * @buf: Buffer to swap
3484 * @buf_words: Number of 16-bit words in buffer.
3485 *
3486 * Swap halves of 16-bit words if needed to convert from
3487 * little-endian byte order to native cpu byte order, or
3488 * vice-versa.
3489 *
3490 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003491 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493void swap_buf_le16(u16 *buf, unsigned int buf_words)
3494{
3495#ifdef __BIG_ENDIAN
3496 unsigned int i;
3497
3498 for (i = 0; i < buf_words; i++)
3499 buf[i] = le16_to_cpu(buf[i]);
3500#endif /* __BIG_ENDIAN */
3501}
3502
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003503/**
3504 * ata_mmio_data_xfer - Transfer data by MMIO
3505 * @ap: port to read/write
3506 * @buf: data buffer
3507 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003508 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003509 *
3510 * Transfer data from/to the device data register by MMIO.
3511 *
3512 * LOCKING:
3513 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003514 */
3515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3517 unsigned int buflen, int write_data)
3518{
3519 unsigned int i;
3520 unsigned int words = buflen >> 1;
3521 u16 *buf16 = (u16 *) buf;
3522 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3523
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003524 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 if (write_data) {
3526 for (i = 0; i < words; i++)
3527 writew(le16_to_cpu(buf16[i]), mmio);
3528 } else {
3529 for (i = 0; i < words; i++)
3530 buf16[i] = cpu_to_le16(readw(mmio));
3531 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003532
3533 /* Transfer trailing 1 byte, if any. */
3534 if (unlikely(buflen & 0x01)) {
3535 u16 align_buf[1] = { 0 };
3536 unsigned char *trailing_buf = buf + buflen - 1;
3537
3538 if (write_data) {
3539 memcpy(align_buf, trailing_buf, 1);
3540 writew(le16_to_cpu(align_buf[0]), mmio);
3541 } else {
3542 align_buf[0] = cpu_to_le16(readw(mmio));
3543 memcpy(trailing_buf, align_buf, 1);
3544 }
3545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003548/**
3549 * ata_pio_data_xfer - Transfer data by PIO
3550 * @ap: port to read/write
3551 * @buf: data buffer
3552 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003553 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003554 *
3555 * Transfer data from/to the device data register by PIO.
3556 *
3557 * LOCKING:
3558 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003559 */
3560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3562 unsigned int buflen, int write_data)
3563{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003564 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003566 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003568 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003570 insw(ap->ioaddr.data_addr, buf, words);
3571
3572 /* Transfer trailing 1 byte, if any. */
3573 if (unlikely(buflen & 0x01)) {
3574 u16 align_buf[1] = { 0 };
3575 unsigned char *trailing_buf = buf + buflen - 1;
3576
3577 if (write_data) {
3578 memcpy(align_buf, trailing_buf, 1);
3579 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3580 } else {
3581 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3582 memcpy(trailing_buf, align_buf, 1);
3583 }
3584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585}
3586
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003587/**
3588 * ata_data_xfer - Transfer data from/to the data register.
3589 * @ap: port to read/write
3590 * @buf: data buffer
3591 * @buflen: buffer length
3592 * @do_write: read/write
3593 *
3594 * Transfer data from/to the device data register.
3595 *
3596 * LOCKING:
3597 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003598 */
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3601 unsigned int buflen, int do_write)
3602{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003603 /* Make the crap hardware pay the costs not the good stuff */
3604 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3605 unsigned long flags;
3606 local_irq_save(flags);
3607 if (ap->flags & ATA_FLAG_MMIO)
3608 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3609 else
3610 ata_pio_data_xfer(ap, buf, buflen, do_write);
3611 local_irq_restore(flags);
3612 } else {
3613 if (ap->flags & ATA_FLAG_MMIO)
3614 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3615 else
3616 ata_pio_data_xfer(ap, buf, buflen, do_write);
3617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003620/**
3621 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3622 * @qc: Command on going
3623 *
3624 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3625 *
3626 * LOCKING:
3627 * Inherited from caller.
3628 */
3629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630static void ata_pio_sector(struct ata_queued_cmd *qc)
3631{
3632 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003633 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 struct ata_port *ap = qc->ap;
3635 struct page *page;
3636 unsigned int offset;
3637 unsigned char *buf;
3638
3639 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003640 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
3642 page = sg[qc->cursg].page;
3643 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3644
3645 /* get the current page and offset */
3646 page = nth_page(page, (offset >> PAGE_SHIFT));
3647 offset %= PAGE_SIZE;
3648
3649 buf = kmap(page) + offset;
3650
3651 qc->cursect++;
3652 qc->cursg_ofs++;
3653
Albert Lee32529e02005-05-26 03:49:42 -04003654 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 qc->cursg++;
3656 qc->cursg_ofs = 0;
3657 }
3658
3659 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3660
3661 /* do the actual data transfer */
3662 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3663 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3664
3665 kunmap(page);
3666}
3667
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003668/**
3669 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3670 * @qc: Command on going
3671 * @bytes: number of bytes
3672 *
3673 * Transfer Transfer data from/to the ATAPI device.
3674 *
3675 * LOCKING:
3676 * Inherited from caller.
3677 *
3678 */
3679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3681{
3682 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003683 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 struct ata_port *ap = qc->ap;
3685 struct page *page;
3686 unsigned char *buf;
3687 unsigned int offset, count;
3688
Albert Lee563a6e12005-08-12 14:17:50 +08003689 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003690 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
3692next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003693 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003694 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003695 * The end of qc->sg is reached and the device expects
3696 * more data to transfer. In order not to overrun qc->sg
3697 * and fulfill length specified in the byte count register,
3698 * - for read case, discard trailing data from the device
3699 * - for write case, padding zero data to the device
3700 */
3701 u16 pad_buf[1] = { 0 };
3702 unsigned int words = bytes >> 1;
3703 unsigned int i;
3704
3705 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003706 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003707 ap->id, bytes);
3708
3709 for (i = 0; i < words; i++)
3710 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3711
Albert Lee14be71f2005-09-27 17:36:35 +08003712 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003713 return;
3714 }
3715
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003716 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 page = sg->page;
3719 offset = sg->offset + qc->cursg_ofs;
3720
3721 /* get the current page and offset */
3722 page = nth_page(page, (offset >> PAGE_SHIFT));
3723 offset %= PAGE_SIZE;
3724
Albert Lee6952df02005-06-06 15:56:03 +08003725 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003726 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
3728 /* don't cross page boundaries */
3729 count = min(count, (unsigned int)PAGE_SIZE - offset);
3730
3731 buf = kmap(page) + offset;
3732
3733 bytes -= count;
3734 qc->curbytes += count;
3735 qc->cursg_ofs += count;
3736
Albert Lee32529e02005-05-26 03:49:42 -04003737 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 qc->cursg++;
3739 qc->cursg_ofs = 0;
3740 }
3741
3742 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3743
3744 /* do the actual data transfer */
3745 ata_data_xfer(ap, buf, count, do_write);
3746
3747 kunmap(page);
3748
Albert Lee563a6e12005-08-12 14:17:50 +08003749 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003753/**
3754 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3755 * @qc: Command on going
3756 *
3757 * Transfer Transfer data from/to the ATAPI device.
3758 *
3759 * LOCKING:
3760 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003761 */
3762
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3764{
3765 struct ata_port *ap = qc->ap;
3766 struct ata_device *dev = qc->dev;
3767 unsigned int ireason, bc_lo, bc_hi, bytes;
3768 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3769
3770 ap->ops->tf_read(ap, &qc->tf);
3771 ireason = qc->tf.nsect;
3772 bc_lo = qc->tf.lbam;
3773 bc_hi = qc->tf.lbah;
3774 bytes = (bc_hi << 8) | bc_lo;
3775
3776 /* shall be cleared to zero, indicating xfer of data */
3777 if (ireason & (1 << 0))
3778 goto err_out;
3779
3780 /* make sure transfer direction matches expected */
3781 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3782 if (do_write != i_write)
3783 goto err_out;
3784
3785 __atapi_pio_bytes(qc, bytes);
3786
3787 return;
3788
3789err_out:
3790 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3791 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003792 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003793 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
3796/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003797 * ata_pio_block - start PIO on a block
3798 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 *
3800 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003801 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 */
3803
3804static void ata_pio_block(struct ata_port *ap)
3805{
3806 struct ata_queued_cmd *qc;
3807 u8 status;
3808
3809 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003810 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 * Sometimes when we enter, BSY will be cleared in
3812 * a chk-status or two. If not, the drive is probably seeking
3813 * or something. Snooze for a couple msecs, then
3814 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003815 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 */
3817 status = ata_busy_wait(ap, ATA_BUSY, 5);
3818 if (status & ATA_BUSY) {
3819 msleep(2);
3820 status = ata_busy_wait(ap, ATA_BUSY, 10);
3821 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003822 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3824 return;
3825 }
3826 }
3827
3828 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003829 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Albert Leefe79e682005-12-06 11:34:59 +08003831 /* check error */
3832 if (status & (ATA_ERR | ATA_DF)) {
3833 qc->err_mask |= AC_ERR_DEV;
3834 ap->hsm_task_state = HSM_ST_ERR;
3835 return;
3836 }
3837
3838 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003840 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003842 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 return;
3844 }
3845
3846 atapi_pio_bytes(qc);
3847 } else {
3848 /* handle BSY=0, DRQ=0 as error */
3849 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003850 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003851 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 return;
3853 }
3854
3855 ata_pio_sector(qc);
3856 }
3857}
3858
3859static void ata_pio_error(struct ata_port *ap)
3860{
3861 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003864 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Albert Lee0565c262006-02-13 18:55:25 +08003866 if (qc->tf.command != ATA_CMD_PACKET)
3867 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3868
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003869 /* make sure qc->err_mask is available to
Albert Lee1c848982005-12-05 15:40:15 +08003870 * know what's wrong and recover
3871 */
Tejun Heoa4631472006-02-11 19:11:13 +09003872 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003873
Albert Lee14be71f2005-09-27 17:36:35 +08003874 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Albert Leea22e2eb2005-12-05 15:38:02 +08003876 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877}
3878
3879static void ata_pio_task(void *_data)
3880{
3881 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003882 unsigned long timeout;
3883 int qc_completed;
3884
3885fsm_start:
3886 timeout = 0;
3887 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Albert Lee14be71f2005-09-27 17:36:35 +08003889 switch (ap->hsm_task_state) {
3890 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 return;
3892
Albert Lee14be71f2005-09-27 17:36:35 +08003893 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 ata_pio_block(ap);
3895 break;
3896
Albert Lee14be71f2005-09-27 17:36:35 +08003897 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003898 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 break;
3900
Albert Lee14be71f2005-09-27 17:36:35 +08003901 case HSM_ST_POLL:
3902 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 timeout = ata_pio_poll(ap);
3904 break;
3905
Albert Lee14be71f2005-09-27 17:36:35 +08003906 case HSM_ST_TMOUT:
3907 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 ata_pio_error(ap);
3909 return;
3910 }
3911
3912 if (timeout)
Tejun Heo8061f5f2006-03-05 15:29:09 +09003913 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003914 else if (!qc_completed)
3915 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916}
3917
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918/**
Tejun Heo8061f5f2006-03-05 15:29:09 +09003919 * atapi_packet_task - Write CDB bytes to hardware
3920 * @_data: Port to which ATAPI device is attached.
3921 *
3922 * When device has indicated its readiness to accept
3923 * a CDB, this function is called. Send the CDB.
3924 * If DMA is to be performed, exit immediately.
3925 * Otherwise, we are in polling mode, so poll
3926 * status under operation succeeds or fails.
3927 *
3928 * LOCKING:
3929 * Kernel thread context (may sleep)
3930 */
3931
3932static void atapi_packet_task(void *_data)
3933{
3934 struct ata_port *ap = _data;
3935 struct ata_queued_cmd *qc;
3936 u8 status;
3937
3938 qc = ata_qc_from_tag(ap, ap->active_tag);
3939 WARN_ON(qc == NULL);
3940 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3941
3942 /* sleep-wait for BSY to clear */
3943 DPRINTK("busy wait\n");
3944 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3945 qc->err_mask |= AC_ERR_TIMEOUT;
3946 goto err_out;
3947 }
3948
3949 /* make sure DRQ is set */
3950 status = ata_chk_status(ap);
3951 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3952 qc->err_mask |= AC_ERR_HSM;
3953 goto err_out;
3954 }
3955
3956 /* send SCSI cdb */
3957 DPRINTK("send cdb\n");
3958 WARN_ON(qc->dev->cdb_len < 12);
3959
3960 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3961 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3962 unsigned long flags;
3963
3964 /* Once we're done issuing command and kicking bmdma,
3965 * irq handler takes over. To not lose irq, we need
3966 * to clear NOINTR flag before sending cdb, but
3967 * interrupt handler shouldn't be invoked before we're
3968 * finished. Hence, the following locking.
3969 */
3970 spin_lock_irqsave(&ap->host_set->lock, flags);
3971 ap->flags &= ~ATA_FLAG_NOINTR;
3972 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3973 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3974 ap->ops->bmdma_start(qc); /* initiate bmdma */
3975 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3976 } else {
3977 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3978
3979 /* PIO commands are handled by polling */
3980 ap->hsm_task_state = HSM_ST;
3981 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3982 }
3983
3984 return;
3985
3986err_out:
3987 ata_poll_qc_complete(qc);
3988}
3989
3990/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 * ata_qc_timeout - Handle timeout of queued command
3992 * @qc: Command that timed out
3993 *
3994 * Some part of the kernel (currently, only the SCSI layer)
3995 * has noticed that the active command on port @ap has not
3996 * completed after a specified length of time. Handle this
3997 * condition by disabling DMA (if necessary) and completing
3998 * transactions, with error if necessary.
3999 *
4000 * This also handles the case of the "lost interrupt", where
4001 * for some reason (possibly hardware bug, possibly driver bug)
4002 * an interrupt was not delivered to the driver, even though the
4003 * transaction completed successfully.
4004 *
4005 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004006 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 */
4008
4009static void ata_qc_timeout(struct ata_queued_cmd *qc)
4010{
4011 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04004012 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04004014 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
4016 DPRINTK("ENTER\n");
4017
Tejun Heoc18d06f2006-02-02 00:56:10 +09004018 ap->hsm_task_state = HSM_ST_IDLE;
4019
Jeff Garzikb8f61532005-08-25 22:01:20 -04004020 spin_lock_irqsave(&host_set->lock, flags);
4021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 switch (qc->tf.protocol) {
4023
4024 case ATA_PROT_DMA:
4025 case ATA_PROT_ATAPI_DMA:
4026 host_stat = ap->ops->bmdma_status(ap);
4027
4028 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004029 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
4031 /* fall through */
4032
4033 default:
4034 ata_altstatus(ap);
4035 drv_stat = ata_chk_status(ap);
4036
4037 /* ack bmdma irq events */
4038 ap->ops->irq_clear(ap);
4039
4040 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
4041 ap->id, qc->tf.command, drv_stat, host_stat);
4042
4043 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004044 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 break;
4046 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04004047
4048 spin_unlock_irqrestore(&host_set->lock, flags);
4049
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004050 ata_eh_qc_complete(qc);
4051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 DPRINTK("EXIT\n");
4053}
4054
4055/**
4056 * ata_eng_timeout - Handle timeout of queued command
4057 * @ap: Port on which timed-out command is active
4058 *
4059 * Some part of the kernel (currently, only the SCSI layer)
4060 * has noticed that the active command on port @ap has not
4061 * completed after a specified length of time. Handle this
4062 * condition by disabling DMA (if necessary) and completing
4063 * transactions, with error if necessary.
4064 *
4065 * This also handles the case of the "lost interrupt", where
4066 * for some reason (possibly hardware bug, possibly driver bug)
4067 * an interrupt was not delivered to the driver, even though the
4068 * transaction completed successfully.
4069 *
4070 * LOCKING:
4071 * Inherited from SCSI layer (none, can sleep)
4072 */
4073
4074void ata_eng_timeout(struct ata_port *ap)
4075{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 DPRINTK("ENTER\n");
4077
Tejun Heof6379022006-02-10 15:10:48 +09004078 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 DPRINTK("EXIT\n");
4081}
4082
4083/**
4084 * ata_qc_new - Request an available ATA command, for queueing
4085 * @ap: Port associated with device @dev
4086 * @dev: Device from whom we request an available command structure
4087 *
4088 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004089 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 */
4091
4092static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4093{
4094 struct ata_queued_cmd *qc = NULL;
4095 unsigned int i;
4096
4097 for (i = 0; i < ATA_MAX_QUEUE; i++)
4098 if (!test_and_set_bit(i, &ap->qactive)) {
4099 qc = ata_qc_from_tag(ap, i);
4100 break;
4101 }
4102
4103 if (qc)
4104 qc->tag = i;
4105
4106 return qc;
4107}
4108
4109/**
4110 * ata_qc_new_init - Request an available ATA command, and initialize it
4111 * @ap: Port associated with device @dev
4112 * @dev: Device from whom we request an available command structure
4113 *
4114 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004115 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 */
4117
4118struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
4119 struct ata_device *dev)
4120{
4121 struct ata_queued_cmd *qc;
4122
4123 qc = ata_qc_new(ap);
4124 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 qc->scsicmd = NULL;
4126 qc->ap = ap;
4127 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004129 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 }
4131
4132 return qc;
4133}
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135/**
4136 * ata_qc_free - free unused ata_queued_cmd
4137 * @qc: Command to complete
4138 *
4139 * Designed to free unused ata_queued_cmd object
4140 * in case something prevents using it.
4141 *
4142 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004143 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 */
4145void ata_qc_free(struct ata_queued_cmd *qc)
4146{
Tejun Heo4ba946e2006-01-23 13:09:36 +09004147 struct ata_port *ap = qc->ap;
4148 unsigned int tag;
4149
Tejun Heoa4631472006-02-11 19:11:13 +09004150 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Tejun Heo4ba946e2006-01-23 13:09:36 +09004152 qc->flags = 0;
4153 tag = qc->tag;
4154 if (likely(ata_tag_valid(tag))) {
4155 if (tag == ap->active_tag)
4156 ap->active_tag = ATA_TAG_POISON;
4157 qc->tag = ATA_TAG_POISON;
4158 clear_bit(tag, &ap->qactive);
4159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160}
4161
Tejun Heo76014422006-02-11 15:13:49 +09004162void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163{
Tejun Heoa4631472006-02-11 19:11:13 +09004164 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4165 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
4167 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4168 ata_sg_clean(qc);
4169
Albert Lee3f3791d2005-08-16 14:25:38 +08004170 /* atapi: mark qc as inactive to prevent the interrupt handler
4171 * from completing the command twice later, before the error handler
4172 * is called. (when rc != 0 and atapi request sense is needed)
4173 */
4174 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004177 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178}
4179
4180static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4181{
4182 struct ata_port *ap = qc->ap;
4183
4184 switch (qc->tf.protocol) {
4185 case ATA_PROT_DMA:
4186 case ATA_PROT_ATAPI_DMA:
4187 return 1;
4188
4189 case ATA_PROT_ATAPI:
4190 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 if (ap->flags & ATA_FLAG_PIO_DMA)
4192 return 1;
4193
4194 /* fall through */
4195
4196 default:
4197 return 0;
4198 }
4199
4200 /* never reached */
4201}
4202
4203/**
4204 * ata_qc_issue - issue taskfile to device
4205 * @qc: command to issue to device
4206 *
4207 * Prepare an ATA command to submission to device.
4208 * This includes mapping the data into a DMA-able
4209 * area, filling in the S/G table, and finally
4210 * writing the taskfile to hardware, starting the command.
4211 *
4212 * LOCKING:
4213 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004215void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216{
4217 struct ata_port *ap = qc->ap;
4218
Tejun Heoe4a70e72006-03-31 20:36:47 +09004219 qc->ap->active_tag = qc->tag;
4220 qc->flags |= ATA_QCFLAG_ACTIVE;
4221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 if (ata_should_dma_map(qc)) {
4223 if (qc->flags & ATA_QCFLAG_SG) {
4224 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004225 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4227 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004228 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 }
4230 } else {
4231 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4232 }
4233
4234 ap->ops->qc_prep(qc);
4235
Tejun Heo8e0e6942006-03-31 20:41:11 +09004236 qc->err_mask |= ap->ops->qc_issue(qc);
4237 if (unlikely(qc->err_mask))
4238 goto err;
4239 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
Tejun Heo8e436af2006-01-23 13:09:36 +09004241sg_err:
4242 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09004243 qc->err_mask |= AC_ERR_SYSTEM;
4244err:
4245 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246}
4247
4248/**
4249 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4250 * @qc: command to issue to device
4251 *
4252 * Using various libata functions and hooks, this function
4253 * starts an ATA command. ATA commands are grouped into
4254 * classes called "protocols", and issuing each type of protocol
4255 * is slightly different.
4256 *
Edward Falk0baab862005-06-02 18:17:13 -04004257 * May be used as the qc_issue() entry in ata_port_operations.
4258 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 * LOCKING:
4260 * spin_lock_irqsave(host_set lock)
4261 *
4262 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004263 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 */
4265
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004266unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
4268 struct ata_port *ap = qc->ap;
4269
4270 ata_dev_select(ap, qc->dev->devno, 1, 0);
4271
4272 switch (qc->tf.protocol) {
4273 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05004274 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 break;
4276
4277 case ATA_PROT_DMA:
4278 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4279 ap->ops->bmdma_setup(qc); /* set up bmdma */
4280 ap->ops->bmdma_start(qc); /* initiate bmdma */
4281 break;
4282
4283 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4284 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004285 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08004286 ap->hsm_task_state = HSM_ST;
Tejun Heo8061f5f2006-03-05 15:29:09 +09004287 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 break;
4289
4290 case ATA_PROT_ATAPI:
4291 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004292 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004293 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 break;
4295
4296 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09004297 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05004298 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004299 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 break;
4301
4302 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09004303 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4305 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo8061f5f2006-03-05 15:29:09 +09004306 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 break;
4308
4309 default:
4310 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004311 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 }
4313
4314 return 0;
4315}
4316
4317/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 * ata_host_intr - Handle host interrupt for given (port, task)
4319 * @ap: Port on which interrupt arrived (possibly...)
4320 * @qc: Taskfile currently active in engine
4321 *
4322 * Handle host interrupt for given queued command. Currently,
4323 * only DMA interrupts are handled. All other commands are
4324 * handled via polling with interrupts disabled (nIEN bit).
4325 *
4326 * LOCKING:
4327 * spin_lock_irqsave(host_set lock)
4328 *
4329 * RETURNS:
4330 * One if interrupt was handled, zero if not (shared irq).
4331 */
4332
4333inline unsigned int ata_host_intr (struct ata_port *ap,
4334 struct ata_queued_cmd *qc)
4335{
4336 u8 status, host_stat;
4337
4338 switch (qc->tf.protocol) {
4339
4340 case ATA_PROT_DMA:
4341 case ATA_PROT_ATAPI_DMA:
4342 case ATA_PROT_ATAPI:
4343 /* check status of DMA engine */
4344 host_stat = ap->ops->bmdma_status(ap);
4345 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4346
4347 /* if it's not our irq... */
4348 if (!(host_stat & ATA_DMA_INTR))
4349 goto idle_irq;
4350
4351 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004352 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
4354 /* fall through */
4355
4356 case ATA_PROT_ATAPI_NODATA:
4357 case ATA_PROT_NODATA:
4358 /* check altstatus */
4359 status = ata_altstatus(ap);
4360 if (status & ATA_BUSY)
4361 goto idle_irq;
4362
4363 /* check main status, clearing INTRQ */
4364 status = ata_chk_status(ap);
4365 if (unlikely(status & ATA_BUSY))
4366 goto idle_irq;
4367 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4368 ap->id, qc->tf.protocol, status);
4369
4370 /* ack bmdma irq events */
4371 ap->ops->irq_clear(ap);
4372
4373 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004374 qc->err_mask |= ac_err_mask(status);
4375 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 break;
4377
4378 default:
4379 goto idle_irq;
4380 }
4381
4382 return 1; /* irq handled */
4383
4384idle_irq:
4385 ap->stats.idle_irq++;
4386
4387#ifdef ATA_IRQ_TRAP
4388 if ((ap->stats.idle_irq % 1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 ata_irq_ack(ap, 0); /* debug trap */
4390 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
Alan Cox23cfce82006-03-21 16:06:53 +00004391 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 }
4393#endif
4394 return 0; /* irq not handled */
4395}
4396
4397/**
4398 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004399 * @irq: irq line (unused)
4400 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 * @regs: unused
4402 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004403 * Default interrupt handler for PCI IDE devices. Calls
4404 * ata_host_intr() for each port that is not disabled.
4405 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004407 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 *
4409 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004410 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 */
4412
4413irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4414{
4415 struct ata_host_set *host_set = dev_instance;
4416 unsigned int i;
4417 unsigned int handled = 0;
4418 unsigned long flags;
4419
4420 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4421 spin_lock_irqsave(&host_set->lock, flags);
4422
4423 for (i = 0; i < host_set->n_ports; i++) {
4424 struct ata_port *ap;
4425
4426 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004427 if (ap &&
4428 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 struct ata_queued_cmd *qc;
4430
4431 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004432 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4433 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 handled |= ata_host_intr(ap, qc);
4435 }
4436 }
4437
4438 spin_unlock_irqrestore(&host_set->lock, flags);
4439
4440 return IRQ_RETVAL(handled);
4441}
4442
Edward Falk0baab862005-06-02 18:17:13 -04004443
Jens Axboe9b847542006-01-06 09:28:07 +01004444/*
4445 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4446 * without filling any other registers
4447 */
4448static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4449 u8 cmd)
4450{
4451 struct ata_taskfile tf;
4452 int err;
4453
4454 ata_tf_init(ap, &tf, dev->devno);
4455
4456 tf.command = cmd;
4457 tf.flags |= ATA_TFLAG_DEVICE;
4458 tf.protocol = ATA_PROT_NODATA;
4459
4460 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4461 if (err)
4462 printk(KERN_ERR "%s: ata command failed: %d\n",
4463 __FUNCTION__, err);
4464
4465 return err;
4466}
4467
4468static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4469{
4470 u8 cmd;
4471
4472 if (!ata_try_flush_cache(dev))
4473 return 0;
4474
4475 if (ata_id_has_flush_ext(dev->id))
4476 cmd = ATA_CMD_FLUSH_EXT;
4477 else
4478 cmd = ATA_CMD_FLUSH;
4479
4480 return ata_do_simple_cmd(ap, dev, cmd);
4481}
4482
4483static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4484{
4485 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4486}
4487
4488static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4489{
4490 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4491}
4492
4493/**
4494 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004495 * @ap: port the device is connected to
4496 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004497 *
4498 * Kick the drive back into action, by sending it an idle immediate
4499 * command and making sure its transfer mode matches between drive
4500 * and host.
4501 *
4502 */
4503int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4504{
4505 if (ap->flags & ATA_FLAG_SUSPENDED) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09004506 struct ata_device *failed_dev;
Jens Axboe9b847542006-01-06 09:28:07 +01004507 ap->flags &= ~ATA_FLAG_SUSPENDED;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09004508 while (ata_set_mode(ap, &failed_dev))
4509 ata_dev_disable(ap, failed_dev);
Jens Axboe9b847542006-01-06 09:28:07 +01004510 }
Tejun Heoe1211e32006-04-01 01:38:18 +09004511 if (!ata_dev_enabled(dev))
Jens Axboe9b847542006-01-06 09:28:07 +01004512 return 0;
4513 if (dev->class == ATA_DEV_ATA)
4514 ata_start_drive(ap, dev);
4515
4516 return 0;
4517}
4518
4519/**
4520 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004521 * @ap: port the device is connected to
4522 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004523 *
4524 * Flush the cache on the drive, if appropriate, then issue a
4525 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004526 */
Nigel Cunningham082776e2006-03-23 23:22:16 +10004527int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
Jens Axboe9b847542006-01-06 09:28:07 +01004528{
Tejun Heoe1211e32006-04-01 01:38:18 +09004529 if (!ata_dev_enabled(dev))
Jens Axboe9b847542006-01-06 09:28:07 +01004530 return 0;
4531 if (dev->class == ATA_DEV_ATA)
4532 ata_flush_cache(ap, dev);
4533
Nigel Cunningham082776e2006-03-23 23:22:16 +10004534 if (state.event != PM_EVENT_FREEZE)
4535 ata_standby_drive(ap, dev);
Jens Axboe9b847542006-01-06 09:28:07 +01004536 ap->flags |= ATA_FLAG_SUSPENDED;
4537 return 0;
4538}
4539
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004540/**
4541 * ata_port_start - Set port up for dma.
4542 * @ap: Port to initialize
4543 *
4544 * Called just after data structures for each port are
4545 * initialized. Allocates space for PRD table.
4546 *
4547 * May be used as the port_start() entry in ata_port_operations.
4548 *
4549 * LOCKING:
4550 * Inherited from caller.
4551 */
4552
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553int ata_port_start (struct ata_port *ap)
4554{
Brian King2f1f6102006-03-23 17:30:15 -06004555 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004556 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4559 if (!ap->prd)
4560 return -ENOMEM;
4561
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004562 rc = ata_pad_alloc(ap, dev);
4563 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004564 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004565 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004566 }
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4569
4570 return 0;
4571}
4572
Edward Falk0baab862005-06-02 18:17:13 -04004573
4574/**
4575 * ata_port_stop - Undo ata_port_start()
4576 * @ap: Port to shut down
4577 *
4578 * Frees the PRD table.
4579 *
4580 * May be used as the port_stop() entry in ata_port_operations.
4581 *
4582 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004583 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004584 */
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586void ata_port_stop (struct ata_port *ap)
4587{
Brian King2f1f6102006-03-23 17:30:15 -06004588 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
4590 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004591 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592}
4593
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004594void ata_host_stop (struct ata_host_set *host_set)
4595{
4596 if (host_set->mmio_base)
4597 iounmap(host_set->mmio_base);
4598}
4599
4600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601/**
4602 * ata_host_remove - Unregister SCSI host structure with upper layers
4603 * @ap: Port to unregister
4604 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4605 *
4606 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004607 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 */
4609
4610static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4611{
4612 struct Scsi_Host *sh = ap->host;
4613
4614 DPRINTK("ENTER\n");
4615
4616 if (do_unregister)
4617 scsi_remove_host(sh);
4618
4619 ap->ops->port_stop(ap);
4620}
4621
4622/**
4623 * ata_host_init - Initialize an ata_port structure
4624 * @ap: Structure to initialize
4625 * @host: associated SCSI mid-layer structure
4626 * @host_set: Collection of hosts to which @ap belongs
4627 * @ent: Probe information provided by low-level driver
4628 * @port_no: Port number associated with this ata_port
4629 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004630 * Initialize a new ata_port structure, and its associated
4631 * scsi_host.
4632 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004634 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 */
4636
4637static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4638 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004639 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640{
4641 unsigned int i;
4642
4643 host->max_id = 16;
4644 host->max_lun = 1;
4645 host->max_channel = 1;
4646 host->unique_id = ata_unique_id++;
4647 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004648
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 ap->flags = ATA_FLAG_PORT_DISABLED;
4650 ap->id = host->unique_id;
4651 ap->host = host;
4652 ap->ctl = ATA_DEVCTL_OBS;
4653 ap->host_set = host_set;
Brian King2f1f6102006-03-23 17:30:15 -06004654 ap->dev = ent->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 ap->port_no = port_no;
4656 ap->hard_port_no =
4657 ent->legacy_mode ? ent->hard_port_no : port_no;
4658 ap->pio_mask = ent->pio_mask;
4659 ap->mwdma_mask = ent->mwdma_mask;
4660 ap->udma_mask = ent->udma_mask;
4661 ap->flags |= ent->host_flags;
4662 ap->ops = ent->port_ops;
4663 ap->cbl = ATA_CBL_NONE;
Tejun Heo1c3fae42006-04-02 20:53:28 +09004664 ap->sata_spd_limit = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 ap->active_tag = ATA_TAG_POISON;
4666 ap->last_ctl = 0xFF;
4667
Tejun Heo86e45b62006-03-05 15:29:09 +09004668 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004669 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Tejun Heoacf356b2006-03-24 14:07:50 +09004671 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4672 struct ata_device *dev = &ap->device[i];
4673 dev->devno = i;
4674 dev->pio_mask = UINT_MAX;
4675 dev->mwdma_mask = UINT_MAX;
4676 dev->udma_mask = UINT_MAX;
4677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
4679#ifdef ATA_IRQ_TRAP
4680 ap->stats.unhandled_irq = 1;
4681 ap->stats.idle_irq = 1;
4682#endif
4683
4684 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4685}
4686
4687/**
4688 * ata_host_add - Attach low-level ATA driver to system
4689 * @ent: Information provided by low-level driver
4690 * @host_set: Collections of ports to which we add
4691 * @port_no: Port number associated with this host
4692 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004693 * Attach low-level ATA driver to system.
4694 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004696 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 *
4698 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004699 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 */
4701
Jeff Garzik057ace52005-10-22 14:27:05 -04004702static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 struct ata_host_set *host_set,
4704 unsigned int port_no)
4705{
4706 struct Scsi_Host *host;
4707 struct ata_port *ap;
4708 int rc;
4709
4710 DPRINTK("ENTER\n");
Tejun Heoaec5c3c2006-03-25 01:33:34 +09004711
4712 if (!ent->port_ops->probe_reset &&
4713 !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
4714 printk(KERN_ERR "ata%u: no reset mechanism available\n",
4715 port_no);
4716 return NULL;
4717 }
4718
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4720 if (!host)
4721 return NULL;
4722
Tejun Heo30afc842006-03-18 18:40:14 +09004723 host->transportt = &ata_scsi_transport_template;
4724
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 ap = (struct ata_port *) &host->hostdata[0];
4726
4727 ata_host_init(ap, host, host_set, ent, port_no);
4728
4729 rc = ap->ops->port_start(ap);
4730 if (rc)
4731 goto err_out;
4732
4733 return ap;
4734
4735err_out:
4736 scsi_host_put(host);
4737 return NULL;
4738}
4739
4740/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004741 * ata_device_add - Register hardware device with ATA and SCSI layers
4742 * @ent: Probe information describing hardware device to be registered
4743 *
4744 * This function processes the information provided in the probe
4745 * information struct @ent, allocates the necessary ATA and SCSI
4746 * host information structures, initializes them, and registers
4747 * everything with requisite kernel subsystems.
4748 *
4749 * This function requests irqs, probes the ATA bus, and probes
4750 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 *
4752 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004753 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 *
4755 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004756 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 */
4758
Jeff Garzik057ace52005-10-22 14:27:05 -04004759int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
4761 unsigned int count = 0, i;
4762 struct device *dev = ent->dev;
4763 struct ata_host_set *host_set;
4764
4765 DPRINTK("ENTER\n");
4766 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004767 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4769 if (!host_set)
4770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 spin_lock_init(&host_set->lock);
4772
4773 host_set->dev = dev;
4774 host_set->n_ports = ent->n_ports;
4775 host_set->irq = ent->irq;
4776 host_set->mmio_base = ent->mmio_base;
4777 host_set->private_data = ent->private_data;
4778 host_set->ops = ent->port_ops;
Alan Cox5444a6f2006-03-27 18:58:20 +01004779 host_set->flags = ent->host_set_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
4781 /* register each port bound to this device */
4782 for (i = 0; i < ent->n_ports; i++) {
4783 struct ata_port *ap;
4784 unsigned long xfer_mode_mask;
4785
4786 ap = ata_host_add(ent, host_set, i);
4787 if (!ap)
4788 goto err_out;
4789
4790 host_set->ports[i] = ap;
4791 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4792 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4793 (ap->pio_mask << ATA_SHIFT_PIO);
4794
4795 /* print per-port info to dmesg */
4796 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4797 "bmdma 0x%lX irq %lu\n",
4798 ap->id,
4799 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4800 ata_mode_string(xfer_mode_mask),
4801 ap->ioaddr.cmd_addr,
4802 ap->ioaddr.ctl_addr,
4803 ap->ioaddr.bmdma_addr,
4804 ent->irq);
4805
4806 ata_chk_status(ap);
4807 host_set->ops->irq_clear(ap);
4808 count++;
4809 }
4810
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004811 if (!count)
4812 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
4814 /* obtain irq, that is shared between channels */
4815 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4816 DRV_NAME, host_set))
4817 goto err_out;
4818
4819 /* perform each probe synchronously */
4820 DPRINTK("probe begin\n");
4821 for (i = 0; i < count; i++) {
4822 struct ata_port *ap;
4823 int rc;
4824
4825 ap = host_set->ports[i];
4826
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004827 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004829 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
4831 if (rc) {
4832 /* FIXME: do something useful here?
4833 * Current libata behavior will
4834 * tear down everything when
4835 * the module is removed
4836 * or the h/w is unplugged.
4837 */
4838 }
4839
4840 rc = scsi_add_host(ap->host, dev);
4841 if (rc) {
4842 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4843 ap->id);
4844 /* FIXME: do something useful here */
4845 /* FIXME: handle unconditional calls to
4846 * scsi_scan_host and ata_host_remove, below,
4847 * at the very least
4848 */
4849 }
4850 }
4851
4852 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004853 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 for (i = 0; i < count; i++) {
4855 struct ata_port *ap = host_set->ports[i];
4856
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004857 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 }
4859
4860 dev_set_drvdata(dev, host_set);
4861
4862 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4863 return ent->n_ports; /* success */
4864
4865err_out:
4866 for (i = 0; i < count; i++) {
4867 ata_host_remove(host_set->ports[i], 1);
4868 scsi_host_put(host_set->ports[i]->host);
4869 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004870err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 kfree(host_set);
4872 VPRINTK("EXIT, returning 0\n");
4873 return 0;
4874}
4875
4876/**
Alan Cox17b14452005-09-15 15:44:00 +01004877 * ata_host_set_remove - PCI layer callback for device removal
4878 * @host_set: ATA host set that was removed
4879 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004880 * Unregister all objects associated with this host set. Free those
Alan Cox17b14452005-09-15 15:44:00 +01004881 * objects.
4882 *
4883 * LOCKING:
4884 * Inherited from calling layer (may sleep).
4885 */
4886
Alan Cox17b14452005-09-15 15:44:00 +01004887void ata_host_set_remove(struct ata_host_set *host_set)
4888{
4889 struct ata_port *ap;
4890 unsigned int i;
4891
4892 for (i = 0; i < host_set->n_ports; i++) {
4893 ap = host_set->ports[i];
4894 scsi_remove_host(ap->host);
4895 }
4896
4897 free_irq(host_set->irq, host_set);
4898
4899 for (i = 0; i < host_set->n_ports; i++) {
4900 ap = host_set->ports[i];
4901
4902 ata_scsi_release(ap->host);
4903
4904 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4905 struct ata_ioports *ioaddr = &ap->ioaddr;
4906
4907 if (ioaddr->cmd_addr == 0x1f0)
4908 release_region(0x1f0, 8);
4909 else if (ioaddr->cmd_addr == 0x170)
4910 release_region(0x170, 8);
4911 }
4912
4913 scsi_host_put(ap->host);
4914 }
4915
4916 if (host_set->ops->host_stop)
4917 host_set->ops->host_stop(host_set);
4918
4919 kfree(host_set);
4920}
4921
4922/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 * ata_scsi_release - SCSI layer callback hook for host unload
4924 * @host: libata host to be unloaded
4925 *
4926 * Performs all duties necessary to shut down a libata port...
4927 * Kill port kthread, disable port, and release resources.
4928 *
4929 * LOCKING:
4930 * Inherited from SCSI layer.
4931 *
4932 * RETURNS:
4933 * One.
4934 */
4935
4936int ata_scsi_release(struct Scsi_Host *host)
4937{
4938 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004939 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
4941 DPRINTK("ENTER\n");
4942
4943 ap->ops->port_disable(ap);
4944 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004945 for (i = 0; i < ATA_MAX_DEVICES; i++)
4946 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
4948 DPRINTK("EXIT\n");
4949 return 1;
4950}
4951
4952/**
4953 * ata_std_ports - initialize ioaddr with standard port offsets.
4954 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004955 *
4956 * Utility function which initializes data_addr, error_addr,
4957 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4958 * device_addr, status_addr, and command_addr to standard offsets
4959 * relative to cmd_addr.
4960 *
4961 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 */
Edward Falk0baab862005-06-02 18:17:13 -04004963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964void ata_std_ports(struct ata_ioports *ioaddr)
4965{
4966 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4967 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4968 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4969 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4970 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4971 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4972 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4973 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4974 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4975 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4976}
4977
Edward Falk0baab862005-06-02 18:17:13 -04004978
Jeff Garzik374b1872005-08-30 05:42:52 -04004979#ifdef CONFIG_PCI
4980
4981void ata_pci_host_stop (struct ata_host_set *host_set)
4982{
4983 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4984
4985 pci_iounmap(pdev, host_set->mmio_base);
4986}
4987
Edward Falk0baab862005-06-02 18:17:13 -04004988/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 * ata_pci_remove_one - PCI layer callback for device removal
4990 * @pdev: PCI device that was removed
4991 *
4992 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004993 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 * Handle this by unregistering all objects associated
4995 * with this PCI device. Free those objects. Then finally
4996 * release PCI resources and disable device.
4997 *
4998 * LOCKING:
4999 * Inherited from PCI layer (may sleep).
5000 */
5001
5002void ata_pci_remove_one (struct pci_dev *pdev)
5003{
5004 struct device *dev = pci_dev_to_dev(pdev);
5005 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
Alan Cox17b14452005-09-15 15:44:00 +01005007 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 pci_release_regions(pdev);
5009 pci_disable_device(pdev);
5010 dev_set_drvdata(dev, NULL);
5011}
5012
5013/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005014int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015{
5016 unsigned long tmp = 0;
5017
5018 switch (bits->width) {
5019 case 1: {
5020 u8 tmp8 = 0;
5021 pci_read_config_byte(pdev, bits->reg, &tmp8);
5022 tmp = tmp8;
5023 break;
5024 }
5025 case 2: {
5026 u16 tmp16 = 0;
5027 pci_read_config_word(pdev, bits->reg, &tmp16);
5028 tmp = tmp16;
5029 break;
5030 }
5031 case 4: {
5032 u32 tmp32 = 0;
5033 pci_read_config_dword(pdev, bits->reg, &tmp32);
5034 tmp = tmp32;
5035 break;
5036 }
5037
5038 default:
5039 return -EINVAL;
5040 }
5041
5042 tmp &= bits->mask;
5043
5044 return (tmp == bits->val) ? 1 : 0;
5045}
Jens Axboe9b847542006-01-06 09:28:07 +01005046
5047int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5048{
5049 pci_save_state(pdev);
5050 pci_disable_device(pdev);
5051 pci_set_power_state(pdev, PCI_D3hot);
5052 return 0;
5053}
5054
5055int ata_pci_device_resume(struct pci_dev *pdev)
5056{
5057 pci_set_power_state(pdev, PCI_D0);
5058 pci_restore_state(pdev);
5059 pci_enable_device(pdev);
5060 pci_set_master(pdev);
5061 return 0;
5062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063#endif /* CONFIG_PCI */
5064
5065
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066static int __init ata_init(void)
5067{
5068 ata_wq = create_workqueue("ata");
5069 if (!ata_wq)
5070 return -ENOMEM;
5071
5072 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5073 return 0;
5074}
5075
5076static void __exit ata_exit(void)
5077{
5078 destroy_workqueue(ata_wq);
5079}
5080
5081module_init(ata_init);
5082module_exit(ata_exit);
5083
Jeff Garzik67846b32005-10-05 02:58:32 -04005084static unsigned long ratelimit_time;
5085static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5086
5087int ata_ratelimit(void)
5088{
5089 int rc;
5090 unsigned long flags;
5091
5092 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5093
5094 if (time_after(jiffies, ratelimit_time)) {
5095 rc = 1;
5096 ratelimit_time = jiffies + (HZ/5);
5097 } else
5098 rc = 0;
5099
5100 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5101
5102 return rc;
5103}
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105/*
5106 * libata is essentially a library of internal helper functions for
5107 * low-level ATA host controller drivers. As such, the API/ABI is
5108 * likely to change as new drivers are added and updated.
5109 * Do not depend on ABI/API stability.
5110 */
5111
5112EXPORT_SYMBOL_GPL(ata_std_bios_param);
5113EXPORT_SYMBOL_GPL(ata_std_ports);
5114EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005115EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116EXPORT_SYMBOL_GPL(ata_sg_init);
5117EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09005118EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5120EXPORT_SYMBOL_GPL(ata_eng_timeout);
5121EXPORT_SYMBOL_GPL(ata_tf_load);
5122EXPORT_SYMBOL_GPL(ata_tf_read);
5123EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5124EXPORT_SYMBOL_GPL(ata_std_dev_select);
5125EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5126EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5127EXPORT_SYMBOL_GPL(ata_check_status);
5128EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129EXPORT_SYMBOL_GPL(ata_exec_command);
5130EXPORT_SYMBOL_GPL(ata_port_start);
5131EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005132EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133EXPORT_SYMBOL_GPL(ata_interrupt);
5134EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06005135EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5137EXPORT_SYMBOL_GPL(ata_bmdma_start);
5138EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5139EXPORT_SYMBOL_GPL(ata_bmdma_status);
5140EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5141EXPORT_SYMBOL_GPL(ata_port_probe);
5142EXPORT_SYMBOL_GPL(sata_phy_reset);
5143EXPORT_SYMBOL_GPL(__sata_phy_reset);
5144EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005145EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005146EXPORT_SYMBOL_GPL(ata_std_softreset);
5147EXPORT_SYMBOL_GPL(sata_std_hardreset);
5148EXPORT_SYMBOL_GPL(ata_std_postreset);
5149EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005150EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09005151EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005152EXPORT_SYMBOL_GPL(ata_dev_classify);
5153EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005155EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005156EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09005157EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5159EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5160EXPORT_SYMBOL_GPL(ata_scsi_error);
5161EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5162EXPORT_SYMBOL_GPL(ata_scsi_release);
5163EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo6a62a042006-02-13 10:02:46 +09005164EXPORT_SYMBOL_GPL(ata_id_string);
5165EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005167EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5168EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005170EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005171EXPORT_SYMBOL_GPL(ata_timing_compute);
5172EXPORT_SYMBOL_GPL(ata_timing_merge);
5173
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174#ifdef CONFIG_PCI
5175EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005176EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5178EXPORT_SYMBOL_GPL(ata_pci_init_one);
5179EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005180EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5181EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Alan Cox67951ad2006-03-22 15:55:54 +00005182EXPORT_SYMBOL_GPL(ata_pci_default_filter);
5183EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005185
5186EXPORT_SYMBOL_GPL(ata_device_suspend);
5187EXPORT_SYMBOL_GPL(ata_device_resume);
5188EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5189EXPORT_SYMBOL_GPL(ata_scsi_device_resume);