blob: b710fc48074024e31435940477795e40bfd2eb39 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040051#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100052#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "scsi_priv.h"
Jeff Garzik193515d2005-11-07 00:59:37 -050055#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
Tejun Heo6aff8f12006-02-15 18:24:09 +090064static unsigned int ata_dev_init_params(struct ata_port *ap,
65 struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ata_set_mode(struct ata_port *ap);
67static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040068static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040070static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u8 *xfer_mode_out,
72 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static unsigned int ata_unique_id = 1;
75static struct workqueue_struct *ata_wq;
76
Jeff Garzik1623c812005-08-30 03:37:42 -040077int atapi_enabled = 0;
78module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
Jeff Garzikc3c013a2006-02-27 22:31:19 -050081int libata_fua = 0;
82module_param_named(fua, libata_fua, int, 0444);
83MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_AUTHOR("Jeff Garzik");
86MODULE_DESCRIPTION("Library module for ATA devices");
87MODULE_LICENSE("GPL");
88MODULE_VERSION(DRV_VERSION);
89
Edward Falk0baab862005-06-02 18:17:13 -040090
91/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
93 * @tf: Taskfile to convert
94 * @fis: Buffer into which data will output
95 * @pmp: Port multiplier port
96 *
97 * Converts a standard ATA taskfile to a Serial ATA
98 * FIS structure (Register - Host to Device).
99 *
100 * LOCKING:
101 * Inherited from caller.
102 */
103
Jeff Garzik057ace52005-10-22 14:27:05 -0400104void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 fis[0] = 0x27; /* Register - Host to Device FIS */
107 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
108 bit 7 indicates Command FIS */
109 fis[2] = tf->command;
110 fis[3] = tf->feature;
111
112 fis[4] = tf->lbal;
113 fis[5] = tf->lbam;
114 fis[6] = tf->lbah;
115 fis[7] = tf->device;
116
117 fis[8] = tf->hob_lbal;
118 fis[9] = tf->hob_lbam;
119 fis[10] = tf->hob_lbah;
120 fis[11] = tf->hob_feature;
121
122 fis[12] = tf->nsect;
123 fis[13] = tf->hob_nsect;
124 fis[14] = 0;
125 fis[15] = tf->ctl;
126
127 fis[16] = 0;
128 fis[17] = 0;
129 fis[18] = 0;
130 fis[19] = 0;
131}
132
133/**
134 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
135 * @fis: Buffer from which data will be input
136 * @tf: Taskfile to output
137 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500138 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
140 * LOCKING:
141 * Inherited from caller.
142 */
143
Jeff Garzik057ace52005-10-22 14:27:05 -0400144void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 tf->command = fis[2]; /* status */
147 tf->feature = fis[3]; /* error */
148
149 tf->lbal = fis[4];
150 tf->lbam = fis[5];
151 tf->lbah = fis[6];
152 tf->device = fis[7];
153
154 tf->hob_lbal = fis[8];
155 tf->hob_lbam = fis[9];
156 tf->hob_lbah = fis[10];
157
158 tf->nsect = fis[12];
159 tf->hob_nsect = fis[13];
160}
161
Albert Lee8cbd6df2005-10-12 15:06:27 +0800162static const u8 ata_rw_cmds[] = {
163 /* pio multi */
164 ATA_CMD_READ_MULTI,
165 ATA_CMD_WRITE_MULTI,
166 ATA_CMD_READ_MULTI_EXT,
167 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100168 0,
169 0,
170 0,
171 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800172 /* pio */
173 ATA_CMD_PIO_READ,
174 ATA_CMD_PIO_WRITE,
175 ATA_CMD_PIO_READ_EXT,
176 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100177 0,
178 0,
179 0,
180 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800181 /* dma */
182 ATA_CMD_READ,
183 ATA_CMD_WRITE,
184 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100185 ATA_CMD_WRITE_EXT,
186 0,
187 0,
188 0,
189 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800190};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800193 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
194 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800196 * Examine the device configuration and tf->flags to calculate
197 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * LOCKING:
200 * caller.
201 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100202int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800204 struct ata_taskfile *tf = &qc->tf;
205 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100208 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800209
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100210 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800211 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
212 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Albert Lee8cbd6df2005-10-12 15:06:27 +0800214 if (dev->flags & ATA_DFLAG_PIO) {
215 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100216 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000217 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
218 /* Unable to use DMA due to host limitation */
219 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800220 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800221 } else {
222 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100223 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100226 cmd = ata_rw_cmds[index + fua + lba48 + write];
227 if (cmd) {
228 tf->command = cmd;
229 return 0;
230 }
231 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100234static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 "UDMA/16",
236 "UDMA/25",
237 "UDMA/33",
238 "UDMA/44",
239 "UDMA/66",
240 "UDMA/100",
241 "UDMA/133",
242 "UDMA7",
243 "MWDMA0",
244 "MWDMA1",
245 "MWDMA2",
246 "PIO0",
247 "PIO1",
248 "PIO2",
249 "PIO3",
250 "PIO4",
251};
252
253/**
254 * ata_udma_string - convert UDMA bit offset to string
255 * @mask: mask of bits supported; only highest bit counts.
256 *
257 * Determine string which represents the highest speed
258 * (highest bit in @udma_mask).
259 *
260 * LOCKING:
261 * None.
262 *
263 * RETURNS:
264 * Constant C string representing highest speed listed in
265 * @udma_mask, or the constant C string "<n/a>".
266 */
267
268static const char *ata_mode_string(unsigned int mask)
269{
270 int i;
271
272 for (i = 7; i >= 0; i--)
273 if (mask & (1 << i))
274 goto out;
275 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
276 if (mask & (1 << i))
277 goto out;
278 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
279 if (mask & (1 << i))
280 goto out;
281
282 return "<n/a>";
283
284out:
285 return xfer_mode_str[i];
286}
287
288/**
289 * ata_pio_devchk - PATA device presence detection
290 * @ap: ATA channel to examine
291 * @device: Device to examine (starting at zero)
292 *
293 * This technique was originally described in
294 * Hale Landis's ATADRVR (www.ata-atapi.com), and
295 * later found its way into the ATA/ATAPI spec.
296 *
297 * Write a pattern to the ATA shadow registers,
298 * and if a device is present, it will respond by
299 * correctly storing and echoing back the
300 * ATA shadow register contents.
301 *
302 * LOCKING:
303 * caller.
304 */
305
306static unsigned int ata_pio_devchk(struct ata_port *ap,
307 unsigned int device)
308{
309 struct ata_ioports *ioaddr = &ap->ioaddr;
310 u8 nsect, lbal;
311
312 ap->ops->dev_select(ap, device);
313
314 outb(0x55, ioaddr->nsect_addr);
315 outb(0xaa, ioaddr->lbal_addr);
316
317 outb(0xaa, ioaddr->nsect_addr);
318 outb(0x55, ioaddr->lbal_addr);
319
320 outb(0x55, ioaddr->nsect_addr);
321 outb(0xaa, ioaddr->lbal_addr);
322
323 nsect = inb(ioaddr->nsect_addr);
324 lbal = inb(ioaddr->lbal_addr);
325
326 if ((nsect == 0x55) && (lbal == 0xaa))
327 return 1; /* we found a device */
328
329 return 0; /* nothing found */
330}
331
332/**
333 * ata_mmio_devchk - PATA device presence detection
334 * @ap: ATA channel to examine
335 * @device: Device to examine (starting at zero)
336 *
337 * This technique was originally described in
338 * Hale Landis's ATADRVR (www.ata-atapi.com), and
339 * later found its way into the ATA/ATAPI spec.
340 *
341 * Write a pattern to the ATA shadow registers,
342 * and if a device is present, it will respond by
343 * correctly storing and echoing back the
344 * ATA shadow register contents.
345 *
346 * LOCKING:
347 * caller.
348 */
349
350static unsigned int ata_mmio_devchk(struct ata_port *ap,
351 unsigned int device)
352{
353 struct ata_ioports *ioaddr = &ap->ioaddr;
354 u8 nsect, lbal;
355
356 ap->ops->dev_select(ap, device);
357
358 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
359 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
360
361 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
362 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
363
364 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
365 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
366
367 nsect = readb((void __iomem *) ioaddr->nsect_addr);
368 lbal = readb((void __iomem *) ioaddr->lbal_addr);
369
370 if ((nsect == 0x55) && (lbal == 0xaa))
371 return 1; /* we found a device */
372
373 return 0; /* nothing found */
374}
375
376/**
377 * ata_devchk - PATA device presence detection
378 * @ap: ATA channel to examine
379 * @device: Device to examine (starting at zero)
380 *
381 * Dispatch ATA device presence detection, depending
382 * on whether we are using PIO or MMIO to talk to the
383 * ATA shadow registers.
384 *
385 * LOCKING:
386 * caller.
387 */
388
389static unsigned int ata_devchk(struct ata_port *ap,
390 unsigned int device)
391{
392 if (ap->flags & ATA_FLAG_MMIO)
393 return ata_mmio_devchk(ap, device);
394 return ata_pio_devchk(ap, device);
395}
396
397/**
398 * ata_dev_classify - determine device type based on ATA-spec signature
399 * @tf: ATA taskfile register set for device to be identified
400 *
401 * Determine from taskfile register contents whether a device is
402 * ATA or ATAPI, as per "Signature and persistence" section
403 * of ATA/PI spec (volume 1, sect 5.14).
404 *
405 * LOCKING:
406 * None.
407 *
408 * RETURNS:
409 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
410 * the event of failure.
411 */
412
Jeff Garzik057ace52005-10-22 14:27:05 -0400413unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 /* Apple's open source Darwin code hints that some devices only
416 * put a proper signature into the LBA mid/high registers,
417 * So, we only check those. It's sufficient for uniqueness.
418 */
419
420 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
421 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
422 DPRINTK("found ATA device by sig\n");
423 return ATA_DEV_ATA;
424 }
425
426 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
427 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
428 DPRINTK("found ATAPI device by sig\n");
429 return ATA_DEV_ATAPI;
430 }
431
432 DPRINTK("unknown device\n");
433 return ATA_DEV_UNKNOWN;
434}
435
436/**
437 * ata_dev_try_classify - Parse returned ATA device signature
438 * @ap: ATA channel to examine
439 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900440 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *
442 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
443 * an ATA/ATAPI-defined set of values is placed in the ATA
444 * shadow registers, indicating the results of device detection
445 * and diagnostics.
446 *
447 * Select the ATA device, and read the values from the ATA shadow
448 * registers. Then parse according to the Error register value,
449 * and the spec-defined values examined by ata_dev_classify().
450 *
451 * LOCKING:
452 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900453 *
454 * RETURNS:
455 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
457
Tejun Heob4dc7622006-01-24 17:05:22 +0900458static unsigned int
459ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct ata_taskfile tf;
462 unsigned int class;
463 u8 err;
464
465 ap->ops->dev_select(ap, device);
466
467 memset(&tf, 0, sizeof(tf));
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400470 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900471 if (r_err)
472 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* see if device passed diags */
475 if (err == 1)
476 /* do nothing */ ;
477 else if ((device == 0) && (err == 0x81))
478 /* do nothing */ ;
479 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900480 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Tejun Heob4dc7622006-01-24 17:05:22 +0900482 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900486 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900488 return ATA_DEV_NONE;
489 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900493 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * @id: IDENTIFY DEVICE results we will examine
495 * @s: string into which data is output
496 * @ofs: offset into identify device page
497 * @len: length of string to return. must be an even number.
498 *
499 * The strings in the IDENTIFY DEVICE page are broken up into
500 * 16-bit chunks. Run through the string, and output each
501 * 8-bit chunk linearly, regardless of platform.
502 *
503 * LOCKING:
504 * caller.
505 */
506
Tejun Heo6a62a042006-02-13 10:02:46 +0900507void ata_id_string(const u16 *id, unsigned char *s,
508 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 unsigned int c;
511
512 while (len > 0) {
513 c = id[ofs] >> 8;
514 *s = c;
515 s++;
516
517 c = id[ofs] & 0xff;
518 *s = c;
519 s++;
520
521 ofs++;
522 len -= 2;
523 }
524}
525
Tejun Heo0e949ff2006-02-12 22:47:04 +0900526/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900527 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900528 * @id: IDENTIFY DEVICE results we will examine
529 * @s: string into which data is output
530 * @ofs: offset into identify device page
531 * @len: length of string to return. must be an odd number.
532 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900533 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900534 * trims trailing spaces and terminates the resulting string with
535 * null. @len must be actual maximum length (even number) + 1.
536 *
537 * LOCKING:
538 * caller.
539 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900540void ata_id_c_string(const u16 *id, unsigned char *s,
541 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900542{
543 unsigned char *p;
544
545 WARN_ON(!(len & 1));
546
Tejun Heo6a62a042006-02-13 10:02:46 +0900547 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900548
549 p = s + strnlen(s, len - 1);
550 while (p > s && p[-1] == ' ')
551 p--;
552 *p = '\0';
553}
Edward Falk0baab862005-06-02 18:17:13 -0400554
Tejun Heo29407402006-02-12 22:47:04 +0900555static u64 ata_id_n_sectors(const u16 *id)
556{
557 if (ata_id_has_lba(id)) {
558 if (ata_id_has_lba48(id))
559 return ata_id_u64(id, 100);
560 else
561 return ata_id_u32(id, 60);
562 } else {
563 if (ata_id_current_chs_valid(id))
564 return ata_id_u32(id, 57);
565 else
566 return id[1] * id[3] * id[6];
567 }
568}
569
Edward Falk0baab862005-06-02 18:17:13 -0400570/**
571 * ata_noop_dev_select - Select device 0/1 on ATA bus
572 * @ap: ATA channel to manipulate
573 * @device: ATA device (numbered from zero) to select
574 *
575 * This function performs no actual function.
576 *
577 * May be used as the dev_select() entry in ata_port_operations.
578 *
579 * LOCKING:
580 * caller.
581 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
583{
584}
585
Edward Falk0baab862005-06-02 18:17:13 -0400586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/**
588 * ata_std_dev_select - Select device 0/1 on ATA bus
589 * @ap: ATA channel to manipulate
590 * @device: ATA device (numbered from zero) to select
591 *
592 * Use the method defined in the ATA specification to
593 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400594 * ATA channel. Works with both PIO and MMIO.
595 *
596 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 *
598 * LOCKING:
599 * caller.
600 */
601
602void ata_std_dev_select (struct ata_port *ap, unsigned int device)
603{
604 u8 tmp;
605
606 if (device == 0)
607 tmp = ATA_DEVICE_OBS;
608 else
609 tmp = ATA_DEVICE_OBS | ATA_DEV1;
610
611 if (ap->flags & ATA_FLAG_MMIO) {
612 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
613 } else {
614 outb(tmp, ap->ioaddr.device_addr);
615 }
616 ata_pause(ap); /* needed; also flushes, for mmio */
617}
618
619/**
620 * ata_dev_select - Select device 0/1 on ATA bus
621 * @ap: ATA channel to manipulate
622 * @device: ATA device (numbered from zero) to select
623 * @wait: non-zero to wait for Status register BSY bit to clear
624 * @can_sleep: non-zero if context allows sleeping
625 *
626 * Use the method defined in the ATA specification to
627 * make either device 0, or device 1, active on the
628 * ATA channel.
629 *
630 * This is a high-level version of ata_std_dev_select(),
631 * which additionally provides the services of inserting
632 * the proper pauses and status polling, where needed.
633 *
634 * LOCKING:
635 * caller.
636 */
637
638void ata_dev_select(struct ata_port *ap, unsigned int device,
639 unsigned int wait, unsigned int can_sleep)
640{
641 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
642 ap->id, device, wait);
643
644 if (wait)
645 ata_wait_idle(ap);
646
647 ap->ops->dev_select(ap, device);
648
649 if (wait) {
650 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
651 msleep(150);
652 ata_wait_idle(ap);
653 }
654}
655
656/**
657 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900658 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900660 * Dump selected 16-bit words from the given IDENTIFY DEVICE
661 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 *
663 * LOCKING:
664 * caller.
665 */
666
Tejun Heo0bd33002006-02-12 22:47:05 +0900667static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 DPRINTK("49==0x%04x "
670 "53==0x%04x "
671 "63==0x%04x "
672 "64==0x%04x "
673 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900674 id[49],
675 id[53],
676 id[63],
677 id[64],
678 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 DPRINTK("80==0x%04x "
680 "81==0x%04x "
681 "82==0x%04x "
682 "83==0x%04x "
683 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900684 id[80],
685 id[81],
686 id[82],
687 id[83],
688 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 DPRINTK("88==0x%04x "
690 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900691 id[88],
692 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Alan Cox11e29e22005-10-21 18:46:32 -0400695/*
696 * Compute the PIO modes available for this device. This is not as
697 * trivial as it seems if we must consider early devices correctly.
698 *
699 * FIXME: pre IDE drive timing (do we care ?).
700 */
701
Jeff Garzik057ace52005-10-22 14:27:05 -0400702static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -0400703{
704 u16 modes;
705
Alan Coxffa29452006-01-09 17:14:40 +0000706 /* Usual case. Word 53 indicates word 64 is valid */
707 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -0400708 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
709 modes <<= 3;
710 modes |= 0x7;
711 return modes;
712 }
713
Alan Coxffa29452006-01-09 17:14:40 +0000714 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
715 number for the maximum. Turn it into a mask and return it */
716 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -0400717 return modes;
Alan Coxffa29452006-01-09 17:14:40 +0000718 /* But wait.. there's more. Design your standards by committee and
719 you too can get a free iordy field to process. However its the
720 speeds not the modes that are supported... Note drivers using the
721 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -0400722}
723
Tejun Heo95064372006-01-23 13:09:37 +0900724static inline void
725ata_queue_packet_task(struct ata_port *ap)
726{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900727 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
728 queue_work(ata_wq, &ap->packet_task);
Tejun Heo95064372006-01-23 13:09:37 +0900729}
730
731static inline void
732ata_queue_pio_task(struct ata_port *ap)
733{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900734 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
735 queue_work(ata_wq, &ap->pio_task);
Tejun Heo95064372006-01-23 13:09:37 +0900736}
737
738static inline void
739ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
740{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900741 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
742 queue_delayed_work(ata_wq, &ap->pio_task, delay);
743}
744
745/**
746 * ata_flush_pio_tasks - Flush pio_task and packet_task
747 * @ap: the target ata_port
748 *
749 * After this function completes, pio_task and packet_task are
750 * guranteed not to be running or scheduled.
751 *
752 * LOCKING:
753 * Kernel thread context (may sleep)
754 */
755
756static void ata_flush_pio_tasks(struct ata_port *ap)
757{
758 int tmp = 0;
759 unsigned long flags;
760
761 DPRINTK("ENTER\n");
762
763 spin_lock_irqsave(&ap->host_set->lock, flags);
764 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
765 spin_unlock_irqrestore(&ap->host_set->lock, flags);
766
767 DPRINTK("flush #1\n");
768 flush_workqueue(ata_wq);
769
770 /*
771 * At this point, if a task is running, it's guaranteed to see
772 * the FLUSH flag; thus, it will never queue pio tasks again.
773 * Cancel and flush.
774 */
775 tmp |= cancel_delayed_work(&ap->pio_task);
776 tmp |= cancel_delayed_work(&ap->packet_task);
777 if (!tmp) {
778 DPRINTK("flush #2\n");
779 flush_workqueue(ata_wq);
780 }
781
782 spin_lock_irqsave(&ap->host_set->lock, flags);
783 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
784 spin_unlock_irqrestore(&ap->host_set->lock, flags);
785
786 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +0900787}
788
Tejun Heo77853bf2006-01-23 13:09:36 +0900789void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900790{
Tejun Heo77853bf2006-01-23 13:09:36 +0900791 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900792
Tejun Heo77853bf2006-01-23 13:09:36 +0900793 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900794 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900795}
796
797/**
798 * ata_exec_internal - execute libata internal command
799 * @ap: Port to which the command is sent
800 * @dev: Device to which the command is sent
801 * @tf: Taskfile registers for the command and the result
802 * @dma_dir: Data tranfer direction of the command
803 * @buf: Data buffer of the command
804 * @buflen: Length of data buffer
805 *
806 * Executes libata internal command with timeout. @tf contains
807 * command on entry and result on return. Timeout and error
808 * conditions are reported via return value. No recovery action
809 * is taken after a command times out. It's caller's duty to
810 * clean up after timeout.
811 *
812 * LOCKING:
813 * None. Should be called with kernel context, might sleep.
814 */
815
816static unsigned
817ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
818 struct ata_taskfile *tf,
819 int dma_dir, void *buf, unsigned int buflen)
820{
821 u8 command = tf->command;
822 struct ata_queued_cmd *qc;
823 DECLARE_COMPLETION(wait);
824 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900825 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900826
827 spin_lock_irqsave(&ap->host_set->lock, flags);
828
829 qc = ata_qc_new_init(ap, dev);
830 BUG_ON(qc == NULL);
831
832 qc->tf = *tf;
833 qc->dma_dir = dma_dir;
834 if (dma_dir != DMA_NONE) {
835 ata_sg_init_one(qc, buf, buflen);
836 qc->nsect = buflen / ATA_SECT_SIZE;
837 }
838
Tejun Heo77853bf2006-01-23 13:09:36 +0900839 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900840 qc->complete_fn = ata_qc_complete_internal;
841
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900842 qc->err_mask = ata_qc_issue(qc);
843 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900844 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900845
846 spin_unlock_irqrestore(&ap->host_set->lock, flags);
847
848 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
849 spin_lock_irqsave(&ap->host_set->lock, flags);
850
851 /* We're racing with irq here. If we lose, the
852 * following test prevents us from completing the qc
853 * again. If completion irq occurs after here but
854 * before the caller cleans up, it will result in a
855 * spurious interrupt. We can live with that.
856 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900857 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900858 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900859 ata_qc_complete(qc);
860 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
861 ap->id, command);
862 }
863
864 spin_unlock_irqrestore(&ap->host_set->lock, flags);
865 }
866
Tejun Heo77853bf2006-01-23 13:09:36 +0900867 *tf = qc->tf;
868 err_mask = qc->err_mask;
869
870 ata_qc_free(qc);
871
872 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +0000876 * ata_pio_need_iordy - check if iordy needed
877 * @adev: ATA device
878 *
879 * Check if the current speed of the device requires IORDY. Used
880 * by various controllers for chip configuration.
881 */
882
883unsigned int ata_pio_need_iordy(const struct ata_device *adev)
884{
885 int pio;
886 int speed = adev->pio_mode - XFER_PIO_0;
887
888 if (speed < 2)
889 return 0;
890 if (speed > 2)
891 return 1;
892
893 /* If we have no drive specific rule, then PIO 2 is non IORDY */
894
895 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
896 pio = adev->id[ATA_ID_EIDE_PIO];
897 /* Is the speed faster than the drive allows non IORDY ? */
898 if (pio) {
899 /* This is cycle times not frequency - watch the logic! */
900 if (pio > 240) /* PIO2 is 240nS per cycle */
901 return 1;
902 return 0;
903 }
904 }
905 return 0;
906}
907
908/**
Tejun Heo49016ac2006-02-21 02:12:11 +0900909 * ata_dev_read_id - Read ID data from the specified device
910 * @ap: port on which target device resides
911 * @dev: target device
912 * @p_class: pointer to class of the target device (may be changed)
913 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +0900914 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +0900915 *
916 * Read ID data from the specified device. ATA_CMD_ID_ATA is
917 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
918 * devices. This function also takes care of EDD signature
919 * misreporting (to be removed once EDD support is gone) and
920 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
921 *
922 * LOCKING:
923 * Kernel thread context (may sleep)
924 *
925 * RETURNS:
926 * 0 on success, -errno otherwise.
927 */
928static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +0900929 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +0900930{
931 unsigned int class = *p_class;
932 unsigned int using_edd;
933 struct ata_taskfile tf;
934 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +0900935 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +0900936 const char *reason;
937 int rc;
938
939 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
940
941 if (ap->ops->probe_reset ||
942 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
943 using_edd = 0;
944 else
945 using_edd = 1;
946
947 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
948
Tejun Heod9572b12006-03-01 16:09:35 +0900949 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
950 if (id == NULL) {
951 rc = -ENOMEM;
952 reason = "out of memory";
953 goto err_out;
954 }
955
Tejun Heo49016ac2006-02-21 02:12:11 +0900956 retry:
957 ata_tf_init(ap, &tf, dev->devno);
958
959 switch (class) {
960 case ATA_DEV_ATA:
961 tf.command = ATA_CMD_ID_ATA;
962 break;
963 case ATA_DEV_ATAPI:
964 tf.command = ATA_CMD_ID_ATAPI;
965 break;
966 default:
967 rc = -ENODEV;
968 reason = "unsupported class";
969 goto err_out;
970 }
971
972 tf.protocol = ATA_PROT_PIO;
973
974 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
975 id, sizeof(id[0]) * ATA_ID_WORDS);
976
977 if (err_mask) {
978 rc = -EIO;
979 reason = "I/O error";
980
981 if (err_mask & ~AC_ERR_DEV)
982 goto err_out;
983
984 /*
985 * arg! EDD works for all test cases, but seems to return
986 * the ATA signature for some ATAPI devices. Until the
987 * reason for this is found and fixed, we fix up the mess
988 * here. If IDENTIFY DEVICE returns command aborted
989 * (as ATAPI devices do), then we issue an
990 * IDENTIFY PACKET DEVICE.
991 *
992 * ATA software reset (SRST, the default) does not appear
993 * to have this problem.
994 */
995 if ((using_edd) && (class == ATA_DEV_ATA)) {
996 u8 err = tf.feature;
997 if (err & ATA_ABORTED) {
998 class = ATA_DEV_ATAPI;
999 goto retry;
1000 }
1001 }
1002 goto err_out;
1003 }
1004
1005 swap_buf_le16(id, ATA_ID_WORDS);
1006
1007 /* print device capabilities */
1008 printk(KERN_DEBUG "ata%u: dev %u cfg "
1009 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1010 ap->id, dev->devno,
1011 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1012
1013 /* sanity check */
1014 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1015 rc = -EINVAL;
1016 reason = "device reports illegal type";
1017 goto err_out;
1018 }
1019
1020 if (post_reset && class == ATA_DEV_ATA) {
1021 /*
1022 * The exact sequence expected by certain pre-ATA4 drives is:
1023 * SRST RESET
1024 * IDENTIFY
1025 * INITIALIZE DEVICE PARAMETERS
1026 * anything else..
1027 * Some drives were very specific about that exact sequence.
1028 */
1029 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1030 err_mask = ata_dev_init_params(ap, dev);
1031 if (err_mask) {
1032 rc = -EIO;
1033 reason = "INIT_DEV_PARAMS failed";
1034 goto err_out;
1035 }
1036
1037 /* current CHS translation info (id[53-58]) might be
1038 * changed. reread the identify device info.
1039 */
1040 post_reset = 0;
1041 goto retry;
1042 }
1043 }
1044
1045 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001046 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001047 return 0;
1048
1049 err_out:
1050 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1051 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001052 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001053 return rc;
1054}
1055
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001056static inline u8 ata_dev_knobble(const struct ata_port *ap,
1057 struct ata_device *dev)
1058{
1059 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1060}
1061
Tejun Heo49016ac2006-02-21 02:12:11 +09001062/**
Tejun Heoffeae412006-03-01 16:09:35 +09001063 * ata_dev_configure - Configure the specified ATA/ATAPI device
1064 * @ap: Port on which target device resides
1065 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 *
Tejun Heoffeae412006-03-01 16:09:35 +09001067 * Configure @dev according to @dev->id. Generic and low-level
1068 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 *
1070 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001071 * Kernel thread context (may sleep)
1072 *
1073 * RETURNS:
1074 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 */
Tejun Heoffeae412006-03-01 16:09:35 +09001076static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 unsigned long xfer_modes;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001079 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (!ata_dev_present(dev)) {
1082 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001083 ap->id, dev->devno);
1084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
Tejun Heoffeae412006-03-01 16:09:35 +09001087 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 /*
1090 * common ATA, ATAPI feature tests
1091 */
1092
Albert Lee8bf62ec2005-05-12 15:29:42 -04001093 /* we require DMA support (bits 8 of word 49) */
1094 if (!ata_id_has_dma(dev->id)) {
1095 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001096 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto err_out_nosup;
1098 }
1099
1100 /* quick-n-dirty find max transfer mode; for printk only */
1101 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1102 if (!xfer_modes)
1103 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001104 if (!xfer_modes)
1105 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Tejun Heo0bd33002006-02-12 22:47:05 +09001107 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* ATA-specific feature tests */
1110 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001111 dev->n_sectors = ata_id_n_sectors(dev->id);
1112
Albert Lee8bf62ec2005-05-12 15:29:42 -04001113 if (ata_id_has_lba(dev->id)) {
1114 dev->flags |= ATA_DFLAG_LBA;
1115
Tejun Heo29407402006-02-12 22:47:04 +09001116 if (ata_id_has_lba48(dev->id))
Albert Lee8bf62ec2005-05-12 15:29:42 -04001117 dev->flags |= ATA_DFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001118
1119 /* print device info to dmesg */
1120 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001121 ap->id, dev->devno,
Tejun Heo49016ac2006-02-21 02:12:11 +09001122 ata_id_major_version(dev->id),
Albert Lee8bf62ec2005-05-12 15:29:42 -04001123 ata_mode_string(xfer_modes),
1124 (unsigned long long)dev->n_sectors,
1125 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
Tejun Heoffeae412006-03-01 16:09:35 +09001126 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001127 /* CHS */
1128
1129 /* Default translation */
1130 dev->cylinders = dev->id[1];
1131 dev->heads = dev->id[3];
1132 dev->sectors = dev->id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001133
1134 if (ata_id_current_chs_valid(dev->id)) {
1135 /* Current CHS translation is valid. */
1136 dev->cylinders = dev->id[54];
1137 dev->heads = dev->id[55];
1138 dev->sectors = dev->id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001139 }
1140
1141 /* print device info to dmesg */
1142 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001143 ap->id, dev->devno,
Tejun Heo49016ac2006-02-21 02:12:11 +09001144 ata_id_major_version(dev->id),
Albert Lee8bf62ec2005-05-12 15:29:42 -04001145 ata_mode_string(xfer_modes),
1146 (unsigned long long)dev->n_sectors,
1147 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
Tejun Heo6e7846e2006-02-12 23:32:58 +09001151 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
1154 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001155 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 rc = atapi_cdb_len(dev->id);
1157 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1158 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001159 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto err_out_nosup;
1161 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001162 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /* print device info to dmesg */
1165 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001166 ap->id, dev->devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ata_mode_string(xfer_modes));
1168 }
1169
Tejun Heo6e7846e2006-02-12 23:32:58 +09001170 ap->host->max_cmd_len = 0;
1171 for (i = 0; i < ATA_MAX_DEVICES; i++)
1172 ap->host->max_cmd_len = max_t(unsigned int,
1173 ap->host->max_cmd_len,
1174 ap->device[i].cdb_len);
1175
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001176 /* limit bridge transfers to udma5, 200 sectors */
1177 if (ata_dev_knobble(ap, dev)) {
1178 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1179 ap->id, dev->devno);
1180 ap->udma_mask &= ATA_UDMA5;
1181 dev->max_sectors = ATA_MAX_SECTORS;
1182 }
1183
1184 if (ap->ops->dev_config)
1185 ap->ops->dev_config(ap, dev);
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190err_out_nosup:
1191 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001192 ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001194 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197/**
1198 * ata_bus_probe - Reset and probe ATA bus
1199 * @ap: Bus to probe
1200 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001201 * Master ATA bus probing function. Initiates a hardware-dependent
1202 * bus reset, then attempts to identify any devices found on
1203 * the bus.
1204 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001206 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 *
1208 * RETURNS:
1209 * Zero on success, non-zero on error.
1210 */
1211
1212static int ata_bus_probe(struct ata_port *ap)
1213{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001214 unsigned int classes[ATA_MAX_DEVICES];
1215 unsigned int i, rc, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Tejun Heo28ca5c52006-03-01 16:09:36 +09001217 ata_port_probe(ap);
1218
1219 /* reset */
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001220 if (ap->ops->probe_reset) {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001221 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001222 if (rc) {
1223 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1224 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001225 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001226
1227 for (i = 0; i < ATA_MAX_DEVICES; i++)
1228 if (classes[i] == ATA_DEV_UNKNOWN)
1229 classes[i] = ATA_DEV_NONE;
1230 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001231 ap->ops->phy_reset(ap);
1232
Tejun Heo28ca5c52006-03-01 16:09:36 +09001233 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1234 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1235 classes[i] = ap->device[i].class;
1236 else
1237 ap->device[i].class = ATA_DEV_UNKNOWN;
1238 }
1239 ata_port_probe(ap);
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Tejun Heo28ca5c52006-03-01 16:09:36 +09001242 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoffeae412006-03-01 16:09:35 +09001244 struct ata_device *dev = &ap->device[i];
1245
Tejun Heo28ca5c52006-03-01 16:09:36 +09001246 dev->class = classes[i];
1247
Tejun Heoffeae412006-03-01 16:09:35 +09001248 if (!ata_dev_present(dev))
1249 continue;
1250
1251 WARN_ON(dev->id != NULL);
1252 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1253 dev->class = ATA_DEV_NONE;
1254 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Tejun Heoffeae412006-03-01 16:09:35 +09001256
1257 if (ata_dev_configure(ap, dev)) {
1258 dev->class++; /* disable device */
1259 continue;
1260 }
1261
Tejun Heoffeae412006-03-01 16:09:35 +09001262 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
Tejun Heo28ca5c52006-03-01 16:09:36 +09001265 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 goto err_out_disable;
1267
1268 ata_set_mode(ap);
1269 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1270 goto err_out_disable;
1271
1272 return 0;
1273
1274err_out_disable:
1275 ap->ops->port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return -1;
1277}
1278
1279/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001280 * ata_port_probe - Mark port as enabled
1281 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001283 * Modify @ap data structure such that the system
1284 * thinks that the entire port is enabled.
1285 *
1286 * LOCKING: host_set lock, or some other form of
1287 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 */
1289
1290void ata_port_probe(struct ata_port *ap)
1291{
1292 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1293}
1294
1295/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001296 * sata_print_link_status - Print SATA link status
1297 * @ap: SATA port to printk link status about
1298 *
1299 * This function prints link speed and status of a SATA link.
1300 *
1301 * LOCKING:
1302 * None.
1303 */
1304static void sata_print_link_status(struct ata_port *ap)
1305{
1306 u32 sstatus, tmp;
1307 const char *speed;
1308
1309 if (!ap->ops->scr_read)
1310 return;
1311
1312 sstatus = scr_read(ap, SCR_STATUS);
1313
1314 if (sata_dev_present(ap)) {
1315 tmp = (sstatus >> 4) & 0xf;
1316 if (tmp & (1 << 0))
1317 speed = "1.5";
1318 else if (tmp & (1 << 1))
1319 speed = "3.0";
1320 else
1321 speed = "<unknown>";
1322 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1323 ap->id, speed, sstatus);
1324 } else {
1325 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1326 ap->id, sstatus);
1327 }
1328}
1329
1330/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001331 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1332 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001334 * This function issues commands to standard SATA Sxxx
1335 * PHY registers, to wake up the phy (and device), and
1336 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 *
1338 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001339 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 *
1341 */
1342void __sata_phy_reset(struct ata_port *ap)
1343{
1344 u32 sstatus;
1345 unsigned long timeout = jiffies + (HZ * 5);
1346
1347 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001348 /* issue phy wake/reset */
1349 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001350 /* Couldn't find anything in SATA I/II specs, but
1351 * AHCI-1.1 10.4.2 says at least 1 ms. */
1352 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Brett Russcdcca892005-03-28 15:10:27 -05001354 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /* wait for phy to become ready, if necessary */
1357 do {
1358 msleep(200);
1359 sstatus = scr_read(ap, SCR_STATUS);
1360 if ((sstatus & 0xf) != 1)
1361 break;
1362 } while (time_before(jiffies, timeout));
1363
Tejun Heo3be680b2005-12-19 22:35:02 +09001364 /* print link status */
1365 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001366
Tejun Heo3be680b2005-12-19 22:35:02 +09001367 /* TODO: phy layer with polling, timeouts, etc. */
1368 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001370 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1374 return;
1375
1376 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1377 ata_port_disable(ap);
1378 return;
1379 }
1380
1381 ap->cbl = ATA_CBL_SATA;
1382}
1383
1384/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001385 * sata_phy_reset - Reset SATA bus.
1386 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001388 * This function resets the SATA bus, and then probes
1389 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 *
1391 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001392 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 *
1394 */
1395void sata_phy_reset(struct ata_port *ap)
1396{
1397 __sata_phy_reset(ap);
1398 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1399 return;
1400 ata_bus_reset(ap);
1401}
1402
1403/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001404 * ata_port_disable - Disable port.
1405 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001407 * Modify @ap data structure such that the system
1408 * thinks that the entire port is disabled, and should
1409 * never attempt to probe or communicate with devices
1410 * on this port.
1411 *
1412 * LOCKING: host_set lock, or some other form of
1413 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
1415
1416void ata_port_disable(struct ata_port *ap)
1417{
1418 ap->device[0].class = ATA_DEV_NONE;
1419 ap->device[1].class = ATA_DEV_NONE;
1420 ap->flags |= ATA_FLAG_PORT_DISABLED;
1421}
1422
Alan Cox452503f2005-10-21 19:01:32 -04001423/*
1424 * This mode timing computation functionality is ported over from
1425 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1426 */
1427/*
1428 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1429 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1430 * for PIO 5, which is a nonstandard extension and UDMA6, which
1431 * is currently supported only by Maxtor drives.
1432 */
1433
1434static const struct ata_timing ata_timing[] = {
1435
1436 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1437 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1438 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1439 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1440
1441 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1442 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1443 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1444
1445/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1446
1447 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1448 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1449 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1450
1451 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1452 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1453 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1454
1455/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1456 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1457 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1458
1459 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1460 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1461 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1462
1463/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1464
1465 { 0xFF }
1466};
1467
1468#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1469#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1470
1471static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1472{
1473 q->setup = EZ(t->setup * 1000, T);
1474 q->act8b = EZ(t->act8b * 1000, T);
1475 q->rec8b = EZ(t->rec8b * 1000, T);
1476 q->cyc8b = EZ(t->cyc8b * 1000, T);
1477 q->active = EZ(t->active * 1000, T);
1478 q->recover = EZ(t->recover * 1000, T);
1479 q->cycle = EZ(t->cycle * 1000, T);
1480 q->udma = EZ(t->udma * 1000, UT);
1481}
1482
1483void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1484 struct ata_timing *m, unsigned int what)
1485{
1486 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1487 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1488 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1489 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1490 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1491 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1492 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1493 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1494}
1495
1496static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1497{
1498 const struct ata_timing *t;
1499
1500 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001501 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001502 return NULL;
1503 return t;
1504}
1505
1506int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1507 struct ata_timing *t, int T, int UT)
1508{
1509 const struct ata_timing *s;
1510 struct ata_timing p;
1511
1512 /*
1513 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001514 */
Alan Cox452503f2005-10-21 19:01:32 -04001515
1516 if (!(s = ata_timing_find_mode(speed)))
1517 return -EINVAL;
1518
Albert Lee75b1f2f2005-11-16 17:06:18 +08001519 memcpy(t, s, sizeof(*s));
1520
Alan Cox452503f2005-10-21 19:01:32 -04001521 /*
1522 * If the drive is an EIDE drive, it can tell us it needs extended
1523 * PIO/MW_DMA cycle timing.
1524 */
1525
1526 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1527 memset(&p, 0, sizeof(p));
1528 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1529 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1530 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1531 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1532 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1533 }
1534 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1535 }
1536
1537 /*
1538 * Convert the timing to bus clock counts.
1539 */
1540
Albert Lee75b1f2f2005-11-16 17:06:18 +08001541 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001542
1543 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001544 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1545 * S.M.A.R.T * and some other commands. We have to ensure that the
1546 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001547 */
1548
1549 if (speed > XFER_PIO_4) {
1550 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1551 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1552 }
1553
1554 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001555 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001556 */
1557
1558 if (t->act8b + t->rec8b < t->cyc8b) {
1559 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1560 t->rec8b = t->cyc8b - t->act8b;
1561 }
1562
1563 if (t->active + t->recover < t->cycle) {
1564 t->active += (t->cycle - (t->active + t->recover)) / 2;
1565 t->recover = t->cycle - t->active;
1566 }
1567
1568 return 0;
1569}
1570
Jeff Garzik057ace52005-10-22 14:27:05 -04001571static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 unsigned int shift;
1573 u8 base;
1574} xfer_mode_classes[] = {
1575 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1576 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1577 { ATA_SHIFT_PIO, XFER_PIO_0 },
1578};
1579
Arjan van de Ven858119e2006-01-14 13:20:43 -08001580static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 int i;
1583
1584 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1585 if (xfer_mode_classes[i].shift == shift)
1586 return xfer_mode_classes[i].base;
1587
1588 return 0xff;
1589}
1590
1591static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1592{
1593 int ofs, idx;
1594 u8 base;
1595
1596 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1597 return;
1598
1599 if (dev->xfer_shift == ATA_SHIFT_PIO)
1600 dev->flags |= ATA_DFLAG_PIO;
1601
1602 ata_dev_set_xfermode(ap, dev);
1603
1604 base = base_from_shift(dev->xfer_shift);
1605 ofs = dev->xfer_mode - base;
1606 idx = ofs + dev->xfer_shift;
1607 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1608
1609 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1610 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1611
1612 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1613 ap->id, dev->devno, xfer_mode_str[idx]);
1614}
1615
1616static int ata_host_set_pio(struct ata_port *ap)
1617{
1618 unsigned int mask;
1619 int x, i;
1620 u8 base, xfer_mode;
1621
1622 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1623 x = fgb(mask);
1624 if (x < 0) {
1625 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1626 return -1;
1627 }
1628
1629 base = base_from_shift(ATA_SHIFT_PIO);
1630 xfer_mode = base + x;
1631
1632 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1633 (int)base, (int)xfer_mode, mask, x);
1634
1635 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1636 struct ata_device *dev = &ap->device[i];
1637 if (ata_dev_present(dev)) {
1638 dev->pio_mode = xfer_mode;
1639 dev->xfer_mode = xfer_mode;
1640 dev->xfer_shift = ATA_SHIFT_PIO;
1641 if (ap->ops->set_piomode)
1642 ap->ops->set_piomode(ap, dev);
1643 }
1644 }
1645
1646 return 0;
1647}
1648
1649static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1650 unsigned int xfer_shift)
1651{
1652 int i;
1653
1654 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1655 struct ata_device *dev = &ap->device[i];
1656 if (ata_dev_present(dev)) {
1657 dev->dma_mode = xfer_mode;
1658 dev->xfer_mode = xfer_mode;
1659 dev->xfer_shift = xfer_shift;
1660 if (ap->ops->set_dmamode)
1661 ap->ops->set_dmamode(ap, dev);
1662 }
1663 }
1664}
1665
1666/**
1667 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1668 * @ap: port on which timings will be programmed
1669 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001670 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1671 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001673 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 */
1675static void ata_set_mode(struct ata_port *ap)
1676{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001677 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 u8 xfer_mode;
1679 int rc;
1680
1681 /* step 1: always set host PIO timings */
1682 rc = ata_host_set_pio(ap);
1683 if (rc)
1684 goto err_out;
1685
1686 /* step 2: choose the best data xfer mode */
1687 xfer_mode = xfer_shift = 0;
1688 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1689 if (rc)
1690 goto err_out;
1691
1692 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1693 if (xfer_shift != ATA_SHIFT_PIO)
1694 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1695
1696 /* step 4: update devices' xfer mode */
1697 ata_dev_set_mode(ap, &ap->device[0]);
1698 ata_dev_set_mode(ap, &ap->device[1]);
1699
1700 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1701 return;
1702
1703 if (ap->ops->post_set_mode)
1704 ap->ops->post_set_mode(ap);
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return;
1707
1708err_out:
1709 ata_port_disable(ap);
1710}
1711
1712/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001713 * ata_tf_to_host - issue ATA taskfile to host controller
1714 * @ap: port to which command is being issued
1715 * @tf: ATA taskfile register set
1716 *
1717 * Issues ATA taskfile register set to ATA host controller,
1718 * with proper synchronization with interrupt handler and
1719 * other threads.
1720 *
1721 * LOCKING:
1722 * spin_lock_irqsave(host_set lock)
1723 */
1724
1725static inline void ata_tf_to_host(struct ata_port *ap,
1726 const struct ata_taskfile *tf)
1727{
1728 ap->ops->tf_load(ap, tf);
1729 ap->ops->exec_command(ap, tf);
1730}
1731
1732/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 * ata_busy_sleep - sleep until BSY clears, or timeout
1734 * @ap: port containing status register to be polled
1735 * @tmout_pat: impatience timeout
1736 * @tmout: overall timeout
1737 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001738 * Sleep until ATA Status register bit BSY clears,
1739 * or a timeout occurs.
1740 *
1741 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
1743
Tejun Heo6f8b9952006-01-24 17:05:21 +09001744unsigned int ata_busy_sleep (struct ata_port *ap,
1745 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 unsigned long timer_start, timeout;
1748 u8 status;
1749
1750 status = ata_busy_wait(ap, ATA_BUSY, 300);
1751 timer_start = jiffies;
1752 timeout = timer_start + tmout_pat;
1753 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1754 msleep(50);
1755 status = ata_busy_wait(ap, ATA_BUSY, 3);
1756 }
1757
1758 if (status & ATA_BUSY)
1759 printk(KERN_WARNING "ata%u is slow to respond, "
1760 "please be patient\n", ap->id);
1761
1762 timeout = timer_start + tmout;
1763 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1764 msleep(50);
1765 status = ata_chk_status(ap);
1766 }
1767
1768 if (status & ATA_BUSY) {
1769 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1770 ap->id, tmout / HZ);
1771 return 1;
1772 }
1773
1774 return 0;
1775}
1776
1777static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1778{
1779 struct ata_ioports *ioaddr = &ap->ioaddr;
1780 unsigned int dev0 = devmask & (1 << 0);
1781 unsigned int dev1 = devmask & (1 << 1);
1782 unsigned long timeout;
1783
1784 /* if device 0 was found in ata_devchk, wait for its
1785 * BSY bit to clear
1786 */
1787 if (dev0)
1788 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1789
1790 /* if device 1 was found in ata_devchk, wait for
1791 * register access, then wait for BSY to clear
1792 */
1793 timeout = jiffies + ATA_TMOUT_BOOT;
1794 while (dev1) {
1795 u8 nsect, lbal;
1796
1797 ap->ops->dev_select(ap, 1);
1798 if (ap->flags & ATA_FLAG_MMIO) {
1799 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1800 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1801 } else {
1802 nsect = inb(ioaddr->nsect_addr);
1803 lbal = inb(ioaddr->lbal_addr);
1804 }
1805 if ((nsect == 1) && (lbal == 1))
1806 break;
1807 if (time_after(jiffies, timeout)) {
1808 dev1 = 0;
1809 break;
1810 }
1811 msleep(50); /* give drive a breather */
1812 }
1813 if (dev1)
1814 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1815
1816 /* is all this really necessary? */
1817 ap->ops->dev_select(ap, 0);
1818 if (dev1)
1819 ap->ops->dev_select(ap, 1);
1820 if (dev0)
1821 ap->ops->dev_select(ap, 0);
1822}
1823
1824/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001825 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1826 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001828 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1829 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 *
1831 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001832 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001833 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 *
1835 */
1836
1837static unsigned int ata_bus_edd(struct ata_port *ap)
1838{
1839 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001840 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /* set up execute-device-diag (bus reset) taskfile */
1843 /* also, take interrupts to a known state (disabled) */
1844 DPRINTK("execute-device-diag\n");
1845 ata_tf_init(ap, &tf, 0);
1846 tf.ctl |= ATA_NIEN;
1847 tf.command = ATA_CMD_EDD;
1848 tf.protocol = ATA_PROT_NODATA;
1849
1850 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001851 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001853 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 /* spec says at least 2ms. but who knows with those
1856 * crazy ATAPI devices...
1857 */
1858 msleep(150);
1859
1860 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1861}
1862
1863static unsigned int ata_bus_softreset(struct ata_port *ap,
1864 unsigned int devmask)
1865{
1866 struct ata_ioports *ioaddr = &ap->ioaddr;
1867
1868 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1869
1870 /* software reset. causes dev0 to be selected */
1871 if (ap->flags & ATA_FLAG_MMIO) {
1872 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1873 udelay(20); /* FIXME: flush */
1874 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1875 udelay(20); /* FIXME: flush */
1876 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1877 } else {
1878 outb(ap->ctl, ioaddr->ctl_addr);
1879 udelay(10);
1880 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1881 udelay(10);
1882 outb(ap->ctl, ioaddr->ctl_addr);
1883 }
1884
1885 /* spec mandates ">= 2ms" before checking status.
1886 * We wait 150ms, because that was the magic delay used for
1887 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1888 * between when the ATA command register is written, and then
1889 * status is checked. Because waiting for "a while" before
1890 * checking status is fine, post SRST, we perform this magic
1891 * delay here as well.
1892 */
1893 msleep(150);
1894
1895 ata_bus_post_reset(ap, devmask);
1896
1897 return 0;
1898}
1899
1900/**
1901 * ata_bus_reset - reset host port and associated ATA channel
1902 * @ap: port to reset
1903 *
1904 * This is typically the first time we actually start issuing
1905 * commands to the ATA channel. We wait for BSY to clear, then
1906 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1907 * result. Determine what devices, if any, are on the channel
1908 * by looking at the device 0/1 error register. Look at the signature
1909 * stored in each device's taskfile registers, to determine if
1910 * the device is ATA or ATAPI.
1911 *
1912 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001913 * PCI/etc. bus probe sem.
1914 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 *
1916 * SIDE EFFECTS:
1917 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1918 */
1919
1920void ata_bus_reset(struct ata_port *ap)
1921{
1922 struct ata_ioports *ioaddr = &ap->ioaddr;
1923 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1924 u8 err;
1925 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1926
1927 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1928
1929 /* determine if device 0/1 are present */
1930 if (ap->flags & ATA_FLAG_SATA_RESET)
1931 dev0 = 1;
1932 else {
1933 dev0 = ata_devchk(ap, 0);
1934 if (slave_possible)
1935 dev1 = ata_devchk(ap, 1);
1936 }
1937
1938 if (dev0)
1939 devmask |= (1 << 0);
1940 if (dev1)
1941 devmask |= (1 << 1);
1942
1943 /* select device 0 again */
1944 ap->ops->dev_select(ap, 0);
1945
1946 /* issue bus reset */
1947 if (ap->flags & ATA_FLAG_SRST)
1948 rc = ata_bus_softreset(ap, devmask);
1949 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1950 /* set up device control */
1951 if (ap->flags & ATA_FLAG_MMIO)
1952 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1953 else
1954 outb(ap->ctl, ioaddr->ctl_addr);
1955 rc = ata_bus_edd(ap);
1956 }
1957
1958 if (rc)
1959 goto err_out;
1960
1961 /*
1962 * determine by signature whether we have ATA or ATAPI devices
1963 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001964 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09001966 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /* re-enable interrupts */
1969 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1970 ata_irq_on(ap);
1971
1972 /* is double-select really necessary? */
1973 if (ap->device[1].class != ATA_DEV_NONE)
1974 ap->ops->dev_select(ap, 1);
1975 if (ap->device[0].class != ATA_DEV_NONE)
1976 ap->ops->dev_select(ap, 0);
1977
1978 /* if no devices were detected, disable this port */
1979 if ((ap->device[0].class == ATA_DEV_NONE) &&
1980 (ap->device[1].class == ATA_DEV_NONE))
1981 goto err_out;
1982
1983 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1984 /* set up device control for ATA_FLAG_SATA_RESET */
1985 if (ap->flags & ATA_FLAG_MMIO)
1986 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1987 else
1988 outb(ap->ctl, ioaddr->ctl_addr);
1989 }
1990
1991 DPRINTK("EXIT\n");
1992 return;
1993
1994err_out:
1995 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1996 ap->ops->port_disable(ap);
1997
1998 DPRINTK("EXIT\n");
1999}
2000
Tejun Heo7a7921e2006-02-02 18:20:00 +09002001static int sata_phy_resume(struct ata_port *ap)
2002{
2003 unsigned long timeout = jiffies + (HZ * 5);
2004 u32 sstatus;
2005
2006 scr_write_flush(ap, SCR_CONTROL, 0x300);
2007
2008 /* Wait for phy to become ready, if necessary. */
2009 do {
2010 msleep(200);
2011 sstatus = scr_read(ap, SCR_STATUS);
2012 if ((sstatus & 0xf) != 1)
2013 return 0;
2014 } while (time_before(jiffies, timeout));
2015
2016 return -1;
2017}
2018
Tejun Heoc2bd5802006-01-24 17:05:22 +09002019/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002020 * ata_std_probeinit - initialize probing
2021 * @ap: port to be probed
2022 *
2023 * @ap is about to be probed. Initialize it. This function is
2024 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002025 *
2026 * NOTE!!! Do not use this function as probeinit if a low level
2027 * driver implements only hardreset. Just pass NULL as probeinit
2028 * in that case. Using this function is probably okay but doing
2029 * so makes reset sequence different from the original
2030 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002031 */
2032extern void ata_std_probeinit(struct ata_port *ap)
2033{
Tejun Heo3a397462006-02-10 23:58:48 +09002034 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002035 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002036 if (sata_dev_present(ap))
2037 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2038 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002039}
2040
2041/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002042 * ata_std_softreset - reset host port via ATA SRST
2043 * @ap: port to reset
2044 * @verbose: fail verbosely
2045 * @classes: resulting classes of attached devices
2046 *
2047 * Reset host port using ATA SRST. This function is to be used
2048 * as standard callback for ata_drive_*_reset() functions.
2049 *
2050 * LOCKING:
2051 * Kernel thread context (may sleep)
2052 *
2053 * RETURNS:
2054 * 0 on success, -errno otherwise.
2055 */
2056int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2057{
2058 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2059 unsigned int devmask = 0, err_mask;
2060 u8 err;
2061
2062 DPRINTK("ENTER\n");
2063
Tejun Heo3a397462006-02-10 23:58:48 +09002064 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2065 classes[0] = ATA_DEV_NONE;
2066 goto out;
2067 }
2068
Tejun Heoc2bd5802006-01-24 17:05:22 +09002069 /* determine if device 0/1 are present */
2070 if (ata_devchk(ap, 0))
2071 devmask |= (1 << 0);
2072 if (slave_possible && ata_devchk(ap, 1))
2073 devmask |= (1 << 1);
2074
Tejun Heoc2bd5802006-01-24 17:05:22 +09002075 /* select device 0 again */
2076 ap->ops->dev_select(ap, 0);
2077
2078 /* issue bus reset */
2079 DPRINTK("about to softreset, devmask=%x\n", devmask);
2080 err_mask = ata_bus_softreset(ap, devmask);
2081 if (err_mask) {
2082 if (verbose)
2083 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2084 ap->id, err_mask);
2085 else
2086 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2087 err_mask);
2088 return -EIO;
2089 }
2090
2091 /* determine by signature whether we have ATA or ATAPI devices */
2092 classes[0] = ata_dev_try_classify(ap, 0, &err);
2093 if (slave_possible && err != 0x81)
2094 classes[1] = ata_dev_try_classify(ap, 1, &err);
2095
Tejun Heo3a397462006-02-10 23:58:48 +09002096 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002097 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2098 return 0;
2099}
2100
2101/**
2102 * sata_std_hardreset - reset host port via SATA phy reset
2103 * @ap: port to reset
2104 * @verbose: fail verbosely
2105 * @class: resulting class of attached device
2106 *
2107 * SATA phy-reset host port using DET bits of SControl register.
2108 * This function is to be used as standard callback for
2109 * ata_drive_*_reset().
2110 *
2111 * LOCKING:
2112 * Kernel thread context (may sleep)
2113 *
2114 * RETURNS:
2115 * 0 on success, -errno otherwise.
2116 */
2117int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2118{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002119 DPRINTK("ENTER\n");
2120
2121 /* Issue phy wake/reset */
2122 scr_write_flush(ap, SCR_CONTROL, 0x301);
2123
2124 /*
2125 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2126 * 10.4.2 says at least 1 ms.
2127 */
2128 msleep(1);
2129
Tejun Heo7a7921e2006-02-02 18:20:00 +09002130 /* Bring phy back */
2131 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002132
Tejun Heoc2bd5802006-01-24 17:05:22 +09002133 /* TODO: phy layer with polling, timeouts, etc. */
2134 if (!sata_dev_present(ap)) {
2135 *class = ATA_DEV_NONE;
2136 DPRINTK("EXIT, link offline\n");
2137 return 0;
2138 }
2139
2140 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2141 if (verbose)
2142 printk(KERN_ERR "ata%u: COMRESET failed "
2143 "(device not ready)\n", ap->id);
2144 else
2145 DPRINTK("EXIT, device not ready\n");
2146 return -EIO;
2147 }
2148
Tejun Heo3a397462006-02-10 23:58:48 +09002149 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2150
Tejun Heoc2bd5802006-01-24 17:05:22 +09002151 *class = ata_dev_try_classify(ap, 0, NULL);
2152
2153 DPRINTK("EXIT, class=%u\n", *class);
2154 return 0;
2155}
2156
2157/**
2158 * ata_std_postreset - standard postreset callback
2159 * @ap: the target ata_port
2160 * @classes: classes of attached devices
2161 *
2162 * This function is invoked after a successful reset. Note that
2163 * the device might have been reset more than once using
2164 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002165 *
2166 * This function is to be used as standard callback for
2167 * ata_drive_*_reset().
2168 *
2169 * LOCKING:
2170 * Kernel thread context (may sleep)
2171 */
2172void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2173{
2174 DPRINTK("ENTER\n");
2175
Tejun Heo56497bd2006-02-15 15:01:42 +09002176 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002177 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2178 ap->cbl = ATA_CBL_SATA;
2179
2180 /* print link status */
2181 if (ap->cbl == ATA_CBL_SATA)
2182 sata_print_link_status(ap);
2183
Tejun Heo3a397462006-02-10 23:58:48 +09002184 /* re-enable interrupts */
2185 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2186 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002187
2188 /* is double-select really necessary? */
2189 if (classes[0] != ATA_DEV_NONE)
2190 ap->ops->dev_select(ap, 1);
2191 if (classes[1] != ATA_DEV_NONE)
2192 ap->ops->dev_select(ap, 0);
2193
Tejun Heo3a397462006-02-10 23:58:48 +09002194 /* bail out if no device is present */
2195 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2196 DPRINTK("EXIT, no device\n");
2197 return;
2198 }
2199
2200 /* set up device control */
2201 if (ap->ioaddr.ctl_addr) {
2202 if (ap->flags & ATA_FLAG_MMIO)
2203 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2204 else
2205 outb(ap->ctl, ap->ioaddr.ctl_addr);
2206 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002207
2208 DPRINTK("EXIT\n");
2209}
2210
2211/**
2212 * ata_std_probe_reset - standard probe reset method
2213 * @ap: prot to perform probe-reset
2214 * @classes: resulting classes of attached devices
2215 *
2216 * The stock off-the-shelf ->probe_reset method.
2217 *
2218 * LOCKING:
2219 * Kernel thread context (may sleep)
2220 *
2221 * RETURNS:
2222 * 0 on success, -errno otherwise.
2223 */
2224int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2225{
2226 ata_reset_fn_t hardreset;
2227
2228 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002229 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002230 hardreset = sata_std_hardreset;
2231
Tejun Heo8a19ac82006-02-02 18:20:00 +09002232 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002233 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002234 ata_std_postreset, classes);
2235}
2236
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002237static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2238 ata_postreset_fn_t postreset,
2239 unsigned int *classes)
2240{
2241 int i, rc;
2242
2243 for (i = 0; i < ATA_MAX_DEVICES; i++)
2244 classes[i] = ATA_DEV_UNKNOWN;
2245
2246 rc = reset(ap, 0, classes);
2247 if (rc)
2248 return rc;
2249
2250 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2251 * is complete and convert all ATA_DEV_UNKNOWN to
2252 * ATA_DEV_NONE.
2253 */
2254 for (i = 0; i < ATA_MAX_DEVICES; i++)
2255 if (classes[i] != ATA_DEV_UNKNOWN)
2256 break;
2257
2258 if (i < ATA_MAX_DEVICES)
2259 for (i = 0; i < ATA_MAX_DEVICES; i++)
2260 if (classes[i] == ATA_DEV_UNKNOWN)
2261 classes[i] = ATA_DEV_NONE;
2262
2263 if (postreset)
2264 postreset(ap, classes);
2265
2266 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2267}
2268
2269/**
2270 * ata_drive_probe_reset - Perform probe reset with given methods
2271 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002272 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002273 * @softreset: softreset method (can be NULL)
2274 * @hardreset: hardreset method (can be NULL)
2275 * @postreset: postreset method (can be NULL)
2276 * @classes: resulting classes of attached devices
2277 *
2278 * Reset the specified port and classify attached devices using
2279 * given methods. This function prefers softreset but tries all
2280 * possible reset sequences to reset and classify devices. This
2281 * function is intended to be used for constructing ->probe_reset
2282 * callback by low level drivers.
2283 *
2284 * Reset methods should follow the following rules.
2285 *
2286 * - Return 0 on sucess, -errno on failure.
2287 * - If classification is supported, fill classes[] with
2288 * recognized class codes.
2289 * - If classification is not supported, leave classes[] alone.
2290 * - If verbose is non-zero, print error message on failure;
2291 * otherwise, shut up.
2292 *
2293 * LOCKING:
2294 * Kernel thread context (may sleep)
2295 *
2296 * RETURNS:
2297 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2298 * if classification fails, and any error code from reset
2299 * methods.
2300 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002301int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002302 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2303 ata_postreset_fn_t postreset, unsigned int *classes)
2304{
2305 int rc = -EINVAL;
2306
Tejun Heo7944ea92006-02-02 18:20:00 +09002307 if (probeinit)
2308 probeinit(ap);
2309
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002310 if (softreset) {
2311 rc = do_probe_reset(ap, softreset, postreset, classes);
2312 if (rc == 0)
2313 return 0;
2314 }
2315
2316 if (!hardreset)
2317 return rc;
2318
2319 rc = do_probe_reset(ap, hardreset, postreset, classes);
2320 if (rc == 0 || rc != -ENODEV)
2321 return rc;
2322
2323 if (softreset)
2324 rc = do_probe_reset(ap, softreset, postreset, classes);
2325
2326 return rc;
2327}
2328
Jeff Garzik057ace52005-10-22 14:27:05 -04002329static void ata_pr_blacklisted(const struct ata_port *ap,
2330 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
2332 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2333 ap->id, dev->devno);
2334}
2335
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002336static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 "WDC AC11000H",
2338 "WDC AC22100H",
2339 "WDC AC32500H",
2340 "WDC AC33100H",
2341 "WDC AC31600H",
2342 "WDC AC32100H",
2343 "WDC AC23200L",
2344 "Compaq CRD-8241B",
2345 "CRD-8400B",
2346 "CRD-8480B",
2347 "CRD-8482B",
2348 "CRD-84",
2349 "SanDisk SDP3B",
2350 "SanDisk SDP3B-64",
2351 "SANYO CD-ROM CRD",
2352 "HITACHI CDR-8",
2353 "HITACHI CDR-8335",
2354 "HITACHI CDR-8435",
2355 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002356 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 "CD-532E-A",
2358 "E-IDE CD-ROM CR-840",
2359 "CD-ROM Drive/F5A",
2360 "WPI CDD-820",
2361 "SAMSUNG CD-ROM SC-148C",
2362 "SAMSUNG CD-ROM SC",
2363 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2365 "_NEC DV5800A",
2366};
2367
Jeff Garzik057ace52005-10-22 14:27:05 -04002368static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Tejun Heo2e026712006-02-12 22:47:04 +09002370 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 int i;
2372
Tejun Heo6a62a042006-02-13 10:02:46 +09002373 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002376 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return 1;
2378
2379 return 0;
2380}
2381
Jeff Garzik057ace52005-10-22 14:27:05 -04002382static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
Jeff Garzik057ace52005-10-22 14:27:05 -04002384 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 unsigned int mask;
2386
2387 master = &ap->device[0];
2388 slave = &ap->device[1];
2389
Tejun Heoa4631472006-02-11 19:11:13 +09002390 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 if (shift == ATA_SHIFT_UDMA) {
2393 mask = ap->udma_mask;
2394 if (ata_dev_present(master)) {
2395 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002396 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 mask = 0;
2398 ata_pr_blacklisted(ap, master);
2399 }
2400 }
2401 if (ata_dev_present(slave)) {
2402 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002403 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 mask = 0;
2405 ata_pr_blacklisted(ap, slave);
2406 }
2407 }
2408 }
2409 else if (shift == ATA_SHIFT_MWDMA) {
2410 mask = ap->mwdma_mask;
2411 if (ata_dev_present(master)) {
2412 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002413 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 mask = 0;
2415 ata_pr_blacklisted(ap, master);
2416 }
2417 }
2418 if (ata_dev_present(slave)) {
2419 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002420 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 mask = 0;
2422 ata_pr_blacklisted(ap, slave);
2423 }
2424 }
2425 }
2426 else if (shift == ATA_SHIFT_PIO) {
2427 mask = ap->pio_mask;
2428 if (ata_dev_present(master)) {
2429 /* spec doesn't return explicit support for
2430 * PIO0-2, so we fake it
2431 */
2432 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2433 tmp_mode <<= 3;
2434 tmp_mode |= 0x7;
2435 mask &= tmp_mode;
2436 }
2437 if (ata_dev_present(slave)) {
2438 /* spec doesn't return explicit support for
2439 * PIO0-2, so we fake it
2440 */
2441 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2442 tmp_mode <<= 3;
2443 tmp_mode |= 0x7;
2444 mask &= tmp_mode;
2445 }
2446 }
2447 else {
2448 mask = 0xffffffff; /* shut up compiler warning */
2449 BUG();
2450 }
2451
2452 return mask;
2453}
2454
2455/* find greatest bit */
2456static int fgb(u32 bitmap)
2457{
2458 unsigned int i;
2459 int x = -1;
2460
2461 for (i = 0; i < 32; i++)
2462 if (bitmap & (1 << i))
2463 x = i;
2464
2465 return x;
2466}
2467
2468/**
2469 * ata_choose_xfer_mode - attempt to find best transfer mode
2470 * @ap: Port for which an xfer mode will be selected
2471 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2472 * @xfer_shift_out: (output) bit shift that selects this mode
2473 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002474 * Based on host and device capabilities, determine the
2475 * maximum transfer mode that is amenable to all.
2476 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002478 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 *
2480 * RETURNS:
2481 * Zero on success, negative on error.
2482 */
2483
Jeff Garzik057ace52005-10-22 14:27:05 -04002484static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 u8 *xfer_mode_out,
2486 unsigned int *xfer_shift_out)
2487{
2488 unsigned int mask, shift;
2489 int x, i;
2490
2491 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2492 shift = xfer_mode_classes[i].shift;
2493 mask = ata_get_mode_mask(ap, shift);
2494
2495 x = fgb(mask);
2496 if (x >= 0) {
2497 *xfer_mode_out = xfer_mode_classes[i].base + x;
2498 *xfer_shift_out = shift;
2499 return 0;
2500 }
2501 }
2502
2503 return -1;
2504}
2505
2506/**
2507 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2508 * @ap: Port associated with device @dev
2509 * @dev: Device to which command will be sent
2510 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002511 * Issue SET FEATURES - XFER MODE command to device @dev
2512 * on port @ap.
2513 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002515 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 */
2517
2518static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2519{
Tejun Heoa0123702005-12-13 14:49:31 +09002520 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 /* set up set-features taskfile */
2523 DPRINTK("set features - xfer mode\n");
2524
Tejun Heoa0123702005-12-13 14:49:31 +09002525 ata_tf_init(ap, &tf, dev->devno);
2526 tf.command = ATA_CMD_SET_FEATURES;
2527 tf.feature = SETFEATURES_XFER;
2528 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2529 tf.protocol = ATA_PROT_NODATA;
2530 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Tejun Heoa0123702005-12-13 14:49:31 +09002532 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2533 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2534 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 DPRINTK("EXIT\n");
2539}
2540
2541/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002542 * ata_dev_init_params - Issue INIT DEV PARAMS command
2543 * @ap: Port associated with device @dev
2544 * @dev: Device to which command will be sent
2545 *
2546 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002547 * Kernel thread context (may sleep)
2548 *
2549 * RETURNS:
2550 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04002551 */
2552
Tejun Heo6aff8f12006-02-15 18:24:09 +09002553static unsigned int ata_dev_init_params(struct ata_port *ap,
2554 struct ata_device *dev)
Albert Lee8bf62ec2005-05-12 15:29:42 -04002555{
Tejun Heoa0123702005-12-13 14:49:31 +09002556 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002557 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002558 u16 sectors = dev->id[6];
2559 u16 heads = dev->id[3];
2560
2561 /* Number of sectors per track 1-255. Number of heads 1-16 */
2562 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002563 return 0;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002564
2565 /* set up init dev params taskfile */
2566 DPRINTK("init dev params \n");
2567
Tejun Heoa0123702005-12-13 14:49:31 +09002568 ata_tf_init(ap, &tf, dev->devno);
2569 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2570 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2571 tf.protocol = ATA_PROT_NODATA;
2572 tf.nsect = sectors;
2573 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002574
Tejun Heo6aff8f12006-02-15 18:24:09 +09002575 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002576
Tejun Heo6aff8f12006-02-15 18:24:09 +09002577 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2578 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002579}
2580
2581/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002582 * ata_sg_clean - Unmap DMA memory associated with command
2583 * @qc: Command containing DMA memory to be released
2584 *
2585 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 *
2587 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002588 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 */
2590
2591static void ata_sg_clean(struct ata_queued_cmd *qc)
2592{
2593 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002594 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002596 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Tejun Heoa4631472006-02-11 19:11:13 +09002598 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2599 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002602 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002604 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002606 /* if we padded the buffer out to 32-bit bound, and data
2607 * xfer direction is from-device, we must copy from the
2608 * pad buffer back into the supplied buffer
2609 */
2610 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2611 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2612
2613 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002614 if (qc->n_elem)
2615 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002616 /* restore last sg */
2617 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2618 if (pad_buf) {
2619 struct scatterlist *psg = &qc->pad_sgent;
2620 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2621 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002622 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002623 }
2624 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002625 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002626 dma_unmap_single(ap->host_set->dev,
2627 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2628 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002629 /* restore sg */
2630 sg->length += qc->pad_len;
2631 if (pad_buf)
2632 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2633 pad_buf, qc->pad_len);
2634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002637 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
2640/**
2641 * ata_fill_sg - Fill PCI IDE PRD table
2642 * @qc: Metadata associated with taskfile to be transferred
2643 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002644 * Fill PCI IDE PRD (scatter-gather) table with segments
2645 * associated with the current disk command.
2646 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002648 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 *
2650 */
2651static void ata_fill_sg(struct ata_queued_cmd *qc)
2652{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002654 struct scatterlist *sg;
2655 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Tejun Heoa4631472006-02-11 19:11:13 +09002657 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002658 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002661 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 u32 addr, offset;
2663 u32 sg_len, len;
2664
2665 /* determine if physical DMA addr spans 64K boundary.
2666 * Note h/w doesn't support 64-bit, so we unconditionally
2667 * truncate dma_addr_t to u32.
2668 */
2669 addr = (u32) sg_dma_address(sg);
2670 sg_len = sg_dma_len(sg);
2671
2672 while (sg_len) {
2673 offset = addr & 0xffff;
2674 len = sg_len;
2675 if ((offset + sg_len) > 0x10000)
2676 len = 0x10000 - offset;
2677
2678 ap->prd[idx].addr = cpu_to_le32(addr);
2679 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2680 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2681
2682 idx++;
2683 sg_len -= len;
2684 addr += len;
2685 }
2686 }
2687
2688 if (idx)
2689 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2690}
2691/**
2692 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2693 * @qc: Metadata associated with taskfile to check
2694 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002695 * Allow low-level driver to filter ATA PACKET commands, returning
2696 * a status indicating whether or not it is OK to use DMA for the
2697 * supplied PACKET command.
2698 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002700 * spin_lock_irqsave(host_set lock)
2701 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 * RETURNS: 0 when ATAPI DMA can be used
2703 * nonzero otherwise
2704 */
2705int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2706{
2707 struct ata_port *ap = qc->ap;
2708 int rc = 0; /* Assume ATAPI DMA is OK by default */
2709
2710 if (ap->ops->check_atapi_dma)
2711 rc = ap->ops->check_atapi_dma(qc);
2712
2713 return rc;
2714}
2715/**
2716 * ata_qc_prep - Prepare taskfile for submission
2717 * @qc: Metadata associated with taskfile to be prepared
2718 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002719 * Prepare ATA taskfile for submission.
2720 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 * LOCKING:
2722 * spin_lock_irqsave(host_set lock)
2723 */
2724void ata_qc_prep(struct ata_queued_cmd *qc)
2725{
2726 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2727 return;
2728
2729 ata_fill_sg(qc);
2730}
2731
Jeff Garzik0cba6322005-05-30 19:49:12 -04002732/**
2733 * ata_sg_init_one - Associate command with memory buffer
2734 * @qc: Command to be associated
2735 * @buf: Memory buffer
2736 * @buflen: Length of memory buffer, in bytes.
2737 *
2738 * Initialize the data-related elements of queued_cmd @qc
2739 * to point to a single memory buffer, @buf of byte length @buflen.
2740 *
2741 * LOCKING:
2742 * spin_lock_irqsave(host_set lock)
2743 */
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2746{
2747 struct scatterlist *sg;
2748
2749 qc->flags |= ATA_QCFLAG_SINGLE;
2750
2751 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002752 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002754 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 qc->buf_virt = buf;
2756
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002757 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002758 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
Jeff Garzik0cba6322005-05-30 19:49:12 -04002761/**
2762 * ata_sg_init - Associate command with scatter-gather table.
2763 * @qc: Command to be associated
2764 * @sg: Scatter-gather table.
2765 * @n_elem: Number of elements in s/g table.
2766 *
2767 * Initialize the data-related elements of queued_cmd @qc
2768 * to point to a scatter-gather table @sg, containing @n_elem
2769 * elements.
2770 *
2771 * LOCKING:
2772 * spin_lock_irqsave(host_set lock)
2773 */
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2776 unsigned int n_elem)
2777{
2778 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002779 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002781 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782}
2783
2784/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002785 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2786 * @qc: Command with memory buffer to be mapped.
2787 *
2788 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 *
2790 * LOCKING:
2791 * spin_lock_irqsave(host_set lock)
2792 *
2793 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002794 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 */
2796
2797static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2798{
2799 struct ata_port *ap = qc->ap;
2800 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002801 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002803 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002805 /* we must lengthen transfers to end on a 32-bit boundary */
2806 qc->pad_len = sg->length & 3;
2807 if (qc->pad_len) {
2808 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2809 struct scatterlist *psg = &qc->pad_sgent;
2810
Tejun Heoa4631472006-02-11 19:11:13 +09002811 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002812
2813 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2814
2815 if (qc->tf.flags & ATA_TFLAG_WRITE)
2816 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2817 qc->pad_len);
2818
2819 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2820 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2821 /* trim sg */
2822 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002823 if (sg->length == 0)
2824 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002825
2826 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2827 sg->length, qc->pad_len);
2828 }
2829
Tejun Heo2e242fa2006-02-20 23:48:38 +09002830 if (trim_sg) {
2831 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05002832 goto skip_map;
2833 }
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002836 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002837 if (dma_mapping_error(dma_address)) {
2838 /* restore sg */
2839 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002844 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Tejun Heo2e242fa2006-02-20 23:48:38 +09002846skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2848 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2849
2850 return 0;
2851}
2852
2853/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002854 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2855 * @qc: Command with scatter-gather table to be mapped.
2856 *
2857 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 *
2859 * LOCKING:
2860 * spin_lock_irqsave(host_set lock)
2861 *
2862 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002863 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 *
2865 */
2866
2867static int ata_sg_setup(struct ata_queued_cmd *qc)
2868{
2869 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002870 struct scatterlist *sg = qc->__sg;
2871 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002872 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09002875 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002877 /* we must lengthen transfers to end on a 32-bit boundary */
2878 qc->pad_len = lsg->length & 3;
2879 if (qc->pad_len) {
2880 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2881 struct scatterlist *psg = &qc->pad_sgent;
2882 unsigned int offset;
2883
Tejun Heoa4631472006-02-11 19:11:13 +09002884 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002885
2886 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2887
2888 /*
2889 * psg->page/offset are used to copy to-be-written
2890 * data in this function or read data in ata_sg_clean.
2891 */
2892 offset = lsg->offset + lsg->length - qc->pad_len;
2893 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2894 psg->offset = offset_in_page(offset);
2895
2896 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2897 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2898 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002899 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002900 }
2901
2902 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2903 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2904 /* trim last sg */
2905 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002906 if (lsg->length == 0)
2907 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002908
2909 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2910 qc->n_elem - 1, lsg->length, qc->pad_len);
2911 }
2912
Jeff Garzike1410f22005-11-14 14:06:26 -05002913 pre_n_elem = qc->n_elem;
2914 if (trim_sg && pre_n_elem)
2915 pre_n_elem--;
2916
2917 if (!pre_n_elem) {
2918 n_elem = 0;
2919 goto skip_map;
2920 }
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002923 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002924 if (n_elem < 1) {
2925 /* restore last sg */
2926 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 DPRINTK("%d sg elements mapped\n", n_elem);
2931
Jeff Garzike1410f22005-11-14 14:06:26 -05002932skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 qc->n_elem = n_elem;
2934
2935 return 0;
2936}
2937
2938/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002939 * ata_poll_qc_complete - turn irq back on and finish qc
2940 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002941 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002942 *
2943 * LOCKING:
2944 * None. (grabs host lock)
2945 */
2946
Albert Leea22e2eb2005-12-05 15:38:02 +08002947void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002948{
2949 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002950 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002951
Jeff Garzikb8f61532005-08-25 22:01:20 -04002952 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002953 ap->flags &= ~ATA_FLAG_NOINTR;
2954 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002955 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002956 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002957}
2958
2959/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002960 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002961 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 *
2963 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002964 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 *
2966 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002967 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 */
2969
2970static unsigned long ata_pio_poll(struct ata_port *ap)
2971{
Albert Leec14b8332005-12-05 15:36:08 +08002972 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002974 unsigned int poll_state = HSM_ST_UNKNOWN;
2975 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Albert Leec14b8332005-12-05 15:36:08 +08002977 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09002978 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08002979
Albert Lee14be71f2005-09-27 17:36:35 +08002980 switch (ap->hsm_task_state) {
2981 case HSM_ST:
2982 case HSM_ST_POLL:
2983 poll_state = HSM_ST_POLL;
2984 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002986 case HSM_ST_LAST:
2987 case HSM_ST_LAST_POLL:
2988 poll_state = HSM_ST_LAST_POLL;
2989 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 break;
2991 default:
2992 BUG();
2993 break;
2994 }
2995
2996 status = ata_chk_status(ap);
2997 if (status & ATA_BUSY) {
2998 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002999 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003000 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return 0;
3002 }
Albert Lee14be71f2005-09-27 17:36:35 +08003003 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return ATA_SHORT_PAUSE;
3005 }
3006
Albert Lee14be71f2005-09-27 17:36:35 +08003007 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return 0;
3009}
3010
3011/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003012 * ata_pio_complete - check if drive is busy or idle
3013 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 *
3015 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003016 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003017 *
3018 * RETURNS:
3019 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 */
3021
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003022static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 struct ata_queued_cmd *qc;
3025 u8 drv_stat;
3026
3027 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003028 * This is purely heuristic. This is a fast path. Sometimes when
3029 * we enter, BSY will be cleared in a chk-status or two. If not,
3030 * the drive is probably seeking or something. Snooze for a couple
3031 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003032 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 */
Albert Leefe79e682005-12-06 11:34:59 +08003034 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3035 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003037 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3038 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003039 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043 }
3044
Albert Leec14b8332005-12-05 15:36:08 +08003045 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003046 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 drv_stat = ata_wait_idle(ap);
3049 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003050 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003051 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
3054
Albert Lee14be71f2005-09-27 17:36:35 +08003055 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Tejun Heoa4631472006-02-11 19:11:13 +09003057 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003058 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003059
3060 /* another command may start at this point */
3061
3062 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Edward Falk0baab862005-06-02 18:17:13 -04003065
3066/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003067 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003068 * @buf: Buffer to swap
3069 * @buf_words: Number of 16-bit words in buffer.
3070 *
3071 * Swap halves of 16-bit words if needed to convert from
3072 * little-endian byte order to native cpu byte order, or
3073 * vice-versa.
3074 *
3075 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003076 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078void swap_buf_le16(u16 *buf, unsigned int buf_words)
3079{
3080#ifdef __BIG_ENDIAN
3081 unsigned int i;
3082
3083 for (i = 0; i < buf_words; i++)
3084 buf[i] = le16_to_cpu(buf[i]);
3085#endif /* __BIG_ENDIAN */
3086}
3087
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003088/**
3089 * ata_mmio_data_xfer - Transfer data by MMIO
3090 * @ap: port to read/write
3091 * @buf: data buffer
3092 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003093 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003094 *
3095 * Transfer data from/to the device data register by MMIO.
3096 *
3097 * LOCKING:
3098 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003099 */
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3102 unsigned int buflen, int write_data)
3103{
3104 unsigned int i;
3105 unsigned int words = buflen >> 1;
3106 u16 *buf16 = (u16 *) buf;
3107 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3108
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003109 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (write_data) {
3111 for (i = 0; i < words; i++)
3112 writew(le16_to_cpu(buf16[i]), mmio);
3113 } else {
3114 for (i = 0; i < words; i++)
3115 buf16[i] = cpu_to_le16(readw(mmio));
3116 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003117
3118 /* Transfer trailing 1 byte, if any. */
3119 if (unlikely(buflen & 0x01)) {
3120 u16 align_buf[1] = { 0 };
3121 unsigned char *trailing_buf = buf + buflen - 1;
3122
3123 if (write_data) {
3124 memcpy(align_buf, trailing_buf, 1);
3125 writew(le16_to_cpu(align_buf[0]), mmio);
3126 } else {
3127 align_buf[0] = cpu_to_le16(readw(mmio));
3128 memcpy(trailing_buf, align_buf, 1);
3129 }
3130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003133/**
3134 * ata_pio_data_xfer - Transfer data by PIO
3135 * @ap: port to read/write
3136 * @buf: data buffer
3137 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003138 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003139 *
3140 * Transfer data from/to the device data register by PIO.
3141 *
3142 * LOCKING:
3143 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003144 */
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3147 unsigned int buflen, int write_data)
3148{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003149 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003151 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003153 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003155 insw(ap->ioaddr.data_addr, buf, words);
3156
3157 /* Transfer trailing 1 byte, if any. */
3158 if (unlikely(buflen & 0x01)) {
3159 u16 align_buf[1] = { 0 };
3160 unsigned char *trailing_buf = buf + buflen - 1;
3161
3162 if (write_data) {
3163 memcpy(align_buf, trailing_buf, 1);
3164 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3165 } else {
3166 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3167 memcpy(trailing_buf, align_buf, 1);
3168 }
3169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003172/**
3173 * ata_data_xfer - Transfer data from/to the data register.
3174 * @ap: port to read/write
3175 * @buf: data buffer
3176 * @buflen: buffer length
3177 * @do_write: read/write
3178 *
3179 * Transfer data from/to the device data register.
3180 *
3181 * LOCKING:
3182 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003183 */
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3186 unsigned int buflen, int do_write)
3187{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003188 /* Make the crap hardware pay the costs not the good stuff */
3189 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3190 unsigned long flags;
3191 local_irq_save(flags);
3192 if (ap->flags & ATA_FLAG_MMIO)
3193 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3194 else
3195 ata_pio_data_xfer(ap, buf, buflen, do_write);
3196 local_irq_restore(flags);
3197 } else {
3198 if (ap->flags & ATA_FLAG_MMIO)
3199 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3200 else
3201 ata_pio_data_xfer(ap, buf, buflen, do_write);
3202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003205/**
3206 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3207 * @qc: Command on going
3208 *
3209 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3210 *
3211 * LOCKING:
3212 * Inherited from caller.
3213 */
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215static void ata_pio_sector(struct ata_queued_cmd *qc)
3216{
3217 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003218 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 struct ata_port *ap = qc->ap;
3220 struct page *page;
3221 unsigned int offset;
3222 unsigned char *buf;
3223
3224 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003225 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
3227 page = sg[qc->cursg].page;
3228 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3229
3230 /* get the current page and offset */
3231 page = nth_page(page, (offset >> PAGE_SHIFT));
3232 offset %= PAGE_SIZE;
3233
3234 buf = kmap(page) + offset;
3235
3236 qc->cursect++;
3237 qc->cursg_ofs++;
3238
Albert Lee32529e02005-05-26 03:49:42 -04003239 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 qc->cursg++;
3241 qc->cursg_ofs = 0;
3242 }
3243
3244 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3245
3246 /* do the actual data transfer */
3247 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3248 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3249
3250 kunmap(page);
3251}
3252
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003253/**
3254 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3255 * @qc: Command on going
3256 * @bytes: number of bytes
3257 *
3258 * Transfer Transfer data from/to the ATAPI device.
3259 *
3260 * LOCKING:
3261 * Inherited from caller.
3262 *
3263 */
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3266{
3267 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003268 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 struct ata_port *ap = qc->ap;
3270 struct page *page;
3271 unsigned char *buf;
3272 unsigned int offset, count;
3273
Albert Lee563a6e12005-08-12 14:17:50 +08003274 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003275 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003278 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003279 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003280 * The end of qc->sg is reached and the device expects
3281 * more data to transfer. In order not to overrun qc->sg
3282 * and fulfill length specified in the byte count register,
3283 * - for read case, discard trailing data from the device
3284 * - for write case, padding zero data to the device
3285 */
3286 u16 pad_buf[1] = { 0 };
3287 unsigned int words = bytes >> 1;
3288 unsigned int i;
3289
3290 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003291 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003292 ap->id, bytes);
3293
3294 for (i = 0; i < words; i++)
3295 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3296
Albert Lee14be71f2005-09-27 17:36:35 +08003297 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003298 return;
3299 }
3300
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003301 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 page = sg->page;
3304 offset = sg->offset + qc->cursg_ofs;
3305
3306 /* get the current page and offset */
3307 page = nth_page(page, (offset >> PAGE_SHIFT));
3308 offset %= PAGE_SIZE;
3309
Albert Lee6952df02005-06-06 15:56:03 +08003310 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003311 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
3313 /* don't cross page boundaries */
3314 count = min(count, (unsigned int)PAGE_SIZE - offset);
3315
3316 buf = kmap(page) + offset;
3317
3318 bytes -= count;
3319 qc->curbytes += count;
3320 qc->cursg_ofs += count;
3321
Albert Lee32529e02005-05-26 03:49:42 -04003322 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 qc->cursg++;
3324 qc->cursg_ofs = 0;
3325 }
3326
3327 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3328
3329 /* do the actual data transfer */
3330 ata_data_xfer(ap, buf, count, do_write);
3331
3332 kunmap(page);
3333
Albert Lee563a6e12005-08-12 14:17:50 +08003334 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336}
3337
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003338/**
3339 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3340 * @qc: Command on going
3341 *
3342 * Transfer Transfer data from/to the ATAPI device.
3343 *
3344 * LOCKING:
3345 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003346 */
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3349{
3350 struct ata_port *ap = qc->ap;
3351 struct ata_device *dev = qc->dev;
3352 unsigned int ireason, bc_lo, bc_hi, bytes;
3353 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3354
3355 ap->ops->tf_read(ap, &qc->tf);
3356 ireason = qc->tf.nsect;
3357 bc_lo = qc->tf.lbam;
3358 bc_hi = qc->tf.lbah;
3359 bytes = (bc_hi << 8) | bc_lo;
3360
3361 /* shall be cleared to zero, indicating xfer of data */
3362 if (ireason & (1 << 0))
3363 goto err_out;
3364
3365 /* make sure transfer direction matches expected */
3366 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3367 if (do_write != i_write)
3368 goto err_out;
3369
3370 __atapi_pio_bytes(qc, bytes);
3371
3372 return;
3373
3374err_out:
3375 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3376 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003377 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003378 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379}
3380
3381/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003382 * ata_pio_block - start PIO on a block
3383 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 *
3385 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003386 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 */
3388
3389static void ata_pio_block(struct ata_port *ap)
3390{
3391 struct ata_queued_cmd *qc;
3392 u8 status;
3393
3394 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003395 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 * Sometimes when we enter, BSY will be cleared in
3397 * a chk-status or two. If not, the drive is probably seeking
3398 * or something. Snooze for a couple msecs, then
3399 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003400 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 */
3402 status = ata_busy_wait(ap, ATA_BUSY, 5);
3403 if (status & ATA_BUSY) {
3404 msleep(2);
3405 status = ata_busy_wait(ap, ATA_BUSY, 10);
3406 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003407 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3409 return;
3410 }
3411 }
3412
3413 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003414 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Albert Leefe79e682005-12-06 11:34:59 +08003416 /* check error */
3417 if (status & (ATA_ERR | ATA_DF)) {
3418 qc->err_mask |= AC_ERR_DEV;
3419 ap->hsm_task_state = HSM_ST_ERR;
3420 return;
3421 }
3422
3423 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003425 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003427 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return;
3429 }
3430
3431 atapi_pio_bytes(qc);
3432 } else {
3433 /* handle BSY=0, DRQ=0 as error */
3434 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003435 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003436 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 return;
3438 }
3439
3440 ata_pio_sector(qc);
3441 }
3442}
3443
3444static void ata_pio_error(struct ata_port *ap)
3445{
3446 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003449 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Albert Lee0565c262006-02-13 18:55:25 +08003451 if (qc->tf.command != ATA_CMD_PACKET)
3452 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3453
Albert Lee1c848982005-12-05 15:40:15 +08003454 /* make sure qc->err_mask is available to
3455 * know what's wrong and recover
3456 */
Tejun Heoa4631472006-02-11 19:11:13 +09003457 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003458
Albert Lee14be71f2005-09-27 17:36:35 +08003459 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Albert Leea22e2eb2005-12-05 15:38:02 +08003461 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
3464static void ata_pio_task(void *_data)
3465{
3466 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003467 unsigned long timeout;
3468 int qc_completed;
3469
3470fsm_start:
3471 timeout = 0;
3472 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Albert Lee14be71f2005-09-27 17:36:35 +08003474 switch (ap->hsm_task_state) {
3475 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 return;
3477
Albert Lee14be71f2005-09-27 17:36:35 +08003478 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 ata_pio_block(ap);
3480 break;
3481
Albert Lee14be71f2005-09-27 17:36:35 +08003482 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003483 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 break;
3485
Albert Lee14be71f2005-09-27 17:36:35 +08003486 case HSM_ST_POLL:
3487 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 timeout = ata_pio_poll(ap);
3489 break;
3490
Albert Lee14be71f2005-09-27 17:36:35 +08003491 case HSM_ST_TMOUT:
3492 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 ata_pio_error(ap);
3494 return;
3495 }
3496
3497 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003498 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003499 else if (!qc_completed)
3500 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501}
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503/**
3504 * ata_qc_timeout - Handle timeout of queued command
3505 * @qc: Command that timed out
3506 *
3507 * Some part of the kernel (currently, only the SCSI layer)
3508 * has noticed that the active command on port @ap has not
3509 * completed after a specified length of time. Handle this
3510 * condition by disabling DMA (if necessary) and completing
3511 * transactions, with error if necessary.
3512 *
3513 * This also handles the case of the "lost interrupt", where
3514 * for some reason (possibly hardware bug, possibly driver bug)
3515 * an interrupt was not delivered to the driver, even though the
3516 * transaction completed successfully.
3517 *
3518 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003519 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 */
3521
3522static void ata_qc_timeout(struct ata_queued_cmd *qc)
3523{
3524 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003525 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003527 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
3529 DPRINTK("ENTER\n");
3530
Tejun Heoc18d06f2006-02-02 00:56:10 +09003531 ata_flush_pio_tasks(ap);
3532 ap->hsm_task_state = HSM_ST_IDLE;
3533
Jeff Garzikb8f61532005-08-25 22:01:20 -04003534 spin_lock_irqsave(&host_set->lock, flags);
3535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 switch (qc->tf.protocol) {
3537
3538 case ATA_PROT_DMA:
3539 case ATA_PROT_ATAPI_DMA:
3540 host_stat = ap->ops->bmdma_status(ap);
3541
3542 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003543 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 /* fall through */
3546
3547 default:
3548 ata_altstatus(ap);
3549 drv_stat = ata_chk_status(ap);
3550
3551 /* ack bmdma irq events */
3552 ap->ops->irq_clear(ap);
3553
3554 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3555 ap->id, qc->tf.command, drv_stat, host_stat);
3556
3557 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003558 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 break;
3560 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003561
3562 spin_unlock_irqrestore(&host_set->lock, flags);
3563
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003564 ata_eh_qc_complete(qc);
3565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 DPRINTK("EXIT\n");
3567}
3568
3569/**
3570 * ata_eng_timeout - Handle timeout of queued command
3571 * @ap: Port on which timed-out command is active
3572 *
3573 * Some part of the kernel (currently, only the SCSI layer)
3574 * has noticed that the active command on port @ap has not
3575 * completed after a specified length of time. Handle this
3576 * condition by disabling DMA (if necessary) and completing
3577 * transactions, with error if necessary.
3578 *
3579 * This also handles the case of the "lost interrupt", where
3580 * for some reason (possibly hardware bug, possibly driver bug)
3581 * an interrupt was not delivered to the driver, even though the
3582 * transaction completed successfully.
3583 *
3584 * LOCKING:
3585 * Inherited from SCSI layer (none, can sleep)
3586 */
3587
3588void ata_eng_timeout(struct ata_port *ap)
3589{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 DPRINTK("ENTER\n");
3591
Tejun Heof6379022006-02-10 15:10:48 +09003592 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 DPRINTK("EXIT\n");
3595}
3596
3597/**
3598 * ata_qc_new - Request an available ATA command, for queueing
3599 * @ap: Port associated with device @dev
3600 * @dev: Device from whom we request an available command structure
3601 *
3602 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003603 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 */
3605
3606static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3607{
3608 struct ata_queued_cmd *qc = NULL;
3609 unsigned int i;
3610
3611 for (i = 0; i < ATA_MAX_QUEUE; i++)
3612 if (!test_and_set_bit(i, &ap->qactive)) {
3613 qc = ata_qc_from_tag(ap, i);
3614 break;
3615 }
3616
3617 if (qc)
3618 qc->tag = i;
3619
3620 return qc;
3621}
3622
3623/**
3624 * ata_qc_new_init - Request an available ATA command, and initialize it
3625 * @ap: Port associated with device @dev
3626 * @dev: Device from whom we request an available command structure
3627 *
3628 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003629 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 */
3631
3632struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3633 struct ata_device *dev)
3634{
3635 struct ata_queued_cmd *qc;
3636
3637 qc = ata_qc_new(ap);
3638 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 qc->scsicmd = NULL;
3640 qc->ap = ap;
3641 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003643 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
3645
3646 return qc;
3647}
3648
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649/**
3650 * ata_qc_free - free unused ata_queued_cmd
3651 * @qc: Command to complete
3652 *
3653 * Designed to free unused ata_queued_cmd object
3654 * in case something prevents using it.
3655 *
3656 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003657 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 */
3659void ata_qc_free(struct ata_queued_cmd *qc)
3660{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003661 struct ata_port *ap = qc->ap;
3662 unsigned int tag;
3663
Tejun Heoa4631472006-02-11 19:11:13 +09003664 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Tejun Heo4ba946e2006-01-23 13:09:36 +09003666 qc->flags = 0;
3667 tag = qc->tag;
3668 if (likely(ata_tag_valid(tag))) {
3669 if (tag == ap->active_tag)
3670 ap->active_tag = ATA_TAG_POISON;
3671 qc->tag = ATA_TAG_POISON;
3672 clear_bit(tag, &ap->qactive);
3673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674}
3675
Tejun Heo76014422006-02-11 15:13:49 +09003676void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
Tejun Heoa4631472006-02-11 19:11:13 +09003678 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3679 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
3681 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3682 ata_sg_clean(qc);
3683
Albert Lee3f3791d2005-08-16 14:25:38 +08003684 /* atapi: mark qc as inactive to prevent the interrupt handler
3685 * from completing the command twice later, before the error handler
3686 * is called. (when rc != 0 and atapi request sense is needed)
3687 */
3688 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003691 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692}
3693
3694static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3695{
3696 struct ata_port *ap = qc->ap;
3697
3698 switch (qc->tf.protocol) {
3699 case ATA_PROT_DMA:
3700 case ATA_PROT_ATAPI_DMA:
3701 return 1;
3702
3703 case ATA_PROT_ATAPI:
3704 case ATA_PROT_PIO:
3705 case ATA_PROT_PIO_MULT:
3706 if (ap->flags & ATA_FLAG_PIO_DMA)
3707 return 1;
3708
3709 /* fall through */
3710
3711 default:
3712 return 0;
3713 }
3714
3715 /* never reached */
3716}
3717
3718/**
3719 * ata_qc_issue - issue taskfile to device
3720 * @qc: command to issue to device
3721 *
3722 * Prepare an ATA command to submission to device.
3723 * This includes mapping the data into a DMA-able
3724 * area, filling in the S/G table, and finally
3725 * writing the taskfile to hardware, starting the command.
3726 *
3727 * LOCKING:
3728 * spin_lock_irqsave(host_set lock)
3729 *
3730 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003731 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 */
3733
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003734unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735{
3736 struct ata_port *ap = qc->ap;
3737
3738 if (ata_should_dma_map(qc)) {
3739 if (qc->flags & ATA_QCFLAG_SG) {
3740 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003741 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3743 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003744 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 }
3746 } else {
3747 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3748 }
3749
3750 ap->ops->qc_prep(qc);
3751
3752 qc->ap->active_tag = qc->tag;
3753 qc->flags |= ATA_QCFLAG_ACTIVE;
3754
3755 return ap->ops->qc_issue(qc);
3756
Tejun Heo8e436af2006-01-23 13:09:36 +09003757sg_err:
3758 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003759 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760}
3761
Edward Falk0baab862005-06-02 18:17:13 -04003762
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763/**
3764 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3765 * @qc: command to issue to device
3766 *
3767 * Using various libata functions and hooks, this function
3768 * starts an ATA command. ATA commands are grouped into
3769 * classes called "protocols", and issuing each type of protocol
3770 * is slightly different.
3771 *
Edward Falk0baab862005-06-02 18:17:13 -04003772 * May be used as the qc_issue() entry in ata_port_operations.
3773 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 * LOCKING:
3775 * spin_lock_irqsave(host_set lock)
3776 *
3777 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003778 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 */
3780
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003781unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
3783 struct ata_port *ap = qc->ap;
3784
3785 ata_dev_select(ap, qc->dev->devno, 1, 0);
3786
3787 switch (qc->tf.protocol) {
3788 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003789 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 break;
3791
3792 case ATA_PROT_DMA:
3793 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3794 ap->ops->bmdma_setup(qc); /* set up bmdma */
3795 ap->ops->bmdma_start(qc); /* initiate bmdma */
3796 break;
3797
3798 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3799 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003800 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003801 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003802 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 break;
3804
3805 case ATA_PROT_ATAPI:
3806 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003807 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003808 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 break;
3810
3811 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003812 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003813 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003814 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 break;
3816
3817 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003818 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3820 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003821 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 break;
3823
3824 default:
3825 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003826 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
3828
3829 return 0;
3830}
3831
3832/**
Edward Falk0baab862005-06-02 18:17:13 -04003833 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 * @qc: Info associated with this ATA transaction.
3835 *
3836 * LOCKING:
3837 * spin_lock_irqsave(host_set lock)
3838 */
3839
3840static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3841{
3842 struct ata_port *ap = qc->ap;
3843 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3844 u8 dmactl;
3845 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3846
3847 /* load PRD table addr. */
3848 mb(); /* make sure PRD table writes are visible to controller */
3849 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3850
3851 /* specify data direction, triple-check start bit is clear */
3852 dmactl = readb(mmio + ATA_DMA_CMD);
3853 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3854 if (!rw)
3855 dmactl |= ATA_DMA_WR;
3856 writeb(dmactl, mmio + ATA_DMA_CMD);
3857
3858 /* issue r/w command */
3859 ap->ops->exec_command(ap, &qc->tf);
3860}
3861
3862/**
Alan Coxb73fc892005-08-26 16:03:19 +01003863 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 * @qc: Info associated with this ATA transaction.
3865 *
3866 * LOCKING:
3867 * spin_lock_irqsave(host_set lock)
3868 */
3869
3870static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3871{
3872 struct ata_port *ap = qc->ap;
3873 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3874 u8 dmactl;
3875
3876 /* start host DMA transaction */
3877 dmactl = readb(mmio + ATA_DMA_CMD);
3878 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3879
3880 /* Strictly, one may wish to issue a readb() here, to
3881 * flush the mmio write. However, control also passes
3882 * to the hardware at this point, and it will interrupt
3883 * us when we are to resume control. So, in effect,
3884 * we don't care when the mmio write flushes.
3885 * Further, a read of the DMA status register _immediately_
3886 * following the write may not be what certain flaky hardware
3887 * is expected, so I think it is best to not add a readb()
3888 * without first all the MMIO ATA cards/mobos.
3889 * Or maybe I'm just being paranoid.
3890 */
3891}
3892
3893/**
3894 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3895 * @qc: Info associated with this ATA transaction.
3896 *
3897 * LOCKING:
3898 * spin_lock_irqsave(host_set lock)
3899 */
3900
3901static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3902{
3903 struct ata_port *ap = qc->ap;
3904 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3905 u8 dmactl;
3906
3907 /* load PRD table addr. */
3908 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3909
3910 /* specify data direction, triple-check start bit is clear */
3911 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3912 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3913 if (!rw)
3914 dmactl |= ATA_DMA_WR;
3915 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3916
3917 /* issue r/w command */
3918 ap->ops->exec_command(ap, &qc->tf);
3919}
3920
3921/**
3922 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3923 * @qc: Info associated with this ATA transaction.
3924 *
3925 * LOCKING:
3926 * spin_lock_irqsave(host_set lock)
3927 */
3928
3929static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3930{
3931 struct ata_port *ap = qc->ap;
3932 u8 dmactl;
3933
3934 /* start host DMA transaction */
3935 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3936 outb(dmactl | ATA_DMA_START,
3937 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3938}
3939
Edward Falk0baab862005-06-02 18:17:13 -04003940
3941/**
3942 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3943 * @qc: Info associated with this ATA transaction.
3944 *
3945 * Writes the ATA_DMA_START flag to the DMA command register.
3946 *
3947 * May be used as the bmdma_start() entry in ata_port_operations.
3948 *
3949 * LOCKING:
3950 * spin_lock_irqsave(host_set lock)
3951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952void ata_bmdma_start(struct ata_queued_cmd *qc)
3953{
3954 if (qc->ap->flags & ATA_FLAG_MMIO)
3955 ata_bmdma_start_mmio(qc);
3956 else
3957 ata_bmdma_start_pio(qc);
3958}
3959
Edward Falk0baab862005-06-02 18:17:13 -04003960
3961/**
3962 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3963 * @qc: Info associated with this ATA transaction.
3964 *
3965 * Writes address of PRD table to device's PRD Table Address
3966 * register, sets the DMA control register, and calls
3967 * ops->exec_command() to start the transfer.
3968 *
3969 * May be used as the bmdma_setup() entry in ata_port_operations.
3970 *
3971 * LOCKING:
3972 * spin_lock_irqsave(host_set lock)
3973 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974void ata_bmdma_setup(struct ata_queued_cmd *qc)
3975{
3976 if (qc->ap->flags & ATA_FLAG_MMIO)
3977 ata_bmdma_setup_mmio(qc);
3978 else
3979 ata_bmdma_setup_pio(qc);
3980}
3981
Edward Falk0baab862005-06-02 18:17:13 -04003982
3983/**
3984 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003985 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003986 *
3987 * Clear interrupt and error flags in DMA status register.
3988 *
3989 * May be used as the irq_clear() entry in ata_port_operations.
3990 *
3991 * LOCKING:
3992 * spin_lock_irqsave(host_set lock)
3993 */
3994
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995void ata_bmdma_irq_clear(struct ata_port *ap)
3996{
3997 if (ap->flags & ATA_FLAG_MMIO) {
3998 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3999 writeb(readb(mmio), mmio);
4000 } else {
4001 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4002 outb(inb(addr), addr);
4003 }
4004
4005}
4006
Edward Falk0baab862005-06-02 18:17:13 -04004007
4008/**
4009 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004010 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004011 *
4012 * Read and return BMDMA status register.
4013 *
4014 * May be used as the bmdma_status() entry in ata_port_operations.
4015 *
4016 * LOCKING:
4017 * spin_lock_irqsave(host_set lock)
4018 */
4019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020u8 ata_bmdma_status(struct ata_port *ap)
4021{
4022 u8 host_stat;
4023 if (ap->flags & ATA_FLAG_MMIO) {
4024 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4025 host_stat = readb(mmio + ATA_DMA_STATUS);
4026 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004027 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 return host_stat;
4029}
4030
Edward Falk0baab862005-06-02 18:17:13 -04004031
4032/**
4033 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004034 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004035 *
4036 * Clears the ATA_DMA_START flag in the dma control register
4037 *
4038 * May be used as the bmdma_stop() entry in ata_port_operations.
4039 *
4040 * LOCKING:
4041 * spin_lock_irqsave(host_set lock)
4042 */
4043
Alan Coxb73fc892005-08-26 16:03:19 +01004044void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
Alan Coxb73fc892005-08-26 16:03:19 +01004046 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 if (ap->flags & ATA_FLAG_MMIO) {
4048 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4049
4050 /* clear start/stop bit */
4051 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4052 mmio + ATA_DMA_CMD);
4053 } else {
4054 /* clear start/stop bit */
4055 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4056 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4057 }
4058
4059 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4060 ata_altstatus(ap); /* dummy read */
4061}
4062
4063/**
4064 * ata_host_intr - Handle host interrupt for given (port, task)
4065 * @ap: Port on which interrupt arrived (possibly...)
4066 * @qc: Taskfile currently active in engine
4067 *
4068 * Handle host interrupt for given queued command. Currently,
4069 * only DMA interrupts are handled. All other commands are
4070 * handled via polling with interrupts disabled (nIEN bit).
4071 *
4072 * LOCKING:
4073 * spin_lock_irqsave(host_set lock)
4074 *
4075 * RETURNS:
4076 * One if interrupt was handled, zero if not (shared irq).
4077 */
4078
4079inline unsigned int ata_host_intr (struct ata_port *ap,
4080 struct ata_queued_cmd *qc)
4081{
4082 u8 status, host_stat;
4083
4084 switch (qc->tf.protocol) {
4085
4086 case ATA_PROT_DMA:
4087 case ATA_PROT_ATAPI_DMA:
4088 case ATA_PROT_ATAPI:
4089 /* check status of DMA engine */
4090 host_stat = ap->ops->bmdma_status(ap);
4091 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4092
4093 /* if it's not our irq... */
4094 if (!(host_stat & ATA_DMA_INTR))
4095 goto idle_irq;
4096
4097 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004098 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
4100 /* fall through */
4101
4102 case ATA_PROT_ATAPI_NODATA:
4103 case ATA_PROT_NODATA:
4104 /* check altstatus */
4105 status = ata_altstatus(ap);
4106 if (status & ATA_BUSY)
4107 goto idle_irq;
4108
4109 /* check main status, clearing INTRQ */
4110 status = ata_chk_status(ap);
4111 if (unlikely(status & ATA_BUSY))
4112 goto idle_irq;
4113 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4114 ap->id, qc->tf.protocol, status);
4115
4116 /* ack bmdma irq events */
4117 ap->ops->irq_clear(ap);
4118
4119 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004120 qc->err_mask |= ac_err_mask(status);
4121 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 break;
4123
4124 default:
4125 goto idle_irq;
4126 }
4127
4128 return 1; /* irq handled */
4129
4130idle_irq:
4131 ap->stats.idle_irq++;
4132
4133#ifdef ATA_IRQ_TRAP
4134 if ((ap->stats.idle_irq % 1000) == 0) {
4135 handled = 1;
4136 ata_irq_ack(ap, 0); /* debug trap */
4137 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4138 }
4139#endif
4140 return 0; /* irq not handled */
4141}
4142
4143/**
4144 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004145 * @irq: irq line (unused)
4146 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 * @regs: unused
4148 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004149 * Default interrupt handler for PCI IDE devices. Calls
4150 * ata_host_intr() for each port that is not disabled.
4151 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004153 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 *
4155 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004156 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 */
4158
4159irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4160{
4161 struct ata_host_set *host_set = dev_instance;
4162 unsigned int i;
4163 unsigned int handled = 0;
4164 unsigned long flags;
4165
4166 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4167 spin_lock_irqsave(&host_set->lock, flags);
4168
4169 for (i = 0; i < host_set->n_ports; i++) {
4170 struct ata_port *ap;
4171
4172 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004173 if (ap &&
4174 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 struct ata_queued_cmd *qc;
4176
4177 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004178 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4179 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 handled |= ata_host_intr(ap, qc);
4181 }
4182 }
4183
4184 spin_unlock_irqrestore(&host_set->lock, flags);
4185
4186 return IRQ_RETVAL(handled);
4187}
4188
4189/**
4190 * atapi_packet_task - Write CDB bytes to hardware
4191 * @_data: Port to which ATAPI device is attached.
4192 *
4193 * When device has indicated its readiness to accept
4194 * a CDB, this function is called. Send the CDB.
4195 * If DMA is to be performed, exit immediately.
4196 * Otherwise, we are in polling mode, so poll
4197 * status under operation succeeds or fails.
4198 *
4199 * LOCKING:
4200 * Kernel thread context (may sleep)
4201 */
4202
4203static void atapi_packet_task(void *_data)
4204{
4205 struct ata_port *ap = _data;
4206 struct ata_queued_cmd *qc;
4207 u8 status;
4208
4209 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09004210 WARN_ON(qc == NULL);
4211 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
4213 /* sleep-wait for BSY to clear */
4214 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004215 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004216 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004217 goto err_out;
4218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
4220 /* make sure DRQ is set */
4221 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004222 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004223 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
4227 /* send SCSI cdb */
4228 DPRINTK("send cdb\n");
Tejun Heo6e7846e2006-02-12 23:32:58 +09004229 WARN_ON(qc->dev->cdb_len < 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Tejun Heoc1389502005-08-22 14:59:24 +09004231 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4232 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4233 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
Tejun Heoc1389502005-08-22 14:59:24 +09004235 /* Once we're done issuing command and kicking bmdma,
4236 * irq handler takes over. To not lose irq, we need
4237 * to clear NOINTR flag before sending cdb, but
4238 * interrupt handler shouldn't be invoked before we're
4239 * finished. Hence, the following locking.
4240 */
4241 spin_lock_irqsave(&ap->host_set->lock, flags);
4242 ap->flags &= ~ATA_FLAG_NOINTR;
Tejun Heo6e7846e2006-02-12 23:32:58 +09004243 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Tejun Heoc1389502005-08-22 14:59:24 +09004244 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4245 ap->ops->bmdma_start(qc); /* initiate bmdma */
4246 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4247 } else {
Tejun Heo6e7846e2006-02-12 23:32:58 +09004248 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Tejun Heoc1389502005-08-22 14:59:24 +09004250 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004251 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004252 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 }
4254
4255 return;
4256
4257err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004258 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259}
4260
Edward Falk0baab862005-06-02 18:17:13 -04004261
Jens Axboe9b847542006-01-06 09:28:07 +01004262/*
4263 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4264 * without filling any other registers
4265 */
4266static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4267 u8 cmd)
4268{
4269 struct ata_taskfile tf;
4270 int err;
4271
4272 ata_tf_init(ap, &tf, dev->devno);
4273
4274 tf.command = cmd;
4275 tf.flags |= ATA_TFLAG_DEVICE;
4276 tf.protocol = ATA_PROT_NODATA;
4277
4278 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4279 if (err)
4280 printk(KERN_ERR "%s: ata command failed: %d\n",
4281 __FUNCTION__, err);
4282
4283 return err;
4284}
4285
4286static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4287{
4288 u8 cmd;
4289
4290 if (!ata_try_flush_cache(dev))
4291 return 0;
4292
4293 if (ata_id_has_flush_ext(dev->id))
4294 cmd = ATA_CMD_FLUSH_EXT;
4295 else
4296 cmd = ATA_CMD_FLUSH;
4297
4298 return ata_do_simple_cmd(ap, dev, cmd);
4299}
4300
4301static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4302{
4303 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4304}
4305
4306static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4307{
4308 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4309}
4310
4311/**
4312 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004313 * @ap: port the device is connected to
4314 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004315 *
4316 * Kick the drive back into action, by sending it an idle immediate
4317 * command and making sure its transfer mode matches between drive
4318 * and host.
4319 *
4320 */
4321int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4322{
4323 if (ap->flags & ATA_FLAG_SUSPENDED) {
4324 ap->flags &= ~ATA_FLAG_SUSPENDED;
4325 ata_set_mode(ap);
4326 }
4327 if (!ata_dev_present(dev))
4328 return 0;
4329 if (dev->class == ATA_DEV_ATA)
4330 ata_start_drive(ap, dev);
4331
4332 return 0;
4333}
4334
4335/**
4336 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004337 * @ap: port the device is connected to
4338 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004339 *
4340 * Flush the cache on the drive, if appropriate, then issue a
4341 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004342 */
4343int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4344{
4345 if (!ata_dev_present(dev))
4346 return 0;
4347 if (dev->class == ATA_DEV_ATA)
4348 ata_flush_cache(ap, dev);
4349
4350 ata_standby_drive(ap, dev);
4351 ap->flags |= ATA_FLAG_SUSPENDED;
4352 return 0;
4353}
4354
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004355/**
4356 * ata_port_start - Set port up for dma.
4357 * @ap: Port to initialize
4358 *
4359 * Called just after data structures for each port are
4360 * initialized. Allocates space for PRD table.
4361 *
4362 * May be used as the port_start() entry in ata_port_operations.
4363 *
4364 * LOCKING:
4365 * Inherited from caller.
4366 */
4367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368int ata_port_start (struct ata_port *ap)
4369{
4370 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004371 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
4373 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4374 if (!ap->prd)
4375 return -ENOMEM;
4376
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004377 rc = ata_pad_alloc(ap, dev);
4378 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004379 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004380 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004381 }
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4384
4385 return 0;
4386}
4387
Edward Falk0baab862005-06-02 18:17:13 -04004388
4389/**
4390 * ata_port_stop - Undo ata_port_start()
4391 * @ap: Port to shut down
4392 *
4393 * Frees the PRD table.
4394 *
4395 * May be used as the port_stop() entry in ata_port_operations.
4396 *
4397 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004398 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004399 */
4400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401void ata_port_stop (struct ata_port *ap)
4402{
4403 struct device *dev = ap->host_set->dev;
4404
4405 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004406 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004409void ata_host_stop (struct ata_host_set *host_set)
4410{
4411 if (host_set->mmio_base)
4412 iounmap(host_set->mmio_base);
4413}
4414
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416/**
4417 * ata_host_remove - Unregister SCSI host structure with upper layers
4418 * @ap: Port to unregister
4419 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4420 *
4421 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004422 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 */
4424
4425static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4426{
4427 struct Scsi_Host *sh = ap->host;
4428
4429 DPRINTK("ENTER\n");
4430
4431 if (do_unregister)
4432 scsi_remove_host(sh);
4433
4434 ap->ops->port_stop(ap);
4435}
4436
4437/**
4438 * ata_host_init - Initialize an ata_port structure
4439 * @ap: Structure to initialize
4440 * @host: associated SCSI mid-layer structure
4441 * @host_set: Collection of hosts to which @ap belongs
4442 * @ent: Probe information provided by low-level driver
4443 * @port_no: Port number associated with this ata_port
4444 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004445 * Initialize a new ata_port structure, and its associated
4446 * scsi_host.
4447 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004449 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 */
4451
4452static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4453 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004454 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
4456 unsigned int i;
4457
4458 host->max_id = 16;
4459 host->max_lun = 1;
4460 host->max_channel = 1;
4461 host->unique_id = ata_unique_id++;
4462 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 ap->flags = ATA_FLAG_PORT_DISABLED;
4465 ap->id = host->unique_id;
4466 ap->host = host;
4467 ap->ctl = ATA_DEVCTL_OBS;
4468 ap->host_set = host_set;
4469 ap->port_no = port_no;
4470 ap->hard_port_no =
4471 ent->legacy_mode ? ent->hard_port_no : port_no;
4472 ap->pio_mask = ent->pio_mask;
4473 ap->mwdma_mask = ent->mwdma_mask;
4474 ap->udma_mask = ent->udma_mask;
4475 ap->flags |= ent->host_flags;
4476 ap->ops = ent->port_ops;
4477 ap->cbl = ATA_CBL_NONE;
4478 ap->active_tag = ATA_TAG_POISON;
4479 ap->last_ctl = 0xFF;
4480
4481 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4482 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004483 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
4485 for (i = 0; i < ATA_MAX_DEVICES; i++)
4486 ap->device[i].devno = i;
4487
4488#ifdef ATA_IRQ_TRAP
4489 ap->stats.unhandled_irq = 1;
4490 ap->stats.idle_irq = 1;
4491#endif
4492
4493 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4494}
4495
4496/**
4497 * ata_host_add - Attach low-level ATA driver to system
4498 * @ent: Information provided by low-level driver
4499 * @host_set: Collections of ports to which we add
4500 * @port_no: Port number associated with this host
4501 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004502 * Attach low-level ATA driver to system.
4503 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004505 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 *
4507 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004508 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 */
4510
Jeff Garzik057ace52005-10-22 14:27:05 -04004511static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 struct ata_host_set *host_set,
4513 unsigned int port_no)
4514{
4515 struct Scsi_Host *host;
4516 struct ata_port *ap;
4517 int rc;
4518
4519 DPRINTK("ENTER\n");
4520 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4521 if (!host)
4522 return NULL;
4523
4524 ap = (struct ata_port *) &host->hostdata[0];
4525
4526 ata_host_init(ap, host, host_set, ent, port_no);
4527
4528 rc = ap->ops->port_start(ap);
4529 if (rc)
4530 goto err_out;
4531
4532 return ap;
4533
4534err_out:
4535 scsi_host_put(host);
4536 return NULL;
4537}
4538
4539/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004540 * ata_device_add - Register hardware device with ATA and SCSI layers
4541 * @ent: Probe information describing hardware device to be registered
4542 *
4543 * This function processes the information provided in the probe
4544 * information struct @ent, allocates the necessary ATA and SCSI
4545 * host information structures, initializes them, and registers
4546 * everything with requisite kernel subsystems.
4547 *
4548 * This function requests irqs, probes the ATA bus, and probes
4549 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 *
4551 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004552 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 *
4554 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004555 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 */
4557
Jeff Garzik057ace52005-10-22 14:27:05 -04004558int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559{
4560 unsigned int count = 0, i;
4561 struct device *dev = ent->dev;
4562 struct ata_host_set *host_set;
4563
4564 DPRINTK("ENTER\n");
4565 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004566 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4568 if (!host_set)
4569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 spin_lock_init(&host_set->lock);
4571
4572 host_set->dev = dev;
4573 host_set->n_ports = ent->n_ports;
4574 host_set->irq = ent->irq;
4575 host_set->mmio_base = ent->mmio_base;
4576 host_set->private_data = ent->private_data;
4577 host_set->ops = ent->port_ops;
4578
4579 /* register each port bound to this device */
4580 for (i = 0; i < ent->n_ports; i++) {
4581 struct ata_port *ap;
4582 unsigned long xfer_mode_mask;
4583
4584 ap = ata_host_add(ent, host_set, i);
4585 if (!ap)
4586 goto err_out;
4587
4588 host_set->ports[i] = ap;
4589 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4590 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4591 (ap->pio_mask << ATA_SHIFT_PIO);
4592
4593 /* print per-port info to dmesg */
4594 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4595 "bmdma 0x%lX irq %lu\n",
4596 ap->id,
4597 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4598 ata_mode_string(xfer_mode_mask),
4599 ap->ioaddr.cmd_addr,
4600 ap->ioaddr.ctl_addr,
4601 ap->ioaddr.bmdma_addr,
4602 ent->irq);
4603
4604 ata_chk_status(ap);
4605 host_set->ops->irq_clear(ap);
4606 count++;
4607 }
4608
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004609 if (!count)
4610 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
4612 /* obtain irq, that is shared between channels */
4613 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4614 DRV_NAME, host_set))
4615 goto err_out;
4616
4617 /* perform each probe synchronously */
4618 DPRINTK("probe begin\n");
4619 for (i = 0; i < count; i++) {
4620 struct ata_port *ap;
4621 int rc;
4622
4623 ap = host_set->ports[i];
4624
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004625 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004627 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
4629 if (rc) {
4630 /* FIXME: do something useful here?
4631 * Current libata behavior will
4632 * tear down everything when
4633 * the module is removed
4634 * or the h/w is unplugged.
4635 */
4636 }
4637
4638 rc = scsi_add_host(ap->host, dev);
4639 if (rc) {
4640 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4641 ap->id);
4642 /* FIXME: do something useful here */
4643 /* FIXME: handle unconditional calls to
4644 * scsi_scan_host and ata_host_remove, below,
4645 * at the very least
4646 */
4647 }
4648 }
4649
4650 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004651 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 for (i = 0; i < count; i++) {
4653 struct ata_port *ap = host_set->ports[i];
4654
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004655 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 }
4657
4658 dev_set_drvdata(dev, host_set);
4659
4660 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4661 return ent->n_ports; /* success */
4662
4663err_out:
4664 for (i = 0; i < count; i++) {
4665 ata_host_remove(host_set->ports[i], 1);
4666 scsi_host_put(host_set->ports[i]->host);
4667 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004668err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 kfree(host_set);
4670 VPRINTK("EXIT, returning 0\n");
4671 return 0;
4672}
4673
4674/**
Alan Cox17b14452005-09-15 15:44:00 +01004675 * ata_host_set_remove - PCI layer callback for device removal
4676 * @host_set: ATA host set that was removed
4677 *
4678 * Unregister all objects associated with this host set. Free those
4679 * objects.
4680 *
4681 * LOCKING:
4682 * Inherited from calling layer (may sleep).
4683 */
4684
Alan Cox17b14452005-09-15 15:44:00 +01004685void ata_host_set_remove(struct ata_host_set *host_set)
4686{
4687 struct ata_port *ap;
4688 unsigned int i;
4689
4690 for (i = 0; i < host_set->n_ports; i++) {
4691 ap = host_set->ports[i];
4692 scsi_remove_host(ap->host);
4693 }
4694
4695 free_irq(host_set->irq, host_set);
4696
4697 for (i = 0; i < host_set->n_ports; i++) {
4698 ap = host_set->ports[i];
4699
4700 ata_scsi_release(ap->host);
4701
4702 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4703 struct ata_ioports *ioaddr = &ap->ioaddr;
4704
4705 if (ioaddr->cmd_addr == 0x1f0)
4706 release_region(0x1f0, 8);
4707 else if (ioaddr->cmd_addr == 0x170)
4708 release_region(0x170, 8);
4709 }
4710
4711 scsi_host_put(ap->host);
4712 }
4713
4714 if (host_set->ops->host_stop)
4715 host_set->ops->host_stop(host_set);
4716
4717 kfree(host_set);
4718}
4719
4720/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 * ata_scsi_release - SCSI layer callback hook for host unload
4722 * @host: libata host to be unloaded
4723 *
4724 * Performs all duties necessary to shut down a libata port...
4725 * Kill port kthread, disable port, and release resources.
4726 *
4727 * LOCKING:
4728 * Inherited from SCSI layer.
4729 *
4730 * RETURNS:
4731 * One.
4732 */
4733
4734int ata_scsi_release(struct Scsi_Host *host)
4735{
4736 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004737 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
4739 DPRINTK("ENTER\n");
4740
4741 ap->ops->port_disable(ap);
4742 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004743 for (i = 0; i < ATA_MAX_DEVICES; i++)
4744 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
4746 DPRINTK("EXIT\n");
4747 return 1;
4748}
4749
4750/**
4751 * ata_std_ports - initialize ioaddr with standard port offsets.
4752 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004753 *
4754 * Utility function which initializes data_addr, error_addr,
4755 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4756 * device_addr, status_addr, and command_addr to standard offsets
4757 * relative to cmd_addr.
4758 *
4759 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 */
Edward Falk0baab862005-06-02 18:17:13 -04004761
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762void ata_std_ports(struct ata_ioports *ioaddr)
4763{
4764 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4765 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4766 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4767 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4768 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4769 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4770 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4771 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4772 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4773 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4774}
4775
Edward Falk0baab862005-06-02 18:17:13 -04004776
Jeff Garzik374b1872005-08-30 05:42:52 -04004777#ifdef CONFIG_PCI
4778
4779void ata_pci_host_stop (struct ata_host_set *host_set)
4780{
4781 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4782
4783 pci_iounmap(pdev, host_set->mmio_base);
4784}
4785
Edward Falk0baab862005-06-02 18:17:13 -04004786/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 * ata_pci_remove_one - PCI layer callback for device removal
4788 * @pdev: PCI device that was removed
4789 *
4790 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004791 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 * Handle this by unregistering all objects associated
4793 * with this PCI device. Free those objects. Then finally
4794 * release PCI resources and disable device.
4795 *
4796 * LOCKING:
4797 * Inherited from PCI layer (may sleep).
4798 */
4799
4800void ata_pci_remove_one (struct pci_dev *pdev)
4801{
4802 struct device *dev = pci_dev_to_dev(pdev);
4803 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Alan Cox17b14452005-09-15 15:44:00 +01004805 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 pci_release_regions(pdev);
4807 pci_disable_device(pdev);
4808 dev_set_drvdata(dev, NULL);
4809}
4810
4811/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004812int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813{
4814 unsigned long tmp = 0;
4815
4816 switch (bits->width) {
4817 case 1: {
4818 u8 tmp8 = 0;
4819 pci_read_config_byte(pdev, bits->reg, &tmp8);
4820 tmp = tmp8;
4821 break;
4822 }
4823 case 2: {
4824 u16 tmp16 = 0;
4825 pci_read_config_word(pdev, bits->reg, &tmp16);
4826 tmp = tmp16;
4827 break;
4828 }
4829 case 4: {
4830 u32 tmp32 = 0;
4831 pci_read_config_dword(pdev, bits->reg, &tmp32);
4832 tmp = tmp32;
4833 break;
4834 }
4835
4836 default:
4837 return -EINVAL;
4838 }
4839
4840 tmp &= bits->mask;
4841
4842 return (tmp == bits->val) ? 1 : 0;
4843}
Jens Axboe9b847542006-01-06 09:28:07 +01004844
4845int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4846{
4847 pci_save_state(pdev);
4848 pci_disable_device(pdev);
4849 pci_set_power_state(pdev, PCI_D3hot);
4850 return 0;
4851}
4852
4853int ata_pci_device_resume(struct pci_dev *pdev)
4854{
4855 pci_set_power_state(pdev, PCI_D0);
4856 pci_restore_state(pdev);
4857 pci_enable_device(pdev);
4858 pci_set_master(pdev);
4859 return 0;
4860}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861#endif /* CONFIG_PCI */
4862
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864static int __init ata_init(void)
4865{
4866 ata_wq = create_workqueue("ata");
4867 if (!ata_wq)
4868 return -ENOMEM;
4869
4870 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4871 return 0;
4872}
4873
4874static void __exit ata_exit(void)
4875{
4876 destroy_workqueue(ata_wq);
4877}
4878
4879module_init(ata_init);
4880module_exit(ata_exit);
4881
Jeff Garzik67846b32005-10-05 02:58:32 -04004882static unsigned long ratelimit_time;
4883static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4884
4885int ata_ratelimit(void)
4886{
4887 int rc;
4888 unsigned long flags;
4889
4890 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4891
4892 if (time_after(jiffies, ratelimit_time)) {
4893 rc = 1;
4894 ratelimit_time = jiffies + (HZ/5);
4895 } else
4896 rc = 0;
4897
4898 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4899
4900 return rc;
4901}
4902
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903/*
4904 * libata is essentially a library of internal helper functions for
4905 * low-level ATA host controller drivers. As such, the API/ABI is
4906 * likely to change as new drivers are added and updated.
4907 * Do not depend on ABI/API stability.
4908 */
4909
4910EXPORT_SYMBOL_GPL(ata_std_bios_param);
4911EXPORT_SYMBOL_GPL(ata_std_ports);
4912EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004913EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914EXPORT_SYMBOL_GPL(ata_sg_init);
4915EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09004916EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4918EXPORT_SYMBOL_GPL(ata_eng_timeout);
4919EXPORT_SYMBOL_GPL(ata_tf_load);
4920EXPORT_SYMBOL_GPL(ata_tf_read);
4921EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4922EXPORT_SYMBOL_GPL(ata_std_dev_select);
4923EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4924EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4925EXPORT_SYMBOL_GPL(ata_check_status);
4926EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927EXPORT_SYMBOL_GPL(ata_exec_command);
4928EXPORT_SYMBOL_GPL(ata_port_start);
4929EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004930EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931EXPORT_SYMBOL_GPL(ata_interrupt);
4932EXPORT_SYMBOL_GPL(ata_qc_prep);
4933EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4934EXPORT_SYMBOL_GPL(ata_bmdma_start);
4935EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4936EXPORT_SYMBOL_GPL(ata_bmdma_status);
4937EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4938EXPORT_SYMBOL_GPL(ata_port_probe);
4939EXPORT_SYMBOL_GPL(sata_phy_reset);
4940EXPORT_SYMBOL_GPL(__sata_phy_reset);
4941EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004942EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004943EXPORT_SYMBOL_GPL(ata_std_softreset);
4944EXPORT_SYMBOL_GPL(sata_std_hardreset);
4945EXPORT_SYMBOL_GPL(ata_std_postreset);
4946EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004947EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004949EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004950EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4952EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09004953EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954EXPORT_SYMBOL_GPL(ata_scsi_error);
4955EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4956EXPORT_SYMBOL_GPL(ata_scsi_release);
4957EXPORT_SYMBOL_GPL(ata_host_intr);
4958EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09004959EXPORT_SYMBOL_GPL(ata_id_string);
4960EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004962EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4963EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
Alan Cox1bc4ccf2006-01-09 17:18:14 +00004965EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04004966EXPORT_SYMBOL_GPL(ata_timing_compute);
4967EXPORT_SYMBOL_GPL(ata_timing_merge);
4968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969#ifdef CONFIG_PCI
4970EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004971EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4973EXPORT_SYMBOL_GPL(ata_pci_init_one);
4974EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01004975EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4976EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01004978
4979EXPORT_SYMBOL_GPL(ata_device_suspend);
4980EXPORT_SYMBOL_GPL(ata_device_resume);
4981EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4982EXPORT_SYMBOL_GPL(ata_scsi_device_resume);